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)
18 input_type = INPUT_TEXT;
22 void Entry::autosize_special(const Part &part, Geometry &ageom) const
24 if(part.get_name()=="text")
26 const Sides &margin = part.get_margin();
27 const GL::Font &font = style->get_font();
28 unsigned en_width = static_cast<unsigned>(font.get_string_width("n")*style->get_font_size());
29 ageom.w = max(ageom.w, edit_width*en_width+margin.left+margin.right);
31 unsigned line_height = static_cast<unsigned>((font.get_ascent()-font.get_descent())*style->get_font_size());
34 unsigned line_spacing = style->get_font_size()*6/5;
35 ageom.h = max(ageom.h, line_height+line_spacing*(edit_height-1)+margin.top+margin.bottom);
38 ageom.h = max(ageom.h, line_height+margin.top+margin.bottom);
40 else if(part.get_name()=="slider" && multiline)
41 autosize_child(*slider, part, ageom);
44 void Entry::set_text(const string &t)
49 signal_text_changed.emit(text.get());
51 set_edit_position(text.size());
54 void Entry::insert(size_t pos, const string &t)
60 signal_text_changed.emit(text.get());
62 adjust_edit_position_for_change(pos, t.size());
69 void Entry::erase(size_t pos, size_t len)
75 signal_text_changed.emit(text.get());
77 adjust_edit_position_for_change(pos, -len);
84 bool Entry::get_selection(size_t &start, size_t &end) const
89 start = selection_pos;
97 void Entry::translate_position(size_t pos, size_t &row, size_t &col) const
99 text.offset_to_coords(pos, row, col);
102 size_t Entry::translate_position(size_t row, size_t col) const
104 return text.coords_to_offset(row, col);
107 void Entry::set_edit_size(unsigned w, unsigned h)
111 signal_autosize_changed.emit();
114 void Entry::set_multiline(bool m)
121 slider = new VSlider;
124 slider->signal_value_changed.connect(sigc::mem_fun(this, &Entry::slider_value_changed));
131 void Entry::rebuild_special(const Part &part)
133 if(part.get_name()=="text")
134 text.build(part, state, geom, first_row, part_cache);
135 else if(part.get_name()=="cursor")
137 State cursor_state = (cursor_blink ? ACTIVE : NORMAL);
138 const Graphic *graphic = part.get_graphic(state|cursor_state);
139 if(!text_part || !graphic || !graphic->get_texture())
143 text.offset_to_coords(edit_pos, row, col);
145 if(row<first_row || row>=first_row+visible_rows)
148 Geometry rgeom = text.coords_to_geometry(*text_part, geom, first_row, row, col);
150 GL::MeshBuilder bld(part_cache.create_mesh(part, *graphic->get_texture()));
151 bld.transform(GL::Matrix::translation(rgeom.x, rgeom.y, 0));
152 graphic->build(part.get_geometry().w, part.get_geometry().h, bld);
154 else if(part.get_name()=="selection")
156 if(!selection_active)
159 const Graphic *graphic = part.get_graphic(state);
160 if(!text_part || !graphic || !graphic->get_texture())
164 get_selection(start, end);
167 text.offset_to_coords(start, row, col);
168 size_t end_row, end_col;
169 text.offset_to_coords(end, end_row, end_col);
171 if(end_row<first_row || row>=first_row+visible_rows)
180 if(end_row>=first_row+visible_rows)
182 end_row = first_row+visible_rows-1;
183 end_col = text.get_line_length(end_row);
188 size_t ec = (row==end_row ? end_col : text.get_line_length(row));
191 Geometry rgeom = text.coords_to_geometry(*text_part, geom, first_row, row, col);
192 Geometry egeom = text.coords_to_geometry(*text_part, geom, first_row, row, ec);
194 GL::MeshBuilder bld(part_cache.create_mesh(part, *graphic->get_texture()));
195 bld.transform(GL::Matrix::translation(rgeom.x, rgeom.y, 0));
196 graphic->build(egeom.x-rgeom.x, part.get_geometry().h, bld);
203 else if(part.get_name()=="slider")
207 reposition_child(*slider, part);
208 Widget::rebuild_special(part);
212 Widget::rebuild_special(part);
215 void Entry::render_special(const Part &part, GL::Renderer &renderer) const
217 if(part.get_name()=="slider" && multiline)
218 slider->render(renderer);
221 void Entry::touch_press(int x, int y, unsigned finger)
225 Widget::touch_press(x, y, finger);
228 bool Entry::key_press(unsigned key, unsigned mod)
230 got_key_press = true;
231 if(key==Input::KEY_BACKSPACE)
234 erase_selection(true);
237 size_t start_pos = text.move_offset(edit_pos, -1);
238 erase(start_pos, edit_pos-start_pos);
241 else if(key==Input::KEY_DELETE)
244 erase_selection(true);
247 size_t end_pos = text.move_offset(edit_pos, 1);
248 erase(edit_pos, end_pos-edit_pos);
251 else if(key==Input::KEY_ENTER && multiline)
252 insert(edit_pos, "\n");
253 else if(key==Input::KEY_END)
256 text.offset_to_coords(edit_pos, row, col);
257 set_edit_position(text.coords_to_offset(row, text.get_line_length(row)), mod==MOD_SHIFT);
259 else if(key==Input::KEY_HOME)
262 text.offset_to_coords(edit_pos, row, col);
263 set_edit_position(text.coords_to_offset(row, 0), mod==MOD_SHIFT);
265 else if(key==Input::KEY_PGUP)
268 text.offset_to_coords(edit_pos, row, col);
269 set_edit_position(text.coords_to_offset((row<visible_rows ? 0 : row-visible_rows), col), mod==MOD_SHIFT);
271 else if(key==Input::KEY_PGDN)
274 text.offset_to_coords(edit_pos, row, col);
275 set_edit_position(text.coords_to_offset(row+visible_rows, col), mod==MOD_SHIFT);
277 else if(key==Input::KEY_LEFT && mod==MOD_SHIFT)
278 move_edit_position(NAV_LEFT, true);
279 else if(key==Input::KEY_RIGHT && mod==MOD_SHIFT)
280 move_edit_position(NAV_RIGHT, true);
281 else if(key==Input::KEY_UP && mod==MOD_SHIFT && multiline)
282 move_edit_position(NAV_UP, true);
283 else if(key==Input::KEY_DOWN && mod==MOD_SHIFT && multiline)
284 move_edit_position(NAV_DOWN, true);
291 bool Entry::character(wchar_t ch)
293 if(got_key_press && ch>=' ' && ch!=0x7F)
296 erase_selection(false);
297 insert(edit_pos, StringCodec::encode<StringCodec::Utf8>(StringCodec::ustring(1, ch)));
304 void Entry::focus_in()
308 check_cursor_blink();
311 void Entry::focus_out()
314 got_key_press = false;
315 check_cursor_blink();
318 bool Entry::navigate(Navigation nav)
320 if(nav==NAV_LEFT || nav==NAV_RIGHT || ((nav==NAV_DOWN || nav==NAV_UP) && multiline))
321 move_edit_position(nav, false);
328 void Entry::animate(const Time::TimeDelta &)
330 cursor_blink = !cursor_blink;
334 void Entry::on_size_change()
340 void Entry::on_style_change()
342 text.set_style(style);
350 text_part = style->find_part("text");
355 check_cursor_blink();
358 void Entry::move_edit_position(Navigation nav, bool select)
361 set_edit_position(text.move_offset(edit_pos, -1), select);
362 else if(nav==NAV_RIGHT)
363 set_edit_position(text.move_offset(edit_pos, 1), select);
364 else if(nav==NAV_DOWN)
367 text.offset_to_coords(edit_pos, row, col);
368 set_edit_position(text.coords_to_offset(row+1, col), select);
373 text.offset_to_coords(edit_pos, row, col);
374 set_edit_position((row>0 ? text.coords_to_offset(row-1, col) : 0), select);
377 throw invalid_argument("Entry::move_edit_position");
380 void Entry::adjust_edit_position_for_change(size_t pos, ptrdiff_t change)
382 size_t old_edit_pos = edit_pos;
383 size_t old_select_pos = selection_pos;
389 if(selection_active && selection_pos>=pos)
390 selection_pos += change;
395 edit_pos -= min<size_t>(edit_pos-pos, -change);
396 if(selection_active && selection_pos>=pos)
397 selection_pos -= min<size_t>(selection_pos-pos, -change);
400 if(edit_pos!=old_edit_pos)
401 signal_edit_position_changed.emit(edit_pos);
402 if(selection_active && (edit_pos!=old_edit_pos || selection_pos!=old_select_pos))
405 if(get_selection(start, end))
406 signal_selection_changed.emit(start, end);
410 void Entry::set_edit_position(size_t ep, bool select)
412 bool selection_was_active = selection_active;
413 if(select && !selection_active)
414 selection_pos = edit_pos;
415 selection_active = select;
417 size_t old_edit_pos = edit_pos;
418 edit_pos = min(ep, text.size());
420 if(edit_pos!=old_edit_pos)
422 signal_edit_position_changed.emit(edit_pos);
424 if(get_selection(start, end))
425 signal_selection_changed.emit(start, end);
426 else if(selection_was_active)
427 signal_selection_changed.emit(edit_pos, edit_pos);
435 void Entry::erase_selection(bool emit_change)
438 if(!get_selection(start, end))
441 text.erase(start, end-start);
443 signal_text_changed.emit(text.get());
444 set_edit_position(start, false);
447 void Entry::check_cursor_blink()
449 const Part *cursor_part = style->find_part("cursor");
450 bool has_blink = (cursor_part && cursor_part->get_graphic(ACTIVE|FOCUS)!=cursor_part->get_graphic(NORMAL|FOCUS));
452 cursor_blink = (state&FOCUS);
453 if((state&FOCUS) && style && has_blink)
455 set_animation_interval(Time::sec/2);
466 void Entry::check_view_range()
468 if(!multiline || !text_part)
471 visible_rows = text.get_visible_lines(*text_part, geom, nullptr);
474 text.offset_to_coords(edit_pos, row, col);
476 size_t old_first_row = first_row;
479 else if(row>=first_row+visible_rows)
480 first_row = row+1-visible_rows;
482 size_t scroll = max<size_t>(text.get_n_lines(), visible_rows)-visible_rows;
486 if(first_row!=old_first_row)
487 signal_scroll_position_changed.emit(first_row);
489 slider->set_range(0, scroll);
490 slider->set_page_size(visible_rows);
491 slider->set_value(scroll-first_row);
494 void Entry::slider_value_changed(double value)
496 if(text.get_n_lines()>visible_rows)
498 size_t old_first_row = first_row;
499 first_row = text.get_n_lines()-visible_rows-static_cast<size_t>(value);
500 if(first_row!=old_first_row)
501 signal_scroll_position_changed.emit(first_row);
507 Entry::Loader::Loader(Entry &e):
508 DataFile::DerivedObjectLoader<Entry, Widget::Loader>(e)
510 add("edit_size", &Entry::edit_width, &Entry::edit_height);
511 add("multiline", &Loader::multiline);
514 void Entry::Loader::multiline(bool m)
516 obj.set_multiline(m);