home *** CD-ROM | disk | FTP | other *** search
/ Freesoft 1999 February / Freesoft_1999-02_cd.bin / Recenz / Utility / DisplayDoctorLinux / scitech-display-doctor-1.0beta-3.i386.rpm / scitech-display-doctor-1.0beta.3.cpio.gz / scitech-display-doctor-1.0beta.3.cpio / usr / lib / nucleus / XF86Setup / tcllib / scrollbar.tcl < prev    next >
Text File  |  1998-09-19  |  11KB  |  412 lines

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