home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / Share / Editores / Perl5 / perl / lib / site / Tk / WaitBox.pm < prev    next >
Encoding:
Perl POD Document  |  1997-08-10  |  9.3 KB  |  318 lines

  1. ##########################################
  2. ##########################################
  3. ##                    ##
  4. ##    WaitBox - a reusable Tk-widget    ##
  5. ##          Wait Dialog        ##
  6. ##                    ##
  7. ##    Version 1.1            ##
  8. ##                    ##
  9. ##    Brent B. Powers    (B2Pi)        ##
  10. ##    Merrill Lynch            ##
  11. ##    powers@swaps-comm.ml.com    ##
  12. ##                    ##
  13. ##                    ##
  14. ##########################################
  15. ##########################################
  16.  
  17. ###############################################################################
  18. ###############################################################################
  19. ## WaitBox
  20. ##    Object Oriented Wait Dialog for TkPerl
  21. ##    (Apologies to John Stoffel and Stephen O. Lidie)
  22. ##
  23.  
  24. ## Changes:
  25. ## Ver 1.1 Changed show to Show, unshow to unShow, and general
  26. ##         cleanup for perl5.002 gamma
  27.  
  28. =head1 NAME
  29.  
  30. Tk::WaitBox - An Object Oriented Wait Dialog for Perl/Tk, of the Please Wait variety.
  31.  
  32. =head1 DESCRIPTION
  33.  
  34. A WaitBox consists of a number of subwidgets:
  35.  
  36. =over 4
  37.  
  38. =item
  39.  
  40. =head2 bitmap
  41.  
  42. A bitmap (configurable via the I<-bitmap> command, the default is an hourglass) on the left side of the WaitBox
  43.  
  44. =head2 label
  45.  
  46. A label (configurable via the I<-txt1> command), with text in the upper portion of the right hand frame
  47.  
  48. =head2 secondary label
  49.  
  50. Another label (configurable via the I<-txt2> command, the default is 'Please Wait'), with text in the lower portion of the right hand frame
  51.  
  52. =head2 userframe
  53.  
  54. A frame displayed, if required, between the label and the secondary label.  For details, see the example code and the Advertised Widget section
  55.  
  56. =head2 cancel button
  57.  
  58. If a cancelroutine (configured via the I<-cancelroutine> command) is defined, a frame will be packed below the labels and bitmap, with a single button.  The text of the button will be 'Cancel' (configurable via the I<-canceltext> command), and the button will call the supplied subroutine when pressed.
  59.  
  60. =back
  61.  
  62. =head1 SYNOPSIS
  63.  
  64. =over 4
  65.  
  66. =item Usage Description
  67.  
  68. =item
  69.  
  70. =head2 Basic Usage
  71.  
  72. To use, create your WaitDialog objects during initialization, or at least before a Show.  When you wish to display the WaitDialog object, invoke the 'Show' method on the WaitDialog object; when you wish to cease displaying the WaitDialog object, invoke the 'unShow' method on the object.
  73.  
  74. =head2 Configuration
  75.  
  76. Configuration may be done at creation or via the configure method.  
  77.  
  78. =head2 Example Code
  79.  
  80. =item
  81.  
  82.  #!/usr/local/bin/perl -w 
  83.  
  84.  use Tk;
  85.  use Tk::WaitBox;
  86.  use strict;
  87.  
  88.  my($root) = MainWindow->new;
  89.  my($utxt) = "Initializing...";
  90.  
  91.  my($wd) = $root->WaitBox(
  92.     -bitmap =>'questhead', # Default would be 'hourglass'
  93.     -txt2 => 'tick-tick-tick', #default would be 'Please Wait'
  94.     -title => 'Takes forever to get service around here',
  95.     -cancelroutine => sub {
  96.         print "\nI'm canceling....\n";
  97.         $wd->unShow;
  98.         $utxt = undef;
  99.     });
  100.  $wd->configure(-txt1 => "Hurry up and Wait, my Drill Sergeant told me");
  101.  $wd->configure(-foreground => 'blue',-background => 'white');
  102.  
  103.  ### Do something quite boring with the user frame
  104.  my($u) = $wd->{SubWidget}(uframe);
  105.  $u->pack(-expand => 1, -fill => 'both');
  106.  $u->Label(-textvariable => \$utxt)->pack(-expand => 1, -fill => 'both');
  107.  
  108.  ## It would definitely be better to do this with a canvas... this is dumb
  109.  my($base) = $u->Frame(-background =>'gray',
  110.                -relief => 'sunken',
  111.                -borderwidth => 2,
  112.                -height => 20)
  113.      ->pack(-side => 'left', -anchor => 'w',-expand => 1,
  114.         -fill => 'both');
  115.  my($bar) = $base->Frame(-borderwidth => 2,
  116.              -relief => 'raised', -height => 20,
  117.              -width => 0, -background => 'blue')
  118.      ->pack(-fill => 'y', -side => 'left');
  119.  
  120.  $wd->configure(-canceltext => 'Halt, Cease, Desist'); # default is 'Cancel'
  121.  
  122.  $wd->Show;
  123.  
  124.  for (1..15) {
  125.      sleep(1);
  126.      $bar->configure(-width => int($_/15*$base->Width));
  127.      $utxt = 100*$_/15 . "% Complete";
  128.      $root->update;
  129.      last if !defined($utxt);
  130.  }
  131.  
  132.  $wd->unShow;
  133.  
  134. =back
  135.  
  136.  
  137. =head1 Advertised Subwidgets
  138.  
  139. =over 4
  140.  
  141. =item uframe
  142.  
  143. uframe is a frame created between the two messages.  It may be used for anything the user has in mind... including exciting cycle wasting displays of sand dropping through an hour glass, Zippy riding either a Gnu or a bronc, et cetera.
  144.  
  145. Assuming that the WaitBox is referenced by $w, the uframe may be addressed as $w->subwidget{'uframe'}.  Having gotten the address, you can do anything (I think) you would like with it
  146.  
  147. =back
  148.  
  149. =head1 Author
  150.  
  151. B<Brent B. Powers, Merrill Lynch (B2Pi)>
  152.  powers@ml.com
  153.  
  154. This code may be distributed under the same conditions as perl itself.
  155.  
  156.  
  157. =cut
  158.  
  159. ###############################################################################
  160. ###############################################################################
  161.  
  162. package Tk::WaitBox;
  163. use strict;
  164. require Tk::Toplevel;
  165.  
  166. @Tk::WaitBox::ISA = qw (Tk::Toplevel);
  167.  
  168. Construct Tk::Widget 'WaitBox';
  169.  
  170. ### A couple of convenience variables
  171. my(@wd_fullpack) = (-expand => 1, -fill => 'both');
  172. my(@wd_packtop) = (-side => 'top');
  173. my(@wd_packleft) = (-side => 'left');
  174.  
  175.  
  176. sub Populate {
  177.     ### Wait box constructor.  Uses new inherited from base class
  178.     my($cw, @args) = @_;
  179.  
  180.     $cw->SUPER::Populate(@args);
  181.  
  182.     ## Create the toplevel window
  183.     $cw->withdraw;
  184.     $cw->protocol('WM_DELETE_WINDOW' => sub {});
  185.     $cw->transient($cw->toplevel);
  186.  
  187.     ### Set up the status
  188.     $cw->{'Shown'} = 0;
  189.  
  190.     ### Set up the cancel button and text
  191.     $cw->{'cancelroutine'} = undef if !defined($cw->{'cancelroutine'});
  192.     $cw->{'canceltext'} = 'Cancel' if !defined($cw->{'canceltext'});
  193.  
  194.     ### OK, create the dialog
  195.     ### Start with the upper frame (which contains two messages)
  196.     ## And maybe more....
  197.     my($wdtop) = $cw->Frame;
  198.     $wdtop->pack(@wd_fullpack, @wd_packtop);
  199.  
  200.     my($fm) = $wdtop->Frame(-borderwidth => 2, -relief => 'raised');
  201.     $fm->pack(@wd_packleft, -ipadx => 20, @wd_fullpack);
  202.  
  203.     my($bitmap) = $fm->Label(Name => 'bitmap');
  204.     $bitmap->pack(@wd_packleft, -ipadx => 36, @wd_fullpack);
  205.  
  206.     ## Text Frame
  207.     $fm = $wdtop->Frame(-borderwidth => 2, -relief => 'raised');
  208.     $fm->pack(@wd_packleft, -ipadx => 20, @wd_fullpack);
  209.  
  210.     my($txt1) = $fm->Label(-wraplength => '3i', -justify => 'center',
  211.                  -textvariable => \$cw->{Configure}{-txt1});
  212.     $txt1->pack(@wd_packtop, -pady => 3, @wd_fullpack);
  213.  
  214.     ### Eventually, I want to create a user configurable frame
  215.     ### in between the two frames
  216.     my($uframe) = $fm->Frame;
  217.     $uframe->pack(@wd_packtop);
  218.     $cw->Advertise(uframe => $uframe);
  219.  
  220.     $cw->{Configure}{-txt2} = "Please Wait"
  221.         unless defined($cw->{Configure}{-txt2});
  222.  
  223.     my($txt2) = $fm->Label(-textvariable => \$cw->{Configure}{-txt2});
  224.     $txt2->pack(@wd_packtop, @wd_fullpack, -pady => 9);
  225.  
  226.     ### We'll let the cancel frame and button wait until Show time
  227.  
  228.     ### Set up configuration
  229.     $cw->ConfigSpecs(-bitmap    => [$bitmap, undef, undef, 'hourglass'],
  230.              -foreground=> [[$txt1,$txt2], 'foreground','Foreground','black'],
  231.              -background=> ['DESCENDANTS', 'background', 'Background',undef],
  232.              -font    => [$txt1,'font','Font','-Adobe-Helvetica-Bold-R-Normal--*-180-*-*-*-*-*-*'],
  233.              -canceltext=> ['PASSIVE', undef, undef, 'Cancel'],
  234.              -cancelroutine=> ['PASSIVE', undef, undef, undef],
  235.              -txt1    => ['PASSIVE', undef, undef, undef],
  236.              -txt2    => ['PASSIVE',undef,undef,undef],
  237.              -resizeable => ['PASSIVE',undef,undef,1]);
  238. }
  239.                                                      
  240. sub Show {
  241.     ## Do last minute configuration and Show the dialog
  242.     my($wd, @args) = @_;
  243.  
  244.     if ( defined($wd->{Configure}{-cancelroutine}) &&
  245.     !defined($wd->{'CanFrame'})) {
  246.     my($canFrame) = $wd->Frame (-background => $wd->cget('-background'));
  247.     $wd->{'CanFrame'} = $canFrame;
  248.     $canFrame->pack(-side => 'top', @wd_packtop, -fill => 'both');
  249.     $canFrame->configure(-cursor => 'top_left_arrow');
  250.     $canFrame->Button(-text => $wd->{Configure}{-canceltext},
  251.               -command => $wd->{Configure}{-cancelroutine})
  252.         ->pack(-padx => 5, -pady => 5,
  253.                -ipadx => 5, -ipady => 5);
  254.     }
  255.  
  256.     ## Grab the input queue and focus
  257.     $wd->parent->configure(-cursor => 'watch');
  258.     $wd->configure(-cursor => 'watch');
  259.     $wd->update;
  260.  
  261.     my($x) = int( ($wd->screenwidth
  262.          - $wd->reqwidth)/2
  263.          - $wd->vrootx);
  264.  
  265.     my($y) = int( ($wd->screenheight
  266.          - $wd->reqheight)/2
  267.          - $wd->vrooty);
  268.  
  269.     $wd->geometry("+$x+$y");
  270.  
  271.     $wd->{'Shown'} = 1;
  272.  
  273.     $wd->deiconify;
  274.     $wd->tkwait('visibility', $wd);
  275.  
  276.     $wd->grab();
  277.     $wd->focus();
  278.     $wd->update;
  279.  
  280.     return $wd;
  281.  
  282. }
  283.  
  284. sub unShow {
  285.     my($wd) = @_;
  286.  
  287.     return if !$wd->{'Shown'};
  288.     $wd->{'CanFrame'}->destroy if (defined($wd->{'CanFrame'}));
  289.     $wd->{'CanFrame'} = undef;
  290.     $wd->parent->configure(-cursor => 'top_left_arrow');
  291.  
  292.     $wd->grab('release');
  293.     $wd->withdraw;
  294.     $wd->parent->update;
  295.     $wd->{'Shown'} = 0;
  296. }
  297.  
  298. 1;
  299.  
  300. __END__
  301. From  powers@swaps.ml.com  Fri Mar  1 07:19:41 1996 
  302. Return-Path: <powers@swaps.ml.com> 
  303. From: powers@swaps.ml.com (Brent B. Powers Swaps Programmer X2293)
  304. Date: Fri, 1 Mar 1996 02:19:28 -0500 
  305. Message-Id: <199603010719.CAA16433@swapsdvlp02.ny-swaps-develop.ml.com> 
  306. To: nik@tiuk.ti.com 
  307. Subject: WaitBox.pm 
  308. P-From: "Brent B. Powers Swaps Programmer x2293" <powers@swaps.ml.com> 
  309.  
  310. Greetings.  Attached is a slightly updated version of WaitBox.pm to go
  311. out (hopefully) with the next release of Tk.  It now works properly
  312. under perl5.002gamma.  Could you please let me know that you did get
  313. this... We're having some trouble with mail gateways.
  314.  
  315. Cheers.
  316.  
  317. Brent B. Powers             Merrill Lynch          powers@swaps.ml.com
  318.