]> git.tdb.fi Git - libs/gltk.git/blob - source/entry.cpp
af6fcc6d0fd31ed5cfdbdc03f4fda57e9db6c55c
[libs/gltk.git] / source / 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_width(10),
20         edit_height(1),
21         edit_pos(0),
22         first_row(0),
23         visible_rows(1),
24         text_part(0),
25         slider(0),
26         got_key_press(false),
27         cursor_blink(true),
28         selection_active(false),
29         selection_pos(0)
30 {
31         input_type = INPUT_TEXT;
32         set_text(t);
33 }
34
35 void Entry::autosize_special(const Part &part, Geometry &ageom) const
36 {
37         if(part.get_name()=="text")
38         {
39                 const Sides &margin = part.get_margin();
40                 const GL::Font &font = style->get_font();
41                 unsigned en_width = static_cast<unsigned>(font.get_string_width("n")*style->get_font_size());
42                 ageom.w = max(ageom.w, edit_width*en_width+margin.left+margin.right);
43
44                 unsigned line_height = static_cast<unsigned>((font.get_ascent()-font.get_descent())*style->get_font_size());
45                 if(multiline)
46                 {
47                         unsigned line_spacing = style->get_font_size()*6/5;
48                         ageom.h = max(ageom.h, line_height+line_spacing*(edit_height-1)+margin.top+margin.bottom);
49                 }
50                 else
51                         ageom.h = max(ageom.h, line_height+margin.top+margin.bottom);
52         }
53         else if(part.get_name()=="slider" && multiline)
54                 autosize_child(*slider, part, ageom);
55 }
56
57 void Entry::set_text(const string &t)
58 {
59         text = t;
60         edit_pos = text.size();
61
62         if(multiline)
63                 check_view_range();
64
65         rebuild();
66 }
67
68 void Entry::set_edit_size(unsigned w, unsigned h)
69 {
70         edit_width = w;
71         edit_height = h;
72         signal_autosize_changed.emit();
73 }
74
75 void Entry::set_multiline(bool m)
76 {
77         multiline = m;
78         if(multiline)
79         {
80                 if(!slider)
81                 {
82                         slider = new VSlider;
83                         add(*slider);
84                         slider->set_step(1);
85                         slider->signal_value_changed.connect(sigc::mem_fun(this, &Entry::slider_value_changed));
86                         rebuild();
87                 }
88                 check_view_range();
89         }
90 }
91
92 void Entry::rebuild_special(const Part &part)
93 {
94         if(part.get_name()=="text")
95                 text.build(part, state, geom, first_row, part_cache);
96         else if(part.get_name()=="cursor")
97         {
98                 State cursor_state = (cursor_blink ? ACTIVE : NORMAL);
99                 const Graphic *graphic = part.get_graphic(state|cursor_state);
100                 if(!text_part || !graphic || !graphic->get_texture())
101                         return;
102
103                 unsigned row, col;
104                 text.offset_to_coords(edit_pos, row, col);
105
106                 if(row<first_row || row>=first_row+visible_rows)
107                         return;
108
109                 Geometry rgeom = text.coords_to_geometry(*text_part, geom, first_row, row, col);
110
111                 GL::MeshBuilder bld(part_cache.create_mesh(part, *graphic->get_texture()));
112                 bld.matrix() *= GL::Matrix::translation(rgeom.x, rgeom.y, 0);
113                 graphic->build(part.get_geometry().w, part.get_geometry().h, bld);
114         }
115         else if(part.get_name()=="selection")
116         {
117                 if(!selection_active)
118                         return;
119
120                 const Graphic *graphic = part.get_graphic(state);
121                 if(!text_part || !graphic || !graphic->get_texture())
122                         return;
123
124                 unsigned start = selection_pos;
125                 unsigned end = edit_pos;
126                 if(start>end)
127                         swap(start, end);
128
129                 unsigned row, col;
130                 text.offset_to_coords(start, row, col);
131                 unsigned end_row, end_col;
132                 text.offset_to_coords(end, end_row, end_col);
133
134                 if(end_row<first_row || row>=first_row+visible_rows)
135                         return;
136
137                 if(row<first_row)
138                 {
139                         row = first_row;
140                         col = 0;
141                 }
142
143                 if(end_row>=first_row+visible_rows)
144                 {
145                         end_row = first_row+visible_rows-1;
146                         end_col = text.get_line_length(end_row);
147                 }
148
149                 while(row<=end_row)
150                 {
151                         unsigned ec = (row==end_row ? end_col : text.get_line_length(row));
152                         if(ec>col)
153                         {
154                                 Geometry rgeom = text.coords_to_geometry(*text_part, geom, first_row, row, col);
155                                 Geometry egeom = text.coords_to_geometry(*text_part, geom, first_row, row, ec);
156
157                                 GL::MeshBuilder bld(part_cache.create_mesh(part, *graphic->get_texture()));
158                                 bld.matrix() *= GL::Matrix::translation(rgeom.x, rgeom.y, 0);
159                                 graphic->build(egeom.x-rgeom.x, part.get_geometry().h, bld);
160                         }
161
162                         ++row;
163                         col = 0;
164                 }
165         }
166         else if(part.get_name()=="slider")
167         {
168                 if(multiline)
169                 {
170                         reposition_child(*slider, part);
171                         Widget::rebuild_special(part);
172                 }
173         }
174         else
175                 Widget::rebuild_special(part);
176 }
177
178 void Entry::render_special(const Part &part, GL::Renderer &renderer) const
179 {
180         if(part.get_name()=="slider" && multiline)
181                 slider->render(renderer);
182 }
183
184 void Entry::touch_press(int x, int y, unsigned finger)
185 {
186         if(finger==0)
187                 set_focus();
188         Widget::touch_press(x, y, finger);
189 }
190
191 bool Entry::key_press(unsigned key, unsigned mod)
192 {
193         got_key_press = true;
194         if(key==Input::KEY_BACKSPACE)
195         {
196                 if(selection_active)
197                         erase_selection();
198                 else if(edit_pos>0)
199                 {
200                         text.erase(--edit_pos, 1);
201                         check_view_range();
202                         rebuild();
203                 }
204         }
205         else if(key==Input::KEY_DELETE)
206         {
207                 if(selection_active)
208                         erase_selection();
209                 else
210                         text.erase(edit_pos, 1);
211         }
212         else if(key==Input::KEY_ENTER && multiline)
213         {
214                 text.insert(edit_pos++, "\n");
215                 check_view_range();
216                 rebuild();
217         }
218         else if(key==Input::KEY_END)
219         {
220                 unsigned row, col;
221                 text.offset_to_coords(edit_pos, row, col);
222                 set_edit_position(text.coords_to_offset(row, text.get_line_length(row)), mod==MOD_SHIFT);
223         }
224         else if(key==Input::KEY_HOME)
225         {
226                 unsigned row, col;
227                 text.offset_to_coords(edit_pos, row, col);
228                 set_edit_position(text.coords_to_offset(row, 0), mod==MOD_SHIFT);
229         }
230         else if(key==Input::KEY_LEFT && mod==MOD_SHIFT)
231                 move_edit_position(NAV_LEFT, true);
232         else if(key==Input::KEY_RIGHT && mod==MOD_SHIFT)
233                 move_edit_position(NAV_RIGHT, true);
234         else if(key==Input::KEY_UP && mod==MOD_SHIFT && multiline)
235                 move_edit_position(NAV_UP, true);
236         else if(key==Input::KEY_DOWN && mod==MOD_SHIFT && multiline)
237                 move_edit_position(NAV_DOWN, true);
238         else
239                 return false;
240
241         return true;
242 }
243
244 bool Entry::character(wchar_t ch)
245 {
246         if(got_key_press && ch>=' ' && ch!=0x7F)
247         {
248                 if(selection_active)
249                         erase_selection();
250                 text.insert(edit_pos, StringCodec::encode<StringCodec::Utf8>(StringCodec::ustring(1, ch)));
251                 ++edit_pos;
252                 rebuild();
253                 return true;
254         }
255
256         return false;
257 }
258
259 void Entry::focus_in()
260 {
261         cursor_blink = true;
262         Widget::focus_in();
263         check_cursor_blink();
264 }
265
266 void Entry::focus_out()
267 {
268         Widget::focus_out();
269         got_key_press = false;
270         check_cursor_blink();
271 }
272
273 bool Entry::navigate(Navigation nav)
274 {
275         if(nav==NAV_LEFT || nav==NAV_RIGHT || ((nav==NAV_DOWN || nav==NAV_UP) && multiline))
276                 move_edit_position(nav, false);
277         else if(nav==NAV_ACCEPT && !signal_enter.empty())
278                 signal_enter.emit();
279         else
280                 return false;
281
282         return true;
283 }
284
285 void Entry::animate(const Time::TimeDelta &)
286 {
287         cursor_blink = !cursor_blink;
288         rebuild();
289 }
290
291 void Entry::on_geometry_change()
292 {
293         if(multiline)
294                 check_view_range();
295 }
296
297 void Entry::on_style_change()
298 {
299         text.set_style(style);
300
301         if(!style)
302         {
303                 text_part = 0;
304                 return;
305         }
306
307         text_part = style->get_part("text");
308
309         if(multiline)
310                 check_view_range();
311
312         check_cursor_blink();
313 }
314
315 void Entry::move_edit_position(Navigation nav, bool select)
316 {
317         if(nav==NAV_LEFT)
318         {
319                 if(edit_pos>0)
320                         set_edit_position(edit_pos-1, select);
321         }
322         else if(nav==NAV_RIGHT)
323         {
324                 if(edit_pos<text.size())
325                         set_edit_position(edit_pos+1, select);
326         }
327         else if(nav==NAV_DOWN)
328         {
329                 unsigned row, col;
330                 text.offset_to_coords(edit_pos, row, col);
331                 set_edit_position(text.coords_to_offset(row+1, col), select);
332         }
333         else if(nav==NAV_UP)
334         {
335                 unsigned row, col;
336                 text.offset_to_coords(edit_pos, row, col);
337                 set_edit_position((row>0 ? text.coords_to_offset(row-1, col) : 0), select);
338         }
339 }
340
341 void Entry::set_edit_position(unsigned ep, bool select)
342 {
343         if(select && !selection_active)
344                 selection_pos = edit_pos;
345         selection_active = select;
346
347         edit_pos = ep;
348         check_view_range();
349         rebuild();
350 }
351
352 void Entry::erase_selection()
353 {
354         if(!selection_active)
355                 return;
356
357         unsigned start = selection_pos;
358         unsigned end = edit_pos;
359         if(start>end)
360                 swap(start, end);
361
362         text.erase(start, end-start);
363         set_edit_position(start, false);
364 }
365
366 void Entry::check_cursor_blink()
367 {
368         cursor_blink = (state&FOCUS);
369         if((state&FOCUS) && style)
370         {
371                 const Part *cursor_part = style->get_part("cursor");
372                 if(cursor_part && cursor_part->get_graphic(ACTIVE|FOCUS)!=cursor_part->get_graphic(NORMAL|FOCUS))
373                 {
374                         set_animation_interval(Time::sec/2);
375                         return;
376                 }
377         }
378
379         stop_animation();
380 }
381
382 void Entry::check_view_range()
383 {
384         if(!multiline || !text_part)
385                 return;
386
387         float font_size = style->get_font_size();
388         unsigned line_spacing = static_cast<unsigned>(font_size*6/5);
389
390         const Sides &margin = text_part->get_margin();
391         visible_rows = max((geom.h-margin.top-margin.bottom)/line_spacing, 1U);
392
393         unsigned row, col;
394         text.offset_to_coords(edit_pos, row, col);
395
396         if(first_row>row)
397                 first_row = row;
398         else if(row>=first_row+visible_rows)
399                 first_row = row+1-visible_rows;
400
401         unsigned scroll = max(text.get_n_lines(), visible_rows)-visible_rows;
402         slider->set_range(0, scroll);
403         slider->set_value(scroll-first_row);
404 }
405
406 void Entry::slider_value_changed(double value)
407 {
408         if(text.get_n_lines()>visible_rows)
409                 first_row = text.get_n_lines()-visible_rows-static_cast<unsigned>(value);
410 }
411
412
413 Entry::Loader::Loader(Entry &e):
414         DataFile::DerivedObjectLoader<Entry, Widget::Loader>(e)
415 {
416         add("edit_size", &Entry::edit_width, &Entry::edit_height);
417         add("multiline", &Loader::multiline);
418 }
419
420 void Entry::Loader::multiline(bool m)
421 {
422         obj.set_multiline(m);
423 }
424
425 } // namespace GLtk
426 } // namespace Msp