X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fwidget.cpp;h=7496f8bdda1e849c3f35768d2d2ca6f29a11f503;hb=d2d5b4c4dedf90a42dd2baff8334318b1d000f64;hp=4b48e8ba11400d2f410790de92b8464e890b68b3;hpb=1aa6cd9b865e366737dcc9d2d36c4f8faed5bc4f;p=libs%2Fgltk.git diff --git a/source/widget.cpp b/source/widget.cpp index 4b48e8b..7496f8b 100644 --- a/source/widget.cpp +++ b/source/widget.cpp @@ -30,31 +30,35 @@ Widget::~Widget() void Widget::set_position(int x, int y) { - geom.x = x; - geom.y = y; - on_geometry_change(); - rebuild(); + set_geometry(Geometry(x, y, geom.w, geom.h)); } void Widget::set_size(unsigned w, unsigned h) { - geom.w = w; - geom.h = h; - on_geometry_change(); - rebuild(); + set_geometry(Geometry(geom.x, geom.y, w, h)); } void Widget::autosize() { - geom.w = 0; - geom.h = 0; + if(!style) + return; + + Geometry ageom; const Style::PartSeq &parts = style->get_parts(); for(Style::PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i) + { if(i->get_name().empty()) { - geom.w = max(geom.w, i->get_geometry().w); - geom.h = max(geom.h, i->get_geometry().h); + const Geometry &pgeom = i->get_geometry(); + const Sides &pmargin = i->get_margin(); + ageom.w = max(ageom.w, pgeom.w+pmargin.left+pmargin.right); + ageom.h = max(ageom.h, pgeom.h+pmargin.top+pmargin.bottom); } + else + autosize_special(*i, ageom); + } + + set_geometry(ageom); } void Widget::set_geometry(const Geometry &g) @@ -70,10 +74,20 @@ void Widget::set_parent(Container *p) throw hierarchy_error("widget already parented"); else if(p==parent) return; - parent = p; - on_reparent(); - update_style(); + try + { + parent = p; + + on_reparent(); + update_style(); + } + catch(...) + { + // The container has not yet added the widget as its child + parent = 0; + throw; + } } void Widget::set_style(const string &s) @@ -147,37 +161,39 @@ void Widget::rebuild() if(!style) return; + part_cache.clear(); const Style::PartSeq &parts = style->get_parts(); - list::iterator j = cached_parts.begin(); - for(Style::PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i, ++j) + for(Style::PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i) { - if(j==cached_parts.end()) - j = cached_parts.insert(j, CachedPart()); if(i->get_name().empty()) - i->build(geom, state, *j); + i->build(geom, state, part_cache); else - rebuild_special(*i, *j); + rebuild_special(*i); } } -void Widget::render() const +void Widget::rebuild_special(const Part &part) +{ + part_cache.insert_special(part); +} + +void Widget::render(GL::Renderer &renderer) const { if(!style) throw logic_error(format("Attempt to render a widget with null style (class=\"%s\", style_name=\"%s\")", get_class(), style_name)); - GL::MatrixStack::Push _pushm(GL::MatrixStack::modelview()); - GL::MatrixStack::modelview() *= GL::Matrix::translation(geom.x, geom.y, 0); - const Style::PartSeq &parts = style->get_parts(); - list::const_iterator j = cached_parts.begin(); - for(Style::PartSeq::const_iterator i=parts.begin(); (i!=parts.end() && j!=cached_parts.end()); ++i, ++j) + GL::MatrixStack::Push _pushm(renderer.matrix_stack()); + renderer.matrix_stack() *= GL::Matrix::translation(geom.x, geom.y, 0); + const PartCache::PartList &parts = part_cache.get_parts(); + for(PartCache::PartList::const_iterator i=parts.begin(); i!=parts.end(); ++i) { - if(j->mesh && j->texture) + if(i->mesh && i->texture) { - GL::Bind bind_tex(j->texture); - j->mesh->draw(); + renderer.set_texture(i->texture); + i->mesh->draw(renderer); } - else if(!i->get_name().empty()) - render_special(*i); + else if(i->part) + render_special(*i->part, renderer); } }