]> git.tdb.fi Git - libs/gltk.git/blob - source/entry.cpp
65bae7214001e842929aab8618982dfbf11cdc84
[libs/gltk.git] / source / entry.cpp
1 #include <SDL/SDL_keysym.h>
2 #include <msp/gl/matrix.h>
3 #include <msp/gl/texture.h>
4 #include <msp/gl/transform.h>
5 #include "entry.h"
6 #include "part.h"
7 #include "style.h"
8
9 using namespace std;
10
11 namespace Msp {
12 namespace GLtk {
13
14 Entry::Entry(const Resources &r, const string &t):
15         Widget(r),
16         text(t),
17         edit_pos(0)
18 {
19         update_style();
20 }
21
22 void Entry::set_text(const string &t)
23 {
24         text=t;
25         if(edit_pos>text.size())
26                 edit_pos=text.size();
27 }
28
29 void Entry::key_press(unsigned key, unsigned, wchar_t ch)
30 {
31         if(key==SDLK_LEFT)
32         {
33                 if(edit_pos>0)
34                         --edit_pos;
35         }
36         else if(key==SDLK_RIGHT)
37         {
38                 if(edit_pos<text.size())
39                         ++edit_pos;
40         }
41         else if(key==SDLK_BACKSPACE)
42         {
43                 if(edit_pos>0)
44                         text.erase(--edit_pos, 1);
45         }
46         else
47         {
48                 text+=ch;
49         }
50 }
51
52 void Entry::focus_in()
53 {
54         if(state!=DISABLED)
55                 state=ACTIVE;
56 }
57
58 void Entry::focus_out()
59 {
60         if(state==ACTIVE)
61                 state=NORMAL;
62 }
63
64 void Entry::render_part(const Part &part) const
65 {
66         if(part.get_name()=="text")
67                 render_text(part, text);
68         /*{
69                 const GL::Font *const font=style->get_font();
70
71                 const float font_size=font->get_default_size();
72                 unsigned text_w=static_cast<unsigned>(font->get_string_width(text)*font_size);
73
74                 GL::push_matrix();
75
76                 part.get_alignment().apply(geom, text_w, static_cast<unsigned>(font->get_ascent()*font_size));
77                 GL::scale_uniform(font_size);
78
79                 const Color &color=style->get_font_color();
80                 glColor3f(color.r, color.g, color.b);
81                 if(state==ACTIVE)
82                 {
83                         cout<<"foo\n";
84                         font->draw_string(text.substr(0, edit_pos));
85                         GL::Texture::unbind();
86                         glBegin(GL_LINES);
87                         glVertex2f(0, 0);
88                         glVertex2f(0, 1);
89                         glEnd();
90                         font->draw_string(text.substr(edit_pos));
91                 }
92                 else
93                         font->draw_string(text);
94                 glColor3f(1, 1, 1);
95
96                 GL::pop_matrix();
97                 render_text(part, text);
98         }*/
99         /*else if(part.get_name()=="cursor")
100         {
101                 unsigned gw=part.get_width();
102                 unsigned gh=(part.get_fill_y() ? geom.h : part.get_height());
103
104                 const float font_size=font->get_default_size();
105                 unsigned text_w=static_cast<unsigned>(font->get_string_width(text)*font_size);
106
107                 GL::push_matrix();
108                 GL::translate((geom.w-gw)*(value-min)/(max-min), (geom.h-gh)*(part.get_alignment().y+1)/2, 0);
109                 const Graphic *graphic=part.get_graphic(state);
110                 graphic->render(gw, gh);
111                 GL::pop_matrix();
112         }*/
113         else
114                 Widget::render_part(part);
115 }
116
117 } // namespace GLtk
118 } // namespace Msp