home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / Share / Editores / Perl5 / perl / lib / site / Tk / ErrorDialog.pm < prev    next >
Encoding:
Perl POD Document  |  1997-08-10  |  4.6 KB  |  158 lines

  1. # ErrorDialog - a translation of `tkerror' from Tcl/Tk to TkPerl.
  2. #
  3. # Currently TkPerl background errors are sent to stdout/stderr; use this
  4. # module if you want them in a window.  You can also "roll your own" by 
  5. # supplying the routine Tk::Error.
  6. #
  7. # Stephen O. Lidie, Lehigh University Computing Center.  95/03/02
  8. # lusol@Lehigh.EDU
  9. #
  10. # This is an OO implementation of `tkerror', with a twist:  since there is
  11. # only one ErrorDialog object, you aren't required to invoke the constructor
  12. # to create it; it will be created automatically when the first background
  13. # error occurs.  However, in order to configure the ErrorDialog object you
  14. # must call the constructor and create it manually.
  15. #
  16. # The ErrorDialog object essentially consists of two subwidgets: an
  17. # ErrorDialog widget to display the background error and a Text widget for the
  18. # traceback information.  If required, you can invoke the configure() method
  19. # to change some characteristics of these subwidgets.
  20. #
  21. # Because an ErrorDialog object is a Frame widget all the composite base
  22. # class methods are available to you.
  23. #
  24. # Advertised widgets:  error_dialog, text.
  25. #
  26. # 1) Call the constructor to create the ErrorDialog object, which in turn
  27. #    returns a blessed reference to the new object:
  28. #
  29. #    require Tk::ErrorDialog;
  30. #
  31. #    $ED = $mw->ErrorDialog(
  32. #        -cleanupcode     => $code,
  33. #        -appendtraceback => $bool,
  34. #    );
  35. #
  36. #       mw -   a window reference, usually the result of a MainWindow->new
  37. #              call.
  38. #       code - a CODE reference if special post-background error processing
  39. #              is required (default is undefined).
  40. #       bool - a boolean indicating whether or not to append successive
  41. #              tracebacks (default is 1, do append).
  42. #
  43.  
  44. package Tk::ErrorDialog;
  45. use English;
  46. use Tk ();
  47. require Tk::Dialog;
  48. @Tk::ErrorDialog::ISA = qw(Tk::Toplevel);
  49.  
  50. Construct Tk::Widget 'ErrorDialog';
  51.  
  52. my %options = ( -buttons => ['OK', 'Skip Messages', 'Stack trace'],
  53.                 -bitmap  => 'error'
  54.               );
  55. my $ED_OBJECT;
  56.  
  57. sub import
  58. {
  59.  my $class = shift;
  60.  while (@_)
  61.   {
  62.    my $key = shift;
  63.    my $val = shift;
  64.    $options{$key} = $val;
  65.   }
  66. }
  67.  
  68. sub Populate {
  69.  
  70.     # ErrorDialog constructor.  Uses `new' method from base class
  71.     # to create object container then creates the dialog toplevel and the
  72.     # traceback toplevel.
  73.  
  74.     my($cw, $args) = @ARG;
  75.  
  76.     my $dr = $cw->Dialog(
  77.         -title          => 'Error in '.$cw->MainWindow->name,
  78.         -text           => 'on-the-fly-text',
  79.         -bitmap         => $options{'-bitmap'},
  80.     -buttons        => $options{'-buttons'},
  81.     );
  82.     $cw->minsize(1, 1);
  83.     $cw->title('Stack Trace for Error');
  84.     $cw->iconname('Stack Trace');
  85.     my $t_ok = $cw->Button(
  86.         -text    => 'OK',
  87.         -command => [
  88.             sub {
  89.         shift->withdraw;
  90.         }, $cw,
  91.         ]
  92.     );
  93.     my $t_text = $cw->Text(
  94.         -relief  => 'sunken',
  95.         -bd      => 2,
  96.         -setgrid => 'true',
  97.         -width   => 60,
  98.         -height  => 20,
  99.     );
  100.     my $t_scroll = $cw->Scrollbar(
  101.         -relief => 'sunken',
  102.         -command => ['yview', $t_text],
  103.     );
  104.     $t_text->configure(-yscrollcommand => ['set', $t_scroll]);
  105.     $t_ok->pack(-side => 'bottom', -padx => '3m', -pady => '2m');
  106.     $t_scroll->pack(-side => 'right', -fill => 'y');
  107.     $t_text->pack(-side => 'left', -expand => 'yes', -fill => 'both');
  108.     $cw->withdraw;
  109.  
  110.     $cw->Advertise(error_dialog => $dr); # advertise dialog widget
  111.     $cw->Advertise(text => $t_text);     # advertise text widget
  112.     $cw->ConfigSpecs(-cleanupcode => [PASSIVE, undef, undef, undef],
  113.                      -appendtraceback => [ PASSIVE, undef, undef, 1 ]);
  114.     $ED_OBJECT = $cw;
  115.     return $cw;
  116.  
  117. } # end new, ErrorDialog constructor
  118.  
  119.  
  120. sub Tk::Error {
  121.  
  122.     # Post a dialog box with the error message and give the user a chance
  123.     # to see a more detailed stack trace.
  124.  
  125.     my($w, $error, @msgs) = @ARG;
  126.  
  127.     my $grab = $w->grab('current');
  128.     $grab->Unbusy if (defined $grab);
  129.  
  130.     $w->ErrorDialog if not defined $ED_OBJECT;
  131.  
  132.     my($d, $t) = ($ED_OBJECT->Subwidget('error_dialog'), $ED_OBJECT->Subwidget('text'));
  133.     chop $error;
  134.     $d->configure(-text => "Error:  $error");
  135.     $d->bell; 
  136.     my $ans = $d->Show;
  137.  
  138.     $t->delete('0.0', 'end') if not $ED_OBJECT->{'-appendtraceback'};
  139.     $t->insert('end', "\n");
  140.     $t->mark('set', 'ltb', 'end');
  141.     $t->insert('end', "--- Begin Traceback ---\n$error\n");
  142.     my $msg;
  143.     for $msg (@msgs) {
  144.     $t->insert('end', "$msg\n");
  145.     }
  146.     $t->yview('ltb');
  147.  
  148.     $ED_OBJECT->deiconify if ($ans =~ /trace/i);
  149.  
  150.     my $c = $ED_OBJECT->{Configure}{'-cleanupcode'};
  151.     &$c if defined $c;        # execute any cleanup code if it was defined
  152.     $w->break if ($ans =~ /skip/i);
  153.  
  154. } # end Tk::Error
  155.  
  156.  
  157. 1;
  158.