]> git.tdb.fi Git - libs/gltk.git/commitdiff
Add a level of indirection to decouple list rows from items
authorMikko Rasa <tdb@tdb.fi>
Thu, 19 Sep 2019 21:01:53 +0000 (00:01 +0300)
committerMikko Rasa <tdb@tdb.fi>
Thu, 19 Sep 2019 21:18:35 +0000 (00:18 +0300)
This is required to support different kinds of views like grid and
possibly tree.

source/list.cpp
source/list.h

index 5d765008765c9ccbece8c28e77c9a62da5a4cecb..a5285832b98994f5f9cc4365d41c28c5b37b1176 100644 (file)
@@ -41,9 +41,9 @@ void List::init()
        item_factory = 0;
        sel_index = -1;
        focus_index = -1;
-       first = 0;
+       first_row = 0;
        max_scroll = 0;
-       view_size = 5;
+       view_rows = 5;
        items_part = 0;
        ignore_slider_change = false;
        dragging = false;
@@ -81,7 +81,7 @@ void List::autosize_special(const Part &part, Geometry &ageom) const
                        max_h = max(max_h, igeom.h);
                }
 
-               unsigned total_h = max_h*(view_size==0 ? items.size() : view_size);
+               unsigned total_h = max_h*(view_rows==0 ? items.size() : view_rows);
 
                ageom.w = max(ageom.w, max_w+margin.left+margin.right);
                ageom.h = max(ageom.h, total_h+margin.top+margin.bottom);
@@ -137,9 +137,9 @@ List::Item *List::create_item(unsigned index)
        return item;
 }
 
-void List::set_view_size(unsigned s)
+void List::set_view_size(unsigned r)
 {
-       view_size = s;
+       view_rows = r;
        signal_autosize_changed.emit();
 }
 
@@ -178,7 +178,7 @@ void List::set_selected_index(int i)
 
 void List::set_selected_item(Widget *item)
 {
-       for(unsigned i=first; (i<items.size() && items[i]->is_visible()); ++i)
+       for(unsigned i=rows[first_row].first; (i<items.size() && items[i]->is_visible()); ++i)
                if(item==items[i])
                        return set_selected_index(i);
 }
@@ -190,34 +190,12 @@ void List::rebuild_special(const Part &part)
        else if(part.get_name()=="items")
        {
                SetFlag flag(ignore_slider_change);
+               reposition_items(true);
+               unsigned old_first_row = first_row;
+               unsigned old_max_scroll = max_scroll;
                check_view_range();
-
-               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;
-                               }
-                       }
-               }
+               if(first_row!=old_first_row || max_scroll!=old_max_scroll)
+                       reposition_items(false);
        }
 
        Widget::rebuild_special(part);
@@ -227,7 +205,7 @@ void List::render_special(const Part &part, GL::Renderer &renderer) const
 {
        if(part.get_name()=="items")
        {
-               for(unsigned i=first; (i<items.size() && items[i]->is_visible()); ++i)
+               for(unsigned i=rows[first_row].first; (i<items.size() && items[i]->is_visible()); ++i)
                        items[i]->render(renderer);
        }
        else if(part.get_name()=="slider")
@@ -253,13 +231,13 @@ void List::button_press(int x, int y, unsigned btn)
                unsigned change = 3;
                if(btn==4)
                {
-                       change = min(first, change);
-                       slider.set_value(max_scroll-(first-change));
+                       change = min(first_row, change);
+                       slider.set_value(max_scroll-(first_row-change));
                }
                else if(btn==5)
                {
-                       change = min(max_scroll-first, change);
-                       slider.set_value(max_scroll-(first+change));
+                       change = min(max_scroll-first_row, change);
+                       slider.set_value(max_scroll-(first_row+change));
                }
        }
        else
@@ -303,22 +281,22 @@ void List::touch_motion(int, int y, unsigned finger)
        if(finger==0 && !items.empty() && dragging)
        {
                int dy = y-drag_start_y;
-               if(dy>0 && first<max_scroll)
+               if(dy>0 && first_row<max_scroll)
                {
-                       int item_h = items[first]->get_geometry().h;
-                       if(dy>item_h)
+                       int row_h = rows[first_row].height;
+                       if(dy>row_h)
                        {
-                               drag_start_y += item_h;
-                               slider.set_value(max_scroll-(first+1));
+                               drag_start_y += row_h;
+                               slider.set_value(max_scroll-(first_row+1));
                        }
                }
-               else if(dy<0 && first>0)
+               else if(dy<0 && first_row>0)
                {
-                       int item_h = items[first-1]->get_geometry().h;
-                       if(-dy>item_h)
+                       int row_h = rows[first_row-1].height;
+                       if(-dy>row_h)
                        {
-                               drag_start_y -= item_h;
-                               slider.set_value(max_scroll-(first-1));
+                               drag_start_y -= row_h;
+                               slider.set_value(max_scroll-(first_row-1));
                        }
                }
        }
@@ -334,7 +312,7 @@ void List::focus_in()
                if(sel_index>=0 && items[sel_index]->is_visible())
                        set_focus_index(sel_index);
                else if(!items.empty())
-                       set_focus_index(first);
+                       set_focus_index(rows[first_row].first);
        }
 }
 
@@ -390,7 +368,59 @@ void List::item_autosize_changed(Item *item)
        rebuild();
 }
 
