Debian Codegraph @

plugins/dm.gui/ReadableEditorDialog.cpp

raw
1 // Project related
2 #include "ReadableEditorDialog.h"
3 #include "XdFileChooserDialog.h"
4 #include "XDataSelector.h"
5 #include "GuiSelector.h"
6 #include "gui/GuiManager.h"
7 #include "TextViewInfoDialog.h"
8 #include "ReadableGuiView.h"
9
10 // General
11 #include "selectionlib.h"
12 #include "wxutil/dialog/MessageBox.h"
13
14 #include "registry/registry.h"
15 #include "string/string.h"
16 #include "string/convert.h"
17 #include "os/file.h"
18 #include "os/path.h"
19 #include "i18n.h"
20 #include "itextstream.h"
21
22 // Modules
23 #include "iundo.h"
24 #include "ui/imainframe.h"
25 #include "ientity.h"
26 #include "imap.h"
27 #include "igame.h"
28 #include "ui/idialogmanager.h"
29
30 #include <wx/sizer.h>
31 #include <wx/textctrl.h>
32 #include <wx/button.h>
33 #include <wx/spinctrl.h>
34 #include <wx/radiobut.h>
35 #include <wx/menu.h>
36
37 namespace ui
38 {
39
40 // consts:
41 namespace
42 {
43 const char* const WINDOW_TITLE = N_("Readable Editor");
44
45 const char* const NO_ENTITY_ERROR = N_("Cannot run Readable Editor on this selection.\n"
46 "Please select a single XData entity.");
47
48 enum MenuItemId
49 {
50 InsertWholePage = 1,
51 InsertLeft,
52 InsertRight,
53 DeleteWholePage,
54 DeleteLeft,
55 DeleteRight,
56 AppendPage,
57 PrependPage,
58 ShowXDataSummary,
59 ShowDuplicatedDefs,
60 ShowGuiImportSummary,
61 };
62 } // namespace
63
64 //////////////////////////////////////////////////////////////////////////////
65 // Public and protected methods:
66 ReadableEditorDialog::ReadableEditorDialog(Entity* entity) :
67 DialogBase(_(WINDOW_TITLE)),
68 _guiView(NULL),
69 _entity(entity),
70 _xdLoader(new XData::XDataLoader()),
71 _currentPageIndex(0),
72 _xdNameSpecified(false),
73 _runningGuiLayoutCheck(false),
74 _runningXDataUniquenessCheck(false),
75 _useDefaultFilename(true),
76 _saveInProgress(false)
77 {
78 wxPanel* mainPanel = loadNamedPanel(this, "ReadableEditorMainPanel");
79
80 wxPanel* previewPanel = findNamedObject<wxPanel>(this, "ReadableEditorPreviewPanel");
81 _guiView = new gui::ReadableGuiView(previewPanel);
82 previewPanel->GetSizer()->Add(_guiView, 1, wxEXPAND);
83
84 setupGeneralPropertiesInterface();
85 setupPageRelatedInterface();
86 setupButtonPanel();
87 createMenus();
88
89 mainPanel->Layout();
90 mainPanel->Fit();
91 Fit();
92 CenterOnParent();
93 }
94
95 int ReadableEditorDialog::ShowModal()
96 {
97 // Load the initial values from the entity
98 if (!initControlsFromEntity())
99 {
100 // User clicked cancel, so destroy the window.
101 return wxID_CANCEL;
102 }
103
104 // Initialize proper editing controls.
105 populateControlsFromXData();
106
107 // Initialise the GL widget after the widgets have been shown
108 _guiView->initialiseView();
109
110 return DialogBase::ShowModal();
111 }
112
113 void ReadableEditorDialog::RunDialog(const cmd::ArgumentList& args)
114 {
115 // Check prerequisites
116 const SelectionInfo& info = GlobalSelectionSystem().getSelectionInfo();
117
118 if (info.entityCount == 1 && info.totalCount == info.entityCount)
119 {
120 // Check the entity type
121 Entity* entity = Node_getEntity(GlobalSelectionSystem().ultimateSelected());
122
123 if (entity != NULL && entity->getKeyValue("editor_readable") == "1")
124 {
125 // Show the dialog
126 ReadableEditorDialog* dialog = new ReadableEditorDialog(entity);
127
128 dialog->ShowModal();
129 dialog->Destroy();
130 return;
131 }
132 }
133
134 // Exactly one redable entity must be selected.
135 wxutil::Messagebox::ShowError(_(NO_ENTITY_ERROR));
136 }
137
138 // UI Creation
139
140 void ReadableEditorDialog::setupGeneralPropertiesInterface()
141 {
142 makeLabelBold(this, "ReadableEditorGeneralLabel");
143 makeLabelBold(this, "ReadableEditorPageLabel");
144
145 // Readable Name
146 _nameEntry = findNamedObject<wxTextCtrl>(this, "ReadableEditorInventoryName");
147 _nameEntry->Connect(wxEVT_CHAR, wxKeyEventHandler(ReadableEditorDialog::onChar), NULL, this);
148
149 // XData Name
150 _xDataNameEntry = findNamedObject<wxTextCtrl>(this, "ReadableEditorXDataName");
151 _xDataNameEntry->Connect(wxEVT_CHAR, wxKeyEventHandler(ReadableEditorDialog::onChar), NULL, this);
152 _xDataNameEntry->Connect(wxEVT_KILL_FOCUS, wxFocusEventHandler(ReadableEditorDialog::onFocusOut), NULL, this);
153
154 // Add a browse-button.
155 findNamedObject<wxButton>(this, "ReadableEditorXDBrowseButton")->Connect(wxEVT_BUTTON,
156 wxCommandEventHandler(ReadableEditorDialog::onBrowseXd), NULL, this);
157
158 // Page count
159 _numPages = findNamedObject<wxSpinCtrl>(this, "ReadableEditorNumPages");
160 _numPages->SetRange(1, 20);
161 _numPages->Connect(wxEVT_SPINCTRL, wxSpinEventHandler(ReadableEditorDialog::onNumPagesChanged), NULL, this);
162 _numPages->Connect(wxEVT_CHAR, wxKeyEventHandler(ReadableEditorDialog::onChar), NULL, this);
163
164 // Page Layout:
165 _oneSidedButton = findNamedObject<wxRadioButton>(this, "ReadableEditorOneSided");
166 _oneSidedButton->Connect(wxEVT_RADIOBUTTON, wxCommandEventHandler(ReadableEditorDialog::onOneSided), NULL, this);
167
168 _twoSidedButton = findNamedObject<wxRadioButton>(this, "ReadableEditorTwoSided");
169 _twoSidedButton->Connect(wxEVT_RADIOBUTTON, wxCommandEventHandler(ReadableEditorDialog::onTwoSided), NULL, this);
170
171 // Pageturn Sound
172 _pageTurnEntry = findNamedObject<wxTextCtrl>(this, "ReadableEditorPageTurnSound");
173 }
174
175 void ReadableEditorDialog::setupPageRelatedInterface()
176 {
177 // Insert Button
178 findNamedObject<wxButton>(this, "ReadableEditorInsertPage")->Connect(wxEVT_BUTTON,
179 wxCommandEventHandler(ReadableEditorDialog::onInsert), NULL, this);
180
181 // Delete Button
182 findNamedObject<wxButton>(this, "ReadableEditorDeletePage")->Connect(wxEVT_BUTTON,
183 wxCommandEventHandler(ReadableEditorDialog::onDelete), NULL, this);
184
185 // Page Switcher
186 findNamedObject<wxButton>(this, "ReadableEditorGotoFirstPage")->Connect(wxEVT_BUTTON,
187 wxCommandEventHandler(ReadableEditorDialog::onFirstPage), NULL, this);
188 findNamedObject<wxButton>(this, "ReadableEditorGotoPreviousPage")->Connect(wxEVT_BUTTON,
189 wxCommandEventHandler(ReadableEditorDialog::onPrevPage), NULL, this);
190 findNamedObject<wxButton>(this, "ReadableEditorGotoNextPage")->Connect(wxEVT_BUTTON,
191 wxCommandEventHandler(ReadableEditorDialog::onNextPage), NULL, this);
192 findNamedObject<wxButton>(this, "ReadableEditorGotoLastPage")->Connect(wxEVT_BUTTON,
193 wxCommandEventHandler(ReadableEditorDialog::onLastPage), NULL, this);
194
195 _curPageDisplay = findNamedObject<wxStaticText>(this, "ReadableEditorCurPage");
196
197 // Add a gui chooser with a browse-button
198 _guiEntry = findNamedObject<wxTextCtrl>(this, "ReadableEditorGuiDefinition");
199 _guiEntry->Connect(wxEVT_CHAR, wxKeyEventHandler(ReadableEditorDialog::onChar), NULL, this);
200 _guiEntry->Connect(wxEVT_KILL_FOCUS, wxFocusEventHandler(ReadableEditorDialog::onFocusOut), NULL, this);
201
202 findNamedObject<wxButton>(this, "ReadableEditorGuiBrowseButton")->Connect(wxEVT_BUTTON,
203 wxCommandEventHandler(ReadableEditorDialog::onBrowseGui), NULL, this);
204
205 // Create "left" and "right" labels and add them to the first row of the table
206 _pageLeftLabel = findNamedObject<wxStaticText>(this, "ReadableEditorPageLeftLabel");
207 _pageRightLabel = findNamedObject<wxStaticText>(this, "ReadableEditorPageRightLabel");
208
209 _textViewTitle = findNamedObject<wxTextCtrl>(this, "ReadableEditorTitleLeft");
210 _textViewTitle->Connect(wxEVT_TEXT, wxCommandEventHandler(ReadableEditorDialog::onTextChanged), NULL, this);
211
212 _textViewRightTitle = findNamedObject<wxTextCtrl>(this, "ReadableEditorTitleRight");
213 _textViewRightTitle->Connect(wxEVT_TEXT, wxCommandEventHandler(ReadableEditorDialog::onTextChanged), NULL, this);
214
215 // Create "body" label and body-textViews and add them to the third row of the table. Add the key-press-event.
216 _textViewBody = findNamedObject<wxTextCtrl>(this, "ReadableEditorBodyLeft");
217 _textViewBody->Connect(wxEVT_TEXT, wxCommandEventHandler(ReadableEditorDialog::onTextChanged), NULL, this);
218
219 _textViewRightBody = findNamedObject<wxTextCtrl>(this, "ReadableEditorBodyRight");
220 _textViewRightBody->Connect(wxEVT_TEXT, wxCommandEventHandler(ReadableEditorDialog::onTextChanged), NULL, this);
221 }
222
223 void ReadableEditorDialog::createMenus()
224 {
225 // Insert menu
226 _insertMenu.reset(new wxMenu);
227 _insertMenu->Append(InsertWholePage, _("Insert whole Page"), "");
228 _insertMenu->Append(InsertLeft, _("Insert on left Side"), "");
229 _insertMenu->Append(InsertRight, _("Insert on right Side"), "");
230
231 _insertMenu->Connect(wxEVT_MENU, wxCommandEventHandler(ReadableEditorDialog::onMenuItemClick), NULL, this);
232
233 // Delete Menu
234 _deleteMenu.reset(new wxMenu);
235 _deleteMenu->Append(DeleteWholePage, _("Delete whole Page"), "");
236 _deleteMenu->Append(DeleteLeft, _("Delete on left Side"), "");
237 _deleteMenu->Append(DeleteRight, _("Delete on right Side"), "");
238
239 _deleteMenu->Connect(wxEVT_MENU, wxCommandEventHandler(ReadableEditorDialog::onMenuItemClick), NULL, this);
240
241 // Append Menu
242 _appendMenu.reset(new wxMenu);
243 _appendMenu->Append(AppendPage, _("Append Page"), "");
244 _appendMenu->Connect(wxEVT_MENU, wxCommandEventHandler(ReadableEditorDialog::onMenuItemClick), NULL, this);
245
246 // Prepend Menu
247 _prependMenu.reset(new wxMenu);
248 _prependMenu->Append(PrependPage, _("Prepend Page"), "");
249 _prependMenu->Connect(wxEVT_MENU, wxCommandEventHandler(ReadableEditorDialog::onMenuItemClick), NULL, this);
250
251 // Tools Menu
252 _toolsMenu.reset(new wxMenu);
253 _toolsMenu->Append(ShowXDataSummary, _("Show last XData import summary"), "");
254 _toolsMenu->Append(ShowDuplicatedDefs, _("Show duplicated definitions"), "");
255 _toolsMenu->Append(ShowGuiImportSummary, _("Show Gui import summary"), "");
256 _toolsMenu->Connect(wxEVT_MENU, wxCommandEventHandler(ReadableEditorDialog::onMenuItemClick), NULL, this);
257 }
258
259 void ReadableEditorDialog::setupButtonPanel()
260 {
261 findNamedObject<wxButton>(this, "ReadableEditorSave")->Connect(wxEVT_BUTTON,
262 wxCommandEventHandler(ReadableEditorDialog::onSave), NULL, this);
263 findNamedObject<wxButton>(this, "ReadableEditorCancel")->Connect(wxEVT_BUTTON,
264 wxCommandEventHandler(ReadableEditorDialog::onCancel), NULL, this);
265 findNamedObject<wxButton>(this, "ReadableEditorSaveAndClose")->Connect(wxEVT_BUTTON,
266 wxCommandEventHandler(ReadableEditorDialog::onSaveClose), NULL, this);
267
268 findNamedObject<wxButton>(this, "ReadableEditorTools")->Connect(wxEVT_BUTTON,
269 wxCommandEventHandler(ReadableEditorDialog::onToolsClicked), NULL, this);
270 }
271
272 //////////////////////////////////////////////////////////////////////////////
273 // Private Methods:
274
275 bool ReadableEditorDialog::initControlsFromEntity()
276 {
277 // Inv_name
278 _nameEntry->SetValue(_entity->getKeyValue("inv_name"));
279
280 // Xdata contents
281 _xDataNameEntry->SetValue(_entity->getKeyValue("xdata_contents"));
282
283 // Construct the map-based Filename
284 _mapBasedFilename = GlobalMapModule().getMapName();
285 std::size_t nameStartPos = _mapBasedFilename.rfind("/") + 1;
286
287 if (nameStartPos != std::string::npos)
288 {
289 _mapBasedFilename = _mapBasedFilename.substr(nameStartPos, _mapBasedFilename.rfind(".") - nameStartPos);
290 }
291
292 std::string defaultXdName = "readables/" + _mapBasedFilename + "/" + _("<Name_Here>");
293 _mapBasedFilename += ".xd";
294
295 // Load xdata
296 if (!_entity->getKeyValue("xdata_contents").empty())
297 {
298 _xdNameSpecified = true;
299
300 try
301 {
302 int result = XdFileChooserDialog::Import(
303 _entity->getKeyValue("xdata_contents"), _xData, _xdFilename, _xdLoader, this
304 );
305
306 if (result == wxID_OK)
307 {
308 _useDefaultFilename = false;
309 refreshWindowTitle();
310 return true;
311 }
312
313 return false; // cancelled
314 }
315 catch (XdFileChooserDialog::ImportFailedException&)
316 {
317 std::string msg = fmt::format(_("Failed to import {0}."), _entity->getKeyValue("xdata_contents"));
318 msg += "\n";
319 msg += _("Creating a new XData definition...");
320 msg += "\n\n";
321 msg += _("Do you want to open the import summary?");
322
323 wxutil::Messagebox dialog(_("Import failed"),
324 msg, ui::IDialog::MESSAGE_ASK, this);
325
326 if (dialog.run() == ui::IDialog::RESULT_YES)
327 {
328 showXdImportSummary();
329 }
330
331 updateGuiView();
332 }
333 }
334
335 //No Xdata definition was defined or failed to import. Use default filename and create a OneSidedXData-object
336 if (_entity->getKeyValue("name").find("book") == std::string::npos)
337 {
338 if (!_xdNameSpecified)
339 _xData.reset(new XData::OneSidedXData(defaultXdName));
340 else
341 _xData.reset(new XData::OneSidedXData(_entity->getKeyValue("xdata_contents")));
342 }
343 else
344 {
345 if (!_xdNameSpecified)
346 _xData.reset(new XData::TwoSidedXData(defaultXdName));
347 else
348 _xData.reset(new XData::OneSidedXData(_entity->getKeyValue("xdata_contents")));
349 }
350 _xData->setNumPages(1);
351
352 refreshWindowTitle();
353
354 return true;
355 }
356
357 bool ReadableEditorDialog::save()
358 {
359 _saveInProgress = true;
360
361 UndoableCommand cmd("editReadable");
362
363 // Name
364 _entity->setKeyValue("inv_name", _nameEntry->GetValue().ToStdString());
365
366 // Xdata contents
367 _entity->setKeyValue("xdata_contents", _xDataNameEntry->GetValue().ToStdString());
368
369 // Current content to XData Object
370 storeXData();
371
372 // Get the storage path and check its validity
373 std::string storagePath = constructStoragePath();
374
375 if (!_useDefaultFilename && !os::fileOrDirExists(storagePath))
376 {
377 // The file does not exist, so we have imported a definition contained inside a PK4.
378 wxutil::Messagebox::ShowError(
379 _("You have imported an XData definition that is contained in a PK4, which can't be accessed for saving.") +
380 std::string("\n\n") +
381 _("Please rename your XData definition, so that it is stored under a different filename."),
382 this
383 );
384
385 _saveInProgress = false;
386 return false;
387 }
388
389 // Start exporting
390 XData::FileStatus fst = _xData->xport(storagePath, XData::Merge);
391
392 if (fst == XData::DefinitionExists)
393 {
394 switch (_xData->xport( storagePath, XData::MergeOverwriteExisting))
395 {
396 case XData::OpenFailed:
397 wxutil::Messagebox::ShowError(
398 fmt::format(_("Failed to open {0} for saving."), _xdFilename),
399 this
400 );
401 _saveInProgress = false;
402 return false;
403 case XData::MergeFailed:
404 wxutil::Messagebox::ShowError(
405 _("Merging failed, because the length of the definition to be overwritten could not be retrieved."),
406 this
407 );
408 _saveInProgress = false;
409 return false;
410 default:
411 //success!
412 _saveInProgress = false;
413 return true;
414 }
415 }
416 else if (fst == XData::OpenFailed)
417 {
418 wxutil::Messagebox::ShowError(
419 fmt::format(_("Failed to open {0} for saving."), _xdFilename),
420 this
421 );
422 }
423
424 _saveInProgress = false;
425 return false;
426 }
427
428 std::string ReadableEditorDialog::constructStoragePath()
429 {
430 // Construct the storage path from Registry keys.
431 std::string storagePath;
432 if (_useDefaultFilename)
433 {
434 bool checkVFS = true;
435 switch (registry::getValue<int>(RKEY_READABLES_STORAGE_FOLDER))
436 {
437 case 0: // Use Mod dir
438 storagePath = GlobalGameManager().getModPath();
439 if (storagePath.empty())
440 {
441 // Mod path not defined. Use base Path
442 storagePath = GlobalRegistry().get(RKEY_ENGINE_PATH) + "base/";
443 wxutil::Messagebox::ShowError(_("Mod path not defined. Using Base path..."), this);
444 }
445 storagePath += XData::XDATA_DIR;
446 break;
447 case 1: // Use Mod Base dir
448 storagePath = GlobalGameManager().getModBasePath();
449 if (storagePath.empty())
450 {
451 // Mod Base Path not defined. Use Mod path or base path successively.
452 storagePath = GlobalGameManager().getModPath();
453 if (storagePath.empty())
454 {
455 storagePath = GlobalRegistry().get(RKEY_ENGINE_PATH) + "base/";
456 wxutil::Messagebox::ShowError(_("Mod Base path not defined, neither is Mod path. Using Engine path..."),
457 this);
458 storagePath += XData::XDATA_DIR;
459 break;
460 }
461 wxutil::Messagebox::ShowError(_("Mod Base path not defined. Using Mod path..."), this);
462 }
463 storagePath += XData::XDATA_DIR;
464 break;
465 default: // Use custom folder
466 storagePath = GlobalRegistry().get(RKEY_READABLES_CUSTOM_FOLDER);
467 if (storagePath.empty())
468 {
469 // Custom path not defined. Use Mod path or base path successively.
470 storagePath = GlobalGameManager().getModPath();
471 if (storagePath.empty())
472 {
473 storagePath = GlobalRegistry().get(RKEY_ENGINE_PATH) + "base/";
474 wxutil::Messagebox::ShowError(_("Mod Base path not defined, neither is Mod path. Using Engine path..."), this);
475 storagePath += XData::XDATA_DIR;
476 break;
477 }
478 storagePath += XData::XDATA_DIR;
479 wxutil::Messagebox::ShowError(_("Mod Base path not defined. Using Mod path..."), this);
480 break;
481 }
482 storagePath += "/" +_mapBasedFilename;
483 checkVFS = false;
484 break;
485 }
486
487 // Check whether this path already exists in the VFS in another absolute folder. If so, rename it!
488 if (checkVFS)
489 {
490 std::string compPath = GlobalFileSystem().findFile(XData::XDATA_DIR + _mapBasedFilename);
491 std::string newFileName = _mapBasedFilename;
492
493 if (!compPath.empty()) // VFS-path does exist, now check whether it is in another absolute folder.
494 {
495 int fileIdx = 2;
496 compPath += XData::XDATA_DIR + _mapBasedFilename;
497
498 while ( (compPath.compare(storagePath + newFileName) != 0) )
499 {
500 newFileName = _mapBasedFilename.substr(0, _mapBasedFilename.rfind(".")) + string::to_string(fileIdx++) + ".xd";
501 compPath = GlobalFileSystem().findFile(XData::XDATA_DIR + newFileName);
502 if (compPath.empty())
503 {
504 wxutil::Messagebox::ShowError(
505 fmt::format(_("{0}{1} already exists in another path.\n\nXData will be stored in {2}{3}!"),
506 XData::XDATA_DIR,
507 _mapBasedFilename,
508 XData::XDATA_DIR,
509 newFileName),
510 this
511 );
512 break;
513 }
514 compPath += XData::XDATA_DIR + newFileName;
515 }
516 }
517 storagePath += newFileName;
518 }
519 }
520 else
521 {
522 // We are exporting a previously imported XData definition.
523 // The _xdFilename itself can be a VFS path or an absolute path, depending on the user's settings
524 if (path_is_absolute(_xdFilename.c_str()))
525 {
526 storagePath = _xdFilename;
527 }
528 else
529 {
530 // Retrieve engine path and append _xdFilename
531 storagePath = GlobalRegistry().get(RKEY_ENGINE_PATH) + _xdFilename;
532 }
533 }
534
535 return storagePath;
536 }
537
538 void ReadableEditorDialog::refreshWindowTitle()
539 {
540 std::string title = constructStoragePath();
541 title = title.substr( title.find_first_not_of( GlobalRegistry().get(RKEY_ENGINE_PATH) ) );
542 title = std::string(_("Readable Editor")) + " - " + title;
543
544 SetTitle(title);
545 }
546
547 void ReadableEditorDialog::storeXData()
548 {
549 //NumPages does not need storing, because it's stored directly after changing it.
550 _xData->setName(_xDataNameEntry->GetValue().ToStdString());
551 _xData->setSndPageTurn(_pageTurnEntry->GetValue().ToStdString());
552
553 storeCurrentPage();
554 }
555
556 void ReadableEditorDialog::toggleTwoSidedEditingInterface(bool show)
557 {
558 if (show)
559 {
560 _textViewRightTitle->GetContainingSizer()->Show(_textViewRightTitle);
561 _textViewRightBody->GetContainingSizer()->Show(_textViewRightBody);
562 _pageLeftLabel->GetContainingSizer()->Show(_pageLeftLabel);
563 _pageRightLabel->GetContainingSizer()->Show(_pageRightLabel);
564 }
565 else
566 {
567 _textViewRightTitle->GetContainingSizer()->Hide(_textViewRightTitle);
568 _textViewRightBody->GetContainingSizer()->Hide(_textViewRightBody);
569 _pageLeftLabel->GetContainingSizer()->Hide(_pageLeftLabel);
570 _pageRightLabel->GetContainingSizer()->Hide(_pageRightLabel);
571 }
572
573 _textViewRightTitle->GetContainingSizer()->Layout();
574 }
575
576 void ReadableEditorDialog::showPage(std::size_t pageIndex)
577 {
578 //Gui Def before:
579 std::string guiBefore = _guiEntry->GetValue().ToStdString();
580
581 // Update CurrentPage Label
582 _currentPageIndex = pageIndex;
583 _curPageDisplay->SetLabel(string::to_string(pageIndex+1));
584
585 if (_xData->getPageLayout() == XData::TwoSided)
586 {
587 // Update Gui statement entry from xData
588 if (!_xData->getGuiPage(pageIndex).empty())
589 {
590 _guiEntry->SetValue(_xData->getGuiPage(pageIndex));
591 }
592 else
593 {
594 _guiEntry->SetValue(XData::DEFAULT_TWOSIDED_GUI);
595 }
596
597 setTextViewAndScroll(_textViewRightTitle, _xData->getPageContent(XData::Title, pageIndex, XData::Right));
598 setTextViewAndScroll(_textViewRightBody, _xData->getPageContent(XData::Body, pageIndex, XData::Right));
599 }
600 else
601 {
602 // Update Gui statement entry from xData
603 if (!_xData->getGuiPage(pageIndex).empty())
604 {
605 _guiEntry->SetValue(_xData->getGuiPage(pageIndex));
606 }
607 else
608 {
609 _guiEntry->SetValue(XData::DEFAULT_ONESIDED_GUI);
610 }
611 }
612
613 // Update page statements textviews from xData
614 setTextViewAndScroll(_textViewTitle, _xData->getPageContent(XData::Title, pageIndex, XData::Left));
615 setTextViewAndScroll(_textViewBody, _xData->getPageContent(XData::Body, pageIndex, XData::Left));
616
617 // Update the GUI View if the gui changed. For the page contents, updateGuiView is called automatically
618 // by onTextChange.
619 if (guiBefore != _guiEntry->GetValue().ToStdString())
620 {
621 updateGuiView();
622 }
623 }
624
625 void ReadableEditorDialog::updateGuiView(wxWindow* parent,
626 const std::string& guiPath,
627 const std::string& xDataName,
628 const std::string& xDataPath)
629 {
630 // If the name of an xData object is passed it will be rendered instead of the current
631 // xData object, to enable previewing of XData definitions induced by the XDataSelector.
632 if (!xDataName.empty())
633 {
634 XData::XDataPtr xd;
635 XData::XDataMap xdMap;
636
637 if (_xdLoader->importDef(xDataName, xdMap, xDataPath))
638 {
639 xd = xdMap.begin()->second;
640 _guiView->setGui(GlobalGuiManager().getGui(xd->getGuiPage(0)));
641 }
642 else
643 {
644 std::string msg = fmt::format(_("Failed to import {0}."), xDataName);
645 msg += "\n\n";
646 msg += _("Do you want to open the import summary?");
647
648 wxutil::Messagebox dialog(_("Import failed"),
649 msg, ui::IDialog::MESSAGE_ASK, parent ? parent : this
650 );
651
652 if (dialog.run() == ui::IDialog::RESULT_YES)
653 {
654 showXdImportSummary();
655 }
656 return;
657 }
658
659 const gui::IGuiPtr& gui = _guiView->getGui();
660
661 if (gui == NULL)
662 {
663 std::string msg = fmt::format(_("Failed to load gui definition {0}."), xd->getGuiPage(0));
664 msg += "\n\n";
665 msg += _("Do you want to open the import summary?");
666
667 wxutil::Messagebox dialog(_("Import failed"),
668 msg, ui::IDialog::MESSAGE_ASK, parent ? parent : this
669 );
670
671 if (dialog.run() == ui::IDialog::RESULT_YES)
672 {
673 showGuiImportSummary();
674 }
675 return;
676 }
677
678 // Load data from xdata into the GUI's state variables
679 if (xd->getPageLayout() == XData::OneSided)
680 {
681 // One-sided has title and body
682 gui->setStateString("title", xd->getPageContent(XData::Title, 0, XData::Left));
683 gui->setStateString("body", xd->getPageContent(XData::Body, 0, XData::Left));
684 }
685 else
686 {
687 // Two-sided has four important state strings
688 gui->setStateString("left_title", xd->getPageContent(XData::Title, 0, XData::Left));
689 gui->setStateString("left_body", xd->getPageContent(XData::Body, 0, XData::Left));
690
691 gui->setStateString("right_title", xd->getPageContent(XData::Title, 0, XData::Right));
692 gui->setStateString("right_body", xd->getPageContent(XData::Body, 0, XData::Right));
693 }
694
695 initGuiState(gui);
696 }
697 else
698 {
699 if (guiPath.empty())
700 {
701 _guiView->setGui(GlobalGuiManager().getGui(_guiEntry->GetValue().ToStdString()));
702 }
703 else
704 {
705 _guiView->setGui(GlobalGuiManager().getGui(guiPath));
706 }
707
708 const gui::IGuiPtr& gui = _guiView->getGui();
709
710 if (gui == NULL)
711 {
712 std::string nameGui = guiPath.empty() ? _guiEntry->GetValue().ToStdString() : guiPath;
713
714 std::string msg = fmt::format(_("Failed to load gui definition {0}."), nameGui);
715 msg += "\n\n";
716 msg += _("Do you want to open the import summary?");
717
718 wxutil::Messagebox dialog(_("Import failed"),
719 msg, ui::IDialog::MESSAGE_ASK, parent ? parent : this
720 );
721 if (dialog.run() == ui::IDialog::RESULT_YES)
722 {
723 showGuiImportSummary();
724 }
725 return;
726 }
727
728 // Load data from xdata into the GUI's state variables
729 if (_xData->getPageLayout() == XData::OneSided)
730 {
731 // One-sided has title and body
732 gui->setStateString("title", _textViewTitle->GetValue().ToStdString());
733 gui->setStateString("body", _textViewBody->GetValue().ToStdString());
734 }
735 else
736 {
737 // Two-sided has four important state strings
738 gui->setStateString("left_title", _textViewTitle->GetValue().ToStdString());
739 gui->setStateString("left_body", _textViewBody->GetValue().ToStdString());
740
741 gui->setStateString("right_title", _textViewRightTitle->GetValue().ToStdString());
742 gui->setStateString("right_body", _textViewRightBody->GetValue().ToStdString());
743 }
744
745 // Set common GUI state for rendering
746 initGuiState(gui);
747 }
748
749 _guiView->redraw();
750 }
751
752 void ReadableEditorDialog::initGuiState(const gui::IGuiPtr& gui)
753 {
754 assert(gui);
755
756 // Some fancy readables have custom backgrounds depending on the page number
757 // This is usually done by game script at runtime, so let's fake something similar
758 gui->setStateString("curPage", string::to_string(_currentPageIndex + 1));
759 gui->setStateString("numPages", string::to_string(_numPages->GetValue()));
760
761 // ContentsFadeIn is reponsible of setting custom backgrounds
762 if (gui->findWindowDef("ContentsFadeIn"))
763 {
764 gui->findWindowDef("ContentsFadeIn")->notime.setValue(false);
765 }
766
767 // Initialise the time of this GUI
768 gui->initTime(0);
769
770 // Run the first frame
771 gui->update(16);
772 }
773
774 void ReadableEditorDialog::storeCurrentPage()
775 {
776 // Store the GUI-Page
777 _xData->setGuiPage(_guiEntry->GetValue().ToStdString(), _currentPageIndex);
778
779 // On OneSidedXData the Side-enum is discarded, so it's save to call this method
780 _xData->setPageContent(XData::Title, _currentPageIndex, XData::Left, _textViewTitle->GetValue().ToStdString());
781 _xData->setPageContent(XData::Body, _currentPageIndex, XData::Left, _textViewBody->GetValue().ToStdString());
782
783 if (_xData->getPageLayout() == XData::TwoSided)
784 {
785 _xData->setPageContent(XData::Title, _currentPageIndex, XData::Right, _textViewRightTitle->GetValue().ToStdString());
786 _xData->setPageContent(XData::Body, _currentPageIndex, XData::Right, _textViewRightBody->GetValue().ToStdString());
787 }
788 }
789
790 void ReadableEditorDialog::populateControlsFromXData()
791 {
792 toggleTwoSidedEditingInterface(_xData->getPageLayout() == XData::TwoSided);
793 showPage(0);
794
795 _xDataNameEntry->SetValue(_xData->getName());
796 _numPages->SetValue(static_cast<int>(_xData->getNumPages()));
797
798 std::string sndString = _xData->getSndPageTurn();
799
800 _pageTurnEntry->SetValue(
801 sndString.empty() ? XData::DEFAULT_SNDPAGETURN : sndString
802 );
803
804 if (_xData->getPageLayout() == XData::TwoSided)
805 {
806 _twoSidedButton->SetValue(true);
807 }
808 else
809 {
810 _oneSidedButton->SetValue(true);
811 }
812 }
813
814 void ReadableEditorDialog::checkXDataUniqueness()
815 {
816 _runningXDataUniquenessCheck = true;
817
818 std::string xdn = _xDataNameEntry->GetValue().ToStdString();
819
820 if (_xData->getName() == xdn)
821 {
822 _runningXDataUniquenessCheck = false;
823 return;
824 }
825
826 _xdLoader->retrieveXdInfo();
827
828 XData::StringVectorMap::const_iterator it = _xdLoader->getDefinitionList().find(xdn);
829
830 if (it != _xdLoader->getDefinitionList().end())
831 {
832 // The definition already exists. Ask the user whether it should be imported. If not make a different name suggestion.
833 wxutil::Messagebox dialog(
834 _("Import definition?"),
835 fmt::format(_("The definition {0} already exists. Should it be imported?"), xdn),
836 ui::IDialog::MESSAGE_ASK, this
837 );
838
839 std::string message = "";
840
841 if (dialog.run() == ui::IDialog::RESULT_YES)
842 {
843 try
844 {
845 int result = XdFileChooserDialog::Import( xdn, _xData, _xdFilename, _xdLoader, this);
846
847 if (result == wxID_OK)
848 {
849 _xdNameSpecified = true;
850 _useDefaultFilename = false;
851 populateControlsFromXData();
852 _runningXDataUniquenessCheck = false;
853 refreshWindowTitle();
854 return;
855 }
856 }
857 catch (XdFileChooserDialog::ImportFailedException&)
858 {
859 message = _("Import failed:");
860 message += "\n\t" + _xdLoader->getImportSummary()[_xdLoader->getImportSummary().size() - 1];
861 message += "\n\n";
862 message += _("Consult the import summary for further information.");
863 message += "\n\n";
864 }
865 }
866
867 // Dialog RESULT_NO, XdFileChooserDialog::RESULT_CANCEL or import failed! Make a different name suggestion!
868 std::string suggestion;
869
870 for (int n = 1; n > 0; n++)
871 {
872 suggestion = xdn + string::to_string(n);
873
874 if (_xdLoader->getDefinitionList().find(suggestion) == _xdLoader->getDefinitionList().end())
875 {
876 // The suggested XData-name does not exist.
877 break;
878 }
879 }
880
881 // Update entry and XData object. Notify the user about the suggestion.
882 _xDataNameEntry->SetValue(suggestion);
883 _xData->setName(suggestion);
884
885 message += fmt::format(_("To avoid duplicated XData definitions "
886 "the current definition has been renamed to {0}."), suggestion);
887
888 wxutil::Messagebox::Show(
889 _("XData has been renamed."),
890 message, ui::IDialog::MESSAGE_CONFIRM, this
891 );
892 }
893 else
894 {
895 _xData->setName(xdn);
896 }
897
898 _xdNameSpecified = true;
899 _useDefaultFilename = true;
900 _runningXDataUniquenessCheck = false;
901 refreshWindowTitle();
902 }
903
904 void ReadableEditorDialog::insertPage()
905 {
906 storeCurrentPage();
907 _xData->setNumPages(_xData->getNumPages()+1);
908
909 _numPages->SetValue(static_cast<int>(_xData->getNumPages()));
910 handleNumberOfPagesChanged();
911
912 for (std::size_t n = _xData->getNumPages() - 1; n > _currentPageIndex; n--)
913 {
914 _xData->setGuiPage(_xData->getGuiPage(n - 1), n);
915
916 _xData->setPageContent(XData::Title, n, XData::Left,
917 _xData->getPageContent(XData::Title, n - 1, XData::Left)
918 );
919
920 _xData->setPageContent(XData::Body, n, XData::Left,
921 _xData->getPageContent(XData::Body, n - 1, XData::Left)
922 );
923 }
924
925 // New Page:
926 _xData->setPageContent(XData::Title, _currentPageIndex, XData::Left, "");
927 _xData->setPageContent(XData::Body, _currentPageIndex, XData::Left, "");
928 _xData->setGuiPage(_xData->getGuiPage(_currentPageIndex + 1), _currentPageIndex);
929
930 // Right side:
931 if (_xData->getPageLayout() == XData::TwoSided)
932 {
933 for (std::size_t n = _xData->getNumPages() - 1; n > _currentPageIndex; n--)
934 {
935 _xData->setGuiPage(_xData->getGuiPage(n - 1), n);
936
937 _xData->setPageContent(XData::Title, n, XData::Right,
938 _xData->getPageContent(XData::Title, n - 1, XData::Right)
939 );
940
941 _xData->setPageContent(XData::Body, n, XData::Right,
942 _xData->getPageContent(XData::Body, n - 1, XData::Right)
943 );
944 }
945
946 // New Page:
947 _xData->setPageContent(XData::Title, _currentPageIndex, XData::Right, "");
948 _xData->setPageContent(XData::Body, _currentPageIndex, XData::Right, "");
949 }
950
951 showPage(_currentPageIndex);
952 }
953
954 void ReadableEditorDialog::deletePage()
955 {
956 if (_currentPageIndex == _xData->getNumPages() - 1)
957 {
958 if (_currentPageIndex == 0)
959 {
960 _xData->setNumPages(0);
961 _xData->setNumPages(1);
962 showPage(0);
963 return;
964 }
965
966 _numPages->SetValue(static_cast<int>(_currentPageIndex));
967 handleNumberOfPagesChanged();
968 }
969 else
970 {
971 for (std::size_t n = _currentPageIndex; n < _xData->getNumPages()-1; n++)
972 {
973 _xData->setGuiPage(_xData->getGuiPage(n+1), n);
974
975 _xData->setPageContent(XData::Title, n, XData::Left,
976 _xData->getPageContent(XData::Title, n+1, XData::Left)
977 );
978
979 _xData->setPageContent(XData::Body, n, XData::Left,
980 _xData->getPageContent(XData::Body, n+1, XData::Left)
981 );
982 }
983
984 // Right Side
985 if (_xData->getPageLayout() == XData::TwoSided)
986 {
987 for (std::size_t n = _currentPageIndex; n < _xData->getNumPages()-1; n++)
988 {
989 _xData->setGuiPage(_xData->getGuiPage(n+1), n);
990
991 _xData->setPageContent(XData::Title, n, XData::Right,
992 _xData->getPageContent(XData::Title, n+1, XData::Right)
993 );
994
995 _xData->setPageContent(XData::Body, n, XData::Right,
996 _xData->getPageContent(XData::Body, n+1, XData::Right)
997 );
998 }
999 }
1000
1001 _xData->setNumPages(_xData->getNumPages() - 1);
1002
1003 _numPages->SetValue(static_cast<int>(_xData->getNumPages()));
1004
1005 showPage(_currentPageIndex);
1006 }
1007 }
1008
1009
1010 void ReadableEditorDialog::deleteSide(bool rightSide)
1011 {
1012 storeCurrentPage();
1013
1014 if (!rightSide)
1015 {
1016 _xData->setPageContent(XData::Title, _currentPageIndex, XData::Left,
1017 _xData->getPageContent(XData::Title, _currentPageIndex, XData::Right)
1018 );
1019
1020 _xData->setPageContent(XData::Body, _currentPageIndex, XData::Left,
1021 _xData->getPageContent(XData::Body, _currentPageIndex, XData::Right)
1022 );
1023 }
1024
1025 if (_currentPageIndex < _xData->getNumPages() - 1)
1026 {
1027 _xData->setPageContent(XData::Title, _currentPageIndex, XData::Right,
1028 _xData->getPageContent(XData::Title, _currentPageIndex + 1, XData::Left)
1029 );
1030
1031 _xData->setPageContent(XData::Body, _currentPageIndex, XData::Right,
1032 _xData->getPageContent(XData::Body, _currentPageIndex + 1, XData::Left)
1033 );
1034
1035 for (std::size_t n = _currentPageIndex + 1; n < _xData->getNumPages() - 1; n++)
1036 {
1037 _xData->setPageContent(XData::Title, n, XData::Left,
1038 _xData->getPageContent(XData::Title, n, XData::Right));
1039
1040 _xData->setPageContent(XData::Title, n, XData::Right,
1041 _xData->getPageContent(XData::Title, n+1, XData::Left));
1042
1043 _xData->setPageContent(XData::Body, n, XData::Left,
1044 _xData->getPageContent(XData::Body, n, XData::Right));
1045
1046 _xData->setPageContent(XData::Body, n, XData::Right,
1047 _xData->getPageContent(XData::Body, n+1, XData::Left));
1048 }
1049
1050 _xData->setPageContent(XData::Title, _xData->getNumPages() - 1, XData::Left,
1051 _xData->getPageContent(XData::Title, _xData->getNumPages() - 1, XData::Right));
1052
1053 _xData->setPageContent(XData::Body, _xData->getNumPages() - 1, XData::Left,
1054 _xData->getPageContent(XData::Body, _xData->getNumPages() - 1, XData::Right));
1055 }
1056
1057 if (_xData->getPageContent(XData::Title, _xData->getNumPages() - 1, XData::Left).empty() &&
1058 _xData->getPageContent(XData::Body, _xData->getNumPages() - 1, XData::Left).empty())
1059 {
1060 // The last page has no content anymore, so delete it.
1061 _numPages->SetValue(static_cast<int>(_xData->getNumPages() - 1));
1062 handleNumberOfPagesChanged();
1063 }
1064 else
1065 {
1066 _xData->setPageContent(XData::Title, _xData->getNumPages() - 1, XData::Right, "");
1067 _xData->setPageContent(XData::Body, _xData->getNumPages() - 1, XData::Right, "");
1068 }
1069
1070 showPage(_currentPageIndex);
1071 }
1072
1073 void ReadableEditorDialog::insertSide(bool rightSide)
1074 {
1075 storeCurrentPage();
1076
1077 if (!_xData->getPageContent(XData::Title, _xData->getNumPages()-1, XData::Right).empty() ||
1078 !_xData->getPageContent(XData::Body, _xData->getNumPages()-1, XData::Right).empty())
1079 {
1080 //Last side has content. Raise numPages before shifting
1081 _numPages->SetValue(static_cast<int>(_xData->getNumPages() + 1));
1082 handleNumberOfPagesChanged();
1083 }
1084
1085 for (std::size_t n = _xData->getNumPages() - 1; n>_currentPageIndex; n--)
1086 {
1087 _xData->setPageContent(XData::Title, n, XData::Right,
1088 _xData->getPageContent(XData::Title, n, XData::Left));
1089
1090 _xData->setPageContent(XData::Title, n, XData::Left,
1091 _xData->getPageContent(XData::Title, n-1, XData::Right));
1092
1093 _xData->setPageContent(XData::Body, n, XData::Right,
1094 _xData->getPageContent(XData::Body, n, XData::Left));
1095
1096 _xData->setPageContent(XData::Body, n, XData::Left,
1097 _xData->getPageContent(XData::Body, n-1, XData::Right));
1098 }
1099
1100 if (!rightSide)
1101 {
1102 _xData->setPageContent(XData::Title, _currentPageIndex, XData::Right,
1103 _xData->getPageContent(XData::Title, _currentPageIndex, XData::Left));
1104
1105 _xData->setPageContent(XData::Body, _currentPageIndex, XData::Right,
1106 _xData->getPageContent(XData::Body, _currentPageIndex, XData::Left));
1107
1108 _xData->setPageContent(XData::Title, _currentPageIndex, XData::Left, "");
1109 _xData->setPageContent(XData::Body, _currentPageIndex, XData::Left, "");
1110 }
1111 else
1112 {
1113 _xData->setPageContent(XData::Title, _currentPageIndex, XData::Right, "");
1114 _xData->setPageContent(XData::Body, _currentPageIndex, XData::Right, "");
1115 }
1116
1117 showPage(_currentPageIndex);
1118 }
1119
1120 void ReadableEditorDialog::useOneSidedEditing()
1121 {
1122 if (_xData->getPageLayout() != XData::OneSided)
1123 {
1124 toggleLayout();
1125 }
1126 }
1127
1128 void ReadableEditorDialog::useTwoSidedEditing()
1129 {
1130 if (_xData->getPageLayout() != XData::TwoSided)
1131 {
1132 toggleLayout();
1133 }
1134 }
1135
1136 void ReadableEditorDialog::toggleLayout()
1137 {
1138 // Convert OneSided XData to TwoSided and vice versa and refresh controls
1139 storeXData();
1140 _xData->togglePageLayout(_xData);
1141 populateControlsFromXData();
1142 }
1143
1144 void ReadableEditorDialog::checkGuiLayout()
1145 {
1146 _runningGuiLayoutCheck = true;
1147
1148 std::string guiName = _guiEntry->GetValue().ToStdString();
1149
1150 std::string msg;
1151
1152 gui::GuiType type = GlobalGuiManager().getGuiType(guiName);
1153
1154 switch (type)
1155 {
1156 case gui::NO_READABLE:
1157 msg = _("The specified gui definition is not a readable.");
1158 break;
1159 case gui::ONE_SIDED_READABLE:
1160 if (_xData->getPageLayout() != XData::OneSided)
1161 {
1162 msg = _("The specified gui definition is not suitable for the currently chosen page-layout.");
1163 }
1164 else
1165 {
1166 _runningGuiLayoutCheck = false;
1167 updateGuiView();
1168 return;
1169 }
1170 break;
1171 case gui::TWO_SIDED_READABLE:
1172 if (_xData->getPageLayout() != XData::TwoSided)
1173 {
1174 msg = _("The specified gui definition is not suitable for the currently chosen page-layout.");
1175 }
1176 else
1177 {
1178 _runningGuiLayoutCheck = false;
1179 updateGuiView();
1180 return;
1181 }
1182 break;
1183 case gui::IMPORT_FAILURE:
1184 msg = _("Failure during import.");
1185 break;
1186 case gui::FILE_NOT_FOUND:
1187 msg = _("The specified Definition does not exist.");
1188 break;
1189 default:
1190 rWarning() << "Invalid GUI type encountered in switch: " << type << std::endl;
1191 break;
1192 }
1193
1194 wxutil::Messagebox dialog(
1195 _("Not a suitable Gui Definition!"),
1196 msg + "\n\n" + std::string(_("Start the Gui Browser?")),
1197 IDialog::MESSAGE_ASK, this);
1198
1199 if (dialog.run() == ui::IDialog::RESULT_YES)
1200 {
1201 XData::PageLayout layoutBefore = _xData->getPageLayout();
1202 std::string selectedGuiName = GuiSelector::Run(_xData->getPageLayout() == XData::TwoSided, this);
1203
1204 if (!selectedGuiName.empty())
1205 {
1206 _guiEntry->SetValue(selectedGuiName);
1207 _runningGuiLayoutCheck = false;
1208 updateGuiView();
1209 return;
1210 }
1211 else
1212 {
1213 if (_xData->getPageLayout() != layoutBefore)
1214 {
1215 toggleLayout();
1216 }
1217
1218 // User clicked cancel. Use default layout:
1219 if (_xData->getPageLayout() == XData::TwoSided)
1220 {
1221 _guiEntry->SetValue(XData::DEFAULT_TWOSIDED_GUI);
1222 }
1223 else
1224 {
1225 _guiEntry->SetValue(XData::DEFAULT_ONESIDED_GUI);
1226 }
1227
1228 updateGuiView();
1229
1230 wxutil::Messagebox::Show(_("Switching to default Gui..."),
1231 _("You didn't choose a Gui. Using the default Gui now."),
1232 ui::IDialog::MESSAGE_CONFIRM, this);
1233 _runningGuiLayoutCheck = false;
1234 return;
1235 }
1236 }
1237 // Entry should hold focus.
1238 _guiEntry->SetFocus();
1239 _runningGuiLayoutCheck = false;
1240 }
1241
1242 void ReadableEditorDialog::showXdImportSummary()
1243 {
1244 XData::StringList summary = _xdLoader->getImportSummary();
1245
1246 if (summary.empty())
1247 {
1248 wxutil::Messagebox::ShowError(_("No import summary available. An XData definition has to be imported first..."),
1249 this);
1250 return;
1251 }
1252
1253 std::string sum;
1254
1255 for (std::size_t n = 0; n < summary.size(); n++)
1256 {
1257 sum += summary[n];
1258 }
1259
1260 TextViewInfoDialog::Show(_("XData import summary"), sum, this);
1261 }
1262
1263 void ReadableEditorDialog::showGuiImportSummary()
1264 {
1265 XData::StringList errors = GlobalGuiManager().getErrorList();
1266 if (errors.empty())
1267 {
1268 wxutil::Messagebox::ShowError(_("No import summary available. Browse Gui Definitions first."), this);
1269 return;
1270 }
1271
1272 std::string summary;
1273
1274 for (std::size_t n = 0; n < errors.size(); n++)
1275 {
1276 summary += errors[n];
1277 }
1278
1279 TextViewInfoDialog::Show(_("Gui import summary"), summary, this);
1280 }
1281
1282 //////////////////////////////////////////////////////////////////////////////
1283 // Callback Methods for Signals:
1284
1285 void ReadableEditorDialog::onMenuItemClick(wxCommandEvent& ev)
1286 {
1287 switch (static_cast<MenuItemId>(ev.GetId()))
1288 {
1289 case InsertWholePage:
1290 insertPage();
1291 break;
1292 case InsertLeft:
1293 insertSide(false);
1294 break;
1295 case InsertRight:
1296 insertSide(true);
1297 break;
1298
1299 case DeleteWholePage:
1300 deletePage();
1301 break;
1302 case DeleteLeft:
1303 deleteSide(false);
1304 break;
1305 case DeleteRight:
1306 deleteSide(true);
1307 break;
1308
1309 case AppendPage:
1310 _numPages->SetValue(static_cast<int>(_xData->getNumPages() + 1));
1311 handleNumberOfPagesChanged();
1312 storeCurrentPage();
1313 showPage(_currentPageIndex + 1);
1314 break;
1315
1316 case PrependPage:
1317 insertPage();
1318 break;
1319
1320 case ShowXDataSummary:
1321 showXdImportSummary();
1322 break;
1323 case ShowGuiImportSummary:
1324 showGuiImportSummary();
1325 break;
1326 case ShowDuplicatedDefs:
1327 showDuplicateDefinitions();
1328 break;
1329 };
1330 }
1331
1332 void ReadableEditorDialog::onCancel(wxCommandEvent& ev)
1333 {
1334 EndModal(wxID_CANCEL);
1335 }
1336
1337 void ReadableEditorDialog::onSave(wxCommandEvent& ev)
1338 {
1339 if (_xdNameSpecified)
1340 {
1341 save();
1342 }
1343 else
1344 {
1345 wxutil::Messagebox::ShowError(_("Please specify an XData name first!"), this);
1346 }
1347 }
1348
1349 void ReadableEditorDialog::onSaveClose(wxCommandEvent& ev)
1350 {
1351 if (!_saveInProgress)
1352 {
1353 if (_xdNameSpecified)
1354 {
1355 if (save())
1356 {
1357 // Done, just destroy the window
1358 EndModal(wxID_OK);
1359 }
1360 }
1361 else
1362 {
1363 wxutil::Messagebox::ShowError(_("Please specify an XData name first!"), this);
1364 }
1365 }
1366 }
1367
1368 void ReadableEditorDialog::onBrowseXd(wxCommandEvent& ev)
1369 {
1370 // FileChooser for xd-files
1371 _xdLoader->retrieveXdInfo();
1372
1373 std::string res = XDataSelector::run(_xdLoader->getDefinitionList(), this);
1374
1375 if (res.empty())
1376 {
1377 updateGuiView();
1378 return;
1379 }
1380
1381 // Import the file:
1382 try
1383 {
1384 if (XdFileChooserDialog::Import(res, _xData, _xdFilename, _xdLoader, this) == wxID_OK)
1385 {
1386 _xdNameSpecified = true;
1387 _useDefaultFilename = false;
1388 populateControlsFromXData();
1389 refreshWindowTitle();
1390 }
1391 else
1392 {
1393 updateGuiView();
1394 }
1395 }
1396 catch (XdFileChooserDialog::ImportFailedException&)
1397 {
1398 std::string msg = fmt::format(_("Failed to import {0}."), res);
1399 msg += "\n\n";
1400 msg += _("Do you want to open the import summary?");
1401
1402 wxutil::Messagebox dialog(_("Import failed"),
1403 msg,
1404 ui::IDialog::MESSAGE_ASK, this);
1405
1406 if (dialog.run() == ui::IDialog::RESULT_YES)
1407 {
1408 showXdImportSummary();
1409 }
1410 updateGuiView();
1411 return;
1412 }
1413 }
1414
1415 void ReadableEditorDialog::onNextPage(wxCommandEvent& ev)
1416 {
1417 if (_currentPageIndex+1 < _xData->getNumPages())
1418 {
1419 storeCurrentPage();
1420 showPage(_currentPageIndex+1);
1421 }
1422 else
1423 {
1424 static_cast<wxWindow*>(ev.GetEventObject())->PopupMenu(_appendMenu.get());
1425 }
1426 }
1427
1428 void ReadableEditorDialog::onPrevPage(wxCommandEvent& ev)
1429 {
1430 if (_currentPageIndex > 0)
1431 {
1432 storeCurrentPage();
1433 showPage(_currentPageIndex-1);
1434 }
1435 else
1436 {
1437 static_cast<wxWindow*>(ev.GetEventObject())->PopupMenu(_prependMenu.get());
1438 }
1439 }
1440
1441 void ReadableEditorDialog::onFirstPage(wxCommandEvent& ev)
1442 {
1443 if (_currentPageIndex != 0)
1444 {
1445 storeCurrentPage();
1446 showPage(0);
1447 }
1448 else
1449 {
1450 static_cast<wxWindow*>(ev.GetEventObject())->PopupMenu(_prependMenu.get());
1451 }
1452 }
1453
1454 void ReadableEditorDialog::onLastPage(wxCommandEvent& ev)
1455 {
1456 if (_currentPageIndex != _xData->getNumPages() - 1)
1457 {
1458 storeCurrentPage();
1459 showPage(_xData->getNumPages()-1);
1460 }
1461 else
1462 {
1463 static_cast<wxWindow*>(ev.GetEventObject())->PopupMenu(_appendMenu.get());
1464 }
1465 }
1466
1467 void ReadableEditorDialog::onBrowseGui(wxCommandEvent& ev)
1468 {
1469 XData::PageLayout layoutBefore = _xData->getPageLayout();
1470 std::string guiDefBefore = _guiEntry->GetValue().ToStdString();
1471 std::string guiName = GuiSelector::Run(_xData->getPageLayout() == XData::TwoSided, this);
1472
1473 if (!guiName.empty())
1474 {
1475 _guiEntry->SetValue(guiName);
1476 }
1477 else
1478 {
1479 if (_xData->getPageLayout() != layoutBefore)
1480 {
1481 toggleLayout();
1482 }
1483
1484 if (_guiEntry->GetValue() != guiDefBefore)
1485 {
1486 _guiEntry->SetValue(guiDefBefore);
1487 }
1488
1489 updateGuiView();
1490 }
1491 }
1492
1493 void ReadableEditorDialog::onInsert(wxCommandEvent& ev)
1494 {
1495 if (_xData->getPageLayout() == XData::TwoSided)
1496 {
1497 static_cast<wxWindow*>(ev.GetEventObject())->PopupMenu(_insertMenu.get());
1498 }
1499 else
1500 {
1501 insertPage();
1502 }
1503 }
1504
1505 void ReadableEditorDialog::onDelete(wxCommandEvent& ev)
1506 {
1507 if (_xData->getPageLayout() == XData::TwoSided)
1508 {
1509 static_cast<wxWindow*>(ev.GetEventObject())->PopupMenu(_deleteMenu.get());
1510 }
1511 else
1512 {
1513 deletePage();
1514 }
1515 }
1516
1517 void ReadableEditorDialog::handleNumberOfPagesChanged()
1518 {
1519 std::size_t nNP = static_cast<std::size_t>(_numPages->GetValue());
1520
1521 _xData->setNumPages(nNP);
1522
1523 if (_currentPageIndex >= nNP)
1524 {
1525 showPage(nNP - 1);
1526 }
1527 }
1528
1529 void ReadableEditorDialog::onNumPagesChanged(wxSpinEvent& ev)
1530 {
1531 handleNumberOfPagesChanged();
1532 }
1533
1534 void ReadableEditorDialog::onToolsClicked(wxCommandEvent& ev)
1535 {
1536 static_cast<wxWindow*>(ev.GetEventObject())->PopupMenu(_toolsMenu.get());
1537 }
1538
1539 void ReadableEditorDialog::showDuplicateDefinitions()
1540 {
1541 _xdLoader->retrieveXdInfo();
1542
1543 XData::StringVectorMap dupDefs;
1544
1545 try
1546 {
1547 dupDefs = _xdLoader->getDuplicateDefinitions();
1548 }
1549 catch (std::runtime_error&)
1550 {
1551 wxutil::Messagebox::Show(
1552 _("Duplicated XData definitions"),
1553 _("There are no duplicated definitions!"),
1554 ui::IDialog::MESSAGE_CONFIRM, this
1555 );
1556 return;
1557 }
1558
1559 std::string out;
1560
1561 for (XData::StringVectorMap::iterator it = dupDefs.begin(); it != dupDefs.end(); ++it)
1562 {
1563 std::string occ;
1564
1565 for (std::size_t n = 0; n < it->second.size()-1; n++)
1566 {
1567 occ += it->second[n] + ", ";
1568 }
1569
1570 occ += it->second[it->second.size() - 1];
1571
1572 out += fmt::format(_("{0} has been defined in:"), it->first);
1573 out +="\n\t";
1574 out += occ;
1575 out += ".\n\n";
1576 }
1577
1578 TextViewInfoDialog::Show(_("Duplicated XData definitions"), out, this);
1579 }
1580
1581 void ReadableEditorDialog::onOneSided(wxCommandEvent& ev)
1582 {
1583 if ((_oneSidedButton->GetValue()))
1584 useOneSidedEditing();
1585 }
1586
1587 void ReadableEditorDialog::onTwoSided(wxCommandEvent& ev)
1588 {
1589 if ((_twoSidedButton->GetValue()))
1590 useTwoSidedEditing();
1591 }
1592
1593 //////////////////////////////////////////////////////////////////////////////
1594 // Callback Methods for Events:
1595
1596 void ReadableEditorDialog::onFocusOut(wxFocusEvent& ev)
1597 {
1598 if (ev.GetEventObject() == _xDataNameEntry)
1599 {
1600 // Only call checkXDataUniqueness if the method is not running yet.
1601 if (!_runningXDataUniquenessCheck)
1602 {
1603 checkXDataUniqueness();
1604 }
1605 }
1606 else // WIDGET_GUI_ENTRY
1607 {
1608 // Only call checkGuiLayout() if the method is not yet running
1609 if (!_runningGuiLayoutCheck)
1610 {
1611 checkGuiLayout();
1612 }
1613 }
1614
1615 ev.Skip();
1616 }
1617
1618 void ReadableEditorDialog::onTextChanged(wxCommandEvent& ev)
1619 {
1620 // Update the preview
1621 updateGuiView();
1622 }
1623
1624 void ReadableEditorDialog::onChar(wxKeyEvent& ev)
1625 {
1626 if (ev.GetEventObject() == _xDataNameEntry)
1627 {
1628 switch (ev.GetKeyCode())
1629 {
1630 // Some forbidden keys
1631 case WXK_SPACE:
1632 case '!':
1633 case '*':
1634 case '+':
1635 case WXK_NUMPAD_MULTIPLY:
1636 case WXK_NUMPAD_SUBTRACT:
1637 case WXK_NUMPAD_ADD:
1638 case WXK_NUMPAD_SEPARATOR:
1639 case ',':
1640 case '.':
1641 case ':':
1642 case ';':
1643 case '?':
1644 case '-':
1645 return;
1646
1647 // Since TAB would be added to the value, inercept it here and
1648 // emulate TAB-traversal to the next control instead
1649 case WXK_TAB:
1650 if (ev.ShiftDown())
1651 {
1652 _nameEntry->SetFocus();
1653 }
1654 else
1655 {
1656 _numPages->SetFocus();
1657 }
1658 return;
1659
1660 // Check Uniqueness of the XData-Name.
1661 case WXK_RETURN:
1662 case WXK_NUMPAD_ENTER:
1663 checkXDataUniqueness();
1664 ev.Skip();
1665 break;
1666
1667 default:
1668 ev.Skip();
1669 break;
1670 }
1671 }
1672 else if (ev.GetEventObject() == _nameEntry)
1673 {
1674 switch (ev.GetKeyCode())
1675 {
1676 // Forbidden key check
1677 case WXK_TAB:
1678 _xDataNameEntry->SetFocus();
1679 return; // forbid the tab
1680 default:
1681 ev.Skip();
1682 return;
1683 }
1684 }
1685 else if (ev.GetEventObject() == _numPages)
1686 {
1687 if (ev.GetKeyCode() != WXK_ESCAPE)
1688 {
1689 ev.Skip();
1690 return;
1691 }
1692
1693 // Restore the old value.
1694 _numPages->SetValue(static_cast<int>(_xData->getNumPages()));
1695 return;
1696 }
1697 else if (ev.GetEventObject() == _guiEntry)
1698 {
1699 if (ev.GetKeyCode() != WXK_RETURN && ev.GetKeyCode() != WXK_NUMPAD_ENTER)
1700 {
1701 ev.Skip();
1702 return;
1703 }
1704
1705 checkGuiLayout();
1706 return;
1707 }
1708
1709 ev.Skip();
1710 }
1711
1712 // Sets the text of a TextView identified by its widget enumerator and scrolls it to the end.
1713 void ReadableEditorDialog::setTextViewAndScroll(wxTextCtrl* view, const std::string& text)
1714 {
1715 view->SetValue(text);
1716 view->ShowPosition(view->GetLastPosition());
1717 }
1718
1719 } // namespace ui