]> git.tdb.fi Git - libs/gltk.git/commitdiff
Don't accept characters in Entry if there hasn't been a key press first
authorMikko Rasa <tdb@tdb.fi>
Sun, 9 Jun 2013 15:06:58 +0000 (18:06 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 9 Jun 2013 15:06:58 +0000 (18:06 +0300)
This rectifies a problem where a program reacts to a key press event by
focusing an Entry, which then receives the associated character event.

source/entry.cpp
source/entry.h

index 52a7b429d269e0013e71c44d7d918a9a8dca55b5..8aee0cf4962ee3ed6a4994a045c622a4631d153a 100644 (file)
@@ -20,7 +20,8 @@ Entry::Entry(const string &t):
        first_row(0),
        visible_rows(1),
        text_part(0),
-       slider(0)
+       slider(0),
+       got_key_press(false)
 {
        set_text(t);
 }
@@ -145,6 +146,7 @@ void Entry::render_special(const Part &part, GL::Renderer &renderer) const
 
 void Entry::key_press(unsigned key, unsigned)
 {
+       got_key_press = true;
        if(key==Input::KEY_LEFT)
        {
                if(edit_pos>0)
@@ -191,7 +193,7 @@ void Entry::key_press(unsigned key, unsigned)
 
 void Entry::character(wchar_t ch)
 {
-       if(ch>=' ')
+       if(got_key_press && ch>=' ')
        {
                text.insert(edit_pos, StringCodec::encode<StringCodec::Utf8>(StringCodec::ustring(1, ch)));
                ++edit_pos;
@@ -199,6 +201,12 @@ void Entry::character(wchar_t ch)
        }
 }
 
+void Entry::focus_out()
+{
+       Widget::focus_out();
+       got_key_press = false;
+}
+
 void Entry::on_geometry_change()
 {
        reposition_slider();
index 3ee0b2c2b2c132ef8164f0e66593dabcec07a620..65a201a66730bde448d7341c918c690c10bb3ccb 100644 (file)
@@ -38,6 +38,7 @@ private:
        unsigned visible_rows;
        const Part *text_part;
        VSlider *slider;
+       bool got_key_press;
 
 public:
        Entry(const std::string & = std::string());
@@ -58,6 +59,7 @@ private:
 public:
        virtual void key_press(unsigned, unsigned);
        virtual void character(wchar_t);
+       virtual void focus_out();
 private:
        virtual void on_geometry_change();
        virtual void on_style_change();