ruler.tcl 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. # ruler.tcl --
  2. #
  3. # This demonstration script creates a canvas widget that displays a ruler
  4. # with tab stops that can be set, moved, and deleted.
  5. if {![info exists widgetDemo]} {
  6. error "This script should be run from the \"widget\" demo."
  7. }
  8. package require Tk
  9. # rulerMkTab --
  10. # This procedure creates a new triangular polygon in a canvas to
  11. # represent a tab stop.
  12. #
  13. # Arguments:
  14. # c - The canvas window.
  15. # x, y - Coordinates at which to create the tab stop.
  16. proc rulerMkTab {c x y} {
  17. upvar #0 demo_rulerInfo v
  18. set newTab [$c create polygon $x $y \
  19. [expr {$x+$v(size)}] [expr {$y+$v(size)}] \
  20. [expr {$x-$v(size)}] [expr {$y+$v(size)}]]
  21. set fill [$c itemcget $newTab -outline]
  22. $c itemconfigure $newTab -fill $fill -outline {}
  23. set v(normalStyle) "-fill $fill"
  24. return $newTab
  25. }
  26. set w .ruler
  27. catch {destroy $w}
  28. toplevel $w
  29. wm title $w "Ruler Demonstration"
  30. wm iconname $w "ruler"
  31. positionWindow $w
  32. set c $w.c
  33. label $w.msg -font $font -wraplength 5i -justify left -text "This canvas widget shows a mock-up of a ruler. You can create tab stops by dragging them out of the well to the right of the ruler. You can also drag existing tab stops. If you drag a tab stop far enough up or down so that it turns dim, it will be deleted when you release the mouse button."
  34. pack $w.msg -side top
  35. ## See Code / Dismiss buttons
  36. set btns [addSeeDismiss $w.buttons $w]
  37. pack $btns -side bottom -fill x
  38. canvas $c -width 14.8c -height 2.5c
  39. pack $w.c -side top -fill x
  40. set demo_rulerInfo(grid) .25c
  41. set demo_rulerInfo(left) [winfo fpixels $c 1c]
  42. set demo_rulerInfo(right) [winfo fpixels $c 13c]
  43. set demo_rulerInfo(top) [winfo fpixels $c 1c]
  44. set demo_rulerInfo(bottom) [winfo fpixels $c 1.5c]
  45. set demo_rulerInfo(size) [winfo fpixels $c .2c]
  46. # Main widget program sets variable tk_demoDirectory
  47. if {[winfo depth $c] > 1} {
  48. set demo_rulerInfo(activeStyle) "-fill red -stipple {}"
  49. set demo_rulerInfo(deleteStyle) [list -fill red \
  50. -stipple @[file join $tk_demoDirectory images gray25.xbm]]
  51. } else {
  52. set demo_rulerInfo(activeStyle) "-fill black -stipple {}"
  53. set demo_rulerInfo(deleteStyle) [list -fill black \
  54. -stipple @[file join $tk_demoDirectory images gray25.xbm]]
  55. }
  56. $c create line 1c 0.5c 1c 1c 13c 1c 13c 0.5c -width 1
  57. for {set i 0} {$i < 12} {incr i} {
  58. set x [expr {$i+1}]
  59. $c create line ${x}c 1c ${x}c 0.6c -width 1
  60. $c create line $x.25c 1c $x.25c 0.8c -width 1
  61. $c create line $x.5c 1c $x.5c 0.7c -width 1
  62. $c create line $x.75c 1c $x.75c 0.8c -width 1
  63. $c create text $x.15c .75c -text $i -anchor sw
  64. }
  65. $c addtag well withtag [$c create rect 13.2c 1c 13.8c 0.5c \
  66. -fill [lindex [$c config -bg] 4]]
  67. $c addtag well withtag [rulerMkTab $c [winfo pixels $c 13.5c] \
  68. [winfo pixels $c .65c]]
  69. $c bind well <Button-1> "rulerNewTab $c %x %y"
  70. $c bind tab <Button-1> "rulerSelectTab $c %x %y"
  71. bind $c <B1-Motion> "rulerMoveTab $c %x %y"
  72. bind $c <ButtonRelease-1> "rulerReleaseTab $c"
  73. # rulerNewTab --
  74. # Does all the work of creating a tab stop, including creating the
  75. # triangle object and adding tags to it to give it tab behavior.
  76. #
  77. # Arguments:
  78. # c - The canvas window.
  79. # x, y - The coordinates of the tab stop.
  80. proc rulerNewTab {c x y} {
  81. upvar #0 demo_rulerInfo v
  82. $c addtag active withtag [rulerMkTab $c $x $y]
  83. $c addtag tab withtag active
  84. set v(x) $x
  85. set v(y) $y
  86. rulerMoveTab $c $x $y
  87. }
  88. # rulerSelectTab --
  89. # This procedure is invoked when mouse button 1 is pressed over
  90. # a tab. It remembers information about the tab so that it can
  91. # be dragged interactively.
  92. #
  93. # Arguments:
  94. # c - The canvas widget.
  95. # x, y - The coordinates of the mouse (identifies the point by
  96. # which the tab was picked up for dragging).
  97. proc rulerSelectTab {c x y} {
  98. upvar #0 demo_rulerInfo v
  99. set v(x) [$c canvasx $x $v(grid)]
  100. set v(y) [expr {$v(top)+2}]
  101. $c addtag active withtag current
  102. eval "$c itemconf active $v(activeStyle)"
  103. $c raise active
  104. }
  105. # rulerMoveTab --
  106. # This procedure is invoked during mouse motion events to drag a tab.
  107. # It adjusts the position of the tab, and changes its appearance if
  108. # it is about to be dragged out of the ruler.
  109. #
  110. # Arguments:
  111. # c - The canvas widget.
  112. # x, y - The coordinates of the mouse.
  113. proc rulerMoveTab {c x y} {
  114. upvar #0 demo_rulerInfo v
  115. if {[$c find withtag active] == ""} {
  116. return
  117. }
  118. set cx [$c canvasx $x $v(grid)]
  119. set cy [$c canvasy $y]
  120. if {$cx < $v(left)} {
  121. set cx $v(left)
  122. }
  123. if {$cx > $v(right)} {
  124. set cx $v(right)
  125. }
  126. if {($cy >= $v(top)) && ($cy <= $v(bottom))} {
  127. set cy [expr {$v(top)+2}]
  128. eval "$c itemconf active $v(activeStyle)"
  129. } else {
  130. set cy [expr {$cy-$v(size)-2}]
  131. eval "$c itemconf active $v(deleteStyle)"
  132. }
  133. $c move active [expr {$cx-$v(x)}] [expr {$cy-$v(y)}]
  134. set v(x) $cx
  135. set v(y) $cy
  136. }
  137. # rulerReleaseTab --
  138. # This procedure is invoked during button release events that end
  139. # a tab drag operation. It deselects the tab and deletes the tab if
  140. # it was dragged out of the ruler.
  141. #
  142. # Arguments:
  143. # c - The canvas widget.
  144. # x, y - The coordinates of the mouse.
  145. proc rulerReleaseTab c {
  146. upvar #0 demo_rulerInfo v
  147. if {[$c find withtag active] == {}} {
  148. return
  149. }
  150. if {$v(y) != $v(top)+2} {
  151. $c delete active
  152. } else {
  153. eval "$c itemconf active $v(normalStyle)"
  154. $c dtag active
  155. }
  156. }