X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flist.cpp;h=897ea39ebadb972decb3e7fe8dd8a876ac58089d;hb=9f38197854e699a6093a906ab43f4238f3cd2388;hp=f61d7c26dec8f3ba194058050a4411f423e6e646;hpb=f0b600c3b1739f7e088da6ab8eb7c2e67adf592c;p=libs%2Fgltk.git diff --git a/source/list.cpp b/source/list.cpp index f61d7c2..897ea39 100644 --- a/source/list.cpp +++ b/source/list.cpp @@ -2,12 +2,13 @@ #include #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,29 +21,16 @@ incompatible_data::incompatible_data(const type_info &ti): List::List(): - data(new BasicListData), - own_data(true) + List(*new BasicListData) { - init(); + own_data = true; } List::List(ListData &d): data(&d), - own_data(false) + observer(new DataObserver(*this)) { - init(); -} - -void List::init() -{ - item_factory = 0; - sel_index = -1; - first = 0; - max_scroll = 0; - view_size = 5; - ignore_slider_change = false; - - observer = new DataObserver(*this); + input_type = INPUT_NAVIGATION; add(slider); slider.set_step(1); @@ -63,22 +51,34 @@ void List::autosize_special(const Part &part, Geometry &ageom) const { const Sides &margin = part.get_margin(); - unsigned max_w = 0; - unsigned total_h = 0; + unsigned items_w = 0; + unsigned items_h = 0; for(unsigned i=0; iautosize(igeom); - max_w = max(max_w, igeom.w); - if(view_size==0 || i::iterator i=items.begin(); i!=items.end(); ++i) - delete *i; + for(Item *i: items) + delete i; items.clear(); unsigned n_items = data->size(); for(unsigned i=0; 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::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::rebuild_special(const Part &part) { if(part.get_name()=="slider") @@ -169,34 +190,12 @@ void List::rebuild_special(const Part &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(); - - 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; - } - } - } + if(first_row!=old_first_row || max_scroll!=old_max_scroll) + reposition_items(false); } Widget::rebuild_special(part); @@ -206,13 +205,25 @@ void List::render_special(const Part &part, GL::Renderer &renderer) const { if(part.get_name()=="items") { - for(unsigned i=first; (iis_visible()); ++i) + for(unsigned i=rows[first_row].first; (iis_visible()); ++i) items[i]->render(renderer); } else if(part.get_name()=="slider") slider.render(renderer); } +bool List::key_press(unsigned key, unsigned mod) +{ + 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 true; +} + void List::button_press(int x, int y, unsigned btn) { if(btn==4 || btn==5) @@ -220,27 +231,149 @@ void List::button_press(int x, int y, unsigned btn) unsigned change = 3; if(btn==4) { - change = min(first, change); - slider.set_value(max_scroll-(first-change)); + change = min(first_row, change); + slider.set_value(max_scroll-(first_row-change)); } else if(btn==5) { - change = min(max_scroll-first, change); - slider.set_value(max_scroll-(first+change)); + change = min(max_scroll-first_row, change); + slider.set_value(max_scroll-(first_row+change)); } } else { Container::button_press(x, y, btn); if(click_focus && btn==1) + set_selected_item(click_focus); + } +} + +void List::touch_press(int x, int y, unsigned finger) +{ + if(finger==0) + { + dragging = true; + drag_start_x = x; + drag_start_y = y; + } +} + +void List::touch_release(int x, int y, unsigned finger) +{ + if(finger==0) + { + int dx = x-drag_start_x; + int dy = y-drag_start_y; + if(dx*dx+dy*dy<25) + { + 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; + } +} + +void List::touch_motion(int, int y, unsigned finger) +{ + if(finger==0 && !items.empty() && dragging) + { + int dy = y-drag_start_y; + if(dy>0 && first_rowis_visible()); ++i) - if(click_focus==items[i]) - { - set_selected_index(i); - break; - } + 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)); + } + } + } +} + +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) +{ + 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); + else + return false; + + return true; +} + +void List::on_style_change() +{ + items_part = (style ? style->get_part("items") : 0); +} + +void List::move_focus(Navigation nav, bool select) +{ + if(nav==NAV_UP && view_mode==GRID) + { + unsigned 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) + { + unsigned row = item_index_to_row(focus_index); + if(row+10) + set_focus_index(focus_index-1); + } + else if(nav==NAV_DOWN || (nav==NAV_RIGHT && view_mode==GRID)) + { + if(static_cast(focus_index+1)=0) + { + scroll_to_focus(); + if(state&FOCUS) + set_input_focus(items[focus_index]); } } @@ -248,48 +381,142 @@ void List::item_autosize_changed(Item *item) { item->autosize(); signal_autosize_changed.emit(); - rebuild(); + mark_rebuild(); } -void List::check_view_range() +void List::reposition_items(bool record_rows) { - if(!style) + if(!items_part) return; - unsigned h = geom.h; - if(const Part *items_part = style->get_part("items")) + if(record_rows) { - const Sides &margin = items_part->get_margin(); - h -= margin.top+margin.bottom; + rows.clear(); + rows.push_back(0); } - max_scroll = items.size(); - for(unsigned i=items.size(); i-->0; ) + 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().h; - if(ih<=h) + const Geometry &igeom = items[i]->get_geometry(); + + if(view_mode!=GRID || (x>0 && x+igeom.w>view_w)) { - h -= ih; - --max_scroll; + x = 0; + if(y) + y -= row_h; + if(record_rows && i>0) + { + rows.back().height = row_h; + rows.push_back(i); + } + row_h = 0; } + + if(first_rowset_visible(false); + else if(igeom.h+margin.bottom<=y) + { + items[i]->set_visible(true); + 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) + items[j]->set_visible(false); + y = 0; + } + + x += igeom.w; + 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); } - if(first>max_scroll) - first = max_scroll; + 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::check_view_range() +{ + if(!style) + return; + + 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); + 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()) + return; + + unsigned focus_row = item_index_to_row(static_cast(focus_index)); + if(focus_row0 && !ignore_slider_change) { - first = 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) +{ + if(index>pos) + index += change; + else if(index==pos) + index = (change>0 ? index+change : -1); +} + List::DataObserver::DataObserver(List &l): list(l) @@ -302,8 +529,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); @@ -312,23 +539,28 @@ 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; - for(vector::iterator i=list.items.begin(); i!=list.items.end(); ++i) - delete *i; + list.focus_index = -1; + for(Item *i: list.items) + delete i; list.items.clear(); list.items_changed(); + + list.signal_selection_cleared.emit(); } void List::DataObserver::refresh_item(unsigned i) @@ -341,15 +573,20 @@ void List::DataObserver::refresh_item(unsigned i) } +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 Child *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); } @@ -365,8 +602,23 @@ 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 Child *c: children) + c->widget->render(renderer); + } +} + + +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")) + { + const Sides &margin = part->get_margin(); + child->set_position(margin.left, margin.bottom); } } @@ -377,12 +629,13 @@ void List::MultiColumnItem::check_widths(vector &widths) const widths.resize(children.size(), 0); unsigned n = 0; - for(list::const_iterator i=children.begin(); i!=children.end(); ++i, ++n) + for(const Child *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; } } @@ -398,10 +651,10 @@ void List::MultiColumnItem::set_widths(const vector &widths) 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) + for(const Child *c: children) { - (*i)->widget->set_position(x, margin.bottom); - x += widths[n]; + c->widget->set_position(x, margin.bottom); + x += widths[n++]; } } @@ -410,14 +663,14 @@ 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 Child *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(Item *i: list->items) + if(i!=this) + if(MultiColumnItem *mci = dynamic_cast(i)) mci->check_widths(widths); vector self_widths(widths); @@ -428,8 +681,8 @@ void List::MultiColumnItem::on_style_change() if(update_all) { - for(vector::const_iterator i=list->items.begin(); i!=list->items.end(); ++i) - if(MultiColumnItem *mci = dynamic_cast(*i)) + for(Item *i: list->items) + if(MultiColumnItem *mci = dynamic_cast(i)) mci->set_widths(self_widths); } @@ -443,25 +696,14 @@ List::BasicItem::BasicItem(const string &text): add(label); } -void List::BasicItem::on_style_change() -{ - if(!style) - return; - - label.autosize(); - if(const Part *part = style->get_part("children")) - { - const Sides &margin = part->get_margin(); - label.set_position(margin.left, margin.bottom); - } -} - List::Loader::Loader(List &l): DataFile::DerivedObjectLoader(l) { add("item", &Loader::item); - add("view_size", &List::view_size); + 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) @@ -469,5 +711,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