-
Notifications
You must be signed in to change notification settings - Fork 11
/
egi_surfcontrols.c
2513 lines (2028 loc) · 80.1 KB
/
egi_surfcontrols.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
/*------------------------------------------------------------------
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
Journal:
2021-4-22:
1. Add ESURF_LABEL and its functions.
2. Modify 'egi_surfbtn_' to 'egi_surfBtn_'
3. Add egi_surfBtn_writeFB().
4. Modify 'egi_surfXXX_display' to egi_surfXXX_writeFB'
2021-4-25:
1. Add ESURF_LISTBOX and its functions.
2021-4-29:
1. ESURF_LISTBOX: add memeber 'var_SelectIdx'
2021-5-01:
1. egi_surfListBox_create(): Add params 'fw' and 'fh'.
2021-5-02:
1. Add: egi_surfListBox_addItem(), egi_surfListBox_redraw()
egi_surfListBox_adjustFirstIdx(), egi_surfListBox_PxySelectItem()
2021-5-05:
1. ESURF_LISTBOX: add memeber 'GuideBlockDownHold'.
2. Add: egi_surfListBox_adjustPastPos()
2021-5-13:
2021-5-14:
2021-5-15:
1. Add ESURF_MENULIST and its functions.
2021-5-16:
1. Add egi_surfMenuList_runMenuProg()
2021-5-18:
1. egi_surfMenuList_updatePath(): Apply sub_MenuList.(x0,y0) to offset
its normal alignment position.
2021-6-1:
1. Add member 'front_imgbuf" for ESURF_LABEL.
2021-07-2:
1. egi_surfLab_free(); to free (*lab)->imgbuf_effect.
2021-07-11:
1. egi_surfLab_updateText(): to use va_list.
2021-07-19/20:
1. Add egi_crun_stdSurfInfo().
2. Add egi_crun_stdSurfConfirm().
2021-07-23:
1 egi_point_on_surfBox(), egi_point_on_surfBtn(), egi_point_on_surfTickBox():
For touched point, also check imgbuf->alpha value of the surfBox.
2022-04-28/29:
1. Add egi_surfMenuList_getMaxOffset().
2. Add egi_surfMenuList_getMaxLayoutSize().
2022-05-03:
1. Add egi_surfMenuList_getItemID().
2022-05-05:
1. Add menulist position mode 'MENULIST_ROOTMODE_LEFT_TOP' for:
egi_surfMenuList_getMaxOffset()
egi_surfMenuList_writeFB()
egi_surfMenuList_updatePath()
2022-06-21:
1. Add EGI_VSCROLLBAR and its functions:
egi_VScrollBar_create(), egi_VScrollBar_free(), egi_VScrollBar_writeFB()
2022-06-22:
1. Add egi_VScrollBar_pxyInVSBar().
2. Add egi_VScrollBar_clickUpdateValues().
2022-06-23:
1. Add egi_VScrollBar_dyUpdateValues()
2. Add egi_VScrollBar_pxyOnGuide()
Midas Zhou
[email protected](Not in use since 2022_03_01)
知之者不如好之者好之者不如乐之者
------------------------------------------------------------------*/
#include "egi_surfcontrols.h"
#include "egi_surface.h"
#include "egi_utils.h"
#include "egi_debug.h"
#include "egi_log.h"
#include "egi_timer.h"
#include <unistd.h>
/*---------------------------------------------------------------
Create an ESURF_BOX and blockcopy its imgbuf from input imgbuf.
@imgbuf An EGI_IMGBUF holds image icons.
@xi,yi Box image origin relative to its continer.
@x0,y0 Box origin relative to its container.
@w,h Width and height of the button image.
Return:
A pointer to ESURF_BOX ok
NULL Fails
----------------------------------------------------------------*/
ESURF_BOX *egi_surfBox_create(EGI_IMGBUF *imgbuf, int xi, int yi, int x0, int y0, int w, int h)
{
ESURF_BOX *box=NULL;
if(imgbuf==NULL)
return NULL;
/* w/h check inside egi_image functions */
//if(w<1 || h<1)
// return NULL;
/* Calloc SURFBOX */
box=calloc(1, sizeof(ESURF_BOX));
if(box==NULL)
return NULL;
/* To copy a block from imgbuf as box image */
box->imgbuf=egi_imgbuf_blockCopy(imgbuf, xi, yi, h, w);
if(box->imgbuf==NULL) {
free(box);
return NULL;
}
/* Assign members */
box->x0=x0;
box->y0=y0;
return box;
}
/*---------------------------------------
Free an ESURF_BOX.
---------------------------------------*/
void egi_surfBox_free(ESURF_BOX **box)
{
if(box==NULL || *box==NULL)
return;
/* Free imgbuf */
egi_imgbuf_free((*box)->imgbuf);
/* Free struct */
free(*box);
*box=NULL;
}
/*-----------------------------------------------------
Check if point (x,y) in on the SURFBOX.
If box->imgbuf has ALPHA values, then it returns FALSE
if alpha value of the point is ZERO!
@box pointer to ESURF_BOX
@x,y Point coordinate, relative to its contaier!
Return:
True or False.
------------------------------------------------------*/
inline bool egi_point_on_surfBox(const ESURF_BOX *box, int x, int y)
{
int off;
if( box==NULL || box->imgbuf==NULL )
return false;
if( x < box->x0 || x > box->x0 + box->imgbuf->width -1 )
return false;
if( y < box->y0 || y > box->y0 + box->imgbuf->height -1 )
return false;
/* If alpha is 0 */
if( box->imgbuf->alpha ) {
off=(y-box->y0)*box->imgbuf->width+(x-box->x0);
if(box->imgbuf->alpha[off]==0)
return false;
}
return true;
}
/*-----------------------------------------
Display an ESURF_BOX onto the FBDEV.
@fbdev: Pointer to FBDEV
@box: Pointer to ESURF_BOX
@cx0,cy0: Its container origin x0,y0
relative to FBDEV.
-----------------------------------------*/
void egi_surfBox_writeFB(FBDEV *fbdev, const ESURF_BOX *box, int cx0, int cy0) {
if( box==NULL || fbdev==NULL )
return;
egi_subimg_writeFB(box->imgbuf, fbdev, 0, -1, cx0+box->x0, cy0+box->y0); /* imgbuf, fb, subnum, subcolor, x0,y0 */
}
/*---------------------------------------------------------------
Create an ESURF_LABEL and blockcopy its imgbuf from input imgbuf.
@imgbuf An EGI_IMGBUF holds image.
@xi,yi Label bkg image origin relative to its container.
@x0,y0 Label origin relative to its container.
@w,h Width and height of the label.
Return:
A pointer to ESURF_LABEL ok
NULL Fails
----------------------------------------------------------------*/
ESURF_LABEL *egi_surfLab_create(EGI_IMGBUF *imgbuf, int xi, int yi, int x0, int y0, int w, int h)
{
ESURF_LABEL *lab=NULL;
if(imgbuf==NULL)
return NULL;
/* w/h check inside egi_image functions */
//if(w<1 || h<1)
// return NULL;
/* Calloc ESURF_LAB */
lab=calloc(1, sizeof(ESURF_LABEL));
if(lab==NULL)
return NULL;
/* To copy a block from imgbuf as box image */
lab->imgbuf=egi_imgbuf_blockCopy(imgbuf, xi, yi, h, w);
if(lab->imgbuf==NULL) {
free(lab);
return NULL;
}
/* Assign members */
lab->x0=x0;
lab->y0=y0;
lab->w=w;
lab->h=h;
return lab;
}
/*---------------------------------------
Free an ESURF_LABEL.
---------------------------------------*/
void egi_surfLab_free(ESURF_LABEL **lab)
{
if(lab==NULL || *lab==NULL)
return;
/* Free imgbuf */
egi_imgbuf_free((*lab)->imgbuf);
/* Free imgbuf_effect */
egi_imgbuf_free((*lab)->imgbuf_effect);
/* Free struct */
free(*lab);
*lab=NULL;
}
/*-----------------------------------------
Display an ESURF_LABEL onto the FBDEV.
@fbdev: Pointer to FBDEV
@lab: Pointer to ESURF_BOX
@face: Font face
@fw,fh: Font size
@color: Text color
@cx0,cy0: Its container origin x0,y0
relative to FBDEV.
-----------------------------------------*/
void egi_surfLab_writeFB(FBDEV *fbdev, const ESURF_LABEL *lab, FT_Face face, int fw, int fh, EGI_16BIT_COLOR color, int cx0, int cy0)
{
if( lab==NULL || fbdev==NULL )
return;
/* Bkg image */
if(lab->imgbuf)
egi_subimg_writeFB(lab->imgbuf, fbdev, 0, -1, cx0+lab->x0, cy0+lab->y0); /* imgbuf, fb, subnum, subcolor, x0,y0 */
/* Front image */
if(lab->front_imgbuf)
egi_subimg_writeFB(lab->front_imgbuf, fbdev, 0, -1, cx0+lab->x0, cy0+lab->y0); /* imgbuf, fb, subnum, subcolor, x0,y0 */
/* Write label text on bkg */
if( lab->text )
FTsymbol_uft8strings_writeFB( fbdev, face, /* FBdev, fontface */
fw, fh, (UFT8_PCHAR)lab->text, /* fw,fh, pstr */
lab->w, 1, 0, /* pixpl, lines, fgap */
cx0+lab->x0, cy0+lab->y0, /* x0,y0, */
color, -1, 255, /* fontcolor, transcolor,opaque */
NULL, NULL, NULL, NULL); /* int *cnt, int *lnleft, int* penx, int* peny */
}
#if 0 /* ----- Use strncpy() ----- */
/*-------------------------------------
Update Label text.
@lab: Pointer to ESUR_LABEL
@text: Text for label.
-------------------------------------*/
void egi_surfLab_updateText( ESURF_LABEL *lab, const char *text )
{
if(lab==NULL || text==NULL)
return;
strncpy( lab->text, text, ESURF_LABTEXT_MAX-1 );
lab->text[ESURF_LABTEXT_MAX-1]='\0';
}
#else /* ----- Use va_list ---- */
/*-------------------------------------
Update Label text with format and args.
@lab: Pointer to ESUR_LABEL
@fmt: Format, ...and args.
-------------------------------------*/
void egi_surfLab_updateText( ESURF_LABEL *lab, const char *fmt, ... )
{
va_list arg;
if(lab==NULL || fmt==NULL)
return;
va_start(arg, fmt);
vsnprintf(lab->text, ESURF_LABTEXT_MAX-1, fmt, arg);
va_end(arg);
}
#endif
/*---------------------------------------------------------------
Create an ESURF_BTN and blockcopy its imgbuf from input imgbuf.
@imgbuf An EGI_IMGBUF holds image icons.
@xi,yi Button image origin relative to its container.
@x0,y0 Button origin relative to its container.
@w,h Width and height of the button image.
Return:
A pointer to ESURF_BTN ok
NULL Fails
----------------------------------------------------------------*/
ESURF_BTN *egi_surfBtn_create(EGI_IMGBUF *imgbuf, int xi, int yi, int x0, int y0, int w, int h)
{
ESURF_BTN *sbtn=NULL;
if(imgbuf==NULL)
return NULL;
/* w/h check inside egi_image functions */
//if(w<1 || h<1)
// return NULL;
/* Calloc SURFBTN */
sbtn=calloc(1, sizeof(ESURF_BTN));
if(sbtn==NULL)
return NULL;
/* To copy a block from imgbuf as button image */
sbtn->imgbuf=egi_imgbuf_blockCopy(imgbuf, xi, yi, h, w);
if(sbtn->imgbuf==NULL) {
egi_dpstd("Fail to blockCopy imgbuf!\n");
free(sbtn);
return NULL;
}
/* Assign members */
sbtn->x0=x0;
sbtn->y0=y0;
return sbtn;
}
/*---------------------------------------
Free an ESURF_BTN.
---------------------------------------*/
void egi_surfBtn_free(ESURF_BTN **sbtn)
{
if(sbtn==NULL || *sbtn==NULL)
return;
/* Free imgbuf */
egi_imgbuf_free((*sbtn)->imgbuf);
egi_imgbuf_free((*sbtn)->imgbuf_effect);
egi_imgbuf_free((*sbtn)->imgbuf_pressed);
/* Free struct */
free(*sbtn);
*sbtn=NULL;
}
/*-----------------------------------------
Display an ESURF_BTN onto the FBDEV.
@fbdev: Pointer to FBDEV
@btn: Pointer to ESURF_BTN
@type: Type of imgbuf.
SURFBTN_IMGBUF_NORMAL/EFFECT/PRESSED.
@cx0,cy0: Its container origin x0,y0
relative to FBDEV.
-----------------------------------------*/
void egi_surfBtn_writeFB(FBDEV *fbdev, const ESURF_BTN *btn, int type, int cx0, int cy0)
{
if( btn==NULL || fbdev==NULL )
return;
switch(type) {
case SURFBTN_IMGBUF_NORMAL:
egi_subimg_writeFB(btn->imgbuf, fbdev, 0, -1, cx0+btn->x0, cy0+btn->y0); /* imgbuf, fb, subnum, subcolor, x0,y0 */
break;
case SURFBTN_IMGBUF_EFFECT:
egi_subimg_writeFB(btn->imgbuf_effect, fbdev, 0, -1, cx0+btn->x0, cy0+btn->y0);
break;
case SURFBTN_IMGBUF_PRESSED:
egi_subimg_writeFB(btn->imgbuf_pressed, fbdev, 0, -1, cx0+btn->x0, cy0+btn->y0);
break;
}
}
/* ------------ :: To call egi_point_on_surfBox() intead! -----------
Check if point (x,y) in on the SURFBTN.
@sbtn pointer to ESURF_BTN
@x,y Point coordinate, relative to its contaier!
Return:
True or False.
-----------------------------------------------------------------------*/
inline bool egi_point_on_surfBtn(const ESURF_BTN *sbtn, int x, int y)
{
#if 1 /* Call egi_point_on_surfBox() intead! */
return egi_point_on_surfBox((ESURF_BOX *)sbtn, x,y);
#else
int off;
if( sbtn==NULL || sbtn->imgbuf==NULL )
return false;
if( x < sbtn->x0 || x > sbtn->x0 + sbtn->imgbuf->width -1 )
return false;
if( y < sbtn->y0 || y > sbtn->y0 + sbtn->imgbuf->height -1 )
return false;
/* If alpha is 0 */
if( box->imgbuf->alpha ) {
off=(y-box->y0)*box->imgbuf->width+(x-box->x0);
if(box->imgbuf->alpha[off]==0)
return false;
}
return true;
#endif
}
/*---------------------------------------------------------------
Create an ESURF_TICKBOX and blockcopy its imgbuf from input imgbuf.
Only tbox->imgbuf_unticked is filled. imgbuf_ticked to be filled
during XXX_firstdraw_XXX().
TODO: NOW only square type.
@imgbuf An EGI_IMGBUF holds image icons.
@xi,yi TICKBOX.imgbuf origin relative to its container(input imgbuf).
@x0,y0 TICKBOX origin relative to its container.
@w,h Width and height of the tickbox.
Return:
A pointer to ESURF_TICKBOX ok
NULL Fails
----------------------------------------------------------------*/
ESURF_TICKBOX *egi_surfTickBox_create(EGI_IMGBUF *imgbuf, int xi, int yi, int x0, int y0, int w, int h)
{
ESURF_TICKBOX *tbox=NULL;
if(imgbuf==NULL)
return NULL;
/* w/h check inside egi_image functions */
//if(w<1 || h<1)
// return NULL;
/* Calloc SURFBTN */
tbox=calloc(1, sizeof(ESURF_TICKBOX));
if(tbox==NULL)
return NULL;
/* To copy a block from imgbuf as image for unticked box */
tbox->imgbuf_unticked=egi_imgbuf_blockCopy(imgbuf, xi, yi, h, w);
if(tbox->imgbuf_unticked==NULL) {
free(tbox);
return NULL;
}
/* tbox->imgbuf_unticked to be filled in XXX_firstdraw_XXX() */
/* Assign members */
tbox->x0=x0;
tbox->y0=y0;
tbox->ticked=false;
return tbox;
}
/*---------------------------------------
Free an ESURF_TICKBOX.
---------------------------------------*/
void egi_surfTickBox_free(ESURF_TICKBOX **tbox)
{
if(tbox==NULL || *tbox==NULL)
return;
/* Free imgbuf */
egi_imgbuf_free((*tbox)->imgbuf_unticked);
egi_imgbuf_free((*tbox)->imgbuf_ticked);
/* Free struct */
free(*tbox);
*tbox=NULL;
}
/*-------------------------------------------
Check whether point (x,y) in on the TICKBOX.
@tbox pointer to ESURF_TICKBOX
@x,y Point coordinate, relative to it contianer.
Return:
True or False.
--------------------------------------------*/
inline bool egi_point_on_surfTickBox(const ESURF_TICKBOX *tbox, int x, int y)
{
#if 1 /* Call egi_point_on_surfBox() instead! */
return egi_point_on_surfBox((ESURF_BOX *)tbox, x, y);
#else
int off;
if( tbox==NULL || tbox->imgbuf_unticked==NULL )
return false;
if( x < tbox->x0 || x > tbox->x0 + tbox->imgbuf_unticked->width -1 )
return false;
if( y < tbox->y0 || y > tbox->y0 + tbox->imgbuf_unticked->height -1 )
return false;
/* If alpha is 0 */
if( box->imgbuf->alpha ) {
off=(y-box->y0)*box->imgbuf->width+(x-box->x0);
if(box->imgbuf->alpha[off]==0)
return false;
}
return true;
#endif
}
/*-----------------------------------------
Display an ESURF_TICKBOX on the FBDEV.
@fbdev: Pointer to FBDEV
@tbox: Pointer to ESURF_TICKBOX
@cx0,cy0: Its container origin x0,y0
relative to FBDEV.
-----------------------------------------*/
void egi_surfTickBox_writeFB(FBDEV *fbdev, const ESURF_TICKBOX *tbox, int cx0, int cy0)
{
if( tbox==NULL || fbdev==NULL )
return;
if(tbox->ticked)
/* imgbuf, fb, subnum, subcolor, x0,y0 */
egi_subimg_writeFB(tbox->imgbuf_ticked, fbdev, 0, -1, cx0+tbox->x0, cy0+tbox->y0);
else
egi_subimg_writeFB(tbox->imgbuf_unticked, fbdev, 0, -1, cx0+tbox->x0, cy0+tbox->y0);
}
/*-----------------------------------------------------------
Create a MenuList.
@mode: Root MenuList position type.
ROOT_LEFT_BOTTOM ROOT_RIGHT_BOTTO
ROOT_LEFT_TOP ROOT_RIGHT_TOP
@x0,y0: For root MenuList: Origin relative to its container
Other sub_MenuList: Offset to normal aligment postion.
Usually just take (0,0), unless you don't keep normal
alignment of MenuList to MenuList.
@mw,mh: Size for each menu item/piece.
@FT_Face: Font face.
@fw,fh: Font size.
@capacity: MAX./capable number of menu itmes in the list.
@root: TRUE: If a root MenuList.
Return:
An pointer to ESURF_MENULIST Ok
NULL Fails
------------------------------------------------------------*/
ESURF_MENULIST *egi_surfMenuList_create( int mode, bool root, int x0, int y0, int mw, int mh,
FT_Face face, int fw, int fh, unsigned int capacity)
{
if( mw <1 || mh <1 )
return NULL;
if( capacity < 1 )
return NULL;
/* 1. Calloc mlist */
ESURF_MENULIST *mlist = calloc(1, sizeof(ESURF_MENULIST));
if(mlist==NULL)
return NULL;
/* 2. Calloc mitems */
mlist->mitems = calloc(capacity, sizeof(EGI_MENUITEM));
if(mlist->mitems==NULL) {
free(mlist);
return NULL;
}
/* 3. Calloc path for root MenuList */
if(root) {
mlist->path= calloc(ESURF_MENULIST_DEPTH_MAX, sizeof(ESURF_MENULIST*));
if(mlist->path==NULL) {
free(mlist->mitems);
free(mlist);
return NULL;
}
/* Assign path[0] to itself */
mlist->path[0]=mlist;
}
/* 4. Assign memebers */
mlist->mode=mode;
mlist->root=root;
mlist->node=-1; /* Default NOT NodeItem */
mlist->x0=x0;
mlist->y0=y0;
mlist->mw=mw;
mlist->mh=mh;
mlist->face=face;
if(fw<1 || fh<1) {
fw=12;
fh=12;
}
mlist->fw=fw;
mlist->fh=fh;
mlist->capacity=capacity;
/* Set default color, User can revise it. */
mlist->bkgcolor=ESURF_MENULIST_BKGCOLOR;
mlist->sidecolor=ESURF_MENULIST_SIDECOLOR;
mlist->hltcolor=ESURF_MENULIST_HLTCOLOR;
mlist->fontcolor=ESURF_MENULIST_FONTCOLOR;
/* If only a root MenuList, without any sub_MenuList, then mlist->depth==0 */
/* Final selected MenuItem index: For root sub_MenuList Only. */
mlist->fidx = -1;
return mlist;
}
/*-----------------------------------------------------------
Free an MenuList.
!!! --- This is a recursive function --- !!!
mlist: An pointer to ESURF_MENULIST
------------------------------------------------------------*/
void egi_surfMenuList_free(ESURF_MENULIST **mlist)
{
int i;
if( mlist==NULL || *mlist==NULL )
return;
/* Free itemm menus */
for(i=0; i < (*mlist)->mcnt; i++) {
/* Free name */
free( (*mlist)->mitems[i].descript);
/* Free linked MenuList */
egi_surfMenuList_free(&((*mlist)->mitems[i].mlist));
}
/* If ROOT */
if( (*mlist)->path )
free( (*mlist)->path );
free(*mlist);
*mlist=NULL;
}
/*-----------------------------------------------------------------
Get Max. offset values for a menulist tree.
!!! --- This is a recursive function --- !!!
Nonte:
1. The very first call MUST input a root menulist.
@mlist: A pointer to a ESURF_MENULIST
@Ox,Oy: Origin coordinate of the mlist relative to ROOT_ORIGIN.
(Relative to Screen COORD, OXY COORD: +OY: -Y direction, +OX: +X direction )
(while mlist->x0/y0 is under Screen COORD )
@offYU,offYD: >=0! Max. offset from ROOT_ORIGIN to Y Upper/Down most.
(Relative to Screen COORD: +YU: -Y direction; +YD: +Y direction; from menulist origin )
Input value is current MAX. value without considering mlist.
@offXR,offXL: >=0! Max. offset from ROOT_ORIGIN to X Right/Left most.
(Relative to Screen COORD, +XR: +X direction; +XL: -X direction; from menulist origin )
Init value should be offset from the ROOT ORIGIN.
Input value is current MAX. value without considering mlist.
Return:
0 OK
<0 Fails
------------------------------------------------------------------*/
int egi_surfMenuList_getMaxOffset(const ESURF_MENULIST *mlist, int Ox, int Oy, int *offYU, int *offYD, int *offXR, int *offXL)
{
int k; // int yu,yd,xr,xl;
int maxu,maxd,maxr,maxl; /* >=0; tmpe. offYU,YD,XR,XL */
int tmpOx, tmpOy;
if(mlist==NULL)
return -1;
/* 1. If root, reset all offset value to 0 */
if(mlist->root) {
Ox=0;
Oy=0;
*offYU=0; *offYD=0;
*offXR=0; *offXL=0;
}
/* 2. Select mode case */
switch(mlist->mode) {
case MENULIST_ROOTMODE_LEFT_BOTTOM:
/* 2a.1 Compute max. offset values. for this menulist. */
if(mlist->root) { /* 2a.1.1 */
maxu=0+mlist->mcnt*mlist->mh;
maxd=0;
maxr=0 +mlist->mw;
maxl=0;
}
else { /* 2a.1.2 */
maxu=Oy+mlist->mcnt*mlist->mh -mlist->y0;
maxd=-Oy +mlist->y0; /* -Oy: Down */
maxr=Ox +mlist->mw; /* -----> OVERLADP and x0 considered in 2a.4 */
maxl= -Ox-mlist->x0; /* -Ox: Left */
}
/* 2a.3. Adjust to get MAX. offset values considering this menulist. */
if(maxu<*offYU) maxu=*offYU;
if(maxd<*offYD) maxd=*offYD;
if(maxr<*offXR) maxr=*offXR;
if(maxl<*offXL) maxl=*offXL;
egi_dpstd("LEFT_BOTTOM: maxu=%d, maxd=%d, maxr=%d, maxl=%d\n", maxu,maxd,maxr,maxl);
/* 2a.4 Update OxOy AFT updating maxu/d/r/l. Hello....MidasHK_2022-05-02 */
Oy -=mlist->y0;
Ox +=mlist->x0;
/* 2a.5 Traverse sub_menulist items. */
tmpOx=Ox; tmpOy=Oy; /* Backup OXY */
for(k=0; k< mlist->mcnt; k++) {
Ox=tmpOx; Oy=tmpOy; /* Restore OxOy, in case changed in subitems */
if( mlist->mitems[k].mlist ) { /* Sub Menulist */
egi_dpstd(DBG_YELLOW"item[%d]: %s\n"DBG_RESET, k, mlist->mitems[k].descript);
/* Origin of the mitems[], relavite to ROOT_ORIGIN. */
Ox=Ox +mlist->mw -ESURF_MENULIST_OVERLAP; /* ----> x0 added in 2a.1.2 as in next recursive call. */
Oy=Oy +k*mlist->mh; /* +Oy: Upp, ----> y0 added in 2a.1.2 as in next recursive call */
egi_surfMenuList_getMaxOffset(mlist->mitems[k].mlist, Ox, Oy, &maxu, &maxd, &maxr, &maxl);
}
}
/* 2a.5 Pass out */
*offYU=maxu;
*offYD=maxd;
*offXR=maxr;
*offXL=maxl;
break;
case MENULIST_ROOTMODE_LEFT_TOP:
/* 2b.1 Compute max. offset values. for this menulist. */
if(mlist->root) { /* 2b.1.1 */
//maxu=0+mlist->mcnt*mlist->mh;
maxu=0;
//maxd=0;
maxd=mlist->mcnt*mlist->mh;
maxr=0 +mlist->mw;
maxl=0;
}
else { /* 2b.1.2 */
//maxu=Oy+mlist->mcnt*mlist->mh -mlist->y0;
maxu=Oy-mlist->y0;
//maxd=-Oy +mlist->y0; /* -Oy: Down */
maxd=-Oy+mlist->mcnt*mlist->mh +mlist->y0;
maxr=Ox +mlist->mw; /* -----> OVERLADP and x0 considered in 2a.4 */
maxl= -Ox-mlist->x0; /* -Ox: Left */
}
/* 2b.3. Adjust to get MAX. offset values considering this menulist. */
if(maxu<*offYU) maxu=*offYU;
if(maxd<*offYD) maxd=*offYD;
if(maxr<*offXR) maxr=*offXR;
if(maxl<*offXL) maxl=*offXL;
egi_dpstd("LEFT_TOP: maxu=%d, maxd=%d, maxr=%d, maxl=%d\n", maxu,maxd,maxr,maxl);
/* 2b.4 Update OxOy AFT updating maxu/d/r/l, for Sub_menulist origin calculation. */
Oy -=mlist->y0;
Ox +=mlist->x0;
/* 2b.5 Traverse sub_menulist items. */
tmpOx=Ox; tmpOy=Oy; /* Backup OXY */
for(k=0; k< mlist->mcnt; k++) {
Ox=tmpOx; Oy=tmpOy; /* Restore OxOy, in case changed in subitems */
if( mlist->mitems[k].mlist ) { /* Sub Menulist */
egi_dpstd(DBG_YELLOW"item[%d]: %s\n"DBG_RESET, k, mlist->mitems[k].descript);
/* Origin of the mitems[], relavite to ROOT_ORIGIN. */
Ox=Ox +mlist->mw -ESURF_MENULIST_OVERLAP; /* ----> x0 added in 2a.1.2 in next recursive call. */
//Oy=Oy +k*mlist->mh; /* +Oy: Upp, ----> y0 added in 2a.1.2 as next in recursive call */
Oy=Oy -k*mlist->mh; /* +Oy: Upp, ----> y0 in 2a.1.2 as in next recursive call */
egi_surfMenuList_getMaxOffset(mlist->mitems[k].mlist, Ox, Oy, &maxu, &maxd, &maxr, &maxl);
}
}
/* 2a.5 Pass out */
*offYU=maxu;
*offYD=maxd;
*offXR=maxr;
*offXL=maxl;
break;
case MENULIST_ROOTMODE_RIGHT_BOTTOM :
case MENULIST_ROOTMODE_RIGHT_TOP:
break;
}
return 0;
}
/*--------------------------------------------
Get MAX. layout size for a menulist tree.
@mlist: A pointer to a ESURF_MENULIST
@w,h: To pass out max. layout size.
Return:
0 OK
<0 Fails
----------------------------------------------*/
int egi_surfMenuList_getMaxLayoutSize(const ESURF_MENULIST *mlist, int *w, int *h)
{
int Ox, Oy;
int offYU, offYD, offXR, offXL;
if(!mlist->root)
return -1;
Ox=Oy=0;
offYU=offYD=offXR=offXL=0;
if(egi_surfMenuList_getMaxOffset(mlist, Ox, Oy, &offYU, &offYD, &offXR, &offXL)<0)
return -2;
if(w) *w=offXR+offXL;
if(h) *h=offYU+offYD;
return 0;
}
/*---------------------------------------------------------------------
Draw a root MenuList with expanded/visible sub_MenuLists.
!!! --- This is a recursive function --- !!!
@FBDEV: Pointer to FBDEV.
@mlist: A pointer to a ESURF_MENULIST.
@offx, offy: Offset to mlist->(x0,y0).
For a root MenuList usually: 0,0.
For a sub_MenuList, offsetXY from its upper level MenuList origin.
@select_idx: Selected Menu index, as of mlist->mitems[].
If mlist is root MenuList, then select_idx will be checked/calcluated.
<0, Ignore.
--------------------------------------------------------------------*/
void egi_surfMenuList_writeFB(FBDEV *fbdev, const ESURF_MENULIST *mlist, int offx, int offy, int select_idx)
{
int i;
if(fbdev==NULL || mlist==NULL)
return;
/* Check select_idx */
if( mlist->root ) {
if(mlist->depth >0)
select_idx = mlist->path[1]->node; /* Node number of next sub MenuList */
else /* ==0 */
select_idx = mlist->fidx; /* As index of selected item */
}
switch(mlist->mode) {
case MENULIST_ROOTMODE_LEFT_BOTTOM:
/* 1. Draw MenuList bkgcolor */
draw_filled_rect2(fbdev, mlist->bkgcolor, mlist->x0 +offx, mlist->y0 +offy,
mlist->x0 +offx +mlist->mw-1, mlist->y0 +offy -(mlist->mh*mlist->mcnt)-1 );
/* Rim/Edge Lines */
//fbset_color2(fbdev, mlist->sidecolor);
fbset_color2(fbdev, COLOR_COMPLEMENT_16BITS(mlist->bkgcolor));
//draw_rect(fbdev, mlist->x0 +offx, mlist->y0 +offy,
// mlist->x0 +offx +mlist->mw-1, mlist->y0 +offy -(mlist->mh*mlist->mcnt)-1 ); /* width 1pix */
draw_wrect(fbdev, mlist->x0 +offx, mlist->y0 +offy,
mlist->x0 +offx +mlist->mw-1, mlist->y0 +offy -(mlist->mh*mlist->mcnt)-1, 2); /* width 2pix */
/* 2. Draw description and mark for each MenuItem */
for(i=0; i < mlist->mcnt; i++) {
/* Highlight color on selected MenuItem */
/* Ignore select_idx <0 */
if( select_idx == i ) // xxx -- fidx MAYBE not cleared as -1 !!!
//draw_filled_rect2( fbdev, mlist->hltcolor, x1,y1, x2,y2 );
draw_blend_filled_rect( fbdev,
mlist->x0 +offx +1, mlist->y0 +offy -i*mlist->mh -1, /* x1,y1 */
mlist->x0 +offx +mlist->mw -1 -1, mlist->y0 +offy -(i+1)*mlist->mh -1 +1, /* x2,y2 */
mlist->hltcolor, 220 );
/* Write Description */
FTsymbol_uft8strings_writeFB( fbdev, mlist->face, /* FBdev, fontface */
mlist->fw, mlist->fh, (const UFT8_PCHAR)mlist->mitems[i].descript, /* fw,fh, pstr */
mlist->mw -15, 1, 0, /* pixpl, lines, fgap */
mlist->x0 +offx -ESURF_MENULIST_OVERLAP +15, mlist->y0 +offy -(i+1)*mlist->mh +5, /* x0,y0, */
mlist->fontcolor, -1, 220, /* fontcolor, transcolor,opaque */
NULL, NULL, NULL, NULL ); /* int *cnt, int *lnleft, int* penx, int* peny */
/* Draw NodeItem mark '>' */
if( mlist->mitems[i].mlist !=NULL) {
const char* nodemark=">";
int pixlen=FTsymbol_uft8strings_pixlen( mlist->face, mlist->fw, mlist->fh, (const UFT8_PCHAR)nodemark);
if(pixlen>0)
FTsymbol_uft8strings_writeFB( fbdev, mlist->face, /* FBdev, fontface */
mlist->fw, mlist->fh, (const UFT8_PCHAR)nodemark, /* fw,fh, pstr */
mlist->mw, 1, 0, /* pixpl, lines, fgap */
mlist->x0 +offx +mlist->mw -pixlen-ESURF_MENULIST_OVERLAP-5, /* x0, y0 */
mlist->y0 +offy -(i+1)*mlist->mh +5,
mlist->fontcolor, -1, 240, /* fontcolor, transcolor,opaque */
NULL, NULL, NULL, NULL ); /* int *cnt, int *lnleft, int* penx, int* peny */
}
}
/* 3. If root_MenuList: Draw all sub_MenuLists in path[]. */
if( mlist->root ) {
/* i=0 as root_MenuList, already finish drawing, see above. */
i=1;
/* Sub_MenList offsetX/Y are all (0,0)s
* Convert to origin relative to root_MenuList container.
*/
offx = mlist->x0 +offx;
offy = mlist->y0 +offy;
/* TODO: mlist->path[] MAY NOT cleared, MUST check depth value! */
while( mlist->path[i] && i< mlist->depth+1 ) {
/* Cal. offset, relative to origin of the root MenuList */
if(i==1) { /* OK, mlist->path[0] pointed to root mlist. */
//offx += mlist->mw -ESURF_MENULIST_OVERLAP; /* --- overlap effect */
//offy -= mlist->mh * (mlist->path[1])->node;
offx += (mlist->path[0])->mw -ESURF_MENULIST_OVERLAP; /* --- overlap effect */
offy += -(mlist->path[0])->mh * (mlist->path[1])->node;
} else {
offx += (mlist->path[i-1])->mw -ESURF_MENULIST_OVERLAP +(mlist->path[i-1])->x0; /* Hi! MidasHK_2022-05-02 */
offy += -(mlist->path[i-1])->mh * (mlist->path[i])->node +(mlist->path[i-1])->y0;
}
/* Cal. select_index */
/* Recursive calling */
//egi_dpstd("Draw mlist->path[%d]...\n", i);
egi_surfMenuList_writeFB(fbdev, mlist->path[i], offx, offy,
i==mlist->depth ? mlist->path[i]->fidx : mlist->path[i+1]->node ); /* Selected menu index */
/* If ROOT or END menulist, take as fidx. */
i++;
}