]> git.tdb.fi Git - libs/gltk.git/commitdiff
Add a signal for Entry widget text changes
authorMikko Rasa <tdb@tdb.fi>
Mon, 16 Sep 2019 16:40:24 +0000 (19:40 +0300)
committerMikko Rasa <tdb@tdb.fi>
Mon, 16 Sep 2019 16:40:24 +0000 (19:40 +0300)
source/entry.cpp
source/entry.h

index 54c5fb7cbcdd49fc48f06dae0761c9843fd6b0de..751f173d0c234a36a10c340987112fa6a7dd1605 100644 (file)
@@ -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::Utf8>(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);
 }
 
index 02d1f2e50e5a49596864ed1d41f4d0667476f267..58e45fbc7ac5014cd1d7b343b955bdecc32dcf14 100644 (file)
@@ -33,6 +33,7 @@ public:
 
        sigc::signal<void, unsigned> signal_edit_position_changed;
        sigc::signal<void, unsigned> signal_scroll_position_changed;
+       sigc::signal<void, const std::string &> signal_text_changed;
 
        // Deprecated
        sigc::signal<void> 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);