-unsigned List::last_to_first(unsigned last) const
+void List::reposition_items(bool record_rows)
+{
+       if(!items_part)
+               return;
+
+       if(record_rows)
+       {
+               rows.clear();
+               rows.push_back(0);
+       }
+
+       const Sides &margin = items_part->get_margin();
+       unsigned view_w = geom.w-min(geom.w, margin.left+margin.right);
+       unsigned y = 0;
+       unsigned row_h = 0;
+       for(unsigned i=0; i<items.size(); ++i)
+       {
+               const Geometry &igeom = items[i]->get_geometry();
+
+               if(y)
+                       y -= row_h;
+               if(record_rows && i>0)
+               {
+                       rows.back().height = row_h;
+                       rows.push_back(i);
+               }
+               row_h = 0;
+
+               if(first_row<rows.size() && i==rows[first_row].first)
+                       y = geom.h-min(geom.h, margin.top);
+
+               if(!y)
+                       items[i]->set_visible(false);
+               else if(igeom.h+margin.bottom<=y)
+               {
+                       items[i]->set_visible(true);
+                       items[i]->set_geometry(Geometry(margin.left, y-igeom.h, view_w, igeom.h));
+               }
+               else
+               {
+                       for(unsigned j=rows.back().first; j<=i; ++j)
+                               items[j]->set_visible(false);
+                       y = 0;
+               }
+
+               row_h = max(row_h, igeom.h);
+       }
+
+       if(record_rows)
+               rows.back().height = row_h;
+}
+
+unsigned List::last_to_first_row(unsigned last) const
 {
        if(!items_part)
                return last;
@@ -399,9 +429,9 @@ unsigned List::last_to_first(unsigned last) const
        unsigned view_h = geom.h-min(geom.h, margin.top+margin.bottom);
 
        unsigned items_h = 0;
-       for(unsigned i=last; i<items.size(); --i)
+       for(unsigned i=last; i<rows.size(); --i)
        {
-               items_h += items[i]->get_geometry().h;
+               items_h += rows[i].height;
                if(items_h>view_h)
                        return min(i+1, last);
        }
@@ -409,6 +439,14 @@ unsigned List::last_to_first(unsigned last) const
        return 0;
 }
 
+unsigned List::item_index_to_row(unsigned index) const
+{
+       for(unsigned i=0; i+1<rows.size(); ++i)
+               if(rows[i+1].first>index)
+                       return i;
+       return rows.size()-1;
+}
+
 void List::check_view_range()
 {
        if(!style)
@@ -417,13 +455,13 @@ void List::check_view_range()
        if(items.empty())
                max_scroll = 0;
        else
-               max_scroll = last_to_first(items.size()-1);
+               max_scroll = last_to_first_row(rows.size()-1);
 
-       if(first>max_scroll)
-               first = max_scroll;
+       if(first_row>max_scroll)
+               first_row = max_scroll;
 
        slider.set_range(0, max_scroll);
-       slider.set_value(max_scroll-first);
+       slider.set_value(max_scroll-first_row);
 }
 
 void List::scroll_to_focus()
@@ -431,17 +469,18 @@ void List::scroll_to_focus()
        if(focus_index<0 || items[focus_index]->is_visible())
                return;
 
-       if(static_cast<unsigned>(focus_index)<first)
-               slider.set_value(max_scroll-focus_index);
+       unsigned focus_row = item_index_to_row(static_cast<unsigned>(focus_index));
+       if(focus_row<first_row)
+               slider.set_value(max_scroll-focus_row);
        else
-               slider.set_value(max_scroll-last_to_first(focus_index));
+               slider.set_value(max_scroll-last_to_first_row(focus_row));
 }
 
 void List::slider_value_changed(double value)
 {
        if(max_scroll>0 && !ignore_slider_change)
        {
-               first = max_scroll-static_cast<unsigned>(value);
+               first_row = max_scroll-static_cast<unsigned>(value);
                rebuild();
        }
 }
@@ -632,7 +671,7 @@ List::Loader::Loader(List &l):
        DataFile::DerivedObjectLoader<List, Widget::Loader>(l)
 {
        add("item", &Loader::item);
-       add("view_size", &List::view_size);
+       add("view_size", &List::view_rows);
 }
 
 void List::Loader::item(const string &v)
index 43a24576a1ecf1904d29d1a58eb104c61a790d00..ef4311e0702c73b818a19ab3abaadb07e1427abb 100644 (file)
@@ -131,6 +131,14 @@ private:
                }
        };
 
+       struct Row
+       {
+               unsigned first;
+               unsigned height;
+
+               Row(unsigned f): first(f), height(0) { }
+       };
+
 public:
        sigc::signal<void, unsigned> signal_item_selected;
        sigc::signal<void> signal_selection_cleared;
@@ -142,9 +150,9 @@ private:
        ItemFactory *item_factory;
        int sel_index;
        int focus_index;
-       unsigned first;
+       unsigned first_row;
        unsigned max_scroll;
-       unsigned view_size;
+       unsigned view_rows;
        const Part *items_part;
        bool ignore_slider_change;
        bool dragging;
@@ -153,6 +161,7 @@ private:
 
        VSlider slider;
        std::vector<Item *> items;
+       std::vector<Row> rows;
 
 public:
        List();
@@ -212,7 +221,9 @@ private:
        void set_focus_index(int);
 
        void item_autosize_changed(Item *);
-       unsigned last_to_first(unsigned) const;
+       void reposition_items(bool);
+       unsigned last_to_first_row(unsigned) const;
+       unsigned item_index_to_row(unsigned) const;
        void check_view_range();
        void scroll_to_focus();
        void slider_value_changed(double);