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

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