]> git.tdb.fi Git - libs/gltk.git/commitdiff
Implement a selection feature in Entry widget
authorMikko Rasa <tdb@tdb.fi>
Wed, 11 Sep 2019 07:49:15 +0000 (10:49 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 11 Sep 2019 07:49:15 +0000 (10:49 +0300)
source/entry.cpp
source/entry.h

index 9f99e794c5e3c7cb98fc0564242af21aac79f172..af6fcc6d0fd31ed5cfdbdc03f4fda57e9db6c55c 100644 (file)
@@ -24,7 +24,9 @@ Entry::Entry(const string &t):
        text_part(0),
        slider(0),
        got_key_press(false),
-       cursor_blink(true)
+       cursor_blink(true),
+       selection_active(false),
+       selection_pos(0)
 {
        input_type = INPUT_TEXT;
        set_text(t);
@@ -110,6 +112,57 @@ void Entry::rebuild_special(const Part &part)
                bld.matrix() *= GL::Matrix::translation(rgeom.x, rgeom.y, 0);
                graphic->build(part.get_geometry().w, part.get_geometry().h, bld);
        }
+       else if(part.get_name()=="selection")
+       {
+               if(!selection_active)
+                       return;
+
+               const Graphic *graphic = part.get_graphic(state);
+               if(!text_part || !graphic || !graphic->get_texture())
+                       return;
+
+               unsigned start = selection_pos;
+               unsigned end = edit_pos;
+               if(start>end)
+                       swap(start, end);
+
+               unsigned row, col;
+               text.offset_to_coords(start, row, col);
+               unsigned end_row, end_col;
+               text.offset_to_coords(end, end_row, end_col);
+
+               if(end_row<first_row || row>=first_row+visible_rows)
+                       return;
+
+               if(row<first_row)
+               {
+                       row = first_row;
+                       col = 0;
+               }
+
+               if(end_row>=first_row+visible_rows)
+               {
+                       end_row = first_row+visible_rows-1;
+                       end_col = text.get_line_length(end_row);
+               }
+
+               while(row<=end_row)
+               {
+                       unsigned ec = (row==end_row ? end_col : text.get_line_length(row));
+                       if(ec>col)
+                       {
+                               Geometry rgeom = text.coords_to_geometry(*text_part, geom, first_row, row, col);
+                               Geometry egeom = text.coords_to_geometry(*text_part, geom, first_row, row, ec);
+
+                               GL::MeshBuilder bld(part_cache.create_mesh(part, *graphic->get_texture()));
+                               bld.matrix() *= GL::Matrix::translation(rgeom.x, rgeom.y, 0);
+                               graphic->build(egeom.x-rgeom.x, part.get_geometry().h, bld);
+                       }
+
+                       ++row;
+                       col = 0;
+               }
+       }
        else if(part.get_name()=="slider")
        {
                if(multiline)
@@ -135,18 +188,27 @@ void Entry::touch_press(int x, int y, unsigned finger)
        Widget::touch_press(x, y, finger);
 }
 
-bool Entry::key_press(unsigned key, unsigned)
+bool Entry::key_press(unsigned key, unsigned mod)
 {
        got_key_press = true;
        if(key==Input::KEY_BACKSPACE)
        {
-               if(edit_pos>0)
+               if(selection_active)
+                       erase_selection();
+               else if(edit_pos>0)
                {
                        text.erase(--edit_pos, 1);
                        check_view_range();
                        rebuild();
                }
        }
+       else if(key==Input::KEY_DELETE)
+       {
+               if(selection_active)
+                       erase_selection();
+               else
+                       text.erase(edit_pos, 1);
+       }
        else if(key==Input::KEY_ENTER && multiline)
        {
                text.insert(edit_pos++, "\n");
@@ -157,14 +219,22 @@ bool Entry::key_press(unsigned key, unsigned)
        {
                unsigned row, col;
                text.offset_to_coords(edit_pos, row, col);
-               set_edit_position(text.coords_to_offset(row, text.get_line_length(row)));
+               set_edit_position(text.coords_to_offset(row, text.get_line_length(row)), mod==MOD_SHIFT);
        }
        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));
+               set_edit_position(text.coords_to_offset(row, 0), mod==MOD_SHIFT);
        }
