-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractMenu.c
1066 lines (971 loc) · 29.4 KB
/
AbstractMenu.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-
* Copyright (c) 1997-2002 The Protein Laboratory, University of Copenhagen
* All rights reserved.
*
* 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 AUTHOR 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 AUTHOR 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.
*
* $Id$
*/
#include "apricot.h"
#include "AbstractMenu.h"
#include "Image.h"
#include "Menu.h"
#include "Widget.h"
#include <AbstractMenu.inc>
#ifdef __cplusplus
extern "C" {
#endif
#undef my
#define inherited CComponent->
#define my ((( PAbstractMenu) self)-> self)
#define var (( PAbstractMenu) self)
typedef Bool MenuProc ( Handle self, PMenuItemReg m, void * params);
typedef MenuProc *PMenuProc;
static int
key_normalize( const char * key)
{
/*
* Valid keys:
* keycode as a string representing decimal number;
* any combination of ^, @, # (Control, Alt, Shift) plus
* exactly one character - lowercase and get ascii code of this
* fN - function key, N is a number from 1 to 16 inclusive
* All other combinations will result in kbNoKey returned
*/
int r = 0, r1;
for (;;) {
if (*key == '^')
r |= kmCtrl;
else if (*key == '@')
r |= kmAlt;
else if (*key == '#')
r |= kmShift;
else
break;
key++;
}
if (!*key) return kbNoKey; /* #, ^, @ alone are not allowed */
if (!key[1]) {
return (r&kmCtrl) && isalpha(*key) ? r | (toupper(*key)-'@') : r | tolower(*key);
} else {
char *e;
if (isdigit(*key)) {
if (r) return kbNoKey;
r = strtol( key, &e, 10);
if (*e) return kbNoKey;
if ( !( r & kmCtrl)) return r;
return ( isalpha( r & kbCharMask)) ?
( r & kbModMask) | ( toupper( r & kbCharMask)-'@') :
r;
} else if (tolower(*key) != 'f')
return kbNoKey;
key++;
r1 = strtol( key, &e, 10);
if (*e || r1 < 1 || r1 > 16) return kbNoKey;
return r | (kbF1 + ((r1-1) << 8));
}
}
static int
is_var_id_name( char * name)
{
int ret;
char * e;
if ( !name || *(name++) != '#') return 0;
ret = strtol( name, &e, 10);
if ( *e || ret < 0) return 0;
return ret;
}
void
AbstractMenu_dispose_menu( Handle self, void * menu)
{
PMenuItemReg m = ( PMenuItemReg) menu;
if ( m == nil) return;
free( m-> text);
free( m-> accel);
free( m-> variable);
free( m-> perlSub);
if ( m-> code) sv_free( m-> code);
if ( m-> data) sv_free( m-> data);
if ( m-> bitmap) {
if ( PObject( m-> bitmap)-> stage < csDead)
SvREFCNT_dec( SvRV(( PObject( m-> bitmap))-> mate));
unprotect_object( m-> bitmap);
}
my-> dispose_menu( self, m-> next);
my-> dispose_menu( self, m-> down);
free( m);
}
/* #define log_write debug_write */
void *
AbstractMenu_new_menu( Handle self, SV * sv, int level)
{
AV * av;
int i, count;
int n;
PMenuItemReg m = nil;
PMenuItemReg curr = nil;
Bool rightAdjust = false;
/* char buf [ 200];
memset( buf, ' ', 200);
buf[ level * 3] = '\0'; */
if ( level == 0)
{
if ( SvTYPE( sv) == SVt_NULL) return nil; /* null menu */
}
if ( !SvROK( sv) || ( SvTYPE( SvRV( sv)) != SVt_PVAV)) {
warn("RTC0034: menu build error: menu is not an array");
return nil;
}
av = (AV *) SvRV( sv);
n = av_len( av);
if ( n == -1) {
if ( level == 0) return nil; /* null menu */
warn("RTC003E: menu build error: empty array passed");
return nil;
}
/* log_write("%s(%d){", buf, n+1); */
/* cycling the list of items */
for ( i = 0; i <= n; i++)
{
SV **itemHolder = av_fetch( av, i, 0);
AV *item;
SV *subItem;
PMenuItemReg r;
SV **holder;
int l_var = -1;
int l_text = -1;
int l_sub = -1;
int l_accel = -1;
int l_key = -1;
int l_data = -1;
if ( itemHolder == nil)
{
warn("RTC0035: menu build error: array panic");
my-> dispose_menu( self, m);
return nil;
}
if ( !SvROK( *itemHolder) || ( SvTYPE( SvRV( *itemHolder)) != SVt_PVAV)) {
warn("RTC0036: menu build error: submenu is not an array");
my-> dispose_menu( self, m);
return nil;
}
/* entering item description */
item = ( AV *) SvRV( *itemHolder);
count = av_len( item) + 1;
if ( count > 6) {
warn("RTC0032: menu build error: extra declaration");
count = 5;
}
if ( !( r = alloc1z( MenuItemReg))) {
warn( "Not enough memory");
my-> dispose_menu( self, m);
return nil;
}
r-> key = kbNoKey;
/* log_write("%sNo: %d, count: %d", buf, i, count); */
if ( count < 2) { /* empty or 1 means line divisor, no matter of text */
r-> flags. divider = true;
rightAdjust = (( level == 0) && ( var-> anchored));
if ( count == 1) l_var = 0;
} else if ( count == 2) {
l_text = 0;
l_sub = 1;
} else if ( count == 3) {
l_var = 0;
l_text = 1;
l_sub = 2;
} else if ( count == 4) {
l_text = 0;
l_accel = 1;
l_key = 2;
l_sub = 3;
} else if ( count == 5) {
l_var = 0;
l_text = 1;
l_accel = 2;
l_key = 3;
l_sub = 4;
} else {
l_var = 0;
l_text = 1;
l_accel = 2;
l_key = 3;
l_sub = 4;
l_data = 5;
}
if ( m) curr = curr-> next = r; else curr = m = r; /* adding to list */
r-> flags. rightAdjust = rightAdjust ? 1 : 0;
r-> id = ++(var-> autoEnum);
#define a_get( l_, fl_, num) if ( num >= 0 ) { \
holder = av_fetch( item, num, 0); \
if ( holder) { \
if ( SvTYPE(*holder) != SVt_NULL) { \
l_ = duplicate_string( SvPV_nolen( *holder)); \
fl_ = prima_is_utf8_sv(*holder); \
} \
} else { \
warn("RTC003A: menu build error: array panic"); \
my-> dispose_menu( self, m); \
return nil; \
} \
}
a_get( r-> accel , r-> flags. utf8_accel, l_accel);
a_get( r-> variable, r-> flags. utf8_variable, l_var);
if ( l_key >= 0) {
holder = av_fetch( item, l_key, 0);
if ( !holder) {
warn("RTC003B: menu build error: array panic");
my-> dispose_menu( self, m);
return nil;
}
r-> key = key_normalize( SvPV_nolen( *holder));
}
if ( r-> variable)
{
#define s r-> variable
int i, decr = 0;
for ( i = 0; i < 2; i++) {
switch ( s[i]) {
case '-':
r-> flags. disabled = 1;
decr++;
break;
case '*':
r-> flags. checked = 1;
decr++;
break;
default:
break;
}
}
if ( decr) memmove( s, s + decr, strlen( s) + 1 - decr);
if ( strlen( s) == 0 || is_var_id_name( s) != 0) {
free( r-> variable);
r-> variable = nil;
}
#undef s
}
/* parsing text */
if ( l_text >= 0)
{
holder = av_fetch( item, l_text, 0);
if ( !holder) {
warn("RTC003C: menu build error: array panic");
my-> dispose_menu( self, m);
return nil;
}
subItem = *holder;
if ( SvROK( subItem)) {
Handle c_object = gimme_the_mate( subItem);
if (( c_object == nilHandle) || !( kind_of( c_object, CImage)))
{
warn("RTC0033: menu build error: not an image passed");
goto TEXT;
}
/* log_write("%sbmp: %s %d", buf, ((PComponent)c_object)->name, kind_of( c_object, CImage)); */
if (((( PImage) c_object)-> w == 0)
|| ((( PImage) c_object)-> h == 0))
{
warn("RTC0037: menu build error: invalid image passed");
goto TEXT;
}
protect_object( r-> bitmap = c_object);
SvREFCNT_inc( SvRV(( PObject( r-> bitmap))-> mate));
} else {
TEXT:
r-> text = duplicate_string( SvPV_nolen( subItem));
r-> flags. utf8_text = prima_is_utf8_sv( subItem);
}
}
/* parsing sub */
if ( l_sub >= 0)
{
holder = av_fetch( item, l_sub, 0);
if ( !holder) {
warn("RTC003D: menu build error: array panic");
my-> dispose_menu( self, m);
return nil;
}
subItem = *holder;
if ( SvROK( subItem))
{
if ( SvTYPE( SvRV( subItem)) == SVt_PVCV)
{
r-> code = newSVsv( subItem);
} else {
r-> down = ( PMenuItemReg) my-> new_menu( self, subItem, level + 1);
if ( r-> down == nil)
{
/* seems error was occured inside this call */
my-> dispose_menu( self, m);
return nil;
}
}
} else {
if ( SvPOK( subItem)) {
r-> perlSub = duplicate_string( SvPV_nolen( subItem));
r-> flags. utf8_perlSub = prima_is_utf8_sv( subItem);
} else {
warn("RTC0038: menu build error: invalid sub name passed");
}
}
}
/* parsing data */
if ( l_data >= 0)
{
holder = av_fetch( item, l_data, 0);
if ( !holder) {
warn("RTC003D: menu build error: array panic");
my-> dispose_menu( self, m);
return nil;
}
r-> data = newSVsv( *holder);
}
}
/* log_write("%s}", buf); */
/* log_write("adda bunch:");
{
PMenuItemReg x = m;
while ( x)
{
log_write( x-> variable);
x = x-> next;
}
}
log_write("end."); */
return m;
}
void
AbstractMenu_init( Handle self, HV * profile)
{
dPROFILE;
inherited init( self, profile);
var-> anchored = kind_of( self, CMenu);
my-> update_sys_handle( self, profile);
my-> set_items( self, pget_sv( items));
if ( var-> system) apc_menu_update( self, nil, var-> tree);
if ( pget_B( selected)) my-> set_selected( self, true);
CORE_INIT_TRANSIENT(AbstractMenu);
}
void
AbstractMenu_done( Handle self)
{
if ( var-> system) apc_menu_destroy( self);
my-> dispose_menu( self, var-> tree);
var-> tree = nil;
inherited done( self);
}
Bool
AbstractMenu_validate_owner( Handle self, Handle * owner, HV * profile)
{
dPROFILE;
*owner = pget_H( owner);
if ( !kind_of( *owner, CWidget)) return false;
return inherited validate_owner( self, owner, profile);
}
void
AbstractMenu_cleanup( Handle self)
{
if ( my-> get_selected( self)) my-> set_selected( self, false);
inherited cleanup( self);
}
void
AbstractMenu_set( Handle self, HV * profile)
{
dPROFILE;
Bool select = false;
if ( pexist( owner)) {
select = pexist( selected) ? pget_B( selected) : my-> get_selected( self);
pdelete( selected);
}
inherited set( self, profile);
if ( select) my-> set_selected( self, true);
}
static SV *
new_av( PMenuItemReg m, int level)
{
AV * glo;
if ( m == nil) return nilSV;
glo = newAV();
while ( m)
{
AV * loc = newAV();
if ( !m-> flags. divider) {
if ( m-> variable) { /* has name */
SV * sv;
int shift = ( m-> flags. checked ? 1 : 0) + ( m-> flags. disabled ? 1 : 0);
if ( shift > 0) { /* has flags */
int len = (int) strlen( m-> variable);
char * name = allocs( len + shift);
if ( name) {
int slen = len + shift;
memcpy( name + shift, m-> variable, len);
if ( m-> flags. checked) name[ --shift] = '*';
if ( m-> flags. disabled) name[ --shift] = '-';
sv = newSVpv( name, slen);
} else
sv = newSVpv( m-> variable, len);
} else /* has name but no flags */
sv = newSVpv( m-> variable, 0);
if ( m-> flags. utf8_variable)
SvUTF8_on( sv);
av_push( loc, sv);
} else { /* has flags but no name - autogenerate */
int len;
char buffer[20];
len = sprintf( buffer, "%s%s#%d",
m-> flags. disabled ? "-" : "",
m-> flags. checked ? "*" : "",
m-> id);
av_push( loc, newSVpv( buffer, ( STRLEN) len));
}
if ( m-> bitmap) {
if ( PObject( m-> bitmap)-> stage < csDead)
av_push( loc, newRV( SvRV((( PObject)( m-> bitmap))-> mate)));
else
av_push( loc, newSVpv( "", 0));
} else {
SV * sv = newSVpv( m-> text, 0);
if ( m-> flags. utf8_text) SvUTF8_on( sv);
av_push( loc, sv);
}
if ( m-> accel) {
SV * sv = newSVpv( m-> accel, 0);
av_push( loc, sv);
if ( m-> flags. utf8_accel) SvUTF8_on( sv);
} else {
av_push( loc, newSVpv( "", 0));
}
av_push( loc, newSViv( m-> key));
if ( m-> down) {
av_push( loc, new_av( m-> down, level + 1));
} else if ( m-> code) {
av_push( loc, newSVsv( m-> code));
} else if ( m-> perlSub) {
SV * sv = newSVpv( m-> perlSub, 0);
if ( m-> flags. utf8_perlSub) SvUTF8_on( sv);
av_push( loc, sv);
} else {
av_push( loc, newSVpv( "", 0));
}
if ( m-> data)
av_push( loc, newSVsv( m-> data));
} else {
/* divider */
if ( m-> variable) {
SV * sv = newSVpv( m-> variable, 0);
if ( m-> flags. utf8_perlSub) SvUTF8_on( sv);
av_push( loc, sv);
} else {
int len;
char buffer[20];
len = sprintf( buffer, "#%d", m-> id);
av_push( loc, newSVpv( buffer, ( STRLEN) len));
}
}
av_push( glo, newRV_noinc(( SV *) loc));
m = m-> next;
}
return newRV_noinc(( SV *) glo);
}
static Bool
var_match( Handle self, PMenuItemReg m, void * params)
{
if ( m-> variable == nil) return false;
return ( strcmp( m-> variable, ( char *) params) == 0);
}
static Bool
id_match( Handle self, PMenuItemReg m, void * params)
{
return m-> id == *(( int*) params);
}
static Bool
key_match( Handle self, PMenuItemReg m, void * params)
{
return (( m-> key == *(( int*) params)) && ( m-> key != kbNoKey) && !( m-> flags. disabled));
}
static PMenuItemReg
find_menuitem( Handle self, char * var_name, Bool match_disabled)
{
int num;
if ( !var_name) return nil;
/* match special case /^#\d+$/ */
if (( num = is_var_id_name( var_name)) != 0)
return ( PMenuItemReg) my-> first_that( self, (void*)id_match, &num, match_disabled);
else
return ( PMenuItemReg) my-> first_that( self, (void*)var_match, var_name, match_disabled);
}
char *
AbstractMenu_make_var_context( Handle self, PMenuItemReg m, char * buffer)
{
if ( !m) return "";
if ( m-> variable)
return m-> variable;
sprintf( buffer, "#%d", m-> id);
return buffer;
}
char *
AbstractMenu_make_id_context( Handle self, int id, char * buffer)
{
return my-> make_var_context( self, my-> first_that( self, (void*)id_match, &id, true), buffer);
}
SV *
AbstractMenu_get_items( Handle self, char * varName)
{
if ( var-> stage > csFrozen) return nilSV;
if ( strlen( varName))
{
PMenuItemReg m = find_menuitem( self, varName, true);
if ( m && m-> down) {
return new_av( m-> down, 1);
} else if ( m) {
return newRV_noinc(( SV *) newAV());
} else {
return nilSV;
}
} else {
return var-> tree ?
new_av( var-> tree, 0) :
newRV_noinc(( SV *) newAV());
}
}
void
AbstractMenu_set_items( Handle self, SV * items)
{
PMenuItemReg oldBranch = var-> tree;
if ( var-> stage > csFrozen) return;
var-> tree = ( PMenuItemReg) my-> new_menu( self, items, 0);
if ( var-> stage <= csNormal && var-> system)
apc_menu_update( self, oldBranch, var-> tree);
my-> dispose_menu( self, oldBranch);
}
static PMenuItemReg
do_link( Handle self, PMenuItemReg m, PMenuProc p, void * params, Bool useDisabled)
{
while( m)
{
if ( !m-> flags. disabled || useDisabled)
{
if ( m-> down)
{
PMenuItemReg i = do_link( self, m-> down, p, params, useDisabled);
if ( i) return i;
}
if ( p( self, m, params)) return m;
}
m = m-> next;
}
return nil;
}
void *
AbstractMenu_first_that( Handle self, void * actionProc, void * params, Bool useDisabled)
{
return actionProc ? do_link( self, var-> tree, ( PMenuProc) actionProc, params, useDisabled) : nil;
}
Bool
AbstractMenu_has_item( Handle self, char * varName)
{
return find_menuitem( self, varName, true) != nil;
}
SV *
AbstractMenu_accel( Handle self, Bool set, char * varName, SV * accel)
{
PMenuItemReg m;
if ( var-> stage > csFrozen) return nilSV;
m = find_menuitem( self, varName, true);
if ( !m) return nilSV;
if ( !set) {
SV * sv = newSVpv( m-> accel ? m-> accel : "", 0);
if ( m-> flags. utf8_accel) SvUTF8_on( sv);
return sv;
}
if ( m-> text == nil) return nilSV;
free( m-> accel);
m-> accel = nil;
m-> accel = duplicate_string( SvPV_nolen( accel));
m-> flags. utf8_accel = prima_is_utf8_sv( accel);
if ( m-> id > 0)
if ( var-> stage <= csNormal && var-> system)
apc_menu_item_set_accel( self, m);
return nilSV;
}
SV *
AbstractMenu_action( Handle self, Bool set, char * varName, SV * action)
{
PMenuItemReg m;
if ( var-> stage > csFrozen) return nilSV;
m = find_menuitem( self, varName, true);
if ( !m) return nilSV;
if ( !set) {
if ( m-> code) return newSVsv( m-> code);
if ( m-> perlSub) {
SV * sv = newSVpv( m-> perlSub, 0);
if ( m-> flags. utf8_perlSub) SvUTF8_on( sv);
return sv;
}
return nilSV;
}
if ( m-> flags. divider || m-> down) return nilSV;
if ( SvROK( action))
{
if ( m-> code) sv_free( m-> code);
m-> code = nil;
if ( SvTYPE( SvRV( action)) == SVt_PVCV)
{
m-> code = newSVsv( action);
free( m-> perlSub);
m-> perlSub = nil;
}
m-> flags. utf8_perlSub = 0;
} else {
char * line = ( char *) SvPV_nolen( action);
free( m-> perlSub);
if ( m-> code) sv_free( m-> code);
m-> code = nil;
m-> perlSub = duplicate_string( line);
m-> flags. utf8_perlSub = prima_is_utf8_sv( action);
}
return nilSV;
}
Bool
AbstractMenu_checked( Handle self, Bool set, char * varName, Bool checked)
{
PMenuItemReg m;
if ( var-> stage > csFrozen) return false;
m = find_menuitem( self, varName, true);
if ( m == nil) return false;
if ( !set)
return m ? m-> flags. checked : false;
if ( m-> flags. divider || m-> down) return false;
m-> flags. checked = checked ? 1 : 0;
if ( m-> id > 0)
if ( var-> stage <= csNormal && var-> system)
apc_menu_item_set_check( self, m);
return checked;
}
SV *
AbstractMenu_data( Handle self, Bool set, char * varName, SV * data)
{
PMenuItemReg m;
if ( var-> stage > csFrozen) return nilSV;
m = find_menuitem( self, varName, true);
if ( m == nil) return nilSV;
if ( !set)
return m-> data ? newSVsv( m-> data) : nilSV;
sv_free( m-> data);
m-> data = newSVsv( data);
return nilSV;
}
Bool
AbstractMenu_enabled( Handle self, Bool set, char * varName, Bool enabled)
{
PMenuItemReg m;
if ( var-> stage > csFrozen) return false;
m = find_menuitem( self, varName, true);
if ( m == nil) return false;
if ( !set)
return m ? !m-> flags. disabled : false;
if (m-> flags. divider) return false;
m-> flags. disabled = ( enabled ? 0 : 1 ) ;
if ( m-> id > 0)
if ( var-> stage <= csNormal && var-> system)
apc_menu_item_set_enabled( self, m);
return enabled;
}
Handle
AbstractMenu_image( Handle self, Bool set, char * varName, Handle image)
{
PMenuItemReg m;
PImage i = ( PImage) image;
if ( var-> stage > csFrozen) return nilHandle;
m = find_menuitem( self, varName, true);
if ( m == nil) return nilHandle;
if ( !m-> bitmap) return nilHandle;
if ( !set) {
if ( PObject( m-> bitmap)-> stage == csDead) return nilHandle;
return m-> bitmap;
}
if (( image == nilHandle) || !( kind_of( image, CImage))) {
warn("RTC0039: invalid object passed to ::image");
return nilHandle;
}
if ( i-> w == 0 || i-> h == 0) {
warn("RTC0039: invalid object passed to ::image");
return nilHandle;
}
SvREFCNT_inc( SvRV(( PObject( image))-> mate));
protect_object( image);
if ( PObject( m-> bitmap)-> stage < csDead)
SvREFCNT_dec( SvRV(( PObject( m-> bitmap))-> mate));
unprotect_object( m-> bitmap);
m-> bitmap = image;
if ( m-> id > 0)
if ( var-> stage <= csNormal && var-> system)
apc_menu_item_set_image( self, m);
return nilHandle;
}
SV *
AbstractMenu_text( Handle self, Bool set, char * varName, SV * text)
{
PMenuItemReg m;
if ( var-> stage > csFrozen) return nilSV;
m = find_menuitem( self, varName, true);
if ( m == nil) return nilSV;
if ( m-> text == nil) return nilSV;
if ( !set) {
SV * sv = newSVpv( m-> text ? m-> text : "", 0);
if ( m-> flags. utf8_text) SvUTF8_on( sv);
return sv;
}
free( m-> text);
m-> text = nil;
m-> text = duplicate_string( SvPV_nolen( text));
m-> flags. utf8_accel = prima_is_utf8_sv( text);
if ( m-> id > 0)
if ( var-> stage <= csNormal && var-> system)
apc_menu_item_set_text( self, m);
return nilSV;
}
SV *
AbstractMenu_key( Handle self, Bool set, char * varName, SV * key)
{
PMenuItemReg m;
if ( var-> stage > csFrozen) return nilSV;
m = find_menuitem( self, varName, true);
if ( m == nil) return nilSV;
if ( m-> flags. divider || m-> down) return nilSV;
if ( !set)
return newSViv( m-> key);
m-> key = key_normalize( SvPV_nolen( key));
if ( m-> id > 0)
if ( var-> stage <= csNormal && var-> system)
apc_menu_item_set_key( self, m);
return nilSV;
}
void
AbstractMenu_set_variable( Handle self, char * varName, SV * newName)
{
PMenuItemReg m;
if ( var-> stage > csFrozen) return;
m = find_menuitem( self, varName, true);
if ( m == nil) return;
free( m-> variable);
if ( SvTYPE(newName) != SVt_NULL) {
STRLEN len;
char * v;
v = SvPV( newName, len);
if ( len > 0) {
m-> variable = duplicate_string( v);
m-> flags. utf8_variable = prima_is_utf8_sv( newName);
return;
}
}
m-> variable = nil;
m-> flags. utf8_variable = 0;
}
Bool
AbstractMenu_sub_call( Handle self, PMenuItemReg m)
{
char buffer[16], *context;
if ( m == nil) return false;
context = AbstractMenu_make_var_context( self, m, buffer);
if ( m-> code) {
if ( m-> flags. utf8_variable) {
SV * sv = newSVpv( context, 0);
SvUTF8_on( sv);
cv_call_perl((( PComponent) var-> owner)-> mate, SvRV( m-> code), "S", sv);
sv_free( sv);
} else
cv_call_perl((( PComponent) var-> owner)-> mate, SvRV( m-> code), "s", context);
} else if ( m-> perlSub) {
if ( m-> flags. utf8_variable) {
SV * sv = newSVpv( context, 0);
SvUTF8_on( sv);
call_perl( var-> owner, m-> perlSub, "S", sv);
sv_free( sv);
} else
call_perl( var-> owner, m-> perlSub, "s", context);
}
return true;
}
Bool
AbstractMenu_sub_call_id( Handle self, int sysId)
{
return my-> sub_call( self, ( PMenuItemReg) my-> first_that( self, (void*)id_match, &sysId, false));
}
#define keyRealize( key) if ((( key & 0xFF) >= 'A') && (( key & 0xFF) <= 'z')) \
key = tolower( key & 0xFF) | \
(( key & ( kmCtrl | kmAlt)) ? \
( key & ( kmCtrl | kmAlt | kmShift)) \
: 0)
Bool
AbstractMenu_sub_call_key ( Handle self, int key)
{
keyRealize( key);
return my-> sub_call( self, ( PMenuItemReg) my-> first_that( self, (void*)key_match, &key, false));
}
typedef struct _Kmcc
{
int key;
Bool enabled;
} Kmcc, *PKmcc;
static Bool
kmcc ( Handle self, PMenuItemReg m, void * params)
{
if ((( PKmcc) params)-> key == m-> key)
{
m-> flags. disabled = ((( PKmcc) params)-> enabled ? 0 : 1);
if ( m-> id > 0)
if ( var-> stage <= csNormal && var-> system)
apc_menu_item_set_enabled( self, m);
}
return false;
}
void
AbstractMenu_set_command( Handle self, char * key, Bool enabled)
{
Kmcc mcc;
mcc. key = key_normalize( key);
mcc. enabled = enabled;
if ( var-> stage > csFrozen) return;
my-> first_that( self, (void*)kmcc, &mcc, true);
}
Bool AbstractMenu_selected( Handle self, Bool set, Bool selected)
{
return false;
}
SV *
AbstractMenu_get_handle( Handle self)
{
char buf[ 256];
snprintf( buf, 256, "0x%08lx", var-> system ? apc_menu_get_handle( self) : self);
return newSVpv( buf, 0);
}
int
AbstractMenu_translate_accel( Handle self, char * accel)
{
if ( !accel) return 0;
while ( *accel) {
if ( *(accel++) == '~') {
switch ( *accel) {
case '~' :
accel++;
break;
case 0:
return 0;
default:
return isalnum( *accel) ? *accel : tolower( *accel);
}
}
}
return 0;
}
int
AbstractMenu_translate_key( Handle self, int code, int key, int mod)
{
mod &= kmAlt | kmShift | kmCtrl;
key = ( key != kbNoKey ? key : code) | mod;
keyRealize( key);
return key;
}
int
AbstractMenu_translate_shortcut( Handle self, char * key)
{
return key_normalize( key);
}
static Bool up_match ( Handle self, PMenuItemReg m, void * params) { return m-> down == params; }
static Bool prev_match ( Handle self, PMenuItemReg m, void * params) { return m-> next == params; }
void
AbstractMenu_remove( Handle self, char * varName)
{
PMenuItemReg up, prev, m;
if ( var-> stage > csFrozen) return;
m = find_menuitem( self, varName, true);
if ( m == nil) return;
if ( var-> stage <= csNormal && var-> system)
apc_menu_item_delete( self, m);
up = ( PMenuItemReg) my-> first_that( self, (void*)up_match, m, true);
prev = ( PMenuItemReg) my-> first_that( self, (void*)prev_match, m, true);
if ( up) up -> down = m-> next;
if ( prev) prev-> next = m-> next;
if ( m == var-> tree) var-> tree = m-> next;
m-> next = nil;
my-> dispose_menu( self, m);
}
void
AbstractMenu_insert( Handle self, SV * menuItems, char * rootName, int index)
{