FifeGUI 0.3.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// Corresponding header include
6#include "fifechan/widgets/radiobutton.hpp"
7
8// Standard library includes
9#include <string>
10
11namespace fcn
12{
14
15 RadioButton::RadioButton()
16 {
17 addMouseListener(this);
18 setMarkerStyle(MarkerStyle::Rhombus);
19 setSelected(false);
20 adjustSize();
21 }
22
23 RadioButton::RadioButton(std::string const & caption, std::string const & group, bool selected) : mGroup(group)
24 {
25 addMouseListener(this);
26 setMarkerStyle(MarkerStyle::Rhombus);
27 setCaption(caption);
28 setGroup(group);
29 setSelected(selected);
30 adjustSize();
31 }
32
33 RadioButton::~RadioButton()
34 {
35 // Remove us from the group list
36 setGroup("");
37 }
38
39 void RadioButton::setSelected(bool selected)
40 {
41 if (selected && !mGroup.empty()) {
42 // deselect all buttons in group
43 GroupIterator iter;
44 GroupIterator iterEnd;
45 iterEnd = mGroupMap.upper_bound(mGroup);
46
47 for (iter = mGroupMap.lower_bound(mGroup); iter != iterEnd; ++iter) {
48 if (iter->second->isSelected()) {
49 iter->second->setSelected(false);
50 }
51 }
52 }
53
54 mSelected = selected;
55 }
56
61
62 void RadioButton::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, RadioButton*>(group, this));
80 }
81
82 mGroup = group;
83 }
84
85 std::string const & RadioButton::getGroup() const
86 {
87 return mGroup;
88 }
89
90} // namespace fcn
void setCaption(std::string const &caption)
Sets the caption of the button.
Definition button.cpp:46
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:429
void setMarkerStyle(MarkerStyle mode)
Set the marker style of the check box.
Definition checkbox.cpp:377
virtual bool isSelected() const
Checks if the check box is selected.
Definition checkbox.cpp:332
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.
void addMouseListener(MouseListener *mouseListener)
Adds a mouse listener to the widget.
Definition widget.cpp:907
Used replacement tokens by configure_file():