[go: up one dir, main page]

File: menu.c

package info (click to toggle)
libforms 1.2.3-1.7
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,904 kB
  • sloc: ansic: 97,669; sh: 11,156; makefile: 951
file content (1016 lines) | stat: -rw-r--r-- 24,107 bytes parent folder | download | duplicates (4)
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
/*
 *  This file is part of the XForms library package.
 *
 *  XForms is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU Lesser General Public License as
 *  published by the Free Software Foundation; either version 2.1, or
 *  (at your option) any later version.
 *
 *  XForms is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with XForms.  If not, see <http://www.gnu.org/licenses/>.
 */


/**
 * \file menu.c
 *
 *  This file is part of the XForms library package.
 *  Copyright (c) 1996-2002  T.C. Zhao and Mark Overmars
 *  All rights reserved.
 *
 *  XForms Class FL_MENU.
 *    call PopUp to handle the actual random access
 *
 *  possible optimization:
 *   upon first creation of the popup, set extern_menu to the popup ID
 *   and let popup manage all the modes/values from then on.
 *
 *  OR use the following to simplify code for extern pup
 *    when extern_menu is used, gets all text and mode, then
 *    all the code would be the same for extern and native menu.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "include/forms.h"
#include "flinternal.h"
#include "private/pmenu.h"

#include <string.h>
#include <stdlib.h>
#include <ctype.h>


#define ISPUP( sp )   ( ( sp )->extern_menu >= 0 )


/***************************************
 * Returns the index into the array of items from the items menu ID
 ***************************************/

static int
val_to_index( FL_OBJECT * ob,
              int         val )
{
    int i;
    FLI_MENU_SPEC *sp = ob->spec;

    if ( ISPUP( sp ) )
        return val;

    for ( i = 1; i <= sp->numitems; i++ )
        if ( val == sp->mval[ i ] )
            return i;

    return -1;
}


/***************************************
 * Creates the menu and shows it. Returns the item selected.
 ***************************************/

static int
do_menu_low_level( FL_OBJECT * ob )
{
    int popup_id,
        i,
        val,
        k;
    FLI_MENU_SPEC *sp = ob->spec;

    /* The number of items can be 0 only if the menu is an external popup */

    if ( sp->numitems == 0 && ! ISPUP( sp ) )
        return 0;

    /* If it's an external popup let the xpopup code deal with everything */

    if ( ISPUP( sp ) )
    {
        if ( ob->label && *ob->label && ob->type != FL_PULLDOWN_MENU )
            fl_setpup_title( sp->extern_menu, ob->label );

        if ( ( val = fl_dopup( sp->extern_menu ) ) > 0 )
            sp->val = val;

        return val;
    }

    /* Create a new popup */

    popup_id = fl_newpup( FL_ObjWin( ob ) );

    if ( ob->type != FL_PULLDOWN_MENU && ! sp->no_title )
        fl_setpup_title( popup_id, ob->label );
    else
        fl_setpup_softedge( popup_id, 1 );

    for ( i = 1; i <= sp->numitems; i++ )
    {
        if ( sp->mval[ i ] == i && ! sp->cb[ i ] )
            fl_addtopup( popup_id, sp->items[ i ] );
        else
        {
            char *s = fl_malloc(   strlen( sp->items[ i ] )
                                 + 6 + log10( INT_MAX ) );

            sprintf( s, "%s%%x%d%s", sp->items[ i ], sp->mval[ i ],
                     sp->cb[ i ] ? "%f" : "" );

            if ( ! sp->cb[ i ] )
                fl_addtopup( popup_id, s );
            else
                fl_addtopup( popup_id, s, sp->cb[ i ] );

            fl_free( s );
        }

        if ( sp->modechange[ i ] || sp->mode[ i ] != FL_PUP_NONE )
        {
            fl_setpup_mode( popup_id, sp->mval[ i ], sp->mode[ i ] );
            sp->modechange[ i ] = 0;
        }

        fl_setpup_shortcut( popup_id, sp->mval[ i ], sp->shortcut[ i ] );
    }

    /* Pulldown menus and those without a title appear directly
       below the menu itself, the others more or less on top of
       the menu */

    if ( ob->type == FL_PULLDOWN_MENU || sp->no_title )
        fl_setpup_position( ob->form->x + ob->x + 1,
                            ob->form->y + ob->y + ob->h + 1 );
    else
        fl_setpup_position( ob->form->x + ob->x + 5,
                            ob->form->y + ob->y + 5 );

    /* Now do the user interaction */

    val = fl_dopup( popup_id );

    /* Deal with whatever is needed according to the return value */

    if ( val > 0 && ( k = val_to_index( ob, val ) ) > 0 )
    {
        /* If shown for the first time, need to get all mode right as the
           menu item string may have embedded mode setting strings in it */

        if ( sp->shown == 0 )
        {
            for ( i = 1; i <= sp->numitems; i++ )
            {
                int m = fl_getpup_mode( popup_id, sp->mval[ i ] );

                sp->modechange[ i ] = sp->mode[ i ] != m;
                sp->mode[ i ] = m;
                sp->shown = 1;
            }
        }
        else
        {
            sp->mode[ k ] = fl_getpup_mode( popup_id, val );
            sp->modechange[ k ] = 1;

            /* Old val also might change mode if binary */

            if ( sp->val > 0 )
            {
                int m = fl_getpup_mode( popup_id, sp->mval[ sp->val ] );

                sp->modechange[ sp->val ] = sp->mode[ sp->val ] != m;
                sp->mode[ sp->val ] = m;
            }
        }

        sp->val = k;
    }

    /* Get rid of the popup */

    fl_freepup( popup_id );

    return val;
}


