#!/usr/local/bin/curseperl
#**************************************************************************
# demo --  Simple perl menu demo
#
# Notes:   Requires curseperl.
#          Demonstrates "latched" (remembered) menu position technique
#          in "long" menu routine.
#
# Author:  Steven L. Kunz
#          Networking & Communications
#          Iowa State University Computation Center
#          Ames, IA  50011
#          Email: skunz@iastate.edu
#
# Date:    Sept, 1993
#**************************************************************************

require "./menu.pl";

# Uncomment this line to enable shell-escapes.
# If your shell does not have a "SHELL" environment variable, replace the
# call parameter with something like "/bin/sh".
# &menu_shell_command($ENV{"SHELL"});

  $menu_default_top = 0; # Storage for mainline top item number.
  $menu_default = 0;     # Storage for mainline arrow location.

  while (1) {
    &menu_init(1,"Perlmenu Version 2.1 - Demonstration");
    &menu_item("Exit this demo","exit");
    &menu_item("See a single-page menu demo","test_short_menu");
    &menu_item("See a multiple-page menu demo","test_long_menu");
    &menu_item("See a radio-button menu demo","test_radio_menu");
    &menu_item("See a multiple-selection menu demo","test_mult_menu");

    $sel = &menu_display("Which demo would you like to see? (Press \"h\" for help)",
                         $menu_default,$menu_default_top);

    if ($sel eq "exit") { last; }
    if ($sel ne "%UP%") { 
      
      &$sel; # Note that this assumes the "action_text" is a subroutine name	
    }
  }
  exit;

#
#  Build a short (one page) demo menu.
#
sub test_short_menu {
  local($sel);

  while (1) {

# Init a numbered menu with a title
    &menu_init(1,"Short Menu (fits on one page)");

# Add item to return to main menu.
    &menu_item("Exit \"Short Menu\" demo","exit");

# Add several items
    &menu_item("Dog","animal");
    &menu_item("Cat","animal");
    &menu_item("Granite","mineral");
    &menu_item("Mouse","animal");
    &menu_item("Shale","mineral");
    &menu_item("Onion","vegetable");
    &menu_item("Carrot","vegetable");

# Display menu and process selection.
# Note that previous position is not remembered because parms 2 and 3 
# are not supplied to store values used on subsequent call.
    $sel= &menu_display("");

    if (($sel eq "%UP%") || ($sel eq "exit")) { return; }
    print "You picked a $sel.\n";
    $ch = <STDIN>;
  }
}

#
# Build demo long menu (several pages)
#
sub test_long_menu {
  local($sel_num);

  local($menu_default_top) = 0; # Storage for local top menu item number.
  local($menu_default) = 0;     # Storage for local arrow location.

  while (1) {

# Init a numbered menu with title
    &menu_init(1,"Long Menu (fits on several pages)");

# Add item to return to main menu.
    &menu_item("Exit \"Long Menu\" demo","exit");

# Build 50 entries in the menu
    $i = 1;
    while ($i < 50) {
      $sel_num = $i + 1;
      &menu_item("Item $sel_num","action-$sel_num");
      $i++;
    }

# Get user selection.
# Note that local parms 2 and 3 are provided to provide storage of the
# default arrow location and top menu item on the screen for subsequent call.
    $sel = &menu_display("",$menu_default,$menu_default_top);

    if (($sel eq "%UP%") || ($sel eq "exit")) { return; }
    print "You picked the item with selection-action $sel.\n";
    $ch = <STDIN>;
  }
}

#
#  Build a radio-button demo menu.
#
sub test_radio_menu {
  local($sel);

# Init a numbered menu with a title
  &menu_init(1,"Radio-button Menu");

# Add a few items
  &menu_item("Setting A","a");
  &menu_item("Setting B","b");
  &menu_item("Setting C","c");

# Display menu and process selection.
# Note that the second operand is the "current selection".
  $sel= &menu_display_radio("","a");

  if ($sel eq "%UP%") { return; }
  print "You set it to $sel.\n";
  $ch = <STDIN>;
}

#
#  Build a multiple-selection demo menu.
#
sub test_mult_menu {
  local($sel,$i,$sel_num);

# Init a numbered menu with a title
  &menu_init(1,"Multiple Selection Menu");

# Add a few items
#  &menu_item("Dog","dog");
#  &menu_item("Cat","cat");
#  &menu_item("Goldfish","goldfish");

# Build 50 entries in the menu
    $i = 1;
    while ($i < 50) {
      $sel_num = $i + 1;
      &menu_item("Item $sel_num","action-$sel_num");
      $i++;
    }

# Display menu and process selection.
  $sel = &menu_display_mult("");

  if ($sel eq "%UP%") { return; }

  if ($sel eq "%NONE%") {
    print "(You didn't select anything)\n";
  } else {
    print "You selected the following:\n";
    split(/[,]/,$sel);  # Put return in @_
    foreach (@_) { print "  $_\n"; }
  }
  $ch = <STDIN>;
}
