summaryrefslogtreecommitdiff
path: root/new/stack.c
blob: 8a3906f27226abd6e1634a839c66ec9924914ca2 (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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// 7 april 2015
#include "uipriv.h"

// TODO
// - rename to uiBox

typedef struct stack stack;
typedef struct stackControl stackControl;

struct stack {
	uiStack s;
	stackControl *controls;
	uintmax_t len;
	uintmax_t cap;
	int vertical;
	uiParent *parent;
	int padded;
	int userHid;
	int containerHid;
	int userDisabled;
	int containerDisabled;
};

struct stackControl {
	uiControl *c;
	int stretchy;
	intmax_t width;		// both used by resize(); preallocated to save time and reduce risk of failure
	intmax_t height;
};

static void stackDestroy(uiControl *c)
{
	stack *s = (stack *) c;
	uintmax_t i;

	for (i = 0; i < s->len; i++)
		uiControlDestroy(s->controls[i].c);
	uiFree(s->controls);
	uiFree(s);
}

static uintptr_t stackHandle(uiControl *c)
{
	return 0;
}

static void stackSetParent(uiControl *c, uiParent *parent)
{
	stack *s = (stack *) c;
	uintmax_t i;
	uiParent *oldparent;

	oldparent = s->parent;
	s->parent = parent;
	for (i = 0; i < s->len; i++)
		uiControlSetParent(s->controls[i].c, s->parent);
	if (oldparent != NULL)
		uiParentUpdate(oldparent);
	if (s->parent != NULL)
		uiParentUpdate(s->parent);
}

static void stackPreferredSize(uiControl *c, uiSizing *d, intmax_t *width, intmax_t *height)
{
	stack *s = (stack *) c;
	int xpadding, ypadding;
	uintmax_t nStretchy;
	// these two contain the largest preferred width and height of all stretchy controls in the stack
	// all stretchy controls will use this value to determine the final preferred size
	intmax_t maxStretchyWidth, maxStretchyHeight;
	uintmax_t i;
	intmax_t preferredWidth, preferredHeight;

	*width = 0;
	*height = 0;
	if (s->len == 0)
		return;

	// 0) get this Stack's padding
	xpadding = 0;
	ypadding = 0;
	if (s->padded) {
		xpadding = d->xPadding;
		ypadding = d->yPadding;
	}

	// 1) initialize the desired rect with the needed padding
	if (s->vertical)
		*height = (s->len - 1) * ypadding;
	else
		*width = (s->len - 1) * xpadding;

	// 2) add in the size of non-stretchy controls and get (but not add in) the largest widths and heights of stretchy controls
	// we still add in like direction of stretchy controls
	nStretchy = 0;
	maxStretchyWidth = 0;
	maxStretchyHeight = 0;
	for (i = 0; i < s->len; i++) {
		if (!uiControlVisible(s->controls[i].c))
			continue;
		uiControlPreferredSize(s->controls[i].c, d, &preferredWidth, &preferredHeight);
		if (s->controls[i].stretchy) {
			nStretchy++;
			if (maxStretchyWidth < preferredWidth)
				maxStretchyWidth = preferredWidth;
			if (maxStretchyHeight < preferredHeight)
				maxStretchyHeight = preferredHeight;
		}
		if (s->vertical) {
			if (*width < preferredWidth)
				*width = preferredWidth;
			if (!s->controls[i].stretchy)
				*height += preferredHeight;
		} else {
			if (!s->controls[i].stretchy)
				*width += preferredWidth;
			if (*height < preferredHeight)
				*height = preferredHeight;
		}
	}

	// 3) and now we can add in stretchy controls
	if (s->vertical)
		*height += nStretchy * maxStretchyHeight;
	else
		*width += nStretchy * maxStretchyWidth;
}

static void stackResize(uiControl *c, intmax_t x, intmax_t y, intmax_t width, intmax_t height, uiSizing *d)
{
	stack *s = (stack *) c;
	int xpadding, ypadding;
	uintmax_t nStretchy;
	intmax_t stretchywid, stretchyht;
	uintmax_t i;
	intmax_t preferredWidth, preferredHeight;

	if (s->len == 0)
		return;

	// -1) get this Stack's padding
	xpadding = 0;
	ypadding = 0;
	if (s->padded) {
		xpadding = d->xPadding;
		ypadding = d->yPadding;
	}

	// 0) inset the available rect by the needed padding
	if (s->vertical)
		height -= (s->len - 1) * ypadding;
	else
		width -= (s->len - 1) * xpadding;

	// 1) get width and height of non-stretchy controls
	// this will tell us how much space will be left for stretchy controls
	stretchywid = width;
	stretchyht = height;
	nStretchy = 0;
	for (i = 0; i < s->len; i++) {
		if (!uiControlVisible(s->controls[i].c))
			continue;
		if (s->controls[i].stretchy) {
			nStretchy++;
			continue;
		}
		uiControlPreferredSize(s->controls[i].c, d, &preferredWidth, &preferredHeight);
		if (s->vertical) {		// all controls have same width
			s->controls[i].width = width;
			s->controls[i].height = preferredHeight;
			stretchyht -= preferredHeight;
		} else {				// all controls have same height
			s->controls[i].width = preferredWidth;
			s->controls[i].height = height;
			stretchywid -= preferredWidth;
		}
	}

	// 2) now get the size of stretchy controls
	if (nStretchy != 0)
		if (s->vertical)
			stretchyht /= nStretchy;
		else
			stretchywid /= nStretchy;
	for (i = 0; i < s->len; i++) {
		if (!uiControlVisible(s->controls[i].c))
			continue;
		if (s->controls[i].stretchy) {
			s->controls[i].width = stretchywid;
			s->controls[i].height = stretchyht;
		}
	}

	// 3) now we can position controls
	for (i = 0; i < s->len; i++) {
		if (!uiControlVisible(s->controls[i].c))
			continue;
		uiControlResize(s->controls[i].c, x, y, s->controls[i].width, s->controls[i].height, d);
		if (s->vertical)
			y += s->controls[i].height + ypadding;
		else
			x += s->controls[i].width + xpadding;
	}
}

static int stackVisible(uiControl *c)
{
	stack *s = (stack *) c;

	return !(s->userHid);
}

static void stackShow(uiControl *c)
{
	stack *s = (stack *) c;
	uintmax_t i;

	s->userHid = 0;
	if (!s->containerHid) {
		for (i = 0; i < s->len; i++)
			uiControlContainerShow(s->controls[i].c);
		if (s->parent != NULL)
			uiParentUpdate(s->parent);
	}
}

static void stackHide(uiControl *c)
{
	stack *s = (stack *) c;
	uintmax_t i;

	s->userHid = 1;
	for (i = 0; i < s->len; i++)
		uiControlContainerHide(s->controls[i].c);
	if (s->parent != NULL)
		uiParentUpdate(s->parent);
}

static void stackContainerShow(uiControl *c)
{
	stack *s = (stack *) c;
	uintmax_t i;

	s->containerHid = 0;
	if (!s->userHid) {
		for (i = 0; i < s->len; i++)
			uiControlContainerShow(s->controls[i].c);
		if (s->parent != NULL)
			uiParentUpdate(s->parent);
	}
}

static void stackContainerHide(uiControl *c)
{
	stack *s = (stack *) c;
	uintmax_t i;

	s->containerHid = 1;
	for (i = 0; i < s->len; i++)
		uiControlContainerHide(s->controls[i].c);
	if (s->parent != NULL)
		uiParentUpdate(s->parent);
}

static void stackEnable(uiControl *c)
{
	stack *s = (stack *) c;
	uintmax_t i;

	s->userDisabled = 0;
	if (!s->containerDisabled)
		for (i = 0; i < s->len; i++)
			uiControlContainerEnable(s->controls[i].c);
}

static void stackDisable(uiControl *c)
{
	stack *s = (stack *) c;
	uintmax_t i;

	s->userDisabled = 1;
	for (i = 0; i < s->len; i++)
		uiControlContainerDisable(s->controls[i].c);
}

static void stackContainerEnable(uiControl *c)
{
	stack *s = (stack *) c;
	uintmax_t i;

	s->containerDisabled = 0;
	if (!s->userDisabled)
		for (i = 0; i < s->len; i++)
			uiControlContainerEnable(s->controls[i].c);
}

static void stackContainerDisable(uiControl *c)
{
	stack *s = (stack *) c;
	uintmax_t i;

	s->containerDisabled = 1;
	for (i = 0; i < s->len; i++)
		uiControlContainerDisable(s->controls[i].c);
}

#define stackCapGrow 32

static void stackAppend(uiStack *ss, uiControl *c, int stretchy)
{
	stack *s = (stack *) ss;

	if (s->len >= s->cap) {
		s->cap += stackCapGrow;
		s->controls = (stackControl *) uiRealloc(s->controls, s->cap * sizeof (stackControl), "stackControl[]");
	}
	s->controls[s->len].c = c;
	s->controls[s->len].stretchy = stretchy;
	s->len++;		// must be here for parent updates to work
	if (s->parent != NULL) {
		uiControlSetParent(s->controls[s->len - 1].c, s->parent);
		uiParentUpdate(s->parent);
	}
}

static void stackDelete(uiStack *ss, uintmax_t index)
{
	stack *s = (stack *) ss;
	uiControl *removed;
	uintmax_t i;

	removed = s->controls[index].c;
	// TODO switch to memmove?
	for (i = index; i < s->len - 1; i++)
		s->controls[i] = s->controls[i + 1];
	// TODO memset the last one to NULL
	s->len--;
	if (s->parent != NULL) {
		uiControlSetParent(removed, NULL);
		uiParentUpdate(s->parent);
	}
}

static int stackPadded(uiStack *ss)
{
	stack *s = (stack *) ss;

	return s->padded;
}

static void stackSetPadded(uiStack *ss, int padded)
{
	stack *s = (stack *) ss;

	s->padded = padded;
	if (s->parent != NULL)
		uiParentUpdate(s->parent);
}

uiStack *uiNewHorizontalStack(void)
{
	stack *s;

	s = uiNew(stack);

	uiControl(s)->Destroy = stackDestroy;
	uiControl(s)->Handle = stackHandle;
	uiControl(s)->SetParent = stackSetParent;
	uiControl(s)->PreferredSize = stackPreferredSize;
	uiControl(s)->Resize = stackResize;
	uiControl(s)->Visible = stackVisible;
	uiControl(s)->Show = stackShow;
	uiControl(s)->Hide = stackHide;
	uiControl(s)->ContainerShow = stackContainerShow;
	uiControl(s)->ContainerHide = stackContainerHide;
	uiControl(s)->Enable = stackEnable;
	uiControl(s)->Disable = stackDisable;
	uiControl(s)->ContainerEnable = stackContainerEnable;
	uiControl(s)->ContainerDisable = stackContainerDisable;

	uiStack(s)->Append = stackAppend;
	uiStack(s)->Delete = stackDelete;
	uiStack(s)->Padded = stackPadded;
	uiStack(s)->SetPadded = stackSetPadded;

	return uiStack(s);
}

uiStack *uiNewVerticalStack(void)
{
	uiStack *ss;
	stack *s;

	ss = uiNewHorizontalStack();
	s = (stack *) ss;
	s->vertical = 1;
	return ss;
}