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 void Entry::autosize()
38 const Sides &margin = text_part->get_margin();
39 const GL::Font &font = style->get_font();
40 unsigned en_width = static_cast<unsigned>(font.get_string_width("n")*style->get_font_size());
41 geom.w = max(geom.w, 10*en_width+margin.left+margin.right);
43 unsigned line_height = static_cast<unsigned>((font.get_ascent()-font.get_descent())*style->get_font_size());
46 unsigned line_spacing = style->get_font_size()*6/5;
47 geom.h = max(geom.h, line_height+line_spacing*2+margin.top+margin.bottom);
50 geom.h = max(geom.h, line_height+margin.top+margin.bottom);
55 if(const Part *slider_part = style->get_part("slider"))
57 Geometry sgeom = slider_part->get_geometry();
58 if(!sgeom.w || !sgeom.h)
62 sgeom.w = slider->get_geometry().w;
64 sgeom.h = slider->get_geometry().h;
67 const Sides &margin = slider_part->get_margin();
68 geom.w = max(geom.w, sgeom.w+margin.left+margin.right);
69 geom.h = max(geom.h, sgeom.h+margin.top+margin.bottom);
80 void Entry::set_text(const string &t)
83 edit_pos = text.size();
91 void Entry::set_multiline(bool m)
101 slider->signal_value_changed.connect(sigc::mem_fun(this, &Entry::slider_value_changed));
108 void Entry::rebuild_special(const Part &part, CachedPart &cache)
110 if(part.get_name()=="text")
111 text.build(part, geom, first_row, cache);
112 else if(part.get_name()=="cursor")
114 const Graphic *graphic = part.get_graphic(state);
115 if(!text_part || !graphic)
122 text.offset_to_coords(edit_pos, row, col);
124 if(row<first_row || row>=first_row+visible_rows)
130 Geometry rgeom = text.coords_to_geometry(*text_part, geom, first_row, row, col);
132 cache.texture = graphic->get_texture();
135 GL::MeshBuilder bld(*cache.mesh);
136 bld.matrix() *= GL::Matrix::translation(rgeom.x, rgeom.y, 0);
137 graphic->build(part.get_geometry().w, part.get_geometry().h, bld);
141 void Entry::render_special(const Part &part, GL::Renderer &renderer) const
143 if(part.get_name()=="slider" && multiline)
144 slider->render(renderer);
147 void Entry::key_press(unsigned key, unsigned)
149 got_key_press = true;
150 if(key==Input::KEY_LEFT)
153 set_edit_position(edit_pos-1);
155 else if(key==Input::KEY_RIGHT)
157 if(edit_pos<text.size())
158 set_edit_position(edit_pos+1);
160 else if(key==Input::KEY_DOWN && multiline)
163 text.offset_to_coords(edit_pos, row, col);
164 set_edit_position(text.coords_to_offset(row+1, col));
166 else if(key==Input::KEY_UP && multiline)
169 text.offset_to_coords(edit_pos, row, col);
170 set_edit_position(row>0 ? text.coords_to_offset(row-1, col) : 0);
172 else if(key==Input::KEY_BACKSPACE)
176 text.erase(--edit_pos, 1);
181 else if(key==Input::KEY_ENTER)
185 text.insert(edit_pos++, "\n");
194 void Entry::character(wchar_t ch)
196 if(got_key_press && ch>=' ')
198 text.insert(edit_pos, StringCodec::encode<StringCodec::Utf8>(StringCodec::ustring(1, ch)));
204 void Entry::focus_out()
207 got_key_press = false;
210 void Entry::on_geometry_change()
218 void Entry::on_style_change()
220 text.set_style(style);
228 text_part = style->get_part("text");
236 void Entry::set_edit_position(unsigned ep)
243 void Entry::reposition_slider()
245 if(!style || !slider)
248 if(const Part *slider_part = style->get_part("slider"))
250 Geometry sgeom = slider_part->get_geometry();
251 if(!sgeom.w || !sgeom.h)
255 sgeom.w = slider->get_geometry().w;
257 sgeom.h = slider->get_geometry().h;
260 slider_part->get_alignment().apply(sgeom, geom, slider_part->get_margin());
261 slider->set_geometry(sgeom);
265 void Entry::check_view_range()
267 if(!multiline || !text_part)
270 float font_size = style->get_font_size();
271 unsigned line_spacing = static_cast<unsigned>(font_size*6/5);
273 const Sides &margin = text_part->get_margin();
274 visible_rows = max((geom.h-margin.top-margin.bottom)/line_spacing, 1U);
277 text.offset_to_coords(edit_pos, row, col);
281 else if(row>=first_row+visible_rows)
282 first_row = row+1-visible_rows;
284 unsigned scroll = max(text.get_n_lines(), visible_rows)-visible_rows;
285 slider->set_range(0, scroll);
286 slider->set_value(scroll-first_row);
289 void Entry::slider_value_changed(double value)
291 if(text.get_n_lines()>visible_rows)
292 first_row = text.get_n_lines()-visible_rows-static_cast<unsigned>(value);
296 Entry::Loader::Loader(Entry &ent):