]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/entry.cpp
Don't accept characters in Entry if there hasn't been a key press first
[libs/gltk.git] / source / entry.cpp
index 6e2063d04abec1de23c73baeb8417094c9ca0a73..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);
 }
@@ -110,7 +111,8 @@ void Entry::rebuild_special(const Part &part, CachedPart &cache)
                text.build(part, geom, first_row, cache);
        else if(part.get_name()=="cursor")
        {
-               if(!text_part || !part.get_graphic(state))
+               const Graphic *graphic = part.get_graphic(state);
+               if(!text_part || !graphic)
                {
                        cache.texture = 0;
                        return;
@@ -127,12 +129,12 @@ void Entry::rebuild_special(const Part &part, CachedPart &cache)
 
                Geometry rgeom = text.coords_to_geometry(*text_part, geom, first_row, row, col);
 
-               cache.texture = part.get_graphic(state)->get_texture();
+               cache.texture = graphic->get_texture();
                cache.clear_mesh();
 
                GL::MeshBuilder bld(*cache.mesh);
                bld.matrix() *= GL::Matrix::translation(rgeom.x, rgeom.y, 0);
-               part.get_graphic(state)->build(part.get_geometry().w, part.get_geometry().h, bld);
+               graphic->build(part.get_geometry().w, part.get_geometry().h, bld);
        }
 }
 
@@ -144,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)
@@ -190,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;
@@ -198,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();