1 #include <msp/gl/matrix.h>
2 #include <msp/strings/format.h>
3 #include <msp/strings/utils.h>
18 Container *p = parent;
24 void Widget::set_position(int x, int y)
26 set_geometry(Geometry(x, y, geom.w, geom.h));
29 void Widget::set_size(unsigned w, unsigned h)
31 set_geometry(Geometry(geom.x, geom.y, w, h));
34 void Widget::autosize()
41 void Widget::autosize(Geometry &ageom) const
46 ageom = Geometry(geom.x, geom.y, 0, 0);
47 for(const Part &p: style->get_parts())
49 if(p.get_name().empty())
51 const Geometry &pgeom = p.get_geometry();
52 const Sides &pmargin = p.get_margin();
53 ageom.w = max(ageom.w, pgeom.w+pmargin.left+pmargin.right);
54 ageom.h = max(ageom.h, pgeom.h+pmargin.top+pmargin.bottom);
57 autosize_special(p, ageom);
61 void Widget::set_geometry(const Geometry &g)
63 bool size_changed = (g.w!=geom.w || g.h!=geom.h);
72 void Widget::map_coords_to_ancestor(int &x, int &y, const Widget &ancestor) const
74 for(const Widget *w=this; w; w=w->get_parent())
79 const Geometry &wgeom = w->get_geometry();
84 throw hierarchy_error("not an ancestor");
87 void Widget::map_coords_to_root(int &x, int &y) const
89 for(const Widget *w=this; w; w=w->get_parent())
91 const Geometry &wgeom = w->get_geometry();
97 void Widget::set_parent(Container *p)
100 throw hierarchy_error("widget already parented");
113 // The container has not yet added the widget as its child
119 void Widget::set_style(const string &s)
125 void Widget::update_style()
128 for(top=this; top->parent; top=top->parent) ;
129 Root *root = dynamic_cast<Root *>(top);
134 string sname = get_class();
135 append(sname, "-", style_name);
137 style = &root->get_resources().get<Style>(sname);
141 signal_autosize_changed.emit();
145 void Widget::set_tooltip(const string &t)
150 void Widget::set_visible(bool v)
157 signal_visibility_changed.emit(visible);
160 void Widget::set_focus()
163 throw hierarchy_error("no parent");
165 throw logic_error("!visible");
167 signal_request_focus.emit();
170 void Widget::set_enabled(bool e)
172 set_state(DISABLED, (e ? NORMAL : DISABLED));
175 void Widget::set_state(State mask, State bits)
177 State old_state = state;
178 state = (state&~mask)|bits;
179 if(style && style->compare_states(old_state, state))
183 void Widget::set_animation_interval(const Time::TimeDelta &iv)
186 throw invalid_argument("Widget::set_animation_interval");
189 signal_request_animation.emit(anim_interval);
192 void Widget::stop_animation()
194 set_animation_interval(Time::zero);
197 void Widget::mark_rebuild()
202 rebuild_needed = true;
203 signal_rebuild_needed.emit();
206 void Widget::rebuild_hierarchy()
210 rebuild_needed = false;
215 void Widget::rebuild()
220 PartCache::Rebuild rebuild_cache(part_cache);
221 for(const Part &p: style->get_parts())
223 if(p.get_name().empty())
224 p.build(geom, state, part_cache);
230 void Widget::rebuild_special(const Part &part)
232 part_cache.insert_special(part);
235 void Widget::render(GL::Renderer &renderer) const
238 throw logic_error(format("Attempt to render a widget with null style (class=\"%s\", style_name=\"%s\")", get_class(), style_name));
240 static const GL::Tag texture_tag("ui_tex");
242 GL::Renderer::Push _push(renderer);
245 map_coords_to_root(x, y);
246 renderer.set_matrix(GL::Matrix::translation(x, y, 0));
247 for(const CachedPart &p: part_cache.get_parts())
249 if(p.mesh && p.texture)
251 renderer.set_texture(texture_tag, p.texture, &style->get_sampler());
252 p.mesh->draw(renderer);
255 render_special(*p.part, renderer);
259 void Widget::pointer_enter()
264 void Widget::pointer_leave()
269 void Widget::touch_press(int x, int y, unsigned finger)
272 button_press(x, y, 1);
275 void Widget::touch_release(int x, int y, unsigned finger)
278 button_release(x, y, 1);
281 void Widget::touch_motion(int x, int y, unsigned finger)
284 pointer_motion(x, y);
287 void Widget::focus_in()
292 void Widget::focus_out()
298 Widget::Loader::Loader(Widget &w):
299 DataFile::ObjectLoader<Widget>(w)
301 add("position", &Loader::position);
302 add("size", &Loader::size);
303 add("style", &Loader::style);
304 add("visible", &Widget::visible);
307 void Widget::Loader::position(int x, int y)
309 obj.set_position(x, y);
312 void Widget::Loader::size(unsigned w, unsigned h)
317 void Widget::Loader::style(const string &s)