2015-04-06 18:04:13 -05:00
|
|
|
// 6 april 2015
|
2015-04-06 23:26:27 -05:00
|
|
|
#import "uipriv_darwin.h"
|
2015-04-06 18:04:13 -05:00
|
|
|
|
2015-04-07 15:59:23 -05:00
|
|
|
// TODO
|
|
|
|
// - showing on size
|
|
|
|
|
2015-04-06 19:01:14 -05:00
|
|
|
@interface uiWindowDelegate : NSObject <NSWindowDelegate>
|
2015-04-08 15:04:27 -05:00
|
|
|
@property (assign) NSWindow *w;
|
|
|
|
@property (assign) uiContainer *container;
|
2015-04-06 19:01:14 -05:00
|
|
|
@property int (*onClosing)(uiWindow *, void *);
|
2015-04-06 18:04:13 -05:00
|
|
|
@property void *onClosingData;
|
2015-04-08 15:04:27 -05:00
|
|
|
@property uiWindow *uiw;
|
2015-04-06 18:04:13 -05:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation uiWindowDelegate
|
|
|
|
|
2015-04-08 14:53:50 -05:00
|
|
|
uiLogObjCClassAllocations
|
2015-04-07 23:26:49 -05:00
|
|
|
|
2015-04-06 18:04:13 -05:00
|
|
|
- (BOOL)windowShouldClose:(id)win
|
|
|
|
{
|
|
|
|
// return exact constants to be safe
|
2015-04-08 15:04:27 -05:00
|
|
|
if ((*(self.onClosing))(self.uiw, self.onClosingData))
|
2015-04-06 18:04:13 -05:00
|
|
|
return YES;
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2015-04-08 03:38:34 -05:00
|
|
|
// after this method returns we assume the window will be released (see below), so we can go too
|
2015-04-08 01:28:42 -05:00
|
|
|
- (void)windowWillClose:(NSNotification *)note
|
|
|
|
{
|
2015-04-08 15:04:27 -05:00
|
|
|
[self.w setDelegate:nil]; // see http://stackoverflow.com/a/29523141/3408572
|
|
|
|
uiFree(self.uiw);
|
2015-04-08 01:28:42 -05:00
|
|
|
[self release];
|
|
|
|
}
|
|
|
|
|
2015-04-06 18:04:13 -05:00
|
|
|
@end
|
|
|
|
|
|
|
|
struct uiWindow {
|
|
|
|
uiWindowDelegate *d;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int defaultOnClosing(uiWindow *w, void *data)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
uiWindow *uiNewWindow(char *title, int width, int height)
|
|
|
|
{
|
2015-04-08 15:04:27 -05:00
|
|
|
uiWindowDelegate *d;
|
2015-04-06 18:04:13 -05:00
|
|
|
|
2015-04-08 15:04:27 -05:00
|
|
|
d = [uiWindowDelegate new];
|
2015-04-06 18:04:13 -05:00
|
|
|
|
2015-04-08 15:04:27 -05:00
|
|
|
d.w = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, (CGFloat) width, (CGFloat) height)
|
2015-04-06 18:04:13 -05:00
|
|
|
styleMask:(NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask)
|
|
|
|
backing:NSBackingStoreBuffered
|
|
|
|
defer:YES];
|
2015-04-08 15:04:27 -05:00
|
|
|
[d.w setTitle:toNSString(title)];
|
2015-04-06 18:04:13 -05:00
|
|
|
// TODO substitutions
|
|
|
|
|
2015-04-08 01:28:42 -05:00
|
|
|
// this is what will destroy the window on close
|
2015-04-08 15:04:27 -05:00
|
|
|
[d.w setReleasedWhenClosed:YES];
|
2015-04-08 01:28:42 -05:00
|
|
|
|
2015-04-08 15:04:27 -05:00
|
|
|
d.container = [[uiContainer alloc] initWithFrame:NSZeroRect];
|
|
|
|
[d.w setContentView:d.container];
|
2015-04-07 14:45:00 -05:00
|
|
|
|
2015-04-08 15:04:27 -05:00
|
|
|
d.onClosing = defaultOnClosing;
|
|
|
|
[d.w setDelegate:d];
|
2015-04-06 18:04:13 -05:00
|
|
|
|
2015-04-08 15:04:27 -05:00
|
|
|
d.uiw = uiNew(uiWindow);
|
|
|
|
d.uiw->d = d;
|
|
|
|
return d.uiw;
|
2015-04-06 18:04:13 -05:00
|
|
|
}
|
|
|
|
|
2015-04-08 15:04:27 -05:00
|
|
|
#define D w->d
|
|
|
|
|
2015-04-06 18:04:13 -05:00
|
|
|
void uiWindowDestroy(uiWindow *w)
|
|
|
|
{
|
2015-04-08 15:04:27 -05:00
|
|
|
[D.w close];
|
2015-04-06 18:04:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
uintptr_t uiWindowHandle(uiWindow *w)
|
|
|
|
{
|
2015-04-08 15:04:27 -05:00
|
|
|
return (uintptr_t) (D.w);
|
2015-04-06 18:04:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO titles
|
|
|
|
|
|
|
|
void uiWindowShow(uiWindow *w)
|
|
|
|
{
|
2015-04-08 15:04:27 -05:00
|
|
|
[D.w makeKeyAndOrderFront:D.w];
|
2015-04-06 18:04:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void uiWindowHide(uiWindow *w)
|
|
|
|
{
|
2015-04-08 15:04:27 -05:00
|
|
|
[D.w orderOut:D.w];
|
2015-04-06 18:04:13 -05:00
|
|
|
}
|
|
|
|
|
2015-04-06 19:01:14 -05:00
|
|
|
void uiWindowOnClosing(uiWindow *w, int (*f)(uiWindow *, void *), void *data)
|
2015-04-06 18:04:13 -05:00
|
|
|
{
|
2015-04-08 15:04:27 -05:00
|
|
|
D.onClosing = f;
|
|
|
|
D.onClosingData = data;
|
2015-04-06 18:04:13 -05:00
|
|
|
}
|
2015-04-07 14:45:00 -05:00
|
|
|
|
|
|
|
void uiWindowSetChild(uiWindow *w, uiControl *c)
|
|
|
|
{
|
2015-04-08 15:04:27 -05:00
|
|
|
D.container.child = c;
|
|
|
|
(*(D.container.child->setParent))(D.container.child, (uintptr_t) (D.container));
|
2015-04-07 14:45:00 -05:00
|
|
|
}
|