-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.c
1285 lines (1118 loc) · 42.5 KB
/
main.c
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
/*
* Copyright (c) 2017 Calvin Rose
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <janet/janet.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "ui.h"
/* Types */
#define UI_FLAG_DESTROYED 1
typedef struct {
uiControl *control;
uint32_t flags;
} UIControlWrapper;
static const JanetAbstractType control_td = {"ui/control", NULL, NULL, NULL, NULL, NULL, NULL, NULL};
static const JanetAbstractType window_td = {"ui/window", NULL, NULL, NULL, NULL, NULL, NULL, NULL};
static const JanetAbstractType button_td = {"ui/button", NULL, NULL, NULL, NULL, NULL, NULL, NULL};
static const JanetAbstractType box_td = {"ui/box", NULL, NULL, NULL, NULL, NULL, NULL, NULL};
static const JanetAbstractType checkbox_td = {"ui/checkbox", NULL, NULL, NULL, NULL, NULL, NULL, NULL};
static const JanetAbstractType entry_td = {"ui/entry", NULL, NULL, NULL, NULL, NULL, NULL, NULL};
static const JanetAbstractType label_td = {"ui/label", NULL, NULL, NULL, NULL, NULL, NULL, NULL};
static const JanetAbstractType tab_td = {"ui/tab", NULL, NULL, NULL, NULL, NULL, NULL, NULL};
static const JanetAbstractType group_td = {"ui/group", NULL, NULL, NULL, NULL, NULL, NULL, NULL};
static const JanetAbstractType spinbox_td = {"ui/spinbox", NULL, NULL, NULL, NULL, NULL, NULL, NULL};
static const JanetAbstractType slider_td = {"ui/slider", NULL, NULL, NULL, NULL, NULL, NULL, NULL};
static const JanetAbstractType progress_bar_td = {"ui/progress-bar", NULL, NULL, NULL, NULL, NULL, NULL, NULL};
static const JanetAbstractType separator_td = {"ui/separator", NULL, NULL, NULL, NULL, NULL, NULL, NULL};
static const JanetAbstractType combobox_td = {"ui/combobox", NULL, NULL, NULL, NULL, NULL, NULL, NULL};
static const JanetAbstractType editable_combobox_td = {"ui/editable-combobox", NULL, NULL, NULL, NULL, NULL, NULL, NULL};
static const JanetAbstractType radio_buttons_td = {"ui/radio-buttons", NULL, NULL, NULL, NULL, NULL, NULL, NULL};
static const JanetAbstractType date_time_picker_td = {"ui/date-time-picker", NULL, NULL, NULL, NULL, NULL, NULL, NULL};
static const JanetAbstractType multiline_entry_td = {"ui/multiline-entry", NULL, NULL, NULL, NULL, NULL, NULL, NULL};
static const JanetAbstractType menu_item_td = {"ui/menu-item", NULL, NULL, NULL, NULL, NULL, NULL, NULL};
static const JanetAbstractType menu_td = {"ui/menu", NULL, NULL, NULL, NULL, NULL, NULL, NULL};
/* Helpers */
static void *janet_getuitype(const Janet *argv, int32_t n, const JanetAbstractType *at) {
UIControlWrapper *uicw = janet_getabstract(argv, n, at);
if (uicw->flags & UI_FLAG_DESTROYED) {
janet_panic("ui control already destoryed");
}
return uicw->control;
}
/* Cast a Janet into a uiControl structure. Returns a pointer to
* the uiControl, or panics if cast fails. */
static uiControl *janet_getcontrol(const Janet *argv, int32_t n) {
Janet x = argv[n];
if (!janet_checktype(x, JANET_ABSTRACT)) {
janet_panicf("expected ui control, got %v", x);
}
UIControlWrapper *abst = janet_unwrap_abstract(x);
const JanetAbstractType *at = janet_abstract_type(abst);
if (at == &control_td ||
at == &window_td ||
at == &button_td ||
at == &box_td ||
at == &checkbox_td ||
at == &entry_td ||
at == &label_td ||
at == &tab_td ||
at == &group_td ||
at == &spinbox_td ||
at == &slider_td ||
at == &progress_bar_td ||
at == &separator_td ||
at == &combobox_td ||
at == &editable_combobox_td ||
at == &radio_buttons_td ||
at == &date_time_picker_td ||
at == &multiline_entry_td ||
at == &menu_item_td ||
at == &menu_td) {
if (abst->flags & UI_FLAG_DESTROYED) {
janet_panic("ui control already destoryed");
}
return uiControl(abst->control);
}
janet_panicf("expected ui control, got %v", x);
return NULL;
}
/* Wrap a pointer to a uiXxx object into an abstract */
static Janet janet_ui_handle_to_control(void *handle, const JanetAbstractType *atype) {
UIControlWrapper *abst = janet_abstract(atype, sizeof(UIControlWrapper));
abst->control = handle;
abst->flags = 0;
return janet_wrap_abstract(abst);
}
/* Convert a function or cfunction to a handle for
* libui callbacks */
static void *janet_ui_to_handler_data(Janet handler) {
const Janet *tup = janet_tuple_n(&handler, 1);
janet_gcroot(janet_wrap_tuple(tup));
return (void *)tup;
}
/* Get the function or cfunction from a libui callback
* handle data pointer */
static Janet janet_ui_from_handler_data(void *data) {
const Janet *tup = (const Janet *)data;
return tup[0];
}
/* Generic handler */
static int janet_ui_handler(void *data) {
Janet funcv = janet_ui_from_handler_data(data);
/* Tuple should already be GC root */
if (janet_checktype(funcv, JANET_FUNCTION)) {
janet_call(janet_unwrap_function(funcv), 0, NULL);
} else if (janet_checktype(funcv, JANET_CFUNCTION)) {
JanetCFunction cfunc = janet_unwrap_cfunction(funcv);
cfunc(0, NULL);
} else {
printf("called invalid handler\n");
}
return 1;
}
static void janet_ui_handler_void(void *data) {
janet_ui_handler(data);
}
static void assert_callable(const Janet *argv, int32_t n) {
if (!janet_checktypes(argv[n], JANET_TFLAG_CALLABLE)) {
janet_panic_type(argv[n], n, JANET_TFLAG_CALLABLE);
}
}
/* Global state */
static JANET_THREAD_LOCAL int inited = 0;
static void assert_inited(void) {
if (!inited) {
const char *initerr;
uiInitOptions o = {0};
if ((initerr = uiInit(&o)) != NULL) {
Janet err = janet_cstringv(initerr);
uiFreeInitError(initerr);
janet_panicv(err);
}
inited = 1;
}
}
static Janet janet_ui_init(int32_t argc, Janet *argv) {
assert_inited();
return janet_wrap_nil();
}
static Janet janet_ui_quit(int32_t argc, Janet *argv) {
assert_inited();
uiQuit();
return janet_wrap_nil();
}
static Janet janet_ui_uninit(int32_t argc, Janet *argv) {
assert_inited();
uiUninit();
return janet_wrap_nil();
}
static Janet janet_ui_main(int32_t argc, Janet *argv) {
assert_inited();
uiMain();
return janet_wrap_nil();
}
static Janet janet_ui_mainstep(int32_t argc, Janet *argv) {
assert_inited();
janet_fixarity(argc, 1);
int32_t step = janet_getinteger(argv, 0);
uiMainStep(step);
return janet_wrap_nil();
}
static Janet janet_ui_mainsteps(int32_t argc, Janet *argv) {
assert_inited();
uiMainSteps();
return janet_wrap_nil();
}
static Janet janet_ui_queue_main(int32_t argc, Janet *argv) {
assert_inited();
janet_fixarity(argc, 1);
assert_callable(argv, 0);
void *handle = janet_ui_to_handler_data(argv[0]);
uiQueueMain(janet_ui_handler_void, handle);
return janet_wrap_nil();
}
static Janet janet_ui_on_should_quit(int32_t argc, Janet *argv) {
assert_inited();
janet_fixarity(argc, 1);
assert_callable(argv, 0);
void *handle = janet_ui_to_handler_data(argv[0]);
uiOnShouldQuit(janet_ui_handler, handle);
return janet_wrap_nil();
}
static Janet janet_ui_timer(int32_t argc, Janet *argv) {
int32_t milliseconds;
janet_fixarity(argc, 2);
milliseconds = janet_getinteger(argv, 0);
assert_callable(argv, 1);
void *handle = janet_ui_to_handler_data(argv[1]);
uiTimer(milliseconds, janet_ui_handler, handle);
return janet_wrap_nil();
}
static Janet janet_ui_open_file(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
uiWindow *window = janet_getuitype(argv, 0, &window_td);
char *str = uiOpenFile(window);
if (NULL != str) return janet_cstringv(str);
return janet_wrap_nil();
}
static Janet janet_ui_save_file(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
uiWindow *window = janet_getuitype(argv, 0, &window_td);
char *str = uiSaveFile(window);
if (NULL != str) return janet_cstringv(str);
return janet_wrap_nil();
}
static Janet janet_ui_message_box(int32_t argc, Janet *argv) {
janet_fixarity(argc, 3);
uiWindow *window = janet_getuitype(argv, 0, &window_td);
const uint8_t *title = janet_getstring(argv, 1);
const uint8_t *description = janet_getstring(argv, 2);
uiMsgBox(window, (const char *)title, (const char *)description);
return janet_wrap_nil();
}
static Janet janet_ui_message_box_error(int32_t argc, Janet *argv) {
janet_fixarity(argc, 3);
uiWindow *window = janet_getuitype(argv, 0, &window_td);
const uint8_t *title = janet_getstring(argv, 1);
const uint8_t *description = janet_getstring(argv, 2);
uiMsgBoxError(window, (const char *)title, (const char *)description);
return janet_wrap_nil();
}
/* Generic controls */
static Janet janet_ui_destroy(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
uiControl *c = janet_getcontrol(argv, 0);
uiControlDestroy(c);
return janet_wrap_nil();
}
static Janet janet_ui_parent(int32_t argc, Janet *argv) {
uiControl *c, *d;
janet_arity(argc, 1, 2);
c = janet_getcontrol(argv, 0);
if (argc == 2) {
d = janet_getcontrol(argv, 1);
uiControlSetParent(c, d);
return argv[0];
}
return janet_ui_handle_to_control(uiControlParent(c), &control_td);
}
static Janet janet_ui_top_level(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
uiControl *c = janet_getcontrol(argv, 0);
return janet_wrap_boolean(uiControlToplevel(c));
}
static Janet janet_ui_visible(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
uiControl *c = janet_getcontrol(argv, 0);
return janet_wrap_boolean(uiControlVisible(c));
}
static Janet janet_ui_enabled(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
uiControl *c = janet_getcontrol(argv, 0);
return janet_wrap_boolean(uiControlEnabled(c));
}
static Janet janet_ui_show(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
uiControl *c = janet_getcontrol(argv, 0);
uiControlShow(c);
return argv[0];
}
static Janet janet_ui_hide(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
uiControl *c = janet_getcontrol(argv, 0);
uiControlHide(c);
return argv[0];
}
static Janet janet_ui_enable(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
uiControl *c = janet_getcontrol(argv, 0);
uiControlEnable(c);
return argv[0];
}
static Janet janet_ui_disable(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
uiControl *c = janet_getcontrol(argv, 0);
uiControlDisable(c);
return argv[0];
}
/* Window */
static int onClosing(uiWindow *w, void *data) {
uiQuit();
return 1;
}
static Janet janet_ui_window(int32_t argc, Janet *argv) {
assert_inited();
const char *title = "";
int32_t width = 800;
int32_t height = 600;
int menuBar = 0;
janet_arity(argc, 0, 4);
if (argc >= 1) title = (const char *) janet_getstring(argv, 0);
if (argc >= 2) width = janet_getinteger(argv, 1);
if (argc >= 3) height = janet_getinteger(argv, 2);
if (argc >= 4) menuBar = janet_getboolean(argv, 3);
uiWindow *window = uiNewWindow(title, width, height, menuBar);
if (NULL == window) janet_panic("could not create windows");
uiWindowOnClosing(window, onClosing, NULL);
return janet_ui_handle_to_control(window, &window_td);
}
static Janet janet_ui_window_title(int32_t argc, Janet *argv) {
janet_arity(argc, 1, 2);
uiWindow *window = janet_getuitype(argv, 0, &window_td);
if (argc == 2) {
const uint8_t *newTitle = janet_getstring(argv, 1);
uiWindowSetTitle(window, (const char *)newTitle);
return argv[0];
}
return janet_cstringv(uiWindowTitle(window));
}
static Janet janet_ui_window_content_size(int32_t argc, Janet *argv) {
janet_arity(argc, 1, 3);
uiWindow *window = janet_getuitype(argv, 0, &window_td);
if (argc == 1) {
int w = 0, h = 0;
uiWindowContentSize(window, &w, &h);
Janet *tup = janet_tuple_begin(2);
tup[0] = janet_wrap_integer(w);
tup[1] = janet_wrap_integer(h);
return janet_wrap_tuple(janet_tuple_end(tup));
} else if (argc == 2) {
int32_t wh = janet_getinteger(argv, 1);
uiWindowSetContentSize(window, wh, wh);
} else {
int32_t w = janet_getinteger(argv, 1);
int32_t h = janet_getinteger(argv, 2);
uiWindowSetContentSize(window, w, h);
}
return argv[0];
}
static Janet janet_ui_window_fullscreen(int32_t argc, Janet *argv) {
janet_arity(argc, 1, 2);
uiWindow *window = janet_getuitype(argv, 0, &window_td);
if (argc == 2) {
int full = janet_getboolean(argv, 1);
uiWindowSetFullscreen(window, full);
return argv[0];
}
return janet_wrap_boolean(uiWindowFullscreen(window));
}
static int window_closing_handler(uiWindow *window, void *data) {
(void) window;
return janet_ui_handler(data);
}
static void window_content_size_changed_handler(uiWindow *window, void *data) {
(void) window;
janet_ui_handler(data);
}
static Janet janet_ui_window_on_closing(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2);
uiWindow *window = janet_getuitype(argv, 0, &window_td);
uiWindowOnClosing(window, window_closing_handler,
janet_ui_to_handler_data(argv[1]));
return argv[0];
}
static Janet janet_ui_window_on_content_size_changed(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2);
uiWindow *window = janet_getuitype(argv, 0, &window_td);
uiWindowOnContentSizeChanged(window, window_content_size_changed_handler,
janet_ui_to_handler_data(argv[1]));
return argv[0];
}
static Janet janet_ui_window_set_child(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2);
uiWindow *window = janet_getuitype(argv, 0, &window_td);
uiControl *c = janet_getcontrol(argv, 1);
uiWindowSetChild(window, c);
return argv[0];
}
static Janet janet_ui_window_borderless(int32_t argc, Janet *argv) {
janet_arity(argc, 1, 2);
uiWindow *window = janet_getuitype(argv, 0, &window_td);
if (argc == 2) {
int borders = janet_getboolean(argv, 1);
uiWindowSetBorderless(window, borders);
return argv[0];
}
return janet_wrap_boolean(uiWindowBorderless(window));
}
static Janet janet_ui_window_margined(int32_t argc, Janet *argv) {
janet_arity(argc, 1, 2);
uiWindow *window = janet_getuitype(argv, 0, &window_td);
if (argc == 2) {
int margined = janet_getboolean(argv, 1);
uiWindowSetMargined(window, margined);
return argv[0];
}
return janet_wrap_boolean(uiWindowMargined(window));
}
/* Button */
static Janet janet_ui_button(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
assert_inited();
const uint8_t *text = janet_getstring(argv, 0);
uiButton *button = uiNewButton((const char *)text);
return janet_ui_handle_to_control(button, &button_td);
}
static Janet janet_ui_button_text(int32_t argc, Janet *argv) {
janet_arity(argc, 1, 2);
uiButton *button = janet_getuitype(argv, 0, &button_td);
if (argc == 2) {
const uint8_t *newText = janet_getstring(argv, 1);
uiButtonSetText(button, (const char *)newText);
return argv[0];
}
return janet_cstringv(uiButtonText(button));
}
static void button_click_handler(uiButton *button, void *data) {
(void)button;
janet_ui_handler(data);
}
static Janet janet_ui_button_on_clicked(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2);
uiButton *button = janet_getuitype(argv, 0, &button_td);
uiButtonOnClicked(button, button_click_handler,
janet_ui_to_handler_data(argv[1]));
return argv[0];
}
/* Box */
static Janet janet_ui_horizontal_box(int32_t argc, Janet *argv) {
janet_fixarity(argc, 0);
(void) argv;
assert_inited();
uiBox *box = uiNewHorizontalBox();
return janet_ui_handle_to_control(box, &box_td);
}
static Janet janet_ui_vertical_box(int32_t argc, Janet *argv) {
janet_fixarity(argc, 0);
(void) argv;
assert_inited();
uiBox *box = uiNewVerticalBox();
return janet_ui_handle_to_control(box, &box_td);
}
static Janet janet_ui_box_append(int32_t argc, Janet *argv) {
int stretchy = 0;
janet_arity(argc, 2, 3);
uiBox *box = janet_getuitype(argv, 0, &box_td);
uiControl *c = janet_getcontrol(argv, 1);
if (argc == 3) stretchy = janet_getboolean(argv, 2);
uiBoxAppend(box, c, stretchy);
return argv[0];
}
static Janet janet_ui_box_delete(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2);
uiBox *box = janet_getuitype(argv, 0, &box_td);
int32_t index = janet_getinteger(argv, 1);
uiBoxDelete(box, index);
return argv[0];
}
static Janet janet_ui_box_padded(int32_t argc, Janet *argv) {
janet_arity(argc, 1, 2);
uiBox *box = janet_getuitype(argv, 0, &box_td);
if (argc == 2) {
int padded = janet_getboolean(argv, 1);
uiBoxSetPadded(box, padded);
return argv[0];
}
return janet_wrap_boolean(uiBoxPadded(box));
}
/* Checkbox */
static Janet janet_ui_checkbox(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
assert_inited();
const uint8_t *text = janet_getstring(argv, 0);
uiCheckbox *cbox = uiNewCheckbox((const char *)text);
return janet_ui_handle_to_control(cbox, &checkbox_td);
}
static Janet janet_ui_checkbox_text(int32_t argc, Janet *argv) {
janet_arity(argc, 1, 2);
uiCheckbox *cbox = janet_getuitype(argv, 0, &checkbox_td);
if (argc == 2) {
const uint8_t *text = janet_getstring(argv, 1);
uiCheckboxSetText(cbox, (const char *)text);
return argv[0];
}
return janet_cstringv(uiCheckboxText(cbox));
}
static Janet janet_ui_checkbox_checked(int32_t argc, Janet *argv) {
janet_arity(argc, 1, 2);
uiCheckbox *cbox = janet_getuitype(argv, 0, &checkbox_td);
if (argc == 2) {
int checked = janet_getboolean(argv, 1);
uiCheckboxSetChecked(cbox, checked);
return argv[0];
}
return janet_wrap_boolean(uiCheckboxChecked(cbox));
}
static void on_toggled_handler(uiCheckbox *c, void *data) {
(void) c;
janet_ui_handler(data);
}
static Janet janet_ui_checkbox_on_toggled(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2);
uiCheckbox *cbox = janet_getuitype(argv, 0, &checkbox_td);
assert_callable(argv, 1);
void *handle = janet_ui_to_handler_data(argv[1]);
uiCheckboxOnToggled(cbox, on_toggled_handler, handle);
return argv[0];
}
/* Entry */
static Janet janet_ui_entry(int32_t argc, Janet *argv) {
janet_fixarity(argc, 0);
(void) argv;
assert_inited();
return janet_ui_handle_to_control(uiNewEntry(), &entry_td);
}
static Janet janet_ui_password_entry(int32_t argc, Janet *argv) {
janet_fixarity(argc, 0);
(void) argv;
assert_inited();
return janet_ui_handle_to_control(uiNewPasswordEntry(), &entry_td);
}
static Janet janet_ui_search_entry(int32_t argc, Janet *argv) {
janet_fixarity(argc, 0);
(void) argv;
assert_inited();
return janet_ui_handle_to_control(uiNewSearchEntry(), &entry_td);
}
static Janet janet_ui_entry_text(int32_t argc, Janet *argv) {
janet_arity(argc, 1, 2);
uiEntry *entry = janet_getuitype(argv, 0, &entry_td);
if (argc == 2) {
const uint8_t *text = janet_getstring(argv, 1);
uiEntrySetText(entry, (const char *)text);
return argv[0];
}
return janet_cstringv(uiEntryText(entry));
}
static Janet janet_ui_entry_read_only(int32_t argc, Janet *argv) {
janet_arity(argc, 1, 2);
uiEntry *entry = janet_getuitype(argv, 0, &entry_td);
if (argc == 2) {
int readonly = janet_getboolean(argv, 1);
uiEntrySetReadOnly(entry, readonly);
return argv[0];
}
return janet_wrap_boolean(uiEntryReadOnly(entry));
}
static void on_entry_changed(uiEntry *e, void *data) {
(void) e;
janet_ui_handler(data);
}
static Janet janet_ui_entry_on_changed(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2);
uiEntry *entry = janet_getuitype(argv, 0, &entry_td);
assert_callable(argv, 1);
void *handle = janet_ui_to_handler_data(argv[1]);
uiEntryOnChanged(entry, on_entry_changed, handle);
return argv[0];
}
/* Label */
static Janet janet_ui_label(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
assert_inited();
const uint8_t *text = janet_getstring(argv, 0);
uiLabel *label = uiNewLabel((const char *)text);
return janet_ui_handle_to_control(label, &label_td);
}
static Janet janet_ui_label_text(int32_t argc, Janet *argv) {
janet_arity(argc, 1, 2);
uiLabel *label = janet_getuitype(argv, 0, &label_td);
if (argc == 2) {
const uint8_t *text = janet_getstring(argv, 1);
uiLabelSetText(label, (const char *)text);
return argv[0];
}
return janet_cstringv(uiLabelText(label));
}
/* Janet */
static Janet janet_ui_tab(int32_t argc, Janet *argv) {
janet_fixarity(argc, 0);
(void) argv;
assert_inited();
return janet_ui_handle_to_control(uiNewTab(), &tab_td);
}
static Janet janet_ui_tab_margined(int32_t argc, Janet *argv) {
janet_arity(argc, 2, 3);
uiTab *tab = janet_getuitype(argv, 0, &tab_td);
int32_t page = janet_getinteger(argv, 1);
if (argc == 3) {
int margined = janet_getboolean(argv, 2);
uiTabSetMargined(tab, page, margined);
return argv[0];
}
return janet_wrap_boolean(uiTabMargined(tab, page));
}
static Janet janet_ui_tab_num_pages(int32_t argc, Janet *argv) {
uiTab *tab = janet_getuitype(argv, 0, &tab_td);
return janet_wrap_integer(uiTabNumPages(tab));
}
static Janet janet_ui_tab_append(int32_t argc, Janet *argv) {
janet_fixarity(argc, 3);
uiTab *tab = janet_getuitype(argv, 0, &tab_td);
const uint8_t *name = janet_getstring(argv, 1);
uiControl *c = janet_getcontrol(argv, 2);
uiTabAppend(tab, (const char *)name, c);
return argv[0];
}
static Janet janet_ui_tab_insert_at(int32_t argc, Janet *argv) {
janet_fixarity(argc, 4);
uiTab *tab = janet_getuitype(argv, 0, &tab_td);
const uint8_t *name = janet_getstring(argv, 1);
int32_t at = janet_getinteger(argv, 2);
uiControl *c = janet_getcontrol(argv, 3);
uiTabInsertAt(tab, (const char *)name, at, c);
return argv[0];
}
static Janet janet_ui_tab_delete(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2);
uiTab *tab = janet_getuitype(argv, 0, &tab_td);
int32_t at = janet_getinteger(argv, 1);
uiTabDelete(tab, at);
return argv[0];
}
/* Janet */
static Janet janet_ui_group(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
assert_inited();
const uint8_t *title = janet_getstring(argv, 0);
uiGroup *group = uiNewGroup((const char *)title);
return janet_ui_handle_to_control(group, &group_td);
}
static Janet janet_ui_group_title(int32_t argc, Janet *argv) {
janet_arity(argc, 1, 2);
uiGroup *group = janet_getuitype(argv, 0, &group_td);
if (argc == 2) {
const uint8_t *title = janet_getstring(argv, 1);
uiGroupSetTitle(group, (const char *)title);
return argv[0];
}
return janet_cstringv(uiGroupTitle(group));
}
static Janet janet_ui_group_margined(int32_t argc, Janet *argv) {
janet_arity(argc, 1, 2);
uiGroup *group = janet_getuitype(argv, 0, &group_td);
if (argc == 2) {
int margined = janet_getboolean(argv, 1);
uiGroupSetMargined(group, margined);
return argv[0];
}
return janet_wrap_boolean(uiGroupMargined(group));
}
static Janet janet_ui_group_set_child(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2);
uiGroup *group = janet_getuitype(argv, 0, &group_td);
uiControl *c = janet_getcontrol(argv, 1);
uiGroupSetChild(group, c);
return janet_wrap_boolean(uiGroupMargined(group));
}
/* Janet */
static Janet janet_ui_spinbox(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2);
assert_inited();
int32_t min = janet_getinteger(argv, 0);
int32_t max = janet_getinteger(argv, 1);
uiSpinbox *spinbox = uiNewSpinbox(min, max);
return janet_ui_handle_to_control(spinbox, &spinbox_td);
}
static Janet janet_ui_spinbox_value(int32_t argc, Janet *argv) {
janet_arity(argc, 1, 2);
uiSpinbox *spinbox = janet_getuitype(argv, 0, &spinbox_td);
if (argc == 2) {
int32_t value = janet_getinteger(argv, 1);
uiSpinboxSetValue(spinbox, value);
return argv[0];
}
return janet_wrap_integer(uiSpinboxValue(spinbox));
}
static void spinbox_on_changed(uiSpinbox *sb, void *data) {
(void) sb;
janet_ui_handler(data);
}
static Janet janet_ui_spinbox_on_changed(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2);
uiSpinbox *spinbox = janet_getuitype(argv, 0, &spinbox_td);
assert_callable(argv, 1);
void *handle = janet_ui_to_handler_data(argv[1]);
uiSpinboxOnChanged(spinbox, spinbox_on_changed, handle);
return argv[0];
}
/* Janet */
static Janet janet_ui_slider(int32_t argc, Janet *argv) {
assert_inited();
janet_fixarity(argc, 2);
int32_t min = janet_getinteger(argv, 0);
int32_t max = janet_getinteger(argv, 1);
return janet_ui_handle_to_control(uiNewSlider(min, max), &slider_td);
}
static Janet janet_ui_slider_value(int32_t argc, Janet *argv) {
janet_arity(argc, 1, 2);
uiSlider *slider = janet_getuitype(argv, 0, &slider_td);
if (argc == 2) {
int32_t value = janet_getinteger(argv, 1);
uiSliderSetValue(slider, value);
return argv[0];
}
return janet_wrap_integer(uiSliderValue(slider));
}
static void slider_on_changed(uiSlider *s, void *data) {
(void) s;
janet_ui_handler(data);
}
static Janet janet_ui_slider_on_changed(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2);
uiSlider *slider = janet_getuitype(argv, 0, &slider_td);
assert_callable(argv, 1);
void *handle = janet_ui_to_handler_data(argv[1]);
uiSliderOnChanged(slider, slider_on_changed, handle);
return argv[0];
}
/* Progress Bar */
static Janet janet_ui_progress_bar(int32_t argc, Janet *argv) {
assert_inited();
return janet_ui_handle_to_control(uiNewProgressBar(), &progress_bar_td);
}
static Janet janet_ui_progress_bar_value(int32_t argc, Janet *argv) {
janet_arity(argc, 1, 2);
uiProgressBar *bar = janet_getuitype(argv, 0, &progress_bar_td);
if (argc == 2) {
int32_t value = janet_getinteger(argv, 1);
uiProgressBarSetValue(bar, value);
return argv[0];
}
return janet_wrap_integer(uiProgressBarValue(bar));
}
/* Separator */
static Janet janet_ui_horizontal_separator(int32_t argc, Janet *argv) {
assert_inited();
return janet_ui_handle_to_control(uiNewHorizontalSeparator(), &separator_td);
}
static Janet janet_ui_vertical_separator(int32_t argc, Janet *argv) {
assert_inited();
return janet_ui_handle_to_control(uiNewVerticalSeparator(), &separator_td);
}
/* Combobox */
static Janet janet_ui_combobox(int32_t argc, Janet *argv) {
assert_inited();
return janet_ui_handle_to_control(uiNewCombobox(), &combobox_td);
}
static Janet janet_ui_combobox_selected(int32_t argc, Janet *argv) {
janet_arity(argc, 1, 2);
uiCombobox *cbox = janet_getuitype(argv, 0, &combobox_td);
if (argc == 2) {
int selected = janet_getboolean(argv, 1);
uiComboboxSetSelected(cbox, selected);
return argv[0];
}
return janet_wrap_integer(uiComboboxSelected(cbox));
}
static Janet janet_ui_combobox_append(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2);
uiCombobox *cbox = janet_getuitype(argv, 0, &combobox_td);
const uint8_t *text = janet_getstring(argv, 1);
uiComboboxAppend(cbox, (const char *)text);
return argv[0];
}
static void combobox_on_selected(uiCombobox *c, void *data) {
(void) c;
janet_ui_handler(data);
}
static Janet janet_ui_combobox_on_selected(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2);
uiCombobox *cbox = janet_getuitype(argv, 0, &combobox_td);
assert_callable(argv, 1);
void *handle = janet_ui_to_handler_data(argv[1]);
uiComboboxOnSelected(cbox, combobox_on_selected, handle);
return argv[0];
}
/* Editable Combobox */
static Janet janet_ui_editable_combobox(int32_t argc, Janet *argv) {
assert_inited();
return janet_ui_handle_to_control(uiNewEditableCombobox(), &editable_combobox_td);
}
static Janet janet_ui_editable_combobox_text(int32_t argc, Janet *argv) {
janet_arity(argc, 1, 2);
uiEditableCombobox *cbox = janet_getuitype(argv, 0, &editable_combobox_td);
if (argc == 2) {
const uint8_t *text = janet_getstring(argv, 1);
uiEditableComboboxSetText(cbox, (const char *)text);
return argv[0];
}
return janet_cstringv(uiEditableComboboxText(cbox));
}
static Janet janet_ui_editable_combobox_append(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2);
uiEditableCombobox *cbox = janet_getuitype(argv, 0, &editable_combobox_td);
const uint8_t *text = janet_getstring(argv, 1);
uiEditableComboboxAppend(cbox, (const char *)text);
return argv[0];
}
static void editable_combobox_on_changed(uiEditableCombobox *c, void *data) {
(void) c;
janet_ui_handler(data);
}
static Janet janet_ui_editable_combobox_on_changed(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2);
uiEditableCombobox *cbox = janet_getuitype(argv, 0, &editable_combobox_td);
assert_callable(argv, 1);
void *handle = janet_ui_to_handler_data(argv[1]);
uiEditableComboboxOnChanged(cbox, editable_combobox_on_changed, handle);
return argv[0];
}
/* Radio buttons */
static Janet janet_ui_radio_buttons(int32_t argc, Janet *argv) {
janet_fixarity(argc, 0);
(void) argv;
assert_inited();
return janet_ui_handle_to_control(uiNewRadioButtons(), &radio_buttons_td);
}
static Janet janet_ui_radio_buttons_selected(int32_t argc, Janet *argv) {
janet_arity(argc, 1, 2);
uiRadioButtons *rb = janet_getuitype(argv, 0, &radio_buttons_td);
if (argc == 2) {
int selected = janet_getboolean(argv, 1);
uiRadioButtonsSetSelected(rb, selected);
return argv[0];
}
return janet_wrap_integer(uiRadioButtonsSelected(rb));
}
static Janet janet_ui_radio_buttons_append(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2);
uiRadioButtons *rb = janet_getuitype(argv, 0, &radio_buttons_td);
const uint8_t *text = janet_getstring(argv, 1);
uiRadioButtonsAppend(rb, (const char *)text);
return argv[0];
}
static void radio_buttons_on_selected(uiRadioButtons *rb, void *data) {
(void) rb;
janet_ui_handler(data);
}
static Janet janet_ui_radio_buttons_on_selected(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2);
uiRadioButtons *rb = janet_getuitype(argv, 0, &radio_buttons_td);
assert_callable(argv, 1);
void *handle = janet_ui_to_handler_data(argv[1]);
uiRadioButtonsOnSelected(rb, radio_buttons_on_selected, handle);
return argv[0];
}
/* Multiline Entry */
static Janet janet_ui_multiline_entry(int32_t argc, Janet *argv) {
janet_arity(argc, 0, 1);
int nowrap = 0;
assert_inited();
if (argc == 1) nowrap = janet_getboolean(argv, 0);
return janet_ui_handle_to_control(
nowrap ? uiNewNonWrappingMultilineEntry() : uiNewMultilineEntry(),
&multiline_entry_td);
}
static Janet janet_ui_multiline_entry_text(int32_t argc, Janet *argv) {
janet_arity(argc, 1, 2);
uiMultilineEntry *me = janet_getuitype(argv, 0, &multiline_entry_td);
if (argc == 2) {
const uint8_t *text = janet_getstring(argv, 1);
uiMultilineEntrySetText(me, (const char *)text);
return argv[0];
}