Add wxGenericTreeCtrl::DoGetNext(Next_Visible) function

This is similar but not totally identical to the existing
GetNextVisible() as it doesn't require the initial item itself to be
visible (notably, it could be the hidden virtual root).

It is also more efficient than the old GetNextVisible() implementation
calling GetNext() in the loop until finding a visible item, as it avoids
looking inside collapsed items in the first place.
This commit is contained in:
Vadim Zeitlin
2023-11-08 01:52:00 +01:00
parent 857fa3670e
commit 8ca1be38e2
2 changed files with 30 additions and 25 deletions
+7
View File
@@ -369,6 +369,13 @@ private:
// operation.
void ResetFindState();
// Find the next item, either looking inside the collapsed items or not.
enum
{
Next_Any = 0,
Next_Visible = 1
};
wxTreeItemId DoGetNext(const wxTreeItemId& item, int flags = 0) const;
// True if we're using custom colours/font, respectively, or false if we're
// using the default colours and should update them whenever system colours
+23 -25
View File
@@ -1491,29 +1491,36 @@ wxTreeItemId wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const
// Only for internal use right now, but should probably be public
wxTreeItemId wxGenericTreeCtrl::GetNext(const wxTreeItemId& item) const
{
return DoGetNext(item, Next_Any);
}
wxTreeItemId
wxGenericTreeCtrl::DoGetNext(const wxTreeItemId& item, int flags) const
{
wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
// First see if there are any children.
wxArrayGenericTreeItems& children = i->GetChildren();
if (children.GetCount() > 0)
if ( !(flags & Next_Visible) || i->IsExpanded() )
{
return children.Item(0);
}
else
{
// Try a sibling of this or ancestor instead
wxTreeItemId p = item;
wxTreeItemId toFind;
do
{
toFind = GetNextSibling(p);
p = GetItemParent(p);
} while (p.IsOk() && !toFind.IsOk());
return toFind;
wxArrayGenericTreeItems& children = i->GetChildren();
if (children.GetCount() > 0)
{
return children.Item(0);
}
}
// Try a sibling of this or ancestor instead
wxTreeItemId p = item;
wxTreeItemId toFind;
do
{
toFind = GetNextSibling(p);
p = GetItemParent(p);
} while (p.IsOk() && !toFind.IsOk());
return toFind;
}
wxTreeItemId wxGenericTreeCtrl::GetFirstVisibleItem() const
@@ -1537,16 +1544,7 @@ wxTreeItemId wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId& item) const
wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
wxASSERT_MSG( IsVisible(item), wxT("this item itself should be visible") );
wxTreeItemId id = item;
if (id.IsOk())
{
while (id = GetNext(id), id.IsOk())
{
if (IsVisible(id))
return id;
}
}
return wxTreeItemId();
return DoGetNext(item, Next_Visible);
}
wxTreeItemId wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const