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 input_type = INPUT_TEXT;
32 void Entry::autosize_special(const Part &part, Geometry &ageom) const
34 if(part.get_name()=="text")
36 const Sides &margin = part.get_margin();
37 const GL::Font &font = style->get_font();
38 unsigned en_width = static_cast<unsigned>(font.get_string_width("n")*style->get_font_size());
39 ageom.w = max(ageom.w, edit_width*en_width+margin.left+margin.right);
41 unsigned line_height = static_cast<unsigned>((font.get_ascent()-font.get_descent())*style->get_font_size());
44 unsigned line_spacing = style->get_font_size()*6/5;
45 ageom.h = max(ageom.h, line_height+line_spacing*(edit_height-1)+margin.top+margin.bottom);
48 ageom.h = max(ageom.h, line_height+margin.top+margin.bottom);
50 else if(part.get_name()=="slider" && multiline)
51 autosize_child(*slider, part, ageom);
54 void Entry::set_text(const string &t)
57 edit_pos = text.size();
65 void Entry::set_edit_size(unsigned w, unsigned h)
69 signal_autosize_changed.emit();
72 void Entry::set_multiline(bool m)
82 slider->signal_value_changed.connect(sigc::mem_fun(this, &Entry::slider_value_changed));
89 void Entry::rebuild_special(const Part &part)
91 if(part.get_name()=="text")
92 text.build(part, state, geom, first_row, part_cache);
93 else if(part.get_name()=="cursor")
95 const Graphic *graphic = part.get_graphic(state);
96 if(!text_part || !graphic || !graphic->get_texture())
100 text.offset_to_coords(edit_pos, row, col);
102 if(row<first_row || row>=first_row+visible_rows)
105 Geometry rgeom = text.coords_to_geometry(*text_part, geom, first_row, row, col);
107 GL::MeshBuilder bld(part_cache.create_mesh(part, *graphic->get_texture()));
108 bld.matrix() *= GL::Matrix::translation(rgeom.x, rgeom.y, 0);
109 graphic->build(part.get_geometry().w, part.get_geometry().h, bld);
111 else if(part.get_name()=="slider")
115 reposition_child(*slider, part);
116 Widget::rebuild_special(part);
120 Widget::rebuild_special(part);
123 void Entry::render_special(const Part &part, GL::Renderer &renderer) const
125 if(part.get_name()=="slider" && multiline)
126 slider->render(renderer);
129 bool Entry::key_press(unsigned key, unsigned)
131 got_key_press = true;
132 if(key==Input::KEY_LEFT)
135 set_edit_position(edit_pos-1);
137 else if(key==Input::KEY_RIGHT)
139 if(edit_pos<text.size())
140 set_edit_position(edit_pos+1);
142 else if(key==Input::KEY_DOWN && multiline)
145 text.offset_to_coords(edit_pos, row, col);
146 set_edit_position(text.coords_to_offset(row+1, col));
148 else if(key==Input::KEY_UP && multiline)
151 text.offset_to_coords(edit_pos, row, col);
152 set_edit_position(row>0 ? text.coords_to_offset(row-1, col) : 0);
154 else if(key==Input::KEY_BACKSPACE)
158 text.erase(--edit_pos, 1);
163 else if(key==Input::KEY_ENTER)
167 text.insert(edit_pos++, "\n");
180 bool Entry::character(wchar_t ch)
182 if(got_key_press && ch>=' ')
184 text.insert(edit_pos, StringCodec::encode<StringCodec::Utf8>(StringCodec::ustring(1, ch)));
193 void Entry::focus_out()
196 got_key_press = false;
199 void Entry::on_geometry_change()
205 void Entry::on_style_change()
207 text.set_style(style);
215 text_part = style->get_part("text");
221 void Entry::set_edit_position(unsigned ep)
228 void Entry::check_view_range()
230 if(!multiline || !text_part)
233 float font_size = style->get_font_size();
234 unsigned line_spacing = static_cast<unsigned>(font_size*6/5);
236 const Sides &margin = text_part->get_margin();
237 visible_rows = max((geom.h-margin.top-margin.bottom)/line_spacing, 1U);
240 text.offset_to_coords(edit_pos, row, col);
244 else if(row>=first_row+visible_rows)
245 first_row = row+1-visible_rows;
247 unsigned scroll = max(text.get_n_lines(), visible_rows)-visible_rows;
248 slider->set_range(0, scroll);
249 slider->set_value(scroll-first_row);
252 void Entry::slider_value_changed(double value)
254 if(text.get_n_lines()>visible_rows)
255 first_row = text.get_n_lines()-visible_rows-static_cast<unsigned>(value);
259 Entry::Loader::Loader(Entry &e):
260 DataFile::DerivedObjectLoader<Entry, Widget::Loader>(e)
262 add("edit_size", &Entry::edit_width, &Entry::edit_height);
263 add("multiline", &Loader::multiline);
266 void Entry::Loader::multiline(bool m)
268 obj.set_multiline(m);