home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / Share / Editores / Perl5 / perl / bin / widget.bat < prev    next >
Encoding:
DOS Batch File  |  1997-08-10  |  16.4 KB  |  465 lines

  1. @rem = '--*-Perl-*--
  2. @echo off
  3. perl -x -S %0 %1 %2 %3 %4 %5 %6 %7 %8 %9
  4. goto endofperl
  5. @rem ';
  6. #!/usr/local/bin/perl -w
  7. #line 8
  8.  
  9. # This script demonstrates the various widgets provided by Tk, along with
  10. # many of the features of the Tk toolkit.  This file only contains code to
  11. # generate the main window for the application, which invokes individual
  12. # demonstrations.  The code for the actual demonstrations is contained in
  13. # separate ".pl" files in the "widget_lib" directory, which are autoloaded 
  14. # by this script as needed.
  15. #
  16. # widget looks in the directory specified on the command line to load user
  17. # contributed demonstrations.  If no directory name is specified when widget is
  18. # invoked and the environment variable WIDTRIB is defined then demonstrations
  19. # are loaded from the WIDTRIB directory. If WIDTRIB is undefined then widget
  20. # defaults to the released user contributed directory.
  21. #
  22. # Stephen O. Lidie, LUCC, 96/03/11.  lusol@Lehigh.EDU
  23. # Stephen O. Lidie, LUCC, 97/01/01.  lusol@Lehigh.EDU
  24. # Stephen O. Lidie, LUCC, 97/02/11.  lusol@Lehigh.EDU
  25. # Stephen O. Lidie, LUCC, 97/06/07.  lusol@Lehigh.EDU
  26. #     Update for Tk402.00x.  Total revamp:  WidgetDemo, Scrolled, released
  27. #     composites, -menuitems, qw//, etcetera.  Perl 5.004 required.
  28.  
  29. require 5.004;
  30.  
  31. use English;
  32. use Tk;
  33. use lib Tk->findINC('demos/widget_lib');
  34. use Tk::widgets qw/Dialog ErrorDialog/;
  35. use WidgetDemo;
  36. use subs qw/inswt invoke lsearch see_code see_vars show_stat view_widget_code/;
  37. use vars qw/$MW $FONT $WIDTRIB/;
  38. use vars qw/$CODE $CODE_TEXT $VARS $VIEW $VIEW_TEXT/;
  39. use vars qw/$BRAKES $LIGHTS $OIL $SOBER $TRANS $WIPERS/;
  40. use vars qw/$COLOR $FONT_STYLE $POINT_SIZE/;
  41. use strict;
  42.  
  43. $MW = MainWindow->new;
  44. $MW->title('Widget Demonstration');
  45. $FONT = '-*-Helvetica-Medium-R-Normal--*-140-*-*-*-*-*-*';
  46. my $widget_lib = Tk->findINC('demos/widget_lib');
  47. my $WIDTRIB = Tk->findINC('demos/widtrib');
  48. $WIDTRIB = $ENV{WIDTRIB} if defined $ENV{WIDTRIB}; 
  49. $WIDTRIB = $ARGV[0] if defined $ARGV[0]; 
  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.  
  55. my $menubar = $MW->Frame;
  56. $menubar->grid(qw/-sticky ew/);
  57. $menubar->gridColumnconfigure(qw/0 -weight 1/);
  58.  
  59. my $file = $menubar->Menubutton(qw/-text File -underline 0 -menuitems/ =>
  60.     [
  61.      [Button    => '~View', -command => [\&view_widget_code, __FILE__]],
  62.      [Separator => ''],
  63.      [Button    => '~Quit', -command => [\&exit]],
  64.     ])->grid(qw/-sticky w/); 
  65. my $about = $menubar->Menubutton(qw/-text About -underline 0 -menuitems/ =>
  66.     [
  67.      [Button    => '~Widget'],
  68.     ])->grid(qw/-row 0 -column 1 -sticky w/); 
  69.  
  70. my $T = $MW->Scrolled('Text',
  71.     -scrollbars => 'e',          
  72.     -wrap       => 'word',
  73.     -width      => 60,
  74.     -height     => 30, 
  75.     -font       => $FONT,
  76.     -setgrid    => 1,
  77. )->grid(qw/-sticky nsew/);
  78. $T->bindtags(qw/widget_demo/);    # remove all bindings but dummy "widget_demo"
  79.  
  80. my $STATUS_VAR;
  81. my $status = $MW->Label(-textvariable => \$STATUS_VAR, qw/-anchor w/);
  82. $status->grid(qw/-sticky ew/);
  83.  
  84. # Create a bunch of tags to use in the text widget, such as those for
  85. # section titles and demo descriptions.  Also define the bindings for
  86. # tags.
  87.  
  88. $T->tag(qw/configure title -font -*-Helvetica-Bold-R-Normal--*-180-*-*-*-*-*-*/);
  89. $T->tag(qw/configure demo -lmargin1 1c -lmargin2 1c -foreground blue/);
  90.  
  91. if ($MW->depth  == 1) {
  92.     $T->tag(qw/configure hot -background black -foreground white/);
  93.     $T->tag(qw/configure visited -lmargin1 1c -lmargin2 1c -underline 1/);
  94. } else {
  95.     $T->tag(qw/configure hot -relief raised -borderwidth 1 -foreground red/);
  96.     $T->tag(qw/configure visited -lmargin1 1c -lmargin2 1c -foreground/ =>
  97.         '#303080');
  98. }
  99.  
  100. $T->tag(qw/bind demo <ButtonRelease-1>/ => sub {invoke $T->index('current')});
  101. my $last_line = '';
  102. $T->tag(qw/bind demo <Enter>/ => [sub {
  103.     my($text, $sv) = @ARG;
  104.     my $e = $text->XEvent;
  105.     my($x, $y) = ($e->x, $e->y);
  106.     $last_line = $text->index("\@$x,$y linestart");
  107.     $text->tag(qw/add hot/, $last_line, "$last_line lineend");
  108.     $text->configure(qw/-cursor hand2/);
  109.     show_stat $sv, $text, $text->index('current');
  110.     }, \$STATUS_VAR]
  111. );
  112. $T->tag(qw/bind demo <Leave>/ => [sub {
  113.     my($text, $sv) = @ARG;
  114.     $text->tag(qw/remove hot 1.0 end/);
  115.     $text->configure(qw/-cursor xterm/);
  116.     $$sv = '';
  117.     }, \$STATUS_VAR]
  118. );
  119. $T->tag(qw/bind demo <Motion>/ => [sub {
  120.     my($text, $sv) = @ARG;
  121.     my $e = $text->XEvent;
  122.     my($x, $y) = ($e->x, $e->y);
  123.     my $new_line = $text->index("\@$x,$y linestart");
  124.     if ($new_line ne $last_line) {
  125.         $text->tag(qw/remove hot 1.0 end/);
  126.         $last_line = $new_line;
  127.         $text->tag(qw/add hot/, $last_line, "$last_line lineend");
  128.     }
  129.     show_stat $sv, $text, $text->index('current');
  130.     }, \$STATUS_VAR]
  131. );
  132.  
  133. # Create the text for the text widget.
  134.  
  135. $T->insert('end', "Perl/Tk Widget Demonstrations\n", 'title');
  136. $T->insert('end', 
  137. "\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");
  138.  
  139. # Define globals for demo toplevels, informative text with tags for
  140. # highlighting and specifying the demo's file name.
  141.  
  142. $T->insert('end', "\n", '', 
  143.        "Labels, buttons, checkbuttons, and radiobuttons\n", 'title');
  144. $T->insert('end', "1. Labels (text and images).\n", [qw/demo demo-labels/]);
  145. $T->insert('end', "2. Buttons.\n", [qw/demo demo-button/]);
  146. $T->insert('end', "3. Checkbuttons (select any of a group).\n",
  147.     [qw/demo demo-check/]);
  148. $T->insert('end', "4. Radiobuttons (select one of a group).\n",
  149.     [qw/demo demo-radio/]);
  150. $T->insert('end', "5. A 15-puzzle game made out of buttons.\n",
  151.     [qw/demo demo-puzzle/]);
  152. $T->insert('end', "6. Iconic buttons that use bitmaps.\n",
  153.     [qw/demo demo-icon/]);
  154. $T->insert('end', "7. Two labels displaying images.\n",
  155.     [qw/demo demo-image1/]);
  156. $T->insert('end', "8. A simple user interface for viewing images.\n",
  157.     [qw/demo demo-image2/]);
  158.  
  159. $T->insert('end', "\n", '', "Listboxes\n", 'title');
  160. $T->insert('end', "1. 50 states.\n", [qw/demo demo-states/]);
  161. $T->insert('end', "2. Colors: change the color scheme for the application.\n",
  162.     [qw/demo demo-colors/]);
  163. $T->insert('end', "3. A collection of famous sayings.\n",
  164.     [qw/demo demo-sayings/]);
  165.  
  166. $T->insert('end', "\n", '', "Entries\n", 'title');
  167. $T->insert('end', "1. Without scrollbars.\n", [qw/demo demo-entry1/]);
  168. $T->insert('end', "2. With scrollbars.\n", [qw/demo demo-entry2/]);
  169. $T->insert('end', "3. Simple Rolodex-like form.\n", [qw/demo demo-form/]);
  170.  
  171. $T->insert('end', "\n", '', "Text\n", 'title');
  172. $T->insert('end', "1. Basic editable text.\n", [qw/demo demo-texts/]);
  173. $T->insert('end', "2. Text display styles.\n", [qw/demo demo-style/]);
  174. $T->insert('end', "3. Hypertext (tag bindings).\n", [qw/demo demo-bind/]);
  175. $T->insert('end', "4. A text widget with embedded windows.\n",
  176.     [qw/demo demo-twind/]);
  177. $T->insert('end', "5. A search tool built with a text widget.\n",
  178.     [qw/demo demo-search/]);
  179.  
  180. $T->insert('end', "\n", '', "Canvases\n", 'title');
  181. $T->insert('end', "1. The canvas item types.\n", [qw/demo demo-items/]);
  182. $T->insert('end', "2. A simple 2-D plot.\n", [qw/demo demo-plot/]);
  183. $T->insert('end', "3. Text items in canvases.\n", [qw/demo demo-ctext/]);
  184. $T->insert('end', "4. An editor for arrowheads on canvas lines.\n",
  185.     [qw/demo demo-arrows/]);
  186. $T->insert('end', "5. A ruler with adjustable tab stops.\n",
  187.     [qw/demo demo-ruler/]);
  188. $T->insert('end', "6. A building floor plan.\n", [qw/demo demo-floor/]);
  189. $T->insert('end', "7. A simple scrollable canvas.\n", [qw/demo demo-cscroll/]);
  190.  
  191. $T->insert('end', "\n", '', "Scales\n", 'title');
  192. $T->insert('end', "1. Vertical scale.\n", [qw/demo demo-vscale/]);
  193. $T->insert('end', "2. Horizontal scale.\n", [qw/demo demo-hscale/]);
  194.  
  195. $T->insert('end', "\n", '', "Menus\n", 'title');
  196. $T->insert('end', "1. A window containing several menus and cascades.\n",
  197.     [qw/demo demo-menus/]);
  198. $T->insert('end', "2. Like above, but in a manner particular to Perl/Tk.\n",
  199.     [qw/demo demo-menus2/]);
  200.  
  201. $T->insert('end', "\n", '', "Simulations\n", 'title');
  202. $T->insert('end', "1. Balls bouncing in a cavity.\n", [qw/demo demo-bounce/]);
  203.  
  204. $T->insert('end', "\n", '', "Miscellaneous\n", 'title');
  205. $T->insert('end', "1. The built-in bitmaps.\n", [qw/demo demo-bitmaps/]);
  206. $T->insert('end', "2. A dialog box with a local grab.\n",
  207.     [qw/demo demo-dialog1/]);
  208. $T->insert('end', "3. A dialog box with a global grab.\n",
  209.     [qw/demo demo-dialog2/]);
  210.  
  211. $T->insert('end', "\n", '', "User Contributed Demonstrations\n", 'title');
  212. opendir(C, $WIDTRIB) or warn "Cannot open $WIDTRIB: $OS_ERROR!";
  213. my(@dirent) = grep /^.+\.pl$/, sort(readdir C);
  214. closedir C;
  215. my $i;
  216. while ($ARG = shift @dirent) {
  217.     open(C, "$WIDTRIB/$ARG") or warn "Cannot open $ARG: $OS_ERROR!";
  218.     my($name) = /^(.*)\.pl$/;
  219.     $ARG = <C>;
  220.     my($title) = /^#\s*(.*)$/;
  221.     close C;
  222.     $T->insert('end', ++$i . ". $title\n", ['demo', "demo-$name"]);
  223. }
  224. $T->configure(-state => 'disabled');
  225.  
  226. # Create all the dialogs required by this demonstration.
  227.  
  228. my $DIALOG_ABOUT = $MW->Dialog(
  229.     -title          => 'About widget',
  230.     -bitmap         => undef,
  231.     -default_button => 'OK',
  232.     -buttons        => ['OK'],
  233.     -text           => "       widget\nVersion $Tk::VERSION\n     97/06/07",
  234. );
  235. $about->entryconfigure('Widget', -command => [$DIALOG_ABOUT => 'Show']);
  236.  
  237. my $DIALOG_ICON = $MW->Dialog(
  238.     -title          => 'Bitmap Menu Entry',
  239.     -bitmap         => undef,
  240.     -default_button => 'OK',
  241.     -buttons        => ['OK'],
  242.     -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.',
  243. );
  244. $DIALOG_ICON->configure(-bitmap => undef); # keep -w from complaining
  245.  
  246. MainLoop;
  247.  
  248. sub AUTOLOAD {
  249.  
  250.     # This routine handles the loading of most demo methods.
  251.  
  252.     my($demo) = @ARG;
  253.  
  254.     $T->Busy;
  255.     {
  256.     local $INPUT_RECORD_SEPARATOR = undef;
  257.     my $file;
  258.     $file = "$WIDTRIB/${demo}.pl" if -f "$WIDTRIB/${demo}.pl";
  259.     $file = "$widget_lib/${demo}.pl" if -f "$widget_lib/${demo}.pl";
  260.     open(DEMO, $file) or warn "Cannot open demo file $file: $OS_ERROR!";
  261.     eval <DEMO>;
  262.     warn $EVAL_ERROR if $EVAL_ERROR;
  263.     close DEMO;
  264.     }
  265.     $T->Unbusy;
  266.     goto &$::AUTOLOAD;
  267.  
  268. } # end AUTOLOAD
  269.  
  270. sub inswt {
  271.  
  272.     # insert_with_tags
  273.     #
  274.     # The procedure below inserts text into a given text widget and applies
  275.     # one or more tags to that text.  The arguments are:
  276.     #
  277.     # w        Window in which to insert
  278.     # text    Text to insert (it's inserted at the "insert" mark)
  279.     # args    One or more tags to apply to text.  If this is empty then all
  280.     #           tags are removed from the text.
  281.  
  282.     my($w, $text, @args) = @ARG;
  283.  
  284.     my $start = $w->index('insert');
  285.     $w->insert('insert', $text);
  286.     foreach my $tag ($w->tag('names', $start)) {
  287.     $w->tag('remove', $tag, $start, 'insert');
  288.     }
  289.     foreach my $i (@args) {
  290.     $w->tag('add', $i, $start, 'insert');
  291.     }
  292.  
  293. } # end inswt
  294.  
  295. sub invoke {
  296.  
  297.     # This procedure is called when the user clicks on a demo description.
  298.  
  299.     my($index) = @ARG;
  300.  
  301.     my @tags = $T->tag('names', $index);
  302.     my $i = lsearch('demo\-.*', @tags);
  303.     return if $i < 0;
  304.     my($demo) = $tags[$i] =~ /demo-(.*)/;
  305.     $T->tag('add', 'visited', "$index linestart",
  306.                               "$index lineend -1 chars");
  307.     {no strict 'refs'; &$demo($demo);}
  308.  
  309. } # end invoke
  310.  
  311. sub lsearch {
  312.  
  313.     # Search the list using the supplied regular expression and return it's
  314.     # ordinal, or -1 if not found.
  315.  
  316.     my($regexp, @list) = @ARG;
  317.     my($i);
  318.  
  319.     for ($i=0; $i<=$#list; $i++) {
  320.         return $i if $list[$i] =~ /$regexp/;
  321.     }
  322.     return -1;
  323.  
  324. } # end lsearch
  325.  
  326. sub see_code {
  327.  
  328.     # This procedure creates a toplevel window that displays the code for
  329.     # a demonstration and allows it to be edited and reinvoked.
  330.  
  331.     my($demo) = @ARG;
  332.  
  333.     my $file = "${demo}.pl";
  334.     if (not Exists $CODE) {
  335.     $CODE = $MW->Toplevel;
  336.     my $code_buttons = $CODE->Frame;
  337.     $code_buttons->pack(qw/-side bottom -fill x/);
  338.     my $code_buttons_dismiss = $code_buttons->Button(
  339.             -text    => 'Dismiss',
  340.             -command => [$CODE => 'withdraw'],
  341.     );
  342.     my $code_buttons_rerun = $code_buttons->Button(
  343.             -text => 'Rerun Demo',
  344.             -command => [sub {
  345.             eval $CODE_TEXT->get(qw/1.0 end/);
  346.             {no strict 'refs'; &$demo($demo);}
  347.         }],
  348.         );
  349.     $code_buttons_dismiss->pack(qw/-side left -expand 1/);
  350.     $code_buttons_rerun->pack(qw/-side left -expand 1/);
  351.     $CODE_TEXT = $CODE->Scrolled('Text', 
  352.                      qw/-scrollbars e -height 40 -setgrid 1/);
  353.     $CODE_TEXT->pack(qw/-side left -expand 1 -fill both/);
  354.     } else {
  355.     $CODE->deiconify;
  356.     $CODE->raise;
  357.     }
  358.     $CODE->iconname($file);
  359.     $file = "$WIDTRIB/${demo}.pl" if -f "$WIDTRIB/${demo}.pl";
  360.     $file = "$widget_lib/${demo}.pl" if -f "$widget_lib/${demo}.pl";
  361.     $CODE->title("Demo code: $file");
  362.     $CODE_TEXT->delete(qw/1.0 end/);
  363.     open(CODE, "<$file") or warn "Cannot open demo file $file: $OS_ERROR!";
  364.     {
  365.     local $INPUT_RECORD_SEPARATOR = undef;
  366.     $CODE_TEXT->insert('1.0', <CODE>);
  367.     }
  368.     close CODE;
  369.     $CODE_TEXT->mark(qw/set insert 1.0/);
  370.  
  371. } # end see_code
  372.  
  373. sub see_vars {
  374.  
  375.     # Create a top-level window that displays a bunch of global variable values
  376.     # and keeps the display up-to-date even when the variables change value.
  377.     # $args is a pointer to a list of list of 2: 
  378.     #
  379.     #   ["variable description", \$VAR]
  380.     #
  381.     # The old trick of passing a string to serve as the description and a soft
  382.     # reference to the variable no longer works with lexicals and use strict.
  383.  
  384.     my($parent, $args) = @ARG;
  385.  
  386.     $VARS->destroy if Exists($VARS);
  387.     $VARS = $parent->Toplevel;
  388.     $VARS->geometry('+300+300');
  389.     $VARS->title('Variable Values');
  390.     $VARS->iconname('Variables');
  391.  
  392.     my $title = $VARS->Label(
  393.         -text   => 'Variable Values:',
  394.         -width  => 20,
  395.         -anchor => 'center',
  396.         -font   => '-*-helvetica-medium-r-normal--*-180-*-*-*-*-*-*',
  397.     );
  398.     $title->pack(qw/-side top -fill x/);
  399.     my($label, $var);
  400.     foreach my $i (@$args) {
  401.     ($label, $var) = @$i;
  402.     my $wf = $VARS->Frame->pack(qw/-anchor w/);
  403.     $wf->Label(-text => "$label: ")->pack(qw/-side left/);
  404.     $wf->Label(-textvariable => $var)->pack(qw/-side left/);
  405.     }
  406.     $VARS->Button(-text => 'OK', -command => [$VARS => 'destroy'])->
  407.         pack(qw/-side bottom -pady 2/);
  408.  
  409. } # end see_vars
  410.  
  411. sub show_stat {
  412.  
  413.     # Display name of current demonstration.  $sv is a reference to the
  414.     # status Label -textvariable, $text is the Text widget reference and
  415.     # $index is the demonstration index in the Text widget.
  416.  
  417.     my($sv, $text, $index) = @ARG;
  418.  
  419.     my @tags = $text->tag('names', $index);
  420.     my $i = lsearch('demo\-.*', @tags);
  421.     return if $i < 0;
  422.     my($demo) = $tags[$i] =~ /demo-(.*)/;
  423.     $$sv = "Click Button-1 to run the \"$demo\" demonstration.";
  424.  
  425. } # end show_stat
  426.  
  427. sub view_widget_code {
  428.  
  429.     # Expose a file's innards to the world too, but only for viewing.
  430.  
  431.     my($widget) = @ARG;
  432.  
  433.     if (not Exists $VIEW) {
  434.     $VIEW = $MW->Toplevel;
  435.     $VIEW->iconname('widget');
  436.     my $view_buttons = $VIEW->Frame;
  437.     $view_buttons->pack(qw/-side bottom -expand 1 -fill x/);
  438.     my $view_buttons_dismiss = $view_buttons->Button(
  439.             -text    => 'Dismiss',
  440.             -command => [$VIEW => 'withdraw'],
  441.     );
  442.     $view_buttons_dismiss->pack(qw/-side left -expand 1/);
  443.     $VIEW_TEXT = $VIEW->Scrolled('Text',
  444.                      qw/-scrollbars e -height 40 -setgrid 1/);
  445.     $VIEW_TEXT->pack(qw/-side left -expand 1 -fill both/);
  446.     } else {
  447.     $VIEW->deiconify;
  448.     $VIEW->raise;
  449.     }
  450.     $VIEW->title("Demo code: $widget");
  451.     $VIEW_TEXT->configure(qw/-state normal/);
  452.     $VIEW_TEXT->delete(qw/1.0 end/);
  453.     open(VIEW, "<$widget") or warn "Cannot open demo file $widget: $OS_ERROR!";
  454.     {
  455.     local $INPUT_RECORD_SEPARATOR = undef;
  456.     $VIEW_TEXT->insert('1.0', <VIEW>);
  457.     }
  458.     close VIEW;
  459.     $VIEW_TEXT->mark(qw/set insert 1.0/);
  460.     $VIEW_TEXT->configure(qw/-state disabled/);
  461.  
  462. } # end view_widget_code
  463. __END__
  464. :endofperl
  465.