From 4ba2e22bae737b9e3e97bf23964a1fb125766b95 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Tue, 29 Jul 2008 11:50:40 +0200 Subject: [PATCH] widget: add detach callbacks Signed-off-by: Julien Danjou --- statusbar.c | 2 ++ structs.h | 2 ++ titlebar.c | 2 ++ widgets/taglist.c | 18 ++++++++++++++++++ widgets/tasklist.c | 19 ++++++++++++++++++- 5 files changed, 42 insertions(+), 1 deletion(-) diff --git a/statusbar.c b/statusbar.c index b486b416c..36e366033 100644 --- a/statusbar.c +++ b/statusbar.c @@ -335,6 +335,8 @@ luaA_statusbar_widget_remove(lua_State *L) wnext = w->next; if(w->widget == *widget) { + if((*widget)->detach) + (*widget)->detach(*widget, *sb); widget_unref(widget); widget_node_list_detach(&(*sb)->widgets, w); p_delete(&w); diff --git a/structs.h b/structs.h index 08827fd2f..c701eb82c 100644 --- a/structs.h +++ b/structs.h @@ -97,6 +97,8 @@ struct widget_t widget_constructor_t *type; /** Widget destructor */ widget_destructor_t *destructor; + /** Widget detach function */ + void (*detach)(widget_t *, void *); /** Draw function */ int (*draw)(draw_context_t *, int, widget_node_t *, int, int, void *); /** Index function */ diff --git a/titlebar.c b/titlebar.c index 7adfc7b86..5b8a01bb5 100644 --- a/titlebar.c +++ b/titlebar.c @@ -388,6 +388,8 @@ luaA_titlebar_widget_remove(lua_State *L) wnext = w->next; if(w->widget == *widget) { + if((*widget)->detach) + (*widget)->detach(*widget, *tb); widget_unref(widget); widget_node_list_detach(&(*tb)->widgets, w); p_delete(&w); diff --git a/widgets/taglist.c b/widgets/taglist.c index 85f474f97..abf865a88 100644 --- a/widgets/taglist.c +++ b/widgets/taglist.c @@ -251,6 +251,23 @@ taglist_destructor(widget_t *widget) p_delete(&d); } +/** Taglist detach function. + * \param widget The widget which is detaching. + * \param object The object we are leaving. + */ +static void +taglist_detach(widget_t *widget, void *object) +{ + taglist_data_t *d = widget->data; + taglist_drawn_area_t *tda; + + if((tda = taglist_drawn_area_getbyobj(d->drawn_area, object))) + { + taglist_drawn_area_list_detach(&d->drawn_area, tda); + taglist_drawn_area_delete(&tda); + } +} + /** Create a brand new taglist widget. * \param align Widget alignment. * \return A taglist widget. @@ -269,6 +286,7 @@ taglist_new(alignment_t align) w->draw = taglist_draw; w->button_press = taglist_button_press; w->destructor = taglist_destructor; + w->detach = taglist_detach; w->data = d = p_new(taglist_data_t, 1); d->label = LUA_REFNIL; diff --git a/widgets/tasklist.c b/widgets/tasklist.c index 17abde747..7c4d2c6cb 100644 --- a/widgets/tasklist.c +++ b/widgets/tasklist.c @@ -125,7 +125,6 @@ tasklist_draw(draw_context_t *ctx, int screen, if(!(odata = tasklist_object_data_getbyobj(d->objects_data, p))) { - /** \todo delete this when the widget is removed from the object */ odata = p_new(tasklist_object_data_t, 1); odata->object = p; tasklist_object_data_list_push(&d->objects_data, odata); @@ -351,6 +350,23 @@ tasklist_destructor(widget_t *widget) p_delete(&d); } +/** Tasklist detach function. + * \param widget The widget which is detaching. + * \param object The object we are leaving. + */ +static void +tasklist_detach(widget_t *widget, void *object) +{ + tasklist_data_t *d = widget->data; + tasklist_object_data_t *od; + + if((od = tasklist_object_data_getbyobj(d->objects_data, object))) + { + tasklist_object_data_list_detach(&d->objects_data, od); + tasklist_object_data_delete(&od); + } +} + /** Create a new widget tasklist. * \param align The widget alignment, which is flex anyway. * \return A brand new tasklist widget. @@ -370,6 +386,7 @@ tasklist_new(alignment_t align __attribute__ ((unused))) w->newindex = luaA_tasklist_newindex; w->data = d = p_new(tasklist_data_t, 1); w->destructor = tasklist_destructor; + w->detach = tasklist_detach; d->show_icons = true; d->label = LUA_REFNIL;