From 789b39be81955770698de0c8d40ebfdad5fa6e27 Mon Sep 17 00:00:00 2001 From: Artur Wieczorek Date: Sun, 7 Nov 2021 22:35:22 +0100 Subject: [PATCH] Fix deleting wxPGProperty from wxPropertyGridPageState in alphabetical mode Categories of properties can be nested so we need to search on all category levels for the the category wxPGProperty belongs to. And wxPGProperty removed from the alphabetical list can be a subproperty of another property so it's parent doesn't have to be the root of the list and we don't have to check this with assertion. Closed #19310. --- src/propgrid/propgridpagestate.cpp | 57 +++++++++++++++++++----------- 1 file changed, 37 insertions(+), 20 deletions(-) diff --git a/src/propgrid/propgridpagestate.cpp b/src/propgrid/propgridpagestate.cpp index ef8b03a752..1b7b9e7ba3 100644 --- a/src/propgrid/propgridpagestate.cpp +++ b/src/propgrid/propgridpagestate.cpp @@ -1836,6 +1836,36 @@ bool wxPropertyGridPageState::IsChildCategory(wxPGProperty* p, return false; } +namespace +{ +static wxPGProperty* FindCategoryForProperty(wxPGProperty* cat, wxPGProperty* prop, int& foundAtIndex) +{ + wxASSERT(cat->IsCategory() || cat->IsRoot()); + + int idx = cat->Index(prop); + if ( idx != wxNOT_FOUND ) + { + foundAtIndex = idx; + return cat; + } + + for ( unsigned int i = 0; i < cat->GetChildCount(); i++ ) + { + wxPGProperty* p = cat->Item(i); + if ( p->IsCategory() ) + { + wxPGProperty* foundCat = FindCategoryForProperty(p, prop, foundAtIndex); + if ( foundCat ) + { + return foundCat; + } + } + } + + return NULL; +} +}; + void wxPropertyGridPageState::DoDelete( wxPGProperty* item, bool doDelete ) { wxCHECK_RET(item != &m_regularArray && item != m_abcArray, @@ -1945,38 +1975,25 @@ void wxPropertyGridPageState::DoDelete( wxPGProperty* item, bool doDelete ) // categorized mode - categorized array parent->RemoveChild(indinparent); - item->m_parent->FixIndicesOfChildren(); + parent->FixIndicesOfChildren(); } else { // non-categorized mode - categorized array // We need to find location of item. - wxPGProperty* cat_parent = &m_regularArray; - int cat_index = m_regularArray.GetChildCount(); - for ( unsigned int i = 0; i < m_regularArray.GetChildCount(); i++ ) + int cat_index; + wxPGProperty* cat_parent = FindCategoryForProperty(&m_regularArray, item, cat_index); + if ( cat_parent ) { - wxPGProperty* p = m_regularArray.Item(i); - if ( p == item ) { cat_index = i; break; } - if ( p->IsCategory() ) - { - int subind = p->Index(item); - if ( subind != wxNOT_FOUND ) - { - cat_parent = p; - cat_index = subind; - break; - } - } + cat_parent->RemoveChild(cat_index); } - cat_parent->RemoveChild(cat_index); // non-categorized mode - non-categorized array if ( !item->IsCategory() ) { - wxASSERT( item->m_parent == m_abcArray ); - item->m_parent->RemoveChild(indinparent); - item->m_parent->FixIndicesOfChildren(indinparent); + parent->RemoveChild(indinparent); + parent->FixIndicesOfChildren(indinparent); } }