blob: bce9782372a40f391e20eedf4f795af5de808dc2 [file] [log] [blame]
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301------------------------------------------------------------------------------
2-- --
3-- GNAT ncurses Binding Samples --
4-- --
5-- ncurses --
6-- --
7-- B O D Y --
8-- --
9------------------------------------------------------------------------------
Steve Kondikae271bc2015-11-15 02:50:53 +010010-- Copyright (c) 2000-2006,2011 Free Software Foundation, Inc. --
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053011-- --
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
Steve Kondikae271bc2015-11-15 02:50:53 +010038-- $Revision: 1.8 $
39-- $Date: 2011/03/23 00:39:28 $
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053040-- Binding Version 01.00
41------------------------------------------------------------------------------
42with ncurses2.util; use ncurses2.util;
43
44with Terminal_Interface.Curses; use Terminal_Interface.Curses;
45with Terminal_Interface.Curses.Menus; use Terminal_Interface.Curses.Menus;
46with Terminal_Interface.Curses.Mouse; use Terminal_Interface.Curses.Mouse;
47
48procedure ncurses2.menu_test is
Steve Kondikae271bc2015-11-15 02:50:53 +010049 function menu_virtualize (c : Key_Code) return Key_Code;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053050 procedure xAdd (l : Line_Position; c : Column_Position; s : String);
51
Steve Kondikae271bc2015-11-15 02:50:53 +010052 function menu_virtualize (c : Key_Code) return Key_Code is
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053053 begin
54 case c is
55 when Character'Pos (newl) | Key_Exit =>
56 return Menu_Request_Code'Last + 1; -- MAX_COMMAND? TODO
57 when Character'Pos ('u') =>
58 return M_ScrollUp_Line;
59 when Character'Pos ('d') =>
60 return M_ScrollDown_Line;
61 when Character'Pos ('b') | Key_Next_Page =>
62 return M_ScrollUp_Page;
63 when Character'Pos ('f') | Key_Previous_Page =>
64 return M_ScrollDown_Page;
65 when Character'Pos ('n') | Key_Cursor_Down =>
66 return M_Next_Item;
67 when Character'Pos ('p') | Key_Cursor_Up =>
68 return M_Previous_Item;
69 when Character'Pos (' ') =>
70 return M_Toggle_Item;
71 when Key_Mouse =>
72 return c;
73 when others =>
74 Beep;
75 return c;
76 end case;
77 end menu_virtualize;
78
79 MENU_Y : constant Line_Count := 8;
80 MENU_X : constant Column_Count := 8;
81
82 type String_Access is access String;
83
84 animals : constant array (Positive range <>) of String_Access :=
85 (new String'("Lions"),
86 new String'("Tigers"),
87 new String'("Bears"),
88 new String'("(Oh my!)"),
89 new String'("Newts"),
90 new String'("Platypi"),
91 new String'("Lemurs"));
92
93 items_a : constant Item_Array_Access :=
94 new Item_Array (1 .. animals'Last + 1);
95
96 tmp : Event_Mask;
97
98 procedure xAdd (l : Line_Position; c : Column_Position; s : String) is
99 begin
100 Add (Line => l, Column => c, Str => s);
101 end xAdd;
102
103 mrows : Line_Count;
104 mcols : Column_Count;
105
106 menuwin : Window;
107
108 m : Menu;
109
110 c1 : Key_Code;
111
112 c : Driver_Result;
Steve Kondikae271bc2015-11-15 02:50:53 +0100113 r : Key_Code;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530114begin
115 tmp := Start_Mouse;
116 xAdd (0, 0, "This is the menu test:");
117 xAdd (2, 0, " Use up and down arrow to move the select bar.");
118 xAdd (3, 0, " 'n' and 'p' act like arrows.");
119 xAdd (4, 0, " 'b' and 'f' scroll up/down (page), 'u' and 'd' (line).");
120 xAdd (5, 0, " Press return to exit.");
121 Refresh;
122
123 for i in animals'Range loop
Steve Kondikae271bc2015-11-15 02:50:53 +0100124 items_a.all (i) := New_Item (animals (i).all);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530125 end loop;
Steve Kondikae271bc2015-11-15 02:50:53 +0100126 items_a.all (animals'Last + 1) := Null_Item;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530127
128 m := New_Menu (items_a);
129
130 Set_Format (m, Line_Position (animals'Last + 1) / 2, 1);
131 Scale (m, mrows, mcols);
132
133 menuwin := Create (mrows + 2, mcols + 2, MENU_Y, MENU_X);
134 Set_Window (m, menuwin);
135 Set_KeyPad_Mode (menuwin, True);
136 Box (menuwin); -- 0,0?
137
138 Set_Sub_Window (m, Derived_Window (menuwin, mrows, mcols, 1, 1));
139
140 Post (m);
141
142 loop
143 c1 := Getchar (menuwin);
144 r := menu_virtualize (c1);
145 c := Driver (m, r);
146 exit when c = Unknown_Request; -- E_UNKNOWN_COMMAND?
147 if c = Request_Denied then
148 Beep;
149 end if;
150 -- continue ?
151 end loop;
152
153 Move_Cursor (Line => Lines - 2, Column => 0);
154 Add (Str => "You chose: ");
155 Add (Str => Name (Current (m)));
156 Add (Ch => newl);
157 Pause; -- the C version didn't use Pause, it spelled it out
158
159 Post (m, False); -- unpost, not clear :-(
160 declare begin
161 Delete (menuwin);
162 exception when Curses_Exception => null; end;
163 -- menuwin has children so will raise the exception.
164
165 Delete (m);
166
167 End_Mouse (tmp);
168end ncurses2.menu_test;