X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flist.cpp;h=10c5af84ab100ef8591a1215b6113868657c8f60;hb=22e2888b01b7b9d0a8df568f25dbe06addd51710;hp=461d5e425c3076426f8ad51029ee971825cf396f;hpb=707b59d45ae50b69c94918f8f74313283b304597;p=libs%2Fgltk.git diff --git a/source/list.cpp b/source/list.cpp index 461d5e4..10c5af8 100644 --- a/source/list.cpp +++ b/source/list.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -39,6 +40,7 @@ void List::init() first = 0; max_scroll = 0; view_size = 5; + ignore_slider_change = false; observer = new DataObserver(*this); @@ -65,7 +67,8 @@ void List::autosize_special(const Part &part, Geometry &ageom) const unsigned total_h = 0; for(unsigned i=0; iget_geometry(); + Geometry igeom; + items[i]->autosize(igeom); max_w = max(max_w, igeom.w); if(view_size==0 || icreate_item(index); else item = new BasicItem(data->get_string(index)); + if(static_cast(index)==sel_index) + item->set_active(true); add(*item); item->autosize(); item->signal_autosize_changed.connect(sigc::bind(sigc::mem_fun(this, &List::item_autosize_changed), item)); @@ -156,6 +162,46 @@ void List::set_selected_index(int i) } } +void List::rebuild_special(const Part &part) +{ + if(part.get_name()=="slider") + reposition_child(slider, part); + else if(part.get_name()=="items") + { + SetFlag flag(ignore_slider_change); + 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; iset_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; + } + } + } + } + + Widget::rebuild_special(part); +} + void List::render_special(const Part &part, GL::Renderer &renderer) const { if(part.get_name()=="items") @@ -181,79 +227,11 @@ void List::button_press(int x, int y, unsigned btn) } } -void List::on_geometry_change() -{ - reposition_slider(); - reposition_items(); - - check_view_range(); -} - -void List::on_style_change() -{ - if(!style) - return; - - reposition_slider(); - reposition_items(); - - check_view_range(); -} - -void List::reposition_slider() -{ - if(!style) - return; - - if(const Part *slider_part = style->get_part("slider")) - { - Geometry sgeom = slider_part->get_geometry(); - slider_part->get_alignment().apply(sgeom, geom, slider_part->get_margin()); - slider.set_geometry(sgeom); - } -} - void List::item_autosize_changed(Item *item) { - signal_autosize_changed.emit(); item->autosize(); - reposition_items(); -} - -void List::reposition_items() -{ - if(!style) - return; - - 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; - for(unsigned i=0; iset_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; - } - } - } - } + signal_autosize_changed.emit(); + rebuild(); } void List::check_view_range() @@ -288,10 +266,10 @@ void List::check_view_range() void List::slider_value_changed(double value) { - if(max_scroll>0) + if(max_scroll>0 && !ignore_slider_change) { first = max_scroll-static_cast(value); - reposition_items(); + rebuild(); } } @@ -339,6 +317,8 @@ void List::DataObserver::cleared() void List::DataObserver::refresh_item(unsigned i) { delete list.items[i]; + // Avoid stale pointer while create_item is executing + list.items[i] = 0; list.items[i] = list.create_item(i); list.items_changed(); } @@ -351,7 +331,8 @@ void List::Item::autosize_special(const Part &part, Geometry &ageom) const const Sides &margin = part.get_margin(); for(list::const_iterator i=children.begin(); i!=children.end(); ++i) { - const Geometry &cgeom = (*i)->widget->get_geometry(); + 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); } @@ -373,6 +354,72 @@ void List::Item::render_special(const Part &part, GL::Renderer &renderer) const } +void List::MultiColumnItem::check_widths(vector &widths) const +{ + if(widths.size()::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 &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::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::const_iterator i=children.begin(); i!=children.end(); ++i) + (*i)->widget->autosize(); + + vector widths; + List *list = static_cast(parent); + for(vector::const_iterator i=list->items.begin(); i!=list->items.end(); ++i) + if(*i!=this) + if(MultiColumnItem *mci = dynamic_cast(*i)) + mci->check_widths(widths); + + vector self_widths(widths); + check_widths(self_widths); + bool update_all = false; + for(unsigned i=0; (!update_all && iwidths[i]; + + if(update_all) + { + for(vector::const_iterator i=list->items.begin(); i!=list->items.end(); ++i) + if(MultiColumnItem *mci = dynamic_cast(*i)) + mci->set_widths(self_widths); + } + + set_widths(self_widths); +} + + List::BasicItem::BasicItem(const string &text): label(text) {