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