TGUI 1.13
Loading...
Searching...
No Matches
MessageBox.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2026 Bruno Van de Velde (vdv_b@tgui.eu)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25#ifndef TGUI_MESSAGE_BOX_HPP
26#define TGUI_MESSAGE_BOX_HPP
27
28#include <TGUI/Renderers/MessageBoxRenderer.hpp>
29#include <TGUI/Widgets/Button.hpp>
30#include <TGUI/Widgets/ChildWindow.hpp>
31#include <TGUI/Widgets/Label.hpp>
32
33#undef MessageBox // windows.h defines MessageBox when NOMB isn't defined before including windows.h
34
36
37namespace tgui
38{
42 class TGUI_API MessageBox : public ChildWindow
43 {
44 public:
45 using Ptr = std::shared_ptr<MessageBox>;
46 using ConstPtr = std::shared_ptr<const MessageBox>;
47
48 static constexpr char StaticWidgetType[] = "MessageBox";
49
53 using Alignment TGUI_DEPRECATED("Use tgui::HorizontalAlignment instead") = HorizontalAlignment; // TGUI_NEXT: Remove this alias
54
62 explicit MessageBox(const char* typeName = StaticWidgetType, bool initRenderer = true);
63
73 [[nodiscard]] static MessageBox::Ptr create(const String& title = "",
74 const String& text = "",
75 const std::vector<String>& buttons = {});
76
80 MessageBox(const MessageBox& other);
81
85 MessageBox(MessageBox&& other) noexcept;
86
90 MessageBox& operator=(const MessageBox& other);
91
95 MessageBox& operator=(MessageBox&& other) noexcept;
96
104 [[nodiscard]] static MessageBox::Ptr copy(const MessageBox::ConstPtr& messageBox);
105
110 [[nodiscard]] MessageBoxRenderer* getSharedRenderer() override;
111 [[nodiscard]] const MessageBoxRenderer* getSharedRenderer() const override;
112
118 [[nodiscard]] MessageBoxRenderer* getRenderer() override;
119
131 void setSize(const Layout2d& size) override;
132 using Widget::setSize;
133
144 void setClientSize(const Layout2d& size) override;
145
154 void setText(const String& text);
155
161 [[nodiscard]] const String& getText() const;
162
171 void addButton(const String& buttonCaption);
172
189 void changeButtons(const std::vector<String>& buttonCaptions);
190
196 [[nodiscard]] std::vector<String> getButtons() const;
197
206
215
224
233
235
236 protected:
238 // Makes sure all widgets lie within the window and places them on the correct position.
240 void rearrange();
241
251 [[nodiscard]] Signal& getSignal(String signalName) override;
252
258 void rendererChanged(const String& property) override;
259
263 [[nodiscard]] std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
264
268 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
269
273 void updateTextSize() override;
274
276 // Makes a copy of the widget
278 [[nodiscard]] Widget::Ptr clone() const override;
279
281
282 private:
284 // Figure out which widget is the label and which are the buttons after copying or loading
286 void identifyLabelAndButtons();
287
289 // Passes our onButtonPress signal handler to the onPress signal of a button
291 void connectButtonPressSignal(std::size_t buttonIndex);
292
296 void addButtonImpl(const String& caption);
297
299
300 public:
301 SignalString onButtonPress = {"ButtonPressed"};
302
304
305 protected:
306 String m_loadedThemeFile;
307 String m_buttonClassName;
308 bool m_autoSize = true;
311
312 std::vector<Button::Ptr> m_buttons;
313
314 Label::Ptr m_label = Label::create();
315 };
316} // namespace tgui
317
318#endif // TGUI_MESSAGE_BOX_HPP
static Label::Ptr create(const String &text="")
Creates a new label widget.
std::shared_ptr< Label > Ptr
Shared widget pointer.
Definition Label.hpp:43
Class to store the position or size of a widget.
Definition Layout.hpp:320
Definition MessageBoxRenderer.hpp:35
SignalString onButtonPress
One of the buttons was pressed. Optional parameter: text of the pressed button.
Definition MessageBox.hpp:301
void setLabelAlignment(HorizontalAlignment labelAlignment)
Changes where the label is located inside the window (left side, centered or right side).
static MessageBox::Ptr create(const String &title="", const String &text="", const std::vector< String > &buttons={})
Creates a new message box widget.
std::vector< String > getButtons() const
Returns the caption of the buttons.
MessageBoxRenderer * getSharedRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
void updateTextSize() override
Called when the text size is changed (either by setTextSize or via the renderer).
MessageBoxRenderer * getRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
std::shared_ptr< const MessageBox > ConstPtr
Shared constant widget pointer.
Definition MessageBox.hpp:46
std::unique_ptr< DataIO::Node > save(SavingRenderersMap &renderers) const override
Saves the widget as a tree node in order to save it to a file.
HorizontalAlignment Alignment
The label and button alignment.
Definition MessageBox.hpp:53
static constexpr char StaticWidgetType[]
Type name of the widget.
Definition MessageBox.hpp:48
void setText(const String &text)
Changes the text of the message box.
MessageBox & operator=(MessageBox &&other) noexcept
Overload of move assignment operator.
const String & getText() const
Returns the text of the message box.
Signal & getSignal(String signalName) override
Retrieves a signal based on its name.
void rendererChanged(const String &property) override
Function called when one of the properties of the renderer is changed.
void setButtonAlignment(HorizontalAlignment buttonAlignment)
Changes where the buttons are located inside the window (left side, centered or right side).
MessageBox(const MessageBox &other)
Copy constructor.
std::shared_ptr< MessageBox > Ptr
Shared widget pointer.
Definition MessageBox.hpp:45
HorizontalAlignment getLabelAlignment() const
Returns where the label is located inside the window (left side, centered or right side).
MessageBox(MessageBox &&other) noexcept
Move constructor.
void addButton(const String &buttonCaption)
Adds a button to the message box.
static MessageBox::Ptr copy(const MessageBox::ConstPtr &messageBox)
Makes a copy of another message box.
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
MessageBox & operator=(const MessageBox &other)
Overload of copy assignment operator.
void changeButtons(const std::vector< String > &buttonCaptions)
Adds, removes, or updates buttons from the message box.
void setSize(const Layout2d &size) override
Changes the size of the message box.
void setClientSize(const Layout2d &size) override
Changes the client size of the child window.
HorizontalAlignment getButtonAlignment() const
Returns where the buttons are located inside the window (left side, centered or right side).
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:59
Wrapper class to store strings.
Definition String.hpp:94
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:85
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:37
HorizontalAlignment
The horizontal alignment.
Definition Layout.hpp:60
@ Center
Center the object horizontally.
Definition Layout.hpp:62
@ Left
Align to the left side.
Definition Layout.hpp:61
SignalTyped< const String & > SignalString
Signal with one "String" as optional unbound parameter.
Definition Signal.hpp:413