/***************************************
 ***************************************/

static int
do_menu( FL_OBJECT *ob )
{
    int val;

    ob->pushed = 1;
    fl_redraw_object( ob );

    val = do_menu_low_level( ob ) > 0;

    ob->pushed = 0;
    fl_redraw_object( ob );

    return val > 0;
}


/***************************************
 * Handles an event, return non-zero if an item has been selected.
 ***************************************/

static int
handle_menu( FL_OBJECT * ob,
             int         event,
             FL_Coord    mx   FL_UNUSED_ARG,
             FL_Coord    my   FL_UNUSED_ARG,
             int         key  FL_UNUSED_ARG,
             void      * ev   FL_UNUSED_ARG )
{
    FLI_MENU_SPEC *sp = ob->spec;
    int val,
        boxtype = ob->boxtype;
    FL_COLOR col;
    int ret = FL_RETURN_NONE;

#if FL_DEBUG >= ML_DEBUG
    M_info2( "handle_menu", fli_event_name( event ) );
#endif

    switch ( event )
    {
        case FL_DRAW:
            /* Draw the object */

            if ( ob->pushed )
            {
                boxtype = FL_UP_BOX;
                col = ob->col2;
            }
            else
                col = ob->col1;

            fl_drw_box( boxtype, ob->x, ob->y, ob->w, ob->h, col, ob->bw );
            fl_drw_text( ob->align, ob->x, ob->y, ob->w, ob->h,
                         ob->lcol, ob->lstyle, ob->lsize, ob->label );

            if ( sp->showsymbol )
            {
                int dm = 0.85 * FL_min( ob->w, ob->h );

                fl_drw_text( 0, ob->x + ob->w - dm - 1, ob->y + 1,
                             dm, dm, col, 0, 0, "@menu" );
            }
            break;

        case FL_ENTER:
            if ( ob->type == FL_TOUCH_MENU && do_menu( ob ) > 0 )
                    ret |= FL_RETURN_CHANGED;
            break;

        case FL_PUSH:
            /* Touch menus and push menus without a title don't do anything
               on a button press */

            if (    key != FL_MBUTTON1
                 || ( ob->type == FL_PUSH_MENU && sp->no_title ) )
                break;

            if ( ob->type == FL_TOUCH_MENU )
            {
                ret |= FL_RETURN_END;
                break;
            }

            if ( do_menu( ob ) > 0 )
                ret |= FL_RETURN_END | FL_RETURN_CHANGED;

            break;

        case FL_RELEASE :
            /* Button release is only important for push menus without a
               title, all others get started by a button press or by just
               moving the mouse on top of it (and they also don't expect
               a release - that gets eaten by the popup handler) */

            if (    key != FL_MBUTTON1
                 || ! ( ob->type == FL_PUSH_MENU && sp->no_title )
                 || mx < ob->x
                 || mx > ob->x + ob->w
                 || my < ob->y
                 || my > ob->y + ob->h )
                break;

            if ( do_menu( ob ) > 0 )
                ret |= FL_RETURN_CHANGED | FL_RETURN_END;

            break;

        case FL_SHORTCUT:
            /* Show menu as highlighted */

            ob->pushed = 1;
            fl_redraw_object( ob );

            /* Do interaction and then redraw without highlighting */

            val = do_menu( ob );
            ob->pushed = 0;
            fl_redraw_object( ob );
            if ( val > 0 )
                ret |= FL_RETURN_CHANGED | FL_RETURN_END;
            break;

        case FL_FREEMEM:
            fl_clear_menu( ob );
            fl_free( ob->spec );
            return 0;
    }

    return ret;
}


