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)
255 erase(edit_pos-1, 1);
257 else if(key==Input::KEY_DELETE)
264 else if(key==Input::KEY_ENTER && multiline)
265 insert(edit_pos, "\n");
266 else if(key==Input::KEY_END)
269 text.offset_to_coords(edit_pos, row, col);
270 set_edit_position(text.coords_to_offset(row, text.get_line_length(row)), mod==MOD_SHIFT);
272 else if(key==Input::KEY_HOME)
275 text.offset_to_coords(edit_pos, row, col);
276 set_edit_position(text.coords_to_offset(row, 0), mod==MOD_SHIFT);
278 else if(key==Input::KEY_LEFT && mod==MOD_SHIFT)
279 move_edit_position(NAV_LEFT, true);
280 else if(key==Input::KEY_RIGHT && mod==MOD_SHIFT)
281 move_edit_position(NAV_RIGHT, true);
282 else if(key==Input::KEY_UP && mod==MOD_SHIFT && multiline)
283 move_edit_position(NAV_UP, true);
284 else if(key==Input::KEY_DOWN && mod==MOD_SHIFT && multiline)
285 move_edit_position(NAV_DOWN, true);
292 bool Entry::character(wchar_t ch)
294 if(got_key_press && ch>=' ' && ch!=0x7F)
298 insert(edit_pos, StringCodec::encode<StringCodec::Utf8>(StringCodec::ustring(1, ch)));
305 void Entry::focus_in()
309 check_cursor_blink();
312 void Entry::focus_out()
315 got_key_press = false;
316 check_cursor_blink();
319 bool Entry::navigate(Navigation nav)
321 if(nav==NAV_LEFT || nav==NAV_RIGHT || ((nav==NAV_DOWN || nav==NAV_UP) && multiline))
322 move_edit_position(nav, false);
323 else if(nav==NAV_ACCEPT && !signal_enter.empty())
331 void Entry::animate(const Time::TimeDelta &)
333 cursor_blink = !cursor_blink;
337 void Entry::on_geometry_change()
343 void Entry::on_style_change()
345 text.set_style(style);
353 text_part = style->get_part("text");
358 check_cursor_blink();
361 void Entry::move_edit_position(Navigation nav, bool select)
366 set_edit_position(edit_pos-1, select);
368 else if(nav==NAV_RIGHT)
370 if(edit_pos<text.size())
371 set_edit_position(edit_pos+1, select);
373 else if(nav==NAV_DOWN)
376 text.offset_to_coords(edit_pos, row, col);
377 set_edit_position(text.coords_to_offset(row+1, col), select);
382 text.offset_to_coords(edit_pos, row, col);
383 set_edit_position((row>0 ? text.coords_to_offset(row-1, col) : 0), select);
387 void Entry::set_edit_position(unsigned ep, bool select)
389 if(select && !selection_active)
390 selection_pos = edit_pos;
391 selection_active = select;
398 void Entry::erase_selection()
400 if(!selection_active)
403 unsigned start = selection_pos;
404 unsigned end = edit_pos;
408 text.erase(start, end-start);
409 set_edit_position(start, false);
412 void Entry::check_cursor_blink()
414 cursor_blink = (state&FOCUS);
415 if((state&FOCUS) && style)
417 const Part *cursor_part = style->get_part("cursor");
418 if(cursor_part && cursor_part->get_graphic(ACTIVE|FOCUS)!=cursor_part->get_graphic(NORMAL|FOCUS))
420 set_animation_interval(Time::sec/2);
428 void Entry::check_view_range()
430 if(!multiline || !text_part)
433 visible_rows = text.get_visible_lines(*text_part, geom, 0);
436 text.offset_to_coords(edit_pos, row, col);
440 else if(row>=first_row+visible_rows)
441 first_row = row+1-visible_rows;
443 unsigned scroll = max(text.get_n_lines(), visible_rows)-visible_rows;
444 slider->set_range(0, scroll);
445 slider->set_value(scroll-first_row);
448 void Entry::slider_value_changed(double value)
450 if(text.get_n_lines()>visible_rows)
451 first_row = text.get_n_lines()-visible_rows-static_cast<unsigned>(value);
455 Entry::Loader::Loader(Entry &e):
456 DataFile::DerivedObjectLoader<Entry, Widget::Loader>(e)
458 add("edit_size", &Entry::edit_width, &Entry::edit_height);
459 add("multiline", &Loader::multiline);
462 void Entry::Loader::multiline(bool m)
464 obj.set_multiline(m);