From 3f2a0f8dfa2d30ec89f0be9de267652f503ab502 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Fri, 17 Apr 2015 18:11:03 -0400 Subject: [PATCH] Laid the foundation for the rewritten Mac OS X controls. --- darwin/newcontrol.m | 15 ++++++++++++++- ui_darwin.h | 3 ++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/darwin/newcontrol.m b/darwin/newcontrol.m index 12967c53..60758fa6 100644 --- a/darwin/newcontrol.m +++ b/darwin/newcontrol.m @@ -12,13 +12,22 @@ struct singleView { BOOL containerHid; BOOL userDisabled; BOOL containerDisabled; + void (*onDestroy)(void *); + void *onDestroyData; }; static void singleDestroy(uiControl *c) { singleView *s = (singleView *) (c->Internal); + [s->immediate retain]; // to keep alive when removing + if (s->parent != NULL) { + [s->immediate removeFromSuperview]; + s->parent = NULL; + } [destroyedControlsView addSubview:s->immediate]; + (*(s->onDestroy))(s->onDestroyData); + [s->immediate release]; } static uintptr_t singleHandle(uiControl *c) @@ -28,6 +37,7 @@ static uintptr_t singleHandle(uiControl *c) return (uintptr_t) (s->view); } +// TODO figure out retain/release for this static void singleSetParent(uiControl *c, uiParent *parent) { singleView *s = (singleView *) (c->Internal); @@ -176,7 +186,7 @@ static void singleContainerDisable(uiControl *c) disable(s); } -void uiDarwinNewControl(uiControl *c, Class class, BOOL inScrollView, BOOL scrollViewHasBorder) +void uiDarwinNewControl(uiControl *c, Class class, BOOL inScrollView, BOOL scrollViewHasBorder, void (*onDestroy)(void *), void *onDestroyData) { singleView *s; @@ -198,6 +208,9 @@ void uiDarwinNewControl(uiControl *c, Class class, BOOL inScrollView, BOOL scrol s->immediate = (NSView *) (s->scrollView); } + s->onDestroy = onDestroy; + s->onDestroyData = onDestroyData; + // and keep a reference to s->immediate for when we remove the control from its parent [s->immediate retain]; diff --git a/ui_darwin.h b/ui_darwin.h index cab61d15..99fb202b 100644 --- a/ui_darwin.h +++ b/ui_darwin.h @@ -10,10 +10,11 @@ This file assumes that you have imported and "ui.h" beforehand. // uiDarwinNewControl() initializes the given uiControl with the given Cocoa control inside. // The second parameter should come from [RealControlType class]. // The two scrollView parameters allow placing scrollbars on the new control. +// The two onDestroy parameters define a function and its parameter to call when the widget is destroyed. // Your control must call uiDarwinControlFreeWhenAppropriate() on the returned uiControl in its -[viewDidMoveToSuperview] method. // If it returns a value other than NO, then the uiControl has been freed and you should set references to it to NULL. extern void uiDarwinNewControl(uiControl *c, Class class, BOOL inScrollView, BOOL scrollViewHasBorder); -extern BOOL uiDarwinControlFreeWhenAppropriate(uiControl *c, NSView *newSuperview); +extern BOOL uiDarwinControlFreeWhenAppropriate(uiControl *c, NSView *newSuperview, void (*onDestroy)(void *), void *onDestroyData); // You can use this function from within your control implementations to return text strings that can be freed with uiTextFree(). extern char *uiDarwinNSStringToText(NSString *);