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 edit_pos = text.size();
68 void Entry::set_edit_size(unsigned w, unsigned h)
72 signal_autosize_changed.emit();
75 void Entry::set_multiline(bool m)
85 slider->signal_value_changed.connect(sigc::mem_fun(this, &Entry::slider_value_changed));
92 void Entry::rebuild_special(const Part &part)
94 if(part.get_name()=="text")
95 text.build(part, state, geom, first_row, part_cache);
96 else if(part.get_name()=="cursor")
98 State cursor_state = (cursor_blink ? ACTIVE : NORMAL);
99 const Graphic *graphic = part.get_graphic(state|cursor_state);
100 if(!text_part || !graphic || !graphic->get_texture())
104 text.offset_to_coords(edit_pos, row, col);
106 if(row<first_row || row>=first_row+visible_rows)
109 Geometry rgeom = text.coords_to_geometry(*text_part, geom, first_row, row, col);
111 GL::MeshBuilder bld(part_cache.create_mesh(part, *graphic->get_texture()));
112 bld.matrix() *= GL::Matrix::translation(rgeom.x, rgeom.y, 0);
113 graphic->build(part.get_geometry().w, part.get_geometry().h, bld);
115 else if(part.get_name()=="selection")
117 if(!selection_active)
120 const Graphic *graphic = part.get_graphic(state);
121 if(!text_part || !graphic || !graphic->get_texture())
124 unsigned start = selection_pos;
125 unsigned end = edit_pos;
130 text.offset_to_coords(start, row, col);
131 unsigned end_row, end_col;
132 text.offset_to_coords(end, end_row, end_col);
134 if(end_row<first_row || row>=first_row+visible_rows)
143 if(end_row>=first_row+visible_rows)
145 end_row = first_row+visible_rows-1;
146 end_col = text.get_line_length(end_row);
151 unsigned ec = (row==end_row ? end_col : text.get_line_length(row));
154 Geometry rgeom = text.coords_to_geometry(*text_part, geom, first_row, row, col);
155 Geometry egeom = text.coords_to_geometry(*text_part, geom, first_row, row, ec);
157 GL::MeshBuilder bld(part_cache.create_mesh(part, *graphic->get_texture()));
158 bld.matrix() *= GL::Matrix::translation(rgeom.x, rgeom.y, 0);
159 graphic->build(egeom.x-rgeom.x, part.get_geometry().h, bld);
166 else if(part.get_name()=="slider")
170 reposition_child(*slider, part);
171 Widget::rebuild_special(part);
175 Widget::rebuild_special(part);
178 void Entry::render_special(const Part &part, GL::Renderer &renderer) const
180 if(part.get_name()=="slider" && multiline)
181 slider->render(renderer);
184 void Entry::touch_press(int x, int y, unsigned finger)
188 Widget::touch_press(x, y, finger);
191 bool Entry::key_press(unsigned key, unsigned mod)
193 got_key_press = true;
194 if(key==Input::KEY_BACKSPACE)
200 text.erase(--edit_pos, 1);
205 else if(key==Input::KEY_DELETE)
210 text.erase(edit_pos, 1);
212 else if(key==Input::KEY_ENTER && multiline)
214 text.insert(edit_pos++, "\n");
218 else if(key==Input::KEY_END)
221 text.offset_to_coords(edit_pos, row, col);
222 set_edit_position(text.coords_to_offset(row, text.get_line_length(row)), mod==MOD_SHIFT);
224 else if(key==Input::KEY_HOME)
227 text.offset_to_coords(edit_pos, row, col);
228 set_edit_position(text.coords_to_offset(row, 0), mod==MOD_SHIFT);
230 else if(key==Input::KEY_LEFT && mod==MOD_SHIFT)
231 move_edit_position(NAV_LEFT, true);
232 else if(key==Input::KEY_RIGHT && mod==MOD_SHIFT)
233 move_edit_position(NAV_RIGHT, true);
234 else if(key==Input::KEY_UP && mod==MOD_SHIFT && multiline)
235 move_edit_position(NAV_UP, true);
236 else if(key==Input::KEY_DOWN && mod==MOD_SHIFT && multiline)
237 move_edit_position(NAV_DOWN, true);
244 bool Entry::character(wchar_t ch)
246 if(got_key_press && ch>=' ' && ch!=0x7F)
250 text.insert(edit_pos, StringCodec::encode<StringCodec::Utf8>(StringCodec::ustring(1, ch)));
259 void Entry::focus_in()
263 check_cursor_blink();
266 void Entry::focus_out()
269 got_key_press = false;
270 check_cursor_blink();
273 bool Entry::navigate(Navigation nav)
275 if(nav==NAV_LEFT || nav==NAV_RIGHT || ((nav==NAV_DOWN || nav==NAV_UP) && multiline))
276 move_edit_position(nav, false);
277 else if(nav==NAV_ACCEPT && !signal_enter.empty())
285 void Entry::animate(const Time::TimeDelta &)
287 cursor_blink = !cursor_blink;
291 void Entry::on_geometry_change()
297 void Entry::on_style_change()
299 text.set_style(style);
307 text_part = style->get_part("text");
312 check_cursor_blink();
315 void Entry::move_edit_position(Navigation nav, bool select)
320 set_edit_position(edit_pos-1, select);
322 else if(nav==NAV_RIGHT)
324 if(edit_pos<text.size())
325 set_edit_position(edit_pos+1, select);
327 else if(nav==NAV_DOWN)
330 text.offset_to_coords(edit_pos, row, col);
331 set_edit_position(text.coords_to_offset(row+1, col), select);
336 text.offset_to_coords(edit_pos, row, col);
337 set_edit_position((row>0 ? text.coords_to_offset(row-1, col) : 0), select);
341 void Entry::set_edit_position(unsigned ep, bool select)
343 if(select && !selection_active)
344 selection_pos = edit_pos;
345 selection_active = select;
352 void Entry::erase_selection()
354 if(!selection_active)
357 unsigned start = selection_pos;
358 unsigned end = edit_pos;
362 text.erase(start, end-start);
363 set_edit_position(start, false);
366 void Entry::check_cursor_blink()
368 cursor_blink = (state&FOCUS);
369 if((state&FOCUS) && style)
371 const Part *cursor_part = style->get_part("cursor");
372 if(cursor_part && cursor_part->get_graphic(ACTIVE|FOCUS)!=cursor_part->get_graphic(NORMAL|FOCUS))
374 set_animation_interval(Time::sec/2);
382 void Entry::check_view_range()
384 if(!multiline || !text_part)
387 float font_size = style->get_font_size();
388 unsigned line_spacing = static_cast<unsigned>(font_size*6/5);
390 const Sides &margin = text_part->get_margin();
391 visible_rows = max((geom.h-margin.top-margin.bottom)/line_spacing, 1U);
394 text.offset_to_coords(edit_pos, row, col);
398 else if(row>=first_row+visible_rows)
399 first_row = row+1-visible_rows;
401 unsigned scroll = max(text.get_n_lines(), visible_rows)-visible_rows;
402 slider->set_range(0, scroll);
403 slider->set_value(scroll-first_row);
406 void Entry::slider_value_changed(double value)
408 if(text.get_n_lines()>visible_rows)
409 first_row = text.get_n_lines()-visible_rows-static_cast<unsigned>(value);
413 Entry::Loader::Loader(Entry &e):
414 DataFile::DerivedObjectLoader<Entry, Widget::Loader>(e)
416 add("edit_size", &Entry::edit_width, &Entry::edit_height);
417 add("multiline", &Loader::multiline);
420 void Entry::Loader::multiline(bool m)
422 obj.set_multiline(m);