X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flist.cpp;h=a9cbcc1c5f02af4080ca895f18525dfcea61af1e;hb=HEAD;hp=a5285832b98994f5f9cc4365d41c28c5b37b1176;hpb=7e328215742ee672b1a93e5f5f358828c53ce921;p=libs%2Fgltk.git diff --git a/source/list.cpp b/source/list.cpp index a528583..a028571 100644 --- a/source/list.cpp +++ b/source/list.cpp @@ -3,12 +3,12 @@ #include #include #include +#include #include "graphic.h" #include "list.h" #include "part.h" #include "style.h" #include "text.h" -#include "vslider.h" using namespace std; @@ -20,71 +20,61 @@ incompatible_data::incompatible_data(const type_info &ti): { } -List::List(): - data(new BasicListData), - own_data(true) +List::List(unique_ptr d): + List(*d) { - init(); + own_data = move(d); } +List::List(): + List(make_unique>()) +{ } + List::List(ListData &d): data(&d), - own_data(false) -{ - init(); -} - -void List::init() + observer(make_unique(*this)) { 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)); } -List::~List() -{ - delete item_factory; - delete observer; - if(own_data) - delete data; -} - void List::autosize_special(const Part &part, Geometry &ageom) const { if(part.get_name()=="items") { 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); + items_w = max(items_w, igeom.w); + items_h = max(items_h, igeom.h); } - unsigned total_h = max_h*(view_rows==0 ? items.size() : view_rows); + if(view_mode==GRID) + { + unsigned r = view_rows; + unsigned c = view_columns; + if(r==0 && c==0) + r = sqrt(items.size()); + if(r==0) + r = (items.size()+c-1)/c; + if(c==0) + c = (items.size()+r-1)/r; + items_w *= c; + items_h *= r; + } + else + items_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); + ageom.w = max(ageom.w, items_w+margin.left+margin.right); + ageom.h = max(ageom.h, items_h+margin.top+margin.bottom); } else if(part.get_name()=="slider") autosize_child(slider, part, ageom); @@ -95,23 +85,16 @@ void List::set_data(ListData &d) if(item_factory) item_factory->set_data(d); - delete observer; - if(own_data) - delete data; + observer.reset(); + own_data.reset(); data = &d; - own_data = false; - observer = new DataObserver(*this); + observer = make_unique(*this); - for(vector::iterator i=items.begin(); i!=items.end(); ++i) - delete *i; items.clear(); - unsigned n_items = data->size(); - for(unsigned i=0; isize(); + for(size_t i=0; i List::create_item(size_t index) { - Item *item = 0; + unique_ptr item; if(item_factory) item = item_factory->create_item(index); else - item = new BasicItem(data->get_string(index)); - if(static_cast(index)==sel_index) + item = make_unique(data->get_string(index)); + if(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)); + item->signal_autosize_changed.connect(sigc::bind(sigc::mem_fun(this, &List::item_autosize_changed), item.get())); return item; } -void List::set_view_size(unsigned r) +void List::set_view_size(unsigned s) +{ + set_view_size(s, s); +} + +void List::set_view_size(unsigned r, unsigned c) { view_rows = r; + view_columns = c; signal_autosize_changed.emit(); } @@ -148,38 +137,37 @@ void List::set_view_all() set_view_size(0); } -void List::set_selected_index(int i) +void List::set_selected_index(size_t i) { - if(i>=static_cast(data->size())) + if(i>=data->size() && i!=INVALID_INDEX) throw out_of_range("List::set_selected_index"); - if(i==sel_index || (i<0 && sel_index<0)) + if(i==sel_index) return; - if(sel_index>=0) + if(sel_index!=INVALID_INDEX) items[sel_index]->set_active(false); - if(i<0) + + sel_index = i; + focus_index = i; + if(i==INVALID_INDEX) { - sel_index = -1; - focus_index = -1; - set_input_focus(0); + set_input_focus(nullptr); 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]); + set_input_focus(items[focus_index].get()); signal_item_selected.emit(sel_index); } } void List::set_selected_item(Widget *item) { - for(unsigned i=rows[first_row].first; (iis_visible()); ++i) - if(item==items[i]) + for(size_t i=rows[first_row].first; (iis_visible()); ++i) + if(item==items[i].get()) return set_selected_index(i); } @@ -191,8 +179,8 @@ void List::rebuild_special(const Part &part) { SetFlag flag(ignore_slider_change); reposition_items(true); - unsigned old_first_row = first_row; - unsigned old_max_scroll = max_scroll; + size_t old_first_row = first_row; + size_t old_max_scroll = max_scroll; check_view_range(); if(first_row!=old_first_row || max_scroll!=old_max_scroll) reposition_items(false); @@ -205,7 +193,7 @@ void List::render_special(const Part &part, GL::Renderer &renderer) const { if(part.get_name()=="items") { - for(unsigned i=rows[first_row].first; (iis_visible()); ++i) + for(size_t i=rows[first_row].first; (iis_visible()); ++i) items[i]->render(renderer); } else if(part.get_name()=="slider") @@ -228,7 +216,7 @@ void List::button_press(int x, int y, unsigned btn) { if(btn==4 || btn==5) { - unsigned change = 3; + size_t change = 3; if(btn==4) { change = min(first_row, change); @@ -305,11 +293,11 @@ void List::touch_motion(int, int y, unsigned finger) void List::focus_in() { Container::focus_in(); - if(focus_index>=0 && items[focus_index]->is_visible()) - set_input_focus(items[focus_index]); + if(focus_index!=INVALID_INDEX && items[focus_index]->is_visible()) + set_input_focus(items[focus_index].get()); else { - if(sel_index>=0 && items[sel_index]->is_visible()) + if(sel_index!=INVALID_INDEX && items[sel_index]->is_visible()) set_focus_index(sel_index); else if(!items.empty()) set_focus_index(rows[first_row].first); @@ -318,7 +306,7 @@ void List::focus_in() bool List::navigate(Navigation nav) { - if((nav==NAV_UP || nav==NAV_DOWN) && !items.empty()) + if((nav==NAV_UP || nav==NAV_DOWN || ((nav==NAV_LEFT || nav==NAV_RIGHT) && view_mode==GRID)) && !items.empty()) move_focus(nav, true); else if(nav==NAV_ACTIVATE) set_selected_index(focus_index); @@ -330,19 +318,35 @@ bool List::navigate(Navigation nav) void List::on_style_change() { - items_part = (style ? style->get_part("items") : 0); + items_part = (style ? style->find_part("items") : nullptr); } void List::move_focus(Navigation nav, bool select) { - if(nav==NAV_UP) + if(nav==NAV_UP && view_mode==GRID) + { + size_t row = item_index_to_row(focus_index); + if(row>0) + set_focus_index(rows[row-1].first+focus_index-rows[row].first); + else + set_focus_index(0); + } + else if(nav==NAV_DOWN && view_mode==GRID) + { + size_t row = item_index_to_row(focus_index); + if(row+10) set_focus_index(focus_index-1); } - else if(nav==NAV_DOWN) + else if(nav==NAV_DOWN || (nav==NAV_RIGHT && view_mode==GRID)) { - if(static_cast(focus_index+1)=0) + if(focus_index!=INVALID_INDEX) { scroll_to_focus(); if(state&FOCUS) - set_input_focus(items[focus_index]); + set_input_focus(items[focus_index].get()); } } @@ -365,7 +369,7 @@ void List::item_autosize_changed(Item *item) { item->autosize(); signal_autosize_changed.emit(); - rebuild(); + mark_rebuild(); } void List::reposition_items(bool record_rows) @@ -381,20 +385,25 @@ void List::reposition_items(bool record_rows) const Sides &margin = items_part->get_margin(); unsigned view_w = geom.w-min(geom.w, margin.left+margin.right); + unsigned x = 0; unsigned y = 0; unsigned row_h = 0; - for(unsigned i=0; iget_geometry(); - if(y) - y -= row_h; - if(record_rows && i>0) + if(view_mode!=GRID || (x>0 && x+igeom.w>view_w)) { - rows.back().height = row_h; - rows.push_back(i); + x = 0; + if(y) + y -= row_h; + if(record_rows && i>0) + { + rows.back().height = row_h; + rows.push_back(i); + } + row_h = 0; } - row_h = 0; if(first_rowset_visible(true); - items[i]->set_geometry(Geometry(margin.left, y-igeom.h, view_w, igeom.h)); + unsigned iw = (view_mode==GRID ? igeom.w : view_w); + items[i]->set_geometry(Geometry(margin.left+x, y-igeom.h, iw, igeom.h)); } else { - for(unsigned j=rows.back().first; j<=i; ++j) + for(size_t j=rows.back().first; j<=i; ++j) items[j]->set_visible(false); y = 0; } + x += igeom.w; row_h = max(row_h, igeom.h); } @@ -420,7 +431,7 @@ void List::reposition_items(bool record_rows) rows.back().height = row_h; } -unsigned List::last_to_first_row(unsigned last) const +size_t List::last_to_first_row(size_t last) const { if(!items_part) return last; @@ -429,7 +440,7 @@ unsigned List::last_to_first_row(unsigned last) const unsigned view_h = geom.h-min(geom.h, margin.top+margin.bottom); unsigned items_h = 0; - for(unsigned i=last; iview_h) @@ -439,9 +450,9 @@ unsigned List::last_to_first_row(unsigned last) const return 0; } -unsigned List::item_index_to_row(unsigned index) const +size_t List::item_index_to_row(size_t index) const { - for(unsigned i=0; i+1index) return i; return rows.size()-1; @@ -461,15 +472,16 @@ void List::check_view_range() first_row = max_scroll; slider.set_range(0, max_scroll); + slider.set_page_size(rows.size()-max_scroll); slider.set_value(max_scroll-first_row); } void List::scroll_to_focus() { - if(focus_index<0 || items[focus_index]->is_visible()) + if(focus_index==INVALID_INDEX || items[focus_index]->is_visible()) return; - unsigned focus_row = item_index_to_row(static_cast(focus_index)); + size_t focus_row = item_index_to_row(focus_index); if(focus_row0 && !ignore_slider_change) { - first_row = max_scroll-static_cast(value); - rebuild(); + first_row = max_scroll-static_cast(value); + mark_rebuild(); } } -void List::adjust_index(int &index, int pos, int change) +void List::adjust_index(size_t &index, size_t pos, ptrdiff_t change) { - if(index>pos) + if(index==INVALID_INDEX) + return; + else if(index>pos) index += change; else if(index==pos) - index = (change>0 ? index+change : -1); + index = (change>0 ? index+change : INVALID_INDEX); } @@ -503,61 +517,61 @@ List::DataObserver::DataObserver(List &l): list.data->signal_refresh_item.connect(sigc::mem_fun(this, &DataObserver::refresh_item)); } -void List::DataObserver::item_added(unsigned i) +void List::DataObserver::item_added(size_t 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.insert(list.items.begin()+i, list.create_item(i)); list.items_changed(); } -void List::DataObserver::item_removed(unsigned i) +void List::DataObserver::item_removed(size_t i) { - bool had_selection = (list.sel_index>=0); + bool had_selection = (list.sel_index!=INVALID_INDEX); 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) + if(had_selection && list.sel_index==INVALID_INDEX) 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.sel_index = INVALID_INDEX; + list.focus_index = INVALID_INDEX; list.items.clear(); list.items_changed(); list.signal_selection_cleared.emit(); } -void List::DataObserver::refresh_item(unsigned i) +void List::DataObserver::refresh_item(size_t i) { - delete list.items[i]; - // Avoid stale pointer while create_item is executing - list.items[i] = 0; + // Destroy the old item before calling create_item + list.items[i].reset(); list.items[i] = list.create_item(i); list.items_changed(); } +List::Item::Item() +{ + input_type = INPUT_NAVIGATION; +} + void List::Item::autosize_special(const Part &part, Geometry &ageom) const { if(part.get_name()=="children") { const Sides &margin = part.get_margin(); - for(list::const_iterator i=children.begin(); i!=children.end(); ++i) + for(const unique_ptr &c: children) { Geometry cgeom; - (*i)->widget->autosize(cgeom); + c->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); } @@ -573,8 +587,8 @@ void List::Item::render_special(const Part &part, GL::Renderer &renderer) const { if(part.get_name()=="children") { - for(list::const_iterator i=children.begin(); i!=children.end(); ++i) - (*i)->widget->render(renderer); + for(const unique_ptr &c: children) + c->widget->render(renderer); } } @@ -586,7 +600,7 @@ void List::SimpleItem::on_style_change() Widget *child = children.front()->widget; child->autosize(); - if(const Part *part = style->get_part("children")) + if(const Part *part = style->find_part("children")) { const Sides &margin = part->get_margin(); child->set_position(margin.left, margin.bottom); @@ -599,13 +613,14 @@ void List::MultiColumnItem::check_widths(vector &widths) const if(widths.size()::const_iterator i=children.begin(); i!=children.end(); ++i, ++n) + size_t n = 0; + for(const unique_ptr &c: children) { Geometry cgeom; - (*i)->widget->autosize(cgeom); + c->widget->autosize(cgeom); // TODO invent a better way to specify spacings widths[n] = max(widths[n], cgeom.w+8); + ++n; } } @@ -614,17 +629,17 @@ void List::MultiColumnItem::set_widths(const vector &widths) if(!style) return; - const Part *part = style->get_part("children"); + const Part *part = style->find_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) + size_t n = 0; + for(const unique_ptr &c: children) { - (*i)->widget->set_position(x, margin.bottom); - x += widths[n]; + c->widget->set_position(x, margin.bottom); + x += widths[n++]; } } @@ -633,26 +648,26 @@ void List::MultiColumnItem::on_style_change() if(!style) return; - for(std::list::const_iterator i=children.begin(); i!=children.end(); ++i) - (*i)->widget->autosize(); + for(const unique_ptr &c: children) + c->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)) + for(const unique_ptr &i: list->items) + if(i.get()!=this) + if(MultiColumnItem *mci = dynamic_cast(i.get())) 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)) + for(const unique_ptr &i: list->items) + if(MultiColumnItem *mci = dynamic_cast(i.get())) mci->set_widths(self_widths); } @@ -671,7 +686,9 @@ List::Loader::Loader(List &l): DataFile::DerivedObjectLoader(l) { add("item", &Loader::item); + add("view_mode", &List::view_mode); add("view_size", &List::view_rows); + add("view_size", &List::view_rows, &List::view_columns); } void List::Loader::item(const string &v) @@ -679,5 +696,17 @@ void List::Loader::item(const string &v) dynamic_cast &>(*obj.data).append(v); } + +void operator>>(const LexicalConverter &conv, List::ViewMode &vm) +{ + const string &str = conv.get(); + if(str=="LIST") + vm = List::LIST; + else if(str=="GRID") + vm = List::GRID; + else + throw lexical_error(format("conversion of '%s' to List::ViewMode", str)); +} + } // namespace GLtk } // namespace Msp