X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fwidget.cpp;h=f642004e07c3825fc1a79432f7a3acc237c1dadc;hb=319cde3c06181ba1c3619567525002926d8b4889;hp=e1538dec221bedc9a190c5904421ce6e1d16d87c;hpb=b23464cda4e4f2c7b69b18549f18c2c893c3fe2d;p=libs%2Fgltk.git diff --git a/source/widget.cpp b/source/widget.cpp index e1538de..f642004 100644 --- a/source/widget.cpp +++ b/source/widget.cpp @@ -1,102 +1,220 @@ #include -#include +#include +#include "container.h" #include "resources.h" +#include "root.h" #include "widget.h" -#include using namespace std; namespace Msp { namespace GLtk { +Widget::Widget(): + style(0), + state(NORMAL), + visible(true), + focusable(true), + parent(0) +{ } + +Widget::~Widget() +{ + if(parent) + { + Container *p = parent; + parent = 0; + p->remove(*this); + } +} + void Widget::set_position(int x, int y) { - geom.x=x; - geom.y=y; + set_geometry(Geometry(x, y, geom.w, geom.h)); } void Widget::set_size(unsigned w, unsigned h) { - geom.w=w; - geom.h=h; + set_geometry(Geometry(geom.x, geom.y, w, h)); } -void Widget::set_geometry(const Geometry &g) +void Widget::autosize() { - geom=g; + if(!style) + return; + + geom.w = 0; + geom.h = 0; + const Style::PartSeq &parts = style->get_parts(); + for(Style::PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i) + if(i->get_name().empty()) + { + const Geometry &pgeom = i->get_geometry(); + const Sides &pmargin = i->get_margin(); + geom.w = max(geom.w, pgeom.w+pmargin.left+pmargin.right); + geom.h = max(geom.h, pgeom.h+pmargin.top+pmargin.bottom); + } } -void Widget::set_style(const string &s) +void Widget::set_geometry(const Geometry &g) { - style_name=s; - update_style(); + geom = g; + on_geometry_change(); + rebuild(); } -void Widget::render() const +void Widget::set_parent(Container *p) { - if(!style) - throw InvalidState("Attempt to render a widget without a style"); + if(parent && p) + throw hierarchy_error("widget already parented"); + else if(p==parent) + return; - GL::push_matrix(); - GL::translate(geom.x, geom.y, 0); - for(PartSeq::const_iterator i=style->get_parts().begin(); i!=style->get_parts().end(); ++i) - render_part(*i); - GL::pop_matrix(); + try + { + parent = p; + + on_reparent(); + update_style(); + } + catch(...) + { + // The container has not yet added the widget as its child + parent = 0; + throw; + } } -Widget::Widget(const Resources &r): - res(r), - style(0), - state(NORMAL), - visible(true) -{ } +void Widget::set_style(const string &s) +{ + style_name = s; + update_style(); +} void Widget::update_style() { - string sname=get_class(); - if(!style_name.empty()) + Widget *top; + for(top=this; top->parent; top=top->parent) ; + Root *root = dynamic_cast(top); + if(!root) + style = 0; + else { - sname+='-'; - sname+=style_name; + string sname = get_class(); + if(!style_name.empty()) + { + sname += '-'; + sname += style_name; + } + + style = &root->get_resources().get