+#include <SDL/SDL_keysym.h>
+#include <msp/gl/matrix.h>
+#include <msp/gl/transform.h>
+#include "entry.h"
+#include "part.h"
+#include "style.h"
+
+using namespace std;
+
+namespace Msp {
+namespace GLtk {
+
+Entry::Entry(const Resources &r, const string &t):
+ Widget(r),
+ text(t),
+ edit_pos(0),
+ active(false)
+{
+}
+
+void Entry::set_text(const string &t)
+{
+ text=t;
+ if(edit_pos>text.size())
+ edit_pos=text.size();
+}
+
+void Entry::key_press(unsigned key, unsigned, wchar_t ch)
+{
+ if(key==SDLK_LEFT)
+ {
+ if(edit_pos>0)
+ --edit_pos;
+ }
+ else if(key==SDLK_RIGHT)
+ {
+ if(edit_pos<text.size())
+ ++edit_pos;
+ }
+ else if(key==SDLK_BACKSPACE)
+ {
+ if(edit_pos>0)
+ text.erase(--edit_pos, 1);
+ }
+ else
+ {
+ text+=ch;
+ }
+}
+
+void Entry::focus_in()
+{
+ active=true;
+}
+
+void Entry::focus_out()
+{
+ active=false;
+}
+
+void Entry::render_part(const Part &part) const
+{
+ if(part.get_name()=="text")
+ {
+ 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);
+
+ GL::push_matrix();
+
+ 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);
+
+ GL::pop_matrix();
+ render_text(part, text);
+ }
+ else
+ Widget::render_part(part);
+}
+
+} // namespace GLtk
+} // namespace Msp