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