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 const Style::PartSeq &parts = style->get_parts();
56 for(Style::PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i)
58 if(i->get_name().empty())
60 const Geometry &pgeom = i->get_geometry();
61 const Sides &pmargin = i->get_margin();
62 ageom.w = max(ageom.w, pgeom.w+pmargin.left+pmargin.right);
63 ageom.h = max(ageom.h, pgeom.h+pmargin.top+pmargin.bottom);
66 autosize_special(*i, ageom);
70 void Widget::set_geometry(const Geometry &g)
72 bool size_changed = (g.w!=geom.w || g.h!=geom.h);
81 void Widget::map_coords_to_ancestor(int &x, int &y, const Widget &ancestor) const
83 for(const Widget *w=this; w; w=w->get_parent())
88 const Geometry &wgeom = w->get_geometry();
93 throw hierarchy_error("not an ancestor");
96 void Widget::map_coords_to_root(int &x, int &y) const
98 for(const Widget *w=this; w; w=w->get_parent())
100 const Geometry &wgeom = w->get_geometry();
106 void Widget::set_parent(Container *p)
109 throw hierarchy_error("widget already parented");
122 // The container has not yet added the widget as its child
128 void Widget::set_style(const string &s)
134 void Widget::update_style()
137 for(top=this; top->parent; top=top->parent) ;
138 Root *root = dynamic_cast<Root *>(top);
143 string sname = get_class();
144 if(!style_name.empty())
150 style = &root->get_resources().get<Style>(sname);
154 signal_autosize_changed.emit();
158 void Widget::set_tooltip(const string &t)
163 void Widget::set_visible(bool v)
170 signal_visibility_changed.emit(visible);
173 void Widget::set_focus()
176 throw hierarchy_error("no parent");
178 throw logic_error("!visible");
180 signal_request_focus.emit();
183 void Widget::set_enabled(bool e)
185 set_state(DISABLED, (e ? NORMAL : DISABLED));
188 void Widget::set_state(State mask, State bits)
190 State old_state = state;
191 state = (state&~mask)|bits;
192 if(style && style->compare_states(old_state, state))
196 void Widget::set_animation_interval(const Time::TimeDelta &iv)
199 throw invalid_argument("Widget::set_animation_interval");
202 signal_request_animation.emit(anim_interval);
205 void Widget::stop_animation()
207 set_animation_interval(Time::zero);
210 void Widget::mark_rebuild()
215 rebuild_needed = true;
216 signal_rebuild_needed.emit();
219 void Widget::rebuild_hierarchy()
223 rebuild_needed = false;
228 void Widget::rebuild()
233 PartCache::Rebuild rebuild_cache(part_cache);
234 const Style::PartSeq &parts = style->get_parts();
235 for(Style::PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i)
237 if(i->get_name().empty())
238 i->build(geom, state, part_cache);
244 void Widget::rebuild_special(const Part &part)
246 part_cache.insert_special(part);
249 void Widget::render(GL::Renderer &renderer) const
252 throw logic_error(format("Attempt to render a widget with null style (class=\"%s\", style_name=\"%s\")", get_class(), style_name));
254 static const GL::Tag texture_tag("ui_tex");
256 GL::Renderer::Push _push(renderer);
259 map_coords_to_root(x, y);
260 renderer.set_matrix(GL::Matrix::translation(x, y, 0));
261 const PartCache::PartList &parts = part_cache.get_parts();
262 for(PartCache::PartList::const_iterator i=parts.begin(); i!=parts.end(); ++i)
264 if(i->mesh && i->texture)
266 renderer.set_texture(texture_tag, i->texture, &style->get_sampler());
267 i->mesh->draw(renderer);
270 render_special(*i->part, renderer);
274 void Widget::pointer_enter()
279 void Widget::pointer_leave()
284 void Widget::touch_press(int x, int y, unsigned finger)
287 button_press(x, y, 1);
290 void Widget::touch_release(int x, int y, unsigned finger)
293 button_release(x, y, 1);
296 void Widget::touch_motion(int x, int y, unsigned finger)
299 pointer_motion(x, y);
302 void Widget::focus_in()
307 void Widget::focus_out()
313 Widget::Loader::Loader(Widget &w):
314 DataFile::ObjectLoader<Widget>(w)
316 add("position", &Loader::position);
317 add("size", &Loader::size);
318 add("style", &Loader::style);
319 add("visible", &Widget::visible);
322 void Widget::Loader::position(int x, int y)
324 obj.set_position(x, y);
327 void Widget::Loader::size(unsigned w, unsigned h)
332 void Widget::Loader::style(const string &s)