| 1 | package Tk::Workspace; | ||
| 2 | # Temp version for CPAN | ||
| 3 | $VERSION=1.76; | ||
| 4 | my $RCSRevKey = '$Revision: 1.75 $'; | ||
| 5 | $RCSRevKey =~ /Revision: (.*?) /; | ||
| 6 | $VERSION=$1; | ||
| 7 | |||
| 8 | require Exporter; | ||
| 9 | use Carp; | ||
| 10 | use Env qw( PS1 ); | ||
| 11 | |||
| 12 | use Tk qw(Ev); | ||
| 13 | use Tk::MainWindow; | ||
| 14 | use Tk::WorkspaceText; | ||
| 15 | use Tk::Entry; | ||
| 16 | use Tk::DialogBox; | ||
| 17 | use Tk::Dialog; | ||
| 18 | use Tk::RemoteFileSelect; | ||
| 19 | use Tk::ColorEditor; | ||
| 20 | use Tk::XFontSelect; | ||
| 21 | use Tk::SearchDialog; | ||
| 22 | |||
| 23 | use Tk::Shell qw( VERSION ishell shell_client shell_cmd ); | ||
| 24 | |||
| 25 | use FileHandle; | ||
| 26 | use IO::File; | ||
| 27 | use IPC::Open3; | ||
| 28 | use IPC::Open2; | ||
| 29 | use IO::Select; | ||
| 30 | use Cwd; | ||
| 31 | |||
| 32 | @ISA=qw(Tk::Widget Exporter); | ||
| 33 | |||
| 34 | # Set this to the pathname of the workspace.xpm on your system. | ||
| 35 | my $iconpath = "/home/kiesling/.icons/workspace.xpm"; | ||
| 36 | |||
| 37 | $SIG{WINCH} = \&do_win_signal_event; | ||
| 38 | sub do_win_signal_event { | ||
| 39 | Tk::Event::DoOneEvent(255); | ||
| 40 | $SIG{WINCH} = \&do_win_signal_event; | ||
| 41 | } | ||
| 42 | |||
| 43 | my ($ptk_major_ver, $ptk_minor_ver) = split /\./, $Tk::VERSION; | ||
| 44 | |||
| 45 | if( ( $ptk_major_ver lt '800' ) || ( $ptk_minor_ver lt '015' ) ) { | ||
| 46 | die "Fatal Error: \nThis version of Workspace.pm Requires Perl/Tk 800.022."; | ||
| 47 | } | ||
| 48 | |||
| 49 | my $cmdhelptext = <<'end-of-cmd-help'; | ||
| 50 | |||
| 51 | Usage: workspace [options] | ||
| 52 | |||
| 53 | Options: | ||
| 54 | -background | -bg <color> Menu and dialog background color. | ||
| 55 | -textbackground <color> Background color of text. | ||
| 56 | -foreground | -fg <color> Menu and dialog text color. | ||
| 57 | -textforeground <color> Foreground color of text. | ||
| 58 | -font | -fn <Xfontdesc> X11 font for menus and dialogs. | ||
| 59 | -importfile <filename> Read <filename> into workspace at | ||
| 60 | startup. | ||
| 61 | -exportfile <filename> Write workspace text to <filename>. | ||
| 62 | -dump Display text on console. | ||
| 63 | -class <Classname> Resource class name. | ||
| 64 | -xrm <pattern> Load X resources containing <pattern>. | ||
| 65 | -display | -screen <displayname> Name of X display. | ||
| 66 | -title <workspacename> Name of workspace. | ||
| 67 | -help Display this message. | ||
| 68 | -iconic Iconify window on startup. | ||
| 69 | -motif Use Motif look-and-feel. | ||
| 70 | -synchronous Synchronous communication with X | ||
| 71 | server. For debugging. | ||
| 72 | -write Write workspace to disk. | ||
| 73 | -quit Exit without saving workspace. | ||
| 74 | |||
| 75 | Options can begin with either one (`-'), or two (`--') dashes. | ||
| 76 | |||
| 77 | end-of-cmd-help | ||
| 78 | |||
| 79 | my @Workspaceobject = | ||
| 80 | ('#!/usr/local/bin/perl', | ||
| 81 | 'my $text=\'\';', | ||
| 82 | 'my $geometry=\'565x351+100+100\';', | ||
| 83 | 'my $wrap=\'word\';', | ||
| 84 | 'my $fg=\'black\';', | ||
| 85 | 'my $bg=\'white\';', | ||
| 86 | 'my $name=\'\';', | ||
| 87 | 'my $menuvisible=\'1\';', | ||
| 88 | 'my $scrollbars=\'\';', | ||
| 89 | 'my $insert=\'1.0\';', | ||
| 90 | 'my $font=\'*-courier-medium-r-*-*-12-*"\';', | ||
| 91 | 'use Tk;', | ||
| 92 | 'use Tk::Workspace;', | ||
| 93 | 'use strict;', | ||
| 94 | 'use FileHandle;', | ||
| 95 | 'use Env qw(HOME);', | ||
| 96 | 'my $workspace = Tk::Workspace -> new ( menubarvisible => $menuvisible, ', | ||
| 97 | 'scroll => $scrollbars );', | ||
| 98 | '$workspace -> name($name);', | ||
| 99 | '$workspace -> textfont($font);', | ||
| 100 | '$workspace -> text -> insert ( \'end\', $text );', | ||
| 101 | '$workspace -> text -> configure( -foreground => $fg, -background => $bg, -font => $font, -insertbackground => $fg );', | ||
| 102 | '$workspace -> text -> pack( -fill => \'both\', -expand => \'1\');', | ||
| 103 | 'bless($workspace,\'Tk::Workspace\');', | ||
| 104 | '$workspace -> wrap( $wrap );', | ||
| 105 | '$workspace -> geometry( $geometry, $insert );', | ||
| 106 | '$workspace -> commandline;', | ||
| 107 | 'MainLoop;' ); | ||
| 108 | |||
| 109 | my $defaultbackgroundcolor="white"; | ||
| 110 | my $defaultforegroundcolor="black"; | ||
| 111 | my $defaulttextfont="*-courier-medium-r-*-*-12-*"; | ||
| 112 | my $menufont="*-helvetica-medium-r-*-*-12-*"; | ||
| 113 | my $clipboard; # Internal clipboard. | ||
| 114 | |||
| 115 | sub new { | ||
| 116 | my $proto = shift; | ||
| 117 | my $class = ref( $proto ) || $proto; | ||
| 118 | my @construct_args = @_; | ||
| 119 | my @cmd_args = &custom_args( @ARGV ); | ||
| 120 | my $self = { | ||
| 121 | window => new MainWindow, | ||
| 122 | name => 'workspace', | ||
| 123 | textfont => undef, | ||
| 124 | # default is approximate width and height of 80x24 char. text widget | ||
| 125 | width => undef, | ||
| 126 | height => undef, | ||
| 127 | # x and y origin are not defined until the workspace is | ||
| 128 | # saved again. | ||
| 129 | x => undef, | ||
| 130 | y => undef, | ||
| 131 | foreground => $defaultforegroundcolor, | ||
| 132 | background => $defaultbackgroundcolor, | ||
| 133 | textfont => '*-courier-medium-r-*-*-12-*', | ||
| 134 | filemenu => undef, | ||
| 135 | editmenu => undef, | ||
| 136 | optionsmenu => undef, | ||
| 137 | wrapmenu => undef, | ||
| 138 | scrollmenu => undef, | ||
| 139 | modemenu => undef, | ||
| 140 | helpmenu => undef, | ||
| 141 | exportmenu => undef, | ||
| 142 | encodingmenu => undef, | ||
| 143 | menubar => undef, | ||
| 144 | popupmenu => undef, | ||
| 145 | menubarvisible => undef, | ||
| 146 | scroll => undef, | ||
| 147 | scrollbuttons => undef, | ||
| 148 | insertionpoint => undef, | ||
| 149 | hasnet => undef, | ||
| 150 | importfile => undef, | ||
| 151 | outputmode => undef, | ||
| 152 | outputfile => undef, | ||
| 153 | filter => undef, | ||
| 154 | text => [], | ||
| 155 | cmdargs => (), | ||
| 156 | searchopts => (), # Flattened hash returned from SearchDialog widget. | ||
| 157 | unicode => undef, | ||
| 158 | encoding => undef, | ||
| 159 | filepath => undef | ||
| 160 | }; | ||
| 161 | bless($self, $class); | ||
| 162 | my $i; | ||
| 163 | for( $i = 0; $i < $#construct_args; ) { | ||
| 164 | $self -> {$construct_args[$i]} = $construct_args[$i + 1]; | ||
| 165 | $i += 2; | ||
| 166 | } | ||
| 167 | push @{$self -> {cmdargs}}, @cmd_args; | ||
| 168 | if( &requirecond( "Net::FTP" ) ) { $self -> hasnet('1') } | ||
| 169 | $self -> {window} -> {parent} = $self; | ||
| 170 | $self -> filepath; | ||
| 171 | $self -> {text} = | ||
| 172 | $self -> {window} -> Scrolled( 'WorkspaceText', | ||
| 173 | -font => $defaulttextfont, | ||
| 174 | -background => $defaultbackgroundcolor, | ||
| 175 | -exportselection => 'true', | ||
| 176 | -borderwidth => 0, | ||
| 177 | Name => 'workspaceText' ); | ||
| 178 | if( &requirecond("Unicode::Map") ) { | ||
| 179 | if( &requirecond("Unicode::String") ) { | ||
| 180 | $self -> {hasunicode} = '1'; | ||
| 181 | } | ||
| 182 | } | ||
| 183 | if( -f $iconpath ) { | ||
| 184 | my $icon = | ||
| 185 | $self -> {text} -> toplevel -> Pixmap(-file => $iconpath); | ||
| 186 | $self -> {window} -> toplevel -> iconimage($icon); | ||
| 187 | } | ||
| 188 | &menus( $self ); | ||
| 189 | &set_scroll( $self ); | ||
| 190 | my $t = $self -> text; | ||
| 191 | $t -> Subwidget('yscrollbar') -> configure(-width=>10); | ||
| 192 | $t -> Subwidget('xscrollbar') -> configure(-width=>10); | ||
| 193 | $t -> setFixedTabs ( 5 ); | ||
| 194 | $self -> window -> protocol( WM_TAKE_FOCUS, sub{ $self -> wmgeometry}); | ||
| 195 | # Prevents errors when trying to paste from an empty clipboard. | ||
| 196 | $t -> clipboardAppend( '' ); | ||
| 197 | $self -> focusFollowsMouse; | ||
| 198 | $self -> {encoding} = 'iso88591'; | ||
| 199 | $t -> focus; | ||
| 200 | $t -> markGravity( 'insert', 'right' ); | ||
| 201 | return $self; | ||
| 202 | } | ||
| 203 | |||
| 204 | # Standard X11 toolkit arguments: | ||
| 205 | # Refer to the Tk::CmdLine manual page. | ||
| 206 | # one parameter each | ||
| 207 | my @std_parm_args = ( '-background', '-bg,', '-class', '-display', | ||
| 208 | '-screen', '-font', '-fn', '-foreground', | ||
| 209 | '-fg', '-title', '-xrm' ); | ||
| 210 | # no parameters | ||
| 211 | my @std_bool_args = ( '-iconic', '-motif', '-synchronous' ); | ||
| 212 | |||
| 213 | sub custom_args { | ||
| 214 | my (@args) = @_; | ||
| 215 | my( @newargs, $i, $need_parm ); | ||
| 216 | $need_parm = 0; | ||
| 217 | LOOP: | ||
| 218 | foreach $i ( @args ) { | ||
| 219 | # POSIX-ly correct. | ||
| 220 | $i =~ s/--/-/; | ||
| 221 | if ( grep /$i/, @std_parm_args ) { | ||
| 222 | die "Missing required parameter for argument $prev_arg.\n" | ||
| 223 | if $need_parm == 1; | ||
| 224 | $need_parm = 1; | ||
| 225 | $prev_arg = $i; | ||
| 226 | next LOOP; | ||
| 227 | } elsif ( grep /$i/, @std_bool_args ) { | ||
| 228 | die "Missing required parameter for argument $prev_arg.\n" | ||
| 229 | if $need_parm == 1; | ||
| 230 | $prev_arg = $i; | ||
| 231 | next LOOP; | ||
| 232 | } else { | ||
| 233 | if( $need_parm == 1 ) { | ||
| 234 | $need_parm = 0; | ||
| 235 | next LOOP; | ||
| 236 | } | ||
| 237 | push @newargs, ($i); | ||
| 238 | } | ||
| 239 | } | ||
| 240 | return @newargs; | ||
| 241 | } | ||
| 242 | |||
| 243 | # Class-specific arguments. | ||
| 244 | # Args that require a parameter. | ||
| 245 | my @parm_args = ( '-importfile', '-textforeground', '-textbackground', | ||
| 246 | '-exportfile' ); | ||
| 247 | # Boolean -- No parameter. | ||
| 248 | my @bool_args = ('-help', '-write', '-quit', '-dump' ); | ||
| 249 | |||
| 250 | sub commandline { | ||
| 251 | my ($self) = @_; | ||
| 252 | my ($need_parm, $i, $prev_arg, $arg, @workargs, $nargs); | ||
| 253 | $nargs = @{$self -> {cmdargs}}; | ||
| 254 | for( $i = $nargs; $i >= 0; $i-- ) { | ||
| 255 | push @workargs, (${$self -> {cmdargs}}[$i]); | ||
| 256 | } | ||
| 257 | while( defined ( $i = pop @workargs ) ) { | ||
| 258 | $i =~ s/--/-/; | ||
| 259 | if( scalar( grep {/$i/} @parm_args ) > 0 ) { | ||
| 260 | die "Missing required parameter for argument $prev_arg.\n" | ||
| 261 | if $need_parm == 1; | ||
| 262 | $need_parm = 1; | ||
| 263 | $prev_arg = $i; | ||
| 264 | } elsif ( grep {/$i/} @bool_args ) { | ||
| 265 | die "Missing parameter for argument $prev_arg.\n" | ||
| 266 | if $need_parm == 1; | ||
| 267 | $need_parm = 0; | ||
| 268 | $prev_arg = $i; | ||
| 269 | # argument that is a boolean | ||
| 270 | $i =~ s/\-//; | ||
| 271 | $self -> $i('1'); | ||
| 272 | } elsif( $need_parm == 1 ) { | ||
| 273 | # parameter for argument. | ||
| 274 | $need_parm = 0; | ||
| 275 | $prev_arg =~ s/\-//; | ||
| 276 | $self -> $prev_arg($i); | ||
| 277 | } else { | ||
| 278 | die "Parameter error: $prev_arg, $i.\n"; | ||
| 279 | } | ||
| 280 | } | ||
| 281 | } | ||
| 282 | |||
| 283 | |||
| 284 | ### | ||
| 285 | ### Class methods | ||
| 286 | ### | ||
| 287 | |||
| 288 | sub bind { | ||
| 289 | |||
| 290 | my $self = shift; | ||
| 291 | |||
| 292 | ($self -> window) -> SUPER::bind('<Alt-i>', | ||
| 293 | sub{$self -> user_import}); | ||
| 294 | ($self -> window) -> SUPER::bind('<Alt-w>', | ||
| 295 | sub{$self -> ws_export}); | ||
| 296 | ($self -> window) -> SUPER::bind('<Alt-x>', | ||
| 297 | sub{$self -> ws_cut}); | ||
| 298 | ($self -> window) -> SUPER::bind('<Alt-c>', | ||
| 299 | sub{$self -> ws_copy}); | ||
| 300 | ($self -> window) -> SUPER::bind('<Alt-v>', | ||
| 301 | sub{$self -> ws_paste}); | ||
| 302 | ($self -> window) -> SUPER::bind('<F1>', | ||
| 303 | sub{$self -> self_help}); | ||
| 304 | ($self -> window) -> SUPER::bind('<Alt-s>', | ||
| 305 | sub{$self -> write_to_disk('')}); | ||
| 306 | ($self -> window) -> SUPER::bind('<Alt-q>', | ||
| 307 | sub{$self -> write_to_disk('1')}); | ||
| 308 | ($self -> window) -> SUPER::bind('<Alt-u>', | ||
| 309 | sub{$self -> ws_undo}); | ||
| 310 | ($self -> window) -> SUPER::bind('<Alt-f>', | ||
| 311 | sub{$self -> ws_search}); | ||
| 312 | ($self -> window) -> SUPER::bind('<Alt-g>', | ||
| 313 | sub{$self -> ws_search_again}); | ||
| 314 | ($self -> window) -> SUPER::bind('<Alt-j>', | ||
| 315 | sub{$self -> goto_line}); | ||
| 316 | ($self -> window) -> SUPER::bind('<Alt-p>', | ||
| 317 | sub{$self -> print_text}); | ||
| 318 | # unbind the right mouse button. | ||
| 319 | ($self -> window) -> SUPER::bind('Tk::TextUndo', '<3>', ''); | ||
| 320 | $self -> {window} -> SUPER::bind( '<ButtonPress-3>', | ||
| 321 | [\&postpopupmenu, $self, Ev('X'), Ev('Y') ] ); | ||
| 322 | } | ||
| 323 | |||
| 324 | sub WrapMenuItems | ||
| 325 | { | ||
| 326 | my ($w) = @_; | ||
| 327 | my $v; | ||
| 328 | tie $v,'Tk::Configure',$w,'-wrap'; | ||
| 329 | return [ | ||
| 330 | [radiobutton => 'Word', -variable => \$v, -value => 'word'], | ||
| 331 | [radiobutton => 'Character', -variable => \$v, -value => 'char'], | ||
| 332 | [radiobutton => 'None', -variable => \$v, -value => 'none'], | ||
| 333 | ]; | ||
| 334 | } | ||
| 335 | |||
| 336 | sub EncodingMenuItems | ||
| 337 | { | ||
| 338 | my ($self) = @_; | ||
| 339 | return [ | ||
| 340 | [radiobutton => 'ISO-8859-1 (Single Byte)', | ||
| 341 | -variable => \$self -> {encoding}, -value => 'iso88591'], | ||
| 342 | [radiobutton => 'UTF16 (Multibyte)', -variable => \$self -> {encoding}, | ||
| 343 | -value => 'utf16'], | ||
| 344 | ]; | ||
| 345 | } | ||
| 346 | |||
| 347 | sub ScrollMenuItems { | ||
| 348 | my ($self) = @_; | ||
| 349 | return [ | ||
| 350 | [checkbutton => 'Left', -command => sub{$self -> scrollbar('w')}, | ||
| 351 | -variable => \$lscroll ], | ||
| 352 | [checkbutton => 'Right', -command => sub{$self -> scrollbar('e')}, | ||
| 353 | -variable => \$rscroll ], | ||
| 354 | [checkbutton => 'Top', -command => sub{$self -> scrollbar('n')}, | ||
| 355 | -variable => \$tscroll ], | ||
| 356 | [checkbutton => 'Bottom', -command => sub{$self -> scrollbar('s')}, | ||
| 357 | -variable => \$bscroll], | ||
| 358 | ]; | ||
| 359 | } | ||
| 360 | |||
| 361 | sub menus { | ||
| 362 | my $self = shift; | ||
| 363 | |||
| 364 | $self -> {menubar} = ($self -> {window} ) -> | ||
| 365 | Menu ( -type => 'menubar', | ||
| 366 | -font => $menufont, | ||
| 367 | Name => 'workspaceMenuBar'); | ||
| 368 | $self -> {popupmenu} = ($self -> {window} ) -> | ||
| 369 | Menu ( -type => 'normal', | ||
| 370 | -tearoff => '', | ||
| 371 | -font => $menufont, | ||
| 372 | Name => 'workspacePopupMenu' ); | ||
| 373 | |||
| 374 | $self -> {filemenu} = ($self -> {menubar}) -> Menu; | ||
| 375 | $self -> {editmenu} = ($self -> {menubar}) -> Menu; | ||
| 376 | $self -> {optionsmenu} = ($self -> {menubar}) -> Menu; | ||
| 377 | $self -> {wrapmenu} = ($self -> {menubar}) -> Menu; | ||
| 378 | $self -> {scrollmenu} = ($self -> {menubar}) -> Menu; | ||
| 379 | $self -> {modemenu} = ($self -> {menubar}) -> Menu; | ||
| 380 | $self -> {helpmenu} = ($self -> {menubar}) -> Menu; | ||
| 381 | |||
| 382 | ($self -> {encodingmenu}) = ($self -> {menubar}) -> Menu; | ||
| 383 | |||
| 384 | |||
| 385 | $self -> {menubar} -> | ||
| 386 | add ('cascade', | ||
| 387 | -label => 'File', | ||
| 388 | -menu => $self -> {filemenu} ); | ||
| 389 | $self -> {menubar} -> | ||
| 390 | add ('cascade', | ||
| 391 | -label => 'Edit', | ||
| 392 | -menu => $self -> {editmenu} ); | ||
| 393 | $self -> {menubar} -> | ||
| 394 | add ('cascade', | ||
| 395 | -label => 'Options', | ||
| 396 | -menu => $self -> {optionsmenu} ); | ||
| 397 | $self -> {menubar} -> add ('separator'); | ||
| 398 | |||
| 399 | $self -> {menubar} -> | ||
| 400 | add ('cascade', | ||
| 401 | -label => 'Help', | ||
| 402 | -menu => $self -> {helpmenu} ); | ||
| 403 | |||
| 404 | if( ( $self -> menubarvisible ) =~ m/1/ ) { | ||
| 405 | $self -> {menubar} -> pack( -anchor => 'w', -fill => 'x' ); | ||
| 406 | } | ||
| 407 | |||
| 408 | $self -> {popupmenu} -> | ||
| 409 | add ('cascade', | ||
| 410 | -label => 'File', | ||
| 411 | -menu => $self -> {filemenu} -> | ||
| 412 | clone( $self -> {popupmenu}, 'normal' )); | ||
| 413 | $self -> {popupmenu} -> | ||
| 414 | add ('cascade', | ||
| 415 | -label => 'Edit', | ||
| 416 | -menu => $self -> {editmenu} -> | ||
| 417 | clone( $self -> {popupmenu}, 'normal' ) ); | ||
| 418 | |||
| 419 | $self -> {popupmenu} -> | ||
| 420 | add ('cascade', | ||
| 421 | -label => 'Options', | ||
| 422 | -menu => $self -> {optionsmenu} -> | ||
| 423 | clone( $self -> {popupmenu}, 'normal' ) ); | ||
| 424 | |||
| 425 | $self -> {popupmenu} -> add ('separator'); | ||
| 426 | $self -> {popupmenu} -> | ||
| 427 | add ('cascade', | ||
| 428 | -label => 'Help', | ||
| 429 | -menu => $self -> {helpmenu} -> | ||
| 430 | clone( $self -> {popupmenu}, 'normal' ) ); | ||
| 431 | |||
| 432 | $self -> {filemenu} -> add ( 'command', -label => 'Import Text...', | ||
| 433 | -state => 'normal', | ||
| 434 | -accelerator => 'Alt-I', | ||
| 435 | -command => sub{$self -> user_import}); | ||
| 436 | $self -> {filemenu} -> add ( 'command', | ||
| 437 | -label => 'Export Text', | ||
| 438 | -accelerator => 'Alt-W', | ||
| 439 | -command => sub{$self -> ws_export}); | ||
| 440 | $self -> {filemenu} -> add ('separator'); | ||
| 441 | $self -> {filemenu} -> add ( 'command', -label => 'System Command...', | ||
| 442 | -state => 'normal', | ||
| 443 | -command => sub{shell_cmd($self)}); | ||
| 444 | $self -> {filemenu} -> add ( 'command', -label => 'Shell', | ||
| 445 | -state => 'normal', | ||
| 446 | -command => sub{ishell($self)}); | ||
| 447 | $self -> {filemenu} -> add ( 'command', -label => 'Filter...', | ||
| 448 | -state => 'normal', | ||
| 449 | -command => sub{&filter_text($self)}); | ||
| 450 | $self -> {filemenu} -> add ('separator'); | ||
| 451 | $self -> {filemenu} -> add ( 'command', -label => 'Save...', | ||
| 452 | -state => 'normal', | ||
| 453 | -accelerator => 'Alt-S', | ||
| 454 | -command => sub{$self -> write_to_disk('')}); | ||
| 455 | $self -> {filemenu} -> add ( 'command', -label => 'Exit...', | ||
| 456 | -state => 'normal', | ||
| 457 | -accelerator => 'Alt-Q', | ||
| 458 | -command => sub{$self -> write_to_disk('1')}); | ||
| 459 | ($self -> { filemenu }) -> configure( -font => $menufont ); | ||
| 460 | $self -> {editmenu} -> add ( 'command', -label => 'Undo', | ||
| 461 | -state => 'normal', | ||
| 462 | -accelerator => 'Alt-U', | ||
| 463 | -font => $menufont, | ||
| 464 | -command => sub{$self -> ws_undo}); | ||
| 465 | $self -> {editmenu} -> add ('separator'); | ||
| 466 | $self -> {editmenu} -> add ( 'command', -label => 'Cut', | ||
| 467 | -state => 'normal', | ||
| 468 | -accelerator => 'Alt-X', | ||
| 469 | -font => $menufont, | ||
| 470 | -command => sub{$self -> ws_cut}); | ||
| 471 | $self -> {editmenu} -> add ( 'command', -label => 'Copy', | ||
| 472 | -accelerator => 'Alt-C', | ||
| 473 | -state => 'normal', | ||
| 474 | -font => $menufont, | ||
| 475 | -command => sub{$self -> ws_copy}); | ||
| 476 | $self -> {editmenu} -> add ( 'command', -label => 'Paste', | ||
| 477 | -accelerator => 'Alt-V', | ||
| 478 | -state => 'normal', | ||
| 479 | -font => $menufont, | ||
| 480 | -command => sub{$self -> ws_paste}); | ||
| 481 | $self -> {editmenu} -> add ('separator'); | ||
| 482 | ($self -> {editmenu}) -> add( 'command', -label => 'Search & Replace...', | ||
| 483 | -accelerator => 'Alt-F', | ||
| 484 | -state => 'normal', | ||
| 485 | -font => $menufont, | ||
| 486 | -command => sub{$self -> ws_search} ); | ||
| 487 | ($self -> {editmenu}) -> add( 'command', -label => 'Repeat Last Search', | ||
| 488 | -accelerator => 'Alt-G', | ||
| 489 | -state => 'normal', | ||
| 490 | -font => $menufont, | ||
| 491 | -command => sub{$self -> ws_search_again}); | ||
| 492 | $self -> {editmenu} -> add ( 'command', -label => 'Evaluate Selection', | ||
| 493 | -state => 'normal', | ||
| 494 | -command => sub{$self -> evalselection()}); | ||
| 495 | ($self -> { editmenu }) -> configure( -font => $menufont ); | ||
| 496 | |||
| 497 | $self -> {editmenu} -> add ('separator'); | ||
| 498 | $self -> {editmenu} -> add ( 'command', -label => 'Goto Line...', | ||
| 499 | -state => 'normal', | ||
| 500 | -font => $menufont, | ||
| 501 | -accelerator => 'Alt-J', | ||
| 502 | -command => sub{$self->goto_line}); | ||
| 503 | ($self -> { optionsmenu }) -> configure( -font => $menufont ); | ||
| 504 | $self -> {optionsmenu} -> add ( 'cascade', | ||
| 505 | -label => 'Word Wrap', | ||
| 506 | -menu => $self -> {wrapmenu} ); | ||
| 507 | $items = &WrapMenuItems($self -> {text}); | ||
| 508 | $self -> {wrapmenu} -> AddItems( @$items ); | ||
| 509 | $self -> {wrapmenu} -> configure (-font => $menufont); | ||
| 510 | $self -> {optionsmenu} -> add ( 'cascade', | ||
| 511 | -label => 'Scroll Bars', | ||
| 512 | -menu => $self -> {scrollmenu} ); | ||
| 513 | $self -> {scrollbuttons} = &ScrollMenuItems( $self ); | ||
| 514 | $self -> {scrollmenu} -> AddItems( @{$self -> {scrollbuttons}} ); | ||
| 515 | $self -> {scrollmenu} -> configure (-font => $menufont); | ||
| 516 | $self -> {optionsmenu} -> add( 'cascade', -labe => 'Output Encoding', | ||
| 517 | -menu => $self -> {encodingmenu}); | ||
| 518 | $items = &EncodingMenuItems($self); | ||
| 519 | $self -> {encodingmenu} -> AddItems( @$items ); | ||
| 520 | $self -> {encodingmenu} -> configure( -font => $menufont ); | ||
| 521 | if( $self -> hasunicode !~ /1/ ) { | ||
| 522 | $self -> {encodingmenu} -> entryconfigure( 2, -state => 'disabled' ); | ||
| 523 | } | ||
| 524 | $self | ||
| 525 | -> {optionsmenu} -> | ||
| 526 | add ( 'command', | ||
| 527 | -label => (($self -> menubarvisible)?'Hide ':'Show '). | ||
| 528 | 'Menubar', | ||
| 529 | -command => [\&togglemenubar, $self ] ); | ||
| 530 | $self -> {optionsmenu} -> add ('separator'); | ||
| 531 | $self -> {optionsmenu} -> add ( 'command', -label => | ||
| 532 | 'Right Margin...', | ||
| 533 | -state => 'normal', | ||
| 534 | -font => $menufont, | ||
| 535 | -command => [\&setFillColumn, $self]); | ||
| 536 | $self -> {optionsmenu} -> add ( 'command', -label => | ||
| 537 | 'Color Editor...', | ||
| 538 | -state => 'normal', | ||
| 539 | -font => $menufont, | ||
| 540 | -command => [\&elementColor, $self]); | ||
| 541 | $self -> {optionsmenu} -> add ( 'command', -label => 'Text Font...', | ||
| 542 | -state => 'normal', | ||
| 543 | -font => $menufont, | ||
| 544 | -command => [\&ws_font, $self]); | ||
| 545 | $self -> {helpmenu} -> add ( 'command', -label => 'About...', | ||
| 546 | -state => 'normal', | ||
| 547 | -font => $menufont, | ||
| 548 | -command => sub{$self -> about}); | ||
| 549 | $self -> {helpmenu} -> add ( 'command', -label => 'Workspace Help...', | ||
| 550 | -state => 'normal', | ||
| 551 | -font => $menufont, | ||
| 552 | -accelerator => "F1", | ||
| 553 | -command => sub{$self -> self_help}); | ||
| 554 | $self -> {helpmenu} -> add ( 'command', -label => 'Text Editor Commands...', | ||
| 555 | -state => 'normal', | ||
| 556 | -font => $menufont, | ||
| 557 | -command => sub{$self -> edit_help}); | ||
| 558 | } | ||
| 559 | |||
| 560 | ### | ||
| 561 | ### Instance methods. | ||
| 562 | ### | ||
| 563 | |||
| 564 | sub hasunicode { | ||
| 565 | my $self = shift; | ||
| 566 | if(@_) { $self -> {hasunicode} = shift } | ||
| 567 | return $self -> {hasunicode}; | ||
| 568 | } | ||
| 569 | |||
| 570 | sub textforeground { | ||
| 571 | my ($self, $arg) = @_; | ||
| 572 | ( $self -> {text} ) -> configure( -foreground => $arg ); | ||
| 573 | } | ||
| 574 | |||
| 575 | sub textbackground { | ||
| 576 | my ($self, $arg) = @_; | ||
| 577 | ( $self -> {text} ) -> configure( -background => $arg ); | ||
| 578 | } | ||
| 579 | |||
| 580 | sub importfile { | ||
| 581 | my ($self, $arg) = @_; | ||
| 582 | open I, "<$arg" or | ||
| 583 | warn "Importfile: Couldn't open $arg: ".@!."\n"; | ||
| 584 | while( <I> ) { | ||
| 585 | $self -> text -> insert( $self -> text -> index( 'insert' ), | ||
| 586 | $_ ); | ||
| 587 | } | ||
| 588 | close I; | ||
| 589 | $self ->{text}->{SubWidget}{workspacetext}{modified} = '1'; | ||
| 590 | } | ||
| 591 | |||
| 592 | sub exportfile { | ||
| 593 | my ($self, $arg) =@_; | ||
| 594 | open O, ">>$arg" or | ||
| 595 | warn "Exportfile: Couldn't open $arg: ".@!."\n"; | ||
| 596 | print O $self -> text -> get( '1.0', 'end' ); | ||
| 597 | close O; | ||
| 598 | } | ||
| 599 | |||
| 600 | sub dump { | ||
| 601 | my ($self, $arg) = @_; | ||
| 602 | print $self -> text -> get( '1.0', $self -> text -> index( 'end' ) ); | ||
| 603 | } | ||
| 604 | |||
| 605 | sub write { | ||
| 606 | my ($self, $args) = @_; | ||
| 607 | $self -> write_to_disk( 0 ); | ||
| 608 | } | ||
| 609 | |||
| 610 | sub quit { | ||
| 611 | my ($self, $arg) = @_; | ||
| 612 | $self -> window -> WmDeleteWindow; | ||
| 613 | } | ||
| 614 | |||
| 615 | sub title { | ||
| 616 | my ($self, $arg) = @_; | ||
| 617 | $self -> window -> configure( -title => $arg ); | ||
| 618 | $self -> window -> update; | ||
| 619 | $self -> name( $arg ); | ||
| 620 | $self ->{text}->{SubWidget}{workspacetext}{modified} = '1'; | ||
| 621 | } | ||
| 622 | |||
| 623 | sub window { | ||
| 624 | my $self = shift; | ||
| 625 | if (@_) { $self -> {window} = shift } | ||
| 626 | return $self -> {window} | ||
| 627 | } | ||
| 628 | |||
| 629 | sub text { | ||
| 630 | my $self = shift; | ||
| 631 | if (@_) { $self -> {text} = shift } | ||
| 632 | return $self -> {text} | ||
| 633 | } | ||
| 634 | |||
| 635 | sub name { | ||
| 636 | my $self = shift; | ||
| 637 | if (@_) { $self -> {name} = shift } | ||
| 638 | return $self -> {name} | ||
| 639 | } | ||
| 640 | |||
| 641 | sub filepath { | ||
| 642 | my $self = shift; | ||
| 643 | if (@_) { | ||
| 644 | $self -> {filepath} = shift; | ||
| 645 | } elsif (not defined $self -> {filepath}) { | ||
| 646 | $self -> {filepath} = &cwd.'/'.$0; | ||
| 647 | $self -> {filepath} =~ s/(\/\/)|(\/\.\/)/\//g; | ||
| 648 | } | ||
| 649 | return $self -> {filepath} | ||
| 650 | } | ||
| 651 | |||
| 652 | sub help { | ||
| 653 | my $self = shift; | ||
| 654 | print STDERR $cmdhelptext; | ||
| 655 | $self -> window -> WmDeleteWindow; | ||
| 656 | } | ||
| 657 | |||
| 658 | sub textfont { | ||
| 659 | my $self = shift; | ||
| 660 | if (@_) { $self -> {textfont} = shift } | ||
| 661 | return $self -> {textfont} | ||
| 662 | } | ||
| 663 | |||
| 664 | sub workspaceobject { | ||
| 665 | return @Workspaceobject; | ||
| 666 | } | ||
| 667 | |||
| 668 | sub menubar { | ||
| 669 | my $self = shift; | ||
| 670 | if (@_) { $self -> {menubar} = shift } | ||
| 671 | return $self -> {menubar} | ||
| 672 | } | ||
| 673 | |||
| 674 | sub menubarvisible { | ||
| 675 | my $self = shift; | ||
| 676 | if (@_) { $self -> {menubarvisible} = shift } | ||
| 677 | return $self -> {menubarvisible} | ||
| 678 | } | ||
| 679 | |||
| 680 | sub popupmenu { | ||
| 681 | my $self = shift; | ||
| 682 | if (@_) { $self -> {popupmenu} = shift } | ||
| 683 | return $self -> {popupmenu} | ||
| 684 | } | ||
| 685 | |||
| 686 | sub filemenu { | ||
| 687 | my $self = shift; | ||
| 688 | if (@_) { $self -> {filemenu} = shift } | ||
| 689 | return $self -> {filemenu}; | ||
| 690 | } | ||
| 691 | |||
| 692 | sub outputfile { | ||
| 693 | my $self = shift; | ||
| 694 | if (@_) { $self -> {outputfile} = shift } | ||
| 695 | return $self -> {outputfile}; | ||
| 696 | } | ||
| 697 | |||
| 698 | sub filter { | ||
| 699 | my $self = shift; | ||
| 700 | if (@_) { $self -> {filter} = shift } | ||
| 701 | return $self -> {filter}; | ||
| 702 | } | ||
| 703 | |||
| 704 | sub wrap { | ||
| 705 | my $self = shift; | ||
| 706 | my $w = $self -> {wrapmenu}; | ||
| 707 | if( @_) { | ||
| 708 | my $m = shift; | ||
| 709 | if ( $m =~ m/word/ ) { $w -> invoke( 1 ) }; | ||
| 710 | if ( $m =~ m/char/ ) { $w -> invoke( 2 ) }; | ||
| 711 | if ( $m =~ m/none/ ) { $w -> invoke( 3 ) }; | ||
| 712 | } | ||
| 713 | return ($self -> {text}) -> cget('-wrap'); | ||
| 714 | } | ||
| 715 | |||
| 716 | sub parent_ws { | ||
| 717 | # We say parent_ws because MainWindows' parents are not recognized | ||
| 718 | # by default. | ||
| 719 | my $self = shift; | ||
| 720 | if (@_) { $self -> {parent_ws} = shift } | ||
| 721 | return $self -> {parent_ws} | ||
| 722 | } | ||
| 723 | |||
| 724 | sub editmenu { | ||
| 725 | my $self = shift; | ||
| 726 | if (@_) { $self -> {editmenu} = shift } | ||
| 727 | return $self -> {editmenu} | ||
| 728 | } | ||
| 729 | |||
| 730 | sub helpmenu { | ||
| 731 | my $self = shift; | ||
| 732 | if (@_) { $self -> {helpmenu} = shift } | ||
| 733 | return $self -> {helpmenu} | ||
| 734 | } | ||
| 735 | |||
| 736 | sub optionsmenu { | ||
| 737 | my $self = shift; | ||
| 738 | if (@_) { $self -> {optionsmenu} = shift } | ||
| 739 | return $self -> {optionsmenu} | ||
| 740 | } | ||
| 741 | |||
| 742 | sub width { | ||
| 743 | my $self = shift; | ||
| 744 | if (@_) { $self -> {width} = shift } | ||
| 745 | return $self -> {width} | ||
| 746 | } | ||
| 747 | |||
| 748 | sub height { | ||
| 749 | my $self = shift; | ||
| 750 | if (@_) { $self -> {height} = shift } | ||
| 751 | return $self -> {height} | ||
| 752 | } | ||
| 753 | |||
| 754 | # show or hide menubar | ||
| 755 | sub togglemenubar { | ||
| 756 | my $self = shift; | ||
| 757 | |||
| 758 | $self -> {text} -> packForget; | ||
| 759 | $self -> {menubar} -> packForget; | ||
| 760 | if( ($self -> {menubarvisible}) =~ m/1/ ) { | ||
| 761 | $self -> {menubarvisible} = ''; | ||
| 762 | } else { | ||
| 763 | $self -> {menubar} -> pack( -side => 'top', -anchor => 'w', | ||
| 764 | -fill => 'x' ); | ||
| 765 | $self -> {menubarvisible} = '1'; | ||
| 766 | } | ||
| 767 | $self -> optionsmenu -> entryconfigure( 4, -label => | ||
| 768 | (($self -> menubarvisible) ? | ||
| 769 | 'Hide ': 'Show ' ) . 'Menubar' ); | ||
| 770 | $self -> {text} -> pack( -side => 'top', -fill => 'both', -expand => '1' ); | ||
| 771 | $self ->{text}->{SubWidget}{workspacetext}{modified} = '1'; | ||
| 772 | return $self -> {menubarvisible} | ||
| 773 | } | ||
| 774 | |||
| 775 | sub x { | ||
| 776 | my $self = shift; | ||
| 777 | if (@_) { $self -> {x} = shift } | ||
| 778 | return $self -> {x} | ||
| 779 | } | ||
| 780 | |||
| 781 | sub outputmode { | ||
| 782 | my $self = shift; | ||
| 783 | if (@_) { $self -> {outputmode} = shift } | ||
| 784 | return $self -> {outputmode} | ||
| 785 | } | ||
| 786 | |||
| 787 | sub y { | ||
| 788 | my $self = shift; | ||
| 789 | if (@_) { $self -> {y} = shift } | ||
| 790 | return $self -> {y} | ||
| 791 | } | ||
| 792 | |||
| 793 | sub scroll { | ||
| 794 | my $self = shift; | ||
| 795 | if (@_) { $self -> {scroll} = shift } | ||
| 796 | return $self -> {scroll} | ||
| 797 | } | ||
| 798 | |||
| 799 | sub hasnet { | ||
| 800 | my $self = shift; | ||
| 801 | if( @_ ) { $self -> {hasnet} = shift } | ||
| 802 | return $self -> {hasnet} | ||
| 803 | } | ||
| 804 | |||
| 805 | sub insertionpoint { | ||
| 806 | my $self = shift; | ||
| 807 | if (@_) { $self -> {insertionpoint} = shift } | ||
| 808 | return $self -> {insertionpoint} | ||
| 809 | } | ||
| 810 | |||
| 811 | sub open { | ||
| 812 | my ($name) = @_; | ||
| 813 | |||
| 814 | my @command_line = ( "\./" . $name . ' &'); | ||
| 815 | system( @command_line ); | ||
| 816 | } | ||
| 817 | |||
| 818 | sub wmgeometry { | ||
| 819 | my ($self) = @_; | ||
| 820 | my $g = $self -> window -> geometry; | ||
| 821 | $g =~ m/([0-9]+)x([0-9]+)\+([0-9]+)\+([0-9]+)/; | ||
| 822 | $self -> width($1); $self -> height($2); $self -> x($3); | ||
| 823 | $self -> y($4); | ||
| 824 | $self -> geometry( $g, $self -> text -> index( 'insert' ) ); | ||
| 825 | } | ||
| 826 | |||
| 827 | sub geometry { | ||
| 828 | my ($self, $g, $i) = @_; | ||
| 829 | my $nargs = scalar @_; | ||
| 830 | if( $nargs == 3 ) { | ||
| 831 | $g =~ m/([0-9]+)x([0-9]+)\+([0-9]+)\+([0-9]+)/; | ||
| 832 | $self -> width($1); $self -> height($2); $self -> x($3); | ||
| 833 | $self -> y($4); | ||
| 834 | $self -> window -> geometry( $g ); | ||
| 835 | $self -> insertionpoint( $i ); | ||
| 836 | $self -> text -> markSet( 'insert', $self -> insertionpoint ); | ||
| 837 | $self -> text -> see( 'insert' ); | ||
| 838 | } elsif ( $nargs == 1 ) { | ||
| 839 | my $cg = $self -> window -> geometry; | ||
| 840 | $cg =~ m/([0-9]+)x([0-9]+)\+([0-9]+)\+([0-9]+)/; | ||
| 841 | $self -> width($1); $self -> height($2); $self -> x($3); | ||
| 842 | $self -> y($4); | ||
| 843 | my $ip = $self -> text -> index( 'insert' ); | ||
| 844 | $self ->{text}->{SubWidget}{workspacetext}{modified} = '1'; | ||
| 845 | return ($cg, $ip); | ||
| 846 | } else { | ||
| 847 | warn "geometry: wrong no. of arguments: $nargs.\n"; | ||
| 848 | } | ||
| 849 | } | ||
| 850 | |||
| 851 | sub postpopupmenu { | ||
| 852 | my $w = shift; | ||
| 853 | my $self = shift; | ||
| 854 | my $x = shift; | ||
| 855 | my $y = shift; | ||
| 856 | # my $g = ($self -> window) -> geometry; | ||
| 857 | # $g =~ m/([0-9]+)x([0-9]+)\+([0-9]+)\+([0-9]+)/; | ||
| 858 | # $self -> width($1); $self -> height($2); $self -> x($3); | ||
| 859 | # $self -> y($4); | ||
| 860 | ($self -> popupmenu) -> post( $x, $y ); | ||
| 861 | } | ||
| 862 | |||
| 863 | sub fill_paragraph { | ||
| 864 | my $self = shift; | ||
| 865 | my $t = $self -> {text}; | ||
| 866 | $t -> paragraphFill; | ||
| 867 | } | ||
| 868 | |||
| 869 | sub select_paragraph { | ||
| 870 | my $self = shift; | ||
| 871 | my $t = $self -> {text}; | ||
| 872 | $t -> selectPara; | ||
| 873 | } | ||
| 874 | |||
| 875 | sub goto_line { | ||
| 876 | my $self = shift; | ||
| 877 | my $d = $self -> window -> DialogBox( -title => 'Goto Line', | ||
| 878 | -buttons => [qw/Ok Cancel/], | ||
| 879 | -default_button => 'Ok' ); | ||
| 880 | my $l = $d -> add( 'Label', -text => 'Line Number: ', | ||
| 881 | -font => $menufont ) | ||
| 882 | -> pack( -side => 'left', -padx => 5, -pady => 5 ); | ||
| 883 | $d -> Subwidget ('B_Ok') -> configure (-font => $menufont); | ||
| 884 | $d -> Subwidget ('B_Cancel') -> configure (-font => $menufont); | ||
| 885 | my $e = $d -> add( 'Entry', -width => 10 ) | ||
| 886 | -> pack( -side => 'left', -padx => 5, -pady => 5 ); | ||
| 887 | my ($row, $col) = split /\./, $self -> text -> index('insert'); | ||
| 888 | $e -> insert( '1.0', $row ); | ||
| 889 | if( ( $resp = $d -> Show ) =~ /Ok/ ) { | ||
| 890 | $self -> text -> markSet( 'insert', $e -> get.'.0' ); | ||
| 891 | $self -> text -> see( 'insert' ); | ||
| 892 | } | ||
| 893 | } | ||
| 894 | |||
| 895 | sub scrollbar { | ||
| 896 | my $self = shift; | ||
| 897 | if (@_) { | ||
| 898 | my ($p) = @_; | ||
| 899 | if (($p=~m/w/)&&($lscroll=='1')){ | ||
| 900 | $self->{scroll}.='w'; | ||
| 901 | $self->{scroll} =~ s/e//; $rscroll = '0'; | ||
| 902 | } | ||
| 903 | elsif (($p=~m/e/)&&($rscroll=='1')) { | ||
| 904 | $self->{scroll}.='e'; | ||
| 905 | $self->{scroll} =~ s/w//; $lscroll = '0'; | ||
| 906 | } | ||
| 907 | elsif (($p=~m/n/)&&($tscroll=='1')) { | ||
| 908 | $self->{scroll} = 'n' . $self -> {scroll}; | ||
| 909 | $self->{scroll} =~ s/s//; $bscroll = '0'; | ||
| 910 | } | ||
| 911 | elsif(($p=~m/s/)&&($bscroll=='1')) { | ||
| 912 | $self->{scroll} = 's' . $self -> {scroll}; | ||
| 913 | $self->{scroll} =~ s/n//; $tscroll = '0'; | ||
| 914 | } | ||
| 915 | else { | ||
| 916 | $self -> {scroll} =~ s/$p//; | ||
| 917 | } | ||
| 918 | &set_scroll( $self ); | ||
| 919 | $self ->{text}->{SubWidget}{workspacetext}{modified} = '1'; | ||
| 920 | return $self -> {scroll}; | ||
| 921 | } | ||
| 922 | } | ||
| 923 | |||
| 924 | sub set_scroll { | ||
| 925 | my ($self) = @_; | ||
| 926 | $self -> {text} -> configure( -scrollbars => $self -> {scroll} ); | ||
| 927 | $self -> {text} -> pack( -expand => '1', -fill => 'both' ); | ||
| 928 | if( $self -> {scroll} =~ /w/ ) { $lscroll = '1' } | ||
| 929 | if( $self -> {scroll} =~ /e/ ) { $rscroll = '1' } | ||
| 930 | if( $self -> {scroll} =~ /n/ ) { $tscroll = '1' } | ||
| 931 | if( $self -> {scroll} =~ /s/ ) { $bscroll = '1' } | ||
| 932 | } | ||
| 933 | |||
| 934 | sub ws_font { | ||
| 935 | my ($self) = @_; | ||
| 936 | my ($oldgeometry, $dialog, $f, $x, $y, $newwidth, $newheight); | ||
| 937 | $dialog = ($self -> {window}) -> XFontSelect; | ||
| 938 | my $f = $dialog -> Show; | ||
| 939 | ($self -> text) -> configure( -font => $f ); | ||
| 940 | $self -> textfont( $f ); | ||
| 941 | $oldgeometry = ($self -> window) -> geometry(); | ||
| 942 | $oldgeometry =~ m/.+x.+\+(.+)\+(.+)/; | ||
| 943 | $x = $1; $y = $2; | ||
| 944 | $newwidth = ($self -> text) -> reqwidth; | ||
| 945 | $newheight = ($self -> text) -> reqheight; | ||
| 946 | $self -> geometry($newwidth . 'x' . $newheight . | ||
| 947 | '+' . $x . '+' . $y, | ||
| 948 | $self -> insertionpoint ); | ||
| 949 | $self ->{text}->{SubWidget}{workspacetext}{modified} = '1'; | ||
| 950 | return; | ||
| 951 | } | ||
| 952 | |||
| 953 | sub setFillColumn { | ||
| 954 | my ($w) = @_; | ||
| 955 | my $d = $w -> window -> DialogBox (-title => 'Set Right Margin', | ||
| 956 | -buttons => [qw/Ok Dismiss/], | ||
| 957 | -default_button => 'Ok' ); | ||
| 958 | my $oldmargin = $w -> text -> fillcolumn; | ||
| 959 | $d -> Subwidget ('B_Ok') -> configure (-font => $menufont); | ||
| 960 | $d -> Subwidget ('B_Dismiss') -> configure (-font => $menufont); | ||
| 961 | my $e = $d -> add ('Entry', -width => 5, | ||
| 962 | -textvariable => \$oldmargin) -> | ||
| 963 | pack (-expand => '1', -fill => 'x', -padx => 5, -pady => 5); | ||
| 964 | my $resp = $d -> Show; | ||
| 965 | my $newmargin = $e -> get; | ||
| 966 | $w -> text -> fillcolumn ($newmargin) if $resp !~ /Dismiss/; | ||
| 967 | } | ||
| 968 | |||
| 969 | sub elementColor { | ||
| 970 | my ($w) = @_; | ||
| 971 | my ($attribute, $color); | ||
| 972 | my $c = | ||
| 973 | $w -> window -> ColorEditor (-widgets => [$w -> text]); | ||
| 974 | $c -> Show; | ||
| 975 | $w ->{text}->{SubWidget}{workspacetext}{modified} = '1'; | ||
| 976 | } | ||
| 977 | |||
| 978 | sub filter_text { | ||
| 979 | my $self = shift; | ||
| 980 | my $resp = $self -> filter_dialog; | ||
| 981 | return if $resp =~ /Cancel/; | ||
| 982 | my $name = $self -> name; | ||
| 983 | my $cmd = $self -> filter; | ||
| 984 | return if $cmd eq ''; | ||
| 985 | my $tmpname = $self -> mktmpfile; | ||
| 986 | my $cmdstring; | ||
| 987 | $cmdstring = "cat $tmpname | $cmd "; | ||
| 988 | $self -> watchcursor; | ||
| 989 | # insert to self | ||
| 990 | if( ( $self -> outputmode ) =~ /self/ ) { | ||
| 991 | $self->text->insert($self->text->index('insert'),`$cmdstring`); | ||
| 992 | `rm -f $tmpname`; | ||
| 993 | } | ||
| 994 | # output to file | ||
| 995 | if( ( $self -> outputmode ) =~ /file/ ){ | ||
| 996 | my $ofilename = $self -> outputfile; | ||
| 997 | if( $ofilename ne '' ) { | ||
| 998 | if( $ofilename =~ /\:/ ) { | ||
| 999 | $ofilename =~ s/^\///; | ||
| 1000 | $self -> remotefilter( 'file', $ofilename, $cmdstring ); | ||
| 1001 | } else { | ||
| 1002 | `$cmdstring >$ofilename`; | ||
| 1003 | } | ||
| 1004 | } | ||
| 1005 | } | ||
| 1006 | # output to terminal | ||
| 1007 | if( ( $self -> outputmode ) =~ /terminal/ ) { | ||
| 1008 | my $ofilename = $self -> outputfile; | ||
| 1009 | $cmdstring = $cmdstring . (($ofilename ne '') ? ' >'.$ofilename : ''); | ||
| 1010 | system $cmdstring; | ||
| 1011 | `rm -f $tmpname`; | ||
| 1012 | } | ||
| 1013 | # output to new workspace | ||
| 1014 | if( ( $self -> outputmode ) =~ /new/ ) { | ||
| 1015 | my $newname = $self -> outputfile; | ||
| 1016 | return if $newname eq ''; | ||
| 1017 | my $outfile = "$tmpname.output"; | ||
| 1018 | `$cmdstring >$outfile`; | ||
| 1019 | if( $newname =~ /\:/ ) { | ||
| 1020 | my ($host, $remotename) = split /\:/, $newname; | ||
| 1021 | $remotename =~ s/^\///; | ||
| 1022 | &create( $remotename ); | ||
| 1023 | `./$remotename -importfile $outfile -write -quit &`; | ||
| 1024 | `rm -f $tmpname $outfile`; | ||
| 1025 | if( ($self -> hasnet) !~ /1/ ) { | ||
| 1026 | $self -> | ||
| 1027 | error( "Network not enabled:\nNetwork library modules not found." ); | ||
| 1028 | return; | ||
| 1029 | } | ||
| 1030 | require Tk::LoginDialog; | ||
| 1031 | my $d = ($self -> {window}) -> LoginDialog; | ||
| 1032 | my $resp = $d -> Show; | ||
| 1033 | my $uid = $d -> cget( '-userid' ); | ||
| 1034 | my $pwd = $d -> cget( '-password' ); | ||
| 1035 | return if $resp !~ /Login/; | ||
| 1036 | my $ftp = Net::FTP -> new( $host, Debug => 1 ); | ||
| 1037 | return if ! defined $ftp; | ||
| 1038 | $ftp -> login( $uid, $pwd ); | ||
| 1039 | $ftp -> put( $remotename ); | ||
| 1040 | $ftp -> close; | ||
| 1041 | } else { | ||
| 1042 | &create( $newname ); | ||
| 1043 | `./$newname -importfile $outfile -write -quit &`; | ||
| 1044 | `rm -f $tmpname $outfile`; | ||
| 1045 | } | ||
| 1046 | } | ||
| 1047 | $self ->{text}->{SubWidget}{workspacetext}{modified} = '1'; | ||
| 1048 | $self -> defaultcursor; | ||
| 1049 | } | ||
| 1050 | |||
| 1051 | sub remotefilter { | ||
| 1052 | my ($self, $mode, $ofilename, $cmdstring ) = @_; | ||
| 1053 | if( ($self -> hasnet) !~ /1/ ) { | ||
| 1054 | $self -> | ||
| 1055 | error( "Network not enabled:\nNetwork library modules not found." ); | ||
| 1056 | return; | ||
| 1057 | } | ||
| 1058 | require Tk::LoginDialog; | ||
| 1059 | my $name = $self -> name; | ||
| 1060 | my ($localfile); | ||
| 1061 | my $d = ($self -> {window}) -> LoginDialog; | ||
| 1062 | my $resp = $d -> Show; | ||
| 1063 | my $uid = $d -> cget( '-userid' ); | ||
| 1064 | my $pwd = $d -> cget( '-password' ); | ||
| 1065 | return if $resp !~ /Login/; | ||
| 1066 | my ($hostid, $outputpath) = split /\:/, $ofilename; | ||
| 1067 | my $ftp = Net::FTP -> new( $hostid, Debug => 1 ); | ||
| 1068 | return if ! defined $ftp; | ||
| 1069 | $ftp -> login( $uid, $pwd ); | ||
| 1070 | if ( $mode =~ /file/ ) { | ||
| 1071 | $localfile = $self -> makelocal( $cmdstring ); | ||
| 1072 | $ftp -> put( $localfile, $outputpath ); | ||
| 1073 | $ftp -> close; | ||
| 1074 | } | ||
| 1075 | } | ||
| 1076 | |||
| 1077 | sub makelocal { | ||
| 1078 | my ($self, $cmdstring) = @_; | ||
| 1079 | my $name = $self -> name; | ||
| 1080 | my $localfile = "/tmp/tmp$name"."$$.tmp"; | ||
| 1081 | open( CMD, "$cmdstring |" )or | ||
| 1082 | return error( "Couldn't start $cmdstring." ); | ||
| 1083 | open LOCALFILE, ">$localfile" or | ||
| 1084 | return error( "Could not open $localfile" ); | ||
| 1085 | while( <CMD> ) { | ||
| 1086 | print LOCALFILE $_; | ||
| 1087 | } | ||
| 1088 | close CMD; | ||
| 1089 | close LOCALFILE; | ||
| 1090 | return $localfile; | ||
| 1091 | } | ||
| 1092 | |||
| 1093 | sub error { | ||
| 1094 | my $self = shift; | ||
| 1095 | my $message = shift; | ||
| 1096 | |||
| 1097 | return if $message eq ''; | ||
| 1098 | my $d = ($self -> {window}) -> Dialog( -title => 'Workspace Error', | ||
| 1099 | -text => $message, | ||
| 1100 | -bitmap => 'info', | ||
| 1101 | -default_button => 'Ok', | ||
| 1102 | -font => $menufont, | ||
| 1103 | -buttons => [qw/Ok/] ); | ||
| 1104 | $d -> Subwidget ('B_Ok') -> configure (-font => $menufont); | ||
| 1105 | $d -> Show; | ||
| 1106 | } | ||
| 1107 | |||
| 1108 | sub mktmpfile { | ||
| 1109 | my $self = shift; | ||
| 1110 | my $name = $self -> name; | ||
| 1111 | open FILE, ">/tmp/$name$$.tmp" | ||
| 1112 | or warn "Could not open /tmp/$name$$\: @!\n"; | ||
| 1113 | my $contents = $self -> text -> get( '1.0', 'end' ); | ||
| 1114 | print FILE $contents; | ||
| 1115 | close FILE; | ||
| 1116 | return "/tmp/$name$$.tmp"; | ||
| 1117 | } | ||
| 1118 | |||
| 1119 | sub filter_dialog { | ||
| 1120 | my $self = shift; | ||
| 1121 | my $dw = ($self->window)->DialogBox( -title => 'Filter', | ||
| 1122 | -buttons => ['Ok', 'Cancel']); | ||
| 1123 | my $f1 = $dw -> Frame( -container => '0' ); | ||
| 1124 | my $f2 = $dw -> Frame( -container => '0', -relief => groove, | ||
| 1125 | -borderwidth => '3' ); | ||
| 1126 | my $f3 = $dw -> Frame( -container => '0' ); | ||
| 1127 | my $cl = $f1 -> Label( -text => 'Filter:', -font => $menufont ); | ||
| 1128 | $cl -> pack( -side => 'left' ); | ||
| 1129 | my $cm = $f1 -> Entry( -width => 47 ) | ||
| 1130 | -> pack( -side => 'left', -padx => 5 ); | ||
| 1131 | $f1 -> pack( -ipady => 10, -fill => 'both', -expand => '1' ); | ||
| 1132 | $f2 -> Label( -text => "\nOutput To:", -font => $menufont ) | ||
| 1133 | -> pack( -anchor => 'w' ); | ||
| 1134 | my $b1 = $f2 -> Radiobutton ( -text => 'Self', | ||
| 1135 | -font => $menufont, | ||
| 1136 | -state => 'normal', | ||
| 1137 | -variable => \$self -> {outputmode}, | ||
| 1138 | -value => 'self' ) | ||
| 1139 | -> pack( -side => 'left' ); | ||
| 1140 | my $b2 = $f2 -> Radiobutton ( -text => 'File', | ||
| 1141 | -font => $menufont, | ||
| 1142 | -state => 'normal', | ||
| 1143 | -variable => \$self -> {outputmode}, | ||
| 1144 | -value => 'file' ) | ||
| 1145 | -> pack( -side => 'left' ); | ||
| 1146 | my $b3 = $f2 -> Radiobutton ( -text => 'Terminal', | ||
| 1147 | -font => $menufont, | ||
| 1148 | -state => 'normal', | ||
| 1149 | -variable => \$self -> {outputmode}, | ||
| 1150 | -value => 'terminal' ) | ||
| 1151 | -> pack( -side => 'left' ); | ||
| 1152 | my $b4 = $f2 -> Radiobutton ( -text => 'New Workspace', | ||
| 1153 | -font => $menufont, | ||
| 1154 | -state => 'normal', | ||
| 1155 | -variable => \$self -> {outputmode}, | ||
| 1156 | -value => 'new' ) | ||
| 1157 | -> pack( -side => 'left' ); | ||
| 1158 | $b1 -> select; | ||
| 1159 | $f2 -> Label( -text => "\n" ) -> pack( -anchor => 'w' ); | ||
| 1160 | $f2 -> pack( -expand => '1', -fill => 'both', | ||
| 1161 | -ipady => 10); | ||
| 1162 | $f3 -> Label( -text => 'Output File: ', | ||
| 1163 | -font => $menufont ) | ||
| 1164 | -> pack( -side => 'left' ); | ||
| 1165 | my $ofil = $f3 -> Entry( -width => 40 ) | ||
| 1166 | -> pack( -side => 'left', -expand => '1', -fill => 'x', -padx => 5 ); | ||
| 1167 | $f3 -> Label( -text => "\n" ) -> pack( -anchor => 'w' ); | ||
| 1168 | $f3 -> pack( -expand => '1', -fill => 'x' ); | ||
| 1169 | my $resp = $dw -> Show; | ||
| 1170 | $self->filter( $cm -> get ); | ||
| 1171 | $self->outputfile( $ofil -> get ); | ||
| 1172 | return $resp; | ||
| 1173 | } | ||
| 1174 | |||
| 1175 | sub write_to_disk { | ||
| 1176 | my $self = shift; | ||
| 1177 | my $quit = shift; | ||
| 1178 | my $workspacename = $self -> name; | ||
| 1179 | my $t = $self -> {text}; | ||
| 1180 | my $tmppath = ($self -> filepath) . '.tmp'; | ||
| 1181 | my $perlpath = `which perl`; | ||
| 1182 | my ($contents, $object, $x, $y, $fg, $bg, $f, $resp, $wrap, $mb); | ||
| 1183 | my ($geometry, $width, $height, $sb, $ip); | ||
| 1184 | |||
| 1185 | if( $quit ) { | ||
| 1186 | if($t->{SubWidget}{workspacetext}{modified} !~ m/1/) { | ||
| 1187 | goto EXIT; | ||
| 1188 | } elsif ( ( $resp = &close_dialog($self) ) =~ m/Cancel/) { | ||
| 1189 | return; | ||
| 1190 | } elsif ($resp !~ m/Yes/ ) { | ||
| 1191 | goto EXIT; | ||
| 1192 | } | ||
| 1193 | } | ||
| 1194 | $self -> watchcursor; | ||
| 1195 | open FILE, ">>" . $tmppath; | ||
| 1196 | $contents = ($self -> text) -> get( '1.0', 'end' ); | ||
| 1197 | print FILE '#!' . $perlpath . "\n"; | ||
| 1198 | |||
| 1199 | $geometry= ($self -> window) -> geometry; | ||
| 1200 | $geometry =~ m/([0-9]+)x([0-9]+)\+([0-9]+)\+([0-9]+)/; | ||
| 1201 | $width = $1; $height = $2; $x = $3; $y = $4; | ||
| 1202 | |||
| 1203 | $wrap = $self -> wrap; | ||
| 1204 | $mb = $self -> menubarvisible; | ||
| 1205 | $sb = $self -> {scroll}; | ||
| 1206 | |||
| 1207 | $fg = ($self -> text) -> cget('-foreground'); | ||
| 1208 | $bg = ($self -> text) -> cget('-background'); | ||
| 1209 | $ip = ($self -> text) -> index( 'insert' ); | ||
| 1210 | $f = $self -> textfont; | ||
| 1211 | |||
| 1212 | # concatenate text. | ||
| 1213 | print FILE 'my $text = <<\'end-of-text\';' . "\n"; | ||
| 1214 | print FILE $contents; | ||
| 1215 | print FILE "end-of-text\n"; | ||
| 1216 | |||
| 1217 | # This re-creates on the default workspace object, except | ||
| 1218 | # the first line, the name, height and width, x and y orgs, | ||
| 1219 | # foreground and background colors, | ||
| 1220 | # and the initial empty text.; | ||
| 1221 | my @tmpobject = @Workspaceobject; | ||
| 1222 | grep { s/name\=\'\'/name=\'$workspacename\'/ } @tmpobject; | ||
| 1223 | grep { s/geometry\=\'.*\'/geometry=\'$geometry\'/ } @tmpobject; | ||
| 1224 | grep { s/wrap\=\'.*\'/wrap=\'$wrap\'/ } @tmpobject; | ||
| 1225 | grep { s/fg\=\'.*\'/fg=\'$fg\'/ } @tmpobject; | ||
| 1226 | grep { s/bg\=\'.*\'/bg=\'$bg\'/ } @tmpobject; | ||
| 1227 | grep { s/font\=\'.*\'/font=\'$f\'/ } @tmpobject; | ||
| 1228 | grep { s/menuvisible\=\'.*\'/menuvisible=\'$mb\'/ } @tmpobject; | ||
| 1229 | grep { s/scrollbars\=\'.*\'/scrollbars=\'$sb\'/ } @tmpobject; | ||
| 1230 | grep { s/insert\=\'.*\'/insert=\'$ip\'/ } @tmpobject; | ||
| 1231 | grep { s/#!\/usr\/bin\/perl// } @tmpobject; | ||
| 1232 | grep { s/my \$text=\'\'\;// } @tmpobject; | ||
| 1233 | foreach $line ( @tmpobject ) { print FILE $line . "\n"; }; | ||
| 1234 | close FILE; | ||
| 1235 | { | ||
| 1236 | my @remove_old = ( 'mv', $tmppath, $self -> filepath ); | ||
| 1237 | system( @remove_old ); | ||
| 1238 | } | ||
| 1239 | { | ||
| 1240 | # set restrictive perms, umask() seems to lock up | ||
| 1241 | chmod 0700, $self -> filepath; | ||
| 1242 | } | ||
| 1243 | $self -> defaultcursor; | ||
| 1244 | $t->{SubWidget}{workspacetext}{modified} = ''; | ||
| 1245 | EXIT: if ( $quit ) { $self -> window -> WmDeleteWindow; } | ||
| 1246 | |||
| 1247 | } | ||
| 1248 | |||
| 1249 | # Create a new Workspace executable if one doesn't exist. | ||
| 1250 | sub create { | ||
| 1251 | my ($workspacename) = ((@_)?@_:'Workspace'); | ||
| 1252 | my $Source; | ||
| 1253 | my $directory = ''; # Where are we. | ||
| 1254 | |||
| 1255 | # Make sure a workspace executable of the same basename | ||
| 1256 | # doesn't exist already. If it does, make the old workspace | ||
| 1257 | # a backup. | ||
| 1258 | if ( -e $workspacename ) { | ||
| 1259 | rename $workspacename, $workspacename . '.bak'; | ||
| 1260 | } | ||
| 1261 | |||
| 1262 | #Name the workspace... | ||
| 1263 | my @tmpobject = @Workspaceobject; | ||
| 1264 | grep { s/name\=\'\'/name\=\'$workspacename\'/ } @tmpobject; | ||
| 1265 | grep | ||
| 1266 | { s/Construct Tk::Workspace/Construct Tk::Workspace \'$workspacename\'\;/ } | ||
| 1267 | @tmpobject; | ||
| 1268 | |||
| 1269 | open FILE, ">" . $workspacename | ||
| 1270 | or die "Can't open Workspace " . $workspacename; | ||
| 1271 | # This creates on the default workspace object. | ||
| 1272 | |||
| 1273 | foreach $line ( @tmpobject ) { print FILE $line . "\n"; } | ||
| 1274 | close FILE; | ||
| 1275 | # Havn't figured out a way to use the umask function w/o | ||
| 1276 | # locking up... until then, set perms to rwx for owner only. | ||
| 1277 | chmod 0700, $workspacename; | ||
| 1278 | utime time, time, ($workspacename); | ||
| 1279 | return( $workspacename ); | ||
| 1280 | } | ||
| 1281 | |||
| 1282 | sub ws_copy { | ||
| 1283 | my $self = shift; | ||
| 1284 | my $selection; | ||
| 1285 | if ( ! (($self -> {text}) -> tagRanges('sel')) ) { return; } | ||
| 1286 | # per clipboard.txt, this asserts workspace text widget's | ||
| 1287 | # ownership of X display clipboard, and clears it. | ||
| 1288 | ($self -> {text}) -> clipboardClear; | ||
| 1289 | $selection = ($self -> {text}) | ||
| 1290 | -> SelectionGet(-selection => 'PRIMARY', | ||
| 1291 | -type => 'STRING' ); | ||
| 1292 | # Appends PRIMARY selection to X display clipboard. | ||
| 1293 | ($self -> {text}) -> clipboardAppend($selection); | ||
| 1294 | $clipboard = $selection; # our clipboard, not X's. | ||
| 1295 | return $selection; | ||
| 1296 | } | ||
| 1297 | |||
| 1298 | sub ws_cut { | ||
| 1299 | my $self = shift; | ||
| 1300 | my $selection; | ||
| 1301 | if ( ! (($self -> {text}) -> tagRanges('sel')) ) { return; } | ||
| 1302 | # per clipboard.txt, this asserts workspace text widget's | ||
| 1303 | # ownership of X display clipboard, and clears it. | ||
| 1304 | ($self -> {text}) -> clipboardClear; | ||
| 1305 | $selection = ($self -> {text}) | ||
| 1306 | -> SelectionGet(-selection => 'PRIMARY', | ||
| 1307 | -type => 'STRING' ); | ||
| 1308 | # Appends PRIMARY selection to X display clipboard. | ||
| 1309 | ($self -> {text}) -> clipboardAppend($selection); | ||
| 1310 | ($self ->{text}) -> | ||
| 1311 | delete(($self -> {text}) -> tagRanges('sel')); | ||
| 1312 | $clipboard = $selection; # our clipboard, not X's. | ||
| 1313 | $self -> {text} -> {SubWidget}{workspacetext}{modified} = '1'; | ||
| 1314 | return $selection; | ||
| 1315 | } | ||
| 1316 | |||
| 1317 | sub ws_paste { | ||
| 1318 | my $self = shift; | ||
| 1319 | my $selection; | ||
| 1320 | my $point; | ||
| 1321 | # Don't use CLIPBOARD because of a bug? in PerlTk... | ||
| 1322 | # | ||
| 1323 | # Checks PRIMARY selection, then X display clipboard, | ||
| 1324 | # and returns if neither is defined. | ||
| 1325 | # ($self -> {text}) -> | ||
| 1326 | # selectionOwn(-selection => 'CLIPBOARD'); | ||
| 1327 | # if ( ! (($self -> {text}) -> tagRanges('sel')) | ||
| 1328 | # or (($selection = ($self -> {text}) | ||
| 1329 | # -> SelectionGet(-selection => 'PRIMARY', | ||
| 1330 | # -type => 'STRING')) == '') ) { | ||
| 1331 | # return; | ||
| 1332 | # } | ||
| 1333 | # if ($self -> {text} -> tagRanges('sel')) { | ||
| 1334 | # $selection = ($self -> {text}) | ||
| 1335 | # -> SelectionGet(-selection => 'PRIMARY', | ||
| 1336 | # -type => 'STRING'); | ||
| 1337 | # } else { | ||
| 1338 | # $selection = $clipboard; | ||
| 1339 | # } | ||
| 1340 | $selection = ($self -> {text}) -> clipboardGet; | ||
| 1341 | $point = ($self -> {text}) -> index("insert"); | ||
| 1342 | ($self -> {text}) -> insert( $point, | ||
| 1343 | $selection); | ||
| 1344 | ($self -> {text}) -> see( 'insert' ); | ||
| 1345 | $self -> {text} -> {SubWidget}{workspacetext}{modified} = '1'; | ||
| 1346 | return $selection; | ||
| 1347 | } | ||
| 1348 | |||
| 1349 | sub ws_undo { | ||
| 1350 | my $self = shift; | ||
| 1351 | my $undo; | ||
| 1352 | $undo = ($self -> {text}) -> undo; | ||
| 1353 | $self ->{text}->{SubWidget}{workspacetext}{modified} = '1'; | ||
| 1354 | return $self | ||
| 1355 | } | ||
| 1356 | |||
| 1357 | sub evalselection { | ||
| 1358 | my $self = shift; | ||
| 1359 | my $s; | ||
| 1360 | my $result; | ||
| 1361 | $s = ($self -> {text}) | ||
| 1362 | -> SelectionGet( -selection => 'PRIMARY', | ||
| 1363 | -type => 'STRING' ); | ||
| 1364 | $result = eval $s; | ||
| 1365 | ($self -> {text}) -> | ||
| 1366 | insert( ( ( $self -> {text} ) -> | ||
| 1367 | tagNextrange( 'sel', '1.0', 'end' ))[1], $result ); | ||
| 1368 | } | ||
| 1369 | |||
| 1370 | sub about { | ||
| 1371 | my $self = shift; | ||
| 1372 | my $aboutdialog; | ||
| 1373 | my $title_text; | ||
| 1374 | my $version_text; | ||
| 1375 | my $name_text; | ||
| 1376 | my $mod_time; | ||
| 1377 | my $line_space; # blank label as separator. | ||
| 1378 | my @filestats = { $device, | ||
| 1379 | $inode, | ||
| 1380 | $nlink, | ||
| 1381 | $uid, | ||
| 1382 | $gid, | ||
| 1383 | $raw_device, | ||
| 1384 | $size, | ||
| 1385 | $atime, | ||
| 1386 | $mtime, | ||
| 1387 | $ctime, | ||
| 1388 | $blksize, | ||
| 1389 | $blocks }; | ||
| 1390 | |||
| 1391 | @filestats = stat ($self -> {name}); | ||
| 1392 | |||
| 1393 | $aboutdialog = | ||
| 1394 | ($self -> {window}) -> | ||
| 1395 | DialogBox( -buttons => ["Ok"], | ||
| 1396 | -title => 'About' ); | ||
| 1397 | $title_text = $aboutdialog -> add ('Label'); | ||
| 1398 | $version_text = $aboutdialog -> add ('Label'); | ||
| 1399 | $name_text = $aboutdialog -> add ('Label'); | ||
| 1400 | $mod_time = $aboutdialog -> add ('Label'); | ||
| 1401 | $line_space = $aboutdialog -> add ('Label'); | ||
| 1402 | |||
| 1403 | $title_text -> configure ( -font => $menufont, | ||
| 1404 | -text => | ||
| 1405 | 'Workspace.pm by rkiesling@mainmatter.com' ); | ||
| 1406 | $version_text -> configure ( -font => $menufont, | ||
| 1407 | -text => "Version: $VERSION"); | ||
| 1408 | $name_text -> configure ( -font => $menufont, | ||
| 1409 | -text => "\'" . $self -> {name} . "\'" ); | ||
| 1410 | $mod_time -> configure ( -font => $menufont, | ||
| 1411 | -text => 'Last File Modification: ' . | ||
| 1412 | localtime($filestats[9]) ); | ||
| 1413 | $line_space -> configure ( -font =>$menufont, | ||
| 1414 | -text => ''); | ||
| 1415 | |||
| 1416 | $name_text -> pack; | ||
| 1417 | $mod_time -> pack; | ||
| 1418 | $line_space -> pack; | ||
| 1419 | $title_text -> pack; | ||
| 1420 | $version_text -> pack; | ||
| 1421 | $aboutdialog -> Show; | ||
| 1422 | } | ||
| 1423 | |||
| 1424 | sub cmd_import { | ||
| 1425 | my( $ws, $args ) = @_; | ||
| 1426 | print "$args\n"; | ||
| 1427 | } | ||
| 1428 | |||
| 1429 | sub user_import { | ||
| 1430 | |||
| 1431 | my $self = shift; | ||
| 1432 | my $import; | ||
| 1433 | my $filedialog; | ||
| 1434 | my $filename = ''; | ||
| 1435 | my ($l, $unistr, $transtr, $mapobj, $ans, $tmpfile, $basename); | ||
| 1436 | my $nofiledialog; | ||
| 1437 | |||
| 1438 | if( ( $ans = $self -> hasunicode ) eq '1' ) { | ||
| 1439 | $l = Unicode::String -> new( '' ); | ||
| 1440 | $unistr = Unicode::String -> new( '' ); | ||
| 1441 | $mapobj = Unicode::Map -> new( "ISO-8859-1" ); | ||
| 1442 | } | ||
| 1443 | $filedialog = ($self -> {window}) | ||
| 1444 | -> RemoteFileSelect ( -directory => '.'); | ||
| 1445 | $filename = $filedialog -> Show; | ||
| 1446 | |||
| 1447 | $self -> watchcursor; | ||
| 1448 | if( $filename =~ /\:/ ) { | ||
| 1449 | my $hostname = $filedialog -> cget( -hostname ); | ||
| 1450 | my $uid = $filedialog -> cget( -userid ); | ||
| 1451 | my $passwd = $filedialog -> cget( -password ); | ||
| 1452 | my $transcript = $filedialog -> cget( -transcript ); | ||
| 1453 | $filename =~ s/^.*\://; | ||
| 1454 | $filename =~ /^.*\/(.*)/; | ||
| 1455 | $basename = $1; | ||
| 1456 | $tmpfile = "/tmp/$basename"; | ||
| 1457 | my $ftp = Net::FTP->new( $hostname, $transcript ); | ||
| 1458 | $ftp -> login( $uid, $passwd ); | ||
| 1459 | if ( ( $ftp -> get( $filename, $tmpfile ) ) ne $tmpfile ) { | ||
| 1460 | print "Could not create $hostname:$filename.\n"; | ||
| 1461 | } | ||
| 1462 | open IMPORT, "< $tmpfile" or &filenotfound($self); | ||
| 1463 | while ( $l = <IMPORT> ) { | ||
| 1464 | $unistr .= $l; | ||
| 1465 | } | ||
| 1466 | if( $ans eq '1' ) { | ||
| 1467 | $transtr = $mapobj -> from_unicode( $unistr ); | ||
| 1468 | ($self -> {text}) -> insert ( 'insert', $transtr ); | ||
| 1469 | } else { | ||
| 1470 | ($self -> {text}) -> insert ( 'insert', $unistr ); | ||
| 1471 | } | ||
| 1472 | $ftp -> quit; | ||
| 1473 | } elsif ( $filename ) { | ||
| 1474 | open IMPORT, "< $filename" or &filenotfound($self); | ||
| 1475 | while ( $l = <IMPORT> ) { | ||
| 1476 | $unistr .= $l; | ||
| 1477 | } | ||
| 1478 | if( $ans eq '1' ) { | ||
| 1479 | $transtr = $mapobj -> from_unicode( $unistr ); | ||
| 1480 | ($self -> {text}) -> insert ( 'insert', $transtr ); | ||
| 1481 | } else { | ||
| 1482 | ($self -> {text}) -> insert ( 'insert', $unistr ); | ||
| 1483 | } | ||
| 1484 | } | ||
| 1485 | ($self -> {text}) -> pack; | ||
| 1486 | close IMPORT; | ||
| 1487 | unlink( $tmpfile ) if -e $tmpfile; | ||
| 1488 | $self ->{text}->{SubWidget}{workspacetext}{modified} = '1'; | ||
| 1489 | $self -> defaultcursor; | ||
| 1490 | } | ||
| 1491 | |||
| 1492 | sub ws_export { | ||
| 1493 | my $self = shift; | ||
| 1494 | my $encoding = $self -> {encoding}; | ||
| 1495 | my ($filedialog, $ftp, $l, $l2, $mapobj); | ||
| 1496 | my $filename = undef; | ||
| 1497 | |||
| 1498 | $filedialog = ($self -> {window})->RemoteFileSelect ( -directory => '.' ); | ||
| 1499 | return if ! defined ( $filename = $filedialog -> Show ); | ||
| 1500 | $self -> watchcursor; | ||
| 1501 | if( $encoding =~ /utf16/ ) { | ||
| 1502 | $mapobj = Unicode::Map -> new('ISO-8859-1'); | ||
| 1503 | $l2 = Unicode::String -> new( '' ); | ||
| 1504 | } | ||
| 1505 | if( $filename =~ /\:/ ) { | ||
| 1506 | my $hostname = $filedialog -> cget( -hostname ); | ||
| 1507 | my $uid = $filedialog -> cget( -userid ); | ||
| 1508 | my $passwd = $filedialog -> cget( -password ); | ||
| 1509 | my $transcript = $filedialog -> cget( -transcript ); | ||
| 1510 | $ftp = Net::FTP->new( $hostname, $transcript ); | ||
| 1511 | $filename =~ s/^.*\://; | ||
| 1512 | $filename =~ /^.*\/(.*)/; | ||
| 1513 | my $basename = $1; | ||
| 1514 | my $tmpfile = "/tmp/$basename"; | ||
| 1515 | open OFN, "+> $tmpfile" or &filenotfound( $self ); | ||
| 1516 | if( $encoding =~ /utf16/ ) { | ||
| 1517 | $ftp -> binary; | ||
| 1518 | $l2 = $mapobj -> | ||
| 1519 | to_unicode( ($self -> {text}) -> get( '1.0', 'end' ) ); | ||
| 1520 | syswrite OFN, $l2, length( $l2 ); | ||
| 1521 | } else { | ||
| 1522 | print OFN ($self -> {text}) -> get( '1.0', 'end' ); | ||
| 1523 | } | ||
| 1524 | close OFN; | ||
| 1525 | $ftp -> login( $uid, $passwd ); | ||
| 1526 | if ( ( $ftp -> put( $tmpfile, $filename ) ) ne $filename ) { | ||
| 1527 | print "Could not create $hostname:$filename.\n"; | ||
| 1528 | } | ||
| 1529 | $ftp -> quit; | ||
| 1530 | unlink ($tmpfile); | ||
| 1531 | } else { | ||
| 1532 | open OFN, "+> $filename" or &filenotfound( $self ); | ||
| 1533 | if( $encoding =~ /utf16/ ) { | ||
| 1534 | $l2 = $mapobj -> to_unicode(($self -> {text}) -> get( '1.0', 'end' )); | ||
| 1535 | syswrite OFN, $l2, length( $l2 ); | ||
| 1536 | } else { | ||
| 1537 | print OFN ($self -> {text}) -> get( '1.0', 'end' ); | ||
| 1538 | } | ||
| 1539 | close OFN; | ||
| 1540 | } | ||
| 1541 | $self -> defaultcursor; | ||
| 1542 | } | ||
| 1543 | |||
| 1544 | sub close_dialog { | ||
| 1545 | my $self = shift; | ||
| 1546 | my $dialog; | ||
| 1547 | my $response; | ||
| 1548 | my $notice = "Save this workspace\nbefore closing?"; | ||
| 1549 | |||
| 1550 | $dialog = ( $self -> {window} ) | ||
| 1551 | -> Dialog( -title => 'Close Workspace', | ||
| 1552 | -text => $notice, -bitmap => 'question', | ||
| 1553 | -buttons => [qw/Yes No Cancel/]); | ||
| 1554 | $dialog -> configure (-font => $menufont); | ||
| 1555 | $dialog -> Subwidget ('B_Yes') -> configure (-font => $menufont); | ||
| 1556 | $dialog -> Subwidget ('B_No') -> configure (-font => $menufont); | ||
| 1557 | $dialog -> Subwidget ('B_Cancel') -> configure (-font => $menufont); | ||
| 1558 | return $response = $dialog -> Show; | ||
| 1559 | } | ||
| 1560 | |||
| 1561 | sub filenotfound { | ||
| 1562 | |||
| 1563 | my $self = shift; | ||
| 1564 | |||
| 1565 | my $nofiledialog = | ||
| 1566 | ($self -> {window}) -> | ||
| 1567 | DialogBox( -buttons => ["OK"], | ||
| 1568 | -title => 'File Error' ); | ||
| 1569 | my $filenotfound = $nofiledialog -> add ( 'Label'); | ||
| 1570 | $filenotfound -> configure ( -font => $menufont, | ||
| 1571 | -text => 'Could not open file.'); | ||
| 1572 | $filenotfound -> pack; | ||
| 1573 | $nofiledialog -> Show; | ||
| 1574 | } | ||
| 1575 | |||
| 1576 | sub my_directory { | ||
| 1577 | open PATHNAME, "pwd |"; | ||
| 1578 | read PATHNAME, $directory, 512; | ||
| 1579 | close PATHNAME; | ||
| 1580 | } | ||
| 1581 | |||
| 1582 | sub self_help { | ||
| 1583 | my $libfilename = &libname; | ||
| 1584 | my $help_text; | ||
| 1585 | my $helpwindow; | ||
| 1586 | my $textwidget; | ||
| 1587 | |||
| 1588 | open HELP, 'pod2text < '.$libfilename.' |' or $help_text = | ||
| 1589 | "Unable to process help text for $libfilename."; | ||
| 1590 | while (<HELP>) { | ||
| 1591 | $help_text .= $_; | ||
| 1592 | } | ||
| 1593 | close( HELP ); | ||
| 1594 | |||
| 1595 | $helpwindow = new MainWindow( -title => "$appfilename Help" ); | ||
| 1596 | my $textframe = $helpwindow -> Frame( -container => 0, | ||
| 1597 | -borderwidth => 1 ) -> pack; | ||
| 1598 | my $buttonframe = $helpwindow -> Frame( -container => 0, | ||
| 1599 | -borderwidth => 1 ) -> pack; | ||
| 1600 | $textwidget = $textframe | ||
| 1601 | -> Scrolled( 'Text', | ||
| 1602 | -font => $defaulttextfont, | ||
| 1603 | -scrollbars => 'e' ) -> pack( -fill => 'both', | ||
| 1604 | -expand => 1 ); | ||
| 1605 | $textwidget -> Subwidget('yscrollbar') -> configure(-width=>10); | ||
| 1606 | $textwidget -> Subwidget('xscrollbar') -> configure(-width=>10); | ||
| 1607 | $textwidget -> insert( 'end', $help_text ); | ||
| 1608 | |||
| 1609 | $buttonframe -> Button( -text => 'Dismiss', | ||
| 1610 | -font => $menufont, | ||
| 1611 | -default => 'active', | ||
| 1612 | -command => sub{$helpwindow -> DESTROY} ) -> | ||
| 1613 | pack; | ||
| 1614 | } | ||
| 1615 | |||
| 1616 | sub edit_help { | ||
| 1617 | my $libfilename = &libname; | ||
| 1618 | my $help_text; | ||
| 1619 | my $helpwindow; | ||
| 1620 | my $textwidget; | ||
| 1621 | |||
| 1622 | $libfilename =~ s/Workspace/WorkspaceText/; | ||
| 1623 | open HELP, 'pod2text < '.$libfilename.' |' or $help_text = | ||
| 1624 | "Unable to process help text for $libfilename."; | ||
| 1625 | while (<HELP>) { | ||
| 1626 | $help_text .= $_; | ||
| 1627 | } | ||
| 1628 | close( HELP ); | ||
| 1629 | |||
| 1630 | $helpwindow = new MainWindow( -title => "$appfilename Help" ); | ||
| 1631 | my $textframe = $helpwindow -> Frame( -container => 0, | ||
| 1632 | -borderwidth => 1 ) -> pack; | ||
| 1633 | my $buttonframe = $helpwindow -> Frame( -container => 0, | ||
| 1634 | -borderwidth => 1 ) -> pack; | ||
| 1635 | $textwidget = $textframe | ||
| 1636 | -> Scrolled( 'Text', | ||
| 1637 | -font => $defaulttextfont, | ||
| 1638 | -scrollbars => 'e' ) -> pack( -fill => 'both', | ||
| 1639 | -expand => 1 ); | ||
| 1640 | $textwidget -> Subwidget('yscrollbar') -> configure(-width=>10); | ||
| 1641 | $textwidget -> Subwidget('xscrollbar') -> configure(-width=>10); | ||
| 1642 | $textwidget -> insert( 'end', $help_text ); | ||
| 1643 | |||
| 1644 | $buttonframe -> Button( -text => 'Dismiss', | ||
| 1645 | -font => $menufont, | ||
| 1646 | -default => 'active', | ||
| 1647 | -command => sub{$helpwindow -> DESTROY} ) -> | ||
| 1648 | pack; | ||
| 1649 | } | ||
| 1650 | |||
| 1651 | # return the pathname to the Workspace.pm module. | ||
| 1652 | sub libname { | ||
| 1653 | my ($i, $val); | ||
| 1654 | foreach $i ( keys( %:: ) ) { | ||
| 1655 | $val = $::{$i}; | ||
| 1656 | if ( $val =~ /Workspace\.pm/ ) { | ||
| 1657 | $val =~ s/\*main::\_\<//; | ||
| 1658 | return $val; | ||
| 1659 | } | ||
| 1660 | } | ||
| 1661 | } | ||
| 1662 | |||
| 1663 | # should the name be "usecond"? | ||
| 1664 | sub requirecond { | ||
| 1665 | my ($modulename) = @_; | ||
| 1666 | my ($filename, $fullname, $result); | ||
| 1667 | $filename = $modulename; | ||
| 1668 | $filename .= '.pm' if $filename !~ /.pm$/; | ||
| 1669 | $filename =~ s/\:\:/\//; | ||
| 1670 | foreach my $prefix ( @INC ) { | ||
| 1671 | $fullname = "$prefix/$filename"; | ||
| 1672 | if( -f $fullname ) { | ||
| 1673 | eval "use $modulename"; | ||
| 1674 | return '1'; | ||
| 1675 | } | ||
| 1676 | } | ||
| 1677 | return ''; | ||
| 1678 | } | ||
| 1679 | |||
| 1680 | # for each subwidget | ||
| 1681 | sub watchcursor { | ||
| 1682 | my $app = shift; | ||
| 1683 | $app -> window -> Busy( -recurse => '1' ); | ||
| 1684 | } | ||
| 1685 | |||
| 1686 | sub defaultcursor { | ||
| 1687 | my $app = shift; | ||
| 1688 | $app -> window -> Unbusy( -recurse => '1' ); | ||
| 1689 | } | ||
| 1690 | |||
| 1691 | sub ws_search_again { | ||
| 1692 | my $self = shift; | ||
| 1693 | my ($t, @oplist, %opts, $opkey, $opval, $i, $firstmatch, $newinsert, | ||
| 1694 | $matchlength, @tkopts, $newcol, $row, $col ); | ||
| 1695 | push @oplist, @{$self -> {searchopts}}; | ||
| 1696 | for($i=0;$i<=@oplist;$i+=2){$opts{$oplist[$i]}=$oplist[$i+1]} | ||
| 1697 | return if $opts{'-searchstring'} eq ''; | ||
| 1698 | my $s = $opts{'-searchstring'}; | ||
| 1699 | ($opts{'-optioncase'} ne '1') ? push @tkopts, ('-nocase') : '' ; | ||
| 1700 | ($opts{'-optionregex'} eq '1') ? push @tkopts, ('-regex') : ' '; | ||
| 1701 | ($opts{'-optionbackward'} eq '1') ? push @tkopts, ('-backwards') : | ||
| 1702 | push @tkopts, ('-forward'); | ||
| 1703 | $t = $self -> text; | ||
| 1704 | $newinsert = $t -> index('insert'); | ||
| 1705 | ($row, $col) = split /\./, $newinsert; | ||
| 1706 | $matchlength = length($s); | ||
| 1707 | $newcol = $col+$matchlength; | ||
| 1708 | $col += 1; | ||
| 1709 | $newinsert="$row\.$col"; | ||
| 1710 | if(($opts{'-replacestring'} ne '' ) && ( $opts{-optionregex} ne '1')){ | ||
| 1711 | local $r = $opts{'-replacestring'}; | ||
| 1712 | $newinsert = $t -> index('insert'); | ||
| 1713 | ($row, $col) = split /\./, $newinsert; | ||
| 1714 | $col += 1; | ||
| 1715 | $newinsert="$row\.$col"; | ||
| 1716 | $firstmatch = $t -> search( @tkopts,$s,$newinsert); | ||
| 1717 | if( $firstmatch ne '' ) { | ||
| 1718 | ($row, $col) = split /\./, $firstmatch; | ||
| 1719 | $t -> markSet( 'insert', $firstmatch ); | ||
| 1720 | $t -> see( 'insert' ); | ||
| 1721 | $matchlength = length($s); | ||
| 1722 | $newcol = $col+$matchlength; | ||
| 1723 | for( $i = $col; $i < $newcol; $i++ ) { | ||
| 1724 | $t -> tagAdd( 'sel', "$row\.$i" ); | ||
| 1725 | } | ||
| 1726 | $t -> delete( $firstmatch, "$row\.$i" ); | ||
| 1727 | $t -> insert( $t -> index('insert'), $r ); | ||
| 1728 | $newcol=$col+length($r); | ||
| 1729 | for( $i = $col; $i < $newcol; $i++ ) { | ||
| 1730 | $t -> tagAdd( 'sel', "$row\.$i" ); | ||
| 1731 | } | ||
| 1732 | } | ||
| 1733 | } else { | ||
| 1734 | $t->tagRemove('sel',$t->index('insert'), "$row\.$newcol"); | ||
| 1735 | $firstmatch = $t -> search( @tkopts,$s,$newinsert); | ||
| 1736 | if( $firstmatch ne '' ) { | ||
| 1737 | ($row, $col) = split /\./, $firstmatch; | ||
| 1738 | $t -> markSet( 'insert', $firstmatch ); | ||
| 1739 | $t -> see( 'insert' ); | ||
| 1740 | $newcol = $col+$matchlength; | ||
| 1741 | for( $i = $col; $i < $newcol; $i++ ) { | ||
| 1742 | $t -> tagAdd( 'sel', "$row\.$i" ); | ||
| 1743 | } | ||
| 1744 | } | ||
| 1745 | } | ||
| 1746 | } | ||
| 1747 | |||
| 1748 | sub ws_search { | ||
| 1749 | my $self = shift; | ||
| 1750 | my ($t, @oplist, %opts, $opkey, $opval, $i, $firstmatch, $nextmatch, | ||
| 1751 | $matchlength, @tkopts, ); | ||
| 1752 | $t = $self -> text; | ||
| 1753 | push @oplist, @{$self -> {searchopts}}; | ||
| 1754 | my $d = $self -> window -> SearchDialog( @oplist ); | ||
| 1755 | @oplist = $d -> Show; | ||
| 1756 | for($i=0;$i<=@oplist;$i+=2){$opts{$oplist[$i]}=$oplist[$i+1]} | ||
| 1757 | return if $opts{'-searchstring'} eq ''; | ||
| 1758 | $t -> tagRemove( 'sel', '1.0', 'end' ); | ||
| 1759 | push @{$self -> {searchopts}}, @oplist; | ||
| 1760 | my $s = $opts{'-searchstring'}; | ||
| 1761 | ($opts{'-optioncase'} ne '1') ? push @tkopts, ('-nocase') : '' ; | ||
| 1762 | ($opts{'-optionregex'} eq '1') ? push @tkopts, ('-regex') : ' '; | ||
| 1763 | ($opts{'-optionbackward'} eq '1') ? push @tkopts, ('-backwards') : | ||
| 1764 | push @tkopts, ('-forward'); | ||
| 1765 | if(($opts{'-replacestring'} ne '' ) && ( $opts{-optionregex} ne '1')){ | ||
| 1766 | local $r = $opts{'-replacestring'}; | ||
| 1767 | $firstmatch = $t -> search( @tkopts,$s,$t->index('insert')); | ||
| 1768 | if( $firstmatch ne '' ) { | ||
| 1769 | local ($row, $col) = split /\./, $firstmatch; | ||
| 1770 | local $newcol; | ||
| 1771 | $t -> markSet( 'insert', $firstmatch ); | ||
| 1772 | $t -> see( 'insert' ); | ||
| 1773 | $matchlength = length($s); | ||
| 1774 | $newcol = $col+$matchlength; | ||
| 1775 | for( $i = $col; $i < $newcol; $i++ ) { | ||
| 1776 | $t -> tagAdd( 'sel', "$row\.$i" ); | ||
| 1777 | } | ||
| 1778 | $t -> delete( $firstmatch, "$row\.$i" ); | ||
| 1779 | $t -> insert( $t -> index('insert'), $r ); | ||
| 1780 | $newcol=$col+length($r); | ||
| 1781 | for( $i = $col; $i < $newcol; $i++ ) { | ||
| 1782 | $t -> tagAdd( 'sel', "$row\.$i" ); | ||
| 1783 | } | ||
| 1784 | } | ||
| 1785 | } else { | ||
| 1786 | $firstmatch = $t -> search( @tkopts,$s,$t->index('insert')); | ||
| 1787 | if( $firstmatch ne '' ) { | ||
| 1788 | local ($row, $col) = split /\./, $firstmatch; | ||
| 1789 | local $newcol; | ||
| 1790 | $t -> markSet( 'insert', $firstmatch ); | ||
| 1791 | $t -> see( 'insert' ); | ||
| 1792 | $matchlength = length($s); | ||
| 1793 | $newcol = $col+$matchlength; | ||
| 1794 | for( $i = $col; $i < $newcol; $i++ ) { | ||
| 1795 | $t -> tagAdd( 'sel', "$row\.$i" ); | ||
| 1796 | } | ||
| 1797 | } | ||
| 1798 | } | ||
| 1799 | } | ||
| 1800 | |||
| 1801 | 1; | ||
| 1802 | __END__ |