1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
#include "alfpriv.h"
#include <objbase.h>
static LONG _alf_initLock = 0;
static LONG _alf_initCounter = 0;
void
ALF_UpdateFonts(HWND win)
{
SendMessage(win, ALF_WM_UPDATEFONTS, 0, 0);
}
void
ALF_Initialize(void)
{
// acquire init lock
while (InterlockedExchange(&_alf_initLock, 1)) {
Sleep(0);
}
// we have the lock!
if (!_alf_initCounter++) {
InitCommonControls();
ALF_LoadCompatFunctions();
ALF_RegisterToplevelClass();
ALF_RegisterControlClass();
ALF_Compat_BufferedPaintInit();
}
// release init lock
InterlockedExchange(&_alf_initLock, 0);
}
void
ALF_UnInitialize(void)
{
// acquire init lock
while (InterlockedExchange(&_alf_initLock, 1)) {
Sleep(0);
}
// we have the lock!
if (!--_alf_initCounter) {
ALF_Compat_BufferedPaintUnInit();
ALF_UnregisterToplevelClass();
UnregisterClass(_alf_controlClass, ALF_HINSTANCE);
ALF_UnloadCompatFunctions();
}
// release init lock
InterlockedExchange(&_alf_initLock, 0);
}
void *
ALF_Alloc(SIZE_T nmemb, SIZE_T size)
{
if (size && nmemb > (SIZE_T)-1 / size)
RaiseException(STATUS_NO_MEMORY, 0, 0, NULL);
return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY|HEAP_GENERATE_EXCEPTIONS, nmemb * size);
}
void *
ALF_ReAlloc(void *ptr, SIZE_T nmemb, SIZE_T size)
{
if (size && nmemb > (SIZE_T)-1 / size)
RaiseException(STATUS_NO_MEMORY, 0, 0, NULL);
if (ptr)
return HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY|HEAP_GENERATE_EXCEPTIONS, ptr, nmemb * size);
else
return ALF_Alloc(nmemb, size);
}
void
ALF_Free(const void *p)
{
HeapFree(GetProcessHeap(), 0, (void*)p);
}
int
ALF_CentipointsToPixels(int cptValue, int dpi)
{
return MulDiv(cptValue, dpi, 7200);
}
int
ALF_GetDpi(HWND window)
{
return (int)SendMessage(window, ALF_WM_GETDPI, 0, 0);
}
struct ALF_ControlHwndById_Closure {
HWND result;
WORD needle;
};
static BOOL CALLBACK
ALF_ControlHwndById_EnumChildProc(HWND hwnd, LPARAM lParam)
{
struct ALF_ControlHwndById_Closure *closure = (struct ALF_ControlHwndById_Closure*)lParam;
if ((WORD)GetWindowLongPtr(hwnd, GWLP_ID) == closure->needle) {
closure->result = hwnd;
return FALSE;
}
return TRUE;
}
HWND
ALF_ControlHwndById(HWND win, WORD id)
{
struct ALF_ControlHwndById_Closure closure = { 0, id };
EnumChildWindows(win, ALF_ControlHwndById_EnumChildProc, (LPARAM)&closure);
return closure.result;
}
void
ALF_SetText(HWND hwnd, const TCHAR *text)
{
SendMessage(hwnd, WM_SETTEXT, 0, (LPARAM)text);
}
TCHAR * // free with ALF_Free
ALF_Text(HWND hwnd)
{
int len = GetWindowTextLength(hwnd);
if (len > 0) {
TCHAR *buf = ALF_New(TCHAR, (SIZE_T)len+1);
if (GetWindowText(hwnd, buf, len+1)) {
return buf;
} else {
ALF_Free(buf);
}
}
return ALF_New(TCHAR, 1);
}
BOOL
ALF_ShouldMessageBubble(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
(void)wparam;
(void)lparam;
if (!GetParent(hwnd))
return FALSE;
return msg == ALF_WM_GETDPI
|| msg == WM_COMMAND || msg == WM_NOTIFY
|| msg == WM_MEASUREITEM || msg == WM_DRAWITEM
|| msg == WM_CTLCOLORBTN || msg == WM_CTLCOLOREDIT
|| msg == WM_CTLCOLORLISTBOX || msg == WM_CTLCOLORSCROLLBAR
|| msg == WM_CTLCOLORSTATIC || msg == WM_NEXTDLGCTL;
}
void
ALF_FillRect(HDC dc, const RECT *rc, ALFColor color)
{
if (color == ALF_COLOR_TRANSPARENT)
return;
COLORREF gdicolor = ALF_ColorToGdi(color);
COLORREF oldBkColor = SetBkColor(dc, gdicolor);
ExtTextOut(dc, 0, 0, ETO_OPAQUE, rc, NULL, 0, NULL);
SetBkColor(dc, oldBkColor);
}
COLORREF
ALF_ColorToGdi(ALFColor color)
{
if (color == ALF_COLOR_TRANSPARENT) {
return (COLORREF)-1;
} else if (color & 0x80000000) {
return GetSysColor((color & 0x7f000000) >> 24);
} else {
return (COLORREF)color;
}
}
void
ALF_InvalidateBackground(HWND win)
{
SendMessage(win, ALF_WM_BACKGROUNDCHANGE, 0, 0);
}
void
ALF_SetBackgroundColor(HWND win, ALFColor color)
{
HWND parent = GetParent(win);
if (parent) {
ALF_Layout_RemoveControlFlag(parent, win, ALF_LAYOUT_INHERITBGCOLOR);
}
SendMessage(win, ALF_WM_SETBGCOLOR, 0, (LPARAM)color);
}
ALFColor
ALF_BackgroundColor(HWND win)
{
return (ALFColor)SendMessage(win, ALF_WM_GETBGCOLOR, 0, 0);
}
ALFColor
ALF_TextColor(HWND win)
{
return (ALFColor)SendMessage(win, ALF_WM_GETTEXTCOLOR, 0, 0);
}
void
ALF_SetTextColor(HWND win, ALFColor color)
{
SendMessage(win, ALF_WM_SETTEXTCOLOR, 0, (LPARAM)color);
}
void
ALF_SetFocus(HWND target)
{
if (GetWindowStyle(target) & WS_CHILD)
SendMessage(GetParent(target), WM_NEXTDLGCTL, (WPARAM)target, TRUE);
else
SendMessage(target, WM_NEXTDLGCTL, (WPARAM)target, TRUE);
}
int
ALF_MessageBox(HWND hwnd, const TCHAR *text, const TCHAR *caption, UINT type)
{
ALFApplication *app = NULL;
if (GetWindowThreadProcessId(hwnd, NULL) == GetCurrentThreadId())
app = ALF_Toplevel_Application(hwnd);
if (app)
ALF_Application_DisableWindowsForModal(app, hwnd);
int retval = MessageBox(hwnd, text, caption, type);
if (app)
ALF_Application_ReenableWindowsForModal(app, hwnd);
return retval;
}
|