From ff5bf2590f02b5585c358b65eeeb6463e100a004 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 26 Nov 1998 19:10:01 +0000 Subject: [PATCH] wxTreeCtrl::Delete() bug corrected, sample expanded to test it git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@1068 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- samples/treectrl/treetest.cpp | 27 +++++++++++++++++++++++++-- samples/treectrl/treetest.h | 16 +++++++++++----- src/generic/treectrl.cpp | 8 +++++++- 3 files changed, 43 insertions(+), 8 deletions(-) diff --git a/samples/treectrl/treetest.cpp b/samples/treectrl/treetest.cpp index 068853eac0..5085521447 100644 --- a/samples/treectrl/treetest.cpp +++ b/samples/treectrl/treetest.cpp @@ -43,6 +43,9 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_MENU(TreeTest_Quit, MyFrame::OnQuit) EVT_MENU(TreeTest_About, MyFrame::OnAbout) EVT_MENU(TreeTest_Dump, MyFrame::OnDump) + EVT_MENU(TreeTest_Delete, MyFrame::OnDelete) + EVT_MENU(TreeTest_DeleteAll, MyFrame::OnDeleteAll) + EVT_MENU(TreeTest_Recreate, MyFrame::OnRecreate) END_EVENT_TABLE() BEGIN_EVENT_TABLE(MyTreeCtrl, wxTreeCtrl) @@ -93,7 +96,10 @@ MyFrame::MyFrame(const wxString& title, int x, int y, int w, int h) // Make a menubar wxMenu *file_menu = new wxMenu; - file_menu->Append(TreeTest_Dump, "&Dump tree items"); + file_menu->Append(TreeTest_Dump, "D&ump tree items"); + file_menu->Append(TreeTest_Delete, "&Delete this item"); + file_menu->Append(TreeTest_DeleteAll, "Delete &all items"); + file_menu->Append(TreeTest_Recreate, "&Recreate the tree"); file_menu->AppendSeparator(); file_menu->Append(TreeTest_About, "&About..."); file_menu->AppendSeparator(); @@ -157,10 +163,27 @@ void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) void MyFrame::OnDump(wxCommandEvent& WXUNUSED(event)) { - wxTreeItemId root=m_treeCtrl->GetSelection(); + wxTreeItemId root = m_treeCtrl->GetSelection(); m_treeCtrl->GetItemsRecursively(root, -1); } +void MyFrame::OnDelete(wxCommandEvent& WXUNUSED(event)) +{ + wxTreeItemId item = m_treeCtrl->GetSelection(); + m_treeCtrl->Delete(item); +} + +void MyFrame::OnDeleteAll(wxCommandEvent& WXUNUSED(event)) +{ + m_treeCtrl->DeleteAllItems(); +} + +void MyFrame::OnRecreate(wxCommandEvent& event) +{ + OnDeleteAll(event); + m_treeCtrl->AddTestItemsToTree(3, 2); +} + // MyTreeCtrl implementation MyTreeCtrl::MyTreeCtrl(wxWindow *parent, const wxWindowID id, const wxPoint& pos, const wxSize& size, diff --git a/samples/treectrl/treetest.h b/samples/treectrl/treetest.h index 68a4583b0a..7d125f5982 100644 --- a/samples/treectrl/treetest.h +++ b/samples/treectrl/treetest.h @@ -56,16 +56,16 @@ public: void OnSelChanging(wxTreeEvent& event); void OnKeyDown(wxTreeEvent& event); - void GetItemsRecursively(const wxTreeItemId& idParent, long cookie); + void GetItemsRecursively(const wxTreeItemId& idParent, long cookie); + + void AddTestItemsToTree(size_t numChildren, + size_t depth); private: void AddItemsRecursively(const wxTreeItemId& idParent, size_t nChildren, size_t depth, - size_t folder); - - void AddTestItemsToTree(size_t numChildren, - size_t depth); + size_t folder); wxImageList *m_imageListNormal; @@ -84,6 +84,9 @@ public: void OnQuit(wxCommandEvent& event); void OnAbout(wxCommandEvent& event); void OnDump(wxCommandEvent& event); + void OnDelete(wxCommandEvent& event); + void OnDeleteAll(wxCommandEvent& event); + void OnRecreate(wxCommandEvent& event); private: MyTreeCtrl *m_treeCtrl; @@ -97,6 +100,9 @@ enum TreeTest_Quit, TreeTest_About, TreeTest_Dump, + TreeTest_Delete, + TreeTest_DeleteAll, + TreeTest_Recreate, TreeTest_Ctrl = 100 }; diff --git a/src/generic/treectrl.cpp b/src/generic/treectrl.cpp index c214bbe26a..e6f33a0259 100644 --- a/src/generic/treectrl.cpp +++ b/src/generic/treectrl.cpp @@ -51,7 +51,7 @@ public: int image, int selImage, wxTreeItemData *data ); - inline ~wxGenericTreeItem(); + ~wxGenericTreeItem(); // trivial accessors wxArrayTreeItems& GetChildren() { return m_children; } @@ -688,6 +688,12 @@ wxTreeItemId wxTreeCtrl::AppendItem(const wxTreeItemId& parentId, void wxTreeCtrl::Delete(const wxTreeItemId& itemId) { wxGenericTreeItem *item = itemId.m_pItem; + wxGenericTreeItem *parent = item->GetParent(); + + if ( parent ) + { + parent->GetChildren().Remove(item); + } delete item;