Debian Codegraph @

demos/widget

raw
1 #!/usr/bin/perl
2
3 use 5.008;
4 use Config;
5 use Tk 804.000;
6 use lib Tk->findINC( 'demos/widget_lib' );
7 use Tk::widgets qw/ DialogBox ErrorDialog LabEntry ROText /;
8 use Tk::Config ();
9 use WidgetDemo;
10 use subs qw/
11 build_about_dialog demos invoke lsearch
12 see_code see_vars show_stat view_widget
13 /;
14 use vars qw/ $MW $FONT $BOLD_FONT $LARGE_FONT $MONOSPACE_FONT $WIDTRIB /;
15 use vars qw/ $CODE $CODE_RERUN $CODE_TEXT $VARS $VIEW $VIEW_TEXT /;
16 use vars qw/ $ALIGN $BRAKES $LIGHTS $OIL $SOBER $TRANS $WIPERS /;
17 use vars qw/ $COLOR $FONT_STYLE $POINT_SIZE $DEMO_FILE %DEMO_DESCRIPTION $XFT /;
18 use strict;
19
20 $XFT = $Tk::Config::xlib =~ /-lXft\b/;
21
22 $MW = Tk::MainWindow->new;
23 $MW->configure(-menu => my $menubar = $MW->Menu);
24
25 {
26 package WidgetWrap;
27 @WidgetWrap::ISA = qw/Tk::MainWindow/;
28
29 # This magic conspires with widget's AUTOLOAD subroutine to make user
30 # contributed demonstrations that don't use WidgetDemo embed properly.
31 # The trick works because widget creates a superclass of Tk::MainWindow
32 # which invokes WidgetDemo() implicitly. You loose if you bypass the
33 # inheritance mechanism and call Tk::MainWindow directly.
34
35 sub new {
36 my ($name) = $::DEMO_FILE =~ m#([^/]+).pl$#;
37 $::MW->WidgetDemo(-name => $name, -text => $::DEMO_DESCRIPTION{$name});
38 }
39 }
40
41 @MainWindow::ISA = 'WidgetWrap';
42
43 $MW->title('Perl/Tk Widget Demonstration');
44 $MW->iconname('widget');
45
46 my $DEFAULT_FONT_FAMILY = $XFT ? "sans-serif" : "Helvetica";
47 {
48 $FONT = "$DEFAULT_FONT_FAMILY 12";
49 $BOLD_FONT = "$DEFAULT_FONT_FAMILY 12 bold";
50 $LARGE_FONT = "$DEFAULT_FONT_FAMILY 18";
51 $MONOSPACE_FONT = "Courier 12";
52 my $l = $MW->Label;
53 my $default_font = $l->cget(-font);
54 if ($default_font) {
55 my %default_font = $MW->fontActual($default_font);
56 $FONT = $MW->fontCreate(%default_font, -family => $DEFAULT_FONT_FAMILY);
57 $BOLD_FONT = $MW->fontCreate(%default_font, -family => $DEFAULT_FONT_FAMILY, -weight => "bold");
58 $LARGE_FONT = $MW->fontCreate(%default_font, -family => $DEFAULT_FONT_FAMILY, -weight => "bold", -size => -18);
59 $MONOSPACE_FONT = $MW->fontCreate(%default_font, -family => $XFT ? "monospace" : "Courier");
60 $MW->optionAdd("*font", $FONT);
61 }
62 $l->destroy;
63 }
64
65 my $widget_lib = Tk->findINC('demos/widget_lib');
66 my $wd = "$widget_lib/WidgetDemo.pm";
67 $WIDTRIB = Tk->findINC('demos/widtrib');
68 unless (Tk::tainting) {
69 $WIDTRIB = $ENV{WIDTRIB} if defined $ENV{WIDTRIB};
70 $WIDTRIB = $ARGV[0] if defined $ARGV[0];
71 }
72
73 # The code below creates the main window, consisting of a menu bar
74 # and a text widget that explains how to use the program, plus lists
75 # all of the demos as hypertext items.
76
77 my $file = $menubar->cascade(qw/-label File -underline 0 -menuitems/ =>
78 [
79 [cascade => '~View', -menuitems =>
80 [
81 [command => '~widget', -command => [\&view_widget, __FILE__]],
82 [command => '~WidgetDemo', -command => [\&view_widget, $wd]],
83 ], # end cascade menuitems
84 ], # end view cascade
85 '',
86 [command => '~Quit', -command => [\&exit], -accelerator => 'Ctrl-Q'],
87 ]);
88
89 my $help = $menubar->cascade(qw/-label Help -underline 0 -menuitems/ =>
90 [
91 [command => '~About'],
92 ]);
93
94 my $T = $MW->Scrolled('ROText',
95 -scrollbars => 'e',
96 -wrap => 'word',
97 -width => 70,
98 -height => 30,
99 -font => $FONT,
100 -setgrid => 1,
101 -highlightthickness => 0,
102 -padx => 4,
103 -pady => 2,
104 -takefocus => 0,
105 )->grid(qw/-sticky nsew/);
106 $MW->gridRowconfigure( 0, -weight => 1); # allow expansion in both ...
107 $MW->gridColumnconfigure(0, -weight => 1); # ... X and Y dimensions
108
109 $MW->bind('<Control-q>' => \&exit);
110
111 my $STATUS_VAR;
112 my $status = $MW->Label(-textvariable => \$STATUS_VAR, qw/-anchor w/);
113 $status->grid(qw/-sticky ew/);
114
115 # Create a bunch of tags to use in the text widget, such as those for
116 # section titles and demo descriptions. Also define the bindings for
117 # tags.
118
119 $T->tagConfigure(qw/title -font/, $LARGE_FONT);
120 $T->tagConfigure(qw/demo -lmargin1 1c -lmargin2 1c -foreground blue/);
121
122 if ($MW->depth == 1) {
123 $T->tagConfigure(qw/hot -background black -foreground white/);
124 $T->tagConfigure(qw/visited -lmargin1 1c -lmargin2 1c -underline 1/);
125 } else {
126 $T->tagConfigure(qw/hot -relief raised -borderwidth 1 -foreground red/);
127 $T->tagConfigure(qw/visited -lmargin1 1c -lmargin2 1c -foreground/ =>
128 '#303080');
129 }
130 $T->tagConfigure('bold', -font => $BOLD_FONT);
131
132 $T->tagBind(qw/demo <ButtonRelease-1>/ => \&invoke);
133 my $last_line = '';
134 $T->tagBind(qw/demo <Enter>/ => [sub {
135 my($text, $sv) = @_;
136 my $e = $text->XEvent;
137 my($x, $y) = ($e->x, $e->y);
138 $last_line = $text->index("\@$x,$y linestart");
139 $text->tagAdd('hot', $last_line, "$last_line lineend");
140 $text->configure(qw/-cursor hand2/);
141 show_stat $sv, $text, $text->index('current');
142 }, \$STATUS_VAR]
143 );
144 $T->tagBind(qw/demo <Leave>/ => [sub {
145 my($text, $sv) = @_;
146 $text->tagRemove(qw/hot 1.0 end/);
147 $text->configure(qw/-cursor xterm/);
148 $$sv = '';
149 }, \$STATUS_VAR]
150 );
151 $T->tagBind(qw/demo <Motion>/ => [sub {
152 my($text, $sv) = @_;
153 my $e = $text->XEvent;
154 my($x, $y) = ($e->x, $e->y);
155 my $new_line = $text->index("\@$x,$y linestart");
156 if ($new_line ne $last_line) {
157 $text->tagRemove(qw/hot 1.0 end/);
158 $last_line = $new_line;
159 $text->tagAdd('hot', $last_line, "$last_line lineend");
160 }
161 show_stat $sv, $text, $text->index('current');
162 }, \$STATUS_VAR]
163 );
164
165 # Create the text for the text widget.
166
167 $T->insert('end', "Perl/Tk Widget Demonstrations\n", 'title');
168 $T->insert('end',
169 "\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', 'bold', " button to see the Perl/Tk code that created the demonstration. If you wish, you can edit the code and click the ", '', "Rerun Demo", 'bold', " button in the code window to reinvoke the demonstration with the modified code.\n"
170 );
171
172 demos 'Getting Started', (
173 'hello' => 'An introduction to Perl/Tk',
174 );
175
176 demos 'Labels, buttons, checkbuttons, and radiobuttons', (
177 'labels' => 'Labels (text and images)',
178 'unicodeout' => 'Labels and Unicode text',
179 'button' => 'Buttons',
180 'check' => 'Checkbuttons (select any of a group)',
181 'radio' => 'Radiobuttons (select one of a group)',
182 'puzzle' => 'A 15-puzzle game made out of buttons',
183 'icon' => 'Iconic buttons that use bitmaps',
184 'image1' => 'Two labels displaying images',
185 'image2' => 'A simple user interface for viewing images',
186 'labelframe' => 'Labelled frames',
187 );
188
189 demos 'Listboxes', (
190 'states' => 'The 50 states',
191 'colors' => 'Change widget\'s color scheme',
192 'sayings' => 'A collection of famous and infamous sayings',
193 );
194
195 demos 'Entries and Spin-boxes', (
196 'entry1' => 'Entries without scrollbars',
197 'entry2' => 'Entries with scrollbars',
198 'entry3' => 'Validated entries and password fields',
199 'spin' => 'Spin-boxes',
200 'form' => 'Simple Rolodex-like form',
201 );
202
203 demos 'Text', (
204 'texts' => 'Basic editable text',
205 'style' => 'Text display styles',
206 'bind' => 'Hypertext (tag bindings)',
207 'twind' => 'A text widget with embedded windows',
208 'search' => 'A search tool built with a text widget',
209 );
210
211 demos 'Canvases', (
212 'items' => 'The canvas item types',
213 'plot' => 'A simple 2-D plot',
214 'ctext' => 'Text items in canvases',
215 'arrows' => 'An editor for arrowheads on canvas lines',
216 'ruler' => 'A ruler with adjustable tab stops',
217 'floor' => 'A building floor plan',
218 'cscroll' => 'A simple scrollable canvas',
219 'transtile' => 'Tiles and transparent images',
220 );
221
222 demos 'Scales', (
223 'hscale' => 'Horizontal scale',
224 'vscale' => 'Vertical scale',
225 );
226
227 demos 'Paned Windows', (
228 'paned1' => 'Horizontal paned window',
229 'paned2' => 'Vertical paned window',
230 );
231
232 demos 'Photos and Images', (
233 'photo1' => 'Transparent pixels',
234 'photo2' => 'Alpha channel compositing',
235 );
236
237 demos 'Menus', (
238 'menus' => 'Menus and cascades (sub-menus)',
239 'menus2' => 'As above, but using Perl/Tk -menuitems',
240 'menbut' => 'Menubuttons',
241 );
242
243 demos 'Common Dialogs', (
244 'msgBox' => 'Message boxes',
245 'filebox' => 'File selection dialog',
246 'choosedir' => 'Directory selection dialog',
247 'clrpick' => 'Color picker',
248 );
249
250 demos 'Tix Widgets', (
251 'balloon' => 'Popup help window when mouse lingers over widget',
252 'browseentry' => 'Entry with Listbox to select list values',
253 'browseentry2' => 'Another BrowseEntry example',
254 'dirtree' => 'Display a directory tree',
255 'form_mgr' => 'The form geometry manager',
256 'HList' => 'A hierarchical listbox widget',
257 'HList2' => 'Multicolumn listbox with individual cell styles',
258 'labframe' => 'A Frame with embedded label',
259 );
260
261 demos 'Simulations', (
262 'bounce' => 'Balls bouncing in a cavity',
263 );
264
265 demos 'Sample Perl Mega-Widgets', (
266 'mega' => 'Introduction to writing pure Perl mega-widgets',
267 'slide' => 'Composite Tk::SlideSwitch - binary on/off switch',
268 'trace2' => 'Derived Tk::TraceText - Text contents defined by a traced variable',
269 );
270
271 demos 'Miscellaneous', (
272 'bitmaps' => 'The built-in bitmaps',
273 'dialog1' => 'A dialog box with a local grab',
274 'dialog2' => 'A dialog box with a global grab',
275 'trace1' => 'Trace a variable\'s value',
276 'progress' => 'Various ProgressBar widgets',
277 'keysyms' => 'Show keyboard symbols',
278 'virtevents1' => 'Synthesizing events',
279 );
280
281 $T->insert('end', "\n", '', "User Contributed Demonstrations\n", 'title');
282 opendir(C, $WIDTRIB) or warn "Cannot open $WIDTRIB: $!";
283 my(@dirent) = grep /^.+\.pl$/, sort(readdir C);
284 closedir C;
285 unshift @dirent, 'TEMPLATE.pl'; # I want it first
286 my $i = 0;
287 while ($_ = shift @dirent) {
288 next if /TEMPLATE\.pl/ and $i != 0;
289 unless (open(C, "$WIDTRIB/$_")) {
290 warn "Cannot open $_: $!" unless /TEMPLATE\.pl/;
291 next;
292 }
293 my($name) = /^(.*)\.pl$/;
294 $_ = <C>;
295 my($title) = /^#\s*(.*)$/;
296 $title =~ s{\r}{}g; # strip DOS newlines
297 $DEMO_DESCRIPTION{$name} = $title;
298 close C;
299 $T->insert('end', ++$i . ". $title\n", ['demo', "demo-$name"]);
300 }
301
302 build_about_dialog $help;
303 $T->focus;
304
305 MainLoop;
306
307 sub AUTOLOAD {
308
309 # This routine handles the loading of most demo methods.
310
311 my($demo) = @_;
312
313 $T->Busy;
314 {
315 $DEMO_FILE = "$WIDTRIB/${demo}.pl" if -f "$WIDTRIB/${demo}.pl";
316 $DEMO_FILE = "$widget_lib/${demo}.pl" if -f "$widget_lib/${demo}.pl";
317 do $DEMO_FILE;
318 warn $@ if $@;
319 }
320 $T->Unbusy;
321 goto &$::AUTOLOAD if defined &$::AUTOLOAD;
322
323 } # end AUTOLOAD
324
325 sub build_about_dialog {
326
327 my $help = shift;
328
329 my $dialog = $MW->DialogBox(
330 -title => "Perl/Tk, based on Tcl/Tk ${Tk::version}.x",
331 -default_button => 'OK',
332 -buttons => ['OK'],
333 );
334 my $df = $dialog->add( 'Labelframe', -text => ' 2006 - Oct - 31 ' );
335 $df->pack( qw/ -fill both -expand 1 / );
336 my( $change ) = $Tk::CHANGE =~ /Change:\s+(.*)/;
337 my( $tk_date ) = scalar localtime( ( stat $Tk::library )[9] );
338 my( $tk ) = "$Tk::VERSION ($change), $tk_date";
339
340 foreach my $item (
341 [ 'Perl', "$], $Config{cf_time}" ],
342 [ 'Tk', $tk ],
343 [ 'Platform', $Tk::platform ],
344 [ 'Library', $Tk::library ],
345 ) {
346 my $l = $item->[0] . ':';
347 my $le = $df->LabEntry(
348 -font => $FONT,
349 -label => ' ' x (13 - length $l) . $l,
350 -labelPack => [qw/-side left -anchor w/],
351 -labelFont => 'Courier 12 bold',
352 -relief => 'flat',
353 -takefocus => 0,
354 -textvariable => $item->[1],
355 -width => 45,
356 );
357 $le->pack(qw/ -fill x -expand 1/);
358 $le->Subwidget( 'entry' )->bindtags( [ ] );
359 }
360
361 $help->cget(-menu)->entryconfigure('About',
362 -command => [$dialog => 'Show'],
363 );
364
365 } # end build_about_dialog
366
367 sub demos {
368
369 # Generate one demo section.
370
371 my ($title, @demos) = @_;
372
373 $T->insert('end', "\n", '', "$title\n", 'title');
374
375 for (my $n = 0; $n <= $#demos; $n += 2) {
376 $T->insert('end', $n / 2 + 1 . '. ' . $demos[$n + 1] . ".\n",
377 ['demo', 'demo-' . $demos[$n]]);
378 }
379
380 } # end demos
381
382 sub invoke {
383
384 # This procedure is called when the user clicks on a demo description.
385
386 my($text) = @_;
387
388 my $index = $text->index('current');
389 my @tags = $T->tagNames($index);
390 my $i = lsearch('demo\-.*', @tags);
391 return if $i < 0;
392 my($demo) = $tags[$i] =~ /demo-(.*)/;
393 $T->tagAdd('visited', "$index linestart", "$index lineend");
394 {
395 no strict 'refs';
396 $SIG{__WARN__} = sub {
397 print @_ unless $_[0] =~ /Subroutine \S+ redefined at/;
398 };
399 &$demo($demo);
400 }
401
402 } # end invoke
403
404 sub lsearch {
405
406 # Search the list using the supplied regular expression and return its
407 # ordinal, or -1 if not found.
408
409 my($regexp, @list) = @_;
410 my($i);
411
412 for ($i=0; $i<=$#list; $i++) {
413 return $i if $list[$i] =~ /$regexp/;
414 }
415 return -1;
416
417 } # end lsearch
418
419 sub see_code {
420
421 # This procedure creates a toplevel window that displays the code for
422 # a demonstration and allows it to be edited and reinvoked.
423
424 my($demo) = @_;
425
426 my $file;
427 $file = ( $demo =~ /\.pl$/ ) ? $demo : "${demo}.pl";
428 if (not Exists $CODE) {
429 $CODE = $MW->Toplevel;
430 my $code_buttons = $CODE->Frame;
431 $code_buttons->pack(qw/-side bottom -fill x/);
432 my $code_buttons_dismiss = $code_buttons->Button(
433 -text => 'Dismiss',
434 -command => [$CODE => 'withdraw'],
435 );
436 $CODE_RERUN = $code_buttons->Button(-text => 'Rerun Demo');
437 $CODE_TEXT = $CODE->Scrolled('Text', qw/ -height 40 -scrollbars oe -font /, $MONOSPACE_FONT);
438 $code_buttons_dismiss->pack(qw/-side left -expand 1/);
439 $CODE_RERUN->pack(qw/-side left -expand 1/);
440 $CODE_TEXT->pack(qw/-side left -expand 1 -fill both/);
441 } else {
442 $CODE->deiconify;
443 $CODE->raise;
444 }
445 $CODE_RERUN->configure(-command => sub {
446 eval $CODE_TEXT->get(qw/1.0 end/);
447 {
448 no strict 'refs';
449 &$demo($demo);
450 }
451 });
452 $CODE->iconname($file);
453 $file = "$WIDTRIB/$file" if -f "$WIDTRIB/$file";
454 $file = "$widget_lib/$file" if -f "$widget_lib/$file";
455 $CODE->title("Demo code: $file");
456 $CODE_TEXT->delete(qw/1.0 end/);
457 open(CODE, "<$file") or warn "Cannot open demo file $file: $!";
458 {
459 local $/ = undef;
460 $CODE_TEXT->insert('1.0', <CODE>);
461 }
462 close CODE;
463 $CODE_TEXT->markSet(qw/insert 1.0/);
464 $CODE_TEXT->focus;
465
466 } # end see_code
467
468 sub see_vars {
469
470 # Create a top-level window that displays a bunch of global variable values
471 # and keeps the display up-to-date even when the variables change value.
472 # $args is a pointer to a list of list of 2:
473 #
474 # ["variable description", \$VAR]
475 #
476 # The old trick of passing a string to serve as the description and a soft
477 # reference to the variable no longer works with lexicals and use strict.
478
479 my($parent, $args) = @_;
480
481 $VARS->destroy if Exists($VARS);
482 $VARS = $parent->Toplevel;
483 $VARS->geometry('+300+300');
484 $VARS->title('Variable Values');
485 $VARS->iconname('Variables');
486
487 my $title = $VARS->Label(
488 -text => 'Variable Values:',
489 -width => 20,
490 -anchor => 'center',
491 -font => $LARGE_FONT,
492 );
493 $title->pack(qw/-side top -fill x/);
494 my($label, $var);
495 foreach my $i (@$args) {
496 ($label, $var) = @$i;
497 my $wf = $VARS->Frame->pack(qw/-anchor w/);
498 $wf->Label(-text => "$label: ")->pack(qw/-side left/);
499 $wf->Label(-textvariable => $var)->pack(qw/-side left/);
500 }
501 $VARS->Button(-text => 'OK', -command => [$VARS => 'destroy'])->
502 pack(qw/-side bottom -pady 2/);
503
504 } # end see_vars
505
506 sub show_stat {
507
508 # Display name of current demonstration. $sv is a reference to the
509 # status Label -textvariable, $text is the Text widget reference and
510 # $index is the demonstration index in the Text widget.
511
512 my($sv, $text, $index) = @_;
513
514 my @tags = $text->tagNames($index);
515 my $i = lsearch('demo\-.*', @tags);
516 return if $i < 0;
517 my($demo) = $tags[$i] =~ /demo-(.*)/;
518 $$sv = "Click Button-1 to run the \"$demo\" demonstration.";
519
520 } # end show_stat
521
522 sub view_widget {
523
524 # Expose a file's innards to the world too, but only for viewing.
525
526 my($widget) = @_;
527
528 if (not Exists $VIEW) {
529 $VIEW = $MW->Toplevel;
530 $VIEW->iconname('widget');
531 my $view_buttons = $VIEW->Frame;
532 $view_buttons->pack(qw/-side bottom -fill x/);
533 my $view_buttons_dismiss = $view_buttons->Button(
534 -text => 'Dismiss',
535 -command => [$VIEW => 'withdraw'],
536 );
537 $view_buttons_dismiss->pack(qw/-side left -expand 1/);
538 $VIEW_TEXT = $VIEW->Scrolled('Text', qw/ -height 40 -scrollbars oe /);
539 $VIEW_TEXT->pack(qw/-side left -expand 1 -fill both/);
540 } else {
541 $VIEW->deiconify;
542 $VIEW->raise;
543 }
544 $VIEW->title("Demo code: $widget");
545 $VIEW_TEXT->configure(qw/-state normal/);
546 $VIEW_TEXT->delete(qw/1.0 end/);
547 open(VIEW, "<$widget") or warn "Cannot open demo file $widget: $!";
548 {
549 local $/ = undef;
550 $VIEW_TEXT->insert('1.0', <VIEW>);
551 }
552 close VIEW;
553 $VIEW_TEXT->markSet(qw/insert 1.0/);
554 $VIEW_TEXT->configure(qw/-state disabled/);
555
556 } # end view_widget
557
558 =head1 NAME
559
560 widget - Demonstration of Perl/Tk widgets
561
562 =head1 SYNOPSYS
563
564 widget [ directory ]
565
566 =head1 DESCRIPTION
567
568 This script demonstrates the various widgets provided by Tk, along with
569 many of the features of the Tk toolkit. This file only contains code to
570 generate the main window for the application, which invokes individual
571 demonstrations. The code for the actual demonstrations is contained in
572 separate ".pl" files in the "widget_lib" directory, which are autoloaded
573 by this script as needed.
574
575 widget looks in the directory specified on the command line to load user
576 contributed demonstrations. If no directory name is specified when widget is
577 invoked and the environment variable WIDTRIB is defined then demonstrations
578 are loaded from the WIDTRIB directory. If WIDTRIB is undefined then widget
579 defaults to the released user contributed directory, "widtrib".
580
581 =head2 History
582
583 #
584 # Stephen O. Lidie, LUCC, 96/03/11. [email protected]
585 # Stephen O. Lidie, LUCC, 97/01/01. [email protected]
586 # Stephen O. Lidie, LUCC, 97/02/11. [email protected]
587 # Stephen O. Lidie, LUCC, 97/06/07. [email protected]
588 # Update for Tk402.00x. Total revamp: WidgetDemo, Scrolled, released
589 # composites, -menuitems, qw//, etcetera. Perl 5.004 required.
590 # Stephen O. Lidie, LUCC, 98/03/10. [email protected]
591 # Update for Tk8.
592 # Stephen O. Lidie, LUCC, 98/06/26. [email protected]
593 # Add Common Dialogs for Tk800.007.
594 # [email protected], 1999/11/29, Lehigh University.
595 # Demo some "dash patch" changes.
596 # [email protected], 2000/01/11, Lehigh University.
597 # Update menubar to Tk 8, fix color palette Menubutton demo.
598 # [email protected], 2000/07/06, Lehigh University.
599 # Remove inswt() from widget and styles.pl to show the proper Perl/Tk
600 # idiom for inserting Text tags. Various and sundry cleanups.
601 # [email protected], 2003/07/29, Lehigh University Computing Center.
602 # Update for Tk 8.4.4.
603
604 =head1 AUTHOR
605
606 Steve Lidie <[email protected]>
607
608 =cut