FifeGUI 0.3.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#include <cassert>
7
8// Project headers
9#include "fifechan/widgets/horizontalbar.hpp"
10
11namespace fcn
12{
14 {
15 setLayout(LayoutPolicy::Horizontal);
16 setOpaque(true);
17 }
18
19 void HorizontalBar::setSpacing(unsigned int spacing)
20 {
21 mSpacing = spacing;
22 setHorizontalSpacing(spacing);
23 }
24
25 unsigned int HorizontalBar::getSpacing() const
26 {
27 return mSpacing;
28 }
29
30 // cppcheck-suppress duplInheritedMember
31 void HorizontalBar::setPadding(unsigned int padding)
32 {
33 mPadding = padding;
34 }
35
36 unsigned int HorizontalBar::getPadding() const
37 {
38 return mPadding;
39 }
40
41 void HorizontalBar::setFixedHeight(unsigned int height)
42 {
43 mFixedHeight = height;
44 }
45
46 unsigned int HorizontalBar::getFixedHeight() const
47 {
48 return mFixedHeight;
49 }
50
52 {
53 mClipping = clip;
54 }
55
57 {
58 return mClipping;
59 }
60
62 {
63 mExpandChildren = expand;
64 }
65
67 {
68 return mExpandChildren;
69 }
70
71 void HorizontalBar::resizeToContent(bool recursion)
72 {
73 assert("layout must be horizontal" && getLayout() == LayoutPolicy::Horizontal);
74 // Disable all expansion to prevent equal sizing
75 bool const savedHExpand = isHorizontalExpand();
77
78 bool const savedUniform = isUniformSize();
79 setUniformSize(false);
80
82
83 // Post-process: shrink bar to fit content exactly (don't expand to container width)
84 if (!mExpandChildren) {
85 int contentW = 0;
86 for (auto const * child : getChildren()) {
87 if (child == nullptr || !child->isVisible()) {
88 continue;
89 }
90 int const childEnd = child->getX() + child->getWidth() + child->getMarginRight();
91 contentW = std::max(contentW, childEnd);
92 }
93 contentW += getPaddingLeft() + getPaddingRight();
94 if (contentW > 0 && contentW < getWidth()) {
95 setWidth(contentW);
96 }
97 }
98
99 setHorizontalExpand(savedHExpand);
100 setUniformSize(savedUniform);
101
102 // Apply fixed height if set
103 if (mFixedHeight > 0) {
105 }
106 }
107
109 {
110 assert("layout must be horizontal" && getLayout() == LayoutPolicy::Horizontal);
111 // Disable all expansion to prevent equal sizing
112 bool const savedHExpand = isHorizontalExpand();
113 setHorizontalExpand(false);
114
115 bool const savedUniform = isUniformSize();
116 setUniformSize(false);
117
119
120 // Post-process: shrink bar to fit content exactly
121 if (!mExpandChildren) {
122 int contentW = 0;
123 for (auto const * child : getChildren()) {
124 if (child == nullptr || !child->isVisible()) {
125 continue;
126 }
127 int const childEnd = child->getX() + child->getWidth() + child->getMarginRight();
128 contentW = std::max(contentW, childEnd);
129 }
130 contentW += getPaddingLeft() + getPaddingRight();
131 if (contentW > 0 && contentW < getWidth()) {
132 setWidth(contentW);
133 }
134 }
135
136 setHorizontalExpand(savedHExpand);
137 setUniformSize(savedUniform);
138
139 // Apply fixed height if set
140 if (mFixedHeight > 0) {
142 }
143 }
144
146 {
147 assert("graphics must not be null" && graphics != nullptr);
148 // Draw background
149 if (isOpaque()) {
150 graphics->setColor(getBackgroundColor());
151 graphics->fillRectangle(Rectangle(0, 0, getWidth(), getHeight()));
152
153 // Draw 1px border at bottom
154 graphics->setColor(getForegroundColor());
155 graphics->drawLine(0, getHeight() - 1, getWidth(), getHeight() - 1);
156 }
157
158 // Draw children
159 Container::draw(graphics);
160 }
161} // 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:88
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...
virtual LayoutPolicy getLayout() const
Gets the layout of the container.
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:83
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:1589
bool isHorizontalExpand() const
Gets if widget is horizontal expandable.
Definition widget.cpp:440
int getWidth() const
Gets the width of the widget.
Definition widget.cpp:252
unsigned int getPaddingLeft() const
Gets the left padding.
Definition widget.cpp:604
Color const & getBackgroundColor() const
Gets the background color.
Definition widget.cpp:762
void setHorizontalExpand(bool expand)
Sets the widget to horizontal expandable.
Definition widget.cpp:435
virtual void setWidth(int width)
Sets the width of the widget.
Definition widget.cpp:244
Color const & getForegroundColor() const
Gets the foreground color.
Definition widget.cpp:752
int getHeight() const
Gets the height of the widget.
Definition widget.cpp:265
virtual void setHeight(int height)
Sets the height of the widget.
Definition widget.cpp:257
unsigned int getPaddingRight() const
Gets the right padding.
Definition widget.cpp:584
Used replacement tokens by configure_file():