1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
|
##==============================================================================
## Tk::Stdio - capture program standard output and standard error,
## accept standard input
##==============================================================================
## Tk::Stdio is based on:
##
## Tk::Stderr - capture program standard error output
##==============================================================================
require 5.006;
package Tk::Stdio;
use strict;
use warnings;
use vars qw($VERSION @ISA);
($VERSION) = q$Revision: 1.0 $ =~ /Revision:\s+(\S+)/ or $VERSION = "0.0";
use base qw(Tk::Derived Tk::MainWindow);
use Tk::Text;
use Tk::Frame;
=pod
=head1 NAME
Tk::Stdio - capture standard output and error,
accept standard input,
display in separate window
=head1 SYNOPSIS
use Tk::Stdio;
$mw = MainWindow->new->InitStdio;
print "something\n"; ## goes to standard IO window
print STDERR 'stuff'; ## likewise
warn 'eek!'; ## likewise
my $input = <STDIN>; ## keyboard entry is in standard IO window
my $char = getc; ## likewise
=head1 DESCRIPTION
This module captures the standard output or error of a program and redirects it to a read
only text widget, which doesn't appear until necessary. When it does appear, the
user can close it; it'll appear again when there is more output. Standard input can be
entered in the widget, which becomes temporarily writable.
=cut
$Tk::Stdio::first_char = '1.0'; # 'line.char' set in READLINE or GETC
##==============================================================================
## Populate
##==============================================================================
sub Populate {
my ( $mw, $args ) = @_;
my $private = $mw->privateData;
$private->{ReferenceCount} = 0;
$private->{Enabled} = 0;
$mw->SUPER::Populate($args);
$mw->withdraw;
$mw->protocol( WM_DELETE_WINDOW => [ $mw => 'withdraw' ] );
my $f = $mw->Frame( Name => 'stderr_frame', )->pack( -fill => 'both', -expand => 1 );
my $text = $f->Scrolled( 'Text',
-wrap => 'char',
-scrollbars => 'oe',
-state => 'disabled',
-fg => 'white',
-bg => 'black',
-insertbackground => 'white',
)->pack( -fill => 'both', -expand => 1 );
$text->bind( '<Button>', sub { $text->SetCursor('end') } );
$text->bind( '<Up>', sub { $text->SetCursor('end') } );
$text->bind( '<Down>', sub { $text->SetCursor('end') } );
$text->bind( '<Next>', sub { $text->SetCursor('end') } );
$text->bind( '<Prior>', sub { $text->SetCursor('end') } );
$text->bind( '<Home>', sub { $text->SetCursor($Tk::Stdio::first_char) } );
$text->bind( 'Tk::Text', '<Left>',
sub {
my $cursor = $text->index('insert');
$text->SetCursor("$cursor -1 chars") if $cursor > $Tk::Stdio::first_char;
}
);
$text->bind( 'Tk::Text', '<Return>',
sub { $text->insert( 'end', "\n" ); $text->SetCursor('end'); }
);
$mw->Advertise( 'text' => $text );
$mw->ConfigSpecs( '-title' => [ qw/METHOD title Title/, 'Standard IO' ], );
$mw->Redirect(1);
return $mw;
} ## end sub Populate
##==============================================================================
## Redirect
##==============================================================================
sub Redirect {
my ( $mw, $boolean ) = @_;
my $private = $mw->privateData;
my $old = $private->{Enabled};
if ( $old && !$boolean ) {
untie *STDIN;
untie *STDOUT;
untie *STDERR;
$SIG{__WARN__} = 'DEFAULT';
}
elsif ( !$old && $boolean ) {
tie *STDIN, 'Tk::Stdio::Handle', $mw;
tie *STDOUT, 'Tk::Stdio::Handle', $mw;
tie *STDERR, 'Tk::Stdio::Handle', $mw;
$SIG{__WARN__} = sub { print STDERR @_ };
}
$private->{Enabled} = $boolean;
return $old;
}
##==============================================================================
## DecrementReferenceCount
##==============================================================================
sub DecrementReferenceCount {
my ($mw) = @_;
my $private = $mw->privateData;
if ( --$private->{ReferenceCount} <= 0 ) {
$mw->destroy;
}
}
##==============================================================================
## IncrementReferenceCount
##==============================================================================
sub IncrementReferenceCount {
my ($mw) = @_;
my $private = $mw->privateData;
++$private->{ReferenceCount};
}
=pod
=head1 METHODS
These are actually added to the MainWindow class.
=over 4
=item I<$mw>->InitStdio;
The first time this method called, it does the following things:
=over 4
=item o
Creates a MainWindow holding a read-only scrollable text widget, and withdraws
this window until it's actually needed.
=item o
Ties STDOUT, STDERR and STDIN to a special handle that adds the output to this text widget.
=item o
Installs a C<< $SIG{__WARN__} >> handler that redirects the output from B<warn>
to this window as well (by printing it to STDERR).
=back
On the remaining calls, it:
=over 4
=item o
Increments a reference count of "other" MainWindows.
=item o
Installs an OnDestroy handler that decrements this reference count, so that it
can detect when it's the only MainWindow left and destroy itself.
=back
=cut
package MainWindow;
use strict;
use warnings;
my $io_window;
##==============================================================================
## InitStdio
##==============================================================================
sub InitStdio {
my ( $mw, $title ) = @_;
unless ( defined $io_window ) {
$io_window = Tk::Stdio->new;
$io_window->title($title) if defined $title;
}
$io_window->IncrementReferenceCount;
$mw->OnDestroy( [ 'DecrementReferenceCount' => $io_window ] );
return $mw;
}
=pod
=item I<$iowin> = I<$mw>->StdioWindow;
Returns a reference to the main window holding the text. You can use this to
configure the window itself or the widgets it contains. The only advertised
subwidget is 'text', which is the scrolled read-only text widget.
=cut
##==============================================================================
## StdioWindow
##==============================================================================
sub StdioWindow {
return $io_window;
}
=pod
=item I<$old> = I<$mw>->RedirectStdio(I<$boolean>);
Enables or disables the redirection of standard output and error to the text window.
Set I<$boolean> to true to enable redirection, false to disable it. Returns the
previous value of the enabled flag.
If B<InitStdio> has never been called, this routine will call it if I<$boolean>
is true.
=cut
##==============================================================================
## RedirectStdio
##==============================================================================
sub RedirectStdio {
my ( $mw, $boolean ) = @_;
unless ( defined $io_window ) {
$mw->InitStdio if $boolean;
return;
}
return $io_window->Redirect($boolean);
}
=pod
=back
=head1 AUTHOR
Alan Stewart <F<astewart1>@F<cox>.F<net>>
based on Tk::Stderr by Kevin Michael Vail <F<kevin>@F<vaildc>.F<net>>
=cut
##==============================================================================
## Define the handle that actually implements things.
##==============================================================================
BEGIN {
package Tk::Stdio::Handle;
use strict;
use warnings;
##==========================================================================
## TIEHANDLE
##==========================================================================
sub TIEHANDLE {
my ( $class, $window ) = @_;
bless \$window, $class;
}
##==========================================================================
## PRINT
##==========================================================================
sub PRINT {
my $window = shift;
my $text = $$window->Subwidget('text');
$text->configure( -state => 'normal' );
$text->insert( 'end', $_ ) foreach (@_);
$text->configure( -state => 'disabled' );
$text->see('end');
$text->SetCursor('end');
$$window->deiconify;
$$window->raise;
$$window->focus;
}
##==========================================================================
## PRINTF
##==========================================================================
sub PRINTF {
my ( $window, $format ) = splice @_, 0, 2;
$window->PRINT( sprintf $format, @_ );
}
##==========================================================================
## READLINE
##==========================================================================
sub READLINE {
my $window = shift;
my $text = $$window->Subwidget('text');
$text->see('end');
$$window->deiconify;
$$window->raise;
$text->focus;
$Tk::Stdio::first_char = $text->index('insert');
my $next_line = int( $Tk::Stdio::first_char + 1 );
$text->configure( -state => 'normal' );
$text->update until $text->index('insert') == $next_line;
$text->configure( -state => 'disabled' );
return $text->get( $Tk::Stdio::first_char, $text->index('insert') );
}
##==========================================================================
## GETC
##==========================================================================
sub GETC {
my $window = shift;
my $text = $$window->Subwidget('text');
$text->see('end');
$$window->deiconify;
$$window->raise;
$text->focus;
$Tk::Stdio::first_char = $text->index('insert');
$text->configure( -state => 'normal' );
$text->update until $text->index('insert') > $Tk::Stdio::first_char;
$text->configure( -state => 'disabled' );
return $text->get($Tk::Stdio::first_char);
}
}
1;
##==============================================================================
## Stdio.pm
## Revision 1.0 2004/07/26 12:00:00 astewart
##==============================================================================
## Stdio.pm is based on:
##
## $Log: Stderr.pm,v $
## Revision 1.2 2003/04/01 03:58:42 kevin
## Add RedirectStderr method to allow redirection to be switched on and off.
##
## Revision 1.1 2003/03/26 21:48:43 kevin
## Fix dependencies in Makefile.PL
##
## Revision 1.0 2003/03/26 19:11:32 kevin
## Initial revision
##==============================================================================
|