FifeGUI 0.3.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// Corresponding header include
6#include "fifechan/widgets/togglebutton.hpp"
7
8// Standard library includes
9#include <string>
10
11// Platform config include
12#include "fifechan/platform.hpp"
13
14namespace fcn
15{
17
18 ToggleButton::ToggleButton()
19 {
20 adjustSize();
21 }
22
23 ToggleButton::ToggleButton(std::string const & caption, std::string const & group, bool selected) :
24 mSelected(selected)
25 {
26 setCaption(caption);
27 setGroup(group);
28 adjustSize();
29 }
30
31 ToggleButton::~ToggleButton()
32 {
33 // Remove us from the group list
34 setGroup("");
35 }
36
38 {
39 return mSelected;
40 }
41
42 void ToggleButton::setSelected(bool selected)
43 {
44 if (selected && !mGroup.empty()) {
45 // deselect all buttons in group
46 GroupIterator iter;
47 GroupIterator iterEnd;
48 iterEnd = mGroupMap.upper_bound(mGroup);
49
50 for (iter = mGroupMap.lower_bound(mGroup); iter != iterEnd; ++iter) {
51 if (iter->second->isSelected()) {
52 iter->second->setSelected(false);
53 }
54 }
55 }
56
57 mSelected = selected;
58 }
59
65
66 void ToggleButton::setGroup(std::string const & group)
67 {
68 // Remove button from previous group
69 if (!mGroup.empty()) {
70 GroupIterator iter;
71 GroupIterator iterEnd;
72 iterEnd = mGroupMap.upper_bound(mGroup);
73
74 for (iter = mGroupMap.lower_bound(mGroup); iter != iterEnd; ++iter) {
75 if (iter->second == this) {
76 mGroupMap.erase(iter);
77 break;
78 }
79 }
80 }
81 // Add button to new group
82 if (!group.empty()) {
83 mGroupMap.insert(std::pair<std::string, ToggleButton*>(group, this));
84 }
85
86 mGroup = group;
87 }
88
89 std::string const & ToggleButton::getGroup() const
90 {
91 return mGroup;
92 }
93
95 {
96 return isSelected();
97 }
98
100 {
101 Key const key = keyEvent.getKey();
102
103 if ((key.getValue() == fcn::Key::KEY_RETURN || key.getValue() == fcn::Key::SPACE) && mKeyPressed) {
104 mKeyPressed = false;
106 keyEvent.consume();
107 }
108 }
109
111 {
112 if (mouseEvent.getButton() == MouseEvent::Button::Left && mMousePressed && mHasMouse) {
113 mMousePressed = false;
115 mouseEvent.consume();
116 } else if (mouseEvent.getButton() == MouseEvent::Button::Left) {
117 mMousePressed = false;
118 mouseEvent.consume();
119 }
120 }
121
122} // namespace fcn
bool mHasMouse
True if the mouse is on top of the button, false otherwise.
Definition button.hpp:205
bool mKeyPressed
True if a key has been pressed, false otherwise.
Definition button.hpp:210
bool mMousePressed
True if a mouse has been pressed, false otherwise.
Definition button.hpp:215
void setCaption(std::string const &caption)
Sets the caption of the button.
Definition button.cpp:46
void adjustSize() override
Adjust internal size after layout or image changes.
void consume()
Marks this event as consumed.
Represents a key event.
Definition keyevent.hpp:26
Key const & getKey() const
Gets the key of the event.
Definition keyevent.cpp:48
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:1291
Used replacement tokens by configure_file():