1 #include <msp/gl/matrix.h>
2 #include <msp/strings/format.h>
17 input_type(INPUT_NONE),
26 Container *p = parent;
32 void Widget::set_position(int x, int y)
34 set_geometry(Geometry(x, y, geom.w, geom.h));
37 void Widget::set_size(unsigned w, unsigned h)
39 set_geometry(Geometry(geom.x, geom.y, w, h));
42 void Widget::autosize()
49 void Widget::autosize(Geometry &ageom) const
54 ageom = Geometry(geom.x, geom.y, 0, 0);
55 for(const Part &p: style->get_parts())
57 if(p.get_name().empty())
59 const Geometry &pgeom = p.get_geometry();
60 const Sides &pmargin = p.get_margin();
61 ageom.w = max(ageom.w, pgeom.w+pmargin.left+pmargin.right);
62 ageom.h = max(ageom.h, pgeom.h+pmargin.top+pmargin.bottom);
65 autosize_special(p, ageom);
69 void Widget::set_geometry(const Geometry &g)
71 bool size_changed = (g.w!=geom.w || g.h!=geom.h);
80 void Widget::map_coords_to_ancestor(int &x, int &y, const Widget &ancestor) const
82 for(const Widget *w=this; w; w=w->get_parent())
87 const Geometry &wgeom = w->get_geometry();
92 throw hierarchy_error("not an ancestor");
95 void Widget::map_coords_to_root(int &x, int &y) const
97 for(const Widget *w=this; w; w=w->get_parent())
99 const Geometry &wgeom = w->get_geometry();
105 void Widget::set_parent(Container *p)
108 throw hierarchy_error("widget already parented");
121 // The container has not yet added the widget as its child
127 void Widget::set_style(const string &s)
133 void Widget::update_style()
136 for(top=this; top->parent; top=top->parent) ;
137 Root *root = dynamic_cast<Root *>(top);
142 string sname = get_class();
143 if(!style_name.empty())
149 style = &root->get_resources().get<Style>(sname);
153 signal_autosize_changed.emit();
157 void Widget::set_tooltip(const string &t)
162 void Widget::set_visible(bool v)
169 signal_visibility_changed.emit(visible);
172 void Widget::set_focus()
175 throw hierarchy_error("no parent");
177 throw logic_error("!visible");
179 signal_request_focus.emit();
182 void Widget::set_enabled(bool e)
184 set_state(DISABLED, (e ? NORMAL : DISABLED));
187 void Widget::set_state(State mask, State bits)
189 State old_state = state;
190 state = (state&~mask)|bits;
191 if(style && style->compare_states(old_state, state))
195 void Widget::set_animation_interval(const Time::TimeDelta &iv)
198 throw invalid_argument("Widget::set_animation_interval");
201 signal_request_animation.emit(anim_interval);
204 void Widget::stop_animation()
206 set_animation_interval(Time::zero);
209 void Widget::mark_rebuild()
214 rebuild_needed = true;
215 signal_rebuild_needed.emit();
218 void Widget::rebuild_hierarchy()
222 rebuild_needed = false;
227 void Widget::rebuild()
232 PartCache::Rebuild rebuild_cache(part_cache);
233 for(const Part &p: style->get_parts())
235 if(p.get_name().empty())
236 p.build(geom, state, part_cache);
242 void Widget::rebuild_special(const Part &part)
244 part_cache.insert_special(part);
247 void Widget::render(GL::Renderer &renderer) const
250 throw logic_error(format("Attempt to render a widget with null style (class=\"%s\", style_name=\"%s\")", get_class(), style_name));
252 static const GL::Tag texture_tag("ui_tex");
254 GL::Renderer::Push _push(renderer);
257 map_coords_to_root(x, y);
258 renderer.set_matrix(GL::Matrix::translation(x, y, 0));
259 for(const CachedPart &p: part_cache.get_parts())
261 if(p.mesh && p.texture)
263 renderer.set_texture(texture_tag, p.texture, &style->get_sampler());
264 p.mesh->draw(renderer);
267 render_special(*p.part, renderer);
271 void Widget::pointer_enter()
276 void Widget::pointer_leave()
281 void Widget::touch_press(int x, int y, unsigned finger)
284 button_press(x, y, 1);
287 void Widget::touch_release(int x, int y, unsigned finger)
290 button_release(x, y, 1);
293 void Widget::touch_motion(int x, int y, unsigned finger)
296 pointer_motion(x, y);
299 void Widget::focus_in()
304 void Widget::focus_out()
310 Widget::Loader::Loader(Widget &w):
311 DataFile::ObjectLoader<Widget>(w)
313 add("position", &Loader::position);
314 add("size", &Loader::size);
315 add("style", &Loader::style);
316 add("visible", &Widget::visible);
319 void Widget::Loader::position(int x, int y)
321 obj.set_position(x, y);
324 void Widget::Loader::size(unsigned w, unsigned h)
329 void Widget::Loader::style(const string &s)