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-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
|
2015-04-11 01:55:51 -05:00
|
|
|
|
|
|
|
// when we reach this point, we need to ensure that all the window's children are destroyed (for OS parity)
|
|
|
|
// this may not happen under certain conditions
|
|
|
|
// I'm not sure if calling uiQuit() in the close handler causes whatever causes our window to release its content view during deallocation to race with uiQuit()'s stopping of the run loop but that's one symptom I've noticed
|
|
|
|
// so let's manually change the content view now
|
|
|
|
// we'll set it to a new stock view; this should be enough to set our real container's superview to nil, triggering the destruction
|
|
|
|
[self.w setContentView:[[NSView alloc] initWithFrame:NSZeroRect]];
|
|
|
|
|
2015-04-08 15:04:27 -05:00
|
|
|
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-10 16:06:59 -05:00
|
|
|
|
|
|
|
// we do not want substitutions
|
|
|
|
// text fields, labels, etc. take their smart quotes and other autocorrect settings from their parent window, which provides a shared "field editor"
|
|
|
|
// so we have to turn them off here
|
|
|
|
// thanks akempgen in irc.freenode.net/#macdev
|
|
|
|
// for some reason, this selector returns NSText but is documented to return NSTextView...
|
|
|
|
// NOTE: if you disagree with me about disabling substitutions, start a github issue with why and I'll be happy to consider it
|
|
|
|
disableAutocorrect((NSTextView *) [d.w fieldEditor:YES forObject:nil]);
|
2015-04-06 18:04:13 -05:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-04-09 10:54:02 -05:00
|
|
|
char *uiWindowTitle(uiWindow *w)
|
|
|
|
{
|
2015-04-10 12:24:34 -05:00
|
|
|
return uiDarwinNSStringToText([D.w title]);
|
2015-04-09 10:54:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void uiWindowSetTitle(uiWindow *w, const char *title)
|
|
|
|
{
|
|
|
|
[D.w setTitle:toNSString(title)];
|
|
|
|
}
|
2015-04-06 18:04:13 -05:00
|
|
|
|
|
|
|
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-10 02:11:20 -05:00
|
|
|
D.container.uiChild = c;
|
|
|
|
uiControlSetParent(D.container.uiChild, (uintptr_t) (D.container));
|
2015-04-07 14:45:00 -05:00
|
|
|
}
|
2015-04-09 14:10:12 -05:00
|
|
|
|
2015-04-09 20:11:56 -05:00
|
|
|
int uiWindowMargined(uiWindow *w)
|
|
|
|
{
|
|
|
|
if ([D.container uiMargined])
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
2015-04-09 14:10:12 -05:00
|
|
|
|
|
|
|
void uiWindowSetMargined(uiWindow *w, int margined)
|
|
|
|
{
|
|
|
|
BOOL m;
|
|
|
|
|
|
|
|
m = NO;
|
|
|
|
if (margined)
|
|
|
|
m = YES;
|
|
|
|
[D.container uiSetMargined:m];
|
|
|
|
}
|