ncurses2-menu_test.adb [plain text]
with ncurses2.util; use ncurses2.util;
with Terminal_Interface.Curses; use Terminal_Interface.Curses;
with Terminal_Interface.Curses.Menus; use Terminal_Interface.Curses.Menus;
with Terminal_Interface.Curses.Mouse; use Terminal_Interface.Curses.Mouse;
procedure ncurses2.menu_test is
function menu_virtualize (c : Key_Code) return Menu_Request_Code;
procedure xAdd (l : Line_Position; c : Column_Position; s : String);
function menu_virtualize (c : Key_Code) return Menu_Request_Code is
begin
case c is
when Character'Pos (newl) | Key_Exit =>
return Menu_Request_Code'Last + 1; when Character'Pos ('u') =>
return M_ScrollUp_Line;
when Character'Pos ('d') =>
return M_ScrollDown_Line;
when Character'Pos ('b') | Key_Next_Page =>
return M_ScrollUp_Page;
when Character'Pos ('f') | Key_Previous_Page =>
return M_ScrollDown_Page;
when Character'Pos ('n') | Key_Cursor_Down =>
return M_Next_Item;
when Character'Pos ('p') | Key_Cursor_Up =>
return M_Previous_Item;
when Character'Pos (' ') =>
return M_Toggle_Item;
when Key_Mouse =>
return c;
when others =>
Beep;
return c;
end case;
end menu_virtualize;
MENU_Y : constant Line_Count := 8;
MENU_X : constant Column_Count := 8;
type String_Access is access String;
animals : constant array (Positive range <>) of String_Access :=
(new String'("Lions"),
new String'("Tigers"),
new String'("Bears"),
new String'("(Oh my!)"),
new String'("Newts"),
new String'("Platypi"),
new String'("Lemurs"));
items_a : constant Item_Array_Access :=
new Item_Array (1 .. animals'Last + 1);
tmp : Event_Mask;
procedure xAdd (l : Line_Position; c : Column_Position; s : String) is
begin
Add (Line => l, Column => c, Str => s);
end xAdd;
mrows : Line_Count;
mcols : Column_Count;
menuwin : Window;
m : Menu;
c1 : Key_Code;
c : Driver_Result;
r : Menu_Request_Code;
begin
tmp := Start_Mouse;
xAdd (0, 0, "This is the menu test:");
xAdd (2, 0, " Use up and down arrow to move the select bar.");
xAdd (3, 0, " 'n' and 'p' act like arrows.");
xAdd (4, 0, " 'b' and 'f' scroll up/down (page), 'u' and 'd' (line).");
xAdd (5, 0, " Press return to exit.");
Refresh;
for i in animals'Range loop
items_a (i) := New_Item (animals (i).all);
end loop;
items_a (animals'Last + 1) := Null_Item;
m := New_Menu (items_a);
Set_Format (m, Line_Position (animals'Last + 1) / 2, 1);
Scale (m, mrows, mcols);
menuwin := Create (mrows + 2, mcols + 2, MENU_Y, MENU_X);
Set_Window (m, menuwin);
Set_KeyPad_Mode (menuwin, True);
Box (menuwin);
Set_Sub_Window (m, Derived_Window (menuwin, mrows, mcols, 1, 1));
Post (m);
loop
c1 := Getchar (menuwin);
r := menu_virtualize (c1);
c := Driver (m, r);
exit when c = Unknown_Request; if c = Request_Denied then
Beep;
end if;
end loop;
Move_Cursor (Line => Lines - 2, Column => 0);
Add (Str => "You chose: ");
Add (Str => Name (Current (m)));
Add (Ch => newl);
Pause;
Post (m, False); declare begin
Delete (menuwin);
exception when Curses_Exception => null; end;
Delete (m);
End_Mouse (tmp);
end ncurses2.menu_test;