TGUI  1.0-alpha
MessageBox.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2022 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
39namespace tgui
40{
44 class TGUI_API MessageBox : public ChildWindow
45 {
46 public:
47
48 typedef std::shared_ptr<MessageBox> Ptr;
49 typedef std::shared_ptr<const MessageBox> ConstPtr;
50
51
55 enum class Alignment
56 {
57 Left,
58 Center,
59 Right
60 };
61
62
70 MessageBox(const char* typeName = "MessageBox", bool initRenderer = true);
71
72
82 static MessageBox::Ptr create(String title = "", String text = "", std::vector<String> buttons = {});
83
84
88 MessageBox(const MessageBox& copy);
89
90
94 MessageBox(MessageBox&& copy) noexcept;
95
96
100 MessageBox& operator= (const MessageBox& right);
101
102
106 MessageBox& operator= (MessageBox&& right) noexcept;
107
108
118
119
125 const MessageBoxRenderer* getSharedRenderer() const;
126
133
134
146 void setSize(const Layout2d& size) override;
147 using Widget::setSize;
148
149
160 void setClientSize(const Layout2d& size) override;
161
162
172 void setText(const String& text);
173
174
181 const String& getText() const;
182
183
193 void addButton(const String& buttonCaption);
194
195
202 std::vector<String> getButtons() const;
203
204
212 void setLabelAlignment(Alignment labelAlignment);
213
214
223
224
232 void setButtonAlignment(Alignment buttonAlignment);
233
234
243
244
246 protected:
247
249 // Makes sure all widgets lie within the window and places them on the correct position.
251 void rearrange();
252
253
263 Signal& getSignal(String signalName) override;
264
265
272 void rendererChanged(const String& property) override;
273
274
278 std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
279
280
284 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
285
286
290 void updateTextSize() override;
291
292
294 // Makes a copy of the widget
296 Widget::Ptr clone() const override;
297
298
300 private:
301
303 // Figure out which widget is the label and which are the buttons after copying or loading
305 void identifyLabelAndButtons();
306
307
309 // Passes our onButtonPress signal handler to the onPress signal of a button
311 void connectButtonPressSignal(std::size_t buttonIndex);
312
313
315 public:
316
317 SignalString onButtonPress = {"ButtonPressed"};
318
319
321 protected:
322
323 String m_loadedThemeFile;
324 String m_buttonClassName;
325 bool m_autoSize = true;
326 Alignment m_labelAlignment = Alignment::Left;
327 Alignment m_buttonAlignment = Alignment::Center;
328
329 std::vector<Button::Ptr> m_buttons;
330
331 Label::Ptr m_label = Label::create();
332 };
333
335}
336
338
339#endif // TGUI_MESSAGE_BOX_HPP
Child window widget.
Definition: ChildWindow.hpp:44
std::shared_ptr< Label > Ptr
Shared widget pointer.
Definition: Label.hpp:47
static Label::Ptr create(String text="")
Creates a new label widget.
Class to store the position or size of a widget.
Definition: Layout.hpp:284
Definition: MessageBoxRenderer.hpp:37
Message box widget.
Definition: MessageBox.hpp:45
Alignment getButtonAlignment() const
Returns where the buttons are located inside the window (left side, centered or right side)
std::vector< String > getButtons() const
Returns the caption of the buttons.
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)
void updateTextSize() override
Called when the text size is changed (either by setTextSize or via the renderer)
static MessageBox::Ptr create(String title="", String text="", std::vector< String > buttons={})
Creates a new message box widget.
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.
MessageBox(const MessageBox &copy)
Copy constructor.
void setText(const String &text)
Changes the text of the message box.
Alignment getLabelAlignment() const
Returns where the label is located inside the window (left side, centered or right side)
const String & getText() const
Returns the text of the message box.
Signal & getSignal(String signalName) override
Retrieves a signal based on its name.
MessageBoxRenderer * getRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
void rendererChanged(const String &property) override
Function called when one of the properties of the renderer is changed.
MessageBox(MessageBox &&copy) noexcept
Move constructor.
void setButtonAlignment(Alignment buttonAlignment)
Changes where the buttons are located inside the window (left side, centered or right side)
MessageBoxRenderer * getSharedRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
std::shared_ptr< MessageBox > Ptr
Shared widget pointer.
Definition: MessageBox.hpp:48
void addButton(const String &buttonCaption)
Adds a button to the message box.
std::shared_ptr< const MessageBox > ConstPtr
Shared constant widget pointer.
Definition: MessageBox.hpp:49
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
static MessageBox::Ptr copy(MessageBox::ConstPtr messageBox)
Makes a copy of another 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:56
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:58
Wrapper class to store strings.
Definition: String.hpp:79
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:73
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36