-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwidgets.h
More file actions
1385 lines (1096 loc) · 31.5 KB
/
widgets.h
File metadata and controls
1385 lines (1096 loc) · 31.5 KB
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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef WIDGETS_H
#define WIDGETS_H
#ifdef WIDGETS_TESTS
#undef NDEBUG
#define TB_IMPL
#define STB_DS_IMPLEMENTATION
#ifndef WIDGETS_IMPL
#define WIDGETS_IMPL
#endif /* !WIDGETS_IMPL */
#endif /* !WIDGETS_TESTS */
#include "termbox.h"
#include <stdbool.h>
enum { WIDGET_CH_MAX = 2 }; /* Max width. */
enum widget_error { WIDGET_NOOP = 0, WIDGET_REDRAW };
/* The rectangle in which the widget will be drawn. */
struct widget_points {
int x1; /* x of top-left corner. */
int x2; /* x of bottom-right corner. */
int y1; /* y of top-left corner. */
int y2; /* y of bottom-right corner. */
};
uint32_t
widget_uc_sanitize(uint32_t uc, int *width);
int
widget_str_width(const char *str);
bool
widget_points_in_bounds(const struct widget_points *points, int x, int y);
void
widget_points_set(struct widget_points *points, int x1, int x2, int y1, int y2);
bool
widget_should_forcebreak(int width);
/* Checks whether adding a character of width 'width' would overflow the
* screen. Returns false even if x + width == max_width as that would be the
* index where new characters would be printed, not the current one. */
bool
widget_should_scroll(int x, int width, int max_width);
/* Resets x to points->x1 and increments y if adding a character of width
* 'width' would overflow the screen and return true, else false. */
bool
widget_advance_xy_if_scroll(
int *x, int *y, struct widget_points *points, int width);
int
widget_print_str(
int x, int y, int max_x, uintattr_t fg, uintattr_t bg, const char *str);
int
widget_pad_center(int part, int total);
/* Border */
void
border_redraw(struct widget_points *points, uintattr_t fg, uintattr_t bg);
/* Input. */
enum input_event {
INPUT_CLEAR = 0,
INPUT_DELETE,
INPUT_DELETE_WORD,
INPUT_RIGHT,
INPUT_RIGHT_WORD,
INPUT_LEFT,
INPUT_LEFT_WORD,
INPUT_ADD /* Must pass an uint32_t argument. */
};
struct input {
bool scroll_horizontal;
int start_y;
uintattr_t bg;
size_t cur_buf; /* Current position inside buf. */
uint32_t *buf; /* We use a basic array instead of something like a linked
* list of small arrays or a gap buffer as pretty much all
* messages are small enough that array
* insertion / deletion performance isn't an issue. */
};
int
input_init(struct input *input, uintattr_t bg, bool scroll_horizontal);
void
input_finish(struct input *input);
/* rows will be filled with the number of rows taken by the input field. */
void
input_redraw(
struct input *input, struct widget_points *points, int *rows, bool dry_run);
enum widget_error
input_handle_event(struct input *input, enum input_event event, ...);
char *
input_buf(struct input *input);
/* Treeview. */
/* Called to draw the data. */
typedef void (*treeview_draw_cb)(
void *data, struct widget_points *points, bool is_selected);
enum treeview_event {
TREEVIEW_EXPAND = 0,
TREEVIEW_UP,
TREEVIEW_DOWN,
/* Must pass a treeview_node struct pointer for INSERT*
* If WIDGET_NOOP is returned here then the operation was invalid and the
* passed node should be freed to avoid leaks. */
TREEVIEW_INSERT, /* Add a child node to the selected node. */
TREEVIEW_INSERT_PARENT, /* Add a node to the selected node's parent. */
/* Jump to a node, pass a pointer to a treeview struct parent->nodes[index]
*/
TREEVIEW_JUMP,
TREEVIEW_DELETE, /* Delete the selected node along with it's children. The
root node cannot be deleted. */
};
struct treeview_node {
bool is_expanded; /* Whether it's children are visible. */
size_t index; /* Index in the nodes array. */
struct treeview_node *parent;
struct treeview_node **nodes;
void *data; /* Any user data. */
treeview_draw_cb draw_cb;
};
struct treeview {
int skipped; /* A hack used to skip lines in recursive rendering. */
int start_y;
struct treeview_node root;
struct treeview_node *selected;
};
struct treeview_node *
treeview_node_alloc(void *data, treeview_draw_cb draw_cb);
int
treeview_node_init(
struct treeview_node *node, void *data, treeview_draw_cb draw_cb);
void
treeview_node_destroy(struct treeview_node *node);
void
treeview_node_finish(struct treeview_node *node);
int
treeview_node_add_child(
struct treeview_node *parent, struct treeview_node *child);
int
treeview_init(struct treeview *treeview);
void
treeview_finish(struct treeview *treeview);
void
treeview_redraw(struct treeview *treeview, struct widget_points *points);
enum widget_error
treeview_event(struct treeview *treeview, enum treeview_event event, ...);
#endif /* !WIDGETS_H */
#ifdef WIDGETS_IMPL
#include "stb_ds.h"
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include <wctype.h>
static int
min(int x, int y) {
return x < y ? x : y;
}
static int
max(int x, int y) {
return x > y ? x : y;
}
uint32_t
widget_uc_sanitize(uint32_t uc, int *width) {
int tmp_width = wcwidth((wchar_t) uc);
switch (uc) {
case '\n':
*width = 0;
return uc;
case '\t':
*width = 1;
return ' ';
default:
if (tmp_width < 0) {
*width = 1;
return uc;
}
if (tmp_width == 0 || tmp_width > WIDGET_CH_MAX) {
*width = wcwidth(L'�');
return L'�';
}
*width = tmp_width;
return uc;
}
}
int
widget_str_width(const char *str) {
int width = 0;
if (str) {
for (size_t i = 0; str[i];) {
int ch_width = 0;
uint32_t uc = 0;
int len = tb_utf8_char_to_unicode(&uc, &str[i]);
if (len == TB_ERR) {
break;
}
widget_uc_sanitize(uc, &ch_width);
i += (size_t) len;
width += ch_width;
}
}
return width;
}
bool
widget_points_in_bounds(const struct widget_points *points, int x, int y) {
return (
x >= points->x1 && x < points->x2 && y >= points->y1 && y < points->y2);
}
/* Ensure that no point is negative or out of bounds. */
void
widget_points_set(
struct widget_points *points, int x1, int x2, int y1, int y2) {
if (!points) {
return;
}
int height = tb_height();
int width = tb_width();
*points = (struct widget_points) {.x1 = min(max(0, x1), width),
.x2 = min(max(0, x2), width),
.y1 = min(max(0, y1), height),
.y2 = min(max(0, y2), height)};
}
bool
widget_should_forcebreak(int width) {
return width == 0;
}
bool
widget_should_scroll(int x, int width, int max_width) {
return (x > (max_width - width) || (widget_should_forcebreak(width)));
}
bool
widget_advance_xy_if_scroll(
int *x, int *y, struct widget_points *points, int width) {
if (x && y && points && (widget_should_scroll(*x, width, points->x2))) {
*x = points->x1;
(*y)++;
return true;
}
return false;
}
int
widget_print_str(
int x, int y, int max_x, uintattr_t fg, uintattr_t bg, const char *str) {
if (!str) {
return 0;
}
uint32_t uc = 0;
int width = 0;
int original = x;
while (*str) {
int len = tb_utf8_char_to_unicode(&uc, str);
if (len == TB_ERR) {
break;
}
str += len;
uc = widget_uc_sanitize(uc, &width);
if ((widget_should_scroll(x, width, max_x))) {
break;
}
tb_set_cell(x, y, uc, fg, bg);
x += width;
}
return x - original;
}
int
widget_pad_center(int part, int total) {
int padding = (int) round(((double) (total - part)) / 2);
if (padding < 0) {
return 0;
}
return padding;
}
enum {
BORDER_NORMAL = 0,
BORDER_CORNER_LEFT,
BORDER_CORNER_RIGHT,
BORDER_CORNER_LEFT_BOTTOM,
BORDER_CORNER_RIGHT_BOTTOM,
BORDER_VERTICAL,
BORDER_MAX
};
static const char *const borders[BORDER_MAX] = {
[BORDER_NORMAL] = "─",
[BORDER_CORNER_LEFT] = "┌",
[BORDER_CORNER_RIGHT] = "┐",
[BORDER_CORNER_LEFT_BOTTOM] = "└",
[BORDER_CORNER_RIGHT_BOTTOM] = "┘",
[BORDER_VERTICAL] = "│",
};
void
border_redraw(struct widget_points *points, uintattr_t fg, uintattr_t bg) {
if (!points) {
return;
}
int height = points->y2 - points->y1;
for (int x = points->x1 + 1; x < (points->x2 - 1); x++) {
widget_print_str(
x, points->y1, points->x2, fg, bg, borders[BORDER_NORMAL]);
}
widget_print_str(
points->x1, points->y1, points->x2, fg, bg, borders[BORDER_CORNER_LEFT]);
widget_print_str(points->x2 - 1, points->y1, points->x2, fg, bg,
borders[BORDER_CORNER_RIGHT]);
/* Not points->y2 - 1 so that we write borders even if height < 2 */
for (int y = points->y1 + 1; y < points->y2; y++) {
widget_print_str(
points->x1, y, points->x2, fg, bg, borders[BORDER_VERTICAL]);
widget_print_str(
points->x2 - 1, y, points->x2, fg, bg, borders[BORDER_VERTICAL]);
}
/* Don't overwrite the left/right or top connection. */
if (height > 2) {
widget_print_str(points->x1, points->y2 - 1, points->x2, fg, bg,
borders[BORDER_CORNER_LEFT_BOTTOM]);
widget_print_str(points->x2 - 1, points->y2 - 1, points->x2, fg, bg,
borders[BORDER_CORNER_RIGHT_BOTTOM]);
for (int x = points->x1 + 1; x < (points->x2 - 1); x++) {
widget_print_str(
x, points->y2 - 1, points->x2, fg, bg, borders[BORDER_NORMAL]);
}
}
}
enum {
BUF_MAX = 2000,
};
static enum widget_error
buf_add(struct input *input, uint32_t ch) {
if (((arrlenu(input->buf)) + 1) > BUF_MAX) {
return WIDGET_NOOP;
}
arrins(input->buf, input->cur_buf, ch);
input->cur_buf++;
return WIDGET_REDRAW;
}
static enum widget_error
buf_left(struct input *input) {
if (input->cur_buf > 0) {
input->cur_buf--;
return WIDGET_REDRAW;
}
return WIDGET_NOOP;
}
static enum widget_error
buf_leftword(struct input *input) {
if (input->cur_buf > 0) {
do {
input->cur_buf--;
} while (input->cur_buf > 0
&& ((iswspace((wint_t) input->buf[input->cur_buf]))
|| !(iswspace((wint_t) input->buf[input->cur_buf - 1]))));
return WIDGET_REDRAW;
}
return WIDGET_NOOP;
}
static enum widget_error
buf_right(struct input *input) {
if (input->cur_buf < arrlenu(input->buf)) {
input->cur_buf++;
return WIDGET_REDRAW;
}
return WIDGET_NOOP;
}
static enum widget_error
buf_rightword(struct input *input) {
size_t buf_len = arrlenu(input->buf);
if (input->cur_buf < buf_len) {
do {
input->cur_buf++;
} while (input->cur_buf < buf_len
&& !((iswspace((wint_t) input->buf[input->cur_buf]))
&& !(iswspace((wint_t) input->buf[input->cur_buf - 1]))));
return WIDGET_REDRAW;
}
return WIDGET_NOOP;
}
static enum widget_error
buf_del(struct input *input) {
if (input->cur_buf > 0) {
--input->cur_buf;
arrdel(input->buf, input->cur_buf);
return WIDGET_REDRAW;
}
return WIDGET_NOOP;
}
static enum widget_error
buf_delword(struct input *input) {
size_t original_cur = input->cur_buf;
if ((buf_leftword(input)) == WIDGET_REDRAW) {
arrdeln(input->buf, input->cur_buf, original_cur - input->cur_buf);
return WIDGET_REDRAW;
}
return WIDGET_NOOP;
}
int
input_init(struct input *input, uintattr_t bg, bool scroll_horizontal) {
if (!input) {
return -1;
}
*input = (struct input) {.bg = bg, .scroll_horizontal = scroll_horizontal};
return 0;
}
void
input_finish(struct input *input) {
if (!input) {
return;
}
arrfree(input->buf);
memset(input, 0, sizeof(*input));
}
void
input_redraw(
struct input *input, struct widget_points *points, int *rows, bool dry_run) {
if (!rows) {
return;
}
*rows = 0;
/* Points might be invalid from the caller. */
if (!input || !points
|| !(widget_points_in_bounds(points, points->x1, points->y1))) {
return;
}
size_t buf_len = arrlenu(input->buf);
if (input->scroll_horizontal) {
*rows = 1;
if (dry_run) {
return;
}
int width = 0;
int max_width = points->x2 - points->x1;
int start_width = -1;
for (size_t i = 0; i < (input->cur_buf + 1) && i < buf_len; i++) {
int ch_width = 0;
widget_uc_sanitize(input->buf[i], &ch_width);
width += ch_width;
}
if (width >= max_width) {
start_width = width - max_width;
}
int x = points->x1;
width = 0;
size_t start = 0;
for (; start < buf_len && width <= start_width; start++) {
int ch_width = 0;
widget_uc_sanitize(input->buf[start], &ch_width);
width += ch_width;
}
tb_set_cursor(points->x1, points->y1);
for (size_t i = start; i < buf_len; i++) {
int ch_width = 0;
uint32_t uc = widget_uc_sanitize(input->buf[i], &ch_width);
if ((x + ch_width) >= points->x2) {
break;
}
if (!widget_should_forcebreak(ch_width)) {
tb_set_cell(x, points->y1, uc, TB_DEFAULT, input->bg);
}
x += ch_width;
if (i + 1 == input->cur_buf) {
tb_set_cursor(x, points->y1);
}
assert((widget_points_in_bounds(points, x, points->y1)));
}
return;
}
int max_height = points->y2 - points->y1;
int cur_x = points->x1;
int cur_line = 1;
int lines = 1;
{
int x = points->x1;
int width = 0;
for (size_t written = 0; written < buf_len; written++) {
widget_uc_sanitize(input->buf[written], &width);
widget_advance_xy_if_scroll(&x, &lines, points, width);
x += width;
/* Check if adding another character would overflow the screen. This
* prevents us from having the cursor stuck in the 1px gap between
* points->x2 and points->x2 - 1 if the character was an emoji. */
widget_advance_xy_if_scroll(&x, &lines, points, WIDGET_CH_MAX);
if ((written + 1) == input->cur_buf) {
cur_x = x;
cur_line = lines;
}
}
}
/* Don't mess up when coming back to the start after deleting a lot of
* text. */
if (lines < max_height) {
input->start_y = 0;
}
int diff_forward = cur_line - (input->start_y + max_height);
int diff_backward = input->start_y - (cur_line - 1);
if (diff_backward > 0) {
input->start_y -= diff_backward;
} else if (diff_forward > 0) {
input->start_y += diff_forward;
}
assert(input->start_y >= 0);
assert(input->start_y < lines);
int width = 0;
int line = 0;
size_t written = 0;
bool lines_fit_in_height = (lines < max_height);
/* Calculate starting index. */
int y = lines_fit_in_height ? (points->y2 - lines) : points->y1;
for (int x = points->x1; written < buf_len; written++) {
if (line >= input->start_y) {
break;
}
widget_uc_sanitize(input->buf[written], &width);
line += widget_advance_xy_if_scroll(&x, &y, points, width);
x += width;
line += widget_advance_xy_if_scroll(&x, &y, points, WIDGET_CH_MAX);
}
int cur_y = lines_fit_in_height
? (y + cur_line - 1)
: (points->y1 + (cur_line - (input->start_y + 1)));
assert((widget_points_in_bounds(points, cur_x, cur_y)));
if (!dry_run) {
tb_set_cursor(cur_x, cur_y);
}
for (int x = points->x1; written < buf_len; written++) {
if (line >= lines || (y - input->start_y) >= points->y2) {
break;
}
assert((widget_points_in_bounds(points, x, y - input->start_y)));
uint32_t uc = widget_uc_sanitize(input->buf[written], &width);
line += widget_advance_xy_if_scroll(&x, &y, points, width);
/* Don't print newlines directly as they mess up the screen. */
if (!widget_should_forcebreak(width) && !dry_run) {
tb_set_cell(x, y - input->start_y, uc, TB_DEFAULT, input->bg);
}
x += width;
line += widget_advance_xy_if_scroll(&x, &y, points, WIDGET_CH_MAX);
}
*rows = (lines_fit_in_height ? (line + 1) : max_height);
}
enum widget_error
input_handle_event(struct input *input, enum input_event event, ...) {
if (!input) {
return WIDGET_NOOP;
}
switch (event) {
case INPUT_CLEAR:
if ((arrlenu(input->buf)) == 0) {
return WIDGET_NOOP;
}
input->cur_buf = 0;
arrsetlen(input->buf, 0);
return WIDGET_REDRAW;
case INPUT_DELETE:
return buf_del(input);
case INPUT_DELETE_WORD:
return buf_delword(input);
case INPUT_RIGHT:
return buf_right(input);
case INPUT_RIGHT_WORD:
return buf_rightword(input);
case INPUT_LEFT:
return buf_left(input);
case INPUT_LEFT_WORD:
return buf_leftword(input);
case INPUT_ADD:
{
va_list vl = {0};
va_start(vl, event);
/* https://bugs.llvm.org/show_bug.cgi?id=41311
* NOLINTNEXTLINE(clang-analyzer-valist.Uninitialized) */
uint32_t ch = va_arg(vl, uint32_t);
va_end(vl);
return buf_add(input, ch);
}
default:
assert(0);
}
return WIDGET_NOOP;
}
char *
input_buf(struct input *input) {
size_t len = arrlenu(input->buf);
enum { max_codepoint_len = 6 };
if (len == 0) {
return NULL;
}
size_t size = 0;
char tmp[max_codepoint_len];
/* Calculate length. */
for (size_t i = 0; i < len; i++) {
size += (size_t) tb_utf8_unicode_to_char(tmp, input->buf[i]);
}
if (size == 0) {
return NULL;
}
char *buf = malloc((size + 1) * sizeof(*buf));
if (!buf) {
return NULL;
}
for (size_t i = 0, i_utf8 = 0; i < size && i_utf8 < len; i_utf8++) {
i += (size_t) tb_utf8_unicode_to_char(&buf[i], input->buf[i_utf8]);
}
/* Original size doesn't include + 1 for NUL so we don't need to subtract.
*/
buf[size] = '\0';
return buf;
}
/* If node is the parent's last child. */
static bool
is_last(const struct treeview_node *node) {
size_t len = node && node->parent ? arrlenu(node->parent->nodes) : 0;
return (len > 0 && node == node->parent->nodes[len - 1]);
}
static struct treeview_node *
leaf(struct treeview_node *node) {
if (node->is_expanded && (arrlenu(node->nodes)) > 0) {
size_t len = arrlenu(node->nodes);
return leaf(node->nodes[len > 0 ? len - 1 : 0]);
}
return node;
}
static struct treeview_node *
parent_next(struct treeview_node *node) {
if (node->parent) {
if ((node->parent->index + 1) < arrlenu(node->parent->nodes)) {
return node->parent->nodes[++node->parent->index];
}
if (node->parent->parent) {
return parent_next(node->parent);
}
}
return node;
}
static int
node_height_up_to_bottom(struct treeview_node *node) {
assert(node);
int height = 1;
if (!node->is_expanded) {
return height;
}
for (size_t i = 0, len = arrlenu(node->nodes); i < len; i++) {
height += node_height_up_to_bottom(node->nodes[i]);
}
return height;
}
static int
node_height_bottom_to_up(struct treeview_node *node) {
assert(node);
int height = 1;
if (!node->parent) {
return height;
}
assert(node->parent->is_expanded);
for (size_t i = 0, len = arrlenu(node->parent->nodes); i < len; i++) {
if (node->parent->nodes[i] == node) {
break;
}
height += node_height_up_to_bottom(node->parent->nodes[i]);
}
return height + node_height_bottom_to_up(node->parent);
}
static int
redraw(struct treeview *treeview, const struct treeview_node *node,
const struct widget_points *points, int x, int y) {
if (!node) {
return y;
}
assert((widget_points_in_bounds(points, x, y)));
/* Stolen from tview's semigraphics. */
const char symbol[] = "├──";
const char symbol_end[] = "└──";
const char symbol_continued[] = "│";
const int gap_size = 3; /* Width of the above symbols (first 3). */
bool is_end = is_last(node);
bool is_not_top_level = (node->parent && node->parent->parent);
int symbol_printed_width = (is_not_top_level ? gap_size : 0);
/* Skip the given offset before actually printing stuff. */
if (node->parent && treeview->skipped++ >= treeview->start_y) {
if (is_not_top_level) {
widget_print_str(x, y, points->x2, TB_DEFAULT, TB_DEFAULT,
(is_end ? symbol_end : symbol));
}
struct widget_points user_points = {0};
widget_points_set(
&user_points, x + symbol_printed_width, points->x2, y, points->y2);
node->draw_cb(node->data, &user_points, (node == treeview->selected));
y++; /* Next node will be on another line. */
}
if (!node->is_expanded || (x + symbol_printed_width) >= points->x2) {
return y;
}
for (size_t i = 0, len = arrlenu(node->nodes); i < len && y < points->y2;
i++) {
int delta = redraw(treeview, node->nodes[i], points,
x + symbol_printed_width, y)
- y;
/* We can cheat here and avoid backtracking to show the parent-child
* relation by just filling the gaps as we would if we inspected them
* ourselves. */
if (is_not_top_level && !is_end) {
for (int j = 0; j < delta; j++) {
widget_print_str(
x, y++, points->x2, TB_DEFAULT, TB_DEFAULT, symbol_continued);
}
} else {
y += delta;
}
}
return y;
}
int
treeview_node_init(
struct treeview_node *node, void *data, treeview_draw_cb draw_cb) {
if (!node || !draw_cb) {
return -1;
}
*node = (struct treeview_node) {
.is_expanded = true, .data = data, .draw_cb = draw_cb};
return 0;
}
struct treeview_node *
treeview_node_alloc(void *data, treeview_draw_cb draw_cb) {
struct treeview_node *node = draw_cb ? malloc(sizeof(*node)) : NULL;
if (node) {
treeview_node_init(node, data, draw_cb);
}
return node;
}
int
treeview_node_add_child(
struct treeview_node *parent, struct treeview_node *child) {
if (!parent || !child || parent == child) {
return -1;
}
child->parent = parent;
arrput(parent->nodes, child);
return 0;
}
static void
node_children_destroy(struct treeview_node *node) {
if (node->nodes) {
for (size_t i = 0, len = arrlenu(node->nodes); i < len; i++) {
treeview_node_destroy(node->nodes[i]);
}
arrfree(node->nodes);
}
}
void
treeview_node_destroy(struct treeview_node *node) {
if (!node) {
return;
}
node_children_destroy(node);
free(node);
}
void
treeview_node_finish(struct treeview_node *node) {
if (!node) {
return;
}
for (size_t i = 0, len = arrlenu(node->nodes); i < len; i++) {
treeview_node_finish(node->nodes[i]);
}
arrfree(node->nodes);
memset(node, 0, sizeof(*node));
}
int
treeview_init(struct treeview *treeview) {
if (!treeview) {
return -1;
}
*treeview = (struct treeview) {
.root = {
.is_expanded = true,
},
};
return 0;
}
void
treeview_finish(struct treeview *treeview) {
if (treeview) {
node_children_destroy(&treeview->root);
memset(treeview, 0, sizeof(*treeview));
}
}
void
treeview_redraw(struct treeview *treeview, struct widget_points *points) {
if (!treeview || !treeview->selected || !points
|| !(widget_points_in_bounds(points, points->x1, points->y1))) {
return;
}
/* -1 as the root node is not visible. */
int selected_height = node_height_bottom_to_up(treeview->selected) - 1;
assert(selected_height > 0);
int diff_forward
= selected_height - (treeview->start_y + (points->y2 - points->y1));
int diff_backward = treeview->start_y - (selected_height - 1);
if (diff_backward > 0) {
treeview->start_y -= diff_backward;
} else if (diff_forward > 0) {