| 1 |
#include "GuiSelector.h" |
| 2 |
|
| 3 |
#include "wxutil/dataview/VFSTreePopulator.h" |
| 4 |
|
| 5 |
#include "i18n.h" |
| 6 |
#include "ui/imainframe.h" |
| 7 |
#include "gui/GuiManager.h" |
| 8 |
#include "wxutil/dialog/MessageBox.h" |
| 9 |
|
| 10 |
#include "ReadablePopulator.h" |
| 11 |
#include "ReadableEditorDialog.h" |
| 12 |
|
| 13 |
#include "wxutil/Bitmap.h" |
| 14 |
#include <wx/notebook.h> |
| 15 |
|
| 16 |
namespace ui |
| 17 |
{ |
| 18 |
|
| 19 |
namespace |
| 20 |
{ |
| 21 |
const char* const WINDOW_TITLE = N_("Choose a Gui Definition..."); |
| 22 |
|
| 23 |
const int WINDOW_WIDTH = 400; |
| 24 |
const int WINDOW_HEIGHT = 500; |
| 25 |
|
| 26 |
const char* const GUI_ICON = "sr_icon_readable.png"; |
| 27 |
const char* const FOLDER_ICON = "folder16.png"; |
| 28 |
} |
| 29 |
|
| 30 |
GuiSelector::GuiSelector(bool twoSided, ReadableEditorDialog* editorDialog) : |
| 31 |
DialogBase(_(WINDOW_TITLE), editorDialog), |
| 32 |
_editorDialog(editorDialog), |
| 33 |
_notebook(NULL), |
| 34 |
_oneSidedStore(new wxutil::TreeModel(_columns)), |
| 35 |
_twoSidedStore(new wxutil::TreeModel(_columns)), |
| 36 |
_oneSidedView(NULL), |
| 37 |
_twoSidedView(NULL), |
| 38 |
_guiIcon(wxutil::GetLocalBitmap(GUI_ICON)), |
| 39 |
_folderIcon(wxutil::GetLocalBitmap(FOLDER_ICON)) |
| 40 |
{ |
| 41 |
// Set the windowsize and default border width in accordance to the HIG |
| 42 |
SetSize(WINDOW_WIDTH, WINDOW_HEIGHT); |
| 43 |
|
| 44 |
populateWindow(); |
| 45 |
|
| 46 |
// Set the current page and connect the switch-page signal afterwards. |
| 47 |
_notebook->SetSelection(twoSided ? 1 : 0); |
| 48 |
_notebook->Connect(wxEVT_NOTEBOOK_PAGE_CHANGED, |
| 49 |
wxBookCtrlEventHandler(GuiSelector::onPageSwitch), NULL, this); |
| 50 |
|
| 51 |
// We start with an empty selection, so de-sensitise the OK button |
| 52 |
FindWindowById(wxID_OK, this)->Enable(false); |
| 53 |
} |
| 54 |
|
| 55 |
bool GuiSelector::Destroy() |
| 56 |
{ |
| 57 |
// Prevent the page switch event from firing after window destruction |
| 58 |
// In wxGTK the window might not be destroyed right away (only later in an idle event) |
| 59 |
// which will trigger page switch events and inadvertently change our GUI selection. |
| 60 |
_notebook->Disconnect(wxEVT_NOTEBOOK_PAGE_CHANGED, |
| 61 |
wxBookCtrlEventHandler(GuiSelector::onPageSwitch), NULL, this); |
| 62 |
|
| 63 |
return wxutil::DialogBase::Destroy(); |
| 64 |
} |
| 65 |
|
| 66 |
std::string GuiSelector::Run(bool twoSided, ReadableEditorDialog* editorDialog) |
| 67 |
{ |
| 68 |
GuiSelector* dialog = new GuiSelector(twoSided, editorDialog); |
| 69 |
|
| 70 |
std::string rv = ""; |
| 71 |
|
| 72 |
try |
| 73 |
{ |
| 74 |
dialog->fillTrees(); // may throw OperationAbortedException |
| 75 |
|
| 76 |
if (dialog->ShowModal() == wxID_OK) |
| 77 |
{ |
| 78 |
rv = "guis/" + dialog->_name; |
| 79 |
} |
| 80 |
} |
| 81 |
catch (wxutil::ModalProgressDialog::OperationAbortedException&) |
| 82 |
{ |
| 83 |
rv = ""; |
| 84 |
} |
| 85 |
|
| 86 |
dialog->Destroy(); |
| 87 |
|
| 88 |
return rv; |
| 89 |
} |
| 90 |
|
| 91 |
void GuiSelector::visit(wxutil::TreeModel& /* store */, wxutil::TreeModel::Row& row, |
| 92 |
const std::string& path, bool isExplicit) |
| 93 |
{ |
| 94 |
// Get the display name by stripping off everything before the last |
| 95 |
// slash |
| 96 |
std::string displayName = path.substr(path.rfind("/") + 1); |
| 97 |
displayName = displayName.substr(0,displayName.rfind(".")); |
| 98 |
|
| 99 |
// Fill in the column values |
| 100 |
row[_columns.name] = wxVariant(wxDataViewIconText(displayName, isExplicit ? _guiIcon : _folderIcon)); |
| 101 |
row[_columns.fullName] = path; |
| 102 |
row[_columns.isFolder] = !isExplicit; |
| 103 |
|
| 104 |
row.SendItemAdded(); |
| 105 |
} |
| 106 |
|
| 107 |
void GuiSelector::fillTrees() |
| 108 |
{ |
| 109 |
wxutil::VFSTreePopulator popOne(_oneSidedStore); |
| 110 |
wxutil::VFSTreePopulator popTwo(_twoSidedStore); |
| 111 |
|
| 112 |
ReadablePopulator walker(popOne, popTwo); |
| 113 |
GlobalGuiManager().foreachGui(walker); |
| 114 |
|
| 115 |
popOne.forEachNode(*this); |
| 116 |
popTwo.forEachNode(*this); |
| 117 |
|
| 118 |
_oneSidedStore->SortModelFoldersFirst(_columns.name, _columns.isFolder); |
| 119 |
_twoSidedStore->SortModelFoldersFirst(_columns.name, _columns.isFolder); |
| 120 |
|
| 121 |
_oneSidedView->AssociateModel(_oneSidedStore.get()); |
| 122 |
_twoSidedView->AssociateModel(_twoSidedStore.get()); |
| 123 |
} |
| 124 |
|
| 125 |
void GuiSelector::populateWindow() |
| 126 |
{ |
| 127 |
// Add a vbox for the dialog elements |
| 128 |
SetSizer(new wxBoxSizer(wxVERTICAL)); |
| 129 |
|
| 130 |
// Add a vbox for the dialog elements |
| 131 |
wxBoxSizer* vbox = new wxBoxSizer(wxVERTICAL); |
| 132 |
GetSizer()->Add(vbox, 1, wxEXPAND | wxALL, 12); |
| 133 |
|
| 134 |
// Create the tabs |
| 135 |
_notebook = new wxNotebook(this, wxID_ANY); |
| 136 |
|
| 137 |
// One-Sided Readables Tab |
| 138 |
auto tempOneSidedModel = wxutil::TreeModel::Ptr(new wxutil::TreeModel(_columns)); |
| 139 |
_oneSidedView = wxutil::TreeView::CreateWithModel(_notebook, tempOneSidedModel.get(), wxDV_NO_HEADER); |
| 140 |
_oneSidedView->AppendIconTextColumn(_("Gui Path"), _columns.name.getColumnIndex(), |
| 141 |
wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT, wxDATAVIEW_COL_SORTABLE); |
| 142 |
_oneSidedView->Connect(wxEVT_DATAVIEW_SELECTION_CHANGED, |
| 143 |
wxDataViewEventHandler(GuiSelector::onSelectionChanged), NULL, this); |
| 144 |
|
| 145 |
_notebook->AddPage(_oneSidedView, _("One-Sided Readable Guis")); |
| 146 |
|
| 147 |
// Two-Sided Readables Tab |
| 148 |
auto tempTwoSidedModel = wxutil::TreeModel::Ptr(new wxutil::TreeModel(_columns)); |
| 149 |
_twoSidedView = wxutil::TreeView::CreateWithModel(_notebook, tempTwoSidedModel.get(), wxDV_NO_HEADER); |
| 150 |
_twoSidedView->AppendIconTextColumn(_("Gui Path"), _columns.name.getColumnIndex(), |
| 151 |
wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT, wxDATAVIEW_COL_SORTABLE); |
| 152 |
_twoSidedView->Connect(wxEVT_DATAVIEW_SELECTION_CHANGED, |
| 153 |
wxDataViewEventHandler(GuiSelector::onSelectionChanged), NULL, this); |
| 154 |
|
| 155 |
_notebook->AddPage(_twoSidedView, _("Two-Sided Readable Guis")); |
| 156 |
|
| 157 |
// Packing |
| 158 |
vbox->Add(_notebook, 1, wxEXPAND | wxBOTTOM, 6); |
| 159 |
vbox->Add(CreateStdDialogButtonSizer(wxOK | wxCANCEL), 0, wxALIGN_RIGHT); |
| 160 |
} |
| 161 |
|
| 162 |
void GuiSelector::onPageSwitch(wxBookCtrlEvent& ev) |
| 163 |
{ |
| 164 |
if (ev.GetSelection() == 0) |
| 165 |
{ |
| 166 |
_editorDialog->useOneSidedEditing(); |
| 167 |
} |
| 168 |
else |
| 169 |
{ |
| 170 |
_editorDialog->useTwoSidedEditing(); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
void GuiSelector::onSelectionChanged(wxDataViewEvent& ev) |
| 175 |
{ |
| 176 |
wxutil::TreeView* view = dynamic_cast<wxutil::TreeView*>(ev.GetEventObject()); |
| 177 |
|
| 178 |
assert(view != NULL); |
| 179 |
|
| 180 |
wxDataViewItem item = view->GetSelection(); |
| 181 |
|
| 182 |
if (item.IsOk()) |
| 183 |
{ |
| 184 |
wxutil::TreeModel::Row row(item, *view->GetModel()); |
| 185 |
|
| 186 |
if (!row[_columns.isFolder].getBool()) |
| 187 |
{ |
| 188 |
_name = row[_columns.fullName]; |
| 189 |
std::string guiPath = "guis/" + _name; |
| 190 |
|
| 191 |
_editorDialog->updateGuiView(this, guiPath); |
| 192 |
FindWindowById(wxID_OK, this)->Enable(true); |
| 193 |
return; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
FindWindowById(wxID_OK, this)->Enable(false); |
| 198 |
} |
| 199 |
|
| 200 |
} // namespace |