summaryrefslogtreecommitdiff
path: root/new/test.c
blob: 978cb551c3eaa4de9f57298a165e2e829c723af5 (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
// 6 april 2015
#include "ui.h"
#include <stdio.h>
#include <string.h>

int onClosing(uiWindow *w, void *data)
{
	printf("in closing!\n");
	uiQuit();
	return 1;
}

uiControl *e;

static void getWindowText(uiControl *b, void *data)
{
	char *text;

	text = uiWindowTitle((uiWindow *) data);
	uiEntrySetText(e, text);
	uiFreeText(text);
}

static void setWindowText(uiControl *b, void *data)
{
	char *text;

	text = uiEntryText(e);
	uiWindowSetTitle((uiWindow *) data, text);
	uiFreeText(text);
}

static void getButtonText(uiControl *b, void *data)
{
	char *text;

	text = uiButtonText((uiControl *) data);
	uiEntrySetText(e, text);
	uiFreeText(text);
}

static void setButtonText(uiControl *b, void *data)
{
	char *text;

	text = uiEntryText(e);
	uiButtonSetText((uiControl *) data, text);
	uiFreeText(text);
}

static void getCheckboxText(uiControl *b, void *data)
{
	char *text;

	text = uiCheckboxText((uiControl *) data);
	uiEntrySetText(e, text);
	uiFreeText(text);
}

static void setCheckboxText(uiControl *b, void *data)
{
	char *text;

	text = uiEntryText(e);
	uiCheckboxSetText((uiControl *) data, text);
	uiFreeText(text);
}

uiWindow *w;
#define nStacks 11
uiControl *stacks[nStacks];
uiControl *spaced;

static void setSpaced(int spaced)
{
	int i;

	uiWindowSetMargined(w, spaced);
	for (i = 0; i < nStacks; i++)
		uiStackSetPadded(stacks[i], spaced);
}

static void toggleSpaced(uiControl *c, void *data)
{
	int s = uiCheckboxChecked(spaced);

	printf("toggled %d\n", s);
	setSpaced(s);
}

// these will also be used to test if setting checks will trigger events
static void forceSpacedOn(uiControl *c, void *data)
{
	uiCheckboxSetChecked(spaced, 1);
}

static void forceSpacedOff(uiControl *c, void *data)
{
	uiCheckboxSetChecked(spaced, 0);
}

static void showSpaced(uiControl *c, void *data)
{
	char msg[] = { 'm', ' ', '0', ' ', 'p', ' ', '0', '\0' };

	if (uiWindowMargined(w))
		msg[2] = '1';
	if (uiStackPadded(stacks[0]))
		msg[6] = '1';
	uiEntrySetText(e, msg);
}

static void showControl(uiControl *c, void *data)
{
	uiControlShow((uiControl *) data);
}

static void hideControl(uiControl *c, void *data)
{
	uiControlHide((uiControl *) data);
}

static void enableControl(uiControl *c, void *data)
{
	uiControlEnable((uiControl *) data);
}

static void disableControl(uiControl *c, void *data)
{
	uiControlDisable((uiControl *) data);
}

static void getLabelText(uiControl *b, void *data)
{
	char *text;

	text = uiLabelText((uiControl *) data);
	uiEntrySetText(e, text);
	uiFreeText(text);
}

static void setLabelText(uiControl *b, void *data)
{
	char *text;

	text = uiEntryText(e);
	uiLabelSetText((uiControl *) data, text);
	uiFreeText(text);
}

uiControl *firstStack;
uiControl *secondStack;
uiControl *movingLabel;

static void moveToFirst(uiControl *c, void *data)
{
	uiStackRemove(secondStack, 1);
	uiStackAdd(firstStack, movingLabel, 1);
}

static void moveToSecond(uiControl *c, void *data)
{
	uiStackRemove(firstStack, 1);
	uiStackAdd(secondStack, movingLabel, 1);
}

int main(int argc, char *argv[])
{
	uiInitOptions o;
	int i;
	const char *err;
	uiControl *getButton, *setButton;
	uiControl *label;
	uiControl *tab;
	int page2stack;

	memset(&o, 0, sizeof (uiInitOptions));
	for (i = 1; i < argc; i++)
		if (strcmp(argv[i], "leaks") == 0)
			o.debugLogAllocations = 1;
		else {
			fprintf(stderr, "%s: unrecognized option %s\n", argv[0], argv[i]);
			return 1;
		}

	err = uiInit(&o);
	if (err != NULL) {
		fprintf(stderr, "error initializing ui: %s\n", err);
		uiFreeInitError(err);
		return 1;
	}

	w = uiNewWindow("Hello", 320, 240);
	uiWindowOnClosing(w, onClosing, NULL);

	stacks[0] = uiNewVerticalStack();

	e = uiNewEntry();
	uiStackAdd(stacks[0], e, 0);

	i = 1;

	stacks[i] = uiNewHorizontalStack();
	getButton = uiNewButton("Get Window Text");
	uiButtonOnClicked(getButton, getWindowText, w);
	setButton = uiNewButton("Set Window Text");
	uiButtonOnClicked(setButton, setWindowText, w);
	uiStackAdd(stacks[i], getButton, 1);
	uiStackAdd(stacks[i], setButton, 1);
	uiStackAdd(stacks[0], stacks[i], 0);
	i++;

	stacks[i] = uiNewHorizontalStack();
	getButton = uiNewButton("Get Button Text");
	uiButtonOnClicked(getButton, getButtonText, getButton);
	setButton = uiNewButton("Set Button Text");
	uiButtonOnClicked(setButton, setButtonText, getButton);
	uiStackAdd(stacks[i], getButton, 1);
	uiStackAdd(stacks[i], setButton, 1);
	uiStackAdd(stacks[0], stacks[i], 0);
	i++;

	// this will also be used to make sure tab stops work properly when inserted out of creation order, especially on Windows
	spaced = uiNewCheckbox("Spaced");
	uiCheckboxOnToggled(spaced, toggleSpaced, NULL);

	stacks[i] = uiNewHorizontalStack();
	getButton = uiNewButton("Get Checkbox Text");
	uiButtonOnClicked(getButton, getCheckboxText, spaced);
	setButton = uiNewButton("Set Checkbox Text");
	uiButtonOnClicked(setButton, setCheckboxText, spaced);
	uiStackAdd(stacks[i], getButton, 1);
	uiStackAdd(stacks[i], setButton, 1);
	uiStackAdd(stacks[0], stacks[i], 0);
	i++;

	label = uiNewLabel("Label");

	stacks[i] = uiNewHorizontalStack();
	getButton = uiNewButton("Get Label Text");
	uiButtonOnClicked(getButton, getLabelText, label);
	setButton = uiNewButton("Set Label Text");
	uiButtonOnClicked(setButton, setLabelText, label);
	uiStackAdd(stacks[i], getButton, 1);
	uiStackAdd(stacks[i], setButton, 1);
	uiStackAdd(stacks[0], stacks[i], 0);
	i++;

	stacks[i] = uiNewHorizontalStack();
	uiStackAdd(stacks[i], spaced, 1);
	getButton = uiNewButton("On");
	uiButtonOnClicked(getButton, forceSpacedOn, NULL);
	setButton = uiNewButton("Off");
	uiButtonOnClicked(setButton, forceSpacedOff, NULL);
	uiStackAdd(stacks[i], getButton, 0);
	uiStackAdd(stacks[i], setButton, 0);
	setButton = uiNewButton("Show");
	uiButtonOnClicked(setButton, showSpaced, NULL);
	uiStackAdd(stacks[i], setButton, 0);
	uiStackAdd(stacks[0], stacks[i], 0);
	i++;

	stacks[i] = uiNewHorizontalStack();
	getButton = uiNewButton("Button");
	uiStackAdd(stacks[i], getButton, 1);
	setButton = uiNewButton("Show");
	uiButtonOnClicked(setButton, showControl, getButton);
	uiStackAdd(stacks[i], setButton, 0);
	setButton = uiNewButton("Hide");
	uiButtonOnClicked(setButton, hideControl, getButton);
	uiStackAdd(stacks[i], setButton, 0);
	setButton = uiNewButton("Enable");
	uiButtonOnClicked(setButton, enableControl, getButton);
	uiStackAdd(stacks[i], setButton, 0);
	setButton = uiNewButton("Disable");
	uiButtonOnClicked(setButton, disableControl, getButton);
	uiStackAdd(stacks[i], setButton, 0);
	uiStackAdd(stacks[0], stacks[i], 0);
	i++;

	stacks[i] = uiNewHorizontalStack();
	setButton = uiNewButton("Show Stack");
	uiButtonOnClicked(setButton, showControl, stacks[i - 1]);
	uiStackAdd(stacks[i], setButton, 1);
	setButton = uiNewButton("Hide Stack");
	uiButtonOnClicked(setButton, hideControl, stacks[i - 1]);
	uiStackAdd(stacks[i], setButton, 1);
	setButton = uiNewButton("Enable Stack");
	uiButtonOnClicked(setButton, enableControl, stacks[i - 1]);
	uiStackAdd(stacks[i], setButton, 1);
	setButton = uiNewButton("Disable Stack");
	uiButtonOnClicked(setButton, disableControl, stacks[i - 1]);
	uiStackAdd(stacks[i], setButton, 1);
	uiStackAdd(stacks[0], stacks[i], 0);
	i++;

	uiStackAdd(stacks[0], label, 0);

	tab = uiNewTab();
	uiWindowSetChild(w, tab);
	uiTabAddPage(tab, "Page 1", stacks[0]);

	page2stack = i;
	stacks[i] = uiNewVerticalStack();
	uiTabAddPage(tab, "Page 2", stacks[i]);
	i++;

	stacks[i] = uiNewHorizontalStack();
	firstStack = stacks[i];
	getButton = uiNewButton("Move Here");
	uiButtonOnClicked(getButton, moveToFirst, NULL);
	uiStackAdd(stacks[i], getButton, 0);
	movingLabel = uiNewLabel("This label moves!");
	uiStackAdd(stacks[i], movingLabel, 1);
	uiStackAdd(stacks[page2stack], stacks[i], 0);;
	i++;

	stacks[i] = uiNewHorizontalStack();
	secondStack = stacks[i];
	getButton = uiNewButton("Move Here");
	uiButtonOnClicked(getButton, moveToSecond, NULL);
	uiStackAdd(stacks[i], getButton, 0);
	uiStackAdd(stacks[page2stack], stacks[i], 0);
	i++;

	if (i != nStacks) {
		fprintf(stderr, "forgot to update nStacks (expected %d)\n", i);
		return 1;
	}
	uiWindowShow(w);
	uiMain();
	printf("after uiMain()\n");
	return 0;
}