]> git.tdb.fi Git - libs/gltk.git/commitdiff
Select list items upon navigation by default
authorMikko Rasa <tdb@tdb.fi>
Tue, 17 Sep 2019 11:55:09 +0000 (14:55 +0300)
committerMikko Rasa <tdb@tdb.fi>
Tue, 17 Sep 2019 11:55:09 +0000 (14:55 +0300)
With control pressed, only the focus is moved.

source/list.cpp
source/list.h

index a9cbcc1c5f02af4080ca895f18525dfcea61af1e..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"
@@ -234,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)
@@ -328,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)
index c0ea6d25a5a0b6353fd36bb45248f02659624610..3701387455c86f40c5f552a9e426fa2c38fd57c1 100644 (file)
@@ -194,6 +194,7 @@ private:
        virtual void render_special(const Part &, GL::Renderer &) const;
 
 public:
+       virtual bool key_press(unsigned, unsigned);
        virtual void button_press(int, int, unsigned);
        virtual void touch_press(int, int, unsigned);
        virtual void touch_release(int, int, unsigned);
@@ -201,6 +202,7 @@ public:
        virtual void focus_in();
        virtual bool navigate(Navigation);
 private:
+       void move_focus(Navigation, bool);
        void set_focus_index(int);
 
        void item_autosize_changed(Item *);