]> git.tdb.fi Git - libs/gltk.git/commitdiff
Add a persistent view size attribute to List
authorMikko Rasa <tdb@tdb.fi>
Wed, 26 Jun 2013 11:24:08 +0000 (14:24 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 26 Jun 2013 11:24:08 +0000 (14:24 +0300)
source/dropdown.cpp
source/list.cpp
source/list.h

index ddf5c9268500479655796caecf9bad0a1ec71e20..ce0ba71f642c45ca7f672e4bf8e80f78d8fb60ed 100644 (file)
@@ -28,6 +28,7 @@ void Dropdown::init()
        dropped = false;
 
        add(list);
+       list.set_view_all();
        list.signal_item_selected.connect(sigc::mem_fun(this, &Dropdown::list_item_selected));
        list.signal_autosize_changed.connect(sigc::mem_fun(this, &Dropdown::list_autosize_changed));
 }
@@ -38,7 +39,7 @@ void Dropdown::autosize()
                return;
 
        Widget::autosize();
-       list.autosize_all();
+       list.autosize();
        geom.w = max(geom.w, list.get_geometry().w);
 
        if(const Part *text_part = style->get_part("text"))
@@ -123,7 +124,7 @@ void Dropdown::list_autosize_changed()
 
 void Dropdown::resize_list()
 {
-       list.autosize_all();
+       list.autosize();
        Geometry lgeom = list.get_geometry();
        lgeom.x = 0;
        lgeom.y = -lgeom.h;
index 710d1fc6fa7cf7c05e2fe88e5c6349aa018cf298..c74af90d4541cb2185f2afc1f0d7a1bff8f6c172 100644 (file)
@@ -31,6 +31,7 @@ void List::init()
        sel_index = -1;
        first = 0;
        max_scroll = 0;
+       view_size = 5;
 
        observer = new DataObserver(*this);
 
@@ -47,11 +48,6 @@ List::~List()
 }
 
 void List::autosize()
-{
-       autosize_rows(5);
-}
-
-void List::autosize_rows(unsigned n)
 {
        if(!style)
                return;
@@ -69,12 +65,12 @@ void List::autosize_rows(unsigned n)
                        items[i]->autosize();
                        const Geometry &igeom = items[i]->get_geometry();
                        max_w = max(max_w, igeom.w);
-                       if(i<n)
+                       if(view_size==0 || i<view_size)
                                total_h += igeom.h;
                }
 
-               if(!items.empty() && items.size()<n)
-                       total_h = total_h*n/items.size();
+               if(!items.empty() && items.size()<view_size)
+                       total_h = total_h*view_size/items.size();
 
                geom.w = max(geom.w, max_w+margin.left+margin.right);
                geom.h = max(geom.h, total_h+margin.top+margin.bottom);
@@ -84,11 +80,6 @@ void List::autosize_rows(unsigned n)
        rebuild();
 }
 
-void List::autosize_all()
-{
-       autosize_rows(data->size());
-}
-
 void List::set_data(ListData &d)
 {
        delete observer;
@@ -114,6 +105,17 @@ List::Item *List::create_item(unsigned index)
        return new BasicItem(data->get_string(index));
 }
 
+void List::set_view_size(unsigned s)
+{
+       view_size = s;
+       signal_autosize_changed.emit();
+}
+
+void List::set_view_all()
+{
+       set_view_size(0);
+}
+
 void List::set_selected_index(int i)
 {
        if(i>static_cast<int>(data->size()))
index 561e388c77a25cd114abc8a4f3471ba79674b8ff..dd44f3b57738e9bd2dbf19cfc87592d0d9bf6b67 100644 (file)
@@ -76,6 +76,7 @@ private:
        int sel_index;
        unsigned first;
        unsigned max_scroll;
+       unsigned view_size;
 
        VSlider slider;
        std::vector<Item *> items;
@@ -91,8 +92,6 @@ public:
        virtual const char *get_class() const { return "list"; }
 
        virtual void autosize();
-       void autosize_rows(unsigned);
-       void autosize_all();
 
        void set_data(ListData &);
        ListData &get_data() { return *data; }
@@ -101,7 +100,10 @@ private:
        void items_changed();
 protected:
        virtual Item *create_item(unsigned);
+
 public:
+       void set_view_size(unsigned);
+       void set_view_all();
 
        void set_selected_index(int);
        int get_selected_index() const { return sel_index; }