From 2f1c7d6861c801e24a003ee5f6132032e04c9bbe Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 11 Dec 2007 08:00:41 +0000 Subject: [PATCH] Make widgets aware of their parents Support removing widgets from panels --- source/panel.cpp | 15 +++++++++++++-- source/panel.h | 1 + source/widget.cpp | 22 +++++++++++++++++++++- source/widget.h | 19 ++++++++++++++++++- 4 files changed, 53 insertions(+), 4 deletions(-) diff --git a/source/panel.cpp b/source/panel.cpp index 529ec19..36585c9 100644 --- a/source/panel.cpp +++ b/source/panel.cpp @@ -27,15 +27,26 @@ Panel::Panel(const Resources &r): Panel::~Panel() { - for(ChildSeq::iterator i=children.begin(); i!=children.end(); ++i) - delete *i; + while(!children.empty()) + delete children.front(); } void Panel::add(Widget &wdg) { + set_parent(wdg, this); children.push_back(&wdg); } +void Panel::remove(Widget &wdg) +{ + ChildSeq::iterator i=find(children.begin(), children.end(), &wdg); + if(i!=children.end()) + { + set_parent(wdg, 0); + children.erase(i); + } +} + void Panel::button_press(int x, int y, unsigned btn) { if(pointer_grab>0) diff --git a/source/panel.h b/source/panel.h index 7279d78..3d26875 100644 --- a/source/panel.h +++ b/source/panel.h @@ -50,6 +50,7 @@ public: ~Panel(); void add(Widget &); + void remove(Widget &); virtual void button_press(int, int, unsigned); virtual void button_release(int, int, unsigned); diff --git a/source/widget.cpp b/source/widget.cpp index e20ea82..e41310a 100644 --- a/source/widget.cpp +++ b/source/widget.cpp @@ -7,6 +7,7 @@ Distributed under the LGPL #include #include +#include "panel.h" #include "resources.h" #include "widget.h" @@ -19,9 +20,16 @@ Widget::Widget(const Resources &r): res(r), style(0), state(NORMAL), - visible(true) + visible(true), + parent(0) { } +Widget::~Widget() +{ + if(parent) + parent->remove(*this); +} + void Widget::set_position(int x, int y) { geom.x=x; @@ -100,6 +108,18 @@ void Widget::update_style() style=res.get