/
Toggle.cpp
319 lines (258 loc) · 6.65 KB
/
Toggle.cpp
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include "Toggle.h"
#include "itextstream.h"
#include "Accelerator.h"
#include <wx/menu.h>
#include <wx/menuitem.h>
#include <wx/toolbar.h>
#include <wx/tglbtn.h>
#include <wx/button.h>
namespace ui
{
Toggle::Toggle(const ToggleCallback& callback) :
_callback(callback),
_callbackActive(false),
_toggled(false)
{}
Toggle::~Toggle()
{
}
bool Toggle::empty() const {
return false;
}
bool Toggle::isToggle() const {
return true;
}
// Set the toggled state to true/false, according to <toggled> and update
// any associated widgets or notify any callbacks.
bool Toggle::setToggled(const bool toggled)
{
if (_callbackActive) {
return false;
}
// Update the toggle status and export it to the GTK button
_toggled = toggled;
updateWidgets();
return true;
}
void Toggle::updateWidgets()
{
_callbackActive = true;
for (MenuItems::const_iterator i = _menuItems.begin(); i != _menuItems.end(); ++i)
{
(*i)->Check(_toggled);
}
for (ToolItems::const_iterator i = _toolItems.begin(); i != _toolItems.end(); ++i)
{
(*i)->GetToolBar()->ToggleTool((*i)->GetId(), _toggled);
}
for (Buttons::const_iterator i = _buttons.begin(); i != _buttons.end(); ++i)
{
(*i)->SetValue(_toggled);
}
for (const auto& pair : _menuElements)
{
pair.first->setToggled(_toggled);
}
_callbackActive = false;
}
// On key press >> toggle the internal state
void Toggle::keyDown() {
toggle();
}
bool Toggle::isToggled() const {
return _toggled;
}
void Toggle::connectMenuItem(const IMenuElementPtr& item)
{
if (!item->isToggle())
{
rWarning() << "Cannot connect non-checkable menu item to this event." << std::endl;
return;
}
if (_menuElements.find(item) != _menuElements.end())
{
rWarning() << "Cannot connect to the same menu item more than once." << std::endl;
return;
}
_menuElements.emplace(item,
item->signal_ItemActivated().connect(sigc::mem_fun(*this, &Toggle::onItemActivated)));
item->setToggled(_toggled);
}
void Toggle::onItemActivated()
{
toggle();
}
void Toggle::disconnectMenuItem(const IMenuElementPtr& item)
{
if (!item->isToggle())
{
rWarning() << "Cannot disconnect from non-checkable menu item." << std::endl;
return;
}
auto found = _menuElements.find(item);
if (found == _menuElements.end())
{
rWarning() << "Cannot disconnect from unconnected menu item." << std::endl;
return;
}
found->second.disconnect();
_menuElements.erase(found);
}
void Toggle::connectMenuItem(wxMenuItem* item)
{
if (!item->IsCheckable())
{
rWarning() << "Cannot connect non-checkable menu item to this event." << std::endl;
return;
}
if (_menuItems.find(item) != _menuItems.end())
{
rWarning() << "Cannot connect to the same menu item more than once." << std::endl;
return;
}
_menuItems.insert(item);
item->Check(_toggled);
// Connect the togglebutton to the callback of this class
assert(item->GetMenu());
item->GetMenu()->Connect(item->GetId(), wxEVT_MENU,
wxCommandEventHandler(Toggle::onMenuItemClicked), NULL, this);
}
void Toggle::disconnectMenuItem(wxMenuItem* item)
{
if (!item->IsCheckable())
{
rWarning() << "Cannot disconnect from non-checkable menu item." << std::endl;
return;
}
if (_menuItems.find(item) == _menuItems.end())
{
rWarning() << "Cannot disconnect from unconnected menu item." << std::endl;
return;
}
_menuItems.erase(item);
// Connect the togglebutton to the callback of this class
assert(item->GetMenu());
item->GetMenu()->Disconnect(item->GetId(), wxEVT_MENU,
wxCommandEventHandler(Toggle::onMenuItemClicked), NULL, this);
}
void Toggle::onMenuItemClicked(wxCommandEvent& ev)
{
// Make sure the event is actually directed at us
for (MenuItems::const_iterator i = _menuItems.begin(); i != _menuItems.end(); ++i)
{
if ((*i)->GetId() == ev.GetId())
{
toggle();
return;
}
}
ev.Skip();
}
void Toggle::connectToolItem(wxToolBarToolBase* item)
{
if (_toolItems.find(item) != _toolItems.end())
{
rWarning() << "Cannot connect to the same tool item more than once." << std::endl;
return;
}
_toolItems.insert(item);
// Update the widget first
item->GetToolBar()->ToggleTool(item->GetId(), _toggled);
// Connect the to the callback of this class
item->GetToolBar()->Bind(wxEVT_TOOL, &Toggle::onToolItemClicked, this, item->GetId());
}
void Toggle::disconnectToolItem(wxToolBarToolBase* item)
{
if (_toolItems.find(item) == _toolItems.end())
{
//rWarning() << "Cannot disconnect from unconnected tool item." << std::endl;
return;
}
_toolItems.erase(item);
// Connect the to the callback of this class
item->GetToolBar()->Unbind(wxEVT_TOOL, &Toggle::onToolItemClicked, this, item->GetId());
}
void Toggle::onToolItemClicked(wxCommandEvent& ev)
{
// Make sure the event is actually directed at us
for (ToolItems::const_iterator i = _toolItems.begin(); i != _toolItems.end(); ++i)
{
if ((*i)->GetId() == ev.GetId())
{
toggle();
return;
}
}
ev.Skip();
}
void Toggle::connectToggleButton(wxToggleButton* button)
{
if (_buttons.find(button) != _buttons.end())
{
rWarning() << "Cannot connect to the same button more than once." << std::endl;
return;
}
_buttons.insert(button);
// Update the widget's state to match the internal one
button->SetValue(_toggled);
// Connect the to the callback of this class
button->Connect(wxEVT_TOGGLEBUTTON, wxCommandEventHandler(Toggle::onToggleButtonClicked), NULL, this);
}
void Toggle::disconnectToggleButton(wxToggleButton* button)
{
if (_buttons.find(button) == _buttons.end())
{
rWarning() << "Cannot disconnect from unconnected button." << std::endl;
return;
}
_buttons.erase(button);
// Connect the to the callback of this class
button->Disconnect(wxEVT_TOGGLEBUTTON, wxCommandEventHandler(Toggle::onToggleButtonClicked), NULL, this);
}
void Toggle::onToggleButtonClicked(wxCommandEvent& ev)
{
toggle();
}
// Invoke the registered callback and update/notify
void Toggle::toggle()
{
if (_callbackActive) {
return;
}
// Check if the toggle event is enabled
if (_enabled) {
// Invert the <toggled> state
_toggled = !_toggled;
// Call the connected function with the new state
_callback(_toggled);
}
// Update any attached GtkObjects in any case
updateWidgets();
}
void Toggle::connectAccelerator(IAccelerator& accel)
{
#if 0
for (wxMenuItem* item : _menuItems)
{
setMenuItemAccelerator(item, static_cast<Accelerator&>(accel));
}
#endif
for (wxToolBarToolBase* tool : _toolItems)
{
setToolItemAccelerator(tool, static_cast<Accelerator&>(accel));
}
}
void Toggle::disconnectAccelerators()
{
#if 0
for (wxMenuItem* item : _menuItems)
{
clearMenuItemAccelerator(item);
}
#endif
for (wxToolBarToolBase* tool : _toolItems)
{
clearToolItemAccelerator(tool);
}
}
}