3 # This demonstration script creates a simple canvas that can be
4 # scrolled in two dimensions.
6 if {![info exists widgetDemo
]} {
7 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."
23 ## See Code / Dismiss buttons
24 set btns
[addSeeDismiss
$w.buttons
$w]
25 pack $btns -side bottom
-fill x
28 scrollbar $w.hscroll
-orient horiz
-command "$c xview"
29 scrollbar $w.vscroll
-command "$c yview"
30 canvas $c -relief sunken
-borderwidth 2 -scrollregion {-11c -11c 50c
20c
} \
31 -xscrollcommand "$w.hscroll set" \
32 -yscrollcommand "$w.vscroll set"
33 pack $w.
grid -expand yes
-fill both
-padx 1 -pady 1
34 grid rowconfig
$w.
grid 0 -weight 1 -minsize 0
35 grid columnconfig
$w.
grid 0 -weight 1 -minsize 0
37 grid $c -padx 1 -in $w.
grid -pady 1 \
38 -row 0 -column 0 -rowspan 1 -columnspan 1 -sticky news
39 grid $w.vscroll
-in $w.
grid -padx 1 -pady 1 \
40 -row 0 -column 1 -rowspan 1 -columnspan 1 -sticky news
41 grid $w.hscroll
-in $w.
grid -padx 1 -pady 1 \
42 -row 1 -column 0 -rowspan 1 -columnspan 1 -sticky news
45 set bg
[lindex [$c config
-bg] 4]
46 for {set i
0} {$i < 20} {incr i
} {
47 set x
[expr {-10 + 3*$i}]
48 for {set j
0; set y
-10} {$j < 10} {incr j
; incr y
3} {
49 $c create rect
${x
}c
${y
}c
[expr {$x+2}]c
[expr {$y+2}]c
\
50 -outline black
-fill $bg -tags rect
51 $c create
text [expr {$x+1}]c
[expr {$y+1}]c
-text "$i,$j" \
52 -anchor center
-tags text
56 $c bind all
<Any-Enter
> "scrollEnter $c"
57 $c bind all
<Any-Leave
> "scrollLeave $c"
58 $c bind all
<1> "scrollButton $c"
59 bind $c <2> "$c scan mark %x %y"
60 bind $c <B2-Motion
> "$c scan dragto %x %y"
61 if {[tk windowingsystem
] eq
"aqua"} {
62 bind $c <MouseWheel
> {
63 %W yview scroll
[expr {- (%D
)}] units
65 bind $c <Option-MouseWheel
> {
66 %W yview scroll
[expr {-10 * (%D
)}] units
68 bind $c <Shift-MouseWheel
> {
69 %W xview scroll
[expr {- (%D
)}] units
71 bind $c <Shift-Option-MouseWheel
> {
72 %W xview scroll
[expr {-10 * (%D
)}] units
76 proc scrollEnter
canvas {
78 set id
[$canvas find withtag current
]
79 if {[lsearch [$canvas gettags current
] text] >= 0} {
82 set oldFill
[lindex [$canvas itemconfig
$id -fill] 4]
83 if {[winfo depth
$canvas] > 1} {
84 $canvas itemconfigure
$id -fill SeaGreen1
86 $canvas itemconfigure
$id -fill black
87 $canvas itemconfigure
[expr {$id+1}] -fill white
91 proc scrollLeave
canvas {
93 set id
[$canvas find withtag current
]
94 if {[lsearch [$canvas gettags current
] text] >= 0} {
97 $canvas itemconfigure
$id -fill $oldFill
98 $canvas itemconfigure
[expr {$id+1}] -fill black
101 proc scrollButton
canvas {
103 set id
[$canvas find withtag current
]
104 if {[lsearch [$canvas gettags current
] text] < 0} {
105 set id
[expr {$id+1}]
107 puts stdout
"You buttoned at [lindex [$canvas itemconf $id -text] 4]"