fixed crash when calling Delete(root) and also when calling Delete(descendant_of_selected_item)

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@16714 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2002-08-23 18:05:58 +00:00
parent 76432737a5
commit 7475e8a313

View File

@@ -322,6 +322,23 @@ static void EventFlagsToSelType(long style,
unselect_others = !(extended_select || (ctrlDown && is_multiple)); unselect_others = !(extended_select || (ctrlDown && is_multiple));
} }
// check if the given item is under another one
static bool IsDescendantOf(wxGenericTreeItem *parent, wxGenericTreeItem *item)
{
while ( item )
{
if ( item == parent )
{
// item is a descendant of parent
return TRUE;
}
item = item->GetParent();
}
return FALSE;
}
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// wxTreeRenameTimer (internal) // wxTreeRenameTimer (internal)
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
@@ -1416,33 +1433,31 @@ void wxGenericTreeCtrl::Delete(const wxTreeItemId& itemId)
wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem; wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
// don't stay with invalid m_key_current or we will crash in wxGenericTreeItem *parent = item->GetParent();
// the next call to OnChar()
bool changeKeyCurrent = FALSE; // don't keep stale pointers around!
wxGenericTreeItem *itemKey = m_key_current; if ( IsDescendantOf(item, m_key_current) )
while ( itemKey )
{ {
if ( itemKey == item ) m_key_current = parent;
{
// m_key_current is a descendant of the item being deleted
changeKeyCurrent = TRUE;
break;
}
itemKey = itemKey->GetParent();
} }
wxGenericTreeItem *parent = item->GetParent(); if ( IsDescendantOf(item, m_current) )
{
m_current = parent;
}
// remove the item from the tree
if ( parent ) if ( parent )
{ {
parent->GetChildren().Remove( item ); // remove by value parent->GetChildren().Remove( item ); // remove by value
} }
else // deleting the root
if ( changeKeyCurrent )
{ {
// may be NULL or not // nothing will be left in the tree
m_key_current = parent; m_anchor = NULL;
} }
// and delete all of its children and the item itself now
item->DeleteChildren(this); item->DeleteChildren(this);
SendDeleteEvent(item); SendDeleteEvent(item);
delete item; delete item;
@@ -1452,12 +1467,7 @@ void wxGenericTreeCtrl::DeleteAllItems()
{ {
if ( m_anchor ) if ( m_anchor )
{ {
m_dirty = TRUE; Delete(m_anchor);
m_anchor->DeleteChildren(this);
delete m_anchor;
m_anchor = NULL;
} }
} }