summaryrefslogtreecommitdiff
path: root/src/fltk-widgets-menus.adb
blob: 03333d255a3347e4a05bbbdb8f80c93e98fa2f1f (plain)
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


with

    Interfaces.C.Strings,
    Ada.Unchecked_Deallocation,
    System;

use type

    System.Address,
    Interfaces.C.int,
    Interfaces.C.unsigned_long,
    Interfaces.C.Strings.chars_ptr;


package body FLTK.Widgets.Menus is


    procedure menu_set_draw_hook
           (W, D : in System.Address);
    pragma Import (C, menu_set_draw_hook, "menu_set_draw_hook");
    pragma Inline (menu_set_draw_hook);

    procedure menu_set_handle_hook
           (W, H : in System.Address);
    pragma Import (C, menu_set_handle_hook, "menu_set_handle_hook");
    pragma Inline (menu_set_handle_hook);




    function new_fl_menu
           (X, Y, W, H : in Interfaces.C.int;
            Text       : in Interfaces.C.char_array)
        return System.Address;
    pragma Import (C, new_fl_menu, "new_fl_menu");
    pragma Inline (new_fl_menu);

    procedure free_fl_menu
           (F : in System.Address);
    pragma Import (C, free_fl_menu, "free_fl_menu");
    pragma Inline (free_fl_menu);




    function fl_menu_add
           (M    : in System.Address;
            T    : in Interfaces.C.char_array;
            S    : in Interfaces.C.unsigned_long;
            C, U : in System.Address;
            F    : in Interfaces.C.unsigned_long)
        return Interfaces.C.int;
    pragma Import (C, fl_menu_add, "fl_menu_add");
    pragma Inline (fl_menu_add);

    function fl_menu_insert
           (M    : in System.Address;
            P    : in Interfaces.C.int;
            T    : in Interfaces.C.char_array;
            S    : in Interfaces.C.unsigned_long;
            C, U : in System.Address;
            F    : in Interfaces.C.unsigned_long)
        return Interfaces.C.int;
    pragma Import (C, fl_menu_insert, "fl_menu_insert");
    pragma Inline (fl_menu_insert);

    procedure fl_menu_remove
           (M : in System.Address;
            P : in Interfaces.C.int);
    pragma Import (C, fl_menu_remove, "fl_menu_remove");
    pragma Inline (fl_menu_remove);

    procedure fl_menu_clear
           (M : in System.Address);
    pragma Import (C, fl_menu_clear, "fl_menu_clear");
    pragma Inline (fl_menu_clear);




    function fl_menu_get_item
           (M : in System.Address;
            I : in Interfaces.C.int)
        return System.Address;
    pragma Import (C, fl_menu_get_item, "fl_menu_get_item");
    pragma Inline (fl_menu_get_item);

    function fl_menu_find_item
           (M : in System.Address;
            T : in Interfaces.C.char_array)
        return System.Address;
    pragma Import (C, fl_menu_find_item, "fl_menu_find_item");
    pragma Inline (fl_menu_find_item);

    function fl_menu_find_item2
           (M, C : in System.Address)
        return System.Address;
    pragma Import (C, fl_menu_find_item2, "fl_menu_find_item2");
    pragma Inline (fl_menu_find_item2);

    function fl_menu_find_index
           (M : in System.Address;
            T : in Interfaces.C.char_array)
        return Interfaces.C.int;
    pragma Import (C, fl_menu_find_index, "fl_menu_find_index");
    pragma Inline (fl_menu_find_index);

    function fl_menu_find_index2
           (M, I : in System.Address)
        return Interfaces.C.int;
    pragma Import (C, fl_menu_find_index2, "fl_menu_find_index2");
    pragma Inline (fl_menu_find_index2);

    function fl_menu_find_index3
           (M, C : in System.Address)
        return Interfaces.C.int;
    pragma Import (C, fl_menu_find_index3, "fl_menu_find_index3");
    pragma Inline (fl_menu_find_index3);

    function fl_menu_size
           (M : in System.Address)
        return Interfaces.C.int;
    pragma Import (C, fl_menu_size, "fl_menu_size");
    pragma Inline (fl_menu_size);




    function fl_menu_mvalue
           (M : in System.Address)
        return System.Address;
    pragma Import (C, fl_menu_mvalue, "fl_menu_mvalue");
    pragma Inline (fl_menu_mvalue);

    function fl_menu_text
           (M : in System.Address)
        return Interfaces.C.Strings.chars_ptr;
    pragma Import (C, fl_menu_text, "fl_menu_text");
    pragma Inline (fl_menu_text);

    function fl_menu_value
           (M : in System.Address)
        return Interfaces.C.int;
    pragma Import (C, fl_menu_value, "fl_menu_value");
    pragma Inline (fl_menu_value);




    function fl_menu_get_textcolor
           (M : in System.Address)
        return Interfaces.C.unsigned;
    pragma Import (C, fl_menu_get_textcolor, "fl_menu_get_textcolor");
    pragma Inline (fl_menu_get_textcolor);

    procedure fl_menu_set_textcolor
           (M : in System.Address;
            C : in Interfaces.C.unsigned);
    pragma Import (C, fl_menu_set_textcolor, "fl_menu_set_textcolor");
    pragma Inline (fl_menu_set_textcolor);

    function fl_menu_get_textfont
           (M : in System.Address)
        return Interfaces.C.int;
    pragma Import (C, fl_menu_get_textfont, "fl_menu_get_textfont");
    pragma Inline (fl_menu_get_textfont);

    procedure fl_menu_set_textfont
           (M : in System.Address;
            F : in Interfaces.C.int);
    pragma Import (C, fl_menu_set_textfont, "fl_menu_set_textfont");
    pragma Inline (fl_menu_set_textfont);

    function fl_menu_get_textsize
           (M : in System.Address)
        return Interfaces.C.int;
    pragma Import (C, fl_menu_get_textsize, "fl_menu_get_textsize");
    pragma Inline (fl_menu_get_textsize);

    procedure fl_menu_set_textsize
           (M : in System.Address;
            S : in Interfaces.C.int);
    pragma Import (C, fl_menu_set_textsize, "fl_menu_set_textsize");
    pragma Inline (fl_menu_set_textsize);




    function fl_menu_get_down_box
           (M : in System.Address)
        return Interfaces.C.int;
    pragma Import (C, fl_menu_get_down_box, "fl_menu_get_down_box");
    pragma Inline (fl_menu_get_down_box);

    procedure fl_menu_set_down_box
           (M : in System.Address;
            T : in Interfaces.C.int);
    pragma Import (C, fl_menu_set_down_box, "fl_menu_set_down_box");
    pragma Inline (fl_menu_set_down_box);

    procedure fl_menu_global
           (M : in System.Address);
    pragma Import (C, fl_menu_global, "fl_menu_global");
    pragma Inline (fl_menu_global);

    function fl_menu_measure
           (M : in     System.Address;
            I : in     Interfaces.C.int;
            H :    out Interfaces.C.int)
        return Interfaces.C.int;
    pragma Import (C, fl_menu_measure, "fl_menu_measure");
    pragma Inline (fl_menu_measure);




    function fl_menu_popup
           (M    : in System.Address;
            X, Y : in Interfaces.C.int;
            T    : in Interfaces.C.char_array;
            N    : in Interfaces.C.int)
        return System.Address;
    pragma Import (C, fl_menu_popup, "fl_menu_popup");
    pragma Inline (fl_menu_popup);

    function fl_menu_pulldown
           (M          : in System.Address;
            X, Y, W, H : in Interfaces.C.int;
            N          : in Interfaces.C.int)
        return System.Address;
    pragma Import (C, fl_menu_pulldown, "fl_menu_pulldown");
    pragma Inline (fl_menu_pulldown);




    procedure fl_menu_draw_item
           (M          : in System.Address;
            I          : in Interfaces.C.int;
            X, Y, W, H : in Interfaces.C.int;
            S          : in Interfaces.C.int);
    pragma Import (C, fl_menu_draw_item, "fl_menu_draw_item");
    pragma Inline (fl_menu_draw_item);




    procedure Item_Hook
           (M, U : in System.Address)
    is
        Ada_Widget : access Widget'Class :=
            Widget_Convert.To_Pointer (fl_widget_get_user_data (M));
        Action : Widget_Callback := Callback_Convert.To_Pointer (U);
    begin
        Action.all (Ada_Widget.all);
    end Item_Hook;




    procedure Free_Item is new Ada.Unchecked_Deallocation
        (Object => FLTK.Menu_Items.Menu_Item, Name => Item_Access);




    procedure Finalize
           (This : in out Menu) is
    begin
        if  This.Void_Ptr /= System.Null_Address and then
            This in Menu'Class
        then
            for Item of This.My_Items loop
                Free_Item (Item);
            end loop;
            free_fl_menu (This.Void_Ptr);
            This.Void_Ptr := System.Null_Address;
        end if;
        Finalize (Widget (This));
    end Finalize;




    package body Forge is

        function Create
               (X, Y, W, H : in Integer;
                Text       : in String)
            return Menu is
        begin
            return This : Menu do
                This.Void_Ptr := new_fl_menu
                       (Interfaces.C.int (X),
                        Interfaces.C.int (Y),
                        Interfaces.C.int (W),
                        Interfaces.C.int (H),
                        Interfaces.C.To_C (Text));
                fl_widget_set_user_data
                       (This.Void_Ptr,
                        Widget_Convert.To_Address (This'Unchecked_Access));
                menu_set_draw_hook (This.Void_Ptr, Draw_Hook'Address);
                menu_set_handle_hook (This.Void_Ptr, Handle_Hook'Address);
                This.My_Items := Item_Vectors.Empty_Vector;
            end return;
        end Create;

    end Forge;




    procedure Add
           (This     : in out Menu;
            Text     : in     String;
            Action   : in     Widget_Callback := null;
            Shortcut : in     Key_Combo := No_Key;
            Flags    : in     Menu_Flag := Flag_Normal)
    is
        Place : Interfaces.C.int;
        Callback, User_Data : System.Address := System.Null_Address;
        New_Item : Item_Access;
    begin
        if Action /= null then
            Callback := Item_Hook'Address;
            User_Data := Callback_Convert.To_Address (Action);
        end if;

        Place := fl_menu_add
           (This.Void_Ptr,
            Interfaces.C.To_C (Text),
            To_C (Shortcut),
            Callback,
            User_Data,
            Interfaces.C.unsigned_long (Flags));

        New_Item := new FLTK.Menu_Items.Menu_Item;
        Wrapper (New_Item.all).Void_Ptr := fl_menu_get_item (This.Void_Ptr, Place);
        Wrapper (New_Item.all).Needs_Dealloc := False;
        This.My_Items.Append (New_Item);
    end Add;


    procedure Insert
           (This     : in out Menu;
            Place    : in     Index;
            Text     : in     String;
            Action   : in     Widget_Callback := null;
            Shortcut : in     Key_Combo := No_Key;
            Flags    : in     Menu_Flag := Flag_Normal)
    is
        Ret_Place : Interfaces.C.int;
        Callback, User_Data : System.Address := System.Null_Address;
        New_Item : Item_Access;
    begin
        if Action /= null then
            Callback := Item_Hook'Address;
            User_Data := Callback_Convert.To_Address (Action);
        end if;

        Ret_Place := fl_menu_insert
           (This.Void_Ptr,
            Interfaces.C.int (Place) - 1,
            Interfaces.C.To_C (Text),
            To_C (Shortcut),
            Callback,
            User_Data,
            Interfaces.C.unsigned_long (Flags));

        New_Item := new FLTK.Menu_Items.Menu_Item;
        Wrapper (New_Item.all).Void_Ptr := fl_menu_get_item (This.Void_Ptr, Ret_Place);
        Wrapper (New_Item.all).Needs_Dealloc := False;
        This.My_Items.Insert (Positive (Ret_Place + 1), New_Item);
    end Insert;


    procedure Remove
           (This  : in out Menu;
            Place : in     Index) is
    begin
        Free_Item (This.My_Items.Reference (Place));
        This.My_Items.Delete (Place);
        fl_menu_remove (This.Void_Ptr, Interfaces.C.int (Place) - 1);
    end Remove;


    procedure Clear
           (This : in out Menu) is
    begin
        for Item of This.My_Items loop
            Free_Item (Item);
        end loop;
        This.My_Items := Item_Vectors.Empty_Vector;
        fl_menu_clear (This.Void_Ptr);
    end Clear;




    function Has_Item
           (This  : in Menu;
            Place : in Index)
        return Boolean is
    begin
        return Place in 1 .. This.Number_Of_Items;
    end Has_Item;


    function Has_Item
           (Place : in Cursor)
        return Boolean is
    begin
        return Place.My_Container.Has_Item (Place.My_Index);
    end Has_Item;


    function Item
           (This  : in Menu;
            Place : in Index)
        return FLTK.Menu_Items.Menu_Item_Reference is
    begin
        return (Data => This.My_Items.Element (Place));
    end Item;


    function Item
           (This  : in Menu;
            Place : in Cursor)
        return FLTK.Menu_Items.Menu_Item_Reference is
    begin
        return This.Item (Place.My_Index);
    end Item;


    function Find_Item
           (This : in Menu;
            Name : in String)
        return FLTK.Menu_Items.Menu_Item_Reference is
    begin
        return (Data => This.My_Items.Element (This.Find_Index (Name)));
    end Find_Item;


    function Find_Item
           (This   : in Menu;
            Action : in Widget_Callback)
        return FLTK.Menu_Items.Menu_Item_Reference is
    begin
        return (Data => This.My_Items.Element (This.Find_Index (Action)));
    end Find_Item;


    function Find_Index
           (This : in Menu;
            Name : in String)
        return Extended_Index
    is
        Ret : Interfaces.C.int;
    begin
        Ret := fl_menu_find_index (This.Void_Ptr, Interfaces.C.To_C (Name));
        return Extended_Index (Ret + 1);
    end Find_Index;


    function Find_Index
           (This : in Menu;
            Item : in FLTK.Menu_Items.Menu_Item)
        return Extended_Index
    is
        Ret : Interfaces.C.int;
    begin
        Ret := fl_menu_find_index2 (This.Void_Ptr, Wrapper (Item).Void_Ptr);
        return Extended_Index (Ret + 1);
    end Find_Index;


    function Find_Index
           (This   : in Menu;
            Action : in Widget_Callback)
        return Extended_Index
    is
        Ret : Interfaces.C.int;
    begin
        Ret := fl_menu_find_index3
           (This.Void_Ptr,
            Callback_Convert.To_Address (Action));
        return Extended_Index (Ret + 1);
    end Find_Index;


    function Number_Of_Items
           (This : in Menu)
        return Natural is
    begin
        return Natural (fl_menu_size (This.Void_Ptr));
    end Number_Of_Items;




    function Iterate
           (This : in Menu)
        return Menu_Iterators.Reversible_Iterator'Class is
    begin
        return It : Iterator := (My_Container => This'Unrestricted_Access);
    end Iterate;


    function First
           (Object : in Iterator)
        return Cursor is
    begin
        return Cu : Cursor :=
           (My_Container => Object.My_Container,
            My_Index     => 1);
    end First;


    function Next
           (Object : in Iterator;
            Place  : in Cursor)
        return Cursor is
    begin
        return Cu : Cursor :=
           (My_Container => Place.My_Container,
            My_Index     => Place.My_Index + 1);
    end Next;


    function Last
           (Object : in Iterator)
        return Cursor is
    begin
        return Cu : Cursor :=
           (My_Container => Object.My_Container,
            My_Index     => Object.My_Container.Number_Of_Items);
    end Last;


    function Previous
           (Object : in Iterator;
            Place  : in Cursor)
        return Cursor is
    begin
        return Cu : Cursor :=
           (My_Container => Place.My_Container,
            My_Index     => Place.My_Index - 1);
    end Previous;




    function Chosen
           (This : in Menu)
        return FLTK.Menu_Items.Menu_Item_Reference is
    begin
        return (Data => This.My_Items.Element (This.Chosen_Index));
    end Chosen;


    function Chosen_Label
           (This : in Menu)
        return String
    is
        Ptr : Interfaces.C.Strings.chars_ptr := fl_menu_text (This.Void_Ptr);
    begin
        if Ptr = Interfaces.C.Strings.Null_Ptr then
            return "";
        else
            --  no dealloc required?
            return Interfaces.C.Strings.Value (Ptr);
        end if;
    end Chosen_Label;


    function Chosen_Index
           (This : in Menu)
        return Extended_Index is
    begin
        return Extended_Index (fl_menu_value (This.Void_Ptr) + 1);
    end Chosen_Index;




    function Get_Text_Color
           (This : in Menu)
        return Color is
    begin
        return Color (fl_menu_get_textcolor (This.Void_Ptr));
    end Get_Text_Color;


    procedure Set_Text_Color
           (This : in out Menu;
            To   : in     Color) is
    begin
        fl_menu_set_textcolor (This.Void_Ptr, Interfaces.C.unsigned (To));
    end Set_Text_Color;


    function Get_Text_Font
           (This : in Menu)
        return Font_Kind is
    begin
        return Font_Kind'Val (fl_menu_get_textfont (This.Void_Ptr));
    end Get_Text_Font;


    procedure Set_Text_Font
           (This : in out Menu;
            To   : in     Font_Kind) is
    begin
        fl_menu_set_textfont (This.Void_Ptr, Font_Kind'Pos (To));
    end Set_Text_Font;


    function Get_Text_Size
           (This : in Menu)
        return Font_Size is
    begin
        return Font_Size (fl_menu_get_textsize (This.Void_Ptr));
    end Get_Text_Size;


    procedure Set_Text_Size
           (This : in out Menu;
            To   : in     Font_Size) is
    begin
        fl_menu_set_textsize (This.Void_Ptr, Interfaces.C.int (To));
    end Set_Text_Size;




    function Get_Down_Box
           (This : in Menu)
        return Box_Kind is
    begin
        return Box_Kind'Val (fl_menu_get_down_box (This.Void_Ptr));
    end Get_Down_Box;


    procedure Set_Down_Box
           (This : in out Menu;
            To   : in     Box_Kind) is
    begin
        fl_menu_set_down_box (This.Void_Ptr, Box_Kind'Pos (To));
    end Set_Down_Box;


    procedure Make_Global
           (This : in out Menu) is
    begin
        fl_menu_global (This.Void_Ptr);
    end Make_Global;


    procedure Measure_Item
           (This : in     Menu;
            Item : in     Index;
            W, H :    out Integer) is
    begin
        W := Integer (fl_menu_measure
           (This.Void_Ptr,
            Interfaces.C.int (Item) - 1,
            Interfaces.C.int (H)));
    end Measure_Item;




    function Popup
           (This    : in Menu;
            X, Y    : in Integer;
            Title   : in String := "";
            Initial : in Extended_Index := No_Index)
        return FLTK.Menu_Items.Menu_Item_Reference
    is
        Ptr : System.Address := fl_menu_popup
           (This.Void_Ptr,
            Interfaces.C.int (X),
            Interfaces.C.int (Y),
            Interfaces.C.To_C (Title),
            Interfaces.C.int (Initial) - 1);
        Place : Index := Index (fl_menu_find_index2 (This.Void_Ptr, Ptr) + 1);
    begin
        return (Data => This.My_Items.Element (Place));
    end Popup;


    function Pulldown
           (This       : in Menu;
            X, Y, W, H : in Integer;
            Initial    : in Extended_Index := No_Index)
        return FLTK.Menu_Items.Menu_Item_Reference
    is
        Ptr : System.Address := fl_menu_pulldown
           (This.Void_Ptr,
            Interfaces.C.int (X),
            Interfaces.C.int (Y),
            Interfaces.C.int (W),
            Interfaces.C.int (H),
            Interfaces.C.int (Initial) - 1);
        Place : Index := Index (fl_menu_find_index2 (This.Void_Ptr, Ptr) + 1);
    begin
        return (Data => This.My_Items.Element (Place));
    end Pulldown;




    procedure Draw_Item
           (This       : in out Menu;
            Item       : in     Index;
            X, Y, W, H : in     Integer;
            Selected   : in     Boolean := False) is
    begin
        fl_menu_draw_item
           (This.Void_Ptr,
            Interfaces.C.int (Item) - 1,
            Interfaces.C.int (X),
            Interfaces.C.int (Y),
            Interfaces.C.int (W),
            Interfaces.C.int (H),
            Boolean'Pos (Selected));
    end Draw_Item;


    function Handle
           (This  : in out Menu;
            Event : in     Event_Kind)
        return Event_Outcome is
    begin
        return Not_Handled;
    end Handle;


end FLTK.Widgets.Menus;