From: Mikko Rasa Date: Wed, 25 Sep 2019 19:10:27 +0000 (+0300) Subject: Refactor edit position adjustments from Entry::insert and delete X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=43625734822bdaaea4e42505b7040d404298721c;p=libs%2Fgltk.git Refactor edit position adjustments from Entry::insert and delete --- diff --git a/source/entry.cpp b/source/entry.cpp index 4c1713a..3212d85 100644 --- a/source/entry.cpp +++ b/source/entry.cpp @@ -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(edit_pos-pos, -change); + if(selection_active) + selection_pos -= min(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) diff --git a/source/entry.h b/source/entry.h index bd1fe2c..da29a51 100644 --- a/source/entry.h +++ b/source/entry.h @@ -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();