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()
47 const Style::PartSeq &parts = style->get_parts();
48 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 ageom.w = max(ageom.w, pgeom.w+pmargin.left+pmargin.right);
55 ageom.h = max(ageom.h, pgeom.h+pmargin.top+pmargin.bottom);
58 autosize_special(*i, ageom);
64 void Widget::set_geometry(const Geometry &g)
71 void Widget::set_parent(Container *p)
74 throw hierarchy_error("widget already parented");
87 // The container has not yet added the widget as its child
93 void Widget::set_style(const string &s)
99 void Widget::update_style()
102 for(top=this; top->parent; top=top->parent) ;
103 Root *root = dynamic_cast<Root *>(top);
108 string sname = get_class();
109 if(!style_name.empty())
115 style = &root->get_resources().get<Style>(sname);
119 signal_autosize_changed.emit();
123 void Widget::set_tooltip(const string &t)
128 void Widget::set_visible(bool v)
135 signal_visibility_changed.emit(visible);
138 void Widget::set_focusable(bool f)
143 void Widget::set_focus()
146 throw hierarchy_error("no parent");
148 throw logic_error("!visible");
150 signal_request_focus.emit();
153 void Widget::set_state(State mask, State bits)
155 state = (state&~mask)|bits;
159 void Widget::rebuild()
165 const Style::PartSeq &parts = style->get_parts();
166 for(Style::PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i)
168 if(i->get_name().empty())
169 i->build(geom, state, part_cache);
175 void Widget::rebuild_special(const Part &part)
177 part_cache.insert_special(part);
180 void Widget::render(GL::Renderer &renderer) const
183 throw logic_error(format("Attempt to render a widget with null style (class=\"%s\", style_name=\"%s\")", get_class(), style_name));
185 GL::MatrixStack::Push _pushm(renderer.matrix_stack());
186 renderer.matrix_stack() *= GL::Matrix::translation(geom.x, geom.y, 0);
187 const PartCache::PartList &parts = part_cache.get_parts();
188 for(PartCache::PartList::const_iterator i=parts.begin(); i!=parts.end(); ++i)
190 if(i->mesh && i->texture)
192 renderer.set_texture(i->texture);
193 i->mesh->draw(renderer);
196 render_special(*i->part, renderer);
200 void Widget::pointer_enter()
205 void Widget::pointer_leave()
210 void Widget::focus_in()
215 void Widget::focus_out()
221 Widget::Loader::Loader(Widget &w):
222 DataFile::ObjectLoader<Widget>(w)
224 add("position", &Loader::position);
225 add("size", &Loader::size);
226 add("style", &Loader::style);
227 add("visible", &Widget::visible);
230 void Widget::Loader::position(int x, int y)
232 obj.set_position(x, y);
235 void Widget::Loader::size(unsigned w, unsigned h)
240 void Widget::Loader::style(const string &s)