]> ncurses.scripts.mit.edu Git - ncurses.git/blob - Ada95/samples/ncurses2-menu_test.adb
0b96315e1c04cbf8a2db48a91bb2c0f5a54b956c
[ncurses.git] / Ada95 / samples / ncurses2-menu_test.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                       GNAT ncurses Binding Samples                       --
4 --                                                                          --
5 --                                 ncurses                                  --
6 --                                                                          --
7 --                                 B O D Y                                  --
8 --                                                                          --
9 ------------------------------------------------------------------------------
10 -- Copyright (c) 2000 Free Software Foundation, Inc.                        --
11 --                                                                          --
12 -- Permission is hereby granted, free of charge, to any person obtaining a  --
13 -- copy of this software and associated documentation files (the            --
14 -- "Software"), to deal in the Software without restriction, including      --
15 -- without limitation the rights to use, copy, modify, merge, publish,      --
16 -- distribute, distribute with modifications, sublicense, and/or sell       --
17 -- copies of the Software, and to permit persons to whom the Software is    --
18 -- furnished to do so, subject to the following conditions:                 --
19 --                                                                          --
20 -- The above copyright notice and this permission notice shall be included  --
21 -- in all copies or substantial portions of the Software.                   --
22 --                                                                          --
23 -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  --
24 -- OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               --
25 -- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   --
26 -- IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   --
27 -- DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    --
28 -- OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    --
29 -- THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               --
30 --                                                                          --
31 -- Except as contained in this notice, the name(s) of the above copyright   --
32 -- holders shall not be used in advertising or otherwise to promote the     --
33 -- sale, use or other dealings in this Software without prior written       --
34 -- authorization.                                                           --
35 ------------------------------------------------------------------------------
36 --  Author: Eugene V. Melaragno <aldomel@ix.netcom.com> 2000
37 --  Version Control
38 --  $Revision: 1.1 $
39 --  Binding Version 01.00
40 ------------------------------------------------------------------------------
41 with ncurses2.util; use ncurses2.util;
42
43 with Terminal_Interface.Curses; use Terminal_Interface.Curses;
44 with Terminal_Interface.Curses.Menus; use Terminal_Interface.Curses.Menus;
45 with Terminal_Interface.Curses.Mouse; use Terminal_Interface.Curses.Mouse;
46
47 procedure ncurses2.menu_test is
48    function menu_virtualize (c : Key_Code) return Menu_Request_Code;
49    procedure xAdd (l : Line_Position; c : Column_Position; s : String);
50
51    function menu_virtualize (c : Key_Code) return Menu_Request_Code is
52    begin
53       case c is
54          when Character'Pos (newl) | Key_Exit =>
55             return Menu_Request_Code'Last + 1; --  MAX_COMMAND? TODO
56          when  Character'Pos ('u')  =>
57             return M_ScrollUp_Line;
58          when  Character'Pos ('d') =>
59             return M_ScrollDown_Line;
60          when  Character'Pos ('b') |  Key_Next_Page =>
61             return M_ScrollUp_Page;
62          when  Character'Pos ('f') |  Key_Previous_Page =>
63             return M_ScrollDown_Page;
64          when  Character'Pos ('n') |  Key_Cursor_Down =>
65             return M_Next_Item;
66          when  Character'Pos ('p') |  Key_Cursor_Up =>
67             return M_Previous_Item;
68          when  Character'Pos (' ') =>
69             return M_Toggle_Item;
70          when  Key_Mouse =>
71             return c;
72          when others =>
73             Beep;
74             return c;
75       end case;
76    end menu_virtualize;
77
78    MENU_Y : constant Line_Count := 8;
79    MENU_X : constant Column_Count := 8;
80
81    type String_Access is access String;
82
83    animals : constant array (Positive range <>) of String_Access :=
84      (new String'("Lions"),
85       new String'("Tigers"),
86       new String'("Bears"),
87       new String'("(Oh my!)"),
88       new String'("Newts"),
89       new String'("Platypi"),
90       new String'("Lemurs"));
91
92    items_a : Item_Array_Access := new Item_Array (1 .. animals'Last + 1);
93
94    tmp : Event_Mask;
95    procedure xAdd (l : Line_Position; c : Column_Position; s : String) is
96    begin
97       Add (Line => l, Column => c, Str => s);
98    end xAdd;
99
100    mrows : Line_Count;
101    mcols : Column_Count;
102
103    menuwin : Window;
104
105    m : Menu;
106
107    c1 : Key_Code;
108
109    c : Driver_Result;
110    r : Menu_Request_Code;
111 begin
112    tmp := Start_Mouse;
113    xAdd (0, 0, "This is the menu test:");
114    xAdd (2, 0, "  Use up and down arrow to move the select bar.");
115    xAdd (3, 0, "  'n' and 'p' act like arrows.");
116    xAdd (4, 0, "  'b' and 'f' scroll up/down (page), 'u' and 'd' (line).");
117    xAdd (5, 0, "  Press return to exit.");
118    Refresh;
119
120    for i in animals'Range loop
121       items_a (i) := New_Item (animals (i).all);
122    end loop;
123    items_a (animals'Last + 1) := Null_Item;
124
125    m := New_Menu (items_a);
126
127    Set_Format (m, Line_Position (animals'Last + 1) / 2, 1);
128    Scale (m, mrows, mcols);
129
130    menuwin := Create (mrows + 2, mcols + 2, MENU_Y, MENU_X);
131    Set_Window (m, menuwin);
132    Set_KeyPad_Mode (menuwin, True);
133    Box (menuwin); -- 0,0?
134
135    Set_Sub_Window (m, Derived_Window (menuwin, mrows, mcols, 1, 1));
136
137    Post (m);
138
139    loop
140       c1 := Getchar (menuwin);
141       r := menu_virtualize (c1);
142       c := Driver (m, r);
143       exit when c = Unknown_Request; -- E_UNKNOWN_COMMAND?
144       if c = Request_Denied then
145          Beep;
146       end if;
147       --  continue ?
148    end loop;
149
150    Move_Cursor (Line => Lines - 2, Column => 0);
151    Add (Str => "You chose: ");
152    Add (Str => Name (Current (m)));
153    Add (Ch => newl);
154    Pause; -- the C version didn't use Pause, it spelled it out
155
156    Post (m, False); --  unpost, not clear :-(
157    declare begin
158       Delete (menuwin);
159    exception when Curses_Exception => null; end;
160    --  menuwin has children so will raise the exception.
161
162    Delete (m);
163
164    tmp := Start_Mouse (No_Events);
165 end ncurses2.menu_test;