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