+       else if(key==Input::KEY_LEFT && mod==MOD_SHIFT)
+               move_edit_position(NAV_LEFT, true);
+       else if(key==Input::KEY_RIGHT && mod==MOD_SHIFT)
+               move_edit_position(NAV_RIGHT, true);
+       else if(key==Input::KEY_UP && mod==MOD_SHIFT && multiline)
+               move_edit_position(NAV_UP, true);
+       else if(key==Input::KEY_DOWN && mod==MOD_SHIFT && multiline)
+               move_edit_position(NAV_DOWN, true);
        else
                return false;
 
@@ -173,8 +243,10 @@ bool Entry::key_press(unsigned key, unsigned)
 
 bool Entry::character(wchar_t ch)
 {
-       if(got_key_press && ch>=' ')
+       if(got_key_press && ch>=' ' && ch!=0x7F)
        {
+               if(selection_active)
+                       erase_selection();
                text.insert(edit_pos, StringCodec::encode<StringCodec::Utf8>(StringCodec::ustring(1, ch)));
                ++edit_pos;
                rebuild();
@@ -200,28 +272,8 @@ void Entry::focus_out()
 
 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);
-       }
+       if(nav==NAV_LEFT || nav==NAV_RIGHT || ((nav==NAV_DOWN || nav==NAV_UP) && multiline))
+               move_edit_position(nav, false);
        else if(nav==NAV_ACCEPT && !signal_enter.empty())
                signal_enter.emit();
        else
@@ -260,13 +312,57 @@ void Entry::on_style_change()
        check_cursor_blink();
 }
 
-void Entry::set_edit_position(unsigned ep)
+void Entry::move_edit_position(Navigation nav, bool select)
 {
+       if(nav==NAV_LEFT)
+       {
+               if(edit_pos>0)
+                       set_edit_position(edit_pos-1, select);
+       }
+       else if(nav==NAV_RIGHT)
+       {
+               if(edit_pos<text.size())
+                       set_edit_position(edit_pos+1, select);
+       }
+       else if(nav==NAV_DOWN)
+       {
+               unsigned row, col;
+               text.offset_to_coords(edit_pos, row, col);
+               set_edit_position(text.coords_to_offset(row+1, col), select);
+       }
+       else if(nav==NAV_UP)
+       {
+               unsigned row, col;
+               text.offset_to_coords(edit_pos, row, col);
+               set_edit_position((row>0 ? text.coords_to_offset(row-1, col) : 0), select);
+       }
+}
+
+void Entry::set_edit_position(unsigned ep, bool select)
+{
+       if(select && !selection_active)
+               selection_pos = edit_pos;
+       selection_active = select;
+
        edit_pos = ep;
        check_view_range();
        rebuild();
 }
 
+void Entry::erase_selection()
+{
+       if(!selection_active)
+               return;
+
+       unsigned start = selection_pos;
+       unsigned end = edit_pos;
+       if(start>end)
+               swap(start, end);
+
+       text.erase(start, end-start);
+       set_edit_position(start, false);
+}
+
 void Entry::check_cursor_blink()
 {
        cursor_blink = (state&FOCUS);
index cd80ab0ac573b860b44775dbaa1aefbe6b88af2e..a5b03fcdd64e77571237f62a4ec4e81c90111ff9 100644 (file)
@@ -46,6 +46,8 @@ private:
        VSlider *slider;
        bool got_key_press;
        bool cursor_blink;
+       bool selection_active;
+       unsigned selection_pos;
 
 public:
        Entry(const std::string & = std::string());
@@ -82,7 +84,9 @@ private:
        virtual void on_geometry_change();
        virtual void on_style_change();
 
-       void set_edit_position(unsigned);
+       void move_edit_position(Navigation, bool);
+       void set_edit_position(unsigned, bool);
+       void erase_selection();
        void check_cursor_blink();
        void check_view_range();
        void slider_value_changed(double);