]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/widget.cpp
Make widgets aware of their parents
[libs/gltk.git] / source / widget.cpp
index 8674a138ab7b6cf240ff7510524c346e3265a77a..e41310a50c5fd5cba82298dd3149983b16a0f8a1 100644 (file)
@@ -1,14 +1,35 @@
+/* $Id$
+
+This file is part of libmspgltk
+Copyright © 2007  Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
 #include <msp/gl/matrix.h>
 #include <msp/gl/transform.h>
+#include "panel.h"
 #include "resources.h"
 #include "widget.h"
 
-#include <iostream>
 using namespace std;
 
 namespace Msp {
 namespace GLtk {
 
+Widget::Widget(const Resources &r):
+       res(r),
+       style(0),
+       state(NORMAL),
+       visible(true),
+       parent(0)
+{ }
+
+Widget::~Widget()
+{
+       if(parent)
+               parent->remove(*this);
+}
+
 void Widget::set_position(int x, int y)
 {
        geom.x=x;
@@ -44,23 +65,6 @@ void Widget::render() const
        GL::pop_matrix();
 }
 
-Widget::Widget(const Resources &r):
-       res(r),
-       style(0),
-       state(NORMAL)
-{ }
-
-void Widget::update_style()
-{
-       string sname=get_class();
-       if(!style_name.empty())
-       {
-               sname+='-';
-               sname+=style_name;
-       }
-       style=res.get<Style>(sname);
-}
-
 void Widget::render_part(const Part &part) const
 {
        render_graphic(part);
@@ -93,6 +97,29 @@ void Widget::render_text(const Part &part, const string &text) const
        GL::pop_matrix();
 }
 
+void Widget::update_style()
+{
+       string sname=get_class();
+       if(!style_name.empty())
+       {
+               sname+='-';
+               sname+=style_name;
+       }
+       style=res.get<Style>(sname);
+}
+
+void Widget::set_parent(Panel *p)
+{
+       if(parent && p)
+               throw InvalidState("Widget is already in a Panel");
+       parent=p;
+}
+
+void Widget::set_parent(Widget &w, Panel *p)
+{
+       w.set_parent(p);
+}
+
 
 Widget::Loader::Loader(Widget &w):
        wdg(w)
@@ -100,6 +127,7 @@ Widget::Loader::Loader(Widget &w):
        add("position", &Loader::position);
        add("size",     &Loader::size);
        add("style",    &Loader::style);
+       add("visible",  &Widget::visible);
 }
 
 void Widget::Loader::position(int x, int y)