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
This commit is contained in:
Vadim Zeitlin
1998-11-26 19:10:01 +00:00
parent 8a0a092b3c
commit ff5bf2590f
3 changed files with 43 additions and 8 deletions

View File

@@ -43,6 +43,9 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(TreeTest_Quit, MyFrame::OnQuit) EVT_MENU(TreeTest_Quit, MyFrame::OnQuit)
EVT_MENU(TreeTest_About, MyFrame::OnAbout) EVT_MENU(TreeTest_About, MyFrame::OnAbout)
EVT_MENU(TreeTest_Dump, MyFrame::OnDump) 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() END_EVENT_TABLE()
BEGIN_EVENT_TABLE(MyTreeCtrl, wxTreeCtrl) 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 // Make a menubar
wxMenu *file_menu = new wxMenu; 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->AppendSeparator();
file_menu->Append(TreeTest_About, "&About..."); file_menu->Append(TreeTest_About, "&About...");
file_menu->AppendSeparator(); file_menu->AppendSeparator();
@@ -157,10 +163,27 @@ void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
void MyFrame::OnDump(wxCommandEvent& WXUNUSED(event)) void MyFrame::OnDump(wxCommandEvent& WXUNUSED(event))
{ {
wxTreeItemId root=m_treeCtrl->GetSelection(); wxTreeItemId root = m_treeCtrl->GetSelection();
m_treeCtrl->GetItemsRecursively(root, -1); 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 implementation
MyTreeCtrl::MyTreeCtrl(wxWindow *parent, const wxWindowID id, MyTreeCtrl::MyTreeCtrl(wxWindow *parent, const wxWindowID id,
const wxPoint& pos, const wxSize& size, const wxPoint& pos, const wxSize& size,

View File

@@ -56,16 +56,16 @@ public:
void OnSelChanging(wxTreeEvent& event); void OnSelChanging(wxTreeEvent& event);
void OnKeyDown(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: private:
void AddItemsRecursively(const wxTreeItemId& idParent, void AddItemsRecursively(const wxTreeItemId& idParent,
size_t nChildren, size_t nChildren,
size_t depth, size_t depth,
size_t folder); size_t folder);
void AddTestItemsToTree(size_t numChildren,
size_t depth);
wxImageList *m_imageListNormal; wxImageList *m_imageListNormal;
@@ -84,6 +84,9 @@ public:
void OnQuit(wxCommandEvent& event); void OnQuit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event); void OnAbout(wxCommandEvent& event);
void OnDump(wxCommandEvent& event); void OnDump(wxCommandEvent& event);
void OnDelete(wxCommandEvent& event);
void OnDeleteAll(wxCommandEvent& event);
void OnRecreate(wxCommandEvent& event);
private: private:
MyTreeCtrl *m_treeCtrl; MyTreeCtrl *m_treeCtrl;
@@ -97,6 +100,9 @@ enum
TreeTest_Quit, TreeTest_Quit,
TreeTest_About, TreeTest_About,
TreeTest_Dump, TreeTest_Dump,
TreeTest_Delete,
TreeTest_DeleteAll,
TreeTest_Recreate,
TreeTest_Ctrl = 100 TreeTest_Ctrl = 100
}; };

View File

@@ -51,7 +51,7 @@ public:
int image, int selImage, int image, int selImage,
wxTreeItemData *data ); wxTreeItemData *data );
inline ~wxGenericTreeItem(); ~wxGenericTreeItem();
// trivial accessors // trivial accessors
wxArrayTreeItems& GetChildren() { return m_children; } wxArrayTreeItems& GetChildren() { return m_children; }
@@ -688,6 +688,12 @@ wxTreeItemId wxTreeCtrl::AppendItem(const wxTreeItemId& parentId,
void wxTreeCtrl::Delete(const wxTreeItemId& itemId) void wxTreeCtrl::Delete(const wxTreeItemId& itemId)
{ {
wxGenericTreeItem *item = itemId.m_pItem; wxGenericTreeItem *item = itemId.m_pItem;
wxGenericTreeItem *parent = item->GetParent();
if ( parent )
{
parent->GetChildren().Remove(item);
}
delete item; delete item;