]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/list.cpp
Avoid segfault with a single multi-column item
[libs/gltk.git] / source / list.cpp
index c74af90d4541cb2185f2afc1f0d7a1bff8f6c172..2a0aa066e98b0a8447df6989a0f7a29ac8a06b8b 100644 (file)
@@ -1,3 +1,4 @@
+#include <msp/debug/demangle.h>
 #include <msp/gl/matrix.h>
 #include <msp/gl/meshbuilder.h>
 #include "graphic.h"
@@ -12,6 +13,11 @@ using namespace std;
 namespace Msp {
 namespace GLtk {
 
+incompatible_data::incompatible_data(const type_info &ti):
+       logic_error("expected "+Debug::demangle(ti.name()))
+{ }
+
+
 List::List():
        data(new BasicListData<string>),
        own_data(true)
@@ -28,6 +34,7 @@ List::List(ListData &d):
 
 void List::init()
 {
+       item_factory = 0;
        sel_index = -1;
        first = 0;
        max_scroll = 0;
@@ -42,28 +49,24 @@ void List::init()
 
 List::~List()
 {
+       delete item_factory;
        delete observer;
        if(own_data)
                delete data;
 }
 
-void List::autosize()
+void List::autosize_special(const Part &part, Geometry &ageom) const
 {
-       if(!style)
-               return;
-
-       Widget::autosize();
-
-       if(const Part *items_part = style->get_part("items"))
+       if(part.get_name()=="items")
        {
-               const Sides &margin = items_part->get_margin();
+               const Sides &margin = part.get_margin();
 
                unsigned max_w = 0;
                unsigned total_h = 0;
                for(unsigned i=0; i<items.size(); ++i)
                {
-                       items[i]->autosize();
-                       const Geometry &igeom = items[i]->get_geometry();
+                       Geometry igeom;
+                       items[i]->autosize(igeom);
                        max_w = max(max_w, igeom.w);
                        if(view_size==0 || i<view_size)
                                total_h += igeom.h;
@@ -72,16 +75,16 @@ void List::autosize()
                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);
+               ageom.w = max(ageom.w, max_w+margin.left+margin.right);
+               ageom.h = max(ageom.h, total_h+margin.top+margin.bottom);
        }
-
-       check_view_range();
-       rebuild();
 }
 
 void List::set_data(ListData &d)
 {
+       if(item_factory)
+               item_factory->set_data(d);
+
        delete observer;
        if(own_data)
                delete data;
@@ -90,6 +93,16 @@ void List::set_data(ListData &d)
        own_data = false;
        observer = new DataObserver(*this);
 
+       for(vector<Item *>::iterator i=items.begin(); i!=items.end(); ++i)
+               delete *i;
+       items.clear();
+       unsigned n_items = data->size();
+       for(unsigned i=0; i<n_items; ++i)
+       {
+               Item *item = create_item(i);
+               items.push_back(item);
+       }
+
        items_changed();
 }
 
@@ -102,7 +115,14 @@ void List::items_changed()
 
 List::Item *List::create_item(unsigned index)
 {
-       return new BasicItem(data->get_string(index));
+       Item *item = 0; 
+       if(item_factory)
+               item = item_factory->create_item(index);
+       else
+               item = new BasicItem(data->get_string(index));
+       add(*item);
+       item->signal_autosize_changed.connect(sigc::mem_fun(this, &List::item_autosize_changed));
+       return item;
 }
 
 void List::set_view_size(unsigned s)
@@ -118,9 +138,12 @@ void List::set_view_all()
 
 void List::set_selected_index(int i)
 {
-       if(i>static_cast<int>(data->size()))
+       if(i>=static_cast<int>(data->size()))
                throw out_of_range("List::set_selected_index");
 
+       if(i==sel_index)
+               return;
+
        if(sel_index>=0)
                items[sel_index]->set_active(false);
        if(i<0)
@@ -190,6 +213,12 @@ void List::reposition_slider()
        }
 }
 
+void List::item_autosize_changed()
+{
+       signal_autosize_changed.emit();
+       reposition_items();
+}
+
 void List::reposition_items()
 {
        if(!style)
@@ -198,15 +227,16 @@ void List::reposition_items()
        if(const Part *items_part = style->get_part("items"))
        {
                const Sides &margin = items_part->get_margin();
-               unsigned w = geom.w-margin.left-margin.right;
-               unsigned y = geom.h-margin.top;
+               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();
+                               Geometry igeom;
+                               items[i]->autosize(igeom);
                                if(igeom.h+margin.bottom<=y)
                                {
                                        items[i]->set_visible(true);
@@ -272,7 +302,7 @@ List::DataObserver::DataObserver(List &l):
        list.data->signal_item_added.connect(sigc::mem_fun(this, &DataObserver::item_added));
        list.data->signal_item_removed.connect(sigc::mem_fun(this, &DataObserver::item_removed));
        list.data->signal_cleared.connect(sigc::mem_fun(this, &DataObserver::cleared));
-       list.data->signal_refresh_strings.connect(sigc::mem_fun(this, &DataObserver::refresh_strings));
+       list.data->signal_refresh_item.connect(sigc::mem_fun(this, &DataObserver::refresh_item));
 }
 
 void List::DataObserver::item_added(unsigned i)
@@ -281,7 +311,6 @@ void List::DataObserver::item_added(unsigned i)
                ++list.sel_index;
 
        Item *item = list.create_item(i);
-       list.add(*item);
        list.items.insert(list.items.begin()+i, item);
        list.items_changed();
 }
@@ -307,24 +336,25 @@ void List::DataObserver::cleared()
        list.items_changed();
 }
 
-void List::DataObserver::refresh_strings()
+void List::DataObserver::refresh_item(unsigned i)
 {
+       delete list.items[i];
+       list.items[i] = list.create_item(i);
        list.items_changed();
 }
 
 
-void List::Item::autosize()
+void List::Item::autosize_special(const Part &part, Geometry &ageom) const
 {
-       Widget::autosize();
-
-       if(const Part *part = style->get_part("children"))
+       if(part.get_name()=="children")
        {
-               const Sides &margin = part->get_margin();
+               const Sides &margin = part.get_margin();
                for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
                {
-                       const Geometry &cgeom = (*i)->widget->get_geometry();
-                       geom.w = max(geom.w, cgeom.x+cgeom.w+margin.right);
-                       geom.h = max(geom.h, cgeom.y+cgeom.h+margin.top);
+                       Geometry cgeom;
+                       (*i)->widget->autosize(cgeom);
+                       ageom.w = max(ageom.w, cgeom.x+cgeom.w+margin.right);
+                       ageom.h = max(ageom.h, cgeom.y+cgeom.h+margin.top);
                }
        }
 }
@@ -344,6 +374,72 @@ void List::Item::render_special(const Part &part, GL::Renderer &renderer) const
 }
 
 
+void List::MultiColumnItem::check_widths(vector<unsigned> &widths) const
+{
+       if(widths.size()<children.size())
+               widths.resize(children.size(), 0);
+
+       unsigned n = 0;
+       for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i, ++n)
+       {
+               Geometry cgeom;
+               (*i)->widget->autosize(cgeom);
+               // TODO invent a better way to specify spacings
+               widths[n] = max(widths[n], cgeom.w+8);
+       }
+}
+
+void List::MultiColumnItem::set_widths(const vector<unsigned> &widths)
+{
+       if(!style)
+               return;
+
+       const Part *part = style->get_part("children");
+       if(!part)
+               return;
+
+       const Sides &margin = part->get_margin();
+       int x = margin.left;
+       unsigned n = 0;
+       for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i, ++n)
+       {
+               (*i)->widget->set_position(x, margin.bottom);
+               x += widths[n];
+       }
+}
+
+void List::MultiColumnItem::on_style_change()
+{
+       if(!style)
+               return;
+
+       for(std::list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
+               (*i)->widget->autosize();
+
+       vector<unsigned> widths;
+       List *list = static_cast<List *>(parent);
+       for(vector<Item *>::const_iterator i=list->items.begin(); i!=list->items.end(); ++i)
+               if(*i!=this)
+                       if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(*i))
+                               mci->check_widths(widths);
+
+       vector<unsigned> self_widths(widths);
+       check_widths(self_widths);
+       bool update_all = false;
+       for(unsigned i=0; (!update_all && i<widths.size() && i<self_widths.size()); ++i)
+               update_all = self_widths[i]>widths[i];
+
+       if(update_all)
+       {
+               for(vector<Item *>::const_iterator i=list->items.begin(); i!=list->items.end(); ++i)
+                       if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(*i))
+                               mci->set_widths(self_widths);
+       }
+       else
+               set_widths(self_widths);
+}
+
+
 List::BasicItem::BasicItem(const string &text):
        label(text)
 {
@@ -368,6 +464,7 @@ List::Loader::Loader(List &l):
        DataFile::DerivedObjectLoader<List, Widget::Loader>(l)
 {
        add("item", &Loader::item);
+       add("view_size", &List::view_size);
 }
 
 void List::Loader::item(const string &v)