TGUI  1.2.0
Loading...
Searching...
No Matches
MessageBox.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2024 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
26#ifndef TGUI_MESSAGE_BOX_HPP
27#define TGUI_MESSAGE_BOX_HPP
28
29
30#include <TGUI/Widgets/Label.hpp>
31#include <TGUI/Widgets/Button.hpp>
32#include <TGUI/Widgets/ChildWindow.hpp>
33#include <TGUI/Renderers/MessageBoxRenderer.hpp>
34
35#undef MessageBox // windows.h defines MessageBox when NOMB isn't defined before including windows.h
36
38
39TGUI_MODULE_EXPORT namespace tgui
40{
44 class TGUI_API MessageBox : public ChildWindow
45 {
46 public:
47
48 using Ptr = std::shared_ptr<MessageBox>;
49 using ConstPtr = std::shared_ptr<const MessageBox>;
50
51 static constexpr const char StaticWidgetType[] = "MessageBox";
52
53
57 enum class Alignment
58 {
59 Left,
60 Center,
61 Right
62 };
63
64
72 MessageBox(const char* typeName = StaticWidgetType, bool initRenderer = true);
73
74
84 TGUI_NODISCARD static MessageBox::Ptr create(const String& title = "", const String& text = "", const std::vector<String>& buttons = {});
85
86
90 MessageBox(const MessageBox& copy);
91
92
96 MessageBox(MessageBox&& copy) noexcept;
97
98
102 MessageBox& operator= (const MessageBox& right);
103
104
108 MessageBox& operator= (MessageBox&& right) noexcept;
109
110
119 TGUI_NODISCARD static MessageBox::Ptr copy(const MessageBox::ConstPtr& messageBox);
120
121
126 TGUI_NODISCARD MessageBoxRenderer* getSharedRenderer() override;
127 TGUI_NODISCARD const MessageBoxRenderer* getSharedRenderer() const override;
128
134 TGUI_NODISCARD MessageBoxRenderer* getRenderer() override;
135
136
148 void setSize(const Layout2d& size) override;
149 using Widget::setSize;
150
151
162 void setClientSize(const Layout2d& size) override;
163
164
174 void setText(const String& text);
175
176
183 TGUI_NODISCARD const String& getText() const;
184
185
195 void addButton(const String& buttonCaption);
196
197
215 void changeButtons(const std::vector<String>& buttonCaptions);
216
217
224 TGUI_NODISCARD std::vector<String> getButtons() const;
225
226
234 void setLabelAlignment(Alignment labelAlignment);
235
236
244 TGUI_NODISCARD Alignment getLabelAlignment() const;
245
246
254 void setButtonAlignment(Alignment buttonAlignment);
255
256
264 TGUI_NODISCARD Alignment getButtonAlignment() const;
265
266
268 protected:
269
271 // Makes sure all widgets lie within the window and places them on the correct position.
273 void rearrange();
274
275
285 TGUI_NODISCARD Signal& getSignal(String signalName) override;
286
287
294 void rendererChanged(const String& property) override;
295
296
300 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
301
302
306 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
307
308
312 void updateTextSize() override;
313
314
316 // Makes a copy of the widget
318 TGUI_NODISCARD Widget::Ptr clone() const override;
319
320
322 private:
323
325 // Figure out which widget is the label and which are the buttons after copying or loading
327 void identifyLabelAndButtons();
328
329
331 // Passes our onButtonPress signal handler to the onPress signal of a button
333 void connectButtonPressSignal(std::size_t buttonIndex);
334
335
339 void addButtonImpl(const String& caption);
340
341
343 public:
344
345 SignalString onButtonPress = {"ButtonPressed"};
346
347
349 protected:
350
351 String m_loadedThemeFile;
352 String m_buttonClassName;
353 bool m_autoSize = true;
354 Alignment m_labelAlignment = Alignment::Left;
355 Alignment m_buttonAlignment = Alignment::Center;
356
357 std::vector<Button::Ptr> m_buttons;
358
359 Label::Ptr m_label = Label::create();
360 };
361
363}
364
366
367#endif // TGUI_MESSAGE_BOX_HPP
Child window widget.
Definition ChildWindow.hpp:47
std::shared_ptr< Label > Ptr
Shared widget pointer.
Definition Label.hpp:47
Class to store the position or size of a widget.
Definition Layout.hpp:305
Definition MessageBoxRenderer.hpp:37
Message box widget.
Definition MessageBox.hpp:45
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
void setLabelAlignment(Alignment labelAlignment)
Changes where the label is located inside the window (left side, centered or right side)
TGUI_NODISCARD Signal & getSignal(String signalName) override
Retrieves a signal based on its name.
static TGUI_NODISCARD MessageBox::Ptr copy(const MessageBox::ConstPtr &messageBox)
Makes a copy of another message box.
TGUI_NODISCARD const String & getText() const
Returns the text of the message box.
void updateTextSize() override
Called when the text size is changed (either by setTextSize or via the renderer)
std::shared_ptr< const MessageBox > ConstPtr
Shared constant widget pointer.
Definition MessageBox.hpp:49
MessageBox(const MessageBox &copy)
Copy constructor.
void setText(const String &text)
Changes the text of the message box.
TGUI_NODISCARD Alignment getLabelAlignment() const
Returns where the label is located inside the window (left side, centered or right side)
TGUI_NODISCARD MessageBoxRenderer * getRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
static TGUI_NODISCARD MessageBox::Ptr create(const String &title="", const String &text="", const std::vector< String > &buttons={})
Creates a new message box widget.
TGUI_NODISCARD 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.
void rendererChanged(const String &property) override
Function called when one of the properties of the renderer is changed.
MessageBox(MessageBox &&copy) noexcept
Move constructor.
std::shared_ptr< MessageBox > Ptr
Shared widget pointer.
Definition MessageBox.hpp:48
void setButtonAlignment(Alignment buttonAlignment)
Changes where the buttons are located inside the window (left side, centered or right side)
TGUI_NODISCARD Alignment getButtonAlignment() const
Returns where the buttons are located inside the window (left side, centered or right side)
TGUI_NODISCARD MessageBoxRenderer * getSharedRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
void addButton(const String &buttonCaption)
Adds a button to the message box.
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.
Alignment
The label and button alignment.
Definition MessageBox.hpp:58
TGUI_NODISCARD std::vector< String > getButtons() const
Returns the caption of the buttons.
TGUI_NODISCARD Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:62
Wrapper class to store strings.
Definition String.hpp:101
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:87
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:39