SDL3pp
A slim C++ wrapper for SDL3
Loading...
Searching...
No Matches
SDL3pp_messagebox.h
1#ifndef SDL3PP_MESSAGEBOX_H_
2#define SDL3PP_MESSAGEBOX_H_
3
4#include <SDL3/SDL_messagebox.h>
5#include "SDL3pp_stdinc.h"
6#include "SDL3pp_video.h"
7
8namespace SDL {
9
27
29using MessageBoxRaw = SDL_MessageBoxData;
30
31// Forward decl
32struct MessageBox;
33
38
47
49 SDL_MESSAGEBOX_ERROR;
50
52 SDL_MESSAGEBOX_WARNING;
53
55 SDL_MESSAGEBOX_INFORMATION;
56
58 SDL_MESSAGEBOX_BUTTONS_LEFT_TO_RIGHT;
59
61 SDL_MESSAGEBOX_BUTTONS_RIGHT_TO_LEFT;
62
64
69
76
78 SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT;
80
82 SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT;
84
86
92using MessageBoxButtonData = SDL_MessageBoxButtonData;
93
99using MessageBoxColor = SDL_MessageBoxColor;
100
105
107using MessageBoxColorType = SDL_MessageBoxColorType;
108
110 SDL_MESSAGEBOX_COLOR_BACKGROUND;
111
113 SDL_MESSAGEBOX_COLOR_TEXT;
114
116 SDL_MESSAGEBOX_COLOR_BUTTON_BORDER;
117
119 SDL_MESSAGEBOX_COLOR_BUTTON_BACKGROUND;
120
122 SDL_MESSAGEBOX_COLOR_BUTTON_SELECTED;
123
125 SDL_MESSAGEBOX_COLOR_COUNT;
128
134using MessageBoxColorScheme = SDL_MessageBoxColorScheme;
135
142{
148 constexpr MessageBox(const MessageBoxRaw& messageBox = {}) noexcept
149 : MessageBoxRaw(messageBox)
150 {
151 }
152
164 WindowRef window,
165 const char* title,
166 const char* message,
167 std::span<const MessageBoxButtonData> buttons,
168 OptionalRef<const MessageBoxColorScheme> colorScheme) noexcept
169 : MessageBoxRaw{flags,
170 window,
171 title,
172 message,
173 int(buttons.size()),
174 buttons.data(),
175 colorScheme}
176 {
177 }
178
184 constexpr MessageBoxFlags GetFlags() const noexcept { return flags; }
185
192 constexpr MessageBox& SetFlags(MessageBoxFlags newFlags) noexcept
193 {
194 flags = newFlags;
195 return *this;
196 }
197
203 WindowRef GetWindow() const noexcept { return window; }
204
211 MessageBox& SetWindow(WindowRef newWindow) noexcept
212 {
213 window = newWindow;
214 return *this;
215 }
216
222 constexpr const char* GetTitle() const noexcept { return title; }
223
230 constexpr MessageBox& SetTitle(const char* newTitle) noexcept
231 {
232 title = newTitle;
233 return *this;
234 }
235
241 constexpr const char* GetMessage() const noexcept { return message; }
242
249 constexpr MessageBox& SetMessage(const char* newMessage) noexcept
250 {
251 message = newMessage;
252 return *this;
253 }
254
260 constexpr int GetNumbuttons() const noexcept { return numbuttons; }
261
268 constexpr MessageBox& SetNumbuttons(int newNumbuttons) noexcept
269 {
270 numbuttons = newNumbuttons;
271 return *this;
272 }
273
279 constexpr std::span<const MessageBoxButtonData> GetButtons() const noexcept
280 {
281 if (numbuttons == 0) return {};
282 return std::span(buttons, size_t(numbuttons));
283 }
284
292 std::span<const MessageBoxButtonData> newButtons) noexcept
293 {
294 if (newButtons.empty()) {
295 numbuttons = 0;
296 buttons = nullptr;
297 } else {
298 numbuttons = newButtons.size();
299 buttons = newButtons.data();
300 }
301 return *this;
302 }
303
309 constexpr const MessageBoxColorScheme* GetColorScheme() const noexcept
310 {
311 return colorScheme;
312 }
313
321 OptionalRef<const MessageBoxColorScheme> newColorScheme) noexcept
322 {
323 colorScheme = newColorScheme;
324 return *this;
325 }
326
360 void Show(int* buttonid) const;
361};
362
396inline void ShowMessageBox(const MessageBoxRaw& messageboxdata, int* buttonid)
397{
398 CheckError(SDL_ShowMessageBox(&messageboxdata, buttonid));
399}
400
401inline void MessageBox::Show(int* buttonid) const
402{
403 SDL::ShowMessageBox(*this, buttonid);
404}
405
448 StringParam title,
449 StringParam message,
450 WindowRef window)
451{
452 CheckError(SDL_ShowSimpleMessageBox(flags, title, message, window));
453}
454
456
457} // namespace SDL
458
459#endif /* SDL3PP_MESSAGEBOX_H_ */
Optional-like shim for references.
Definition SDL3pp_optionalRef.h:20
Helpers to use C++ strings parameters.
Definition SDL3pp_strings.h:43
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition SDL3pp_error.h:199
constexpr MessageBoxFlags MESSAGEBOX_BUTTONS_RIGHT_TO_LEFT
buttons placed right to left
Definition SDL3pp_messagebox.h:60
constexpr MessageBoxFlags MESSAGEBOX_INFORMATION
informational dialog
Definition SDL3pp_messagebox.h:54
constexpr MessageBoxButtonFlags MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT
Marks the default button when escape is hit.
Definition SDL3pp_messagebox.h:81
SDL_MessageBoxColorType MessageBoxColorType
An enumeration of indices inside the colors array of MessageBoxColorScheme.
Definition SDL3pp_messagebox.h:107
void Show(int *buttonid) const
Create a modal message box.
Definition SDL3pp_messagebox.h:401
constexpr MessageBoxColorType MESSAGEBOX_COLOR_COUNT
Size of the colors array of MessageBoxColorScheme.
Definition SDL3pp_messagebox.h:124
constexpr MessageBoxColorType MESSAGEBOX_COLOR_BUTTON_BORDER
BUTTON_BORDER.
Definition SDL3pp_messagebox.h:115
constexpr MessageBoxColorType MESSAGEBOX_COLOR_BUTTON_BACKGROUND
BUTTON_BACKGROUND.
Definition SDL3pp_messagebox.h:118
SDL_MessageBoxData MessageBoxRaw
Alias to raw representation for MessageBox.
Definition SDL3pp_messagebox.h:29
constexpr MessageBoxButtonFlags MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT
Marks the default button when return is hit.
Definition SDL3pp_messagebox.h:77
constexpr MessageBoxFlags MESSAGEBOX_ERROR
error dialog
Definition SDL3pp_messagebox.h:48
SDL_MessageBoxColorScheme MessageBoxColorScheme
A set of colors to use for message box dialogs.
Definition SDL3pp_messagebox.h:134
SDL_MessageBoxButtonData MessageBoxButtonData
Individual button data.
Definition SDL3pp_messagebox.h:92
constexpr MessageBoxFlags MESSAGEBOX_BUTTONS_LEFT_TO_RIGHT
buttons placed left to right
Definition SDL3pp_messagebox.h:57
constexpr MessageBoxColorType MESSAGEBOX_COLOR_TEXT
TEXT.
Definition SDL3pp_messagebox.h:112
constexpr MessageBoxFlags MESSAGEBOX_WARNING
warning dialog
Definition SDL3pp_messagebox.h:51
constexpr MessageBoxColorType MESSAGEBOX_COLOR_BUTTON_SELECTED
BUTTON_SELECTED.
Definition SDL3pp_messagebox.h:121
constexpr MessageBoxColorType MESSAGEBOX_COLOR_BACKGROUND
BACKGROUND.
Definition SDL3pp_messagebox.h:109
void ShowSimpleMessageBox(MessageBoxFlags flags, StringParam title, StringParam message, WindowRef window)
Display a simple modal message box.
Definition SDL3pp_messagebox.h:447
Uint32 MessageBoxFlags
Message box flags.
Definition SDL3pp_messagebox.h:46
SDL_MessageBoxColor MessageBoxColor
RGB value used in a message box color scheme.
Definition SDL3pp_messagebox.h:99
void ShowMessageBox(const MessageBoxRaw &messageboxdata, int *buttonid)
Create a modal message box.
Definition SDL3pp_messagebox.h:396
Uint32 MessageBoxButtonFlags
MessageBoxButtonData flags.
Definition SDL3pp_messagebox.h:75
::Uint32 Uint32
An unsigned 32-bit integer type.
Definition SDL3pp_stdinc.h:290
ResourceRef< Window > WindowRef
Reference for Window.
Definition SDL3pp_video.h:54
Main include header for the SDL3pp library.
MessageBox structure containing title, text, window, etc.
Definition SDL3pp_messagebox.h:142
constexpr MessageBox & SetFlags(MessageBoxFlags newFlags) noexcept
Set the flags.
Definition SDL3pp_messagebox.h:192
constexpr const char * GetMessage() const noexcept
Get the message.
Definition SDL3pp_messagebox.h:241
constexpr MessageBox & SetButtons(std::span< const MessageBoxButtonData > newButtons) noexcept
Set the buttons.
Definition SDL3pp_messagebox.h:291
constexpr std::span< const MessageBoxButtonData > GetButtons() const noexcept
Get the buttons.
Definition SDL3pp_messagebox.h:279
constexpr MessageBox(const MessageBoxRaw &messageBox={}) noexcept
Wraps MessageBox.
Definition SDL3pp_messagebox.h:148
constexpr const char * GetTitle() const noexcept
Get the title.
Definition SDL3pp_messagebox.h:222
MessageBox(MessageBoxFlags flags, WindowRef window, const char *title, const char *message, std::span< const MessageBoxButtonData > buttons, OptionalRef< const MessageBoxColorScheme > colorScheme) noexcept
Constructs from its fields.
Definition SDL3pp_messagebox.h:163
constexpr int GetNumbuttons() const noexcept
Get the numbuttons.
Definition SDL3pp_messagebox.h:260
MessageBox & SetWindow(WindowRef newWindow) noexcept
Set the window.
Definition SDL3pp_messagebox.h:211
constexpr MessageBox & SetMessage(const char *newMessage) noexcept
Set the message.
Definition SDL3pp_messagebox.h:249
constexpr MessageBox & SetColorScheme(OptionalRef< const MessageBoxColorScheme > newColorScheme) noexcept
Set the colorScheme.
Definition SDL3pp_messagebox.h:320
constexpr const MessageBoxColorScheme * GetColorScheme() const noexcept
Get the colorScheme.
Definition SDL3pp_messagebox.h:309
constexpr MessageBox & SetNumbuttons(int newNumbuttons) noexcept
Set the numbuttons.
Definition SDL3pp_messagebox.h:268
constexpr MessageBoxFlags GetFlags() const noexcept
Get the flags.
Definition SDL3pp_messagebox.h:184
WindowRef GetWindow() const noexcept
Get the window.
Definition SDL3pp_messagebox.h:203
constexpr MessageBox & SetTitle(const char *newTitle) noexcept
Set the title.
Definition SDL3pp_messagebox.h:230