arrow.tcl 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. # arrow.tcl --
  2. #
  3. # This demonstration script creates a canvas widget that displays a
  4. # large line with an arrowhead whose shape can be edited interactively.
  5. if {![info exists widgetDemo]} {
  6. error "This script should be run from the \"widget\" demo."
  7. }
  8. package require Tk
  9. # arrowSetup --
  10. # This procedure regenerates all the text and graphics in the canvas
  11. # window. It's called when the canvas is initially created, and also
  12. # whenever any of the parameters of the arrow head are changed
  13. # interactively.
  14. #
  15. # Arguments:
  16. # c - Name of the canvas widget.
  17. proc arrowSetup c {
  18. upvar #0 demo_arrowInfo v
  19. # Remember the current box, if there is one.
  20. set tags [$c gettags current]
  21. if {$tags != ""} {
  22. set cur [lindex $tags [lsearch -glob $tags box?]]
  23. } else {
  24. set cur ""
  25. }
  26. # Create the arrow and outline.
  27. $c delete all
  28. eval {$c create line $v(x1) $v(y) $v(x2) $v(y) -arrow last \
  29. -width [expr {10*$v(width)}] -arrowshape [list \
  30. [expr {10*$v(a)}] [expr {10*$v(b)}] [expr {10*$v(c)}]]} \
  31. $v(bigLineStyle)
  32. set xtip [expr {$v(x2)-10*$v(b)}]
  33. set deltaY [expr {10*$v(c)+5*$v(width)}]
  34. $c create line $v(x2) $v(y) $xtip [expr {$v(y)+$deltaY}] \
  35. [expr {$v(x2)-10*$v(a)}] $v(y) $xtip [expr {$v(y)-$deltaY}] \
  36. $v(x2) $v(y) -width 2 -capstyle round -joinstyle round
  37. # Create the boxes for reshaping the line and arrowhead.
  38. eval {$c create rect [expr {$v(x2)-10*$v(a)-5}] [expr {$v(y)-5}] \
  39. [expr {$v(x2)-10*$v(a)+5}] [expr {$v(y)+5}] \
  40. -tags {box1 box}} $v(boxStyle)
  41. eval {$c create rect [expr {$xtip-5}] [expr {$v(y)-$deltaY-5}] \
  42. [expr {$xtip+5}] [expr {$v(y)-$deltaY+5}] \
  43. -tags {box2 box}} $v(boxStyle)
  44. eval {$c create rect [expr {$v(x1)-5}] [expr {$v(y)-5*$v(width)-5}] \
  45. [expr {$v(x1)+5}] [expr {$v(y)-5*$v(width)+5}] \
  46. -tags {box3 box}} $v(boxStyle)
  47. if {$cur != ""} {
  48. eval $c itemconfigure $cur $v(activeStyle)
  49. }
  50. # Create three arrows in actual size with the same parameters
  51. $c create line [expr {$v(x2)+50}] 0 [expr {$v(x2)+50}] 1000 \
  52. -width 2
  53. set tmp [expr {$v(x2)+100}]
  54. $c create line $tmp [expr {$v(y)-125}] $tmp [expr {$v(y)-75}] \
  55. -width $v(width) \
  56. -arrow both -arrowshape "$v(a) $v(b) $v(c)"
  57. $c create line [expr {$tmp-25}] $v(y) [expr {$tmp+25}] $v(y) \
  58. -width $v(width) \
  59. -arrow both -arrowshape "$v(a) $v(b) $v(c)"
  60. $c create line [expr {$tmp-25}] [expr {$v(y)+75}] [expr {$tmp+25}] \
  61. [expr {$v(y)+125}] -width $v(width) \
  62. -arrow both -arrowshape "$v(a) $v(b) $v(c)"
  63. # Create a bunch of other arrows and text items showing the
  64. # current dimensions.
  65. set tmp [expr {$v(x2)+10}]
  66. $c create line $tmp [expr {$v(y)-5*$v(width)}] \
  67. $tmp [expr {$v(y)-$deltaY}] \
  68. -arrow both -arrowshape $v(smallTips)
  69. $c create text [expr {$v(x2)+15}] [expr {$v(y)-$deltaY+5*$v(c)}] \
  70. -text $v(c) -anchor w
  71. set tmp [expr {$v(x1)-10}]
  72. $c create line $tmp [expr {$v(y)-5*$v(width)}] \
  73. $tmp [expr {$v(y)+5*$v(width)}] \
  74. -arrow both -arrowshape $v(smallTips)
  75. $c create text [expr {$v(x1)-15}] $v(y) -text $v(width) -anchor e
  76. set tmp [expr {$v(y)+5*$v(width)+10*$v(c)+10}]
  77. $c create line [expr {$v(x2)-10*$v(a)}] $tmp $v(x2) $tmp \
  78. -arrow both -arrowshape $v(smallTips)
  79. $c create text [expr {$v(x2)-5*$v(a)}] [expr {$tmp+5}] \
  80. -text $v(a) -anchor n
  81. set tmp [expr {$tmp+25}]
  82. $c create line [expr {$v(x2)-10*$v(b)}] $tmp $v(x2) $tmp \
  83. -arrow both -arrowshape $v(smallTips)
  84. $c create text [expr {$v(x2)-5*$v(b)}] [expr {$tmp+5}] \
  85. -text $v(b) -anchor n
  86. $c create text $v(x1) 310 -text "-width $v(width)" \
  87. -anchor w -font {Helvetica 18}
  88. $c create text $v(x1) 330 -text "-arrowshape {$v(a) $v(b) $v(c)}" \
  89. -anchor w -font {Helvetica 18}
  90. incr v(count)
  91. }
  92. set w .arrow
  93. catch {destroy $w}
  94. toplevel $w
  95. wm title $w "Arrowhead Editor Demonstration"
  96. wm iconname $w "arrow"
  97. positionWindow $w
  98. set c $w.c
  99. label $w.msg -font $font -wraplength 5i -justify left -text "This widget allows you to experiment with different widths and arrowhead shapes for lines in canvases. To change the line width or the shape of the arrowhead, drag any of the three boxes attached to the oversized arrow. The arrows on the right give examples at normal scale. The text at the bottom shows the configuration options as you'd enter them for a canvas line item."
  100. pack $w.msg -side top
  101. ## See Code / Dismiss buttons
  102. set btns [addSeeDismiss $w.buttons $w]
  103. pack $btns -side bottom -fill x
  104. canvas $c -width 500 -height 350 -relief sunken -borderwidth 2
  105. pack $c -expand yes -fill both
  106. set demo_arrowInfo(a) 8
  107. set demo_arrowInfo(b) 10
  108. set demo_arrowInfo(c) 3
  109. set demo_arrowInfo(width) 2
  110. set demo_arrowInfo(motionProc) arrowMoveNull
  111. set demo_arrowInfo(x1) 40
  112. set demo_arrowInfo(x2) 350
  113. set demo_arrowInfo(y) 150
  114. set demo_arrowInfo(smallTips) {5 5 2}
  115. set demo_arrowInfo(count) 0
  116. if {[winfo depth $c] > 1} {
  117. if {[tk windowingsystem] eq "aqua"} {
  118. set demo_arrowInfo(bigLineStyle) "-fill systemSelectedTextBackgroundColor"
  119. } else {
  120. set demo_arrowInfo(bigLineStyle) "-fill LightSeaGreen"
  121. }
  122. set demo_arrowInfo(boxStyle) "-fill {} -width 1"
  123. set demo_arrowInfo(activeStyle) "-fill red -width 1"
  124. } else {
  125. # Main widget program sets variable tk_demoDirectory
  126. set demo_arrowInfo(bigLineStyle) "-fill black \
  127. -stipple @[file join $tk_demoDirectory images grey.25]"
  128. set demo_arrowInfo(boxStyle) "-fill {} -outline black -width 1"
  129. set demo_arrowInfo(activeStyle) "-fill black -outline black -width 1"
  130. }
  131. arrowSetup $c
  132. $c bind box <Enter> "$c itemconfigure current $demo_arrowInfo(activeStyle)"
  133. $c bind box <Leave> "$c itemconfigure current $demo_arrowInfo(boxStyle)"
  134. $c bind box <B1-Enter> " "
  135. $c bind box <B1-Leave> " "
  136. $c bind box1 <Button-1> {set demo_arrowInfo(motionProc) arrowMove1}
  137. $c bind box2 <Button-1> {set demo_arrowInfo(motionProc) arrowMove2}
  138. $c bind box3 <Button-1> {set demo_arrowInfo(motionProc) arrowMove3}
  139. $c bind box <B1-Motion> "\$demo_arrowInfo(motionProc) $c %x %y"
  140. bind $c <ButtonRelease-1> "arrowSetup $c"
  141. # arrowMove1 --
  142. # This procedure is called for each mouse motion event on box1 (the
  143. # one at the vertex of the arrow). It updates the controlling parameters
  144. # for the line and arrowhead.
  145. #
  146. # Arguments:
  147. # c - The name of the canvas window.
  148. # x, y - The coordinates of the mouse.
  149. proc arrowMove1 {c x y} {
  150. upvar #0 demo_arrowInfo v
  151. set newA [expr {($v(x2)+5-round([$c canvasx $x]))/10}]
  152. if {$newA < 0} {
  153. set newA 0
  154. }
  155. if {$newA > 25} {
  156. set newA 25
  157. }
  158. if {$newA != $v(a)} {
  159. $c move box1 [expr {10*($v(a)-$newA)}] 0
  160. set v(a) $newA
  161. }
  162. }
  163. # arrowMove2 --
  164. # This procedure is called for each mouse motion event on box2 (the
  165. # one at the trailing tip of the arrowhead). It updates the controlling
  166. # parameters for the line and arrowhead.
  167. #
  168. # Arguments:
  169. # c - The name of the canvas window.
  170. # x, y - The coordinates of the mouse.
  171. proc arrowMove2 {c x y} {
  172. upvar #0 demo_arrowInfo v
  173. set newB [expr {($v(x2)+5-round([$c canvasx $x]))/10}]
  174. if {$newB < 0} {
  175. set newB 0
  176. }
  177. if {$newB > 25} {
  178. set newB 25
  179. }
  180. set newC [expr {($v(y)+5-round([$c canvasy $y])-5*$v(width))/10}]
  181. if {$newC < 0} {
  182. set newC 0
  183. }
  184. if {$newC > 20} {
  185. set newC 20
  186. }
  187. if {($newB != $v(b)) || ($newC != $v(c))} {
  188. $c move box2 [expr {10*($v(b)-$newB)}] [expr {10*($v(c)-$newC)}]
  189. set v(b) $newB
  190. set v(c) $newC
  191. }
  192. }
  193. # arrowMove3 --
  194. # This procedure is called for each mouse motion event on box3 (the
  195. # one that controls the thickness of the line). It updates the
  196. # controlling parameters for the line and arrowhead.
  197. #
  198. # Arguments:
  199. # c - The name of the canvas window.
  200. # x, y - The coordinates of the mouse.
  201. proc arrowMove3 {c x y} {
  202. upvar #0 demo_arrowInfo v
  203. set newWidth [expr {($v(y)+2-round([$c canvasy $y]))/5}]
  204. if {$newWidth < 0} {
  205. set newWidth 0
  206. }
  207. if {$newWidth > 20} {
  208. set newWidth 20
  209. }
  210. if {$newWidth != $v(width)} {
  211. $c move box3 0 [expr {5*($v(width)-$newWidth)}]
  212. set v(width) $newWidth
  213. }
  214. }