Reconnect events to the widget after recreating it in the sample

This ensures that the expected events are given even after the widget is
recreated due to a change of some controls on its page (and not because
it's recreated by one of the menu commands in the parent frame).

We also don't need to connect to these events from WidgetsFrame any
longer (and, in fact, doing it would be wrong as we'd connect twice),
which simplifies its code.
This commit is contained in:
Vadim Zeitlin
2024-02-19 00:40:30 +01:00
parent 828b7ee844
commit a8b4753d1d
32 changed files with 105 additions and 25 deletions
+25 -25
View File
@@ -257,8 +257,6 @@ protected:
WidgetsPage *CurrentPage();
private:
void ConnectToWidgetEvents();
// the panel containing everything
wxPanel *m_panel;
@@ -773,23 +771,6 @@ WidgetsPage *WidgetsFrame::CurrentPage()
return wxStaticCast(page, WidgetsPage);
}
void WidgetsFrame::ConnectToWidgetEvents()
{
auto& app = wxGetApp();
const Widgets& widgets = CurrentPage()->GetWidgets();
for ( Widgets::const_iterator it = widgets.begin();
it != widgets.end();
++it )
{
wxWindow* const w = *it;
wxCHECK_RET(w, "null widget");
app.ConnectToWidgetEvents(w);
}
}
WidgetsFrame::~WidgetsFrame()
{
#if USE_LOG
@@ -847,7 +828,18 @@ void WidgetsFrame::OnPageChanged(WidgetsBookCtrlEvent& event)
curPage->SetScrollRate(10, 10);
curPage->FitInside();
ConnectToWidgetEvents();
auto& app = wxGetApp();
for ( const auto w : CurrentPage()->GetWidgets() )
{
app.ConnectToWidgetEvents(w);
}
// From now on, we're interested in these notifications as we'll need
// to reconnect to the widget events if it's recreated (unfortunately
// we can't rely getting them on creation as some page don't generate
// them -- but neither can we rely on not getting them as some pages do
// generate them, hence the use of m_notifyRecreate flag).
curPage->EnableRecreationNotifications();
}
// re-apply the attributes to the widget(s)
@@ -1016,11 +1008,6 @@ void WidgetsFrame::OnSetBorder(wxCommandEvent& event)
WidgetsPage *page = CurrentPage();
page->RecreateWidget();
ConnectToWidgetEvents();
// re-apply the attributes to the widget(s)
page->SetUpWidget();
}
void WidgetsFrame::OnSetVariant(wxCommandEvent& event)
@@ -1482,6 +1469,19 @@ wxCheckBox *WidgetsPage::CreateCheckBoxAndAddToSizer(wxSizer *sizer,
return checkbox;
}
void WidgetsPage::NotifyWidgetRecreation(wxWindow* widget)
{
if ( !m_notifyRecreate )
{
// We're in the process of initialization, don't notify yet.
return;
}
SetUpWidget();
wxGetApp().ConnectToWidgetEvents(widget);
}
/* static */
bool WidgetsPage::IsUsingLogWindow()
{