]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/entry.cpp
Derive Root from Graphics::EventSource
[libs/gltk.git] / source / entry.cpp
index 9f7c761802f5de84af3e77a764788f929865cf7c..f5037215592e1a45143ab8b305cf563007c7dbe0 100644 (file)
@@ -8,6 +8,7 @@ Distributed under the LGPL
 #include <msp/gl/matrix.h>
 #include <msp/gl/texture.h>
 #include <msp/gl/transform.h>
+#include <msp/input/keys.h>
 #include "entry.h"
 #include "graphic.h"
 #include "part.h"
@@ -20,39 +21,41 @@ namespace GLtk {
 
 Entry::Entry(const Resources &r, const string &t):
        Widget(r),
-       text(t),
+       text(style),
        edit_pos(0)
 {
        update_style();
+       set_text(t);
 }
 
 void Entry::set_text(const string &t)
 {
        text=t;
-       if(edit_pos>text.size())
-               edit_pos=text.size();
+       edit_pos=text.size();
 }
 
 void Entry::key_press(unsigned key, unsigned, wchar_t ch)
 {
-       if(key==100)
+       if(key==Input::KEY_LEFT)
        {
                if(edit_pos>0)
                        --edit_pos;
        }
-       else if(key==102)
+       else if(key==Input::KEY_RIGHT)
        {
                if(edit_pos<text.size())
                        ++edit_pos;
        }
-       else if(key==22)
+       else if(key==Input::KEY_BACKSPACE)
        {
                if(edit_pos>0)
                        text.erase(--edit_pos, 1);
        }
-       else
+       else if(key==Input::KEY_ENTER)
+               signal_enter.emit();
+       else if(ch>=' ')
        {
-               text+=ch;
+               text.insert(edit_pos, Codecs::encode<Codecs::Utf8>(Codecs::ustring(1, ch)));
                ++edit_pos;
        }
 }
@@ -60,7 +63,7 @@ void Entry::key_press(unsigned key, unsigned, wchar_t ch)
 void Entry::render_special(const Part &part) const
 {
        if(part.get_name()=="text")
-               render_text(part, text);
+               text.render(part, geom);
        else if(part.get_name()=="cursor")
        {
                if(!part.get_graphic(state))
@@ -70,8 +73,8 @@ void Entry::render_special(const Part &part) const
                const float font_size=font->get_default_size();
 
                Geometry rgeom=part.get_geometry();
-               rgeom.x=static_cast<unsigned>(font->get_string_width(text.substr(0, edit_pos))*font_size);
-               rgeom.w=static_cast<unsigned>(font->get_string_width(text)*font_size);
+               rgeom.x=static_cast<unsigned>(font->get_string_width(text.get().substr(0, edit_pos))*font_size);
+               rgeom.w=static_cast<unsigned>(font->get_string_width(text.get())*font_size);
                part.get_alignment().apply(rgeom, geom, part.get_margin());
 
                GL::push_matrix();