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)
67 unsigned old_edit_pos = edit_pos;
70 if(selection_active && selection_pos>=pos)
71 selection_pos += t.size();
73 if(edit_pos!=old_edit_pos)
74 signal_edit_position_changed.emit(edit_pos);
82 void Entry::erase(unsigned pos, unsigned len)
86 unsigned old_edit_pos = edit_pos;
89 else if(edit_pos>=pos)
93 if(selection_pos>=pos+len)
95 else if(selection_pos>=pos)
99 if(edit_pos!=old_edit_pos)
100 signal_edit_position_changed.emit(edit_pos);
108 bool Entry::get_selection(unsigned &start, unsigned &end) const
110 if(!selection_active)
113 start = selection_pos;
121 void Entry::translate_position(unsigned pos, unsigned &row, unsigned &col) const
123 text.offset_to_coords(pos, row, col);
126 unsigned Entry::translate_position(unsigned row, unsigned col) const
128 return text.coords_to_offset(row, col);
131 void Entry::set_edit_size(unsigned w, unsigned h)
135 signal_autosize_changed.emit();
138 void Entry::set_multiline(bool m)
145 slider = new VSlider;
148 slider->signal_value_changed.connect(sigc::mem_fun(this, &Entry::slider_value_changed));
155 void Entry::rebuild_special(const Part &part)
157 if(part.get_name()=="text")
158 text.build(part, state, geom, first_row, part_cache);
159 else if(part.get_name()=="cursor")
161 State cursor_state = (cursor_blink ? ACTIVE : NORMAL);
162 const Graphic *graphic = part.get_graphic(state|cursor_state);
163 if(!text_part || !graphic || !graphic->get_texture())
167 text.offset_to_coords(edit_pos, row, col);
169 if(row<first_row || row>=first_row+visible_rows)
172 Geometry rgeom = text.coords_to_geometry(*text_part, geom, first_row, row, col);
174 GL::MeshBuilder bld(part_cache.create_mesh(part, *graphic->get_texture()));
175 bld.matrix() *= GL::Matrix::translation(rgeom.x, rgeom.y, 0);
176 graphic->build(part.get_geometry().w, part.get_geometry().h, bld);
178 else if(part.get_name()=="selection")
180 if(!selection_active)
183 const Graphic *graphic = part.get_graphic(state);
184 if(!text_part || !graphic || !graphic->get_texture())
188 get_selection(start, end);
191 text.offset_to_coords(start, row, col);
192 unsigned end_row, end_col;
193 text.offset_to_coords(end, end_row, end_col);
195 if(end_row<first_row || row>=first_row+visible_rows)
204 if(end_row>=first_row+visible_rows)
206 end_row = first_row+visible_rows-1;
207 end_col = text.get_line_length(end_row);
212 unsigned ec = (row==end_row ? end_col : text.get_line_length(row));
215 Geometry rgeom = text.coords_to_geometry(*text_part, geom, first_row, row, col);
216 Geometry egeom = text.coords_to_geometry(*text_part, geom, first_row, row, ec);
218 GL::MeshBuilder bld(part_cache.create_mesh(part, *graphic->get_texture()));
219 bld.matrix() *= GL::Matrix::translation(rgeom.x, rgeom.y, 0);
220 graphic->build(egeom.x-rgeom.x, part.get_geometry().h, bld);
227 else if(part.get_name()=="slider")
231 reposition_child(*slider, part);
232 Widget::rebuild_special(part);
236 Widget::rebuild_special(part);
239 void Entry::render_special(const Part &part, GL::Renderer &renderer) const
241 if(part.get_name()=="slider" && multiline)
242 slider->render(renderer);
245 void Entry::touch_press(int x, int y, unsigned finger)
249 Widget::touch_press(x, y, finger);
252 bool Entry::key_press(unsigned key, unsigned mod)
254 got_key_press = true;
255 if(key==Input::KEY_BACKSPACE)
260 erase(edit_pos-1, 1);
262 else if(key==Input::KEY_DELETE)
269 else if(key==Input::KEY_ENTER && multiline)
270 insert(edit_pos, "\n");
271 else if(key==Input::KEY_END)
274 text.offset_to_coords(edit_pos, row, col);
275 set_edit_position(text.coords_to_offset(row, text.get_line_length(row)), mod==MOD_SHIFT);
277 else if(key==Input::KEY_HOME)
280 text.offset_to_coords(edit_pos, row, col);
281 set_edit_position(text.coords_to_offset(row, 0), mod==MOD_SHIFT);
283 else if(key==Input::KEY_LEFT && mod==MOD_SHIFT)
284 move_edit_position(NAV_LEFT, true);
285 else if(key==Input::KEY_RIGHT && mod==MOD_SHIFT)
286 move_edit_position(NAV_RIGHT, true);
287 else if(key==Input::KEY_UP && mod==MOD_SHIFT && multiline)
288 move_edit_position(NAV_UP, true);
289 else if(key==Input::KEY_DOWN && mod==MOD_SHIFT && multiline)
290 move_edit_position(NAV_DOWN, true);
297 bool Entry::character(wchar_t ch)
299 if(got_key_press && ch>=' ' && ch!=0x7F)
303 insert(edit_pos, StringCodec::encode<StringCodec::Utf8>(StringCodec::ustring(1, ch)));
310 void Entry::focus_in()
314 check_cursor_blink();
317 void Entry::focus_out()
320 got_key_press = false;
321 check_cursor_blink();
324 bool Entry::navigate(Navigation nav)
326 if(nav==NAV_LEFT || nav==NAV_RIGHT || ((nav==NAV_DOWN || nav==NAV_UP) && multiline))
327 move_edit_position(nav, false);
328 else if(nav==NAV_ACCEPT && !signal_enter.empty())
336 void Entry::animate(const Time::TimeDelta &)
338 cursor_blink = !cursor_blink;
342 void Entry::on_geometry_change()
348 void Entry::on_style_change()
350 text.set_style(style);
358 text_part = style->get_part("text");
363 check_cursor_blink();
366 void Entry::move_edit_position(Navigation nav, bool select)
371 set_edit_position(edit_pos-1, select);
373 else if(nav==NAV_RIGHT)
375 if(edit_pos<text.size())
376 set_edit_position(edit_pos+1, select);
378 else if(nav==NAV_DOWN)
381 text.offset_to_coords(edit_pos, row, col);
382 set_edit_position(text.coords_to_offset(row+1, col), select);
387 text.offset_to_coords(edit_pos, row, col);
388 set_edit_position((row>0 ? text.coords_to_offset(row-1, col) : 0), select);
392 void Entry::set_edit_position(unsigned ep, bool select)
394 if(select && !selection_active)
395 selection_pos = edit_pos;
396 selection_active = select;
398 unsigned old_edit_pos = edit_pos;
399 edit_pos = min(ep, text.size());
401 if(edit_pos!=old_edit_pos)
402 signal_edit_position_changed.emit(edit_pos);
409 void Entry::erase_selection()
412 if(!get_selection(start, end))
415 text.erase(start, end-start);
416 set_edit_position(start, false);
419 void Entry::check_cursor_blink()
421 cursor_blink = (state&FOCUS);
422 if((state&FOCUS) && style)
424 const Part *cursor_part = style->get_part("cursor");
425 if(cursor_part && cursor_part->get_graphic(ACTIVE|FOCUS)!=cursor_part->get_graphic(NORMAL|FOCUS))
427 set_animation_interval(Time::sec/2);
435 void Entry::check_view_range()
437 if(!multiline || !text_part)
440 visible_rows = text.get_visible_lines(*text_part, geom, 0);
443 text.offset_to_coords(edit_pos, row, col);
445 unsigned old_first_row = first_row;
448 else if(row>=first_row+visible_rows)
449 first_row = row+1-visible_rows;
451 if(first_row!=old_first_row)
452 signal_scroll_position_changed.emit(first_row);
454 unsigned scroll = max(text.get_n_lines(), visible_rows)-visible_rows;
455 slider->set_range(0, scroll);
456 slider->set_value(scroll-first_row);
459 void Entry::slider_value_changed(double value)
461 if(text.get_n_lines()>visible_rows)
463 unsigned old_first_row = first_row;
464 first_row = text.get_n_lines()-visible_rows-static_cast<unsigned>(value);
465 if(first_row!=old_first_row)
466 signal_scroll_position_changed.emit(first_row);
471 Entry::Loader::Loader(Entry &e):
472 DataFile::DerivedObjectLoader<Entry, Widget::Loader>(e)
474 add("edit_size", &Entry::edit_width, &Entry::edit_height);
475 add("multiline", &Loader::multiline);
478 void Entry::Loader::multiline(bool m)
480 obj.set_multiline(m);