]> git.tdb.fi Git - libs/gltk.git/blob - source/entry.cpp
Reorder class members
[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 <SDL/SDL_keysym.h>
9 #include <msp/gl/matrix.h>
10 #include <msp/gl/texture.h>
11 #include <msp/gl/transform.h>
12 #include "entry.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==SDLK_LEFT)
39         {
40                 if(edit_pos>0)
41                         --edit_pos;
42         }
43         else if(key==SDLK_RIGHT)
44         {
45                 if(edit_pos<text.size())
46                         ++edit_pos;
47         }
48         else if(key==SDLK_BACKSPACE)
49         {
50                 if(edit_pos>0)
51                         text.erase(--edit_pos, 1);
52         }
53         else
54         {
55                 text+=ch;
56         }
57 }
58
59 void Entry::focus_in()
60 {
61         if(state!=DISABLED)
62                 state=ACTIVE;
63 }
64
65 void Entry::focus_out()
66 {
67         if(state==ACTIVE)
68                 state=NORMAL;
69 }
70
71 void Entry::render_part(const Part &part) const
72 {
73         if(part.get_name()=="text")
74                 render_text(part, text);
75         /*{
76                 const GL::Font *const font=style->get_font();
77
78                 const float font_size=font->get_default_size();
79                 unsigned text_w=static_cast<unsigned>(font->get_string_width(text)*font_size);
80
81                 GL::push_matrix();
82
83                 part.get_alignment().apply(geom, text_w, static_cast<unsigned>(font->get_ascent()*font_size));
84                 GL::scale_uniform(font_size);
85
86                 const Color &color=style->get_font_color();
87                 glColor3f(color.r, color.g, color.b);
88                 if(state==ACTIVE)
89                 {
90                         cout<<"foo\n";
91                         font->draw_string(text.substr(0, edit_pos));
92                         GL::Texture::unbind();
93                         glBegin(GL_LINES);
94                         glVertex2f(0, 0);
95                         glVertex2f(0, 1);
96                         glEnd();
97                         font->draw_string(text.substr(edit_pos));
98                 }
99                 else
100                         font->draw_string(text);
101                 glColor3f(1, 1, 1);
102
103                 GL::pop_matrix();
104                 render_text(part, text);
105         }*/
106         /*else if(part.get_name()=="cursor")
107         {
108                 unsigned gw=part.get_width();
109                 unsigned gh=(part.get_fill_y() ? geom.h : part.get_height());
110
111                 const float font_size=font->get_default_size();
112                 unsigned text_w=static_cast<unsigned>(font->get_string_width(text)*font_size);
113
114                 GL::push_matrix();
115                 GL::translate((geom.w-gw)*(value-min)/(max-min), (geom.h-gh)*(part.get_alignment().y+1)/2, 0);
116                 const Graphic *graphic=part.get_graphic(state);
117                 graphic->render(gw, gh);
118                 GL::pop_matrix();
119         }*/
120         else
121                 Widget::render_part(part);
122 }
123
124 } // namespace GLtk
125 } // namespace Msp