| 1 |
# |
| 2 |
# DialogBox is similar to Dialog except that it allows any widget |
| 3 |
# in the top frame. Widgets can be added with the add method. Currently |
| 4 |
# there exists no way of deleting a widget once it has been added. |
| 5 |
|
| 6 |
package Tk::DialogBox; |
| 7 |
|
| 8 |
use strict; |
| 9 |
use Carp; |
| 10 |
|
| 11 |
use vars qw($VERSION); |
| 12 |
$VERSION = '4.016'; # was: sprintf '4.%03d', q$Revision: #13 $ =~ /\D(\d+)\s*$/; |
| 13 |
|
| 14 |
use base qw(Tk::Toplevel); |
| 15 |
|
| 16 |
Tk::Widget->Construct('DialogBox'); |
| 17 |
|
| 18 |
sub Populate { |
| 19 |
my ($cw, $args) = @_; |
| 20 |
|
| 21 |
$cw->SUPER::Populate($args); |
| 22 |
my $buttons = delete $args->{'-buttons'}; |
| 23 |
$buttons = ['OK'] unless defined $buttons; |
| 24 |
my $default_button = delete $args->{'-default_button'}; |
| 25 |
$default_button = $buttons->[0] unless defined $default_button; |
| 26 |
my $cancel_button = delete $args->{'-cancel_button'}; |
| 27 |
if (!$cancel_button && @$buttons == 1) { |
| 28 |
$cancel_button = $buttons->[0]; |
| 29 |
} |
| 30 |
|
| 31 |
$cw->{'selected_button'} = ''; |
| 32 |
$cw->transient($cw->Parent->toplevel); |
| 33 |
$cw->withdraw; |
| 34 |
if ($cancel_button) { |
| 35 |
$cw->protocol('WM_DELETE_WINDOW' => sub { $cw->{'cancel_button'}->invoke }); |
| 36 |
} else { |
| 37 |
$cw->protocol('WM_DELETE_WINDOW' => sub { $cw->{'selected_button'} = undef }); |
| 38 |
} |
| 39 |
# Make sure waitVariable exits if a waiting dialog is destroyed |
| 40 |
$cw->OnDestroy(sub { $cw->{'selected_button'} = $cw->{'selected_button'} }); |
| 41 |
|
| 42 |
# create the two frames |
| 43 |
my $top = $cw->Component('Frame', 'top'); |
| 44 |
$top->configure(-relief => 'raised', -bd => 1) unless $Tk::platform eq 'MSWin32'; |
| 45 |
my $bot = $cw->Component('Frame', 'bottom'); |
| 46 |
$bot->configure(-relief => 'raised', -bd => 1) unless $Tk::platform eq 'MSWin32'; |
| 47 |
$bot->pack(qw/-side bottom -fill both -ipady 3 -ipadx 3/); |
| 48 |
$top->pack(qw/-side top -fill both -ipady 3 -ipadx 3 -expand 1/); |
| 49 |
|
| 50 |
# create a row of buttons in the bottom. |
| 51 |
my $bl; # foreach my $var: perl > 5.003_08 |
| 52 |
foreach $bl (@$buttons) |
| 53 |
{ |
| 54 |
my $b = $bot->Button(-text => $bl, -command => sub { $cw->{'selected_button'} = "$bl" } ); |
| 55 |
$b->bind('<Return>' => [ $b, 'Invoke']); |
| 56 |
$cw->Advertise("B_$bl" => $b); |
| 57 |
if ($Tk::platform eq 'MSWin32') |
| 58 |
{ |
| 59 |
$b->configure(-width => 10, -pady => 0); |
| 60 |
} |
| 61 |
if ($bl eq $default_button) { |
| 62 |
my $db = $bot->Frame(-relief => 'sunken', -bd => 1); |
| 63 |
$b->raise($db); |
| 64 |
$b->pack(-in => $db, -padx => '2', -pady => '2'); |
| 65 |
$db->pack(-side => 'left', -expand => 1, -padx => 1, -pady => 1); |
| 66 |
$cw->{'default_button'} = $b; |
| 67 |
$cw->bind('<Return>' => [ $b, 'Invoke']); |
| 68 |
} else { |
| 69 |
$b->pack(-side => 'left', -expand => 1, -padx => 1, -pady => 1); |
| 70 |
} |
| 71 |
if (defined $cancel_button && $bl eq $cancel_button) { |
| 72 |
$cw->{'cancel_button'} = $b; |
| 73 |
} |
| 74 |
} |
| 75 |
if (defined $default_button && !$cw->{'default_button'}) { |
| 76 |
warn "Default button `$default_button' does not exist.\n"; |
| 77 |
} |
| 78 |
if (defined $cancel_button && !$cw->{'cancel_button'}) { |
| 79 |
warn "Cancel button `$cancel_button' does not exist.\n"; |
| 80 |
} |
| 81 |
$cw->ConfigSpecs(-command => ['CALLBACK', undef, undef, undef ], |
| 82 |
-foreground => ['DESCENDANTS', 'foreground','Foreground', 'black'], |
| 83 |
-background => ['DESCENDANTS', 'background','Background', undef], |
| 84 |
-focus => ['PASSIVE', undef, undef, undef], |
| 85 |
-showcommand => ['CALLBACK', undef, undef, undef], |
| 86 |
); |
| 87 |
$cw->Delegates('Construct',$top); |
| 88 |
} |
| 89 |
|
| 90 |
sub add { |
| 91 |
my ($cw, $wnam, @args) = @_; |
| 92 |
my $w = $cw->Subwidget('top')->$wnam(@args); |
| 93 |
$cw->Advertise("\L$wnam" => $w); |
| 94 |
return $w; |
| 95 |
} |
| 96 |
|
| 97 |
sub Wait |
| 98 |
{ |
| 99 |
my $cw = shift; |
| 100 |
$cw->Callback(-showcommand => $cw); |
| 101 |
$cw->waitVariable(\$cw->{'selected_button'}); |
| 102 |
$cw->grabRelease if Tk::Exists($cw); |
| 103 |
$cw->withdraw if Tk::Exists($cw); |
| 104 |
$cw->Callback(-command => $cw->{'selected_button'}); |
| 105 |
} |
| 106 |
|
| 107 |
sub Show { |
| 108 |
|
| 109 |
croak 'DialogBox: "Show" method requires at least 1 argument' |
| 110 |
if scalar @_ < 1; |
| 111 |
my $cw = shift; |
| 112 |
my ($grab) = @_; |
| 113 |
my $old_focus = $cw->focusSave; |
| 114 |
my $old_grab = $cw->grabSave; |
| 115 |
|
| 116 |
shift if defined $grab && length $grab && ($grab =~ /global/); |
| 117 |
$cw->Popup(@_); |
| 118 |
|
| 119 |
Tk::catch { |
| 120 |
if (defined $grab && length $grab && ($grab =~ /global/)) { |
| 121 |
$cw->grabGlobal; |
| 122 |
} else { |
| 123 |
$cw->grab; |
| 124 |
} |
| 125 |
}; |
| 126 |
if (my $focusw = $cw->cget(-focus)) { |
| 127 |
$focusw->focus; |
| 128 |
} elsif (defined $cw->{'default_button'}) { |
| 129 |
$cw->{'default_button'}->focus; |
| 130 |
} else { |
| 131 |
$cw->focus; |
| 132 |
} |
| 133 |
$cw->Wait; |
| 134 |
&$old_focus; |
| 135 |
&$old_grab; |
| 136 |
return $cw->{'selected_button'}; |
| 137 |
} |
| 138 |
|
| 139 |
sub SelectButton |
| 140 |
{ |
| 141 |
my ($w, $button_text) = @_; |
| 142 |
$w->{'selected_button'} = $button_text; |
| 143 |
} |
| 144 |
|
| 145 |
sub Exit |
| 146 |
{ |
| 147 |
my $cw = shift; |
| 148 |
#kill the dialogbox, by faking a 'DONE' |
| 149 |
$cw->{'selected_button'} = $cw->{'default_button'}->cget(-text); |
| 150 |
} |
| 151 |
|
| 152 |
1; |