From: Mikko Rasa Date: Wed, 31 Aug 2016 08:17:33 +0000 (+0300) Subject: Refactor the traversal logic out of List::check_view_range X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=db2a069cd5ec4c8809c2ba6ddeec2ddcd32d49ef;p=libs%2Fgltk.git Refactor the traversal logic out of List::check_view_range It'll be useful for other things as well. --- diff --git a/source/list.cpp b/source/list.cpp index 0f887f3..b971342 100644 --- a/source/list.cpp +++ b/source/list.cpp @@ -254,29 +254,39 @@ void List::item_autosize_changed(Item *item) rebuild(); } -void List::check_view_range() +unsigned List::last_to_first(unsigned last) const { if(!style) - return; + return last; - unsigned h = geom.h; + unsigned view_h = geom.h; if(const Part *items_part = style->get_part("items")) { const Sides &margin = items_part->get_margin(); - h -= margin.top+margin.bottom; + view_h -= margin.top+margin.bottom; } - max_scroll = items.size(); - for(unsigned i=items.size(); i-->0; ) + unsigned items_h = 0; + for(unsigned i=last; iget_geometry().h; - if(ih<=h) - { - h -= ih; - --max_scroll; - } + items_h += items[i]->get_geometry().h; + if(items_h>view_h) + return min(i+1, last); } + return 0; +} + +void List::check_view_range() +{ + if(!style) + return; + + if(items.empty()) + max_scroll = 0; + else + max_scroll = last_to_first(items.size()-1); + if(first>max_scroll) first = max_scroll; diff --git a/source/list.h b/source/list.h index 246a729..ad12786 100644 --- a/source/list.h +++ b/source/list.h @@ -193,6 +193,7 @@ public: private: void item_autosize_changed(Item *); + unsigned last_to_first(unsigned) const; void check_view_range(); void slider_value_changed(double); };