File: test.pl

package info (click to toggle)
libcurses-widgets-perl 1.997-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 364 kB
  • ctags: 160
  • sloc: perl: 2,107; makefile: 3
file content (316 lines) | stat: -rwxr-xr-x 8,399 bytes parent folder | download | duplicates (5)
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/usr/bin/perl -w
#
# Simple script that demonstrates the uses of Curses::Widgetss
#
# $Id: test.pl,v 1.104 2002/11/14 01:36:48 corliss Exp corliss $
#

use strict;
use Curses;
use Curses::Widgets;  # Included to import select_colour & scankey
use Curses::Widgets::TextField;
use Curses::Widgets::ButtonSet;
use Curses::Widgets::ProgressBar;
use Curses::Widgets::TextMemo;
use Curses::Widgets::ListBox;
use Curses::Widgets::Calendar;
use Curses::Widgets::ComboBox;
use Curses::Widgets::Menu;
use Curses::Widgets::Label;

#####################################################################
#
# Set up the environment
#
#####################################################################

my ($mwh, $key, $i, $p);
my (@widgets, @descriptions);

#####################################################################
#
# Program Logic starts here
#
#####################################################################

# Unless specifically noted, most functions are provided by the Curses
# package, *not* Curses::Widgets.  See the pod for Curses for more
# information on the functions.  Additional information is available
# with the (n)Curses man pages (section 3), if you have them.
$mwh = new Curses;
noecho();
halfdelay(5);
$mwh->keypad(1);
$mwh->syncok(1);
curs_set(0);
leaveok(1);

# Draw the main window, and wait for a key press (the scankey
# function is imported from the Curses::Widgets module)
main_win();
comment_box(<< '__EOF__');
Welcome to the Curses::Widgets Test Script!

Press any key to begin.
__EOF__
$key = scankey($mwh);


# Create each of the widgets beforehand
$widgets[0] = Curses::Widgets::Menu->new({
  FOREGROUND  => 'white',
  BACKGROUND  => 'green',
  BORDER      => 1,
  CURSORPOS => [qw(File)],
  MENUS       => {
    MENUORDER   => [qw(File Help)],
    File        => {
      ITEMORDER => [qw(Open Save Exit)],
      Open      => sub { 1 },
      Save      => sub { 1 },
      Exit      => sub { exit 0 },
      },
    Help        => {
      ITEMORDER => [qw(Help About)],
      Help      => sub { 1 },
      About     => sub { 1 },
      },
    },
  
  });
$descriptions[0] = << '__EOF__';
Curses::Widgets::Menu -- Menus

Use the arrow keys to navigate, and <ESC> to exit a menu without selecting anything.  Use <TAB> to move to the next widget.
__EOF__

$widgets[1] = Curses::Widgets::ButtonSet->new({
  Y           => 2,
  X           => 2,
  FOREGROUND  => 'white',
  BACKGROUND  => 'black',
  BORDER      => 0,
  LABELS      => [ qw( OK CANCEL HELP ) ],
  LENGTH      => 8,
  HORIZONTAL  => 1,
  });
$descriptions[1] = << '__EOF__';
Curses::Widgets::ButtonSet -- Horizontal set without borders.

Use the arrow keys to navigate among the buttons, and press <RETURN> or <TAB> to move to the next widget (set).
__EOF__

$widgets[2] = Curses::Widgets::ButtonSet->new({
  Y           => 3,
  X           => 1,
  FOREGROUND  => 'white',
  BACKGROUND  => 'blue',
  BORDER      => 1,
  LABELS      => [ qw( Button1 Button2 Button3 Quit ) ],
  LENGTH      => 9,
  HORIZONTAL  => 0,
  });
$descriptions[2] = << '__EOF__';
Curses::Widgets::ButtonSet -- Vertical set with borders.

Use the arrow keys to navigate among the buttons, and press <RETURN> or <TAB> to move to the next widget (set).
__EOF__

$widgets[3] = Curses::Widgets::TextField->new({
  Y           => 4,
  X           => 14,
  COLUMNS     => 20,
  MAXLENGTH   => 30,
  FOREGROUND  => 'green',
  BACKGROUND  => 'blue',
  VALUE       => 'Test Value',
  BORDERCOL   => 'black',
  BORDER      => 1,
  CAPTION     => 'Test Field',
  CAPTIONCOL  => 'yellow',
  });
$descriptions[3] = << '__EOF__';
Curses::Widgets::TextField -- Text field with a border and caption.

Press <RETURN> or <TAB> to move to the next widget (set).
__EOF__

$widgets[4] = Curses::Widgets::ProgressBar->new({
  Y           => 7,
  X           => 14,
  LENGTH      => 20,
  FOREGROUND  => 'yellow',
  BACKGROUND  => 'green',
  BORDER      => 1,
  BORDERCOL   => 'black',
  CAPTION     => 'Progress',
  CAPTIONCOL  => 'white',
  });
$descriptions[4] = << '__EOF__';
Curses::Widgets::ProgressBar -- Horizontal progress bar with border.

