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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
|
#include "alfpriv.h"
#include <objbase.h>
static LONG _alf_initLock = 0;
static LONG _alf_initCounter = 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();
}
// 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_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);
}
TCHAR *
ALF_StrDup(const TCHAR *psz)
{
int l = lstrlen(psz);
TCHAR *r = ALF_New(TCHAR, (SIZE_T)l + 1);
CopyMemory(r, psz, ((SIZE_T)l + 1) * sizeof(TCHAR));
return r;
}
TCHAR *
ALF_StrOrIntresourceDup(const TCHAR *psz)
{
if (IS_INTRESOURCE(psz))
return (TCHAR *)psz;
else
return ALF_StrDup(psz);
}
void
ALF_StrOrIntresourceFree(const TCHAR *psz)
{
if (!IS_INTRESOURCE(psz))
ALF_Free(psz);
}
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;
}
BOOL
ALF_ReflectMessage(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam, LRESULT *pRet)
{
switch (msg) {
case WM_DRAWITEM: {
if (wparam) {
DRAWITEMSTRUCT *dis = (DRAWITEMSTRUCT *)lparam;
*pRet = SendMessage(dis->hwndItem, 0x2000 + WM_DRAWITEM, wparam, lparam);
return TRUE;
} else {
// sent by a menu
return FALSE;
}
}
case WM_MEASUREITEM: {
if (wparam) {
// no HWND in MEASUREITEMSTRUCT, need to find the control by its id
HWND hwndControl = ALF_ControlHwndById(hwnd, (WORD)wparam);
if (hwndControl) {
*pRet = SendMessage(hwndControl, 0x2000 + WM_MEASUREITEM, wparam, lparam);
return TRUE;
} else {
return FALSE;
}
} else {
// sent by a menu
return FALSE;
}
return TRUE;
}
case WM_DELETEITEM: {
DELETEITEMSTRUCT *dis = (DELETEITEMSTRUCT *)lparam;
*pRet = SendMessage(dis->hwndItem, 0x2000 + WM_DELETEITEM, wparam, lparam);
return TRUE;
}
case WM_VKEYTOITEM:
case WM_CHARTOITEM:
*pRet = SendMessage((HWND)lparam, 0x2000 + msg, wparam, lparam);
return TRUE;
case WM_COMPAREITEM: {
COMPAREITEMSTRUCT *cis = (COMPAREITEMSTRUCT *)lparam;
*pRet = SendMessage(cis->hwndItem, 0x2000 + WM_COMPAREITEM, wparam, lparam);
return TRUE;
}
case WM_NOTIFY: {
NMHDR *nmhdr = (NMHDR *)lparam;
if (nmhdr->hwndFrom) {
*pRet = SendMessage(nmhdr->hwndFrom, 0x2000 + WM_NOTIFY, wparam, lparam);
return !!*pRet;
} else {
return FALSE;
}
}
case WM_COMMAND:
if (lparam) {
*pRet = SendMessage((HWND)lparam, 0x2000 + WM_COMMAND, wparam, lparam);
return !!*pRet;
} else {
return FALSE;
}
case WM_HSCROLL:
case WM_VSCROLL:
SendMessage((HWND)lparam, 0x2000 + msg, wparam, lparam);
return FALSE; // always continue processing in parent
case WM_CTLCOLOR:
case WM_CTLCOLORMSGBOX:
case WM_CTLCOLOREDIT:
case WM_CTLCOLORLISTBOX:
case WM_CTLCOLORBTN:
case WM_CTLCOLORSTATIC:
case WM_CTLCOLORSCROLLBAR:
*pRet = SendMessage((HWND)lparam, 0x2000 + msg, wparam, lparam);
return !!*pRet; // let owner handle color stuff if the control didn’t return a brush
default:
return FALSE;
}
}
|