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.
This commit is contained in:
Vadim Zeitlin 2019-07-08 12:53:02 +02:00
parent 7206194d08
commit 3d9656395a
3 changed files with 50 additions and 10 deletions

View File

@ -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.

View File

@ -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();
}

View File

@ -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