lmi-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi-commits] [lmi] master 18a41d25 4/5: Show message box safely in wx w


From: Greg Chicares
Subject: [lmi-commits] [lmi] master 18a41d25 4/5: Show message box safely in wx warning_alert() implementation
Date: Wed, 22 Feb 2023 18:09:53 -0500 (EST)

branch: master
commit 18a41d25a792617b8535eb396a78b1c658a7c572
Author: Vadim Zeitlin <vadim@tt-solutions.com>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>

    Show message box safely in wx warning_alert() implementation
    
    This makes this function safe to call even before the GUI is
    initialized.
    
    While this function can't simply use wxSafeShowMessage() because it
    doesn't have quite the semantics we want, it can use a lower-level
    function which only shows the message box without using stderr.
    
    Note that we could also do
    
        if(wxSafeShowMessage("Warning", s))
            {
                std::cerr << "Warning: " << s << std::endl;
            }
    
    which would have almost the same effect and have the advantage of being
    simpler, but we prefer to use this version in order to log the message
    to stderr before showing the message box instead of doing it after
    showing it and so behave in exactly the same way as the current code.
---
 alert_wx.cpp | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/alert_wx.cpp b/alert_wx.cpp
index 2365c31c..c28042ca 100644
--- a/alert_wx.cpp
+++ b/alert_wx.cpp
@@ -27,6 +27,7 @@
 #include "force_linking.hpp"
 
 #include <wx/app.h>                     // wxTheApp
+#include <wx/apptrait.h>
 #include <wx/frame.h>
 #include <wx/msgdlg.h>
 #if defined LMI_MSW
@@ -73,7 +74,14 @@ void status_alert(std::string const& s)
 void warning_alert(std::string const& s)
 {
     std::cerr << "Warning: " << s << std::endl;
-    wxMessageBox(s, "Warning", wxOK, wxTheApp ? wxTheApp->GetTopWindow() : 
nullptr);
+
+    // We don't use wxSafeShowMessage() here because it would only log the
+    // message to stderr if the message box cannot be shown, while we want to
+    // always log it, even in addition to showing the message box.
+    //
+    // So we use a lower-level function used by wxSafeShowMessage() and simply
+    // ignore its return value, indicating whether it could show the box or 
not.
+    wxApp::GetValidTraits().SafeMessageBox(s, "Warning");
 }
 
 /// It seems silly to offer an option that should never be declined,



reply via email to

[Prev in Thread] Current Thread [Next in Thread]