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):
31 void Entry::autosize_special(const Part &part, Geometry &ageom) const
33 if(part.get_name()=="text")
35 const Sides &margin = part.get_margin();
36 const GL::Font &font = style->get_font();
37 unsigned en_width = static_cast<unsigned>(font.get_string_width("n")*style->get_font_size());
38 ageom.w = max(ageom.w, edit_width*en_width+margin.left+margin.right);
40 unsigned line_height = static_cast<unsigned>((font.get_ascent()-font.get_descent())*style->get_font_size());
43 unsigned line_spacing = style->get_font_size()*6/5;
44 ageom.h = max(ageom.h, line_height+line_spacing*(edit_height-1)+margin.top+margin.bottom);
47 ageom.h = max(ageom.h, line_height+margin.top+margin.bottom);
49 else if(part.get_name()=="slider" && multiline)
51 Geometry sgeom = part.get_geometry();
52 if(!sgeom.w || !sgeom.h)
55 slider->autosize(wgeom);
62 const Sides &margin = part.get_margin();
63 ageom.w = max(ageom.w, sgeom.w+margin.left+margin.right);
64 ageom.h = max(ageom.h, sgeom.h+margin.top+margin.bottom);
68 void Entry::set_text(const string &t)
71 edit_pos = text.size();
79 void Entry::set_edit_size(unsigned w, unsigned h)
83 signal_autosize_changed.emit();
86 void Entry::set_multiline(bool m)
96 slider->signal_value_changed.connect(sigc::mem_fun(this, &Entry::slider_value_changed));
103 void Entry::rebuild_special(const Part &part)
105 if(part.get_name()=="text")
106 text.build(part, state, geom, first_row, part_cache);
107 else if(part.get_name()=="cursor")
109 const Graphic *graphic = part.get_graphic(state);
110 if(!text_part || !graphic || !graphic->get_texture())
114 text.offset_to_coords(edit_pos, row, col);
116 if(row<first_row || row>=first_row+visible_rows)
119 Geometry rgeom = text.coords_to_geometry(*text_part, geom, first_row, row, col);
121 GL::MeshBuilder bld(part_cache.create_mesh(part, *graphic->get_texture()));
122 bld.matrix() *= GL::Matrix::translation(rgeom.x, rgeom.y, 0);
123 graphic->build(part.get_geometry().w, part.get_geometry().h, bld);
126 Widget::rebuild_special(part);
129 void Entry::render_special(const Part &part, GL::Renderer &renderer) const
131 if(part.get_name()=="slider" && multiline)
132 slider->render(renderer);
135 void Entry::key_press(unsigned key, unsigned)
137 got_key_press = true;
138 if(key==Input::KEY_LEFT)
141 set_edit_position(edit_pos-1);
143 else if(key==Input::KEY_RIGHT)
145 if(edit_pos<text.size())
146 set_edit_position(edit_pos+1);
148 else if(key==Input::KEY_DOWN && multiline)
151 text.offset_to_coords(edit_pos, row, col);
152 set_edit_position(text.coords_to_offset(row+1, col));
154 else if(key==Input::KEY_UP && multiline)
157 text.offset_to_coords(edit_pos, row, col);
158 set_edit_position(row>0 ? text.coords_to_offset(row-1, col) : 0);
160 else if(key==Input::KEY_BACKSPACE)
164 text.erase(--edit_pos, 1);
169 else if(key==Input::KEY_ENTER)
173 text.insert(edit_pos++, "\n");
182 void Entry::character(wchar_t ch)
184 if(got_key_press && ch>=' ')
186 text.insert(edit_pos, StringCodec::encode<StringCodec::Utf8>(StringCodec::ustring(1, ch)));
192 void Entry::focus_out()
195 got_key_press = false;
198 void Entry::on_geometry_change()
206 void Entry::on_style_change()
208 text.set_style(style);
216 text_part = style->get_part("text");
224 void Entry::set_edit_position(unsigned ep)
231 void Entry::reposition_slider()
233 if(!style || !slider)
236 if(const Part *slider_part = style->get_part("slider"))
238 Geometry sgeom = slider_part->get_geometry();
239 if(!sgeom.w || !sgeom.h)
242 slider->autosize(wgeom);
249 slider_part->get_alignment().apply(sgeom, geom, slider_part->get_margin());
250 slider->set_geometry(sgeom);
254 void Entry::check_view_range()
256 if(!multiline || !text_part)
259 float font_size = style->get_font_size();
260 unsigned line_spacing = static_cast<unsigned>(font_size*6/5);
262 const Sides &margin = text_part->get_margin();
263 visible_rows = max((geom.h-margin.top-margin.bottom)/line_spacing, 1U);
266 text.offset_to_coords(edit_pos, row, col);
270 else if(row>=first_row+visible_rows)
271 first_row = row+1-visible_rows;
273 unsigned scroll = max(text.get_n_lines(), visible_rows)-visible_rows;
274 slider->set_range(0, scroll);
275 slider->set_value(scroll-first_row);
278 void Entry::slider_value_changed(double value)
280 if(text.get_n_lines()>visible_rows)
281 first_row = text.get_n_lines()-visible_rows-static_cast<unsigned>(value);
285 Entry::Loader::Loader(Entry &e):
286 DataFile::DerivedObjectLoader<Entry, Widget::Loader>(e)
288 add("edit_size", &Entry::edit_width, &Entry::edit_height);
289 add("multiline", &Loader::multiline);
292 void Entry::Loader::multiline(bool m)
294 obj.set_multiline(m);