From 39883a270a0f1e7b093183e3dfbe613250cb2205 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 7 Mar 2021 15:37:35 +0100 Subject: [PATCH] Disable main window when showing msgbox in wxSafeShowMessage() Pass correct parent HWND to ::MessageBox() in order to disable the window while the message box is shown, as this function is supposed to be similar to modal wxMessageBox() and it was unexpected that the application could be reentered via the event handlers from inside it. This required adding wxAppTraits::GetMainHWND() in order to only use the HWND in GUI applications from the function defined in non-GUI code. --- include/wx/msw/apptbase.h | 3 +++ include/wx/msw/apptrait.h | 4 ++++ src/common/log.cpp | 4 +++- src/msw/app.cpp | 6 ++++++ 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/include/wx/msw/apptbase.h b/include/wx/msw/apptbase.h index 2442bf3b61..c6d6ce4b45 100644 --- a/include/wx/msw/apptbase.h +++ b/include/wx/msw/apptbase.h @@ -58,6 +58,9 @@ public: // write text to the console, return true if ok or false on error virtual bool WriteToStderr(const wxString& text) = 0; + // return the main application window or 0 if none + virtual WXHWND GetMainHWND() const = 0; + protected: #if wxUSE_THREADS // implementation of WaitForThread() for the console applications which is diff --git a/include/wx/msw/apptrait.h b/include/wx/msw/apptrait.h index 4d6c29abc4..30a61d4f31 100644 --- a/include/wx/msw/apptrait.h +++ b/include/wx/msw/apptrait.h @@ -30,6 +30,7 @@ public: #endif // wxUSE_THREADS virtual bool CanUseStderr() wxOVERRIDE { return true; } virtual bool WriteToStderr(const wxString& text) wxOVERRIDE; + virtual WXHWND GetMainHWND() const wxOVERRIDE { return NULL; } }; #if wxUSE_GUI @@ -55,6 +56,7 @@ public: virtual bool CanUseStderr() wxOVERRIDE; virtual bool WriteToStderr(const wxString& text) wxOVERRIDE; + virtual WXHWND GetMainHWND() const wxOVERRIDE; }; #elif defined(__WXGTK__) @@ -85,6 +87,7 @@ public: virtual bool CanUseStderr() { return false; } virtual bool WriteToStderr(const wxString& WXUNUSED(text)) { return false; } + virtual WXHWND GetMainHWND() const wxOVERRIDE { return NULL; } }; #elif defined(__WXQT__) @@ -110,6 +113,7 @@ public: virtual bool CanUseStderr() { return false; } virtual bool WriteToStderr(const wxString&) { return false; } + virtual WXHWND GetMainHWND() const wxOVERRIDE { return NULL; } }; #endif diff --git a/src/common/log.cpp b/src/common/log.cpp index 3bf1c33d40..8122b7d9e2 100644 --- a/src/common/log.cpp +++ b/src/common/log.cpp @@ -183,7 +183,9 @@ private: void wxSafeShowMessage(const wxString& title, const wxString& text) { #ifdef __WINDOWS__ - ::MessageBox(NULL, text.t_str(), title.t_str(), MB_OK | MB_ICONSTOP); + const wxAppTraits* const traits = wxApp::GetTraitsIfExists(); + ::MessageBox(traits ? traits->GetMainHWND() : NULL, + text.t_str(), title.t_str(), MB_OK | MB_ICONSTOP); #else wxFprintf(stderr, wxS("%s: %s\n"), title.c_str(), text.c_str()); fflush(stderr); diff --git a/src/msw/app.cpp b/src/msw/app.cpp index 17adabf9a8..d8b19f0295 100644 --- a/src/msw/app.cpp +++ b/src/msw/app.cpp @@ -603,6 +603,12 @@ bool wxGUIAppTraits::WriteToStderr(const wxString& WXUNUSED(text)) #endif // wxUSE_DYNLIB_CLASS/!wxUSE_DYNLIB_CLASS +WXHWND wxGUIAppTraits::GetMainHWND() const +{ + const wxWindow* const w = wxApp::GetMainTopWindow(); + return w ? w->GetHWND() : NULL; +} + // =========================================================================== // wxApp implementation // ===========================================================================