]> git.tdb.fi Git - libs/gltk.git/blob - source/entry.cpp
Handle home and end keys in Entry widget
[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 {
29         input_type = INPUT_TEXT;
30         set_text(t);
31 }
32
33 void Entry::autosize_special(const Part &part, Geometry &ageom) const
34 {
35         if(part.get_name()=="text")
36         {
37                 const Sides &margin = 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                 ageom.w = max(ageom.w, edit_width*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                         ageom.h = max(ageom.h, line_height+line_spacing*(edit_height-1)+margin.top+margin.bottom);
47                 }
48                 else
49                         ageom.h = max(ageom.h, line_height+margin.top+margin.bottom);
50         }
51         else if(part.get_name()=="slider" && multiline)
52                 autosize_child(*slider, part, ageom);
53 }
54
55 void Entry::set_text(const string &t)
56 {
57         text = t;
58         edit_pos = text.size();
59
60         if(multiline)
61                 check_view_range();
62
63         rebuild();
64 }
65
66 void Entry::set_edit_size(unsigned w, unsigned h)
67 {
68         edit_width = w;
69         edit_height = h;
70         signal_autosize_changed.emit();
71 }
72
73 void Entry::set_multiline(bool m)
74 {
75         multiline = m;
76         if(multiline)
77         {
78                 if(!slider)
79                 {
80                         slider = new VSlider;
81                         add(*slider);
82                         slider->set_step(1);
83                         slider->signal_value_changed.connect(sigc::mem_fun(this, &Entry::slider_value_changed));
84                         rebuild();
85                 }
86                 check_view_range();
87         }
88 }
89
90 void Entry::rebuild_special(const Part &part)
91 {
92         if(part.get_name()=="text")
93                 text.build(part, state, geom, first_row, part_cache);
94         else if(part.get_name()=="cursor")
95         {
96                 State cursor_state = (cursor_blink ? ACTIVE : NORMAL);
97                 const Graphic *graphic = part.get_graphic(state|cursor_state);
98                 if(!text_part || !graphic || !graphic->get_texture())
99                         return;
100
101                 unsigned row, col;
102                 text.offset_to_coords(edit_pos, row, col);
103
104                 if(row<first_row || row>=first_row+visible_rows)
105                         return;
106
107                 Geometry rgeom = text.coords_to_geometry(*text_part, geom, first_row, row, col);
108
109                 GL::MeshBuilder bld(part_cache.create_mesh(part, *graphic->get_texture()));
110                 bld.matrix() *= GL::Matrix::translation(rgeom.x, rgeom.y, 0);
111                 graphic->build(part.get_geometry().w, part.get_geometry().h, bld);
112         }
113         else if(part.get_name()=="slider")
114         {
115                 if(multiline)
116                 {
117                         reposition_child(*slider, part);
118                         Widget::rebuild_special(part);
119                 }
120         }
121         else
122                 Widget::rebuild_special(part);
123 }
124
125 void Entry::render_special(const Part &part, GL::Renderer &renderer) const
126 {
127         if(part.get_name()=="slider" && multiline)
128                 slider->render(renderer);
129 }
130
131 void Entry::touch_press(int x, int y, unsigned finger)
132 {
133         if(finger==0)
134                 set_focus();
135         Widget::touch_press(x, y, finger);
136 }
137
138 bool Entry::key_press(unsigned key, unsigned)
139 {
140         got_key_press = true;
141         if(key==Input::KEY_BACKSPACE)
142         {
143                 if(edit_pos>0)
144                 {
145                         text.erase(--edit_pos, 1);
146                         check_view_range();
147                         rebuild();
148                 }
149         }
150         else if(key==Input::KEY_ENTER && multiline)
151         {
152                 text.insert(edit_pos++, "\n");
153                 check_view_range();
154                 rebuild();
155         }
156         else if(key==Input::KEY_END)
157         {
158                 unsigned row, col;
159                 text.offset_to_coords(edit_pos, row, col);
160                 set_edit_position(text.coords_to_offset(row, text.get_line_length(row)));
161         }
162         else if(key==Input::KEY_HOME)
163         {
164                 unsigned row, col;
165                 text.offset_to_coords(edit_pos, row, col);
166                 set_edit_position(text.coords_to_offset(row, 0));
167         }
168         else
169                 return false;
170
171         return true;
172 }
173
174 bool Entry::character(wchar_t ch)
175 {
176         if(got_key_press && ch>=' ')
177         {
178                 text.insert(edit_pos, StringCodec::encode<StringCodec::Utf8>(StringCodec::ustring(1, ch)));
179                 ++edit_pos;
180                 rebuild();
181                 return true;
182         }
183
184         return false;
185 }
186
187 void Entry::focus_in()
188 {
189         cursor_blink = true;
190         Widget::focus_in();
191         check_cursor_blink();
192 }
193
194 void Entry::focus_out()
195 {
196         Widget::focus_out();
197         got_key_press = false;
198         check_cursor_blink();
199 }
200
201 bool Entry::navigate(Navigation nav)
202 {
203         if(nav==NAV_LEFT)
204         {
205                 if(edit_pos>0)
206                         set_edit_position(edit_pos-1);
207         }
208         else if(nav==NAV_RIGHT)
209         {
210                 if(edit_pos<text.size())
211                         set_edit_position(edit_pos+1);
212         }
213         else if(nav==NAV_DOWN && multiline)
214         {
215                 unsigned row, col;
216                 text.offset_to_coords(edit_pos, row, col);
217                 set_edit_position(text.coords_to_offset(row+1, col));
218         }
219         else if(nav==NAV_UP && multiline)
220         {
221                 unsigned row, col;
222                 text.offset_to_coords(edit_pos, row, col);
223                 set_edit_position(row>0 ? text.coords_to_offset(row-1, col) : 0);
224         }
225         else if(nav==NAV_ACCEPT && !signal_enter.empty())
226                 signal_enter.emit();
227         else
228                 return false;
229
230         return true;
231 }
232
233 void Entry::animate(const Time::TimeDelta &)
234 {
235         cursor_blink = !cursor_blink;
236         rebuild();
237 }
238
239 void Entry::on_geometry_change()
240 {
241         if(multiline)
242                 check_view_range();
243 }
244
245 void Entry::on_style_change()
246 {
247         text.set_style(style);
248
249         if(!style)
250         {
251                 text_part = 0;
252                 return;
253         }
254
255         text_part = style->get_part("text");
256
257         if(multiline)
258                 check_view_range();
259
260         check_cursor_blink();
261 }
262
263 void Entry::set_edit_position(unsigned ep)
264 {
265         edit_pos = ep;
266         check_view_range();
267         rebuild();
268 }
269
270 void Entry::check_cursor_blink()
271 {
272         cursor_blink = (state&FOCUS);
273         if((state&FOCUS) && style)
274         {
275                 const Part *cursor_part = style->get_part("cursor");
276                 if(cursor_part && cursor_part->get_graphic(ACTIVE|FOCUS)!=cursor_part->get_graphic(NORMAL|FOCUS))
277                 {
278                         set_animation_interval(Time::sec/2);
279                         return;
280                 }
281         }
282
283         stop_animation();
284 }
285
286 void Entry::check_view_range()
287 {
288         if(!multiline || !text_part)
289                 return;
290
291         float font_size = style->get_font_size();
292         unsigned line_spacing = static_cast<unsigned>(font_size*6/5);
293
294         const Sides &margin = text_part->get_margin();
295         visible_rows = max((geom.h-margin.top-margin.bottom)/line_spacing, 1U);
296
297         unsigned row, col;
298         text.offset_to_coords(edit_pos, row, col);
299
300         if(first_row>row)
301                 first_row = row;
302         else if(row>=first_row+visible_rows)
303                 first_row = row+1-visible_rows;
304
305         unsigned scroll = max(text.get_n_lines(), visible_rows)-visible_rows;
306         slider->set_range(0, scroll);
307         slider->set_value(scroll-first_row);
308 }
309
310 void Entry::slider_value_changed(double value)
311 {
312         if(text.get_n_lines()>visible_rows)
313                 first_row = text.get_n_lines()-visible_rows-static_cast<unsigned>(value);
314 }
315
316
317 Entry::Loader::Loader(Entry &e):
318         DataFile::DerivedObjectLoader<Entry, Widget::Loader>(e)
319 {
320         add("edit_size", &Entry::edit_width, &Entry::edit_height);
321         add("multiline", &Loader::multiline);
322 }
323
324 void Entry::Loader::multiline(bool m)
325 {
326         obj.set_multiline(m);
327 }
328
329 } // namespace GLtk
330 } // namespace Msp