]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/dropdown.cpp
Implement keyboard navigation for most widgets
[libs/gltk.git] / source / dropdown.cpp
index 8cc6187f2243a552b0943dde919dff6e6c0d18cb..ef0fa22f97c8f150009988b3487c716b1e576cf2 100644 (file)
@@ -25,8 +25,7 @@ Dropdown::Dropdown(ListData &d):
 
 void Dropdown::init()
 {
-       // Necessary to have the parent container raise the dropdown on click
-       input_type = INPUT_TEXT;
+       input_type = INPUT_NAVIGATION;
 
        dropped = false;
 
@@ -86,21 +85,27 @@ void Dropdown::button_press(int x, int y, unsigned btn)
        {
                Container::button_press(x, y, btn);
                if(!click_focus)
-               {
-                       dropped = false;
-                       list.set_visible(false);
-                       clear_state(ACTIVE);
-                       signal_ungrab_pointer.emit();
-               }
+                       close_list();
        }
        else if(btn==1)
+               open_list();
+}
+
+bool Dropdown::navigate(Navigation nav)
+{
+       if(dropped)
        {
-               dropped = true;
-               list.set_visible(true);
-               resize_list();
-               set_state(ACTIVE);
-               signal_grab_pointer.emit();
+               if(nav==NAV_CANCEL)
+                       close_list();
+               else
+                       list.navigate(nav);
        }
+       else if(nav==NAV_ACTIVATE)
+               open_list();
+       else
+               return false;
+
+       return true;
 }
 
 void Dropdown::on_geometry_change()
@@ -116,6 +121,24 @@ void Dropdown::on_style_change()
                resize_list();
 }
 
+void Dropdown::open_list()
+{
+       dropped = true;
+       list.set_visible(true);
+       resize_list();
+       set_state(ACTIVE);
+       set_input_focus(&list);
+       signal_grab_pointer.emit();
+}
+
+void Dropdown::close_list()
+{
+       dropped = false;
+       list.set_visible(false);
+       clear_state(ACTIVE);
+       signal_ungrab_pointer.emit();
+}
+
 void Dropdown::list_autosize_changed()
 {
        if(dropped)