ruler.tcl 5.3 KB

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