/
ToolbarManager.cpp
220 lines (174 loc) · 5.54 KB
/
ToolbarManager.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
#include "ToolbarManager.h"
#include <stdexcept>
#include "itextstream.h"
#include "ui/ieventmanager.h"
#include "iregistry.h"
#include "i18n.h"
#include <wx/toolbar.h>
#include "module/StaticModule.h"
#include "wxutil/Bitmap.h"
namespace ui
{
namespace
{
const char* const CMD_VARIANT_NAME = "command";
}
const std::string& ToolbarManager::getName() const
{
static std::string _name(MODULE_TOOLBARMANAGER);
return _name;
}
const StringSet& ToolbarManager::getDependencies() const
{
static StringSet _dependencies;
if (_dependencies.empty())
{
_dependencies.insert(MODULE_XMLREGISTRY);
}
return _dependencies;
}
void ToolbarManager::initialiseModule(const IApplicationContext& ctx)
{
_nextToolItemId = 100;
try
{
// Query the registry
loadToolbars();
}
catch (const std::runtime_error& e)
{
rError() << "ToolbarManager: Warning: " << e.what() << std::endl;
}
}
wxToolBar* ToolbarManager::createToolbar(const std::string& toolbarName, wxWindow* parent)
{
// Check if the toolbarName exists
if (!toolbarExists(toolbarName))
{
rError() << "ToolbarManager: Critical: Named toolbar doesn't exist: " << toolbarName << std::endl;
return nullptr;
}
// Instantiate the toolbar with buttons
rMessage() << "ToolbarManager: Instantiating toolbar: " << toolbarName << std::endl;
// Build the path into the registry, where the toolbar should be found
auto toolbarPath = std::string("//ui//toolbar") + "[@name='"+ toolbarName +"']";
auto toolbarList = GlobalRegistry().findXPath(toolbarPath);
if (toolbarList.empty())
{
rError() << "ToolbarManager: Critical: Could not instantiate " << toolbarName << std::endl;
return nullptr;
}
return createToolbarFromNode(toolbarList[0], parent);
}
wxToolBarToolBase* ToolbarManager::createToolItem(wxToolBar* toolbar, const xml::Node& node)
{
const std::string nodeName = node.getName();
wxToolBarToolBase* toolItem = nullptr;
if (nodeName == "separator")
{
toolItem = toolbar->AddSeparator();
}
else if (nodeName == "toolbutton" || nodeName == "toggletoolbutton")
{
// Found a button, load the values that are shared by both types
std::string name = node.getAttributeValue("name");
std::string icon = node.getAttributeValue("icon");
std::string tooltip = _(node.getAttributeValue("tooltip").c_str());
std::string action = node.getAttributeValue("action");
// Don't assign a label to the tool item since OSX is painting
// the first few characters over the bitmap
if (!icon.empty())
{
name.clear();
}
if (nodeName == "toolbutton")
{
// Create a new ToolButton and assign the right callback
toolItem = toolbar->AddTool(_nextToolItemId++, name,
wxutil::GetLocalBitmap(icon),
tooltip);
}
else
{
// Create a new ToggleToolButton and assign the right callback
toolItem = toolbar->AddTool(_nextToolItemId++, name,
wxutil::GetLocalBitmap(icon),
tooltip, wxITEM_CHECK);
}
toolItem->SetClientData(new wxVariant(action, CMD_VARIANT_NAME));
GlobalEventManager().registerToolItem(action, toolItem);
}
return toolItem;
}
wxToolBar* ToolbarManager::createToolbarFromNode(xml::Node& node, wxWindow* parent)
{
// Get all action children elements
auto toolItemList = node.getChildren();
wxToolBar* toolbar = nullptr;
if (toolItemList.empty())
{
throw std::runtime_error("No elements in toolbar.");
}
// Try to set the alignment, if the attribute is properly set
auto align = node.getAttributeValue("align");
// Create a new toolbar
toolbar = new wxToolBar(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
align == "vertical" ? wxTB_VERTICAL : wxTB_HORIZONTAL,
node.getAttributeValue("name"));
// Adjust the toolbar bitmap size to add some padding - despite its name
// this will not resize the actual icons, just the buttons
toolbar->SetToolBitmapSize(wxSize(20, 20));
for (const auto& toolNode : toolItemList)
{
// Create and get the toolItem with the parsing
createToolItem(toolbar, toolNode);
}
toolbar->Realize();
toolbar->Bind(wxEVT_DESTROY, &ToolbarManager::onToolbarDestroy, this);
return toolbar;
}
void ToolbarManager::onToolbarDestroy(wxWindowDestroyEvent& ev)
{
auto toolbar = wxDynamicCast(ev.GetEventObject(), wxToolBar);
if (toolbar == nullptr)
{
return;
}
for (std::size_t tool = 0; tool < toolbar->GetToolsCount(); tool++)
{
auto toolItem = toolbar->GetToolByPos(tool);
auto cmdData = wxDynamicCast(toolItem->GetClientData(), wxVariant);
if (cmdData != nullptr && cmdData->GetName() == CMD_VARIANT_NAME)
{
GlobalEventManager().unregisterToolItem(cmdData->GetString().ToStdString(), toolItem);
// free the client data, the toolbar item doesn't delete it
toolbar->SetToolClientData(toolItem->GetId(), nullptr);
delete cmdData;
}
}
}
bool ToolbarManager::toolbarExists(const std::string& toolbarName)
{
return _toolbars.count(toolbarName) > 0;
}
void ToolbarManager::loadToolbars()
{
auto toolbarList = GlobalRegistry().findXPath("//ui//toolbar");
if (toolbarList.empty())
{
throw std::runtime_error("No toolbars found.");
}
for (std::size_t i = 0; i < toolbarList.size(); ++i)
{
auto toolbarName = toolbarList[i].getAttributeValue("name");
if (toolbarExists(toolbarName))
{
//rMessage() << "This toolbar already exists: ";
continue;
}
rMessage() << "Found toolbar: " << toolbarName << std::endl;
_toolbars.insert(toolbarName);
}
}
module::StaticModuleRegistration<ToolbarManager> toolbarManagerModule;
} // namespace ui