| 1 |
## command.tcl |
| 2 |
## |
| 3 |
## This demo shows the use of the table widget's -command options |
| 4 |
## |
| 5 |
## [email protected] |
| 6 |
## Converted to perl/tk by John Cerney |
| 7 |
|
| 8 |
|
| 9 |
use Tk; |
| 10 |
|
| 11 |
use Tk::TableMatrix; |
| 12 |
|
| 13 |
my ($rows,$cols) = (10,10); # number of rows/cols |
| 14 |
my $top = MainWindow->new; |
| 15 |
|
| 16 |
# Sub to fill the array variable |
| 17 |
sub fill{ |
| 18 |
|
| 19 |
my ($array,$x,$y) = @_; |
| 20 |
my ($i,$j); |
| 21 |
for( $i = -$x; $i<$x; $i++){ |
| 22 |
for( $j = -$y; $j<$y; $j++){ |
| 23 |
$array->{"$i,$j"} = "$i x $j"; |
| 24 |
} |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
## Test out the use of a callback to define tags on rows and columns |
| 31 |
sub rowSub{ |
| 32 |
my $row = shift; |
| 33 |
return "OddRow" if( $row > 0 && $row % 2) |
| 34 |
} |
| 35 |
sub colSub{ |
| 36 |
my $col = shift; |
| 37 |
return "OddCol" if( $col > 0 && $col%2) ; |
| 38 |
} |
| 39 |
|
| 40 |
sub tblCmd{ |
| 41 |
my ($array, $set, $row,$col,$val) = @_; |
| 42 |
# my @args = @_; |
| 43 |
# print "In Table Command, Args = '".join("', '",@args)."'\n"; |
| 44 |
|
| 45 |
my $index = "$row,$col"; |
| 46 |
if( $set ){ |
| 47 |
$array->{$index} = $val; |
| 48 |
} |
| 49 |
else{ |
| 50 |
if( defined( $array->{$index})){ |
| 51 |
return $array->{$index}; |
| 52 |
} |
| 53 |
else{ |
| 54 |
return ''; |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
my $label = $top->Label(-text => "TableMatrix -command Example"); |
| 61 |
|
| 62 |
# Label the changes with the value of currentTest |
| 63 |
my $currentText = ''; |
| 64 |
my $currentLabel = $top->Label(-textvariable => \$currentText); |
| 65 |
|
| 66 |
# Entry that changes with the value of activeText |
| 67 |
my $activeText = ''; |
| 68 |
my $activeEntry = $top->Entry(-textvariable => \$activeText); |
| 69 |
|
| 70 |
my $arrayVar = {}; |
| 71 |
|
| 72 |
fill($arrayVar, $rows,$cols); # fill up the array variable |
| 73 |
|
| 74 |
|
| 75 |
my $t = $top->Scrolled('TableMatrix', -rows => $rows, -cols => $cols, |
| 76 |
-width => 6, -height => 6, |
| 77 |
-titlerows => 1, -titlecols => 2, |
| 78 |
-command => [\&tblCmd, $arrayVar], |
| 79 |
-roworigin => -1, -colorigin => -2, |
| 80 |
-rowtagcommand => \&rowSub, |
| 81 |
-coltagcommand => \&colSub, |
| 82 |
-selectmode => 'extended', |
| 83 |
-flashmode => 'on', |
| 84 |
-variable => $arrayVar, |
| 85 |
|
| 86 |
); |
| 87 |
|
| 88 |
$t->configure( -browsecommand => sub{ |
| 89 |
my ($index) = @_; |
| 90 |
$currentText = $index; |
| 91 |
$activeText = $t->get($index); |
| 92 |
}); |
| 93 |
|
| 94 |
|
| 95 |
$t->configure( -validate => 1, |
| 96 |
-validatecommand => sub{ |
| 97 |
my ($row,$col,$old,$new,$index) = @_; |
| 98 |
$activeText = $new; |
| 99 |
return 1; |
| 100 |
} |
| 101 |
); |
| 102 |
$t->configure( |
| 103 |
-selectioncommand => sub{ |
| 104 |
my ($NumRows,$Numcols,$selection,$noCells) = @_; |
| 105 |
my @args = @_; |
| 106 |
print "In Selection Command, Args = '".join("', '",@args)."'\n"; |
| 107 |
return $selection; |
| 108 |
} |
| 109 |
); |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
# hideous Color definitions here: |
| 115 |
$t->tagConfigure('OddRow', -bg => 'orange', -fg => 'purple'); |
| 116 |
$t->tagConfigure('OddCol', -bg => 'brown', -fg => 'pink'); |
| 117 |
|
| 118 |
$t->colWidth( -2 => 7, -1 => 7, 1=> 5, 2 => 8, 4=> 14); |
| 119 |
|
| 120 |
$label->pack( -expand => 1, -fill => 'both'); |
| 121 |
$currentLabel->pack( -expand => 1, -fill => 'both'); |
| 122 |
$activeEntry->pack( -expand => 1, -fill => 'both'); |
| 123 |
|
| 124 |
$t->pack(-expand => 1, -fill => 'both'); |
| 125 |
|
| 126 |
|
| 127 |
Tk::MainLoop; |