FifeGUI 0.2.0
A C++ GUI library designed for games.
togglebutton.cpp
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#include "fifechan/widgets/togglebutton.hpp"
6
7#include <string>
8
9namespace fcn
10{
12
13 ToggleButton::ToggleButton()
14 {
15 setSelected(false);
16 adjustSize();
17 }
18
19 ToggleButton::ToggleButton(std::string const & caption, std::string const & group, bool selected)
20 {
21 setCaption(caption);
22 setGroup(group);
23 setSelected(selected);
24 adjustSize();
25 }
26
27 ToggleButton::~ToggleButton()
28 {
29 // Remove us from the group list
30 setGroup("");
31 }
32
34 {
35 return mSelected;
36 }
37
38 void ToggleButton::setSelected(bool selected)
39 {
40 if (selected && !mGroup.empty()) {
41 // deselect all buttons in group
42 GroupIterator iter;
43 GroupIterator iterEnd;
44 iterEnd = mGroupMap.upper_bound(mGroup);
45
46 for (iter = mGroupMap.lower_bound(mGroup); iter != iterEnd; ++iter) {
47 if (iter->second->isSelected()) {
48 iter->second->setSelected(false);
49 }
50 }
51 }
52
53 mSelected = selected;
54 }
55
61
62 void ToggleButton::setGroup(std::string const & group)
63 {
64 // Remove button from previous group
65 if (!mGroup.empty()) {
66 GroupIterator iter;
67 GroupIterator iterEnd;
68 iterEnd = mGroupMap.upper_bound(mGroup);
69
70 for (iter = mGroupMap.lower_bound(mGroup); iter != iterEnd; ++iter) {
71 if (iter->second == this) {
72 mGroupMap.erase(iter);
73 break;
74 }
75 }
76 }
77 // Add button to new group
78 if (!group.empty()) {
79 mGroupMap.insert(std::pair<std::string, ToggleButton*>(group, this));
80 }
81
82 mGroup = group;
83 }
84
85 std::string const & ToggleButton::getGroup() const
86 {
87 return mGroup;
88 }
89
91 {
92 return isSelected();
93 }
94
96 {
97 Key const key = keyEvent.getKey();
98
99 if ((key.getValue() == Key::Enter || key.getValue() == Key::Space) && mKeyPressed) {
100 mKeyPressed = false;
102 keyEvent.consume();
103 }
104 }
105
107 {
108 if (mouseEvent.getButton() == MouseEvent::Button::Left && mMousePressed && mHasMouse) {
109 mMousePressed = false;
111 mouseEvent.consume();
112 } else if (mouseEvent.getButton() == MouseEvent::Button::Left) {
113 mMousePressed = false;
114 mouseEvent.consume();
115 }
116 }
117
118} // namespace fcn
bool mHasMouse
True if the mouse is on top of the button, false otherwise.
Definition button.hpp:201
bool mKeyPressed
True if a key has been pressed, false otherwise.
Definition button.hpp:206
bool mMousePressed
True if a mouse has been pressed, false otherwise.
Definition button.hpp:211
void setCaption(std::string const &caption)
Sets the caption of the button.
Definition button.cpp:43
void adjustSize() override
Adjust internal size after layout or image changes.
void consume()
Marks the event as consumed.
Represents a key event.
Definition keyevent.hpp:22
Key const & getKey() const
Gets the key of the event.
Definition keyevent.cpp:39
Represents a keyboard key or character code.
Definition key.hpp:20
int getValue() const
Gets the value of the key.
Definition key.cpp:28
Represents a mouse event.
MouseEvent::Button getButton() const
Gets the button of the mouse event.
virtual bool isSelected() const
Checks if the check box is selected.
void mouseReleased(MouseEvent &mouseEvent) override
Called when a mouse button has been released on the widget area.
bool isPressed() const override
Checks if the button is pressed.
bool mSelected
True if the check box is selected, false otherwise.
std::multimap< std::string, ToggleButton * > GroupMap
Typdef.
virtual void toggleSelected()
Toggles the check box between being selected and not being selected.
virtual void setSelected(bool selected)
Sets the check box to be selected or not.
GroupMap::iterator GroupIterator
Typdef.
void keyReleased(KeyEvent &keyEvent) override
Called if a key is released when the widget has keyboard focus.
void setGroup(std::string const &group)
Sets the group the toggle button should belong to.
std::string mGroup
Holds the group of the toggle button.
static GroupMap mGroupMap
Holds all available toggle button groups.
std::string const & getGroup() const
Gets the group the toggle button belongs to.
void distributeActionEvent()
Distributes an action event to all action listeners of the widget.
Definition widget.cpp:1113