Please wait until the bar progresses to 100%.
__EOF__

$p = << "__EOF__";
This is an example memo that uses the Widgets class textwrap function to split
according to whitespace and column limits.
__EOF__

$widgets[5] = Curses::Widgets::TextMemo->new({
  Y           => 10,
  X           => 14,
  COLUMNS     => 20,
  FOREGROUND  => 'green',
  BACKGROUND  => 'blue',
  VALUE       => $p,
  BORDERCOL   => 'black',
  BORDER      => 1,
  CAPTION     => 'Test Memo',
  CAPTIONCOL  => 'yellow',
  });
$descriptions[5] = << '__EOF__';
Curses::Widgets::TextMemo -- Text memo with a border and caption.

Press <RETURN> or <TAB> to move to the next widget (set).
__EOF__

$widgets[6] = Curses::Widgets::ListBox->new({
  Y           => 2,
  X           => 38,
  COLUMNS     => 20,
  LISTITEMS   => ['Ham', 'Eggs', 'Cheese', 'Hash Browns', 'Toast'],
  MULTISEL    => 1,
  VALUE       => [0, 2],
  SELECTEDCOL => 'green',
  CAPTION     => 'List Box',
  CAPTIONCOL  => 'yellow',
  });
$descriptions[6] = << '__EOF__';
Curses::Widgets::ListBox -- This list box supports multiple and single selection modes.  Use <SPACE> or <RETURN> to toggle a selection.

Press <TAB> to move to the next widget (set).
__EOF__

$widgets[7] = Curses::Widgets::Calendar->new({
  Y             => 7,
  X             => 38,
  FOREGROUND    => 'black',
  BACKGROUND    => 'white',
  BORDER        => 1,
  CAPTION       => 'Appointments',
  CAPTIONCOL    => 'blue',
  HIGHLIGHT     => [1, 5, 17, 26],
  HIGHLIGHTCOL  => 'green',
  HEADERCOL     => 'red',
  });
$descriptions[7] = << '__EOF__';
Curses::Widgets::Calendar -- This calendar supports date highlighting and broad navigation capabilities.

Press <TAB> to move to the next widget (set).
__EOF__

$widgets[8] = Curses::Widgets::ComboBox->new({
  Y           => 3,
  X           => 62,
  FOREGROUND  => 'white',
  BACKGROUND  => 'red',
  LISTITEMS   => [qw(Mr. Mrs. Ms.)],
  COLUMNS     => 4,
  BORDER      => 1,
  });
$descriptions[8] = << '__EOF__';
Curses::Widgets::ComboBox -- This is a text field that also has a drop-down list to select values from.  Just press the down arrow.

Press <TAB> to move to the next widget (set).
__EOF__

# Draw each of the widgets
foreach (@widgets) { $_->draw($mwh) };
comment_box();

# Interactively demonstrate each widget
for ($i = 0; $i < scalar @widgets; $i++) { 
  comment_box($descriptions[$i]);
  if (ref($widgets[$i]) !~ /Progress/) {
    $widgets[$i]->execute($mwh);
    $widgets[$i]->draw($mwh);
  } else {
    while ($widgets[$i]->getField('VALUE') <
      $widgets[$i]->getField('MAX')) {
      $widgets[$i]->input(30);
      $widgets[$i]->draw($mwh);
      sleep 1;
    }
  }
}

# Label description
comment_box(<< '__EOF__');
This comment box has been demonstrating the use of the Curses::Widgets::Label.  Labels support left, centered, and right alignments, and with and without borders.

Press any key to continue.
__EOF__
scankey($mwh);

# Parting comments
comment_box(<< '__EOF__');
This concludes the Curses::Widgets demonstration.  Please send all comments, suggestions, criticisms, and bug reports to corliss@digitalmages.com.

Press any key to exit.
__EOF__
scankey($mwh);

exit 0;

END {
  # The END block just ensures that Curses always cleans up behind
  # itself
  endwin();
}

exit 0;

#####################################################################
#
# Subroutines follow here
#
#####################################################################

sub main_win {

  $mwh->erase();

  # This function selects a few common colours for the foreground colour
  $mwh->attrset(COLOR_PAIR(select_colour(qw(red black))));
  $mwh->box(ACS_VLINE, ACS_HLINE);
  $mwh->attrset(0);

  $mwh->standout();
  $mwh->addstr(0, 1, "Welcome to the Curses::Widgets " .
    "v${Curses::Widgets::VERSION} Demo!");
  $mwh->standend();
}

sub comment_box {
  my $message = shift;
  my ($cwh, $y, $x, @lines, $i, $line);
  my $label;

  # Get the main screen max y & X
  $mwh->getmaxyx($y, $x);

  # Render the comment box
  $label = Curses::Widgets::Label->new({
    CAPTION     => 'Comments',
    BORDER      => 1,
    LINES       => 5,
    COLUMNS     => $x - 4,
    Y           => $y - 8,
    X           => 1,
    VALUE       => $message,
    FOREGROUND  => 'white',
    BACKGROUND  => 'blue',
    });
  $label->draw($mwh);
}