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_focusable(bool f)
174 input_type = (f ? INPUT_TEXT : INPUT_NONE);
177 void Widget::set_focus()
180 throw hierarchy_error("no parent");
182 throw logic_error("!visible");
184 signal_request_focus.emit();
187 void Widget::set_enabled(bool e)
189 set_state(DISABLED, (e ? NORMAL : DISABLED));
192 void Widget::set_state(State mask, State bits)
194 State old_state = state;
195 state = (state&~mask)|bits;
196 if(style && style->compare_states(old_state, state))
200 void Widget::set_animation_interval(const Time::TimeDelta &iv)
203 throw invalid_argument("Widget::set_animation_interval");
206 signal_request_animation.emit(anim_interval);
209 void Widget::stop_animation()
211 set_animation_interval(Time::zero);
214 void Widget::mark_rebuild()
219 rebuild_needed = true;
220 signal_rebuild_needed.emit();
223 void Widget::rebuild_hierarchy()
227 rebuild_needed = false;
232 void Widget::rebuild()
237 PartCache::Rebuild rebuild_cache(part_cache);
238 const Style::PartSeq &parts = style->get_parts();
239 for(Style::PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i)
241 if(i->get_name().empty())
242 i->build(geom, state, part_cache);
248 void Widget::rebuild_special(const Part &part)
250 part_cache.insert_special(part);
253 void Widget::render(GL::Renderer &renderer) const
256 throw logic_error(format("Attempt to render a widget with null style (class=\"%s\", style_name=\"%s\")", get_class(), style_name));
258 static const GL::Tag texture_tag("ui_tex");
260 GL::Renderer::Push _push(renderer);
263 map_coords_to_root(x, y);
264 renderer.set_matrix(GL::Matrix::translation(x, y, 0));
265 const PartCache::PartList &parts = part_cache.get_parts();
266 for(PartCache::PartList::const_iterator i=parts.begin(); i!=parts.end(); ++i)
268 if(i->mesh && i->texture)
270 renderer.set_texture(texture_tag, i->texture, &style->get_sampler());
271 i->mesh->draw(renderer);
274 render_special(*i->part, renderer);
278 void Widget::pointer_enter()
283 void Widget::pointer_leave()
288 void Widget::touch_press(int x, int y, unsigned finger)
291 button_press(x, y, 1);
294 void Widget::touch_release(int x, int y, unsigned finger)
297 button_release(x, y, 1);
300 void Widget::touch_motion(int x, int y, unsigned finger)
303 pointer_motion(x, y);
306 void Widget::focus_in()
311 void Widget::focus_out()
317 Widget::Loader::Loader(Widget &w):
318 DataFile::ObjectLoader<Widget>(w)
320 add("position", &Loader::position);
321 add("size", &Loader::size);
322 add("style", &Loader::style);
323 add("visible", &Widget::visible);
326 void Widget::Loader::position(int x, int y)
328 obj.set_position(x, y);
331 void Widget::Loader::size(unsigned w, unsigned h)
336 void Widget::Loader::style(const string &s)