From 54cfd3075de77d3ee243f6b3463ea07744effe5a Mon Sep 17 00:00:00 2001 From: Jinhao Date: Sun, 2 Dec 2018 06:44:11 +0800 Subject: [PATCH] add methods for listbox visible range --- include/nana/gui/widgets/listbox.hpp | 21 ++++++++++++ source/gui/widgets/listbox.cpp | 48 ++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/include/nana/gui/widgets/listbox.hpp b/include/nana/gui/widgets/listbox.hpp index ecdb9be8..d5bb0607 100644 --- a/include/nana/gui/widgets/listbox.hpp +++ b/include/nana/gui/widgets/listbox.hpp @@ -1555,6 +1555,27 @@ the nana::detail::basic_window member pointer scheme * @return the reference of *this. */ listbox& category_icon(const paint::image& img_expanded, const paint::image&& img_collapsed); + + /// Returns first visible element + /** + * It may return an item or a category item. + * @return the index of first visible element. + */ + index_pair first_visible() const; + + /// Returns last visible element + /** + * It may return an item or a category item. + * @return the index of last visible element. + */ + index_pair last_visible() const; + + /// Returns all visible items + /** + * It returns all visible items that are displayed in listbox window. + * @return index_pairs containing all visible items. + */ + index_pairs visibles() const; private: drawerbase::listbox::essence & _m_ess() const; nana::any* _m_anyobj(size_type cat, size_type index, bool allocate_if_empty) const override; diff --git a/source/gui/widgets/listbox.cpp b/source/gui/widgets/listbox.cpp index deb6d881..09c62a06 100644 --- a/source/gui/widgets/listbox.cpp +++ b/source/gui/widgets/listbox.cpp @@ -5989,6 +5989,54 @@ namespace nana return *this; } + auto listbox::first_visible() const ->index_pair + { + return _m_ess().first_display(); + } + + auto listbox::last_visible() const -> index_pair + { + return _m_ess().lister.advance(_m_ess().first_display(), _m_ess().count_of_exposed(true)); + } + + auto listbox::visibles() const -> index_pairs + { + index_pairs indexes; + + auto idx = _m_ess().first_display(); + + auto n = _m_ess().count_of_exposed(true); + while (n > 0) + { + if (idx.empty()) + break; + + if (idx.is_category()) + { + indexes.push_back(idx); + --n; + } + else + { + auto const count = (std::min)(_m_ess().lister.size_item(idx.cat) - idx.item, n); + for (std::size_t i = 0; i < count; ++i) + { + indexes.push_back(idx); + ++idx.item; + } + if (count) + { + n -= count; + --idx.item; + } + } + + idx = _m_ess().lister.advance(idx, 1); + } + + return indexes; + } + drawerbase::listbox::essence & listbox::_m_ess() const { return get_drawer_trigger().ess();