SDL_windowsmessagebox.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
  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. #include "../../core/windows/SDL_windows.h"
  21. #include "SDL_assert.h"
  22. #include "SDL_windowsvideo.h"
  23. #ifndef SS_EDITCONTROL
  24. #define SS_EDITCONTROL 0x2000
  25. #endif
  26. /* Display a Windows message box */
  27. #pragma pack(push, 1)
  28. typedef struct
  29. {
  30. WORD dlgVer;
  31. WORD signature;
  32. DWORD helpID;
  33. DWORD exStyle;
  34. DWORD style;
  35. WORD cDlgItems;
  36. short x;
  37. short y;
  38. short cx;
  39. short cy;
  40. } DLGTEMPLATEEX;
  41. typedef struct
  42. {
  43. DWORD helpID;
  44. DWORD exStyle;
  45. DWORD style;
  46. short x;
  47. short y;
  48. short cx;
  49. short cy;
  50. DWORD id;
  51. } DLGITEMTEMPLATEEX;
  52. #pragma pack(pop)
  53. typedef struct
  54. {
  55. DLGTEMPLATEEX* lpDialog;
  56. Uint8 *data;
  57. size_t size;
  58. size_t used;
  59. } WIN_DialogData;
  60. static INT_PTR MessageBoxDialogProc(HWND hDlg, UINT iMessage, WPARAM wParam, LPARAM lParam)
  61. {
  62. switch ( iMessage ) {
  63. case WM_COMMAND:
  64. /* Return the ID of the button that was pushed */
  65. EndDialog(hDlg, LOWORD(wParam));
  66. return TRUE;
  67. default:
  68. break;
  69. }
  70. return FALSE;
  71. }
  72. static SDL_bool ExpandDialogSpace(WIN_DialogData *dialog, size_t space)
  73. {
  74. size_t size = dialog->size;
  75. if (size == 0) {
  76. size = space;
  77. } else {
  78. while ((dialog->used + space) > size) {
  79. size *= 2;
  80. }
  81. }
  82. if (size > dialog->size) {
  83. void *data = SDL_realloc(dialog->data, size);
  84. if (!data) {
  85. SDL_OutOfMemory();
  86. return SDL_FALSE;
  87. }
  88. dialog->data = data;
  89. dialog->size = size;
  90. dialog->lpDialog = (DLGTEMPLATEEX*)dialog->data;
  91. }
  92. return SDL_TRUE;
  93. }
  94. static SDL_bool AlignDialogData(WIN_DialogData *dialog, size_t size)
  95. {
  96. size_t padding = (dialog->used % size);
  97. if (!ExpandDialogSpace(dialog, padding)) {
  98. return SDL_FALSE;
  99. }
  100. dialog->used += padding;
  101. return SDL_TRUE;
  102. }
  103. static SDL_bool AddDialogData(WIN_DialogData *dialog, const void *data, size_t size)
  104. {
  105. if (!ExpandDialogSpace(dialog, size)) {
  106. return SDL_FALSE;
  107. }
  108. SDL_memcpy(dialog->data+dialog->used, data, size);
  109. dialog->used += size;
  110. return SDL_TRUE;
  111. }
  112. static SDL_bool AddDialogString(WIN_DialogData *dialog, const char *string)
  113. {
  114. WCHAR *wstring;
  115. WCHAR *p;
  116. size_t count;
  117. SDL_bool status;
  118. if (!string) {
  119. string = "";
  120. }
  121. wstring = WIN_UTF8ToString(string);
  122. if (!wstring) {
  123. return SDL_FALSE;
  124. }
  125. /* Find out how many characters we have, including null terminator */
  126. count = 0;
  127. for (p = wstring; *p; ++p) {
  128. ++count;
  129. }
  130. ++count;
  131. status = AddDialogData(dialog, wstring, count*sizeof(WCHAR));
  132. SDL_free(wstring);
  133. return status;
  134. }
  135. static int s_BaseUnitsX;
  136. static int s_BaseUnitsY;
  137. static void Vec2ToDLU(short *x, short *y)
  138. {
  139. SDL_assert(s_BaseUnitsX != 0); /* we init in WIN_ShowMessageBox(), which is the only public function... */
  140. *x = MulDiv(*x, 4, s_BaseUnitsX);
  141. *y = MulDiv(*y, 8, s_BaseUnitsY);
  142. }
  143. 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)
  144. {
  145. DLGITEMTEMPLATEEX item;
  146. WORD marker = 0xFFFF;
  147. WORD extraData = 0;
  148. SDL_zero(item);
  149. item.style = style;
  150. item.exStyle = exStyle;
  151. item.x = x;
  152. item.y = y;
  153. item.cx = w;
  154. item.cy = h;
  155. item.id = id;
  156. Vec2ToDLU(&item.x, &item.y);
  157. Vec2ToDLU(&item.cx, &item.cy);
  158. if (!AlignDialogData(dialog, sizeof(DWORD))) {
  159. return SDL_FALSE;
  160. }
  161. if (!AddDialogData(dialog, &item, sizeof(item))) {
  162. return SDL_FALSE;
  163. }
  164. if (!AddDialogData(dialog, &marker, sizeof(marker))) {
  165. return SDL_FALSE;
  166. }
  167. if (!AddDialogData(dialog, &type, sizeof(type))) {
  168. return SDL_FALSE;
  169. }
  170. if (!AddDialogString(dialog, caption)) {
  171. return SDL_FALSE;
  172. }
  173. if (!AddDialogData(dialog, &extraData, sizeof(extraData))) {
  174. return SDL_FALSE;
  175. }
  176. ++dialog->lpDialog->cDlgItems;
  177. return SDL_TRUE;
  178. }
  179. static SDL_bool AddDialogStatic(WIN_DialogData *dialog, int x, int y, int w, int h, const char *text)
  180. {
  181. DWORD style = WS_VISIBLE | WS_CHILD | SS_LEFT | SS_NOPREFIX | SS_EDITCONTROL;
  182. return AddDialogControl(dialog, 0x0082, style, 0, x, y, w, h, -1, text);
  183. }
  184. static SDL_bool AddDialogButton(WIN_DialogData *dialog, int x, int y, int w, int h, const char *text, int id, SDL_bool isDefault)
  185. {
  186. DWORD style = WS_VISIBLE | WS_CHILD;
  187. if (isDefault) {
  188. style |= BS_DEFPUSHBUTTON;
  189. } else {
  190. style |= BS_PUSHBUTTON;
  191. }
  192. return AddDialogControl(dialog, 0x0080, style, 0, x, y, w, h, id, text);
  193. }
  194. static void FreeDialogData(WIN_DialogData *dialog)
  195. {
  196. SDL_free(dialog->data);
  197. SDL_free(dialog);
  198. }
  199. static WIN_DialogData *CreateDialogData(int w, int h, const char *caption)
  200. {
  201. WIN_DialogData *dialog;
  202. DLGTEMPLATEEX dialogTemplate;
  203. WORD WordToPass;
  204. SDL_zero(dialogTemplate);
  205. dialogTemplate.dlgVer = 1;
  206. dialogTemplate.signature = 0xffff;
  207. dialogTemplate.style = (WS_CAPTION | DS_CENTER | DS_SHELLFONT);
  208. dialogTemplate.x = 0;
  209. dialogTemplate.y = 0;
  210. dialogTemplate.cx = w;
  211. dialogTemplate.cy = h;
  212. Vec2ToDLU(&dialogTemplate.cx, &dialogTemplate.cy);
  213. dialog = (WIN_DialogData *)SDL_calloc(1, sizeof(*dialog));
  214. if (!dialog) {
  215. return NULL;
  216. }
  217. if (!AddDialogData(dialog, &dialogTemplate, sizeof(dialogTemplate))) {
  218. FreeDialogData(dialog);
  219. return NULL;
  220. }
  221. /* No menu */
  222. WordToPass = 0;
  223. if (!AddDialogData(dialog, &WordToPass, 2)) {
  224. FreeDialogData(dialog);
  225. return NULL;
  226. }
  227. /* No custom class */
  228. if (!AddDialogData(dialog, &WordToPass, 2)) {
  229. FreeDialogData(dialog);
  230. return NULL;
  231. }
  232. /* title */
  233. if (!AddDialogString(dialog, caption)) {
  234. FreeDialogData(dialog);
  235. return NULL;
  236. }
  237. /* Font stuff */
  238. {
  239. /*
  240. * We want to use the system messagebox font.
  241. */
  242. BYTE ToPass;
  243. NONCLIENTMETRICSA NCM;
  244. NCM.cbSize = sizeof(NCM);
  245. SystemParametersInfoA(SPI_GETNONCLIENTMETRICS, 0, &NCM, 0);
  246. /* Font size - convert to logical font size for dialog parameter. */
  247. {
  248. HDC ScreenDC = GetDC(0);
  249. WordToPass = (WORD)(-72 * NCM.lfMessageFont.lfHeight / GetDeviceCaps(ScreenDC, LOGPIXELSY));
  250. ReleaseDC(0, ScreenDC);
  251. }
  252. if (!AddDialogData(dialog, &WordToPass, 2)) {
  253. FreeDialogData(dialog);
  254. return NULL;
  255. }
  256. /* Font weight */
  257. WordToPass = (WORD)NCM.lfMessageFont.lfWeight;
  258. if (!AddDialogData(dialog, &WordToPass, 2)) {
  259. FreeDialogData(dialog);
  260. return NULL;
  261. }
  262. /* italic? */
  263. ToPass = NCM.lfMessageFont.lfItalic;
  264. if (!AddDialogData(dialog, &ToPass, 1)) {
  265. FreeDialogData(dialog);
  266. return NULL;
  267. }
  268. /* charset? */
  269. ToPass = NCM.lfMessageFont.lfCharSet;
  270. if (!AddDialogData(dialog, &ToPass, 1)) {
  271. FreeDialogData(dialog);
  272. return NULL;
  273. }
  274. /* font typeface. */
  275. if (!AddDialogString(dialog, NCM.lfMessageFont.lfFaceName)) {
  276. FreeDialogData(dialog);
  277. return NULL;
  278. }
  279. }
  280. return dialog;
  281. }
  282. int
  283. WIN_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
  284. {
  285. WIN_DialogData *dialog;
  286. int i, x, y;
  287. UINT_PTR which;
  288. const SDL_MessageBoxButtonData *buttons = messageboxdata->buttons;
  289. HFONT DialogFont;
  290. SIZE Size;
  291. RECT TextSize;
  292. wchar_t* wmessage;
  293. TEXTMETRIC TM;
  294. const int ButtonWidth = 88;
  295. const int ButtonHeight = 26;
  296. const int TextMargin = 16;
  297. const int ButtonMargin = 12;
  298. /* Jan 25th, 2013 - dant@fleetsa.com
  299. *
  300. *
  301. * I've tried to make this more reasonable, but I've run in to a lot
  302. * of nonsense.
  303. *
  304. * The original issue is the code was written in pixels and not
  305. * dialog units (DLUs). All DialogBox functions use DLUs, which
  306. * vary based on the selected font (yay).
  307. *
  308. * According to MSDN, the most reliable way to convert is via
  309. * MapDialogUnits, which requires an HWND, which we don't have
  310. * at time of template creation.
  311. *
  312. * We do however have:
  313. * The system font (DLU width 8 for me)
  314. * The font we select for the dialog (DLU width 6 for me)
  315. *
  316. * Based on experimentation, *neither* of these return the value
  317. * actually used. Stepping in to MapDialogUnits(), the conversion
  318. * is fairly clear, and uses 7 for me.
  319. *
  320. * As a result, some of this is hacky to ensure the sizing is
  321. * somewhat correct.
  322. *
  323. * Honestly, a long term solution is to use CreateWindow, not CreateDialog.
  324. *
  325. *
  326. * In order to get text dimensions we need to have a DC with the desired font.
  327. * I'm assuming a dialog box in SDL is rare enough we can to the create.
  328. */
  329. HDC FontDC = CreateCompatibleDC(0);
  330. {
  331. /* Create a duplicate of the font used in system message boxes. */
  332. LOGFONT lf;
  333. NONCLIENTMETRICS NCM;
  334. NCM.cbSize = sizeof(NCM);
  335. SystemParametersInfo(SPI_GETNONCLIENTMETRICS, 0, &NCM, 0);
  336. lf = NCM.lfMessageFont;
  337. DialogFont = CreateFontIndirect(&lf);
  338. }
  339. /* Select the font in to our DC */
  340. SelectObject(FontDC, DialogFont);
  341. {
  342. /* Get the metrics to try and figure our DLU conversion. */
  343. GetTextMetrics(FontDC, &TM);
  344. s_BaseUnitsX = TM.tmAveCharWidth + 1;
  345. s_BaseUnitsY = TM.tmHeight;
  346. }
  347. /* Measure the *pixel* size of the string. */
  348. wmessage = WIN_UTF8ToString(messageboxdata->message);
  349. SDL_zero(TextSize);
  350. Size.cx = DrawText(FontDC, wmessage, -1, &TextSize, DT_CALCRECT);
  351. /* Add some padding for hangs, etc. */
  352. TextSize.right += 2;
  353. TextSize.bottom += 2;
  354. /* Done with the DC, and the string */
  355. DeleteDC(FontDC);
  356. SDL_free(wmessage);
  357. /* Increase the size of the dialog by some border spacing around the text. */
  358. Size.cx = TextSize.right - TextSize.left;
  359. Size.cy = TextSize.bottom - TextSize.top;
  360. Size.cx += TextMargin * 2;
  361. Size.cy += TextMargin * 2;
  362. /* Ensure the size is wide enough for all of the buttons. */
  363. if (Size.cx < messageboxdata->numbuttons * (ButtonWidth + ButtonMargin) + ButtonMargin)
  364. Size.cx = messageboxdata->numbuttons * (ButtonWidth + ButtonMargin) + ButtonMargin;
  365. /* Add vertical space for the buttons and border. */
  366. Size.cy += ButtonHeight + TextMargin;
  367. dialog = CreateDialogData(Size.cx, Size.cy, messageboxdata->title);
  368. if (!dialog) {
  369. return -1;
  370. }
  371. if (!AddDialogStatic(dialog, TextMargin, TextMargin, TextSize.right - TextSize.left, TextSize.bottom - TextSize.top, messageboxdata->message)) {
  372. FreeDialogData(dialog);
  373. return -1;
  374. }
  375. /* Align the buttons to the right/bottom. */
  376. x = Size.cx - ButtonWidth - ButtonMargin;
  377. y = Size.cy - ButtonHeight - ButtonMargin;
  378. for (i = 0; i < messageboxdata->numbuttons; ++i) {
  379. SDL_bool isDefault;
  380. if (buttons[i].flags & SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT) {
  381. isDefault = SDL_TRUE;
  382. } else {
  383. isDefault = SDL_FALSE;
  384. }
  385. if (!AddDialogButton(dialog, x, y, ButtonWidth, ButtonHeight, buttons[i].text, i, isDefault)) {
  386. FreeDialogData(dialog);
  387. return -1;
  388. }
  389. x -= ButtonWidth + ButtonMargin;
  390. }
  391. /* FIXME: If we have a parent window, get the Instance and HWND for them */
  392. which = DialogBoxIndirect(NULL, (DLGTEMPLATE*)dialog->lpDialog, NULL, (DLGPROC)MessageBoxDialogProc);
  393. *buttonid = buttons[which].buttonid;
  394. FreeDialogData(dialog);
  395. return 0;
  396. }
  397. #endif /* SDL_VIDEO_DRIVER_WINDOWS */
  398. /* vi: set ts=4 sw=4 expandtab: */