X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flist.cpp;h=a9cbcc1c5f02af4080ca895f18525dfcea61af1e;hb=35f6cfcfb7a7278251d890988fdf6a6bbf793ef8;hp=b9713422f11236fa86bc23a87a64f59523365569;hpb=db2a069cd5ec4c8809c2ba6ddeec2ddcd32d49ef;p=libs%2Fgltk.git diff --git a/source/list.cpp b/source/list.cpp index b971342..a9cbcc1 100644 --- a/source/list.cpp +++ b/source/list.cpp @@ -35,12 +35,18 @@ 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; + dragging = false; + drag_start_x = 0; + drag_start_y = 0; observer = new DataObserver(*this); @@ -155,16 +161,28 @@ void List::set_selected_index(int i) 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=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") @@ -236,17 +254,109 @@ void List::button_press(int x, int y, unsigned btn) { 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) { - for(unsigned i=first; (iis_visible()); ++i) - if(click_focus==items[i]) - { - set_selected_index(i); - break; - } + 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 && firstget_geometry().h; + if(dy>item_h) + { + drag_start_y += item_h; + slider.set_value(max_scroll-(first+1)); + } + } + else if(dy<0 && first>0) + { + int item_h = items[first-1]->get_geometry().h; + if(-dy>item_h) + { + drag_start_y -= item_h; + slider.set_value(max_scroll-(first-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(first); + } +} + +bool List::navigate(Navigation nav) +{ + 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)=0) + { + scroll_to_focus(); + if(state&FOCUS) + set_input_focus(items[focus_index]); + } +} + void List::item_autosize_changed(Item *item) { item->autosize(); @@ -294,6 +404,17 @@ 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 && !ignore_slider_change) @@ -303,6 +424,14 @@ void List::slider_value_changed(double value) } } +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) @@ -315,8 +444,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); @@ -326,10 +455,8 @@ void List::DataObserver::item_added(unsigned i) void List::DataObserver::item_removed(unsigned i) { bool had_selection = (list.sel_index>=0); - if(list.sel_index>static_cast(i)) - --list.sel_index; - else if(list.sel_index==static_cast(i)) - list.sel_index = -1; + 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); @@ -342,6 +469,7 @@ void List::DataObserver::item_removed(unsigned i) 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();