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 void Entry::autosize()
37 const Sides &margin = text_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 geom.w = max(geom.w, 10*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 geom.h = max(geom.h, line_height+line_spacing*2+margin.top+margin.bottom);
49 geom.h = max(geom.h, line_height+margin.top+margin.bottom);
54 if(const Part *slider_part = style->get_part("slider"))
56 Geometry sgeom = slider_part->get_geometry();
57 if(!sgeom.w || !sgeom.h)
61 sgeom.w = slider->get_geometry().w;
63 sgeom.h = slider->get_geometry().h;
66 const Sides &margin = slider_part->get_margin();
67 geom.w = max(geom.w, sgeom.w+margin.left+margin.right);
68 geom.h = max(geom.h, sgeom.h+margin.top+margin.bottom);
79 void Entry::set_text(const string &t)
82 edit_pos = text.size();
90 void Entry::set_multiline(bool m)
100 slider->signal_value_changed.connect(sigc::mem_fun(this, &Entry::slider_value_changed));
107 void Entry::rebuild_special(const Part &part, CachedPart &cache)
109 if(part.get_name()=="text")
110 text.build(part, geom, first_row, cache);
111 else if(part.get_name()=="cursor")
113 if(!text_part || !part.get_graphic(state))
120 text.offset_to_coords(edit_pos, row, col);
122 if(row<first_row || row>=first_row+visible_rows)
128 Geometry rgeom = text.coords_to_geometry(*text_part, geom, first_row, row, col);
130 cache.texture = part.get_graphic(state)->get_texture();
133 GL::MeshBuilder bld(*cache.mesh);
134 bld.matrix() *= GL::Matrix::translation(rgeom.x, rgeom.y, 0);
135 part.get_graphic(state)->build(part.get_geometry().w, part.get_geometry().h, bld);
139 void Entry::render_special(const Part &part, GL::Renderer &renderer) const
141 if(part.get_name()=="slider" && multiline)
142 slider->render(renderer);
145 void Entry::key_press(unsigned key, unsigned)
147 if(key==Input::KEY_LEFT)
150 set_edit_position(edit_pos-1);
152 else if(key==Input::KEY_RIGHT)
154 if(edit_pos<text.size())
155 set_edit_position(edit_pos+1);
157 else if(key==Input::KEY_DOWN && multiline)
160 text.offset_to_coords(edit_pos, row, col);
161 set_edit_position(text.coords_to_offset(row+1, col));
163 else if(key==Input::KEY_UP && multiline)
166 text.offset_to_coords(edit_pos, row, col);
167 set_edit_position(row>0 ? text.coords_to_offset(row-1, col) : 0);
169 else if(key==Input::KEY_BACKSPACE)
173 text.erase(--edit_pos, 1);
178 else if(key==Input::KEY_ENTER)
182 text.insert(edit_pos++, "\n");
191 void Entry::character(wchar_t ch)
195 text.insert(edit_pos, StringCodec::encode<StringCodec::Utf8>(StringCodec::ustring(1, ch)));
201 void Entry::on_geometry_change()
209 void Entry::on_style_change()
211 text.set_style(style);
219 text_part = style->get_part("text");
227 void Entry::set_edit_position(unsigned ep)
234 void Entry::reposition_slider()
236 if(!style || !slider)
239 if(const Part *slider_part = style->get_part("slider"))
241 Geometry sgeom = slider_part->get_geometry();
242 if(!sgeom.w || !sgeom.h)
246 sgeom.w = slider->get_geometry().w;
248 sgeom.h = slider->get_geometry().h;
251 slider_part->get_alignment().apply(sgeom, geom, slider_part->get_margin());
252 slider->set_geometry(sgeom);
256 void Entry::check_view_range()
258 if(!multiline || !text_part)
261 float font_size = style->get_font_size();
262 unsigned line_spacing = static_cast<unsigned>(font_size*6/5);
264 const Sides &margin = text_part->get_margin();
265 visible_rows = max((geom.h-margin.top-margin.bottom)/line_spacing, 1U);
268 text.offset_to_coords(edit_pos, row, col);
272 else if(row>=first_row+visible_rows)
273 first_row = row+1-visible_rows;
275 unsigned scroll = max(text.get_n_lines(), visible_rows)-visible_rows;
276 slider->set_range(0, scroll);
277 slider->set_value(scroll-first_row);
280 void Entry::slider_value_changed(double value)
282 if(text.get_n_lines()>visible_rows)
283 first_row = text.get_n_lines()-visible_rows-static_cast<unsigned>(value);
287 Entry::Loader::Loader(Entry &ent):