-
Notifications
You must be signed in to change notification settings - Fork 47
/
MenuElement.cpp
320 lines (256 loc) · 6.32 KB
/
MenuElement.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
#include "MenuElement.h"
#include "i18n.h"
#include "itextstream.h"
#include "igame.h"
#include "string/split.h"
#include "string/join.h"
#include <wx/menu.h>
#include <wx/menuitem.h>
#include <limits>
#include <iterator>
#include <iostream>
#include "MenuBar.h"
#include "MenuFolder.h"
#include "MenuItem.h"
#include "MenuSeparator.h"
namespace ui
{
namespace menu
{
int MenuElement::_nextMenuItemId = 100;
MenuElement::MenuElement(const MenuElementPtr& parent) :
_parent(parent ? MenuElementWeakPtr(parent) : MenuElementWeakPtr()),
_isVisible(true),
_needsRefresh(false)
{}
MenuElement::~MenuElement()
{}
const std::string& MenuElement::getName() const
{
return _name;
}
void MenuElement::setName(const std::string& name)
{
_name = name;
}
MenuElementPtr MenuElement::getParent() const
{
return _parent.lock();
}
void MenuElement::setParent(const MenuElementPtr& parent)
{
_parent = parent;
}
void MenuElement::setCaption(const std::string& caption)
{
_caption = caption;
}
std::string MenuElement::getCaption() const
{
return _caption;
}
void MenuElement::setIcon(const std::string& icon)
{
_icon = icon;
}
std::size_t MenuElement::numChildren() const
{
return _children.size();
}
bool MenuElement::isVisible() const
{
return _isVisible;
}
void MenuElement::setIsVisible(bool visible)
{
_isVisible = visible;
}
void MenuElement::addChild(const MenuElementPtr& newChild)
{
newChild->setParent(shared_from_this());
_children.push_back(newChild);
}
void MenuElement::insertChild(const MenuElementPtr& newChild, const MenuElementPtr& insertBefore)
{
newChild->setParent(shared_from_this());
_children.insert(std::find(_children.begin(), _children.end(), insertBefore), newChild);
}
void MenuElement::removeChild(const MenuElementPtr& child)
{
for (MenuElementList::iterator i = _children.begin(); i != _children.end(); ++i)
{
if (*i == child)
{
// Deconstruct the child before removal
child->deconstruct();
// Release from parent and remove from the list
child->setParent(MenuElementPtr());
_children.erase(i);
break;
}
}
}
void MenuElement::removeAllChildren()
{
for (const MenuElementPtr& child : _children)
{
child->setParent(MenuElementPtr());
}
_children.clear();
}
const std::string& MenuElement::getEvent() const
{
return _event;
}
void MenuElement::setEvent(const std::string& eventName)
{
_event = eventName;
}
int MenuElement::getMenuPosition(const MenuElementPtr& child, bool includeHidden)
{
int pos = 0;
for (const MenuElementPtr& candidate : _children)
{
if (!candidate->isVisible()) continue; // skip hidden items
if (candidate == child) break;
++pos;
}
return pos;
}
MenuElementPtr MenuElement::find(const std::string& menuPath)
{
// Split the path and analyse it
std::list<std::string> parts;
string::split(parts, menuPath, "/");
// Any path items at all?
if (parts.empty()) return MenuElementPtr();
// Path is not empty, try to find the first item among the item's children
for (const MenuElementPtr& 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 = string::join(parts, "/");
return candidate->find(childPath);
}
}
// Nothing found
return MenuElementPtr();
}
bool MenuElement::needsRefresh()
{
return _needsRefresh;
}
void MenuElement::setNeedsRefresh(bool needsRefresh)
{
_needsRefresh = needsRefresh;
}
void MenuElement::setAccelerator(const std::string& accelStr)
{}
MenuElementPtr MenuElement::CreateFromNode(const xml::Node& node)
{
MenuElementPtr item;
// Don't create anything if the menu item requires a non-existent game feature
if (auto feature = node.getAttributeValue("gamefeature");
!feature.empty() && !GlobalGameManager().currentGame()->hasFeature(feature))
{
return {};
}
std::string nodeName = node.getName();
if (nodeName == "menuItem")
{
item = std::make_shared<MenuItem>();
// Get the EventPtr according to the event
item->setEvent(node.getAttributeValue("command"));
item->setIcon(node.getAttributeValue("icon"));
}
else if (nodeName == "menuSeparator")
{
item = std::make_shared<MenuSeparator>();
}
else if (nodeName == "subMenu")
{
item = std::make_shared<MenuFolder>();
}
else if (nodeName == "menu")
{
item = std::make_shared<MenuBar>();
}
else
{
rError() << "MenuElement: Unknown node found: " << node.getName() << std::endl;
return item;
}
item->setName(node.getAttributeValue("name"));
// Put the caption through gettext before passing it to setCaption
item->setCaption(_(node.getAttributeValue("caption").c_str()));
// 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
MenuElementPtr childItem = CreateFromNode(childNode);
// Add the child to the list
if (childItem)
item->addChild(childItem);
}
return item;
}
MenuElementPtr MenuElement::CreateForType(ItemType type)
{
MenuElementPtr item;
switch (type)
{
case ItemType::Item:
item = std::make_shared<MenuItem>();
break;
case ItemType::Bar:
item = std::make_shared<MenuBar>();
break;
case ItemType::Folder:
item = std::make_shared<MenuFolder>();
break;
case ItemType::Separator:
item = std::make_shared<MenuSeparator>();
break;
default:
rError() << "MenuElement: Cannot create node for type " << (int)type << std::endl;
};
return item;
}
void MenuElement::setNeedsRefreshRecursively(bool needsRefresh)
{
setNeedsRefresh(needsRefresh);
for (const MenuElementPtr& child : _children)
{
child->setNeedsRefreshRecursively(needsRefresh);
}
}
void MenuElement::constructChildren()
{
for (const MenuElementPtr& child : _children)
{
child->construct();
}
}
void MenuElement::deconstructChildren()
{
for (const MenuElementPtr& child : _children)
{
child->deconstruct();
}
}
} // namespace
} // namespace