Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
478 lines (398 loc) · 12.6 KB

SDL_windowsmessagebox.c

File metadata and controls

478 lines (398 loc) · 12.6 KB
 
1
2
/*
Simple DirectMedia Layer
Feb 15, 2013
Feb 15, 2013
3
Copyright (C) 1997-2013 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
25
26
27
28
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_config.h"
#if SDL_VIDEO_DRIVER_WINDOWS
#include "SDL.h"
#include "SDL_windowsvideo.h"
Mar 26, 2013
Mar 26, 2013
29
30
31
32
#ifndef SS_EDITCONTROL
#define SS_EDITCONTROL 0x2000
#endif
33
34
/* Display a Windows message box */
Mar 26, 2013
Mar 26, 2013
35
36
37
38
#pragma pack(push, 1)
typedef struct
{
May 18, 2013
May 18, 2013
39
40
41
42
43
44
45
46
47
48
WORD dlgVer;
WORD signature;
DWORD helpID;
DWORD exStyle;
DWORD style;
WORD cDlgItems;
short x;
short y;
short cx;
short cy;
Mar 26, 2013
Mar 26, 2013
49
50
} DLGTEMPLATEEX;
May 18, 2013
May 18, 2013
53
54
55
56
57
58
59
60
DWORD helpID;
DWORD exStyle;
DWORD style;
short x;
short y;
short cx;
short cy;
DWORD id;
Mar 26, 2013
Mar 26, 2013
61
62
63
64
65
66
67
} DLGITEMTEMPLATEEX;
#pragma pack(pop)
typedef struct
{
DLGTEMPLATEEX* lpDialog;
68
69
70
71
72
73
74
75
76
77
78
Uint8 *data;
size_t size;
size_t used;
} WIN_DialogData;
static INT_PTR MessageBoxDialogProc(HWND hDlg, UINT iMessage, WPARAM wParam, LPARAM lParam)
{
switch ( iMessage ) {
case WM_COMMAND:
/* Return the ID of the button that was pushed */
May 18, 2013
May 18, 2013
79
EndDialog(hDlg, LOWORD(wParam));
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
return TRUE;
default:
break;
}
return FALSE;
}
static SDL_bool ExpandDialogSpace(WIN_DialogData *dialog, size_t space)
{
size_t size = dialog->size;
if (size == 0) {
size = space;
} else {
while ((dialog->used + space) > size) {
size *= 2;
}
}
if (size > dialog->size) {
void *data = SDL_realloc(dialog->data, size);
if (!data) {
SDL_OutOfMemory();
return SDL_FALSE;
}
dialog->data = data;
dialog->size = size;
Mar 26, 2013
Mar 26, 2013
107
dialog->lpDialog = (DLGTEMPLATEEX*)dialog->data;
108
109
110
}
return SDL_TRUE;
}
May 18, 2013
May 18, 2013
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
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;
}
Mar 26, 2013
Mar 26, 2013
165
166
static int s_BaseUnitsX;
static int s_BaseUnitsY;
Apr 3, 2013
Apr 3, 2013
167
static void Vec2ToDLU(short *x, short *y)
Mar 26, 2013
Mar 26, 2013
168
{
May 18, 2013
May 18, 2013
169
SDL_assert(s_BaseUnitsX != 0); /* we init in WIN_ShowMessageBox(), which is the only public function... */
Mar 26, 2013
Mar 26, 2013
170
171
172
173
174
175
*x = MulDiv(*x, 4, s_BaseUnitsX);
*y = MulDiv(*y, 8, s_BaseUnitsY);
}
176
177
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)
{
Mar 26, 2013
Mar 26, 2013
178
DLGITEMTEMPLATEEX item;
179
180
181
182
183
WORD marker = 0xFFFF;
WORD extraData = 0;
SDL_zero(item);
item.style = style;
Mar 26, 2013
Mar 26, 2013
184
item.exStyle = exStyle;
185
186
187
188
189
190
item.x = x;
item.y = y;
item.cx = w;
item.cy = h;
item.id = id;
Mar 26, 2013
Mar 26, 2013
191
192
193
Vec2ToDLU(&item.x, &item.y);
Vec2ToDLU(&item.cx, &item.cy);
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
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;
}
if (!AddDialogString(dialog, caption)) {
return SDL_FALSE;
}
if (!AddDialogData(dialog, &extraData, sizeof(extraData))) {
return SDL_FALSE;
}
Mar 26, 2013
Mar 26, 2013
212
++dialog->lpDialog->cDlgItems;
213
214
215
216
217
218
return SDL_TRUE;
}
static SDL_bool AddDialogStatic(WIN_DialogData *dialog, int x, int y, int w, int h, const char *text)
{
Mar 26, 2013
Mar 26, 2013
219
DWORD style = WS_VISIBLE | WS_CHILD | SS_LEFT | SS_NOPREFIX | SS_EDITCONTROL;
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
return AddDialogControl(dialog, 0x0082, style, 0, x, y, w, h, -1, text);
}
static SDL_bool AddDialogButton(WIN_DialogData *dialog, int x, int y, int w, int h, const char *text, int id, SDL_bool isDefault)
{
DWORD style = WS_VISIBLE | WS_CHILD;
if (isDefault) {
style |= BS_DEFPUSHBUTTON;
} else {
style |= BS_PUSHBUTTON;
}
return AddDialogControl(dialog, 0x0080, style, 0, x, y, w, h, id, text);
}
static void FreeDialogData(WIN_DialogData *dialog)
{
if (dialog->data) {
SDL_free(dialog->data);
}
SDL_free(dialog);
}
static WIN_DialogData *CreateDialogData(int w, int h, const char *caption)
{
WIN_DialogData *dialog;
Mar 26, 2013
Mar 26, 2013
245
246
DLGTEMPLATEEX dialogTemplate;
WORD WordToPass;
247
248
SDL_zero(dialogTemplate);
Mar 26, 2013
Mar 26, 2013
249
250
251
dialogTemplate.dlgVer = 1;
dialogTemplate.signature = 0xffff;
dialogTemplate.style = (WS_CAPTION | DS_CENTER | DS_SHELLFONT);
252
253
254
255
dialogTemplate.x = 0;
dialogTemplate.y = 0;
dialogTemplate.cx = w;
dialogTemplate.cy = h;
Mar 26, 2013
Mar 26, 2013
256
Vec2ToDLU(&dialogTemplate.cx, &dialogTemplate.cy);
257
258
259
260
261
262
263
264
265
266
267
dialog = (WIN_DialogData *)SDL_calloc(1, sizeof(*dialog));
if (!dialog) {
return NULL;
}
if (!AddDialogData(dialog, &dialogTemplate, sizeof(dialogTemplate))) {
FreeDialogData(dialog);
return NULL;
}
May 18, 2013
May 18, 2013
268
/* No menu */
Mar 26, 2013
Mar 26, 2013
269
270
271
272
273
274
WordToPass = 0;
if (!AddDialogData(dialog, &WordToPass, 2)) {
FreeDialogData(dialog);
return NULL;
}
May 18, 2013
May 18, 2013
275
/* No custom class */
Mar 26, 2013
Mar 26, 2013
276
if (!AddDialogData(dialog, &WordToPass, 2)) {
277
278
279
280
FreeDialogData(dialog);
return NULL;
}
May 18, 2013
May 18, 2013
281
/* title */
282
283
284
285
286
if (!AddDialogString(dialog, caption)) {
FreeDialogData(dialog);
return NULL;
}
May 18, 2013
May 18, 2013
287
/* Font stuff */
Mar 26, 2013
Mar 26, 2013
288
{
May 18, 2013
May 18, 2013
289
290
291
/*
* We want to use the system messagebox font.
*/
Mar 26, 2013
Mar 26, 2013
292
BYTE ToPass;
May 18, 2013
May 18, 2013
293
Mar 26, 2013
Mar 26, 2013
294
295
296
NONCLIENTMETRICSA NCM;
NCM.cbSize = sizeof(NCM);
SystemParametersInfoA(SPI_GETNONCLIENTMETRICS, 0, &NCM, 0);
May 18, 2013
May 18, 2013
297
298
/* Font size - convert to logical font size for dialog parameter. */
Mar 26, 2013
Mar 26, 2013
299
300
301
302
303
304
305
306
307
308
309
{
HDC ScreenDC = GetDC(0);
WordToPass = (WORD)(-72 * NCM.lfMessageFont.lfHeight / GetDeviceCaps(ScreenDC, LOGPIXELSY));
ReleaseDC(0, ScreenDC);
}
if (!AddDialogData(dialog, &WordToPass, 2)) {
FreeDialogData(dialog);
return NULL;
}
May 18, 2013
May 18, 2013
310
/* Font weight */
Mar 26, 2013
Mar 26, 2013
311
312
313
314
315
316
WordToPass = (WORD)NCM.lfMessageFont.lfWeight;
if (!AddDialogData(dialog, &WordToPass, 2)) {
FreeDialogData(dialog);
return NULL;
}
May 18, 2013
May 18, 2013
317
/* italic? */
Mar 26, 2013
Mar 26, 2013
318
319
320
321
322
323
ToPass = NCM.lfMessageFont.lfItalic;
if (!AddDialogData(dialog, &ToPass, 1)) {
FreeDialogData(dialog);
return NULL;
}
May 18, 2013
May 18, 2013
324
/* charset? */
Mar 26, 2013
Mar 26, 2013
325
326
327
328
329
330
ToPass = NCM.lfMessageFont.lfCharSet;
if (!AddDialogData(dialog, &ToPass, 1)) {
FreeDialogData(dialog);
return NULL;
}
May 18, 2013
May 18, 2013
331
/* font typeface. */
Mar 26, 2013
Mar 26, 2013
332
333
334
335
336
337
if (!AddDialogString(dialog, NCM.lfMessageFont.lfFaceName)) {
FreeDialogData(dialog);
return NULL;
}
}
338
339
340
341
342
343
344
return dialog;
}
int
WIN_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
{
WIN_DialogData *dialog;
Mar 26, 2013
Mar 26, 2013
345
int i, x, y, which;
346
const SDL_MessageBoxButtonData *buttons = messageboxdata->buttons;
Mar 26, 2013
Mar 26, 2013
347
348
349
350
351
352
353
354
355
356
357
HFONT DialogFont;
SIZE Size;
RECT TextSize;
wchar_t* wmessage;
TEXTMETRIC TM;
const int ButtonWidth = 88;
const int ButtonHeight = 26;
const int TextMargin = 16;
const int ButtonMargin = 12;
May 18, 2013
May 18, 2013
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
/* 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 26, 2013
Mar 26, 2013
392
HDC FontDC = CreateCompatibleDC(0);
May 18, 2013
May 18, 2013
393
Mar 26, 2013
Mar 26, 2013
394
{
May 18, 2013
May 18, 2013
395
/* Create a duplicate of the font used in system message boxes. */
Mar 26, 2013
Mar 26, 2013
396
397
398
399
400
401
402
403
LOGFONT lf;
NONCLIENTMETRICS NCM;
NCM.cbSize = sizeof(NCM);
SystemParametersInfo(SPI_GETNONCLIENTMETRICS, 0, &NCM, 0);
lf = NCM.lfMessageFont;
DialogFont = CreateFontIndirect(&lf);
}
May 18, 2013
May 18, 2013
404
/* Select the font in to our DC */
Mar 26, 2013
Mar 26, 2013
405
406
407
SelectObject(FontDC, DialogFont);
{
May 18, 2013
May 18, 2013
408
/* Get the metrics to try and figure our DLU conversion. */
Mar 26, 2013
Mar 26, 2013
409
410
411
412
GetTextMetrics(FontDC, &TM);
s_BaseUnitsX = TM.tmAveCharWidth + 1;
s_BaseUnitsY = TM.tmHeight;
}
May 18, 2013
May 18, 2013
413
414
/* Measure the *pixel* size of the string. */
Mar 26, 2013
Mar 26, 2013
415
416
417
418
wmessage = WIN_UTF8ToString(messageboxdata->message);
SDL_zero(TextSize);
Size.cx = DrawText(FontDC, wmessage, -1, &TextSize, DT_CALCRECT);
May 18, 2013
May 18, 2013
419
/* Add some padding for hangs, etc. */
Mar 26, 2013
Mar 26, 2013
420
421
422
TextSize.right += 2;
TextSize.bottom += 2;
May 18, 2013
May 18, 2013
423
/* Done with the DC, and the string */
Mar 26, 2013
Mar 26, 2013
424
425
426
DeleteDC(FontDC);
SDL_free(wmessage);
May 18, 2013
May 18, 2013
427
/* Increase the size of the dialog by some border spacing around the text. */
Mar 26, 2013
Mar 26, 2013
428
429
430
431
432
Size.cx = TextSize.right - TextSize.left;
Size.cy = TextSize.bottom - TextSize.top;
Size.cx += TextMargin * 2;
Size.cy += TextMargin * 2;
May 18, 2013
May 18, 2013
433
/* Ensure the size is wide enough for all of the buttons. */
Mar 26, 2013
Mar 26, 2013
434
435
436
if (Size.cx < messageboxdata->numbuttons * (ButtonWidth + ButtonMargin) + ButtonMargin)
Size.cx = messageboxdata->numbuttons * (ButtonWidth + ButtonMargin) + ButtonMargin;
May 18, 2013
May 18, 2013
437
/* Add vertical space for the buttons and border. */
Mar 26, 2013
Mar 26, 2013
438
439
440
Size.cy += ButtonHeight + TextMargin;
dialog = CreateDialogData(Size.cx, Size.cy, messageboxdata->title);
441
442
443
444
if (!dialog) {
return -1;
}
Mar 26, 2013
Mar 26, 2013
445
if (!AddDialogStatic(dialog, TextMargin, TextMargin, TextSize.right - TextSize.left, TextSize.bottom - TextSize.top, messageboxdata->message)) {
446
447
448
449
FreeDialogData(dialog);
return -1;
}
May 18, 2013
May 18, 2013
450
/* Align the buttons to the right/bottom. */
Mar 26, 2013
Mar 26, 2013
451
452
x = Size.cx - ButtonWidth - ButtonMargin;
y = Size.cy - ButtonHeight - ButtonMargin;
453
454
455
456
457
458
459
460
for (i = 0; i < messageboxdata->numbuttons; ++i) {
SDL_bool isDefault;
if (buttons[i].flags & SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT) {
isDefault = SDL_TRUE;
} else {
isDefault = SDL_FALSE;
}
Mar 26, 2013
Mar 26, 2013
461
if (!AddDialogButton(dialog, x, y, ButtonWidth, ButtonHeight, buttons[i].text, i, isDefault)) {
462
463
464
FreeDialogData(dialog);
return -1;
}
Mar 26, 2013
Mar 26, 2013
465
x -= ButtonWidth + ButtonMargin;
466
467
468
}
/* FIXME: If we have a parent window, get the Instance and HWND for them */
Mar 26, 2013
Mar 26, 2013
469
which = DialogBoxIndirect(NULL, (DLGTEMPLATE*)dialog->lpDialog, NULL, (DLGPROC)MessageBoxDialogProc);
470
471
472
473
474
475
476
477
478
*buttonid = buttons[which].buttonid;
FreeDialogData(dialog);
return 0;
}
#endif /* SDL_VIDEO_DRIVER_WINDOWS */
/* vi: set ts=4 sw=4 expandtab: */