forked from andlabs/libui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
261 lines (199 loc) · 4.86 KB
/
main.cpp
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
/* vim: set noet ts=4 sw=4 sts=4: */
// example for giving table control a workout
//
// TODO:
// delete row(s)
// editing cells
// show selected rows
// column sorting
// other column types (numbers, colours etc)
#include <cstdio>
#include <cstring>
#include <cassert>
#include <vector>
#include <string>
#include "../../ui.h"
#include "planet.h"
static uiTableModel* model=0;
static uiWindow *mainwin;
// our set of dummy data
static std::vector<Planet> db;
//
// handler funcs to expose our data via a uiTableModel
//
static int handlerNumColumns(uiTableModelHandler *h, uiTableModel *m)
{
//printf("handlerNumColumns()\n");
return 7;
}
static uiTableModelColumnType handlerColumnType(uiTableModelHandler *h, uiTableModel *m, int col)
{
switch( col) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
default:
return uiTableModelColumnString;
}
}
static int handlerNumRows(uiTableModelHandler *h, uiTableModel *m)
{
return db.size();
}
static void* handlerCellValue(uiTableModelHandler *h, uiTableModel *m, int row, int col)
{
assert(row>=0 && row<(int)db.size());
assert(col>=0 && col<7);
Planet &planet = db[row];
char buf[32];
switch (col) {
case 0:
return uiTableModelStrdup( planet.name.c_str() );
case 1:
return uiTableModelStrdup( Planet::econDesc[planet.economy] );
case 2:
return uiTableModelStrdup( Planet::govtDesc[planet.govt] );
case 3:
// TODO: investigate how to show numeric columns!
sprintf(buf,"%d", planet.techLevel);
return uiTableModelStrdup(buf);
case 4:
sprintf(buf,"%d", planet.turnover);
return uiTableModelStrdup(buf);
case 5:
sprintf(buf,"%dKm", planet.radius);
return uiTableModelStrdup(buf);
case 6:
sprintf(buf,"%dB", planet.population);
return uiTableModelStrdup(buf);
default: return NULL;
}
}
static void handlerSetCellValue(uiTableModelHandler *h, uiTableModel *m, int row, int col, const void *val)
{
// printf("handlerSetCell(row %d, col %d)\n", row,col);
}
static uiTableModelHandler handler = {
handlerNumColumns,
handlerColumnType,
handlerNumRows,
handlerCellValue,
handlerSetCellValue
};
//
// event handlers
//
static int onClosing(uiWindow *w, void *data)
{
uiQuit();
return 1;
}
static int onShouldQuit(void *data)
{
uiWindow *mainwin = uiWindow(data);
uiControlDestroy(uiControl(mainwin));
return 1;
}
static void onSelectionChanged(uiTable *t, void *data)
{
printf("Selection changed!\n");
uiTableIter *sel = uiTableGetSelection(t);
while (uiTableIterAdvance(sel)) {
printf("%d ", uiTableIterCurrent(sel));
}
uiTableIterComplete(sel);
printf("\n");
// uiTableDumpSelection(t);
}
static void onAddEntries(uiButton *, void *data)
{
uiSpinbox* entry = (uiSpinbox*)data;
int cnt = uiSpinboxValue(entry);
printf("add %d\n", cnt);
Planet p;
int i;
for( i=0; i<cnt; ++i) {
int idx = (int)db.size();
p.Randomise();
db.push_back( p );
uiTableModelRowInserted(model,idx);
}
}
//
// build gui
//
static uiControl *makeTable(void)
{
uiTable* t;
model = uiNewTableModel(&handler);
t = uiNewTable(model, uiTableStyleMultiSelect);
uiTableAppendTextColumn(t, "Name", 0);
uiTableAppendTextColumn(t, "Economy", 1);
uiTableAppendTextColumn(t, "Government", 2);
uiTableAppendTextColumn(t, "Tech Level", 3);
uiTableAppendTextColumn(t, "Turnover", 4);
uiTableAppendTextColumn(t, "Radius", 5);
uiTableAppendTextColumn(t, "Population", 6);
uiTableOnSelectionChanged(t, onSelectionChanged, NULL);
return uiControl(t);
}
static uiControl *makeLayout(void)
{
uiBox *vbox;
uiSpinbox *entry;
vbox = uiNewVerticalBox();
uiBoxSetPadded(vbox,1);
uiBoxAppend(vbox,makeTable(),1);
{
uiGroup* group = uiNewGroup("Add Entries");
uiGroupSetMargined(group, 1);
{
uiBox* hbox = uiNewHorizontalBox();
uiBoxSetPadded(hbox,1);
uiBoxAppend(hbox, uiControl(uiNewLabel("number of entries")), 0);
entry = uiNewSpinbox(1,100000);
uiSpinboxSetValue(entry, 10);
uiBoxAppend(hbox, uiControl(entry),0);
uiButton* addButton = uiNewButton("Add");
uiButtonOnClicked(addButton, onAddEntries, entry);
uiBoxAppend(hbox, uiControl(addButton),0);
uiGroupSetChild(group, uiControl(hbox));
}
uiBoxAppend(vbox, uiControl(group), 0);
}
return uiControl(vbox);
}
int main(void)
{
#ifdef _WIN32
setbuf(stdout, NULL);
#endif
uiInitOptions options;
const char *err;
memset(&options, 0, sizeof (uiInitOptions));
err = uiInit(&options);
if (err != NULL) {
fprintf(stderr, "error initializing libui: %s", err);
uiFreeInitError(err);
return 1;
}
Planet p;
int i;
for( i=0; i<5; ++i) {
p.Randomise();
db.push_back( p );
}
mainwin = uiNewWindow("libui Table Example", 640, 480, 1);
uiWindowOnClosing(mainwin, onClosing, NULL);
uiOnShouldQuit(onShouldQuit, mainwin);
uiControl* layout = makeLayout();
uiWindowSetChild(mainwin, layout);
uiWindowSetMargined(mainwin, 1);
uiControlShow(uiControl(mainwin));
uiMain();
return 0;
}