# HG changeset patch # User Peter Nelson # Date 2024-02-26 17:19:12 # Node ID 57cd1bc354dec0e0de52bec3928dab0ac7344266 # Parent 565dadbd1dcbc47efba9ea206842080643b49b78 Codechange: Add `GetVisibleRangeIterators()` to `Scrollbar`. diff --git a/src/widget_type.h b/src/widget_type.h --- a/src/widget_type.h +++ b/src/widget_type.h @@ -834,6 +834,20 @@ public: int GetScrolledRowFromWidget(int clickpos, const Window * const w, WidgetID widget, int padding = 0, int line_height = -1) const; /** + * Get a pair of iterators for the range of visible elements in a container. + * @param container Container of elements represented by the scrollbar. + * @returns Pair of iterators of visible elements. + */ + template + auto GetVisibleRangeIterators(Tcontainer &container) const + { + assert(this->GetCount() == container.size()); // Scrollbar and container size must match. + auto first = std::next(std::begin(container), this->GetPosition()); + auto last = std::next(first, std::min(this->GetCapacity(), this->GetCount() - this->GetPosition())); + return std::make_pair(first, last); + } + + /** * Return an iterator pointing to the element of a scrolled widget that a user clicked in. * @param container Container of elements represented by the scrollbar. * @param clickpos Vertical position of the mouse click (without taking scrolling into account).