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)
69 if(selection_active && selection_pos>=pos)
70 selection_pos += t.size();
78 void Entry::erase(unsigned pos, unsigned len)
84 else if(edit_pos>=pos)
88 if(selection_pos>=pos+len)
90 else if(selection_pos>=pos)
100 void Entry::set_edit_position(unsigned pos)
102 edit_pos = min(pos, text.size());
103 selection_active = false;
111 bool Entry::get_selection(unsigned &start, unsigned &end) const
113 if(!selection_active)
116 start = selection_pos;
124 void Entry::set_edit_size(unsigned w, unsigned h)
128 signal_autosize_changed.emit();
131 void Entry::set_multiline(bool m)
138 slider = new VSlider;
141 slider->signal_value_changed.connect(sigc::mem_fun(this, &Entry::slider_value_changed));
148 void Entry::rebuild_special(const Part &part)
150 if(part.get_name()=="text")
151 text.build(part, state, geom, first_row, part_cache);
152 else if(part.get_name()=="cursor")
154 State cursor_state = (cursor_blink ? ACTIVE : NORMAL);
155 const Graphic *graphic = part.get_graphic(state|cursor_state);
156 if(!text_part || !graphic || !graphic->get_texture())
160 text.offset_to_coords(edit_pos, row, col);
162 if(row<first_row || row>=first_row+visible_rows)
165 Geometry rgeom = text.coords_to_geometry(*text_part, geom, first_row, row, col);
167 GL::MeshBuilder bld(part_cache.create_mesh(part, *graphic->get_texture()));
168 bld.matrix() *= GL::Matrix::translation(rgeom.x, rgeom.y, 0);
169 graphic->build(part.get_geometry().w, part.get_geometry().h, bld);
171 else if(part.get_name()=="selection")
173 if(!selection_active)
176 const Graphic *graphic = part.get_graphic(state);
177 if(!text_part || !graphic || !graphic->get_texture())
180 unsigned start = selection_pos;
181 unsigned end = edit_pos;
186 text.offset_to_coords(start, row, col);
187 unsigned end_row, end_col;
188 text.offset_to_coords(end, end_row, end_col);
190 if(end_row<first_row || row>=first_row+visible_rows)
199 if(end_row>=first_row+visible_rows)
201 end_row = first_row+visible_rows-1;
202 end_col = text.get_line_length(end_row);
207 unsigned ec = (row==end_row ? end_col : text.get_line_length(row));
210 Geometry rgeom = text.coords_to_geometry(*text_part, geom, first_row, row, col);
211 Geometry egeom = text.coords_to_geometry(*text_part, geom, first_row, row, ec);
213 GL::MeshBuilder bld(part_cache.create_mesh(part, *graphic->get_texture()));
214 bld.matrix() *= GL::Matrix::translation(rgeom.x, rgeom.y, 0);
215 graphic->build(egeom.x-rgeom.x, part.get_geometry().h, bld);
222 else if(part.get_name()=="slider")
226 reposition_child(*slider, part);
227 Widget::rebuild_special(part);
231 Widget::rebuild_special(part);
234 void Entry::render_special(const Part &part, GL::Renderer &renderer) const
236 if(part.get_name()=="slider" && multiline)
237 slider->render(renderer);
240 void Entry::touch_press(int x, int y, unsigned finger)
244 Widget::touch_press(x, y, finger);
247 bool Entry::key_press(unsigned key, unsigned mod)
249 got_key_press = true;
250 if(key==Input::KEY_BACKSPACE)
256 text.erase(--edit_pos, 1);
261 else if(key==Input::KEY_DELETE)
266 text.erase(edit_pos, 1);
268 else if(key==Input::KEY_ENTER && multiline)
270 text.insert(edit_pos++, "\n");
274 else if(key==Input::KEY_END)
277 text.offset_to_coords(edit_pos, row, col);
278 set_edit_position(text.coords_to_offset(row, text.get_line_length(row)), mod==MOD_SHIFT);
280 else if(key==Input::KEY_HOME)
283 text.offset_to_coords(edit_pos, row, col);
284 set_edit_position(text.coords_to_offset(row, 0), mod==MOD_SHIFT);
286 else if(key==Input::KEY_LEFT && mod==MOD_SHIFT)
287 move_edit_position(NAV_LEFT, true);
288 else if(key==Input::KEY_RIGHT && mod==MOD_SHIFT)
289 move_edit_position(NAV_RIGHT, true);
290 else if(key==Input::KEY_UP && mod==MOD_SHIFT && multiline)
291 move_edit_position(NAV_UP, true);
292 else if(key==Input::KEY_DOWN && mod==MOD_SHIFT && multiline)
293 move_edit_position(NAV_DOWN, true);
300 bool Entry::character(wchar_t ch)
302 if(got_key_press && ch>=' ' && ch!=0x7F)
306 text.insert(edit_pos, StringCodec::encode<StringCodec::Utf8>(StringCodec::ustring(1, ch)));
315 void Entry::focus_in()
319 check_cursor_blink();
322 void Entry::focus_out()
325 got_key_press = false;
326 check_cursor_blink();
329 bool Entry::navigate(Navigation nav)
331 if(nav==NAV_LEFT || nav==NAV_RIGHT || ((nav==NAV_DOWN || nav==NAV_UP) && multiline))
332 move_edit_position(nav, false);
333 else if(nav==NAV_ACCEPT && !signal_enter.empty())
341 void Entry::animate(const Time::TimeDelta &)
343 cursor_blink = !cursor_blink;
347 void Entry::on_geometry_change()
353 void Entry::on_style_change()
355 text.set_style(style);
363 text_part = style->get_part("text");
368 check_cursor_blink();
371 void Entry::move_edit_position(Navigation nav, bool select)
376 set_edit_position(edit_pos-1, select);
378 else if(nav==NAV_RIGHT)
380 if(edit_pos<text.size())
381 set_edit_position(edit_pos+1, select);
383 else if(nav==NAV_DOWN)
386 text.offset_to_coords(edit_pos, row, col);
387 set_edit_position(text.coords_to_offset(row+1, col), select);
392 text.offset_to_coords(edit_pos, row, col);
393 set_edit_position((row>0 ? text.coords_to_offset(row-1, col) : 0), select);
397 void Entry::set_edit_position(unsigned ep, bool select)
399 if(select && !selection_active)
400 selection_pos = edit_pos;
401 selection_active = select;
408 void Entry::erase_selection()
410 if(!selection_active)
413 unsigned start = selection_pos;
414 unsigned end = edit_pos;
418 text.erase(start, end-start);
419 set_edit_position(start, false);
422 void Entry::check_cursor_blink()
424 cursor_blink = (state&FOCUS);
425 if((state&FOCUS) && style)
427 const Part *cursor_part = style->get_part("cursor");
428 if(cursor_part && cursor_part->get_graphic(ACTIVE|FOCUS)!=cursor_part->get_graphic(NORMAL|FOCUS))
430 set_animation_interval(Time::sec/2);
438 void Entry::check_view_range()
440 if(!multiline || !text_part)
443 float font_size = style->get_font_size();
444 unsigned line_spacing = static_cast<unsigned>(font_size*6/5);
446 const Sides &margin = text_part->get_margin();
447 visible_rows = max((geom.h-margin.top-margin.bottom)/line_spacing, 1U);
450 text.offset_to_coords(edit_pos, row, col);
454 else if(row>=first_row+visible_rows)
455 first_row = row+1-visible_rows;
457 unsigned scroll = max(text.get_n_lines(), visible_rows)-visible_rows;
458 slider->set_range(0, scroll);
459 slider->set_value(scroll-first_row);
462 void Entry::slider_value_changed(double value)
464 if(text.get_n_lines()>visible_rows)
465 first_row = text.get_n_lines()-visible_rows-static_cast<unsigned>(value);
469 Entry::Loader::Loader(Entry &e):
470 DataFile::DerivedObjectLoader<Entry, Widget::Loader>(e)
472 add("edit_size", &Entry::edit_width, &Entry::edit_height);
473 add("multiline", &Loader::multiline);
476 void Entry::Loader::multiline(bool m)
478 obj.set_multiline(m);