arrow.tcl 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. set demo_arrowInfo(bigLineStyle) "-fill SkyBlue1"
  118. set demo_arrowInfo(boxStyle) "-fill {} -outline black -width 1"
  119. set demo_arrowInfo(activeStyle) "-fill red -outline black -width 1"
  120. } else {
  121. # Main widget program sets variable tk_demoDirectory
  122. set demo_arrowInfo(bigLineStyle) "-fill black \
  123. -stipple @[file join $tk_demoDirectory images grey.25]"
  124. set demo_arrowInfo(boxStyle) "-fill {} -outline black -width 1"
  125. set demo_arrowInfo(activeStyle) "-fill black -outline black -width 1"
  126. }
  127. arrowSetup $c
  128. $c bind box <Enter> "$c itemconfigure current $demo_arrowInfo(activeStyle)"
  129. $c bind box <Leave> "$c itemconfigure current $demo_arrowInfo(boxStyle)"
  130. $c bind box <B1-Enter> " "
  131. $c bind box <B1-Leave> " "
  132. $c bind box1 <1> {set demo_arrowInfo(motionProc) arrowMove1}
  133. $c bind box2 <1> {set demo_arrowInfo(motionProc) arrowMove2}
  134. $c bind box3 <1> {set demo_arrowInfo(motionProc) arrowMove3}
  135. $c bind box <B1-Motion> "\$demo_arrowInfo(motionProc) $c %x %y"
  136. bind $c <Any-ButtonRelease-1> "arrowSetup $c"
  137. # arrowMove1 --
  138. # This procedure is called for each mouse motion event on box1 (the
  139. # one at the vertex of the arrow). It updates the controlling parameters
  140. # for the line and arrowhead.
  141. #
  142. # Arguments:
  143. # c - The name of the canvas window.
  144. # x, y - The coordinates of the mouse.
  145. proc arrowMove1 {c x y} {
  146. upvar #0 demo_arrowInfo v
  147. set newA [expr {($v(x2)+5-round([$c canvasx $x]))/10}]
  148. if {$newA < 0} {
  149. set newA 0
  150. }
  151. if {$newA > 25} {
  152. set newA 25
  153. }
  154. if {$newA != $v(a)} {
  155. $c move box1 [expr {10*($v(a)-$newA)}] 0
  156. set v(a) $newA
  157. }
  158. }
  159. # arrowMove2 --
  160. # This procedure is called for each mouse motion event on box2 (the
  161. # one at the trailing tip of the arrowhead). It updates the controlling
  162. # parameters for the line and arrowhead.
  163. #
  164. # Arguments:
  165. # c - The name of the canvas window.
  166. # x, y - The coordinates of the mouse.
  167. proc arrowMove2 {c x y} {
  168. upvar #0 demo_arrowInfo v
  169. set newB [expr {($v(x2)+5-round([$c canvasx $x]))/10}]
  170. if {$newB < 0} {
  171. set newB 0
  172. }
  173. if {$newB > 25} {
  174. set newB 25
  175. }
  176. set newC [expr {($v(y)+5-round([$c canvasy $y])-5*$v(width))/10}]
  177. if {$newC < 0} {
  178. set newC 0
  179. }
  180. if {$newC > 20} {
  181. set newC 20
  182. }
  183. if {($newB != $v(b)) || ($newC != $v(c))} {
  184. $c move box2 [expr {10*($v(b)-$newB)}] [expr {10*($v(c)-$newC)}]
  185. set v(b) $newB
  186. set v(c) $newC
  187. }
  188. }
  189. # arrowMove3 --
  190. # This procedure is called for each mouse motion event on box3 (the
  191. # one that controls the thickness of the line). It updates the
  192. # controlling parameters for the line and arrowhead.
  193. #
  194. # Arguments:
  195. # c - The name of the canvas window.
  196. # x, y - The coordinates of the mouse.
  197. proc arrowMove3 {c x y} {
  198. upvar #0 demo_arrowInfo v
  199. set newWidth [expr {($v(y)+2-round([$c canvasy $y]))/5}]
  200. if {$newWidth < 0} {
  201. set newWidth 0
  202. }
  203. if {$newWidth > 20} {
  204. set newWidth 20
  205. }
  206. if {$newWidth != $v(width)} {
  207. $c move box3 0 [expr {5*($v(width)-$newWidth)}]
  208. set v(width) $newWidth
  209. }
  210. }