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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
|
/* Copyright 2020 Jaakko Keränen <jaakko.keranen@iki.fi>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
#include "util.h"
#include "app.h"
#include "bookmarks.h"
#include "color.h"
#include "command.h"
#include "gmutil.h"
#include "labelwidget.h"
#include "inputwidget.h"
#include "bindingswidget.h"
#include "keys.h"
#include "widget.h"
#include "text.h"
#include "window.h"
#include <the_Foundation/math.h>
#include <the_Foundation/path.h>
#include <SDL_timer.h>
iBool isCommand_SDLEvent(const SDL_Event *d) {
return d->type == SDL_USEREVENT && d->user.code == command_UserEventCode;
}
iBool isCommand_UserEvent(const SDL_Event *d, const char *cmd) {
return d->type == SDL_USEREVENT && d->user.code == command_UserEventCode &&
equal_Command(d->user.data1, cmd);
}
const char *command_UserEvent(const SDL_Event *d) {
if (d->type == SDL_USEREVENT && d->user.code == command_UserEventCode) {
return d->user.data1;
}
return "";
}
void toString_Sym(int key, int kmods, iString *str) {
#if defined (iPlatformApple)
if (kmods & KMOD_CTRL) {
appendChar_String(str, 0x2303);
}
if (kmods & KMOD_ALT) {
appendChar_String(str, 0x2325);
}
if (kmods & KMOD_SHIFT) {
appendChar_String(str, 0x21e7);
}
if (kmods & KMOD_GUI) {
appendChar_String(str, 0x2318);
}
#else
if (kmods & KMOD_CTRL) {
appendCStr_String(str, "Ctrl+");
}
if (kmods & KMOD_ALT) {
appendCStr_String(str, "Alt+");
}
if (kmods & KMOD_SHIFT) {
appendCStr_String(str, "Shift+");
}
if (kmods & KMOD_GUI) {
appendCStr_String(str, "Meta+");
}
#endif
if (key == 0x20) {
appendCStr_String(str, "Space");
}
else if (key == SDLK_LEFT) {
appendChar_String(str, 0x2190);
}
else if (key == SDLK_RIGHT) {
appendChar_String(str, 0x2192);
}
else if (key == SDLK_UP) {
appendChar_String(str, 0x2191);
}
else if (key == SDLK_DOWN) {
appendChar_String(str, 0x2193);
}
else if (key < 128 && (isalnum(key) || ispunct(key))) {
appendChar_String(str, upper_Char(key));
}
else if (key == SDLK_BACKSPACE) {
appendChar_String(str, 0x232b); /* Erase to the Left */
}
else if (key == SDLK_DELETE) {
appendChar_String(str, 0x2326); /* Erase to the Right */
}
else {
appendCStr_String(str, SDL_GetKeyName(key));
}
}
iBool isMod_Sym(int key) {
return key == SDLK_LALT || key == SDLK_RALT || key == SDLK_LCTRL || key == SDLK_RCTRL ||
key == SDLK_LGUI || key == SDLK_RGUI || key == SDLK_LSHIFT || key == SDLK_RSHIFT;
}
int normalizedMod_Sym(int key) {
if (key == SDLK_RSHIFT) key = SDLK_LSHIFT;
if (key == SDLK_RCTRL) key = SDLK_LCTRL;
if (key == SDLK_RALT) key = SDLK_LALT;
if (key == SDLK_RGUI) key = SDLK_LGUI;
return key;
}
int keyMods_Sym(int kmods) {
kmods &= (KMOD_SHIFT | KMOD_ALT | KMOD_CTRL | KMOD_GUI);
/* Don't treat left/right modifiers differently. */
if (kmods & KMOD_SHIFT) kmods |= KMOD_SHIFT;
if (kmods & KMOD_ALT) kmods |= KMOD_ALT;
if (kmods & KMOD_CTRL) kmods |= KMOD_CTRL;
if (kmods & KMOD_GUI) kmods |= KMOD_GUI;
return kmods;
}
iRangei intersect_Rangei(iRangei a, iRangei b) {
if (a.end < b.start || a.start > b.end) {
return (iRangei){ 0, 0 };
}
return (iRangei){ iMax(a.start, b.start), iMin(a.end, b.end) };
}
iRangei union_Rangei(iRangei a, iRangei b) {
if (isEmpty_Rangei(a)) return b;
if (isEmpty_Rangei(b)) return a;
return (iRangei){ iMin(a.start, b.start), iMax(a.end, b.end) };
}
/*----------------------------------------------------------------------------------------------*/
iBool isFinished_Anim(const iAnim *d) {
return frameTime_Window(get_Window()) >= d->due;
}
void init_Anim(iAnim *d, float value) {
d->due = d->when = SDL_GetTicks();
d->from = d->to = value;
d->flags = 0;
}
iLocalDef float pos_Anim_(const iAnim *d, uint32_t now) {
return (float) (now - d->when) / (float) (d->due - d->when);
}
iLocalDef float easeIn_(float t) {
return t * t;
}
iLocalDef float easeOut_(float t) {
return t * (2.0f - t);
}
iLocalDef float easeBoth_(float t) {
if (t < 0.5f) {
return easeIn_(t * 2.0f) * 0.5f;
}
return 0.5f + easeOut_((t - 0.5f) * 2.0f) * 0.5f;
}
static float valueAt_Anim_(const iAnim *d, const uint32_t now) {
if (now >= d->due) {
return d->to;
}
if (now <= d->when) {
return d->from;
}
float t = pos_Anim_(d, now);
if ((d->flags & easeBoth_AnimFlag) == easeBoth_AnimFlag) {
t = easeBoth_(t);
}
else if (d->flags & easeIn_AnimFlag) {
t = easeIn_(t);
}
else if (d->flags & easeOut_AnimFlag) {
t = easeOut_(t);
}
return d->from * (1.0f - t) + d->to * t;
}
void setValue_Anim(iAnim *d, float to, uint32_t span) {
if (span == 0) {
d->from = d->to = to;
d->when = d->due = frameTime_Window(get_Window()); /* effectively in the past */
}
else if (fabsf(to - d->to) > 0.00001f) {
const uint32_t now = SDL_GetTicks();
d->from = valueAt_Anim_(d, now);
d->to = to;
d->when = now;
d->due = now + span;
}
}
void setValueEased_Anim(iAnim *d, float to, uint32_t span) {
if (fabsf(to - d->to) <= 0.00001f) {
d->to = to; /* Pretty much unchanged. */
return;
}
const uint32_t now = SDL_GetTicks();
if (isFinished_Anim(d)) {
d->from = d->to;
d->flags = easeBoth_AnimFlag;
}
else {
d->from = valueAt_Anim_(d, now);
d->flags = easeOut_AnimFlag;
}
d->to = to;
d->when = now;
d->due = now + span;
}
void setFlags_Anim(iAnim *d, int flags, iBool set) {
iChangeFlags(d->flags, flags, set);
}
void stop_Anim(iAnim *d) {
d->from = d->to = value_Anim(d);
d->when = d->due = SDL_GetTicks();
}
float pos_Anim(const iAnim *d) {
return pos_Anim_(d, frameTime_Window(get_Window()));
}
float value_Anim(const iAnim *d) {
return valueAt_Anim_(d, frameTime_Window(get_Window()));
}
/*-----------------------------------------------------------------------------------------------*/
void init_Click(iClick *d, iAnyObject *widget, int button) {
d->isActive = iFalse;
d->button = button;
d->bounds = as_Widget(widget);
d->startPos = zero_I2();
d->pos = zero_I2();
}
enum iClickResult processEvent_Click(iClick *d, const SDL_Event *event) {
if (event->type == SDL_MOUSEMOTION) {
const iInt2 pos = init_I2(event->motion.x, event->motion.y);
if (d->isActive) {
d->pos = pos;
return drag_ClickResult;
}
}
if (event->type != SDL_MOUSEBUTTONDOWN && event->type != SDL_MOUSEBUTTONUP) {
return none_ClickResult;
}
const SDL_MouseButtonEvent *mb = &event->button;
if (mb->button != d->button) {
return none_ClickResult;
}
const iInt2 pos = init_I2(mb->x, mb->y);
if (event->type == SDL_MOUSEBUTTONDOWN && mb->clicks == 2) {
if (contains_Widget(d->bounds, pos)) {
d->pos = pos;
setMouseGrab_Widget(NULL);
return double_ClickResult;
}
}
if (!d->isActive) {
if (mb->state == SDL_PRESSED) {
if (contains_Widget(d->bounds, pos)) {
d->isActive = iTrue;
d->startPos = d->pos = pos;
//setFlags_Widget(d->bounds, hover_WidgetFlag, iFalse);
setMouseGrab_Widget(d->bounds);
return started_ClickResult;
}
}
}
else { /* Active. */
if (mb->state == SDL_RELEASED) {
enum iClickResult result = contains_Widget(d->bounds, pos)
? finished_ClickResult
: aborted_ClickResult;
d->isActive = iFalse;
d->pos = pos;
setMouseGrab_Widget(NULL);
return result;
}
}
return none_ClickResult;
}
void cancel_Click(iClick *d) {
if (d->isActive) {
d->isActive = iFalse;
setMouseGrab_Widget(NULL);
}
}
iBool isMoved_Click(const iClick *d) {
return dist_I2(d->startPos, d->pos) > 2;
}
iInt2 pos_Click(const iClick *d) {
return d->pos;
}
iRect rect_Click(const iClick *d) {
return initCorners_Rect(min_I2(d->startPos, d->pos), max_I2(d->startPos, d->pos));
}
iInt2 delta_Click(const iClick *d) {
return sub_I2(d->pos, d->startPos);
}
/*-----------------------------------------------------------------------------------------------*/
iWidget *makePadding_Widget(int size) {
iWidget *pad = new_Widget();
setSize_Widget(pad, init1_I2(size));
return pad;
}
iLabelWidget *makeHeading_Widget(const char *text) {
iLabelWidget *heading = new_LabelWidget(text, NULL);
setFlags_Widget(as_Widget(heading), frameless_WidgetFlag | fixedSize_WidgetFlag, iTrue);
setBackgroundColor_Widget(as_Widget(heading), none_ColorId);
return heading;
}
iWidget *makeVDiv_Widget(void) {
iWidget *div = new_Widget();
setFlags_Widget(div, resizeChildren_WidgetFlag | arrangeVertical_WidgetFlag, iTrue);
return div;
}
iWidget *makeHDiv_Widget(void) {
iWidget *div = new_Widget();
setFlags_Widget(div, resizeChildren_WidgetFlag | arrangeHorizontal_WidgetFlag, iTrue);
return div;
}
iWidget *addAction_Widget(iWidget *parent, int key, int kmods, const char *command) {
iLabelWidget *action = newKeyMods_LabelWidget("", key, kmods, command);
setSize_Widget(as_Widget(action), zero_I2());
addChildFlags_Widget(parent, iClob(action), hidden_WidgetFlag);
return as_Widget(action);
}
/*-----------------------------------------------------------------------------------------------*/
static iBool isCommandIgnoredByMenus_(const char *cmd) {
return equal_Command(cmd, "media.updated") || equal_Command(cmd, "media.player.update") ||
startsWith_CStr(cmd, "feeds.update.") ||
equal_Command(cmd, "document.request.updated") || equal_Command(cmd, "window.resized") ||
(equal_Command(cmd, "mouse.clicked") && !arg_Command(cmd)); /* button released */
}
static iBool menuHandler_(iWidget *menu, const char *cmd) {
if (isVisible_Widget(menu)) {
if (equalWidget_Command(cmd, menu, "menu.opened")) {
return iFalse;
}
if (equal_Command(cmd, "menu.open") && pointer_Command(cmd) == menu->parent) {
/* Don't reopen self; instead, root will close the menu. */
return iFalse;
}
if ((equal_Command(cmd, "mouse.clicked") || equal_Command(cmd, "mouse.missed")) &&
arg_Command(cmd)) {
/* Dismiss open menus when clicking outside them. */
closeMenu_Widget(menu);
return iTrue;
}
if (!isCommandIgnoredByMenus_(cmd)) {
closeMenu_Widget(menu);
}
}
return iFalse;
}
iWidget *makeMenu_Widget(iWidget *parent, const iMenuItem *items, size_t n) {
iWidget *menu = new_Widget();
setFrameColor_Widget(menu, uiSeparator_ColorId);
setBackgroundColor_Widget(menu, uiBackground_ColorId);
setFlags_Widget(menu,
keepOnTop_WidgetFlag | collapse_WidgetFlag | hidden_WidgetFlag |
arrangeVertical_WidgetFlag | arrangeSize_WidgetFlag |
resizeChildrenToWidestChild_WidgetFlag | overflowScrollable_WidgetFlag,
iTrue);
for (size_t i = 0; i < n; ++i) {
const iMenuItem *item = &items[i];
if (equal_CStr(item->label, "---")) {
iWidget *sep = addChild_Widget(menu, iClob(new_Widget()));
setBackgroundColor_Widget(sep, uiSeparator_ColorId);
sep->rect.size.y = gap_UI / 3;
setFlags_Widget(sep, hover_WidgetFlag | fixedHeight_WidgetFlag, iTrue);
}
else {
iLabelWidget *label = addChildFlags_Widget(
menu,
iClob(newKeyMods_LabelWidget(item->label, item->key, item->kmods, item->command)),
frameless_WidgetFlag | alignLeft_WidgetFlag | drawKey_WidgetFlag);
updateSize_LabelWidget(label); /* drawKey was set */
}
}
addChild_Widget(parent, iClob(menu));
setCommandHandler_Widget(menu, menuHandler_);
iWidget *cancel = addAction_Widget(menu, SDLK_ESCAPE, 0, "cancel");
setId_Widget(cancel, "menu.cancel");
setFlags_Widget(cancel, disabled_WidgetFlag, iTrue);
return menu;
}
void openMenu_Widget(iWidget *d, iInt2 coord) {
/* Menu closes when commands are emitted, so handle any pending ones beforehand. */
postCommand_App("cancel"); /* dismiss any other menus */
processEvents_App(postedEventsOnly_AppEventMode);
setFlags_Widget(d, hidden_WidgetFlag, iFalse);
setFlags_Widget(d, commandOnMouseMiss_WidgetFlag, iTrue);
setFlags_Widget(findChild_Widget(d, "menu.cancel"), disabled_WidgetFlag, iFalse);
arrange_Widget(d);
d->rect.pos = coord;
/* Ensure the full menu is visible. */
const iInt2 rootSize = rootSize_Window(get_Window());
const iRect bounds = bounds_Widget(d);
const int leftExcess = -left_Rect(bounds);
const int rightExcess = right_Rect(bounds) - rootSize.x;
const int topExcess = -top_Rect(bounds);
const int bottomExcess = bottom_Rect(bounds) - rootSize.y;
if (bottomExcess > 0) {
d->rect.pos.y -= bottomExcess;
}
if (topExcess > 0) {
d->rect.pos.y += topExcess;
}
if (rightExcess > 0) {
d->rect.pos.x -= rightExcess;
}
if (leftExcess > 0) {
d->rect.pos.x += leftExcess;
}
refresh_App();
postCommand_Widget(d, "menu.opened");
}
void closeMenu_Widget(iWidget *d) {
setFlags_Widget(d, hidden_WidgetFlag, iTrue);
setFlags_Widget(findChild_Widget(d, "menu.cancel"), disabled_WidgetFlag, iTrue);
refresh_App();
postCommand_Widget(d, "menu.closed");
}
iLabelWidget *findMenuItem_Widget(iWidget *menu, const char *command) {
iForEach(ObjectList, i, children_Widget(menu)) {
if (isInstance_Object(i.object, &Class_LabelWidget)) {
iLabelWidget *menuItem = i.object;
if (!cmp_String(command_LabelWidget(menuItem), command)) {
return menuItem;
}
}
}
return NULL;
}
int checkContextMenu_Widget(iWidget *menu, const SDL_Event *ev) {
if (menu && ev->type == SDL_MOUSEBUTTONDOWN && ev->button.button == SDL_BUTTON_RIGHT) {
if (isVisible_Widget(menu)) {
closeMenu_Widget(menu);
return 0x1;
}
const iInt2 mousePos = init_I2(ev->button.x, ev->button.y);
if (contains_Widget(menu->parent, mousePos)) {
openMenu_Widget(menu, localCoord_Widget(menu->parent, mousePos));
return 0x2;
}
}
return 0;
}
iLabelWidget *makeMenuButton_LabelWidget(const char *label, const iMenuItem *items, size_t n) {
iLabelWidget *button = new_LabelWidget(label, "menu.open");
iWidget *menu = makeMenu_Widget(as_Widget(button), items, n);
setId_Widget(menu, "menu");
return button;
}
/*-----------------------------------------------------------------------------------------------*/
static iBool isTabPage_Widget_(const iWidget *tabs, const iWidget *page) {
return page->parent == findChild_Widget(tabs, "tabs.pages");
}
static iBool tabSwitcher_(iWidget *tabs, const char *cmd) {
if (equal_Command(cmd, "tabs.switch")) {
iWidget *target = pointerLabel_Command(cmd, "page");
if (!target) {
target = findChild_Widget(tabs, cstr_Rangecc(range_Command(cmd, "id")));
}
if (!target) return iFalse;
if (flags_Widget(target) & focusable_WidgetFlag) {
setFocus_Widget(target);
}
if (isTabPage_Widget_(tabs, target)) {
showTabPage_Widget(tabs, target);
return iTrue;
}
else if (hasParent_Widget(target, tabs)) {
/* Some widget on a page. */
while (!isTabPage_Widget_(tabs, target)) {
target = target->parent;
}
showTabPage_Widget(tabs, target);
return iTrue;
}
}
else if (equal_Command(cmd, "tabs.next") || equal_Command(cmd, "tabs.prev")) {
iWidget *pages = findChild_Widget(tabs, "tabs.pages");
int tabIndex = 0;
iConstForEach(ObjectList, i, pages->children) {
const iWidget *child = constAs_Widget(i.object);
if (isVisible_Widget(child)) break;
tabIndex++;
}
tabIndex += (equal_Command(cmd, "tabs.next") ? +1 : -1);
showTabPage_Widget(tabs, child_Widget(pages, iWrap(tabIndex, 0, childCount_Widget(pages))));
refresh_Widget(tabs);
return iTrue;
}
return iFalse;
}
iWidget *makeTabs_Widget(iWidget *parent) {
iWidget *tabs = makeVDiv_Widget();
iWidget *buttons = addChild_Widget(tabs, iClob(new_Widget()));
setFlags_Widget(buttons,
resizeWidthOfChildren_WidgetFlag | arrangeHorizontal_WidgetFlag |
arrangeHeight_WidgetFlag,
iTrue);
setId_Widget(buttons, "tabs.buttons");
iWidget *content = addChildFlags_Widget(tabs, iClob(makeHDiv_Widget()), expand_WidgetFlag);
setId_Widget(content, "tabs.content");
iWidget *pages = addChildFlags_Widget(
content, iClob(new_Widget()), expand_WidgetFlag | resizeChildren_WidgetFlag);
setId_Widget(pages, "tabs.pages");
addChild_Widget(parent, iClob(tabs));
setCommandHandler_Widget(tabs, tabSwitcher_);
return tabs;
}
static void addTabPage_Widget_(iWidget *tabs, enum iWidgetAddPos addPos, iWidget *page,
const char *label, int key, int kmods) {
iWidget * pages = findChild_Widget(tabs, "tabs.pages");
const iBool isSel = childCount_Widget(pages) == 0;
iWidget * buttons = findChild_Widget(tabs, "tabs.buttons");
iWidget * button = addChildPos_Widget(
buttons,
iClob(newKeyMods_LabelWidget(label, key, kmods, format_CStr("tabs.switch page:%p", page))),
addPos);
setFlags_Widget(buttons, hidden_WidgetFlag, iFalse);
setFlags_Widget(button, selected_WidgetFlag, isSel);
setFlags_Widget(button, commandOnClick_WidgetFlag | expand_WidgetFlag, iTrue);
addChildPos_Widget(pages, page, addPos);
setFlags_Widget(page, hidden_WidgetFlag | disabled_WidgetFlag, !isSel);
}
void appendTabPage_Widget(iWidget *tabs, iWidget *page, const char *label, int key, int kmods) {
addTabPage_Widget_(tabs, back_WidgetAddPos, page, label, key, kmods);
}
void prependTabPage_Widget(iWidget *tabs, iWidget *page, const char *label, int key, int kmods) {
addTabPage_Widget_(tabs, front_WidgetAddPos, page, label, key, kmods);
}
iWidget *tabPage_Widget(iWidget *tabs, size_t index) {
iWidget *pages = findChild_Widget(tabs, "tabs.pages");
return child_Widget(pages, index);
}
iWidget *removeTabPage_Widget(iWidget *tabs, size_t index) {
iWidget *buttons = findChild_Widget(tabs, "tabs.buttons");
iWidget *pages = findChild_Widget(tabs, "tabs.pages");
iWidget *button = removeChild_Widget(buttons, child_Widget(buttons, index));
iRelease(button);
iWidget *page = child_Widget(pages, index);
setFlags_Widget(page, hidden_WidgetFlag | disabled_WidgetFlag, iFalse);
removeChild_Widget(pages, page); /* `page` is now ours */
if (tabCount_Widget(tabs) <= 1 && flags_Widget(buttons) & collapse_WidgetFlag) {
setFlags_Widget(buttons, hidden_WidgetFlag, iTrue);
}
return page;
}
void resizeToLargestPage_Widget(iWidget *tabs) {
arrange_Widget(tabs);
iInt2 largest = zero_I2();
iWidget *pages = findChild_Widget(tabs, "tabs.pages");
iConstForEach(ObjectList, i, children_Widget(pages)) {
largest = max_I2(largest, ((const iWidget *) i.object)->rect.size);
}
iForEach(ObjectList, j, children_Widget(pages)) {
setSize_Widget(j.object, largest);
}
setSize_Widget(tabs, addY_I2(largest, height_Widget(findChild_Widget(tabs, "tabs.buttons"))));
}
iLabelWidget *tabButtonForPage_Widget_(iWidget *tabs, const iWidget *page) {
iWidget *buttons = findChild_Widget(tabs, "tabs.buttons");
iForEach(ObjectList, i, buttons->children) {
iAssert(isInstance_Object(i.object, &Class_LabelWidget));
iAny *label = i.object;
if (pointerLabel_Command(cstr_String(command_LabelWidget(label)), "page") == page) {
return label;
}
}
return NULL;
}
void showTabPage_Widget(iWidget *tabs, const iWidget *page) {
/* Select the corresponding button. */ {
iWidget *buttons = findChild_Widget(tabs, "tabs.buttons");
iForEach(ObjectList, i, buttons->children) {
iAssert(isInstance_Object(i.object, &Class_LabelWidget));
iAny *label = i.object;
const iBool isSel =
(pointerLabel_Command(cstr_String(command_LabelWidget(label)), "page") == page);
setFlags_Widget(label, selected_WidgetFlag, isSel);
}
}
/* Show/hide pages. */ {
iWidget *pages = findChild_Widget(tabs, "tabs.pages");
iForEach(ObjectList, i, pages->children) {
iWidget *child = as_Widget(i.object);
setFlags_Widget(child, hidden_WidgetFlag | disabled_WidgetFlag, child != page);
}
}
/* Notify. */
if (!isEmpty_String(id_Widget(page))) {
postCommandf_App("tabs.changed id:%s", cstr_String(id_Widget(page)));
}
}
iLabelWidget *tabPageButton_Widget(iWidget *tabs, const iAnyObject *page) {
return tabButtonForPage_Widget_(tabs, page);
}
iBool isTabButton_Widget(const iWidget *d) {
return d->parent && cmp_String(id_Widget(d->parent), "tabs.buttons") == 0;
}
void setTabPageLabel_Widget(iWidget *tabs, const iAnyObject *page, const iString *label) {
iLabelWidget *button = tabButtonForPage_Widget_(tabs, page);
setText_LabelWidget(button, label);
arrange_Widget(tabs);
}
size_t tabPageIndex_Widget(const iWidget *tabs, const iAnyObject *page) {
iWidget *pages = findChild_Widget(tabs, "tabs.pages");
return childIndex_Widget(pages, page);
}
const iWidget *currentTabPage_Widget(const iWidget *tabs) {
iWidget *pages = findChild_Widget(tabs, "tabs.pages");
iConstForEach(ObjectList, i, pages->children) {
if (isVisible_Widget(i.object)) {
return constAs_Widget(i.object);
}
}
return NULL;
}
size_t tabCount_Widget(const iWidget *tabs) {
return childCount_Widget(findChild_Widget(tabs, "tabs.pages"));
}
/*-----------------------------------------------------------------------------------------------*/
static void acceptFilePath_(iWidget *dlg) {
iInputWidget *input = findChild_Widget(dlg, "input");
iString *path = makeAbsolute_Path(text_InputWidget(input));
postCommandf_App("%s path:%s", cstr_String(id_Widget(dlg)), cstr_String(path));
destroy_Widget(dlg);
delete_String(path);
}
iBool filePathHandler_(iWidget *dlg, const char *cmd) {
iWidget *ptr = as_Widget(pointer_Command(cmd));
if (equal_Command(cmd, "input.ended")) {
if (hasParent_Widget(ptr, dlg)) {
if (arg_Command(cmd)) {
acceptFilePath_(dlg);
}
else {
destroy_Widget(dlg);
}
return iTrue;
}
return iFalse;
}
else if (ptr && !hasParent_Widget(ptr, dlg)) {
/* Command from outside the dialog, so dismiss the dialog. */
if (!equal_Command(cmd, "focus.lost")) {
destroy_Widget(dlg);
}
return iFalse;
}
else if (equal_Command(cmd, "filepath.cancel")) {
end_InputWidget(findChild_Widget(dlg, "input"), iFalse);
destroy_Widget(dlg);
return iTrue;
}
else if (equal_Command(cmd, "filepath.accept")) {
acceptFilePath_(dlg);
return iTrue;
}
return iFalse;
}
iWidget *makeSheet_Widget(const char *id) {
iWidget *sheet = new_Widget();
setPadding1_Widget(sheet, 3 * gap_UI);
setId_Widget(sheet, id);
setFrameColor_Widget(sheet, uiSeparator_ColorId);
setBackgroundColor_Widget(sheet, uiBackground_ColorId);
setFlags_Widget(sheet,
mouseModal_WidgetFlag | keepOnTop_WidgetFlag | arrangeVertical_WidgetFlag |
arrangeSize_WidgetFlag | centerHorizontal_WidgetFlag |
overflowScrollable_WidgetFlag,
iTrue);
return sheet;
}
void centerSheet_Widget(iWidget *sheet) {
arrange_Widget(sheet->parent);
// const iInt2 rootSize = rootSize_Window(get_Window());
// const iInt2 orig = localCoord_Widget(
// sheet->parent,
// init_I2(rootSize.x / 2 - sheet->rect.size.x / 2, bounds_Widget(sheet).pos.y));
// sheet->rect.pos = orig;
postRefresh_App();
}
void makeFilePath_Widget(iWidget * parent,
const iString *initialPath,
const char * title,
const char * acceptLabel,
const char * command) {
setFocus_Widget(NULL);
// processEvents_App(postedEventsOnly_AppEventMode);
iWidget *dlg = makeSheet_Widget(command);
setCommandHandler_Widget(dlg, filePathHandler_);
addChild_Widget(parent, iClob(dlg));
addChildFlags_Widget(dlg, iClob(new_LabelWidget(title, NULL)), frameless_WidgetFlag);
iInputWidget *input = addChild_Widget(dlg, iClob(new_InputWidget(0)));
if (initialPath) {
setText_InputWidget(input, collect_String(makeRelative_Path(initialPath)));
}
setId_Widget(as_Widget(input), "input");
as_Widget(input)->rect.size.x = dlg->rect.size.x;
addChild_Widget(dlg, iClob(makePadding_Widget(gap_UI)));
iWidget *div = new_Widget(); {
setFlags_Widget(div, arrangeHorizontal_WidgetFlag | arrangeSize_WidgetFlag, iTrue);
addChild_Widget(div, iClob(newKeyMods_LabelWidget("Cancel", SDLK_ESCAPE, 0, "filepath.cancel")));
addChild_Widget(div, iClob(newKeyMods_LabelWidget(acceptLabel, SDLK_RETURN, 0, "filepath.accept")));
}
addChild_Widget(dlg, iClob(div));
centerSheet_Widget(dlg);
setFocus_Widget(as_Widget(input));
}
static void acceptValueInput_(iWidget *dlg) {
const iInputWidget *input = findChild_Widget(dlg, "input");
if (!isEmpty_String(id_Widget(dlg))) {
const iString *val = text_InputWidget(input);
postCommandf_App("%s arg:%d value:%s",
cstr_String(id_Widget(dlg)),
toInt_String(val),
cstr_String(val));
}
}
static void updateValueInputWidth_(iWidget *dlg) {
const iInt2 rootSize = rootSize_Window(get_Window());
iWidget * title = findChild_Widget(dlg, "valueinput.title");
iWidget * prompt = findChild_Widget(dlg, "valueinput.prompt");
dlg->rect.size.x = iMaxi(iMaxi(rootSize.x / 2, title->rect.size.x), prompt->rect.size.x);
as_Widget(findChild_Widget(dlg, "input"))->rect.size.x = dlg->rect.size.x;
centerSheet_Widget(dlg);
}
iBool valueInputHandler_(iWidget *dlg, const char *cmd) {
iWidget *ptr = as_Widget(pointer_Command(cmd));
if (equal_Command(cmd, "window.resized")) {
if (isVisible_Widget(dlg)) {
updateValueInputWidth_(dlg);
}
return iFalse;
}
if (equal_Command(cmd, "input.ended")) {
if (hasParent_Widget(ptr, dlg)) {
if (arg_Command(cmd)) {
acceptValueInput_(dlg);
}
else {
postCommandf_App("valueinput.cancelled id:%s", cstr_String(id_Widget(dlg)));
setId_Widget(dlg, ""); /* no further commands to emit */
}
destroy_Widget(dlg);
return iTrue;
}
return iFalse;
}
else if (equal_Command(cmd, "cancel")) {
postCommandf_App("valueinput.cancelled id:%s", cstr_String(id_Widget(dlg)));
setId_Widget(dlg, ""); /* no further commands to emit */
destroy_Widget(dlg);
return iTrue;
}
else if (equal_Command(cmd, "valueinput.accept")) {
acceptValueInput_(dlg);
destroy_Widget(dlg);
return iTrue;
}
return iFalse;
}
iWidget *makeValueInput_Widget(iWidget *parent, const iString *initialValue, const char *title,
const char *prompt, const char *acceptLabel, const char *command) {
if (parent) {
setFocus_Widget(NULL);
// processEvents_App(postedEventsOnly_AppEventMode);
}
iWidget *dlg = makeSheet_Widget(command);
setCommandHandler_Widget(dlg, valueInputHandler_);
if (parent) {
addChild_Widget(parent, iClob(dlg));
}
setId_Widget(
addChildFlags_Widget(dlg, iClob(new_LabelWidget(title, NULL)), frameless_WidgetFlag),
"valueinput.title");
setId_Widget(
addChildFlags_Widget(dlg, iClob(new_LabelWidget(prompt, NULL)), frameless_WidgetFlag),
"valueinput.prompt");
iInputWidget *input = addChild_Widget(dlg, iClob(new_InputWidget(0)));
if (initialValue) {
setText_InputWidget(input, initialValue);
}
setId_Widget(as_Widget(input), "input");
updateValueInputWidth_(dlg);
addChild_Widget(dlg, iClob(makePadding_Widget(gap_UI)));
iWidget *div = new_Widget(); {
setFlags_Widget(div, arrangeHorizontal_WidgetFlag | arrangeSize_WidgetFlag, iTrue);
addChild_Widget(div, iClob(newKeyMods_LabelWidget("Cancel", SDLK_ESCAPE, 0, "cancel")));
addChild_Widget(
div,
iClob(newKeyMods_LabelWidget(acceptLabel ? acceptLabel : uiTextAction_ColorEscape "OK",
SDLK_RETURN,
0,
"valueinput.accept")));
}
addChild_Widget(dlg, iClob(div));
centerSheet_Widget(dlg);
if (parent) {
setFocus_Widget(as_Widget(input));
}
return dlg;
}
void updateValueInput_Widget(iWidget *d, const char *title, const char *prompt) {
setTextCStr_LabelWidget(findChild_Widget(d, "valueinput.title"), title);
setTextCStr_LabelWidget(findChild_Widget(d, "valueinput.prompt"), prompt);
updateValueInputWidth_(d);
}
static iBool messageHandler_(iWidget *msg, const char *cmd) {
/* Almost any command dismisses the sheet. */
if (!(equal_Command(cmd, "media.updated") || equal_Command(cmd, "media.player.update") ||
equal_Command(cmd, "document.request.updated") || startsWith_CStr(cmd, "window."))) {
destroy_Widget(msg);
}
return iFalse;
}
iWidget *makeMessage_Widget(const char *title, const char *msg) {
iWidget *dlg = makeQuestion_Widget(
title, msg, (const char *[]){ "Continue" }, (const char *[]){ "message.ok" }, 1);
addAction_Widget(dlg, SDLK_ESCAPE, 0, "message.ok");
addAction_Widget(dlg, SDLK_SPACE, 0, "message.ok");
return dlg;
}
iWidget *makeQuestion_Widget(const char *title, const char *msg, const char *labels[],
const char *commands[], size_t count) {
processEvents_App(postedEventsOnly_AppEventMode);
iWidget *dlg = makeSheet_Widget("");
setCommandHandler_Widget(dlg, messageHandler_);
addChildFlags_Widget(dlg, iClob(new_LabelWidget(title, NULL)), frameless_WidgetFlag);
addChildFlags_Widget(dlg, iClob(new_LabelWidget(msg, NULL)), frameless_WidgetFlag);
addChild_Widget(dlg, iClob(makePadding_Widget(gap_UI)));
iWidget *div = new_Widget(); {
setFlags_Widget(div, arrangeHorizontal_WidgetFlag | arrangeSize_WidgetFlag, iTrue);
for (size_t i = 0; i < count; ++i) {
/* The last one is the default option. */
const int key = (i == count - 1 ? SDLK_RETURN : 0);
addChild_Widget(div, iClob(newKeyMods_LabelWidget(labels[i], key, 0, commands[i])));
}
}
addChild_Widget(dlg, iClob(div));
addChild_Widget(get_Window()->root, iClob(dlg));
centerSheet_Widget(dlg);
return dlg;
}
void setToggle_Widget(iWidget *d, iBool active) {
if (d) {
setFlags_Widget(d, selected_WidgetFlag, active);
iLabelWidget *label = (iLabelWidget *) d;
if (!cmp_String(text_LabelWidget(label), "YES") ||
!cmp_String(text_LabelWidget(label), "NO")) {
updateText_LabelWidget((iLabelWidget *) d,
collectNewFormat_String("%s", isSelected_Widget(d) ? "YES" : "NO"));
}
else {
refresh_Widget(d);
}
}
}
static iBool toggleHandler_(iWidget *d, const char *cmd) {
if (equal_Command(cmd, "toggle") && pointer_Command(cmd) == d) {
setToggle_Widget(d, (flags_Widget(d) & selected_WidgetFlag) == 0);
postCommand_Widget(d,
format_CStr("%s.changed arg:%d",
cstr_String(id_Widget(d)),
isSelected_Widget(d) ? 1 : 0));
return iTrue;
}
return iFalse;
}
iWidget *makeToggle_Widget(const char *id) {
iWidget *toggle = as_Widget(new_LabelWidget("YES", "toggle")); /* "YES" for sizing */
setId_Widget(toggle, id);
updateTextCStr_LabelWidget((iLabelWidget *) toggle, "NO"); /* actual initial value */
setCommandHandler_Widget(toggle, toggleHandler_);
return toggle;
}
static void appendFramelessTabPage_(iWidget *tabs, iWidget *page, const char *title, int shortcut,
int kmods) {
appendTabPage_Widget(tabs, page, title, shortcut, kmods);
setFlags_Widget(
(iWidget *) back_ObjectList(children_Widget(findChild_Widget(tabs, "tabs.buttons"))),
frameless_WidgetFlag,
iTrue);
}
static iWidget *appendTwoColumnPage_(iWidget *tabs, const char *title, int shortcut, iWidget **headings,
iWidget **values) {
iWidget *page = new_Widget();
setFlags_Widget(page, arrangeVertical_WidgetFlag | arrangeSize_WidgetFlag |
resizeHeightOfChildren_WidgetFlag | borderTop_WidgetFlag, iTrue);
addChildFlags_Widget(page, iClob(new_Widget()), expand_WidgetFlag);
setPadding_Widget(page, 0, gap_UI, 0, gap_UI);
iWidget *columns = new_Widget();
addChildFlags_Widget(page, iClob(columns), arrangeHorizontal_WidgetFlag | arrangeSize_WidgetFlag);
*headings = addChildFlags_Widget(
columns, iClob(new_Widget()), arrangeVertical_WidgetFlag | arrangeSize_WidgetFlag);
*values = addChildFlags_Widget(
columns, iClob(new_Widget()), arrangeVertical_WidgetFlag | arrangeSize_WidgetFlag);
addChildFlags_Widget(page, iClob(new_Widget()), expand_WidgetFlag);
appendFramelessTabPage_(tabs, page, title, shortcut, shortcut ? KMOD_PRIMARY : 0);
return page;
}
static void makeTwoColumnHeading_(const char *title, iWidget *headings, iWidget *values) {
addChild_Widget(headings,
iClob(makeHeading_Widget(format_CStr(uiHeading_ColorEscape "%s", title))));
addChild_Widget(values, iClob(makeHeading_Widget("")));
}
static void expandInputFieldWidth_(iInputWidget *input) {
iWidget *page = as_Widget(input)->parent->parent->parent->parent; /* tabs > page > values > input */
as_Widget(input)->rect.size.x =
right_Rect(bounds_Widget(page)) - left_Rect(bounds_Widget(constAs_Widget(input)));
}
static void addRadioButton_(iWidget *parent, const char *id, const char *label, const char *cmd) {
setId_Widget(
addChildFlags_Widget(parent, iClob(new_LabelWidget(label, cmd)), radio_WidgetFlag),
id);
}
static void addFontButtons_(iWidget *parent, const char *id) {
const char *fontNames[] = {
"Nunito", "Fira Sans", "Literata", "Tinos"
};
iForIndices(i, fontNames) {
addRadioButton_(parent,
format_CStr("prefs.%s.%u", id, i),
fontNames[i],
format_CStr("%s.set arg:%u", id, i));
}
}
iWidget *makePreferences_Widget(void) {
iWidget *dlg = makeSheet_Widget("prefs");
addChildFlags_Widget(dlg,
iClob(new_LabelWidget(uiHeading_ColorEscape "PREFERENCES", NULL)),
frameless_WidgetFlag);
iWidget *tabs = makeTabs_Widget(dlg);
setId_Widget(tabs, "prefs.tabs");
iWidget *headings, *values;
/* General preferences. */ {
appendTwoColumnPage_(tabs, "General", '1', &headings, &values);
addChild_Widget(headings, iClob(makeHeading_Widget("Downloads folder:")));
setId_Widget(addChild_Widget(values, iClob(new_InputWidget(0))), "prefs.downloads");
addChild_Widget(headings, iClob(makeHeading_Widget("Outline on scrollbar:")));
addChild_Widget(values, iClob(makeToggle_Widget("prefs.hoveroutline")));
addChild_Widget(headings, iClob(makeHeading_Widget("Smooth scrolling:")));
addChild_Widget(values, iClob(makeToggle_Widget("prefs.smoothscroll")));
addChild_Widget(headings, iClob(makeHeading_Widget("Load image on scroll:")));
addChild_Widget(values, iClob(makeToggle_Widget("prefs.imageloadscroll")));
}
/* Window. */ {
appendTwoColumnPage_(tabs, "Window", '2', &headings, &values);
#if defined (iPlatformApple) || defined (iPlatformMSys)
addChild_Widget(headings, iClob(makeHeading_Widget("Use system theme:")));
addChild_Widget(values, iClob(makeToggle_Widget("prefs.ostheme")));
#endif
addChild_Widget(headings, iClob(makeHeading_Widget("Theme:")));
iWidget *themes = new_Widget();
/* Themes. */ {
setId_Widget(addChild_Widget(themes, iClob(new_LabelWidget("Pure Black", "theme.set arg:0"))), "prefs.theme.0");
setId_Widget(addChild_Widget(themes, iClob(new_LabelWidget("Dark", "theme.set arg:1"))), "prefs.theme.1");
setId_Widget(addChild_Widget(themes, iClob(new_LabelWidget("Light", "theme.set arg:2"))), "prefs.theme.2");
setId_Widget(addChild_Widget(themes, iClob(new_LabelWidget("Pure White", "theme.set arg:3"))), "prefs.theme.3");
}
addChildFlags_Widget(values, iClob(themes), arrangeHorizontal_WidgetFlag | arrangeSize_WidgetFlag);
addChild_Widget(headings, iClob(makeHeading_Widget("Retain window size:")));
addChild_Widget(values, iClob(makeToggle_Widget("prefs.retainwindow")));
addChild_Widget(headings, iClob(makeHeading_Widget("UI scale factor:")));
setId_Widget(addChild_Widget(values, iClob(new_InputWidget(8))), "prefs.uiscale");
makeTwoColumnHeading_("WIDE LAYOUT", headings, values);
addChild_Widget(headings, iClob(makeHeading_Widget("Site icon:")));
addChild_Widget(values, iClob(makeToggle_Widget("prefs.sideicon")));
}
/* Colors. */ {
appendTwoColumnPage_(tabs, "Colors", '3', &headings, &values);
makeTwoColumnHeading_("PAGE CONTENTS", headings, values);
for (int i = 0; i < 2; ++i) {
const iBool isDark = (i == 0);
const char *mode = isDark ? "dark" : "light";
const iMenuItem themes[] = {
{ "Colorful Dark", 0, 0, format_CStr("doctheme.%s.set arg:%d", mode, colorfulDark_GmDocumentTheme) },
{ "Colorful Light", 0, 0, format_CStr("doctheme.%s.set arg:%d", mode, colorfulLight_GmDocumentTheme) },
{ "Black", 0, 0, format_CStr("doctheme.%s.set arg:%d", mode, black_GmDocumentTheme) },
{ "Gray", 0, 0, format_CStr("doctheme.%s.set arg:%d", mode, gray_GmDocumentTheme) },
{ "White", 0, 0, format_CStr("doctheme.%s.set arg:%d", mode, white_GmDocumentTheme) },
{ "Sepia", 0, 0, format_CStr("doctheme.%s.set arg:%d", mode, sepia_GmDocumentTheme) },
{ "High Contrast", 0, 0, format_CStr("doctheme.%s.set arg:%d", mode, highContrast_GmDocumentTheme) },
};
addChild_Widget(headings, iClob(makeHeading_Widget(isDark ? "Dark theme:" : "Light theme:")));
setId_Widget(addChild_Widget(values,
iClob(makeMenuButton_LabelWidget(
themes[0].label, themes, iElemCount(themes)))),
format_CStr("prefs.doctheme.%s", mode));
}
addChild_Widget(headings, iClob(makeHeading_Widget("Saturation:")));
iWidget *sats = new_Widget();
/* Saturation levels. */ {
addRadioButton_(sats, "prefs.saturation.3", "Full", "saturation.set arg:100");
addRadioButton_(sats, "prefs.saturation.2", "Reduced", "saturation.set arg:66");
addRadioButton_(sats, "prefs.saturation.1", "Minimal", "saturation.set arg:33");
addRadioButton_(sats, "prefs.saturation.0", "Monochrome", "saturation.set arg:0");
}
addChildFlags_Widget(values, iClob(sats), arrangeHorizontal_WidgetFlag | arrangeSize_WidgetFlag);
}
/* Layout. */ {
appendTwoColumnPage_(tabs, "Style", '4', &headings, &values);
/* Fonts. */ {
iWidget *fonts;
addChild_Widget(headings, iClob(makeHeading_Widget("Heading font:")));
fonts = new_Widget();
addFontButtons_(fonts, "headingfont");
addChildFlags_Widget(values, iClob(fonts), arrangeHorizontal_WidgetFlag | arrangeSize_WidgetFlag);
addChild_Widget(headings, iClob(makeHeading_Widget("Body font:")));
fonts = new_Widget();
addFontButtons_(fonts, "font");
addChildFlags_Widget(values, iClob(fonts), arrangeHorizontal_WidgetFlag | arrangeSize_WidgetFlag);
addChild_Widget(headings, iClob(makeHeading_Widget("Monospace body:")));
iWidget *mono = new_Widget();
/* TODO: Needs labels! */
setTextCStr_LabelWidget(
addChild_Widget(mono, iClob(makeToggle_Widget("prefs.mono.gemini"))), "Gemini");
setTextCStr_LabelWidget(
addChild_Widget(mono, iClob(makeToggle_Widget("prefs.mono.gopher"))), "Gopher");
addChildFlags_Widget(values, iClob(mono), arrangeHorizontal_WidgetFlag | arrangeSize_WidgetFlag);
}
addChild_Widget(headings, iClob(makePadding_Widget(2 * gap_UI)));
addChild_Widget(values, iClob(makePadding_Widget(2 * gap_UI)));
addChild_Widget(headings, iClob(makeHeading_Widget("Line width:")));
iWidget *widths = new_Widget();
/* Line widths. */ {
addRadioButton_(widths, "prefs.linewidth.30", "\u20132", "linewidth.set arg:30");
addRadioButton_(widths, "prefs.linewidth.34", "\u20131", "linewidth.set arg:34");
addRadioButton_(widths, "prefs.linewidth.38", "Normal", "linewidth.set arg:38");
addRadioButton_(widths, "prefs.linewidth.43", "+1", "linewidth.set arg:43");
addRadioButton_(widths, "prefs.linewidth.48", "+2", "linewidth.set arg:48");
addRadioButton_(widths, "prefs.linewidth.1000", "Window", "linewidth.set arg:1000");
}
addChildFlags_Widget(values, iClob(widths), arrangeHorizontal_WidgetFlag | arrangeSize_WidgetFlag);
addChild_Widget(headings, iClob(makeHeading_Widget("Quote indicator:")));
iWidget *quote = new_Widget(); {
addRadioButton_(quote, "prefs.quoteicon.1", "Icon", "quoteicon.set arg:1");
addRadioButton_(quote, "prefs.quoteicon.0", "Line", "quoteicon.set arg:0");
}
addChildFlags_Widget(values, iClob(quote), arrangeHorizontal_WidgetFlag | arrangeSize_WidgetFlag);
addChild_Widget(headings, iClob(makeHeading_Widget("Big 1st paragaph:")));
addChild_Widget(values, iClob(makeToggle_Widget("prefs.biglede")));
}
/* Proxies. */ {
appendTwoColumnPage_(tabs, "Proxies", '5', &headings, &values);
addChild_Widget(headings, iClob(makeHeading_Widget("Gemini proxy:")));
setId_Widget(addChild_Widget(values, iClob(new_InputWidget(0))), "prefs.proxy.gemini");
addChild_Widget(headings, iClob(makeHeading_Widget("Gopher proxy:")));
setId_Widget(addChild_Widget(values, iClob(new_InputWidget(0))), "prefs.proxy.gopher");
addChild_Widget(headings, iClob(makeHeading_Widget("HTTP proxy:")));
setId_Widget(addChild_Widget(values, iClob(new_InputWidget(0))), "prefs.proxy.http");
}
/* Keybindings. */ {
iBindingsWidget *bind = new_BindingsWidget();
setFlags_Widget(as_Widget(bind), borderTop_WidgetFlag, iTrue);
appendFramelessTabPage_(tabs, iClob(bind), "Keys", '6', KMOD_PRIMARY);
}
resizeToLargestPage_Widget(tabs);
arrange_Widget(dlg);
/* Set input field sizes. */ {
expandInputFieldWidth_(findChild_Widget(tabs, "prefs.downloads"));
expandInputFieldWidth_(findChild_Widget(tabs, "prefs.proxy.gemini"));
expandInputFieldWidth_(findChild_Widget(tabs, "prefs.proxy.gopher"));
expandInputFieldWidth_(findChild_Widget(tabs, "prefs.proxy.http"));
}
iWidget *div = new_Widget(); {
setFlags_Widget(div, arrangeHorizontal_WidgetFlag | arrangeSize_WidgetFlag, iTrue);
addChild_Widget(div, iClob(newKeyMods_LabelWidget("Dismiss", SDLK_ESCAPE, 0, "prefs.dismiss")));
}
addChild_Widget(dlg, iClob(div));
addAction_Widget(dlg, prevTab_KeyShortcut, "tabs.prev");
addAction_Widget(dlg, nextTab_KeyShortcut, "tabs.next");
addChild_Widget(get_Window()->root, iClob(dlg));
centerSheet_Widget(dlg);
return dlg;
}
iWidget *makeBookmarkEditor_Widget(void) {
iWidget *dlg = makeSheet_Widget("bmed");
setId_Widget(addChildFlags_Widget(
dlg,
iClob(new_LabelWidget(uiHeading_ColorEscape "EDIT BOOKMARK", NULL)),
frameless_WidgetFlag),
"bmed.heading");
iWidget *page = new_Widget();
addChild_Widget(dlg, iClob(page));
setFlags_Widget(page, arrangeHorizontal_WidgetFlag | arrangeSize_WidgetFlag, iTrue);
iWidget *headings = addChildFlags_Widget(
page, iClob(new_Widget()), arrangeVertical_WidgetFlag | arrangeSize_WidgetFlag);
iWidget *values = addChildFlags_Widget(
page, iClob(new_Widget()), arrangeVertical_WidgetFlag | arrangeSize_WidgetFlag);
iInputWidget *inputs[3];
addChild_Widget(headings, iClob(makeHeading_Widget("Title:")));
setId_Widget(addChild_Widget(values, iClob(inputs[0] = new_InputWidget(0))), "bmed.title");
addChild_Widget(headings, iClob(makeHeading_Widget("URL:")));
setId_Widget(addChild_Widget(values, iClob(inputs[1] = new_InputWidget(0))), "bmed.url");
addChild_Widget(headings, iClob(makeHeading_Widget("Tags:")));
setId_Widget(addChild_Widget(values, iClob(inputs[2] = new_InputWidget(0))), "bmed.tags");
arrange_Widget(dlg);
for (int i = 0; i < 3; ++i) {
as_Widget(inputs[i])->rect.size.x = 100 * gap_UI - headings->rect.size.x;
}
iWidget *div = new_Widget(); {
setFlags_Widget(div, arrangeHorizontal_WidgetFlag | arrangeSize_WidgetFlag, iTrue);
addChild_Widget(div, iClob(newKeyMods_LabelWidget("Cancel", SDLK_ESCAPE, 0, "cancel")));
addChild_Widget(
div,
iClob(newKeyMods_LabelWidget(
uiTextCaution_ColorEscape "Save Bookmark", SDLK_RETURN, KMOD_PRIMARY, "bmed.accept")));
}
addChild_Widget(dlg, iClob(div));
addChild_Widget(get_Window()->root, iClob(dlg));
centerSheet_Widget(dlg);
return dlg;
}
static iBool handleBookmarkCreationCommands_SidebarWidget_(iWidget *editor, const char *cmd) {
if (equal_Command(cmd, "bmed.accept") || equal_Command(cmd, "cancel")) {
if (equal_Command(cmd, "bmed.accept")) {
const iString *title = text_InputWidget(findChild_Widget(editor, "bmed.title"));
const iString *url = text_InputWidget(findChild_Widget(editor, "bmed.url"));
const iString *tags = text_InputWidget(findChild_Widget(editor, "bmed.tags"));
add_Bookmarks(bookmarks_App(),
url,
title,
tags,
first_String(text_LabelWidget(findChild_Widget(editor, "bmed.icon"))));
postCommand_App("bookmarks.changed");
}
destroy_Widget(editor);
return iTrue;
}
return iFalse;
}
iWidget *makeBookmarkCreation_Widget(const iString *url, const iString *title, iChar icon) {
iWidget *dlg = makeBookmarkEditor_Widget();
setId_Widget(dlg, "bmed.create");
setTextCStr_LabelWidget(findChild_Widget(dlg, "bmed.heading"),
uiHeading_ColorEscape "ADD BOOKMARK");
iUrl parts;
init_Url(&parts, url);
setTextCStr_InputWidget(findChild_Widget(dlg, "bmed.title"),
title ? cstr_String(title) : cstr_Rangecc(parts.host));
setText_InputWidget(findChild_Widget(dlg, "bmed.url"), url);
setId_Widget(
addChildFlags_Widget(
dlg,
iClob(new_LabelWidget(cstrCollect_String(newUnicodeN_String(&icon, 1)), NULL)),
collapse_WidgetFlag | hidden_WidgetFlag | disabled_WidgetFlag),
"bmed.icon");
setCommandHandler_Widget(dlg, handleBookmarkCreationCommands_SidebarWidget_);
return dlg;
}
iWidget *makeIdentityCreation_Widget(void) {
iWidget *dlg = makeSheet_Widget("ident");
setId_Widget(addChildFlags_Widget(
dlg,
iClob(new_LabelWidget(uiHeading_ColorEscape "NEW IDENTITY", NULL)),
frameless_WidgetFlag),
"ident.heading");
iWidget *page = new_Widget();
addChildFlags_Widget(
dlg,
iClob(
new_LabelWidget("Creating a 2048-bit self-signed RSA certificate.", NULL)),
frameless_WidgetFlag);
addChild_Widget(dlg, iClob(page));
setFlags_Widget(page, arrangeHorizontal_WidgetFlag | arrangeSize_WidgetFlag, iTrue);
iWidget *headings = addChildFlags_Widget(
page, iClob(new_Widget()), arrangeVertical_WidgetFlag | arrangeSize_WidgetFlag);
iWidget *values = addChildFlags_Widget(
page, iClob(new_Widget()), arrangeVertical_WidgetFlag | arrangeSize_WidgetFlag);
iInputWidget *inputs[6];
addChild_Widget(headings, iClob(makeHeading_Widget("Valid until:")));
setId_Widget(addChild_Widget(values, iClob(newHint_InputWidget(19, "YYYY-MM-DD HH:MM:SS"))), "ident.until");
addChild_Widget(headings, iClob(makeHeading_Widget("Common name:")));
setId_Widget(addChild_Widget(values, iClob(inputs[0] = new_InputWidget(0))), "ident.common");
/* Temporary? */ {
addChild_Widget(headings, iClob(makeHeading_Widget("Temporary:")));
iWidget *tmpGroup = new_Widget();
setFlags_Widget(tmpGroup, arrangeSize_WidgetFlag | arrangeHorizontal_WidgetFlag, iTrue);
addChild_Widget(tmpGroup, iClob(makeToggle_Widget("ident.temp")));
setId_Widget(
addChildFlags_Widget(
tmpGroup,
iClob(new_LabelWidget(uiTextCaution_ColorEscape "\u26a0 not saved to disk", NULL)),
hidden_WidgetFlag | frameless_WidgetFlag),
"ident.temp.note");
addChild_Widget(values, iClob(tmpGroup));
}
addChild_Widget(headings, iClob(makePadding_Widget(gap_UI)));
addChild_Widget(values, iClob(makePadding_Widget(gap_UI)));
addChild_Widget(headings, iClob(makeHeading_Widget("Email:")));
setId_Widget(addChild_Widget(values, iClob(inputs[1] = newHint_InputWidget(0, "optional"))), "ident.email");
addChild_Widget(headings, iClob(makeHeading_Widget("User ID:")));
setId_Widget(addChild_Widget(values, iClob(inputs[2] = newHint_InputWidget(0, "optional"))), "ident.userid");
addChild_Widget(headings, iClob(makeHeading_Widget("Domain:")));
setId_Widget(addChild_Widget(values, iClob(inputs[3] = newHint_InputWidget(0, "optional"))), "ident.domain");
addChild_Widget(headings, iClob(makeHeading_Widget("Organization:")));
setId_Widget(addChild_Widget(values, iClob(inputs[4] = newHint_InputWidget(0, "optional"))), "ident.org");
addChild_Widget(headings, iClob(makeHeading_Widget("Country:")));
setId_Widget(addChild_Widget(values, iClob(inputs[5] = newHint_InputWidget(0, "optional"))), "ident.country");
arrange_Widget(dlg);
for (size_t i = 0; i < iElemCount(inputs); ++i) {
as_Widget(inputs[i])->rect.size.x = 100 * gap_UI - headings->rect.size.x;
}
iWidget *div = new_Widget(); {
setFlags_Widget(div, arrangeHorizontal_WidgetFlag | arrangeSize_WidgetFlag, iTrue);
addChild_Widget(div, iClob(newKeyMods_LabelWidget("Cancel", SDLK_ESCAPE, 0, "cancel")));
addChild_Widget(
div,
iClob(newKeyMods_LabelWidget(
uiTextAction_ColorEscape "Create Identity", SDLK_RETURN, KMOD_PRIMARY, "ident.accept")));
}
addChild_Widget(dlg, iClob(div));
addChild_Widget(get_Window()->root, iClob(dlg));
centerSheet_Widget(dlg);
return dlg;
}
|