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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
|
#include <slang.h>
#include <stdlib.h>
#include <string.h>
#include "newt.h"
#include "newt_pr.h"
struct items {
char * text;
const void *data;
unsigned char selected;
struct items *next;
struct items *prev;
struct items *branch;
int flags;
int depth;
};
struct CheckboxTree {
newtComponent sb;
int curWidth; /* size of text w/o scrollbar or border*/
int curHeight; /* size of text w/o border */
struct items * itemlist;
struct items ** flatList, ** currItem, ** firstItem;
int flatCount;
int flags;
int pad;
char * seq;
char * result;
};
static void ctDraw(newtComponent c);
static void ctDestroy(newtComponent co);
static void ctPlace(newtComponent co, int newLeft, int newTop);
struct eventResult ctEvent(newtComponent co, struct event ev);
static void ctMapped(newtComponent co, int isMapped);
static struct items * findItem(struct items * items, const void * data);
static void buildFlatList(newtComponent co);
static void doBuildFlatList(struct CheckboxTree * ct, struct items * item);
enum countWhat { COUNT_EXPOSED=0, COUNT_SELECTED=1 };
static int countItems(struct items * item, enum countWhat justExposed);
static struct componentOps ctOps = {
ctDraw,
ctEvent,
ctDestroy,
ctPlace,
ctMapped,
} ;
static int countItems(struct items * item, enum countWhat what) {
int count = 0;
while (item) {
if ((!item->branch && item->selected == what) || (what == COUNT_EXPOSED))
count++;
if (item->branch || (what == COUNT_EXPOSED && item->selected))
count += countItems(item->branch, what);
item = item->next;
}
return count;
}
static void doBuildFlatList(struct CheckboxTree * ct, struct items * item) {
while (item) {
ct->flatList[ct->flatCount++] = item;
if (item->branch && item->selected) doBuildFlatList(ct, item->branch);
item = item->next;
}
}
static void buildFlatList(newtComponent co) {
struct CheckboxTree * ct = co->data;
if (ct->flatList) free(ct->flatList);
ct->flatCount = countItems(ct->itemlist, COUNT_EXPOSED);
ct->flatList = malloc(sizeof(*ct->flatList) * (ct->flatCount+1));
ct->flatCount = 0;
doBuildFlatList(ct, ct->itemlist);
ct->flatList[ct->flatCount] = NULL;
}
int newtCheckboxTreeAddItem(newtComponent co,
const char * text, const void * data,
int flags, int index, ...) {
va_list argList;
int numIndexes;
int * indexes;
int i;
va_start(argList, index);
numIndexes = 0;
i = index;
while (i != NEWT_ARG_LAST) {
numIndexes++;
i = va_arg(argList, int);
}
va_end(argList);
indexes = alloca(sizeof(*indexes) * (numIndexes + 1));
va_start(argList, index);
numIndexes = 0;
i = index;
va_start(argList, index);
while (i != NEWT_ARG_LAST) {
indexes[numIndexes++] = i;
i = va_arg(argList, int);
}
va_end(argList);
indexes[numIndexes++] = NEWT_ARG_LAST;
return newtCheckboxTreeAddArray(co, text, data, flags, indexes);
}
static int doFindItemPath(struct items * items, void * data, int * path,
int * len) {
int where = 0;
while (items) {
if (items->data == data) {
if (path) path[items->depth] = where;
if (len) *len = items->depth + 1;
return 1;
}
if (items->branch && doFindItemPath(items->branch, data, path, len)) {
if (path) path[items->depth] = where;
return 1;
}
items = items->next;
where++;
}
return 0;
}
int * newtCheckboxTreeFindItem(newtComponent co, void * data) {
int len;
int * path;
struct CheckboxTree * ct = co->data;
if (!doFindItemPath(ct->itemlist, data, NULL, &len)) return NULL;
path = malloc(sizeof(*path) * (len + 1));
doFindItemPath(ct->itemlist, data, path, NULL);
path[len] = NEWT_ARG_LAST;
return path;
}
int newtCheckboxTreeAddArray(newtComponent co,
const char * text, const void * data,
int flags, int * indexes) {
struct items * curList, * newNode, * item = NULL;
struct items ** listPtr = NULL;
int i, index, numIndexes;
struct CheckboxTree * ct = co->data;
numIndexes = 0;
while (indexes[numIndexes] != NEWT_ARG_LAST) numIndexes++;
if (!ct->itemlist) {
if (numIndexes > 1) return -1;
ct->itemlist = malloc(sizeof(*ct->itemlist));
item = ct->itemlist;
item->prev = NULL;
item->next = NULL;
} else {
curList = ct->itemlist;
listPtr = &ct->itemlist;
i = 0;
index = indexes[i];
while (i < numIndexes) {
item = curList;
if (index == NEWT_ARG_APPEND) {
item = NULL;
} else {
while (index && item)
item = item->next, index--;
}
i++;
if (i < numIndexes) {
curList = item->branch;
listPtr = &item->branch;
if (!curList && (i + 1 != numIndexes)) return -1;
index = indexes[i];
}
}
if (!curList) { /* create a new branch */
item = malloc(sizeof(*curList->prev));
item->next = item->prev = NULL;
*listPtr = item;
} else if (!item) { /* append to end */
item = curList;
while (item->next) item = item->next;
item->next = malloc(sizeof(*curList->prev));
item->next->prev = item;
item = item->next;
item->next = NULL;
} else {
newNode = malloc(sizeof(*newNode));
newNode->prev = item->prev;
newNode->next = item;
if (item->prev) item->prev->next = newNode;
item->prev = newNode;
item = newNode;
if (!item->prev) *listPtr = item;
}
}
item->text = strdup(text);
item->data = data;
if (flags & NEWT_FLAG_SELECTED) {
item->selected = 1;
} else {
item->selected = 0;
}
item->flags = flags;
item->branch = NULL;
item->depth = numIndexes - 1;
i = 4 + (3 * item->depth);
if ((strlen(text) + i + ct->pad) > co->width) {
co->width = strlen(text) + i + ct->pad;
}
return 0;
}
static struct items * findItem(struct items * items, const void * data) {
struct items * i;
while (items) {
if (items->data == data) return items;
if (items->branch) {
i = findItem(items->branch, data);
if (i) return i;
}
items = items->next;
}
return NULL;
}
static void listSelected(struct items * items, int * num, const void ** list, int seqindex) {
while (items) {
if ((seqindex ? items->selected==seqindex : items->selected) && !items->branch)
list[(*num)++] = (void *) items->data;
if (items->branch)
listSelected(items->branch, num, list, seqindex);
items = items->next;
}
}
const void ** newtCheckboxTreeGetSelection(newtComponent co, int *numitems)
{
return newtCheckboxTreeGetMultiSelection(co, numitems, 0);
}
const void ** newtCheckboxTreeGetMultiSelection(newtComponent co, int *numitems, char seqnum)
{
struct CheckboxTree * ct;
const void **retval;
int seqindex=0;
if(!co || !numitems) return NULL;
ct = co->data;
if (seqnum) {
while( ct->seq[seqindex] && ( ct->seq[seqindex] != seqnum )) seqindex++;
} else {
seqindex = 0;
}
*numitems = countItems(ct->itemlist, (seqindex ? seqindex : COUNT_SELECTED));
if (!*numitems) return NULL;
retval = malloc(*numitems * sizeof(void *));
*numitems = 0;
listSelected(ct->itemlist, numitems, retval, seqindex);
return retval;
}
newtComponent newtCheckboxTree(int left, int top, int height, int flags) {
return newtCheckboxTreeMulti(left, top, height, NULL, flags);
}
newtComponent newtCheckboxTreeMulti(int left, int top, int height, char *seq, int flags) {
newtComponent co;
struct CheckboxTree * ct;
co = malloc(sizeof(*co));
ct = malloc(sizeof(struct CheckboxTree));
co->callback = NULL;
co->data = ct;
co->ops = &ctOps;
co->takesFocus = 1;
co->height = height;
co->width = 0;
co->isMapped = 0;
ct->itemlist = NULL;
ct->firstItem = NULL;
ct->currItem = NULL;
ct->flatList = NULL;
if (seq)
ct->seq = strdup(seq);
else
ct->seq = strdup(" *");
if (flags & NEWT_FLAG_SCROLL) {
ct->sb = newtVerticalScrollbar(left, top, height,
COLORSET_LISTBOX, COLORSET_ACTLISTBOX);
ct->pad = 2;
} else {
ct->sb = NULL;
ct->pad = 0;
}
return co;
}
static void ctMapped(newtComponent co, int isMapped) {
struct CheckboxTree * ct = co->data;
co->isMapped = isMapped;
if (ct->sb)
ct->sb->ops->mapped(ct->sb, isMapped);
}
static void ctPlace(newtComponent co, int newLeft, int newTop) {
struct CheckboxTree * ct = co->data;
co->top = newTop;
co->left = newLeft;
if (ct->sb)
ct->sb->ops->place(ct->sb, co->left + co->width - 1, co->top);
}
int ctSetItem(newtComponent co, struct items *item, enum newtFlagsSense sense)
{
struct CheckboxTree * ct = co->data;
struct items * currItem;
struct items * firstItem;
if (!item)
return 1;
switch(sense) {
case NEWT_FLAGS_RESET:
item->selected = 0;
break;
case NEWT_FLAGS_SET:
item->selected = 1;
break;
case NEWT_FLAGS_TOGGLE:
if (item->branch)
item->selected = !item->selected;
else {
item->selected++;
if (item->selected==strlen(ct->seq))
item->selected = 0;
}
break;
}
if (item->branch) {
currItem = *ct->currItem;
firstItem = *ct->firstItem;
buildFlatList(co);
ct->currItem = ct->flatList;
while (*ct->currItem != currItem) ct->currItem++;
ct->firstItem = ct->flatList;
if (ct->flatCount > co->height) {
struct items ** last = ct->flatList + ct->flatCount - co->height;
while (*ct->firstItem != firstItem && ct->firstItem != last)
ct->firstItem++;
}
}
return 0;
}
static void ctSetItems(struct items *item, int selected)
{
for (; item; item = item->next) {
if (!item->branch)
item->selected = selected;
else
ctSetItems(item->branch, selected);
}
}
static void ctDraw(newtComponent co) {
struct CheckboxTree * ct = co->data;
struct items ** item;
int i, j;
char * spaces = NULL;
int currRow = -1;
if (!co->isMapped) return ;
if (!ct->firstItem) {
buildFlatList(co);
ct->firstItem = ct->currItem = ct->flatList;
}
item = ct->firstItem;
i = 0;
while (*item && i < co->height) {
newtGotorc(co->top + i, co->left);
if (*item == *ct->currItem) {
SLsmg_set_color(NEWT_COLORSET_ACTLISTBOX);
currRow = co->top + i;
} else
SLsmg_set_color(NEWT_COLORSET_LISTBOX);
for (j = 0; j < (*item)->depth; j++)
SLsmg_write_string(" ");
if ((*item)->branch) {
if ((*item)->selected)
SLsmg_write_string("<-> ");
else
SLsmg_write_string("<+> ");
} else {
char tmp[5];
snprintf(tmp,5,"[%c] ",ct->seq[(*item)->selected]);
SLsmg_write_string(tmp);
}
SLsmg_write_nstring((*item)->text, co->width - 4 -
(3 * (*item)->depth));
item++;
i++;
}
/* There could be empty lines left (i.e. if the user closes an expanded
list which is the last thing in the tree, and whose elements are
displayed at the bottom of the screen */
if (i < co->height) {
spaces = alloca(co->width);
memset(spaces, ' ', co->width);
SLsmg_set_color(NEWT_COLORSET_LISTBOX);
}
while (i < co->height) {
newtGotorc(co->top + i, co->left);
SLsmg_write_nstring(spaces, co->width);
i++;
}
if(ct->sb) {
newtScrollbarSet(ct->sb, ct->currItem - ct->flatList,
ct->flatCount - 1);
ct->sb->ops->draw(ct->sb);
}
newtGotorc(currRow, co->left + 1);
}
static void ctDestroy(newtComponent co) {
struct CheckboxTree * ct = co->data;
struct items * item, * nextitem;
nextitem = item = ct->itemlist;
while (item != NULL) {
nextitem = item->next;
free(item->text);
free(item);
item = nextitem;
}
free(ct->seq);
free(ct);
free(co);
}
struct eventResult ctEvent(newtComponent co, struct event ev) {
struct CheckboxTree * ct = co->data;
struct eventResult er;
struct items ** listEnd, ** lastItem;
int key, selnum = 1;
er.result = ER_IGNORED;
if(ev.when == EV_EARLY || ev.when == EV_LATE) {
return er;
}
switch(ev.event) {
case EV_KEYPRESS:
key = ev.u.key;
if (key == (char) key && key != ' ') {
for (selnum = 0; ct->seq[selnum]; selnum++)
if (key == ct->seq[selnum])
break;
if (!ct->seq[selnum])
switch (key) {
case '-': selnum = 0; break;
case '+':
case '*': selnum = 1; break;
}
if (ct->seq[selnum])
key = '*';
}
switch(key) {
case ' ':
case NEWT_KEY_ENTER:
ctSetItem(co, *ct->currItem, NEWT_FLAGS_TOGGLE);
er.result = ER_SWALLOWED;
if (!(*ct->currItem)->branch || (*ct->currItem)->selected)
key = NEWT_KEY_DOWN;
else
key = '*';
break;
case '*':
if ((*ct->currItem)->branch) {
ctSetItems((*ct->currItem)->branch, selnum);
if (!(*ct->currItem)->selected)
key = NEWT_KEY_DOWN;
} else {
(*ct->currItem)->selected = selnum;
key = NEWT_KEY_DOWN;
}
er.result = ER_SWALLOWED;
break;
}
switch (key) {
case '*':
ctDraw(co);
if(co->callback) co->callback(co, co->callbackData);
return er;
case NEWT_KEY_HOME:
ct->currItem = ct->flatList;
ct->firstItem = ct->flatList;
ctDraw(co);
if(co->callback) co->callback(co, co->callbackData);
er.result = ER_SWALLOWED;
return er;
case NEWT_KEY_END:
ct->currItem = ct->flatList + ct->flatCount - 1;
if (ct->flatCount <= co->height)
ct->firstItem = ct->flatList;
else
ct->firstItem = ct->flatList + ct->flatCount - co->height;
ctDraw(co);
if(co->callback) co->callback(co, co->callbackData);
er.result = ER_SWALLOWED;
return er;
case NEWT_KEY_DOWN:
if (ev.u.key != NEWT_KEY_DOWN) {
if(co->callback) co->callback(co, co->callbackData);
if (strlen(ct->seq) != 2) {
ctDraw(co);
return er;
}
}
if ((ct->currItem - ct->flatList + 1) < ct->flatCount) {
ct->currItem++;
if (ct->currItem - ct->firstItem >= co->height)
ct->firstItem++;
ctDraw(co);
} else if (ev.u.key != NEWT_KEY_DOWN)
ctDraw(co);
if(co->callback) co->callback(co, co->callbackData);
er.result = ER_SWALLOWED;
return er;
case NEWT_KEY_UP:
if (ct->currItem != ct->flatList) {
ct->currItem--;
if (ct->currItem < ct->firstItem)
ct->firstItem = ct->currItem;
ctDraw(co);
}
er.result = ER_SWALLOWED;
if(co->callback) co->callback(co, co->callbackData);
return er;
case NEWT_KEY_PGUP:
if (ct->firstItem - co->height < ct->flatList) {
ct->firstItem = ct->currItem = ct->flatList;
} else {
ct->currItem -= co->height;
ct->firstItem -= co->height;
}
ctDraw(co);
if(co->callback) co->callback(co, co->callbackData);
er.result = ER_SWALLOWED;
return er;
case NEWT_KEY_PGDN:
listEnd = ct->flatList + ct->flatCount - 1;
lastItem = ct->firstItem + co->height - 1;
if (lastItem + co->height > listEnd) {
ct->firstItem = listEnd - co->height + 1;
ct->currItem = listEnd;
} else {
ct->currItem += co->height;
ct->firstItem += co->height;
}
ctDraw(co);
if(co->callback) co->callback(co, co->callbackData);
er.result = ER_SWALLOWED;
return er;
}
break;
case EV_FOCUS:
ctDraw(co);
er.result = ER_SWALLOWED;
break;
case EV_UNFOCUS:
ctDraw(co);
er.result = ER_SWALLOWED;
break;
default:
break;
}
return er;
}
const void * newtCheckboxTreeGetCurrent(newtComponent co) {
struct CheckboxTree * ct = co->data;
if (!ct->currItem) return NULL;
return (*ct->currItem)->data;
}
void newtCheckboxTreeSetEntry(newtComponent co, const void * data, const char * text)
{
struct CheckboxTree * ct;
struct items * item;
int i;
if (!co) return;
ct = co->data;
item = findItem(ct->itemlist, data);
if (!item) return;
free(item->text);
item->text = strdup(text);
i = 4 + (3 * item->depth);
if ((strlen(text) + i + ct->pad) > co->width) {
co->width = strlen(text) + i + ct->pad;
}
ctDraw(co);
}
char newtCheckboxTreeGetEntryValue(newtComponent co, const void * data)
{
struct CheckboxTree * ct;
struct items * item;
if (!co) return -1;
ct = co->data;
item = findItem(ct->itemlist, data);
if (!item) return -1;
if (item->branch)
return item->selected ? NEWT_CHECKBOXTREE_EXPANDED : NEWT_CHECKBOXTREE_COLLAPSED;
else
return ct->seq[item->selected];
}
void newtCheckboxTreeSetEntryValue(newtComponent co, const void * data, char value)
{
struct CheckboxTree * ct;
struct items * item;
int i;
if (!co) return;
ct = co->data;
item = findItem(ct->itemlist, data);
if (!item || item->branch) return;
for(i = 0; ct->seq[i]; i++)
if (value == ct->seq[i])
break;
if (!ct->seq[i]) return;
item->selected = i;
ctDraw(co);
}
|