diff --git a/new/control.c b/new/control.c index c3ee677..ad03c49 100644 --- a/new/control.c +++ b/new/control.c @@ -1,6 +1,11 @@ // 7 april 2015 #include "uipriv.h" +void uiControlDestroy(uiControl *c) +{ + (*(c->destroy))(c); +} + uintptr_t uiControlHandle(uiControl *c) { return (*(c->handle))(c); diff --git a/new/newcontrol_windows.c b/new/newcontrol_windows.c index 1ea9947..eff4c9a 100644 --- a/new/newcontrol_windows.c +++ b/new/newcontrol_windows.c @@ -15,6 +15,13 @@ struct uiSingleHWNDControl { #define S(c) ((uiSingleHWNDControl *) (c)) +static void singleDestroy(uiControl *c) +{ + if (DestroyWindow(S(c)->hwnd) == 0) + logLastError("error destroying control in singleDestroy()"); + // the uiSingleHWNDControl is destroyed in the subclass procedure +} + static uintptr_t singleHandle(uiControl *c) { return (uintptr_t) (S(c)->hwnd); @@ -57,6 +64,8 @@ static LRESULT CALLBACK singleSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, return lResult; break; case WM_NCDESTROY: + // TODO call an onDestroy handler + uiFree(c); if ((*fv_RemoveWindowSubclass)(hwnd, singleSubclassProc, uIdSubclass) == FALSE) logLastError("error removing Windows control subclass in singleSubclassProc()"); break; @@ -79,6 +88,7 @@ uiControl *uiWindowsNewControl(uiWindowsNewControlParams *p) if (c->hwnd == NULL) logLastError("error creating control in uiWindowsNewControl()"); + c->control.destroy = singleDestroy; c->control.handle = singleHandle; c->control.setParent = singleSetParent; c->control.preferredSize = singlePreferredSize; diff --git a/new/stack.c b/new/stack.c index 433c6b5..45fcf54 100644 --- a/new/stack.c +++ b/new/stack.c @@ -17,6 +17,20 @@ struct stack { #define S(c) ((stack *) (c)) +static void stackDestroy(uiControl *c) +{ + stack *s = (stack *) c; + uintmax_t i; + + for (i = 0; i < S(c)->len; i++) + uiControlDestroy(s->controls[i]); + uiFree(s->controls); + uiFree(s->stretchy); + uiFree(s->width); + uiFree(s->height); + uiFree(s); +} + static uintptr_t stackHandle(uiControl *c) { return 0; @@ -163,6 +177,7 @@ uiControl *uiNewHorizontalStack(void) s = uiNew(stack); + s->control.destroy = stackDestroy; s->control.handle = stackHandle; s->control.setParent = stackSetParent; s->control.preferredSize = stackPreferredSize; diff --git a/new/ui.h b/new/ui.h index 6d2a33f..d22e949 100644 --- a/new/ui.h +++ b/new/ui.h @@ -16,6 +16,9 @@ void uiMain(void); void uiQuit(void); typedef struct uiControl uiControl; +// TODO public? +void uiControlDestroy(uiControl *); +// TODO before destroy? uintptr_t uiControlHandle(uiControl *); typedef struct uiWindow uiWindow; diff --git a/new/uipriv.h b/new/uipriv.h index 2999d0c..ed156c2 100644 --- a/new/uipriv.h +++ b/new/uipriv.h @@ -12,6 +12,7 @@ struct uiSize { // TODO handle destruction struct uiControl { + void (*destroy)(uiControl *); uintptr_t (*handle)(uiControl *); void (*setParent)(uiControl *, uintptr_t); uiSize (*preferredSize)(uiControl *, uiSizing *); diff --git a/new/window_windows.c b/new/window_windows.c index 58f4106..d68843b 100644 --- a/new/window_windows.c +++ b/new/window_windows.c @@ -46,6 +46,8 @@ static LRESULT CALLBACK uiWindowWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPA return 0; break; // fall through to DefWindowProcW() case WM_DESTROY: + if (w->child != NULL) + uiControlDestroy(w->child); uiFree(w); break; // fall through to DefWindowProcW() }