/***************************************
 * Creates a menu object
 ***************************************/

FL_OBJECT *
fl_create_menu( int          type,
                FL_Coord     x,
                FL_Coord     y,
                FL_Coord     w,
                FL_Coord     h,
                const char * label )
{
    FL_OBJECT *obj;
    FLI_MENU_SPEC *sp;

    obj = fl_make_object( FL_MENU, type, x, y, w, h, label, handle_menu );

    obj->boxtype = FL_FLAT_BOX;
    obj->col1   = FL_MENU_COL1;
    obj->col2   = FL_MENU_COL2;
    obj->lcol   = FL_MENU_LCOL;
    obj->lstyle = FL_NORMAL_STYLE;
    obj->align  = FL_MENU_ALIGN;

    if ( type == FL_TOUCH_MENU )
        fl_set_object_return( obj, FL_RETURN_CHANGED );
    else
        fl_set_object_return( obj, FL_RETURN_END_CHANGED );

    sp = obj->spec = fl_calloc( 1, sizeof *sp );
    sp->extern_menu = -1;

    return obj;
}


/***************************************
 * Adds a menu object
 ***************************************/

FL_OBJECT *
fl_add_menu( int          type,
             FL_Coord     x,
             FL_Coord     y,
             FL_Coord     w,
             FL_Coord     h,
             const char * label )
{
    FL_OBJECT *ob;

    ob = fl_create_menu( type, x, y, w, h, label );
    fl_add_object( fl_current_form, ob );

    return ob;
}


/***************************************
 * Clears the menu object
 ***************************************/

void
fl_clear_menu( FL_OBJECT * ob )
{
    int i;
    FLI_MENU_SPEC *sp = ob->spec;

    if ( ISPUP( sp ) )
    {
        fl_freepup( sp->extern_menu );
        sp->extern_menu = -1;
        return;
    }

    sp->val = 0;
    sp->cur_val = 0;

    for ( i = 1; i <= sp->numitems; i++ )
    {
        fli_safe_free( sp->items[ i ] );
        fli_safe_free( sp->shortcut[ i ] );
        sp->mode[ i ] = FL_PUP_NONE;
        sp->cb[ i ] = NULL;
    }

    sp->numitems = 0;
}


/***************************************
 * Adds a line to the menu item.
 ***************************************/

static void
addto_menu( FL_OBJECT  * ob,
            const char * str,
            ... )
{
    FLI_MENU_SPEC *sp = ob->spec;
    int n;
    char *p;

    if (    ISPUP( sp )
         || sp->numitems >= FL_MENU_MAXITEMS
         || sp->cur_val == INT_MAX )
        return;

    n = ++sp->numitems;

    sp->items[ n ]    = fl_strdup( str );
    sp->shortcut[ n ] = fl_strdup( "" );
    sp->mode[ n ]     = FL_PUP_NONE;
    sp->cb[ n ]       = NULL;

    /* Check if a callback function needs to be set */

    if ( ( p = strstr( sp->items[ n ], "%f" ) ) )
    {
        va_list ap;

        va_start( ap, str );
        sp->cb[ n ] = va_arg( ap, FL_PUP_CB );
        va_end( ap );
        memmove( p, p + 2, strlen( p ) - 1 );
    }

    /* Set the value for the menu (either the index or extract it from "%xn" */

    if ( ! ( p = strstr( sp->items[ n ], "%x" ) ) )
        sp->mval[ n ] = ++sp->cur_val;
    else
    {
        if ( ! isdigit( ( unsigned char ) p[ 2 ] ) )
        {
            M_err( "addto_menu", "Missing number after %%x" );
            sp->mval[ n ] = ++sp->cur_val;
        }
        else
        {
            char *eptr;

            sp->mval[ n ] = strtol( p + 2, &eptr, 10 );
            while ( *eptr && isspace( ( unsigned char ) *eptr ) )
                eptr++;
            if ( *eptr )
                memmove( p, eptr, strlen( eptr ) + 1 );
            else
                *p = '\0';
        }
    }
}


