]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/list.cpp
Improve list focus management
[libs/gltk.git] / source / list.cpp
index 0f887f3a6641316a6610da9f403a4bc5abefb7f4..a5928ec91a76793d453ef67cba2bd062d89165be 100644 (file)
@@ -35,8 +35,11 @@ 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;
@@ -155,12 +158,17 @@ 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);
        }
 }
@@ -247,6 +255,50 @@ void List::button_press(int x, int y, unsigned btn)
        }
 }
 
+void List::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<unsigned>(focus_index+1)<items.size())
+                       set_focus_index(focus_index+1);
+       }
+       else if(nav==NAV_ACTIVATE)
+               set_selected_index(focus_index);
+       else
+               return false;
+
+       return true;
+}
+
+void List::set_focus_index(int i)
+{
+       focus_index = i;
+       if(focus_index>=0)
+       {
+               scroll_to_focus();
+               if(state&FOCUS)
+                       set_input_focus(items[focus_index]);
+       }
+}
+
 void List::item_autosize_changed(Item *item)
 {
        item->autosize();
@@ -254,29 +306,39 @@ void List::item_autosize_changed(Item *item)
        rebuild();
 }
 
-void List::check_view_range()
+unsigned List::last_to_first(unsigned last) const
 {
        if(!style)
-               return;
+               return last;
 
-       unsigned h = geom.h;
+       unsigned view_h = geom.h;
        if(const Part *items_part = style->get_part("items"))
        {
                const Sides &margin = items_part->get_margin();
-               h -= margin.top+margin.bottom;
+               view_h -= margin.top+margin.bottom;
        }
 
-       max_scroll = items.size();
-       for(unsigned i=items.size(); i-->0; )
+       unsigned items_h = 0;
+       for(unsigned i=last; i<items.size(); --i)
        {
-               unsigned ih = items[i]->get_geometry().h;
-               if(ih<=h)
-               {
-                       h -= ih;
-                       --max_scroll;
-               }
+               items_h += items[i]->get_geometry().h;
+               if(items_h>view_h)
+                       return min(i+1, last);
        }
 
+       return 0;
+}
+
+void List::check_view_range()
+{
+       if(!style)
+               return;
+
+       if(items.empty())
+               max_scroll = 0;
+       else
+               max_scroll = last_to_first(items.size()-1);
+
        if(first>max_scroll)
                first = max_scroll;
 
@@ -284,6 +346,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<unsigned>(focus_index)<first)
+               slider.set_value(max_scroll-focus_index);
+       else
+               slider.set_value(max_scroll-last_to_first(focus_index));
+}
+
 void List::slider_value_changed(double value)
 {
        if(max_scroll>0 && !ignore_slider_change)
@@ -293,6 +366,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)
@@ -305,8 +386,8 @@ List::DataObserver::DataObserver(List &l):
 
 void List::DataObserver::item_added(unsigned i)
 {
-       if(list.sel_index>=static_cast<int>(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);
@@ -316,10 +397,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<int>(i))
-               --list.sel_index;
-       else if(list.sel_index==static_cast<int>(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);
@@ -332,6 +411,7 @@ void List::DataObserver::item_removed(unsigned i)
 void List::DataObserver::cleared()
 {
        list.sel_index = -1;
+       list.focus_index = -1;
        for(vector<Item *>::iterator i=list.items.begin(); i!=list.items.end(); ++i)
                delete *i;
        list.items.clear();