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)
62 signal_text_changed.emit(text.get());
64 set_edit_position(text.size());
67 void Entry::insert(unsigned pos, const string &t)
74 unsigned old_edit_pos = edit_pos;
77 if(selection_active && selection_pos>=pos)
78 selection_pos += t.size();
80 signal_text_changed.emit(text.get());
81 if(edit_pos!=old_edit_pos)
82 signal_edit_position_changed.emit(edit_pos);
90 void Entry::erase(unsigned pos, unsigned len)
97 unsigned old_edit_pos = edit_pos;
100 else if(edit_pos>=pos)
104 if(selection_pos>=pos+len)
105 selection_pos -= len;
106 else if(selection_pos>=pos)
110 signal_text_changed.emit(text.get());
111 if(edit_pos!=old_edit_pos)
112 signal_edit_position_changed.emit(edit_pos);
120 bool Entry::get_selection(unsigned &start, unsigned &end) const
122 if(!selection_active)
125 start = selection_pos;
133 void Entry::translate_position(unsigned pos, unsigned &row, unsigned &col) const
135 text.offset_to_coords(pos, row, col);
138 unsigned Entry::translate_position(unsigned row, unsigned col) const
140 return text.coords_to_offset(row, col);
143 void Entry::set_edit_size(unsigned w, unsigned h)
147 signal_autosize_changed.emit();
150 void Entry::set_multiline(bool m)
157 slider = new VSlider;
160 slider->signal_value_changed.connect(sigc::mem_fun(this, &Entry::slider_value_changed));
167 void Entry::rebuild_special(const Part &part)
169 if(part.get_name()=="text")
170 text.build(part, state, geom, first_row, part_cache);
171 else if(part.get_name()=="cursor")
173 State cursor_state = (cursor_blink ? ACTIVE : NORMAL);
174 const Graphic *graphic = part.get_graphic(state|cursor_state);
175 if(!text_part || !graphic || !graphic->get_texture())
179 text.offset_to_coords(edit_pos, row, col);
181 if(row<first_row || row>=first_row+visible_rows)
184 Geometry rgeom = text.coords_to_geometry(*text_part, geom, first_row, row, col);
186 GL::MeshBuilder bld(part_cache.create_mesh(part, *graphic->get_texture()));
187 bld.matrix() *= GL::Matrix::translation(rgeom.x, rgeom.y, 0);
188 graphic->build(part.get_geometry().w, part.get_geometry().h, bld);
190 else if(part.get_name()=="selection")
192 if(!selection_active)
195 const Graphic *graphic = part.get_graphic(state);
196 if(!text_part || !graphic || !graphic->get_texture())
200 get_selection(start, end);
203 text.offset_to_coords(start, row, col);
204 unsigned end_row, end_col;
205 text.offset_to_coords(end, end_row, end_col);
207 if(end_row<first_row || row>=first_row+visible_rows)
216 if(end_row>=first_row+visible_rows)
218 end_row = first_row+visible_rows-1;
219 end_col = text.get_line_length(end_row);
224 unsigned ec = (row==end_row ? end_col : text.get_line_length(row));
227 Geometry rgeom = text.coords_to_geometry(*text_part, geom, first_row, row, col);
228 Geometry egeom = text.coords_to_geometry(*text_part, geom, first_row, row, ec);
230 GL::MeshBuilder bld(part_cache.create_mesh(part, *graphic->get_texture()));
231 bld.matrix() *= GL::Matrix::translation(rgeom.x, rgeom.y, 0);
232 graphic->build(egeom.x-rgeom.x, part.get_geometry().h, bld);
239 else if(part.get_name()=="slider")
243 reposition_child(*slider, part);
244 Widget::rebuild_special(part);
248 Widget::rebuild_special(part);
251 void Entry::render_special(const Part &part, GL::Renderer &renderer) const
253 if(part.get_name()=="slider" && multiline)
254 slider->render(renderer);
257 void Entry::touch_press(int x, int y, unsigned finger)
261 Widget::touch_press(x, y, finger);
264 bool Entry::key_press(unsigned key, unsigned mod)
266 got_key_press = true;
267 if(key==Input::KEY_BACKSPACE)
270 erase_selection(true);
273 unsigned start_pos = text.move_offset(edit_pos, -1);
274 erase(start_pos, edit_pos-start_pos);
277 else if(key==Input::KEY_DELETE)
280 erase_selection(true);
283 unsigned end_pos = text.move_offset(edit_pos, 1);
284 erase(edit_pos, end_pos-edit_pos);
287 else if(key==Input::KEY_ENTER && multiline)
288 insert(edit_pos, "\n");
289 else if(key==Input::KEY_END)
292 text.offset_to_coords(edit_pos, row, col);
293 set_edit_position(text.coords_to_offset(row, text.get_line_length(row)), mod==MOD_SHIFT);
295 else if(key==Input::KEY_HOME)
298 text.offset_to_coords(edit_pos, row, col);
299 set_edit_position(text.coords_to_offset(row, 0), mod==MOD_SHIFT);
301 else if(key==Input::KEY_LEFT && mod==MOD_SHIFT)
302 move_edit_position(NAV_LEFT, true);
303 else if(key==Input::KEY_RIGHT && mod==MOD_SHIFT)
304 move_edit_position(NAV_RIGHT, true);
305 else if(key==Input::KEY_UP && mod==MOD_SHIFT && multiline)
306 move_edit_position(NAV_UP, true);
307 else if(key==Input::KEY_DOWN && mod==MOD_SHIFT && multiline)
308 move_edit_position(NAV_DOWN, true);
315 bool Entry::character(wchar_t ch)
317 if(got_key_press && ch>=' ' && ch!=0x7F)
320 erase_selection(false);
321 insert(edit_pos, StringCodec::encode<StringCodec::Utf8>(StringCodec::ustring(1, ch)));
328 void Entry::focus_in()
332 check_cursor_blink();
335 void Entry::focus_out()
338 got_key_press = false;
339 check_cursor_blink();
342 bool Entry::navigate(Navigation nav)
344 if(nav==NAV_LEFT || nav==NAV_RIGHT || ((nav==NAV_DOWN || nav==NAV_UP) && multiline))
345 move_edit_position(nav, false);
346 else if(nav==NAV_ACCEPT && !signal_enter.empty())
354 void Entry::animate(const Time::TimeDelta &)
356 cursor_blink = !cursor_blink;
360 void Entry::on_geometry_change()
366 void Entry::on_style_change()
368 text.set_style(style);
376 text_part = style->get_part("text");
381 check_cursor_blink();
384 void Entry::move_edit_position(Navigation nav, bool select)
387 set_edit_position(text.move_offset(edit_pos, -1), select);
388 else if(nav==NAV_RIGHT)
389 set_edit_position(text.move_offset(edit_pos, 1), select);
390 else if(nav==NAV_DOWN)
393 text.offset_to_coords(edit_pos, row, col);
394 set_edit_position(text.coords_to_offset(row+1, col), select);
399 text.offset_to_coords(edit_pos, row, col);
400 set_edit_position((row>0 ? text.coords_to_offset(row-1, col) : 0), select);
403 throw invalid_argument("Entry::move_edit_position");
406 void Entry::set_edit_position(unsigned ep, bool select)
408 if(select && !selection_active)
409 selection_pos = edit_pos;
410 selection_active = select;
412 unsigned old_edit_pos = edit_pos;
413 edit_pos = min(ep, text.size());
415 if(edit_pos!=old_edit_pos)
416 signal_edit_position_changed.emit(edit_pos);
423 void Entry::erase_selection(bool emit_change)
426 if(!get_selection(start, end))
429 text.erase(start, end-start);
431 signal_text_changed.emit(text.get());
432 set_edit_position(start, false);
435 void Entry::check_cursor_blink()
437 cursor_blink = (state&FOCUS);
438 if((state&FOCUS) && style)
440 const Part *cursor_part = style->get_part("cursor");
441 if(cursor_part && cursor_part->get_graphic(ACTIVE|FOCUS)!=cursor_part->get_graphic(NORMAL|FOCUS))
443 set_animation_interval(Time::sec/2);
451 void Entry::check_view_range()
453 if(!multiline || !text_part)
456 visible_rows = text.get_visible_lines(*text_part, geom, 0);
459 text.offset_to_coords(edit_pos, row, col);
461 unsigned old_first_row = first_row;
464 else if(row>=first_row+visible_rows)
465 first_row = row+1-visible_rows;
467 unsigned scroll = max(text.get_n_lines(), visible_rows)-visible_rows;
471 if(first_row!=old_first_row)
472 signal_scroll_position_changed.emit(first_row);
474 slider->set_range(0, scroll);
475 slider->set_value(scroll-first_row);
478 void Entry::slider_value_changed(double value)
480 if(text.get_n_lines()>visible_rows)
482 unsigned old_first_row = first_row;
483 first_row = text.get_n_lines()-visible_rows-static_cast<unsigned>(value);
484 if(first_row!=old_first_row)
485 signal_scroll_position_changed.emit(first_row);
490 Entry::Loader::Loader(Entry &e):
491 DataFile::DerivedObjectLoader<Entry, Widget::Loader>(e)
493 add("edit_size", &Entry::edit_width, &Entry::edit_height);
494 add("multiline", &Loader::multiline);
497 void Entry::Loader::multiline(bool m)
499 obj.set_multiline(m);