]> git.tdb.fi Git - libs/gltk.git/blob - entry.cpp
6e2063d04abec1de23c73baeb8417094c9ca0a73
[libs/gltk.git] / entry.cpp
1 #include <msp/gl/matrix.h>
2 #include <msp/gl/meshbuilder.h>
3 #include <msp/gl/texture.h>
4 #include <msp/input/keys.h>
5 #include "entry.h"
6 #include "graphic.h"
7 #include "part.h"
8 #include "style.h"
9 #include "vslider.h"
10
11 using namespace std;
12
13 namespace Msp {
14 namespace GLtk {
15
16 Entry::Entry(const string &t):
17         text(),
18         multiline(false),
19         edit_pos(0),
20         first_row(0),
21         visible_rows(1),
22         text_part(0),
23         slider(0)
24 {
25         set_text(t);
26 }
27
28 void Entry::autosize()
29 {
30         if(!style)
31                 return;
32
33         Widget::autosize();
34
35         if(text_part)
36         {
37                 const Sides &margin = text_part->get_margin();
38                 const GL::Font &font = style->get_font();
39                 unsigned en_width = static_cast<unsigned>(font.get_string_width("n")*style->get_font_size());
40                 geom.w = max(geom.w, 10*en_width+margin.left+margin.right);
41
42                 unsigned line_height = static_cast<unsigned>((font.get_ascent()-font.get_descent())*style->get_font_size());
43                 if(multiline)
44                 {
45                         unsigned line_spacing = style->get_font_size()*6/5;
46                         geom.h = max(geom.h, line_height+line_spacing*2+margin.top+margin.bottom);
47                 }
48                 else
49                         geom.h = max(geom.h, line_height+margin.top+margin.bottom);
50         }
51
52         if(multiline)
53         {
54                 if(const Part *slider_part = style->get_part("slider"))
55                 {
56                         Geometry sgeom = slider_part->get_geometry();
57                         if(!sgeom.w || !sgeom.h)
58                         {
59                                 slider->autosize();
60                                 if(!sgeom.w)
61                                         sgeom.w = slider->get_geometry().w;
62                                 if(!sgeom.h)
63                                         sgeom.h = slider->get_geometry().h;
64                         }
65
66                         const Sides &margin = slider_part->get_margin();
67                         geom.w = max(geom.w, sgeom.w+margin.left+margin.right);
68                         geom.h = max(geom.h, sgeom.h+margin.top+margin.bottom);
69
70                         reposition_slider();
71                 }
72
73                 check_view_range();
74         }
75
76         rebuild();
77 }
78
79 void Entry::set_text(const string &t)
80 {
81         text = t;
82         edit_pos = text.size();
83
84         if(multiline)
85                 check_view_range();
86
87         rebuild();
88 }
89
90 void Entry::set_multiline(bool m)
91 {
92         multiline = m;
93         if(multiline)
94         {
95                 if(!slider)
96                 {
97                         slider = new VSlider;
98                         add(*slider);
99                         slider->set_step(1);
100                         slider->signal_value_changed.connect(sigc::mem_fun(this, &Entry::slider_value_changed));
101                         reposition_slider();
102                 }
103                 check_view_range();
104         }
105 }
106
107 void Entry::rebuild_special(const Part &part, CachedPart &cache)
108 {
109         if(part.get_name()=="text")
110                 text.build(part, geom, first_row, cache);
111         else if(part.get_name()=="cursor")
112         {
113                 if(!text_part || !part.get_graphic(state))
114                 {
115                         cache.texture = 0;
116                         return;
117                 }
118
119                 unsigned row, col;
120                 text.offset_to_coords(edit_pos, row, col);
121
122                 if(row<first_row || row>=first_row+visible_rows)
123                 {
124                         cache.texture = 0;
125                         return;
126                 }
127
128                 Geometry rgeom = text.coords_to_geometry(*text_part, geom, first_row, row, col);
129
130                 cache.texture = part.get_graphic(state)->get_texture();
131                 cache.clear_mesh();
132
133                 GL::MeshBuilder bld(*cache.mesh);
134                 bld.matrix() *= GL::Matrix::translation(rgeom.x, rgeom.y, 0);
135                 part.get_graphic(state)->build(part.get_geometry().w, part.get_geometry().h, bld);
136         }
137 }
138
139 void Entry::render_special(const Part &part, GL::Renderer &renderer) const
140 {
141         if(part.get_name()=="slider" && multiline)
142                 slider->render(renderer);
143 }
144
145 void Entry::key_press(unsigned key, unsigned)
146 {
147         if(key==Input::KEY_LEFT)
148         {
149                 if(edit_pos>0)
150                         set_edit_position(edit_pos-1);
151         }
152         else if(key==Input::KEY_RIGHT)
153         {
154                 if(edit_pos<text.size())
155                         set_edit_position(edit_pos+1);
156         }
157         else if(key==Input::KEY_DOWN && multiline)
158         {
159                 unsigned row, col;
160                 text.offset_to_coords(edit_pos, row, col);
161                 set_edit_position(text.coords_to_offset(row+1, col));
162         }
163         else if(key==Input::KEY_UP && multiline)
164         {
165                 unsigned row, col;
166                 text.offset_to_coords(edit_pos, row, col);
167                 set_edit_position(row>0 ? text.coords_to_offset(row-1, col) : 0);
168         }
169         else if(key==Input::KEY_BACKSPACE)
170         {
171                 if(edit_pos>0)
172                 {
173                         text.erase(--edit_pos, 1);
174                         check_view_range();
175                         rebuild();
176                 }
177         }
178         else if(key==Input::KEY_ENTER)
179         {
180                 if(multiline)
181                 {
182                         text.insert(edit_pos++, "\n");
183                         check_view_range();
184                         rebuild();
185                 }
186                 else
187                         signal_enter.emit();
188         }
189 }
190
191 void Entry::character(wchar_t ch)
192 {
193         if(ch>=' ')
194         {
195                 text.insert(edit_pos, StringCodec::encode<StringCodec::Utf8>(StringCodec::ustring(1, ch)));
196                 ++edit_pos;
197                 rebuild();
198         }
199 }
200
201 void Entry::on_geometry_change()
202 {
203         reposition_slider();
204
205         if(multiline)
206                 check_view_range();
207 }
208
209 void Entry::on_style_change()
210 {
211         text.set_style(style);
212
213         if(!style)
214         {
215                 text_part = 0;
216                 return;
217         }
218
219         text_part = style->get_part("text");
220
221         reposition_slider();
222
223         if(multiline)
224                 check_view_range();
225 }
226
227 void Entry::set_edit_position(unsigned ep)
228 {
229         edit_pos = ep;
230         check_view_range();
231         rebuild();
232 }
233
234 void Entry::reposition_slider()
235 {
236         if(!style || !slider)
237                 return;
238
239         if(const Part *slider_part = style->get_part("slider"))
240         {
241                 Geometry sgeom = slider_part->get_geometry();
242                 if(!sgeom.w || !sgeom.h)
243                 {
244                         slider->autosize();
245                         if(!sgeom.w)
246                                 sgeom.w = slider->get_geometry().w;
247                         if(!sgeom.h)
248                                 sgeom.h = slider->get_geometry().h;
249                 }
250
251                 slider_part->get_alignment().apply(sgeom, geom, slider_part->get_margin());
252                 slider->set_geometry(sgeom);
253         }
254 }
255
256 void Entry::check_view_range()
257 {
258         if(!multiline || !text_part)
259                 return;
260
261         float font_size = style->get_font_size();
262         unsigned line_spacing = static_cast<unsigned>(font_size*6/5);
263
264         const Sides &margin = text_part->get_margin();
265         visible_rows = max((geom.h-margin.top-margin.bottom)/line_spacing, 1U);
266
267         unsigned row, col;
268         text.offset_to_coords(edit_pos, row, col);
269
270         if(first_row>row)
271                 first_row = row;
272         else if(row>=first_row+visible_rows)
273                 first_row = row+1-visible_rows;
274
275         unsigned scroll = max(text.get_n_lines(), visible_rows)-visible_rows;
276         slider->set_range(0, scroll);
277         slider->set_value(scroll-first_row);
278 }
279
280 void Entry::slider_value_changed(double value)
281 {
282         if(text.get_n_lines()>visible_rows)
283                 first_row = text.get_n_lines()-visible_rows-static_cast<unsigned>(value);
284 }
285
286
287 Entry::Loader::Loader(Entry &ent):
288         Widget::Loader(ent)
289 { }
290
291 } // namespace GLtk
292 } // namespace Msp