-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPoorTui.h
1584 lines (1353 loc) · 42.2 KB
/
PoorTui.h
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
/* PoorTui.h
Primary Definitions, Structures and Functions to generate a ! p o o r ! - ! T U I !
! Could contain 3rd party MIT/GPL/"FreeSoftware" technologies !
(see detailed comments in related declarations/functions and at the EOF)
This is a ! p_o_o_r ! Application by (c) Pit Demmer. (PIT-Licensed 01.04.2022 - xx.xx.xxxx)
PIT-licensed" isn't a joke! - Right now I do not know about the license model I wanna use!
But Sources will be free!
I'm just struggling with professional 3rd parties distributing binaries
(or executing a compiling process on customer machines) without contributing to
"FreeSoftware"
So, do not distribute binaries/hexdumps without being in contact with me!
*/
//#include "Terminal.h"
//#include "esc.h"
#include "AnsiESC.h"
FILE *LOG_TuiCopy = NULL;
#define TUIsetColorStyle(file, color, style) ESCsetColorStyle(file, &userColors[color], 1); ESCsetTxtStyle(file, &userStyles[style], 1)
typedef struct TuiHeadersSTRUCT{
char *caption;
int txtColor;
int txtStyle;
int timeColor;
int timeStyle;
int printRunTime :1;
int printRealTime :1;
}TuiHeadersSTRUCT;
// global access on headers
TuiHeadersSTRUCT *userHeaders = NULL;
TuiHeadersSTRUCT *userFooters = NULL;
int TUI_HeaderCnt = 0;
int TUI_FooterCnt = 0;
typedef struct TuiMenuPosSTRUCT{
char *caption;
int keyCode;
int enabled;
int posCnt; // count of sub-positions
struct TuiMenuPosSTRUCT *nextPos; // on same level
struct TuiMenuPosSTRUCT *prevPos; // on same level
struct TuiMenuPosSTRUCT *pos1st; // first on sub level
struct TuiMenuPosSTRUCT *parentPos; // parent on upper level
int nextID; // helper for reallocating
int prevID; // "" "" ""
int pos1ID; // "" "" ""
int parentID; // "" "" ""
int selected :1; // if position is selected
int isOption :1; // if position is a check or option...
int isCheck :1;
int activated :1;
int isSpacer :1; // "-" as caption defines a spacer
int printSmall :1; // force to print small (preCalcSubMenu)
int printInverted :1; // force to print inverted (preCalcSubMenu)
}TuiMenuPosSTRUCT;
typedef struct TuiMenusSTRUCT{
int txtColor;
int txtStyle;
int selectColor;
int selectStyle;
int selectKeyColor;
int selectKeyStyle;
int disabledColor;
int disabledStyle;
int keyColor;
int keyStyle;
int timeColor;
int timeStyle;
int posCnt;
int pos1ID; // helper for fixing pos1st while reallocating - shifts
TuiMenuPosSTRUCT *pos1st;
int printRunTime :1;
int printRealTime :1;
}TuiMenusSTRUCT;
// global access on Menu Definitions
TuiMenusSTRUCT *userTopMenus = NULL;
TuiMenusSTRUCT *userBotMenus = NULL;
TuiMenusSTRUCT *userLeftMenus = NULL;
TuiMenusSTRUCT *userRightMenus = NULL;
TuiMenusSTRUCT *userPopUpMenus = NULL;
int TUI_TopMenuCnt = 0;
int TUI_BotMenuCnt = 0;
int TUI_LeftMenuCnt = 0;
int TUI_RightMenuCnt = 0;
int TUI_PopUpMenuCnt = 0;
#define TUI_MENU_TOP 1
#define TUI_MENU_LEFT 2
#define TUI_MENU_RIGHT 3
#define TUI_MENU_BOTTOM 4
typedef struct TuiDesktopsSTRUCT{
int header;
int topMenu;
int bottomMenu;
int leftMenu;
int rightMenu;
int footer;
}TuiDesktopsSTRUCT;
TuiDesktopsSTRUCT *userDesktopDefs = NULL;
TuiMenuPosSTRUCT *TUIgetSelectedPos(TuiMenuPosSTRUCT *menuPos){
// Get the last selected pos in a menu
TuiMenuPosSTRUCT *selectedPos = NULL;
while (menuPos){
if (menuPos->selected && menuPos->enabled){
selectedPos = menuPos;
if (!menuPos->pos1st){
return selectedPos;
}
else{
menuPos = menuPos->pos1st;
}
}
else {
menuPos = menuPos->nextPos;
}
}
return selectedPos;
}
int TUIfindSelectedMenu(TuiDesktopsSTRUCT *deskDef, TuiMenuPosSTRUCT *menuPos){
// Find the selected menu on a desktop
// Return 0 - 4 for none, top, left, right, bottom
// Return selectedPos in menuPos
int menuID[4];
menuID[0] = deskDef->topMenu;
menuID[1] = deskDef->leftMenu;
menuID[2] = deskDef->rightMenu;
menuID[3] = deskDef->bottomMenu;
TuiMenusSTRUCT *menuDef[4];
menuDef[0] = userTopMenus;
menuDef[1] = userLeftMenus;
menuDef[2] = userRightMenus;
menuDef[3] = userBotMenus;
for (int i = 0; i < 4; i++){
if (menuID[i]){
menuPos = TUIgetSelectedPos(menuDef[menuID[i] - 1]->pos1st);
if (menuPos){
return i + 1;
}
}
}
menuPos = NULL;
return 0;
}
void TUIsecureMinMaxXY(int *minX, int *minY, int *maxX, int *maxY){
// Secure the min/max values for rendering
if (*minX == 0){
*minX = 1;
}
if (*maxX == 0){
*maxX = TERM_ScreenWidth;
}
if (*minY == 0){
*minY = 1;
}
if (*maxY == 0){
*maxY = TERM_ScreenHeight;
}
}
int TUIgetPosLen(TuiMenuPosSTRUCT *menuPos){
if (!menuPos->printSmall){
return strlen(menuPos->caption);
}
else{
if (menuPos->isCheck || menuPos->isOption){
return 7;
}
return 3;
}
}
int TUIgetSubLen(TuiMenuPosSTRUCT *menuPos, int all, int fullLen){
// menuPos is the 1st position of a sub-menu
// all is a flag to get the length, including all following sub-menus
// fullLen is the current length of the (sum of) calling menu(s)
int len = 0;
int loopLen = 0;
int smallLen = 3; // Key + 2 spaces
TuiMenuPosSTRUCT *selectedPos = NULL;
int isSmall = menuPos->printSmall;
while (menuPos){
if (!isSmall){
len = strlen(menuPos->caption);
loopLen = (loopLen > len) ? loopLen : len;
}
else if(isSmall && smallLen == 3 && (menuPos->isCheck || menuPos->isOption)){
smallLen = 7; // [ ] A
if (!all){
break;
}
}
if (all && menuPos->selected && menuPos->enabled){
selectedPos = menuPos->pos1st;
}
menuPos = menuPos->nextPos;
}
if (isSmall){
loopLen = smallLen;
}
fullLen += loopLen;
if (selectedPos){
fullLen = TUIgetSubLen(selectedPos, all, fullLen);
}
return fullLen;
}
int TUIsetSubSmall(TuiMenuPosSTRUCT *menuPos){
// menuPos is the 1st position of a sub-menu
// crawl through all sub-menus and set 1st menuPos->printSmall = 0 to 1
while (menuPos){
if (!menuPos->printSmall){
menuPos->printSmall = -1;
return 1;
}
while (menuPos){
if (menuPos->selected && menuPos->enabled && menuPos->pos1st){
// next level
menuPos = menuPos->pos1st;
break;
}
else{
// next position
menuPos = menuPos->nextPos;
}
}
}
return 0;
}
void TUIclearSmallInverted(TuiMenuPosSTRUCT *menuPos){
while (menuPos){
menuPos->printSmall = 0;
menuPos->printInverted = 0;
if (menuPos->selected && menuPos->enabled){
// SubMenu found
menuPos = menuPos->pos1st;
menuPos->printSmall = 0;
menuPos->printInverted = 0;
}
menuPos = menuPos->nextPos;
}
}
int TUIprintMenuPos(int posX, int posY, int printSmall, int renderWidth, TuiMenuPosSTRUCT *menuPos, TuiMenusSTRUCT *menuDef){
// Where to render
if (posX && posY){
ESClocate(LOG_TuiCopy, posX, posY);
}
else if (posX){
ESClocateX(LOG_TuiCopy, posX);
}
if (!renderWidth){
renderWidth = TUIgetSubLen(menuPos, 0, 0);
}
if (printSmall || menuPos->printSmall){
// render just the keys (+ eventually check/option brackets)
if (menuPos->selected && menuPos->enabled){
// enabled & selected
TUIsetColorStyle(LOG_TuiCopy, menuDef->selectColor, menuDef->selectStyle);
}
else if (menuPos->enabled){
// enabled
TUIsetColorStyle(LOG_TuiCopy, menuDef->txtColor, menuDef->txtStyle);
}
else{
// disabled
TUIsetColorStyle(LOG_TuiCopy, menuDef->disabledColor, menuDef->disabledStyle);
}
fprintf(LOG_TuiCopy, " "); // Leading Space
if (menuPos->isCheck){
// is a check
fprintf(LOG_TuiCopy, "[");
if (menuPos->activated){
fprintf(LOG_TuiCopy, "x");
}
else{
fprintf(LOG_TuiCopy, " ");
}
fprintf(LOG_TuiCopy, "] ");
}
else if (menuPos->isOption){
// is an option
fprintf(LOG_TuiCopy, "(");
if (menuPos->activated){
fprintf(LOG_TuiCopy, "x");
}
else{
fprintf(LOG_TuiCopy, " ");
}
fprintf(LOG_TuiCopy, ") ");
}
else if (printSmall == 7){
if (menuPos->isSpacer){
DEClineX(LOG_TuiCopy, 4);
}
else{
STRprintSpaces(LOG_TuiCopy, 4);
}
}
else{
// we're fine
}
if (menuPos->selected && menuPos->enabled){
TUIsetColorStyle(LOG_TuiCopy, menuDef->selectKeyColor, menuDef->selectKeyStyle);
}
else if (menuPos->enabled && !menuPos->isSpacer){
// Key in keyColor and keyStyle
TUIsetColorStyle(LOG_TuiCopy, menuDef->keyColor, menuDef->keyStyle);
}
else{
// Color & Style is fine
}
if (menuPos->isSpacer){
DEClineX(LOG_TuiCopy, 1);
}
else{
fprintf(LOG_TuiCopy, "%c", menuPos->caption[menuPos->keyCode]);
}
if (menuPos->selected && menuPos->enabled){
// enabled & selected
TUIsetColorStyle(LOG_TuiCopy, menuDef->selectColor, menuDef->selectStyle);
}
else if (menuPos->enabled){
// enabled
TUIsetColorStyle(LOG_TuiCopy, menuDef->txtColor, menuDef->txtStyle);
}
else{
// disabled
TUIsetColorStyle(LOG_TuiCopy, menuDef->disabledColor, menuDef->disabledStyle);
}
fprintf(LOG_TuiCopy, " "); // Trailing Space
}
else{
// render full line
int len = (int)strlen(menuPos->caption);
for (int i = 0; i < len; i++){
if (menuPos->enabled && menuPos->selected){
// enabled - selected
if (i && (i == menuPos->keyCode)){
// is key
TUIsetColorStyle(LOG_TuiCopy, menuDef->selectKeyColor, menuDef->selectKeyStyle);
}
else{
TUIsetColorStyle(LOG_TuiCopy, menuDef->selectColor, menuDef->selectStyle);
}
}
else if (menuPos->enabled){
// enabled
if (i && (i == menuPos->keyCode)){
// is key
TUIsetColorStyle(LOG_TuiCopy, menuDef->keyColor, menuDef->keyStyle);
}
else{
TUIsetColorStyle(LOG_TuiCopy, menuDef->txtColor, menuDef->txtStyle);
}
}
else{
// disabled
TUIsetColorStyle(LOG_TuiCopy, menuDef->disabledColor, menuDef->disabledStyle);
}
if (i == 2 && (menuPos->isCheck || menuPos->isOption)){
// Set Value of check/option
if (menuPos->activated){
fprintf(LOG_TuiCopy, "x");
}
else{
fprintf(LOG_TuiCopy, " ");
}
}
else{
if (menuPos->isSpacer){
/* code */
}
else{
fprintf(LOG_TuiCopy, "%c", menuPos->caption[i]);
}
}
}
if (menuPos->isSpacer){
fprintf(LOG_TuiCopy, " ");
DEClineX(LOG_TuiCopy, renderWidth - 2);
fprintf(LOG_TuiCopy, " ");
}
else{
STRprintSpaces(LOG_TuiCopy, renderWidth - strlen(menuPos->caption));
}
}
return renderWidth;
}
int TUIpreRenderSub(int posX, int width, TuiMenuPosSTRUCT *menuPos, int minX, int maxX){
int mainLen = TUIgetSubLen(menuPos, 0, 0);
int subLen = TUIgetSubLen(menuPos, 1, 0) - mainLen;
int diff = mainLen - width;
int invertX = posX - diff;
// [main-part][sub-part]
// 0 = to the right
// 1 = to the left
int sX[2][2];
int eX[2][2];
int spaceX[2][2];
int main_sub[2][2];
// Start- and Endpoints
sX[0][0] = posX;
sX[0][1] = posX - subLen;
sX[1][0] = invertX;
sX[1][1] = invertX - subLen;
mainLen--;
eX[0][0] = posX + mainLen + subLen;
eX[0][1] = posX + mainLen;
eX[1][0] = invertX + mainLen + subLen;
eX[1][1] = invertX + mainLen;
// Space left to minX / maxX
spaceX[0][0] = maxX - eX[0][0];
spaceX[0][1] = sX[0][1] - minX;
spaceX[1][0] = maxX - eX[1][0];
spaceX[1][1] = sX[1][1] - minX;
// Final validity
main_sub[0][0] = (spaceX[0][0] >= 0) && (sX[0][0] >= minX);
main_sub[0][1] = (spaceX[0][1] >= 0) && (eX[0][1] <= maxX);
main_sub[1][0] = (spaceX[1][0] >= 0) && (sX[1][0] >= minX);
main_sub[1][1] = (spaceX[1][1] >= 0) && (eX[1][1] <= maxX);
// Find the valid one with the biggest space
int id = 0;
for (int i = 0; i < 2; i++){
for (int j = 0; j < 2; j++){
id++;
if (main_sub[i][j]){
if (main_sub[0][0]){
if ((spaceX[0][0] < spaceX[i][j])){
spaceX[0][0] = spaceX[i][j];
main_sub[0][0] = id;
}
}
else{
spaceX[0][0] = spaceX[i][j];
main_sub[0][0] = id;
}
}
}
}
// Set the valid ones
int mainSet = 0;
int subSet = 0;
switch (main_sub[0][0]){
case 1:
// main regular - subs to the right
break;
case 2:
// main regular - subs to the left
subSet = -1;
break;
case 3:
// main inverted - subs to the right
mainSet = -1;
break;
case 4:
// main inverted - subs to the left
mainSet = -1;
subSet = -1;
break;
default:
// too big to render
if (TUIsetSubSmall(menuPos)){
TUIpreRenderSub(posX, width, menuPos, minX, maxX);
return 1;
}
else{
// we can't go smaller - finally too big to render
return 0;
}
}
// Set the printInverted
menuPos->printInverted = mainSet;
while (menuPos){
if (menuPos->selected && menuPos->enabled){
// next level
menuPos = menuPos->pos1st;
if (menuPos){
menuPos->printInverted = subSet;
}
}
else{
// next position
menuPos = menuPos->nextPos;
}
}
return 1;
}
int TUIrenderSub(int posX, int posY, int width, TuiMenuPosSTRUCT *menuPos, TuiMenusSTRUCT *menuDef, int isMain, int menuType, int minX, int minY, int maxX, int maxY){
int height = 0;
int selected = 0;
// int ignoreY = 0;
int maxHeight = maxY - minY + 1;
TuiMenuPosSTRUCT *selectedPos = NULL;
TuiMenuPosSTRUCT *menuPos1st = menuPos;
while (menuPos){
height++;
if (menuPos->selected && menuPos->enabled){
selected = height;
selectedPos = menuPos->pos1st;
}
menuPos = menuPos->nextPos;
}
menuPos = menuPos1st;
int stepY = 1;
if (menuType == TUI_MENU_BOTTOM){
// Down -> Up (Bottom Menu) - flip posY for Y-calculation
posY = maxY - posY + minY;
stepY = -1;
}
int shiftY = posY;
int spaceBelowReg = maxY - (posY + height - 1); // space below regular rendered menu
// Up -> Down
if (spaceBelowReg < 0){
// top alignment doesn't fit
int spaceAboveInv = (posY - height + 1) - minY; // space above inverted rendered menu
if (spaceAboveInv < 0){
// bottom alignment doesn't fit either - move up
shiftY = minY;
int spaceAboveReg = 0; // space above regular rendered menu
spaceBelowReg = maxY - (shiftY + height - 1);
if (spaceBelowReg < 0 && selected){
// very top alignment doesn't fit either - take care on position of selection
int selPosReg = shiftY + selected - (selected > 0); // selected, absolute position regular
if (selPosReg > maxY){
// selection is hidden - lift selection to the bottom
shiftY -= (selPosReg - maxY);
// re-use spaceAboveReg and spaceBelowReg
spaceBelowReg = height - selected; // cnt of hidden elements below selection
spaceAboveReg = maxHeight - 1; // cnt of visible elements above selection
}
else{
// selection is visible
// re-use spaceAboveReg and spaceBelowReg
spaceBelowReg = (height - selected) - (maxY - selPosReg); // cnt of hidden elements below selection
spaceAboveReg = selPosReg - minY; // cnt of visible elements above selection
}
if (spaceBelowReg > spaceAboveReg){
// more hidden below - move up
shiftY -= (spaceAboveReg / 2);
}
else{
// more visible above - move up all invisible
shiftY -= spaceBelowReg;
}
}
}
else{
// bottom alignment fits - move up
shiftY = posY - height + 1;
}
}
posY = shiftY;
if (menuType == TUI_MENU_BOTTOM){
// Down -> Up (Bottom Menu) - flip back posY from Y-calculation
posY = maxY - posY + minY;
stepY = -1;
}
int subLen = TUIgetSubLen(menuPos, 0, 0);
int endX = 0;
int nextPosX = 0;
if (isMain){
// 1st sub, (below/above width)
if (menuPos->printInverted){
// inverted
int diff = subLen - width;
posX -= diff;
}
endX = posX + subLen;
nextPosX = posX;
}
else{
// next subs (left/right of width)
if (menuPos->printInverted){
// left of width
endX = posX + width;
posX -= subLen;
nextPosX = posX;
}
else{
// right of width
nextPosX = posX;
posX += width;
endX = posX + subLen;
}
}
int newWidth = endX - nextPosX;
int printSmall = menuPos->printSmall;
if (printSmall){
printSmall = subLen;
}
selectedPos = NULL;
int lastValidY = 0;
while (menuPos){
if (posY >= minY && posY <= maxY){
ESClocate(LOG_TuiCopy, posX, posY);
lastValidY = posY;
if (menuPos->selected && menuPos->enabled){
// selected
selectedPos = menuPos->pos1st;
selected = posY;
}
//printf("Pos:Max %d:%d", posY, maxY);
TUIprintMenuPos(posX, posY, printSmall, subLen, menuPos, menuDef);
}
posY += stepY;
menuPos = menuPos->nextPos;
}
if (selectedPos){
TUIrenderSub(nextPosX, selected, newWidth, selectedPos, menuDef, 0, menuType, minX, minY, maxX, maxY);
}
return lastValidY + stepY;
}
int TUIrenderHeaderFooter(int posX, int posY, int width, TuiHeadersSTRUCT *headerDef, int justRefresh){
int renderLen = 0;
int renderWidth = 0;
int renderRealTime = 0;
int renderRunTime = 0;
int renderSmall = 0;
// int dontRender = 0;
int maxYdummy = 0;
TUIsecureMinMaxXY(&posX, &posY, &width, &maxYdummy);
char strHLP[STR_MID_SIZE];
//struct TuiHeadersSTRUCT *headerDef = &userHeaders[headerID];
// Width - respecting posX
width = width - posX + 1;
renderWidth = width;
strcpy(strHLP, headerDef->caption);
renderLen = strlen(strHLP);
if (headerDef->printRealTime){
width -= 20; // 01.01.2023 09:09:21 (+ trailing space)
renderRealTime = 1;
}
if (headerDef->printRunTime){
width -= 16; // 00000d 00:00:00 (+ leading space)
renderRunTime = 1;
}
if (renderRealTime && renderRunTime){
renderLen += 3; // cause runtime is smaller than realtime and we'll eventually add three spaces later
}
if (renderLen > width){
// menu/caption doesn't fit - f*ck
if (renderRunTime){
width += 16; // 00000d 00:00:00 (+ leading space)
if (renderRealTime && renderRunTime){
renderLen -= 3; // runtime removed...
}
renderRunTime = 0;
if (renderLen > width){
// still too small...
if (renderRealTime){
width += 20; // 01.01.2023 09:09:21 (+ trailing space)
renderRealTime = 0;
if (renderLen > width){
// we're finally f*cked
renderSmall = 1;
}
}
else{
// we're finally f*cked
renderSmall = 1;
}
}
}
else if (renderRealTime){
width += 20; // 01.01.2023 09:09:21 (+ trailing space)
renderRealTime = 0;
if (renderLen > width){
// we're finally f*cked
renderSmall = 1;
}
}
if (renderLen > width){
// too long without time
renderSmall = 1;
}
if (renderSmall){
// cut caption
if (width >= 3){
/* code */
strncpy(strHLP, headerDef->caption, width - 3);
strHLP[width - 3] = '~';
strHLP[width - 2] = '\0';
}
else{
// too small to render
return 0;
}
}
else{
// full caption fits
}
}
else{
// full caption fits
}
// Where to render
if (posX && posY){
ESClocate(LOG_TuiCopy, posX, posY);
}
else if (posX){
ESClocateX(LOG_TuiCopy, posX);
}
if (!justRefresh){
// Style & Color
TUIsetColorStyle(LOG_TuiCopy, headerDef->txtColor, headerDef->txtStyle);
if (renderRealTime ^ renderRunTime) {
// one time active - center is left of time...
STRprintCentered(LOG_TuiCopy, strHLP, width);
STRprintSpaces(LOG_TuiCopy, renderWidth - width);
}
else{
// center is center
if (renderRealTime & renderRunTime){
// add three spaces, cause runtime is shorter than realtime
strHLP[renderLen - 3] = ' '; strHLP[renderLen - 2] = ' '; strHLP[renderLen - 1] = ' '; strHLP[renderLen] = '\0';
}
STRprintCentered(LOG_TuiCopy, strHLP, renderWidth);
}
}
else{
// we just refresh time(s)
ESCcursorRight(LOG_TuiCopy, renderWidth);
}
if (renderRealTime || renderRunTime){
// Set style & color of times
TUIsetColorStyle(LOG_TuiCopy, headerDef->timeColor, headerDef->timeStyle);
}
// Do we print the Realtime (all time right alignment)
if (renderRealTime){
// 01.01.2023 00:00:00
ESCcursorLeft(LOG_TuiCopy, 19);
fprintf(LOG_TuiCopy, "%s %s", gStrDate, gStrTime);
ESCcursorRight(LOG_TuiCopy, 1);
}
// Do we print the RunTime
if (renderRunTime){
// 00000d 00:00:00
if (renderRealTime){
// left alignment, cause RealTime already exist
ESCcursorLeft(LOG_TuiCopy, renderWidth - 2);
}
else{
// right alignment
ESCcursorLeft(LOG_TuiCopy, 15);
}
fprintf(LOG_TuiCopy, "%s", gStrRunTime);
}
return 1;
}
#define TUIrenderHeader(posX, posY, width, headerID, justRefresh) TUIrenderHeaderFooter(posX, posY, width, &userHeaders[headerID], justRefresh)
#define TUIrenderFooter(posX, posY, width, footerID, justRefresh) TUIrenderHeaderFooter(posX, posY, width, &userFooters[footerID], justRefresh)
int TUIrenderHorzMenu(int posX, int posY, int menuType, TuiMenusSTRUCT *menuDef, int minX, int minY, int maxX, int maxY){
int renderSmall = 0; // just the key + 2 spaces
int renderSelect = 0; // "just the key" + selected key in full width
int renderLen = 0; // full size len of menu positions (gets the final length, too)
int renderStyle = 0; // 0 = full; 1 = small, but full selected; 2 = small
TuiMenuPosSTRUCT *selectedMenu = NULL;
int subXs = 0; // subMenu X
int subXw = 0; // subMenu Width
TUIsecureMinMaxXY(&minX, &minY, &maxX, &maxY);
if (!posX){
// Start at 1st Pos
posX = minX;
}
if (!posY){
if (menuType == TUI_MENU_BOTTOM){
// BottomMenu
posY = maxY;
}
else{
// TopMenu
posY = minY;
}
}
// renderWidth - respecting posX
int renderWidth = maxX - posX + 1;
TuiMenuPosSTRUCT *menuPos = menuDef->pos1st;
// delete small and inverted flags from sub-structure before render
TUIclearSmallInverted(menuPos);
// Calculate len of top-level MenuLine options - without time & date
while (menuPos != NULL){
renderLen += strlen(menuPos->caption);
renderSmall += 3;
if (menuPos->selected && menuPos->enabled){
selectedMenu = menuPos;
renderSelect = strlen(menuPos->caption) - 3;
}
menuPos = menuPos->nextPos;
}
renderSelect += renderSmall;
if (renderLen > renderWidth){
// full menu doesn't fit
renderStyle = 1;
renderLen = renderSelect;
if (renderLen > renderWidth){
// small, but full selected, doesn't fits
renderStyle = 2;
renderLen = renderSmall;
if (renderLen > renderWidth){
// impossible to render
return 0;
}
}
}
// Where to render
ESClocate(LOG_TuiCopy, posX, posY);
menuPos = menuDef->pos1st;
while (menuPos){
if (renderStyle){
// we can't fully render this menu
menuPos->printSmall = -1;
}
if (menuPos->selected && menuPos->enabled){
// selected - start of selected pos
subXs = posX;
if (renderStyle == 1){
// but the selected pos is in full width
menuPos->printSmall = 0;
}
}
posX += TUIprintMenuPos(posX, posY, 0, TUIgetPosLen(menuPos), menuPos, menuDef);
if (menuPos->selected && menuPos->enabled){
// selected - length of selected pos
subXw = posX - subXs;
}
menuPos = menuPos->nextPos;
}
// Fill line with right colored spaces
TUIsetColorStyle(LOG_TuiCopy, menuDef->txtColor, menuDef->txtStyle);
STRprintSpaces(LOG_TuiCopy, renderWidth - renderLen);
if (selectedMenu && selectedMenu->pos1st){
if (menuType == TUI_MENU_BOTTOM){
// BottomMenu
posY--;
maxY--;
}
else{
// TopMenu
posY++;
minY++;
}
if (TUIpreRenderSub(subXs, subXw, selectedMenu->pos1st, minX, maxX)){
TUIrenderSub(subXs, posY, subXw, selectedMenu->pos1st, menuDef, 1, menuType, minX, minY, maxX, maxY);
}
}
return renderWidth;
}
#define TUIrenderTopMenu(menuDef, justRefresh, minX, minY, maxX, maxY) TUIrenderHorzMenu(0, 0, TUI_MENU_TOP, menuDef, minX, minY, maxX, maxY)
#define TUIrenderBottomMenu(menuDef, justRefresh, minX, minY, maxX, maxY) TUIrenderHorzMenu(0, 0, TUI_MENU_BOTTOM, menuDef, minX, minY, maxX, maxY)
int TUIrenderVertMenu(int posX, int posY, int menuType, int doLead, int doTrail, TuiMenusSTRUCT *menuDef, int minX, int minY, int maxX, int maxY){
// Take care on 0, 0, 0, 0
TUIsecureMinMaxXY(&minX, &minY, &maxX, &maxY);
// Take care on 0, 0
if (!posX){
if (menuType == TUI_MENU_RIGHT){
// RightMenu
posX = maxX - 2;
}
else{
// LeftMenu
posX = minX;
}
}
if (!posY){
posY = minY;
}
// TUIclearSmallInverted(menuDef->pos1st); // We can't do this here anymore - could be already set
if (TUIpreRenderSub(posX, 3, menuDef->pos1st, minX, maxX)){
// Leading Line