1 #include <msp/gl/matrix.h>
2 #include <msp/strings/format.h>
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()
49 const Style::PartSeq &parts = style->get_parts();
50 for(Style::PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i)
52 if(i->get_name().empty())
54 const Geometry &pgeom = i->get_geometry();
55 const Sides &pmargin = i->get_margin();
56 ageom.w = max(ageom.w, pgeom.w+pmargin.left+pmargin.right);
57 ageom.h = max(ageom.h, pgeom.h+pmargin.top+pmargin.bottom);
60 autosize_special(*i, ageom);
66 void Widget::set_geometry(const Geometry &g)
68 bool size_changed = (g.w!=geom.w || g.h!=geom.h);
72 // TODO maybe rename this to on_size_change
78 void Widget::map_coords_to_ancestor(int &x, int &y, const Widget &ancestor) const
80 for(const Widget *w=this; w; w=w->get_parent())
85 const Geometry &wgeom = w->get_geometry();
90 throw hierarchy_error("not an ancestor");
93 void Widget::set_parent(Container *p)
96 throw hierarchy_error("widget already parented");
109 // The container has not yet added the widget as its child
115 void Widget::set_style(const string &s)
121 void Widget::update_style()
124 for(top=this; top->parent; top=top->parent) ;
125 Root *root = dynamic_cast<Root *>(top);
130 string sname = get_class();
131 if(!style_name.empty())
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_focusable(bool f)
165 void Widget::set_focus()
168 throw hierarchy_error("no parent");
170 throw logic_error("!visible");
172 signal_request_focus.emit();
175 void Widget::set_enabled(bool e)
177 set_state(DISABLED, (e ? NORMAL : DISABLED));
180 void Widget::set_state(State mask, State bits)
182 state = (state&~mask)|bits;
186 void Widget::rebuild()
192 const Style::PartSeq &parts = style->get_parts();
193 for(Style::PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i)
195 if(i->get_name().empty())
196 i->build(geom, state, part_cache);
202 void Widget::rebuild_special(const Part &part)
204 part_cache.insert_special(part);
207 void Widget::render(GL::Renderer &renderer) const
210 throw logic_error(format("Attempt to render a widget with null style (class=\"%s\", style_name=\"%s\")", get_class(), style_name));
212 GL::MatrixStack::Push _pushm(renderer.matrix_stack());
213 renderer.matrix_stack() *= GL::Matrix::translation(geom.x, geom.y, 0);
214 const PartCache::PartList &parts = part_cache.get_parts();
215 for(PartCache::PartList::const_iterator i=parts.begin(); i!=parts.end(); ++i)
217 if(i->mesh && i->texture)
219 renderer.set_texture(i->texture);
220 i->mesh->draw(renderer);
223 render_special(*i->part, renderer);
227 void Widget::pointer_enter()
232 void Widget::pointer_leave()
237 void Widget::focus_in()
242 void Widget::focus_out()
248 Widget::Loader::Loader(Widget &w):
249 DataFile::ObjectLoader<Widget>(w)
251 add("position", &Loader::position);
252 add("size", &Loader::size);
253 add("style", &Loader::style);
254 add("visible", &Widget::visible);
257 void Widget::Loader::position(int x, int y)
259 obj.set_position(x, y);
262 void Widget::Loader::size(unsigned w, unsigned h)
267 void Widget::Loader::style(const string &s)