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()
45 const Style::PartSeq &parts = style->get_parts();
46 for(Style::PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i)
47 if(i->get_name().empty())
49 const Geometry &pgeom = i->get_geometry();
50 const Sides &pmargin = i->get_margin();
51 geom.w = max(geom.w, pgeom.w+pmargin.left+pmargin.right);
52 geom.h = max(geom.h, pgeom.h+pmargin.top+pmargin.bottom);
56 void Widget::set_geometry(const Geometry &g)
63 void Widget::set_parent(Container *p)
66 throw hierarchy_error("widget already parented");
79 // The container has not yet added the widget as its child
85 void Widget::set_style(const string &s)
91 void Widget::update_style()
94 for(top=this; top->parent; top=top->parent) ;
95 Root *root = dynamic_cast<Root *>(top);
100 string sname = get_class();
101 if(!style_name.empty())
107 style = &root->get_resources().get<Style>(sname);
111 signal_autosize_changed.emit();
115 void Widget::set_tooltip(const string &t)
120 void Widget::set_visible(bool v)
127 signal_visibility_changed.emit(visible);
130 void Widget::set_focusable(bool f)
135 void Widget::set_focus()
138 throw hierarchy_error("no parent");
140 throw logic_error("!visible");
142 signal_request_focus.emit();
145 void Widget::set_state(State mask, State bits)
147 state = (state&~mask)|bits;
151 void Widget::rebuild()
156 const Style::PartSeq &parts = style->get_parts();
157 list<CachedPart>::iterator j = cached_parts.begin();
158 for(Style::PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i, ++j)
160 if(j==cached_parts.end())
161 j = cached_parts.insert(j, CachedPart());
162 if(i->get_name().empty())
163 i->build(geom, state, *j);
165 rebuild_special(*i, *j);
169 void Widget::render(GL::Renderer &renderer) const
172 throw logic_error(format("Attempt to render a widget with null style (class=\"%s\", style_name=\"%s\")", get_class(), style_name));
174 GL::MatrixStack::Push _pushm(renderer.matrix_stack());
175 renderer.matrix_stack() *= GL::Matrix::translation(geom.x, geom.y, 0);
176 const Style::PartSeq &parts = style->get_parts();
177 list<CachedPart>::const_iterator j = cached_parts.begin();
178 for(Style::PartSeq::const_iterator i=parts.begin(); (i!=parts.end() && j!=cached_parts.end()); ++i, ++j)
180 if(j->mesh && j->texture)
182 renderer.set_texture(j->texture);
183 j->mesh->draw(renderer);
185 else if(!i->get_name().empty())
186 render_special(*i, renderer);
190 void Widget::pointer_enter()
195 void Widget::pointer_leave()
200 void Widget::focus_in()
205 void Widget::focus_out()
211 Widget::Loader::Loader(Widget &w):
212 DataFile::ObjectLoader<Widget>(w)
214 add("position", &Loader::position);
215 add("size", &Loader::size);
216 add("style", &Loader::style);
217 add("visible", &Widget::visible);
220 void Widget::Loader::position(int x, int y)
222 obj.set_position(x, y);
225 void Widget::Loader::size(unsigned w, unsigned h)
230 void Widget::Loader::style(const string &s)