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()
47 const Style::PartSeq &parts = style->get_parts();
48 for(Style::PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i)
50 if(i->get_name().empty())
52 const Geometry &pgeom = i->get_geometry();
53 const Sides &pmargin = i->get_margin();
54 ageom.w = max(ageom.w, pgeom.w+pmargin.left+pmargin.right);
55 ageom.h = max(ageom.h, pgeom.h+pmargin.top+pmargin.bottom);
58 autosize_special(*i, ageom);
64 void Widget::set_geometry(const Geometry &g)
66 bool size_changed = (g.w!=geom.w || g.h!=geom.h);
70 // TODO maybe rename this to on_size_change
76 void Widget::set_parent(Container *p)
79 throw hierarchy_error("widget already parented");
92 // The container has not yet added the widget as its child
98 void Widget::set_style(const string &s)
104 void Widget::update_style()
107 for(top=this; top->parent; top=top->parent) ;
108 Root *root = dynamic_cast<Root *>(top);
113 string sname = get_class();
114 if(!style_name.empty())
120 style = &root->get_resources().get<Style>(sname);
124 signal_autosize_changed.emit();
128 void Widget::set_tooltip(const string &t)
133 void Widget::set_visible(bool v)
140 signal_visibility_changed.emit(visible);
143 void Widget::set_focusable(bool f)
148 void Widget::set_focus()
151 throw hierarchy_error("no parent");
153 throw logic_error("!visible");
155 signal_request_focus.emit();
158 void Widget::set_state(State mask, State bits)
160 state = (state&~mask)|bits;
164 void Widget::rebuild()
170 const Style::PartSeq &parts = style->get_parts();
171 for(Style::PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i)
173 if(i->get_name().empty())
174 i->build(geom, state, part_cache);
180 void Widget::rebuild_special(const Part &part)
182 part_cache.insert_special(part);
185 void Widget::render(GL::Renderer &renderer) const
188 throw logic_error(format("Attempt to render a widget with null style (class=\"%s\", style_name=\"%s\")", get_class(), style_name));
190 GL::MatrixStack::Push _pushm(renderer.matrix_stack());
191 renderer.matrix_stack() *= GL::Matrix::translation(geom.x, geom.y, 0);
192 const PartCache::PartList &parts = part_cache.get_parts();
193 for(PartCache::PartList::const_iterator i=parts.begin(); i!=parts.end(); ++i)
195 if(i->mesh && i->texture)
197 renderer.set_texture(i->texture);
198 i->mesh->draw(renderer);
201 render_special(*i->part, renderer);
205 void Widget::pointer_enter()
210 void Widget::pointer_leave()
215 void Widget::focus_in()
220 void Widget::focus_out()
226 Widget::Loader::Loader(Widget &w):
227 DataFile::ObjectLoader<Widget>(w)
229 add("position", &Loader::position);
230 add("size", &Loader::size);
231 add("style", &Loader::style);
232 add("visible", &Widget::visible);
235 void Widget::Loader::position(int x, int y)
237 obj.set_position(x, y);
240 void Widget::Loader::size(unsigned w, unsigned h)
245 void Widget::Loader::style(const string &s)