/***************************************
 * Sets the menu to a particular menu string
 ***************************************/

void
fl_set_menu( FL_OBJECT  * ob,
             const char * menustr,
             ... )
{
    char *t,
         *c;
    va_list ap;
    FLI_MENU_SPEC *sp = ob->spec;

#if FL_DEBUG >= ML_ERR
    if ( ! IsValidClass( ob, FL_MENU ) )
    {
        M_err( "fl_set_menu", "%s is not Menu class", ob ? ob->label : "" );
        return;
    }
#endif

    fl_clear_menu( ob );

    /* Split up menu string at '|' chars and create an entry for each part */

    va_start( ap, menustr );

    t = fl_strdup( menustr );

    for ( c = strtok( t, "|" );
          c && sp->numitems < FL_CHOICE_MAXITEMS;
          c = strtok( NULL, "|" ) )
    {
        FL_PUP_CB cb;

        if ( strstr( c, "%f" ) )
        {
            cb = va_arg( ap, FL_PUP_CB );
            addto_menu( ob, c, cb );
        }
        else
            addto_menu( ob, c );
    }

    if ( t )
        fl_free( t );

    va_end( ap );
}


/***************************************
 * Adds a line to the menu item.
 ***************************************/

int
fl_addto_menu( FL_OBJECT  * ob,
               const char * menustr,
               ... )
{
    FLI_MENU_SPEC *sp= ob->spec;
    char *t,
         *c;
    va_list ap;

#if FL_DEBUG >= ML_ERR
    if ( ! IsValidClass( ob, FL_MENU ) )
    {
        M_err( "fl_addto_menu", "%s is not Menu class", ob ? ob->label : "" );
        return 0;
    }
#endif

    /* Split up menu string at '|' chars and create an entry for each part */

    va_start( ap, menustr );

    t = fl_strdup( menustr );

    for ( c = strtok( t, "|" );
          c && sp->numitems < FL_CHOICE_MAXITEMS;
          c = strtok( NULL, "|" ) )
    {
        FL_PUP_CB cb;

        if ( strstr( c, "%f" ) )
        {
            cb = va_arg( ap, FL_PUP_CB );
            addto_menu( ob, c, cb );
        }
        else
            addto_menu( ob, c );
    }

    if ( t )
        fl_free( t );

    va_end( ap );

    return sp->numitems;
}


/***************************************
 * Replaces a line in the menu item.
 ***************************************/

void
fl_replace_menu_item( FL_OBJECT  * ob,
                      int          numb,
                      const char * str,
                      ... )
{
    FLI_MENU_SPEC *sp = ob->spec;
    char *s,
         *p,
         *eptr;
         
    if ( ISPUP( sp ) )
    {
        fli_replacepup_text( sp->extern_menu, numb, str );
        return;
    }

    if ( ( numb = val_to_index( ob, numb ) ) <= 0 )
        return;

    if ( sp->items[ numb ] )
        fl_free( sp->items[ numb ] );
    sp->cb[ numb ] = NULL;

    s = strdup( str );

    if ( ( p = strstr( s, "%f" ) ) )
    {
        va_list ap;

        va_start( ap, str );
        sp->cb[ numb ] = va_arg( ap, FL_PUP_CB );
        va_end( ap );
        memmove( p, p + 2, strlen( p ) - 1 );
    }

    if ( ( p = strstr( s, "%x" ) ) )
    {
        if ( isdigit( ( unsigned char ) p[ 2 ] ) )
        {
            sp->mval[ numb ] = strtol( p + 2, &eptr, 10 );
            while ( *eptr && isspace( ( unsigned char ) *eptr ) )
                eptr++;
            if ( *eptr )
                memmove( p, eptr, strlen( eptr ) + 1 );
            else
                *p = '\0';
        }
        else
        {
            M_err( "fl_replace_menu_item", "Missing number after %%x" );
            memmove( p, p + 2, strlen( p ) - 1 );
        }
    }

    sp->items[ numb ] = s;
}


/***************************************
 * Removes a line from the menu item.
 ***************************************/

