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)
99 bool Entry::get_selection(unsigned &start, unsigned &end) const
101 if(!selection_active)
104 start = selection_pos;
112 void Entry::set_edit_size(unsigned w, unsigned h)
116 signal_autosize_changed.emit();
119 void Entry::set_multiline(bool m)
126 slider = new VSlider;
129 slider->signal_value_changed.connect(sigc::mem_fun(this, &Entry::slider_value_changed));
136 void Entry::rebuild_special(const Part &part)
138 if(part.get_name()=="text")
139 text.build(part, state, geom, first_row, part_cache);
140 else if(part.get_name()=="cursor")
142 State cursor_state = (cursor_blink ? ACTIVE : NORMAL);
143 const Graphic *graphic = part.get_graphic(state|cursor_state);
144 if(!text_part || !graphic || !graphic->get_texture())
148 text.offset_to_coords(edit_pos, row, col);
150 if(row<first_row || row>=first_row+visible_rows)
153 Geometry rgeom = text.coords_to_geometry(*text_part, geom, first_row, row, col);
155 GL::MeshBuilder bld(part_cache.create_mesh(part, *graphic->get_texture()));
156 bld.matrix() *= GL::Matrix::translation(rgeom.x, rgeom.y, 0);
157 graphic->build(part.get_geometry().w, part.get_geometry().h, bld);
159 else if(part.get_name()=="selection")
161 if(!selection_active)
164 const Graphic *graphic = part.get_graphic(state);
165 if(!text_part || !graphic || !graphic->get_texture())
169 get_selection(start, end);
172 text.offset_to_coords(start, row, col);
173 unsigned end_row, end_col;
174 text.offset_to_coords(end, end_row, end_col);
176 if(end_row<first_row || row>=first_row+visible_rows)
185 if(end_row>=first_row+visible_rows)
187 end_row = first_row+visible_rows-1;
188 end_col = text.get_line_length(end_row);
193 unsigned ec = (row==end_row ? end_col : text.get_line_length(row));
196 Geometry rgeom = text.coords_to_geometry(*text_part, geom, first_row, row, col);
197 Geometry egeom = text.coords_to_geometry(*text_part, geom, first_row, row, ec);
199 GL::MeshBuilder bld(part_cache.create_mesh(part, *graphic->get_texture()));
200 bld.matrix() *= GL::Matrix::translation(rgeom.x, rgeom.y, 0);
201 graphic->build(egeom.x-rgeom.x, part.get_geometry().h, bld);
208 else if(part.get_name()=="slider")
212 reposition_child(*slider, part);
213 Widget::rebuild_special(part);
217 Widget::rebuild_special(part);
220 void Entry::render_special(const Part &part, GL::Renderer &renderer) const
222 if(part.get_name()=="slider" && multiline)
223 slider->render(renderer);
226 void Entry::touch_press(int x, int y, unsigned finger)
230 Widget::touch_press(x, y, finger);
233 bool Entry::key_press(unsigned key, unsigned mod)
235 got_key_press = true;
236 if(key==Input::KEY_BACKSPACE)
241 erase(edit_pos-1, 1);
243 else if(key==Input::KEY_DELETE)
250 else if(key==Input::KEY_ENTER && multiline)
251 insert(edit_pos, "\n");
252 else if(key==Input::KEY_END)
255 text.offset_to_coords(edit_pos, row, col);
256 set_edit_position(text.coords_to_offset(row, text.get_line_length(row)), mod==MOD_SHIFT);
258 else if(key==Input::KEY_HOME)
261 text.offset_to_coords(edit_pos, row, col);
262 set_edit_position(text.coords_to_offset(row, 0), mod==MOD_SHIFT);
264 else if(key==Input::KEY_LEFT && mod==MOD_SHIFT)
265 move_edit_position(NAV_LEFT, true);
266 else if(key==Input::KEY_RIGHT && mod==MOD_SHIFT)
267 move_edit_position(NAV_RIGHT, true);
268 else if(key==Input::KEY_UP && mod==MOD_SHIFT && multiline)
269 move_edit_position(NAV_UP, true);
270 else if(key==Input::KEY_DOWN && mod==MOD_SHIFT && multiline)
271 move_edit_position(NAV_DOWN, true);
278 bool Entry::character(wchar_t ch)
280 if(got_key_press && ch>=' ' && ch!=0x7F)
284 insert(edit_pos, StringCodec::encode<StringCodec::Utf8>(StringCodec::ustring(1, ch)));
291 void Entry::focus_in()
295 check_cursor_blink();
298 void Entry::focus_out()
301 got_key_press = false;
302 check_cursor_blink();
305 bool Entry::navigate(Navigation nav)
307 if(nav==NAV_LEFT || nav==NAV_RIGHT || ((nav==NAV_DOWN || nav==NAV_UP) && multiline))
308 move_edit_position(nav, false);
309 else if(nav==NAV_ACCEPT && !signal_enter.empty())
317 void Entry::animate(const Time::TimeDelta &)
319 cursor_blink = !cursor_blink;
323 void Entry::on_geometry_change()
329 void Entry::on_style_change()
331 text.set_style(style);
339 text_part = style->get_part("text");
344 check_cursor_blink();
347 void Entry::move_edit_position(Navigation nav, bool select)
352 set_edit_position(edit_pos-1, select);
354 else if(nav==NAV_RIGHT)
356 if(edit_pos<text.size())
357 set_edit_position(edit_pos+1, select);
359 else if(nav==NAV_DOWN)
362 text.offset_to_coords(edit_pos, row, col);
363 set_edit_position(text.coords_to_offset(row+1, col), select);
368 text.offset_to_coords(edit_pos, row, col);
369 set_edit_position((row>0 ? text.coords_to_offset(row-1, col) : 0), select);
373 void Entry::set_edit_position(unsigned ep, bool select)
375 if(select && !selection_active)
376 selection_pos = edit_pos;
377 selection_active = select;
379 edit_pos = min(ep, text.size());
385 void Entry::erase_selection()
388 if(!get_selection(start, end))
391 text.erase(start, end-start);
392 set_edit_position(start, false);
395 void Entry::check_cursor_blink()
397 cursor_blink = (state&FOCUS);
398 if((state&FOCUS) && style)
400 const Part *cursor_part = style->get_part("cursor");
401 if(cursor_part && cursor_part->get_graphic(ACTIVE|FOCUS)!=cursor_part->get_graphic(NORMAL|FOCUS))
403 set_animation_interval(Time::sec/2);
411 void Entry::check_view_range()
413 if(!multiline || !text_part)
416 visible_rows = text.get_visible_lines(*text_part, geom, 0);
419 text.offset_to_coords(edit_pos, row, col);
423 else if(row>=first_row+visible_rows)
424 first_row = row+1-visible_rows;
426 unsigned scroll = max(text.get_n_lines(), visible_rows)-visible_rows;
427 slider->set_range(0, scroll);
428 slider->set_value(scroll-first_row);
431 void Entry::slider_value_changed(double value)
433 if(text.get_n_lines()>visible_rows)
434 first_row = text.get_n_lines()-visible_rows-static_cast<unsigned>(value);
438 Entry::Loader::Loader(Entry &e):
439 DataFile::DerivedObjectLoader<Entry, Widget::Loader>(e)
441 add("edit_size", &Entry::edit_width, &Entry::edit_height);
442 add("multiline", &Loader::multiline);
445 void Entry::Loader::multiline(bool m)
447 obj.set_multiline(m);