]> git.tdb.fi Git - libs/gltk.git/blob - source/entry.cpp
Add Widget::set_focus
[libs/gltk.git] / source / entry.cpp
1 /* $Id$
2
3 This file is part of libmspgltk
4 Copyright © 2007  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <msp/gl/matrix.h>
9 #include <msp/gl/texture.h>
10 #include <msp/gl/transform.h>
11 #include <msp/input/keys.h>
12 #include "entry.h"
13 #include "graphic.h"
14 #include "part.h"
15 #include "style.h"
16
17 using namespace std;
18
19 #include <iostream>
20
21 namespace Msp {
22 namespace GLtk {
23
24 Entry::Entry(const Resources &r, const string &t):
25         Widget(r),
26         text(t),
27         edit_pos(text.size())
28 {
29         update_style();
30 }
31
32 void Entry::set_text(const string &t)
33 {
34         text=t;
35         edit_pos=text.size();
36 }
37
38 void Entry::key_press(unsigned key, unsigned, wchar_t ch)
39 {
40         if(key==Input::KEY_LEFT)
41         {
42                 if(edit_pos>0)
43                         --edit_pos;
44         }
45         else if(key==Input::KEY_RIGHT)
46         {
47                 if(edit_pos<text.size())
48                         ++edit_pos;
49         }
50         else if(key==Input::KEY_BACKSPACE)
51         {
52                 if(edit_pos>0)
53                         text.erase(--edit_pos, 1);
54         }
55         else if(key==Input::KEY_ENTER)
56                 signal_enter.emit();
57         else if(ch>=' ')
58         {
59                 text.insert(edit_pos, Codecs::encode<Codecs::Utf8>(Codecs::ustring(1, ch)));
60                 ++edit_pos;
61         }
62 }
63
64 void Entry::render_special(const Part &part) const
65 {
66         if(part.get_name()=="text")
67                 render_text(part, text);
68         else if(part.get_name()=="cursor")
69         {
70                 if(!part.get_graphic(state))
71                         return;
72
73                 const GL::Font *const font=style->get_font();
74                 const float font_size=font->get_default_size();
75
76                 Geometry rgeom=part.get_geometry();
77                 rgeom.x=static_cast<unsigned>(font->get_string_width(text.substr(0, edit_pos))*font_size);
78                 rgeom.w=static_cast<unsigned>(font->get_string_width(text)*font_size);
79                 part.get_alignment().apply(rgeom, geom, part.get_margin());
80
81                 GL::push_matrix();
82                 GL::translate(rgeom.x, rgeom.y, 0);
83                 part.get_graphic(state)->render(part.get_geometry().w, rgeom.h);
84                 GL::pop_matrix();
85         }
86 }
87
88
89 Entry::Loader::Loader(Entry &ent):
90         Widget::Loader(ent)
91 { }
92
93 } // namespace GLtk
94 } // namespace Msp