Skip to content

Commit

Permalink
Add wzDisplayDialogAdvanced()
Browse files Browse the repository at this point in the history
  • Loading branch information
past-due committed Nov 28, 2023
1 parent 29e7469 commit 00bacce
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/framework/wzapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ enum DialogType {
Dialog_Information
};
WZ_DECL_NONNULL(2, 3) void wzDisplayDialog(DialogType type, const char *title, const char *message); ///< Throw up a modal warning dialog - title & message are UTF-8 text
WZ_DECL_NONNULL(2, 3) size_t wzDisplayDialogAdvanced(DialogType type, const char *title, const char *message, std::vector<std::string> buttonsText);

WzString wzGetPlatform();
std::vector<screeninfo> wzAvailableResolutions();
Expand Down
74 changes: 74 additions & 0 deletions lib/sdl/main_sdl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,80 @@ void wzDisplayDialog(DialogType type, const char *title, const char *message)
SDL_ShowSimpleMessageBox(sdl_messagebox_flags, title, message, WZwindow);
}

// Displays a message box with specified button(s)
// Returns 0 on failure, or the (clicked button index + 1)
size_t wzDisplayDialogAdvanced(DialogType type, const char *title, const char *message, std::vector<std::string> buttonsText)
{
if (!WZbackend.has_value())
{
// while this could be thread_local, thread_local may not yet be supported on all platforms properly
// and wzDisplayDialog should probably be called **only** on the main thread anyway
static bool processingDialog = false;
if (!processingDialog)
{
// in headless mode, do not display a messagebox (which might block the main thread)
// but just log the details
processingDialog = true;
debug(LOG_INFO, "Suppressed dialog (headless):\n\tTitle: %s\n\tMessage: %s", title, message);
processingDialog = false;
}
return 0;
}

if (buttonsText.empty())
{
// always at least show an "OK" button
buttonsText.push_back("OK");
}

std::vector<SDL_MessageBoxButtonData> buttons;
buttons.reserve(buttonsText.size());
for (size_t i = 0; i < buttonsText.size(); ++i)
{
Uint32 buttonFlags = 0;
if (i == 0)
{
buttonFlags |= SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT;
}
if (i == buttonsText.size() - 1)
{
buttonFlags |= SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT;
}
buttons.push_back(SDL_MessageBoxButtonData{buttonFlags, static_cast<int>(i + 1), buttonsText[i].c_str()});
}

Uint32 sdl_messagebox_flags = 0;
switch (type)
{
case Dialog_Error:
sdl_messagebox_flags = SDL_MESSAGEBOX_ERROR;
break;
case Dialog_Warning:
sdl_messagebox_flags = SDL_MESSAGEBOX_WARNING;
break;
case Dialog_Information:
sdl_messagebox_flags = SDL_MESSAGEBOX_INFORMATION;
break;
}

const SDL_MessageBoxData messageboxdata = {
sdl_messagebox_flags, /* .flags */
WZwindow, /* .window */
title, /* .title */
message, /* .message */
static_cast<int>(buttons.size()), /* .numbuttons */
buttons.data(), /* .buttons */
nullptr /* .colorScheme */
};
int buttonid = 0;
if (SDL_ShowMessageBox(&messageboxdata, &buttonid) < 0) {
// error displaying message box
debug(LOG_INFO, "Failed to display message box");
return 0;
}
return buttonid;
}

WINDOW_MODE wzGetCurrentWindowMode()
{
if (!WZbackend.has_value())
Expand Down

0 comments on commit 00bacce

Please sign in to comment.