From 474578fd04355c3247e8f9f6383d17e37c2c3015 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Mon, 16 Sep 2019 19:40:24 +0300 Subject: [PATCH] Add a signal for Entry widget text changes --- source/entry.cpp | 18 ++++++++++++++---- source/entry.h | 3 ++- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/source/entry.cpp b/source/entry.cpp index 54c5fb7..751f173 100644 --- a/source/entry.cpp +++ b/source/entry.cpp @@ -62,6 +62,9 @@ void Entry::set_text(const string &t) void Entry::insert(unsigned pos, const string &t) { + if(t.empty()) + return; + text.insert(pos, t); unsigned old_edit_pos = edit_pos; @@ -70,6 +73,7 @@ void Entry::insert(unsigned pos, const string &t) 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); @@ -81,6 +85,9 @@ void Entry::insert(unsigned pos, const string &t) void Entry::erase(unsigned pos, unsigned len) { + if(!len) + return; + text.erase(pos, len); unsigned old_edit_pos = edit_pos; @@ -96,6 +103,7 @@ void Entry::erase(unsigned pos, unsigned len) selection_pos = pos; } + signal_text_changed.emit(text.get()); if(edit_pos!=old_edit_pos) signal_edit_position_changed.emit(edit_pos); @@ -255,14 +263,14 @@ bool Entry::key_press(unsigned key, unsigned mod) if(key==Input::KEY_BACKSPACE) { if(selection_active) - erase_selection(); + erase_selection(true); else if(edit_pos>0) erase(edit_pos-1, 1); } else if(key==Input::KEY_DELETE) { if(selection_active) - erase_selection(); + erase_selection(true); else erase(edit_pos, 1); } @@ -299,7 +307,7 @@ bool Entry::character(wchar_t ch) if(got_key_press && ch>=' ' && ch!=0x7F) { if(selection_active) - erase_selection(); + erase_selection(false); insert(edit_pos, StringCodec::encode(StringCodec::ustring(1, ch))); return true; } @@ -406,13 +414,15 @@ void Entry::set_edit_position(unsigned ep, bool select) rebuild(); } -void Entry::erase_selection() +void Entry::erase_selection(bool emit_change) { unsigned start, end; if(!get_selection(start, end)) return; text.erase(start, end-start); + if(emit_change) + signal_text_changed.emit(text.get()); set_edit_position(start, false); } diff --git a/source/entry.h b/source/entry.h index 02d1f2e..58e45fb 100644 --- a/source/entry.h +++ b/source/entry.h @@ -33,6 +33,7 @@ public: sigc::signal signal_edit_position_changed; sigc::signal signal_scroll_position_changed; + sigc::signal signal_text_changed; // Deprecated sigc::signal signal_enter; @@ -99,7 +100,7 @@ private: void move_edit_position(Navigation, bool); void set_edit_position(unsigned, bool); - void erase_selection(); + void erase_selection(bool); void check_cursor_blink(); void check_view_range(); void slider_value_changed(double); -- 2.45.2