FifeGUI 0.2.0
A C++ GUI library designed for games.
activitybar.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/graphics.hpp"
9#include "fifechan/widgets/activitybar.hpp"
10#include "fifechan/widgets/togglebutton.hpp"
11
12namespace fcn
13{
14 ActivityBar::ActivityBar(int width) : mWidth(width)
15 {
16 setLayout(LayoutPolicy::Vertical);
17 setOpaque(true);
18 }
19
20 void ActivityBar::setWidth(int width)
21 {
22 mWidth = width;
23 // Ensure widget dimension reflects the configured width
24 setSize(mWidth, getHeight());
25 }
26
27 // cppcheck-suppress duplInheritedMember
29 {
30 return mWidth;
31 }
32
33 void ActivityBar::setSpacing(unsigned int spacing)
34 {
35 mSpacing = spacing;
36 setVerticalSpacing(spacing);
37 }
38
39 unsigned int ActivityBar::getSpacing() const
40 {
41 return mSpacing;
42 }
43
44 // cppcheck-suppress duplInheritedMember
45 void ActivityBar::addActionListener(ActionListener* listener)
46 {
47 mActionListeners.push_back(listener);
48 }
49
50 // cppcheck-suppress constParameterPointer
51 // cppcheck-suppress duplInheritedMember
52 void ActivityBar::removeActionListener(ActionListener* listener)
53 {
54 mActionListeners.remove(listener);
55 }
56
58 {
59 for (auto* child : getChildren()) {
60 auto* toggle = dynamic_cast<ToggleButton*>(child);
61 if (toggle != nullptr) {
62 toggle->setSelected(true);
63 }
64 }
65 }
66
68 {
69 for (auto* child : getChildren()) {
70 auto* toggle = dynamic_cast<ToggleButton*>(child);
71 if (toggle != nullptr) {
72 toggle->setSelected(false);
73 }
74 }
75 }
76
77 void ActivityBar::resizeToContent(bool recursion)
78 {
80 // Enforce configured width after layout
81 setSize(mWidth, getHeight());
82 }
83
85 {
87 // Enforce configured width after adjust
88 setSize(mWidth, getHeight());
89 }
90
92 {
93 // Draw background
94 if (isOpaque()) {
95 graphics->setColor(getBackgroundColor());
96 graphics->fillRectangle(Rectangle(0, 0, getWidth(), getHeight()));
97
98 // Draw border line on right edge
99 graphics->setColor(getForegroundColor());
100 graphics->drawLine(getWidth() - 1, 0, getWidth() - 1, getHeight());
101 }
102
103 // Draw children
104 Container::draw(graphics);
105 }
106
108 {
109 // Forward action events to all listeners
110 for (auto* listener : mActionListeners) {
111 listener->action(event);
112 }
113 }
114} // namespace fcn
Represents an action trigger (e.g., button click).
void hideAll()
Hides all panels (toggles all buttons to unselected state).
void showAll()
Shows all panels (toggles all buttons to selected state).
void addActionListener(ActionListener *listener)
Adds an action listener to the activity bar.
void resizeToContent(bool recursion=true) override
Resizes the widget's size to fit the content exactly, calls recursively all childs.
void adjustSize() override
Resizes the widget's size to fit the content exactly.
ActivityBar(int width=48)
Constructor.
void setSpacing(unsigned int spacing)
Sets the spacing between items.
void removeActionListener(ActionListener *listener)
Removes an action listener from the activity bar.
unsigned int getSpacing() const
Gets the spacing between items.
void setWidth(int width) override
Sets the width of the activity bar.
void action(ActionEvent const &event) override
Handles an action event emitted by a widget.
int getWidth() const
Gets the width of the activity bar.
void draw(Graphics *graphics) override
Draws the widget.
void draw(Graphics *graphics) override
Draws the widget.
Definition container.cpp:25
virtual void setLayout(LayoutPolicy policy)
Sets the layout of the container.
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 setVerticalSpacing(unsigned int spacing)
Set the vertical spacing between rows.
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.
Represents a rectangular area (X, Y, Width, Height).
Definition rectangle.hpp:22
An implementation of a toggleable button.
std::list< Widget * > const & getChildren() const
Gets the children of the widget.
Definition widget.cpp:1592
virtual void setSize(int width, int height)
Sets the size of the widget.
Definition widget.cpp:1064
Color const & getBackgroundColor() const
Gets the background color.
Definition widget.cpp:761
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
Used replacement tokens by configure_file():