]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/entry.cpp
Handle home and end keys in Entry widget
[libs/gltk.git] / source / entry.cpp
index 6616054f799b2be0f5dd1cf445989a6771ff30bd..9f99e794c5e3c7cb98fc0564242af21aac79f172 100644 (file)
@@ -23,7 +23,8 @@ Entry::Entry(const string &t):
        visible_rows(1),
        text_part(0),
        slider(0),
-       got_key_press(false)
+       got_key_press(false),
+       cursor_blink(true)
 {
        input_type = INPUT_TEXT;
        set_text(t);
@@ -92,7 +93,8 @@ void Entry::rebuild_special(const Part &part)
                text.build(part, state, geom, first_row, part_cache);
        else if(part.get_name()=="cursor")
        {
-               const Graphic *graphic = part.get_graphic(state);
+               State cursor_state = (cursor_blink ? ACTIVE : NORMAL);
+               const Graphic *graphic = part.get_graphic(state|cursor_state);
                if(!text_part || !graphic || !graphic->get_texture())
                        return;
 
@@ -126,6 +128,13 @@ 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;
@@ -144,6 +153,18 @@ bool Entry::key_press(unsigned key, unsigned)
                check_view_range();
                rebuild();
        }
+       else if(key==Input::KEY_END)
+       {
+               unsigned row, col;
+               text.offset_to_coords(edit_pos, row, col);
+               set_edit_position(text.coords_to_offset(row, text.get_line_length(row)));
+       }
+       else if(key==Input::KEY_HOME)
+       {
+               unsigned row, col;
+               text.offset_to_coords(edit_pos, row, col);
+               set_edit_position(text.coords_to_offset(row, 0));
+       }
        else
                return false;
 
@@ -163,10 +184,18 @@ bool Entry::character(wchar_t ch)
        return false;
 }
 
+void Entry::focus_in()
+{
+       cursor_blink = true;
+       Widget::focus_in();
+       check_cursor_blink();
+}
+
 void Entry::focus_out()
 {
        Widget::focus_out();
        got_key_press = false;
+       check_cursor_blink();
 }
 
 bool Entry::navigate(Navigation nav)
@@ -201,6 +230,12 @@ bool Entry::navigate(Navigation nav)
        return true;
 }
 
+void Entry::animate(const Time::TimeDelta &)
+{
+       cursor_blink = !cursor_blink;
+       rebuild();
+}
+
 void Entry::on_geometry_change()
 {
        if(multiline)
@@ -221,6 +256,8 @@ void Entry::on_style_change()
 
        if(multiline)
                check_view_range();
+
+       check_cursor_blink();
 }
 
 void Entry::set_edit_position(unsigned ep)
@@ -230,6 +267,22 @@ void Entry::set_edit_position(unsigned ep)
        rebuild();
 }
 
+void Entry::check_cursor_blink()
+{
+       cursor_blink = (state&FOCUS);
+       if((state&FOCUS) && style)
+       {
+               const Part *cursor_part = style->get_part("cursor");
+               if(cursor_part && cursor_part->get_graphic(ACTIVE|FOCUS)!=cursor_part->get_graphic(NORMAL|FOCUS))
+               {
+                       set_animation_interval(Time::sec/2);
+                       return;
+               }
+       }
+
+       stop_animation();
+}
+
 void Entry::check_view_range()
 {
        if(!multiline || !text_part)