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-12 15:00:09 -05:00
|
|
|
// TODO
|
|
|
|
// - free chilld containers properly
|
|
|
|
|
2015-04-06 19:01:14 -05:00
|
|
|
@interface uiWindowDelegate : NSObject <NSWindowDelegate>
|
2015-04-08 15:04:27 -05:00
|
|
|
@property (assign) NSWindow *w;
|
2015-04-13 11:53:05 -05:00
|
|
|
@property uiParent *content;
|
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-16 13:50:05 -05:00
|
|
|
@property struct window *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-16 14:08:49 -05:00
|
|
|
if ((*(self.onClosing))(uiWindow(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)
|
2015-04-13 11:53:05 -05:00
|
|
|
// because we need to set the content view's superview to the destroyed controls view to trigger deletion, we need to do this manually
|
|
|
|
// first, replace the current content view...
|
2015-04-11 01:55:51 -05:00
|
|
|
[self.w setContentView:[[NSView alloc] initWithFrame:NSZeroRect]];
|
2015-04-13 11:53:05 -05:00
|
|
|
// ...then, trigger the deletion
|
|
|
|
[destroyedControlsView addSubview:((NSView *) uiParentHandle(self.content))];
|
2015-04-11 01:55:51 -05:00
|
|
|
|
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
|
|
|
|
|
2015-04-16 13:50:05 -05:00
|
|
|
struct window {
|
|
|
|
uiWindow w;
|
2015-04-06 18:04:13 -05:00
|
|
|
uiWindowDelegate *d;
|
2015-04-13 11:53:05 -05:00
|
|
|
int margined;
|
2015-04-06 18:04:13 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
static int defaultOnClosing(uiWindow *w, void *data)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-04-16 13:50:05 -05:00
|
|
|
#define D (((struct window *) w)->d)
|
2015-04-06 18:04:13 -05:00
|
|
|
|
2015-04-16 13:50:05 -05:00
|
|
|
static void windowDestroy(uiWindow *w)
|
2015-04-06 18:04:13 -05:00
|
|
|
{
|
2015-04-08 15:04:27 -05:00
|
|
|
[D.w close];
|
2015-04-06 18:04:13 -05:00
|
|
|
}
|
|
|
|
|
2015-04-16 13:50:05 -05:00
|
|
|
static uintptr_t windowHandle(uiWindow *w)
|
2015-04-06 18:04:13 -05:00
|
|
|
{
|
2015-04-08 15:04:27 -05:00
|
|
|
return (uintptr_t) (D.w);
|
2015-04-06 18:04:13 -05:00
|
|
|
}
|
|
|
|
|
2015-04-16 13:50:05 -05:00
|
|
|
static char *windowTitle(uiWindow *w)
|
2015-04-09 10:54:02 -05:00
|
|
|
{
|
2015-04-10 12:24:34 -05:00
|
|
|
return uiDarwinNSStringToText([D.w title]);
|
2015-04-09 10:54:02 -05:00
|
|
|
}
|
|
|
|
|
2015-04-16 13:50:05 -05:00
|
|
|
static void windowSetTitle(uiWindow *w, const char *title)
|
2015-04-09 10:54:02 -05:00
|
|
|
{
|
|
|
|
[D.w setTitle:toNSString(title)];
|
|
|
|
}
|
2015-04-06 18:04:13 -05:00
|
|
|
|
2015-04-16 13:50:05 -05:00
|
|
|
static void windowShow(uiWindow *w)
|
2015-04-06 18:04:13 -05:00
|
|
|
{
|
2015-04-08 15:04:27 -05:00
|
|
|
[D.w makeKeyAndOrderFront:D.w];
|
2015-04-06 18:04:13 -05:00
|
|
|
}
|
|
|
|
|
2015-04-16 13:50:05 -05:00
|
|
|
static void windowHide(uiWindow *w)
|
2015-04-06 18:04:13 -05:00
|
|
|
{
|
2015-04-08 15:04:27 -05:00
|
|
|
[D.w orderOut:D.w];
|
2015-04-06 18:04:13 -05:00
|
|
|
}
|
|
|
|
|
2015-04-16 13:50:05 -05:00
|
|
|
static void windowOnClosing(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
|
|
|
|
2015-04-16 13:50:05 -05:00
|
|
|
static void windowSetChild(uiWindow *w, uiControl *c)
|
2015-04-07 14:45:00 -05:00
|
|
|
{
|
2015-04-17 12:43:01 -05:00
|
|
|
uiParentSetMainControl(D.content, c);
|
2015-04-07 14:45:00 -05:00
|
|
|
}
|
2015-04-09 14:10:12 -05:00
|
|
|
|
2015-04-16 13:50:05 -05:00
|
|
|
static int windowMargined(uiWindow *ww)
|
2015-04-09 20:11:56 -05:00
|
|
|
{
|
2015-04-16 13:50:05 -05:00
|
|
|
struct window *w = (struct window *) ww;
|
|
|
|
|
2015-04-13 11:53:05 -05:00
|
|
|
return w->margined;
|
2015-04-09 20:11:56 -05:00
|
|
|
}
|
2015-04-09 14:10:12 -05:00
|
|
|
|
2015-04-16 13:50:05 -05:00
|
|
|
static void windowSetMargined(uiWindow *ww, int margined)
|
2015-04-09 14:10:12 -05:00
|
|
|
{
|
2015-04-16 13:50:05 -05:00
|
|
|
struct window *w = (struct window *) ww;
|
|
|
|
|
2015-04-13 11:53:05 -05:00
|
|
|
w->margined = margined;
|
|
|
|
if (w->margined)
|
|
|
|
uiParentSetMargins(D.content, macXMargin, macYMargin, macXMargin, macYMargin);
|
|
|
|
else
|
|
|
|
uiParentSetMargins(D.content, 0, 0, 0, 0);
|
|
|
|
uiParentUpdate(D.content);
|
2015-04-09 14:10:12 -05:00
|
|
|
}
|
2015-04-16 13:50:05 -05:00
|
|
|
|
|
|
|
uiWindow *uiNewWindow(const char *title, int width, int height)
|
|
|
|
{
|
|
|
|
uiWindowDelegate *d;
|
|
|
|
|
|
|
|
d = [uiWindowDelegate new];
|
|
|
|
|
|
|
|
d.w = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, (CGFloat) width, (CGFloat) height)
|
|
|
|
styleMask:(NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask)
|
|
|
|
backing:NSBackingStoreBuffered
|
|
|
|
defer:YES];
|
|
|
|
[d.w setTitle:toNSString(title)];
|
|
|
|
|
|
|
|
// 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]);
|
|
|
|
|
|
|
|
// this is what will destroy the window on close
|
|
|
|
[d.w setReleasedWhenClosed:YES];
|
|
|
|
|
|
|
|
d.content = uiNewParent(0);
|
|
|
|
[d.w setContentView:((NSView *) uiParentHandle(d.content))];
|
|
|
|
|
|
|
|
d.onClosing = defaultOnClosing;
|
|
|
|
[d.w setDelegate:d];
|
|
|
|
|
|
|
|
d.uiw = uiNew(struct window);
|
|
|
|
d.uiw->d = d;
|
|
|
|
|
|
|
|
uiWindow(d.uiw)->Destroy = windowDestroy;
|
|
|
|
uiWindow(d.uiw)->Handle = windowHandle;
|
|
|
|
uiWindow(d.uiw)->Title = windowTitle;
|
|
|
|
uiWindow(d.uiw)->SetTitle = windowSetTitle;
|
|
|
|
uiWindow(d.uiw)->Show = windowShow;
|
|
|
|
uiWindow(d.uiw)->Hide = windowHide;
|
2015-04-16 14:08:49 -05:00
|
|
|
uiWindow(d.uiw)->OnClosing = windowOnClosing;
|
2015-04-16 13:50:05 -05:00
|
|
|
uiWindow(d.uiw)->SetChild = windowSetChild;
|
|
|
|
uiWindow(d.uiw)->Margined = windowMargined;
|
|
|
|
uiWindow(d.uiw)->SetMargined = windowSetMargined;
|
|
|
|
|
2015-04-16 14:08:49 -05:00
|
|
|
return uiWindow(d.uiw);
|
2015-04-16 13:50:05 -05:00
|
|
|
}
|