]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/entry.cpp
Refactor edit position adjustments from Entry::insert and delete
[libs/gltk.git] / source / entry.cpp
index ed0d7e2081a270a90894277f619f33c4daf4aeb5..3212d85b1691a0082670f8dba996f1adc24193fe 100644 (file)
@@ -5,8 +5,8 @@
 #include "entry.h"
 #include "graphic.h"
 #include "part.h"
+#include "slider.h"
 #include "style.h"
-#include "vslider.h"
 
 using namespace std;
 
@@ -70,17 +70,9 @@ void Entry::insert(unsigned pos, const string &t)
                return;
 
        text.insert(pos, t);
-
-       unsigned old_edit_pos = edit_pos;
-       if(edit_pos>=pos)
-               edit_pos += t.size();
-       if(selection_active && selection_pos>=pos)
-               selection_pos += t.size();
-
        signal_text_changed.emit(text.get());
-       if(edit_pos!=old_edit_pos)
-               signal_edit_position_changed.emit(edit_pos);
 
+       adjust_edit_position_for_change(pos, t.size());
        if(multiline)
                check_view_range();
 
@@ -93,24 +85,9 @@ void Entry::erase(unsigned pos, unsigned len)
                return;
 
        text.erase(pos, len);
-
-       unsigned old_edit_pos = edit_pos;
-       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;
-       }
-
        signal_text_changed.emit(text.get());
-       if(edit_pos!=old_edit_pos)
-               signal_edit_position_changed.emit(edit_pos);
 
+       adjust_edit_position_for_change(pos, -len);
        if(multiline)
                check_view_range();
 
@@ -415,6 +392,29 @@ void Entry::move_edit_position(Navigation nav, bool select)
                throw invalid_argument("Entry::move_edit_position");
 }
 
+void Entry::adjust_edit_position_for_change(unsigned pos, int change)
+{
+       unsigned old_edit_pos = edit_pos;
+
+       if(change>0)
+       {
+               if(edit_pos>=pos)
+                       edit_pos += change;
+               if(selection_active && selection_pos>=pos)
+                       selection_pos += change;
+       }
+       else if(change<0)
+       {
+               if(edit_pos>=pos)
+                       edit_pos -= min<unsigned>(edit_pos-pos, -change);
+               if(selection_active)
+                       selection_pos -= min<unsigned>(selection_pos-pos, -change);
+       }
+
+       if(edit_pos!=old_edit_pos)
+               signal_edit_position_changed.emit(edit_pos);
+}
+
 void Entry::set_edit_position(unsigned ep, bool select)
 {
        if(select && !selection_active)
@@ -446,18 +446,21 @@ void Entry::erase_selection(bool emit_change)
 
 void Entry::check_cursor_blink()
 {
+       const Part *cursor_part = style->get_part("cursor");
+       bool has_blink = (cursor_part && cursor_part->get_graphic(ACTIVE|FOCUS)!=cursor_part->get_graphic(NORMAL|FOCUS));
+
        cursor_blink = (state&FOCUS);
-       if((state&FOCUS) && style)
+       if((state&FOCUS) && style && has_blink)
        {
-               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;
-               }
+               set_animation_interval(Time::sec/2);
+               mark_rebuild();
+       }
+       else
+       {
+               if(has_blink)
+                       mark_rebuild();
+               stop_animation();
        }
-
-       stop_animation();
 }
 
 void Entry::check_view_range()
@@ -484,6 +487,7 @@ void Entry::check_view_range()
                signal_scroll_position_changed.emit(first_row);
 
        slider->set_range(0, scroll);
+       slider->set_page_size(visible_rows);
        slider->set_value(scroll-first_row);
 }