FifeGUI 0.2.0
A C++ GUI library designed for games.
radiobutton.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/radiobutton.hpp"
6
7#include <string>
8
9namespace fcn
10{
12
13 RadioButton::RadioButton()
14 {
15 setMarkerStyle(MarkerStyle::Rhombus);
16 setSelected(false);
17 adjustSize();
18 }
19
20 RadioButton::RadioButton(std::string const & caption, std::string const & group, bool selected)
21 {
22 setMarkerStyle(MarkerStyle::Rhombus);
23 setCaption(caption);
24 setGroup(group);
25 setSelected(selected);
26 adjustSize();
27 }
28
29 RadioButton::~RadioButton()
30 {
31 // Remove us from the group list
32 setGroup("");
33 }
34
35 void RadioButton::setSelected(bool selected)
36 {
37 if (selected && !mGroup.empty()) {
38 // deselect all buttons in group
39 GroupIterator iter;
40 GroupIterator iterEnd;
41 iterEnd = mGroupMap.upper_bound(mGroup);
42
43 for (iter = mGroupMap.lower_bound(mGroup); iter != iterEnd; ++iter) {
44 if (iter->second->isSelected()) {
45 iter->second->setSelected(false);
46 }
47 }
48 }
49
50 mSelected = selected;
51 }
52
57
58 void RadioButton::setGroup(std::string const & group)
59 {
60 // Remove button from previous group
61 if (!mGroup.empty()) {
62 GroupIterator iter;
63 GroupIterator iterEnd;
64 iterEnd = mGroupMap.upper_bound(mGroup);
65
66 for (iter = mGroupMap.lower_bound(mGroup); iter != iterEnd; ++iter) {
67 if (iter->second == this) {
68 mGroupMap.erase(iter);
69 break;
70 }
71 }
72 }
73 // Add button to new group
74 if (!group.empty()) {
75 mGroupMap.insert(std::pair<std::string, RadioButton*>(group, this));
76 }
77
78 mGroup = group;
79 }
80
81 std::string const & RadioButton::getGroup() const
82 {
83 return mGroup;
84 }
85
86} // namespace fcn
void setCaption(std::string const &caption)
Sets the caption of the button.
Definition button.cpp:43
bool mSelected
True if the check box is selected, false otherwise.
Definition checkbox.hpp:211
void adjustSize() override
Resizes the widget's size to fit the content exactly.
Definition checkbox.cpp:421
void setMarkerStyle(MarkerStyle mode)
Set the marker style of the check box.
Definition checkbox.cpp:373
virtual bool isSelected() const
Checks if the check box is selected.
Definition checkbox.cpp:328
GroupMap::iterator GroupIterator
Typdef.
std::string const & getGroup() const
Gets the group the radio button belongs to.
void toggleSelected() override
Toggles the check box between being selected and not being selected.
std::multimap< std::string, RadioButton * > GroupMap
Typdef.
static GroupMap mGroupMap
Holds all available radio button groups.
void setSelected(bool selected) override
Sets the check box to be selected or not.
std::string mGroup
Holds the group of the radio button.
void setGroup(std::string const &group)
Sets the group the radio button should belong to.