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.
Sam Lantinga 2019-08-02 17:19:50 -07:00
parent d52ffcf985
commit 63197c4338
8 changed files with 101 additions and 35 deletions

0
Xcode/SDL/SDL.xcodeproj/project.pbxproj Normal file → Executable file
View File

View File

@ -36,9 +36,11 @@ extern "C" {
*/
typedef enum
{
SDL_MESSAGEBOX_ERROR = 0x00000010, /**< error dialog */
SDL_MESSAGEBOX_WARNING = 0x00000020, /**< warning dialog */
SDL_MESSAGEBOX_INFORMATION = 0x00000040 /**< informational dialog */
SDL_MESSAGEBOX_ERROR = 0x00000010, /**< error dialog */
SDL_MESSAGEBOX_WARNING = 0x00000020, /**< warning dialog */
SDL_MESSAGEBOX_INFORMATION = 0x00000040, /**< informational dialog */
SDL_MESSAGEBOX_BUTTONS_LEFT_TO_RIGHT = 0x00000080, /**< buttons placed left to right */
SDL_MESSAGEBOX_BUTTONS_RIGHT_TO_LEFT = 0x00000100, /**< buttons placed right to left */
} SDL_MessageBoxFlags;
/**

View File

@ -2380,11 +2380,19 @@ int Android_JNI_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *bu
button_texts = (*env)->NewObjectArray(env, messageboxdata->numbuttons,
clazz, NULL);
for (i = 0; i < messageboxdata->numbuttons; ++i) {
temp = messageboxdata->buttons[i].flags;
const SDL_MessageBoxButtonData *sdlButton;
if (messageboxdata->flags & SDL_MESSAGEBOX_BUTTONS_RIGHT_TO_LEFT) {
sdlButton = &messageboxdata->buttons[messageboxdata->numbuttons - 1 - i];
} else {
sdlButton = &messageboxdata->buttons[i];
}
temp = sdlButton->flags;
(*env)->SetIntArrayRegion(env, button_flags, i, 1, &temp);
temp = messageboxdata->buttons[i].buttonid;
temp = sdlButton->buttonid;
(*env)->SetIntArrayRegion(env, button_ids, i, 1, &temp);
text = (*env)->NewStringUTF(env, messageboxdata->buttons[i].text);
text = (*env)->NewStringUTF(env, sdlButton->text);
(*env)->SetObjectArrayElement(env, button_texts, i, text);
(*env)->DeleteLocalRef(env, text);
}

View File

@ -112,10 +112,19 @@ Cocoa_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
const SDL_MessageBoxButtonData *buttons = messageboxdata->buttons;
int i;
for (i = 0; i < messageboxdata->numbuttons; ++i) {
NSButton *button = [alert addButtonWithTitle:[NSString stringWithUTF8String:buttons[i].text]];
if (buttons[i].flags & SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT) {
const SDL_MessageBoxButtonData *sdlButton;
NSButton *button;
if (messageboxdata->flags & SDL_MESSAGEBOX_BUTTONS_RIGHT_TO_LEFT) {
sdlButton = &messageboxdata->buttons[messageboxdata->numbuttons - 1 - i];
} else {
sdlButton = &messageboxdata->buttons[i];
}
button = [alert addButtonWithTitle:[NSString stringWithUTF8String:sdlButton->text]];
if (sdlButton->flags & SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT) {
[button setKeyEquivalent:@"\r"];
} else if (buttons[i].flags & SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT) {
} else if (sdlButton->flags & SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT) {
[button setKeyEquivalent:@"\033"];
} else {
[button setKeyEquivalent:@""];
@ -132,6 +141,9 @@ Cocoa_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
NSInteger clicked = presenter->clicked;
if (clicked >= NSAlertFirstButtonReturn) {
clicked -= NSAlertFirstButtonReturn;
if (messageboxdata->flags & SDL_MESSAGEBOX_BUTTONS_RIGHT_TO_LEFT) {
clicked = messageboxdata->numbuttons - 1 - clicked;
}
*buttonid = buttons[clicked].buttonid;
} else {
returnValue = SDL_SetError("Did not get a valid `clicked button' id: %ld", (long)clicked);

View File

@ -73,15 +73,22 @@ UIKit_ShowMessageBoxAlertController(const SDL_MessageBoxData *messageboxdata, in
for (i = 0; i < messageboxdata->numbuttons; i++) {
UIAlertAction *action;
UIAlertActionStyle style = UIAlertActionStyleDefault;
const SDL_MessageBoxButtonData *sdlButton;
if (buttons[i].flags & SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT) {
if (messageboxdata->flags & SDL_MESSAGEBOX_BUTTONS_RIGHT_TO_LEFT) {
sdlButton = &messageboxdata->buttons[messageboxdata->numbuttons - 1 - i];
} else {
sdlButton = &messageboxdata->buttons[i];
}
if (sdlButton->flags & SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT) {
style = UIAlertActionStyleCancel;
}
action = [UIAlertAction actionWithTitle:@(buttons[i].text)
action = [UIAlertAction actionWithTitle:@(sdlButton->text)
style:style
handler:^(UIAlertAction *action) {
clickedindex = i;
clickedindex = (int)(sdlButton - messageboxdata->buttons);
}];
[alert addAction:action];
}
@ -150,7 +157,13 @@ UIKit_ShowMessageBoxAlertView(const SDL_MessageBoxData *messageboxdata, int *but
alert.message = @(messageboxdata->message);
for (i = 0; i < messageboxdata->numbuttons; i++) {
[alert addButtonWithTitle:@(buttons[i].text)];
const SDL_MessageBoxButtonData *sdlButton;
if (messageboxdata->flags & SDL_MESSAGEBOX_BUTTONS_RIGHT_TO_LEFT) {
sdlButton = &messageboxdata->buttons[messageboxdata->numbuttons - 1 - i];
} else {
sdlButton = &messageboxdata->buttons[i];
}
[alert addButtonWithTitle:@(sdlButton->text)];
}
delegate.clickedIndex = &clickedindex;
@ -161,6 +174,9 @@ UIKit_ShowMessageBoxAlertView(const SDL_MessageBoxData *messageboxdata, int *but
alert.delegate = nil;
if (messageboxdata->flags & SDL_MESSAGEBOX_BUTTONS_RIGHT_TO_LEFT) {
clickedindex = messageboxdata->numbuttons - 1 - clickedindex;
}
*buttonid = messageboxdata->buttons[clickedindex].buttonid;
return YES;
#else

View File

@ -367,7 +367,7 @@ static SDL_bool AddDialogButton(WIN_DialogData *dialog, int x, int y, int w, int
if (dialog->numbuttons == 0) {
style |= WS_GROUP;
}
return AddDialogControl(dialog, DLGITEMTYPEBUTTON, style, 0, x, y, w, h, IDBUTTONINDEX0 + dialog->numbuttons, text, 0);
return AddDialogControl(dialog, DLGITEMTYPEBUTTON, style, 0, x, y, w, h, id, text, 0);
}
static void FreeDialogData(WIN_DialogData *dialog)
@ -708,23 +708,36 @@ WIN_ShowOldMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
/* Align the buttons to the right/bottom. */
x = Size.cx - (ButtonWidth + ButtonMargin) * messageboxdata->numbuttons;
y = Size.cy - ButtonHeight - ButtonMargin;
for (i = messageboxdata->numbuttons - 1; i >= 0; --i) {
for (i = 0; i < messageboxdata->numbuttons; i++) {
SDL_bool isdefault = SDL_FALSE;
const char *buttontext;
const SDL_MessageBoxButtonData *sdlButton;
if (buttons[i].flags & SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT) {
/* We always have to create the dialog buttons from left to right
* so that the tab order is correct. Select the info to use
* depending on which order was requested. */
if (messageboxdata->flags & SDL_MESSAGEBOX_BUTTONS_LEFT_TO_RIGHT) {
sdlButton = &messageboxdata->buttons[i];
} else {
sdlButton = &messageboxdata->buttons[messageboxdata->numbuttons - 1 - i];
}
if (sdlButton->flags & SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT) {
defbuttoncount++;
if (defbuttoncount == 1) {
isdefault = SDL_TRUE;
}
}
buttontext = EscapeAmpersands(&ampescape, &ampescapesize, buttons[i].text);
if (buttontext == NULL || !AddDialogButton(dialog, x, y, ButtonWidth, ButtonHeight, buttontext, buttons[i].buttonid, isdefault)) {
buttontext = EscapeAmpersands(&ampescape, &ampescapesize, sdlButton->text);
/* Make sure to provide the correct ID to keep buttons indexed in the
* same order as how they are in messageboxdata. */
if (buttontext == NULL || !AddDialogButton(dialog, x, y, ButtonWidth, ButtonHeight, buttontext, IDBUTTONINDEX0 + (int)(sdlButton - messageboxdata->buttons), isdefault)) {
FreeDialogData(dialog);
SDL_free(ampescape);
return -1;
}
x += ButtonWidth + ButtonMargin;
}
SDL_free(ampescape);
@ -737,7 +750,7 @@ WIN_ShowOldMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
result = DialogBoxIndirectParam(NULL, (DLGTEMPLATE*)dialog->lpDialog, ParentWindow, (DLGPROC)MessageBoxDialogProc, (LPARAM)messageboxdata);
if (result >= IDBUTTONINDEX0 && result - IDBUTTONINDEX0 < messageboxdata->numbuttons) {
*buttonid = messageboxdata->buttons[(messageboxdata->numbuttons - 1) - (result - IDBUTTONINDEX0)].buttonid;
*buttonid = messageboxdata->buttons[result - IDBUTTONINDEX0].buttonid;
retval = 0;
} else if (result == IDCLOSED) {
/* Dialog window closed by user or system. */
@ -841,15 +854,16 @@ WIN_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
for (i = 0; i < messageboxdata->numbuttons; i++)
{
const char *buttontext;
pButton = &pButtons[messageboxdata->numbuttons-1-i];
if (messageboxdata->flags & SDL_MESSAGEBOX_BUTTONS_LEFT_TO_RIGHT) {
pButton = &pButtons[i];
} else {
pButton = &pButtons[messageboxdata->numbuttons - 1 - i];
}
if (messageboxdata->buttons[i].flags & SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT) {
nCancelButton = messageboxdata->buttons[i].buttonid;
pButton->nButtonID = 2;
pButton->nButtonID = IDCANCEL;
} else {
pButton->nButtonID = messageboxdata->buttons[i].buttonid + 1;
if (pButton->nButtonID >= 2) {
pButton->nButtonID++;
}
pButton->nButtonID = IDBUTTONINDEX0 + i;
}
buttontext = EscapeAmpersands(&ampescape, &ampescapesize, messageboxdata->buttons[i].text);
if (buttontext == NULL) {
@ -886,12 +900,12 @@ WIN_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
/* Check the Task Dialog was successful and give the result */
if (SUCCEEDED(hr)) {
if (nButton == 2) {
if (nButton == IDCANCEL) {
*buttonid = nCancelButton;
} else if (nButton > 2) {
*buttonid = nButton-1-1;
} else if (nButton >= IDBUTTONINDEX0 && nButton < IDBUTTONINDEX0 + messageboxdata->numbuttons) {
*buttonid = messageboxdata->buttons[nButton - IDBUTTONINDEX0].buttonid;
} else {
*buttonid = nButton-1;
*buttonid = -1;
}
return 0;
}

View File

@ -75,13 +75,19 @@ WINRT_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
MessageDialog ^ dialog = ref new MessageDialog(WINRT_UTF8ToPlatformString(messageboxdata->message));
dialog->Title = WINRT_UTF8ToPlatformString(messageboxdata->title);
for (int i = 0; i < messageboxdata->numbuttons; ++i) {
UICommand ^ button = ref new UICommand(WINRT_UTF8ToPlatformString(messageboxdata->buttons[i].text));
button->Id = safe_cast<IntPtr>(i);
const SDL_MessageBoxButtonData *sdlButton;
if (messageboxdata->flags & SDL_MESSAGEBOX_BUTTONS_RIGHT_TO_LEFT) {
sdlButton = &messageboxdata->buttons[messageboxdata->numbuttons - 1 - i];
} else {
sdlButton = &messageboxdata->buttons[i];
}
UICommand ^ button = ref new UICommand(WINRT_UTF8ToPlatformString(sdlButton->text));
button->Id = safe_cast<IntPtr>(sdlButton - messageboxdata->buttons);
dialog->Commands->Append(button);
if (messageboxdata->buttons[i].flags & SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT) {
if (sdlButton->flags & SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT) {
dialog->CancelCommandIndex = i;
}
if (messageboxdata->buttons[i].flags & SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT) {
if (sdlButton->flags & SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT) {
dialog->DefaultCommandIndex = i;
}
}

View File

@ -334,7 +334,11 @@ X11_MessageBoxInitPositions( SDL_MessageBoxDataX11 *data )
data->dialog_height = IntMax( data->dialog_height, ybuttons + 2 * button_height );
/* Location for first button. */
x = ( data->dialog_width - width_of_buttons ) / 2;
if ( messageboxdata->flags & SDL_MESSAGEBOX_BUTTONS_RIGHT_TO_LEFT ) {
x = data->dialog_width - ( data->dialog_width - width_of_buttons ) / 2 - ( button_width + button_spacing );
} else {
x = ( data->dialog_width - width_of_buttons ) / 2;
}
y = ybuttons + ( data->dialog_height - ybuttons - button_height ) / 2;
for ( i = 0; i < data->numbuttons; i++ ) {
@ -349,7 +353,11 @@ X11_MessageBoxInitPositions( SDL_MessageBoxDataX11 *data )
data->buttonpos[ i ].y = y + ( button_height - button_text_height - 1 ) / 2 + button_text_height;
/* Scoot over for next button. */
x += button_width + button_spacing;
if ( messageboxdata->flags & SDL_MESSAGEBOX_BUTTONS_RIGHT_TO_LEFT ) {
x -= button_width + button_spacing;
} else {
x += button_width + button_spacing;
}
}
}