items.tcl
1 # items.tcl -- 2 # 3 # This demonstration script creates a canvas that displays the 4 # canvas item types. 5 6 if {![info exists widgetDemo]} { 7 error "This script should be run from the \"widget\" demo." 8 } 9 10 package require Tk 11 12 set w .items 13 catch {destroy $w} 14 toplevel $w 15 wm title $w "Canvas Item Demonstration" 16 wm iconname $w "Items" 17 positionWindow $w 18 set c $w.frame.c 19 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 Left-Button drag:\tmoves item under pointer.\n Middle-Button drag:\trepositions view.\n Right-Button drag:\tstrokes out area.\n Ctrl+f:\t\tprints items under area." 21 pack $w.msg -side top 22 23 ## See Code / Dismiss buttons 24 set btns [addSeeDismiss $w.buttons $w] 25 pack $btns -side bottom -fill x 26 27 frame $w.frame 28 pack $w.frame -side top -fill both -expand yes 29 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 ttk::scrollbar $w.frame.vscroll -command "$c yview" 35 ttk::scrollbar $w.frame.hscroll -orient horiz -command "$c xview" 36 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 45 46 # Display a 3x3 rectangular grid. 47 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 53 54 set font1 {Helvetica 12} 55 set font2 {Helvetica 24 bold} 56 if {[winfo depth $c] > 1} { 57 set blue DeepSkyBlue3 58 set red red 59 set bisque bisque3 60 set green SeaGreen3 61 } else { 62 set blue black 63 set red black 64 set bisque black 65 set green black 66 } 67 68 # Set up demos within each of the areas of the grid. 69 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 84 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] \ 93 -fill $red -tags item 94 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 {} -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 -outline {} -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 -fill $blue -outline {} -tags item 105 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 112 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 119 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 catch { 130 $c create text 25c 14c -font $font2 -anchor c -fill $red -angle 15 \ 131 -text "Angled characters" -tags item 132 } 133 134 $c create text 5c 16.2c -text Arcs -anchor n 135 $c create arc 0.5c 17c 7c 20c -fill $green -outline black \ 136 -start 45 -extent 270 -style pieslice -tags item 137 $c create arc 6.5c 17c 9.5c 20c -width 4m -style arc \ 138 -outline $blue -start -135 -extent 270 -tags item \ 139 -outlinestipple @[file join $tk_demoDirectory images gray25.xbm] 140 $c create arc 0.5c 20c 9.5c 24c -width 4m -style pieslice \ 141 -fill {} -outline $red -start 225 -extent -90 -tags item 142 $c create arc 5.5c 20.5c 9.5c 23.5c -width 4m -style chord \ 143 -fill $blue -outline {} -start 45 -extent 270 -tags item 144 145 $c create text 15c 16.2c -text "Bitmaps and Images" -anchor n 146 catch { 147 image create photo items.ousterhout \ 148 -file [file join $tk_demoDirectory images ouster.png] 149 image create photo items.ousterhout.active -format "png -alpha 0.5" \ 150 -file [file join $tk_demoDirectory images ouster.png] 151 $c create image 13c 20c -tags item -image items.ousterhout \ 152 -activeimage items.ousterhout.active 153 } 154 $c create bitmap 17c 18.5c -tags item \ 155 -bitmap @[file join $tk_demoDirectory images noletter.xbm] 156 $c create bitmap 17c 21.5c -tags item \ 157 -bitmap @[file join $tk_demoDirectory images letters.xbm] 158 159 $c create text 25c 16.2c -text Windows -anchor n 160 button $c.button -text "Press Me" -command "butPress $c $red" 161 $c create window 21c 18c -window $c.button -anchor nw -tags item 162 entry $c.entry -width 20 -relief sunken 163 $c.entry insert end "Edit this text" 164 $c create window 21c 21c -window $c.entry -anchor nw -tags item 165 scale $c.scale -from 0 -to 100 -length 6c -sliderlength .4c \ 166 -width .5c -tickinterval 0 167 $c create window 28.5c 17.5c -window $c.scale -anchor n -tags item 168 $c create text 21c 17.9c -text Button: -anchor sw 169 $c create text 21c 20.9c -text Entry: -anchor sw 170 $c create text 28.5c 17.4c -text Scale: -anchor s 171 172 # Set up event bindings for canvas: 173 174 $c bind item <Enter> "itemEnter $c" 175 $c bind item <Leave> "itemLeave $c" 176 if {[tk windowingsystem] eq "aqua" && ![package vsatisfies [package provide Tk] 8.7-]} { 177 bind $c <Button-2> "itemMark $c %x %y" 178 bind $c <B2-Motion> "itemStroke $c %x %y" 179 bind $c <Button-3> "$c scan mark %x %y" 180 bind $c <B3-Motion> "$c scan dragto %x %y" 181 } else { 182 bind $c <Button-2> "$c scan mark %x %y" 183 bind $c <B2-Motion> "$c scan dragto %x %y" 184 bind $c <Button-3> "itemMark $c %x %y" 185 bind $c <B3-Motion> "itemStroke $c %x %y" 186 } 187 bind $c <<NextChar>> "itemsUnderArea $c" 188 bind $c <Button-1> "itemStartDrag $c %x %y" 189 bind $c <B1-Motion> "itemDrag $c %x %y" 190 191 # Utility procedures for highlighting the item under the pointer: 192 193 proc itemEnter {c} { 194 global restoreCmd 195 196 if {[winfo depth $c] == 1} { 197 set restoreCmd {} 198 return 199 } 200 set type [$c type current] 201 if {$type == "window" || $type == "image"} { 202 set restoreCmd {} 203 return 204 } elseif {$type == "bitmap"} { 205 set bg [lindex [$c itemconf current -background] 4] 206 set restoreCmd [list $c itemconfig current -background $bg] 207 $c itemconfig current -background SteelBlue2 208 return 209 } elseif {$type == "image"} { 210 set restoreCmd [list $c itemconfig current -state normal] 211 $c itemconfig current -state active 212 return 213 } 214 set fill [lindex [$c itemconfig current -fill] 4] 215 if {(($type == "rectangle") || ($type == "oval") || ($type == "arc")) 216 && ($fill == "")} { 217 set outline [lindex [$c itemconfig current -outline] 4] 218 set restoreCmd "$c itemconfig current -outline $outline" 219 $c itemconfig current -outline SteelBlue2 220 } else { 221 set restoreCmd "$c itemconfig current -fill $fill" 222 $c itemconfig current -fill SteelBlue2 223 } 224 } 225 226 proc itemLeave {c} { 227 global restoreCmd 228 229 eval $restoreCmd 230 } 231 232 # Utility procedures for stroking out a rectangle and printing what's 233 # underneath the rectangle's area. 234 235 proc itemMark {c x y} { 236 global areaX1 areaY1 237 set areaX1 [$c canvasx $x] 238 set areaY1 [$c canvasy $y] 239 $c delete area 240 } 241 242 proc itemStroke {c x y} { 243 global areaX1 areaY1 areaX2 areaY2 244 set x [$c canvasx $x] 245 set y [$c canvasy $y] 246 if {($areaX1 != $x) && ($areaY1 != $y)} { 247 $c delete area 248 $c addtag area withtag [$c create rect $areaX1 $areaY1 $x $y \ 249 -outline black] 250 set areaX2 $x 251 set areaY2 $y 252 } 253 } 254 255 proc itemsUnderArea {c} { 256 global areaX1 areaY1 areaX2 areaY2 257 set area [$c find withtag area] 258 set items "" 259 foreach i [$c find enclosed $areaX1 $areaY1 $areaX2 $areaY2] { 260 if {[lsearch [$c gettags $i] item] >= 0} { 261 lappend items $i 262 } 263 } 264 puts stdout "Items enclosed by area: $items" 265 set items "" 266 foreach i [$c find overlapping $areaX1 $areaY1 $areaX2 $areaY2] { 267 if {[lsearch [$c gettags $i] item] >= 0} { 268 lappend items $i 269 } 270 } 271 puts stdout "Items overlapping area: $items" 272 } 273 274 set areaX1 0 275 set areaY1 0 276 set areaX2 0 277 set areaY2 0 278 279 # Utility procedures to support dragging of items. 280 281 proc itemStartDrag {c x y} { 282 global lastX lastY 283 set lastX [$c canvasx $x] 284 set lastY [$c canvasy $y] 285 } 286 287 proc itemDrag {c x y} { 288 global lastX lastY 289 set x [$c canvasx $x] 290 set y [$c canvasy $y] 291 $c move current [expr {$x-$lastX}] [expr {$y-$lastY}] 292 set lastX $x 293 set lastY $y 294 } 295 296 # Procedure that's invoked when the button embedded in the canvas 297 # is invoked. 298 299 proc butPress {w color} { 300 set i [$w create text 25c 18.1c -text "Oooohhh!!" -fill $color -anchor n] 301 after 500 "$w delete $i" 302 }