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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
|
#include "alfpriv.h"
#include <shlwapi.h>
static
DWORD ALF_DllGetVersion(const char *dll)
{
HMODULE hDll = GetModuleHandleA(dll);
if (hDll) {
DLLGETVERSIONPROC pDllGetVersion;
pDllGetVersion = (DLLGETVERSIONPROC)(void*)GetProcAddress(hDll, "DllGetVersion");
if (pDllGetVersion) {
DLLVERSIONINFO dvi;
HRESULT hr;
ZeroMemory(&dvi, sizeof(dvi));
dvi.cbSize = sizeof(dvi);
hr = (*pDllGetVersion)(&dvi);
if (SUCCEEDED(hr)) {
return (DWORD)MAKELONG(dvi.dwMinorVersion, dvi.dwMajorVersion);
}
}
}
return 0;
}
static int WINAPI
ALF_Compat_fallbackGetSystemMetricsForDpi(int nIndex, UINT dpi)
{
(void)dpi;
if (!ALF_Compat_IsMinWindowsVersion(4, 0)) {
// old NT does not support several properties, so we fake them
switch (nIndex) {
case SM_CXEDGE:
case SM_CYEDGE:
return 2;
}
}
return GetSystemMetrics(nIndex);
}
static BOOL WINAPI
ALF_Compat_fallbackIsAppThemed(void)
{
return FALSE;
}
static UINT WINAPI
ALF_Compat_fallbackGetDpiForWindow(HWND win)
{
(void)win;
UINT dpi = 0;
HDC hdcScreen = GetDC(NULL);
if (hdcScreen) {
dpi = (UINT)GetDeviceCaps(hdcScreen, LOGPIXELSY);
ReleaseDC(NULL, hdcScreen);
}
if (!dpi) {
dpi = 96; // FIXME! fallback to default DPI
}
return dpi;
}
static BOOL WINAPI
ALF_Compat_fallbackSystemParametersInfoForDpi(UINT uiAction, UINT uiParam, PVOID pvParam, UINT fWinIni, UINT dpi)
{
(void)dpi;
// NOTE: we only use SystemParametersInfoForDpi for SPI_GETNONCLIENTMETRICS,
// therefore we only provide a fallback for that.
// SystemParametersInfoForDpi only accepts the Vista+ version of NONCLIENTMETRICSW.
if (uiAction == SPI_GETNONCLIENTMETRICS && uiParam == sizeof(ALF_NONCLIENTMETRICSW_VISTA)) {
ALF_NONCLIENTMETRICSW_VISTA *ncmw = (ALF_NONCLIENTMETRICSW_VISTA*)pvParam;
// SystemParametersInfoForDpi is Unicode-only, but
// we need to provide a fallback even on ANSI systems
#ifdef UNICODE
SIZE_T s = sizeof(*ncmw);
if (!ALF_Compat_IsMinWindowsVersion(6, 0)) {
// pre-vista OS doesn't contain last member
s -= sizeof(ncmw->iPaddedBorderWidth);
ncmw->iPaddedBorderWidth = 0;
}
ncmw->cbSize = (UINT)s;
return SystemParametersInfo(SPI_GETNONCLIENTMETRICS, (UINT)s, ncmw, fWinIni);
#else
ALF_NONCLIENTMETRICSA_VISTA ncma;
ZeroMemory(&ncma, sizeof(ncma));
SIZE_T s = sizeof(ncma);
if (!ALF_Compat_IsMinWindowsVersion(6, 0)) {
// pre-vista OS doesn't contain last member
s -= sizeof(ncma.iPaddedBorderWidth);
ncma.iPaddedBorderWidth = 0;
}
ncma.cbSize = s;
if (SystemParametersInfo(SPI_GETNONCLIENTMETRICS, s, &ncma, fWinIni)) {
ncmw->iBorderWidth = ncma.iBorderWidth;
ncmw->iScrollWidth = ncma.iScrollWidth;
ncmw->iScrollHeight = ncma.iScrollHeight;
ncmw->iCaptionWidth = ncma.iCaptionWidth;
ncmw->iCaptionHeight = ncma.iCaptionHeight;
ALF_Compat_LogFontAtoW(&ncma.lfCaptionFont, &ncmw->lfCaptionFont);
ncmw->iSmCaptionWidth = ncma.iSmCaptionWidth;
ncmw->iSmCaptionHeight = ncma.iSmCaptionHeight;
ALF_Compat_LogFontAtoW(&ncma.lfSmCaptionFont, &ncmw->lfSmCaptionFont);
ncmw->iMenuWidth = ncma.iMenuWidth;
ncmw->iMenuHeight = ncma.iMenuHeight;
ALF_Compat_LogFontAtoW(&ncma.lfMenuFont, &ncmw->lfMenuFont);
ALF_Compat_LogFontAtoW(&ncma.lfStatusFont, &ncmw->lfStatusFont);
ALF_Compat_LogFontAtoW(&ncma.lfMessageFont, &ncmw->lfMessageFont);
ncmw->iPaddedBorderWidth = ncma.iPaddedBorderWidth;
return TRUE;
}
return FALSE;
#endif
}
return FALSE;
}
static BOOL WINAPI
ALF_Compat_fallbackAdjustWindowRectExForDpi(LPRECT lpRect, DWORD dwStyle, BOOL bMenu, DWORD dwExStyle, UINT dpi)
{
(void)dpi;
return AdjustWindowRectEx(lpRect, dwStyle, bMenu, dwExStyle);
}
long
ALF_GetAveCharWidth(HDC hdc)
{
// see: HOWTO: Calculate Dialog Units When Not Using the System Font
SIZE s;
GetTextExtentPoint32A(hdc, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", 52, &s);
return (s.cx / 26 + 1) / 2;
}
static HTHEME WINAPI
ALF_Compat_fallbackOpenThemeData(HWND window, LPCWSTR classNames)
{
(void)window;
(void)classNames;
return NULL;
}
static HRESULT WINAPI
ALF_Compat_fallbackCloseThemeData(HTHEME hTheme)
{
(void)hTheme;
return S_OK;
}
static BOOL WINAPI
ALF_Compat_fallbackIsThemeBackgroundPartiallyTransparent(HTHEME hTheme, int iPartId, int iStateId)
{
(void)hTheme; (void)iPartId; (void)iStateId;
return TRUE;
}
static HRESULT WINAPI
ALF_Compat_fallbackDrawThemeParentBackground(HWND hwnd, HDC hdc, RECT *prc)
{
HWND parent = GetParent(hwnd);
if (!parent)
return E_NOTIMPL;
RECT rc;
if (prc) {
rc = *prc;
} else {
GetClientRect(hwnd, &rc);
}
MapWindowRect(hwnd, parent, &rc);
RECT childr;
GetClientRect(hwnd, &childr);
MapWindowRect(hwnd, parent, &childr);
POINT o;
OffsetViewportOrgEx(hdc, -childr.left, -childr.top, &o);
HRGN oldClipRgn = CreateRectRgn(0, 0, 0, 0);
if (!GetClipRgn(hdc, oldClipRgn)) {
DeleteObject(oldClipRgn);
oldClipRgn = NULL;
}
IntersectClipRect(hdc, rc.left, rc.top, rc.right, rc.bottom);
SendMessage(parent, WM_ERASEBKGND, (WPARAM)hdc, 0);
SendMessage(parent, WM_PRINTCLIENT, (WPARAM)hdc, (LPARAM)PRF_CLIENT);
SelectClipRgn(hdc, oldClipRgn);
if (oldClipRgn) {
DeleteObject(oldClipRgn);
}
SetViewportOrgEx(hdc, o.x, o.y, NULL);
return S_OK;
}
static HRESULT WINAPI
ALF_Compat_fallbackDrawThemeBackground(HTHEME hTheme, HDC hdc, int iPartId, int iStateId, const RECT *pRect, const RECT *pClipRect)
{
(void)hTheme;
(void)hdc;
(void)iPartId;
(void)iStateId;
(void)pRect;
(void)pClipRect;
return E_NOTIMPL;
}
static HRESULT WINAPI
ALF_Compat_fallbackGetThemeBackgroundContentRect(HTHEME hTheme, HDC hdc, int iPartId, int iStateId, const RECT *pBoundingRect, RECT *pContentRect)
{
(void)hTheme;
(void)hdc;
(void)iPartId;
(void)iStateId;
(void)pBoundingRect;
(void)pContentRect;
return E_NOTIMPL;
}
static HRESULT WINAPI
ALF_Compat_fallbackGetThemeTextExtent(HTHEME hTheme, HDC hdc, int iPartId,
int iStateId, LPCWSTR pszText, int iCharCount,
DWORD dwTextFlags, const RECT *pBoundingRect,
RECT *pExtentRect)
{
(void)hTheme; (void)hdc; (void)iPartId; (void)iStateId; (void)pszText;
(void)iCharCount; (void)dwTextFlags; (void)pBoundingRect; (void)pExtentRect;
return E_NOTIMPL;
}
static HRESULT WINAPI
ALF_Compat_fallbackDrawThemeText(HTHEME hTheme, HDC hdc, int iPartId, int iStateId,
LPCWSTR pszText, int iCharCount, DWORD dwTextFlags,
DWORD dwTextFlags2, const RECT *pRect)
{
(void)hTheme; (void)hdc; (void)iPartId; (void)iStateId; (void)pszText;
(void)iCharCount; (void)dwTextFlags; (void)dwTextFlags2; (void)pRect;
return E_NOTIMPL;
}
static BOOL WINAPI
ALF_Compat_fallbackTrackMouseEvent(LPTRACKMOUSEEVENT tme)
{
(void)tme;
return FALSE;
}
static HRESULT WINAPI
ALF_Compat_fallbackBufferedPaintInit(void)
{
return E_NOTIMPL;
}
static HRESULT WINAPI
ALF_Compat_fallbackBufferedPaintUnInit(void)
{
return E_NOTIMPL;
}
static ALF_Compat_HANIMATIONBUFFER WINAPI
ALF_Compat_fallbackBeginBufferedAnimation(HWND hwnd, HDC hdcTarget,
const RECT *prcTarget, DWORD dwFormat,
ALF_Compat_BP_PAINTPARAMS *pPaintParams,
ALF_Compat_BP_ANIMATIONPARAMS *pAnimationParams,
HDC *phdcFrom, HDC *phdcTo)
{
(void)hwnd; (void)hdcTarget; (void)prcTarget; (void)dwFormat;
(void)pPaintParams; (void)pAnimationParams; (void)phdcFrom; (void)phdcTo;
return NULL;
}
static HRESULT WINAPI
ALF_Compat_fallbackEndBufferedAnimation(ALF_Compat_HANIMATIONBUFFER hbpAnimation, BOOL fUpdateTarget)
{
(void)hbpAnimation; (void)fUpdateTarget;
return E_NOTIMPL;
}
static BOOL WINAPI
ALF_Compat_fallbackBufferedPaintRenderAnimation(HWND hwnd, HDC hdc)
{
(void)hwnd; (void)hdc;
return FALSE;
}
static HRESULT WINAPI
ALF_Compat_fallbackGetThemeTransitionDuration(HTHEME hTheme,
int iPartId,
int iStateIdFrom,
int iStateIdTo,
int iPropId,
DWORD *pdwDuration)
{
(void)hTheme; (void)iPartId; (void)iStateIdFrom; (void)iStateIdTo; (void)iPropId; (void)pdwDuration;
return E_NOTIMPL;
}
static HRESULT WINAPI
ALF_Compat_fallbackGetThemePartSize(HTHEME hTheme,
HDC hdc,
int iPartId,
int iStateId,
RECT *prc,
THEMESIZE eSize,
SIZE *psz)
{
(void)hTheme; (void)hdc; (void)iPartId; (void)iStateId; (void)prc; (void)eSize; (void)psz;
return E_NOTIMPL;
}
static HRESULT WINAPI
ALF_Compat_fallbackGetThemeColor(HTHEME hTheme,
int iPartId,
int iStateId,
int iPropId,
COLORREF *pColor)
{
(void)hTheme; (void)iPartId; (void)iStateId; (void)iPropId; (void)pColor;
return E_NOTIMPL;
}
#define LOAD_FUNC(dll, name) do { \
if (_alf_dll_##dll) \
*((FARPROC*)&ALF_Compat_##name) = GetProcAddress(_alf_dll_##dll, #name); \
else \
ALF_Compat_##name = NULL; \
\
if (!ALF_Compat_##name) \
ALF_Compat_##name = ALF_Compat_fallback##name; \
} while (0)
#define UNLOAD_FUNC(name) do { ALF_Compat_##name = NULL; } while (0)
static HMODULE _alf_dll_uxtheme = NULL;
static HMODULE _alf_dll_user32 = NULL;
static HMODULE _alf_dll_comctl32 = NULL;
void ALF_LoadCompatFunctions(void)
{
if (ALF_Compat_IsWinNT()) // don't attempt to load uxtheme.dll on non-NT platforms (ugly error on Win32s)
_alf_dll_uxtheme = LoadLibraryA("uxtheme.dll");
_alf_dll_user32 = LoadLibraryA("user32.dll");
_alf_dll_comctl32 = LoadLibraryA("comctl32.dll");
if (ALF_DllGetVersion("comctl32.dll") >= 0x60000)
LOAD_FUNC(uxtheme, IsAppThemed);
else
ALF_Compat_IsAppThemed = ALF_Compat_fallbackIsAppThemed;
LOAD_FUNC(user32, GetDpiForWindow);
LOAD_FUNC(user32, AdjustWindowRectExForDpi);
LOAD_FUNC(user32, GetSystemMetricsForDpi);
LOAD_FUNC(user32, SystemParametersInfoForDpi);
*((FARPROC*)&ALF_Compat_TrackMouseEvent) = GetProcAddress(_alf_dll_comctl32, "_TrackMouseEvent");
if (!ALF_Compat_TrackMouseEvent)
ALF_Compat_TrackMouseEvent = ALF_Compat_fallbackTrackMouseEvent;
LOAD_FUNC(uxtheme, OpenThemeData);
LOAD_FUNC(uxtheme, CloseThemeData);
LOAD_FUNC(uxtheme, IsThemeBackgroundPartiallyTransparent);
LOAD_FUNC(uxtheme, DrawThemeParentBackground);
LOAD_FUNC(uxtheme, DrawThemeBackground);
LOAD_FUNC(uxtheme, GetThemeBackgroundContentRect);
LOAD_FUNC(uxtheme, GetThemeTextExtent);
LOAD_FUNC(uxtheme, DrawThemeText);
LOAD_FUNC(uxtheme, BufferedPaintInit);
LOAD_FUNC(uxtheme, BufferedPaintUnInit);
LOAD_FUNC(uxtheme, BeginBufferedAnimation);
LOAD_FUNC(uxtheme, EndBufferedAnimation);
LOAD_FUNC(uxtheme, BufferedPaintRenderAnimation);
LOAD_FUNC(uxtheme, GetThemeTransitionDuration);
LOAD_FUNC(uxtheme, GetThemePartSize);
LOAD_FUNC(uxtheme, GetThemeColor);
}
void ALF_UnloadCompatFunctions(void)
{
UNLOAD_FUNC(IsAppThemed);
UNLOAD_FUNC(GetDpiForWindow);
UNLOAD_FUNC(AdjustWindowRectExForDpi);
UNLOAD_FUNC(GetSystemMetricsForDpi);
UNLOAD_FUNC(SystemParametersInfoForDpi);
UNLOAD_FUNC(TrackMouseEvent);
UNLOAD_FUNC(OpenThemeData);
UNLOAD_FUNC(CloseThemeData);
UNLOAD_FUNC(IsThemeBackgroundPartiallyTransparent);
UNLOAD_FUNC(DrawThemeParentBackground);
UNLOAD_FUNC(DrawThemeBackground);
UNLOAD_FUNC(GetThemeBackgroundContentRect);
UNLOAD_FUNC(GetThemeTextExtent);
UNLOAD_FUNC(DrawThemeText);
UNLOAD_FUNC(BufferedPaintInit);
UNLOAD_FUNC(BufferedPaintUnInit);
UNLOAD_FUNC(BeginBufferedAnimation);
UNLOAD_FUNC(EndBufferedAnimation);
UNLOAD_FUNC(BufferedPaintRenderAnimation);
UNLOAD_FUNC(GetThemeTransitionDuration);
UNLOAD_FUNC(GetThemePartSize);
UNLOAD_FUNC(GetThemeColor);
FreeLibrary(_alf_dll_uxtheme);
FreeLibrary(_alf_dll_user32);
FreeLibrary(_alf_dll_comctl32);
}
#undef LOAD_FUNC
#undef UNLOAD_FUNC
BOOL (WINAPI *ALF_Compat_IsAppThemed)(void) = NULL;
UINT (WINAPI *ALF_Compat_GetDpiForWindow)(HWND /*window*/) = NULL;
BOOL (WINAPI *ALF_Compat_AdjustWindowRectExForDpi)(LPRECT,DWORD,BOOL,DWORD,UINT) = NULL;
int (WINAPI *ALF_Compat_GetSystemMetricsForDpi)(int, UINT) = NULL;
BOOL (WINAPI *ALF_Compat_SystemParametersInfoForDpi)(UINT,UINT,PVOID,UINT,UINT) = NULL;
HTHEME (WINAPI *ALF_Compat_OpenThemeData)(HWND, LPCWSTR) = NULL;
HRESULT (WINAPI *ALF_Compat_CloseThemeData)(HTHEME) = NULL;
BOOL (WINAPI *ALF_Compat_IsThemeBackgroundPartiallyTransparent)(HTHEME,int,int) = NULL;
HRESULT (WINAPI *ALF_Compat_DrawThemeParentBackground)(HWND,HDC,RECT *) = NULL;
HRESULT (WINAPI *ALF_Compat_DrawThemeBackground)(HTHEME, HDC, int, int, const RECT *, const RECT *) = NULL;
HRESULT (WINAPI *ALF_Compat_GetThemeBackgroundContentRect)(HTHEME,HDC,int,int,const RECT *,RECT *) = NULL;
HRESULT (WINAPI *ALF_Compat_GetThemeTextExtent)(HTHEME,HDC,int,int,LPCWSTR,int,DWORD,const RECT *, RECT *) = NULL;
HRESULT (WINAPI *ALF_Compat_DrawThemeText)(HTHEME,HDC,int,int,LPCWSTR,int,DWORD,DWORD,const RECT *) = NULL;
BOOL (WINAPI *ALF_Compat_TrackMouseEvent)(LPTRACKMOUSEEVENT tme) = NULL;
HRESULT (WINAPI *ALF_Compat_GetThemePartSize)(HTHEME,HDC,int,int,RECT *,THEMESIZE,SIZE *) = NULL;
HRESULT (WINAPI *ALF_Compat_BufferedPaintInit)(void) = NULL;
HRESULT (WINAPI *ALF_Compat_BufferedPaintUnInit)(void) = NULL;
ALF_Compat_HANIMATIONBUFFER (WINAPI *ALF_Compat_BeginBufferedAnimation)(HWND,HDC,const RECT *,DWORD,ALF_Compat_BP_PAINTPARAMS *,ALF_Compat_BP_ANIMATIONPARAMS *,HDC *,HDC *) = NULL;
HRESULT (WINAPI *ALF_Compat_EndBufferedAnimation)(ALF_Compat_HANIMATIONBUFFER,BOOL) = NULL;
BOOL (WINAPI *ALF_Compat_BufferedPaintRenderAnimation)(HWND,HDC) = NULL;
HRESULT (WINAPI *ALF_Compat_GetThemeTransitionDuration)(HTHEME,int,int,int,int,DWORD*) = NULL;
HRESULT (WINAPI *ALF_Compat_GetThemeColor)(HTHEME,int,int,int,COLORREF*);
|