]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/list.cpp
Cache the items part in List
[libs/gltk.git] / source / list.cpp
index 5d179e195c36c412785a17ec7d731f65856f420d..5d765008765c9ccbece8c28e77c9a62da5a4cecb 100644 (file)
@@ -44,6 +44,7 @@ void List::init()
        first = 0;
        max_scroll = 0;
        view_size = 5;
+       items_part = 0;
        ignore_slider_change = false;
        dragging = false;
        drag_start_x = 0;
@@ -71,18 +72,16 @@ void List::autosize_special(const Part &part, Geometry &ageom) const
                const Sides &margin = part.get_margin();
 
                unsigned max_w = 0;
-               unsigned total_h = 0;
+               unsigned max_h = 0;
                for(unsigned i=0; i<items.size(); ++i)
                {
                        Geometry igeom;
                        items[i]->autosize(igeom);
                        max_w = max(max_w, igeom.w);
-                       if(view_size==0 || i<view_size)
-                               total_h += igeom.h;
+                       max_h = max(max_h, igeom.h);
                }
 
-               if(!items.empty() && items.size()<view_size)
-                       total_h = total_h*view_size/items.size();
+               unsigned total_h = max_h*(view_size==0 ? items.size() : view_size);
 
                ageom.w = max(ageom.w, max_w+margin.left+margin.right);
                ageom.h = max(ageom.h, total_h+margin.top+margin.bottom);
@@ -351,6 +350,11 @@ bool List::navigate(Navigation nav)
        return true;
 }
 
+void List::on_style_change()
+{
+       items_part = (style ? style->get_part("items") : 0);
+}
+
 void List::move_focus(Navigation nav, bool select)
 {
        if(nav==NAV_UP)
@@ -388,15 +392,11 @@ void List::item_autosize_changed(Item *item)
 
 unsigned List::last_to_first(unsigned last) const
 {
-       if(!style)
+       if(!items_part)
                return last;
 
-       unsigned view_h = geom.h;
-       if(const Part *items_part = style->get_part("items"))
-       {
-               const Sides &margin = items_part->get_margin();
-               view_h -= margin.top+margin.bottom;
-       }
+       const Sides &margin = items_part->get_margin();
+       unsigned view_h = geom.h-min(geom.h, margin.top+margin.bottom);
 
        unsigned items_h = 0;
        for(unsigned i=last; i<items.size(); --i)
@@ -540,6 +540,21 @@ void List::Item::render_special(const Part &part, GL::Renderer &renderer) const
 }
 
 
+void List::SimpleItem::on_style_change()
+{
+       if(!style || children.empty())
+               return;
+
+       Widget *child = children.front()->widget;
+       child->autosize();
+       if(const Part *part = style->get_part("children"))
+       {
+               const Sides &margin = part->get_margin();
+               child->set_position(margin.left, margin.bottom);
+       }
+}
+
+
 void List::MultiColumnItem::check_widths(vector<unsigned> &widths) const
 {
        if(widths.size()<children.size())
@@ -612,19 +627,6 @@ List::BasicItem::BasicItem(const string &text):
        add(label);
 }
 
-void List::BasicItem::on_style_change()
-{
-       if(!style)
-               return;
-
-       label.autosize();
-       if(const Part *part = style->get_part("children"))
-       {
-               const Sides &margin = part->get_margin();
-               label.set_position(margin.left, margin.bottom);
-       }
-}
-
 
 List::Loader::Loader(List &l):
        DataFile::DerivedObjectLoader<List, Widget::Loader>(l)