From 76c71e580059c07f242738e9d56c003f103dccbd Mon Sep 17 00:00:00 2001 From: twanvl Date: Sat, 2 Aug 2008 17:04:30 +0000 Subject: [PATCH] Catch all exceptions in onPaint functions, because otherwise we show a message box. Message boxes while in a paint handler lead to a crash on win32. Made a CATCH_ALL_ERRORS macro containing the common catch statements. git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@1034 0fc631ac-6414-0410-93d0-97cfa31319b6 --- src/gui/control/card_viewer.cpp | 4 +++- src/gui/control/gallery_list.cpp | 4 +++- src/gui/control/graph.cpp | 20 +++++++++++--------- src/gui/packages_window.cpp | 6 ++++++ src/gui/symbol/control.cpp | 4 +++- src/main.cpp | 20 +++----------------- src/util/error.hpp | 12 ++++++++++++ 7 files changed, 41 insertions(+), 29 deletions(-) diff --git a/src/gui/control/card_viewer.cpp b/src/gui/control/card_viewer.cpp index 758417a3..a489621c 100644 --- a/src/gui/control/card_viewer.cpp +++ b/src/gui/control/card_viewer.cpp @@ -87,7 +87,9 @@ void CardViewer::onPaint(wxPaintEvent&) { // draw if (!up_to_date) { up_to_date = true; - draw(dc); + try { + draw(dc); + } CATCH_ALL_ERRORS(false); // don't show message boxes in onPaint! } } diff --git a/src/gui/control/gallery_list.cpp b/src/gui/control/gallery_list.cpp index 5e644a77..35ac2b36 100644 --- a/src/gui/control/gallery_list.cpp +++ b/src/gui/control/gallery_list.cpp @@ -183,7 +183,9 @@ wxSize GalleryList::DoGetBestSize() const { void GalleryList::onPaint(wxPaintEvent&) { wxBufferedPaintDC dc(this); - OnDraw(dc); + try { + OnDraw(dc); + } CATCH_ALL_ERRORS(false); // don't show message boxes in onPaint! } void GalleryList::OnDraw(DC& dc) { wxSize cs = GetClientSize(); diff --git a/src/gui/control/graph.cpp b/src/gui/control/graph.cpp index 6e304494..53dddb2d 100644 --- a/src/gui/control/graph.cpp +++ b/src/gui/control/graph.cpp @@ -985,16 +985,18 @@ size_t GraphControl::getDimensionality() const { void GraphControl::onPaint(wxPaintEvent&) { wxBufferedPaintDC dc(this); - wxSize cs = GetClientSize(); - RotatedDC rdc(dc, 0, RealRect(RealPoint(0,0),cs), 1, QUALITY_LOW); - rdc.SetPen(*wxTRANSPARENT_PEN); - rdc.SetBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); - rdc.DrawRectangle(rdc.getInternalRect()); - if (graph) { - for (int layer = LAYER_BOTTOM ; layer < LAYER_COUNT ; ++layer) { - graph->draw(rdc, current_item, (DrawLayer)layer); + try { + wxSize cs = GetClientSize(); + RotatedDC rdc(dc, 0, RealRect(RealPoint(0,0),cs), 1, QUALITY_LOW); + rdc.SetPen(*wxTRANSPARENT_PEN); + rdc.SetBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); + rdc.DrawRectangle(rdc.getInternalRect()); + if (graph) { + for (int layer = LAYER_BOTTOM ; layer < LAYER_COUNT ; ++layer) { + graph->draw(rdc, current_item, (DrawLayer)layer); + } } - } + } CATCH_ALL_ERRORS(false); // don't show message boxes in onPaint! } void GraphControl::onSize(wxSizeEvent&) { diff --git a/src/gui/packages_window.cpp b/src/gui/packages_window.cpp index bcccac84..6c941bb4 100644 --- a/src/gui/packages_window.cpp +++ b/src/gui/packages_window.cpp @@ -115,6 +115,7 @@ class PackageInfoPanel : public wxPanel { DECLARE_EVENT_TABLE(); void onPaint(wxPaintEvent&); + void draw(DC&); }; PackageInfoPanel::PackageInfoPanel(Window* parent) @@ -128,6 +129,11 @@ void PackageInfoPanel::setPackage(const InstallablePackageP& package) { void PackageInfoPanel::onPaint(wxPaintEvent&) { wxBufferedPaintDC dc(this); + try { + draw(dc); + } CATCH_ALL_ERRORS(false); // don't show message boxes in onPaint! +} +void PackageInfoPanel::draw(DC& dc) { wxSize cs = GetClientSize(); dc.SetPen(*wxTRANSPARENT_PEN); dc.SetBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); diff --git a/src/gui/symbol/control.cpp b/src/gui/symbol/control.cpp index d8019d17..808ad857 100644 --- a/src/gui/symbol/control.cpp +++ b/src/gui/symbol/control.cpp @@ -200,7 +200,9 @@ void SymbolControl::draw(DC& dc) { } void SymbolControl::onPaint(wxPaintEvent&) { wxBufferedPaintDC dc(this); - draw(dc); + try { + draw(dc); + } CATCH_ALL_ERRORS(false); // don't show message boxes in onPaint! } // ----------------------------------------------------------------------------- : Events diff --git a/src/main.cpp b/src/main.cpp index 59e0e810..4487b709 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -40,7 +40,7 @@ class MSE : public wxApp { */ int OnRun(); /// On exit: write the settings to the config file - int OnExit(); + int OnExit(); /// On exception: display error message bool OnExceptionInMainLoop(); }; @@ -209,14 +209,7 @@ int MSE::OnRun() { (new WelcomeWindow())->Show(); return wxApp::OnRun(); - } catch (const Error& e) { - handle_error(e, false); - } catch (const std::exception& e) { - // we don't throw std::exception ourselfs, so this is probably something serious - handle_error(InternalError(String(e.what(), IF_UNICODE(wxConvLocal, wxSTRING_MAXLEN) )), false); - } catch (...) { - handle_error(InternalError(_("An unexpected exception occurred!")), false); - } + } CATCH_ALL_ERRORS(true); return EXIT_FAILURE; } @@ -234,13 +227,6 @@ int MSE::OnExit() { bool MSE::OnExceptionInMainLoop() { try { throw; // rethrow the exception, so we can examine it - } catch (const Error& e) { - handle_error(e, false); - } catch (const std::exception& e) { - // we don't throw std::exception ourselfs, so this is probably something serious - handle_error(InternalError(String(e.what(), IF_UNICODE(wxConvLocal, wxSTRING_MAXLEN) )), false); - } catch (...) { - handle_error(InternalError(_("An unexpected exception occurred!")), false); - } + } CATCH_ALL_ERRORS(true); return true; } diff --git a/src/util/error.hpp b/src/util/error.hpp index 0c087dea..f76cff76 100644 --- a/src/util/error.hpp +++ b/src/util/error.hpp @@ -123,5 +123,17 @@ void handle_warning(const String& w, bool now = true); /** Should be called repeatedly (e.g. in an onIdle event handler) */ void handle_pending_errors(); +/// Catch all types of errors, and pass then to handle_error +#define CATCH_ALL_ERRORS(handle_now) \ + catch (const Error& e) { \ + handle_error(e, false, handle_now); \ + } catch (const std::exception& e) { \ + /* we don't throw std::exception ourselfs, so this is probably something serious */ \ + String message(e.what(), IF_UNICODE(wxConvLocal, wxSTRING_MAXLEN) ); \ + handle_error(InternalError(message), false, handle_now); \ + } catch (...) { \ + handle_error(InternalError(_("An unexpected exception occurred!")), false, handle_now); \ + } + // ----------------------------------------------------------------------------- : EOF #endif