/
MenuManager.cpp
286 lines (221 loc) · 6.23 KB
/
MenuManager.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
#include "MenuManager.h"
#include "itextstream.h"
#include "iregistry.h"
#include "ui/imainframe.h"
#include "MenuBar.h"
#include "MenuFolder.h"
#include "MenuRootElement.h"
#include "module/StaticModule.h"
namespace ui
{
namespace menu
{
namespace
{
// The menu root key in the registry
const char* const RKEY_MENU_ROOT = "user/ui/menu";
}
MenuManager::MenuManager() :
_root(new MenuRootElement())
{}
void MenuManager::clear()
{
_root.reset();
}
void MenuManager::loadFromRegistry()
{
// Clear existing elements first
_root.reset(new MenuRootElement());
xml::NodeList menuNodes = GlobalRegistry().findXPath(RKEY_MENU_ROOT);
if (!menuNodes.empty())
{
for (const xml::Node& menuNode : menuNodes)
{
MenuElementPtr menubar = MenuElement::CreateFromNode(menuNode);
// Add the menubar as child of the root
_root->addChild(menubar);
}
}
else
{
rError() << "MenuManager: Could not find menu root in registry." << std::endl;
}
}
void MenuManager::setVisibility(const std::string& path, bool visible)
{
// Sanity check for empty menu
if (!_root) return;
MenuElementPtr element = _root->find(path);
if (element)
{
element->setIsVisible(visible);
// The corresponding top level menu needs reconstruction
MenuElementPtr parentMenu = findTopLevelMenu(element);
if (parentMenu)
{
parentMenu->setNeedsRefresh(true);
}
}
}
wxMenuBar* MenuManager::getMenuBar(const std::string& name)
{
if (!_root) return nullptr; // root has already been removed
MenuElementPtr menuBar = _root->find(name);
if (menuBar)
{
assert(std::dynamic_pointer_cast<MenuBar>(menuBar));
return std::static_pointer_cast<MenuBar>(menuBar)->getMenuBar();
}
rError() << "MenuManager: Warning: Menubar with name " << name << " not found!" << std::endl;
return nullptr;
}
void MenuManager::add(const std::string& insertPath,
const std::string& name,
ItemType type,
const std::string& caption,
const std::string& icon,
const std::string& eventName)
{
if (!_root) return; // root has already been removed
MenuElementPtr parent = _root->find(insertPath);
if (!parent)
{
rWarning() << "Cannot insert element at non-existent parent " << insertPath << std::endl;
return;
}
MenuElementPtr element = MenuElement::CreateForType(type);
element->setName(name);
element->setCaption(caption);
element->setIcon(icon);
element->setEvent(eventName);
parent->addChild(element);
handleElementAdded(element);
}
void MenuManager::insert(const std::string& insertPath,
const std::string& name,
ItemType type,
const std::string& caption,
const std::string& icon,
const std::string& eventName)
{
if (!_root) return; // root has already been removed
MenuElementPtr insertBefore = _root->find(insertPath);
if (!insertBefore || !insertBefore->getParent())
{
rWarning() << "Cannot insert before non-existent item or item doesn't have a parent"
<< insertPath << std::endl;
return;
}
MenuElementPtr element = MenuElement::CreateForType(type);
element->setName(name);
element->setCaption(caption);
element->setIcon(icon);
element->setEvent(eventName);
insertBefore->getParent()->insertChild(element, insertBefore);
handleElementAdded(element);
}
bool MenuManager::exists(const std::string& path)
{
if (!_root) return false; // root has already been removed
return _root->find(path) != nullptr;
}
void MenuManager::remove(const std::string& path)
{
if (!_root) return; // root has already been removed
MenuElementPtr element = _root->find(path);
if (!element)
{
return; // no warning, some code just tries to remove stuff unconditionally
}
if (!element->getParent())
{
rWarning() << "Cannot remove item without a parent " << path << std::endl;
return;
}
element->getParent()->removeChild(element);
handleElementRemoved(element);
}
void MenuManager::handleElementAdded(const MenuElementPtr& element)
{
// The corresponding top level menu needs reconstruction
MenuElementPtr parentMenu = findTopLevelMenu(element);
if (parentMenu)
{
parentMenu->setNeedsRefresh(true);
}
#ifdef __linux__
// In Linux, we don't receive the wxEVT_MENU_OPEN event on the MenuFolder
// Option 1 is to add the event handler to the wxFrame housing the wxMenuBar
// but we don't always know what wxFrame that is (it's added later)
// Option 2 is to mark the whole menu as "needs refresh" => let's go with this
while (parentMenu)
{
if (std::dynamic_pointer_cast<MenuBar>(parentMenu))
{
std::static_pointer_cast<MenuBar>(parentMenu)->setNeedsRefresh(true);
break;
}
parentMenu = parentMenu->getParent();
}
#endif
// When inserting a new menu in a menubar, make sure it is constructed
if (element->getParent() &&
std::dynamic_pointer_cast<MenuBar>(element->getParent()) &&
std::static_pointer_cast<MenuBar>(element->getParent())->isConstructed())
{
std::static_pointer_cast<MenuBar>(element->getParent())->setNeedsRefresh(true);
}
}
void MenuManager::handleElementRemoved(const MenuElementPtr& element)
{
// The corresponding top level menu needs reconstruction
MenuElementPtr parentMenu = findTopLevelMenu(element);
if (parentMenu)
{
parentMenu->setNeedsRefresh(true);
}
}
MenuElementPtr MenuManager::findTopLevelMenu(const MenuElementPtr& element)
{
MenuElementPtr candidate = element;
while (candidate)
{
MenuElementPtr parent = candidate->getParent();
if (candidate &&
std::dynamic_pointer_cast<MenuFolder>(candidate) &&
std::dynamic_pointer_cast<MenuBar>(parent))
{
return candidate;
}
candidate = parent;
}
return MenuElementPtr();
}
const std::string& MenuManager::getName() const
{
static std::string _name(MODULE_MENUMANAGER);
return _name;
}
const StringSet& MenuManager::getDependencies() const
{
static StringSet _dependencies
{
MODULE_MAINFRAME
};
return _dependencies;
}
void MenuManager::initialiseModule(const IApplicationContext& ctx)
{
rMessage() << getName() << "::initialiseModule called." << std::endl;
loadFromRegistry();
GlobalMainFrame().signal_MainFrameShuttingDown().connect(
sigc::mem_fun(this, &MenuManager::clear)
);
}
void MenuManager::shutdownModule()
{
clear();
}
module::StaticModuleRegistration<MenuManager> menuManagerModule;
} // namespace ui
}