TGUI  1.6.1
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#ifndef TGUI_MESSAGE_BOX_HPP
26#define TGUI_MESSAGE_BOX_HPP
27
28#include <TGUI/Widgets/Label.hpp>
29#include <TGUI/Widgets/Button.hpp>
30#include <TGUI/Widgets/ChildWindow.hpp>
31#include <TGUI/Renderers/MessageBoxRenderer.hpp>
32
33#undef MessageBox // windows.h defines MessageBox when NOMB isn't defined before including windows.h
34
36
37TGUI_MODULE_EXPORT namespace tgui
38{
42 class TGUI_API MessageBox : public ChildWindow
43 {
44 public:
45
46 using Ptr = std::shared_ptr<MessageBox>;
47 using ConstPtr = std::shared_ptr<const MessageBox>;
48
49 static constexpr const char StaticWidgetType[] = "MessageBox";
50
54 using Alignment TGUI_DEPRECATED("Use tgui::HorizontalAlignment instead") = HorizontalAlignment; // TGUI_NEXT: Remove this alias
55
63 MessageBox(const char* typeName = StaticWidgetType, bool initRenderer = true);
64
74 TGUI_NODISCARD static MessageBox::Ptr create(const String& title = "", const String& text = "", const std::vector<String>& buttons = {});
75
79 MessageBox(const MessageBox& copy);
80
84 MessageBox(MessageBox&& copy) noexcept;
85
89 MessageBox& operator= (const MessageBox& right);
90
94 MessageBox& operator= (MessageBox&& right) noexcept;
95
103 TGUI_NODISCARD static MessageBox::Ptr copy(const MessageBox::ConstPtr& messageBox);
104
109 TGUI_NODISCARD MessageBoxRenderer* getSharedRenderer() override;
110 TGUI_NODISCARD const MessageBoxRenderer* getSharedRenderer() const override;
111
117 TGUI_NODISCARD MessageBoxRenderer* getRenderer() override;
118
130 void setSize(const Layout2d& size) override;
131 using Widget::setSize;
132
143 void setClientSize(const Layout2d& size) override;
144
153 void setText(const String& text);
154
160 TGUI_NODISCARD const String& getText() const;
161
170 void addButton(const String& buttonCaption);
171
188 void changeButtons(const std::vector<String>& buttonCaptions);
189
195 TGUI_NODISCARD std::vector<String> getButtons() const;
196
205
214
223
232
234 protected:
235
237 // Makes sure all widgets lie within the window and places them on the correct position.
239 void rearrange();
240
250 TGUI_NODISCARD Signal& getSignal(String signalName) override;
251
257 void rendererChanged(const String& property) override;
258
262 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
263
267 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
268
272 void updateTextSize() override;
273
275 // Makes a copy of the widget
277 TGUI_NODISCARD Widget::Ptr clone() const override;
278
280 private:
281
283 // Figure out which widget is the label and which are the buttons after copying or loading
285 void identifyLabelAndButtons();
286
288 // Passes our onButtonPress signal handler to the onPress signal of a button
290 void connectButtonPressSignal(std::size_t buttonIndex);
291
295 void addButtonImpl(const String& caption);
296
298 public:
299
300 SignalString onButtonPress = {"ButtonPressed"};
301
303 protected:
304
305 String m_loadedThemeFile;
306 String m_buttonClassName;
307 bool m_autoSize = true;
308 HorizontalAlignment m_labelAlignment = HorizontalAlignment::Left;
309 HorizontalAlignment m_buttonAlignment = HorizontalAlignment::Center;
310
311 std::vector<Button::Ptr> m_buttons;
312
313 Label::Ptr m_label = Label::create();
314 };
315
317}
318
320
321#endif // TGUI_MESSAGE_BOX_HPP
Child window widget.
Definition ChildWindow.hpp:45
std::shared_ptr< Label > Ptr
Shared widget pointer.
Definition Label.hpp:44
Class to store the position or size of a widget.
Definition Layout.hpp:323
Definition MessageBoxRenderer.hpp:35
Message box widget.
Definition MessageBox.hpp:43
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:47
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.
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(MessageBox &&copy) noexcept
Move constructor.
std::shared_ptr< MessageBox > Ptr
Shared widget pointer.
Definition MessageBox.hpp:46
HorizontalAlignment getLabelAlignment() const
Returns where the label is located inside the window (left side, centered or right side)
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.
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:61
Wrapper class to store strings.
Definition String.hpp:96
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:86
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:38
HorizontalAlignment
The horizontal alignment.
Definition Layout.hpp:62