3 # This demonstration script creates a canvas that displays the
6 if {![info exists widgetDemo]} {
7 error "This script should be run from the \"widget\" demo."
15 wm title $w "Canvas Item Demonstration"
16 wm iconname $w "Items"
20 label $w.msg -font $font -wraplength 5i -justify left -text "This window contains a canvas widget with examples of the various kinds of items supported by canvases. The following operations are supported:\n Button-1 drag:\tmoves item under pointer.\n Button-2 drag:\trepositions view.\n Button-3 drag:\tstrokes out area.\n Ctrl+f:\t\tprints items under area."
23 ## See Code / Dismiss buttons
24 set btns [addSeeDismiss $w.buttons $w]
25 pack $btns -side bottom -fill x
28 pack $w.frame -side top -fill both -expand yes
30 canvas $c -scrollregion {0c 0c 30c 24c} -width 15c -height 10c \
31 -relief sunken -borderwidth 2 \
32 -xscrollcommand "$w.frame.hscroll set" \
33 -yscrollcommand "$w.frame.vscroll set"
34 scrollbar $w.frame.vscroll -command "$c yview"
35 scrollbar $w.frame.hscroll -orient horiz -command "$c xview"
37 grid $c -in $w.frame \
38 -row 0 -column 0 -rowspan 1 -columnspan 1 -sticky news
39 grid $w.frame.vscroll \
40 -row 0 -column 1 -rowspan 1 -columnspan 1 -sticky news
41 grid $w.frame.hscroll \
42 -row 1 -column 0 -rowspan 1 -columnspan 1 -sticky news
43 grid rowconfig $w.frame 0 -weight 1 -minsize 0
44 grid columnconfig $w.frame 0 -weight 1 -minsize 0
46 # Display a 3x3 rectangular grid.
48 $c create rect 0c 0c 30c 24c -width 2
49 $c create line 0c 8c 30c 8c -width 2
50 $c create line 0c 16c 30c 16c -width 2
51 $c create line 10c 0c 10c 24c -width 2
52 $c create line 20c 0c 20c 24c -width 2
54 set font1 {Helvetica 12}
55 set font2 {Helvetica 24 bold}
56 if {[winfo depth $c] > 1} {
68 # Set up demos within each of the areas of the grid.
70 $c create text 5c .2c -text Lines -anchor n
71 $c create line 1c 1c 3c 1c 1c 4c 3c 4c -width 2m -fill $blue \
72 -cap butt -join miter -tags item
73 $c create line 4.67c 1c 4.67c 4c -arrow last -tags item
74 $c create line 6.33c 1c 6.33c 4c -arrow both -tags item
75 $c create line 5c 6c 9c 6c 9c 1c 8c 1c 8c 4.8c 8.8c 4.8c 8.8c 1.2c \
76 8.2c 1.2c 8.2c 4.6c 8.6c 4.6c 8.6c 1.4c 8.4c 1.4c 8.4c 4.4c \
77 -width 3 -fill $red -tags item
78 # Main widget program sets variable tk_demoDirectory
79 $c create line 1c 5c 7c 5c 7c 7c 9c 7c -width .5c \
80 -stipple @[file join $tk_demoDirectory images gray25.xbm] \
81 -arrow both -arrowshape {15 15 7} -tags item
82 $c create line 1c 7c 1.75c 5.8c 2.5c 7c 3.25c 5.8c 4c 7c -width .5c \
83 -cap round -join round -tags item
85 $c create text 15c .2c -text "Curves (smoothed lines)" -anchor n
86 $c create line 11c 4c 11.5c 1c 13.5c 1c 14c 4c -smooth on \
87 -fill $blue -tags item
88 $c create line 15.5c 1c 19.5c 1.5c 15.5c 4.5c 19.5c 4c -smooth on \
89 -arrow both -width 3 -tags item
90 $c create line 12c 6c 13.5c 4.5c 16.5c 7.5c 18c 6c \
91 16.5c 4.5c 13.5c 7.5c 12c 6c -smooth on -width 3m -cap round \
92 -stipple @[file join $tk_demoDirectory images gray25.xbm] \
95 $c create text 25c .2c -text Polygons -anchor n
96 $c create polygon 21c 1.0c 22.5c 1.75c 24c 1.0c 23.25c 2.5c \
97 24c 4.0c 22.5c 3.25c 21c 4.0c 21.75c 2.5c -fill $green \
98 -outline black -width 4 -tags item
99 $c create polygon 25c 4c 25c 4c 25c 1c 26c 1c 27c 4c 28c 1c \
100 29c 1c 29c 4c 29c 4c -fill $red -smooth on -tags item
101 $c create polygon 22c 4.5c 25c 4.5c 25c 6.75c 28c 6.75c \
102 28c 5.25c 24c 5.25c 24c 6.0c 26c 6c 26c 7.5c 22c 7.5c \
103 -stipple @[file join $tk_demoDirectory images gray25.xbm] \
104 -outline black -tags item
106 $c create text 5c 8.2c -text Rectangles -anchor n
107 $c create rectangle 1c 9.5c 4c 12.5c -outline $red -width 3m -tags item
108 $c create rectangle 0.5c 13.5c 4.5c 15.5c -fill $green -tags item
109 $c create rectangle 6c 10c 9c 15c -outline {} \
110 -stipple @[file join $tk_demoDirectory images gray25.xbm] \
111 -fill $blue -tags item
113 $c create text 15c 8.2c -text Ovals -anchor n
114 $c create oval 11c 9.5c 14c 12.5c -outline $red -width 3m -tags item
115 $c create oval 10.5c 13.5c 14.5c 15.5c -fill $green -tags item
116 $c create oval 16c 10c 19c 15c -outline {} \
117 -stipple @[file join $tk_demoDirectory images gray25.xbm] \
118 -fill $blue -tags item
120 $c create text 25c 8.2c -text Text -anchor n
121 $c create rectangle 22.4c 8.9c 22.6c 9.1c
122 $c create text 22.5c 9c -anchor n -font $font1 -width 4c \
123 -text "A short string of text, word-wrapped, justified left, and anchored north (at the top). The rectangles show the anchor points for each piece of text." -tags item
124 $c create rectangle 25.4c 10.9c 25.6c 11.1c
125 $c create text 25.5c 11c -anchor w -font $font1 -fill $blue \
126 -text "Several lines,\n each centered\nindividually,\nand all anchored\nat the left edge." \
127 -justify center -tags item
128 $c create rectangle 24.9c 13.9c 25.1c 14.1c
129 $c create text 25c 14c -font $font2 -anchor c -fill $red -angle 15 \
130 -text "Angled characters" -tags item
132 $c create text 5c 16.2c -text Arcs -anchor n
133 $c create arc 0.5c 17c 7c 20c -fill $green -outline black \
134 -start 45 -extent 270 -style pieslice -tags item
135 $c create arc 6.5c 17c 9.5c 20c -width 4m -style arc \
136 -outline $blue -start -135 -extent 270 -tags item \
137 -outlinestipple @[file join $tk_demoDirectory images gray25.xbm]
138 $c create arc 0.5c 20c 9.5c 24c -width 4m -style pieslice \
139 -fill {} -outline $red -start 225 -extent -90 -tags item
140 $c create arc 5.5c 20.5c 9.5c 23.5c -width 4m -style chord \
141 -fill $blue -outline {} -start 45 -extent 270 -tags item
143 image create photo items.ousterhout \
144 -file [file join $tk_demoDirectory images ouster.png]
145 image create photo items.ousterhout.active -format "png -alpha 0.5" \
146 -file [file join $tk_demoDirectory images ouster.png]
147 $c create text 15c 16.2c -text "Bitmaps and Images" -anchor n
148 $c create image 13c 20c -tags item -image items.ousterhout \
149 -activeimage items.ousterhout.active
150 $c create bitmap 17c 18.5c -tags item \
151 -bitmap @[file join $tk_demoDirectory images noletter.xbm]
152 $c create bitmap 17c 21.5c -tags item \
153 -bitmap @[file join $tk_demoDirectory images letters.xbm]
155 $c create text 25c 16.2c -text Windows -anchor n
156 button $c.button -text "Press Me" -command "butPress $c $red"
157 $c create window 21c 18c -window $c.button -anchor nw -tags item
158 entry $c.entry -width 20 -relief sunken
159 $c.entry insert end "Edit this text"
160 $c create window 21c 21c -window $c.entry -anchor nw -tags item
161 scale $c.scale -from 0 -to 100 -length 6c -sliderlength .4c \
162 -width .5c -tickinterval 0
163 $c create window 28.5c 17.5c -window $c.scale -anchor n -tags item
164 $c create text 21c 17.9c -text Button: -anchor sw
165 $c create text 21c 20.9c -text Entry: -anchor sw
166 $c create text 28.5c 17.4c -text Scale: -anchor s
168 # Set up event bindings for canvas:
170 $c bind item <Any-Enter> "itemEnter $c"
171 $c bind item <Any-Leave> "itemLeave $c"
172 bind $c <2> "$c scan mark %x %y"
173 bind $c <B2-Motion> "$c scan dragto %x %y"
174 bind $c <3> "itemMark $c %x %y"
175 bind $c <B3-Motion> "itemStroke $c %x %y"
176 bind $c <<NextChar>> "itemsUnderArea $c"
177 bind $c <1> "itemStartDrag $c %x %y"
178 bind $c <B1-Motion> "itemDrag $c %x %y"
180 # Utility procedures for highlighting the item under the pointer:
185 if {[winfo depth $c] == 1} {
189 set type [$c type current]
190 if {$type == "window" || $type == "image"} {
193 } elseif {$type == "bitmap"} {
194 set bg [lindex [$c itemconf current -background] 4]
195 set restoreCmd [list $c itemconfig current -background $bg]
196 $c itemconfig current -background SteelBlue2
198 } elseif {$type == "image"} {
199 set restoreCmd [list $c itemconfig current -state normal]
200 $c itemconfig current -state active
203 set fill [lindex [$c itemconfig current -fill] 4]
204 if {(($type == "rectangle") || ($type == "oval") || ($type == "arc"))
206 set outline [lindex [$c itemconfig current -outline] 4]
207 set restoreCmd "$c itemconfig current -outline $outline"
208 $c itemconfig current -outline SteelBlue2
210 set restoreCmd "$c itemconfig current -fill $fill"
211 $c itemconfig current -fill SteelBlue2
221 # Utility procedures for stroking out a rectangle and printing what's
222 # underneath the rectangle's area.
224 proc itemMark {c x y} {
226 set areaX1 [$c canvasx $x]
227 set areaY1 [$c canvasy $y]
231 proc itemStroke {c x y} {
232 global areaX1 areaY1 areaX2 areaY2
233 set x [$c canvasx $x]
234 set y [$c canvasy $y]
235 if {($areaX1 != $x) && ($areaY1 != $y)} {
237 $c addtag area withtag [$c create rect $areaX1 $areaY1 $x $y \
244 proc itemsUnderArea {c} {
245 global areaX1 areaY1 areaX2 areaY2
246 set area [$c find withtag area]
248 foreach i [$c find enclosed $areaX1 $areaY1 $areaX2 $areaY2] {
249 if {[lsearch [$c gettags $i] item] != -1} {
253 puts stdout "Items enclosed by area: $items"
255 foreach i [$c find overlapping $areaX1 $areaY1 $areaX2 $areaY2] {
256 if {[lsearch [$c gettags $i] item] != -1} {
260 puts stdout "Items overlapping area: $items"
268 # Utility procedures to support dragging of items.
270 proc itemStartDrag {c x y} {
272 set lastX [$c canvasx $x]
273 set lastY [$c canvasy $y]
276 proc itemDrag {c x y} {
278 set x [$c canvasx $x]
279 set y [$c canvasy $y]
280 $c move current [expr {$x-$lastX}] [expr {$y-$lastY}]
285 # Procedure that's invoked when the button embedded in the canvas
288 proc butPress {w color} {
289 set i [$w create text 25c 18.1c -text "Oooohhh!!" -fill $color -anchor n]
290 after 500 "$w delete $i"