1 #include <msp/gl/matrix.h>
2 #include <msp/strings/format.h>
17 input_type(INPUT_NONE),
25 Container *p = parent;
31 void Widget::set_position(int x, int y)
33 set_geometry(Geometry(x, y, geom.w, geom.h));
36 void Widget::set_size(unsigned w, unsigned h)
38 set_geometry(Geometry(geom.x, geom.y, w, h));
41 void Widget::autosize()
48 void Widget::autosize(Geometry &ageom) const
53 ageom = Geometry(geom.x, geom.y, 0, 0);
54 const Style::PartSeq &parts = style->get_parts();
55 for(Style::PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i)
57 if(i->get_name().empty())
59 const Geometry &pgeom = i->get_geometry();
60 const Sides &pmargin = i->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(*i, 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 const Style::PartSeq &parts = style->get_parts();
234 for(Style::PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i)
236 if(i->get_name().empty())
237 i->build(geom, state, part_cache);
243 void Widget::rebuild_special(const Part &part)
245 part_cache.insert_special(part);
248 void Widget::render(GL::Renderer &renderer) const
251 throw logic_error(format("Attempt to render a widget with null style (class=\"%s\", style_name=\"%s\")", get_class(), style_name));
253 static const GL::Tag texture_tag("ui_tex");
255 GL::Renderer::Push _push(renderer);
258 map_coords_to_root(x, y);
259 renderer.set_matrix(GL::Matrix::translation(x, y, 0));
260 const PartCache::PartList &parts = part_cache.get_parts();
261 for(PartCache::PartList::const_iterator i=parts.begin(); i!=parts.end(); ++i)
263 if(i->mesh && i->texture)
265 renderer.set_texture(texture_tag, i->texture, &style->get_sampler());
266 i->mesh->draw(renderer);
269 render_special(*i->part, renderer);
273 void Widget::pointer_enter()
278 void Widget::pointer_leave()
283 void Widget::touch_press(int x, int y, unsigned finger)
286 button_press(x, y, 1);
289 void Widget::touch_release(int x, int y, unsigned finger)
292 button_release(x, y, 1);
295 void Widget::touch_motion(int x, int y, unsigned finger)
298 pointer_motion(x, y);
301 void Widget::focus_in()
306 void Widget::focus_out()
312 Widget::Loader::Loader(Widget &w):
313 DataFile::ObjectLoader<Widget>(w)
315 add("position", &Loader::position);
316 add("size", &Loader::size);
317 add("style", &Loader::style);
318 add("visible", &Widget::visible);
321 void Widget::Loader::position(int x, int y)
323 obj.set_position(x, y);
326 void Widget::Loader::size(unsigned w, unsigned h)
331 void Widget::Loader::style(const string &s)