#!/usr/bin/perl use strict; use warnings; use Tk; use Tk::Scrollbar; use Tk::Text; our ($textL, $textR, $yscroll, $xscrollleft, $xscrollright); my $main = Tk::MainWindow->new(); my $topframe = $main->Frame()->pack(); my $topleftframe = $topframe->Frame()->pack(-side => 'left'); my $toprightframe = $topframe->Frame()->pack(-side => 'left'); # Left again, because the yscroll is actually going on the right # and putting right here would put yscroll in the middle # Create left Text widget... $textL= $topleftframe->Text(-wrap => 'none' )->pack( -expand => 1, -fill => 'both', ); # Create right Text Widget... $textR = $toprightframe->Text(-width=>'20', -wrap => 'none' )->pack( -expand => 1, -fill => 'both', ); #################################### # scrollbars #################################### # Setup vertical scroll bar for both text windows $yscroll = $topframe->Scrollbar()->pack( -fill => 'y', -side => 'right', ); # Setup two individual horizontal scroll bars for the text windows $xscrollleft = $topleftframe->Scrollbar( -orient=>'horizontal', )->pack( -fill => 'x', -side => 'bottom', -expand => 1, ); $xscrollright = $toprightframe->Scrollbar( -orient=>'horizontal', )->pack( -fill => 'x', -side => 'bottom', -expand => 1, ); # Connect scrollbars to text widgets (and back again)... $xscrollright ->configure( -command => [ \&xscroll_right, $xscrollright, $textR]); $xscrollleft ->configure( -command => [ \&xscroll_left, $xscrollleft, $textL]); $yscroll->configure( -command => [ \&scroll_both, $yscroll, [$textL, $textR]]); $textL ->configure( -yscrollcommand => [ \&set_yscroll ]); $textR ->configure( -yscrollcommand => [ \&set_yscroll ]); $textL ->configure( -xscrollcommand => [ 'set', $xscrollleft]); $textR ->configure( -xscrollcommand => [ 'set', $xscrollright]); # Load some data into them (FOR TESTING ONLY) insert_data(); MainLoop(); # This handles updating yscroll, and making sure both the text # boxes are sync'ed when one of them scrolls via the cursor keys sub set_yscroll { $yscroll->set(@_); foreach my $w ($textL, $textR) { $w->yviewMoveto($_[0]); } } sub scroll_both { my ($sb,$wigs,@args) = @_; my $w; foreach $w (@$wigs) { $w->yview(@args); } } sub xscroll_right { my ($sb,$w,@args) = @_; $w->xview(@args); } sub xscroll_left { my ($sb,$w,@args) = @_; $w->xview(@args); } sub insert_data { my $testdata = <<'EOTESTDATA'; 0 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 Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah 0 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 Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah 0 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 EOTESTDATA $textL->insert('end', $testdata); $textR->insert('end', $testdata); }