| 1 |
package Tk::ErrorDialog; |
| 2 |
|
| 3 |
use vars qw($VERSION); |
| 4 |
#$VERSION = sprintf '4.%03d', q$Revision: #7 $ =~ /\D(\d+)\s*$/; |
| 5 |
$VERSION = '4.008'; |
| 6 |
|
| 7 |
use Tk (); |
| 8 |
require Tk::Dialog; |
| 9 |
use base qw(Tk::Toplevel); |
| 10 |
|
| 11 |
|
| 12 |
# ErrorDialog - a translation of bgerror() from Tcl/Tk to Perl/Tk. |
| 13 |
# |
| 14 |
# Currently TkPerl background errors are sent to stdout/stderr; use this |
| 15 |
# module if you want them in a window. You can also "roll your own" by |
| 16 |
# supplying the routine Tk::Error. |
| 17 |
|
| 18 |
use strict; |
| 19 |
|
| 20 |
Construct Tk::Widget 'ErrorDialog'; |
| 21 |
|
| 22 |
my %options = ( -buttons => ['OK', 'Skip Messages', 'Stack trace'], |
| 23 |
-bitmap => 'error' |
| 24 |
); |
| 25 |
my %ED_OBJECT; # ErrorDialog per MainWindow (singleton) |
| 26 |
|
| 27 |
sub import |
| 28 |
{ |
| 29 |
my $class = shift; |
| 30 |
while (@_) |
| 31 |
{ |
| 32 |
my $key = shift; |
| 33 |
my $val = shift; |
| 34 |
$options{$key} = $val; |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
sub Populate { |
| 39 |
|
| 40 |
# ErrorDialog constructor. Uses `new' method from base class |
| 41 |
# to create object container then creates the dialog toplevel and the |
| 42 |
# traceback toplevel. |
| 43 |
|
| 44 |
my($cw, $args) = @_; |
| 45 |
|
| 46 |
my $mw = $cw->MainWindow; |
| 47 |
my $dr = $mw->Dialog( |
| 48 |
-title => 'Error in '.$mw->name, |
| 49 |
-text => 'on-the-fly-text', |
| 50 |
-bitmap => $options{'-bitmap'}, |
| 51 |
-buttons => $options{'-buttons'}, |
| 52 |
); |
| 53 |
$cw->minsize(1, 1); |
| 54 |
$cw->title('Stack Trace for Error'); |
| 55 |
$cw->iconname('Stack Trace'); |
| 56 |
my $t_ok = $cw->Button( |
| 57 |
-text => 'OK', |
| 58 |
-command => [ |
| 59 |
sub { |
| 60 |
shift->withdraw; |
| 61 |
}, $cw, |
| 62 |
] |
| 63 |
); |
| 64 |
my $t_text = $cw->Text( |
| 65 |
-relief => 'sunken', |
| 66 |
-bd => 2, |
| 67 |
-setgrid => 'true', |
| 68 |
-width => 60, |
| 69 |
-height => 20, |
| 70 |
); |
| 71 |
my $t_scroll = $cw->Scrollbar( |
| 72 |
-relief => 'sunken', |
| 73 |
-command => ['yview', $t_text], |
| 74 |
); |
| 75 |
$t_text->configure(-yscrollcommand => ['set', $t_scroll]); |
| 76 |
$t_ok->pack(-side => 'bottom', -padx => '3m', -pady => '2m'); |
| 77 |
$t_scroll->pack(-side => 'right', -fill => 'y'); |
| 78 |
$t_text->pack(-side => 'left', -expand => 'yes', -fill => 'both'); |
| 79 |
$cw->withdraw; |
| 80 |
|
| 81 |
$cw->Advertise(error_dialog => $dr); # advertise dialog widget |
| 82 |
$cw->Advertise(text => $t_text); # advertise text widget |
| 83 |
$cw->ConfigSpecs(-cleanupcode => [PASSIVE => undef, undef, undef], |
| 84 |
-appendtraceback => [ PASSIVE => undef, undef, 1 ]); |
| 85 |
$ED_OBJECT{$mw} = $cw; |
| 86 |
$cw->protocol('WM_DELETE_WINDOW' => sub {$cw->withdraw}); |
| 87 |
return $cw; |
| 88 |
|
| 89 |
} # end Populate |
| 90 |
|
| 91 |
sub Tk::Error { |
| 92 |
|
| 93 |
# Post a dialog box with the error message and give the user a chance |
| 94 |
# to see a more detailed stack trace. |
| 95 |
|
| 96 |
my($w, $error, @msgs) = @_; |
| 97 |
|
| 98 |
my $grab = $w->grab('current'); |
| 99 |
$grab->Unbusy if (defined $grab); |
| 100 |
|
| 101 |
my $mw = $w->MainWindow; |
| 102 |
my $ed = $ED_OBJECT{$mw} || $w->ErrorDialog; |
| 103 |
|
| 104 |
my($d, $t) = ($ed->Subwidget('error_dialog'), $ed->Subwidget('text')); |
| 105 |
|
| 106 |
$d->configure(-text => "Error: $error"); |
| 107 |
$d->bell; |
| 108 |
$mw->deiconify if $mw->state ne 'normal'; |
| 109 |
my $ans = $d->Show; |
| 110 |
|
| 111 |
$t->delete('0.0', 'end') if not $ed->{'-appendtraceback'}; |
| 112 |
$t->insert('end', "\n"); |
| 113 |
$t->mark('set', 'ltb', 'end'); |
| 114 |
$t->insert('end', "--- Begin Traceback ---\n$error\n"); |
| 115 |
my $msg; |
| 116 |
for $msg (@msgs) { |
| 117 |
$t->insert('end', "$msg\n"); |
| 118 |
} |
| 119 |
$t->yview('ltb'); |
| 120 |
|
| 121 |
$ed->deiconify if ($ans =~ /trace/i); |
| 122 |
|
| 123 |
my $c = $ed->{Configure}{'-cleanupcode'}; |
| 124 |
&$c if defined $c; # execute any cleanup code if it was defined |
| 125 |
$w->break if ($ans =~ /skip/i); |
| 126 |
|
| 127 |
} # end Tk::Error |
| 128 |
|
| 129 |
1; |