X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flist.cpp;h=a5285832b98994f5f9cc4365d41c28c5b37b1176;hb=7e328215742ee672b1a93e5f5f358828c53ce921;hp=672cf4911bbf028e4e3850579959794e9c4f11f2;hpb=91997dd3189b93a67179822ec2fed5f2a7bddb74;p=libs%2Fgltk.git diff --git a/source/list.cpp b/source/list.cpp index 672cf49..a528583 100644 --- a/source/list.cpp +++ b/source/list.cpp @@ -1,13 +1,8 @@ -/* $Id$ - -This file is part of libmspgltk -Copyright © 2007-2009 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - -#include +#include +#include #include -#include +#include +#include #include "graphic.h" #include "list.h" #include "part.h" @@ -20,246 +15,668 @@ using namespace std; namespace Msp { namespace GLtk { +incompatible_data::incompatible_data(const type_info &ti): + logic_error("expected "+Debug::demangle(ti.name())) +{ } + + List::List(): - sel_index(-1), - first(0), - n_visible(1), - row_height(1), - items_part(0) + data(new BasicListData), + own_data(true) +{ + init(); +} + +List::List(ListData &d): + data(&d), + own_data(false) { + init(); +} + +void List::init() +{ + input_type = INPUT_NAVIGATION; + + item_factory = 0; + sel_index = -1; + focus_index = -1; + first_row = 0; + max_scroll = 0; + view_rows = 5; + items_part = 0; + ignore_slider_change = false; + dragging = false; + drag_start_x = 0; + drag_start_y = 0; + + observer = new DataObserver(*this); + add(slider); slider.set_step(1); slider.signal_value_changed.connect(sigc::mem_fun(this, &List::slider_value_changed)); } -void List::autosize() +List::~List() { - if(!style) - return; - - float font_size = style->get_font()->get_default_size(); + delete item_factory; + delete observer; + if(own_data) + delete data; +} - geom.w = 0; - for(vector::iterator i=items.begin(); i!=items.end(); ++i) +void List::autosize_special(const Part &part, Geometry &ageom) const +{ + if(part.get_name()=="items") { - unsigned w = static_cast(style->get_font()->get_string_width(*i)*font_size); - geom.w = max(geom.w, w); + const Sides &margin = part.get_margin(); + + unsigned max_w = 0; + unsigned max_h = 0; + for(unsigned i=0; iautosize(igeom); + max_w = max(max_w, igeom.w); + max_h = max(max_h, igeom.h); + } + + unsigned total_h = max_h*(view_rows==0 ? items.size() : view_rows); + + 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); +} - geom.h = items.size()*row_height; +void List::set_data(ListData &d) +{ + if(item_factory) + item_factory->set_data(d); + + delete observer; + if(own_data) + delete data; - if(items_part) + data = &d; + own_data = false; + observer = new DataObserver(*this); + + for(vector::iterator i=items.begin(); i!=items.end(); ++i) + delete *i; + items.clear(); + unsigned n_items = data->size(); + for(unsigned i=0; iget_margin(); - geom.w += margin.left+margin.right; - geom.h += margin.top+margin.bottom; + Item *item = create_item(i); + items.push_back(item); } + + items_changed(); +} + +void List::items_changed() +{ + signal_autosize_changed.emit(); + rebuild(); } -void List::append(const string &v) +List::Item *List::create_item(unsigned index) { - items.push_back(v); - check_view_range(); + 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->autosize(); + item->signal_autosize_changed.connect(sigc::bind(sigc::mem_fun(this, &List::item_autosize_changed), item)); + return item; } -void List::insert(unsigned i, const string &v) +void List::set_view_size(unsigned r) { - if(i>items.size()) - throw InvalidParameterValue("Index out of range"); + view_rows = r; + signal_autosize_changed.emit(); +} - items.insert(items.begin()+i, v); - check_view_range(); +void List::set_view_all() +{ + set_view_size(0); } -void List::remove(unsigned i) +void List::set_selected_index(int i) { - if(i>items.size()) - throw InvalidParameterValue("Index out of range"); + if(i>=static_cast(data->size())) + throw out_of_range("List::set_selected_index"); + + if(i==sel_index || (i<0 && sel_index<0)) + return; - items.erase(items.begin()+i); - if(sel_index>static_cast(i)) - --sel_index; - else if(sel_index==static_cast(i)) + 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); + } +} - check_view_range(); +void List::set_selected_item(Widget *item) +{ + for(unsigned i=rows[first_row].first; (iis_visible()); ++i) + if(item==items[i]) + return set_selected_index(i); } -void List::clear() +void List::rebuild_special(const Part &part) { - items.clear(); - sel_index = -1; + if(part.get_name()=="slider") + reposition_child(slider, part); + else if(part.get_name()=="items") + { + SetFlag flag(ignore_slider_change); + reposition_items(true); + unsigned old_first_row = first_row; + unsigned old_max_scroll = max_scroll; + check_view_range(); + if(first_row!=old_first_row || max_scroll!=old_max_scroll) + reposition_items(false); + } - check_view_range(); + Widget::rebuild_special(part); } -void List::set_selected_index(int i) +void List::render_special(const Part &part, GL::Renderer &renderer) const { - if(i<0) - sel_index = -1; - else if(i(items.size())) + if(part.get_name()=="items") { - sel_index = i; - signal_item_selected.emit(sel_index, items[sel_index]); + for(unsigned i=rows[first_row].first; (iis_visible()); ++i) + items[i]->render(renderer); } - else - throw InvalidParameterValue("Index out of range"); + else if(part.get_name()=="slider") + slider.render(renderer); } -const string &List::get_selected() const +bool List::key_press(unsigned key, unsigned mod) { - if(sel_index<0) - throw InvalidState("No selection"); + if(key==Input::KEY_UP && mod==MOD_CTRL) + move_focus(NAV_UP, false); + else if(key==Input::KEY_DOWN && mod==MOD_CTRL) + move_focus(NAV_DOWN, false); + else + return false; - return items[sel_index]; + return true; } 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) { - if(items_part) - y += items_part->get_margin().top; - - unsigned i = (geom.h-1-y)/row_height; - if(i(row_height), 0); - Text(*style, items[first+i]).render(part, pgeom); + Container::touch_press(drag_start_x, drag_start_y, finger); + if(touch_focus) + set_selected_item(touch_focus); + Container::touch_motion(x, y, finger); + Container::touch_release(x, y, finger); } + dragging = false; } - else if(part.get_name()=="selection") +} + +void List::touch_motion(int, int y, unsigned finger) +{ + if(finger==0 && !items.empty() && dragging) { - if(sel_index>=static_cast(first) && sel_index(first+n_visible)) + int dy = y-drag_start_y; + if(dy>0 && first_rowrender(rgeom.w, rgeom.h); - GL::pop_matrix(); + int row_h = rows[first_row].height; + if(dy>row_h) + { + drag_start_y += row_h; + slider.set_value(max_scroll-(first_row+1)); + } + } + else if(dy<0 && first_row>0) + { + int row_h = rows[first_row-1].height; + if(-dy>row_h) + { + drag_start_y -= row_h; + slider.set_value(max_scroll-(first_row-1)); + } } } - else if(part.get_name()=="slider") - slider.render(); } -void List::on_geometry_change() +void List::focus_in() +{ + Container::focus_in(); + 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(rows[first_row].first); + } +} + +bool List::navigate(Navigation nav) { - reposition_slider(); + if((nav==NAV_UP || nav==NAV_DOWN) && !items.empty()) + move_focus(nav, true); + else if(nav==NAV_ACTIVATE) + set_selected_index(focus_index); + else + return false; - check_view_range(); + return true; } void List::on_style_change() { - if(!style) + items_part = (style ? style->get_part("items") : 0); +} + +void List::move_focus(Navigation nav, bool select) +{ + if(nav==NAV_UP) + { + if(focus_index>0) + set_focus_index(focus_index-1); + } + else if(nav==NAV_DOWN) { - items_part = 0; + if(static_cast(focus_index+1)=0) + { + scroll_to_focus(); + if(state&FOCUS) + set_input_focus(items[focus_index]); + } +} + +void List::item_autosize_changed(Item *item) +{ + item->autosize(); + signal_autosize_changed.emit(); + rebuild(); +} + +void List::reposition_items(bool record_rows) +{ + if(!items_part) return; + + if(record_rows) + { + rows.clear(); + rows.push_back(0); } - reposition_slider(); + const Sides &margin = items_part->get_margin(); + unsigned view_w = geom.w-min(geom.w, margin.left+margin.right); + unsigned y = 0; + unsigned row_h = 0; + for(unsigned i=0; iget_geometry(); + + if(y) + y -= row_h; + if(record_rows && i>0) + { + rows.back().height = row_h; + rows.push_back(i); + } + row_h = 0; - items_part = style->get_part("items"); + if(first_rowget_font(); - row_height = static_cast((font.get_ascent()-font.get_descent())*font.get_default_size()); + if(!y) + items[i]->set_visible(false); + else if(igeom.h+margin.bottom<=y) + { + items[i]->set_visible(true); + items[i]->set_geometry(Geometry(margin.left, y-igeom.h, view_w, igeom.h)); + } + else + { + for(unsigned j=rows.back().first; j<=i; ++j) + items[j]->set_visible(false); + y = 0; + } - check_view_range(); + row_h = max(row_h, igeom.h); + } + + if(record_rows) + rows.back().height = row_h; +} + +unsigned List::last_to_first_row(unsigned last) const +{ + if(!items_part) + return last; + + const Sides &margin = items_part->get_margin(); + unsigned view_h = geom.h-min(geom.h, margin.top+margin.bottom); + + unsigned items_h = 0; + for(unsigned i=last; iview_h) + return min(i+1, last); + } + + return 0; +} + +unsigned List::item_index_to_row(unsigned index) const +{ + for(unsigned i=0; i+1index) + return i; + return rows.size()-1; } -void List::reposition_slider() +void List::check_view_range() { if(!style) return; - if(const Part *slider_part = style->get_part("slider")) + if(items.empty()) + max_scroll = 0; + else + max_scroll = last_to_first_row(rows.size()-1); + + if(first_row>max_scroll) + first_row = max_scroll; + + slider.set_range(0, max_scroll); + slider.set_value(max_scroll-first_row); +} + +void List::scroll_to_focus() +{ + if(focus_index<0 || items[focus_index]->is_visible()) + return; + + unsigned focus_row = item_index_to_row(static_cast(focus_index)); + if(focus_row0 && !ignore_slider_change) { - Geometry sgeom = slider_part->get_geometry(); - slider_part->get_alignment().apply(sgeom, geom, slider_part->get_margin()); - slider.set_geometry(sgeom); + first_row = max_scroll-static_cast(value); + rebuild(); } } -void List::check_view_range() +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) +{ + 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_item.connect(sigc::mem_fun(this, &DataObserver::refresh_item)); +} + +void List::DataObserver::item_added(unsigned i) +{ + 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); + list.items_changed(); +} + +void List::DataObserver::item_removed(unsigned i) +{ + 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) { - unsigned h = geom.h; - if(items_part) + 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(); +} + + +void List::Item::autosize_special(const Part &part, Geometry &ageom) const +{ + if(part.get_name()=="children") { - const Sides &margin = items_part->get_margin(); - h -= margin.top+margin.bottom; + const Sides &margin = part.get_margin(); + for(list::const_iterator i=children.begin(); i!=children.end(); ++i) + { + 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); + } } +} - n_visible = h/row_height; +void List::Item::set_active(bool a) +{ + set_state(ACTIVE, (a ? ACTIVE : NORMAL)); +} - if(first+n_visible>items.size()) +void List::Item::render_special(const Part &part, GL::Renderer &renderer) const +{ + if(part.get_name()=="children") { - if(items.size()>n_visible) - first = items.size()-n_visible; - else - first = 0; + for(list::const_iterator i=children.begin(); i!=children.end(); ++i) + (*i)->widget->render(renderer); } +} + - if(items.size()>n_visible) +void List::SimpleItem::on_style_change() +{ + if(!style || children.empty()) + return; + + Widget *child = children.front()->widget; + child->autosize(); + if(const Part *part = style->get_part("children")) { - slider.set_range(0, items.size()-n_visible); - slider.set_value(items.size()-n_visible-first); + const Sides &margin = part->get_margin(); + child->set_position(margin.left, margin.bottom); } - else +} + + +void List::MultiColumnItem::check_widths(vector &widths) const +{ + if(widths.size()::const_iterator i=children.begin(); i!=children.end(); ++i, ++n) { - slider.set_range(0, 0); - slider.set_value(0); + Geometry cgeom; + (*i)->widget->autosize(cgeom); + // TODO invent a better way to specify spacings + widths[n] = max(widths[n], cgeom.w+8); } } -void List::slider_value_changed(double value) +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) { - if(items.size()>n_visible) - first = items.size()-n_visible-static_cast(value); + add(label); } List::Loader::Loader(List &l): - Widget::Loader(l) + DataFile::DerivedObjectLoader(l) { add("item", &Loader::item); + add("view_size", &List::view_rows); } void List::Loader::item(const string &v) { - dynamic_cast(wdg).append(v); + dynamic_cast &>(*obj.data).append(v); } } // namespace GLtk