3 # This demonstration script creates a simple canvas that can be
4 # scrolled in two dimensions.
6 # RCS: @(#) $Id: cscroll.tcl,v 1.3.4.1 2005/12/13 03:44:42 das Exp $
8 if {![info exists widgetDemo
]} {
9 error "This script should be run from the \"widget\" demo."
15 wm title
$w "Scrollable Canvas Demonstration"
16 wm iconname
$w "cscroll"
20 label $w.msg
-font $font -wraplength 4i
-justify left
-text "This window displays a canvas widget that can be scrolled either using the scrollbars or by dragging with button 2 in the canvas. If you click button 1 on one of the rectangles, its indices will be printed on stdout."
24 pack $w.buttons
-side bottom
-fill x
-pady 2m
25 button $w.buttons.dismiss
-text Dismiss
-command "destroy $w"
26 button $w.buttons.code
-text "See Code" -command "showCode $w"
27 pack $w.buttons.dismiss
$w.buttons.code
-side left
-expand 1
30 scrollbar $w.hscroll
-orient horiz
-command "$c xview"
31 scrollbar $w.vscroll
-command "$c yview"
32 canvas $c -relief sunken
-borderwidth 2 -scrollregion {-11c -11c 50c
20c
} \
33 -xscrollcommand "$w.hscroll set" \
34 -yscrollcommand "$w.vscroll set"
35 pack $w.
grid -expand yes
-fill both
-padx 1 -pady 1
36 grid rowconfig
$w.
grid 0 -weight 1 -minsize 0
37 grid columnconfig
$w.
grid 0 -weight 1 -minsize 0
39 grid $c -padx 1 -in $w.
grid -pady 1 \
40 -row 0 -column 0 -rowspan 1 -columnspan 1 -sticky news
41 grid $w.vscroll
-in $w.
grid -padx 1 -pady 1 \
42 -row 0 -column 1 -rowspan 1 -columnspan 1 -sticky news
43 grid $w.hscroll
-in $w.
grid -padx 1 -pady 1 \
44 -row 1 -column 0 -rowspan 1 -columnspan 1 -sticky news
47 set bg
[lindex [$c config
-bg] 4]
48 for {set i
0} {$i < 20} {incr i
} {
49 set x
[expr {-10 + 3*$i}]
50 for {set j
0; set y
-10} {$j < 10} {incr j
; incr y
3} {
51 $c create rect
${x
}c
${y
}c
[expr {$x+2}]c
[expr {$y+2}]c
\
52 -outline black
-fill $bg -tags rect
53 $c create
text [expr {$x+1}]c
[expr {$y+1}]c
-text "$i,$j" \
54 -anchor center
-tags text
58 $c bind all
<Any-Enter
> "scrollEnter $c"
59 $c bind all
<Any-Leave
> "scrollLeave $c"
60 $c bind all
<1> "scrollButton $c"
61 bind $c <2> "$c scan mark %x %y"
62 bind $c <B2-Motion
> "$c scan dragto %x %y"
63 if {[tk windowingsystem
] eq
"aqua"} {
64 bind $c <MouseWheel
> {
65 %W yview scroll
[expr {- (%D
)}] units
67 bind $c <Option-MouseWheel
> {
68 %W yview scroll
[expr {-10 * (%D
)}] units
70 bind $c <Shift-MouseWheel
> {
71 %W xview scroll
[expr {- (%D
)}] units
73 bind $c <Shift-Option-MouseWheel
> {
74 %W xview scroll
[expr {-10 * (%D
)}] units
78 proc scrollEnter
canvas {
80 set id
[$canvas find withtag current
]
81 if {[lsearch [$canvas gettags current
] text] >= 0} {
84 set oldFill
[lindex [$canvas itemconfig
$id -fill] 4]
85 if {[winfo depth
$canvas] > 1} {
86 $canvas itemconfigure
$id -fill SeaGreen1
88 $canvas itemconfigure
$id -fill black
89 $canvas itemconfigure
[expr {$id+1}] -fill white
93 proc scrollLeave
canvas {
95 set id
[$canvas find withtag current
]
96 if {[lsearch [$canvas gettags current
] text] >= 0} {
99 $canvas itemconfigure
$id -fill $oldFill
100 $canvas itemconfigure
[expr {$id+1}] -fill black
103 proc scrollButton
canvas {
105 set id
[$canvas find withtag current
]
106 if {[lsearch [$canvas gettags current
] text] < 0} {
107 set id
[expr {$id+1}]
109 puts stdout
"You buttoned at [lindex [$canvas itemconf $id -text] 4]"