summaryrefslogtreecommitdiff
path: root/new/window_darwin.m
blob: 1a60f8bfa605d25d7b6474c8bce47a1f055120a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// 6 april 2015
#include "ui_darwin.h"

@interface uiWindowDelegate : NSObject <NSWindowDelegate>
@property uiWindow *w;
@property int (*onClosing)(uiWindow *, void *);
@property void *onClosingData;
@end

@implementation uiWindowDelegate

// TODO will this *destroy* the window?
- (BOOL)windowShouldClose:(id)win
{
	// return exact constants to be safe
	if ((*(self.onClosing))(self.w, self.onClosingData))
		return YES;
	return NO;
}

@end

struct uiWindow {
	NSWindow *w;
	uiWindowDelegate *d;
};

static int defaultOnClosing(uiWindow *w, void *data)
{
	return 1;
}

uiWindow *uiNewWindow(char *title, int width, int height)
{
	uiWindow *w;

	w = (uiWindow *) uiAlloc(sizeof (uiWindow));

	w->w = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, (CGFloat) width, (CGFloat) height)
		styleMask:(NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask)
		backing:NSBackingStoreBuffered
		defer:YES];
	[w->w setTitle:toNSString(title)];
	// TODO substitutions

	w->d = [uiWindowDelegate new];
	w->d.w = w;
	w->d.onClosing = defaultOnClosing;
	[w->w setDelegate:w->d];

	return w;
}

void uiWindowDestroy(uiWindow *w)
{
	// TODO
	// TODO will w->d be destroyed?
}

uintptr_t uiWindowHandle(uiWindow *w)
{
	return (uintptr_t) (w->w);
}

// TODO titles

void uiWindowShow(uiWindow *w)
{
	[w->w makeKeyAndOrderFront:w->w];
}

void uiWindowHide(uiWindow *w)
{
	[w->w orderOut:w->w];
}

void uiWindowOnClosing(uiWindow *w, int (*f)(uiWindow *, void *), void *data)
{
	w->d.onClosing = f;
	w->d.onClosingData = data;
}