Cleaned up window_darwin.m's data structure juggling.

This commit is contained in:
Pietro Gagliardi 2015-04-08 16:04:27 -04:00
parent 93ead17eef
commit 7b6a11d36a
1 changed files with 28 additions and 31 deletions

View File

@ -4,12 +4,12 @@
// TODO // TODO
// - showing on size // - showing on size
// TODO clean this up
@interface uiWindowDelegate : NSObject <NSWindowDelegate> @interface uiWindowDelegate : NSObject <NSWindowDelegate>
@property (assign) NSWindow *win; @property (assign) NSWindow *w;
@property uiWindow *w; @property (assign) uiContainer *container;
@property int (*onClosing)(uiWindow *, void *); @property int (*onClosing)(uiWindow *, void *);
@property void *onClosingData; @property void *onClosingData;
@property uiWindow *uiw;
@end @end
@implementation uiWindowDelegate @implementation uiWindowDelegate
@ -19,7 +19,7 @@ uiLogObjCClassAllocations
- (BOOL)windowShouldClose:(id)win - (BOOL)windowShouldClose:(id)win
{ {
// return exact constants to be safe // return exact constants to be safe
if ((*(self.onClosing))(self.w, self.onClosingData)) if ((*(self.onClosing))(self.uiw, self.onClosingData))
return YES; return YES;
return NO; return NO;
} }
@ -27,17 +27,14 @@ uiLogObjCClassAllocations
// after this method returns we assume the window will be released (see below), so we can go too // after this method returns we assume the window will be released (see below), so we can go too
- (void)windowWillClose:(NSNotification *)note - (void)windowWillClose:(NSNotification *)note
{ {
[self.win setDelegate:nil]; // see http://stackoverflow.com/a/29523141/3408572 [self.w setDelegate:nil]; // see http://stackoverflow.com/a/29523141/3408572
uiFree(self.w); uiFree(self.uiw);
[self release]; [self release];
} }
@end @end
struct uiWindow { struct uiWindow {
NSWindow *w;
uiContainer *container;
uiControl *child;
uiWindowDelegate *d; uiWindowDelegate *d;
}; };
@ -48,63 +45,63 @@ static int defaultOnClosing(uiWindow *w, void *data)
uiWindow *uiNewWindow(char *title, int width, int height) uiWindow *uiNewWindow(char *title, int width, int height)
{ {
uiWindow *w; uiWindowDelegate *d;
w = uiNew(uiWindow); d = [uiWindowDelegate new];
w->w = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, (CGFloat) width, (CGFloat) height) d.w = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, (CGFloat) width, (CGFloat) height)
styleMask:(NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask) styleMask:(NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask)
backing:NSBackingStoreBuffered backing:NSBackingStoreBuffered
defer:YES]; defer:YES];
[w->w setTitle:toNSString(title)]; [d.w setTitle:toNSString(title)];
// TODO substitutions // TODO substitutions
// this is what will destroy the window on close // this is what will destroy the window on close
[w->w setReleasedWhenClosed:YES]; [d.w setReleasedWhenClosed:YES];
w->container = [[uiContainer alloc] initWithFrame:NSZeroRect]; d.container = [[uiContainer alloc] initWithFrame:NSZeroRect];
[w->w setContentView:((NSView *) w->container)]; [d.w setContentView:d.container];
w->d = [uiWindowDelegate new]; d.onClosing = defaultOnClosing;
w->d.win = w->w; [d.w setDelegate:d];
w->d.w = w;
w->d.onClosing = defaultOnClosing;
[w->w setDelegate:w->d];
return w; d.uiw = uiNew(uiWindow);
d.uiw->d = d;
return d.uiw;
} }
#define D w->d
void uiWindowDestroy(uiWindow *w) void uiWindowDestroy(uiWindow *w)
{ {
[w->w close]; [D.w close];
} }
uintptr_t uiWindowHandle(uiWindow *w) uintptr_t uiWindowHandle(uiWindow *w)
{ {
return (uintptr_t) (w->w); return (uintptr_t) (D.w);
} }
// TODO titles // TODO titles
void uiWindowShow(uiWindow *w) void uiWindowShow(uiWindow *w)
{ {
[w->w makeKeyAndOrderFront:w->w]; [D.w makeKeyAndOrderFront:D.w];
} }
void uiWindowHide(uiWindow *w) void uiWindowHide(uiWindow *w)
{ {
[w->w orderOut:w->w]; [D.w orderOut:D.w];
} }
void uiWindowOnClosing(uiWindow *w, int (*f)(uiWindow *, void *), void *data) void uiWindowOnClosing(uiWindow *w, int (*f)(uiWindow *, void *), void *data)
{ {
w->d.onClosing = f; D.onClosing = f;
w->d.onClosingData = data; D.onClosingData = data;
} }
void uiWindowSetChild(uiWindow *w, uiControl *c) void uiWindowSetChild(uiWindow *w, uiControl *c)
{ {
w->child = c; D.container.child = c;
w->container.child = c; (*(D.container.child->setParent))(D.container.child, (uintptr_t) (D.container));
(*(w->child->setParent))(w->child, (uintptr_t) (w->container));
} }