]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/list.cpp
Select list items upon navigation by default
[libs/gltk.git] / source / list.cpp
index a5928ec91a76793d453ef67cba2bd062d89165be..5d179e195c36c412785a17ec7d731f65856f420d 100644 (file)
@@ -2,6 +2,7 @@
 #include <msp/debug/demangle.h>
 #include <msp/gl/matrix.h>
 #include <msp/gl/meshbuilder.h>
+#include <msp/input/keys.h>
 #include "graphic.h"
 #include "list.h"
 #include "part.h"
@@ -44,6 +45,9 @@ void List::init()
        max_scroll = 0;
        view_size = 5;
        ignore_slider_change = false;
+       dragging = false;
+       drag_start_x = 0;
+       drag_start_y = 0;
 
        observer = new DataObserver(*this);
 
@@ -173,6 +177,13 @@ void List::set_selected_index(int i)
        }
 }
 
+void List::set_selected_item(Widget *item)
+{
+       for(unsigned i=first; (i<items.size() && items[i]->is_visible()); ++i)
+               if(item==items[i])
+                       return set_selected_index(i);
+}
+
 void List::rebuild_special(const Part &part)
 {
        if(part.get_name()=="slider")
@@ -224,6 +235,18 @@ void List::render_special(const Part &part, GL::Renderer &renderer) const
                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)
@@ -244,19 +267,67 @@ 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; (i<items.size() && items[i]->is_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 && first<max_scroll)
+               {
+                       int item_h = items[first]->get_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
@@ -270,22 +341,31 @@ void List::focus_in()
 
 bool List::navigate(Navigation nav)
 {
-       if(nav==NAV_UP && !items.empty())
+       if((nav==NAV_UP || nav==NAV_DOWN) && !items.empty())
+               move_focus(nav, true);
+       else if(nav==NAV_ACTIVATE)
+               set_selected_index(focus_index);
+       else
+               return false;
+
+       return true;
+}
+
+void List::move_focus(Navigation nav, bool select)
+{
+       if(nav==NAV_UP)
        {
                if(focus_index>0)
                        set_focus_index(focus_index-1);
        }
-       else if(nav==NAV_DOWN && !items.empty())
+       else if(nav==NAV_DOWN)
        {
                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;
+       if(select)
+               set_selected_index(focus_index);
 }
 
 void List::set_focus_index(int i)