TGUI 1.13
Loading...
Searching...
No Matches
Container.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_CONTAINER_HPP
26#define TGUI_CONTAINER_HPP
27
28#include <TGUI/FormLoadOptions.hpp>
29#include <TGUI/Widget.hpp>
30
32
33namespace tgui
34{
44 class TGUI_API Container : public Widget
45 {
46 public:
47 using Ptr = std::shared_ptr<Container>;
48 using ConstPtr = std::shared_ptr<const Container>;
49
57 Container(const char* typeName, bool initRenderer);
58
62 Container(const Container& other);
63
67 Container(Container&& other) noexcept;
68
72 ~Container() override;
73
77 Container& operator=(const Container& right);
78
82 Container& operator=(Container&& right) noexcept;
83
88 void setSize(const Layout2d& size) override;
89 using Widget::setSize;
90
96 [[nodiscard]] const std::vector<Widget::Ptr>& getWidgets() const
97 {
98 return m_widgets;
99 }
100
107 template <typename Function>
108 void sortWidgets(Function&& function)
109 {
110 std::sort(m_widgets.begin(), m_widgets.end(), std::forward<Function>(function));
111 }
112
124 virtual void add(const Widget::Ptr& widgetPtr, const String& widgetName = "");
125
138 [[nodiscard]] Widget::Ptr get(const String& widgetName) const;
139
153 template <class WidgetType>
154 [[nodiscard]] typename WidgetType::Ptr get(const String& widgetName) const
155 {
156 return std::dynamic_pointer_cast<WidgetType>(get(widgetName));
157 }
158
166 virtual bool remove(const Widget::Ptr& widget);
167
171 virtual void removeAllWidgets();
172
177 [[nodiscard]] virtual Vector2f getInnerSize() const;
178
185 [[nodiscard]] virtual Vector2f getChildWidgetsOffset() const
186 {
187 return Vector2f{0, 0};
188 }
189
200 void loadWidgetsFromFile(const String& filename, bool replaceExisting = true);
201
210 void loadWidgetsFromFile(const String& filename, const FormLoadOptions& options);
211
219 void saveWidgetsToFile(const String& filename) const;
220
229 void loadWidgetsFromStream(std::stringstream& stream, bool replaceExisting = true);
230
239 void loadWidgetsFromStream(std::stringstream&& stream, bool replaceExisting = true);
240
247 void loadWidgetsFromStream(std::stringstream& stream, const FormLoadOptions& options);
248
255 void loadWidgetsFromStream(std::stringstream&& stream, const FormLoadOptions& options);
256
263 void saveWidgetsToStream(std::stringstream& stream, const String& rootDirectory = "") const;
264
275 void loadWidgetsFromNodeTree(const std::unique_ptr<DataIO::Node>& rootNode, bool replaceExisting = true);
276
288 [[nodiscard]] std::unique_ptr<DataIO::Node> saveWidgetsToNodeTree(const String& rootDirectory = "") const;
289
297 void moveWidgetToFront(const Widget::Ptr& widget);
298
306 void moveWidgetToBack(const Widget::Ptr& widget);
307
318 std::size_t moveWidgetForward(const Widget::Ptr& widget);
319
330 std::size_t moveWidgetBackward(const Widget::Ptr& widget);
331
342 virtual bool setWidgetIndex(const Widget::Ptr& widget, std::size_t index);
343
349 [[nodiscard]] int getWidgetIndex(const Widget::Ptr& widget) const;
350
359 [[nodiscard]] Widget::Ptr getFocusedChild() const;
360
369 [[nodiscard]] Widget::Ptr getFocusedLeaf() const;
370
371#ifndef TGUI_REMOVE_DEPRECATED_CODE
381 TGUI_DEPRECATED("Use getWidgetAtPos instead") [[nodiscard]] virtual Widget::Ptr getWidgetAtPosition(Vector2f pos) const;
382#endif
395 [[nodiscard]] virtual Widget::Ptr getWidgetAtPos(Vector2f pos, bool recursive) const;
396
405 bool focusNextWidget(bool recursive = true);
406
415 bool focusPreviousWidget(bool recursive = true);
416
425 void setFocused(bool focused) override;
426
432 bool processMouseMoveEvent(Vector2f pos);
433
440 bool processMousePressEvent(Event::MouseButton button, Vector2f pos);
441
449
457 bool processScrollEvent(float delta, Vector2f pos, bool touch);
458
465
471 bool processTextEnteredEvent(char32_t key);
472
477 void childWidgetFocused(const Widget::Ptr& child);
478
482 bool leftMousePressed(Vector2f pos) override;
483
487 void rightMousePressed(Vector2f pos) override;
488
492 void leftMouseReleased(Vector2f pos) override;
493
497 void rightMouseReleased(Vector2f pos) override;
498
502 void mouseMoved(Vector2f pos) override;
503
507 void keyPressed(const Event::KeyEvent& event) override;
508
518 bool canHandleKeyPress(const Event::KeyEvent& event) override;
519
523 void textEntered(char32_t key) override;
524
528 bool scrolled(float delta, Vector2f pos, bool touch) override;
529
533 void mouseNoLongerOnWidget() override;
534
538 void leftMouseButtonNoLongerDown() override;
539
543 void rightMouseButtonNoLongerDown() override;
544
547 // Shows the tool tip when the widget is located below the mouse.
548 // Returns its tool tip or the tool tip from a child widget if the mouse is on top of the widget.
549 // A nullptr is returned when the mouse is not on top of the widget or when the tool tip is empty.
551 [[nodiscard]] Widget::Ptr askToolTip(Vector2f mousePos) override;
552
555 // This function is called every frame with the time passed since the last frame.
557 bool updateTime(Duration elapsedTime) override;
558
563 void setParent(Container* parent) override;
564
569 void setParentGui(BackendGui* gui);
570
576 void updateChildrenWithAutoLayout();
577
579
580 protected:
586 void rendererChanged(const String& property) override;
587
594 void draw(BackendRenderTarget& target, RenderStates states) const override;
595
599 [[nodiscard]] std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
600
604 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
605 using Widget::load;
606
610 void updateTextSize() override;
611
613 // Checks above which widget the mouse is standing.
614 // If there is no widget below the mouse then this function will return a null pointer.
616 [[nodiscard]] Widget::Ptr getWidgetBelowMouse(Vector2f mousePos) const;
617
619 // Checks which widget is below the mouse and updates the cached value.
620 // Note that a nullptr is stored and returned if the widget that is found by getWidgetBelowMouse is disabled.
622 Widget::Ptr updateWidgetBelowMouse(Vector2f mousePos);
623
625 // Find out what the index of the focused widget is. Returns 0 when no widget is focused and index+1 otherwise.
627 [[nodiscard]] std::size_t getFocusedWidgetIndex() const;
628
630 // Try to focus the given child widget
632 bool tryFocusWidget(const Widget::Ptr& widget, bool reverseWidgetOrder, bool recursive);
633
635 // Transform the mouse position for the child widget based its origin, rotation and scaling.
637 [[nodiscard]] static Vector2f transformMousePos(const Widget::Ptr& widget, Vector2f mousePos);
638
640 // Finishes adding a widget to the container
642 void widgetAdded(const Widget::Ptr& widgetPtr);
643
645 // Turns texture and font filenames into paths relative to the form file
647 void injectFormFilePath(const std::unique_ptr<DataIO::Node>& node,
648 const String& path,
649 std::map<String, bool>& checkedFilenames) const;
650
661 void loadWidgetsFromNodeTree(const std::unique_ptr<DataIO::Node>& rootNode, const FormLoadOptions& options);
662
664
665 protected:
666 std::vector<Widget::Ptr> m_widgets;
667
668 Widget::Ptr m_widgetBelowMouse;
669 Widget::Ptr m_widgetWithLeftMouseDown;
670 Widget::Ptr m_widgetWithRightMouseDown;
671 Widget::Ptr m_focusedWidget;
672 bool m_draggingWidget = false;
673
674 Vector2f m_prevInnerSize;
675
676 // Does focusing the next widget always keep a widget from this container focused (e.g. in a ChildWindow)?
677 bool m_isolatedFocus = false;
678
679 friend class SubwidgetContainer; // Needs access to save and load functions
680
682 };
683
687 class TGUI_API RootContainer : public Container
688 {
689 public:
690 using Ptr = std::shared_ptr<RootContainer>;
691 using ConstPtr = std::shared_ptr<const RootContainer>;
692
693 static constexpr char StaticWidgetType[] = "RootContainer";
694
702 explicit RootContainer(const char* typeName = StaticWidgetType, bool initRenderer = true);
703
710 void setFocused(bool focused) override;
711
719 [[nodiscard]] bool isMouseOnWidget(Vector2f pos) const override;
720
724 void mouseNoLongerOnWidget() override;
725
732 void draw(BackendRenderTarget& target, RenderStates states) const override;
733
735
736 private:
738 // Returns a nullptr.
740 Widget::Ptr clone() const override
741 {
742 return nullptr;
743 }
744 };
745} // namespace tgui
746
747#endif // TGUI_CONTAINER_HPP
Base class for the Gui.
Definition BackendGui.hpp:45
Base class for render targets.
Definition BackendRenderTarget.hpp:46
virtual bool remove(const Widget::Ptr &widget)
Removes a single widget that was added to the container.
bool processMousePressEvent(Event::MouseButton button, Vector2f pos)
Inform the container about a mouse press event.
void saveWidgetsToFile(const String &filename) const
Saves the child widgets to a text file.
std::unique_ptr< DataIO::Node > saveWidgetsToNodeTree(const String &rootDirectory="") const
Saves the child widgets to a tree of nodes that contain all information about the widgets.
bool processMouseReleaseEvent(Event::MouseButton button, Vector2f pos)
Inform the container about a mouse release event.
Container(Container &&other) noexcept
Move constructor.
std::size_t moveWidgetBackward(const Widget::Ptr &widget)
Places a widget one step backward in the z-order.
Container & operator=(Container &&right) noexcept
Overload of move assignment operator.
bool focusPreviousWidget(bool recursive=true)
Focuses the previous widget in this container.
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.
~Container() override
Destructor.
bool canHandleKeyPress(const Event::KeyEvent &event) override
Called by the parent of the widget to check if keyPressed would process the event.
void draw(BackendRenderTarget &target, RenderStates states) const override
Draw the child widgets to a render target.
std::shared_ptr< Container > Ptr
Shared widget pointer.
Definition Container.hpp:47
bool processScrollEvent(float delta, Vector2f pos, bool touch)
Inform the container about a scroll event (either mouse wheel or two finger scrolling on touchscreen)...
void rendererChanged(const String &property) override
Function called when one of the properties of the renderer is changed.
virtual void add(const Widget::Ptr &widgetPtr, const String &widgetName="")
Adds a widget to the container.
Container & operator=(const Container &right)
Overload of copy assignment operator.
virtual void removeAllWidgets()
Removes all widgets that were added to the container.
void loadWidgetsFromStream(std::stringstream &&stream, bool replaceExisting=true)
Loads the child widgets from a string stream.
virtual Vector2f getInnerSize() const
Returns the space available for widgets inside the container.
Widget::Ptr get(const String &widgetName) const
Returns a pointer to a widget that was added earlier.
bool focusNextWidget(bool recursive=true)
Focuses the next widget in this container.
const std::vector< Widget::Ptr > & getWidgets() const
Returns a list of all the widgets in this container.
Definition Container.hpp:96
void loadWidgetsFromStream(std::stringstream &stream, const FormLoadOptions &options)
Loads the child widgets from a string stream.
void loadWidgetsFromNodeTree(const std::unique_ptr< DataIO::Node > &rootNode, bool replaceExisting=true)
Loads the child widgets from a tree of nodes that contain all information about the widgets.
void sortWidgets(Function &&function)
Sorts a list of all the widgets in this container.
Definition Container.hpp:108
void loadWidgetsFromNodeTree(const std::unique_ptr< DataIO::Node > &rootNode, const FormLoadOptions &options)
Loads child widgets from a parsed form root using renderer and theme data from the node tree and opti...
virtual Widget::Ptr getWidgetAtPos(Vector2f pos, bool recursive) const
Returns the widget that is located at the given position.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
void moveWidgetToFront(const Widget::Ptr &widget)
Places a widget before all other widgets, to the front of the z-order.
void loadWidgetsFromFile(const String &filename, bool replaceExisting=true)
Loads the child widgets from a text file.
void moveWidgetToBack(const Widget::Ptr &widget)
Places a widget behind all other widgets, to the back of the z-order.
virtual bool setWidgetIndex(const Widget::Ptr &widget, std::size_t index)
Changes the index of a widget in this container.
void loadWidgetsFromFile(const String &filename, const FormLoadOptions &options)
Loads the child widgets from a text file.
Container(const Container &other)
Copy constructor.
std::size_t moveWidgetForward(const Widget::Ptr &widget)
Places a widget one step forward in the z-order.
virtual Vector2f getChildWidgetsOffset() const
Returns the distance between the position of the container and a widget that would be drawn inside th...
Definition Container.hpp:185
int getWidgetIndex(const Widget::Ptr &widget) const
Returns the current index of a widget in this container.
void loadWidgetsFromStream(std::stringstream &&stream, const FormLoadOptions &options)
Loads the child widgets from a string stream with load options.
bool leftMousePressed(Vector2f pos) override
Called by the parent when the left mouse button goes down on top of the widget.
void updateTextSize() override
Called when the text size is changed (either by setTextSize or via the renderer).
bool processTextEnteredEvent(char32_t key)
Inform the container about a key press event.
void setFocused(bool focused) override
Focus or unfocus the widget.
bool processKeyPressEvent(Event::KeyEvent event)
Inform the container about a key press event.
bool scrolled(float delta, Vector2f pos, bool touch) override
Called by the parent on scroll event (either from mouse wheel of from two finger scrolling on a touch...
void saveWidgetsToStream(std::stringstream &stream, const String &rootDirectory="") const
Saves the child widgets to a text file.
void setSize(const Layout2d &size) override
Changes the size of the container.
void loadWidgetsFromStream(std::stringstream &stream, bool replaceExisting=true)
Loads the child widgets from a string stream.
bool processMouseMoveEvent(Vector2f pos)
Inform the container about a mouse move event.
virtual Widget::Ptr getWidgetAtPosition(Vector2f pos) const
Returns the leaf child widget that is located at the given position.
WidgetType::Ptr get(const String &widgetName) const
Returns a pointer to a widget that was added earlier.
Definition Container.hpp:154
Widget::Ptr getFocusedChild() const
Returns the child widget that is focused inside this container.
Widget::Ptr getFocusedLeaf() const
Returns the leaf child widget that is focused inside this container.
std::shared_ptr< const Container > ConstPtr
Shared constant widget pointer.
Definition Container.hpp:48
Wrapper for durations.
Definition Duration.hpp:52
Class to store the position or size of a widget.
Definition Layout.hpp:320
std::shared_ptr< RootContainer > Ptr
Shared widget pointer.
Definition Container.hpp:690
std::shared_ptr< const RootContainer > ConstPtr
Shared constant widget pointer.
Definition Container.hpp:691
static constexpr char StaticWidgetType[]
Type name of the widget.
Definition Container.hpp:693
bool isMouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
void draw(BackendRenderTarget &target, RenderStates states) const override
Draws all widgets to a render target.
void setFocused(bool focused) override
Focus or unfocus the widget.
Wrapper class to store strings.
Definition String.hpp:94
virtual void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers)
Loads the widget from a tree of nodes.
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
KeyPressed event parameters.
Definition Event.hpp:166
MouseButton
Mouse buttons.
Definition Event.hpp:147
Options for loadWidgetsFromFile / loadWidgetsFromStream.
Definition FormLoadOptions.hpp:42
States used for drawing.
Definition RenderStates.hpp:38