From 3e5232d9bd88e95f06db958bdcac4eaf738a8223 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 20 Nov 2013 19:04:55 +0200 Subject: [PATCH] Refactor list item positioning code This fixes a bug where scrolling didn't quite work properly until each item had been visible at least once. --- source/list.cpp | 98 +++++++++++++++++++------------------------------ source/list.h | 7 +--- 2 files changed, 40 insertions(+), 65 deletions(-) diff --git a/source/list.cpp b/source/list.cpp index 042eb20..87b27a4 100644 --- a/source/list.cpp +++ b/source/list.cpp @@ -110,9 +110,8 @@ void List::set_data(ListData &d) void List::items_changed() { - check_view_range(); signal_autosize_changed.emit(); - reposition_items(); + rebuild(); } List::Item *List::create_item(unsigned index) @@ -122,8 +121,8 @@ List::Item *List::create_item(unsigned index) item = item_factory->create_item(index); else item = new BasicItem(data->get_string(index)); + item->signal_autosize_changed.connect(sigc::bind(sigc::mem_fun(this, &List::item_autosize_changed), item)); add(*item); - item->signal_autosize_changed.connect(sigc::mem_fun(this, &List::item_autosize_changed)); return item; } @@ -162,6 +161,38 @@ void List::rebuild_special(const Part &part) { if(part.get_name()=="slider") reposition_child(slider, part); + else if(part.get_name()=="items") + { + const Sides &margin = part.get_margin(); + unsigned w = geom.w-min(geom.w, margin.left+margin.right); + unsigned y = geom.h-min(geom.h, margin.top); + for(unsigned i=0; iset_visible(false); + else + { + Geometry igeom = items[i]->get_geometry(); + if(igeom.h+margin.bottom<=y) + { + items[i]->set_visible(true); + y -= igeom.h; + igeom.x = margin.left; + igeom.y = y; + igeom.w = w; + items[i]->set_geometry(igeom); + } + else + { + items[i]->set_visible(false); + y = 0; + } + } + } + + check_view_range(); + } + Widget::rebuild_special(part); } @@ -190,64 +221,11 @@ void List::button_press(int x, int y, unsigned btn) } } -void List::on_geometry_change() -{ - reposition_items(); - - check_view_range(); -} - -void List::on_style_change() -{ - if(!style) - return; - - reposition_items(); - - check_view_range(); -} - -void List::item_autosize_changed() +void List::item_autosize_changed(Item *item) { + item->autosize(); signal_autosize_changed.emit(); - reposition_items(); -} - -void List::reposition_items() -{ - if(!style) - return; - - if(const Part *items_part = style->get_part("items")) - { - const Sides &margin = items_part->get_margin(); - unsigned w = geom.w-min(geom.w, margin.left+margin.right); - unsigned y = geom.h-min(geom.h, margin.top); - for(unsigned i=0; iset_visible(false); - else - { - Geometry igeom; - items[i]->autosize(igeom); - if(igeom.h+margin.bottom<=y) - { - items[i]->set_visible(true); - y -= igeom.h; - igeom.x = margin.left; - igeom.y = y; - igeom.w = w; - items[i]->set_geometry(igeom); - } - else - { - items[i]->set_visible(false); - y = 0; - } - } - } - } + rebuild(); } void List::check_view_range() @@ -285,7 +263,7 @@ void List::slider_value_changed(double value) if(max_scroll>0) { first = max_scroll-static_cast(value); - reposition_items(); + rebuild(); } } diff --git a/source/list.h b/source/list.h index 9805176..5b43661 100644 --- a/source/list.h +++ b/source/list.h @@ -188,12 +188,9 @@ private: public: virtual void button_press(int, int, unsigned); -private: - virtual void on_geometry_change(); - virtual void on_style_change(); - void item_autosize_changed(); - void reposition_items(); +private: + void item_autosize_changed(Item *); void check_view_range(); void slider_value_changed(double); }; -- 2.45.2