1 #include <msp/gl/matrix.h>
2 #include <msp/gl/meshbuilder.h>
3 #include <msp/gl/texture.h>
4 #include <msp/input/keys.h>
16 Entry::Entry(const string &t):
28 selection_active(false),
31 input_type = INPUT_TEXT;
35 void Entry::autosize_special(const Part &part, Geometry &ageom) const
37 if(part.get_name()=="text")
39 const Sides &margin = part.get_margin();
40 const GL::Font &font = style->get_font();
41 unsigned en_width = static_cast<unsigned>(font.get_string_width("n")*style->get_font_size());
42 ageom.w = max(ageom.w, edit_width*en_width+margin.left+margin.right);
44 unsigned line_height = static_cast<unsigned>((font.get_ascent()-font.get_descent())*style->get_font_size());
47 unsigned line_spacing = style->get_font_size()*6/5;
48 ageom.h = max(ageom.h, line_height+line_spacing*(edit_height-1)+margin.top+margin.bottom);
51 ageom.h = max(ageom.h, line_height+margin.top+margin.bottom);
53 else if(part.get_name()=="slider" && multiline)
54 autosize_child(*slider, part, ageom);
57 void Entry::set_text(const string &t)
60 set_edit_position(text.size());
63 void Entry::insert(unsigned pos, const string &t)
70 unsigned old_edit_pos = edit_pos;
73 if(selection_active && selection_pos>=pos)
74 selection_pos += t.size();
76 signal_text_changed.emit(text.get());
77 if(edit_pos!=old_edit_pos)
78 signal_edit_position_changed.emit(edit_pos);
86 void Entry::erase(unsigned pos, unsigned len)
93 unsigned old_edit_pos = edit_pos;
96 else if(edit_pos>=pos)
100 if(selection_pos>=pos+len)
101 selection_pos -= len;
102 else if(selection_pos>=pos)
106 signal_text_changed.emit(text.get());
107 if(edit_pos!=old_edit_pos)
108 signal_edit_position_changed.emit(edit_pos);
116 bool Entry::get_selection(unsigned &start, unsigned &end) const
118 if(!selection_active)
121 start = selection_pos;
129 void Entry::translate_position(unsigned pos, unsigned &row, unsigned &col) const
131 text.offset_to_coords(pos, row, col);
134 unsigned Entry::translate_position(unsigned row, unsigned col) const
136 return text.coords_to_offset(row, col);
139 void Entry::set_edit_size(unsigned w, unsigned h)
143 signal_autosize_changed.emit();
146 void Entry::set_multiline(bool m)
153 slider = new VSlider;
156 slider->signal_value_changed.connect(sigc::mem_fun(this, &Entry::slider_value_changed));
163 void Entry::rebuild_special(const Part &part)
165 if(part.get_name()=="text")
166 text.build(part, state, geom, first_row, part_cache);
167 else if(part.get_name()=="cursor")
169 State cursor_state = (cursor_blink ? ACTIVE : NORMAL);
170 const Graphic *graphic = part.get_graphic(state|cursor_state);
171 if(!text_part || !graphic || !graphic->get_texture())
175 text.offset_to_coords(edit_pos, row, col);
177 if(row<first_row || row>=first_row+visible_rows)
180 Geometry rgeom = text.coords_to_geometry(*text_part, geom, first_row, row, col);
182 GL::MeshBuilder bld(part_cache.create_mesh(part, *graphic->get_texture()));
183 bld.matrix() *= GL::Matrix::translation(rgeom.x, rgeom.y, 0);
184 graphic->build(part.get_geometry().w, part.get_geometry().h, bld);
186 else if(part.get_name()=="selection")
188 if(!selection_active)
191 const Graphic *graphic = part.get_graphic(state);
192 if(!text_part || !graphic || !graphic->get_texture())
196 get_selection(start, end);
199 text.offset_to_coords(start, row, col);
200 unsigned end_row, end_col;
201 text.offset_to_coords(end, end_row, end_col);
203 if(end_row<first_row || row>=first_row+visible_rows)
212 if(end_row>=first_row+visible_rows)
214 end_row = first_row+visible_rows-1;
215 end_col = text.get_line_length(end_row);
220 unsigned ec = (row==end_row ? end_col : text.get_line_length(row));
223 Geometry rgeom = text.coords_to_geometry(*text_part, geom, first_row, row, col);
224 Geometry egeom = text.coords_to_geometry(*text_part, geom, first_row, row, ec);
226 GL::MeshBuilder bld(part_cache.create_mesh(part, *graphic->get_texture()));
227 bld.matrix() *= GL::Matrix::translation(rgeom.x, rgeom.y, 0);
228 graphic->build(egeom.x-rgeom.x, part.get_geometry().h, bld);
235 else if(part.get_name()=="slider")
239 reposition_child(*slider, part);
240 Widget::rebuild_special(part);
244 Widget::rebuild_special(part);
247 void Entry::render_special(const Part &part, GL::Renderer &renderer) const
249 if(part.get_name()=="slider" && multiline)
250 slider->render(renderer);
253 void Entry::touch_press(int x, int y, unsigned finger)
257 Widget::touch_press(x, y, finger);
260 bool Entry::key_press(unsigned key, unsigned mod)
262 got_key_press = true;
263 if(key==Input::KEY_BACKSPACE)
266 erase_selection(true);
268 erase(edit_pos-1, 1);
270 else if(key==Input::KEY_DELETE)
273 erase_selection(true);
277 else if(key==Input::KEY_ENTER && multiline)
278 insert(edit_pos, "\n");
279 else if(key==Input::KEY_END)
282 text.offset_to_coords(edit_pos, row, col);
283 set_edit_position(text.coords_to_offset(row, text.get_line_length(row)), mod==MOD_SHIFT);
285 else if(key==Input::KEY_HOME)
288 text.offset_to_coords(edit_pos, row, col);
289 set_edit_position(text.coords_to_offset(row, 0), mod==MOD_SHIFT);
291 else if(key==Input::KEY_LEFT && mod==MOD_SHIFT)
292 move_edit_position(NAV_LEFT, true);
293 else if(key==Input::KEY_RIGHT && mod==MOD_SHIFT)
294 move_edit_position(NAV_RIGHT, true);
295 else if(key==Input::KEY_UP && mod==MOD_SHIFT && multiline)
296 move_edit_position(NAV_UP, true);
297 else if(key==Input::KEY_DOWN && mod==MOD_SHIFT && multiline)
298 move_edit_position(NAV_DOWN, true);
305 bool Entry::character(wchar_t ch)
307 if(got_key_press && ch>=' ' && ch!=0x7F)
310 erase_selection(false);
311 insert(edit_pos, StringCodec::encode<StringCodec::Utf8>(StringCodec::ustring(1, ch)));
318 void Entry::focus_in()
322 check_cursor_blink();
325 void Entry::focus_out()
328 got_key_press = false;
329 check_cursor_blink();
332 bool Entry::navigate(Navigation nav)
334 if(nav==NAV_LEFT || nav==NAV_RIGHT || ((nav==NAV_DOWN || nav==NAV_UP) && multiline))
335 move_edit_position(nav, false);
336 else if(nav==NAV_ACCEPT && !signal_enter.empty())
344 void Entry::animate(const Time::TimeDelta &)
346 cursor_blink = !cursor_blink;
350 void Entry::on_geometry_change()
356 void Entry::on_style_change()
358 text.set_style(style);
366 text_part = style->get_part("text");
371 check_cursor_blink();
374 void Entry::move_edit_position(Navigation nav, bool select)
379 set_edit_position(edit_pos-1, select);
381 else if(nav==NAV_RIGHT)
383 if(edit_pos<text.size())
384 set_edit_position(edit_pos+1, select);
386 else if(nav==NAV_DOWN)
389 text.offset_to_coords(edit_pos, row, col);
390 set_edit_position(text.coords_to_offset(row+1, col), select);
395 text.offset_to_coords(edit_pos, row, col);
396 set_edit_position((row>0 ? text.coords_to_offset(row-1, col) : 0), select);
400 void Entry::set_edit_position(unsigned ep, bool select)
402 if(select && !selection_active)
403 selection_pos = edit_pos;
404 selection_active = select;
406 unsigned old_edit_pos = edit_pos;
407 edit_pos = min(ep, text.size());
409 if(edit_pos!=old_edit_pos)
410 signal_edit_position_changed.emit(edit_pos);
417 void Entry::erase_selection(bool emit_change)
420 if(!get_selection(start, end))
423 text.erase(start, end-start);
425 signal_text_changed.emit(text.get());
426 set_edit_position(start, false);
429 void Entry::check_cursor_blink()
431 cursor_blink = (state&FOCUS);
432 if((state&FOCUS) && style)
434 const Part *cursor_part = style->get_part("cursor");
435 if(cursor_part && cursor_part->get_graphic(ACTIVE|FOCUS)!=cursor_part->get_graphic(NORMAL|FOCUS))
437 set_animation_interval(Time::sec/2);
445 void Entry::check_view_range()
447 if(!multiline || !text_part)
450 visible_rows = text.get_visible_lines(*text_part, geom, 0);
453 text.offset_to_coords(edit_pos, row, col);
455 unsigned old_first_row = first_row;
458 else if(row>=first_row+visible_rows)
459 first_row = row+1-visible_rows;
461 unsigned scroll = max(text.get_n_lines(), visible_rows)-visible_rows;
465 if(first_row!=old_first_row)
466 signal_scroll_position_changed.emit(first_row);
468 slider->set_range(0, scroll);
469 slider->set_value(scroll-first_row);
472 void Entry::slider_value_changed(double value)
474 if(text.get_n_lines()>visible_rows)
476 unsigned old_first_row = first_row;
477 first_row = text.get_n_lines()-visible_rows-static_cast<unsigned>(value);
478 if(first_row!=old_first_row)
479 signal_scroll_position_changed.emit(first_row);
484 Entry::Loader::Loader(Entry &e):
485 DataFile::DerivedObjectLoader<Entry, Widget::Loader>(e)
487 add("edit_size", &Entry::edit_width, &Entry::edit_height);
488 add("multiline", &Loader::multiline);
491 void Entry::Loader::multiline(bool m)
493 obj.set_multiline(m);