summaryrefslogtreecommitdiff
path: root/new/newcontrol_darwin.m
blob: 7c19061ee70ac10609032680bf59b2f970beaad2 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// 7 april 2015
#include "uipriv_darwin.h"

typedef struct singleView singleView;

struct singleView {
	NSView *view;
	NSScrollView *scrollView;
	NSView *immediate;		// the control that is added to the parent container; either view or scrollView
	uiParent *parent;
	BOOL userHid;
	BOOL containerHid;
	BOOL userDisabled;
	BOOL containerDisabled;
};

static void singleDestroy(uiControl *c)
{
	singleView *s = (singleView *) (c->internal);

	[destroyedControlsView addSubview:s->immediate];
}

static uintptr_t singleHandle(uiControl *c)
{
	singleView *s = (singleView *) (c->internal);

	return (uintptr_t) (s->view);
}

static void singleSetParent(uiControl *c, uiParent *parent)
{
	singleView *s = (singleView *) (c->internal);
	NSView *parentView;
	uiParent *oldparent;

	oldparent = s->parent;
	s->parent = parent;
	if (oldparent != NULL) {
		[s->immediate removeFromSuperview];
		uiParentUpdate(oldparent);
	}
	if (s->parent != NULL) {
		parentView = (NSView *) uiParentHandle(s->parent);
		[parentView addSubview:s->immediate];
		uiParentUpdate(s->parent);
	}
}

// also good for NSBox and NSProgressIndicator
static void singlePreferredSize(uiControl *c, uiSizing *d, intmax_t *width, intmax_t *height)
{
	singleView *s = (singleView *) (c->internal);
	NSControl *control;
	NSRect r;

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

static void singleResize(uiControl *c, intmax_t x, intmax_t y, intmax_t width, intmax_t height, uiSizing *d)
{
	singleView *s = (singleView *) (c->internal);
	NSRect frame;

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

static int singleVisible(uiControl *c)
{
	singleView *s = (singleView *) (c->internal);

	if (s->userHid)
		return 0;
	return 1;
}

static void singleShow(uiControl *c)
{
	singleView *s = (singleView *) (c->internal);

	s->userHid = NO;
	if (!s->containerHid) {
		[s->immediate setHidden:NO];
		if (s->parent != NULL)
			uiParentUpdate(s->parent);
	}
}

static void singleHide(uiControl *c)
{
	singleView *s = (singleView *) (c->internal);

	s->userHid = YES;
	[s->immediate setHidden:YES];
	if (s->parent != NULL)
		uiParentUpdate(s->parent);
}

static void singleContainerShow(uiControl *c)
{
	singleView *s = (singleView *) (c->internal);

	s->containerHid = NO;
	if (!s->userHid) {
		[s->immediate setHidden:NO];
		if (s->parent != NULL)
			uiParentUpdate(s->parent);
	}
}

static void singleContainerHide(uiControl *c)
{
	singleView *s = (singleView *) (c->internal);

	s->containerHid = YES;
	[s->immediate setHidden:YES];
	if (s->parent != NULL)
		uiParentUpdate(s->parent);
}

static void enable(singleView *s)
{
	if ([s->view respondsToSelector:@selector(setEnabled:)])
		[((NSControl *) (s->view)) setEnabled:YES];
}

static void disable(singleView *s)
{
	if ([s->view respondsToSelector:@selector(setEnabled:)])
		[((NSControl *) (s->view)) setEnabled:NO];
}

static void singleEnable(uiControl *c)
{
	singleView *s = (singleView *) (c->internal);

	s->userDisabled = NO;
	if (!s->containerDisabled)
		enable(s);
}

static void singleDisable(uiControl *c)
{
	singleView *s = (singleView *) (c->internal);

	s->userDisabled = YES;
	disable(s);
}

static void singleContainerEnable(uiControl *c)
{
	singleView *s = (singleView *) (c->internal);

	s->containerDisabled = NO;
	if (!s->userDisabled)
		enable(s);
}

static void singleContainerDisable(uiControl *c)
{
	singleView *s = (singleView *) (c->internal);

	s->containerDisabled = YES;
	disable(s);
}

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

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

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

	// and keep a reference to s->immediate for when we remove the control from its parent
	[s->immediate retain];

	c = uiNew(uiControl);
	c->internal = s;
	c->destroy = singleDestroy;
	c->handle = singleHandle;
	c->setParent = singleSetParent;
	c->preferredSize = singlePreferredSize;
	c->resize = singleResize;
	c->visible = singleVisible;
	c->show = singleShow;
	c->hide = singleHide;
	c->containerShow = singleContainerShow;
	c->containerHide = singleContainerHide;
	c->enable = singleEnable;
	c->disable = singleDisable;
	c->containerEnable = singleContainerEnable;
	c->containerDisable = singleContainerDisable;

	return c;
}

BOOL uiDarwinControlFreeWhenAppropriate(uiControl *c, NSView *newSuperview)
{
	singleView *s = (singleView *) (c->internal);

	if (newSuperview == destroyedControlsView) {
		[s->immediate release];		// we don't need the reference anymore
		uiFree(s);
		uiFree(c);
		return YES;
	}
	return NO;
}