]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/list.cpp
Adjust event handling to match changes in mspgui
[libs/gltk.git] / source / list.cpp
index 113871ef8ed51f35e35944fd0b155e9f2d65e958..a1803694a23633cd0a3911bafeb007e2eef1f141 100644 (file)
@@ -1,10 +1,3 @@
-/* $Id$
-
-This file is part of libmspgltk
-Copyright © 2007-2009  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
 #include <msp/gl/immediate.h>
 #include <msp/gl/matrix.h>
 #include <msp/gl/transform.h>
@@ -20,63 +13,94 @@ using namespace std;
 namespace Msp {
 namespace GLtk {
 
-List::List(const Resources &r):
-       Widget(r),
-       Container(r),
+List::List():
        sel_index(-1),
        first(0),
        n_visible(1),
        row_height(1),
-       items_part(0),
-       slider(res)
+       items_part(0)
 {
        add(slider);
        slider.set_step(1);
        slider.signal_value_changed.connect(sigc::mem_fun(this, &List::slider_value_changed));
-
-       update_style();
 }
 
 void List::autosize()
 {
-       float font_size = style->get_font()->get_default_size();
+       autosize_rows(5);
+}
 
-       geom.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);
-               geom.w = max(geom.w, w);
-       }
+void List::autosize_rows(unsigned n)
+{
+       if(!style)
+               return;
 
-       geom.h = items.size()*row_height;
+       Widget::autosize();
 
        if(items_part)
        {
                const Sides &margin = items_part->get_margin();
-               geom.w += margin.left+margin.right;
-               geom.h += margin.top+margin.bottom;
+               float font_size = style->get_font()->get_default_size();
+
+               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);
+               }
+
+               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"))
+       {
+               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)
 {
        items.push_back(v);
        check_view_range();
+       signal_autosize_changed.emit();
 }
 
 void List::insert(unsigned i, const string &v)
 {
        if(i>items.size())
-               throw InvalidParameterValue("Index out of range");
+               throw out_of_range("List::insert");
 
        items.insert(items.begin()+i, v);
        check_view_range();
+       signal_autosize_changed.emit();
 }
 
 void List::remove(unsigned i)
 {
        if(i>items.size())
-               throw InvalidParameterValue("Index out of range");
+               throw out_of_range("List::remove");
 
        items.erase(items.begin()+i);
        if(sel_index>static_cast<int>(i))
@@ -85,6 +109,7 @@ void List::remove(unsigned i)
                sel_index = -1;
 
        check_view_range();
+       signal_autosize_changed.emit();
 }
 
 void List::clear()
@@ -93,6 +118,7 @@ void List::clear()
        sel_index = -1;
 
        check_view_range();
+       signal_autosize_changed.emit();
 }
 
 void List::set_selected_index(int i)
@@ -105,35 +131,17 @@ void List::set_selected_index(int i)
                signal_item_selected.emit(sel_index, items[sel_index]);
        }
        else
-               throw InvalidParameterValue("Index out of range");
+               throw out_of_range("List::set_selected_index");
 }
 
 const string &List::get_selected() const
 {
        if(sel_index<0)
-               throw InvalidState("No selection");
+               throw logic_error("sel_index<0");
 
        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")
@@ -177,6 +185,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();
@@ -186,6 +212,12 @@ void List::on_geometry_change()
 
 void List::on_style_change()
 {
+       if(!style)
+       {
+               items_part = 0;
+               return;
+       }
+
        reposition_slider();
 
        items_part = style->get_part("items");
@@ -198,6 +230,9 @@ void List::on_style_change()
 
 void List::reposition_slider()
 {
+       if(!style)
+               return;
+
        if(const Part *slider_part = style->get_part("slider"))
        {
                Geometry sgeom = slider_part->get_geometry();
@@ -252,7 +287,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