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):
29 input_type = INPUT_TEXT;
33 void Entry::autosize_special(const Part &part, Geometry &ageom) const
35 if(part.get_name()=="text")
37 const Sides &margin = part.get_margin();
38 const GL::Font &font = style->get_font();
39 unsigned en_width = static_cast<unsigned>(font.get_string_width("n")*style->get_font_size());
40 ageom.w = max(ageom.w, edit_width*en_width+margin.left+margin.right);
42 unsigned line_height = static_cast<unsigned>((font.get_ascent()-font.get_descent())*style->get_font_size());
45 unsigned line_spacing = style->get_font_size()*6/5;
46 ageom.h = max(ageom.h, line_height+line_spacing*(edit_height-1)+margin.top+margin.bottom);
49 ageom.h = max(ageom.h, line_height+margin.top+margin.bottom);
51 else if(part.get_name()=="slider" && multiline)
52 autosize_child(*slider, part, ageom);
55 void Entry::set_text(const string &t)
58 edit_pos = text.size();
66 void Entry::set_edit_size(unsigned w, unsigned h)
70 signal_autosize_changed.emit();
73 void Entry::set_multiline(bool m)
83 slider->signal_value_changed.connect(sigc::mem_fun(this, &Entry::slider_value_changed));
90 void Entry::rebuild_special(const Part &part)
92 if(part.get_name()=="text")
93 text.build(part, state, geom, first_row, part_cache);
94 else if(part.get_name()=="cursor")
96 State cursor_state = (cursor_blink ? ACTIVE : NORMAL);
97 const Graphic *graphic = part.get_graphic(state|cursor_state);
98 if(!text_part || !graphic || !graphic->get_texture())
102 text.offset_to_coords(edit_pos, row, col);
104 if(row<first_row || row>=first_row+visible_rows)
107 Geometry rgeom = text.coords_to_geometry(*text_part, geom, first_row, row, col);
109 GL::MeshBuilder bld(part_cache.create_mesh(part, *graphic->get_texture()));
110 bld.matrix() *= GL::Matrix::translation(rgeom.x, rgeom.y, 0);
111 graphic->build(part.get_geometry().w, part.get_geometry().h, bld);
113 else if(part.get_name()=="slider")
117 reposition_child(*slider, part);
118 Widget::rebuild_special(part);
122 Widget::rebuild_special(part);
125 void Entry::render_special(const Part &part, GL::Renderer &renderer) const
127 if(part.get_name()=="slider" && multiline)
128 slider->render(renderer);
131 void Entry::touch_press(int x, int y, unsigned finger)
135 Widget::touch_press(x, y, finger);
138 bool Entry::key_press(unsigned key, unsigned)
140 got_key_press = true;
141 if(key==Input::KEY_BACKSPACE)
145 text.erase(--edit_pos, 1);
150 else if(key==Input::KEY_ENTER && multiline)
152 text.insert(edit_pos++, "\n");
156 else if(key==Input::KEY_END)
159 text.offset_to_coords(edit_pos, row, col);
160 set_edit_position(text.coords_to_offset(row, text.get_line_length(row)));
162 else if(key==Input::KEY_HOME)
165 text.offset_to_coords(edit_pos, row, col);
166 set_edit_position(text.coords_to_offset(row, 0));
174 bool Entry::character(wchar_t ch)
176 if(got_key_press && ch>=' ')
178 text.insert(edit_pos, StringCodec::encode<StringCodec::Utf8>(StringCodec::ustring(1, ch)));
187 void Entry::focus_in()
191 check_cursor_blink();
194 void Entry::focus_out()
197 got_key_press = false;
198 check_cursor_blink();
201 bool Entry::navigate(Navigation nav)
206 set_edit_position(edit_pos-1);
208 else if(nav==NAV_RIGHT)
210 if(edit_pos<text.size())
211 set_edit_position(edit_pos+1);
213 else if(nav==NAV_DOWN && multiline)
216 text.offset_to_coords(edit_pos, row, col);
217 set_edit_position(text.coords_to_offset(row+1, col));
219 else if(nav==NAV_UP && multiline)
222 text.offset_to_coords(edit_pos, row, col);
223 set_edit_position(row>0 ? text.coords_to_offset(row-1, col) : 0);
225 else if(nav==NAV_ACCEPT && !signal_enter.empty())
233 void Entry::animate(const Time::TimeDelta &)
235 cursor_blink = !cursor_blink;
239 void Entry::on_geometry_change()
245 void Entry::on_style_change()
247 text.set_style(style);
255 text_part = style->get_part("text");
260 check_cursor_blink();
263 void Entry::set_edit_position(unsigned ep)
270 void Entry::check_cursor_blink()
272 cursor_blink = (state&FOCUS);
273 if((state&FOCUS) && style)
275 const Part *cursor_part = style->get_part("cursor");
276 if(cursor_part && cursor_part->get_graphic(ACTIVE|FOCUS)!=cursor_part->get_graphic(NORMAL|FOCUS))
278 set_animation_interval(Time::sec/2);
286 void Entry::check_view_range()
288 if(!multiline || !text_part)
291 float font_size = style->get_font_size();
292 unsigned line_spacing = static_cast<unsigned>(font_size*6/5);
294 const Sides &margin = text_part->get_margin();
295 visible_rows = max((geom.h-margin.top-margin.bottom)/line_spacing, 1U);
298 text.offset_to_coords(edit_pos, row, col);
302 else if(row>=first_row+visible_rows)
303 first_row = row+1-visible_rows;
305 unsigned scroll = max(text.get_n_lines(), visible_rows)-visible_rows;
306 slider->set_range(0, scroll);
307 slider->set_value(scroll-first_row);
310 void Entry::slider_value_changed(double value)
312 if(text.get_n_lines()>visible_rows)
313 first_row = text.get_n_lines()-visible_rows-static_cast<unsigned>(value);
317 Entry::Loader::Loader(Entry &e):
318 DataFile::DerivedObjectLoader<Entry, Widget::Loader>(e)
320 add("edit_size", &Entry::edit_width, &Entry::edit_height);
321 add("multiline", &Loader::multiline);
324 void Entry::Loader::multiline(bool m)
326 obj.set_multiline(m);