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()
48 const Style::PartSeq &parts = style->get_parts();
49 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 geom.w = max(geom.w, pgeom.w+pmargin.left+pmargin.right);
55 geom.h = max(geom.h, pgeom.h+pmargin.top+pmargin.bottom);
59 void Widget::set_geometry(const Geometry &g)
66 void Widget::set_parent(Container *p)
69 throw hierarchy_error("widget already parented");
82 // The container has not yet added the widget as its child
88 void Widget::set_style(const string &s)
94 void Widget::update_style()
97 for(top=this; top->parent; top=top->parent) ;
98 Root *root = dynamic_cast<Root *>(top);
103 string sname = get_class();
104 if(!style_name.empty())
110 style = &root->get_resources().get<Style>(sname);
114 signal_autosize_changed.emit();
118 void Widget::set_tooltip(const string &t)
123 void Widget::set_visible(bool v)
130 signal_visibility_changed.emit(visible);
133 void Widget::set_focusable(bool f)
138 void Widget::set_focus()
141 throw hierarchy_error("no parent");
143 throw logic_error("!visible");
145 signal_request_focus.emit();
148 void Widget::set_state(State mask, State bits)
150 state = (state&~mask)|bits;
154 void Widget::rebuild()
160 const Style::PartSeq &parts = style->get_parts();
161 for(Style::PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i)
163 if(i->get_name().empty())
164 i->build(geom, state, part_cache);
170 void Widget::rebuild_special(const Part &part)
172 part_cache.insert_special(part);
175 void Widget::render(GL::Renderer &renderer) const
178 throw logic_error(format("Attempt to render a widget with null style (class=\"%s\", style_name=\"%s\")", get_class(), style_name));
180 GL::MatrixStack::Push _pushm(renderer.matrix_stack());
181 renderer.matrix_stack() *= GL::Matrix::translation(geom.x, geom.y, 0);
182 const PartCache::PartList &parts = part_cache.get_parts();
183 for(PartCache::PartList::const_iterator i=parts.begin(); i!=parts.end(); ++i)
185 if(i->mesh && i->texture)
187 renderer.set_texture(i->texture);
188 i->mesh->draw(renderer);
191 render_special(*i->part, renderer);
195 void Widget::pointer_enter()
200 void Widget::pointer_leave()
205 void Widget::focus_in()
210 void Widget::focus_out()
216 Widget::Loader::Loader(Widget &w):
217 DataFile::ObjectLoader<Widget>(w)
219 add("position", &Loader::position);
220 add("size", &Loader::size);
221 add("style", &Loader::style);
222 add("visible", &Widget::visible);
225 void Widget::Loader::position(int x, int y)
227 obj.set_position(x, y);
230 void Widget::Loader::size(unsigned w, unsigned h)
235 void Widget::Loader::style(const string &s)