-
Notifications
You must be signed in to change notification settings - Fork 47
/
PropertyNotebook.cpp
333 lines (261 loc) · 8.37 KB
/
PropertyNotebook.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
#include "PropertyNotebook.h"
#include "ui/iuserinterface.h"
#include "wxutil/Bitmap.h"
#include "registry/registry.h"
#include "AuiLayout.h"
#include "i18n.h"
#include <wx/settings.h>
namespace ui
{
namespace
{
const std::string RKEY_ROOT = "user/ui/mainFrame/propertyPanel/";
const std::string RKEY_LAST_SHOWN_PAGE = RKEY_ROOT + "lastShownPage";
const std::string RKEY_PAGES = RKEY_ROOT + "pages";
const std::string PAGE_NODE_NAME = "page";
const std::string CONTROL_NAME_ATTRIBUTE = "controlName";
}
PropertyNotebook::PropertyNotebook(wxWindow* parent, AuiLayout& owner) :
wxAuiNotebook(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
wxNB_TOP | wxAUI_NB_TAB_MOVE | wxAUI_NB_SCROLL_BUTTONS),
_layout(owner),
_dropHint(nullptr)
{
SetName("PropertyNotebook");
_imageList.reset(new wxImageList(16, 16));
SetImageList(_imageList.get());
Bind(wxEVT_AUINOTEBOOK_PAGE_CHANGED, &PropertyNotebook::onPageSwitch, this);
Bind(wxEVT_AUINOTEBOOK_TAB_RIGHT_UP, &PropertyNotebook::onTabRightClick, this);
_popupMenu = std::make_shared<wxutil::PopupMenu>();
_popupMenu->addItem(
new wxMenuItem(nullptr, wxID_ANY, _("Undock"), ""),
[this]() { undockSelectedTab(); }
);
_popupMenu->addItem(
new wxMenuItem(nullptr, wxID_ANY, _("Close"), ""),
[this]() { closeSelectedTab(); }
);
}
PropertyNotebook::~PropertyNotebook()
{}
void PropertyNotebook::addControl(const std::string& controlName)
{
if (controlExists(controlName))
{
rWarning() << "Control " << controlName << " has already been added as tab" << std::endl;
return;
}
auto control = GlobalUserInterface().findControl(controlName);
if (!control)
{
throw std::logic_error("There's no such control: " + controlName);
}
// Make sure the notebook is visible before adding pages
Show();
// Load the icon
int tabIconIndex = control->getIcon().empty() ? -1 :
_imageList->Add(wxutil::GetLocalBitmap(control->getIcon()));
auto content = control->createWidget(this);
content->Reparent(this);
AddPage(content, control->getDisplayName(), false, tabIconIndex);
// Add this page by copy to the local list
_pages.emplace_back(Page
{
control->getControlName(),
tabIconIndex,
content,
});
// Select the new page
SetSelection(FindPage(content));
}
void PropertyNotebook::removeControl(const std::string& controlName)
{
// Find the page with that name
for (auto i = _pages.begin(); i != _pages.end(); ++i)
{
// Skip the wrong ones
if (i->controlName != controlName) continue;
// Remove the page from the notebook
DeletePage(FindPage(i->page));
// Remove the entry and break the loop
_pages.erase(i);
break;
}
}
std::string PropertyNotebook::getSelectedPageName()
{
auto selectedPageIndex = GetSelection();
if (selectedPageIndex < 0) return {};
// Look up the page in the _pages dictionary by the page widget
auto win = GetPage(static_cast<size_t>(selectedPageIndex));
return findControlNameByWindow(win);
}
void PropertyNotebook::onPageSwitch(wxBookCtrlEvent& ev)
{
// Store the page's name into the registry for later retrieval
registry::setValue(RKEY_LAST_SHOWN_PAGE, getSelectedPageName());
auto selectedPageIndex = GetSelection();
// Set the active/inactive status of all windows
for (std::size_t i = 0; i < GetPageCount(); ++i)
{
auto win = GetPage(i);
if (i == selectedPageIndex)
{
_layout.ensureControlIsActive(win);
}
else
{
_layout.ensureControlIsInactive(win);
}
}
// Be sure to skip the event, otherwise pages stay hidden
ev.Skip();
}
void PropertyNotebook::onTabRightClick(wxAuiNotebookEvent& ev)
{
SetSelection(ev.GetSelection());
_popupMenu->show(this);
}
std::string PropertyNotebook::getSelectedControlName()
{
auto selectedPageIndex = GetSelection();
if (selectedPageIndex < 0) return {};
// Look up the page in the _pages dictionary by the page widget
auto win = GetPage(static_cast<size_t>(selectedPageIndex));
if (win == nullptr) return {};
return findControlNameByWindow(win);
}
void PropertyNotebook::undockSelectedTab()
{
auto controlName = getSelectedControlName();
closeSelectedTab();
// Get the control name and create a floating window
_layout.createFloatingControl(controlName);
}
void PropertyNotebook::closeSelectedTab()
{
auto controlName = getSelectedControlName();
if (!controlName.empty())
{
removeControl(controlName);
}
}
std::string PropertyNotebook::findControlNameByWindow(wxWindow* window)
{
for (const auto& page : _pages)
{
if (page.page == window)
{
return page.controlName;
}
}
return {};
}
int PropertyNotebook::findControlIndexByName(const std::string& controlName)
{
for (const auto& page : _pages)
{
if (page.controlName == controlName)
{
return FindPage(page.page);
}
}
return -1;
}
int PropertyNotebook::findImageIndexForControl(const std::string& controlName)
{
for (const auto& page : _pages)
{
if (page.controlName == controlName)
{
return page.tabIconIndex;
}
}
return -1;
}
void PropertyNotebook::saveState()
{
GlobalRegistry().deleteXPath(RKEY_PAGES);
auto pagesKey = GlobalRegistry().createKey(RKEY_PAGES);
// Save the names and position of all controls
for (std::size_t i = 0; i < GetPageCount(); ++i)
{
auto controlName = findControlNameByWindow(GetPage(i));
if (controlName.empty()) continue;
auto paneNode = pagesKey.createChild(PAGE_NODE_NAME);
paneNode.setAttributeValue(CONTROL_NAME_ATTRIBUTE, controlName);
}
registry::setValue(RKEY_LAST_SHOWN_PAGE, getSelectedPageName());
}
void PropertyNotebook::restoreState()
{
std::vector<std::string> sortedControlNames;
// Restore all missing pages
auto pageNodes = GlobalRegistry().findXPath(RKEY_PAGES + "//*");
for (int i = 0; i < pageNodes.size(); ++i)
{
if (pageNodes.at(i).getName() != PAGE_NODE_NAME) continue;
auto controlName = pageNodes.at(i).getAttributeValue(CONTROL_NAME_ATTRIBUTE);
auto existingIndex = findControlIndexByName(controlName);
if (existingIndex == -1)
{
addControl(controlName);
existingIndex = findControlIndexByName(controlName);
}
if (existingIndex != i)
{
// Move to correct position, keeping image and caption intact
auto window = GetPage(existingIndex);
auto imageIndex = findImageIndexForControl(controlName);
auto pageText = GetPageText(existingIndex);
RemovePage(existingIndex);
InsertPage(i, window, pageText, false, imageIndex);
}
}
auto lastShownPage = registry::getValue<std::string>(RKEY_LAST_SHOWN_PAGE);
for (const auto& page : _pages)
{
if (page.controlName == lastShownPage)
{
SetSelection(FindPage(page.page));
break;
}
}
}
bool PropertyNotebook::controlExists(const std::string& controlName)
{
return findControlIndexByName(controlName) != -1;
}
void PropertyNotebook::focusControl(const std::string& controlName)
{
if (auto index = findControlIndexByName(controlName); index != -1)
{
SetSelection(index);
// To prevent the Surface Inspector from focusing the entry box after pressing the shortcut
if (auto panel = wxDynamicCast(GetPage(index), wxPanel); panel != nullptr)
{
panel->SetFocusIgnoringChildren();
}
}
}
void PropertyNotebook::showDropHint(const wxRect& size)
{
if (!_dropHint)
{
_dropHint = new wxFrame(this, wxID_ANY, wxEmptyString,
wxDefaultPosition, wxSize(1, 1),
wxFRAME_TOOL_WINDOW | wxFRAME_FLOAT_ON_PARENT | wxFRAME_NO_TASKBAR | wxNO_BORDER);
_dropHint->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_ACTIVECAPTION));
_dropHint->SetTransparent(128);
}
_dropHint->SetSize(size);
_dropHint->Raise();
_dropHint->Show();
}
void PropertyNotebook::hideDropHint()
{
if (!_dropHint) return;
_dropHint->Destroy();
_dropHint = nullptr;
}
}