FifeGUI 0.3.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// Standard library includes
9#include <list>
10#include <memory>
11#include <string>
12
13// Platform config include
14#include "fifechan/platform.hpp"
15
16// Project headers (subdirs before local)
17#include "fifechan/graphics.hpp"
18#include "fifechan/listeners/containerlistener.hpp"
19#include "fifechan/widget.hpp"
20
21namespace fcn
22{
35 class FIFEGUI_API Container : public Widget
36 {
37 public:
49 enum class LayoutPolicy : uint8_t
50 {
51 Absolute = 0,
52 AutoSize,
53 Vertical,
54 Horizontal,
55 Circular
56 };
57
73
74 ~Container() override;
75
76 Container(Container const &) = delete;
77 Container& operator=(Container const &) = delete;
78 Container(Container&&) = delete;
79 Container& operator=(Container&&) = delete;
80
90 virtual void setOpacity(float opacity);
91
106 virtual void setOpaque(bool opaque);
107
114 virtual bool isOpaque() const;
115
122 virtual void add(Widget* widget);
123
129 virtual void addWidget(std::unique_ptr<Widget> widget);
130
141 virtual void add(Widget* widget, int x, int y);
142
150 virtual void addWidget(std::unique_ptr<Widget> widget, int x, int y);
151
160 void remove(Widget* widget) override;
161
167 void removeAllChildren() override;
168
176 Widget* findWidgetById(std::string const & id) override;
177
185 void addContainerListener(ContainerListener* containerListener);
186
192 void removeContainerListener(ContainerListener* containerListener);
193
200 Widget* getChild(unsigned int index) const;
201
202 // Inherited from Widget
203
206
212 void resizeToContent(bool recursion = true) override;
213
217 void adjustSize() override;
218
224 void expandContent(bool recursion) override;
225 void draw(Graphics* graphics) override;
226 Rectangle getChildrenArea() override;
227 bool isLayouted() override
228 {
229 return mLayout != LayoutPolicy::Absolute;
230 };
231
239 virtual void setLayout(LayoutPolicy policy);
240
248 virtual LayoutPolicy getLayout() const;
249
258 virtual void setUniformSize(bool uniform);
259
267 virtual bool isUniformSize() const;
268
275 virtual void setVerticalSpacing(unsigned int spacing);
276
283 virtual unsigned int getVerticalSpacing() const;
284
291 virtual void setHorizontalSpacing(unsigned int spacing);
292
299 virtual unsigned int getHorizontalSpacing() const;
300
306 void setBackgroundWidget(Widget* widget);
307
313 Widget* getBackgroundWidget();
314
315 protected:
322 void distributeWidgetAddedEvent(Widget* source);
323
330 void distributeWidgetRemovedEvent(Widget* source);
331
335 float mOpacity{1.0F};
336
340 bool mOpaque{true};
341
345 using ContainerListenerList = std::list<ContainerListener*>;
346
351
353 using ContainerListenerIterator = ContainerListenerList::iterator;
354
358 LayoutPolicy mLayout{LayoutPolicy::Absolute};
359
363 bool mUniform{false};
364
368 unsigned int mVerticalSpacing{2};
369
373 unsigned int mHorizontalSpacing{2};
374
379 };
380} // namespace fcn
381
382#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.
void draw(Graphics *graphics) override
Draws the widget.
Definition container.cpp:25
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.
virtual void setOpacity(float opacity)
Sets the opacity of the container.
Definition container.cpp:72
Widget * findWidgetById(std::string const &id) override
Finds a widget given an id.
void resizeToContent(bool recursion=true) override
Resize this container to fit its children.
virtual bool isOpaque() const
Checks if the container is opaque or not.
Definition container.cpp:88
void expandContent(bool recursion) override
Expand children to occupy available space in this container.
float mOpacity
Opacity of the container, between 0.0 (fully transparent) and 1.0 (fully opaque).
virtual void add(Widget *widget)
Adds a widget to the container.
Definition container.cpp:93
LayoutPolicy
The layout policy of the container.
Definition container.hpp:50
ContainerListenerList::iterator ContainerListenerIterator
Iterator for ContainerListenerList.
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:99
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:83
std::list< ContainerListener * > ContainerListenerList
Typdef.
Abstract interface providing primitive drawing functions (lines, rectangles, etc.).
Definition graphics.hpp:58
Represents a rectangular area (X, Y, Width, Height).
Definition rectangle.hpp:22
Abstract base class defining the common behavior, properties, and lifecycle of all GUI elements.
Definition widget.hpp:56
Widget()
Constructor.
Definition widget.cpp:52
void expandContent()
Expands the child widgets to the size of this widget, calls recursively all childs.
Definition widget.hpp:1597
virtual void resizeToContent(bool recursion=true)
Resizes the widget's size to fit the content exactly, calls recursively all childs.
Definition widget.hpp:1580
Used replacement tokens by configure_file():