#! /usr/bin/perl
##
## This code contributed by Yury Pshenichny <yura@zp.ua>
## based on demo.panel by Chris Leach <leachcj@bp.com>
## and pretty much redone by me
use ExtUtils::testlib;
use Curses;
eval { new_menu() };
if ($@ =~ /not defined by your vendor/) {
print STDERR "Curses was not compiled with menu function.\n";
exit 1;
}
my $ilist = [
[ "AAA" => "A descr" ],
[ "BBB" => "B descr" ],
[ "CCC" => "C descr" ],
[ "DDD" => "D descr" ],
[ "EEE" => "E descr" ],
[ "FFF" => "F descr" ],
[ "GGG" => "G descr" ],
[ "HHH" => "H descr (This item has a very long descr) "]
];
initscr;
noecho;
my @il;
my @pack;
foreach my $I (@$ilist) {
my $item = new_item($I->[0], $I->[1]);
if ($item eq '') { fatal("new_item($I->[0], $I->[1]) failed: $item") }
push @il, $item;
push @pack, ${$item};
}
push @pack, 0;
my $menu = new_menu(pack "L!*", @pack);
if ($menu eq '') { fatal("new_menu failed") }
my $rows;
my $cols;
set_menu_mark($menu, '->');
set_menu_format($menu, 3, 1);
scale_menu($menu, $rows, $cols);
my $mwin = newwin($rows + 2, $cols + 2, 8, 15);
my $msub = derwin($mwin, $rows, $cols, 1, 1);
set_menu_win($menu, $mwin);
set_menu_sub($menu, $msub);
box($mwin, 0, 0);
keypad($mwin, 1);
post_menu($menu);
addstr(0, 0, "Use KEY_UP/KEY_DOWN/KEY_PPAGE/KEY_NPAGE to navigate");
addstr(1, 0, "Press 'ENTER' to select item, or 'F1' to exit");
refresh();
my $ci;
while(1) {
my $ch = getch($mwin);
if ($ch == KEY_UP) {
menu_driver($menu, REQ_UP_ITEM);
}
elsif ($ch == KEY_DOWN) {
menu_driver($menu, REQ_DOWN_ITEM);
}
elsif ($ch == KEY_PPAGE) {
menu_driver($menu, REQ_SCR_UPAGE);
}
elsif ($ch == KEY_NPAGE) {
menu_driver($menu, REQ_SCR_DPAGE);
}
elsif ($ch == KEY_F(1)) {
last;
}
elsif ($ch eq "\r" or $ch eq "\n") {
$ci = current_item($menu);
last;
}
elsif ($ch =~ /^\S$/) {
menu_driver($menu, $ch);
}
else {
beep();
}
}
if ($ci) {
addstr(0, 0, "You selected " . item_name($ci) . "\n");
}
else {
addstr(0, 0, "You didn't select anything\n");
}
clrtoeol(1,0);
refresh();
sleep 2;
unpost_menu($menu);
delwin($mwin);
free_menu($menu);
map { free_item($_) } @il;
endwin();
exit 0;
sub fatal {
clrtobot(0, 0);
addstr(0, 0, "@_\n");
refresh();
sleep 2;
exit 1;
}