From: Mikko Rasa Date: Thu, 1 Sep 2016 16:42:41 +0000 (+0300) Subject: Improve list focus management X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=6762feb95d685b556ae97a9cd1ce964d2b2ee5fe;p=libs%2Fgltk.git Improve list focus management Now that containers no longer get an implicit navigation event when focused, list must take care of focusing the correct item itself. --- diff --git a/source/list.cpp b/source/list.cpp index 6496018..a5928ec 100644 --- a/source/list.cpp +++ b/source/list.cpp @@ -167,7 +167,8 @@ void List::set_selected_index(int i) sel_index = i; focus_index = i; items[sel_index]->set_active(true); - set_input_focus(items[focus_index]); + if(state&FOCUS) + set_input_focus(items[focus_index]); signal_item_selected.emit(sel_index); } } @@ -256,41 +257,28 @@ void List::button_press(int x, int y, unsigned btn) void List::focus_in() { - if(focus_index<0) - focus_index = sel_index; - - if(focus_index>=0) - { - scroll_to_focus(); + 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) + if(nav==NAV_UP && !items.empty()) { - if(focus_index<0 && !items.empty()) - focus_index = items.size()-1; - else if(focus_index>0) - --focus_index; - else - return false; - - scroll_to_focus(); - set_input_focus(items[focus_index]); + if(focus_index>0) + set_focus_index(focus_index-1); } - else if(nav==NAV_DOWN) + else if(nav==NAV_DOWN && !items.empty()) { - if(focus_index<0 && !items.empty()) - focus_index = 0; - else if(static_cast(focus_index+1)(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(); diff --git a/source/list.h b/source/list.h index 78954fc..5a95b08 100644 --- a/source/list.h +++ b/source/list.h @@ -193,8 +193,9 @@ public: virtual void button_press(int, int, unsigned); virtual void focus_in(); virtual bool navigate(Navigation); - private: + void set_focus_index(int); + void item_autosize_changed(Item *); unsigned last_to_first(unsigned) const; void check_view_range();