1 #include <msp/gl/matrix.h>
2 #include <msp/strings/format.h>
17 Container *p = parent;
23 void Widget::set_position(int x, int y)
25 set_geometry(Geometry(x, y, geom.w, geom.h));
28 void Widget::set_size(unsigned w, unsigned h)
30 set_geometry(Geometry(geom.x, geom.y, w, h));
33 void Widget::autosize()
40 void Widget::autosize(Geometry &ageom) const
45 ageom = Geometry(geom.x, geom.y, 0, 0);
46 for(const Part &p: style->get_parts())
48 if(p.get_name().empty())
50 const Geometry &pgeom = p.get_geometry();
51 const Sides &pmargin = p.get_margin();
52 ageom.w = max(ageom.w, pgeom.w+pmargin.left+pmargin.right);
53 ageom.h = max(ageom.h, pgeom.h+pmargin.top+pmargin.bottom);
56 autosize_special(p, ageom);
60 void Widget::set_geometry(const Geometry &g)
62 bool size_changed = (g.w!=geom.w || g.h!=geom.h);
71 void Widget::map_coords_to_ancestor(int &x, int &y, const Widget &ancestor) const
73 for(const Widget *w=this; w; w=w->get_parent())
78 const Geometry &wgeom = w->get_geometry();
83 throw hierarchy_error("not an ancestor");
86 void Widget::map_coords_to_root(int &x, int &y) const
88 for(const Widget *w=this; w; w=w->get_parent())
90 const Geometry &wgeom = w->get_geometry();
96 void Widget::set_parent(Container *p)
99 throw hierarchy_error("widget already parented");
112 // The container has not yet added the widget as its child
118 void Widget::set_style(const string &s)
124 void Widget::update_style()
127 for(top=this; top->parent; top=top->parent) ;
128 Root *root = dynamic_cast<Root *>(top);
133 string sname = get_class();
134 if(!style_name.empty())
140 style = &root->get_resources().get<Style>(sname);
144 signal_autosize_changed.emit();
148 void Widget::set_tooltip(const string &t)
153 void Widget::set_visible(bool v)
160 signal_visibility_changed.emit(visible);
163 void Widget::set_focus()
166 throw hierarchy_error("no parent");
168 throw logic_error("!visible");
170 signal_request_focus.emit();
173 void Widget::set_enabled(bool e)
175 set_state(DISABLED, (e ? NORMAL : DISABLED));
178 void Widget::set_state(State mask, State bits)
180 State old_state = state;
181 state = (state&~mask)|bits;
182 if(style && style->compare_states(old_state, state))
186 void Widget::set_animation_interval(const Time::TimeDelta &iv)
189 throw invalid_argument("Widget::set_animation_interval");
192 signal_request_animation.emit(anim_interval);
195 void Widget::stop_animation()
197 set_animation_interval(Time::zero);
200 void Widget::mark_rebuild()
205 rebuild_needed = true;
206 signal_rebuild_needed.emit();
209 void Widget::rebuild_hierarchy()
213 rebuild_needed = false;
218 void Widget::rebuild()
223 PartCache::Rebuild rebuild_cache(part_cache);
224 for(const Part &p: style->get_parts())
226 if(p.get_name().empty())
227 p.build(geom, state, part_cache);
233 void Widget::rebuild_special(const Part &part)
235 part_cache.insert_special(part);
238 void Widget::render(GL::Renderer &renderer) const
241 throw logic_error(format("Attempt to render a widget with null style (class=\"%s\", style_name=\"%s\")", get_class(), style_name));
243 static const GL::Tag texture_tag("ui_tex");
245 GL::Renderer::Push _push(renderer);
248 map_coords_to_root(x, y);
249 renderer.set_matrix(GL::Matrix::translation(x, y, 0));
250 for(const CachedPart &p: part_cache.get_parts())
252 if(p.mesh && p.texture)
254 renderer.set_texture(texture_tag, p.texture, &style->get_sampler());
255 p.mesh->draw(renderer);
258 render_special(*p.part, renderer);
262 void Widget::pointer_enter()
267 void Widget::pointer_leave()
272 void Widget::touch_press(int x, int y, unsigned finger)
275 button_press(x, y, 1);
278 void Widget::touch_release(int x, int y, unsigned finger)
281 button_release(x, y, 1);
284 void Widget::touch_motion(int x, int y, unsigned finger)
287 pointer_motion(x, y);
290 void Widget::focus_in()
295 void Widget::focus_out()
301 Widget::Loader::Loader(Widget &w):
302 DataFile::ObjectLoader<Widget>(w)
304 add("position", &Loader::position);
305 add("size", &Loader::size);
306 add("style", &Loader::style);
307 add("visible", &Widget::visible);
310 void Widget::Loader::position(int x, int y)
312 obj.set_position(x, y);
315 void Widget::Loader::size(unsigned w, unsigned h)
320 void Widget::Loader::style(const string &s)