/
GuiSelector.cpp
202 lines (156 loc) · 5.82 KB
/
GuiSelector.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
#include "GuiSelector.h"
#include "wxutil/VFSTreePopulator.h"
#include "i18n.h"
#include "imainframe.h"
#include "iuimanager.h"
#include "gui/GuiManager.h"
#include "wxutil/dialog/MessageBox.h"
#include "ReadablePopulator.h"
#include "ReadableEditorDialog.h"
#include <wx/artprov.h>
#include <wx/notebook.h>
namespace ui
{
namespace
{
const char* const WINDOW_TITLE = N_("Choose a Gui Definition...");
const int WINDOW_WIDTH = 400;
const int WINDOW_HEIGHT = 500;
const char* const GUI_ICON = "sr_icon_readable.png";
const char* const FOLDER_ICON = "folder16.png";
}
GuiSelector::GuiSelector(bool twoSided, ReadableEditorDialog* editorDialog) :
DialogBase(_(WINDOW_TITLE), editorDialog),
_editorDialog(editorDialog),
_notebook(NULL),
_oneSidedStore(new wxutil::TreeModel(_columns)),
_twoSidedStore(new wxutil::TreeModel(_columns)),
_oneSidedView(NULL),
_twoSidedView(NULL)
{
_guiIcon.CopyFromBitmap(wxArtProvider::GetBitmap(GlobalUIManager().ArtIdPrefix() + GUI_ICON));
_folderIcon.CopyFromBitmap(wxArtProvider::GetBitmap(GlobalUIManager().ArtIdPrefix() + FOLDER_ICON));
// Set the windowsize and default border width in accordance to the HIG
SetSize(WINDOW_WIDTH, WINDOW_HEIGHT);
populateWindow();
// Set the current page and connect the switch-page signal afterwards.
_notebook->SetSelection(twoSided ? 1 : 0);
_notebook->Connect(wxEVT_NOTEBOOK_PAGE_CHANGED,
wxBookCtrlEventHandler(GuiSelector::onPageSwitch), NULL, this);
// We start with an empty selection, so de-sensitise the OK button
FindWindowById(wxID_OK, this)->Enable(false);
}
bool GuiSelector::Destroy()
{
// Prevent the page switch event from firing after window destruction
// In wxGTK the window might not be destroyed right away (only later in an idle event)
// which will trigger page switch events and inadvertently change our GUI selection.
_notebook->Disconnect(wxEVT_NOTEBOOK_PAGE_CHANGED,
wxBookCtrlEventHandler(GuiSelector::onPageSwitch), NULL, this);
return wxutil::DialogBase::Destroy();
}
std::string GuiSelector::Run(bool twoSided, ReadableEditorDialog* editorDialog)
{
GuiSelector* dialog = new GuiSelector(twoSided, editorDialog);
std::string rv = "";
try
{
dialog->fillTrees(); // may throw OperationAbortedException
if (dialog->ShowModal() == wxID_OK)
{
rv = "guis/" + dialog->_name;
}
}
catch (wxutil::ModalProgressDialog::OperationAbortedException&)
{
rv = "";
}
dialog->Destroy();
return rv;
}
void GuiSelector::visit(wxutil::TreeModel& /* store */, wxutil::TreeModel::Row& row,
const std::string& path, bool isExplicit)
{
// Get the display name by stripping off everything before the last
// slash
std::string displayName = path.substr(path.rfind("/") + 1);
displayName = displayName.substr(0,displayName.rfind("."));
// Fill in the column values
row[_columns.name] = wxVariant(wxDataViewIconText(displayName, isExplicit ? _guiIcon : _folderIcon));
row[_columns.fullName] = path;
row[_columns.isFolder] = !isExplicit;
row.SendItemAdded();
}
void GuiSelector::fillTrees()
{
wxutil::VFSTreePopulator popOne(_oneSidedStore);
wxutil::VFSTreePopulator popTwo(_twoSidedStore);
ReadablePopulator walker(popOne, popTwo);
GlobalGuiManager().foreachGui(walker);
popOne.forEachNode(*this);
popTwo.forEachNode(*this);
_oneSidedStore->SortModelFoldersFirst(_columns.name, _columns.isFolder);
_twoSidedStore->SortModelFoldersFirst(_columns.name, _columns.isFolder);
_oneSidedView->AssociateModel(_oneSidedStore.get());
_twoSidedView->AssociateModel(_twoSidedStore.get());
}
void GuiSelector::populateWindow()
{
// Add a vbox for the dialog elements
SetSizer(new wxBoxSizer(wxVERTICAL));
// Add a vbox for the dialog elements
wxBoxSizer* vbox = new wxBoxSizer(wxVERTICAL);
GetSizer()->Add(vbox, 1, wxEXPAND | wxALL, 12);
// Create the tabs
_notebook = new wxNotebook(this, wxID_ANY);
// One-Sided Readables Tab
auto tempOneSidedModel = wxutil::TreeModel::Ptr(new wxutil::TreeModel(_columns));
_oneSidedView = wxutil::TreeView::CreateWithModel(_notebook, tempOneSidedModel.get(), wxDV_NO_HEADER);
_oneSidedView->AppendIconTextColumn(_("Gui Path"), _columns.name.getColumnIndex(),
wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT, wxDATAVIEW_COL_SORTABLE);
_oneSidedView->Connect(wxEVT_DATAVIEW_SELECTION_CHANGED,
wxDataViewEventHandler(GuiSelector::onSelectionChanged), NULL, this);
_notebook->AddPage(_oneSidedView, _("One-Sided Readable Guis"));
// Two-Sided Readables Tab
auto tempTwoSidedModel = wxutil::TreeModel::Ptr(new wxutil::TreeModel(_columns));
_twoSidedView = wxutil::TreeView::CreateWithModel(_notebook, tempTwoSidedModel.get(), wxDV_NO_HEADER);
_twoSidedView->AppendIconTextColumn(_("Gui Path"), _columns.name.getColumnIndex(),
wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT, wxDATAVIEW_COL_SORTABLE);
_twoSidedView->Connect(wxEVT_DATAVIEW_SELECTION_CHANGED,
wxDataViewEventHandler(GuiSelector::onSelectionChanged), NULL, this);
_notebook->AddPage(_twoSidedView, _("Two-Sided Readable Guis"));
// Packing
vbox->Add(_notebook, 1, wxEXPAND | wxBOTTOM, 6);
vbox->Add(CreateStdDialogButtonSizer(wxOK | wxCANCEL), 0, wxALIGN_RIGHT);
}
void GuiSelector::onPageSwitch(wxBookCtrlEvent& ev)
{
if (ev.GetSelection() == 0)
{
_editorDialog->useOneSidedEditing();
}
else
{
_editorDialog->useTwoSidedEditing();
}
}
void GuiSelector::onSelectionChanged(wxDataViewEvent& ev)
{
wxutil::TreeView* view = dynamic_cast<wxutil::TreeView*>(ev.GetEventObject());
assert(view != NULL);
wxDataViewItem item = view->GetSelection();
if (item.IsOk())
{
wxutil::TreeModel::Row row(item, *view->GetModel());
if (!row[_columns.isFolder].getBool())
{
_name = row[_columns.fullName];
std::string guiPath = "guis/" + _name;
_editorDialog->updateGuiView(this, guiPath);
FindWindowById(wxID_OK, this)->Enable(true);
return;
}
}
FindWindowById(wxID_OK, this)->Enable(false);
}
} // namespace