From 8ca1be38e24d7d48e6cc6e33418058e8adad3016 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 8 Nov 2023 01:52:00 +0100 Subject: [PATCH] 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. --- include/wx/generic/treectlg.h | 7 +++++ src/generic/treectlg.cpp | 48 +++++++++++++++++------------------ 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/include/wx/generic/treectlg.h b/include/wx/generic/treectlg.h index e431299f88..e2913b0338 100644 --- a/include/wx/generic/treectlg.h +++ b/include/wx/generic/treectlg.h @@ -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 diff --git a/src/generic/treectlg.cpp b/src/generic/treectlg.cpp index 4789e2314c..49c7e25e83 100644 --- a/src/generic/treectlg.cpp +++ b/src/generic/treectlg.cpp @@ -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