| 1 | ########################################## | ||
| 2 | ########################################## | ||
| 3 | ## ## | ||
| 4 | ## WaitBox - a reusable Tk-widget ## | ||
| 5 | ## Wait Dialog ## | ||
| 6 | ## ## | ||
| 7 | ## Version 1.3 ## | ||
| 8 | ## ## | ||
| 9 | ## Brent B. Powers (B2Pi) ## | ||
| 10 | ## Powers@B2Pi.com ## | ||
| 11 | ## ## | ||
| 12 | ########################################## | ||
| 13 | ########################################## | ||
| 14 | |||
| 15 | ############################################################################### | ||
| 16 | ############################################################################### | ||
| 17 | ## WaitBox | ||
| 18 | ## Object Oriented Wait Dialog for TkPerl | ||
| 19 | ## (Apologies to John Stoffel and Stephen O. Lidie) | ||
| 20 | ## | ||
| 21 | ## Changes: | ||
| 22 | ## Ver 1.1 Changed show to Show, unshow to unShow, and general | ||
| 23 | ## cleanup for perl5.002 gamma | ||
| 24 | ## Ver 1.2 Changed to general distribution, add VERSION and Version | ||
| 25 | ## Ver 1.3 Added -takefocus param, on suggestion of Ben Hochstedler | ||
| 26 | ## <benh@med.ge.com>, some other stuff | ||
| 27 | ## | ||
| 28 | ############################################################################### | ||
| 29 | ############################################################################### | ||
| 30 | |||
| 31 | package Tk::WaitBox; | ||
| 32 | |||
| 33 | use strict; | ||
| 34 | use Tk::Toplevel; | ||
| 35 | |||
| 36 | @Tk::WaitBox::ISA = qw (Tk::Toplevel); | ||
| 37 | |||
| 38 | Tk::Widget->Construct('WaitBox'); | ||
| 39 | |||
| 40 | $Tk::WaitBox::VERSION = '1.3'; | ||
| 41 | |||
| 42 | ### A couple of convenience variables | ||
| 43 | my(@wd_fullpack) = (-expand => 1, -fill => 'both'); | ||
| 44 | my(@wd_packtop) = (-side => 'top'); | ||
| 45 | my(@wd_packleft) = (-side => 'left'); | ||
| 46 | |||
| 47 | 1; | ||
| 48 | |||
| 49 | sub Populate { | ||
| 50 | ### Wait box constructor. Uses new inherited from base class | ||
| 51 | my($cw, $wdtop, $fm, $bitmap, $txt1, $uframe, $txt2); | ||
| 52 | $cw = shift; | ||
| 53 | $cw->SUPER::Populate(@_); | ||
| 54 | |||
| 55 | ## Create the toplevel window | ||
| 56 | $cw->withdraw; | ||
| 57 | $cw->protocol('WM_DELETE_WINDOW' => sub {}); | ||
| 58 | $cw->transient($cw->toplevel); | ||
| 59 | |||
| 60 | ### Set up the status | ||
| 61 | $cw->{Shown} = 0; | ||
| 62 | |||
| 63 | ### Set up the cancel button and text | ||
| 64 | $cw->{cancelroutine} = undef if !defined($cw->{cancelroutine}); | ||
| 65 | $cw->{canceltext} = 'Cancel' if !defined($cw->{canceltext}); | ||
| 66 | |||
| 67 | ### OK, create the dialog | ||
| 68 | ### Start with the upper frame (which contains two messages) | ||
| 69 | ## And maybe more.... | ||
| 70 | $wdtop = $cw->Frame->pack(@wd_fullpack, @wd_packtop); | ||
| 71 | |||
| 72 | $fm = $wdtop->Frame(-borderwidth => 2, -relief => 'raised') | ||
| 73 | ->pack(@wd_packleft, -ipadx => 20, @wd_fullpack); | ||
| 74 | |||
| 75 | $bitmap = $fm->Label(Name => 'bitmap') | ||
| 76 | ->pack(@wd_packleft, -ipadx => 36, @wd_fullpack); | ||
| 77 | |||
| 78 | ## Text Frame | ||
| 79 | $fm = $wdtop->Frame(-borderwidth => 2, -relief => 'raised') | ||
| 80 | ->pack(@wd_packleft, -ipadx => 20, @wd_fullpack); | ||
| 81 | |||
| 82 | $txt1 = $fm->Label(-wraplength => '3i', -justify => 'center', | ||
| 83 | -textvariable => \$cw->{Configure}{-txt1}) | ||
| 84 | ->pack(@wd_packtop, -pady => 3, @wd_fullpack); | ||
| 85 | |||
| 86 | ### Eventually, I want to create a user configurable frame | ||
| 87 | ### in between the two frames | ||
| 88 | $uframe = $fm->Frame | ||
| 89 | ->pack(@wd_packtop); | ||
| 90 | $cw->Advertise(uframe => $uframe); | ||
| 91 | |||
| 92 | $cw->{Configure}{-txt2} = "Please Wait" | ||
| 93 | unless defined($cw->{Configure}{-txt2}); | ||
| 94 | |||
| 95 | $txt2 = $fm->Label(-textvariable => \$cw->{Configure}{-txt2}) | ||
| 96 | ->pack(@wd_packtop, @wd_fullpack, -pady => 9); | ||
| 97 | |||
| 98 | ### We'll let the cancel frame and button wait until Show time | ||
| 99 | |||
| 100 | ### Set up configuration | ||
| 101 | $cw->ConfigSpecs(-bitmap => [$bitmap, undef, undef, 'hourglass'], | ||
| 102 | -foreground=> [[$txt1,$txt2], 'foreground','Foreground','black'], | ||
| 103 | -background=> ['DESCENDANTS', 'background', 'Background',undef], | ||
| 104 | -font => [$txt1,'font','Font','-Adobe-Helvetica-Bold-R-Normal--*-180-*'], | ||
| 105 | -canceltext=> ['PASSIVE', undef, undef, 'Cancel'], | ||
| 106 | -cancelroutine=> ['PASSIVE', undef, undef, undef], | ||
| 107 | -txt1 => ['PASSIVE', undef, undef, undef], | ||
| 108 | -txt2 => ['PASSIVE',undef,undef,undef], | ||
| 109 | -resizeable => ['PASSIVE',undef,undef,1], | ||
| 110 | -takefocus => ['SELF', undef, undef, 1]); | ||
| 111 | } | ||
| 112 | |||
| 113 | sub Version {return $Tk::WaitBox::VERSION;} | ||
| 114 | |||
| 115 | sub Show { | ||
| 116 | ## Do last minute configuration and Show the dialog | ||
| 117 | my($wd, @args) = @_; | ||
| 118 | |||
| 119 | if ( defined($wd->{Configure}{-cancelroutine}) && | ||
| 120 | !defined($wd->{CanFrame})) { | ||
| 121 | my($canFrame) = $wd->Frame (-background => $wd->cget('-background')); | ||
| 122 | $wd->{CanFrame} = $canFrame; | ||
| 123 | $canFrame->pack(-side => 'top', @wd_packtop, -fill => 'both'); | ||
| 124 | $canFrame->configure(-cursor => 'top_left_arrow'); | ||
| 125 | $canFrame->Button(-text => $wd->{Configure}{-canceltext}, | ||
| 126 | -command => $wd->{Configure}{-cancelroutine}) | ||
| 127 | ->pack(-padx => 5, -pady => 5, | ||
| 128 | -ipadx => 5, -ipady => 5); | ||
| 129 | } | ||
| 130 | |||
| 131 | ## Grab the input queue and focus | ||
| 132 | $wd->parent->configure(-cursor => 'watch') if $wd->{Configure}{-takefocus}; | ||
| 133 | $wd->configure(-cursor => 'watch'); | ||
| 134 | $wd->update; | ||
| 135 | |||
| 136 | my($x) = int( ($wd->screenwidth | ||
| 137 | - $wd->reqwidth)/2 | ||
| 138 | - $wd->vrootx); | ||
| 139 | |||
| 140 | my($y) = int( ($wd->screenheight | ||
| 141 | - $wd->reqheight)/2 | ||
| 142 | - $wd->vrooty); | ||
| 143 | |||
| 144 | $wd->geometry("+$x+$y"); | ||
| 145 | |||
| 146 | $wd->{Shown} = 1; | ||
| 147 | |||
| 148 | $wd->deiconify; | ||
| 149 | $wd->tkwait('visibility', $wd); | ||
| 150 | |||
| 151 | if ($wd->{Configure}{-takefocus}) { | ||
| 152 | $wd->grab(); | ||
| 153 | $wd->focus(); | ||
| 154 | } | ||
| 155 | $wd->update; | ||
| 156 | |||
| 157 | return $wd; | ||
| 158 | |||
| 159 | } | ||
| 160 | |||
| 161 | sub unShow { | ||
| 162 | my($wd) = @_; | ||
| 163 | |||
| 164 | return unless $wd->{Shown}; | ||
| 165 | $wd->{CanFrame}->destroy if defined($wd->{CanFrame}); | ||
| 166 | $wd->{CanFrame} = undef; | ||
| 167 | $wd->parent->configure(-cursor => 'top_left_arrow'); | ||
| 168 | |||
| 169 | $wd->grab('release'); | ||
| 170 | $wd->withdraw; | ||
| 171 | $wd->parent->update; | ||
| 172 | $wd->{Shown} = 0; | ||
| 173 | } | ||
| 174 | |||
| 175 | __END__ |