From 828b7ee84487c96f92c4c03d73ce6d8e995b7066 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 18 Feb 2024 19:01:44 +0100 Subject: [PATCH] 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. --- samples/widgets/widgets.cpp | 95 ++++++++++++++++++++----------------- 1 file changed, 52 insertions(+), 43 deletions(-) diff --git a/samples/widgets/widgets.cpp b/samples/widgets/widgets.cpp index 507295570e..ca482caa5e 100644 --- a/samples/widgets/widgets.cpp +++ b/samples/widgets/widgets.cpp @@ -183,6 +183,10 @@ public: // real implementation of WidgetsPage method with the same name bool IsUsingLogWindow() const; + // connects handlers showing some interesting widget events to the given + // widget + void ConnectToWidgetEvents(wxWindow* w); + private: #if USE_LOG wxLog* m_logTarget; @@ -253,9 +257,6 @@ protected: WidgetsPage *CurrentPage(); private: - void OnWidgetFocus(wxFocusEvent& event); - void OnWidgetContextMenu(wxContextMenuEvent& event); - void ConnectToWidgetEvents(); // the panel containing everything @@ -430,6 +431,51 @@ bool WidgetsApp::IsUsingLogWindow() const #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 // ---------------------------------------------------------------------------- @@ -729,6 +775,8 @@ WidgetsPage *WidgetsFrame::CurrentPage() void WidgetsFrame::ConnectToWidgetEvents() { + auto& app = wxGetApp(); + const Widgets& widgets = CurrentPage()->GetWidgets(); for ( Widgets::const_iterator it = widgets.begin(); @@ -738,21 +786,7 @@ void WidgetsFrame::ConnectToWidgetEvents() wxWindow* const w = *it; wxCHECK_RET(w, "null widget"); - w->Bind(wxEVT_SET_FOCUS, &WidgetsFrame::OnWidgetFocus, this); - 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); + app.ConnectToWidgetEvents(w); } } @@ -1270,31 +1304,6 @@ void WidgetsFrame::OnSetHint(wxCommandEvent& WXUNUSED(event)) #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 // ----------------------------------------------------------------------------