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 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);
75 // TODO maybe rename this to on_size_change
81 void Widget::map_coords_to_ancestor(int &x, int &y, const Widget &ancestor) const
83 for(const Widget *w=this; w; w=w->get_parent())
88 const Geometry &wgeom = w->get_geometry();
93 throw hierarchy_error("not an ancestor");
96 void Widget::set_parent(Container *p)
99 throw hierarchy_error("widget already parented");
112 // The container has not yet added the widget as its child
118 void Widget::set_style(const string &s)
124 void Widget::update_style()
127 for(top=this; top->parent; top=top->parent) ;
128 Root *root = dynamic_cast<Root *>(top);
133 string sname = get_class();
134 if(!style_name.empty())
140 style = &root->get_resources().get<Style>(sname);
144 signal_autosize_changed.emit();
148 void Widget::set_tooltip(const string &t)
153 void Widget::set_visible(bool v)
160 signal_visibility_changed.emit(visible);
163 void Widget::set_focusable(bool f)
168 void Widget::set_focus()
171 throw hierarchy_error("no parent");
173 throw logic_error("!visible");
175 signal_request_focus.emit();
178 void Widget::set_enabled(bool e)
180 set_state(DISABLED, (e ? NORMAL : DISABLED));
183 void Widget::set_state(State mask, State bits)
185 state = (state&~mask)|bits;
189 void Widget::rebuild()
195 const Style::PartSeq &parts = style->get_parts();
196 for(Style::PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i)
198 if(i->get_name().empty())
199 i->build(geom, state, part_cache);
205 void Widget::rebuild_special(const Part &part)
207 part_cache.insert_special(part);
210 void Widget::render(GL::Renderer &renderer) const
213 throw logic_error(format("Attempt to render a widget with null style (class=\"%s\", style_name=\"%s\")", get_class(), style_name));
215 GL::MatrixStack::Push _pushm(renderer.matrix_stack());
216 renderer.matrix_stack() *= GL::Matrix::translation(geom.x, geom.y, 0);
217 const PartCache::PartList &parts = part_cache.get_parts();
218 for(PartCache::PartList::const_iterator i=parts.begin(); i!=parts.end(); ++i)
220 if(i->mesh && i->texture)
222 renderer.set_texture(i->texture);
223 i->mesh->draw(renderer);
226 render_special(*i->part, renderer);
230 void Widget::pointer_enter()
235 void Widget::pointer_leave()
240 void Widget::focus_in()
245 void Widget::focus_out()
251 Widget::Loader::Loader(Widget &w):
252 DataFile::ObjectLoader<Widget>(w)
254 add("position", &Loader::position);
255 add("size", &Loader::size);
256 add("style", &Loader::style);
257 add("visible", &Widget::visible);
260 void Widget::Loader::position(int x, int y)
262 obj.set_position(x, y);
265 void Widget::Loader::size(unsigned w, unsigned h)
270 void Widget::Loader::style(const string &s)