]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/entry.cpp
Enable loading of entry widgets from datafiles
[libs/gltk.git] / source / entry.cpp
index 6daa8ef1b1083c1361522d64bc3e845df228c2ad..eed2c39a942ced2e7299a6f357be9142cd6b4c15 100644 (file)
@@ -1,7 +1,15 @@
-#include <SDL/SDL_keysym.h>
+/* $Id$
+
+This file is part of libmspgltk
+Copyright © 2007  Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
 #include <msp/gl/matrix.h>
+#include <msp/gl/texture.h>
 #include <msp/gl/transform.h>
 #include "entry.h"
+#include "graphic.h"
 #include "part.h"
 #include "style.h"
 
@@ -13,9 +21,9 @@ namespace GLtk {
 Entry::Entry(const Resources &r, const string &t):
        Widget(r),
        text(t),
-       edit_pos(0),
-       active(false)
+       edit_pos(0)
 {
+       update_style();
 }
 
 void Entry::set_text(const string &t)
@@ -27,17 +35,17 @@ void Entry::set_text(const string &t)
 
 void Entry::key_press(unsigned key, unsigned, wchar_t ch)
 {
-       if(key==SDLK_LEFT)
+       if(key==100)
        {
                if(edit_pos>0)
                        --edit_pos;
        }
-       else if(key==SDLK_RIGHT)
+       else if(key==102)
        {
                if(edit_pos<text.size())
                        ++edit_pos;
        }
-       else if(key==SDLK_BACKSPACE)
+       else if(key==22)
        {
                if(edit_pos>0)
                        text.erase(--edit_pos, 1);
@@ -45,54 +53,57 @@ void Entry::key_press(unsigned key, unsigned, wchar_t ch)
        else
        {
                text+=ch;
+               ++edit_pos;
        }
 }
 
 void Entry::focus_in()
 {
-       active=true;
+       if(state!=DISABLED)
+               state=ACTIVE;
 }
 
 void Entry::focus_out()
 {
-       active=false;
+       if(state==ACTIVE)
+               state=NORMAL;
 }
 
-void Entry::render_part(const Part &part) const
+void Entry::render_special(const Part &part) const
 {
        if(part.get_name()=="text")
+               render_text(part, text);
+       else if(part.get_name()=="cursor")
        {
-               const GL::Font *const font=style->get_font();
+               if(!part.get_graphic(state))
+                       return;
 
+               const GL::Font *const font=style->get_font();
                const float font_size=font->get_default_size();
-               unsigned text_w=static_cast<unsigned>(font->get_string_width(text)*font_size);
+
+               const Geometry &pgeom=part.get_geometry();
+               unsigned gw=pgeom.w;
+               unsigned gh=(part.get_fill_y() ? geom.h : pgeom.h);
+
+               Geometry rgeom;
+               rgeom.w=static_cast<unsigned>(font->get_string_width(text)*font_size);
+               rgeom.h=(part.get_fill_y() ? geom.h : pgeom.h);
+               part.get_alignment().apply(rgeom, geom, part.get_margin());
+               rgeom.x+=static_cast<unsigned>(font->get_string_width(text.substr(0, edit_pos))*font_size);
 
                GL::push_matrix();
+               GL::translate(rgeom.x, rgeom.y, 0);
 
-               part.get_alignment().apply(geom, text_w, static_cast<unsigned>(font->get_ascent()*font_size));
-               GL::scale_uniform(font_size);
-
-               const Color &color=style->get_font_color();
-               glColor3f(color.r, color.g, color.b);
-               if(active)
-               {
-                       font->draw_string(text.substr(0, edit_pos));
-                       glBegin(GL_LINES);
-                       glVertex2f(0, 0);
-                       glVertex2f(0, 1);
-                       glEnd();
-                       font->draw_string(text.substr(edit_pos));
-               }
-               else
-                       font->draw_string(text);
-               glColor3f(1, 1, 1);
+               part.get_graphic(state)->render(gw, gh);
 
                GL::pop_matrix();
-               render_text(part, text);
        }
-       else
-               Widget::render_part(part);
 }
 
+
+Entry::Loader::Loader(Entry &ent):
+       Widget::Loader(ent)
+{ }
+
 } // namespace GLtk
 } // namespace Msp