]> git.tdb.fi Git - libs/gltk.git/blob - source/entry.cpp
9f7c761802f5de84af3e77a764788f929865cf7c
[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 "entry.h"
12 #include "graphic.h"
13 #include "part.h"
14 #include "style.h"
15
16 using namespace std;
17
18 namespace Msp {
19 namespace GLtk {
20
21 Entry::Entry(const Resources &r, const string &t):
22         Widget(r),
23         text(t),
24         edit_pos(0)
25 {
26         update_style();
27 }
28
29 void Entry::set_text(const string &t)
30 {
31         text=t;
32         if(edit_pos>text.size())
33                 edit_pos=text.size();
34 }
35
36 void Entry::key_press(unsigned key, unsigned, wchar_t ch)
37 {
38         if(key==100)
39         {
40                 if(edit_pos>0)
41                         --edit_pos;
42         }
43         else if(key==102)
44         {
45                 if(edit_pos<text.size())
46                         ++edit_pos;
47         }
48         else if(key==22)
49         {
50                 if(edit_pos>0)
51                         text.erase(--edit_pos, 1);
52         }
53         else
54         {
55                 text+=ch;
56                 ++edit_pos;
57         }
58 }
59
60 void Entry::render_special(const Part &part) const
61 {
62         if(part.get_name()=="text")
63                 render_text(part, text);
64         else if(part.get_name()=="cursor")
65         {
66                 if(!part.get_graphic(state))
67                         return;
68
69                 const GL::Font *const font=style->get_font();
70                 const float font_size=font->get_default_size();
71
72                 Geometry rgeom=part.get_geometry();
73                 rgeom.x=static_cast<unsigned>(font->get_string_width(text.substr(0, edit_pos))*font_size);
74                 rgeom.w=static_cast<unsigned>(font->get_string_width(text)*font_size);
75                 part.get_alignment().apply(rgeom, geom, part.get_margin());
76
77                 GL::push_matrix();
78                 GL::translate(rgeom.x, rgeom.y, 0);
79                 part.get_graphic(state)->render(part.get_geometry().w, rgeom.h);
80                 GL::pop_matrix();
81         }
82 }
83
84
85 Entry::Loader::Loader(Entry &ent):
86         Widget::Loader(ent)
87 { }
88
89 } // namespace GLtk
90 } // namespace Msp