Skip to content

Latest commit

 

History

History
918 lines (797 loc) · 28.7 KB

SDL_windowsmessagebox.c

File metadata and controls

918 lines (797 loc) · 28.7 KB
 
1
2
/*
Simple DirectMedia Layer
Jan 17, 2020
Jan 17, 2020
3
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"
#if SDL_VIDEO_DRIVER_WINDOWS
Mar 24, 2018
Mar 24, 2018
25
26
27
28
29
30
31
32
#ifdef HAVE_LIMITS_H
#include <limits.h>
#else
#ifndef SIZE_MAX
#define SIZE_MAX ((size_t)-1)
#endif
#endif
33
34
35
36
#include "../../core/windows/SDL_windows.h"
#include "SDL_assert.h"
#include "SDL_windowsvideo.h"
Mar 2, 2018
Mar 2, 2018
37
#include "SDL_windowstaskdialog.h"
38
39
40
41
42
#ifndef SS_EDITCONTROL
#define SS_EDITCONTROL 0x2000
#endif
Mar 24, 2018
Mar 24, 2018
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
#ifndef IDOK
#define IDOK 1
#endif
#ifndef IDCANCEL
#define IDCANCEL 2
#endif
/* Custom dialog return codes */
#define IDCLOSED 20
#define IDINVALPTRINIT 50
#define IDINVALPTRCOMMAND 51
#define IDINVALPTRSETFOCUS 52
#define IDINVALPTRDLGITEM 53
/* First button ID */
#define IDBUTTONINDEX0 100
#define DLGITEMTYPEBUTTON 0x0080
#define DLGITEMTYPESTATIC 0x0082
/* Windows only sends the lower 16 bits of the control ID when a button
* gets clicked. There are also some predefined and custom IDs that lower
* the available number further. 2^16 - 101 buttons should be enough for
* everyone, no need to make the code more complex.
*/
#define MAX_BUTTONS (0xffff - 100)
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
/* Display a Windows message box */
#pragma pack(push, 1)
typedef struct
{
WORD dlgVer;
WORD signature;
DWORD helpID;
DWORD exStyle;
DWORD style;
WORD cDlgItems;
short x;
short y;
short cx;
short cy;
} DLGTEMPLATEEX;
typedef struct
{
DWORD helpID;
DWORD exStyle;
DWORD style;
short x;
short y;
short cx;
short cy;
DWORD id;
} DLGITEMTEMPLATEEX;
#pragma pack(pop)
typedef struct
{
DLGTEMPLATEEX* lpDialog;
Uint8 *data;
size_t size;
size_t used;
Mar 24, 2018
Mar 24, 2018
109
WORD numbuttons;
110
111
} WIN_DialogData;
Mar 24, 2018
Mar 24, 2018
112
113
114
115
116
117
118
119
120
static SDL_bool GetButtonIndex(const SDL_MessageBoxData *messageboxdata, Uint32 flags, size_t *i)
{
for (*i = 0; *i < (size_t)messageboxdata->numbuttons; ++*i) {
if (messageboxdata->buttons[*i].flags & flags) {
return SDL_TRUE;
}
}
return SDL_FALSE;
}
121
122
123
static INT_PTR MessageBoxDialogProc(HWND hDlg, UINT iMessage, WPARAM wParam, LPARAM lParam)
{
Mar 24, 2018
Mar 24, 2018
124
125
126
const SDL_MessageBoxData *messageboxdata;
size_t buttonindex;
127
switch ( iMessage ) {
Mar 24, 2018
Mar 24, 2018
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
case WM_INITDIALOG:
if (lParam == 0) {
EndDialog(hDlg, IDINVALPTRINIT);
return TRUE;
}
messageboxdata = (const SDL_MessageBoxData *)lParam;
SetWindowLongPtr(hDlg, GWLP_USERDATA, lParam);
if (GetButtonIndex(messageboxdata, SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, &buttonindex)) {
/* Focus on the first default return-key button */
HWND buttonctl = GetDlgItem(hDlg, (int)(IDBUTTONINDEX0 + buttonindex));
if (buttonctl == NULL) {
EndDialog(hDlg, IDINVALPTRDLGITEM);
}
PostMessage(hDlg, WM_NEXTDLGCTL, (WPARAM)buttonctl, TRUE);
} else {
/* Give the focus to the dialog window instead */
SetFocus(hDlg);
}
return FALSE;
case WM_SETFOCUS:
messageboxdata = (const SDL_MessageBoxData *)GetWindowLongPtr(hDlg, GWLP_USERDATA);
if (messageboxdata == NULL) {
EndDialog(hDlg, IDINVALPTRSETFOCUS);
return TRUE;
}
/* Let the default button be focused if there is one. Otherwise, prevent any initial focus. */
if (GetButtonIndex(messageboxdata, SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, &buttonindex)) {
return FALSE;
}
return TRUE;
Mar 24, 2018
Mar 24, 2018
161
162
163
164
165
166
messageboxdata = (const SDL_MessageBoxData *)GetWindowLongPtr(hDlg, GWLP_USERDATA);
if (messageboxdata == NULL) {
EndDialog(hDlg, IDINVALPTRCOMMAND);
return TRUE;
}
167
/* Return the ID of the button that was pushed */
Mar 24, 2018
Mar 24, 2018
168
169
170
171
172
173
174
175
176
177
178
179
180
181
if (wParam == IDOK) {
if (GetButtonIndex(messageboxdata, SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, &buttonindex)) {
EndDialog(hDlg, IDBUTTONINDEX0 + buttonindex);
}
} else if (wParam == IDCANCEL) {
if (GetButtonIndex(messageboxdata, SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, &buttonindex)) {
EndDialog(hDlg, IDBUTTONINDEX0 + buttonindex);
} else {
/* Closing of window was requested by user or system. It would be rude not to comply. */
EndDialog(hDlg, IDCLOSED);
}
} else if (wParam >= IDBUTTONINDEX0 && (int)wParam - IDBUTTONINDEX0 < messageboxdata->numbuttons) {
EndDialog(hDlg, wParam);
}
182
183
184
185
186
187
188
189
190
191
return TRUE;
default:
break;
}
return FALSE;
}
static SDL_bool ExpandDialogSpace(WIN_DialogData *dialog, size_t space)
{
Mar 24, 2018
Mar 24, 2018
192
193
/* Growing memory in 64 KiB steps. */
const size_t sizestep = 0x10000;
194
195
196
size_t size = dialog->size;
if (size == 0) {
Mar 24, 2018
Mar 24, 2018
197
198
199
200
201
202
/* Start with 4 KiB or a multiple of 64 KiB to fit the data. */
size = 0x1000;
if (SIZE_MAX - sizestep < space) {
size = space;
} else if (space > size) {
size = (space + sizestep) & ~(sizestep - 1);
Mar 24, 2018
Mar 24, 2018
204
205
206
207
208
209
210
211
212
213
} else if (SIZE_MAX - dialog->used < space) {
SDL_OutOfMemory();
return SDL_FALSE;
} else if (SIZE_MAX - (dialog->used + space) < sizestep) {
/* Close to the maximum. */
size = dialog->used + space;
} else if (size < dialog->used + space) {
/* Round up to the next 64 KiB block. */
size = dialog->used + space;
size += sizestep - size % sizestep;
Mar 24, 2018
Mar 24, 2018
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
if (size > dialog->size) {
void *data = SDL_realloc(dialog->data, size);
if (!data) {
SDL_OutOfMemory();
return SDL_FALSE;
}
dialog->data = data;
dialog->size = size;
dialog->lpDialog = (DLGTEMPLATEEX*)dialog->data;
}
return SDL_TRUE;
}
static SDL_bool AlignDialogData(WIN_DialogData *dialog, size_t size)
{
size_t padding = (dialog->used % size);
if (!ExpandDialogSpace(dialog, padding)) {
return SDL_FALSE;
}
dialog->used += padding;
return SDL_TRUE;
}
static SDL_bool AddDialogData(WIN_DialogData *dialog, const void *data, size_t size)
{
if (!ExpandDialogSpace(dialog, size)) {
return SDL_FALSE;
}
SDL_memcpy(dialog->data+dialog->used, data, size);
dialog->used += size;
return SDL_TRUE;
}
static SDL_bool AddDialogString(WIN_DialogData *dialog, const char *string)
{
WCHAR *wstring;
WCHAR *p;
size_t count;
SDL_bool status;
if (!string) {
string = "";
}
wstring = WIN_UTF8ToString(string);
if (!wstring) {
return SDL_FALSE;
}
/* Find out how many characters we have, including null terminator */
count = 0;
for (p = wstring; *p; ++p) {
++count;
}
++count;
status = AddDialogData(dialog, wstring, count*sizeof(WCHAR));
SDL_free(wstring);
return status;
}
static int s_BaseUnitsX;
static int s_BaseUnitsY;
static void Vec2ToDLU(short *x, short *y)
{
SDL_assert(s_BaseUnitsX != 0); /* we init in WIN_ShowMessageBox(), which is the only public function... */
*x = MulDiv(*x, 4, s_BaseUnitsX);
*y = MulDiv(*y, 8, s_BaseUnitsY);
}
Mar 24, 2018
Mar 24, 2018
293
static SDL_bool AddDialogControl(WIN_DialogData *dialog, WORD type, DWORD style, DWORD exStyle, int x, int y, int w, int h, int id, const char *caption, WORD ordinal)
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
{
DLGITEMTEMPLATEEX item;
WORD marker = 0xFFFF;
WORD extraData = 0;
SDL_zero(item);
item.style = style;
item.exStyle = exStyle;
item.x = x;
item.y = y;
item.cx = w;
item.cy = h;
item.id = id;
Vec2ToDLU(&item.x, &item.y);
Vec2ToDLU(&item.cx, &item.cy);
if (!AlignDialogData(dialog, sizeof(DWORD))) {
return SDL_FALSE;
}
if (!AddDialogData(dialog, &item, sizeof(item))) {
return SDL_FALSE;
}
if (!AddDialogData(dialog, &marker, sizeof(marker))) {
return SDL_FALSE;
}
if (!AddDialogData(dialog, &type, sizeof(type))) {
return SDL_FALSE;
}
Mar 24, 2018
Mar 24, 2018
323
324
325
326
327
328
329
330
331
332
333
if (type == DLGITEMTYPEBUTTON || (type == DLGITEMTYPESTATIC && caption != NULL)) {
if (!AddDialogString(dialog, caption)) {
return SDL_FALSE;
}
} else {
if (!AddDialogData(dialog, &marker, sizeof(marker))) {
return SDL_FALSE;
}
if (!AddDialogData(dialog, &ordinal, sizeof(ordinal))) {
return SDL_FALSE;
}
334
335
336
337
}
if (!AddDialogData(dialog, &extraData, sizeof(extraData))) {
return SDL_FALSE;
}
Mar 24, 2018
Mar 24, 2018
338
339
340
if (type == DLGITEMTYPEBUTTON) {
dialog->numbuttons++;
}
341
342
343
344
345
++dialog->lpDialog->cDlgItems;
return SDL_TRUE;
}
Mar 24, 2018
Mar 24, 2018
346
347
348
349
350
351
352
static SDL_bool AddDialogStaticText(WIN_DialogData *dialog, int x, int y, int w, int h, const char *text)
{
DWORD style = WS_VISIBLE | WS_CHILD | SS_LEFT | SS_NOPREFIX | SS_EDITCONTROL | WS_GROUP;
return AddDialogControl(dialog, DLGITEMTYPESTATIC, style, 0, x, y, w, h, -1, text, 0);
}
static SDL_bool AddDialogStaticIcon(WIN_DialogData *dialog, int x, int y, int w, int h, Uint16 ordinal)
Mar 24, 2018
Mar 24, 2018
354
355
DWORD style = WS_VISIBLE | WS_CHILD | SS_ICON | WS_GROUP;
return AddDialogControl(dialog, DLGITEMTYPESTATIC, style, 0, x, y, w, h, -2, NULL, ordinal);
356
357
358
359
}
static SDL_bool AddDialogButton(WIN_DialogData *dialog, int x, int y, int w, int h, const char *text, int id, SDL_bool isDefault)
{
Mar 24, 2018
Mar 24, 2018
360
DWORD style = WS_VISIBLE | WS_CHILD | WS_TABSTOP;
361
362
363
364
365
if (isDefault) {
style |= BS_DEFPUSHBUTTON;
} else {
style |= BS_PUSHBUTTON;
}
Mar 24, 2018
Mar 24, 2018
366
367
368
369
/* The first button marks the start of the group. */
if (dialog->numbuttons == 0) {
style |= WS_GROUP;
}
Aug 3, 2019
Aug 3, 2019
370
return AddDialogControl(dialog, DLGITEMTYPEBUTTON, style, 0, x, y, w, h, id, text, 0);
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
}
static void FreeDialogData(WIN_DialogData *dialog)
{
SDL_free(dialog->data);
SDL_free(dialog);
}
static WIN_DialogData *CreateDialogData(int w, int h, const char *caption)
{
WIN_DialogData *dialog;
DLGTEMPLATEEX dialogTemplate;
WORD WordToPass;
SDL_zero(dialogTemplate);
dialogTemplate.dlgVer = 1;
dialogTemplate.signature = 0xffff;
dialogTemplate.style = (WS_CAPTION | DS_CENTER | DS_SHELLFONT);
dialogTemplate.x = 0;
dialogTemplate.y = 0;
dialogTemplate.cx = w;
dialogTemplate.cy = h;
Vec2ToDLU(&dialogTemplate.cx, &dialogTemplate.cy);
dialog = (WIN_DialogData *)SDL_calloc(1, sizeof(*dialog));
if (!dialog) {
return NULL;
}
if (!AddDialogData(dialog, &dialogTemplate, sizeof(dialogTemplate))) {
FreeDialogData(dialog);
return NULL;
}
/* No menu */
WordToPass = 0;
if (!AddDialogData(dialog, &WordToPass, 2)) {
FreeDialogData(dialog);
return NULL;
}
/* No custom class */
if (!AddDialogData(dialog, &WordToPass, 2)) {
FreeDialogData(dialog);
return NULL;
}
/* title */
if (!AddDialogString(dialog, caption)) {
FreeDialogData(dialog);
return NULL;
}
/* Font stuff */
{
/*
* We want to use the system messagebox font.
*/
BYTE ToPass;
NONCLIENTMETRICSA NCM;
NCM.cbSize = sizeof(NCM);
SystemParametersInfoA(SPI_GETNONCLIENTMETRICS, 0, &NCM, 0);
/* Font size - convert to logical font size for dialog parameter. */
{
Sep 14, 2015
Sep 14, 2015
437
438
439
440
441
442
HDC ScreenDC = GetDC(NULL);
int LogicalPixelsY = GetDeviceCaps(ScreenDC, LOGPIXELSY);
if (!LogicalPixelsY) /* This can happen if the application runs out of GDI handles */
LogicalPixelsY = 72;
WordToPass = (WORD)(-72 * NCM.lfMessageFont.lfHeight / LogicalPixelsY);
ReleaseDC(NULL, ScreenDC);
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
}
if (!AddDialogData(dialog, &WordToPass, 2)) {
FreeDialogData(dialog);
return NULL;
}
/* Font weight */
WordToPass = (WORD)NCM.lfMessageFont.lfWeight;
if (!AddDialogData(dialog, &WordToPass, 2)) {
FreeDialogData(dialog);
return NULL;
}
/* italic? */
ToPass = NCM.lfMessageFont.lfItalic;
if (!AddDialogData(dialog, &ToPass, 1)) {
FreeDialogData(dialog);
return NULL;
}
/* charset? */
ToPass = NCM.lfMessageFont.lfCharSet;
if (!AddDialogData(dialog, &ToPass, 1)) {
FreeDialogData(dialog);
return NULL;
}
/* font typeface. */
if (!AddDialogString(dialog, NCM.lfMessageFont.lfFaceName)) {
FreeDialogData(dialog);
return NULL;
}
}
return dialog;
}
Mar 24, 2018
Mar 24, 2018
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
/* Escaping ampersands is necessary to disable mnemonics in dialog controls.
* The caller provides a char** for dst and a size_t* for dstlen where the
* address of the work buffer and its size will be stored. Their values must be
* NULL and 0 on the first call. src is the string to be escaped. On error, the
* function returns NULL and, on success, returns a pointer to the escaped
* sequence as a read-only string that is valid until the next call or until the
* work buffer is freed. Once all strings have been processed, it's the caller's
* responsibilty to free the work buffer with SDL_free, even on errors.
*/
static const char *EscapeAmpersands(char **dst, size_t *dstlen, const char *src)
{
char *newdst;
size_t ampcount = 0;
size_t srclen = 0;
if (src == NULL) {
return NULL;
}
while (src[srclen]) {
if (src[srclen] == '&') {
ampcount++;
}
srclen++;
}
srclen++;
if (ampcount == 0) {
/* Nothing to do. */
return src;
}
if (SIZE_MAX - srclen < ampcount) {
return NULL;
}
if (*dst == NULL || *dstlen < srclen + ampcount) {
/* Allocating extra space in case the next strings are a bit longer. */
size_t extraspace = SIZE_MAX - (srclen + ampcount);
if (extraspace > 512) {
extraspace = 512;
}
*dstlen = srclen + ampcount + extraspace;
SDL_free(*dst);
*dst = NULL;
newdst = SDL_malloc(*dstlen);
if (newdst == NULL) {
return NULL;
}
*dst = newdst;
} else {
newdst = *dst;
}
/* The escape character is the ampersand itself. */
while (srclen--) {
if (*src == '&') {
*newdst++ = '&';
}
*newdst++ = *src++;
}
return *dst;
}
Mar 2, 2018
Mar 2, 2018
544
545
546
/* This function is called if a Task Dialog is unsupported. */
static int
WIN_ShowOldMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
547
548
{
WIN_DialogData *dialog;
Mar 24, 2018
Mar 24, 2018
549
int i, x, y, retval;
550
551
552
553
554
HFONT DialogFont;
SIZE Size;
RECT TextSize;
wchar_t* wmessage;
TEXTMETRIC TM;
Mar 24, 2018
Mar 24, 2018
555
556
557
558
559
560
HDC FontDC;
INT_PTR result;
char *ampescape = NULL;
size_t ampescapesize = 0;
Uint16 defbuttoncount = 0;
Uint16 icon = 0;
May 27, 2017
May 27, 2017
562
HWND ParentWindow = NULL;
563
564
565
566
567
const int ButtonWidth = 88;
const int ButtonHeight = 26;
const int TextMargin = 16;
const int ButtonMargin = 12;
Mar 24, 2018
Mar 24, 2018
568
569
570
571
572
573
574
const int IconWidth = GetSystemMetrics(SM_CXICON);
const int IconHeight = GetSystemMetrics(SM_CYICON);
const int IconMargin = 20;
if (messageboxdata->numbuttons > MAX_BUTTONS) {
return SDL_SetError("Number of butons exceeds limit of %d", MAX_BUTTONS);
}
Mar 24, 2018
Mar 24, 2018
576
577
switch (messageboxdata->flags) {
case SDL_MESSAGEBOX_ERROR:
Jun 29, 2018
Jun 29, 2018
578
icon = (Uint16)(size_t)IDI_ERROR;
Mar 24, 2018
Mar 24, 2018
579
580
break;
case SDL_MESSAGEBOX_WARNING:
Jun 29, 2018
Jun 29, 2018
581
icon = (Uint16)(size_t)IDI_WARNING;
Mar 24, 2018
Mar 24, 2018
582
583
break;
case SDL_MESSAGEBOX_INFORMATION:
Jun 29, 2018
Jun 29, 2018
584
icon = (Uint16)(size_t)IDI_INFORMATION;
Mar 24, 2018
Mar 24, 2018
585
586
break;
}
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
/* Jan 25th, 2013 - dant@fleetsa.com
*
*
* I've tried to make this more reasonable, but I've run in to a lot
* of nonsense.
*
* The original issue is the code was written in pixels and not
* dialog units (DLUs). All DialogBox functions use DLUs, which
* vary based on the selected font (yay).
*
* According to MSDN, the most reliable way to convert is via
* MapDialogUnits, which requires an HWND, which we don't have
* at time of template creation.
*
* We do however have:
* The system font (DLU width 8 for me)
* The font we select for the dialog (DLU width 6 for me)
*
* Based on experimentation, *neither* of these return the value
* actually used. Stepping in to MapDialogUnits(), the conversion
* is fairly clear, and uses 7 for me.
*
* As a result, some of this is hacky to ensure the sizing is
* somewhat correct.
*
* Honestly, a long term solution is to use CreateWindow, not CreateDialog.
*
*
* In order to get text dimensions we need to have a DC with the desired font.
* I'm assuming a dialog box in SDL is rare enough we can to the create.
*/
Mar 24, 2018
Mar 24, 2018
620
FontDC = CreateCompatibleDC(0);
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
{
/* Create a duplicate of the font used in system message boxes. */
LOGFONT lf;
NONCLIENTMETRICS NCM;
NCM.cbSize = sizeof(NCM);
SystemParametersInfo(SPI_GETNONCLIENTMETRICS, 0, &NCM, 0);
lf = NCM.lfMessageFont;
DialogFont = CreateFontIndirect(&lf);
}
/* Select the font in to our DC */
SelectObject(FontDC, DialogFont);
{
/* Get the metrics to try and figure our DLU conversion. */
GetTextMetrics(FontDC, &TM);
Aug 15, 2017
Aug 15, 2017
638
639
640
641
642
643
644
645
646
647
648
/* Calculation from the following documentation:
* https://support.microsoft.com/en-gb/help/125681/how-to-calculate-dialog-base-units-with-non-system-based-font
* This fixes bug 2137, dialog box calculation with a fixed-width system font
*/
{
SIZE extent;
GetTextExtentPoint32A(FontDC, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", 52, &extent);
s_BaseUnitsX = (extent.cx / 26 + 1) / 2;
}
/*s_BaseUnitsX = TM.tmAveCharWidth + 1;*/
649
650
651
652
653
654
s_BaseUnitsY = TM.tmHeight;
}
/* Measure the *pixel* size of the string. */
wmessage = WIN_UTF8ToString(messageboxdata->message);
SDL_zero(TextSize);
Mar 24, 2018
Mar 24, 2018
655
DrawText(FontDC, wmessage, -1, &TextSize, DT_CALCRECT | DT_LEFT | DT_NOPREFIX | DT_EDITCONTROL);
Mar 24, 2018
Mar 24, 2018
657
658
659
660
661
/* Add margins and some padding for hangs, etc. */
TextSize.left += TextMargin;
TextSize.right += TextMargin + 2;
TextSize.top += TextMargin;
TextSize.bottom += TextMargin + 2;
662
663
664
665
666
667
668
669
670
671
672
/* Done with the DC, and the string */
DeleteDC(FontDC);
SDL_free(wmessage);
/* Increase the size of the dialog by some border spacing around the text. */
Size.cx = TextSize.right - TextSize.left;
Size.cy = TextSize.bottom - TextSize.top;
Size.cx += TextMargin * 2;
Size.cy += TextMargin * 2;
Mar 24, 2018
Mar 24, 2018
673
674
675
676
677
678
679
/* Make dialog wider and shift text over for the icon. */
if (icon) {
Size.cx += IconMargin + IconWidth;
TextSize.left += IconMargin + IconWidth;
TextSize.right += IconMargin + IconWidth;
}
680
681
682
683
/* Ensure the size is wide enough for all of the buttons. */
if (Size.cx < messageboxdata->numbuttons * (ButtonWidth + ButtonMargin) + ButtonMargin)
Size.cx = messageboxdata->numbuttons * (ButtonWidth + ButtonMargin) + ButtonMargin;
Mar 24, 2018
Mar 24, 2018
684
685
686
687
688
/* Reset the height to the icon size if it is actually bigger than the text. */
if (icon && Size.cy < IconMargin * 2 + IconHeight) {
Size.cy = IconMargin * 2 + IconHeight;
}
689
690
691
692
693
694
695
696
/* Add vertical space for the buttons and border. */
Size.cy += ButtonHeight + TextMargin;
dialog = CreateDialogData(Size.cx, Size.cy, messageboxdata->title);
if (!dialog) {
return -1;
}
Mar 24, 2018
Mar 24, 2018
697
698
699
700
701
702
if (icon && ! AddDialogStaticIcon(dialog, IconMargin, IconMargin, IconWidth, IconHeight, icon)) {
FreeDialogData(dialog);
return -1;
}
if (!AddDialogStaticText(dialog, TextSize.left, TextSize.top, TextSize.right - TextSize.left, TextSize.bottom - TextSize.top, messageboxdata->message)) {
703
704
705
706
707
FreeDialogData(dialog);
return -1;
}
/* Align the buttons to the right/bottom. */
Oct 7, 2016
Oct 7, 2016
708
x = Size.cx - (ButtonWidth + ButtonMargin) * messageboxdata->numbuttons;
709
y = Size.cy - ButtonHeight - ButtonMargin;
Aug 3, 2019
Aug 3, 2019
710
for (i = 0; i < messageboxdata->numbuttons; i++) {
Mar 24, 2018
Mar 24, 2018
711
712
SDL_bool isdefault = SDL_FALSE;
const char *buttontext;
Aug 3, 2019
Aug 3, 2019
713
714
715
716
717
718
719
720
721
722
const SDL_MessageBoxButtonData *sdlButton;
/* We always have to create the dialog buttons from left to right
* so that the tab order is correct. Select the info to use
* depending on which order was requested. */
if (messageboxdata->flags & SDL_MESSAGEBOX_BUTTONS_LEFT_TO_RIGHT) {
sdlButton = &messageboxdata->buttons[i];
} else {
sdlButton = &messageboxdata->buttons[messageboxdata->numbuttons - 1 - i];
}
Aug 3, 2019
Aug 3, 2019
724
if (sdlButton->flags & SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT) {
Mar 24, 2018
Mar 24, 2018
725
726
727
728
defbuttoncount++;
if (defbuttoncount == 1) {
isdefault = SDL_TRUE;
}
Mar 24, 2018
Mar 24, 2018
730
Aug 3, 2019
Aug 3, 2019
731
732
733
734
buttontext = EscapeAmpersands(&ampescape, &ampescapesize, sdlButton->text);
/* Make sure to provide the correct ID to keep buttons indexed in the
* same order as how they are in messageboxdata. */
if (buttontext == NULL || !AddDialogButton(dialog, x, y, ButtonWidth, ButtonHeight, buttontext, IDBUTTONINDEX0 + (int)(sdlButton - messageboxdata->buttons), isdefault)) {
735
FreeDialogData(dialog);
Mar 24, 2018
Mar 24, 2018
736
SDL_free(ampescape);
Aug 3, 2019
Aug 3, 2019
739
Oct 7, 2016
Oct 7, 2016
740
x += ButtonWidth + ButtonMargin;
Mar 24, 2018
Mar 24, 2018
742
SDL_free(ampescape);
May 27, 2017
May 27, 2017
744
745
/* If we have a parent window, get the Instance and HWND for them
* so that our little dialog gets exclusive focus at all times. */
May 27, 2017
May 27, 2017
746
if (messageboxdata->window) {
May 27, 2017
May 27, 2017
747
ParentWindow = ((SDL_WindowData*)messageboxdata->window->driverdata)->hwnd;
May 27, 2017
May 27, 2017
748
}
May 27, 2017
May 27, 2017
749
Mar 24, 2018
Mar 24, 2018
750
751
result = DialogBoxIndirectParam(NULL, (DLGTEMPLATE*)dialog->lpDialog, ParentWindow, (DLGPROC)MessageBoxDialogProc, (LPARAM)messageboxdata);
if (result >= IDBUTTONINDEX0 && result - IDBUTTONINDEX0 < messageboxdata->numbuttons) {
Aug 3, 2019
Aug 3, 2019
752
*buttonid = messageboxdata->buttons[result - IDBUTTONINDEX0].buttonid;
Mar 24, 2018
Mar 24, 2018
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
retval = 0;
} else if (result == IDCLOSED) {
/* Dialog window closed by user or system. */
/* This could use a special return code. */
retval = 0;
*buttonid = -1;
} else {
if (result == 0) {
SDL_SetError("Invalid parent window handle");
} else if (result == -1) {
SDL_SetError("The message box encountered an error.");
} else if (result == IDINVALPTRINIT || result == IDINVALPTRSETFOCUS || result == IDINVALPTRCOMMAND) {
SDL_SetError("Invalid message box pointer in dialog procedure");
} else if (result == IDINVALPTRDLGITEM) {
SDL_SetError("Couldn't find dialog control of the default enter-key button");
} else {
SDL_SetError("An unknown error occured");
}
retval = -1;
}
773
774
FreeDialogData(dialog);
Mar 24, 2018
Mar 24, 2018
775
return retval;
Mar 2, 2018
Mar 2, 2018
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
/* TaskDialogIndirect procedure
* This is because SDL targets Windows XP (0x501), so this is not defined in the platform SDK.
*/
typedef HRESULT(FAR WINAPI *TASKDIALOGINDIRECTPROC)(const TASKDIALOGCONFIG *pTaskConfig, int *pnButton, int *pnRadioButton, BOOL *pfVerificationFlagChecked);
int
WIN_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
{
HWND ParentWindow = NULL;
wchar_t *wmessage;
wchar_t *wtitle;
TASKDIALOGCONFIG TaskConfig;
TASKDIALOG_BUTTON *pButtons;
TASKDIALOG_BUTTON *pButton;
HMODULE hComctl32;
TASKDIALOGINDIRECTPROC pfnTaskDialogIndirect;
HRESULT hr;
Mar 24, 2018
Mar 24, 2018
795
796
char *ampescape = NULL;
size_t ampescapesize = 0;
Mar 2, 2018
Mar 2, 2018
797
798
799
800
int nButton;
int nCancelButton;
int i;
Mar 24, 2018
Mar 24, 2018
801
802
803
804
if (SIZE_MAX / sizeof(TASKDIALOG_BUTTON) < messageboxdata->numbuttons) {
return SDL_OutOfMemory();
}
Mar 2, 2018
Mar 2, 2018
805
806
807
/* If we cannot load comctl32.dll use the old messagebox! */
hComctl32 = LoadLibrary(TEXT("Comctl32.dll"));
if (hComctl32 == NULL) {
Mar 24, 2018
Mar 24, 2018
808
return WIN_ShowOldMessageBox(messageboxdata, buttonid);
Mar 2, 2018
Mar 2, 2018
809
}
Mar 24, 2018
Mar 24, 2018
810
Mar 2, 2018
Mar 2, 2018
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
/* If TaskDialogIndirect doesn't exist use the old messagebox!
This will fail prior to Windows Vista.
The manifest file in the application may require targeting version 6 of comctl32.dll, even
when we use LoadLibrary here!
If you don't want to bother with manifests, put this #pragma in your app's source code somewhere:
pragma comment(linker,"\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
*/
pfnTaskDialogIndirect = (TASKDIALOGINDIRECTPROC) GetProcAddress(hComctl32, "TaskDialogIndirect");
if (pfnTaskDialogIndirect == NULL) {
FreeLibrary(hComctl32);
return WIN_ShowOldMessageBox(messageboxdata, buttonid);
}
/* If we have a parent window, get the Instance and HWND for them
so that our little dialog gets exclusive focus at all times. */
if (messageboxdata->window) {
ParentWindow = ((SDL_WindowData *) messageboxdata->window->driverdata)->hwnd;
}
wmessage = WIN_UTF8ToString(messageboxdata->message);
wtitle = WIN_UTF8ToString(messageboxdata->title);
SDL_zero(TaskConfig);
TaskConfig.cbSize = sizeof (TASKDIALOGCONFIG);
TaskConfig.hwndParent = ParentWindow;
TaskConfig.dwFlags = TDF_SIZE_TO_CONTENT;
TaskConfig.pszWindowTitle = wtitle;
if (messageboxdata->flags & SDL_MESSAGEBOX_ERROR) {
TaskConfig.pszMainIcon = TD_ERROR_ICON;
} else if (messageboxdata->flags & SDL_MESSAGEBOX_WARNING) {
TaskConfig.pszMainIcon = TD_WARNING_ICON;
} else if (messageboxdata->flags & SDL_MESSAGEBOX_INFORMATION) {
TaskConfig.pszMainIcon = TD_INFORMATION_ICON;
} else {
TaskConfig.pszMainIcon = NULL;
}
TaskConfig.pszContent = wmessage;
TaskConfig.cButtons = messageboxdata->numbuttons;
pButtons = SDL_malloc(sizeof (TASKDIALOG_BUTTON) * messageboxdata->numbuttons);
TaskConfig.nDefaultButton = 0;
Mar 3, 2018
Mar 3, 2018
852
nCancelButton = 0;
Mar 2, 2018
Mar 2, 2018
853
854
for (i = 0; i < messageboxdata->numbuttons; i++)
{
Mar 24, 2018
Mar 24, 2018
855
const char *buttontext;
Aug 3, 2019
Aug 3, 2019
856
857
858
859
860
if (messageboxdata->flags & SDL_MESSAGEBOX_BUTTONS_LEFT_TO_RIGHT) {
pButton = &pButtons[i];
} else {
pButton = &pButtons[messageboxdata->numbuttons - 1 - i];
}
Mar 2, 2018
Mar 2, 2018
861
862
if (messageboxdata->buttons[i].flags & SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT) {
nCancelButton = messageboxdata->buttons[i].buttonid;
Aug 3, 2019
Aug 3, 2019
863
pButton->nButtonID = IDCANCEL;
Mar 2, 2018
Mar 2, 2018
864
} else {
Aug 3, 2019
Aug 3, 2019
865
pButton->nButtonID = IDBUTTONINDEX0 + i;
Mar 2, 2018
Mar 2, 2018
866
}
Mar 24, 2018
Mar 24, 2018
867
868
869
870
871
872
873
874
875
876
877
878
879
880
buttontext = EscapeAmpersands(&ampescape, &ampescapesize, messageboxdata->buttons[i].text);
if (buttontext == NULL) {
int j;
FreeLibrary(hComctl32);
SDL_free(ampescape);
SDL_free(wmessage);
SDL_free(wtitle);
for (j = 0; j < i; j++) {
SDL_free((wchar_t *) pButtons[j].pszButtonText);
}
SDL_free(pButtons);
return -1;
}
pButton->pszButtonText = WIN_UTF8ToString(buttontext);
Mar 2, 2018
Mar 2, 2018
881
882
883
884
885
886
887
888
889
890
891
if (messageboxdata->buttons[i].flags & SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT) {
TaskConfig.nDefaultButton = pButton->nButtonID;
}
}
TaskConfig.pButtons = pButtons;
/* Show the Task Dialog */
hr = pfnTaskDialogIndirect(&TaskConfig, &nButton, NULL, NULL);
/* Free everything */
FreeLibrary(hComctl32);
Mar 24, 2018
Mar 24, 2018
892
SDL_free(ampescape);
Mar 2, 2018
Mar 2, 2018
893
894
895
896
897
898
899
900
901
SDL_free(wmessage);
SDL_free(wtitle);
for (i = 0; i < messageboxdata->numbuttons; i++) {
SDL_free((wchar_t *) pButtons[i].pszButtonText);
}
SDL_free(pButtons);
/* Check the Task Dialog was successful and give the result */
if (SUCCEEDED(hr)) {
Aug 3, 2019
Aug 3, 2019
902
if (nButton == IDCANCEL) {
Mar 2, 2018
Mar 2, 2018
903
*buttonid = nCancelButton;
Aug 3, 2019
Aug 3, 2019
904
905
} else if (nButton >= IDBUTTONINDEX0 && nButton < IDBUTTONINDEX0 + messageboxdata->numbuttons) {
*buttonid = messageboxdata->buttons[nButton - IDBUTTONINDEX0].buttonid;
Mar 2, 2018
Mar 2, 2018
906
} else {
Aug 3, 2019
Aug 3, 2019
907
*buttonid = -1;
Mar 2, 2018
Mar 2, 2018
908
909
910
911
912
913
914
915
}
return 0;
}
/* We failed showing the Task Dialog, use the old message box! */
return WIN_ShowOldMessageBox(messageboxdata, buttonid);
}
916
917
918
#endif /* SDL_VIDEO_DRIVER_WINDOWS */
/* vi: set ts=4 sw=4 expandtab: */