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::set_parent(Container *p)
81 throw hierarchy_error("widget already parented");
94 // The container has not yet added the widget as its child
100 void Widget::set_style(const string &s)
106 void Widget::update_style()
109 for(top=this; top->parent; top=top->parent) ;
110 Root *root = dynamic_cast<Root *>(top);
115 string sname = get_class();
116 if(!style_name.empty())
122 style = &root->get_resources().get<Style>(sname);
126 signal_autosize_changed.emit();
130 void Widget::set_tooltip(const string &t)
135 void Widget::set_visible(bool v)
142 signal_visibility_changed.emit(visible);
145 void Widget::set_focusable(bool f)
150 void Widget::set_focus()
153 throw hierarchy_error("no parent");
155 throw logic_error("!visible");
157 signal_request_focus.emit();
160 void Widget::set_state(State mask, State bits)
162 state = (state&~mask)|bits;
166 void Widget::rebuild()
172 const Style::PartSeq &parts = style->get_parts();
173 for(Style::PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i)
175 if(i->get_name().empty())
176 i->build(geom, state, part_cache);
182 void Widget::rebuild_special(const Part &part)
184 part_cache.insert_special(part);
187 void Widget::render(GL::Renderer &renderer) const
190 throw logic_error(format("Attempt to render a widget with null style (class=\"%s\", style_name=\"%s\")", get_class(), style_name));
192 GL::MatrixStack::Push _pushm(renderer.matrix_stack());
193 renderer.matrix_stack() *= GL::Matrix::translation(geom.x, geom.y, 0);
194 const PartCache::PartList &parts = part_cache.get_parts();
195 for(PartCache::PartList::const_iterator i=parts.begin(); i!=parts.end(); ++i)
197 if(i->mesh && i->texture)
199 renderer.set_texture(i->texture);
200 i->mesh->draw(renderer);
203 render_special(*i->part, renderer);
207 void Widget::pointer_enter()
212 void Widget::pointer_leave()
217 void Widget::focus_in()
222 void Widget::focus_out()
228 Widget::Loader::Loader(Widget &w):
229 DataFile::ObjectLoader<Widget>(w)
231 add("position", &Loader::position);
232 add("size", &Loader::size);
233 add("style", &Loader::style);
234 add("visible", &Widget::visible);
237 void Widget::Loader::position(int x, int y)
239 obj.set_position(x, y);
242 void Widget::Loader::size(unsigned w, unsigned h)
247 void Widget::Loader::style(const string &s)