items.tcl 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. # items.tcl --
  2. #
  3. # This demonstration script creates a canvas that displays the
  4. # canvas item types.
  5. if {![info exists widgetDemo]} {
  6. error "This script should be run from the \"widget\" demo."
  7. }
  8. package require Tk
  9. set w .items
  10. catch {destroy $w}
  11. toplevel $w
  12. wm title $w "Canvas Item Demonstration"
  13. wm iconname $w "Items"
  14. positionWindow $w
  15. set c $w.frame.c
  16. 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."
  17. pack $w.msg -side top
  18. ## See Code / Dismiss buttons
  19. set btns [addSeeDismiss $w.buttons $w]
  20. pack $btns -side bottom -fill x
  21. frame $w.frame
  22. pack $w.frame -side top -fill both -expand yes
  23. canvas $c -scrollregion {0c 0c 30c 24c} -width 15c -height 10c \
  24. -relief sunken -borderwidth 2 \
  25. -xscrollcommand "$w.frame.hscroll set" \
  26. -yscrollcommand "$w.frame.vscroll set"
  27. ttk::scrollbar $w.frame.vscroll -command "$c yview"
  28. ttk::scrollbar $w.frame.hscroll -orient horiz -command "$c xview"
  29. grid $c -in $w.frame \
  30. -row 0 -column 0 -rowspan 1 -columnspan 1 -sticky news
  31. grid $w.frame.vscroll \
  32. -row 0 -column 1 -rowspan 1 -columnspan 1 -sticky news
  33. grid $w.frame.hscroll \
  34. -row 1 -column 0 -rowspan 1 -columnspan 1 -sticky news
  35. grid rowconfig $w.frame 0 -weight 1 -minsize 0
  36. grid columnconfig $w.frame 0 -weight 1 -minsize 0
  37. # Display a 3x3 rectangular grid.
  38. $c create rect 0c 0c 30c 24c -width 2
  39. $c create line 0c 8c 30c 8c -width 2
  40. $c create line 0c 16c 30c 16c -width 2
  41. $c create line 10c 0c 10c 24c -width 2
  42. $c create line 20c 0c 20c 24c -width 2
  43. set font1 {Helvetica 12}
  44. set font2 {Helvetica 24 bold}
  45. if {[winfo depth $c] > 1} {
  46. set blue DeepSkyBlue3
  47. set red red
  48. set bisque bisque3
  49. set green SeaGreen3
  50. } else {
  51. set blue black
  52. set red black
  53. set bisque black
  54. set green black
  55. }
  56. # Set up demos within each of the areas of the grid.
  57. $c create text 5c .2c -text Lines -anchor n
  58. $c create line 1c 1c 3c 1c 1c 4c 3c 4c -width 2m -fill $blue \
  59. -cap butt -join miter -tags item
  60. $c create line 4.67c 1c 4.67c 4c -arrow last -tags item
  61. $c create line 6.33c 1c 6.33c 4c -arrow both -tags item
  62. $c create line 5c 6c 9c 6c 9c 1c 8c 1c 8c 4.8c 8.8c 4.8c 8.8c 1.2c \
  63. 8.2c 1.2c 8.2c 4.6c 8.6c 4.6c 8.6c 1.4c 8.4c 1.4c 8.4c 4.4c \
  64. -width 3 -fill $red -tags item
  65. # Main widget program sets variable tk_demoDirectory
  66. $c create line 1c 5c 7c 5c 7c 7c 9c 7c -width .5c \
  67. -stipple @[file join $tk_demoDirectory images gray25.xbm] \
  68. -arrow both -arrowshape {15 15 7} -tags item
  69. $c create line 1c 7c 1.75c 5.8c 2.5c 7c 3.25c 5.8c 4c 7c -width .5c \
  70. -cap round -join round -tags item
  71. $c create text 15c .2c -text "Curves (smoothed lines)" -anchor n
  72. $c create line 11c 4c 11.5c 1c 13.5c 1c 14c 4c -smooth on \
  73. -fill $blue -tags item
  74. $c create line 15.5c 1c 19.5c 1.5c 15.5c 4.5c 19.5c 4c -smooth on \
  75. -arrow both -width 3 -tags item
  76. $c create line 12c 6c 13.5c 4.5c 16.5c 7.5c 18c 6c \
  77. 16.5c 4.5c 13.5c 7.5c 12c 6c -smooth on -width 3m -cap round \
  78. -stipple @[file join $tk_demoDirectory images gray25.xbm] \
  79. -fill $red -tags item
  80. $c create text 25c .2c -text Polygons -anchor n
  81. $c create polygon 21c 1.0c 22.5c 1.75c 24c 1.0c 23.25c 2.5c \
  82. 24c 4.0c 22.5c 3.25c 21c 4.0c 21.75c 2.5c -fill $green \
  83. -outline {} -width 4 -tags item
  84. $c create polygon 25c 4c 25c 4c 25c 1c 26c 1c 27c 4c 28c 1c \
  85. 29c 1c 29c 4c 29c 4c -fill $red -outline {} -smooth on -tags item
  86. $c create polygon 22c 4.5c 25c 4.5c 25c 6.75c 28c 6.75c \
  87. 28c 5.25c 24c 5.25c 24c 6.0c 26c 6c 26c 7.5c 22c 7.5c \
  88. -stipple @[file join $tk_demoDirectory images gray25.xbm] \
  89. -fill $blue -outline {} -tags item
  90. $c create text 5c 8.2c -text Rectangles -anchor n
  91. $c create rectangle 1c 9.5c 4c 12.5c -outline $red -width 3m -tags item
  92. $c create rectangle 0.5c 13.5c 4.5c 15.5c -fill $green -tags item
  93. $c create rectangle 6c 10c 9c 15c -outline {} \
  94. -stipple @[file join $tk_demoDirectory images gray25.xbm] \
  95. -fill $blue -tags item
  96. $c create text 15c 8.2c -text Ovals -anchor n
  97. $c create oval 11c 9.5c 14c 12.5c -outline $red -width 3m -tags item
  98. $c create oval 10.5c 13.5c 14.5c 15.5c -fill $green -tags item
  99. $c create oval 16c 10c 19c 15c -outline {} \
  100. -stipple @[file join $tk_demoDirectory images gray25.xbm] \
  101. -fill $blue -tags item
  102. $c create text 25c 8.2c -text Text -anchor n
  103. $c create rectangle 22.4c 8.9c 22.6c 9.1c
  104. $c create text 22.5c 9c -anchor n -font $font1 -width 4c \
  105. -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
  106. $c create rectangle 25.4c 10.9c 25.6c 11.1c
  107. $c create text 25.5c 11c -anchor w -font $font1 -fill $blue \
  108. -text "Several lines,\n each centered\nindividually,\nand all anchored\nat the left edge." \
  109. -justify center -tags item
  110. $c create rectangle 24.9c 13.9c 25.1c 14.1c
  111. catch {
  112. $c create text 25c 14c -font $font2 -anchor c -fill $red -angle 15 \
  113. -text "Angled characters" -tags item
  114. }
  115. $c create text 5c 16.2c -text Arcs -anchor n
  116. $c create arc 0.5c 17c 7c 20c -fill $green -outline black \
  117. -start 45 -extent 270 -style pieslice -tags item
  118. $c create arc 6.5c 17c 9.5c 20c -width 4m -style arc \
  119. -outline $blue -start -135 -extent 270 -tags item \
  120. -outlinestipple @[file join $tk_demoDirectory images gray25.xbm]
  121. $c create arc 0.5c 20c 9.5c 24c -width 4m -style pieslice \
  122. -fill {} -outline $red -start 225 -extent -90 -tags item
  123. $c create arc 5.5c 20.5c 9.5c 23.5c -width 4m -style chord \
  124. -fill $blue -outline {} -start 45 -extent 270 -tags item
  125. $c create text 15c 16.2c -text "Bitmaps and Images" -anchor n
  126. catch {
  127. image create photo items.ousterhout \
  128. -file [file join $tk_demoDirectory images ouster.png]
  129. image create photo items.ousterhout.active -format "png -alpha 0.5" \
  130. -file [file join $tk_demoDirectory images ouster.png]
  131. $c create image 13c 20c -tags item -image items.ousterhout \
  132. -activeimage items.ousterhout.active
  133. }
  134. $c create bitmap 17c 18.5c -tags item \
  135. -bitmap @[file join $tk_demoDirectory images noletter.xbm]
  136. $c create bitmap 17c 21.5c -tags item \
  137. -bitmap @[file join $tk_demoDirectory images letters.xbm]
  138. $c create text 25c 16.2c -text Windows -anchor n
  139. button $c.button -text "Press Me" -command "butPress $c $red"
  140. $c create window 21c 18c -window $c.button -anchor nw -tags item
  141. entry $c.entry -width 20 -relief sunken
  142. $c.entry insert end "Edit this text"
  143. $c create window 21c 21c -window $c.entry -anchor nw -tags item
  144. scale $c.scale -from 0 -to 100 -length 6c -sliderlength .4c \
  145. -width .5c -tickinterval 0
  146. $c create window 28.5c 17.5c -window $c.scale -anchor n -tags item
  147. $c create text 21c 17.9c -text Button: -anchor sw
  148. $c create text 21c 20.9c -text Entry: -anchor sw
  149. $c create text 28.5c 17.4c -text Scale: -anchor s
  150. # Set up event bindings for canvas:
  151. $c bind item <Enter> "itemEnter $c"
  152. $c bind item <Leave> "itemLeave $c"
  153. if {[tk windowingsystem] eq "aqua" && ![package vsatisfies [package provide Tk] 8.7-]} {
  154. bind $c <Button-2> "itemMark $c %x %y"
  155. bind $c <B2-Motion> "itemStroke $c %x %y"
  156. bind $c <Button-3> "$c scan mark %x %y"
  157. bind $c <B3-Motion> "$c scan dragto %x %y"
  158. } else {
  159. bind $c <Button-2> "$c scan mark %x %y"
  160. bind $c <B2-Motion> "$c scan dragto %x %y"
  161. bind $c <Button-3> "itemMark $c %x %y"
  162. bind $c <B3-Motion> "itemStroke $c %x %y"
  163. }
  164. bind $c <<NextChar>> "itemsUnderArea $c"
  165. bind $c <Button-1> "itemStartDrag $c %x %y"
  166. bind $c <B1-Motion> "itemDrag $c %x %y"
  167. # Utility procedures for highlighting the item under the pointer:
  168. proc itemEnter {c} {
  169. global restoreCmd
  170. if {[winfo depth $c] == 1} {
  171. set restoreCmd {}
  172. return
  173. }
  174. set type [$c type current]
  175. if {$type == "window" || $type == "image"} {
  176. set restoreCmd {}
  177. return
  178. } elseif {$type == "bitmap"} {
  179. set bg [lindex [$c itemconf current -background] 4]
  180. set restoreCmd [list $c itemconfig current -background $bg]
  181. $c itemconfig current -background SteelBlue2
  182. return
  183. } elseif {$type == "image"} {
  184. set restoreCmd [list $c itemconfig current -state normal]
  185. $c itemconfig current -state active
  186. return
  187. }
  188. set fill [lindex [$c itemconfig current -fill] 4]
  189. if {(($type == "rectangle") || ($type == "oval") || ($type == "arc"))
  190. && ($fill == "")} {
  191. set outline [lindex [$c itemconfig current -outline] 4]
  192. set restoreCmd "$c itemconfig current -outline $outline"
  193. $c itemconfig current -outline SteelBlue2
  194. } else {
  195. set restoreCmd "$c itemconfig current -fill $fill"
  196. $c itemconfig current -fill SteelBlue2
  197. }
  198. }
  199. proc itemLeave {c} {
  200. global restoreCmd
  201. eval $restoreCmd
  202. }
  203. # Utility procedures for stroking out a rectangle and printing what's
  204. # underneath the rectangle's area.
  205. proc itemMark {c x y} {
  206. global areaX1 areaY1
  207. set areaX1 [$c canvasx $x]
  208. set areaY1 [$c canvasy $y]
  209. $c delete area
  210. }
  211. proc itemStroke {c x y} {
  212. global areaX1 areaY1 areaX2 areaY2
  213. set x [$c canvasx $x]
  214. set y [$c canvasy $y]
  215. if {($areaX1 != $x) && ($areaY1 != $y)} {
  216. $c delete area
  217. $c addtag area withtag [$c create rect $areaX1 $areaY1 $x $y \
  218. -outline black]
  219. set areaX2 $x
  220. set areaY2 $y
  221. }
  222. }
  223. proc itemsUnderArea {c} {
  224. global areaX1 areaY1 areaX2 areaY2
  225. set area [$c find withtag area]
  226. set items ""
  227. foreach i [$c find enclosed $areaX1 $areaY1 $areaX2 $areaY2] {
  228. if {[lsearch [$c gettags $i] item] >= 0} {
  229. lappend items $i
  230. }
  231. }
  232. puts stdout "Items enclosed by area: $items"
  233. set items ""
  234. foreach i [$c find overlapping $areaX1 $areaY1 $areaX2 $areaY2] {
  235. if {[lsearch [$c gettags $i] item] >= 0} {
  236. lappend items $i
  237. }
  238. }
  239. puts stdout "Items overlapping area: $items"
  240. }
  241. set areaX1 0
  242. set areaY1 0
  243. set areaX2 0
  244. set areaY2 0
  245. # Utility procedures to support dragging of items.
  246. proc itemStartDrag {c x y} {
  247. global lastX lastY
  248. set lastX [$c canvasx $x]
  249. set lastY [$c canvasy $y]
  250. }
  251. proc itemDrag {c x y} {
  252. global lastX lastY
  253. set x [$c canvasx $x]
  254. set y [$c canvasy $y]
  255. $c move current [expr {$x-$lastX}] [expr {$y-$lastY}]
  256. set lastX $x
  257. set lastY $y
  258. }
  259. # Procedure that's invoked when the button embedded in the canvas
  260. # is invoked.
  261. proc butPress {w color} {
  262. set i [$w create text 25c 18.1c -text "Oooohhh!!" -fill $color -anchor n]
  263. after 500 "$w delete $i"
  264. }