summaryrefslogtreecommitdiff
path: root/new/newcontrol_darwin.m
blob: 8b0bd48ba71e71dbce5e6aff45b2a7cdf192468d (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// 7 april 2015
#include "uipriv_darwin.h"

typedef struct uiSingleViewControl uiSingleViewControl;

struct uiSingleViewControl {
	uiControl control;
	NSView *view;
	NSScrollView *scrollView;
	NSView *immediate;		// the control that is added to the parent container; either view or scrollView
	uintptr_t parent;
};

#define S(c) ((uiSingleViewControl *) (c))

static void singleDestroy(uiControl *c)
{
	[S(c)->view removeFromSuperview];
}

static uintptr_t singleHandle(uiControl *c)
{
	return (uintptr_t) (S(c)->view);
}

static void singleSetParent(uiControl *c, uintptr_t parent)
{
	uiSingleViewControl *s = S(c);
	uintptr_t oldparent;
	NSView *parentView;

	oldparent = s->parent;
	s->parent = parent;
	parentView = (NSView *) (s->parent);
	// TODO will this change parents?
	[parentView addSubview:s->immediate];
	updateParent(oldparent);
	updateParent(s->parent);
}

// also good for NSBox and NSProgressIndicator
static uiSize singlePreferredSize(uiControl *c, uiSizing *d)
{
	uiSize size;
	NSControl *control;
	NSRect r;

	control = (NSControl *) (S(c)->view);
	[control sizeToFit];
	// use alignmentRect here instead of frame because we'll be resizing based on that
	r = [control alignmentRectForFrame:[control frame]];
	size.width = (intmax_t) r.size.width;
	size.height = (intmax_t) r.size.height;
	return size;
}

static void singleResize(uiControl *c, intmax_t x, intmax_t y, intmax_t width, intmax_t height, uiSizing *d)
{
	NSRect frame;

	frame.origin.x = x;
	// mac os x coordinate system has (0,0) in the lower-left
	frame.origin.y = ([[S(c)->immediate superview] bounds].size.height - height) - y;
	frame.size.width = width;
	frame.size.height = height;
	frame = [S(c)->immediate frameForAlignmentRect:frame];
	[S(c)->immediate setFrame:frame];
}

uiControl *uiDarwinNewControl(Class class, BOOL inScrollView, BOOL scrollViewHasBorder)
{
	uiSingleViewControl *c;

	c = uiNew(uiSingleViewControl);
	// thanks to autoxr and arwyn in irc.freenode.net/#macdev
	c->view = (NSView *) [[class alloc] initWithFrame:NSZeroRect];
	c->immediate = c->view;

	if (inScrollView) {
		c->scrollView = [[NSScrollView alloc] initWithFrame:NSZeroRect];
		[c->scrollView setDocumentView:c->view];
		[c->scrollView setHasHorizontalScroller:YES];
		[c->scrollView setHasVerticalScroller:YES];
		[c->scrollView setAutohidesScrollers:YES];
		if (scrollViewHasBorder)
			[c->scrollView setBorderType:NSBezelBorder];
		else
			[c->scrollView setBorderType:NSNoBorder];
		c->immediate = (NSView *) (c->scrollView);
	}

	c->control.destroy = singleDestroy;
	c->control.handle = singleHandle;
	c->control.setParent = singleSetParent;
	c->control.preferredSize = singlePreferredSize;
	c->control.resize = singleResize;

	return (uiControl *) c;
}

BOOL uiDarwinControlFreeWhenAppropriate(uiControl *c, NSView *newSuperview)
{
	if (newSuperview == nil) {
		uiFree(c);
		return YES;
	}
	return NO;
}