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

  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. #
  6. # @(#) arrow.tcl 1.3 95/06/29 14:26:02
  7.  
  8. # arrowSetup --
  9. # This procedure regenerates all the text and graphics in the canvas
  10. # window.  It's called when the canvas is initially created, and also
  11. # whenever any of the parameters of the arrow head are changed
  12. # interactively.
  13. #
  14. # Arguments:
  15. # c -        Name of the canvas widget.
  16.  
  17. proc arrowSetup c {
  18.     upvar #0 demo_arrowInfo v
  19.  
  20.     # Remember the current box, if there is one.
  21.  
  22.     set tags [$c gettags current]
  23.     if {$tags != ""} {
  24.     set cur [lindex $tags [lsearch -glob $tags box?]]
  25.     } else {
  26.     set cur ""
  27.     }
  28.  
  29.     # Create the arrow and outline.
  30.  
  31.     $c delete all
  32.     eval "$c create line $v(x1) $v(y) $v(x2) $v(y) -width [expr 10*$v(width)] \
  33.         -arrowshape {[expr 10*$v(a)] [expr 10*$v(b)] [expr 10*$v(c)]} \
  34.         -arrow last $v(bigLineStyle)"
  35.     set xtip [expr $v(x2)-10*$v(b)]
  36.     set deltaY [expr 10*$v(c)+5*$v(width)]
  37.     $c create line $v(x2) $v(y) $xtip [expr $v(y)+$deltaY] \
  38.         [expr $v(x2)-10*$v(a)] $v(y) $xtip [expr $v(y)-$deltaY] \
  39.         $v(x2) $v(y) -width 2 -capstyle round -joinstyle round
  40.  
  41.     # Create the boxes for reshaping the line and arrowhead.
  42.  
  43.     eval "$c create rect [expr $v(x2)-10*$v(a)-5] [expr $v(y)-5] \
  44.         [expr $v(x2)-10*$v(a)+5] [expr $v(y)+5] $v(boxStyle) \
  45.         -tags {box1 box}"
  46.     eval "$c create rect [expr $xtip-5] [expr $v(y)-$deltaY-5] \
  47.         [expr $xtip+5] [expr $v(y)-$deltaY+5] $v(boxStyle) \
  48.         -tags {box2 box}"
  49.     eval "$c create rect [expr $v(x1)-5] [expr $v(y)-5*$v(width)-5] \
  50.         [expr $v(x1)+5] [expr $v(y)-5*$v(width)+5] $v(boxStyle) \
  51.         -tags {box3 box}"
  52.     if {$cur != ""} {
  53.     eval $c itemconfigure $cur $v(activeStyle)
  54.     }
  55.  
  56.     # Create three arrows in actual size with the same parameters
  57.  
  58.     $c create line [expr $v(x2)+50] 0 [expr $v(x2)+50] 1000 \
  59.         -width 2
  60.     set tmp [expr $v(x2)+100]
  61.     $c create line $tmp [expr $v(y)-125] $tmp [expr $v(y)-75] \
  62.         -width $v(width) \
  63.         -arrow both -arrowshape "$v(a) $v(b) $v(c)"
  64.     $c create line [expr $tmp-25] $v(y) [expr $tmp+25] $v(y) \
  65.         -width $v(width) \
  66.         -arrow both -arrowshape "$v(a) $v(b) $v(c)"
  67.     $c create line [expr $tmp-25] [expr $v(y)+75] [expr $tmp+25] \
  68.         [expr $v(y)+125] -width $v(width) \
  69.         -arrow both -arrowshape "$v(a) $v(b) $v(c)"
  70.  
  71.     # Create a bunch of other arrows and text items showing the
  72.     # current dimensions.
  73.  
  74.     set tmp [expr $v(x2)+10]
  75.     $c create line $tmp [expr $v(y)-5*$v(width)] \
  76.         $tmp [expr $v(y)-$deltaY] \
  77.         -arrow both -arrowshape $v(smallTips)
  78.     $c create text [expr $v(x2)+15] [expr $v(y)-$deltaY+5*$v(c)] \
  79.         -text $v(c) -anchor w
  80.     set tmp [expr $v(x1)-10]
  81.     $c create line $tmp [expr $v(y)-5*$v(width)] \
  82.         $tmp [expr $v(y)+5*$v(width)] \
  83.         -arrow both -arrowshape $v(smallTips)
  84.     $c create text [expr $v(x1)-15] $v(y) -text $v(width) -anchor e
  85.     set tmp [expr $v(y)+5*$v(width)+10*$v(c)+10]
  86.     $c create line [expr $v(x2)-10*$v(a)] $tmp $v(x2) $tmp \
  87.         -arrow both -arrowshape $v(smallTips)
  88.     $c create text [expr $v(x2)-5*$v(a)] [expr $tmp+5] \
  89.         -text $v(a) -anchor n
  90.     set tmp [expr $tmp+25]
  91.     $c create line [expr $v(x2)-10*$v(b)] $tmp $v(x2) $tmp \
  92.         -arrow both -arrowshape $v(smallTips)
  93.     $c create text [expr $v(x2)-5*$v(b)] [expr $tmp+5] \
  94.         -text $v(b) -anchor n
  95.  
  96.     $c create text $v(x1) 310 -text "-width  $v(width)" \
  97.         -anchor w -font -*-Helvetica-Medium-R-Normal--*-180-*-*-*-*-*-*
  98.     $c create text $v(x1) 330 -text "-arrowshape  {$v(a)  $v(b)  $v(c)}" \
  99.         -anchor w -font -*-Helvetica-Medium-R-Normal--*-180-*-*-*-*-*-*
  100.  
  101.     incr v(count)
  102. }
  103.  
  104. set w .arrow
  105. global tk_library
  106. catch {destroy $w}
  107. toplevel $w
  108. wm title $w "Arrowhead Editor Demonstration"
  109. wm iconname $w "arrow"
  110. positionWindow $w
  111. set c $w.c
  112.  
  113. 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."
  114. pack $w.msg -side top
  115.  
  116. frame $w.buttons
  117. pack  $w.buttons -side bottom -expand y -fill x -pady 2m
  118. button $w.buttons.dismiss -text Dismiss -command "destroy $w"
  119. button $w.buttons.code -text "See Code" -command "showCode $w"
  120. pack $w.buttons.dismiss $w.buttons.code -side left -expand 1
  121.  
  122. canvas $c -width 500 -height 350 -relief sunken -borderwidth 2
  123. pack $c -expand yes -fill both
  124.  
  125. set demo_arrowInfo(a) 8
  126. set demo_arrowInfo(b) 10
  127. set demo_arrowInfo(c) 3
  128. set demo_arrowInfo(width) 2
  129. set demo_arrowInfo(motionProc) arrowMoveNull
  130. set demo_arrowInfo(x1) 40
  131. set demo_arrowInfo(x2) 350
  132. set demo_arrowInfo(y) 150
  133. set demo_arrowInfo(smallTips) {5 5 2}
  134. set demo_arrowInfo(count) 0
  135. if {[winfo depth $c] > 1} {
  136.     set demo_arrowInfo(bigLineStyle) "-fill SkyBlue1"
  137.     set demo_arrowInfo(boxStyle) "-fill {} -outline black -width 1"
  138.     set demo_arrowInfo(activeStyle) "-fill red -outline black -width 1"
  139. } else {
  140.     set demo_arrowInfo(bigLineStyle) "-fill black -stipple @$tk_library/demos/images/grey.25"
  141.     set demo_arrowInfo(boxStyle) "-fill {} -outline black -width 1"
  142.     set demo_arrowInfo(activeStyle) "-fill black -outline black -width 1"
  143. }
  144. arrowSetup $c
  145. $c bind box <Enter> "$c itemconfigure current $demo_arrowInfo(activeStyle)"
  146. $c bind box <Leave> "$c itemconfigure current $demo_arrowInfo(boxStyle)"
  147. $c bind box <B1-Enter> " "
  148. $c bind box <B1-Leave> " "
  149. $c bind box1 <1> {set demo_arrowInfo(motionProc) arrowMove1}
  150. $c bind box2 <1> {set demo_arrowInfo(motionProc) arrowMove2}
  151. $c bind box3 <1> {set demo_arrowInfo(motionProc) arrowMove3}
  152. $c bind box <B1-Motion> "\$demo_arrowInfo(motionProc) $c %x %y"
  153. bind $c <Any-ButtonRelease-1> "arrowSetup $c"
  154.  
  155. # arrowMove1 --
  156. # This procedure is called for each mouse motion event on box1 (the
  157. # one at the vertex of the arrow).  It updates the controlling parameters
  158. # for the line and arrowhead.
  159. #
  160. # Arguments:
  161. # c -        The name of the canvas window.
  162. # x, y -    The coordinates of the mouse.
  163.  
  164. proc arrowMove1 {c x y} {
  165.     upvar #0 demo_arrowInfo v
  166.     set newA [expr ($v(x2)+5-round([$c canvasx $x]))/10]
  167.     if {$newA < 0} {
  168.     set newA 0
  169.     }
  170.     if {$newA > 25} {
  171.     set newA 25
  172.     }
  173.     if {$newA != $v(a)} {
  174.     $c move box1 [expr 10*($v(a)-$newA)] 0
  175.     set v(a) $newA
  176.     }
  177. }
  178.  
  179. # arrowMove2 --
  180. # This procedure is called for each mouse motion event on box2 (the
  181. # one at the trailing tip of the arrowhead).  It updates the controlling
  182. # parameters for the line and arrowhead.
  183. #
  184. # Arguments:
  185. # c -        The name of the canvas window.
  186. # x, y -    The coordinates of the mouse.
  187.  
  188. proc arrowMove2 {c x y} {
  189.     upvar #0 demo_arrowInfo v
  190.     set newB [expr ($v(x2)+5-round([$c canvasx $x]))/10]
  191.     if {$newB < 0} {
  192.     set newB 0
  193.     }
  194.     if {$newB > 25} {
  195.     set newB 25
  196.     }
  197.     set newC [expr ($v(y)+5-round([$c canvasy $y])-5*$v(width))/10]
  198.     if {$newC < 0} {
  199.     set newC 0
  200.     }
  201.     if {$newC > 20} {
  202.     set newC 20
  203.     }
  204.     if {($newB != $v(b)) || ($newC != $v(c))} {
  205.     $c move box2 [expr 10*($v(b)-$newB)] [expr 10*($v(c)-$newC)]
  206.     set v(b) $newB
  207.     set v(c) $newC
  208.     }
  209. }
  210.  
  211. # arrowMove3 --
  212. # This procedure is called for each mouse motion event on box3 (the
  213. # one that controls the thickness of the line).  It updates the
  214. # controlling parameters for the line and arrowhead.
  215. #
  216. # Arguments:
  217. # c -        The name of the canvas window.
  218. # x, y -    The coordinates of the mouse.
  219.  
  220. proc arrowMove3 {c x y} {
  221.     upvar #0 demo_arrowInfo v
  222.     set newWidth [expr ($v(y)+2-round([$c canvasy $y]))/5]
  223.     if {$newWidth < 0} {
  224.     set newWidth 0
  225.     }
  226.     if {$newWidth > 20} {
  227.     set newWidth 20
  228.     }
  229.     if {$newWidth != $v(width)} {
  230.     $c move box3 0 [expr 5*($v(width)-$newWidth)]
  231.     set v(width) $newWidth
  232.     }
  233. }
  234.