]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/entry.cpp
Refactor visible height calculation in Entry and Text
[libs/gltk.git] / source / entry.cpp
index 87f88c75eadd39f94929f21745b131f363a394e6..6d13ccdd0377eaaff11b29db0c27981d5b9cacc0 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);
@@ -55,7 +57,17 @@ void Entry::autosize_special(const Part &part, Geometry &ageom) const
 void Entry::set_text(const string &t)
 {
        text = t;
-       edit_pos = text.size();
+       set_edit_position(text.size());
+}
+
+void Entry::insert(unsigned pos, const string &t)
+{
+       text.insert(pos, t);
+
+       if(edit_pos>=pos)
+               edit_pos += t.size();
+       if(selection_active && selection_pos>=pos)
+               selection_pos += t.size();
 
        if(multiline)
                check_view_range();
@@ -63,6 +75,52 @@ void Entry::set_text(const string &t)
        rebuild();
 }
 
+void Entry::erase(unsigned pos, unsigned len)
+{
+       text.erase(pos, len);
+
+       if(edit_pos>=pos+len)
+               edit_pos -= len;
+       else if(edit_pos>=pos)
+               edit_pos = pos;
+       if(selection_active)
+       {
+               if(selection_pos>=pos+len)
+                       selection_pos -= len;
+               else if(selection_pos>=pos)
+                       selection_pos = pos;
+       }
+
+       if(multiline)
+               check_view_range();
+
+       rebuild();
+}
+
+void Entry::set_edit_position(unsigned pos)
+{
+       edit_pos = min(pos, text.size());
+       selection_active = false;
+
+       if(multiline)
+               check_view_range();
+
+       rebuild();
+}
+
+bool Entry::get_selection(unsigned &start, unsigned &end) const
+{
+       if(!selection_active)
+               return false;
+
+       start = selection_pos;
+       end = edit_pos;
+       if(start>end)
+               swap(start, end);
+
+       return true;
+}
+
 void Entry::set_edit_size(unsigned w, unsigned h)
 {
        edit_width = w;
@@ -110,6 +168,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,24 +244,53 @@ 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");
                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)), 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), 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;
 
@@ -161,8 +299,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();
@@ -188,28 +328,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
@@ -248,13 +368,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);
@@ -276,11 +440,7 @@ void Entry::check_view_range()
        if(!multiline || !text_part)
                return;
 
-       float font_size = style->get_font_size();
-       unsigned line_spacing = static_cast<unsigned>(font_size*6/5);
-
-       const Sides &margin = text_part->get_margin();
-       visible_rows = max((geom.h-margin.top-margin.bottom)/line_spacing, 1U);
+       visible_rows = text.get_visible_lines(*text_part, geom, 0);
 
        unsigned row, col;
        text.offset_to_coords(edit_pos, row, col);