]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/entry.cpp
Focus entry widget on touch press
[libs/gltk.git] / source / entry.cpp
index 43dad5e7de46dbcb69569427c083352eee0aabac..8d3c47f82331ab5e3eb643b3e8af0f2941268b72 100644 (file)
@@ -25,6 +25,7 @@ Entry::Entry(const string &t):
        slider(0),
        got_key_press(false)
 {
+       input_type = INPUT_TEXT;
        set_text(t);
 }
 
@@ -125,32 +126,17 @@ void Entry::render_special(const Part &part, GL::Renderer &renderer) const
                slider->render(renderer);
 }
 
+void Entry::touch_press(int x, int y, unsigned finger)
+{
+       if(finger==0)
+               set_focus();
+       Widget::touch_press(x, y, finger);
+}
+
 bool Entry::key_press(unsigned key, unsigned)
 {
        got_key_press = true;
-       if(key==Input::KEY_LEFT)
-       {
-               if(edit_pos>0)
-                       set_edit_position(edit_pos-1);
-       }
-       else if(key==Input::KEY_RIGHT)
-       {
-               if(edit_pos<text.size())
-                       set_edit_position(edit_pos+1);
-       }
-       else if(key==Input::KEY_DOWN && multiline)
-       {
-               unsigned row, col;
-               text.offset_to_coords(edit_pos, row, col);
-               set_edit_position(text.coords_to_offset(row+1, col));
-       }
-       else if(key==Input::KEY_UP && multiline)
-       {
-               unsigned row, col;
-               text.offset_to_coords(edit_pos, row, col);
-               set_edit_position(row>0 ? text.coords_to_offset(row-1, col) : 0);
-       }
-       else if(key==Input::KEY_BACKSPACE)
+       if(key==Input::KEY_BACKSPACE)
        {
                if(edit_pos>0)
                {
@@ -159,16 +145,11 @@ bool Entry::key_press(unsigned key, unsigned)
                        rebuild();
                }
        }
-       else if(key==Input::KEY_ENTER)
+       else if(key==Input::KEY_ENTER && multiline)
        {
-               if(multiline)
-               {
-                       text.insert(edit_pos++, "\n");
-                       check_view_range();
-                       rebuild();
-               }
-               else
-                       signal_enter.emit();
+               text.insert(edit_pos++, "\n");
+               check_view_range();
+               rebuild();
        }
        else
                return false;
@@ -195,6 +176,38 @@ void Entry::focus_out()
        got_key_press = false;
 }
 
+bool Entry::navigate(Navigation nav)
+{
+       if(nav==NAV_LEFT)
+       {
+               if(edit_pos>0)
+                       set_edit_position(edit_pos-1);
+       }
+       else if(nav==NAV_RIGHT)
+       {
+               if(edit_pos<text.size())
+                       set_edit_position(edit_pos+1);
+       }
+       else if(nav==NAV_DOWN && multiline)
+       {
+               unsigned row, col;
+               text.offset_to_coords(edit_pos, row, col);
+               set_edit_position(text.coords_to_offset(row+1, col));
+       }
+       else if(nav==NAV_UP && multiline)
+       {
+               unsigned row, col;
+               text.offset_to_coords(edit_pos, row, col);
+               set_edit_position(row>0 ? text.coords_to_offset(row-1, col) : 0);
+       }
+       else if(nav==NAV_ACCEPT && !signal_enter.empty())
+               signal_enter.emit();
+       else
+               return false;
+
+       return true;
+}
+
 void Entry::on_geometry_change()
 {
        if(multiline)