blib/lib/Tk/JDialog.pm

filter: Statementstmt 85.7% 12 / 14 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 89.4% 17 / 19 CC 1 SCAR 0.6
CC
1
Cov
86%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Tk::JDialog;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 Tk::JDialog - a translation of `tk_dialog' from Tcl/Tk to TkPerl (based on John Stoffel's idea).
10  
11 =head1 VERSION
12  
13 Version 1.01
14  
15 =cut
16  
17 our $VERSION = '1.20';
18  
19  
20 =head1 SYNOPSIS
21  
22 use Tk::JDialog;
23  
24 my $Dialog = $mw->JDialog( -option => value, ... );
25 ...
26 my $button_label = $Dialog->Show;
27  
28 =head1 DESCRIPTION
29  
30 This is an OO implementation of `tk_dialog'. First, create all your Dialog
31 objects during program initialization. When it's time to use a dialog,
32 invoke the `show' method on a dialog object; the method then displays
33 the dialog, waits for a button to be invoked, and returns the text
34 label of the selected button.
35  
36 A Dialog object essentially consists of two subwidgets: a Label widget for
37 the bitmap and a Label wigdet for the text of the dialog. If required, you
38 can invoke the `configure' method to change any characteristic of these
39 subwidgets.
40  
41 Because a Dialog object is a Toplevel widget all the 'composite' base class
42 methods are available to you.
43  
44 =head1 EXAMPLE
45  
46 #!/usr/bin/perl
47  
48 use Tk::JDialog;
49  
50 my $mw = MainWindow->new;
51 my $Dialog = $mw->JDialog(
52 -title => 'Choose!', #DISPLAY A WINDOW TITLE
53 -text => 'Press Ok to Continue', #DISPLAY A CAPTION
54 -bitmap => 'info', #DISPLAY BUILT-IN info BITMAP.
55 -default_button => '~Ok',
56 -escape_button => '~Cancel',
57 -buttons => ['~Ok', '~Cancel', '~Quit'], #DISPLAY 3 BUTTONS
58 -images => ['/tmp/ok.xpm', '', ''], #EXAMPLE WITH IMAGE FILE
59 );
60 my $button_label = $Dialog->Show( );
61 print "..You pressed [$button_label]!\n";
62 exit(0);
63  
64 =head1 OPTIONS
65  
66 =over 4
67  
68 =item -title
69  
70 (string) - Title to display in the dialog's decorative window frame.
71 Default: ''.
72  
73 =item -text
74  
75 (string) - Message to display in the dialog widget. Default: ''.
76  
77 =item -bitmap
78  
79 (string) - Bitmap to display in the dialog.
80 If non-empty, specifies a bitmap to display in the top portion of
81 the Dialog, to the left of the text. If this is an empty string
82 then no bitmap is displayed in the Dialog.
83 There are several built-in Tk bitmaps: 'error', 'hourglass', 'info',
84 'questhead', 'question', 'warning', 'Tk', and 'transparent'.
85 You can also use a bitmap file name, ie. '@/path/to/my/bitmap'
86 Default: ''.
87  
88 =item -default_button
89  
90 (string) - Text label of the button that is to display the
91 default border and is to be selected if the user presses [Enter].
92 (''signifies no default button). Default: ''.
93  
94 =item -escape_button
95  
96 (string) - Text label of the button that is to be invoked when the
97 user presses the <Esc> key. Default: ''.
98  
99 =item -button_labels
100  
101 (Reference) - A reference to a list of one or more strings to
102 display in buttons across the bottom of the dialog. These strings
103 (labels) are also returned by the Show() method corresponding to
104 the button selected. NOTE: A tilde ("~") can be placed before a
105 letter in a label string to indicate the <Alt-<letterkey>> that
106 the user can also press to select the button, for example:
107 "~Ok" means select this button if the user presses <Alt-<O>>.
108 The tilde is not displayed for the button text. The text is also
109 not displayed if an image file is specified in the corresponding
110 optional -images array, but is returned if the button is pressed.
111 If this option is not given, a single button labeled "OK" is created.
112  
113 =item -images
114  
115 (Reference) - Specify the optional path and file id for an image
116 for each button to display an image in lieu of the label text
117 ('' if a corresponding button is to use text). NOTE: button
118 will use text if the image file is not found. Also the
119 "-button_labels" option MUST ALWAYS be specified anyway to provide
120 the required return string.
121  
122 =item -noballoons
123  
124 (boolean) - if true (1) then no balloon displaying the "button_labels"
125 label text value will be displayed when the mouse hovers over the
126 corresponding buttons which display imiages. If false (0), then
127 text balloons will be displayed when hovering. Default: 0.
128  
129 =back
130  
131 =head1 METHODS
132  
133 =over 4
134  
135 =item Show ( [ -global | -nograb ] )
136  
137 $answer = $dialog->B<Show>( [ -global | -nograb ] );
138  
139 This method displays the Dialog box, waits for the user's response, and
140 stores the text string of the selected Button in $answer. This allows
141 the programmer to determine which button the user selected.
142
143 NOTE: Execution goes into a wait-loop here until the the user makes a
144 selection!
145
146 If -global is specified a global (rather than local) grab is
147 performed (No other window or widget can be minipulated via the keyboard
148 or mouse until a button is selected) making the dialog "modal".
149 Default: "-nograb" (the dialog is "non-modal" while awaiting input).
150  
151 The actual Dialog is shown using the Popup method. Any other
152 options supplied to Show are passed to Popup, and can be used to
153 position the Dialog on the screen. Please read L<Tk::Popup> for
154 details.
155  
156 =item Populate ( -option => value, ... )
157  
158 (Constructor) - my $Dialog = $mw->JDialog( -option => value, ... );
159  
160 =back
161  
162 =head1 ADVERTISED WIDGETS
163  
164 Tk::JDialog inherits all the Tk::Dialog exposed widgets and methods plus
165 the following two subwidgets:
166
167 =over 4
168  
169 =item message
170  
171 The dialog's Label widget containing the message text.
172
173 =item bitmap
174  
175 The dialog's Label widget containing the bitmap image.
176  
177 =back
178  
179 =head1 AUTHOR
180  
181 Jim Turner, C<< <turnerjw784 at yahoo.com> >>
182  
183 =head1 BUGS
184  
185 Please report any bugs or feature requests to C<bug-tk-jdialog at rt.cpan.org>,
186 or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Tk-JDialog>.
187 I will be notified, and then you'll automatically be notified of progress on
188 your bug as I make changes.
189  
190 =head1 SUPPORT
191  
192 You can find documentation for this module with the perldoc command.
193  
194 perldoc Tk::JDialog
195  
196  
197 You can also look for information at:
198  
199 =over 4
200  
201 =item * RT: CPAN's request tracker (report bugs here)
202  
203 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Tk-JDialog>
204  
205 =item * AnnoCPAN: Annotated CPAN documentation
206  
207 L<http://annocpan.org/dist/Tk-JDialog>
208  
209 =item * CPAN Ratings
210  
211 L<http://cpanratings.perl.org/d/Tk-JDialog>
212  
213 =item * Search CPAN
214  
215 L<http://search.cpan.org/dist/Tk-JDialog/>
216  
217 =back
218  
219  
220 =head1 ACKNOWLEDGEMENTS
221  
222 Tk::JDialog derived from the L<Tk::Dialog> wiget from Tcl/Tk to TkPerl
223 (based on John Stoffel's idea). It addes the options: -escape_button
224 and -images,
225  
226 =head1 LICENSE AND COPYRIGHT
227  
228 Copyright 1997-2023 Jim Turner.
229  
230 This program is free software; you can redistribute it and/or
231 modify it under the terms of the GNU Lesser General Public
232 License as published by the Free Software Foundation; either
233 version 2.1 of the License, or (at your option) any later version.
234  
235 This program is distributed in the hope that it will be useful,
236 but WITHOUT ANY WARRANTY; without even the implied warranty of
237 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
238 Lesser General Public License for more details.
239  
240 You should have received a copy of the GNU Lesser General Public
241 License along with this program; if not, write to the Free
242 Software Foundation, Inc.,
243 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
244  
245 =head1 SEE ALSO
246  
247 L<Tk::Dialog>, L<Tk::Label>, L<Tk::Widget>, L<Tk>
248  
249 =cut
250  
251 # JDialog - a translation of `tk_dialog' from Tcl/Tk to TkPerl (based on
252 # John Stoffel's idea).
253 #
254 # Modified 2/13/97 by Jim Turner of Computer Sciences Corporation to
255 # add underline character (alt-key) activation of buttons, fix bug in the
256 # bindings for <Return> key where default button always activated even if
257 # another button had the keyboard focus. Now, the default button starts
258 # with the input focus!!!
259 #
260 # Jim Turner also added the "escape_button" option on 2/14/97 to allow
261 # programmer to specify a button to invoke if user presses the <Escape> key!
262 # Jim Turner also added the "images" option on 2/14/97 to allow programmer
263 # to specify gifs in leu of text for the buttons.
264 #
265 # Jim Turner also removed the "wraplength" option on 2/19/97 to allow
266 # longer label strings (>3") to not be broken. User can specify -wraplength!
267 # Stephen O. Lidie, Lehigh University Computing Center. 94/12/27
268 # lusol@Lehigh.EDU
269 #
270 # 04/22/97 Jim Turner fixed bug where screen completely locks up if the calling
271 # script invokes a Motif app (ie. xv or another Perl/Tk app) shortly after
272 # calling this dialog box. Did not seem to adversely effect keyboard focus.
273 # fixed by commenting out 1 line of code (&$old_focus);
274 #
275 # This is an OO implementation of `tk_dialog'. First, create all your Dialog
276 # objects during program initialization. When it's time to use a dialog,
277 # invoke the `show' method on a dialog object; the method then displays the
278 # dialog, waits for a button to be invoked, and returns the text label of the
279 # selected button.
280 #
281 # A Dialog object essentially consists of two subwidgets: a Label widget for
282 # the bitmap and a Label wigdet for the text of the dialog. If required, you
283 # can invoke the `configure' method to change any characteristic of these
284 # subwidgets.
285 #
286 # Because a Dialog object is a Toplevel widget all the 'composite' base class
287 # methods are available to you.
288  
289 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
290 #use strict qw(vars);
291 our $useBalloon = 0;
292 0 ▶use Tk ":eventtypes";
Subroutine BEGIN: called CC=1 SCAR=2.6
293 eval 'use Tk::Balloon; $useBalloon = 1; 1';
294 require Tk::Toplevel;
295  
296 @Tk::JDialog::ISA = qw(Tk::Toplevel);
297  
298 Tk::Widget->Construct('JDialog');
299  
300 sub Populate
301 {
302 # Dialog object constructor. Uses `new' method from base class
303 # to create object container then creates the dialog toplevel.
304  
305 my($cw, $args) = @_;
306  
307 $cw->SUPER::Populate($args);
308  
309 my ($w_bitmap,$w_but,$pad1,$pad2,$underlinepos,$mychar,$blshow,$i);
310 my ($btnopt,$undopt,$balloon);
311  
312 my $buttons = delete $args->{'-buttons'};
313 my $images = delete $args->{'-images'};
314 $buttons = ['~OK'] unless (defined $buttons);
315 my $default_button = delete $args->{-default_button};
316 my $escape_button = delete $args->{-escape_button};
317 my $noballoons = delete $args->{-noballoons};
318 $useBalloon = 0 if ($noballoons);
319 $default_button = $buttons->[0] unless (defined $default_button);
320  
321 # Create the Toplevel window and divide it into top and bottom parts.
322  
323 $cw->{'selected_button'} = '';
324 my (@pl) = (-side => 'top', -fill => 'both');
325 ($pad1, $pad2) =
326 ([-padx => '3m', -pady => '3m'], [-padx => '3m', -pady => '2m']);
327  
328 $cw->withdraw;
329 $cw->iconname('JDialog');
330 $cw->protocol('WM_DELETE_WINDOW' => sub {});
331 $cw->transient($cw->Parent->toplevel) unless ($^O =~ /Win/i);
332  
333 my $w_top = $cw->Frame(Name => 'top',-relief => 'raised', -borderwidth => 1);
334 my $w_bot = $cw->Frame(Name => 'bot',-relief => 'raised', -borderwidth => 1);
335 $w_top->pack(@pl);
336 $w_bot->pack(@pl);
337  
338 # Fill the top part with the bitmap and message.
339  
340 @pl = (-side => 'left');
341  
342 $w_bitmap = $w_top->Label(Name => 'bitmap');
343 $w_bitmap->pack(@pl, @$pad1);
344 my $w_msg = $w_top->Label(
345 #-wraplength => '3i', --!!! Removed 2/19 by Jim Turner
346 -justify => 'left'
347 );
348  
349 $w_msg->pack(-side => 'right', -expand => 1, -fill => 'both', @$pad1);
350  
351 # Create a row of buttons at the bottom of the dialog.
352  
353 my ($w_default_button, $bl) = (undef, '');
354 $cw->{'default_button'} = undef;
355 $cw->{'escape_button'} = undef;
356 $i = 0;
357 foreach $bl (@$buttons) {
358 $blshow = $bl;
359 $underlinepos = ($blshow =~ s/^(.*)~/$1/) ? length($1): undef;
360 if (defined($$images[$i]) && $$images[$i] gt ' ' && -e $$images[$i]) {
361 $cw->Photo($blshow, -file => $$images[$i]);
362 $btnopt = '-image';
363 } else {
364 $btnopt = '-text';
365 }
366 if (defined($underlinepos)) {
367 $mychar = substr($blshow,$underlinepos,1);
368 $w_but = $w_bot->Button(
369 $btnopt => $blshow,
370 -underline => $underlinepos,
371 -command => [
372 sub {
373 $_[0]->{'selected_button'} = $_[1];
374 }, $cw, $bl,
375 ]
376 );
377 $cw->bind("<Alt-\l$mychar>", [$w_but => "Invoke"]);
378 $cw->bind("<Key-\l$mychar>", [$w_but => "Invoke"]);
379 } else {
380 $w_but = $w_bot->Button(
381 $btnopt => $blshow,
382 -command => [
383 sub {
384 $_[0]->{'selected_button'} = $_[1];
385 }, $cw, $bl,
386 ]
387 );
388 }
389 if ($useBalloon && $btnopt eq '-image') {
390
391 $balloon = $cw->Balloon();
392 $balloon->attach($w_but, -state => 'balloon', -balloonmsg => $blshow);
393 }
394 if ($bl eq $default_button) {
395 $w_default_button = $w_bot->Frame(
396 -relief => 'sunken',
397 -borderwidth => 1
398 );
399 $w_but->raise($w_default_button);
400 $w_default_button->pack(@pl, -expand => 1, @$pad2);
401 $w_but->pack(-in => $w_default_button, -padx => '2m',
402 -pady => '2m'
403 );
404  
405 $cw->{'default_button'} = $w_but;
406 goto JWT_SKIP1;
407 $cw->bind(
408 '<Return>' => [
409 sub {
410 $_[1]->flash;
411 $_[2]->{'selected_button'} = $_[3];
412 }, $w_but, $cw, $bl,
413 ]
414 );
415 JWT_SKIP1:
416 } else {
417 $w_but->pack(@pl, -expand => 1, @$pad2);
418 $cw->{'default_button'} = $w_but unless(defined($cw->{'default_button'}));
419 }
420 if (defined($escape_button) && $bl eq $escape_button) {
421 $cw->{'escape_button'} = $w_but;
422 $cw->bind('<Escape>' => [$w_but => "Invoke"]);
423 }
424 ++$i;
425 } # end for all buttons
426  
427 $cw->Advertise(message => $w_msg);
428 $cw->Advertise(bitmap => $w_bitmap );
429 #!!!$cw->{'default_button'} = $w_default_button;
430 if ($^O =~ /Win/i) {
431 $cw->ConfigSpecs(
432 -image => ['bitmap',undef,undef,undef],
433 -bitmap => ['bitmap',undef,undef,undef],
434 -fg => ['ADVERTISED','foreground','Foreground','black'],
435 -foreground => ['ADVERTISED','foreground','Foreground','black'],
436 -bg => ['DESCENDANTS','background','Background',undef],
437 -background => ['DESCENDANTS','background','Background',undef],
438 -font => ['message','font','Font','{MS Sans} 14'],
439 DEFAULT => ['message',undef,undef,undef]
440 );
441 } else {
442 $cw->ConfigSpecs(
443 -image => ['bitmap',undef,undef,undef],
444 -bitmap => ['bitmap',undef,undef,undef],
445 -fg => ['ADVERTISED','foreground','Foreground','black'],
446 -foreground => ['ADVERTISED','foreground','Foreground','black'],
447 -bg => ['DESCENDANTS','background','Background',undef],
448 -background => ['DESCENDANTS','background','Background',undef],
449 # JWT for TNT! -font => ['message','font','Font','-*-Times-Medium-R-Normal-*-180-*-*-*-*-*-*'],
450 #x -font => ['message','font','Font','-adobe-helvetica-bold-r-normal--17-120-100-100-p-92-iso8859-1'],
451 -font => ['message','font','Font','Helvetica -17 bold'],
452 DEFAULT => ['message',undef,undef,undef]
453 );
454 }
455 } # end Dialog constructor
456  
457 sub Show { # Dialog object public method - display the dialog.
458  
459 my ($cw, $grab_type) = @_;
460  
461 croak "Dialog: `show' method requires at least 1 argument"
462 if scalar @_ < 1 ;
463  
464 my $old_grab = $cw->grabSave;
465  
466 # Update all geometry information, center the dialog in the display
467 # and deiconify it
468  
469 $cw->Popup();
470  
471 # set a grab and claim the focus.
472 if (defined $cw->{'default_button'}) {
473 $cw->{'default_button'}->focus;
474 } else {
475 $cw->focus;
476 }
477 unless (!defined($ENV{DESKTOP_SESSION}) || $ENV{DESKTOP_SESSION} =~ /kde/o) {
478 if (defined $grab_type && length $grab_type) {
479 $cw->grab($grab_type) if ($grab_type !~ /no/io); #JWT: ADDED 20010517 TO ALLOW NON-GRABBING!
480 } else {
481 $cw->grab;
482 }
483 }
484 $cw->update;
485  
486 # Wait for the user to respond, restore the focus and grab, withdraw
487 # the dialog and return the label of the selected button.
488  
489 $cw->waitVariable(\$cw->{'selected_button'});
490 $cw->grabRelease;
491 $cw->withdraw;
492 #DIALOG (WINDOW) IS POPPED UP SHORTLY AFTERWARDS!
493 &$old_grab;
494 return $cw->{'selected_button'};
495  
496 } # end Dialog show method
497  
498 1; # End of Tk::JDialog
↑ Summary