home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tk42r2x.zip / TclTk / lib / tk4.2 / scrlbar.tcl < prev    next >
Text File  |  1999-07-27  |  11KB  |  411 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. # SCCS: @(#) scrlbar.tcl 1.23 96/04/16 11:42:23
  7. #
  8. # Copyright (c) 1994 The Regents of the University of California.
  9. # Copyright (c) 1994-1996 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 {![winfo exists $w]} return
  181.     if {$element == "arrow1"} {
  182.     tkScrollByUnits $w hv -1
  183.     } elseif {$element == "trough1"} {
  184.     tkScrollByPages $w hv -1
  185.     } elseif {$element == "trough2"} {
  186.     tkScrollByPages $w hv 1
  187.     } elseif {$element == "arrow2"} {
  188.     tkScrollByUnits $w hv 1
  189.     } else {
  190.     return
  191.     }
  192.     if {$repeat == "again"} {
  193.     set tkPriv(afterId) [after [$w cget -repeatinterval] \
  194.         tkScrollSelect $w $element again]
  195.     } elseif {$repeat == "initial"} {
  196.     set delay [$w cget -repeatdelay]
  197.     if {$delay > 0} {
  198.         set tkPriv(afterId) [after $delay tkScrollSelect $w $element again]
  199.     }
  200.     }
  201. }
  202.  
  203. # tkScrollStartDrag --
  204. # This procedure is called to initiate a drag of the slider.  It just
  205. # remembers the starting position of the mouse and slider.
  206. #
  207. # Arguments:
  208. # w -        The scrollbar widget.
  209. # x, y -    The mouse position at the start of the drag operation.
  210.  
  211. proc tkScrollStartDrag {w x y} {
  212.     global tkPriv
  213.  
  214.     if {[$w cget -command] == ""} {
  215.     return
  216.     }
  217.     set tkPriv(pressX) $x
  218.     set tkPriv(pressY) $y
  219.     set tkPriv(initValues) [$w get]
  220.     set iv0 [lindex $tkPriv(initValues) 0]
  221.     if {[llength $tkPriv(initValues)] == 2} {
  222.     set tkPriv(initPos) $iv0
  223.     } else {
  224.     if {$iv0 == 0} {
  225.         set tkPriv(initPos) 0.0
  226.     } else {
  227.         set tkPriv(initPos) [expr (double([lindex $tkPriv(initValues) 2])) \
  228.             / [lindex $tkPriv(initValues) 0]]
  229.     }
  230.     }
  231. }
  232.  
  233. # tkScrollDrag --
  234. # This procedure is called for each mouse motion even when the slider
  235. # is being dragged.  It notifies the associated widget if we're not
  236. # jump scrolling, and it just updates the scrollbar if we are jump
  237. # scrolling.
  238. #
  239. # Arguments:
  240. # w -        The scrollbar widget.
  241. # x, y -    The current mouse position.
  242.  
  243. proc tkScrollDrag {w x y} {
  244.     global tkPriv
  245.  
  246.     if {$tkPriv(initPos) == ""} {
  247.     return
  248.     }
  249.     set delta [$w delta [expr $x - $tkPriv(pressX)] [expr $y - $tkPriv(pressY)]]
  250.     if [$w cget -jump] {
  251.     if {[llength $tkPriv(initValues)] == 2} {
  252.         $w set [expr [lindex $tkPriv(initValues) 0] + $delta] \
  253.             [expr [lindex $tkPriv(initValues) 1] + $delta]
  254.     } else {
  255.         set delta [expr round($delta * [lindex $tkPriv(initValues) 0])]
  256.         eval $w set [lreplace $tkPriv(initValues) 2 3 \
  257.             [expr [lindex $tkPriv(initValues) 2] + $delta] \
  258.             [expr [lindex $tkPriv(initValues) 3] + $delta]]
  259.     }
  260.     } else {
  261.     tkScrollToPos $w [expr $tkPriv(initPos) + $delta]
  262.     }
  263. }
  264.  
  265. # tkScrollEndDrag --
  266. # This procedure is called to end an interactive drag of the slider.
  267. # It scrolls the window if we're in jump mode, otherwise it does nothing.
  268. #
  269. # Arguments:
  270. # w -        The scrollbar widget.
  271. # x, y -    The mouse position at the end of the drag operation.
  272.  
  273. proc tkScrollEndDrag {w x y} {
  274.     global tkPriv
  275.  
  276.     if {$tkPriv(initPos) == ""} {
  277.     return
  278.     }
  279.     if [$w cget -jump] {
  280.     set delta [$w delta [expr $x - $tkPriv(pressX)] \
  281.         [expr $y - $tkPriv(pressY)]]
  282.     tkScrollToPos $w [expr $tkPriv(initPos) + $delta]
  283.     }
  284.     set tkPriv(initPos) ""
  285. }
  286.  
  287. # tkScrollByUnits --
  288. # This procedure tells the scrollbar's associated widget to scroll up
  289. # or down by a given number of units.  It notifies the associated widget
  290. # in different ways for old and new command syntaxes.
  291. #
  292. # Arguments:
  293. # w -        The scrollbar widget.
  294. # orient -    Which kinds of scrollbars this applies to:  "h" for
  295. #        horizontal, "v" for vertical, "hv" for both.
  296. # amount -    How many units to scroll:  typically 1 or -1.
  297.  
  298. proc tkScrollByUnits {w orient amount} {
  299.     set cmd [$w cget -command]
  300.     if {($cmd == "") || ([string first \
  301.         [string index [$w cget -orient] 0] $orient] < 0)} {
  302.     return
  303.     }
  304.     set info [$w get]
  305.     if {[llength $info] == 2} {
  306.     uplevel #0 $cmd scroll $amount units
  307.     } else {
  308.     uplevel #0 $cmd [expr [lindex $info 2] + $amount]
  309.     }
  310. }
  311.  
  312. # tkScrollByPages --
  313. # This procedure tells the scrollbar's associated widget to scroll up
  314. # or down by a given number of screenfuls.  It notifies the associated
  315. # widget in different ways for old and new command syntaxes.
  316. #
  317. # Arguments:
  318. # w -        The scrollbar widget.
  319. # orient -    Which kinds of scrollbars this applies to:  "h" for
  320. #        horizontal, "v" for vertical, "hv" for both.
  321. # amount -    How many screens to scroll:  typically 1 or -1.
  322.  
  323. proc tkScrollByPages {w orient amount} {
  324.     set cmd [$w cget -command]
  325.     if {($cmd == "") || ([string first \
  326.         [string index [$w cget -orient] 0] $orient] < 0)} {
  327.     return
  328.     }
  329.     set info [$w get]
  330.     if {[llength $info] == 2} {
  331.     uplevel #0 $cmd scroll $amount pages
  332.     } else {
  333.     uplevel #0 $cmd [expr [lindex $info 2] + $amount*([lindex $info 1] - 1)]
  334.     }
  335. }
  336.  
  337. # tkScrollToPos --
  338. # This procedure tells the scrollbar's associated widget to scroll to
  339. # a particular location, given by a fraction between 0 and 1.  It notifies
  340. # the associated widget in different ways for old and new command syntaxes.
  341. #
  342. # Arguments:
  343. # w -        The scrollbar widget.
  344. # pos -        A fraction between 0 and 1 indicating a desired position
  345. #        in the document.
  346.  
  347. proc tkScrollToPos {w pos} {
  348.     set cmd [$w cget -command]
  349.     if {($cmd == "")} {
  350.     return
  351.     }
  352.     set info [$w get]
  353.     if {[llength $info] == 2} {
  354.     uplevel #0 $cmd moveto $pos
  355.     } else {
  356.     uplevel #0 $cmd [expr round([lindex $info 0]*$pos)]
  357.     }
  358. }
  359.  
  360. # tkScrollTopBottom
  361. # Scroll to the top or bottom of the document, depending on the mouse
  362. # position.
  363. #
  364. # Arguments:
  365. # w -        The scrollbar widget.
  366. # x, y -    Mouse coordinates within the widget.
  367.  
  368. proc tkScrollTopBottom {w x y} {
  369.     global tkPriv
  370.     set element [$w identify $x $y]
  371.     if [string match *1 $element] {
  372.     tkScrollToPos $w 0
  373.     } elseif [string match *2 $element] {
  374.     tkScrollToPos $w 1
  375.     }
  376.  
  377.     # Set tkPriv(relief), since it's needed by tkScrollButtonUp.
  378.  
  379.     set tkPriv(relief) [$w cget -activerelief]
  380. }
  381.  
  382. # tkScrollButton2Down
  383. # This procedure is invoked when button 2 is pressed over a scrollbar.
  384. # If the button is over the trough or slider, it sets the scrollbar to
  385. # the mouse position and starts a slider drag.  Otherwise it just
  386. # behaves the same as button 1.
  387. #
  388. # Arguments:
  389. # w -        The scrollbar widget.
  390. # x, y -    Mouse coordinates within the widget.
  391.  
  392. proc tkScrollButton2Down {w x y} {
  393.     global tkPriv
  394.     set element [$w identify $x $y]
  395.     if {($element == "arrow1") || ($element == "arrow2")} {
  396.     tkScrollButtonDown $w $x $y
  397.     return
  398.     }
  399.     tkScrollToPos $w [$w fraction $x $y]
  400.     set tkPriv(relief) [$w cget -activerelief]
  401.  
  402.     # Need the "update idletasks" below so that the widget calls us
  403.     # back to reset the actual scrollbar position before we start the
  404.     # slider drag.
  405.  
  406.     update idletasks
  407.     $w configure -activerelief sunken
  408.     $w activate slider
  409.     tkScrollStartDrag $w $x $y
  410. }
  411.