Fix bug where the wrong button was the default in the old message box because buttons were added backwards, breaking the indexing used by GetButtonIndex.
authorSam Lantinga <slouken@libsdl.org>
Fri, 02 Aug 2019 17:19:50 -0700
changeset 129822b00c2a7cec4
parent 12981 42160260ac58
child 12983 2b95ace6e7bd
Fix bug where the wrong button was the default in the old message box because buttons were added backwards, breaking the indexing used by GetButtonIndex.

Add messagebox flags to explicilty request left-to-right button order or right-to-left. If neither is specified it'll be some platform default.
Xcode/SDL/SDL.xcodeproj/project.pbxproj
include/SDL_messagebox.h
src/core/android/SDL_android.c
src/video/cocoa/SDL_cocoamessagebox.m
src/video/uikit/SDL_uikitmessagebox.m
src/video/windows/SDL_windowsmessagebox.c
src/video/winrt/SDL_winrtmessagebox.cpp
src/video/x11/SDL_x11messagebox.c
     1.1 --- a/include/SDL_messagebox.h	Fri Aug 02 17:12:49 2019 -0700
     1.2 +++ b/include/SDL_messagebox.h	Fri Aug 02 17:19:50 2019 -0700
     1.3 @@ -36,9 +36,11 @@
     1.4   */
     1.5  typedef enum
     1.6  {
     1.7 -    SDL_MESSAGEBOX_ERROR        = 0x00000010,   /**< error dialog */
     1.8 -    SDL_MESSAGEBOX_WARNING      = 0x00000020,   /**< warning dialog */
     1.9 -    SDL_MESSAGEBOX_INFORMATION  = 0x00000040    /**< informational dialog */
    1.10 +    SDL_MESSAGEBOX_ERROR                 = 0x00000010,   /**< error dialog */
    1.11 +    SDL_MESSAGEBOX_WARNING               = 0x00000020,   /**< warning dialog */
    1.12 +    SDL_MESSAGEBOX_INFORMATION           = 0x00000040,   /**< informational dialog */
    1.13 +    SDL_MESSAGEBOX_BUTTONS_LEFT_TO_RIGHT = 0x00000080,   /**< buttons placed left to right */
    1.14 +    SDL_MESSAGEBOX_BUTTONS_RIGHT_TO_LEFT = 0x00000100,   /**< buttons placed right to left */
    1.15  } SDL_MessageBoxFlags;
    1.16  
    1.17  /**
     2.1 --- a/src/core/android/SDL_android.c	Fri Aug 02 17:12:49 2019 -0700
     2.2 +++ b/src/core/android/SDL_android.c	Fri Aug 02 17:19:50 2019 -0700
     2.3 @@ -2380,11 +2380,19 @@
     2.4      button_texts = (*env)->NewObjectArray(env, messageboxdata->numbuttons,
     2.5          clazz, NULL);
     2.6      for (i = 0; i < messageboxdata->numbuttons; ++i) {
     2.7 -        temp = messageboxdata->buttons[i].flags;
     2.8 +        const SDL_MessageBoxButtonData *sdlButton;
     2.9 +
    2.10 +        if (messageboxdata->flags & SDL_MESSAGEBOX_BUTTONS_RIGHT_TO_LEFT) {
    2.11 +            sdlButton = &messageboxdata->buttons[messageboxdata->numbuttons - 1 - i];
    2.12 +        } else {
    2.13 +            sdlButton = &messageboxdata->buttons[i];
    2.14 +        }
    2.15 +
    2.16 +        temp = sdlButton->flags;
    2.17          (*env)->SetIntArrayRegion(env, button_flags, i, 1, &temp);
    2.18 -        temp = messageboxdata->buttons[i].buttonid;
    2.19 +        temp = sdlButton->buttonid;
    2.20          (*env)->SetIntArrayRegion(env, button_ids, i, 1, &temp);
    2.21 -        text = (*env)->NewStringUTF(env, messageboxdata->buttons[i].text);
    2.22 +        text = (*env)->NewStringUTF(env, sdlButton->text);
    2.23          (*env)->SetObjectArrayElement(env, button_texts, i, text);
    2.24          (*env)->DeleteLocalRef(env, text);
    2.25      }
     3.1 --- a/src/video/cocoa/SDL_cocoamessagebox.m	Fri Aug 02 17:12:49 2019 -0700
     3.2 +++ b/src/video/cocoa/SDL_cocoamessagebox.m	Fri Aug 02 17:19:50 2019 -0700
     3.3 @@ -112,10 +112,19 @@
     3.4      const SDL_MessageBoxButtonData *buttons = messageboxdata->buttons;
     3.5      int i;
     3.6      for (i = 0; i < messageboxdata->numbuttons; ++i) {
     3.7 -        NSButton *button = [alert addButtonWithTitle:[NSString stringWithUTF8String:buttons[i].text]];
     3.8 -        if (buttons[i].flags & SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT) {
     3.9 +        const SDL_MessageBoxButtonData *sdlButton;
    3.10 +		NSButton *button;
    3.11 +
    3.12 +        if (messageboxdata->flags & SDL_MESSAGEBOX_BUTTONS_RIGHT_TO_LEFT) {
    3.13 +            sdlButton = &messageboxdata->buttons[messageboxdata->numbuttons - 1 - i];
    3.14 +        } else {
    3.15 +            sdlButton = &messageboxdata->buttons[i];
    3.16 +        }
    3.17 +
    3.18 +        button = [alert addButtonWithTitle:[NSString stringWithUTF8String:sdlButton->text]];
    3.19 +        if (sdlButton->flags & SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT) {
    3.20              [button setKeyEquivalent:@"\r"];
    3.21 -        } else if (buttons[i].flags & SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT) {
    3.22 +        } else if (sdlButton->flags & SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT) {
    3.23              [button setKeyEquivalent:@"\033"];
    3.24          } else {
    3.25              [button setKeyEquivalent:@""];
    3.26 @@ -132,6 +141,9 @@
    3.27      NSInteger clicked = presenter->clicked;
    3.28      if (clicked >= NSAlertFirstButtonReturn) {
    3.29          clicked -= NSAlertFirstButtonReturn;
    3.30 +        if (messageboxdata->flags & SDL_MESSAGEBOX_BUTTONS_RIGHT_TO_LEFT) {
    3.31 +			clicked = messageboxdata->numbuttons - 1 - clicked;
    3.32 +		}
    3.33          *buttonid = buttons[clicked].buttonid;
    3.34      } else {
    3.35          returnValue = SDL_SetError("Did not get a valid `clicked button' id: %ld", (long)clicked);
     4.1 --- a/src/video/uikit/SDL_uikitmessagebox.m	Fri Aug 02 17:12:49 2019 -0700
     4.2 +++ b/src/video/uikit/SDL_uikitmessagebox.m	Fri Aug 02 17:19:50 2019 -0700
     4.3 @@ -73,15 +73,22 @@
     4.4      for (i = 0; i < messageboxdata->numbuttons; i++) {
     4.5          UIAlertAction *action;
     4.6          UIAlertActionStyle style = UIAlertActionStyleDefault;
     4.7 +        const SDL_MessageBoxButtonData *sdlButton;
     4.8  
     4.9 -        if (buttons[i].flags & SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT) {
    4.10 +        if (messageboxdata->flags & SDL_MESSAGEBOX_BUTTONS_RIGHT_TO_LEFT) {
    4.11 +            sdlButton = &messageboxdata->buttons[messageboxdata->numbuttons - 1 - i];
    4.12 +        } else {
    4.13 +            sdlButton = &messageboxdata->buttons[i];
    4.14 +        }
    4.15 +
    4.16 +        if (sdlButton->flags & SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT) {
    4.17              style = UIAlertActionStyleCancel;
    4.18          }
    4.19  
    4.20 -        action = [UIAlertAction actionWithTitle:@(buttons[i].text)
    4.21 +        action = [UIAlertAction actionWithTitle:@(sdlButton->text)
    4.22                                            style:style
    4.23                                          handler:^(UIAlertAction *action) {
    4.24 -                                            clickedindex = i;
    4.25 +				                            clickedindex = (int)(sdlButton - messageboxdata->buttons);
    4.26                                          }];
    4.27          [alert addAction:action];
    4.28      }
    4.29 @@ -150,7 +157,13 @@
    4.30      alert.message = @(messageboxdata->message);
    4.31  
    4.32      for (i = 0; i < messageboxdata->numbuttons; i++) {
    4.33 -        [alert addButtonWithTitle:@(buttons[i].text)];
    4.34 +        const SDL_MessageBoxButtonData *sdlButton;
    4.35 +        if (messageboxdata->flags & SDL_MESSAGEBOX_BUTTONS_RIGHT_TO_LEFT) {
    4.36 +            sdlButton = &messageboxdata->buttons[messageboxdata->numbuttons - 1 - i];
    4.37 +        } else {
    4.38 +            sdlButton = &messageboxdata->buttons[i];
    4.39 +        }
    4.40 +        [alert addButtonWithTitle:@(sdlButton->text)];
    4.41      }
    4.42  
    4.43      delegate.clickedIndex = &clickedindex;
    4.44 @@ -161,6 +174,9 @@
    4.45  
    4.46      alert.delegate = nil;
    4.47  
    4.48 +	if (messageboxdata->flags & SDL_MESSAGEBOX_BUTTONS_RIGHT_TO_LEFT) {
    4.49 +		clickedindex = messageboxdata->numbuttons - 1 - clickedindex;
    4.50 +	}
    4.51      *buttonid = messageboxdata->buttons[clickedindex].buttonid;
    4.52      return YES;
    4.53  #else
     5.1 --- a/src/video/windows/SDL_windowsmessagebox.c	Fri Aug 02 17:12:49 2019 -0700
     5.2 +++ b/src/video/windows/SDL_windowsmessagebox.c	Fri Aug 02 17:19:50 2019 -0700
     5.3 @@ -367,7 +367,7 @@
     5.4      if (dialog->numbuttons == 0) {
     5.5          style |= WS_GROUP;
     5.6      }
     5.7 -    return AddDialogControl(dialog, DLGITEMTYPEBUTTON, style, 0, x, y, w, h, IDBUTTONINDEX0 + dialog->numbuttons, text, 0);
     5.8 +    return AddDialogControl(dialog, DLGITEMTYPEBUTTON, style, 0, x, y, w, h, id, text, 0);
     5.9  }
    5.10  
    5.11  static void FreeDialogData(WIN_DialogData *dialog)
    5.12 @@ -708,23 +708,36 @@
    5.13      /* Align the buttons to the right/bottom. */
    5.14      x = Size.cx - (ButtonWidth + ButtonMargin) * messageboxdata->numbuttons;
    5.15      y = Size.cy - ButtonHeight - ButtonMargin;
    5.16 -    for (i = messageboxdata->numbuttons - 1; i >= 0; --i) {
    5.17 +    for (i = 0; i < messageboxdata->numbuttons; i++) {
    5.18          SDL_bool isdefault = SDL_FALSE;
    5.19          const char *buttontext;
    5.20 +        const SDL_MessageBoxButtonData *sdlButton;
    5.21  
    5.22 -        if (buttons[i].flags & SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT) {
    5.23 +        /* We always have to create the dialog buttons from left to right
    5.24 +         * so that the tab order is correct.  Select the info to use
    5.25 +         * depending on which order was requested. */
    5.26 +        if (messageboxdata->flags & SDL_MESSAGEBOX_BUTTONS_LEFT_TO_RIGHT) {
    5.27 +            sdlButton = &messageboxdata->buttons[i];
    5.28 +        } else {
    5.29 +            sdlButton = &messageboxdata->buttons[messageboxdata->numbuttons - 1 - i];
    5.30 +        }
    5.31 +
    5.32 +        if (sdlButton->flags & SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT) {
    5.33              defbuttoncount++;
    5.34              if (defbuttoncount == 1) {
    5.35                  isdefault = SDL_TRUE;
    5.36              }
    5.37          }
    5.38  
    5.39 -        buttontext = EscapeAmpersands(&ampescape, &ampescapesize, buttons[i].text);
    5.40 -        if (buttontext == NULL || !AddDialogButton(dialog, x, y, ButtonWidth, ButtonHeight, buttontext, buttons[i].buttonid, isdefault)) {
    5.41 +        buttontext = EscapeAmpersands(&ampescape, &ampescapesize, sdlButton->text);
    5.42 +        /* Make sure to provide the correct ID to keep buttons indexed in the
    5.43 +         * same order as how they are in messageboxdata. */
    5.44 +        if (buttontext == NULL || !AddDialogButton(dialog, x, y, ButtonWidth, ButtonHeight, buttontext, IDBUTTONINDEX0 + (int)(sdlButton - messageboxdata->buttons), isdefault)) {
    5.45              FreeDialogData(dialog);
    5.46              SDL_free(ampescape);
    5.47              return -1;
    5.48          }
    5.49 +
    5.50          x += ButtonWidth + ButtonMargin;
    5.51      }
    5.52      SDL_free(ampescape);
    5.53 @@ -737,7 +750,7 @@
    5.54  
    5.55      result = DialogBoxIndirectParam(NULL, (DLGTEMPLATE*)dialog->lpDialog, ParentWindow, (DLGPROC)MessageBoxDialogProc, (LPARAM)messageboxdata);
    5.56      if (result >= IDBUTTONINDEX0 && result - IDBUTTONINDEX0 < messageboxdata->numbuttons) {
    5.57 -        *buttonid = messageboxdata->buttons[(messageboxdata->numbuttons - 1) - (result - IDBUTTONINDEX0)].buttonid;
    5.58 +        *buttonid = messageboxdata->buttons[result - IDBUTTONINDEX0].buttonid;
    5.59          retval = 0;
    5.60      } else if (result == IDCLOSED) {
    5.61          /* Dialog window closed by user or system. */
    5.62 @@ -841,15 +854,16 @@
    5.63      for (i = 0; i < messageboxdata->numbuttons; i++)
    5.64      {
    5.65          const char *buttontext;
    5.66 -        pButton = &pButtons[messageboxdata->numbuttons-1-i];
    5.67 +        if (messageboxdata->flags & SDL_MESSAGEBOX_BUTTONS_LEFT_TO_RIGHT) {
    5.68 +            pButton = &pButtons[i];
    5.69 +        } else {
    5.70 +            pButton = &pButtons[messageboxdata->numbuttons - 1 - i];
    5.71 +        }
    5.72          if (messageboxdata->buttons[i].flags & SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT) {
    5.73              nCancelButton = messageboxdata->buttons[i].buttonid;
    5.74 -            pButton->nButtonID = 2;
    5.75 +            pButton->nButtonID = IDCANCEL;
    5.76          } else {
    5.77 -            pButton->nButtonID = messageboxdata->buttons[i].buttonid + 1;
    5.78 -            if (pButton->nButtonID >= 2) {
    5.79 -                pButton->nButtonID++;
    5.80 -            }
    5.81 +            pButton->nButtonID = IDBUTTONINDEX0 + i;
    5.82          }
    5.83          buttontext = EscapeAmpersands(&ampescape, &ampescapesize, messageboxdata->buttons[i].text);
    5.84          if (buttontext == NULL) {
    5.85 @@ -886,12 +900,12 @@
    5.86  
    5.87      /* Check the Task Dialog was successful and give the result */
    5.88      if (SUCCEEDED(hr)) {
    5.89 -        if (nButton == 2) {
    5.90 +        if (nButton == IDCANCEL) {
    5.91              *buttonid = nCancelButton;
    5.92 -        } else if (nButton > 2) {
    5.93 -            *buttonid = nButton-1-1;
    5.94 +        } else if (nButton >= IDBUTTONINDEX0 && nButton < IDBUTTONINDEX0 + messageboxdata->numbuttons) {
    5.95 +            *buttonid = messageboxdata->buttons[nButton - IDBUTTONINDEX0].buttonid;
    5.96          } else {
    5.97 -            *buttonid = nButton-1;
    5.98 +            *buttonid = -1;
    5.99          }
   5.100          return 0;
   5.101      }
     6.1 --- a/src/video/winrt/SDL_winrtmessagebox.cpp	Fri Aug 02 17:12:49 2019 -0700
     6.2 +++ b/src/video/winrt/SDL_winrtmessagebox.cpp	Fri Aug 02 17:19:50 2019 -0700
     6.3 @@ -75,13 +75,19 @@
     6.4      MessageDialog ^ dialog = ref new MessageDialog(WINRT_UTF8ToPlatformString(messageboxdata->message));
     6.5      dialog->Title = WINRT_UTF8ToPlatformString(messageboxdata->title);
     6.6      for (int i = 0; i < messageboxdata->numbuttons; ++i) {
     6.7 -        UICommand ^ button = ref new UICommand(WINRT_UTF8ToPlatformString(messageboxdata->buttons[i].text));
     6.8 -        button->Id = safe_cast<IntPtr>(i);
     6.9 +        const SDL_MessageBoxButtonData *sdlButton;
    6.10 +        if (messageboxdata->flags & SDL_MESSAGEBOX_BUTTONS_RIGHT_TO_LEFT) {
    6.11 +            sdlButton = &messageboxdata->buttons[messageboxdata->numbuttons - 1 - i];
    6.12 +        } else {
    6.13 +            sdlButton = &messageboxdata->buttons[i];
    6.14 +        }
    6.15 +        UICommand ^ button = ref new UICommand(WINRT_UTF8ToPlatformString(sdlButton->text));
    6.16 +        button->Id = safe_cast<IntPtr>(sdlButton - messageboxdata->buttons);
    6.17          dialog->Commands->Append(button);
    6.18 -        if (messageboxdata->buttons[i].flags & SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT) {
    6.19 +        if (sdlButton->flags & SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT) {
    6.20              dialog->CancelCommandIndex = i;
    6.21          }
    6.22 -        if (messageboxdata->buttons[i].flags & SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT) {
    6.23 +        if (sdlButton->flags & SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT) {
    6.24              dialog->DefaultCommandIndex = i;
    6.25          }
    6.26      }
     7.1 --- a/src/video/x11/SDL_x11messagebox.c	Fri Aug 02 17:12:49 2019 -0700
     7.2 +++ b/src/video/x11/SDL_x11messagebox.c	Fri Aug 02 17:19:50 2019 -0700
     7.3 @@ -334,7 +334,11 @@
     7.4          data->dialog_height = IntMax( data->dialog_height, ybuttons + 2 * button_height );
     7.5  
     7.6          /* Location for first button. */
     7.7 -        x = ( data->dialog_width - width_of_buttons ) / 2;
     7.8 +        if ( messageboxdata->flags & SDL_MESSAGEBOX_BUTTONS_RIGHT_TO_LEFT ) {
     7.9 +			x = data->dialog_width - ( data->dialog_width - width_of_buttons ) / 2 - ( button_width + button_spacing );
    7.10 +		} else {
    7.11 +			x = ( data->dialog_width - width_of_buttons ) / 2;
    7.12 +		}
    7.13          y = ybuttons + ( data->dialog_height - ybuttons - button_height ) / 2;
    7.14  
    7.15          for ( i = 0; i < data->numbuttons; i++ ) {
    7.16 @@ -349,7 +353,11 @@
    7.17              data->buttonpos[ i ].y = y + ( button_height - button_text_height - 1 ) / 2 + button_text_height;
    7.18  
    7.19              /* Scoot over for next button. */
    7.20 -            x += button_width + button_spacing;
    7.21 +			if ( messageboxdata->flags & SDL_MESSAGEBOX_BUTTONS_RIGHT_TO_LEFT ) {
    7.22 +				x -= button_width + button_spacing;
    7.23 +			} else {
    7.24 +				x += button_width + button_spacing;
    7.25 +			}
    7.26          }
    7.27      }
    7.28