#! /usr/bin/perl # Like all good Perl programmers use strict; use warnings; # Tk elements we will be using use Tk; use Tk::Adjuster; use Tk::DialogBox; use Tk::ROText; use Tk::Text; # A hash to hold all Tk widgets my $w = {}; # Main window $w->{main} = MainWindow->new(-title => 'Tk Babelizer'); # Scrolled Text widget for the input. $w->{in} = $w->{main}->Scrolled( 'Text', -width => 60, -height => 15, -wrap => 'word', -scrollbars => 'osoe' )->pack( -fill => 'both', -expand => 1 ); # An adjuster bar so the user can change the in box to out box ratio. $w->{adj} = $w->{main}->Adjuster->packAfter($w->{in}, -side => 'top'); # Frame to hold the stuff on the bottom. $w->{bottom} = $w->{main}->Frame->pack(-fill => 'both', -expand => 1); # A frame within the bottom frame to hold some buttons. $w->{buttons} = $w->{bottom}->Frame->pack( -expand => 1 ); # A button to run the babelizer process $w->{buttons}->Button( -text => 'Translate me!', -command => [ \&babelizer, $w ] )->pack( -side => 'left' ); # A button to clear the windows $w->{buttons}->Button( -text => 'Clear the windows', -command => [ \&clear_windows, $w ] )->pack( -side => 'left' ); # A button to exit $w->{buttons}->Button( -text => 'Quit', -command => [ \&quit, $w ] )->pack( -side => 'left' ); # The read-only text output (Scrolled again) $w->{out} = $w->{bottom}->Scrolled( 'ROText', -width => 60, -height => 15, -wrap => 'word', -scrollbars => 'osoe' )->pack( -fill => 'both', -expand => 1 ); # Done creating widgets, enter event loop. MainLoop; # Events # This sub runs the babelizer sub babelizer { my $w = shift; # No globals here - $w is passed to us my $text = $w->{in}->get('1.0', 'end') || ''; $text =~ s/\w+/blah/g; $w->{out}->delete('1.0', 'end'); $w->{out}->insert('1.0', $text); $w->{main}->update; } # This sub just clears the windows sub clear_windows { my $w = shift; $w->{in}->delete('1.0', 'end'); $w->{out}->delete('1.0', 'end'); $w->{main}->update; } # Pop open a confirm window to quit sub quit { my $w = shift; my $dialog = $w->{main}->DialogBox( -title => 'Confirm exit', -buttons => [ 'OK', 'Cancel' ] ); $dialog->Label( -text => 'Are you sure you want to quit?' )->pack; my $popup = $dialog->Show; if ($popup eq "OK") { exit; } }