]> git.tdb.fi Git - libs/gltk.git/commitdiff
Refactor edit position adjustments from Entry::insert and delete
authorMikko Rasa <tdb@tdb.fi>
Wed, 25 Sep 2019 19:10:27 +0000 (22:10 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 25 Sep 2019 19:10:27 +0000 (22:10 +0300)
source/entry.cpp
source/entry.h

index 4c1713a022306d4b7670ef3953be2a62457f85a9..3212d85b1691a0082670f8dba996f1adc24193fe 100644 (file)
@@ -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)
index bd1fe2cf22a112a28021622281d54dbdf47e5900..da29a5134f5fd4c3e7e9a86e58eeade3530057c4 100644 (file)
@@ -99,6 +99,7 @@ private:
        virtual void on_style_change();
 
        void move_edit_position(Navigation, bool);
+       void adjust_edit_position_for_change(unsigned, int);
        void set_edit_position(unsigned, bool);
        void erase_selection(bool);
        void check_cursor_blink();