Add wxDataViewCtrl::ExpandChildren()

This convenient method allows to expand all children of the item
recursively.

This is directly supported by both native implementations, so it makes
sense to have this in wxDataViewCtrl itself, even if it could be also
(less efficiently) implemented in the user code.
This commit is contained in:
Vadim Zeitlin
2020-12-05 14:41:09 +01:00
parent faffc5fe0a
commit d47fa718cd
15 changed files with 87 additions and 20 deletions

View File

@@ -874,7 +874,7 @@ public:
void HitTest( const wxPoint & point, wxDataViewItem & item, wxDataViewColumn* &column );
wxRect GetItemRect( const wxDataViewItem & item, const wxDataViewColumn* column );
void Expand( unsigned int row );
void Expand( unsigned int row, bool expandChildren = false );
void Collapse( unsigned int row );
bool IsExpanded( unsigned int row ) const;
bool HasChildren( unsigned int row ) const;
@@ -990,6 +990,9 @@ private:
return NULL;
}
// Helper of public Expand(), must be called with a valid node.
void DoExpand(wxDataViewTreeNode* node, unsigned int row, bool expandChildren);
private:
wxDataViewCtrl *m_owner;
int m_lineHeight;
@@ -3907,7 +3910,7 @@ bool wxDataViewMainWindow::HasChildren( unsigned int row ) const
return true;
}
void wxDataViewMainWindow::Expand( unsigned int row )
void wxDataViewMainWindow::Expand( unsigned int row, bool expandChildren )
{
if (IsList())
return;
@@ -3916,6 +3919,14 @@ void wxDataViewMainWindow::Expand( unsigned int row )
if (!node)
return;
return DoExpand(node, row, expandChildren);
}
void
wxDataViewMainWindow::DoExpand(wxDataViewTreeNode* node,
unsigned int row,
bool expandChildren)
{
if (!node->HasChildren())
return;
@@ -3961,6 +3972,28 @@ void wxDataViewMainWindow::Expand( unsigned int row )
// Send the expanded event
SendExpanderEvent(wxEVT_DATAVIEW_ITEM_EXPANDED,node->GetItem());
}
// Note that we have to expand the children when expanding recursively even
// when this node itself was already open.
if ( expandChildren )
{
const wxDataViewTreeNodes& children = node->GetChildNodes();
for ( wxDataViewTreeNodes::const_iterator i = children.begin();
i != children.end();
++i )
{
wxDataViewTreeNode* const child = *i;
// Row currently corresponds to the previous item, so increment it
// first to correspond to this child.
DoExpand(child, ++row, true);
// We don't need +1 here because we'll increment the row during the
// next loop iteration.
row += child->GetSubTreeCount();
}
}
}
void wxDataViewMainWindow::Collapse(unsigned int row)
@@ -6309,11 +6342,11 @@ int wxDataViewCtrl::GetRowByItem( const wxDataViewItem & item ) const
return m_clientArea->GetRowByItem( item );
}
void wxDataViewCtrl::DoExpand( const wxDataViewItem & item )
void wxDataViewCtrl::DoExpand( const wxDataViewItem & item, bool expandChildren )
{
int row = m_clientArea->GetRowByItem( item );
if (row != -1)
m_clientArea->Expand(row);
m_clientArea->Expand(row, expandChildren);
}
void wxDataViewCtrl::Collapse( const wxDataViewItem & item )