]> git.tdb.fi Git - libs/gltk.git/blob - source/entry.cpp
eed2c39a942ced2e7299a6f357be9142cd6b4c15
[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::focus_in()
61 {
62         if(state!=DISABLED)
63                 state=ACTIVE;
64 }
65
66 void Entry::focus_out()
67 {
68         if(state==ACTIVE)
69                 state=NORMAL;
70 }
71
72 void Entry::render_special(const Part &part) const
73 {
74         if(part.get_name()=="text")
75                 render_text(part, text);
76         else if(part.get_name()=="cursor")
77         {
78                 if(!part.get_graphic(state))
79                         return;
80
81                 const GL::Font *const font=style->get_font();
82                 const float font_size=font->get_default_size();
83
84                 const Geometry &pgeom=part.get_geometry();
85                 unsigned gw=pgeom.w;
86                 unsigned gh=(part.get_fill_y() ? geom.h : pgeom.h);
87
88                 Geometry rgeom;
89                 rgeom.w=static_cast<unsigned>(font->get_string_width(text)*font_size);
90                 rgeom.h=(part.get_fill_y() ? geom.h : pgeom.h);
91                 part.get_alignment().apply(rgeom, geom, part.get_margin());
92                 rgeom.x+=static_cast<unsigned>(font->get_string_width(text.substr(0, edit_pos))*font_size);
93
94                 GL::push_matrix();
95                 GL::translate(rgeom.x, rgeom.y, 0);
96
97                 part.get_graphic(state)->render(gw, gh);
98
99                 GL::pop_matrix();
100         }
101 }
102
103
104 Entry::Loader::Loader(Entry &ent):
105         Widget::Loader(ent)
106 { }
107
108 } // namespace GLtk
109 } // namespace Msp