Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / perl-5.8.0 / bin / widget
CommitLineData
86530b38
AT
1#!/import/bw/tools/local/perl-5.8.0/bin/perl -w
2
3eval 'exec /import/bw/tools/local/perl-5.8.0/bin/perl -w -S $0 ${1+"$@"}'
4 if 0; # not running under some shell
5
6require 5.004;
7
8use Tk 800.000;
9use lib Tk->findINC('demos/widget_lib');
10use Tk::widgets qw/Dialog ErrorDialog ROText/;
11use WidgetDemo;
12use subs qw/invoke lsearch see_code see_vars show_stat view_widget_code/;
13use vars qw/$MW $FONT $WIDTRIB/;
14use vars qw/$CODE $CODE_RERUN $CODE_TEXT $VARS $VIEW $VIEW_TEXT/;
15use vars qw/$BRAKES $LIGHTS $OIL $SOBER $TRANS $WIPERS/;
16use vars qw/$COLOR $FONT_STYLE $POINT_SIZE $DEMO_FILE %DEMO_DESCRIPTION/;
17use strict;
18
19
20$MW = Tk::MainWindow->new;
21$MW->configure(-menu => my $menubar = $MW->Menu);
22
23{
24 package WidgetWrap;
25 @WidgetWrap::ISA = qw/Tk::MainWindow/;
26
27 # This magic conspires with widget's AUTOLOAD subroutine to make user
28 # contributed demonstrations that don't use WidgetDemo embed properly.
29 # The trick works because widget creates a superclass of Tk::MainWindow
30 # which invokes WidgetDemo() implicitly. You loose if you bypass the
31 # inheritance mechanism and call Tk::MainWindow directly.
32
33 sub new {
34 my ($name) = $::DEMO_FILE =~ m#([^/]+).pl$#;
35 $::MW->WidgetDemo(-name => $name, -text => $::DEMO_DESCRIPTION{$name});
36 }
37}
38
39@MainWindow::ISA = 'WidgetWrap';
40
41$MW->title('Widget Demonstration');
42$FONT = '-*-Helvetica-Medium-R-Normal--*-140-*-*-*-*-*-*';
43my $widget_lib = Tk->findINC('demos/widget_lib');
44my $wd = "$widget_lib/WidgetDemo.pm";
45$WIDTRIB = Tk->findINC('demos/widtrib');
46unless (Tk::tainting) {
47 $WIDTRIB = $ENV{WIDTRIB} if defined $ENV{WIDTRIB};
48 $WIDTRIB = $ARGV[0] if defined $ARGV[0];
49}
50
51# The code below creates the main window, consisting of a menu bar
52# and a text widget that explains how to use the program, plus lists
53# all of the demos as hypertext items.
54
55my $file = $menubar->cascade(qw/-label File -underline 0 -menuitems/ =>
56 [
57 [cascade => '~View', -menuitems =>
58 [
59 [command => '~widget', -command => [\&view_widget_code, __FILE__]],
60 [command => '~WidgetDemo', -command => [\&view_widget_code, $wd]],
61 ], # end cascade menuitems
62 ], # end view cascade
63 '',
64 [command => '~Quit', -command => [\&exit]],
65 ]);
66
67my $help = $menubar->cascade(qw/-label Help -underline 0 -menuitems/ =>
68 [
69 [command => '~About'],
70 ]);
71
72my $T = $MW->Scrolled('ROText',
73 -scrollbars => 'e',
74 -wrap => 'word',
75 -width => 60,
76 -height => 30,
77 -font => $FONT,
78 -setgrid => 1,
79)->grid(qw/-sticky nsew/);
80$MW->gridRowconfigure( 0, -weight => 1); # allow expansion in both ...
81$MW->gridColumnconfigure(0, -weight => 1); # ... X and Y dimensions
82
83my $STATUS_VAR;
84my $status = $MW->Label(-textvariable => \$STATUS_VAR, qw/-anchor w/);
85$status->grid(qw/-sticky ew/);
86
87# Create a bunch of tags to use in the text widget, such as those for
88# section titles and demo descriptions. Also define the bindings for
89# tags.
90
91$T->tagConfigure(qw/title -font -*-Helvetica-Bold-R-Normal--*-180-*-*-*-*-*-*/);
92$T->tagConfigure(qw/demo -lmargin1 1c -lmargin2 1c -foreground blue/);
93
94if ($MW->depth == 1) {
95 $T->tagConfigure(qw/hot -background black -foreground white/);
96 $T->tagConfigure(qw/visited -lmargin1 1c -lmargin2 1c -underline 1/);
97} else {
98 $T->tagConfigure(qw/hot -relief raised -borderwidth 1 -foreground red/);
99 $T->tagConfigure(qw/visited -lmargin1 1c -lmargin2 1c -foreground/ =>
100 '#303080');
101}
102
103$T->tagBind(qw/demo <ButtonRelease-1>/ => \&invoke);
104my $last_line = '';
105$T->tagBind(qw/demo <Enter>/ => [sub {
106 my($text, $sv) = @_;
107 my $e = $text->XEvent;
108 my($x, $y) = ($e->x, $e->y);
109 $last_line = $text->index("\@$x,$y linestart");
110 $text->tagAdd('hot', $last_line, "$last_line lineend");
111 $text->configure(qw/-cursor hand2/);
112 show_stat $sv, $text, $text->index('current');
113 }, \$STATUS_VAR]
114);
115$T->tagBind(qw/demo <Leave>/ => [sub {
116 my($text, $sv) = @_;
117 $text->tagRemove(qw/hot 1.0 end/);
118 $text->configure(qw/-cursor xterm/);
119 $$sv = '';
120 }, \$STATUS_VAR]
121);
122$T->tagBind(qw/demo <Motion>/ => [sub {
123 my($text, $sv) = @_;
124 my $e = $text->XEvent;
125 my($x, $y) = ($e->x, $e->y);
126 my $new_line = $text->index("\@$x,$y linestart");
127 if ($new_line ne $last_line) {
128 $text->tagRemove(qw/hot 1.0 end/);
129 $last_line = $new_line;
130 $text->tagAdd('hot', $last_line, "$last_line lineend");
131 }
132 show_stat $sv, $text, $text->index('current');
133 }, \$STATUS_VAR]
134);
135
136# Create the text for the text widget.
137
138$T->insert('end', "Perl/Tk Widget Demonstrations\n", 'title');
139$T->insert('end',
140"\nThis application provides a front end for several short scripts that demonstrate what you can do with Tk widgets. Each of the numbered lines below describes a demonstration; you can click on it to invoke the demonstration. Once the demonstration window appears, you can click the \"See Code\" button to see the Perl/Tk code that created the demonstration. If you wish, you can edit the code and click the \"Rerun Demo\" button in the code window to reinvoke the demonstration with the modified code.\n");
141
142# Define globals for demo toplevels, informative text with tags for
143# highlighting and specifying the demo's file name.
144
145$T->insert('end', "\n", '',
146 "Labels, buttons, checkbuttons, and radiobuttons\n", 'title');
147$T->insert('end', "1. Labels (text and images).\n", [qw/demo demo-labels/]);
148$T->insert('end', "2. Buttons.\n", [qw/demo demo-button/]);
149$T->insert('end', "3. Checkbuttons (select any of a group).\n",
150 [qw/demo demo-check/]);
151$T->insert('end', "4. Radiobuttons (select one of a group).\n",
152 [qw/demo demo-radio/]);
153$T->insert('end', "5. A 15-puzzle game made out of buttons.\n",
154 [qw/demo demo-puzzle/]);
155$T->insert('end', "6. Iconic buttons that use bitmaps.\n",
156 [qw/demo demo-icon/]);
157$T->insert('end', "7. Two labels displaying images.\n",
158 [qw/demo demo-image1/]);
159$T->insert('end', "8. A simple user interface for viewing images.\n",
160 [qw/demo demo-image2/]);
161
162$T->insert('end', "\n", '', "Listboxes\n", 'title');
163$T->insert('end', "1. 50 states.\n", [qw/demo demo-states/]);
164$T->insert('end', "2. Colors: change the color scheme for the application.\n",
165 [qw/demo demo-colors/]);
166$T->insert('end', "3. A collection of famous sayings.\n",
167 [qw/demo demo-sayings/]);
168
169$T->insert('end', "\n", '', "Entries\n", 'title');
170$T->insert('end', "1. Without scrollbars.\n", [qw/demo demo-entry1/]);
171$T->insert('end', "2. With scrollbars.\n", [qw/demo demo-entry2/]);
172$T->insert('end', "3. Simple Rolodex-like form.\n", [qw/demo demo-form/]);
173
174$T->insert('end', "\n", '', "Text\n", 'title');
175$T->insert('end', "1. Basic editable text.\n", [qw/demo demo-texts/]);
176$T->insert('end', "2. Text display styles.\n", [qw/demo demo-style/]);
177$T->insert('end', "3. Hypertext (tag bindings).\n", [qw/demo demo-bind/]);
178$T->insert('end', "4. A text widget with embedded windows.\n",
179 [qw/demo demo-twind/]);
180$T->insert('end', "5. A search tool built with a text widget.\n",
181 [qw/demo demo-search/]);
182
183$T->insert('end', "\n", '', "Canvases\n", 'title');
184$T->insert('end', "1. The canvas item types.\n", [qw/demo demo-items/]);
185$T->insert('end', "2. A simple 2-D plot.\n", [qw/demo demo-plot/]);
186$T->insert('end', "3. Text items in canvases.\n", [qw/demo demo-ctext/]);
187$T->insert('end', "4. An editor for arrowheads on canvas lines.\n",
188 [qw/demo demo-arrows/]);
189$T->insert('end', "5. A ruler with adjustable tab stops.\n",
190 [qw/demo demo-ruler/]);
191$T->insert('end', "6. A building floor plan.\n", [qw/demo demo-floor/]);
192$T->insert('end', "7. A simple scrollable canvas.\n", [qw/demo demo-cscroll/]);
193$T->insert('end', "8. Tiles and transparent images.\n", [qw/demo demo-transtile/]);
194
195$T->insert('end', "\n", '', "Scales\n", 'title');
196$T->insert('end', "1. Vertical scale.\n", [qw/demo demo-vscale/]);
197$T->insert('end', "2. Horizontal scale.\n", [qw/demo demo-hscale/]);
198
199$T->insert('end', "\n", '', "Menus\n", 'title');
200$T->insert('end', "1. A window containing several menus and cascades.\n",
201 [qw/demo demo-menus/]);
202$T->insert('end', "2. Like above, but in a manner particular to Perl/Tk.\n",
203 [qw/demo demo-menus2/]);
204$T->insert('end', "3. Menubuttons.\n", [qw/demo demo-menbut/]);
205
206$T->insert('end', "\n", '', "Common Dialogs\n", 'title');
207$T->insert('end', "1. Message boxes.\n", [qw/demo demo-msgBox/]);
208#$T->insert('end', "2. File selector.\n", [qw/demo demo-fselect/]);
209$T->insert('end', "2. File selection dialog.\n", [qw/demo demo-filebox/]);
210$T->insert('end', "3. Color picker.\n", [qw/demo demo-clrpick/]);
211
212$T->insert('end', "\n", '', "Simulations\n", 'title');
213$T->insert('end', "1. Balls bouncing in a cavity.\n", [qw/demo demo-bounce/]);
214
215$T->insert('end', "\n", '', "Miscellaneous\n", 'title');
216$T->insert('end', "1. The built-in bitmaps.\n", [qw/demo demo-bitmaps/]);
217$T->insert('end', "2. A dialog box with a local grab.\n",
218 [qw/demo demo-dialog1/]);
219$T->insert('end', "3. A dialog box with a global grab.\n",
220 [qw/demo demo-dialog2/]);
221
222$T->insert('end', "\n", '', "User Contributed Demonstrations\n", 'title');
223opendir(C, $WIDTRIB) or warn "Cannot open $WIDTRIB: $!";
224my(@dirent) = grep /^.+\.pl$/, sort(readdir C);
225closedir C;
226unshift @dirent, 'TEMPLATE.pl'; # I want it first
227my $i = 0;
228while ($_ = shift @dirent) {
229 next if /TEMPLATE\.pl/ and $i != 0;
230 unless (open(C, "$WIDTRIB/$_")) {
231 warn "Cannot open $_: $!" unless /TEMPLATE\.pl/;
232 next;
233 }
234 my($name) = /^(.*)\.pl$/;
235 $_ = <C>;
236 my($title) = /^#\s*(.*)$/;
237 $DEMO_DESCRIPTION{$name} = $title;
238 close C;
239 $T->insert('end', ++$i . ". $title\n", ['demo', "demo-$name"]);
240}
241
242# Create all the dialogs required by this demonstration.
243
244my $DIALOG_ABOUT = $MW->Dialog(
245 -title => 'About widget',
246 -bitmap => 'info',
247 -default_button => 'OK',
248 -buttons => ['OK'],
249 -text => " widget\n\nPerl Version $]" .
250 "\nTk Version $Tk::VERSION\n\n 2000/07/07",
251);
252$help->cget(-menu)->entryconfigure('About',
253 -command => [$DIALOG_ABOUT => 'Show'],
254);
255
256my $DIALOG_ICON = $MW->Dialog(
257 -title => 'Bitmap Menu Entry',
258 -bitmap => undef,
259 -default_button => 'OK',
260 -buttons => ['OK'],
261 -text => 'The menu entry you invoked displays a bitmap rather than a text string. Other than this, it is just like any other menu entry.',
262);
263$DIALOG_ICON->configure(-bitmap => undef); # keep -w from complaining
264
265MainLoop;
266
267sub AUTOLOAD {
268
269 # This routine handles the loading of most demo methods.
270
271 my($demo) = @_;
272
273 $T->Busy;
274 {
275 $DEMO_FILE = "$WIDTRIB/${demo}.pl" if -f "$WIDTRIB/${demo}.pl";
276 $DEMO_FILE = "$widget_lib/${demo}.pl" if -f "$widget_lib/${demo}.pl";
277 do $DEMO_FILE;
278 warn $@ if $@;
279 }
280 $T->Unbusy;
281 goto &$::AUTOLOAD if defined &$::AUTOLOAD;
282
283} # end AUTOLOAD
284
285sub invoke {
286
287 # This procedure is called when the user clicks on a demo description.
288
289 my($text) = @_;
290
291 my $index = $text->index('current');
292 my @tags = $T->tagNames($index);
293 my $i = lsearch('demo\-.*', @tags);
294 return if $i < 0;
295 my($demo) = $tags[$i] =~ /demo-(.*)/;
296 $T->tagAdd('visited', "$index linestart", "$index lineend");
297 {
298 no strict 'refs';
299 &$demo($demo);
300 }
301
302} # end invoke
303
304sub lsearch {
305
306 # Search the list using the supplied regular expression and return it's
307 # ordinal, or -1 if not found.
308
309 my($regexp, @list) = @_;
310 my($i);
311
312 for ($i=0; $i<=$#list; $i++) {
313 return $i if $list[$i] =~ /$regexp/;
314 }
315 return -1;
316
317} # end lsearch
318
319sub see_code {
320
321 # This procedure creates a toplevel window that displays the code for
322 # a demonstration and allows it to be edited and reinvoked.
323
324 my($demo) = @_;
325
326 my $file = "${demo}.pl";
327 if (not Exists $CODE) {
328 $CODE = $MW->Toplevel;
329 my $code_buttons = $CODE->Frame;
330 $code_buttons->pack(qw/-side bottom -fill x/);
331 my $code_buttons_dismiss = $code_buttons->Button(
332 -text => 'Dismiss',
333 -command => [$CODE => 'withdraw'],
334 );
335 $CODE_RERUN = $code_buttons->Button(-text => 'Rerun Demo');
336 $CODE_TEXT = $CODE->Scrolled('Text',
337 qw/-scrollbars e -height 40 -setgrid 1/);
338 $code_buttons_dismiss->pack(qw/-side left -expand 1/);
339 $CODE_RERUN->pack(qw/-side left -expand 1/);
340 $CODE_TEXT->pack(qw/-side left -expand 1 -fill both/);
341 } else {
342 $CODE->deiconify;
343 $CODE->raise;
344 }
345 $CODE_RERUN->configure(-command => sub {
346 eval $CODE_TEXT->get(qw/1.0 end/);
347 {
348 no strict 'refs';
349 &$demo($demo);
350 }
351 });
352 $CODE->iconname($file);
353 $file = "$WIDTRIB/${demo}.pl" if -f "$WIDTRIB/${demo}.pl";
354 $file = "$widget_lib/${demo}.pl" if -f "$widget_lib/${demo}.pl";
355 $CODE->title("Demo code: $file");
356 $CODE_TEXT->delete(qw/1.0 end/);
357 open(CODE, "<$file") or warn "Cannot open demo file $file: $!";
358 {
359 local $/ = undef;
360 $CODE_TEXT->insert('1.0', <CODE>);
361 }
362 close CODE;
363 $CODE_TEXT->markSet(qw/insert 1.0/);
364
365} # end see_code
366
367sub see_vars {
368
369 # Create a top-level window that displays a bunch of global variable values
370 # and keeps the display up-to-date even when the variables change value.
371 # $args is a pointer to a list of list of 2:
372 #
373 # ["variable description", \$VAR]
374 #
375 # The old trick of passing a string to serve as the description and a soft
376 # reference to the variable no longer works with lexicals and use strict.
377
378 my($parent, $args) = @_;
379
380 $VARS->destroy if Exists($VARS);
381 $VARS = $parent->Toplevel;
382 $VARS->geometry('+300+300');
383 $VARS->title('Variable Values');
384 $VARS->iconname('Variables');
385
386 my $title = $VARS->Label(
387 -text => 'Variable Values:',
388 -width => 20,
389 -anchor => 'center',
390 -font => '-*-helvetica-medium-r-normal--*-180-*-*-*-*-*-*',
391 );
392 $title->pack(qw/-side top -fill x/);
393 my($label, $var);
394 foreach my $i (@$args) {
395 ($label, $var) = @$i;
396 my $wf = $VARS->Frame->pack(qw/-anchor w/);
397 $wf->Label(-text => "$label: ")->pack(qw/-side left/);
398 $wf->Label(-textvariable => $var)->pack(qw/-side left/);
399 }
400 $VARS->Button(-text => 'OK', -command => [$VARS => 'destroy'])->
401 pack(qw/-side bottom -pady 2/);
402
403} # end see_vars
404
405sub show_stat {
406
407 # Display name of current demonstration. $sv is a reference to the
408 # status Label -textvariable, $text is the Text widget reference and
409 # $index is the demonstration index in the Text widget.
410
411 my($sv, $text, $index) = @_;
412
413 my @tags = $text->tagNames($index);
414 my $i = lsearch('demo\-.*', @tags);
415 return if $i < 0;
416 my($demo) = $tags[$i] =~ /demo-(.*)/;
417 $$sv = "Click Button-1 to run the \"$demo\" demonstration.";
418
419} # end show_stat
420
421sub view_widget_code {
422
423 # Expose a file's innards to the world too, but only for viewing.
424
425 my($widget) = @_;
426
427 if (not Exists $VIEW) {
428 $VIEW = $MW->Toplevel;
429 $VIEW->iconname('widget');
430 my $view_buttons = $VIEW->Frame;
431 $view_buttons->pack(qw/-side bottom -expand 1 -fill x/);
432 my $view_buttons_dismiss = $view_buttons->Button(
433 -text => 'Dismiss',
434 -command => [$VIEW => 'withdraw'],
435 );
436 $view_buttons_dismiss->pack(qw/-side left -expand 1/);
437 $VIEW_TEXT = $VIEW->Scrolled('Text',
438 qw/-scrollbars e -height 40 -setgrid 1/);
439 $VIEW_TEXT->pack(qw/-side left -expand 1 -fill both/);
440 } else {
441 $VIEW->deiconify;
442 $VIEW->raise;
443 }
444 $VIEW->title("Demo code: $widget");
445 $VIEW_TEXT->configure(qw/-state normal/);
446 $VIEW_TEXT->delete(qw/1.0 end/);
447 open(VIEW, "<$widget") or warn "Cannot open demo file $widget: $!";
448 {
449 local $/ = undef;
450 $VIEW_TEXT->insert('1.0', <VIEW>);
451 }
452 close VIEW;
453 $VIEW_TEXT->markSet(qw/insert 1.0/);
454 $VIEW_TEXT->configure(qw/-state disabled/);
455
456} # end view_widget_code
457
458__END__
459
460=head1 NAME
461
462widget - Demonstration of Perl/Tk widgets
463
464=head1 SYNOPSYS
465
466 widget [ directory ]
467
468=head1 DESCRIPTION
469
470This script demonstrates the various widgets provided by Tk, along with
471many of the features of the Tk toolkit. This file only contains code to
472generate the main window for the application, which invokes individual
473demonstrations. The code for the actual demonstrations is contained in
474separate ".pl" files in the "widget_lib" directory, which are autoloaded
475by this script as needed.
476
477widget looks in the directory specified on the command line to load user
478contributed demonstrations. If no directory name is specified when widget is
479invoked and the environment variable WIDTRIB is defined then demonstrations
480are loaded from the WIDTRIB directory. If WIDTRIB is undefined then widget
481defaults to the released user contributed directory, "widtrib".
482
483=head2 History
484
485 #
486 # Stephen O. Lidie, LUCC, 96/03/11. lusol@Lehigh.EDU
487 # Stephen O. Lidie, LUCC, 97/01/01. lusol@Lehigh.EDU
488 # Stephen O. Lidie, LUCC, 97/02/11. lusol@Lehigh.EDU
489 # Stephen O. Lidie, LUCC, 97/06/07. lusol@Lehigh.EDU
490 # Update for Tk402.00x. Total revamp: WidgetDemo, Scrolled, released
491 # composites, -menuitems, qw//, etcetera. Perl 5.004 required.
492 # Stephen O. Lidie, LUCC, 98/03/10. lusol@Lehigh.EDU
493 # Update for Tk8.
494 # Stephen O. Lidie, LUCC, 98/06/26. Stephen.O.Lidie@Lehigh.EDU
495 # Add Common Dialogs for Tk800.007.
496 # Stephen.O.Lidie@Lehigh.EDU, 1999/11/29, Lehigh University.
497 # Demo some "dash patch" changes.
498 # Stephen.O.Lidie@Lehigh.EDU, 2000/01/11, Lehigh University.
499 # Update menubar to Tk 8, fix color palette Menubutton demo.
500 # Stephen.O.Lidie@Lehigh.EDU, 2000/07/06, Lehigh University.
501 # Remove inswt() from widget and styles.pl to show the proper Perl/Tk
502 # idiom for inserting Text tags. Various and sundry cleanups.
503
504=head1 AUTHOR
505
506Steve Lidie <Stephen.O.Lidie@Lehigh.EDU>
507
508=cut