home *** CD-ROM | disk | FTP | other *** search
/ The Best of Windows 95.com 1996 September / WIN95_09962.iso / vrml / cp2b2x.exe / DATA.Z / items.tcl < prev    next >
Text File  |  1996-04-23  |  9KB  |  272 lines

  1. # items.tcl --
  2. #
  3. # This demonstration script creates a canvas that displays the
  4. # canvas item types.
  5. #
  6. # @(#) items.tcl 1.5 95/10/04 15:00:39
  7.  
  8. set w .items
  9. catch {destroy $w}
  10. toplevel $w
  11. wm title $w "Canvas Item Demonstration"
  12. wm iconname $w "Items"
  13. positionWindow $w
  14. set c $w.frame.c
  15.  
  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  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."
  17. pack $w.msg -side top
  18.  
  19. frame $w.buttons
  20. pack  $w.buttons -side bottom -expand y -fill x -pady 2m
  21. button $w.buttons.dismiss -text Dismiss -command "destroy $w"
  22. button $w.buttons.code -text "See Code" -command "showCode $w"
  23. pack $w.buttons.dismiss $w.buttons.code -side left -expand 1
  24.  
  25. frame $w.frame
  26. pack $w.frame -side top -fill both -expand yes
  27.  
  28. canvas $c -scrollregion {0c 0c 30c 24c} -width 15c -height 10c \
  29.     -relief sunken -borderwidth 2 \
  30.     -xscrollcommand "$w.frame.hscroll set" \
  31.     -yscrollcommand "$w.frame.vscroll set"
  32. scrollbar $w.frame.vscroll -command "$c yview"
  33. scrollbar $w.frame.hscroll -orient horiz -command "$c xview"
  34. pack $w.frame.hscroll -side bottom -fill x
  35. pack $w.frame.vscroll -side right -fill y
  36. pack $c -in $w.frame -expand yes -fill both
  37.  
  38. # Display a 3x3 rectangular grid.
  39.  
  40. $c create rect 0c 0c 30c 24c -width 2
  41. $c create line 0c 8c 30c 8c -width 2
  42. $c create line 0c 16c 30c 16c -width 2
  43. $c create line 10c 0c 10c 24c -width 2
  44. $c create line 20c 0c 20c 24c -width 2
  45.  
  46. set font1 -Adobe-Helvetica-Medium-R-Normal--*-120-*-*-*-*-*-*
  47. set font2 -Adobe-Helvetica-Bold-R-Normal--*-240-*-*-*-*-*-*
  48. if {[winfo depth $c] > 1} {
  49.     set blue DeepSkyBlue3
  50.     set red red
  51.     set bisque bisque3
  52.     set green SeaGreen3
  53. } else {
  54.     set blue black
  55.     set red black
  56.     set bisque black
  57.     set green black
  58. }
  59.  
  60. # Set up demos within each of the areas of the grid.
  61.  
  62. $c create text 5c .2c -text Lines -anchor n
  63. $c create line 1c 1c 3c 1c 1c 4c 3c 4c -width 2m -fill $blue \
  64.     -cap butt -join miter -tags item
  65. $c create line 4.67c 1c 4.67c 4c -arrow last -tags item
  66. $c create line 6.33c 1c 6.33c 4c -arrow both -tags item
  67. $c create line 5c 6c 9c 6c 9c 1c 8c 1c 8c 4.8c 8.8c 4.8c 8.8c 1.2c \
  68.     8.2c 1.2c 8.2c 4.6c 8.6c 4.6c 8.6c 1.4c 8.4c 1.4c 8.4c 4.4c \
  69.     -width 3 -fill $red -tags item
  70. $c create line 1c 5c 7c 5c 7c 7c 9c 7c -width .5c \
  71.     -stipple @$tk_library/demos/images/gray25.bmp \
  72.     -arrow both -arrowshape {15 15 7} -tags item
  73. $c create line 1c 7c 1.75c 5.8c 2.5c 7c 3.25c 5.8c 4c 7c -width .5c \
  74.     -cap round -join round -tags item
  75.  
  76. $c create text 15c .2c -text "Curves (smoothed lines)" -anchor n
  77. $c create line 11c 4c 11.5c 1c 13.5c 1c 14c 4c -smooth on \
  78.     -fill $blue -tags item
  79. $c create line 15.5c 1c 19.5c 1.5c 15.5c 4.5c 19.5c 4c -smooth on \
  80.     -arrow both -width 3 -tags item
  81. $c create line 12c 6c 13.5c 4.5c 16.5c 7.5c 18c 6c \
  82.     16.5c 4.5c 13.5c 7.5c 12c 6c -smooth on -width 3m -cap round \
  83.     -stipple @$tk_library/demos/images/gray25.bmp -fill $red -tags item
  84.  
  85. $c create text 25c .2c -text Polygons -anchor n
  86. $c create polygon 21c 1.0c 22.5c 1.75c 24c 1.0c 23.25c 2.5c \
  87.     24c 4.0c 22.5c 3.25c 21c 4.0c 21.75c 2.5c -fill $green \
  88.     -outline black -width 4 -tags item
  89. $c create polygon 25c 4c 25c 4c 25c 1c 26c 1c 27c 4c 28c 1c \
  90.     29c 1c 29c 4c 29c 4c -fill $red -smooth on -tags item
  91. $c create polygon 22c 4.5c 25c 4.5c 25c 6.75c 28c 6.75c \
  92.     28c 5.25c 24c 5.25c 24c 6.0c 26c 6c 26c 7.5c 22c 7.5c \
  93.     -stipple @$tk_library/demos/images/gray25.bmp \
  94.     -outline black -tags item
  95.  
  96. $c create text 5c 8.2c -text Rectangles -anchor n
  97. $c create rectangle 1c 9.5c 4c 12.5c -outline $red -width 3m -tags item
  98. $c create rectangle 0.5c 13.5c 4.5c 15.5c -fill $green -tags item
  99. $c create rectangle 6c 10c 9c 15c -outline {} \
  100.     -stipple @$tk_library/demos/images/gray25.bmp -fill $blue -tags item
  101.  
  102. $c create text 15c 8.2c -text Ovals -anchor n
  103. $c create oval 11c 9.5c 14c 12.5c -outline $red -width 3m -tags item
  104. $c create oval 10.5c 13.5c 14.5c 15.5c -fill $green -tags item
  105. $c create oval 16c 10c 19c 15c -outline {} \
  106.     -stipple @$tk_library/demos/images/gray25.bmp -fill $blue -tags item
  107.  
  108. $c create text 25c 8.2c -text Text -anchor n
  109. $c create rectangle 22.4c 8.9c 22.6c 9.1c
  110. $c create text 22.5c 9c -anchor n -font $font1 -width 4c \
  111.     -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
  112. $c create rectangle 25.4c 10.9c 25.6c 11.1c
  113. $c create text 25.5c 11c -anchor w -font $font1 -fill $blue \
  114.     -text "Several lines,\n each centered\nindividually,\nand all anchored\nat the left edge." \
  115.     -justify center -tags item
  116. $c create rectangle 24.9c 13.9c 25.1c 14.1c
  117. $c create text 25c 14c -font $font2 -anchor c -fill $red -stipple gray50 \
  118.     -text "Stippled characters" -tags item
  119.  
  120. $c create text 5c 16.2c -text Arcs -anchor n
  121. $c create arc 0.5c 17c 7c 20c -fill $green -outline black \
  122.     -start 45 -extent 270 -style pieslice -tags item
  123. $c create arc 6.5c 17c 9.5c 20c -width 4m -style arc \
  124.     -outline $blue -start -135 -extent 270 \
  125.     -outlinestipple @$tk_library/demos/images/gray25.bmp -tags item
  126. $c create arc 0.5c 20c 9.5c 24c -width 4m -style pieslice \
  127.     -fill {} -outline $red -start 225 -extent -90 -tags item
  128. $c create arc 5.5c 20.5c 9.5c 23.5c -width 4m -style chord \
  129.     -fill $blue -outline {} -start 45 -extent 270  -tags item
  130.  
  131. $c create text 15c 16.2c -text Bitmaps -anchor n
  132. $c create bitmap 13c 20c -bitmap @$tk_library/demos/images/face.bmp -tags item
  133. $c create bitmap 17c 18.5c \
  134.     -bitmap @$tk_library/demos/images/noletter.bmp -tags item
  135. $c create bitmap 17c 21.5c \
  136.     -bitmap @$tk_library/demos/images/letters.bmp -tags item
  137.  
  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.  
  151. # Set up event bindings for canvas:
  152.  
  153. $c bind item <Any-Enter> "itemEnter $c"
  154. $c bind item <Any-Leave> "itemLeave $c"
  155. bind $c <2> "$c scan mark %x %y"
  156. bind $c <B2-Motion> "$c scan dragto %x %y"
  157. bind $c <3> "itemMark $c %x %y"
  158. bind $c <B3-Motion> "itemStroke $c %x %y"
  159. bind $c <Control-f> "itemsUnderArea $c"
  160. bind $c <1> "itemStartDrag $c %x %y"
  161. bind $c <B1-Motion> "itemDrag $c %x %y"
  162.  
  163. # Utility procedures for highlighting the item under the pointer:
  164.  
  165. proc itemEnter {c} {
  166.     global restoreCmd
  167.  
  168.     if {[winfo depth $c] == 1} {
  169.     set restoreCmd {}
  170.     return
  171.     }
  172.     set type [$c type current]
  173.     if {$type == "window"} {
  174.     set restoreCmd {}
  175.     return
  176.     }
  177.     if {$type == "bitmap"} {
  178.     set bg [lindex [$c itemconf current -background] 4]
  179.     set restoreCmd [list $c itemconfig current -background $bg]
  180.     $c itemconfig current -background SteelBlue2
  181.     return
  182.     }
  183.     set fill [lindex [$c itemconfig current -fill] 4]
  184.     if {(($type == "rectangle") || ($type == "oval") || ($type == "arc"))
  185.         && ($fill == "")} {
  186.     set outline [lindex [$c itemconfig current -outline] 4]
  187.     set restoreCmd "$c itemconfig current -outline $outline"
  188.     $c itemconfig current -outline SteelBlue2
  189.     } else {
  190.     set restoreCmd "$c itemconfig current -fill $fill"
  191.     $c itemconfig current -fill SteelBlue2
  192.     }
  193. }
  194.  
  195. proc itemLeave {c} {
  196.     global restoreCmd
  197.  
  198.     eval $restoreCmd
  199. }
  200.  
  201. # Utility procedures for stroking out a rectangle and printing what's
  202. # underneath the rectangle's area.
  203.  
  204. proc itemMark {c x y} {
  205.     global areaX1 areaY1
  206.     set areaX1 [$c canvasx $x]
  207.     set areaY1 [$c canvasy $y]
  208.     $c delete area
  209. }
  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.  
  224. proc itemsUnderArea {c} {
  225.     global areaX1 areaY1 areaX2 areaY2
  226.     set area [$c find withtag area]
  227.     set items ""
  228.     foreach i [$c find enclosed $areaX1 $areaY1 $areaX2 $areaY2] {
  229.     if {[lsearch [$c gettags $i] item] != -1} {
  230.         lappend items $i
  231.     }
  232.     }
  233.     puts stdout "Items enclosed by area: $items"
  234.     set items ""
  235.     foreach i [$c find overlapping $areaX1 $areaY1 $areaX2 $areaY2] {
  236.     if {[lsearch [$c gettags $i] item] != -1} {
  237.         lappend items $i
  238.     }
  239.     }
  240.     puts stdout "Items overlapping area: $items"
  241. }
  242.  
  243. set areaX1 0
  244. set areaY1 0
  245. set areaX2 0
  246. set areaY2 0
  247.  
  248. # Utility procedures to support dragging of items.
  249.  
  250. proc itemStartDrag {c x y} {
  251.     global lastX lastY
  252.     set lastX [$c canvasx $x]
  253.     set lastY [$c canvasy $y]
  254. }
  255.  
  256. proc itemDrag {c x y} {
  257.     global lastX lastY
  258.     set x [$c canvasx $x]
  259.     set y [$c canvasy $y]
  260.     $c move current [expr $x-$lastX] [expr $y-$lastY]
  261.     set lastX $x
  262.     set lastY $y
  263. }
  264.  
  265. # Procedure that's invoked when the button embedded in the canvas
  266. # is invoked.
  267.  
  268. proc butPress {w color} {
  269.     set i [$w create text 25c 18.1c -text "Ouch!!" -fill $color -anchor n]
  270.     after 500 "$w delete $i"
  271. }
  272.