]> git.tdb.fi Git - libs/gltk.git/commitdiff
Refactor list item positioning code
authorMikko Rasa <tdb@tdb.fi>
Wed, 20 Nov 2013 17:04:55 +0000 (19:04 +0200)
committerMikko Rasa <tdb@tdb.fi>
Wed, 20 Nov 2013 17:04:55 +0000 (19:04 +0200)
This fixes a bug where scrolling didn't quite work properly until each
item had been visible at least once.

source/list.cpp
source/list.h

index 042eb20ab7b85dbd052d3274351b03287c09614e..87b27a4dc641988b5438b9799500264da7d4fbd6 100644 (file)
@@ -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; i<items.size(); ++i)
+               {
+                       if(i<first || !y)
+                               items[i]->set_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; i<items.size(); ++i)
-               {
-                       if(i<first || !y)
-                               items[i]->set_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<unsigned>(value);
-               reposition_items();
+               rebuild();
        }
 }
 
index 9805176f188da400610f35d17b68546ed6cd27ff..5b43661668d49ffdabe72f99ccd6d8d906395b7b 100644 (file)
@@ -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);
 };