void
fl_delete_menu_item( FL_OBJECT * ob,
                     int         numb )
{
    int i;
    FLI_MENU_SPEC *sp = ob->spec;

    if ( ISPUP( sp ) || ( numb = val_to_index( ob, numb ) ) <= 0 )
        return;

    fli_safe_free( sp->items[ numb ] );
    fli_safe_free( sp->shortcut[ numb ] );

    for ( i = numb; i < sp->numitems; i++ )
    {
        sp->items[ i ]      = sp->items[ i + 1 ];
        sp->mode[ i ]       = sp->mode[ i + 1 ];
        sp->modechange[ i ] = sp->modechange[ i + 1 ];
        sp->mval[ i ]       = sp->mval[ i + 1 ];
        sp->shortcut[ i ]   = sp->shortcut[ i + 1 ];
        sp->cb[ i ]         = sp->cb[ i + 1 ];
    }

    if ( sp->val == numb )
        sp->val = -1;

    sp->items[ sp->numitems ]      = NULL;
    sp->shortcut[ sp->numitems ]   = NULL;
    sp->mode[ sp->numitems ]       = FL_PUP_NONE;
    sp->modechange[ sp->numitems ] = 0;
    sp->mval[ sp->numitems ]       = 0;
    sp->cb[ sp->numitems ]         = NULL;

    sp->numitems--;
}


/***************************************
 * Sets a callback function for a menu item
 ***************************************/

FL_PUP_CB
fl_set_menu_item_callback( FL_OBJECT * ob,
                           int         numb,
                           FL_PUP_CB   cb )
{
    FLI_MENU_SPEC *sp = ob->spec;
    FL_PUP_CB old_cb;

    if ( ISPUP( sp ) || ( numb = val_to_index( ob, numb ) ) <= 0 )
        return NULL;

    old_cb = sp->cb[ numb ];
    sp->cb[ numb ] = cb;
    return old_cb;
}


/***************************************
 * Sets a shortcut for a menu item
 ***************************************/

void
fl_set_menu_item_shortcut( FL_OBJECT  * ob,
                           int          numb,
                           const char * str )
{
    FLI_MENU_SPEC *sp = ob->spec;

    if ( ISPUP( sp ) || ( numb = val_to_index( ob, numb ) ) <= 0 )
        return;

    fli_safe_free( sp->shortcut[ numb ] );
    sp->shortcut[ numb ] = fl_strdup( str ? str : "" );
}


/***************************************
 * Sets the display mode for the menu item
 ***************************************/

void
fl_set_menu_item_mode( FL_OBJECT    * ob,
                       int            numb,
                       unsigned int   mode )
{
    FLI_MENU_SPEC *sp = ob->spec;

    if ( ISPUP( sp ) )
        fl_setpup_mode( sp->extern_menu, numb, mode );
    else
    {
        if ( ( numb = val_to_index( ob, numb ) ) <= 0 )
            return;

        sp->mode[ numb ] = mode;
        sp->modechange[ numb ] = 1;

        if ( mode & FL_PUP_CHECK )
            sp->val = numb;
    }
}


/***************************************
 * Sets the display mode for the menu item
 ***************************************/

int
fl_set_menu_item_id( FL_OBJECT * ob,
                     int         index,
                     int         id )
{
    FLI_MENU_SPEC *sp = ob->spec;
    int old_id;

    if ( ISPUP( sp ) || id < 1 || index < 1 || index > sp->numitems )
        return -1;

    old_id = sp->mval[ index ];
    sp->mval[ index ] = id;
    return old_id;
}


/***************************************
 * Makes the menu symbol visible or not
 ***************************************/

void
fl_show_menu_symbol( FL_OBJECT * ob,
                     int         show )
{
    FLI_MENU_SPEC *sp = ob->spec;

    if ( ISPUP( sp ) )
         return;

    sp->showsymbol = show;
    fl_redraw_object( ob );
}


/***************************************
 * Returns the number of the menu item selected.
 ***************************************/

int
fl_get_menu( FL_OBJECT * ob )
{
    FLI_MENU_SPEC *sp = ob->spec;

#if FL_DEBUG >= ML_ERR
    if ( ! IsValidClass( ob, FL_MENU ) )
    {
        M_err( "fl_get_menu", "%s is not Menu class", ob ? ob->label : "" );
        return 0;
    }
#endif

    return val_to_index( ob, sp->val );
}


/***************************************
 * Returns rhe number of items in a menu
 ***************************************/

