scale.tcl
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:8k
源码类别:

通讯编程

开发平台:

Visual C++

  1. # scale.tcl --
  2. #
  3. # This file defines the default bindings for Tk scale widgets and provides
  4. # procedures that help in implementing the bindings.
  5. #
  6. # RCS: @(#) $Id: scale.tcl,v 1.9.2.5 2006/03/17 10:50:11 patthoyts Exp $
  7. #
  8. # Copyright (c) 1994 The Regents of the University of California.
  9. # Copyright (c) 1994-1995 Sun Microsystems, Inc.
  10. #
  11. # See the file "license.terms" for information on usage and redistribution
  12. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  13. #
  14. #-------------------------------------------------------------------------
  15. # The code below creates the default class bindings for entries.
  16. #-------------------------------------------------------------------------
  17. # Standard Motif bindings:
  18. bind Scale <Enter> {
  19.     if {$tk_strictMotif} {
  20. set tk::Priv(activeBg) [%W cget -activebackground]
  21. %W configure -activebackground [%W cget -background]
  22.     }
  23.     tk::ScaleActivate %W %x %y
  24. }
  25. bind Scale <Motion> {
  26.     tk::ScaleActivate %W %x %y
  27. }
  28. bind Scale <Leave> {
  29.     if {$tk_strictMotif} {
  30. %W configure -activebackground $tk::Priv(activeBg)
  31.     }
  32.     if {[%W cget -state] eq "active"} {
  33. %W configure -state normal
  34.     }
  35. }
  36. bind Scale <1> {
  37.     tk::ScaleButtonDown %W %x %y
  38. }
  39. bind Scale <B1-Motion> {
  40.     tk::ScaleDrag %W %x %y
  41. }
  42. bind Scale <B1-Leave> { }
  43. bind Scale <B1-Enter> { }
  44. bind Scale <ButtonRelease-1> {
  45.     tk::CancelRepeat
  46.     tk::ScaleEndDrag %W
  47.     tk::ScaleActivate %W %x %y
  48. }
  49. bind Scale <2> {
  50.     tk::ScaleButton2Down %W %x %y
  51. }
  52. bind Scale <B2-Motion> {
  53.     tk::ScaleDrag %W %x %y
  54. }
  55. bind Scale <B2-Leave> { }
  56. bind Scale <B2-Enter> { }
  57. bind Scale <ButtonRelease-2> {
  58.     tk::CancelRepeat
  59.     tk::ScaleEndDrag %W
  60.     tk::ScaleActivate %W %x %y
  61. }
  62. if {$tcl_platform(platform) eq "windows"} {
  63.     # On Windows do the same with button 3, as that is the right mouse button
  64.     bind Scale <3> [bind Scale <2>]
  65.     bind Scale <B3-Motion> [bind Scale <B2-Motion>]
  66.     bind Scale <B3-Leave> [bind Scale <B2-Leave>]
  67.     bind Scale <B3-Enter> [bind Scale <B2-Enter>]
  68.     bind Scale <ButtonRelease-3> [bind Scale <ButtonRelease-2>]
  69. }
  70. bind Scale <Control-1> {
  71.     tk::ScaleControlPress %W %x %y
  72. }
  73. bind Scale <Up> {
  74.     tk::ScaleIncrement %W up little noRepeat
  75. }
  76. bind Scale <Down> {
  77.     tk::ScaleIncrement %W down little noRepeat
  78. }
  79. bind Scale <Left> {
  80.     tk::ScaleIncrement %W up little noRepeat
  81. }
  82. bind Scale <Right> {
  83.     tk::ScaleIncrement %W down little noRepeat
  84. }
  85. bind Scale <Control-Up> {
  86.     tk::ScaleIncrement %W up big noRepeat
  87. }
  88. bind Scale <Control-Down> {
  89.     tk::ScaleIncrement %W down big noRepeat
  90. }
  91. bind Scale <Control-Left> {
  92.     tk::ScaleIncrement %W up big noRepeat
  93. }
  94. bind Scale <Control-Right> {
  95.     tk::ScaleIncrement %W down big noRepeat
  96. }
  97. bind Scale <Home> {
  98.     %W set [%W cget -from]
  99. }
  100. bind Scale <End> {
  101.     %W set [%W cget -to]
  102. }
  103. # ::tk::ScaleActivate --
  104. # This procedure is invoked to check a given x-y position in the
  105. # scale and activate the slider if the x-y position falls within
  106. # the slider.
  107. #
  108. # Arguments:
  109. # w - The scale widget.
  110. # x, y - Mouse coordinates.
  111. proc ::tk::ScaleActivate {w x y} {
  112.     if {[$w cget -state] eq "disabled"} {
  113. return
  114.     }
  115.     if {[$w identify $x $y] eq "slider"} {
  116. set state active
  117.     } else {
  118. set state normal
  119.     }
  120.     if {[$w cget -state] ne $state} {
  121. $w configure -state $state
  122.     }
  123. }
  124. # ::tk::ScaleButtonDown --
  125. # This procedure is invoked when a button is pressed in a scale.  It
  126. # takes different actions depending on where the button was pressed.
  127. #
  128. # Arguments:
  129. # w - The scale widget.
  130. # x, y - Mouse coordinates of button press.
  131. proc ::tk::ScaleButtonDown {w x y} {
  132.     variable ::tk::Priv
  133.     set Priv(dragging) 0
  134.     set el [$w identify $x $y]
  135.     # save the relief
  136.     set Priv($w,relief) [$w cget -sliderrelief]
  137.     if {$el eq "trough1"} {
  138. ScaleIncrement $w up little initial
  139.     } elseif {$el eq "trough2"} {
  140. ScaleIncrement $w down little initial
  141.     } elseif {$el eq "slider"} {
  142. set Priv(dragging) 1
  143. set Priv(initValue) [$w get]
  144. set coords [$w coords]
  145. set Priv(deltaX) [expr {$x - [lindex $coords 0]}]
  146. set Priv(deltaY) [expr {$y - [lindex $coords 1]}]
  147.         switch -exact -- $Priv($w,relief) {
  148.             "raised" { $w configure -sliderrelief sunken }
  149.             "ridge"  { $w configure -sliderrelief groove }
  150.         }
  151.     }
  152. }
  153. # ::tk::ScaleDrag --
  154. # This procedure is called when the mouse is dragged with
  155. # mouse button 1 down.  If the drag started inside the slider
  156. # (i.e. the scale is active) then the scale's value is adjusted
  157. # to reflect the mouse's position.
  158. #
  159. # Arguments:
  160. # w - The scale widget.
  161. # x, y - Mouse coordinates.
  162. proc ::tk::ScaleDrag {w x y} {
  163.     variable ::tk::Priv
  164.     if {!$Priv(dragging)} {
  165. return
  166.     }
  167.     $w set [$w get [expr {$x-$Priv(deltaX)}] [expr {$y-$Priv(deltaY)}]]
  168. }
  169. # ::tk::ScaleEndDrag --
  170. # This procedure is called to end an interactive drag of the
  171. # slider.  It just marks the drag as over.
  172. #
  173. # Arguments:
  174. # w - The scale widget.
  175. proc ::tk::ScaleEndDrag {w} {
  176.     variable ::tk::Priv
  177.     set Priv(dragging) 0
  178.     if {[info exists Priv($w,relief)]} {
  179.         $w configure -sliderrelief $Priv($w,relief)
  180.         unset Priv($w,relief)
  181.     }
  182. }
  183. # ::tk::ScaleIncrement --
  184. # This procedure is invoked to increment the value of a scale and
  185. # to set up auto-repeating of the action if that is desired.  The
  186. # way the value is incremented depends on the "dir" and "big"
  187. # arguments.
  188. #
  189. # Arguments:
  190. # w - The scale widget.
  191. # dir - "up" means move value towards -from, "down" means
  192. # move towards -to.
  193. # big - Size of increments: "big" or "little".
  194. # repeat - Whether and how to auto-repeat the action:  "noRepeat"
  195. # means don't auto-repeat, "initial" means this is the
  196. # first action in an auto-repeat sequence, and "again"
  197. # means this is the second repetition or later.
  198. proc ::tk::ScaleIncrement {w dir big repeat} {
  199.     variable ::tk::Priv
  200.     if {![winfo exists $w]} return
  201.     if {$big eq "big"} {
  202. set inc [$w cget -bigincrement]
  203. if {$inc == 0} {
  204.     set inc [expr {abs([$w cget -to] - [$w cget -from])/10.0}]
  205. }
  206. if {$inc < [$w cget -resolution]} {
  207.     set inc [$w cget -resolution]
  208. }
  209.     } else {
  210. set inc [$w cget -resolution]
  211.     }
  212.     if {([$w cget -from] > [$w cget -to]) ^ ($dir eq "up")} {
  213. set inc [expr {-$inc}]
  214.     }
  215.     $w set [expr {[$w get] + $inc}]
  216.     if {$repeat eq "again"} {
  217. set Priv(afterId) [after [$w cget -repeatinterval] 
  218. [list tk::ScaleIncrement $w $dir $big again]]
  219.     } elseif {$repeat eq "initial"} {
  220. set delay [$w cget -repeatdelay]
  221. if {$delay > 0} {
  222.     set Priv(afterId) [after $delay 
  223.     [list tk::ScaleIncrement $w $dir $big again]]
  224. }
  225.     }
  226. }
  227. # ::tk::ScaleControlPress --
  228. # This procedure handles button presses that are made with the Control
  229. # key down.  Depending on the mouse position, it adjusts the scale
  230. # value to one end of the range or the other.
  231. #
  232. # Arguments:
  233. # w - The scale widget.
  234. # x, y - Mouse coordinates where the button was pressed.
  235. proc ::tk::ScaleControlPress {w x y} {
  236.     set el [$w identify $x $y]
  237.     if {$el eq "trough1"} {
  238. $w set [$w cget -from]
  239.     } elseif {$el eq "trough2"} {
  240. $w set [$w cget -to]
  241.     }
  242. }
  243. # ::tk::ScaleButton2Down
  244. # This procedure is invoked when button 2 is pressed over a scale.
  245. # It sets the value to correspond to the mouse position and starts
  246. # a slider drag.
  247. #
  248. # Arguments:
  249. # w - The scrollbar widget.
  250. # x, y - Mouse coordinates within the widget.
  251. proc ::tk::ScaleButton2Down {w x y} {
  252.     variable ::tk::Priv
  253.     if {[$w cget -state] eq "disabled"} {
  254.       return
  255.     }
  256.     $w configure -state active
  257.     $w set [$w get $x $y]
  258.     set Priv(dragging) 1
  259.     set Priv(initValue) [$w get]
  260.     set Priv($w,relief) [$w cget -sliderrelief]
  261.     set coords "$x $y"
  262.     set Priv(deltaX) 0
  263.     set Priv(deltaY) 0
  264. }