From 3d9656395a0d00bece6befdf641b4fb5508fdfa5 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 8 Jul 2019 12:53:02 +0200 Subject: [PATCH] Fix unwanted message boxes in widgets sample once and for all Add IsUsingLogWindow() that can be used to check if the log messages go into the listbox instead of being shown in a message box, which was annoying when starting or quitting the sample. This is a bit more complicated than the hack previously used in TextWidgetsPage::OnText(), but more general and can be easily reused for the focus loss messages, for example. --- samples/widgets/textctrl.cpp | 9 +------ samples/widgets/widgets.cpp | 47 ++++++++++++++++++++++++++++++++++-- samples/widgets/widgets.h | 4 +++ 3 files changed, 50 insertions(+), 10 deletions(-) diff --git a/samples/widgets/textctrl.cpp b/samples/widgets/textctrl.cpp index cda0bc9362..ff6ea02226 100644 --- a/samples/widgets/textctrl.cpp +++ b/samples/widgets/textctrl.cpp @@ -989,15 +989,8 @@ void TextWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event) void TextWidgetsPage::OnText(wxCommandEvent& event) { - // small hack to suppress the very first message: by then the logging is - // not yet redirected and so initial setting of the text value results in - // an annoying message box - static bool s_firstTime = true; - if ( s_firstTime ) - { - s_firstTime = false; + if ( !IsUsingLogWindow() ) return; - } // Replace middle of long text with ellipsis just to avoid filling up the // log control with too much unnecessary stuff. diff --git a/samples/widgets/widgets.cpp b/samples/widgets/widgets.cpp index 0d26fc05e8..fbdc36e528 100644 --- a/samples/widgets/widgets.cpp +++ b/samples/widgets/widgets.cpp @@ -139,6 +139,13 @@ const wxChar *WidgetsCategories[MAX_PAGES] = { class WidgetsApp : public wxApp { public: + WidgetsApp() + { +#if USE_LOG + m_logTarget = NULL; +#endif // USE_LOG + } + // override base class virtuals // ---------------------------- @@ -146,8 +153,20 @@ public: // initialization (doing it here and not in the ctor allows to have an error // return: if OnInit() returns false, the application terminates) virtual bool OnInit() wxOVERRIDE; + + // real implementation of WidgetsPage method with the same name + bool IsUsingLogWindow() const; + +private: +#if USE_LOG + wxLog* m_logTarget; +#endif // USE_LOG + + wxDECLARE_NO_COPY_CLASS(WidgetsApp); }; +wxDECLARE_APP(WidgetsApp); // This provides a convenient wxGetApp() accessor. + // Define a new frame type: this is going to be our main frame class WidgetsFrame : public wxFrame { @@ -375,9 +394,22 @@ bool WidgetsApp::OnInit() wxFrame *frame = new WidgetsFrame(title + " widgets demo"); frame->Show(); +#if USE_LOG + m_logTarget = wxLog::GetActiveTarget(); +#endif // USE_LOG + return true; } +bool WidgetsApp::IsUsingLogWindow() const +{ +#if USE_LOG + return wxLog::GetActiveTarget() == m_logTarget; +#else // !USE_LOG + return false; +#endif // USE_LOG +} + // ---------------------------------------------------------------------------- // WidgetsFrame construction // ---------------------------------------------------------------------------- @@ -1200,8 +1232,13 @@ void WidgetsFrame::OnSetHint(wxCommandEvent& WXUNUSED(event)) void WidgetsFrame::OnWidgetFocus(wxFocusEvent& event) { - wxLogMessage("Widgets %s focus", - event.GetEventType() == wxEVT_SET_FOCUS ? "got" : "lost"); + // Don't show annoying message boxes when starting or closing the sample, + // only log these events in our own logger. + if ( wxGetApp().IsUsingLogWindow() ) + { + wxLogMessage("Widgets %s focus", + event.GetEventType() == wxEVT_SET_FOCUS ? "got" : "lost"); + } event.Skip(); } @@ -1384,3 +1421,9 @@ wxCheckBox *WidgetsPage::CreateCheckBoxAndAddToSizer(wxSizer *sizer, return checkbox; } + +/* static */ +bool WidgetsPage::IsUsingLogWindow() +{ + return wxGetApp().IsUsingLogWindow(); +} diff --git a/samples/widgets/widgets.h b/samples/widgets/widgets.h index c632fffe6c..77d4ffe642 100644 --- a/samples/widgets/widgets.h +++ b/samples/widgets/widgets.h @@ -155,6 +155,10 @@ public: // the default attributes for the widget static WidgetAttributes& GetAttrs(); + // return true if we're showing logs in the log window (always the case + // except during startup and shutdown) + static bool IsUsingLogWindow(); + protected: // several helper functions for page creation