/
MenuItem.cpp
473 lines (391 loc) · 9.76 KB
/
MenuItem.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#include "MenuItem.h"
#include "i18n.h"
#include "itextstream.h"
#include "iradiant.h"
#include "ieventmanager.h"
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/join.hpp>
#include <wx/menu.h>
#include <wx/menuitem.h>
#include <iostream>
#include "LocalBitmapArtProvider.h"
namespace ui
{
int MenuItem::_nextMenuItemId = 100;
MenuItem::MenuItem(const MenuItemPtr& parent) :
_parent(parent ? MenuItemWeakPtr(parent) : MenuItemWeakPtr()),
_widget(nullptr),
_type(menuNothing),
_constructed(false)
{}
MenuItem::~MenuItem()
{
disconnectEvent();
}
std::string MenuItem::getName() const
{
return _name;
}
void MenuItem::setName(const std::string& name)
{
_name = name;
}
bool MenuItem::isRoot() const
{
return (_type == menuRoot);
}
MenuItemPtr MenuItem::parent() const
{
return _parent.lock();
}
void MenuItem::setParent(const MenuItemPtr& parent)
{
_parent = parent;
}
void MenuItem::setCaption(const std::string& caption)
{
_caption = caption;
}
std::string MenuItem::getCaption() const
{
return _caption;
}
void MenuItem::setIcon(const std::string& icon)
{
_icon = icon;
}
bool MenuItem::isEmpty() const
{
return (_type != menuItem);
}
eMenuItemType MenuItem::getType() const
{
return _type;
}
void MenuItem::setType(eMenuItemType type)
{
_type = type;
}
std::size_t MenuItem::numChildren() const
{
return _children.size();
}
void MenuItem::addChild(const MenuItemPtr& newChild)
{
newChild->setParent(shared_from_this());
_children.push_back(newChild);
}
void MenuItem::removeChild(const MenuItemPtr& child)
{
for (MenuItemList::iterator i = _children.begin(); i != _children.end(); ++i)
{
if (*i == child)
{
child->setParent(MenuItemPtr());
_children.erase(i);
return;
}
}
}
void MenuItem::removeAllChildren()
{
for (const MenuItemPtr& child : _children)
{
child->setParent(MenuItemPtr());
}
_children.clear();
}
std::string MenuItem::getEvent() const
{
return _event;
}
void MenuItem::setEvent(const std::string& eventName)
{
_event = eventName;
}
int MenuItem::getMenuPosition(const MenuItemPtr& child)
{
if (!_constructed)
{
construct();
}
// Check if this is the right item type for this operation
if (_type == menuFolder)
{
// A menufolder is a menuitem with a contained submenu, retrieve it
wxMenu* container = dynamic_cast<wxMenu*>(_widget);
// Get the list of child widgets
wxMenuItemList& children = container->GetMenuItems();
// The child Widget for comparison
wxObject* childWidget = child->getWidget();
int position = 0;
for (wxMenuItemList::const_iterator i = children.begin(); i != children.end(); ++i, ++position)
{
// Get the widget pointer from the current list item
wxMenuItem* item = *i;
if (item == childWidget ||
(child->getType() == menuFolder && item->GetSubMenu() == childWidget))
{
return position;
}
}
}
else if (_type == menuBar)
{
wxMenuBar* container = dynamic_cast<wxMenuBar*>(_widget);
if (container == NULL)
{
rWarning() << "Cannot find menu position, cannot cast to wxMenuBar." << std::endl;
return -1;
}
// The child Widget for comparison
wxObject* childWidget = child->getWidget();
// Iterate over all registered menus
for (int position = 0; position < container->GetMenuCount(); ++position)
{
// Get the widget pointer from the current list item
if (container->GetMenu(position) == childWidget)
{
return position;
}
}
}
return -1; // not found or wrong type
}
wxObject* MenuItem::getWidget()
{
// Check for toggle, allocate the Gtk::Widget*
if (!_constructed)
{
construct();
}
return _widget;
}
MenuItemPtr MenuItem::find(const std::string& menuPath)
{
// Split the path and analyse it
std::list<std::string> parts;
boost::algorithm::split(parts, menuPath, boost::algorithm::is_any_of("/"));
// Any path items at all?
if (parts.empty()) return MenuItemPtr();
// Path is not empty, try to find the first item among the item's children
for (const MenuItemPtr& candidate : _children)
{
if (candidate->getName() == parts.front())
{
// Remove the first part, it has been processed
parts.pop_front();
// Is this the end of the path (no more items)?
if (parts.empty())
{
// Yes, return the found item
return candidate;
}
// No, pass the query down the hierarchy
std::string childPath = boost::algorithm::join(parts, "/");
return candidate->find(childPath);
}
}
// Nothing found
return MenuItemPtr();
}
void MenuItem::construct()
{
if (_type == menuBar)
{
wxMenuBar* menuBar = new wxMenuBar;
_widget = menuBar;
for (std::size_t i = 0; i < _children.size(); i++)
{
// Cast each children onto wxMenu and append it to the menu
wxMenu* menu = dynamic_cast<wxMenu*>(_children[i]->getWidget());
if (menuItem != NULL)
{
menuBar->Append(menu, _children[i]->getCaption());
}
else
{
rError() << "MenuItem::construct: Cannot cast child to wxMenu" << std::endl;
}
}
}
else if (_type == menuSeparator)
{
_widget = NULL; // separator is handled when adding to the parent menu itself
}
else if (_type == menuFolder)
{
// Create the menuitem, don't pass a title to the constructor,
// otherwise the caption gets immediately added as first child
wxMenu* menu = new wxMenu;
_widget = menu;
for (std::size_t i = 0; i < _children.size(); i++)
{
if (_children[i]->getType() == menuSeparator)
{
_children[i]->_widget = menu->AppendSeparator();
continue;
}
wxMenuItem* menuItem = dynamic_cast<wxMenuItem*>(_children[i]->getWidget());
if (menuItem != NULL)
{
menu->Append(menuItem);
if (_children[i]->getEvent().empty())
{
// No event attached to this menu item, disable it
menu->Enable(menuItem->GetId(), false);
continue;
}
// Now is the time to connect the event, the item has a valid menu parent at this point
IEventPtr event = GlobalEventManager().findEvent(_children[i]->getEvent());
if (event != NULL)
{
event->connectMenuItem(menuItem);
}
continue;
}
wxMenu* subMenu = dynamic_cast<wxMenu*>(_children[i]->getWidget());
if (subMenu != NULL)
{
menu->AppendSubMenu(subMenu, _children[i]->getCaption());
continue;
}
}
}
else if (_type == menuItem)
{
if (!_event.empty())
{
// Try to lookup the event name
IEventPtr event = GlobalEventManager().findEvent(_event);
if (!event->empty())
{
// Retrieve an accelerator string formatted for a menu
const std::string accelText =
GlobalEventManager().getAcceleratorStr(event, true);
// Create a new menuitem
// greebo: Accelerators seem to globally catch the key events, add a space to fool wxWidgets
wxMenuItem* item = new wxMenuItem(NULL, _nextMenuItemId++, _caption + "\t " + accelText);
_widget = item;
if (!_icon.empty())
{
item->SetBitmap(wxArtProvider::GetBitmap(LocalBitmapArtProvider::ArtIdPrefix() + _icon));
}
item->SetCheckable(event->isToggle());
}
else
{
rWarning() << "MenuItem: Cannot find associated event: " << _event << std::endl;
}
}
else
{
// Create an empty, desensitised menuitem, will be disabled once attached to the parent menu
wxMenuItem* item = new wxMenuItem(NULL, _nextMenuItemId++, _caption.empty() ? "-" : _caption);
_widget = item;
}
}
else if (_type == menuRoot)
{
// Cannot instantiate root MenuItem, ignore
}
_constructed = true;
}
void MenuItem::connectEvent()
{
if (!_event.empty() && _type == menuItem)
{
// Try to lookup the event name
IEventPtr event = GlobalEventManager().findEvent(_event);
wxMenuItem* menuItem = dynamic_cast<wxMenuItem*>(_widget);
if (event != NULL && menuItem != NULL)
{
event->connectMenuItem(menuItem);
}
}
}
void MenuItem::disconnectEvent()
{
if (!_event.empty() && _type == menuItem)
{
IEventPtr ev = GlobalEventManager().findEvent(_event);
wxMenuItem* item = dynamic_cast<wxMenuItem*>(_widget);
// Tell the eventmanager to disconnect the widget in any case
// even if has been destroyed already.
if (ev != NULL && item != NULL)
{
ev->disconnectMenuItem(item);
}
}
}
void MenuItem::setWidget(wxObject* object)
{
// Disconnect the old widget before setting a new one
disconnectEvent();
_widget = object;
_constructed = true;
}
eMenuItemType MenuItem::GetTypeForXmlNode(const xml::Node& node)
{
std::string nodeName = node.getName();
if (nodeName == "menuItem")
{
return menuItem;
}
else if (nodeName == "menuSeparator")
{
return menuSeparator;
}
else if (nodeName == "subMenu")
{
return menuFolder;
}
else if (nodeName == "menu")
{
return menuBar;
}
else
{
rError() << "MenuItem: Unknown node found: " << node.getName() << std::endl;
return menuNothing;
}
}
MenuItemPtr MenuItem::CreateFromNode(const xml::Node& node)
{
MenuItemPtr item = std::make_shared<MenuItem>();
item->setName(node.getAttributeValue("name"));
// Put the caption through gettext before passing it to setCaption
item->setCaption(_(node.getAttributeValue("caption").c_str()));
// Parse type name
item->setType(GetTypeForXmlNode(node));
if (item->getType() == menuItem)
{
// Get the EventPtr according to the event
item->setEvent(node.getAttributeValue("command"));
item->setIcon(node.getAttributeValue("icon"));
}
else if (item->getType() == menuNothing)
{
return MenuItemPtr();
}
// Parse subnodes
xml::NodeList childNodes = node.getChildren();
for (const xml::Node& childNode : childNodes)
{
if (childNode.getName() == "text" || childNode.getName() == "comment")
{
continue;
}
// Allocate a new child item
MenuItemPtr childItem = CreateFromNode(childNode);
// Add the child to the list
if (childItem)
{
item->addChild(childItem);
}
}
return item;
}
} // namespace ui