]> git.saurik.com Git - wxWidgets.git/commitdiff
fixed crash in OnChar() after m_key_current was deleted
authorVadim Zeitlin <vadim@wxwidgets.org>
Tue, 14 Mar 2000 16:51:43 +0000 (16:51 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Tue, 14 Mar 2000 16:51:43 +0000 (16:51 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@6698 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

src/generic/treectrl.cpp

index 9a43e04af00e5e629dc76fe3ae4f552e9ba85fed..b4bf89804949141614b103ee0652a4b78563ae90 100644 (file)
@@ -1072,13 +1072,36 @@ void wxTreeCtrl::DeleteChildren(const wxTreeItemId& itemId)
 void wxTreeCtrl::Delete(const wxTreeItemId& itemId)
 {
     wxGenericTreeItem *item = itemId.m_pItem;
-    wxGenericTreeItem *parent = item->GetParent();
 
+    // don't stay with invalid m_key_current or we will crash in the next call
+    // to OnChar()
+    bool changeKeyCurrent = FALSE;
+    wxGenericTreeItem *itemKey = m_key_current;
+    while ( itemKey && !changeKeyCurrent )
+    {
+        if ( itemKey == item )
+        {
+            // m_key_current is a descendant of the item being deleted
+            changeKeyCurrent = TRUE;
+        }
+        else
+        {
+            itemKey = itemKey->GetParent();
+        }
+    }
+
+    wxGenericTreeItem *parent = item->GetParent();
     if ( parent )
     {
         parent->GetChildren().Remove( item );  // remove by value
     }
 
+    if ( changeKeyCurrent )
+    {
+        // may be NULL or not
+        m_key_current = parent;
+    }
+
     item->DeleteChildren(this);
     SendDeleteEvent(item);
     delete item;
@@ -1331,7 +1354,12 @@ void wxTreeCtrl::SelectItem(const wxTreeItemId& itemId,
     // shift press
     if (extended_select)
     {
-        if (m_current == NULL) m_current=m_key_current=GetRootItem().m_pItem;
+        if ( !m_current )
+        {
+            m_current =
+            m_key_current = GetRootItem().m_pItem;
+        }
+
         // don't change the mark (m_current)
         SelectItemRange(m_current, item);
     }
@@ -1951,7 +1979,6 @@ void wxTreeCtrl::OnChar( wxKeyEvent &event )
                 else
                 {
                     wxTreeItemId next = GetNextSibling( m_key_current );
-//                    if (next == 0)
                     if (!next)
                     {
                         wxTreeItemId current = m_key_current;
@@ -1961,7 +1988,6 @@ void wxTreeCtrl::OnChar( wxKeyEvent &event )
                             if (current) next = GetNextSibling( current );
                         }
                     }
-//                    if (next != 0)
                     if (next)
                     {
                         SelectItem( next, unselect_others, extended_select );