| 1 |
## buttons.tcl |
| 2 |
## |
| 3 |
## demonstrates the simulation of a button array |
| 4 |
## |
| 5 |
## [email protected] |
| 6 |
## modifications made by [email protected] |
| 7 |
## Mdofied by John Cerney for perl/tk |
| 8 |
|
| 9 |
use Tk; |
| 10 |
|
| 11 |
use Tk::TableMatrix; |
| 12 |
|
| 13 |
use Data::Dumper qw( DumperX); |
| 14 |
my $top = MainWindow->new; |
| 15 |
|
| 16 |
my $tab = {}; |
| 17 |
my ($rows,$cols) = (10,10); # number of rows/cols |
| 18 |
|
| 19 |
# create the table |
| 20 |
my $t = $top->Scrolled('TableMatrix', -rows => $rows, -cols => $cols, |
| 21 |
-titlerows => 1, -titlecols => 1 , |
| 22 |
-roworigin => -1, -colorigin => -1 , |
| 23 |
-colwidth => 4 , |
| 24 |
-width => 8, -height => 8 , |
| 25 |
-variable => $tab , |
| 26 |
-flashmode => 'off', |
| 27 |
-cursor => 'top_left_arrow' , |
| 28 |
-borderwidth => 2 , |
| 29 |
-state => 'disabled' |
| 30 |
); |
| 31 |
$t->pack; |
| 32 |
|
| 33 |
# set up tags for the various states of the buttons |
| 34 |
$t->tagConfigure('OFF', -bg => 'red', -relief => 'raised'); |
| 35 |
$t->tagConfigure('ON', -bg => 'green', -relief => 'sunken'); |
| 36 |
$t->tagConfigure('sel', -bg => 'gray75', -relief => 'flat'); |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
# clean up if mouse leaves the widget |
| 41 |
$t->bind('<FocusOut>',sub{ |
| 42 |
my $w = shift; |
| 43 |
$w->selectionClear('all'); |
| 44 |
}); |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
# highlight the cell under the mouse |
| 50 |
$t->bind('<Motion>', sub{ |
| 51 |
my $w = shift; |
| 52 |
my $Ev = $w->XEvent; |
| 53 |
if( $w->selectionIncludes('@' . $Ev->x.",".$Ev->y)){ |
| 54 |
Tk->break; |
| 55 |
} |
| 56 |
$w->selectionClear('all'); |
| 57 |
$w->selectionSet('@' . $Ev->x.",".$Ev->y); |
| 58 |
Tk->break; |
| 59 |
## "break" prevents the call to TableMatrixCheckBorder |
| 60 |
}); |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
# mousebutton 1 toggles the value of the cell |
| 65 |
# use of "selection includes" would work here |
| 66 |
$t->bind('<1>', sub { |
| 67 |
my $w = shift; |
| 68 |
$w->focus; |
| 69 |
my $dude = $w->curselection; |
| 70 |
my ($rc) = @{$w->curselection}; |
| 71 |
my $var = $w->cget(-var); |
| 72 |
if( $var->{$rc} =~ /ON/ ){ |
| 73 |
$var->{$rc} = 'OFF'; |
| 74 |
$w->tagCell('OFF',$rc); |
| 75 |
} |
| 76 |
else{ |
| 77 |
$var->{$rc} = 'ON'; |
| 78 |
$w->tagCell('ON',$rc); |
| 79 |
} |
| 80 |
}); |
| 81 |
|
| 82 |
|
| 83 |
# inititialize the array, titles, and celltags |
| 84 |
for( $i = 0; $i < $rows; $i++){ |
| 85 |
$tab->{"$i,-1"} = $i; |
| 86 |
for( $j = 0; $j < $cols; $j++){ |
| 87 |
unless($i) { $tab->{"-1,$j"}= $j;}; |
| 88 |
$tab->{"$i,$j"} = "OFF"; |
| 89 |
$t->tagCell('OFF', "$i,$j"); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
Tk::MainLoop; |