]> git.tdb.fi Git - libs/gltk.git/blob - source/entry.cpp
Make the entry widget cursor blink
[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
157                 return false;
158
159         return true;
160 }
161
162 bool Entry::character(wchar_t ch)
163 {
164         if(got_key_press && ch>=' ')
165         {
166                 text.insert(edit_pos, StringCodec::encode<StringCodec::Utf8>(StringCodec::ustring(1, ch)));
167                 ++edit_pos;
168                 rebuild();
169                 return true;
170         }
171
172         return false;
173 }
174
175 void Entry::focus_in()
176 {
177         cursor_blink = true;
178         Widget::focus_in();
179         check_cursor_blink();
180 }
181
182 void Entry::focus_out()
183 {
184         Widget::focus_out();
185         got_key_press = false;
186         check_cursor_blink();
187 }
188
189 bool Entry::navigate(Navigation nav)
190 {
191         if(nav==NAV_LEFT)
192         {
193                 if(edit_pos>0)
194                         set_edit_position(edit_pos-1);
195         }
196         else if(nav==NAV_RIGHT)
197         {
198                 if(edit_pos<text.size())
199                         set_edit_position(edit_pos+1);
200         }
201         else if(nav==NAV_DOWN && multiline)
202         {
203                 unsigned row, col;
204                 text.offset_to_coords(edit_pos, row, col);
205                 set_edit_position(text.coords_to_offset(row+1, col));
206         }
207         else if(nav==NAV_UP && multiline)
208         {
209                 unsigned row, col;
210                 text.offset_to_coords(edit_pos, row, col);
211                 set_edit_position(row>0 ? text.coords_to_offset(row-1, col) : 0);
212         }
213         else if(nav==NAV_ACCEPT && !signal_enter.empty())
214                 signal_enter.emit();
215         else
216                 return false;
217
218         return true;
219 }
220
221 void Entry::animate(const Time::TimeDelta &)
222 {
223         cursor_blink = !cursor_blink;
224         rebuild();
225 }
226
227 void Entry::on_geometry_change()
228 {
229         if(multiline)
230                 check_view_range();
231 }
232
233 void Entry::on_style_change()
234 {
235         text.set_style(style);
236
237         if(!style)
238         {
239                 text_part = 0;
240                 return;
241         }
242
243         text_part = style->get_part("text");
244
245         if(multiline)
246                 check_view_range();
247
248         check_cursor_blink();
249 }
250
251 void Entry::set_edit_position(unsigned ep)
252 {
253         edit_pos = ep;
254         check_view_range();
255         rebuild();
256 }
257
258 void Entry::check_cursor_blink()
259 {
260         cursor_blink = (state&FOCUS);
261         if((state&FOCUS) && style)
262         {
263                 const Part *cursor_part = style->get_part("cursor");
264                 if(cursor_part && cursor_part->get_graphic(ACTIVE|FOCUS)!=cursor_part->get_graphic(NORMAL|FOCUS))
265                 {
266                         set_animation_interval(Time::sec/2);
267                         return;
268                 }
269         }
270
271         stop_animation();
272 }
273
274 void Entry::check_view_range()
275 {
276         if(!multiline || !text_part)
277                 return;
278
279         float font_size = style->get_font_size();
280         unsigned line_spacing = static_cast<unsigned>(font_size*6/5);
281
282         const Sides &margin = text_part->get_margin();
283         visible_rows = max((geom.h-margin.top-margin.bottom)/line_spacing, 1U);
284
285         unsigned row, col;
286         text.offset_to_coords(edit_pos, row, col);
287
288         if(first_row>row)
289                 first_row = row;
290         else if(row>=first_row+visible_rows)
291                 first_row = row+1-visible_rows;
292
293         unsigned scroll = max(text.get_n_lines(), visible_rows)-visible_rows;
294         slider->set_range(0, scroll);
295         slider->set_value(scroll-first_row);
296 }
297
298 void Entry::slider_value_changed(double value)
299 {
300         if(text.get_n_lines()>visible_rows)
301                 first_row = text.get_n_lines()-visible_rows-static_cast<unsigned>(value);
302 }
303
304
305 Entry::Loader::Loader(Entry &e):
306         DataFile::DerivedObjectLoader<Entry, Widget::Loader>(e)
307 {
308         add("edit_size", &Entry::edit_width, &Entry::edit_height);
309         add("multiline", &Loader::multiline);
310 }
311
312 void Entry::Loader::multiline(bool m)
313 {
314         obj.set_multiline(m);
315 }
316
317 } // namespace GLtk
318 } // namespace Msp