File: demo_template

package info (click to toggle)
libperlmenu-perl 4.0-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 556 kB
  • sloc: perl: 4,691; makefile: 8
file content (214 lines) | stat: -rwxr-xr-x 6,667 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/local/bin/perl5
#**************************************************************************
# demo_template --  Full-screen data entry template demo
#
# Notes:   Perl4 - Requires curseperl
#          Perl5 - Requires William Setzer's "Curses" extension
#
#          Demonstrates data entry using templates (template file is
#          "template_data" in the current directory)
#
# Author:  Steven L. Kunz
#          Networked Applications
#          Iowa State University Computation Center
#          Ames, IA  50011
#          Email: skunz@iastate.edu
#
# Date:    February 1997
#**************************************************************************

# Perl5+Curses ONLY!
# Comment these lines for use with Perl4/curseperl
BEGIN { $Curses::OldCurses = 1; }
use Curses;                     # PerlMenu needs "Curses"
use perlmenu;                   # Main menu package (Perl5 only)
require "./menuutil.pl";        # For "pause" and "print_nl" routines.

# Perl4/curseperl ONLY!
# Uncomment these lines for use with Perl4/curseperl
# (Did you remember to run "create_menu.pl"?)
#require "./menu.pl";           # Main menu package (Perl4 only)
#require "./menuutil.pl";       # For "pause" and "print_nl" routines.

$| = 1;				# Flush after every write to stdout

@input_data = ();	# Place to put data entered on screen
@defaults = ();		# Default data
@protect = ();		# Protected markers
@required = ();		# Required field markers
$bell = "\007";		# Ascii bell character
$row = $col = 0;	# Storage for row/col used by menuutil.pl

#
# Since we are not using menus in this example, we need to call "menu_init"
# to initialize the curses environment.  Not necessary if you have at
# least one menu display (which will include a menu_init) first.
#
&menu_init();

#
# Activate left and right markers, both in standout rendition.
#
&menu_template_prefs("*"," ",1,"*"," ",1);

#
# Load the template from in-line code (ending at "END_OF_TEMPLATE" in this
# source file).  Data entry fields denoted by underscores ("_") or
# back-slashes ("\");
#
# To demonstate loading the template from the data file, comment out all
# statements from "&menu_load_template_array(...)" to (and including) the
# "END_OF_TEMPLATE" statement and ADD the following line instead:
#
#        &menu_load_template("./template_data");
#
# This will load the template from the datafile "./template_data" instead
# of from the in-line source.
#
&menu_load_template_array(split("\n", <<'END_OF_TEMPLATE'));

                        Template Entry Demonstration

   Address Data Example                    Record # ___

   Name: [_____________________________________________]
   Addr: [_____________________________________________]
   City: [__________________]  State: [__]  Zip: [\\\\\] 

   Phone: (\\\) \\\-\\\\            Password: [^^^^^^^^]

   Enter all information available.
   Edit fields with left/right arrow keys or "delete".
   Switch fields with "Tab" or up/down arrow keys.
   Indicate completion by pressing "Return".
   Refresh screen with "Control-L".
   Abort this demo here with "Control-X".
END_OF_TEMPLATE

&menu_overlay_template(0,28,"Perl Menu Version 4.0",1,1);
&menu_overlay_template($LINES-5,10,
	"Fields marked with a \"*\" are required.",1);

#
# Define "Control X" as "abort data input"
#
&menu_template_setexit("\cX");

#
# Set defaults for all records the same in this example.
# For record updating you would set the defaults to the existing values
# from an old record.
#
$defaults[0] = 0;			# Record number
$defaults[1] = "Sample name";		# Name
$defaults[2] = "Sample address";	# Addr
$defaults[3] = "Sample city";		# City
$defaults[4] = "IA";			# State
$defaults[5] = "";			# Zip
$defaults[6] = "";			# Phone - area code
$defaults[7] = "";			# Phone - first three digits
$defaults[8] = "";			# Phone - last four digits
$defaults[9] = "Barney";		# Password

#
# Set protected fields for all records in this example.
# This lets us supply a record number as a default in the first field but
# not allow the user to change it.
#
$protect[0] = 1;	# Record number (protected, filled in by call parm)
$protect[1] = 0;	# All remaining fields are unprotected
$protect[2] = 0;
$protect[3] = 0;
$protect[4] = 0;
$protect[5] = 0;
$protect[6] = 0;
$protect[7] = 0;
$protect[8] = 0;
$protect[9] = 0;

#
# Set required fields for records in this example.
# Note that the offset value is "2" to prevent overlaying the "["
# on the template.
#
$required[0] = 0;
$required[1] = 2;	# Name
$required[2] = 0;
$required[3] = 0;
$required[4] = 0;
$required[5] = 0;
$required[6] = 0;
$required[7] = 0;
$required[8] = 0;
$required[9] = 2;	# Password

#
# Input three records
#
for ($i = 1; $i <= 3; $i++) {

  $defaults[0] = $i;	# Set the record number in the protected field

  # IMPORTANT: Note the use of pointers to arrays here
  &menu_display_template(*input_data,*defaults,*protect,"template_exit",
			 *required);
  last if (&menu_getexit() eq "\cX");

  # Demonstrate a template overlay the first time
  if ($i == 1) {
    @bad_data = @input_data; # Reload the data we just got
    &menu_overlay_template($LINES-5,10,"This is a template overlay.$bell");
    &menu_overlay_template($LINES-4,10,"(It could be an error message)");
    &menu_overlay_template($LINES-3,10,
	"Note that the data is from the previous screen.");
    # Let them reenter data
    &menu_display_template(*input_data,*bad_data,*protect,"template_exit",
			   *required);
    &menu_overlay_clear();
    last if (&menu_getexit() eq "\cX");
  }

  # Display what we got the last time
  &clear_screen();
  &print_nl("Record #$i",1);
  &print_nl("Here is what was returned in \@input_data:",2); 
  for ($j = 0; $j <= $#input_data; $j++) {
    &print_nl("\$input_data[$j]: $input_data[$j]",1);
  }
  &pause("");
}

&clear_screen();
&refresh();
&endwin;
exit(0);

#**********
# TEMPLATE_EXIT - Exit routine for "menu_display_template"
#**********
sub template_exit {
  local($direction,$last_index,$next_index,$still_required) = @_;

# Return now if they are skipping between fields
  if ($direction) { return($next_index); }

#
# Check for forced exit (aborted data entry).
# Note that this routine uses a "-2" return code, which means "ignore
# required fields checking".
#
  if (&menu_getexit() eq "\cX") { return(-2); }

# User says they are done (they pressed "Return").
  &menu_overlay_clear(); # Clear any old overlays

# Put out message if there are still required fields.
  if ($still_required) {
    &menu_overlay_template($LINES-5,10,
	"Fields marked with a \"*\" are STILL required.",1);
    return(-1);		# Still need required field(s) - auto-position
  }

# Let them be done.
  return(-1);
}