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)
50 autosize_child(*slider, part, ageom);
53 void Entry::set_text(const string &t)
56 edit_pos = text.size();
64 void Entry::set_edit_size(unsigned w, unsigned h)
68 signal_autosize_changed.emit();
71 void Entry::set_multiline(bool m)
81 slider->signal_value_changed.connect(sigc::mem_fun(this, &Entry::slider_value_changed));
88 void Entry::rebuild_special(const Part &part)
90 if(part.get_name()=="text")
91 text.build(part, state, geom, first_row, part_cache);
92 else if(part.get_name()=="cursor")
94 const Graphic *graphic = part.get_graphic(state);
95 if(!text_part || !graphic || !graphic->get_texture())
99 text.offset_to_coords(edit_pos, row, col);
101 if(row<first_row || row>=first_row+visible_rows)
104 Geometry rgeom = text.coords_to_geometry(*text_part, geom, first_row, row, col);
106 GL::MeshBuilder bld(part_cache.create_mesh(part, *graphic->get_texture()));
107 bld.matrix() *= GL::Matrix::translation(rgeom.x, rgeom.y, 0);
108 graphic->build(part.get_geometry().w, part.get_geometry().h, bld);
110 else if(part.get_name()=="slider")
114 reposition_child(*slider, part);
115 Widget::rebuild_special(part);
119 Widget::rebuild_special(part);
122 void Entry::render_special(const Part &part, GL::Renderer &renderer) const
124 if(part.get_name()=="slider" && multiline)
125 slider->render(renderer);
128 void Entry::key_press(unsigned key, unsigned)
130 got_key_press = true;
131 if(key==Input::KEY_LEFT)
134 set_edit_position(edit_pos-1);
136 else if(key==Input::KEY_RIGHT)
138 if(edit_pos<text.size())
139 set_edit_position(edit_pos+1);
141 else if(key==Input::KEY_DOWN && multiline)
144 text.offset_to_coords(edit_pos, row, col);
145 set_edit_position(text.coords_to_offset(row+1, col));
147 else if(key==Input::KEY_UP && multiline)
150 text.offset_to_coords(edit_pos, row, col);
151 set_edit_position(row>0 ? text.coords_to_offset(row-1, col) : 0);
153 else if(key==Input::KEY_BACKSPACE)
157 text.erase(--edit_pos, 1);
162 else if(key==Input::KEY_ENTER)
166 text.insert(edit_pos++, "\n");
175 void Entry::character(wchar_t ch)
177 if(got_key_press && ch>=' ')
179 text.insert(edit_pos, StringCodec::encode<StringCodec::Utf8>(StringCodec::ustring(1, ch)));
185 void Entry::focus_out()
188 got_key_press = false;
191 void Entry::on_geometry_change()
197 void Entry::on_style_change()
199 text.set_style(style);
207 text_part = style->get_part("text");
213 void Entry::set_edit_position(unsigned ep)
220 void Entry::check_view_range()
222 if(!multiline || !text_part)
225 float font_size = style->get_font_size();
226 unsigned line_spacing = static_cast<unsigned>(font_size*6/5);
228 const Sides &margin = text_part->get_margin();
229 visible_rows = max((geom.h-margin.top-margin.bottom)/line_spacing, 1U);
232 text.offset_to_coords(edit_pos, row, col);
236 else if(row>=first_row+visible_rows)
237 first_row = row+1-visible_rows;
239 unsigned scroll = max(text.get_n_lines(), visible_rows)-visible_rows;
240 slider->set_range(0, scroll);
241 slider->set_value(scroll-first_row);
244 void Entry::slider_value_changed(double value)
246 if(text.get_n_lines()>visible_rows)
247 first_row = text.get_n_lines()-visible_rows-static_cast<unsigned>(value);
251 Entry::Loader::Loader(Entry &e):
252 DataFile::DerivedObjectLoader<Entry, Widget::Loader>(e)
254 add("edit_size", &Entry::edit_width, &Entry::edit_height);
255 add("multiline", &Loader::multiline);
258 void Entry::Loader::multiline(bool m)
260 obj.set_multiline(m);