home *** CD-ROM | disk | FTP | other *** search
/ Ultra Pack / UltraComputing Partner Applications.iso / SunLabs / tclTK / src / tk4.0 / library / scrollbar.tcl < prev    next >
Encoding:
Text File  |  1995-06-29  |  9.5 KB  |  358 lines

  1. # scrollbar.tcl --
  2. #
  3. # This file defines the default bindings for Tk scrollbar widgets.
  4. # It also provides procedures that help in implementing the bindings.
  5. #
  6. # @(#) scrollbar.tcl 1.13 95/06/29 14:03:03
  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. #-------------------------------------------------------------------------
  16. # The code below creates the default class bindings for scrollbars.
  17. #-------------------------------------------------------------------------
  18.  
  19. # Standard Motif bindings:
  20.  
  21. bind Scrollbar <Enter> {
  22.     if $tk_strictMotif {
  23.     set tkPriv(activeBg) [%W cget -activebackground]
  24.     %W config -activebackground [%W cget -background]
  25.     }
  26.     %W activate [%W identify %x %y]
  27. }
  28. bind Scrollbar <Motion> {
  29.     %W activate [%W identify %x %y]
  30. }
  31. bind Scrollbar <Leave> {
  32.     if $tk_strictMotif {
  33.     %W config -activebackground $tkPriv(activeBg)
  34.     }
  35.     %W activate {}
  36. }
  37. bind Scrollbar <1> {
  38.     tkScrollButtonDown %W %x %y
  39. }
  40. bind Scrollbar <B1-Motion> {
  41.     tkScrollDrag %W %x %y
  42. }
  43. bind Scrollbar <ButtonRelease-1> {
  44.     tkScrollButtonUp %W %x %y
  45. }
  46. bind Scrollbar <B1-Leave> {
  47.     # Prevents <Leave> binding from being invoked.
  48. }
  49. bind Scrollbar <B1-Enter> {
  50.     # Prevents <Enter> binding from being invoked.
  51. }
  52. bind Scrollbar <2> {
  53.     tkScrollButtonDown %W %x %y
  54. }
  55. bind Scrollbar <B2-Motion> {
  56.     tkScrollDrag %W %x %y
  57. }
  58. bind Scrollbar <ButtonRelease-2> {
  59.     tkScrollButtonUp %W %x %y
  60. }
  61. bind Scrollbar <B2-Leave> {
  62.     # Prevents <Leave> binding from being invoked.
  63. }
  64. bind Scrollbar <B2-Enter> {
  65.     # Prevents <Enter> binding from being invoked.
  66. }
  67. bind Scrollbar <Control-1> {
  68.     tkScrollTopBottom %W %x %y
  69. }
  70. bind Scrollbar <Control-2> {
  71.     tkScrollTopBottom %W %x %y
  72. }
  73.  
  74. bind Scrollbar <Up> {
  75.     tkScrollByUnits %W v -1
  76. }
  77. bind Scrollbar <Down> {
  78.     tkScrollByUnits %W v 1
  79. }
  80. bind Scrollbar <Control-Up> {
  81.     tkScrollByPages %W v -1
  82. }
  83. bind Scrollbar <Control-Down> {
  84.     tkScrollByPages %W v 1
  85. }
  86. bind Scrollbar <Left> {
  87.     tkScrollByUnits %W h -1
  88. }
  89. bind Scrollbar <Right> {
  90.     tkScrollByUnits %W h 1
  91. }
  92. bind Scrollbar <Control-Left> {
  93.     tkScrollByPages %W h -1
  94. }
  95. bind Scrollbar <Control-Right> {
  96.     tkScrollByPages %W h 1
  97. }
  98. bind Scrollbar <Prior> {
  99.     tkScrollByPages %W hv -1
  100. }
  101. bind Scrollbar <Next> {
  102.     tkScrollByPages %W hv 1
  103. }
  104. bind Scrollbar <Home> {
  105.     tkScrollToPos %W 0
  106. }
  107. bind Scrollbar <End> {
  108.     tkScrollToPos %W 1
  109. }
  110.  
  111. # tkScrollButtonDown --
  112. # This procedure is invoked when a button is pressed in a scrollbar.
  113. # It changes the way the scrollbar is displayed and takes actions
  114. # depending on where the mouse is.
  115. #
  116. # Arguments:
  117. # w -        The scrollbar widget.
  118. # x, y -    Mouse coordinates.
  119.  
  120. proc tkScrollButtonDown {w x y} {
  121.     global tkPriv
  122.     set tkPriv(relief) [$w cget -activerelief]
  123.     $w configure -activerelief sunken
  124.     set element [$w identify $x $y]
  125.     if {$element == "slider"} {
  126.     tkScrollStartDrag $w $x $y
  127.     } else {
  128.     tkScrollSelect $w $element initial
  129.     }
  130. }
  131.  
  132. # tkScrollButtonUp --
  133. # This procedure is invoked when a button is released in a scrollbar.
  134. # It cancels scans and auto-repeats that were in progress, and restores
  135. # the way the active element is displayed.
  136. #
  137. # Arguments:
  138. # w -        The scrollbar widget.
  139. # x, y -    Mouse coordinates.
  140.  
  141. proc tkScrollButtonUp {w x y} {
  142.     global tkPriv
  143.     tkCancelRepeat
  144.     $w configure -activerelief $tkPriv(relief)
  145.     tkScrollEndDrag $w $x $y
  146.     $w activate [$w identify $x $y]
  147. }
  148.  
  149. # tkScrollSelect --
  150. # This procedure is invoked when button 1 is pressed over the scrollbar.
  151. # It invokes one of several scrolling actions depending on where in
  152. # the scrollbar the button was pressed.
  153. #
  154. # Arguments:
  155. # w -        The scrollbar widget.
  156. # element -    The element of the scrollbar that was selected, such
  157. #        as "arrow1" or "trough2".  Shouldn't be "slider".
  158. # repeat -    Whether and how to auto-repeat the action:  "noRepeat"
  159. #        means don't auto-repeat, "initial" means this is the
  160. #        first action in an auto-repeat sequence, and "again"
  161. #        means this is the second repetition or later.
  162.  
  163. proc tkScrollSelect {w element repeat} {
  164.     global tkPriv
  165.     if {$element == "arrow1"} {
  166.     tkScrollByUnits $w hv -1
  167.     } elseif {$element == "trough1"} {
  168.     tkScrollByPages $w hv -1
  169.     } elseif {$element == "trough2"} {
  170.     tkScrollByPages $w hv 1
  171.     } elseif {$element == "arrow2"} {
  172.     tkScrollByUnits $w hv 1
  173.     } else {
  174.     return
  175.     }
  176.     if {$repeat == "again"} {
  177.     set tkPriv(afterId) [after [$w cget -repeatinterval] \
  178.         tkScrollSelect $w $element again]
  179.     } elseif {$repeat == "initial"} {
  180.     set tkPriv(afterId) [after [$w cget -repeatdelay] \
  181.         tkScrollSelect $w $element again]
  182.     }
  183. }
  184.  
  185. # tkScrollStartDrag --
  186. # This procedure is called to initiate a drag of the slider.  It just
  187. # remembers the starting position of the mouse and slider.
  188. #
  189. # Arguments:
  190. # w -        The scrollbar widget.
  191. # x, y -    The mouse position at the start of the drag operation.
  192.  
  193. proc tkScrollStartDrag {w x y} {
  194.     global tkPriv
  195.  
  196.     if {[$w cget -command] == ""} {
  197.     return
  198.     }
  199.     set tkPriv(pressX) $x
  200.     set tkPriv(pressY) $y
  201.     set tkPriv(initValues) [$w get]
  202.     set iv0 [lindex $tkPriv(initValues) 0]
  203.     if {[llength $tkPriv(initValues)] == 2} {
  204.     set tkPriv(initPos) $iv0
  205.     } else {
  206.     if {$iv0 == 0} {
  207.         set tkPriv(initPos) 0.0
  208.     } else {
  209.         set tkPriv(initPos) [expr (double([lindex $tkPriv(initValues) 2])) \
  210.             / [lindex $tkPriv(initValues) 0]]
  211.     }
  212.     }
  213. }
  214.  
  215. # tkScrollDrag --
  216. # This procedure is called for each mouse motion even when the slider
  217. # is being dragged.  It notifies the associated widget if we're not
  218. # jump scrolling, and it just updates the scrollbar if we are jump
  219. # scrolling.
  220. #
  221. # Arguments:
  222. # w -        The scrollbar widget.
  223. # x, y -    The current mouse position.
  224.  
  225. proc tkScrollDrag {w x y} {
  226.     global tkPriv
  227.  
  228.     if {$tkPriv(initPos) == ""} {
  229.     return
  230.     }
  231.     set delta [$w delta [expr $x - $tkPriv(pressX)] [expr $y - $tkPriv(pressY)]]
  232.     if [$w cget -jump] {
  233.     if {[llength $tkPriv(initValues)] == 2} {
  234.         $w set [expr [lindex $tkPriv(initValues) 0] + $delta] \
  235.             [expr [lindex $tkPriv(initValues) 1] + $delta]
  236.     } else {
  237.         set delta [expr round($delta * [lindex $tkPriv(initValues) 0])]
  238.         eval $w set [lreplace $tkPriv(initValues) 2 3 \
  239.             [expr [lindex $tkPriv(initValues) 2] + $delta] \
  240.             [expr [lindex $tkPriv(initValues) 3] + $delta]]
  241.     }
  242.     } else {
  243.     tkScrollToPos $w [expr $tkPriv(initPos) + $delta]
  244.     }
  245. }
  246.  
  247. # tkScrollEndDrag --
  248. # This procedure is called to end an interactive drag of the slider.
  249. # It scrolls the window if we're in jump mode, otherwise it does nothing.
  250. #
  251. # Arguments:
  252. # w -        The scrollbar widget.
  253. # x, y -    The mouse position at the end of the drag operation.
  254.  
  255. proc tkScrollEndDrag {w x y} {
  256.     global tkPriv
  257.  
  258.     if {$tkPriv(initPos) == ""} {
  259.     return
  260.     }
  261.     if [$w cget -jump] {
  262.     set delta [$w delta [expr $x - $tkPriv(pressX)] \
  263.         [expr $y - $tkPriv(pressY)]]
  264.     tkScrollToPos $w [expr $tkPriv(initPos) + $delta]
  265.     }
  266.     set tkPriv(initPos) ""
  267. }
  268.  
  269. # tkScrollByUnits --
  270. # This procedure tells the scrollbar's associated widget to scroll up
  271. # or down by a given number of units.  It notifies the associated widget
  272. # in different ways for old and new command syntaxes.
  273. #
  274. # Arguments:
  275. # w -        The scrollbar widget.
  276. # orient -    Which kinds of scrollbars this applies to:  "h" for
  277. #        horizontal, "v" for vertical, "hv" for both.
  278. # amount -    How many units to scroll:  typically 1 or -1.
  279.  
  280. proc tkScrollByUnits {w orient amount} {
  281.     set cmd [$w cget -command]
  282.     if {($cmd == "") || ([string first \
  283.         [string index [$w cget -orient] 0] $orient] < 0)} {
  284.     return
  285.     }
  286.     set info [$w get]
  287.     if {[llength $info] == 2} {
  288.     uplevel #0 $cmd scroll $amount units
  289.     } else {
  290.     uplevel #0 $cmd [expr [lindex $info 2] + $amount]
  291.     }
  292. }
  293.  
  294. # tkScrollByPages --
  295. # This procedure tells the scrollbar's associated widget to scroll up
  296. # or down by a given number of screenfuls.  It notifies the associated
  297. # widget in different ways for old and new command syntaxes.
  298. #
  299. # Arguments:
  300. # w -        The scrollbar widget.
  301. # orient -    Which kinds of scrollbars this applies to:  "h" for
  302. #        horizontal, "v" for vertical, "hv" for both.
  303. # amount -    How many screens to scroll:  typically 1 or -1.
  304.  
  305. proc tkScrollByPages {w orient amount} {
  306.     set cmd [$w cget -command]
  307.     if {($cmd == "") || ([string first \
  308.         [string index [$w cget -orient] 0] $orient] < 0)} {
  309.     return
  310.     }
  311.     set info [$w get]
  312.     if {[llength $info] == 2} {
  313.     uplevel #0 $cmd scroll $amount pages
  314.     } else {
  315.     uplevel #0 $cmd [expr [lindex $info 2] + $amount*([lindex $info 1] - 1)]
  316.     }
  317. }
  318.  
  319. # tkScrollToPos --
  320. # This procedure tells the scrollbar's associated widget to scroll to
  321. # a particular location, given by a fraction between 0 and 1.  It notifies
  322. # the associated widget in different ways for old and new command syntaxes.
  323. #
  324. # Arguments:
  325. # w -        The scrollbar widget.
  326. # pos -        A fraction between 0 and 1 indicating a desired position
  327. #        in the document.
  328.  
  329. proc tkScrollToPos {w pos} {
  330.     set cmd [$w cget -command]
  331.     if {($cmd == "")} {
  332.     return
  333.     }
  334.     set info [$w get]
  335.     if {[llength $info] == 2} {
  336.     uplevel #0 $cmd moveto $pos
  337.     } else {
  338.     uplevel #0 $cmd [expr round([lindex $info 0]*$pos)]
  339.     }
  340. }
  341.  
  342. # tkScrollTopBottom
  343. # Scroll to the top or bottom of the document, depending on the mouse
  344. # position.
  345. #
  346. # Arguments:
  347. # w -        The scrollbar widget.
  348. # x, y -    Mouse coordinates within the widget.
  349.  
  350. proc tkScrollTopBottom {w x y} {
  351.     set element [$w identify $x $y]
  352.     if [string match *1 $element] {
  353.     tkScrollToPos $w 0
  354.     } elseif [string match *2 $element] {
  355.     tkScrollToPos $w 1
  356.     }
  357. }
  358.