]> git.tdb.fi Git - libs/gltk.git/blob - source/entry.cpp
Render text in Dropdown directly from the List
[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(ch>=' ')
56         {
57                 text.insert(edit_pos, Codecs::encode<Codecs::Utf8>(Codecs::ustring(1, ch)));
58                 ++edit_pos;
59         }
60 }
61
62 void Entry::render_special(const Part &part) const
63 {
64         if(part.get_name()=="text")
65                 render_text(part, text);
66         else if(part.get_name()=="cursor")
67         {
68                 if(!part.get_graphic(state))
69                         return;
70
71                 const GL::Font *const font=style->get_font();
72                 const float font_size=font->get_default_size();
73
74                 Geometry rgeom=part.get_geometry();
75                 rgeom.x=static_cast<unsigned>(font->get_string_width(text.substr(0, edit_pos))*font_size);
76                 rgeom.w=static_cast<unsigned>(font->get_string_width(text)*font_size);
77                 part.get_alignment().apply(rgeom, geom, part.get_margin());
78
79                 GL::push_matrix();
80                 GL::translate(rgeom.x, rgeom.y, 0);
81                 part.get_graphic(state)->render(part.get_geometry().w, rgeom.h);
82                 GL::pop_matrix();
83         }
84 }
85
86
87 Entry::Loader::Loader(Entry &ent):
88         Widget::Loader(ent)
89 { }
90
91 } // namespace GLtk
92 } // namespace Msp