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");
162 bool Entry::character(wchar_t ch)
164 if(got_key_press && ch>=' ')
166 text.insert(edit_pos, StringCodec::encode<StringCodec::Utf8>(StringCodec::ustring(1, ch)));
175 void Entry::focus_in()
179 check_cursor_blink();
182 void Entry::focus_out()
185 got_key_press = false;
186 check_cursor_blink();
189 bool Entry::navigate(Navigation nav)
194 set_edit_position(edit_pos-1);
196 else if(nav==NAV_RIGHT)
198 if(edit_pos<text.size())
199 set_edit_position(edit_pos+1);
201 else if(nav==NAV_DOWN && multiline)
204 text.offset_to_coords(edit_pos, row, col);
205 set_edit_position(text.coords_to_offset(row+1, col));
207 else if(nav==NAV_UP && multiline)
210 text.offset_to_coords(edit_pos, row, col);
211 set_edit_position(row>0 ? text.coords_to_offset(row-1, col) : 0);
213 else if(nav==NAV_ACCEPT && !signal_enter.empty())
221 void Entry::animate(const Time::TimeDelta &)
223 cursor_blink = !cursor_blink;
227 void Entry::on_geometry_change()
233 void Entry::on_style_change()
235 text.set_style(style);
243 text_part = style->get_part("text");
248 check_cursor_blink();
251 void Entry::set_edit_position(unsigned ep)
258 void Entry::check_cursor_blink()
260 cursor_blink = (state&FOCUS);
261 if((state&FOCUS) && style)
263 const Part *cursor_part = style->get_part("cursor");
264 if(cursor_part && cursor_part->get_graphic(ACTIVE|FOCUS)!=cursor_part->get_graphic(NORMAL|FOCUS))
266 set_animation_interval(Time::sec/2);
274 void Entry::check_view_range()
276 if(!multiline || !text_part)
279 float font_size = style->get_font_size();
280 unsigned line_spacing = static_cast<unsigned>(font_size*6/5);
282 const Sides &margin = text_part->get_margin();
283 visible_rows = max((geom.h-margin.top-margin.bottom)/line_spacing, 1U);
286 text.offset_to_coords(edit_pos, row, col);
290 else if(row>=first_row+visible_rows)
291 first_row = row+1-visible_rows;
293 unsigned scroll = max(text.get_n_lines(), visible_rows)-visible_rows;
294 slider->set_range(0, scroll);
295 slider->set_value(scroll-first_row);
298 void Entry::slider_value_changed(double value)
300 if(text.get_n_lines()>visible_rows)
301 first_row = text.get_n_lines()-visible_rows-static_cast<unsigned>(value);
305 Entry::Loader::Loader(Entry &e):
306 DataFile::DerivedObjectLoader<Entry, Widget::Loader>(e)
308 add("edit_size", &Entry::edit_width, &Entry::edit_height);
309 add("multiline", &Loader::multiline);
312 void Entry::Loader::multiline(bool m)
314 obj.set_multiline(m);