-
Notifications
You must be signed in to change notification settings - Fork 388
Expand file tree
/
Copy pathWButtonGroup.C
More file actions
185 lines (148 loc) · 3.88 KB
/
WButtonGroup.C
File metadata and controls
185 lines (148 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium.
*
* See the LICENSE file for terms of use.
*/
#include "WebUtils.h"
#include "Wt/WButtonGroup"
#include "Wt/WRadioButton"
namespace Wt {
WButtonGroup::WButtonGroup(WObject* parent)
: WObject(parent),
checkedChangedConnected_(false)
{ }
WButtonGroup::~WButtonGroup()
{
for (unsigned i = 0; i < buttons_.size(); ++i)
buttons_[i].button->setGroup(0);
}
void WButtonGroup::addButton(WRadioButton *button, int id)
{
Button b;
b.button = button;
b.id = id != -1 ? id : generateId();
buttons_.push_back(b);
button->setGroup(this);
if (checkedChangedConnected_)
button->changed().connect(this, &WButtonGroup::onButtonChange);
}
void WButtonGroup::removeButton(WRadioButton *button)
{
for (unsigned i = 0; i < buttons_.size(); ++i)
if (buttons_[i].button == button) {
buttons_.erase(buttons_.begin() + i);
button->setGroup(0);
return;
}
}
WRadioButton *WButtonGroup::button(int id) const
{
for (unsigned i = 0; i < buttons_.size(); ++i)
if (buttons_[i].id == id)
return buttons_[i].button;
return 0;
}
int WButtonGroup::id(WRadioButton *button) const
{
for (unsigned i = 0; i < buttons_.size(); ++i)
if (buttons_[i].button == button)
return buttons_[i].id;
return -1;
}
std::vector<WRadioButton *> WButtonGroup::buttons() const
{
std::vector<WRadioButton *> buttons;
for (unsigned i = 0; i < buttons_.size(); ++i)
buttons.push_back(buttons_[i].button);
return buttons;
}
int WButtonGroup::count() const
{
return buttons_.size();
}
int WButtonGroup::checkedId() const
{
int idx = selectedButtonIndex();
return idx == -1 ? -1 : buttons_[idx].id;
}
void WButtonGroup::setCheckedButton(WRadioButton *button)
{
for (unsigned i = 0; i < buttons_.size(); ++i) {
WRadioButton *b = buttons_[i].button;
if (b == button && !b->isChecked())
b->setChecked(true);
else if (b != button && b->isChecked())
b->setChecked(false);
}
}
WRadioButton *WButtonGroup::checkedButton() const
{
int idx = selectedButtonIndex();
return idx != -1 ? buttons_[idx].button : 0;
}
void WButtonGroup::setSelectedButtonIndex(int idx)
{
setCheckedButton(idx != -1 ? buttons_[idx].button : 0);
}
int WButtonGroup::selectedButtonIndex() const
{
for (unsigned i = 0; i < buttons_.size(); ++i)
if (buttons_[i].button->isChecked())
return i;
return -1;
}
#ifndef WT_TARGET_JAVA
WRadioButton* WButtonGroup::selectedButton() const
{
return checkedButton();
}
#endif // WT_TARGET_JAVA
void WButtonGroup::setFormData(const FormData& formData)
{
if (!Utils::isEmpty(formData.values)) {
const std::string& value = formData.values[0];
for (unsigned i = 0; i < buttons_.size(); ++i) {
if (value == buttons_[i].button->id()) {
if (buttons_[i].button->flags_.test(WAbstractToggleButton::BIT_STATE_CHANGED))
return;
uncheckOthers(buttons_[i].button);
buttons_[i].button->state_ = Checked;
return;
}
}
} else {
/*
* none checked (form submit) or always for ajax. In any case
* we don't do anything, since none checked can only be if
* there were actually none checked to start with ?
*/
}
}
void WButtonGroup::uncheckOthers(WRadioButton *button)
{
for (unsigned i = 0; i < buttons_.size(); ++i)
if (buttons_[i].button != button)
buttons_[i].button->state_ = Unchecked;
}
int WButtonGroup::generateId() const
{
int id = 0;
for (unsigned i = 0; i < buttons_.size(); ++i)
id = std::max(buttons_[i].id + 1, id);
return id;
}
Signal<WRadioButton *>& WButtonGroup::checkedChanged()
{
if (!checkedChangedConnected_) {
checkedChangedConnected_ = true;
for (unsigned i = 0; i < buttons_.size(); ++i)
buttons_[i].button->changed()
.connect(this, &WButtonGroup::onButtonChange);
}
return checkedChanged_;
}
void WButtonGroup::onButtonChange()
{
checkedChanged_.emit(checkedButton());
}
}