src/video/windows/SDL_windowsmessagebox.c
changeset 11937 f21a2cd69acf
parent 11920 c8b4a5166613
child 12042 23bc0cb86dc6
     1.1 --- a/src/video/windows/SDL_windowsmessagebox.c	Mon Mar 19 14:52:53 2018 -0700
     1.2 +++ b/src/video/windows/SDL_windowsmessagebox.c	Sat Mar 24 10:26:40 2018 -0700
     1.3 @@ -22,6 +22,14 @@
     1.4  
     1.5  #if SDL_VIDEO_DRIVER_WINDOWS
     1.6  
     1.7 +#ifdef HAVE_LIMITS_H
     1.8 +#include <limits.h>
     1.9 +#else
    1.10 +#ifndef SIZE_MAX
    1.11 +#define SIZE_MAX ((size_t)-1)
    1.12 +#endif
    1.13 +#endif
    1.14 +
    1.15  #include "../../core/windows/SDL_windows.h"
    1.16  
    1.17  #include "SDL_assert.h"
    1.18 @@ -32,6 +40,34 @@
    1.19  #define SS_EDITCONTROL  0x2000
    1.20  #endif
    1.21  
    1.22 +#ifndef IDOK
    1.23 +#define IDOK 1
    1.24 +#endif
    1.25 +
    1.26 +#ifndef IDCANCEL
    1.27 +#define IDCANCEL 2
    1.28 +#endif
    1.29 +
    1.30 +/* Custom dialog return codes */
    1.31 +#define IDCLOSED 20
    1.32 +#define IDINVALPTRINIT 50
    1.33 +#define IDINVALPTRCOMMAND 51
    1.34 +#define IDINVALPTRSETFOCUS 52
    1.35 +#define IDINVALPTRDLGITEM 53
    1.36 +/* First button ID */
    1.37 +#define IDBUTTONINDEX0 100
    1.38 +
    1.39 +#define DLGITEMTYPEBUTTON 0x0080
    1.40 +#define DLGITEMTYPESTATIC 0x0082
    1.41 +
    1.42 +/* Windows only sends the lower 16 bits of the control ID when a button
    1.43 + * gets clicked. There are also some predefined and custom IDs that lower
    1.44 + * the available number further. 2^16 - 101 buttons should be enough for
    1.45 + * everyone, no need to make the code more complex.
    1.46 + */
    1.47 +#define MAX_BUTTONS (0xffff - 100)
    1.48 +
    1.49 +
    1.50  /* Display a Windows message box */
    1.51  
    1.52  #pragma pack(push, 1)
    1.53 @@ -70,15 +106,79 @@
    1.54      Uint8 *data;
    1.55      size_t size;
    1.56      size_t used;
    1.57 +    WORD numbuttons;
    1.58  } WIN_DialogData;
    1.59  
    1.60 +static SDL_bool GetButtonIndex(const SDL_MessageBoxData *messageboxdata, Uint32 flags, size_t *i)
    1.61 +{
    1.62 +    for (*i = 0; *i < (size_t)messageboxdata->numbuttons; ++*i) {
    1.63 +        if (messageboxdata->buttons[*i].flags & flags) {
    1.64 +            return SDL_TRUE;
    1.65 +        }
    1.66 +    }
    1.67 +    return SDL_FALSE;
    1.68 +}
    1.69  
    1.70  static INT_PTR MessageBoxDialogProc(HWND hDlg, UINT iMessage, WPARAM wParam, LPARAM lParam)
    1.71  {
    1.72 +    const SDL_MessageBoxData *messageboxdata;
    1.73 +    size_t buttonindex;
    1.74 +
    1.75      switch ( iMessage ) {
    1.76 +    case WM_INITDIALOG:
    1.77 +        if (lParam == 0) {
    1.78 +            EndDialog(hDlg, IDINVALPTRINIT);
    1.79 +            return TRUE;
    1.80 +        }
    1.81 +        messageboxdata = (const SDL_MessageBoxData *)lParam;
    1.82 +        SetWindowLongPtr(hDlg, GWLP_USERDATA, lParam);
    1.83 +
    1.84 +        if (GetButtonIndex(messageboxdata, SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, &buttonindex)) {
    1.85 +            /* Focus on the first default return-key button */
    1.86 +            HWND buttonctl = GetDlgItem(hDlg, (int)(IDBUTTONINDEX0 + buttonindex));
    1.87 +            if (buttonctl == NULL) {
    1.88 +                EndDialog(hDlg, IDINVALPTRDLGITEM);
    1.89 +            }
    1.90 +            PostMessage(hDlg, WM_NEXTDLGCTL, (WPARAM)buttonctl, TRUE);
    1.91 +        } else {
    1.92 +            /* Give the focus to the dialog window instead */
    1.93 +            SetFocus(hDlg);
    1.94 +        }
    1.95 +        return FALSE;
    1.96 +    case WM_SETFOCUS:
    1.97 +        messageboxdata = (const SDL_MessageBoxData *)GetWindowLongPtr(hDlg, GWLP_USERDATA);
    1.98 +        if (messageboxdata == NULL) {
    1.99 +            EndDialog(hDlg, IDINVALPTRSETFOCUS);
   1.100 +            return TRUE;
   1.101 +        }
   1.102 +
   1.103 +        /* Let the default button be focused if there is one. Otherwise, prevent any initial focus. */
   1.104 +        if (GetButtonIndex(messageboxdata, SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, &buttonindex)) {
   1.105 +            return FALSE;
   1.106 +        }
   1.107 +        return TRUE;
   1.108      case WM_COMMAND:
   1.109 +        messageboxdata = (const SDL_MessageBoxData *)GetWindowLongPtr(hDlg, GWLP_USERDATA);
   1.110 +        if (messageboxdata == NULL) {
   1.111 +            EndDialog(hDlg, IDINVALPTRCOMMAND);
   1.112 +            return TRUE;
   1.113 +        }
   1.114 +
   1.115          /* Return the ID of the button that was pushed */
   1.116 -        EndDialog(hDlg, LOWORD(wParam));
   1.117 +        if (wParam == IDOK) {
   1.118 +            if (GetButtonIndex(messageboxdata, SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, &buttonindex)) {
   1.119 +                EndDialog(hDlg, IDBUTTONINDEX0 + buttonindex);
   1.120 +            }
   1.121 +        } else if (wParam == IDCANCEL) {
   1.122 +            if (GetButtonIndex(messageboxdata, SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, &buttonindex)) {
   1.123 +                EndDialog(hDlg, IDBUTTONINDEX0 + buttonindex);
   1.124 +            } else {
   1.125 +                /* Closing of window was requested by user or system. It would be rude not to comply. */
   1.126 +                EndDialog(hDlg, IDCLOSED);
   1.127 +            }
   1.128 +        } else if (wParam >= IDBUTTONINDEX0 && (int)wParam - IDBUTTONINDEX0 < messageboxdata->numbuttons) {
   1.129 +            EndDialog(hDlg, wParam);
   1.130 +        }
   1.131          return TRUE;
   1.132  
   1.133      default:
   1.134 @@ -89,15 +189,30 @@
   1.135  
   1.136  static SDL_bool ExpandDialogSpace(WIN_DialogData *dialog, size_t space)
   1.137  {
   1.138 +    /* Growing memory in 64 KiB steps. */
   1.139 +    const size_t sizestep = 0x10000;
   1.140      size_t size = dialog->size;
   1.141  
   1.142      if (size == 0) {
   1.143 -        size = space;
   1.144 -    } else {
   1.145 -        while ((dialog->used + space) > size) {
   1.146 -            size *= 2;
   1.147 +        /* Start with 4 KiB or a multiple of 64 KiB to fit the data. */
   1.148 +        size = 0x1000;
   1.149 +        if (SIZE_MAX - sizestep < space) {
   1.150 +            size = space;
   1.151 +        } else if (space > size) {
   1.152 +            size = (space + sizestep) & ~(sizestep - 1);
   1.153          }
   1.154 +    } else if (SIZE_MAX - dialog->used < space) {
   1.155 +        SDL_OutOfMemory();
   1.156 +        return SDL_FALSE;
   1.157 +    } else if (SIZE_MAX - (dialog->used + space) < sizestep) {
   1.158 +        /* Close to the maximum. */
   1.159 +        size = dialog->used + space;
   1.160 +    } else if (size < dialog->used + space) {
   1.161 +        /* Round up to the next 64 KiB block. */
   1.162 +        size = dialog->used + space;
   1.163 +        size += sizestep - size % sizestep;
   1.164      }
   1.165 +
   1.166      if (size > dialog->size) {
   1.167          void *data = SDL_realloc(dialog->data, size);
   1.168          if (!data) {
   1.169 @@ -175,7 +290,7 @@
   1.170  }
   1.171  
   1.172  
   1.173 -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)
   1.174 +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)
   1.175  {
   1.176      DLGITEMTEMPLATEEX item;
   1.177      WORD marker = 0xFFFF;
   1.178 @@ -205,32 +320,54 @@
   1.179      if (!AddDialogData(dialog, &type, sizeof(type))) {
   1.180          return SDL_FALSE;
   1.181      }
   1.182 -    if (!AddDialogString(dialog, caption)) {
   1.183 -        return SDL_FALSE;
   1.184 +    if (type == DLGITEMTYPEBUTTON || (type == DLGITEMTYPESTATIC && caption != NULL)) {
   1.185 +        if (!AddDialogString(dialog, caption)) {
   1.186 +            return SDL_FALSE;
   1.187 +        }
   1.188 +    } else {
   1.189 +        if (!AddDialogData(dialog, &marker, sizeof(marker))) {
   1.190 +            return SDL_FALSE;
   1.191 +        }
   1.192 +        if (!AddDialogData(dialog, &ordinal, sizeof(ordinal))) {
   1.193 +            return SDL_FALSE;
   1.194 +        }
   1.195      }
   1.196      if (!AddDialogData(dialog, &extraData, sizeof(extraData))) {
   1.197          return SDL_FALSE;
   1.198      }
   1.199 +    if (type == DLGITEMTYPEBUTTON) {
   1.200 +        dialog->numbuttons++;
   1.201 +    }
   1.202      ++dialog->lpDialog->cDlgItems;
   1.203  
   1.204      return SDL_TRUE;
   1.205  }
   1.206  
   1.207 -static SDL_bool AddDialogStatic(WIN_DialogData *dialog, int x, int y, int w, int h, const char *text)
   1.208 +static SDL_bool AddDialogStaticText(WIN_DialogData *dialog, int x, int y, int w, int h, const char *text)
   1.209  {
   1.210 -    DWORD style = WS_VISIBLE | WS_CHILD | SS_LEFT | SS_NOPREFIX | SS_EDITCONTROL;
   1.211 -    return AddDialogControl(dialog, 0x0082, style, 0, x, y, w, h, -1, text);
   1.212 +    DWORD style = WS_VISIBLE | WS_CHILD | SS_LEFT | SS_NOPREFIX | SS_EDITCONTROL | WS_GROUP;
   1.213 +    return AddDialogControl(dialog, DLGITEMTYPESTATIC, style, 0, x, y, w, h, -1, text, 0);
   1.214 +}
   1.215 +
   1.216 +static SDL_bool AddDialogStaticIcon(WIN_DialogData *dialog, int x, int y, int w, int h, Uint16 ordinal)
   1.217 +{
   1.218 +    DWORD style = WS_VISIBLE | WS_CHILD | SS_ICON | WS_GROUP;
   1.219 +    return AddDialogControl(dialog, DLGITEMTYPESTATIC, style, 0, x, y, w, h, -2, NULL, ordinal);
   1.220  }
   1.221  
   1.222  static SDL_bool AddDialogButton(WIN_DialogData *dialog, int x, int y, int w, int h, const char *text, int id, SDL_bool isDefault)
   1.223  {
   1.224 -    DWORD style = WS_VISIBLE | WS_CHILD;
   1.225 +    DWORD style = WS_VISIBLE | WS_CHILD | WS_TABSTOP;
   1.226      if (isDefault) {
   1.227          style |= BS_DEFPUSHBUTTON;
   1.228      } else {
   1.229          style |= BS_PUSHBUTTON;
   1.230      }
   1.231 -    return AddDialogControl(dialog, 0x0080, style, 0, x, y, w, h, id, text);
   1.232 +    /* The first button marks the start of the group. */
   1.233 +    if (dialog->numbuttons == 0) {
   1.234 +        style |= WS_GROUP;
   1.235 +    }
   1.236 +    return AddDialogControl(dialog, DLGITEMTYPEBUTTON, style, 0, x, y, w, h, IDBUTTONINDEX0 + dialog->numbuttons, text, 0);
   1.237  }
   1.238  
   1.239  static void FreeDialogData(WIN_DialogData *dialog)
   1.240 @@ -341,18 +478,87 @@
   1.241      return dialog;
   1.242  }
   1.243  
   1.244 +/* Escaping ampersands is necessary to disable mnemonics in dialog controls.
   1.245 + * The caller provides a char** for dst and a size_t* for dstlen where the
   1.246 + * address of the work buffer and its size will be stored. Their values must be
   1.247 + * NULL and 0 on the first call. src is the string to be escaped. On error, the
   1.248 + * function returns NULL and, on success, returns a pointer to the escaped
   1.249 + * sequence as a read-only string that is valid until the next call or until the
   1.250 + * work buffer is freed. Once all strings have been processed, it's the caller's
   1.251 + * responsibilty to free the work buffer with SDL_free, even on errors.
   1.252 + */
   1.253 +static const char *EscapeAmpersands(char **dst, size_t *dstlen, const char *src)
   1.254 +{
   1.255 +    char *newdst;
   1.256 +    size_t ampcount = 0;
   1.257 +    size_t srclen = 0;
   1.258 +
   1.259 +    if (src == NULL) {
   1.260 +        return NULL;
   1.261 +    }
   1.262 +
   1.263 +    while (src[srclen]) {
   1.264 +        if (src[srclen] == '&') {
   1.265 +            ampcount++;
   1.266 +        }
   1.267 +        srclen++;
   1.268 +    }
   1.269 +    srclen++;
   1.270 +
   1.271 +    if (ampcount == 0) {
   1.272 +        /* Nothing to do. */
   1.273 +        return src;
   1.274 +    }
   1.275 +    if (SIZE_MAX - srclen < ampcount) {
   1.276 +        return NULL;
   1.277 +    }
   1.278 +    if (*dst == NULL || *dstlen < srclen + ampcount) {
   1.279 +        /* Allocating extra space in case the next strings are a bit longer. */
   1.280 +        size_t extraspace = SIZE_MAX - (srclen + ampcount);
   1.281 +        if (extraspace > 512) {
   1.282 +            extraspace = 512;
   1.283 +        }
   1.284 +        *dstlen = srclen + ampcount + extraspace;
   1.285 +        SDL_free(*dst);
   1.286 +        *dst = NULL;
   1.287 +        newdst = SDL_malloc(*dstlen);
   1.288 +        if (newdst == NULL) {
   1.289 +            return NULL;
   1.290 +        }
   1.291 +        *dst = newdst;
   1.292 +    } else {
   1.293 +        newdst = *dst;
   1.294 +    }
   1.295 +
   1.296 +    /* The escape character is the ampersand itself. */
   1.297 +    while (srclen--) {
   1.298 +        if (*src == '&') {
   1.299 +            *newdst++ = '&';
   1.300 +        }
   1.301 +        *newdst++ = *src++;
   1.302 +    }
   1.303 +
   1.304 +    return *dst;
   1.305 +}
   1.306 +
   1.307  /* This function is called if a Task Dialog is unsupported. */
   1.308  static int
   1.309  WIN_ShowOldMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
   1.310  {
   1.311      WIN_DialogData *dialog;
   1.312 -    int i, x, y;
   1.313 +    int i, x, y, retval;
   1.314      const SDL_MessageBoxButtonData *buttons = messageboxdata->buttons;
   1.315      HFONT DialogFont;
   1.316      SIZE Size;
   1.317      RECT TextSize;
   1.318      wchar_t* wmessage;
   1.319      TEXTMETRIC TM;
   1.320 +    HDC FontDC;
   1.321 +    INT_PTR result;
   1.322 +    char *ampescape = NULL;
   1.323 +    size_t ampescapesize = 0;
   1.324 +    Uint16 defbuttoncount = 0;
   1.325 +    Uint16 icon = 0;
   1.326  
   1.327      HWND ParentWindow = NULL;
   1.328  
   1.329 @@ -360,7 +566,25 @@
   1.330      const int ButtonHeight = 26;
   1.331      const int TextMargin = 16;
   1.332      const int ButtonMargin = 12;
   1.333 +    const int IconWidth = GetSystemMetrics(SM_CXICON);
   1.334 +    const int IconHeight = GetSystemMetrics(SM_CYICON);
   1.335 +    const int IconMargin = 20;
   1.336  
   1.337 +    if (messageboxdata->numbuttons > MAX_BUTTONS) {
   1.338 +        return SDL_SetError("Number of butons exceeds limit of %d", MAX_BUTTONS);
   1.339 +    }
   1.340 +
   1.341 +    switch (messageboxdata->flags) {
   1.342 +    case SDL_MESSAGEBOX_ERROR:
   1.343 +        icon = (Uint16)IDI_ERROR;
   1.344 +        break;
   1.345 +    case SDL_MESSAGEBOX_WARNING:
   1.346 +        icon = (Uint16)IDI_WARNING;
   1.347 +        break;
   1.348 +    case SDL_MESSAGEBOX_INFORMATION:
   1.349 +        icon = (Uint16)IDI_INFORMATION;
   1.350 +        break;
   1.351 +    }
   1.352  
   1.353      /* Jan 25th, 2013 - dant@fleetsa.com
   1.354       *
   1.355 @@ -394,7 +618,7 @@
   1.356       * In order to get text dimensions we need to have a DC with the desired font.
   1.357       * I'm assuming a dialog box in SDL is rare enough we can to the create.
   1.358       */
   1.359 -    HDC FontDC = CreateCompatibleDC(0);
   1.360 +    FontDC = CreateCompatibleDC(0);
   1.361  
   1.362      {
   1.363          /* Create a duplicate of the font used in system message boxes. */
   1.364 @@ -429,11 +653,13 @@
   1.365      /* Measure the *pixel* size of the string. */
   1.366      wmessage = WIN_UTF8ToString(messageboxdata->message);
   1.367      SDL_zero(TextSize);
   1.368 -    DrawText(FontDC, wmessage, -1, &TextSize, DT_CALCRECT);
   1.369 +    DrawText(FontDC, wmessage, -1, &TextSize, DT_CALCRECT | DT_LEFT | DT_NOPREFIX | DT_EDITCONTROL);
   1.370  
   1.371 -    /* Add some padding for hangs, etc. */
   1.372 -    TextSize.right += 2;
   1.373 -    TextSize.bottom += 2;
   1.374 +    /* Add margins and some padding for hangs, etc. */
   1.375 +    TextSize.left += TextMargin;
   1.376 +    TextSize.right += TextMargin + 2;
   1.377 +    TextSize.top += TextMargin;
   1.378 +    TextSize.bottom += TextMargin + 2;
   1.379  
   1.380      /* Done with the DC, and the string */
   1.381      DeleteDC(FontDC);
   1.382 @@ -445,10 +671,22 @@
   1.383      Size.cx += TextMargin * 2;
   1.384      Size.cy += TextMargin * 2;
   1.385  
   1.386 +    /* Make dialog wider and shift text over for the icon. */
   1.387 +    if (icon) {
   1.388 +        Size.cx += IconMargin + IconWidth;
   1.389 +        TextSize.left += IconMargin + IconWidth;
   1.390 +        TextSize.right += IconMargin + IconWidth;
   1.391 +    }
   1.392 +
   1.393      /* Ensure the size is wide enough for all of the buttons. */
   1.394      if (Size.cx < messageboxdata->numbuttons * (ButtonWidth + ButtonMargin) + ButtonMargin)
   1.395          Size.cx = messageboxdata->numbuttons * (ButtonWidth + ButtonMargin) + ButtonMargin;
   1.396  
   1.397 +    /* Reset the height to the icon size if it is actually bigger than the text. */
   1.398 +    if (icon && Size.cy < IconMargin * 2 + IconHeight) {
   1.399 +        Size.cy = IconMargin * 2 + IconHeight;
   1.400 +    }
   1.401 +
   1.402      /* Add vertical space for the buttons and border. */
   1.403      Size.cy += ButtonHeight + TextMargin;
   1.404  
   1.405 @@ -457,7 +695,12 @@
   1.406          return -1;
   1.407      }
   1.408  
   1.409 -    if (!AddDialogStatic(dialog, TextMargin, TextMargin, TextSize.right - TextSize.left, TextSize.bottom - TextSize.top, messageboxdata->message)) {
   1.410 +    if (icon && ! AddDialogStaticIcon(dialog, IconMargin, IconMargin, IconWidth, IconHeight, icon)) {
   1.411 +        FreeDialogData(dialog);
   1.412 +        return -1;
   1.413 +    }
   1.414 +
   1.415 +    if (!AddDialogStaticText(dialog, TextSize.left, TextSize.top, TextSize.right - TextSize.left, TextSize.bottom - TextSize.top, messageboxdata->message)) {
   1.416          FreeDialogData(dialog);
   1.417          return -1;
   1.418      }
   1.419 @@ -466,19 +709,25 @@
   1.420      x = Size.cx - (ButtonWidth + ButtonMargin) * messageboxdata->numbuttons;
   1.421      y = Size.cy - ButtonHeight - ButtonMargin;
   1.422      for (i = messageboxdata->numbuttons - 1; i >= 0; --i) {
   1.423 -        SDL_bool isDefault;
   1.424 +        SDL_bool isdefault = SDL_FALSE;
   1.425 +        const char *buttontext;
   1.426  
   1.427          if (buttons[i].flags & SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT) {
   1.428 -            isDefault = SDL_TRUE;
   1.429 -        } else {
   1.430 -            isDefault = SDL_FALSE;
   1.431 +            defbuttoncount++;
   1.432 +            if (defbuttoncount == 1) {
   1.433 +                isdefault = SDL_TRUE;
   1.434 +            }
   1.435          }
   1.436 -        if (!AddDialogButton(dialog, x, y, ButtonWidth, ButtonHeight, buttons[i].text, buttons[i].buttonid, isDefault)) {
   1.437 +
   1.438 +        buttontext = EscapeAmpersands(&ampescape, &ampescapesize, buttons[i].text);
   1.439 +        if (buttontext == NULL || !AddDialogButton(dialog, x, y, ButtonWidth, ButtonHeight, buttontext, buttons[i].buttonid, isdefault)) {
   1.440              FreeDialogData(dialog);
   1.441 +            SDL_free(ampescape);
   1.442              return -1;
   1.443          }
   1.444          x += ButtonWidth + ButtonMargin;
   1.445      }
   1.446 +    SDL_free(ampescape);
   1.447  
   1.448      /* If we have a parent window, get the Instance and HWND for them
   1.449       * so that our little dialog gets exclusive focus at all times. */
   1.450 @@ -486,10 +735,32 @@
   1.451          ParentWindow = ((SDL_WindowData*)messageboxdata->window->driverdata)->hwnd;
   1.452      }
   1.453  
   1.454 -    *buttonid = (int)DialogBoxIndirect(NULL, (DLGTEMPLATE*)dialog->lpDialog, ParentWindow, (DLGPROC)MessageBoxDialogProc);
   1.455 +    result = DialogBoxIndirectParam(NULL, (DLGTEMPLATE*)dialog->lpDialog, ParentWindow, (DLGPROC)MessageBoxDialogProc, (LPARAM)messageboxdata);
   1.456 +    if (result >= IDBUTTONINDEX0 && result - IDBUTTONINDEX0 < messageboxdata->numbuttons) {
   1.457 +        *buttonid = messageboxdata->buttons[(messageboxdata->numbuttons - 1) - (result - IDBUTTONINDEX0)].buttonid;
   1.458 +        retval = 0;
   1.459 +    } else if (result == IDCLOSED) {
   1.460 +        /* Dialog window closed by user or system. */
   1.461 +        /* This could use a special return code. */
   1.462 +        retval = 0;
   1.463 +        *buttonid = -1;
   1.464 +    } else {
   1.465 +        if (result == 0) {
   1.466 +            SDL_SetError("Invalid parent window handle");
   1.467 +        } else if (result == -1) {
   1.468 +            SDL_SetError("The message box encountered an error.");
   1.469 +        } else if (result == IDINVALPTRINIT || result == IDINVALPTRSETFOCUS || result == IDINVALPTRCOMMAND) {
   1.470 +            SDL_SetError("Invalid message box pointer in dialog procedure");
   1.471 +        } else if (result == IDINVALPTRDLGITEM) {
   1.472 +            SDL_SetError("Couldn't find dialog control of the default enter-key button");
   1.473 +        } else {
   1.474 +            SDL_SetError("An unknown error occured");
   1.475 +        }
   1.476 +        retval = -1;
   1.477 +    }
   1.478  
   1.479      FreeDialogData(dialog);
   1.480 -    return 0;
   1.481 +    return retval;
   1.482  }
   1.483  
   1.484  /* TaskDialogIndirect procedure
   1.485 @@ -509,16 +780,22 @@
   1.486      HMODULE hComctl32;
   1.487      TASKDIALOGINDIRECTPROC pfnTaskDialogIndirect;
   1.488      HRESULT hr;
   1.489 +    char *ampescape = NULL;
   1.490 +    size_t ampescapesize = 0;
   1.491      int nButton;
   1.492      int nCancelButton;
   1.493      int i;
   1.494  
   1.495 +    if (SIZE_MAX / sizeof(TASKDIALOG_BUTTON) < messageboxdata->numbuttons) {
   1.496 +        return SDL_OutOfMemory();
   1.497 +    }
   1.498 +
   1.499      /* If we cannot load comctl32.dll use the old messagebox! */
   1.500      hComctl32 = LoadLibrary(TEXT("Comctl32.dll"));
   1.501      if (hComctl32 == NULL) {
   1.502 -        return WIN_ShowOldMessageBox(messageboxdata,buttonid);
   1.503 +        return WIN_ShowOldMessageBox(messageboxdata, buttonid);
   1.504      }
   1.505 -    
   1.506 +
   1.507      /* If TaskDialogIndirect doesn't exist use the old messagebox!
   1.508         This will fail prior to Windows Vista.
   1.509         The manifest file in the application may require targeting version 6 of comctl32.dll, even
   1.510 @@ -563,6 +840,7 @@
   1.511      nCancelButton = 0;
   1.512      for (i = 0; i < messageboxdata->numbuttons; i++)
   1.513      {
   1.514 +        const char *buttontext;
   1.515          pButton = &pButtons[messageboxdata->numbuttons-1-i];
   1.516          if (messageboxdata->buttons[i].flags & SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT) {
   1.517              nCancelButton = messageboxdata->buttons[i].buttonid;
   1.518 @@ -573,7 +851,20 @@
   1.519                  pButton->nButtonID++;
   1.520              }
   1.521          }
   1.522 -        pButton->pszButtonText = WIN_UTF8ToString(messageboxdata->buttons[i].text);
   1.523 +        buttontext = EscapeAmpersands(&ampescape, &ampescapesize, messageboxdata->buttons[i].text);
   1.524 +        if (buttontext == NULL) {
   1.525 +            int j;
   1.526 +            FreeLibrary(hComctl32);
   1.527 +            SDL_free(ampescape);
   1.528 +            SDL_free(wmessage);
   1.529 +            SDL_free(wtitle);
   1.530 +            for (j = 0; j < i; j++) {
   1.531 +                SDL_free((wchar_t *) pButtons[j].pszButtonText);
   1.532 +            }
   1.533 +            SDL_free(pButtons);
   1.534 +            return -1;
   1.535 +        }
   1.536 +        pButton->pszButtonText = WIN_UTF8ToString(buttontext);
   1.537          if (messageboxdata->buttons[i].flags & SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT) {
   1.538              TaskConfig.nDefaultButton = pButton->nButtonID;
   1.539          }
   1.540 @@ -585,6 +876,7 @@
   1.541  
   1.542      /* Free everything */
   1.543      FreeLibrary(hComctl32);
   1.544 +    SDL_free(ampescape);
   1.545      SDL_free(wmessage);
   1.546      SDL_free(wtitle);
   1.547      for (i = 0; i < messageboxdata->numbuttons; i++) {