int
fl_get_menu_maxitems( FL_OBJECT * ob )
{
    FLI_MENU_SPEC *sp = ob->spec;

#if FL_DEBUG >= ML_ERR
    if ( ! IsValidClass( ob, FL_MENU ) )
    {
        M_err( "fl_get_menu_maxitems", "%s is not Menu class",
               ob ? ob->label : "" );
        return 0;
    }
#endif

    return ISPUP( sp ) ? fl_getpup_items( sp->extern_menu ) : sp->numitems;
}


/***************************************
 * Returns the text of the menu item selected.
 ***************************************/

const char *
fl_get_menu_text( FL_OBJECT * ob )
{
    FLI_MENU_SPEC *sp = ob->spec;

#if FL_DEBUG >= ML_ERR
    if ( ! IsValidClass( ob, FL_MENU ) )
    {
        M_err( "fl_get_menu_text", "%s is not Menu class",
               ob ? ob->label : "" );
        return NULL;
    }
#endif

    if ( ISPUP( sp ) )
        return fl_getpup_text( sp->extern_menu, sp->val );

    return ( sp->val < 1 || sp->val > sp->numitems ) ?
            NULL : sp->items[ sp->val ];
}


/***************************************
 * Returns a string with the menu items text
 ***************************************/

const char *
fl_get_menu_item_text( FL_OBJECT * ob,
                       int         numb )
{
    FLI_MENU_SPEC *sp = ob->spec;

#if FL_DEBUG >= ML_ERR
    if ( ! IsValidClass( ob, FL_MENU ) )
    {
        M_err( "fl_get_menu_item_text", "%s is not Menu class",
               ob ? ob->label : "" );
        return NULL;
    }
#endif

    if ( ISPUP(sp ) )
        return fl_getpup_text( sp->extern_menu, numb );

    numb = val_to_index( ob, numb );

    return numb <= 0 ? NULL : sp->items[ numb ];
}


/***************************************
 * Returns the mode of a menu item
 ***************************************/

unsigned int
fl_get_menu_item_mode( FL_OBJECT * ob,
                       int         numb )
{
    FLI_MENU_SPEC *sp = ob->spec;

#if FL_DEBUG >= ML_ERR
    if ( ! IsValidClass( ob, FL_MENU ) )
    {
        M_err( "fl_get_menu_item_mode", "%s is not Menu class",
               ob ? ob->label : "" );
        return 0;
    }
#endif

    if ( ISPUP( sp ) )
        return fl_getpup_mode( sp->extern_menu, numb );

    numb = val_to_index( ob, numb );

    return numb <= 0 ? 0 : sp->mode[ numb ];
}


/***************************************
 * Makes an already existing popup a menu
 ***************************************/

void
fl_set_menu_popup( FL_OBJECT * ob,
                   int         pup )
{
#if FL_DEBUG >= ML_ERR
    if ( ! IsValidClass( ob, FL_MENU ) )
    {
        M_err( "fl_set_menu_popup", "%s is not Menu class",
               ob ? ob->label : "" );
        return;
    }
#endif

    ( ( FLI_MENU_SPEC * ) ob->spec )->extern_menu = pup;

    if ( ob->type == FL_PULLDOWN_MENU )
        fl_setpup_shadow( pup, 0 );
}


/***************************************
 * Creates a popup and makes that a menu
 ***************************************/

int
fl_set_menu_entries( FL_OBJECT    * ob,
                     FL_PUP_ENTRY * ent )
{
    int n;

    fl_clear_menu( ob );

    n = fl_newpup( FL_ObjWin( ob ) );
    fl_set_menu_popup( ob, fl_setpup_entries( n, ent ) );

    if ( ob->type == FL_PULLDOWN_MENU )
    {
        fl_setpup_bw( n, ob->bw );
        fl_setpup_shadow( n, 0 );
    }

    return n;
}


/***************************************
 * If the menu is really a popup in disguise returns the popups number
 ***************************************/

int
fl_get_menu_popup( FL_OBJECT * ob )
{
    FLI_MENU_SPEC *sp = ob->spec;

    return ISPUP( sp ) ? sp->extern_menu : -1;
}


/***************************************
 * Allows to change the no-title attribute of a menu
 ***************************************/

int
fl_set_menu_notitle( FL_OBJECT * ob,
                     int         off )
{
    FLI_MENU_SPEC *sp = ob->spec;
    int old = sp->no_title;

    sp->no_title = off;
    return old;
}


/*
 * Local variables:
 * tab-width: 4
 * indent-tabs-mode: nil
 * End:
 */