SDL_windowsmessagebox.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2021 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../../SDL_internal.h"
  19. #if SDL_VIDEO_DRIVER_WINDOWS
  20. #ifdef HAVE_LIMITS_H
  21. #include <limits.h>
  22. #endif
  23. #ifndef SIZE_MAX
  24. #define SIZE_MAX ((size_t)-1)
  25. #endif
  26. #include "../../core/windows/SDL_windows.h"
  27. #include "SDL_windowsvideo.h"
  28. #include "SDL_windowstaskdialog.h"
  29. #ifndef SS_EDITCONTROL
  30. #define SS_EDITCONTROL 0x2000
  31. #endif
  32. #ifndef IDOK
  33. #define IDOK 1
  34. #endif
  35. #ifndef IDCANCEL
  36. #define IDCANCEL 2
  37. #endif
  38. /* Custom dialog return codes */
  39. #define IDCLOSED 20
  40. #define IDINVALPTRINIT 50
  41. #define IDINVALPTRCOMMAND 51
  42. #define IDINVALPTRSETFOCUS 52
  43. #define IDINVALPTRDLGITEM 53
  44. /* First button ID */
  45. #define IDBUTTONINDEX0 100
  46. #define DLGITEMTYPEBUTTON 0x0080
  47. #define DLGITEMTYPESTATIC 0x0082
  48. /* Windows only sends the lower 16 bits of the control ID when a button
  49. * gets clicked. There are also some predefined and custom IDs that lower
  50. * the available number further. 2^16 - 101 buttons should be enough for
  51. * everyone, no need to make the code more complex.
  52. */
  53. #define MAX_BUTTONS (0xffff - 100)
  54. /* Display a Windows message box */
  55. #pragma pack(push, 1)
  56. typedef struct
  57. {
  58. WORD dlgVer;
  59. WORD signature;
  60. DWORD helpID;
  61. DWORD exStyle;
  62. DWORD style;
  63. WORD cDlgItems;
  64. short x;
  65. short y;
  66. short cx;
  67. short cy;
  68. } DLGTEMPLATEEX;
  69. typedef struct
  70. {
  71. DWORD helpID;
  72. DWORD exStyle;
  73. DWORD style;
  74. short x;
  75. short y;
  76. short cx;
  77. short cy;
  78. DWORD id;
  79. } DLGITEMTEMPLATEEX;
  80. #pragma pack(pop)
  81. typedef struct
  82. {
  83. DLGTEMPLATEEX* lpDialog;
  84. Uint8 *data;
  85. size_t size;
  86. size_t used;
  87. WORD numbuttons;
  88. } WIN_DialogData;
  89. static SDL_bool GetButtonIndex(const SDL_MessageBoxData *messageboxdata, Uint32 flags, size_t *i)
  90. {
  91. for (*i = 0; *i < (size_t)messageboxdata->numbuttons; ++*i) {
  92. if (messageboxdata->buttons[*i].flags & flags) {
  93. return SDL_TRUE;
  94. }
  95. }
  96. return SDL_FALSE;
  97. }
  98. static INT_PTR MessageBoxDialogProc(HWND hDlg, UINT iMessage, WPARAM wParam, LPARAM lParam)
  99. {
  100. const SDL_MessageBoxData *messageboxdata;
  101. size_t buttonindex;
  102. switch ( iMessage ) {
  103. case WM_INITDIALOG:
  104. if (lParam == 0) {
  105. EndDialog(hDlg, IDINVALPTRINIT);
  106. return TRUE;
  107. }
  108. messageboxdata = (const SDL_MessageBoxData *)lParam;
  109. SetWindowLongPtr(hDlg, GWLP_USERDATA, lParam);
  110. if (GetButtonIndex(messageboxdata, SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, &buttonindex)) {
  111. /* Focus on the first default return-key button */
  112. HWND buttonctl = GetDlgItem(hDlg, (int)(IDBUTTONINDEX0 + buttonindex));
  113. if (buttonctl == NULL) {
  114. EndDialog(hDlg, IDINVALPTRDLGITEM);
  115. }
  116. PostMessage(hDlg, WM_NEXTDLGCTL, (WPARAM)buttonctl, TRUE);
  117. } else {
  118. /* Give the focus to the dialog window instead */
  119. SetFocus(hDlg);
  120. }
  121. return FALSE;
  122. case WM_SETFOCUS:
  123. messageboxdata = (const SDL_MessageBoxData *)GetWindowLongPtr(hDlg, GWLP_USERDATA);
  124. if (messageboxdata == NULL) {
  125. EndDialog(hDlg, IDINVALPTRSETFOCUS);
  126. return TRUE;
  127. }
  128. /* Let the default button be focused if there is one. Otherwise, prevent any initial focus. */
  129. if (GetButtonIndex(messageboxdata, SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, &buttonindex)) {
  130. return FALSE;
  131. }
  132. return TRUE;
  133. case WM_COMMAND:
  134. messageboxdata = (const SDL_MessageBoxData *)GetWindowLongPtr(hDlg, GWLP_USERDATA);
  135. if (messageboxdata == NULL) {
  136. EndDialog(hDlg, IDINVALPTRCOMMAND);
  137. return TRUE;
  138. }
  139. /* Return the ID of the button that was pushed */
  140. if (wParam == IDOK) {
  141. if (GetButtonIndex(messageboxdata, SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, &buttonindex)) {
  142. EndDialog(hDlg, IDBUTTONINDEX0 + buttonindex);
  143. }
  144. } else if (wParam == IDCANCEL) {
  145. if (GetButtonIndex(messageboxdata, SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, &buttonindex)) {
  146. EndDialog(hDlg, IDBUTTONINDEX0 + buttonindex);
  147. } else {
  148. /* Closing of window was requested by user or system. It would be rude not to comply. */
  149. EndDialog(hDlg, IDCLOSED);
  150. }
  151. } else if (wParam >= IDBUTTONINDEX0 && (int)wParam - IDBUTTONINDEX0 < messageboxdata->numbuttons) {
  152. EndDialog(hDlg, wParam);
  153. }
  154. return TRUE;
  155. default:
  156. break;
  157. }
  158. return FALSE;
  159. }
  160. static SDL_bool ExpandDialogSpace(WIN_DialogData *dialog, size_t space)
  161. {
  162. /* Growing memory in 64 KiB steps. */
  163. const size_t sizestep = 0x10000;
  164. size_t size = dialog->size;
  165. if (size == 0) {
  166. /* Start with 4 KiB or a multiple of 64 KiB to fit the data. */
  167. size = 0x1000;
  168. if (SIZE_MAX - sizestep < space) {
  169. size = space;
  170. } else if (space > size) {
  171. size = (space + sizestep) & ~(sizestep - 1);
  172. }
  173. } else if (SIZE_MAX - dialog->used < space) {
  174. SDL_OutOfMemory();
  175. return SDL_FALSE;
  176. } else if (SIZE_MAX - (dialog->used + space) < sizestep) {
  177. /* Close to the maximum. */
  178. size = dialog->used + space;
  179. } else if (size < dialog->used + space) {
  180. /* Round up to the next 64 KiB block. */
  181. size = dialog->used + space;
  182. size += sizestep - size % sizestep;
  183. }
  184. if (size > dialog->size) {
  185. void *data = SDL_realloc(dialog->data, size);
  186. if (!data) {
  187. SDL_OutOfMemory();
  188. return SDL_FALSE;
  189. }
  190. dialog->data = data;
  191. dialog->size = size;
  192. dialog->lpDialog = (DLGTEMPLATEEX*)dialog->data;
  193. }
  194. return SDL_TRUE;
  195. }
  196. static SDL_bool AlignDialogData(WIN_DialogData *dialog, size_t size)
  197. {
  198. size_t padding = (dialog->used % size);
  199. if (!ExpandDialogSpace(dialog, padding)) {
  200. return SDL_FALSE;
  201. }
  202. dialog->used += padding;
  203. return SDL_TRUE;
  204. }
  205. static SDL_bool AddDialogData(WIN_DialogData *dialog, const void *data, size_t size)
  206. {
  207. if (!ExpandDialogSpace(dialog, size)) {
  208. return SDL_FALSE;
  209. }
  210. SDL_memcpy(dialog->data+dialog->used, data, size);
  211. dialog->used += size;
  212. return SDL_TRUE;
  213. }
  214. static SDL_bool AddDialogString(WIN_DialogData *dialog, const char *string)
  215. {
  216. WCHAR *wstring;
  217. WCHAR *p;
  218. size_t count;
  219. SDL_bool status;
  220. if (!string) {
  221. string = "";
  222. }
  223. wstring = WIN_UTF8ToString(string);
  224. if (!wstring) {
  225. return SDL_FALSE;
  226. }
  227. /* Find out how many characters we have, including null terminator */
  228. count = 0;
  229. for (p = wstring; *p; ++p) {
  230. ++count;
  231. }
  232. ++count;
  233. status = AddDialogData(dialog, wstring, count*sizeof(WCHAR));
  234. SDL_free(wstring);
  235. return status;
  236. }
  237. static int s_BaseUnitsX;
  238. static int s_BaseUnitsY;
  239. static void Vec2ToDLU(short *x, short *y)
  240. {
  241. SDL_assert(s_BaseUnitsX != 0); /* we init in WIN_ShowMessageBox(), which is the only public function... */
  242. *x = MulDiv(*x, 4, s_BaseUnitsX);
  243. *y = MulDiv(*y, 8, s_BaseUnitsY);
  244. }
  245. 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)
  246. {
  247. DLGITEMTEMPLATEEX item;
  248. WORD marker = 0xFFFF;
  249. WORD extraData = 0;
  250. SDL_zero(item);
  251. item.style = style;
  252. item.exStyle = exStyle;
  253. item.x = x;
  254. item.y = y;
  255. item.cx = w;
  256. item.cy = h;
  257. item.id = id;
  258. Vec2ToDLU(&item.x, &item.y);
  259. Vec2ToDLU(&item.cx, &item.cy);
  260. if (!AlignDialogData(dialog, sizeof(DWORD))) {
  261. return SDL_FALSE;
  262. }
  263. if (!AddDialogData(dialog, &item, sizeof(item))) {
  264. return SDL_FALSE;
  265. }
  266. if (!AddDialogData(dialog, &marker, sizeof(marker))) {
  267. return SDL_FALSE;
  268. }
  269. if (!AddDialogData(dialog, &type, sizeof(type))) {
  270. return SDL_FALSE;
  271. }
  272. if (type == DLGITEMTYPEBUTTON || (type == DLGITEMTYPESTATIC && caption != NULL)) {
  273. if (!AddDialogString(dialog, caption)) {
  274. return SDL_FALSE;
  275. }
  276. } else {
  277. if (!AddDialogData(dialog, &marker, sizeof(marker))) {
  278. return SDL_FALSE;
  279. }
  280. if (!AddDialogData(dialog, &ordinal, sizeof(ordinal))) {
  281. return SDL_FALSE;
  282. }
  283. }
  284. if (!AddDialogData(dialog, &extraData, sizeof(extraData))) {
  285. return SDL_FALSE;
  286. }
  287. if (type == DLGITEMTYPEBUTTON) {
  288. dialog->numbuttons++;
  289. }
  290. ++dialog->lpDialog->cDlgItems;
  291. return SDL_TRUE;
  292. }
  293. static SDL_bool AddDialogStaticText(WIN_DialogData *dialog, int x, int y, int w, int h, const char *text)
  294. {
  295. DWORD style = WS_VISIBLE | WS_CHILD | SS_LEFT | SS_NOPREFIX | SS_EDITCONTROL | WS_GROUP;
  296. return AddDialogControl(dialog, DLGITEMTYPESTATIC, style, 0, x, y, w, h, -1, text, 0);
  297. }
  298. static SDL_bool AddDialogStaticIcon(WIN_DialogData *dialog, int x, int y, int w, int h, Uint16 ordinal)
  299. {
  300. DWORD style = WS_VISIBLE | WS_CHILD | SS_ICON | WS_GROUP;
  301. return AddDialogControl(dialog, DLGITEMTYPESTATIC, style, 0, x, y, w, h, -2, NULL, ordinal);
  302. }
  303. static SDL_bool AddDialogButton(WIN_DialogData *dialog, int x, int y, int w, int h, const char *text, int id, SDL_bool isDefault)
  304. {
  305. DWORD style = WS_VISIBLE | WS_CHILD | WS_TABSTOP;
  306. if (isDefault) {
  307. style |= BS_DEFPUSHBUTTON;
  308. } else {
  309. style |= BS_PUSHBUTTON;
  310. }
  311. /* The first button marks the start of the group. */
  312. if (dialog->numbuttons == 0) {
  313. style |= WS_GROUP;
  314. }
  315. return AddDialogControl(dialog, DLGITEMTYPEBUTTON, style, 0, x, y, w, h, id, text, 0);
  316. }
  317. static void FreeDialogData(WIN_DialogData *dialog)
  318. {
  319. SDL_free(dialog->data);
  320. SDL_free(dialog);
  321. }
  322. static WIN_DialogData *CreateDialogData(int w, int h, const char *caption)
  323. {
  324. WIN_DialogData *dialog;
  325. DLGTEMPLATEEX dialogTemplate;
  326. WORD WordToPass;
  327. SDL_zero(dialogTemplate);
  328. dialogTemplate.dlgVer = 1;
  329. dialogTemplate.signature = 0xffff;
  330. dialogTemplate.style = (WS_CAPTION | DS_CENTER | DS_SHELLFONT);
  331. dialogTemplate.x = 0;
  332. dialogTemplate.y = 0;
  333. dialogTemplate.cx = w;
  334. dialogTemplate.cy = h;
  335. Vec2ToDLU(&dialogTemplate.cx, &dialogTemplate.cy);
  336. dialog = (WIN_DialogData *)SDL_calloc(1, sizeof(*dialog));
  337. if (!dialog) {
  338. return NULL;
  339. }
  340. if (!AddDialogData(dialog, &dialogTemplate, sizeof(dialogTemplate))) {
  341. FreeDialogData(dialog);
  342. return NULL;
  343. }
  344. /* No menu */
  345. WordToPass = 0;
  346. if (!AddDialogData(dialog, &WordToPass, 2)) {
  347. FreeDialogData(dialog);
  348. return NULL;
  349. }
  350. /* No custom class */
  351. if (!AddDialogData(dialog, &WordToPass, 2)) {
  352. FreeDialogData(dialog);
  353. return NULL;
  354. }
  355. /* title */
  356. if (!AddDialogString(dialog, caption)) {
  357. FreeDialogData(dialog);
  358. return NULL;
  359. }
  360. /* Font stuff */
  361. {
  362. /*
  363. * We want to use the system messagebox font.
  364. */
  365. BYTE ToPass;
  366. NONCLIENTMETRICSA NCM;
  367. NCM.cbSize = sizeof(NCM);
  368. SystemParametersInfoA(SPI_GETNONCLIENTMETRICS, 0, &NCM, 0);
  369. /* Font size - convert to logical font size for dialog parameter. */
  370. {
  371. HDC ScreenDC = GetDC(NULL);
  372. int LogicalPixelsY = GetDeviceCaps(ScreenDC, LOGPIXELSY);
  373. if (!LogicalPixelsY) /* This can happen if the application runs out of GDI handles */
  374. LogicalPixelsY = 72;
  375. WordToPass = (WORD)(-72 * NCM.lfMessageFont.lfHeight / LogicalPixelsY);
  376. ReleaseDC(NULL, ScreenDC);
  377. }
  378. if (!AddDialogData(dialog, &WordToPass, 2)) {
  379. FreeDialogData(dialog);
  380. return NULL;
  381. }
  382. /* Font weight */
  383. WordToPass = (WORD)NCM.lfMessageFont.lfWeight;
  384. if (!AddDialogData(dialog, &WordToPass, 2)) {
  385. FreeDialogData(dialog);
  386. return NULL;
  387. }
  388. /* italic? */
  389. ToPass = NCM.lfMessageFont.lfItalic;
  390. if (!AddDialogData(dialog, &ToPass, 1)) {
  391. FreeDialogData(dialog);
  392. return NULL;
  393. }
  394. /* charset? */
  395. ToPass = NCM.lfMessageFont.lfCharSet;
  396. if (!AddDialogData(dialog, &ToPass, 1)) {
  397. FreeDialogData(dialog);
  398. return NULL;
  399. }
  400. /* font typeface. */
  401. if (!AddDialogString(dialog, NCM.lfMessageFont.lfFaceName)) {
  402. FreeDialogData(dialog);
  403. return NULL;
  404. }
  405. }
  406. return dialog;
  407. }
  408. /* Escaping ampersands is necessary to disable mnemonics in dialog controls.
  409. * The caller provides a char** for dst and a size_t* for dstlen where the
  410. * address of the work buffer and its size will be stored. Their values must be
  411. * NULL and 0 on the first call. src is the string to be escaped. On error, the
  412. * function returns NULL and, on success, returns a pointer to the escaped
  413. * sequence as a read-only string that is valid until the next call or until the
  414. * work buffer is freed. Once all strings have been processed, it's the caller's
  415. * responsibilty to free the work buffer with SDL_free, even on errors.
  416. */
  417. static const char *EscapeAmpersands(char **dst, size_t *dstlen, const char *src)
  418. {
  419. char *newdst;
  420. size_t ampcount = 0;
  421. size_t srclen = 0;
  422. if (src == NULL) {
  423. return NULL;
  424. }
  425. while (src[srclen]) {
  426. if (src[srclen] == '&') {
  427. ampcount++;
  428. }
  429. srclen++;
  430. }
  431. srclen++;
  432. if (ampcount == 0) {
  433. /* Nothing to do. */
  434. return src;
  435. }
  436. if (SIZE_MAX - srclen < ampcount) {
  437. return NULL;
  438. }
  439. if (*dst == NULL || *dstlen < srclen + ampcount) {
  440. /* Allocating extra space in case the next strings are a bit longer. */
  441. size_t extraspace = SIZE_MAX - (srclen + ampcount);
  442. if (extraspace > 512) {
  443. extraspace = 512;
  444. }
  445. *dstlen = srclen + ampcount + extraspace;
  446. SDL_free(*dst);
  447. *dst = NULL;
  448. newdst = SDL_malloc(*dstlen);
  449. if (newdst == NULL) {
  450. return NULL;
  451. }
  452. *dst = newdst;
  453. } else {
  454. newdst = *dst;
  455. }
  456. /* The escape character is the ampersand itself. */
  457. while (srclen--) {
  458. if (*src == '&') {
  459. *newdst++ = '&';
  460. }
  461. *newdst++ = *src++;
  462. }
  463. return *dst;
  464. }
  465. /* This function is called if a Task Dialog is unsupported. */
  466. static int
  467. WIN_ShowOldMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
  468. {
  469. WIN_DialogData *dialog;
  470. int i, x, y, retval;
  471. HFONT DialogFont;
  472. SIZE Size;
  473. RECT TextSize;
  474. wchar_t* wmessage;
  475. TEXTMETRIC TM;
  476. HDC FontDC;
  477. INT_PTR result;
  478. char *ampescape = NULL;
  479. size_t ampescapesize = 0;
  480. Uint16 defbuttoncount = 0;
  481. Uint16 icon = 0;
  482. HWND ParentWindow = NULL;
  483. const int ButtonWidth = 88;
  484. const int ButtonHeight = 26;
  485. const int TextMargin = 16;
  486. const int ButtonMargin = 12;
  487. const int IconWidth = GetSystemMetrics(SM_CXICON);
  488. const int IconHeight = GetSystemMetrics(SM_CYICON);
  489. const int IconMargin = 20;
  490. if (messageboxdata->numbuttons > MAX_BUTTONS) {
  491. return SDL_SetError("Number of butons exceeds limit of %d", MAX_BUTTONS);
  492. }
  493. switch (messageboxdata->flags) {
  494. case SDL_MESSAGEBOX_ERROR:
  495. icon = (Uint16)(size_t)IDI_ERROR;
  496. break;
  497. case SDL_MESSAGEBOX_WARNING:
  498. icon = (Uint16)(size_t)IDI_WARNING;
  499. break;
  500. case SDL_MESSAGEBOX_INFORMATION:
  501. icon = (Uint16)(size_t)IDI_INFORMATION;
  502. break;
  503. }
  504. /* Jan 25th, 2013 - [email protected]
  505. *
  506. * I've tried to make this more reasonable, but I've run in to a lot
  507. * of nonsense.
  508. *
  509. * The original issue is the code was written in pixels and not
  510. * dialog units (DLUs). All DialogBox functions use DLUs, which
  511. * vary based on the selected font (yay).
  512. *
  513. * According to MSDN, the most reliable way to convert is via
  514. * MapDialogUnits, which requires an HWND, which we don't have
  515. * at time of template creation.
  516. *
  517. * We do however have:
  518. * The system font (DLU width 8 for me)
  519. * The font we select for the dialog (DLU width 6 for me)
  520. *
  521. * Based on experimentation, *neither* of these return the value
  522. * actually used. Stepping in to MapDialogUnits(), the conversion
  523. * is fairly clear, and uses 7 for me.
  524. *
  525. * As a result, some of this is hacky to ensure the sizing is
  526. * somewhat correct.
  527. *
  528. * Honestly, a long term solution is to use CreateWindow, not CreateDialog.
  529. *
  530. * In order to get text dimensions we need to have a DC with the desired font.
  531. * I'm assuming a dialog box in SDL is rare enough we can to the create.
  532. */
  533. FontDC = CreateCompatibleDC(0);
  534. {
  535. /* Create a duplicate of the font used in system message boxes. */
  536. LOGFONT lf;
  537. NONCLIENTMETRICS NCM;
  538. NCM.cbSize = sizeof(NCM);
  539. SystemParametersInfo(SPI_GETNONCLIENTMETRICS, 0, &NCM, 0);
  540. lf = NCM.lfMessageFont;
  541. DialogFont = CreateFontIndirect(&lf);
  542. }
  543. /* Select the font in to our DC */
  544. SelectObject(FontDC, DialogFont);
  545. {
  546. /* Get the metrics to try and figure our DLU conversion. */
  547. GetTextMetrics(FontDC, &TM);
  548. /* Calculation from the following documentation:
  549. * https://support.microsoft.com/en-gb/help/125681/how-to-calculate-dialog-base-units-with-non-system-based-font
  550. * This fixes bug 2137, dialog box calculation with a fixed-width system font
  551. */
  552. {
  553. SIZE extent;
  554. GetTextExtentPoint32A(FontDC, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", 52, &extent);
  555. s_BaseUnitsX = (extent.cx / 26 + 1) / 2;
  556. }
  557. /*s_BaseUnitsX = TM.tmAveCharWidth + 1;*/
  558. s_BaseUnitsY = TM.tmHeight;
  559. }
  560. /* Measure the *pixel* size of the string. */
  561. wmessage = WIN_UTF8ToString(messageboxdata->message);
  562. SDL_zero(TextSize);
  563. DrawText(FontDC, wmessage, -1, &TextSize, DT_CALCRECT | DT_LEFT | DT_NOPREFIX | DT_EDITCONTROL);
  564. /* Add margins and some padding for hangs, etc. */
  565. TextSize.left += TextMargin;
  566. TextSize.right += TextMargin + 2;
  567. TextSize.top += TextMargin;
  568. TextSize.bottom += TextMargin + 2;
  569. /* Done with the DC, and the string */
  570. DeleteDC(FontDC);
  571. SDL_free(wmessage);
  572. /* Increase the size of the dialog by some border spacing around the text. */
  573. Size.cx = TextSize.right - TextSize.left;
  574. Size.cy = TextSize.bottom - TextSize.top;
  575. Size.cx += TextMargin * 2;
  576. Size.cy += TextMargin * 2;
  577. /* Make dialog wider and shift text over for the icon. */
  578. if (icon) {
  579. Size.cx += IconMargin + IconWidth;
  580. TextSize.left += IconMargin + IconWidth;
  581. TextSize.right += IconMargin + IconWidth;
  582. }
  583. /* Ensure the size is wide enough for all of the buttons. */
  584. if (Size.cx < messageboxdata->numbuttons * (ButtonWidth + ButtonMargin) + ButtonMargin)
  585. Size.cx = messageboxdata->numbuttons * (ButtonWidth + ButtonMargin) + ButtonMargin;
  586. /* Reset the height to the icon size if it is actually bigger than the text. */
  587. if (icon && Size.cy < IconMargin * 2 + IconHeight) {
  588. Size.cy = IconMargin * 2 + IconHeight;
  589. }
  590. /* Add vertical space for the buttons and border. */
  591. Size.cy += ButtonHeight + TextMargin;
  592. dialog = CreateDialogData(Size.cx, Size.cy, messageboxdata->title);
  593. if (!dialog) {
  594. return -1;
  595. }
  596. if (icon && ! AddDialogStaticIcon(dialog, IconMargin, IconMargin, IconWidth, IconHeight, icon)) {
  597. FreeDialogData(dialog);
  598. return -1;
  599. }
  600. if (!AddDialogStaticText(dialog, TextSize.left, TextSize.top, TextSize.right - TextSize.left, TextSize.bottom - TextSize.top, messageboxdata->message)) {
  601. FreeDialogData(dialog);
  602. return -1;
  603. }
  604. /* Align the buttons to the right/bottom. */
  605. x = Size.cx - (ButtonWidth + ButtonMargin) * messageboxdata->numbuttons;
  606. y = Size.cy - ButtonHeight - ButtonMargin;
  607. for (i = 0; i < messageboxdata->numbuttons; i++) {
  608. SDL_bool isdefault = SDL_FALSE;
  609. const char *buttontext;
  610. const SDL_MessageBoxButtonData *sdlButton;
  611. /* We always have to create the dialog buttons from left to right
  612. * so that the tab order is correct. Select the info to use
  613. * depending on which order was requested. */
  614. if (messageboxdata->flags & SDL_MESSAGEBOX_BUTTONS_LEFT_TO_RIGHT) {
  615. sdlButton = &messageboxdata->buttons[i];
  616. } else {
  617. sdlButton = &messageboxdata->buttons[messageboxdata->numbuttons - 1 - i];
  618. }
  619. if (sdlButton->flags & SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT) {
  620. defbuttoncount++;
  621. if (defbuttoncount == 1) {
  622. isdefault = SDL_TRUE;
  623. }
  624. }
  625. buttontext = EscapeAmpersands(&ampescape, &ampescapesize, sdlButton->text);
  626. /* Make sure to provide the correct ID to keep buttons indexed in the
  627. * same order as how they are in messageboxdata. */
  628. if (buttontext == NULL || !AddDialogButton(dialog, x, y, ButtonWidth, ButtonHeight, buttontext, IDBUTTONINDEX0 + (int)(sdlButton - messageboxdata->buttons), isdefault)) {
  629. FreeDialogData(dialog);
  630. SDL_free(ampescape);
  631. return -1;
  632. }
  633. x += ButtonWidth + ButtonMargin;
  634. }
  635. SDL_free(ampescape);
  636. /* If we have a parent window, get the Instance and HWND for them
  637. * so that our little dialog gets exclusive focus at all times. */
  638. if (messageboxdata->window) {
  639. ParentWindow = ((SDL_WindowData*)messageboxdata->window->driverdata)->hwnd;
  640. }
  641. result = DialogBoxIndirectParam(NULL, (DLGTEMPLATE*)dialog->lpDialog, ParentWindow, (DLGPROC)MessageBoxDialogProc, (LPARAM)messageboxdata);
  642. if (result >= IDBUTTONINDEX0 && result - IDBUTTONINDEX0 < messageboxdata->numbuttons) {
  643. *buttonid = messageboxdata->buttons[result - IDBUTTONINDEX0].buttonid;
  644. retval = 0;
  645. } else if (result == IDCLOSED) {
  646. /* Dialog window closed by user or system. */
  647. /* This could use a special return code. */
  648. retval = 0;
  649. *buttonid = -1;
  650. } else {
  651. if (result == 0) {
  652. SDL_SetError("Invalid parent window handle");
  653. } else if (result == -1) {
  654. SDL_SetError("The message box encountered an error.");
  655. } else if (result == IDINVALPTRINIT || result == IDINVALPTRSETFOCUS || result == IDINVALPTRCOMMAND) {
  656. SDL_SetError("Invalid message box pointer in dialog procedure");
  657. } else if (result == IDINVALPTRDLGITEM) {
  658. SDL_SetError("Couldn't find dialog control of the default enter-key button");
  659. } else {
  660. SDL_SetError("An unknown error occured");
  661. }
  662. retval = -1;
  663. }
  664. FreeDialogData(dialog);
  665. return retval;
  666. }
  667. /* TaskDialogIndirect procedure
  668. * This is because SDL targets Windows XP (0x501), so this is not defined in the platform SDK.
  669. */
  670. typedef HRESULT(FAR WINAPI *TASKDIALOGINDIRECTPROC)(const TASKDIALOGCONFIG *pTaskConfig, int *pnButton, int *pnRadioButton, BOOL *pfVerificationFlagChecked);
  671. int
  672. WIN_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
  673. {
  674. HWND ParentWindow = NULL;
  675. wchar_t *wmessage;
  676. wchar_t *wtitle;
  677. TASKDIALOGCONFIG TaskConfig;
  678. TASKDIALOG_BUTTON *pButtons;
  679. TASKDIALOG_BUTTON *pButton;
  680. HMODULE hComctl32;
  681. TASKDIALOGINDIRECTPROC pfnTaskDialogIndirect;
  682. HRESULT hr;
  683. char *ampescape = NULL;
  684. size_t ampescapesize = 0;
  685. int nButton;
  686. int nCancelButton;
  687. int i;
  688. if (SIZE_MAX / sizeof(TASKDIALOG_BUTTON) < messageboxdata->numbuttons) {
  689. return SDL_OutOfMemory();
  690. }
  691. /* If we cannot load comctl32.dll use the old messagebox! */
  692. hComctl32 = LoadLibrary(TEXT("comctl32.dll"));
  693. if (hComctl32 == NULL) {
  694. return WIN_ShowOldMessageBox(messageboxdata, buttonid);
  695. }
  696. /* If TaskDialogIndirect doesn't exist use the old messagebox!
  697. This will fail prior to Windows Vista.
  698. The manifest file in the application may require targeting version 6 of comctl32.dll, even
  699. when we use LoadLibrary here!
  700. If you don't want to bother with manifests, put this #pragma in your app's source code somewhere:
  701. pragma comment(linker,"\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
  702. */
  703. pfnTaskDialogIndirect = (TASKDIALOGINDIRECTPROC) GetProcAddress(hComctl32, "TaskDialogIndirect");
  704. if (pfnTaskDialogIndirect == NULL) {
  705. FreeLibrary(hComctl32);
  706. return WIN_ShowOldMessageBox(messageboxdata, buttonid);
  707. }
  708. /* If we have a parent window, get the Instance and HWND for them
  709. so that our little dialog gets exclusive focus at all times. */
  710. if (messageboxdata->window) {
  711. ParentWindow = ((SDL_WindowData *) messageboxdata->window->driverdata)->hwnd;
  712. }
  713. wmessage = WIN_UTF8ToString(messageboxdata->message);
  714. wtitle = WIN_UTF8ToString(messageboxdata->title);
  715. SDL_zero(TaskConfig);
  716. TaskConfig.cbSize = sizeof (TASKDIALOGCONFIG);
  717. TaskConfig.hwndParent = ParentWindow;
  718. TaskConfig.dwFlags = TDF_SIZE_TO_CONTENT;
  719. TaskConfig.pszWindowTitle = wtitle;
  720. if (messageboxdata->flags & SDL_MESSAGEBOX_ERROR) {
  721. TaskConfig.pszMainIcon = TD_ERROR_ICON;
  722. } else if (messageboxdata->flags & SDL_MESSAGEBOX_WARNING) {
  723. TaskConfig.pszMainIcon = TD_WARNING_ICON;
  724. } else if (messageboxdata->flags & SDL_MESSAGEBOX_INFORMATION) {
  725. TaskConfig.pszMainIcon = TD_INFORMATION_ICON;
  726. } else {
  727. TaskConfig.pszMainIcon = NULL;
  728. }
  729. TaskConfig.pszContent = wmessage;
  730. TaskConfig.cButtons = messageboxdata->numbuttons;
  731. pButtons = SDL_malloc(sizeof (TASKDIALOG_BUTTON) * messageboxdata->numbuttons);
  732. TaskConfig.nDefaultButton = 0;
  733. nCancelButton = 0;
  734. for (i = 0; i < messageboxdata->numbuttons; i++)
  735. {
  736. const char *buttontext;
  737. if (messageboxdata->flags & SDL_MESSAGEBOX_BUTTONS_LEFT_TO_RIGHT) {
  738. pButton = &pButtons[i];
  739. } else {
  740. pButton = &pButtons[messageboxdata->numbuttons - 1 - i];
  741. }
  742. if (messageboxdata->buttons[i].flags & SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT) {
  743. nCancelButton = messageboxdata->buttons[i].buttonid;
  744. pButton->nButtonID = IDCANCEL;
  745. } else {
  746. pButton->nButtonID = IDBUTTONINDEX0 + i;
  747. }
  748. buttontext = EscapeAmpersands(&ampescape, &ampescapesize, messageboxdata->buttons[i].text);
  749. if (buttontext == NULL) {
  750. int j;
  751. FreeLibrary(hComctl32);
  752. SDL_free(ampescape);
  753. SDL_free(wmessage);
  754. SDL_free(wtitle);
  755. for (j = 0; j < i; j++) {
  756. SDL_free((wchar_t *) pButtons[j].pszButtonText);
  757. }
  758. SDL_free(pButtons);
  759. return -1;
  760. }
  761. pButton->pszButtonText = WIN_UTF8ToString(buttontext);
  762. if (messageboxdata->buttons[i].flags & SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT) {
  763. TaskConfig.nDefaultButton = pButton->nButtonID;
  764. }
  765. }
  766. TaskConfig.pButtons = pButtons;
  767. /* Show the Task Dialog */
  768. hr = pfnTaskDialogIndirect(&TaskConfig, &nButton, NULL, NULL);
  769. /* Free everything */
  770. FreeLibrary(hComctl32);
  771. SDL_free(ampescape);
  772. SDL_free(wmessage);
  773. SDL_free(wtitle);
  774. for (i = 0; i < messageboxdata->numbuttons; i++) {
  775. SDL_free((wchar_t *) pButtons[i].pszButtonText);
  776. }
  777. SDL_free(pButtons);
  778. /* Check the Task Dialog was successful and give the result */
  779. if (SUCCEEDED(hr)) {
  780. if (nButton == IDCANCEL) {
  781. *buttonid = nCancelButton;
  782. } else if (nButton >= IDBUTTONINDEX0 && nButton < IDBUTTONINDEX0 + messageboxdata->numbuttons) {
  783. *buttonid = messageboxdata->buttons[nButton - IDBUTTONINDEX0].buttonid;
  784. } else {
  785. *buttonid = -1;
  786. }
  787. return 0;
  788. }
  789. /* We failed showing the Task Dialog, use the old message box! */
  790. return WIN_ShowOldMessageBox(messageboxdata, buttonid);
  791. }
  792. #endif /* SDL_VIDEO_DRIVER_WINDOWS */
  793. /* vi: set ts=4 sw=4 expandtab: */