-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgui.c
More file actions
553 lines (460 loc) · 17 KB
/
gui.c
File metadata and controls
553 lines (460 loc) · 17 KB
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/*
* OpenVPN-GUI -- A Windows GUI for OpenVPN.
*
* Copyright (C) 2011 Michael Berger <michael.berger@gmx.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program (see the file COPYING included with this
* distribution); if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#define WINVER 0x0500
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <prsht.h>
#include <tchar.h>
#include <stdio.h>
#include <stdarg.h>
#include "gui.h"
//
// WARNING:
// The following structures must NOT be DWORD padded because they are
// followed by strings, etc that do not have to be DWORD aligned.
//
#include <pshpack2.h>
typedef struct {
WORD dlgVer;
WORD signature;
DWORD helpID;
DWORD exStyle;
DWORD style;
WORD cDlgItems;
short x;
short y;
short cx;
short cy;
// Everything else in this structure is variable length,
// and therefore must be determined dynamically
// sz_Or_Ord menu; // name or ordinal of a menu resource
// sz_Or_Ord windowClass; // name or ordinal of a window class
// WCHAR title[titleLen]; // title string of the dialog box
// WORD pointsize; // only if DS_SETFONT is set
// WORD weight; // only if DS_SETFONT is set
// BYTE italic; // only if DS_SETFONT is set
// BYTE charset; // only if DS_SETFONT is set
// WCHAR typeface[stringLen]; // only if DS_SETFONT is set
} DLGTEMPLATEEX;
typedef DLGTEMPLATEEX *LPDLGTEMPLATEEX;
typedef CONST DLGTEMPLATEEX *LPCDLGTEMPLATEEX;
typedef struct {
DWORD helpID;
DWORD exStyle;
DWORD style;
short x;
short y;
short cx;
short cy;
DWORD id;
// Everything else in this structure is variable length,
// and therefore must be determined dynamically
// sz_Or_Ord windowClass; // name or ordinal of a window class
// sz_Or_Ord title; // initial text or resource identifier of the control
// WORD extraCount; // bytes of creation data that follow this member
} DLGITEMTEMPLATEEX;
typedef DLGITEMTEMPLATEEX *LPDLGITEMTEMPLATEEX;
typedef CONST DLGITEMTEMPLATEEX *LPCDLGITEMTEMPLATEEX;
#include <poppack.h> // Resume normal packing
BOOL IsWindowsVistaOrLater()
{
OSVERSIONINFO osvi;
ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&osvi);
if (osvi.dwMajorVersion >= 6)
return TRUE;
else
return FALSE;
}
BOOL IsDialogEx(LPCDLGTEMPLATE pdt)
{
return ((LPCDLGTEMPLATEEX) pdt)->signature == 0xFFFF;
}
INT_PTR ExtendedDialogBoxIndirectParam(const HINSTANCE hInstance, const LPCDLGTEMPLATE hDialogTemplate, const HWND hWndParent, const DLGPROC lpDialogFunc, const LPARAM dwInitParam)
{
INT_PTR nResult;
LPCDLGTEMPLATE hNewDialogTemplate;
// Windows Vista and above use Segoe UI 9 pt as default font while all the static dialog box templates
// are using MS Shell Dlg 8 pt font which is fine for Windows 2000 and Windows XP. Therefore dynamically
// build a new dialog box template for Windows Vista and later OS versions with Segoe UI 9 pt font.
if (IsWindowsVistaOrLater())
{
if ((hNewDialogTemplate = CreateModifiedDialogTemplate(hDialogTemplate)))
{
nResult = DialogBoxIndirectParam(hInstance, hNewDialogTemplate, hWndParent, lpDialogFunc, dwInitParam);
DeleteModifiedDialogTemplate(hNewDialogTemplate);
}
else
{
// Something went wrong so use the unmodified template as fallback
nResult = DialogBoxIndirectParam(hInstance, hDialogTemplate, hWndParent, lpDialogFunc, dwInitParam);
}
}
else
{
// No need to modify the dialog box template as we are running on Windows 2000 or Windows XP
nResult = DialogBoxIndirectParam(hInstance, hDialogTemplate, hWndParent, lpDialogFunc, dwInitParam);
}
return nResult;
}
HWND ExtendedCreateDialogIndirectParam(const HINSTANCE hInstance, const LPCDLGTEMPLATE hDialogTemplate, const HWND hWndParent, const DLGPROC lpDialogFunc, const LPARAM dwInitParam)
{
HWND hwndResult;
LPCDLGTEMPLATE hNewDialogTemplate;
// Windows Vista and above use Segoe UI 9 pt as default font while all the static dialog box templates
// are using MS Shell Dlg 8 pt font which is fine for Windows 2000 and Windows XP. Therefore dynamically
// build a new dialog box template for Windows Vista and later OS versions with Segoe UI 9 pt font.
if (IsWindowsVistaOrLater())
{
if ((hNewDialogTemplate = CreateModifiedDialogTemplate(hDialogTemplate)))
{
hwndResult = CreateDialogIndirectParam(hInstance, hNewDialogTemplate, hWndParent, lpDialogFunc, dwInitParam);
DeleteModifiedDialogTemplate(hNewDialogTemplate);
}
else
{
// Something went wrong so use the unmodified template as fallback
hwndResult = CreateDialogIndirectParam(hInstance, hDialogTemplate, hWndParent, lpDialogFunc, dwInitParam);
}
}
else
{
// No need to modify the dialog box template as we are running on Windows 2000 or Windows XP
hwndResult = CreateDialogIndirectParam(hInstance, hDialogTemplate, hWndParent, lpDialogFunc, dwInitParam);
}
return hwndResult;
}
void DeleteModifiedDialogTemplate(LPCDLGTEMPLATE hDialogTemplate)
{
HeapFree(GetProcessHeap(), 0, (LPVOID) hDialogTemplate);
return;
}
LPCDLGTEMPLATE CreateModifiedDialogTemplate(LPCDLGTEMPLATE pdtSrc)
{
DWORD dwSize;
LPDLGTEMPLATE pdtDest = NULL;
// Get the size of the supplied source dialog box template and allocate twice as much
// space for the new dialog box template. This should be enough space to perform all
// required modifications.
dwSize = GetSizeOfDialogTemplate(pdtSrc);
if ((pdtDest = (LPDLGTEMPLATE) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwSize * 2)))
{
LPBYTE pbSrc = (LPBYTE) pdtSrc, pbDest = (LPBYTE) pdtDest;
WORD cItems;
DWORD dwStyle;
BOOL bDialogEx = IsDialogEx(pdtSrc);
if (bDialogEx)
{
cItems = ((LPCDLGTEMPLATEEX) pdtSrc)->cDlgItems;
dwStyle = ((LPCDLGTEMPLATEEX) pdtSrc)->style;
// Copy the static part of the DLGTEMPLATEEX structure
memcpy(pdtDest, pdtSrc, sizeof(DLGTEMPLATEEX));
pbSrc = (LPBYTE) (((LPCDLGTEMPLATEEX) pdtSrc) + 1);
pbDest = (LPBYTE) (((LPCDLGTEMPLATEEX) pdtDest) + 1);
}
else
{
cItems = pdtSrc->cdit;
dwStyle = pdtSrc->style;
// Copy the static part of the DLGTEMPLATE structure
memcpy(pdtDest, pdtSrc, sizeof(DLGTEMPLATE));
pbSrc = (LPBYTE) (pdtSrc + 1);
pbDest = (LPBYTE) (pdtDest + 1);
}
CopyElement(&pbDest, &pbSrc); // menu resource
CopyElement(&pbDest, &pbSrc); // window class
CopyElement(&pbDest, &pbSrc); // title
// If the style specifies DS_SETFONT, there is a font information in the dialog template
if (dwStyle & DS_SETFONT)
{
// Replace the font point size by our own value
ReplaceFontPointSize(&pbDest, &pbSrc);
// If this is a DLGTEMPLATEEX then there is also the font weight,
// italic flag and charset which must be copied
if (bDialogEx)
{
// WORD weight;
// BYTE italic;
// BYTE charset;
memcpy(pbDest, pbSrc, sizeof(WORD) + (2 * sizeof(BYTE)));
pbSrc += sizeof(WORD) + (2 * sizeof(BYTE));
pbDest += sizeof(WORD) + (2 * sizeof(BYTE));
}
// Replace the font typeface name by our own value
ReplaceFontTypeface(&pbDest, &pbSrc);
}
pbSrc = (LPBYTE) (((DWORD_PTR) pbSrc + 3) & ~3); // DWORD align
pbDest = (LPBYTE) (((DWORD_PTR) pbDest + 3) & ~3); // DWORD align
// Loop through the control of the dialog box template
while (cItems--)
{
WORD cbCreationDataSize;
if (bDialogEx)
{
// Copy the static part of the DLGITEMTEMPLATEEX structure
memcpy(pbDest, pbSrc, sizeof(DLGITEMTEMPLATEEX));
pbSrc += sizeof(DLGITEMTEMPLATEEX);
pbDest += sizeof(DLGITEMTEMPLATEEX);
}
else
{
// Copy the static part of the DLGITEMTEMPLATE structure
memcpy(pbDest, pbSrc, sizeof(DLGITEMTEMPLATE));
pbSrc += sizeof(DLGITEMTEMPLATE);
pbDest += sizeof(DLGITEMTEMPLATE);
}
CopyElement(&pbDest, &pbSrc); // window class
CopyElement(&pbDest, &pbSrc); // title
cbCreationDataSize = *((LPWORD) pbSrc);
// Copy the WORD containing the size of the creation data
memcpy(pbDest, pbSrc, sizeof(WORD));
pbSrc += sizeof(WORD);
pbDest += sizeof(WORD);
if (cbCreationDataSize)
{
if (!bDialogEx)
{
// In the case of a DLGTEMPLATE structure the creation data size includes
// the WORD containing the size so the actual data size is 2 bytes less
cbCreationDataSize -= 2;
}
// Copy the creation data
memcpy(pbDest, pbSrc, cbCreationDataSize);
pbSrc += cbCreationDataSize;
pbDest += cbCreationDataSize;
}
pbSrc = (LPBYTE) (((DWORD_PTR) pbSrc + 3) & ~3); // DWORD align
pbDest = (LPBYTE) (((DWORD_PTR) pbDest + 3) & ~3); // DWORD align
}
}
return pdtDest;
}
DWORD GetSizeOfDialogTemplate(LPCDLGTEMPLATE pdt)
{
WORD cItems;
DWORD dwStyle;
LPBYTE pb = (LPBYTE) pdt;
BOOL bDialogEx = IsDialogEx(pdt);
if (bDialogEx)
{
cItems = ((LPCDLGTEMPLATEEX) pdt)->cDlgItems;
dwStyle = ((LPCDLGTEMPLATEEX) pdt)->style;
// Skip the static part of the DLGTEMPLATEEX structure
pb = (LPBYTE) (((LPCDLGTEMPLATEEX) pdt) + 1);
}
else
{
cItems = pdt->cdit;
dwStyle = pdt->style;
// Skip the static part of the DLGTEMPLATE structure
pb = (LPBYTE) (pdt + 1);
}
SkipElement(&pb); // menu resource
SkipElement(&pb); // window class
SkipElement(&pb); // title
// If the style specifies DS_SETFONT, there is a font information in the dialog template
if (dwStyle & DS_SETFONT)
{
// WORD pointsize
pb += sizeof(WORD);
// If this is a DLGTEMPLATEEX then there is also the font weight,
// italic flag and charset which must be skipped
if (bDialogEx)
{
// WORD weight;
// BYTE italic;
// BYTE charset;
pb += sizeof(WORD) + sizeof(BYTE) + sizeof(BYTE);
}
// Skip the typeface name
SkipElement(&pb);
}
pb = (LPBYTE) (((DWORD_PTR) pb + 3) & ~3); // DWORD align
// Loop through the control of the dialog box template
while (cItems--)
{
WORD cbCreationDataSize;
if (bDialogEx)
{
// Skip the static part of the DLGITEMTEMPLATEEX structure
pb += sizeof(DLGITEMTEMPLATEEX);
}
else
{
// Skip the static part of the DLGITEMTEMPLATE structure
pb += sizeof(DLGITEMTEMPLATE);
}
SkipElement(&pb); // window class
SkipElement(&pb); // title
cbCreationDataSize = *((LPWORD) pb);
// Skip the WORD containing the size of the creation data
pb += sizeof(WORD);
if (cbCreationDataSize)
{
if (!bDialogEx)
{
// In the case of a DLGTEMPLATE structure the creation data size includes
// the WORD containing the size so the actual data size is 2 bytes less
cbCreationDataSize -= 2;
}
// Skip the creation data
pb += cbCreationDataSize;
}
pb = (LPBYTE) (((DWORD_PTR) pb + 3) & ~3); // DWORD align
}
// Return the template size
return (DWORD) (pb - (LPBYTE) pdt);
}
void SkipElement(LPBYTE *ppb)
{
// The first WORD of the element determines the following structure:
// 0x0000 means no other data is following
// 0xFFFF means the following WORD contains data
// Everything else means the element is a NULL terminated Unicode string
if (*(LPWORD) (*ppb) == 0xFFFF)
{
// Skip the first WORD of the element and the WORD containing data
*ppb += sizeof(WORD) + sizeof(WORD);
return;
}
// Skip Unicode string
while (*(LPWORD) (*ppb) != 0)
{
*ppb += sizeof(WORD);
}
// Skip the first WORD of the element or NULL terminating the Unicode string
*ppb += sizeof(WORD);
return;
}
void CopyElement(LPBYTE *ppbDest, LPBYTE *ppbSrc)
{
// The first WORD of the element determines the following structure:
// 0x0000 means no other data is following
// 0xFFFF means the following WORD contains data
// Everything else means the element is a NULL terminated Unicode string
if (*(LPWORD) (*ppbSrc) == 0xFFFF)
{
// Copy the first WORD of the element and the WORD containing data
memcpy(*ppbDest, *ppbSrc, sizeof(WORD) + sizeof(WORD));
*ppbSrc += sizeof(WORD) + sizeof(WORD);
*ppbDest += sizeof(WORD) + sizeof(WORD);
return;
}
// Copy Unicode string
while (*(LPWORD) (*ppbSrc) != 0)
{
memcpy(*ppbDest, *ppbSrc, sizeof(WORD));
*ppbSrc += sizeof(WORD);
*ppbDest += sizeof(WORD);
}
// Copy the first WORD of the element or NULL terminating the Unicode string
memcpy(*ppbDest, *ppbSrc, sizeof(WORD));
*ppbSrc += sizeof(WORD);
*ppbDest += sizeof(WORD);
return;
}
void ReplaceFontPointSize(LPBYTE *ppbDest, LPBYTE *ppbSrc)
{
// Use 9 point font size
*(LPWORD) (*ppbDest) = 9;
*ppbSrc += sizeof(WORD);
*ppbDest += sizeof(WORD);
return;
}
void ReplaceFontTypeface(LPBYTE *ppbDest, LPBYTE *ppbSrc)
{
WCHAR typeface[] = L"Segoe UI";
// Use Segoe UI font
wcscpy((LPWSTR) *ppbDest, typeface);
*ppbSrc += (lstrlenW((LPWSTR) *ppbSrc) + 1) * sizeof(WCHAR);
*ppbDest += (lstrlenW(typeface) + 1) * sizeof(WCHAR);
return;
}
void RemoveSysMenu(HWND hwndDlg)
{
SendMessage(hwndDlg, WM_SETICON, (WPARAM) (ICON_BIG), (LPARAM) NULL);
SendMessage(hwndDlg, WM_SETICON, (WPARAM) (ICON_SMALL), (LPARAM) NULL);
return;
}
BOOL GetControlRect(const HWND hWnd, LPRECT lpRect)
{
HWND hWndParent = GetParent(hWnd);
POINT p = {0, 0};
// Get upper left position of the control within the window
MapWindowPoints(hWnd, hWndParent, &p, 1);
// Get size of the control
if (GetClientRect(hWnd, lpRect))
{
// Add upper left offset to the control size for absolute
// position within the window
(*lpRect).left += p.x;
(*lpRect).top += p.y;
(*lpRect).right += p.x;
(*lpRect).bottom += p.y;
return TRUE;
}
return FALSE;
}
void ClipOrCenterRectToMonitor(const LPRECT prc, const UINT flags)
{
HMONITOR hMonitor;
MONITORINFO mi;
RECT rc;
int w = prc->right - prc->left;
int h = prc->bottom - prc->top;
// Get the nearest monitor to the passed rect
hMonitor = MonitorFromRect(prc, MONITOR_DEFAULTTONEAREST);
// Get the work area or entire monitor rect
mi.cbSize = sizeof(mi);
GetMonitorInfo(hMonitor, &mi);
if (flags & MONITOR_WORKAREA)
{
rc = mi.rcWork;
}
else
{
rc = mi.rcMonitor;
}
// Center or clip the passed rect to the monitor rect
if (flags & MONITOR_CENTER)
{
prc->left = rc.left + (rc.right - rc.left - w) / 2;
prc->top = rc.top + (rc.bottom - rc.top - h) / 2;
prc->right = prc->left + w;
prc->bottom = prc->top + h;
}
else
{
prc->left = max(rc.left, min(rc.right - w, prc->left));
prc->top = max(rc.top, min(rc.bottom - h, prc->top));
prc->right = prc->left + w;
prc->bottom = prc->top + h;
}
}
void ClipOrCenterWindowToMonitor(const HWND hwnd, const UINT flags)
{
RECT rc;
GetWindowRect(hwnd, &rc);
ClipOrCenterRectToMonitor(&rc, flags);
SetWindowPos(hwnd, NULL, rc.left, rc.top, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
}