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()
159 const Style::PartSeq &parts = style->get_parts();
160 list<CachedPart>::iterator j = cached_parts.begin();
161 for(Style::PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i, ++j)
163 if(j==cached_parts.end())
164 j = cached_parts.insert(j, CachedPart());
165 if(i->get_name().empty())
166 i->build(geom, state, *j);
168 rebuild_special(*i, *j);
172 void Widget::render(GL::Renderer &renderer) const
175 throw logic_error(format("Attempt to render a widget with null style (class=\"%s\", style_name=\"%s\")", get_class(), style_name));
177 GL::MatrixStack::Push _pushm(renderer.matrix_stack());
178 renderer.matrix_stack() *= GL::Matrix::translation(geom.x, geom.y, 0);
179 const Style::PartSeq &parts = style->get_parts();
180 list<CachedPart>::const_iterator j = cached_parts.begin();
181 for(Style::PartSeq::const_iterator i=parts.begin(); (i!=parts.end() && j!=cached_parts.end()); ++i, ++j)
183 if(j->mesh && j->texture)
185 renderer.set_texture(j->texture);
186 j->mesh->draw(renderer);
188 else if(!i->get_name().empty())
189 render_special(*i, renderer);
193 void Widget::pointer_enter()
198 void Widget::pointer_leave()
203 void Widget::focus_in()
208 void Widget::focus_out()
214 Widget::Loader::Loader(Widget &w):
215 DataFile::ObjectLoader<Widget>(w)
217 add("position", &Loader::position);
218 add("size", &Loader::size);
219 add("style", &Loader::style);
220 add("visible", &Widget::visible);
223 void Widget::Loader::position(int x, int y)
225 obj.set_position(x, y);
228 void Widget::Loader::size(unsigned w, unsigned h)
233 void Widget::Loader::style(const string &s)