1. added wxTreeCtrl::DeleteChildren()

2. wxTreeCtrl::CollapseAndReset() doesn't destroy the item itself any more
3. wxTreeCtrl::Delete() sends wxEVT_COMMAND_TREE_CHILDREN event (but
   DeleteChildren() and DeleteAll() do not!)
4. Sample modified to show the new function too
5. Micro redraw bug fixed (small vertical line was sometimes drawn when it
   shouldn't have been)


git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@1224 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin 1998-12-17 18:41:00 +00:00
parent 34b5942848
commit 372edb9d37
5 changed files with 62 additions and 17 deletions

View File

@ -370,7 +370,11 @@ public:
// delete this item and associated data if any
void Delete(const wxTreeItemId& item);
// delete all children (but don't delete the item itself)
// NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
void DeleteChildren(const wxTreeItemId& item);
// delete all items from the tree
// NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
void DeleteAllItems();
// expand this item

View File

@ -338,7 +338,11 @@ public:
// delete this item and associated data if any
void Delete(const wxTreeItemId& item);
// delete all children (but don't delete the item itself)
// NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
void DeleteChildren(const wxTreeItemId& item);
// delete all items from the tree
// NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
void DeleteAllItems();
// expand this item

View File

@ -46,6 +46,7 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(TreeTest_Bold, MyFrame::OnSetBold)
EVT_MENU(TreeTest_UnBold, MyFrame::OnClearBold)
EVT_MENU(TreeTest_Delete, MyFrame::OnDelete)
EVT_MENU(TreeTest_DeleteChildren, MyFrame::OnDeleteChildren)
EVT_MENU(TreeTest_DeleteAll, MyFrame::OnDeleteAll)
EVT_MENU(TreeTest_Recreate, MyFrame::OnRecreate)
END_EVENT_TABLE()
@ -100,10 +101,12 @@ MyFrame::MyFrame(const wxString& title, int x, int y, int w, int h)
wxMenu *file_menu = new wxMenu;
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_Delete, "&Delete this item");
file_menu->Append(TreeTest_DeleteChildren, "Delete &children");
file_menu->Append(TreeTest_DeleteAll, "Delete &all items");
file_menu->AppendSeparator();
file_menu->Append(TreeTest_Bold, "Make item &bold");
file_menu->Append(TreeTest_UnBold, "Make item &not bold");
file_menu->AppendSeparator();
@ -190,6 +193,12 @@ void MyFrame::OnDelete(wxCommandEvent& WXUNUSED(event))
m_treeCtrl->Delete(item);
}
void MyFrame::OnDeleteChildren(wxCommandEvent& WXUNUSED(event))
{
wxTreeItemId item = m_treeCtrl->GetSelection();
m_treeCtrl->DeleteChildren(item);
}
void MyFrame::OnDeleteAll(wxCommandEvent& WXUNUSED(event))
{
m_treeCtrl->DeleteAllItems();

View File

@ -86,6 +86,7 @@ public:
void OnAbout(wxCommandEvent& event);
void OnDump(wxCommandEvent& event);
void OnDelete(wxCommandEvent& event);
void OnDeleteChildren(wxCommandEvent& event);
void OnDeleteAll(wxCommandEvent& event);
void OnRecreate(wxCommandEvent& event);
@ -109,6 +110,7 @@ enum
TreeTest_Bold,
TreeTest_UnBold,
TreeTest_Delete,
TreeTest_DeleteChildren,
TreeTest_DeleteAll,
TreeTest_Recreate,
TreeTest_Ctrl = 100

View File

@ -81,6 +81,7 @@ public:
wxGenericTreeItem *GetParent() const { return m_parent; }
// operations
void DeleteChildren();
void Reset();
// get count of all children (and grand children if 'recursively')
@ -182,9 +183,16 @@ wxGenericTreeItem::~wxGenericTreeItem()
{
delete m_data;
DeleteChildren();
}
void wxGenericTreeItem::DeleteChildren()
{
size_t count = m_children.Count();
for ( size_t n = 0; n < count; n++ )
delete m_children[n];
m_children.Empty();
}
void wxGenericTreeItem::SetText( const wxString &text, wxDC& dc )
@ -207,7 +215,7 @@ void wxGenericTreeItem::Reset()
m_level = 0;
m_children.Empty();
DeleteChildren();
m_isCollapsed = TRUE;
m_parent = (wxGenericTreeItem *)NULL;
@ -690,11 +698,25 @@ wxTreeItemId wxTreeCtrl::AppendItem(const wxTreeItemId& parentId,
image, selImage, data);
}
void wxTreeCtrl::DeleteChildren(const wxTreeItemId& itemId)
{
wxGenericTreeItem *item = itemId.m_pItem;
item->DeleteChildren();
m_dirty = TRUE;
}
void wxTreeCtrl::Delete(const wxTreeItemId& itemId)
{
wxGenericTreeItem *item = itemId.m_pItem;
wxGenericTreeItem *parent = item->GetParent();
// notify the parent...
wxTreeEvent event( wxEVT_COMMAND_TREE_DELETE_ITEM, GetId() );
event.m_item = item;
event.SetEventObject( this );
ProcessEvent( event );
if ( parent )
{
parent->GetChildren().Remove(item);
@ -779,7 +801,7 @@ void wxTreeCtrl::Collapse(const wxTreeItemId& itemId)
void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
{
Collapse(item);
Delete(item);
DeleteChildren(item);
}
void wxTreeCtrl::Toggle(const wxTreeItemId& itemId)
@ -1061,22 +1083,26 @@ void wxTreeCtrl::PaintLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &
}
}
if ( !item->IsExpanded() )
return;
int semiOldY = y;
wxArrayTreeItems& children = item->GetChildren();
size_t count = children.Count();
for ( size_t n = 0; n < count; n++ )
if ( item->IsExpanded() )
{
y += m_lineHeight;
semiOldY = y;
int semiOldY = y;
PaintLevel( children[n], dc, level+1, y );
wxArrayTreeItems& children = item->GetChildren();
size_t count = children.Count();
for ( size_t n = 0; n < count; n++ )
{
y += m_lineHeight;
semiOldY = y;
PaintLevel( children[n], dc, level+1, y );
}
// it may happen that the item is expanded but has no items (when you
// delete all its children for example) - don't draw the vertical line
// in this case
if ( count > 0 )
dc.DrawLine( horizX+15, oldY+5, horizX+15, semiOldY );
}
dc.DrawLine( horizX+15, oldY+5, horizX+15, semiOldY );
}
// -----------------------------------------------------------------------------