FifeGUI 0.2.0
A C++ GUI library designed for games.
container.hpp
1// SPDX-License-Identifier: LGPL-2.1-or-later OR BSD-3-Clause
2// SPDX-FileCopyrightText: 2004 - 2008 Olof Naessén and Per Larsson
3// SPDX-FileCopyrightText: 2013 - 2026 Fifengine contributors
4
5#ifndef INCLUDE_FIFECHAN_WIDGETS_CONTAINER_HPP_
6#define INCLUDE_FIFECHAN_WIDGETS_CONTAINER_HPP_
7
8#include <list>
9#include <memory>
10#include <string>
11
12#include "fifechan/containerlistener.hpp"
13#include "fifechan/graphics.hpp"
14#include "fifechan/platform.hpp"
15#include "fifechan/widget.hpp"
16
17namespace fcn
18{
31 class FIFEGUI_API Container : public Widget
32 {
33 public:
37 enum class LayoutPolicy : uint8_t
38 {
39 Absolute,
40 AutoSize,
41 Vertical,
42 Horizontal,
43 Circular
44 };
45
53
54 ~Container() override;
55
56 Container(Container const &) = delete;
57 Container& operator=(Container const &) = delete;
58 Container(Container&&) = delete;
59 Container& operator=(Container&&) = delete;
60
73 virtual void setOpaque(bool opaque);
74
81 virtual bool isOpaque() const;
82
89 virtual void add(Widget* widget);
90
96 virtual void addWidget(std::unique_ptr<Widget> widget);
97
108 virtual void add(Widget* widget, int x, int y);
109
117 virtual void addWidget(std::unique_ptr<Widget> widget, int x, int y);
118
127 void remove(Widget* widget) override;
128
134 void removeAllChildren() override;
135
143 Widget* findWidgetById(std::string const & id) override;
144
152 void addContainerListener(ContainerListener* containerListener);
153
159 void removeContainerListener(ContainerListener* containerListener);
160
167 Widget* getChild(unsigned int index) const;
168
169 // Inherited from Widget
170
173
179 void resizeToContent(bool recursion) override;
180
184 void adjustSize() override;
185
191 void expandContent(bool recursion) override;
192 void draw(Graphics* graphics) override;
193 Rectangle getChildrenArea() override;
194 bool isLayouted() override
195 {
196 return mLayout != LayoutPolicy::Absolute;
197 };
198
206 virtual void setLayout(LayoutPolicy policy);
207
215 virtual LayoutPolicy getLayout() const;
216
225 virtual void setUniformSize(bool uniform);
226
234 virtual bool isUniformSize() const;
235
242 virtual void setVerticalSpacing(unsigned int spacing);
243
250 virtual unsigned int getVerticalSpacing() const;
251
258 virtual void setHorizontalSpacing(unsigned int spacing);
259
266 virtual unsigned int getHorizontalSpacing() const;
267
273 void setBackgroundWidget(Widget* widget);
274
280 Widget* getBackgroundWidget();
281
282 protected:
289 void distributeWidgetAddedEvent(Widget* source);
290
297 void distributeWidgetRemovedEvent(Widget* source);
298
302 bool mOpaque{true};
303
307 using ContainerListenerList = std::list<ContainerListener*>;
308
313
317 using ContainerListenerIterator = ContainerListenerList::iterator;
318
322 LayoutPolicy mLayout{LayoutPolicy::Absolute};
323
327 bool mUniform{false};
328
332 unsigned int mVerticalSpacing{2};
333
337 unsigned int mHorizontalSpacing{2};
338
343 };
344} // namespace fcn
345
346#endif // INCLUDE_FIFECHAN_WIDGETS_CONTAINER_HPP_
Interface for listening to container modification events.
bool isLayouted() override
Helper function to decide if we need to layout.
void remove(Widget *widget) override
Removes a widget from the Container.
Definition container.cpp:99
void draw(Graphics *graphics) override
Draws the widget.
Definition container.cpp:22
bool mOpaque
True if the container is opaque, false otherwise.
void addContainerListener(ContainerListener *containerListener)
Adds a container listener to the container.
Container()
Constructor.
unsigned int mVerticalSpacing
VerticalSpacing.
LayoutPolicy mLayout
Layout.
unsigned int mHorizontalSpacing
HorizontalSpacing.
Widget * mBackgroundWidget
Optional widget that is rendered behind other children as the container background.
Widget * findWidgetById(std::string const &id) override
Finds a widget given an id.
virtual bool isOpaque() const
Checks if the container is opaque or not.
Definition container.cpp:61
void expandContent(bool recursion) override
Expand children to occupy available space in this container.
virtual void add(Widget *widget)
Adds a widget to the container.
Definition container.cpp:66
LayoutPolicy
The layout policy of the container.
Definition container.hpp:38
void resizeToContent(bool recursion) override
Resize this container to fit its children.
ContainerListenerList::iterator ContainerListenerIterator
Typedef.
void removeAllChildren() override
Removes all widgets from the the container.
Rectangle getChildrenArea() override
Gets the area of the widget occupied by the widget's children.
virtual void addWidget(std::unique_ptr< Widget > widget)
Adds a widget to the container, transferring ownership.
Definition container.cpp:72
bool mUniform
Indicates if the childs should be expanded to a uniform size.
void removeContainerListener(ContainerListener *containerListener)
Removes a container listener from the container.
void adjustSize() override
Adjust the size of the container after layout computations.
Widget * getChild(unsigned int index) const
Gets child by index.
ContainerListenerList mContainerListeners
The container listeners of the container.
virtual void setOpaque(bool opaque)
Sets the container to be opaque or not.
Definition container.cpp:56
std::list< ContainerListener * > ContainerListenerList
Typdef.
Abstract interface providing primitive drawing functions (lines, rectangles, etc.).
Definition graphics.hpp:57
Represents a rectangular area (X, Y, Width, Height).
Definition rectangle.hpp:20
Abstract base class defining the common behavior, properties, and lifecycle of all GUI elements.
Definition widget.hpp:45
Widget()
Constructor.
Definition widget.cpp:36
void expandContent()
Expands the child widgets to the size of this widget, calls recursively all childs.
Definition widget.hpp:1440
void resizeToContent()
Resizes the widget's size to fit the content exactly, calls recursively all childs.
Definition widget.hpp:1417