From b60ff74a9ebd7459ba6801e7677320e8954b80d3 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Thu, 9 Apr 2015 16:21:09 -0400 Subject: [PATCH] Decided to split control removal from parent into its own method removeParent() rather than automatically doing it in setParent(). --- newcontrol_darwin.m | 17 +++++++++++++---- newcontrol_unix.c | 19 +++++++++++++------ newcontrol_windows.c | 18 ++++++++++++++---- stack.c | 17 ++++++++++++++--- uipriv.h | 1 + 5 files changed, 55 insertions(+), 17 deletions(-) diff --git a/newcontrol_darwin.m b/newcontrol_darwin.m index 8b0bd48b..77c23ca8 100644 --- a/newcontrol_darwin.m +++ b/newcontrol_darwin.m @@ -13,6 +13,7 @@ struct uiSingleViewControl { #define S(c) ((uiSingleViewControl *) (c)) +// TODO this will need to change if we want to provide removal static void singleDestroy(uiControl *c) { [S(c)->view removeFromSuperview]; @@ -26,18 +27,25 @@ static uintptr_t singleHandle(uiControl *c) static void singleSetParent(uiControl *c, uintptr_t parent) { uiSingleViewControl *s = S(c); - uintptr_t oldparent; NSView *parentView; - oldparent = s->parent; s->parent = parent; parentView = (NSView *) (s->parent); - // TODO will this change parents? [parentView addSubview:s->immediate]; - updateParent(oldparent); updateParent(s->parent); } +static void singleRemoveParent(uiControl *c) +{ + uiSingleViewControl *s = S(c); + uintptr_t oldparent; + + oldparent = s->parent; + s->parent = 0; + [s->immediate removeFromSuperview]; + updateParent(oldparent); +} + // also good for NSBox and NSProgressIndicator static uiSize singlePreferredSize(uiControl *c, uiSizing *d) { @@ -92,6 +100,7 @@ uiControl *uiDarwinNewControl(Class class, BOOL inScrollView, BOOL scrollViewHas c->control.destroy = singleDestroy; c->control.handle = singleHandle; c->control.setParent = singleSetParent; + c->control.removeParent = singleRemoveParent; c->control.preferredSize = singlePreferredSize; c->control.resize = singleResize; diff --git a/newcontrol_unix.c b/newcontrol_unix.c index 0f050bef..870406c9 100644 --- a/newcontrol_unix.c +++ b/newcontrol_unix.c @@ -25,17 +25,23 @@ static uintptr_t singleHandle(uiControl *c) } static void singleSetParent(uiControl *c, uintptr_t parent) +{ + uiSingleWidgetControl *s = S(c); + + s->parent = parent; + gtk_container_add(GTK_CONTAINER(s->parent), s->immediate); + updateParent(s->parent); +} + +static void singleRemoveParent(uiControl *c) { uiSingleWidgetControl *s = S(c); uintptr_t oldparent; oldparent = s->parent; - s->parent = parent; - if (oldparent != 0) - gtk_container_remove(GTK_CONTAINER(oldparent), s->immediate); - gtk_container_add(GTK_CONTAINER(s->parent), s->immediate); + s->parent = 0; + gtk_container_remove(GTK_CONTAINER(oldparent), s->immediate); updateParent(oldparent); - updateParent(s->parent); } static uiSize singlePreferredSize(uiControl *c, uiSizing *d) @@ -101,7 +107,7 @@ uiControl *uiUnixNewControl(GType type, gboolean inScrolledWindow, gboolean scro // with this: // - end user call works (shoudn't be in any container) // - call in uiContainer works (both refs freed) - // this also ensures singleSetParent() works properly when changing parents + // this also ensures singleRemoveParent() works properly g_object_ref_sink(c->immediate); // and let's free the uiSingleWidgetControl with it g_signal_connect(c->immediate, "destroy", G_CALLBACK(onDestroy), c); @@ -109,6 +115,7 @@ uiControl *uiUnixNewControl(GType type, gboolean inScrolledWindow, gboolean scro c->control.destroy = singleDestroy; c->control.handle = singleHandle; c->control.setParent = singleSetParent; + c->control.removeParent = singleRemoveParent; c->control.preferredSize = singlePreferredSize; c->control.resize = singleResize; diff --git a/newcontrol_windows.c b/newcontrol_windows.c index 56719b32..1ff404b1 100644 --- a/newcontrol_windows.c +++ b/newcontrol_windows.c @@ -30,16 +30,25 @@ static uintptr_t singleHandle(uiControl *c) } static void singleSetParent(uiControl *c, uintptr_t parent) +{ + uiSingleHWNDControl *s = S(c); + + s->parent = parent; + if (SetParent(s->hwnd, (HWND) (s->parent)) == NULL) + logLastError("error setting control parent in singleSetParent()"); + updateParent(s->parent); +} + +static void singleRemoveParent(uiControl *c) { uiSingleHWNDControl *s = S(c); uintptr_t oldparent; oldparent = s->parent; - s->parent = parent; - if (SetParent(s->hwnd, (HWND) (s->parent)) == NULL) - logLastError("error changing control parent in singleSetParent()"); + s->parent = 0; + if (SetParent(s->hwnd, initialParent) == NULL) + logLastError("error removing control parent in singleSetParent()"); updateParent(oldparent); - updateParent(s->parent); } static uiSize singlePreferredSize(uiControl *c, uiSizing *d) @@ -102,6 +111,7 @@ uiControl *uiWindowsNewControl(uiWindowsNewControlParams *p) c->control.destroy = singleDestroy; c->control.handle = singleHandle; c->control.setParent = singleSetParent; + c->control.removeParent = singleRemoveParent; c->control.preferredSize = singlePreferredSize; c->control.resize = singleResize; diff --git a/stack.c b/stack.c index eaa7a7e7..80c2d75c 100644 --- a/stack.c +++ b/stack.c @@ -41,16 +41,26 @@ static void stackSetParent(uiControl *c, uintptr_t parent) { stack *s = S(c); uintmax_t i; - uintptr_t oldparent; - oldparent = s->parent; s->parent = parent; for (i = 0; i < S(c)->len; i++) (*(s->controls[i]->setParent))(s->controls[i], s->parent); - updateParent(oldparent); updateParent(s->parent); } +static void stackRemoveParent(uiControl *c) +{ + stack *s = S(c); + uintmax_t i; + uintptr_t oldparent; + + oldparent = s->parent; + s->parent = 0; + for (i = 0; i < S(c)->len; i++) + (*(s->controls[i]->removeParent))(s->controls[i]); + updateParent(oldparent); +} + static uiSize stackPreferredSize(uiControl *c, uiSizing *d) { stack *s = S(c); @@ -197,6 +207,7 @@ uiControl *uiNewHorizontalStack(void) s->control.destroy = stackDestroy; s->control.handle = stackHandle; s->control.setParent = stackSetParent; + s->control.removeParent = stackRemoveParent; s->control.preferredSize = stackPreferredSize; s->control.resize = stackResize; diff --git a/uipriv.h b/uipriv.h index 26d06ba6..7745c2a3 100644 --- a/uipriv.h +++ b/uipriv.h @@ -24,6 +24,7 @@ struct uiControl { void (*destroy)(uiControl *); uintptr_t (*handle)(uiControl *); void (*setParent)(uiControl *, uintptr_t); + void (*removeParent)(uiControl *); uiSize (*preferredSize)(uiControl *, uiSizing *); void (*resize)(uiControl *, intmax_t, intmax_t, intmax_t, intmax_t, uiSizing *); };