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::set_parent(Container *p)
98 throw hierarchy_error("widget already parented");
111 // The container has not yet added the widget as its child
117 void Widget::set_style(const string &s)
123 void Widget::update_style()
126 for(top=this; top->parent; top=top->parent) ;
127 Root *root = dynamic_cast<Root *>(top);
132 string sname = get_class();
133 if(!style_name.empty())
139 style = &root->get_resources().get<Style>(sname);
143 signal_autosize_changed.emit();
147 void Widget::set_tooltip(const string &t)
152 void Widget::set_visible(bool v)
159 signal_visibility_changed.emit(visible);
162 void Widget::set_focusable(bool f)
164 input_type = (f ? INPUT_TEXT : INPUT_NONE);
167 void Widget::set_focus()
170 throw hierarchy_error("no parent");
172 throw logic_error("!visible");
174 signal_request_focus.emit();
177 void Widget::set_enabled(bool e)
179 set_state(DISABLED, (e ? NORMAL : DISABLED));
182 void Widget::set_state(State mask, State bits)
184 State old_state = state;
185 state = (state&~mask)|bits;
186 if(style && style->compare_states(old_state, state))
190 void Widget::set_animation_interval(const Time::TimeDelta &iv)
193 throw invalid_argument("Widget::set_animation_interval");
196 signal_request_animation.emit(anim_interval);
199 void Widget::stop_animation()
201 set_animation_interval(Time::zero);
204 void Widget::mark_rebuild()
209 rebuild_needed = true;
210 signal_rebuild_needed.emit();
213 void Widget::rebuild_hierarchy()
217 rebuild_needed = false;
222 void Widget::rebuild()
227 PartCache::Rebuild rebuild_cache(part_cache);
228 const Style::PartSeq &parts = style->get_parts();
229 for(Style::PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i)
231 if(i->get_name().empty())
232 i->build(geom, state, part_cache);
238 void Widget::rebuild_special(const Part &part)
240 part_cache.insert_special(part);
243 void Widget::render(GL::Renderer &renderer) const
246 throw logic_error(format("Attempt to render a widget with null style (class=\"%s\", style_name=\"%s\")", get_class(), style_name));
248 GL::Renderer::Push _push(renderer);
249 renderer.transform(GL::Matrix::translation(geom.x, geom.y, 0));
250 const PartCache::PartList &parts = part_cache.get_parts();
251 for(PartCache::PartList::const_iterator i=parts.begin(); i!=parts.end(); ++i)
253 if(i->mesh && i->texture)
255 renderer.set_texture(i->texture);
256 i->mesh->draw(renderer);
259 render_special(*i->part, renderer);
263 void Widget::pointer_enter()
268 void Widget::pointer_leave()
273 void Widget::touch_press(int x, int y, unsigned finger)
276 button_press(x, y, 1);
279 void Widget::touch_release(int x, int y, unsigned finger)
282 button_release(x, y, 1);
285 void Widget::touch_motion(int x, int y, unsigned finger)
288 pointer_motion(x, y);
291 void Widget::focus_in()
296 void Widget::focus_out()
302 Widget::Loader::Loader(Widget &w):
303 DataFile::ObjectLoader<Widget>(w)
305 add("position", &Loader::position);
306 add("size", &Loader::size);
307 add("style", &Loader::style);
308 add("visible", &Widget::visible);
311 void Widget::Loader::position(int x, int y)
313 obj.set_position(x, y);
316 void Widget::Loader::size(unsigned w, unsigned h)
321 void Widget::Loader::style(const string &s)