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

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