]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/list.cpp
Implement autosize() method for most widgets
[libs/gltk.git] / source / list.cpp
index 672cf4911bbf028e4e3850579959794e9c4f11f2..ea3969ce16dbb466591a642bdcff01c1e1150ed5 100644 (file)
@@ -1,7 +1,7 @@
 /* $Id$
 
 This file is part of libmspgltk
-Copyright © 2007-2009  Mikko Rasa, Mikkosoft Productions
+Copyright © 2007-2011  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 
@@ -33,27 +33,58 @@ List::List():
 }
 
 void List::autosize()
+{
+       autosize_rows(5);
+}
+
+void List::autosize_rows(unsigned n)
 {
        if(!style)
                return;
 
-       float font_size = style->get_font()->get_default_size();
+       Widget::autosize();
 
-       geom.w = 0;
-       for(vector<string>::iterator i=items.begin(); i!=items.end(); ++i)
+       if(items_part)
        {
-               unsigned w = static_cast<unsigned>(style->get_font()->get_string_width(*i)*font_size);
-               geom.w = max(geom.w, w);
-       }
+               const Sides &margin = items_part->get_margin();
+               float font_size = style->get_font()->get_default_size();
 
-       geom.h = items.size()*row_height;
+               unsigned max_w = 0;
+               for(vector<string>::iterator i=items.begin(); i!=items.end(); ++i)
+               {
+                       unsigned w = static_cast<unsigned>(style->get_font()->get_string_width(*i)*font_size);
+                       max_w = max(max_w, w);
+               }
 
-       if(items_part)
+               geom.w = max(geom.w, max_w+margin.left+margin.right);
+               geom.h = max(geom.h, n*row_height+margin.top+margin.bottom);
+       }
+
+       if(const Part *slider_part = style->get_part("slider"))
        {
-               const Sides &margin = items_part->get_margin();
-               geom.w += margin.left+margin.right;
-               geom.h += margin.top+margin.bottom;
+               Geometry sgeom = slider_part->get_geometry();
+               if(!sgeom.w || !sgeom.h)
+               {
+                       slider.autosize();
+                       if(!sgeom.w)
+                               sgeom.w = slider.get_geometry().w;
+                       if(!sgeom.h)
+                               sgeom.h = slider.get_geometry().h;
+               }
+
+               const Sides &margin = slider_part->get_margin();
+               geom.w = max(geom.w, sgeom.w+margin.left+margin.right);
+               geom.h = max(geom.h, sgeom.h+margin.top+margin.bottom);
+
+               reposition_slider();
        }
+
+       check_view_range();
+}
+
+void List::autosize_all()
+{
+       autosize_rows(items.size());
 }
 
 void List::append(const string &v)
@@ -114,24 +145,6 @@ const string &List::get_selected() const
        return items[sel_index];
 }
 
-void List::button_press(int x, int y, unsigned btn)
-{
-       Container::button_press(x, y, btn);
-       if(!click_focus && btn==1)
-       {
-               if(items_part)
-                       y += items_part->get_margin().top;
-
-               unsigned i = (geom.h-1-y)/row_height;
-               if(i<n_visible && first+i<items.size())
-               {
-                       sel_index = first+i;
-
-                       signal_item_selected.emit(sel_index, items[sel_index]);
-               }
-       }
-}
-
 void List::render_special(const Part &part) const
 {
        if(part.get_name()=="items")
@@ -175,6 +188,24 @@ void List::render_special(const Part &part) const
                slider.render();
 }
 
+void List::button_press(int x, int y, unsigned btn)
+{
+       Container::button_press(x, y, btn);
+       if(!click_focus && btn==1)
+       {
+               if(items_part)
+                       y += items_part->get_margin().top;
+
+               unsigned i = (geom.h-1-y)/row_height;
+               if(i<n_visible && first+i<items.size())
+               {
+                       sel_index = first+i;
+
+                       signal_item_selected.emit(sel_index, items[sel_index]);
+               }
+       }
+}
+
 void List::on_geometry_change()
 {
        reposition_slider();
@@ -259,7 +290,7 @@ List::Loader::Loader(List &l):
 
 void List::Loader::item(const string &v)
 {
-       dynamic_cast<List &>(wdg).append(v);
+       dynamic_cast<List &>(obj).append(v);
 }
 
 } // namespace GLtk