]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/widget.cpp
Store the Resources reference only in Root widget
[libs/gltk.git] / source / widget.cpp
index 2af3b60b3024a96e5689637ed152e152b2eec275..434ec454ea022beb1a31964025c7a98808d25d8f 100644 (file)
@@ -9,8 +9,9 @@ Distributed under the LGPL
 #include <msp/gl/matrix.h>
 #include <msp/gl/transform.h>
 #include <msp/strings/formatter.h>
-#include "panel.h"
+#include "container.h"
 #include "resources.h"
+#include "root.h"
 #include "widget.h"
 
 using namespace std;
@@ -18,8 +19,7 @@ using namespace std;
 namespace Msp {
 namespace GLtk {
 
-Widget::Widget(const Resources &r):
-       res(r),
+Widget::Widget():
        style(0),
        state(NORMAL),
        visible(true),
@@ -92,7 +92,7 @@ void Widget::set_focus()
 void Widget::render() const
 {
        if(!style)
-               throw InvalidState(format("Attempt to render a widget without a style (class=\"%s\")", get_class()));
+               throw InvalidState(format("Attempt to render a widget with null style (class=\"%s\", style_name=\"%s\")", get_class(), style_name));
 
        GL::push_matrix();
        GL::translate(geom.x, geom.y, 0);
@@ -131,16 +131,31 @@ void Widget::focus_out()
 
 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<Root *>(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<Style>(sname);
        }
-       style = res.get<Style>(sname);
+
        on_style_change();
 }
 
+void Widget::update_style(Widget &w)
+{
+       w.update_style();
+}
+
 void Widget::set_parent(Container *p)
 {
        if(parent && p)
@@ -148,6 +163,7 @@ void Widget::set_parent(Container *p)
        parent = p;
 
        on_reparent();
+       update_style();
 }
 
 void Widget::set_parent(Widget &w, Container *p)