]> git.tdb.fi Git - libs/gltk.git/blob - source/entry.cpp
6616054f799b2be0f5dd1cf445989a6771ff30bd
[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 bool Entry::key_press(unsigned key, unsigned)
130 {
131         got_key_press = true;
132         if(key==Input::KEY_BACKSPACE)
133         {
134                 if(edit_pos>0)
135                 {
136                         text.erase(--edit_pos, 1);
137                         check_view_range();
138                         rebuild();
139                 }
140         }
141         else if(key==Input::KEY_ENTER && multiline)
142         {
143                 text.insert(edit_pos++, "\n");
144                 check_view_range();
145                 rebuild();
146         }
147         else
148                 return false;
149
150         return true;
151 }
152
153 bool Entry::character(wchar_t ch)
154 {
155         if(got_key_press && ch>=' ')
156         {
157                 text.insert(edit_pos, StringCodec::encode<StringCodec::Utf8>(StringCodec::ustring(1, ch)));
158                 ++edit_pos;
159                 rebuild();
160                 return true;
161         }
162
163         return false;
164 }
165
166 void Entry::focus_out()
167 {
168         Widget::focus_out();
169         got_key_press = false;
170 }
171
172 bool Entry::navigate(Navigation nav)
173 {
174         if(nav==NAV_LEFT)
175         {
176                 if(edit_pos>0)
177                         set_edit_position(edit_pos-1);
178         }
179         else if(nav==NAV_RIGHT)
180         {
181                 if(edit_pos<text.size())
182                         set_edit_position(edit_pos+1);
183         }
184         else if(nav==NAV_DOWN && multiline)
185         {
186                 unsigned row, col;
187                 text.offset_to_coords(edit_pos, row, col);
188                 set_edit_position(text.coords_to_offset(row+1, col));
189         }
190         else if(nav==NAV_UP && multiline)
191         {
192                 unsigned row, col;
193                 text.offset_to_coords(edit_pos, row, col);
194                 set_edit_position(row>0 ? text.coords_to_offset(row-1, col) : 0);
195         }
196         else if(nav==NAV_ACCEPT && !signal_enter.empty())
197                 signal_enter.emit();
198         else
199                 return false;
200
201         return true;
202 }
203
204 void Entry::on_geometry_change()
205 {
206         if(multiline)
207                 check_view_range();
208 }
209
210 void Entry::on_style_change()
211 {
212         text.set_style(style);
213
214         if(!style)
215         {
216                 text_part = 0;
217                 return;
218         }
219
220         text_part = style->get_part("text");
221
222         if(multiline)
223                 check_view_range();
224 }
225
226 void Entry::set_edit_position(unsigned ep)
227 {
228         edit_pos = ep;
229         check_view_range();
230         rebuild();
231 }
232
233 void Entry::check_view_range()
234 {
235         if(!multiline || !text_part)
236                 return;
237
238         float font_size = style->get_font_size();
239         unsigned line_spacing = static_cast<unsigned>(font_size*6/5);
240
241         const Sides &margin = text_part->get_margin();
242         visible_rows = max((geom.h-margin.top-margin.bottom)/line_spacing, 1U);
243
244         unsigned row, col;
245         text.offset_to_coords(edit_pos, row, col);
246
247         if(first_row>row)
248                 first_row = row;
249         else if(row>=first_row+visible_rows)
250                 first_row = row+1-visible_rows;
251
252         unsigned scroll = max(text.get_n_lines(), visible_rows)-visible_rows;
253         slider->set_range(0, scroll);
254         slider->set_value(scroll-first_row);
255 }
256
257 void Entry::slider_value_changed(double value)
258 {
259         if(text.get_n_lines()>visible_rows)
260                 first_row = text.get_n_lines()-visible_rows-static_cast<unsigned>(value);
261 }
262
263
264 Entry::Loader::Loader(Entry &e):
265         DataFile::DerivedObjectLoader<Entry, Widget::Loader>(e)
266 {
267         add("edit_size", &Entry::edit_width, &Entry::edit_height);
268         add("multiline", &Loader::multiline);
269 }
270
271 void Entry::Loader::multiline(bool m)
272 {
273         obj.set_multiline(m);
274 }
275
276 } // namespace GLtk
277 } // namespace Msp