From aa47c15abdbd582c5a7c78d745412c15e4c41b45 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 19 Dec 2017 21:47:01 +0100 Subject: [PATCH] Add WXDestroyWithoutChildren() and use it from wxStaticBoxSizer Factor out the code from wxStaticBoxSizer dtor into a wxStaticBox method to improve encapsulation: the static box knows better than another class how to detach its children from it before destroying it. No real changes yet. --- include/wx/statbox.h | 6 ++++++ src/common/sizer.cpp | 15 +-------------- src/common/statboxcmn.cpp | 16 ++++++++++++++++ 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/include/wx/statbox.h b/include/wx/statbox.h index 968903b009..26f3f0dd14 100644 --- a/include/wx/statbox.h +++ b/include/wx/statbox.h @@ -45,6 +45,12 @@ public: *borderOther = BORDER; } + // This is an internal function currently used by wxStaticBoxSizer only. + // + // Reparent all children of the static box under its parent and destroy the + // box itself. + virtual void WXDestroyWithoutChildren(); + protected: // choose the default border for this window virtual wxBorder GetDefaultBorder() const wxOVERRIDE { return wxBORDER_NONE; } diff --git a/src/common/sizer.cpp b/src/common/sizer.cpp index ce59f88a1e..b0b94674b0 100644 --- a/src/common/sizer.cpp +++ b/src/common/sizer.cpp @@ -2582,20 +2582,7 @@ wxStaticBoxSizer::~wxStaticBoxSizer() // previous wxWidgets versions, so ensure they are left alive. if ( m_staticBox ) - { - // Notice that we must make a copy of the list as it will be changed by - // Reparent() calls in the loop. - const wxWindowList children = m_staticBox->GetChildren(); - wxWindow* const parent = m_staticBox->GetParent(); - for ( wxWindowList::const_iterator i = children.begin(); - i != children.end(); - ++i ) - { - (*i)->Reparent(parent); - } - - delete m_staticBox; - } + m_staticBox->WXDestroyWithoutChildren(); } void wxStaticBoxSizer::RecalcSizes() diff --git a/src/common/statboxcmn.cpp b/src/common/statboxcmn.cpp index 3539c3c356..68b42d7af4 100644 --- a/src/common/statboxcmn.cpp +++ b/src/common/statboxcmn.cpp @@ -36,6 +36,22 @@ wxStaticBoxBase::wxStaticBoxBase() #endif } +void wxStaticBoxBase::WXDestroyWithoutChildren() +{ + // Notice that we must make a copy of the list as it will be changed by + // Reparent() calls in the loop. + const wxWindowList children = GetChildren(); + wxWindow* const parent = GetParent(); + for ( wxWindowList::const_iterator i = children.begin(); + i != children.end(); + ++i ) + { + (*i)->Reparent(parent); + } + + delete this; +} + // ---------------------------------------------------------------------------- // XTI // ----------------------------------------------------------------------------