Move widget event handlers to WidgetsApp in the widgets sample

No real changes, just move the function connecting to the widget events
from WidgetsFrame to WidgetsApp to allow reusing it from outside
WidgetsFrame in the next commit.
This commit is contained in:
Vadim Zeitlin 2024-02-18 19:01:44 +01:00
parent ccce75f3cc
commit 828b7ee844

View file

@ -183,6 +183,10 @@ public:
// real implementation of WidgetsPage method with the same name // real implementation of WidgetsPage method with the same name
bool IsUsingLogWindow() const; bool IsUsingLogWindow() const;
// connects handlers showing some interesting widget events to the given
// widget
void ConnectToWidgetEvents(wxWindow* w);
private: private:
#if USE_LOG #if USE_LOG
wxLog* m_logTarget; wxLog* m_logTarget;
@ -253,9 +257,6 @@ protected:
WidgetsPage *CurrentPage(); WidgetsPage *CurrentPage();
private: private:
void OnWidgetFocus(wxFocusEvent& event);
void OnWidgetContextMenu(wxContextMenuEvent& event);
void ConnectToWidgetEvents(); void ConnectToWidgetEvents();
// the panel containing everything // the panel containing everything
@ -430,6 +431,51 @@ bool WidgetsApp::IsUsingLogWindow() const
#endif // USE_LOG #endif // USE_LOG
} }
namespace
{
void OnFocus(wxFocusEvent& event)
{
// Don't show annoying message boxes when starting or closing the sample,
// only log these events in our own logger.
if ( wxGetApp().IsUsingLogWindow() )
{
wxWindow* win = (wxWindow*)event.GetEventObject();
wxLogMessage("Widget '%s' %s focus", win->GetClassInfo()->GetClassName(),
event.GetEventType() == wxEVT_SET_FOCUS ? "got" : "lost");
}
event.Skip();
}
} // anonymous namespace
void WidgetsApp::ConnectToWidgetEvents(wxWindow* w)
{
w->Bind(wxEVT_SET_FOCUS, OnFocus);
w->Bind(wxEVT_KILL_FOCUS, OnFocus);
w->Bind(wxEVT_ENTER_WINDOW, [w](wxMouseEvent& event)
{
wxLogMessage("Mouse entered into '%s'", w->GetClassInfo()->GetClassName());
event.Skip();
});
w->Bind(wxEVT_LEAVE_WINDOW, [w](wxMouseEvent& event)
{
wxLogMessage("Mouse left '%s'", w->GetClassInfo()->GetClassName());
event.Skip();
});
w->Bind(wxEVT_CONTEXT_MENU, [w](wxContextMenuEvent& event)
{
wxLogMessage("Context menu event for '%s' at %dx%d",
w->GetClassInfo()->GetClassName(),
event.GetPosition().x,
event.GetPosition().y);
event.Skip();
});
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// WidgetsFrame construction // WidgetsFrame construction
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -729,6 +775,8 @@ WidgetsPage *WidgetsFrame::CurrentPage()
void WidgetsFrame::ConnectToWidgetEvents() void WidgetsFrame::ConnectToWidgetEvents()
{ {
auto& app = wxGetApp();
const Widgets& widgets = CurrentPage()->GetWidgets(); const Widgets& widgets = CurrentPage()->GetWidgets();
for ( Widgets::const_iterator it = widgets.begin(); for ( Widgets::const_iterator it = widgets.begin();
@ -738,21 +786,7 @@ void WidgetsFrame::ConnectToWidgetEvents()
wxWindow* const w = *it; wxWindow* const w = *it;
wxCHECK_RET(w, "null widget"); wxCHECK_RET(w, "null widget");
w->Bind(wxEVT_SET_FOCUS, &WidgetsFrame::OnWidgetFocus, this); app.ConnectToWidgetEvents(w);
w->Bind(wxEVT_KILL_FOCUS, &WidgetsFrame::OnWidgetFocus, this);
w->Bind(wxEVT_ENTER_WINDOW, [w](wxMouseEvent& event)
{
wxLogMessage("Mouse entered into '%s'", w->GetClassInfo()->GetClassName());
event.Skip();
});
w->Bind(wxEVT_LEAVE_WINDOW, [w](wxMouseEvent& event)
{
wxLogMessage("Mouse left '%s'", w->GetClassInfo()->GetClassName());
event.Skip();
});
w->Bind(wxEVT_CONTEXT_MENU, &WidgetsFrame::OnWidgetContextMenu, this);
} }
} }
@ -1270,31 +1304,6 @@ void WidgetsFrame::OnSetHint(wxCommandEvent& WXUNUSED(event))
#endif // wxUSE_MENUS #endif // wxUSE_MENUS
void WidgetsFrame::OnWidgetFocus(wxFocusEvent& event)
{
// Don't show annoying message boxes when starting or closing the sample,
// only log these events in our own logger.
if ( wxGetApp().IsUsingLogWindow() )
{
wxWindow* win = (wxWindow*)event.GetEventObject();
wxLogMessage("Widget '%s' %s focus", win->GetClassInfo()->GetClassName(),
event.GetEventType() == wxEVT_SET_FOCUS ? "got" : "lost");
}
event.Skip();
}
void WidgetsFrame::OnWidgetContextMenu(wxContextMenuEvent& event)
{
wxWindow* win = (wxWindow*)event.GetEventObject();
wxLogMessage("Context menu event for %s at %dx%d",
win->GetClassInfo()->GetClassName(),
event.GetPosition().x,
event.GetPosition().y);
event.Skip();
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// WidgetsPageInfo // WidgetsPageInfo
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------