X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flist.cpp;h=a5928ec91a76793d453ef67cba2bd062d89165be;hb=6762feb95d685b556ae97a9cd1ce964d2b2ee5fe;hp=a4550c4ee9ad220dfd22336657894b9cca13de9a;hpb=729cb06f85e2888a7ac1e72375380257936106c9;p=libs%2Fgltk.git diff --git a/source/list.cpp b/source/list.cpp index a4550c4..a5928ec 100644 --- a/source/list.cpp +++ b/source/list.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -34,11 +35,15 @@ List::List(ListData &d): void List::init() { + input_type = INPUT_NAVIGATION; + item_factory = 0; sel_index = -1; + focus_index = -1; first = 0; max_scroll = 0; view_size = 5; + ignore_slider_change = false; observer = new DataObserver(*this); @@ -78,6 +83,8 @@ void List::autosize_special(const Part &part, Geometry &ageom) const ageom.w = max(ageom.w, max_w+margin.left+margin.right); ageom.h = max(ageom.h, total_h+margin.top+margin.bottom); } + else if(part.get_name()=="slider") + autosize_child(slider, part, ageom); } void List::set_data(ListData &d) @@ -108,20 +115,22 @@ void List::set_data(ListData &d) void List::items_changed() { - check_view_range(); signal_autosize_changed.emit(); - reposition_items(); + rebuild(); } List::Item *List::create_item(unsigned index) { - Item *item = 0; + Item *item = 0; if(item_factory) item = item_factory->create_item(index); else item = new BasicItem(data->get_string(index)); + if(static_cast(index)==sel_index) + item->set_active(true); add(*item); - item->signal_autosize_changed.connect(sigc::mem_fun(this, &List::item_autosize_changed)); + item->autosize(); + item->signal_autosize_changed.connect(sigc::bind(sigc::mem_fun(this, &List::item_autosize_changed), item)); return item; } @@ -141,21 +150,69 @@ void List::set_selected_index(int i) if(i>=static_cast(data->size())) throw out_of_range("List::set_selected_index"); - if(i==sel_index) + if(i==sel_index || (i<0 && sel_index<0)) return; if(sel_index>=0) items[sel_index]->set_active(false); if(i<0) + { sel_index = -1; + focus_index = -1; + set_input_focus(0); + signal_selection_cleared.emit(); + } else { sel_index = i; + focus_index = i; items[sel_index]->set_active(true); + if(state&FOCUS) + set_input_focus(items[focus_index]); signal_item_selected.emit(sel_index); } } +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") @@ -169,91 +226,107 @@ void List::render_special(const Part &part, GL::Renderer &renderer) const void List::button_press(int x, int y, unsigned btn) { - Container::button_press(x, y, btn); - if(click_focus && btn==1) + if(btn==4 || btn==5) { - for(unsigned i=first; (iis_visible()); ++i) - if(click_focus==items[i]) - { - set_selected_index(i); - break; - } + unsigned change = 3; + if(btn==4) + { + change = min(first, change); + slider.set_value(max_scroll-(first-change)); + } + else if(btn==5) + { + change = min(max_scroll-first, change); + slider.set_value(max_scroll-(first+change)); + } + } + else + { + Container::button_press(x, y, btn); + if(click_focus && btn==1) + { + for(unsigned i=first; (iis_visible()); ++i) + if(click_focus==items[i]) + { + set_selected_index(i); + break; + } + } } } -void List::on_geometry_change() +void List::focus_in() { - reposition_slider(); - reposition_items(); - - check_view_range(); + if(focus_index>=0 && items[focus_index]->is_visible()) + set_input_focus(items[focus_index]); + else + { + if(sel_index>=0 && items[sel_index]->is_visible()) + set_focus_index(sel_index); + else if(!items.empty()) + set_focus_index(first); + } } -void List::on_style_change() +bool List::navigate(Navigation nav) { - if(!style) - return; - - reposition_slider(); - reposition_items(); + if(nav==NAV_UP && !items.empty()) + { + if(focus_index>0) + set_focus_index(focus_index-1); + } + else if(nav==NAV_DOWN && !items.empty()) + { + if(static_cast(focus_index+1)get_part("slider")) + focus_index = i; + if(focus_index>=0) { - Geometry sgeom = slider_part->get_geometry(); - slider_part->get_alignment().apply(sgeom, geom, slider_part->get_margin()); - slider.set_geometry(sgeom); + scroll_to_focus(); + if(state&FOCUS) + set_input_focus(items[focus_index]); } } -void List::item_autosize_changed() +void List::item_autosize_changed(Item *item) { + item->autosize(); signal_autosize_changed.emit(); - reposition_items(); + rebuild(); } -void List::reposition_items() +unsigned List::last_to_first(unsigned last) const { if(!style) - return; + return last; + unsigned view_h = geom.h; if(const Part *items_part = style->get_part("items")) { const Sides &margin = items_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]->autosize(igeom); - 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; - } - } - } + view_h -= margin.top+margin.bottom; } + + unsigned items_h = 0; + for(unsigned i=last; iget_geometry().h; + if(items_h>view_h) + return min(i+1, last); + } + + return 0; } void List::check_view_range() @@ -261,23 +334,10 @@ void List::check_view_range() if(!style) return; - unsigned h = geom.h; - if(const Part *items_part = style->get_part("items")) - { - const Sides &margin = items_part->get_margin(); - h -= margin.top+margin.bottom; - } - - max_scroll = items.size(); - for(unsigned i=items.size(); i-->0; ) - { - unsigned ih = items[i]->get_geometry().h; - if(ih<=h) - { - h -= ih; - --max_scroll; - } - } + if(items.empty()) + max_scroll = 0; + else + max_scroll = last_to_first(items.size()-1); if(first>max_scroll) first = max_scroll; @@ -286,15 +346,34 @@ void List::check_view_range() slider.set_value(max_scroll-first); } +void List::scroll_to_focus() +{ + if(focus_index<0 || items[focus_index]->is_visible()) + return; + + if(static_cast(focus_index)0) + if(max_scroll>0 && !ignore_slider_change) { first = max_scroll-static_cast(value); - reposition_items(); + rebuild(); } } +void List::adjust_index(int &index, int pos, int change) +{ + if(index>pos) + index += change; + else if(index==pos) + index = (change>0 ? index+change : -1); +} + List::DataObserver::DataObserver(List &l): list(l) @@ -307,8 +386,8 @@ List::DataObserver::DataObserver(List &l): void List::DataObserver::item_added(unsigned i) { - if(list.sel_index>=static_cast(i)) - ++list.sel_index; + adjust_index(list.sel_index, i, 1); + adjust_index(list.focus_index, i, 1); Item *item = list.create_item(i); list.items.insert(list.items.begin()+i, item); @@ -317,28 +396,35 @@ void List::DataObserver::item_added(unsigned i) void List::DataObserver::item_removed(unsigned i) { - if(list.sel_index>static_cast(i)) - --list.sel_index; - else if(list.sel_index==static_cast(i)) - list.sel_index = -1; + bool had_selection = (list.sel_index>=0); + adjust_index(list.sel_index, i, -1); + adjust_index(list.focus_index, i, -1); delete list.items[i]; list.items.erase(list.items.begin()+i); list.items_changed(); + + if(had_selection && list.sel_index<0) + list.signal_selection_cleared.emit(); } void List::DataObserver::cleared() { list.sel_index = -1; + list.focus_index = -1; for(vector::iterator i=list.items.begin(); i!=list.items.end(); ++i) delete *i; list.items.clear(); list.items_changed(); + + list.signal_selection_cleared.emit(); } 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(); } @@ -374,6 +460,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) {