2015-08-15 19:37:02 -05:00
|
|
|
// 15 august 2015
|
|
|
|
#import "uipriv_darwin.h"
|
|
|
|
|
|
|
|
struct uiWindow {
|
|
|
|
uiDarwinControl c;
|
|
|
|
NSWindow *window;
|
|
|
|
uiControl *child;
|
|
|
|
int margined;
|
|
|
|
int (*onClosing)(uiWindow *, void *);
|
|
|
|
void *onClosingData;
|
|
|
|
};
|
|
|
|
|
|
|
|
@interface windowDelegateClass : NSObject<NSWindowDelegate> {
|
2016-01-07 13:41:20 -06:00
|
|
|
struct mapTable *windows;
|
2015-08-15 19:37:02 -05:00
|
|
|
}
|
|
|
|
- (BOOL)windowShouldClose:(id)sender;
|
|
|
|
- (void)registerWindow:(uiWindow *)w;
|
|
|
|
- (void)unregisterWindow:(uiWindow *)w;
|
|
|
|
- (uiWindow *)lookupWindow:(NSWindow *)w;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation windowDelegateClass
|
|
|
|
|
|
|
|
- (id)init
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (self)
|
2015-08-17 00:29:34 -05:00
|
|
|
self->windows = newMap();
|
2015-08-15 19:37:02 -05:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
2016-01-07 13:41:20 -06:00
|
|
|
mapDestroy(self->windows);
|
2015-08-15 19:37:02 -05:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)windowShouldClose:(id)sender
|
|
|
|
{
|
|
|
|
uiWindow *w;
|
|
|
|
|
|
|
|
w = [self lookupWindow:((NSWindow *) sender)];
|
|
|
|
// w should not be NULL; we are only the delegate of registered windows
|
|
|
|
if ((*(w->onClosing))(w, w->onClosingData))
|
|
|
|
uiControlDestroy(uiControl(w));
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)registerWindow:(uiWindow *)w
|
|
|
|
{
|
2015-08-17 00:41:04 -05:00
|
|
|
mapSet(self->windows, w->window, w);
|
2015-08-15 19:37:02 -05:00
|
|
|
[w->window setDelegate:self];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)unregisterWindow:(uiWindow *)w
|
|
|
|
{
|
|
|
|
[w->window setDelegate:nil];
|
2016-01-07 13:41:20 -06:00
|
|
|
mapDelete(self->windows, w->window);
|
2015-08-15 19:37:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
- (uiWindow *)lookupWindow:(NSWindow *)w
|
|
|
|
{
|
2016-01-07 13:41:20 -06:00
|
|
|
uiWindow *v;
|
2015-08-15 19:37:02 -05:00
|
|
|
|
2016-04-25 11:28:36 -05:00
|
|
|
v = uiWindow(mapGet(self->windows, w));
|
2016-01-07 13:41:20 -06:00
|
|
|
// this CAN (and IS ALLOWED TO) return NULL, just in case we're called with some OS X-provided window as the key window
|
|
|
|
return v;
|
2015-08-15 19:37:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
static windowDelegateClass *windowDelegate = nil;
|
|
|
|
|
2016-04-25 11:28:36 -05:00
|
|
|
static void uiWindowDestroy(uiControl *c)
|
2015-08-15 19:37:02 -05:00
|
|
|
{
|
2016-04-25 11:28:36 -05:00
|
|
|
uiWindow *w = uiWindow(c);
|
2015-08-15 19:37:02 -05:00
|
|
|
NSView *childView;
|
|
|
|
|
|
|
|
// hide the window
|
|
|
|
[w->window orderOut:w->window];
|
|
|
|
if (w->child != NULL) {
|
|
|
|
childView = (NSView *) uiControlHandle(w->child);
|
|
|
|
[childView removeFromSuperview];
|
|
|
|
uiControlSetParent(w->child, NULL);
|
|
|
|
uiControlDestroy(w->child);
|
|
|
|
}
|
|
|
|
[windowDelegate unregisterWindow:w];
|
|
|
|
}
|
|
|
|
|
2016-04-25 11:28:36 -05:00
|
|
|
uiDarwinControlDefaultHandle(uiWindow, window)
|
|
|
|
// TODO?
|
|
|
|
uiDarwinControlDefaultParent(uiWindow, window)
|
|
|
|
uiDarwinControlDefaultSetParent(uiWindow, window)
|
|
|
|
// end TODO
|
|
|
|
|
|
|
|
static int uiWindowToplevel(uiControl *c)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int uiWindowVisible(uiControl *c)
|
|
|
|
{
|
|
|
|
uiWindow *w = uiWindow(c);
|
|
|
|
|
|
|
|
return [w->window isVisible];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uiWindowShow(uiControl *c)
|
2015-08-15 19:37:02 -05:00
|
|
|
{
|
|
|
|
uiWindow *w = (uiWindow *) c;
|
|
|
|
|
|
|
|
[w->window makeKeyAndOrderFront:w->window];
|
|
|
|
}
|
|
|
|
|
2016-04-25 11:28:36 -05:00
|
|
|
static void uiWindowHide(uiControl *c)
|
2015-08-15 19:37:02 -05:00
|
|
|
{
|
|
|
|
uiWindow *w = (uiWindow *) c;
|
|
|
|
|
|
|
|
[w->window orderOut:w->window];
|
|
|
|
}
|
|
|
|
|
2016-04-25 11:28:36 -05:00
|
|
|
uiDarwinControlDefaultEnabled(uiWindow, window)
|
|
|
|
uiDarwinControlDefaultEnable(uiWindow, window)
|
|
|
|
uiDarwinControlDefaultDisable(uiWindow, window)
|
|
|
|
|
|
|
|
static void uiWindowSyncEnableState(uiControl *c, int enabled)
|
2015-08-21 00:09:07 -05:00
|
|
|
{
|
|
|
|
uiWindow *w = uiWindow(c);
|
|
|
|
|
|
|
|
if (w->child != NULL)
|
2016-04-25 11:28:36 -05:00
|
|
|
uiControlSyncUpdateState(w->child, enabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uiWindowSetSuperview(uiDarwinControl *c, NSView *superview)
|
|
|
|
{
|
|
|
|
// TODO
|
2015-08-21 00:09:07 -05:00
|
|
|
}
|
2015-08-15 19:37:02 -05:00
|
|
|
|
2015-08-18 19:15:09 -05:00
|
|
|
static void windowRelayout(uiDarwinControl *c)
|
|
|
|
{
|
|
|
|
uiWindow *w = uiWindow(c);
|
|
|
|
uiDarwinControl *cc;
|
|
|
|
NSView *childView;
|
2015-08-22 20:29:22 -05:00
|
|
|
NSView *contentView;
|
2015-08-18 19:15:09 -05:00
|
|
|
|
|
|
|
if (w->child == NULL)
|
|
|
|
return;
|
|
|
|
cc = uiDarwinControl(w->child);
|
|
|
|
childView = (NSView *) uiControlHandle(w->child);
|
2015-08-22 20:29:22 -05:00
|
|
|
contentView = [w->window contentView];
|
|
|
|
[contentView removeConstraints:[contentView constraints]];
|
2015-08-18 19:15:09 -05:00
|
|
|
// first relayout the child
|
|
|
|
(*(cc->Relayout))(cc);
|
|
|
|
// now relayout ourselves
|
2015-08-22 20:29:22 -05:00
|
|
|
layoutSingleView(contentView, childView, w->margined);
|
2015-08-18 19:15:09 -05:00
|
|
|
}
|
|
|
|
|
2015-08-15 19:37:02 -05:00
|
|
|
char *uiWindowTitle(uiWindow *w)
|
|
|
|
{
|
|
|
|
return uiDarwinNSStringToText([w->window title]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void uiWindowSetTitle(uiWindow *w, const char *title)
|
|
|
|
{
|
|
|
|
[w->window setTitle:toNSString(title)];
|
|
|
|
}
|
|
|
|
|
|
|
|
void uiWindowOnClosing(uiWindow *w, int (*f)(uiWindow *, void *), void *data)
|
|
|
|
{
|
|
|
|
w->onClosing = f;
|
|
|
|
w->onClosingData = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void uiWindowSetChild(uiWindow *w, uiControl *child)
|
|
|
|
{
|
|
|
|
NSView *childView;
|
|
|
|
|
|
|
|
if (w->child != NULL) {
|
|
|
|
childView = (NSView *) uiControlHandle(w->child);
|
|
|
|
[childView removeFromSuperview];
|
|
|
|
uiControlSetParent(w->child, NULL);
|
|
|
|
}
|
|
|
|
w->child = child;
|
|
|
|
if (w->child != NULL) {
|
|
|
|
uiControlSetParent(w->child, uiControl(w));
|
|
|
|
childView = (NSView *) uiControlHandle(w->child);
|
|
|
|
[[w->window contentView] addSubview:childView];
|
|
|
|
}
|
2016-04-25 11:28:36 -05:00
|
|
|
windowRelayout(w);
|
2015-08-15 19:37:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int uiWindowMargined(uiWindow *w)
|
|
|
|
{
|
|
|
|
return w->margined;
|
|
|
|
}
|
|
|
|
|
|
|
|
void uiWindowSetMargined(uiWindow *w, int margined)
|
|
|
|
{
|
|
|
|
w->margined = margined;
|
2016-04-25 11:28:36 -05:00
|
|
|
windowRelayout(w);
|
2015-08-15 19:37:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static int defaultOnClosing(uiWindow *w, void *data)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uiWindow *uiNewWindow(const char *title, int width, int height, int hasMenubar)
|
|
|
|
{
|
|
|
|
uiWindow *w;
|
|
|
|
|
|
|
|
finalizeMenus();
|
|
|
|
|
2016-04-25 11:28:36 -05:00
|
|
|
uiDarwinNewControl(uiWindow, w);
|
2015-08-15 19:37:02 -05:00
|
|
|
|
|
|
|
w->window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, (CGFloat) width, (CGFloat) height)
|
|
|
|
styleMask:(NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask)
|
|
|
|
backing:NSBackingStoreBuffered
|
|
|
|
defer:YES];
|
|
|
|
[w->window setTitle:toNSString(title)];
|
|
|
|
|
|
|
|
// explicitly release when closed
|
|
|
|
// the only thing that closes the window is us anyway
|
|
|
|
[w->window setReleasedWhenClosed:YES];
|
|
|
|
|
2015-08-17 11:57:45 -05:00
|
|
|
if (windowDelegate == nil) {
|
2015-08-15 19:37:02 -05:00
|
|
|
windowDelegate = [windowDelegateClass new];
|
2015-08-17 11:57:45 -05:00
|
|
|
[delegates addObject:windowDelegate];
|
|
|
|
}
|
2015-08-15 19:37:02 -05:00
|
|
|
[windowDelegate registerWindow:w];
|
|
|
|
uiWindowOnClosing(w, defaultOnClosing, NULL);
|
|
|
|
|
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
|
|
|
// utility function for menus
|
|
|
|
uiWindow *windowFromNSWindow(NSWindow *w)
|
|
|
|
{
|
|
|
|
if (w == nil)
|
|
|
|
return NULL;
|
|
|
|
if (windowDelegate == nil) // no windows were created yet; we're called with some OS X-provided window
|
|
|
|
return NULL;
|
|
|
|
return [windowDelegate lookupWindow:w];
|
|
|
|
}
|