FifeGUI 0.2.0
A C++ GUI library designed for games.
horizontalbar.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later OR BSD-3-Clause
2// SPDX-FileCopyrightText: 2026 Fifengine contributors
3
4// Standard library includes
5#include <algorithm>
6
7// Project headers
8#include "fifechan/widgets/horizontalbar.hpp"
9
10namespace fcn
11{
13 {
14 setLayout(LayoutPolicy::Horizontal);
15 setOpaque(true);
16 }
17
18 void HorizontalBar::setSpacing(unsigned int spacing)
19 {
20 mSpacing = spacing;
21 setHorizontalSpacing(spacing);
22 }
23
24 unsigned int HorizontalBar::getSpacing() const
25 {
26 return mSpacing;
27 }
28
29 // cppcheck-suppress duplInheritedMember
30 void HorizontalBar::setPadding(unsigned int padding)
31 {
32 mPadding = padding;
33 }
34
35 unsigned int HorizontalBar::getPadding() const
36 {
37 return mPadding;
38 }
39
40 void HorizontalBar::setFixedHeight(unsigned int height)
41 {
42 mFixedHeight = height;
43 }
44
45 unsigned int HorizontalBar::getFixedHeight() const
46 {
47 return mFixedHeight;
48 }
49
51 {
52 mClipping = clip;
53 }
54
56 {
57 return mClipping;
58 }
59
61 {
62 mExpandChildren = expand;
63 }
64
66 {
67 return mExpandChildren;
68 }
69
70 void HorizontalBar::resizeToContent(bool recursion)
71 {
72 // Disable all expansion to prevent equal sizing
73 bool const savedHExpand = isHorizontalExpand();
75
76 bool const savedUniform = isUniformSize();
77 setUniformSize(false);
78
80
81 // Post-process: shrink bar to fit content exactly (don't expand to container width)
82 if (!mExpandChildren) {
83 int contentW = 0;
84 for (auto const * child : getChildren()) {
85 if (child == nullptr || !child->isVisible()) {
86 continue;
87 }
88 int const childEnd = child->getX() + child->getWidth() + child->getMarginRight();
89 contentW = std::max(contentW, childEnd);
90 }
91 contentW += getPaddingLeft() + getPaddingRight();
92 if (contentW > 0 && contentW < getWidth()) {
93 setWidth(contentW);
94 }
95 }
96
97 setHorizontalExpand(savedHExpand);
98 setUniformSize(savedUniform);
99
100 // Apply fixed height if set
101 if (mFixedHeight > 0) {
103 }
104 }
105
107 {
108 // Disable all expansion to prevent equal sizing
109 bool const savedHExpand = isHorizontalExpand();
110 setHorizontalExpand(false);
111
112 bool const savedUniform = isUniformSize();
113 setUniformSize(false);
114
116
117 // Post-process: shrink bar to fit content exactly
118 if (!mExpandChildren) {
119 int contentW = 0;
120 for (auto const * child : getChildren()) {
121 if (child == nullptr || !child->isVisible()) {
122 continue;
123 }
124 int const childEnd = child->getX() + child->getWidth() + child->getMarginRight();
125 contentW = std::max(contentW, childEnd);
126 }
127 contentW += getPaddingLeft() + getPaddingRight();
128 if (contentW > 0 && contentW < getWidth()) {
129 setWidth(contentW);
130 }
131 }
132
133 setHorizontalExpand(savedHExpand);
134 setUniformSize(savedUniform);
135
136 // Apply fixed height if set
137 if (mFixedHeight > 0) {
139 }
140 }
141
143 {
144 // Draw background
145 if (isOpaque()) {
146 graphics->setColor(getBackgroundColor());
147 graphics->fillRectangle(Rectangle(0, 0, getWidth(), getHeight()));
148
149 // Draw 1px border at bottom
150 graphics->setColor(getForegroundColor());
151 graphics->drawLine(0, getHeight() - 1, getWidth(), getHeight() - 1);
152 }
153
154 // Draw children
155 Container::draw(graphics);
156 }
157} // namespace fcn
virtual bool isUniformSize() const
True if the container tries to expand the childs to a uniform size.
void draw(Graphics *graphics) override
Draws the widget.
Definition container.cpp:25
virtual void setLayout(LayoutPolicy policy)
Sets the layout of the container.
virtual void setHorizontalSpacing(unsigned int spacing)
Set the horizontal spacing between columns.
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:87
virtual void setUniformSize(bool uniform)
If enabled, the free space is distributed in a way that the size of the childrens will be equal (if p...
void adjustSize() override
Adjust the size of the container after layout computations.
virtual void setOpaque(bool opaque)
Sets the container to be opaque or not.
Definition container.cpp:82
Abstract interface providing primitive drawing functions (lines, rectangles, etc.).
Definition graphics.hpp:58
virtual void drawLine(int x1, int y1, int x2, int y2)=0
Draws a line.
virtual void setColor(Color const &color)=0
Sets the color to use when drawing.
virtual void fillRectangle(Rectangle const &rectangle)=0
Draws a filled rectangle.
unsigned int getFixedHeight() const
Gets the fixed height of the bar.
unsigned int mFixedHeight
Fixed height (0 = content-driven).
bool isExpandChildren() const
Checks if children expand to fill available width.
HorizontalBar()
Constructor.
unsigned int mSpacing
Spacing between children.
unsigned int mPadding
Padding around the bar content.
void setSpacing(unsigned int spacing)
Sets the spacing between children.
void draw(Graphics *graphics) override
Draws the widget.
bool mExpandChildren
Whether children should expand to fill width.
bool mClipping
Whether to clip overflowing content.
void setExpandChildren(bool expand)
Sets whether children should expand to fill available width.
void setClipping(bool clip)
Sets the overflow policy.
unsigned int getSpacing() const
Gets the spacing between children.
bool isClipping() const
Checks if overflowing children are clipped.
void setFixedHeight(unsigned int height)
Sets the default height of the bar.
void setPadding(unsigned int padding)
Sets the padding around the bar content.
unsigned int getPadding() const
Gets the padding around the bar content.
void adjustSize() override
Adjust the size of the container after layout computations.
void resizeToContent(bool recursion=true) override
Resize this container to fit its children.
Represents a rectangular area (X, Y, Width, Height).
Definition rectangle.hpp:22
std::list< Widget * > const & getChildren() const
Gets the children of the widget.
Definition widget.cpp:1592
bool isHorizontalExpand() const
Gets if widget is horizontal expandable.
Definition widget.cpp:439
int getWidth() const
Gets the width of the widget.
Definition widget.cpp:251
unsigned int getPaddingLeft() const
Gets the left padding.
Definition widget.cpp:603
Color const & getBackgroundColor() const
Gets the background color.
Definition widget.cpp:761
void setHorizontalExpand(bool expand)
Sets the widget to horizontal expandable.
Definition widget.cpp:434
virtual void setWidth(int width)
Sets the width of the widget.
Definition widget.cpp:243
Color const & getForegroundColor() const
Gets the foreground color.
Definition widget.cpp:751
int getHeight() const
Gets the height of the widget.
Definition widget.cpp:264
virtual void setHeight(int height)
Sets the height of the widget.
Definition widget.cpp:256
unsigned int getPaddingRight() const
Gets the right padding.
Definition widget.cpp:583
Used replacement tokens by configure_file():