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

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;
}