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