File Coverage

blib/lib/Tk/WaitBoxFixed.pm
Criterion Covered Total %
statement 4 6 66.6
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 6 8 75.0


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