File Coverage

blib/lib/Tk/JDialog.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             package Tk::JDialog;
2              
3 1     1   16274 use 5.006;
  1         3  
  1         47  
4 1     1   5 use strict;
  1         1  
  1         34  
5 1     1   4 use warnings;
  1         6  
  1         93  
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.01';
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 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 > that
106             the user can also press to select the button, for example:
107             "~Ok" means select this button if the user presses >.
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( [ -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 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<< >>
182              
183             =head1 BUGS
184              
185             Please report any bugs or feature requests to C, or through
186             the web interface at L. I will be notified,
187             and then you'll automatically be notified of progress on your bug as I make changes.
188              
189             =head1 SUPPORT
190              
191             You can find documentation for this module with the perldoc command.
192              
193             perldoc Tk::JDialog
194              
195              
196             You can also look for information at:
197              
198             =over 4
199              
200             =item * RT: CPAN's request tracker (report bugs here)
201              
202             L
203              
204             =item * AnnoCPAN: Annotated CPAN documentation
205              
206             L
207              
208             =item * CPAN Ratings
209              
210             L
211              
212             =item * Search CPAN
213              
214             L
215              
216             =back
217              
218              
219             =head1 ACKNOWLEDGEMENTS
220              
221             Tk::JDialog derived from the L wiget from Tcl/Tk to TkPerl (based on
222             John Stoffel's idea). It addes the options: -escape_button, images,
223              
224             =head1 LICENSE AND COPYRIGHT
225              
226             Copyright 2015 Jim Turner.
227              
228             This program is free software; you can redistribute it and/or
229             modify it under the terms of the GNU Lesser General Public
230             License as published by the Free Software Foundation; either
231             version 2.1 of the License, or (at your option) any later version.
232              
233             This program is distributed in the hope that it will be useful,
234             but WITHOUT ANY WARRANTY; without even the implied warranty of
235             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
236             Lesser General Public License for more details.
237              
238             You should have received a copy of the GNU Lesser General Public
239             License along with this program; if not, write to the Free
240             Software Foundation, Inc.,
241             51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
242              
243             =head1 SEE ALSO
244              
245             L, L, L, L
246              
247             =cut
248              
249             # JDialog - a translation of `tk_dialog' from Tcl/Tk to TkPerl (based on
250             # John Stoffel's idea).
251             #
252             # Modified 2/13/97 by Jim Turner of Computer Sciences Corporation to
253             # add underline character (alt-key) activation of buttons, fix bug in the
254             # bindings for key where default button always activated even if
255             # another button had the keyboard focus. Now, the default button starts
256             # with the input focus!!!
257             #
258             # Jim Turner also added the "escape_button" option on 2/14/97 to allow
259             # programmer to specify a button to invoke if user presses the key!
260             # Jim Turner also added the "images" option on 2/14/97 to allow programmer
261             # to specify gifs in leu of text for the buttons.
262             #
263             # Jim Turner also removed the "wraplength" option on 2/19/97 to allow
264             # longer label strings (>3") to not be broken. User can specify -wraplength!
265             # Stephen O. Lidie, Lehigh University Computing Center. 94/12/27
266             # lusol@Lehigh.EDU
267             #
268             # 04/22/97 Jim Turner fixed bug where screen completely locks up if the calling
269             # script invokes a Motif app (ie. xv or another Perl/Tk app) shortly after
270             # calling this dialog box. Did not seem to adversely effect keyboard focus.
271             # fixed by commenting out 1 line of code (&$old_focus);
272             #
273             # This is an OO implementation of `tk_dialog'. First, create all your Dialog
274             # objects during program initialization. When it's time to use a dialog,
275             # invoke the `show' method on a dialog object; the method then displays the
276             # dialog, waits for a button to be invoked, and returns the text label of the
277             # selected button.
278             #
279             # A Dialog object essentially consists of two subwidgets: a Label widget for
280             # the bitmap and a Label wigdet for the text of the dialog. If required, you
281             # can invoke the `configure' method to change any characteristic of these
282             # subwidgets.
283             #
284             # Because a Dialog object is a Toplevel widget all the 'composite' base class
285             # methods are available to you.
286              
287 1     1   4 use Carp;
  1         1  
  1         86  
288             #use strict qw(vars);
289             our $useBalloon;
290 1     1   268 use Tk ":eventtypes";
  0            
  0            
291             use Tk::Balloon; $useBalloon = 1;
292             require Tk::Toplevel;
293              
294             @Tk::JDialog::ISA = qw(Tk::Toplevel);
295              
296             Tk::Widget->Construct('JDialog');
297              
298             sub Populate
299             {
300              
301             # Dialog object constructor. Uses `new' method from base class
302             # to create object container then creates the dialog toplevel.
303              
304             my($cw, $args) = @_;
305              
306             $cw->SUPER::Populate($args);
307              
308             my ($w_bitmap,$w_but,$pad1,$pad2,$underlinepos,$mychar,$blshow,$i);
309             my ($btnopt,$undopt,$balloon);
310              
311             my $buttons = delete $args->{'-buttons'};
312             my $images = delete $args->{'-images'};
313             $buttons = ['OK'] unless (defined $buttons);
314             my $default_button = delete $args->{-default_button};
315             my $escape_button = delete $args->{-escape_button};
316             my $noballoons = delete $args->{-noballoons};
317             $useBalloon = 0 if ($noballoons);
318             $default_button = $buttons->[0] unless (defined $default_button);
319              
320             # Create the Toplevel window and divide it into top and bottom parts.
321              
322             $cw->{'selected_button'} = '';
323             my (@pl) = (-side => 'top', -fill => 'both');
324             ($pad1, $pad2) =
325             ([-padx => '3m', -pady => '3m'], [-padx => '3m', -pady => '2m']);
326              
327             $cw->withdraw;
328             $cw->iconname('JDialog');
329             $cw->protocol('WM_DELETE_WINDOW' => sub {});
330             #????????????????? $cw->transient($cw->toplevel) unless ($^O =~ /Win/i);
331              
332             my $w_top = $cw->Frame(Name => 'top',-relief => 'raised', -borderwidth => 1);
333             my $w_bot = $cw->Frame(Name => 'bot',-relief => 'raised', -borderwidth => 1);
334             $w_top->pack(@pl);
335             $w_bot->pack(@pl);
336              
337             # Fill the top part with the bitmap and message.
338              
339             @pl = (-side => 'left');
340              
341             $w_bitmap = $w_top->Label(Name => 'bitmap');
342             $w_bitmap->pack(@pl, @$pad1);
343             my $w_msg = $w_top->Label(
344             #-wraplength => '3i', --!!! Removed 2/19 by Jim Turner
345             -justify => 'left' );
346              
347             $w_msg->pack(-side => 'right', -expand => 1, -fill => 'both', @$pad1);
348              
349             # Create a row of buttons at the bottom of the dialog.
350              
351             my($w_default_button, $bl) = (undef, '');
352             $cw->{'default_button'} = undef;
353             $cw->{'escape_button'} = undef;
354             $i = 0;
355             foreach $bl (@$buttons) {
356              
357             $blshow = $bl;
358             $underlinepos = ($blshow =~ s/^(.*)~/$1/) ? length($1): undef;
359             if (defined($$images[$i]) && $$images[$i] gt ' ' && -e $$images[$i])
360             {
361             $cw->Photo($blshow, -file => $$images[$i]);
362             $btnopt = '-image';
363             }
364             else
365             {
366             $btnopt = '-text';
367             }
368             if (defined($underlinepos))
369             {
370             $mychar = substr($blshow,$underlinepos,1);
371             $w_but = $w_bot->Button(
372             $btnopt => $blshow,
373             -underline => $underlinepos,
374             -command => [
375             sub {
376             $_[0]->{'selected_button'} = $_[1];
377             }, $cw, $bl,
378             ]
379             );
380             $cw->bind("", [$w_but => "Invoke"]);
381             $cw->bind("", [$w_but => "Invoke"]);
382             }
383             else
384             {
385             $w_but = $w_bot->Button(
386             $btnopt => $blshow,
387             -command => [
388             sub {
389             $_[0]->{'selected_button'} = $_[1];
390             }, $cw, $bl,
391             ]
392             );
393             }
394             if ($useBalloon && $btnopt eq '-image')
395             {
396             $balloon = $cw->Balloon();
397             $balloon->attach($w_but, -state => 'balloon', -balloonmsg => $blshow);
398             }
399             if ($bl eq $default_button) {
400             $w_default_button = $w_bot->Frame(
401             -relief => 'sunken',
402             -borderwidth => 1
403             );
404             $w_but->raise($w_default_button);
405             $w_default_button->pack(@pl, -expand => 1, @$pad2);
406             $w_but->pack(-in => $w_default_button, -padx => '2m',
407             -pady => '2m');
408             $cw->{'default_button'} = $w_but;
409             goto JWT_SKIP1;
410             $cw->bind(
411             '' => [
412             sub {
413             $_[1]->flash;
414             $_[2]->{'selected_button'} = $_[3];
415             }, $w_but, $cw, $bl,
416             ]
417             );
418             JWT_SKIP1:
419             } else {
420             $w_but->pack(@pl, -expand => 1, @$pad2);
421             $cw->{'default_button'} = $w_but unless(defined($cw->{'default_button'}));
422             }
423             if ($bl eq $escape_button)
424             {
425             $cw->{'escape_button'} = $w_but;
426             $cw->bind('' => [$w_but => "Invoke"]);
427             }
428             ++$i;
429             } # forend all buttons
430              
431             $cw->Advertise(message => $w_msg);
432             $cw->Advertise(bitmap => $w_bitmap );
433             #!!!$cw->{'default_button'} = $w_default_button;
434              
435             if ($^O =~ /Win/i)
436             {
437             $cw->ConfigSpecs(
438             -image => ['bitmap',undef,undef,undef],
439             -bitmap => ['bitmap',undef,undef,undef],
440             -fg => ['ADVERTISED','foreground','Foreground','black'],
441             -foreground => ['ADVERTISED','foreground','Foreground','black'],
442             -bg => ['DESCENDANTS','background','Background',undef],
443             -background => ['DESCENDANTS','background','Background',undef],
444             DEFAULT => ['message',undef,undef,undef]
445             );
446             }
447             else
448             {
449             $cw->ConfigSpecs(
450             -image => ['bitmap',undef,undef,undef],
451             -bitmap => ['bitmap',undef,undef,undef],
452             -fg => ['ADVERTISED','foreground','Foreground','black'],
453             -foreground => ['ADVERTISED','foreground','Foreground','black'],
454             -bg => ['DESCENDANTS','background','Background',undef],
455             -background => ['DESCENDANTS','background','Background',undef],
456             # JWT for TNT! -font => ['message','font','Font','-*-Times-Medium-R-Normal-*-180-*-*-*-*-*-*'],
457             -font => ['message','font','Font','-adobe-helvetica-bold-r-normal--17-120-100-100-p-92-iso8859-1'],
458             DEFAULT => ['message',undef,undef,undef]
459             );
460             }
461             } # end Dialog constructor
462              
463             sub Show {
464              
465             # Dialog object public method - display the dialog.
466              
467             my ($cw, $grab_type) = @_;
468              
469             croak "Dialog: `show' method requires at least 1 argument"
470             if scalar @_ < 1 ;
471              
472             my $old_focus = $cw->focusSave; # don't need (Jim Turner) after fixing BUG!
473             my $old_grab = $cw->grabSave;
474              
475             # Update all geometry information, center the dialog in the display
476             # and deiconify it
477              
478             $cw->Popup();
479              
480             # set a grab and claim the focus.
481              
482             if (defined $cw->{'default_button'})
483             {
484             $cw->{'default_button'}->focus;
485             }
486             else
487             {
488             $cw->focus;
489             }
490             if ($ENV{DESKTOP_SESSION} =~ /AfterStep version 2.2.1[2-9]/io) { #JWT:ADDED 20140606 B/C TO GET AFTERSTEP TO GIVE "TRANSIENT" WINDOWS THE FOCUS?!
491             # no strict 'subs';
492             Tk::Event::DoOneEvent(ALL_EVENTS);
493             select(undef, undef, undef, 0.25); #FANCY QUICK-NAP FUNCTION!
494             }
495             unless ($ENV{DESKTOP_SESSION} =~ /kde/o)
496             {
497             if (defined $grab_type && length $grab_type) {
498             $cw->grab($grab_type) if ($grab_type !~ /no/io); #JWT: ADDED 20010517 TO ALLOW NON-GRABBING!
499             } else {
500             $cw->grab;
501             }
502             }
503             ############## $cw->waitVisibility; #SEEMS TO HANG ON NEWER TK'S.
504             $cw->update;
505              
506             # Wait for the user to respond, restore the focus and grab, withdraw
507             # the dialog and return the label of the selected button.
508              
509             $cw->waitVariable(\$cw->{'selected_button'});
510             $cw->grabRelease;
511             $cw->withdraw;
512             &$old_focus if ($ENV{DESKTOP_SESSION} =~ /AfterStep version 2.2.1[2-9]/io); #FIXED BUG CAUSING COMPLETE SCREEN LOCKUP IF ANOTHER
513             #MOTIF APP (WINDOW) IS POPPED UP SHORTLY AFTERWARDS!
514             &$old_grab;
515             return $cw->{'selected_button'};
516              
517             } # end Dialog show method
518              
519             1; # End of Tk::JDialog