aboutsummaryrefslogtreecommitdiff
path: root/src/libui_sdl/libui/darwin/group.m
blob: 0050bbdd3c84a7a2491f85b3affcea38ee1fa135 (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
// 14 august 2015
#import "uipriv_darwin.h"

struct uiGroup {
	uiDarwinControl c;
	NSBox *box;
	uiControl *child;
	NSLayoutPriority oldHorzHuggingPri;
	NSLayoutPriority oldVertHuggingPri;
	int margined;
	struct singleChildConstraints constraints;
	NSLayoutPriority horzHuggingPri;
	NSLayoutPriority vertHuggingPri;
};

static void removeConstraints(uiGroup *g)
{
	// set to contentView instead of to the box itself, otherwise we get clipping underneath the label
	singleChildConstraintsRemove(&(g->constraints), [g->box contentView]);
}

static void uiGroupDestroy(uiControl *c)
{
	uiGroup *g = uiGroup(c);

	removeConstraints(g);
	if (g->child != NULL) {
		uiControlSetParent(g->child, NULL);
		uiDarwinControlSetSuperview(uiDarwinControl(g->child), nil);
		uiControlDestroy(g->child);
	}
	[g->box release];
	uiFreeControl(uiControl(g));
}

uiDarwinControlDefaultHandle(uiGroup, box)
uiDarwinControlDefaultParent(uiGroup, box)
uiDarwinControlDefaultSetParent(uiGroup, box)
uiDarwinControlDefaultToplevel(uiGroup, box)
uiDarwinControlDefaultVisible(uiGroup, box)
uiDarwinControlDefaultShow(uiGroup, box)
uiDarwinControlDefaultHide(uiGroup, box)
uiDarwinControlDefaultEnabled(uiGroup, box)
uiDarwinControlDefaultEnable(uiGroup, box)
uiDarwinControlDefaultDisable(uiGroup, box)

static void uiGroupSyncEnableState(uiDarwinControl *c, int enabled)
{
	uiGroup *g = uiGroup(c);

	if (uiDarwinShouldStopSyncEnableState(uiDarwinControl(g), enabled))
		return;
	if (g->child != NULL)
		uiDarwinControlSyncEnableState(uiDarwinControl(g->child), enabled);
}

uiDarwinControlDefaultSetSuperview(uiGroup, box)

static void groupRelayout(uiGroup *g)
{
	NSView *childView;

	removeConstraints(g);
	if (g->child == NULL)
		return;
	childView = (NSView *) uiControlHandle(g->child);
	singleChildConstraintsEstablish(&(g->constraints),
		[g->box contentView], childView,
		uiDarwinControlHugsTrailingEdge(uiDarwinControl(g->child)),
		uiDarwinControlHugsBottom(uiDarwinControl(g->child)),
		g->margined,
		@"uiGroup");
	// needed for some very rare drawing errors...
	jiggleViewLayout(g->box);
}

// TODO rename these since I'm starting to get confused by what they mean by hugging
BOOL uiGroupHugsTrailingEdge(uiDarwinControl *c)
{
	uiGroup *g = uiGroup(c);

	// TODO make a function?
	return g->horzHuggingPri < NSLayoutPriorityWindowSizeStayPut;
}

BOOL uiGroupHugsBottom(uiDarwinControl *c)
{
	uiGroup *g = uiGroup(c);

	return g->vertHuggingPri < NSLayoutPriorityWindowSizeStayPut;
}

static void uiGroupChildEdgeHuggingChanged(uiDarwinControl *c)
{
	uiGroup *g = uiGroup(c);

	groupRelayout(g);
}

static NSLayoutPriority uiGroupHuggingPriority(uiDarwinControl *c, NSLayoutConstraintOrientation orientation)
{
	uiGroup *g = uiGroup(c);

	if (orientation == NSLayoutConstraintOrientationHorizontal)
		return g->horzHuggingPri;
	return g->vertHuggingPri;
}

static void uiGroupSetHuggingPriority(uiDarwinControl *c, NSLayoutPriority priority, NSLayoutConstraintOrientation orientation)
{
	uiGroup *g = uiGroup(c);

	if (orientation == NSLayoutConstraintOrientationHorizontal)
		g->horzHuggingPri = priority;
	else
		g->vertHuggingPri = priority;
	uiDarwinNotifyEdgeHuggingChanged(uiDarwinControl(g));
}

static void uiGroupChildVisibilityChanged(uiDarwinControl *c)
{
	uiGroup *g = uiGroup(c);

	groupRelayout(g);
}

char *uiGroupTitle(uiGroup *g)
{
	return uiDarwinNSStringToText([g->box title]);
}

void uiGroupSetTitle(uiGroup *g, const char *title)
{
	[g->box setTitle:toNSString(title)];
}

void uiGroupSetChild(uiGroup *g, uiControl *child)
{
	NSView *childView;

	if (g->child != NULL) {
		removeConstraints(g);
		uiDarwinControlSetHuggingPriority(uiDarwinControl(g->child), g->oldHorzHuggingPri, NSLayoutConstraintOrientationHorizontal);
		uiDarwinControlSetHuggingPriority(uiDarwinControl(g->child), g->oldVertHuggingPri, NSLayoutConstraintOrientationVertical);
		uiControlSetParent(g->child, NULL);
		uiDarwinControlSetSuperview(uiDarwinControl(g->child), nil);
	}
	g->child = child;
	if (g->child != NULL) {
		childView = (NSView *) uiControlHandle(g->child);
		uiControlSetParent(g->child, uiControl(g));
		uiDarwinControlSetSuperview(uiDarwinControl(g->child), [g->box contentView]);
		uiDarwinControlSyncEnableState(uiDarwinControl(g->child), uiControlEnabledToUser(uiControl(g)));
		// don't hug, just in case we're a stretchy group
		g->oldHorzHuggingPri = uiDarwinControlHuggingPriority(uiDarwinControl(g->child), NSLayoutConstraintOrientationHorizontal);
		g->oldVertHuggingPri = uiDarwinControlHuggingPriority(uiDarwinControl(g->child), NSLayoutConstraintOrientationVertical);
		uiDarwinControlSetHuggingPriority(uiDarwinControl(g->child), NSLayoutPriorityDefaultLow, NSLayoutConstraintOrientationHorizontal);
		uiDarwinControlSetHuggingPriority(uiDarwinControl(g->child), NSLayoutPriorityDefaultLow, NSLayoutConstraintOrientationVertical);
	}
	groupRelayout(g);
}

int uiGroupMargined(uiGroup *g)
{
	return g->margined;
}

void uiGroupSetMargined(uiGroup *g, int margined)
{
	g->margined = margined;
	singleChildConstraintsSetMargined(&(g->constraints), g->margined);
}

uiGroup *uiNewGroup(const char *title)
{
	uiGroup *g;

	uiDarwinNewControl(uiGroup, g);

	g->box = [[NSBox alloc] initWithFrame:NSZeroRect];
	[g->box setTitle:toNSString(title)];
	[g->box setBoxType:NSBoxPrimary];
	[g->box setBorderType:NSLineBorder];
	[g->box setTransparent:NO];
	[g->box setTitlePosition:NSAtTop];
	// we can't use uiDarwinSetControlFont() because the selector is different
	[g->box setTitleFont:[NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSSmallControlSize]]];

	// default to low hugging to not hug edges
	g->horzHuggingPri = NSLayoutPriorityDefaultLow;
	g->vertHuggingPri = NSLayoutPriorityDefaultLow;

	return g;
}