Skip to content

Commit d536156

Browse files
committed
Unify dialog boxes on file loading errors into one place.
1 parent 8b9e5db commit d536156

File tree

3 files changed

+14
-37
lines changed

3 files changed

+14
-37
lines changed

src/gui/app.cc

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ wxBEGIN_EVENT_TABLE(Application, wxApp) EVT_TIMER(wxID_ANY, Application::OnSplas
8383

8484
// Process command line arguments, if any.
8585
for (int i = 1; i < argc; i++) {
86-
const AppLoadResult result = LoadFile(argv[i]);
86+
const AppLoadResult result = LoadFile(argv[i], nullptr);
8787
if (result == GBT_APP_OPEN_FAILED) {
8888
wxMessageBox(wxString::Format(_("Gambit could not open file for reading:\n%s"), argv[i]),
8989
_("Unable to open file"), wxOK | wxICON_ERROR, nullptr);
@@ -131,10 +131,12 @@ void Application::DismissSplash()
131131
m_splash = nullptr;
132132
}
133133

134-
AppLoadResult Application::LoadFile(const wxString &p_filename)
134+
AppLoadResult Application::LoadFile(const wxString &p_filename, wxWindow *p_parent)
135135
{
136-
std::ifstream infile((const char *)p_filename.mb_str());
136+
std::ifstream infile(p_filename.mb_str());
137137
if (!infile.good()) {
138+
wxMessageBox(_("Gambit could not open file for reading:\n") + p_filename,
139+
_("Unable to open file"), wxOK | wxICON_ERROR, p_parent);
138140
return GBT_APP_OPEN_FAILED;
139141
}
140142

@@ -146,21 +148,21 @@ AppLoadResult Application::LoadFile(const wxString &p_filename)
146148
(void)new GameFrame(nullptr, doc);
147149
return GBT_APP_FILE_OK;
148150
}
149-
else {
150-
delete doc;
151-
}
151+
delete doc;
152152

153153
try {
154154
const Game nfg = ReadGame(infile);
155155

156156
m_fileHistory.AddFileToHistory(p_filename);
157157
m_fileHistory.Save(*wxConfigBase::Get());
158158
doc = new GameDocument(nfg);
159-
doc->SetFilename(wxT(""));
159+
doc->SetFilename("");
160160
(void)new GameFrame(nullptr, doc);
161161
return GBT_APP_FILE_OK;
162162
}
163163
catch (InvalidFileException &) {
164+
wxMessageBox(_("File is not in a format Gambit recognizes:\n") + p_filename,
165+
_("Unable to read file"), wxOK | wxICON_ERROR, p_parent);
164166
return GBT_APP_PARSE_FAILED;
165167
}
166168
}

src/gui/app.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,10 @@ class Application final : public wxApp {
6363
}
6464
void RemoveMenu(wxMenu *p_menu) { m_fileHistory.RemoveMenu(p_menu); }
6565

66-
AppLoadResult LoadFile(const wxString &);
66+
AppLoadResult LoadFile(const wxString &, wxWindow *);
67+
;
6768
#ifdef __WXMAC__
68-
void MacOpenFile(const wxString &filename) override { LoadFile(filename); }
69+
void MacOpenFile(const wxString &filename) override { LoadFile(filename, nullptr); }
6970
#endif // __WXMAC__
7071

7172
//!

src/gui/gameframe.cc

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -649,20 +649,7 @@ void GameFrame::OnFileOpen(wxCommandEvent &)
649649
if (dialog.ShowModal() == wxID_OK) {
650650
const wxString filename = dialog.GetPath();
651651
wxGetApp().SetCurrentDir(wxPathOnly(filename));
652-
653-
const AppLoadResult result = wxGetApp().LoadFile(filename);
654-
if (result == GBT_APP_OPEN_FAILED) {
655-
wxMessageDialog msgdialog(
656-
this, wxT("Gambit could not open file '") + filename + wxT("' for reading."),
657-
wxT("Unable to open file"), wxOK | wxICON_ERROR);
658-
msgdialog.ShowModal();
659-
}
660-
else if (result == GBT_APP_PARSE_FAILED) {
661-
wxMessageDialog msgdialog(
662-
this, wxT("File '") + filename + wxT("' is not in a format Gambit recognizes."),
663-
wxT("Unable to read file"), wxOK | wxICON_ERROR);
664-
msgdialog.ShowModal();
665-
}
652+
wxGetApp().LoadFile(filename, this);
666653
}
667654
}
668655

@@ -903,20 +890,7 @@ void GameFrame::OnFileExit(wxCommandEvent &p_event)
903890
void GameFrame::OnFileMRUFile(wxCommandEvent &p_event)
904891
{
905892
const wxString filename = wxGetApp().GetHistoryFile(p_event.GetId() - wxID_FILE1);
906-
const AppLoadResult result = wxGetApp().LoadFile(filename);
907-
908-
if (result == GBT_APP_OPEN_FAILED) {
909-
wxMessageDialog dialog(this,
910-
wxT("Gambit could not open file '") + filename + wxT("' for reading."),
911-
wxT("Unable to open file"), wxOK | wxICON_ERROR);
912-
dialog.ShowModal();
913-
}
914-
else if (result == GBT_APP_PARSE_FAILED) {
915-
wxMessageDialog dialog(
916-
this, wxT("File '") + filename + wxT("' is not in a format Gambit recognizes."),
917-
wxT("Unable to read file"), wxOK | wxICON_ERROR);
918-
dialog.ShowModal();
919-
}
893+
wxGetApp().LoadFile(filename, this);
920894
}
921895

922896
//----------------------------------------------------------------------

0 commit comments

Comments
 (0)