home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 May / PCWorld_2002-05_cd.bin / Software / TemaCD / activetcltk / ActiveTcl8.3.4.1-8.win32-ix86.exe / ActiveTcl8.3.4.1-win32-ix86 / lib / tk8.3 / scrlbar.tcl < prev    next >
Encoding:
Text File  |  2001-10-22  |  11.5 KB  |  417 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. # RCS: @(#) $Id: scrlbar.tcl,v 1.8 2000/01/06 02:22:24 hobbs Exp $
  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. if {[string compare $tcl_platform(platform) "windows"] && \
  21.     [string compare $tcl_platform(platform) "macintosh"]} {
  22.  
  23. bind Scrollbar <Enter> {
  24.     if {$tk_strictMotif} {
  25.     set tkPriv(activeBg) [%W cget -activebackground]
  26.     %W config -activebackground [%W cget -background]
  27.     }
  28.     %W activate [%W identify %x %y]
  29. }
  30. bind Scrollbar <Motion> {
  31.     %W activate [%W identify %x %y]
  32. }
  33.  
  34. # The "info exists" command in the following binding handles the
  35. # situation where a Leave event occurs for a scrollbar without the Enter
  36. # event.  This seems to happen on some systems (such as Solaris 2.4) for
  37. # unknown reasons.
  38.  
  39. bind Scrollbar <Leave> {
  40.     if {$tk_strictMotif && [info exists tkPriv(activeBg)]} {
  41.     %W config -activebackground $tkPriv(activeBg)
  42.     }
  43.     %W activate {}
  44. }
  45. bind Scrollbar <1> {
  46.     tkScrollButtonDown %W %x %y
  47. }
  48. bind Scrollbar <B1-Motion> {
  49.     tkScrollDrag %W %x %y
  50. }
  51. bind Scrollbar <B1-B2-Motion> {
  52.     tkScrollDrag %W %x %y
  53. }
  54. bind Scrollbar <ButtonRelease-1> {
  55.     tkScrollButtonUp %W %x %y
  56. }
  57. bind Scrollbar <B1-Leave> {
  58.     # Prevents <Leave> binding from being invoked.
  59. }
  60. bind Scrollbar <B1-Enter> {
  61.     # Prevents <Enter> binding from being invoked.
  62. }
  63. bind Scrollbar <2> {
  64.     tkScrollButton2Down %W %x %y
  65. }
  66. bind Scrollbar <B1-2> {
  67.     # Do nothing, since button 1 is already down.
  68. }
  69. bind Scrollbar <B2-1> {
  70.     # Do nothing, since button 2 is already down.
  71. }
  72. bind Scrollbar <B2-Motion> {
  73.     tkScrollDrag %W %x %y
  74. }
  75. bind Scrollbar <ButtonRelease-2> {
  76.     tkScrollButtonUp %W %x %y
  77. }
  78. bind Scrollbar <B1-ButtonRelease-2> {
  79.     # Do nothing:  B1 release will handle it.
  80. }
  81. bind Scrollbar <B2-ButtonRelease-1> {
  82.     # Do nothing:  B2 release will handle it.
  83. }
  84. bind Scrollbar <B2-Leave> {
  85.     # Prevents <Leave> binding from being invoked.
  86. }
  87. bind Scrollbar <B2-Enter> {
  88.     # Prevents <Enter> binding from being invoked.
  89. }
  90. bind Scrollbar <Control-1> {
  91.     tkScrollTopBottom %W %x %y
  92. }
  93. bind Scrollbar <Control-2> {
  94.     tkScrollTopBottom %W %x %y
  95. }
  96.  
  97. bind Scrollbar <Up> {
  98.     tkScrollByUnits %W v -1
  99. }
  100. bind Scrollbar <Down> {
  101.     tkScrollByUnits %W v 1
  102. }
  103. bind Scrollbar <Control-Up> {
  104.     tkScrollByPages %W v -1
  105. }
  106. bind Scrollbar <Control-Down> {
  107.     tkScrollByPages %W v 1
  108. }
  109. bind Scrollbar <Left> {
  110.     tkScrollByUnits %W h -1
  111. }
  112. bind Scrollbar <Right> {
  113.     tkScrollByUnits %W h 1
  114. }
  115. bind Scrollbar <Control-Left> {
  116.     tkScrollByPages %W h -1
  117. }
  118. bind Scrollbar <Control-Right> {
  119.     tkScrollByPages %W h 1
  120. }
  121. bind Scrollbar <Prior> {
  122.     tkScrollByPages %W hv -1
  123. }
  124. bind Scrollbar <Next> {
  125.     tkScrollByPages %W hv 1
  126. }
  127. bind Scrollbar <Home> {
  128.     tkScrollToPos %W 0
  129. }
  130. bind Scrollbar <End> {
  131.     tkScrollToPos %W 1
  132. }
  133. }
  134. # tkScrollButtonDown --
  135. # This procedure is invoked when a button is pressed in a scrollbar.
  136. # It changes the way the scrollbar is displayed and takes actions
  137. # depending on where the mouse is.
  138. #
  139. # Arguments:
  140. # w -        The scrollbar widget.
  141. # x, y -    Mouse coordinates.
  142.  
  143. proc tkScrollButtonDown {w x y} {
  144.     global tkPriv
  145.     set tkPriv(relief) [$w cget -activerelief]
  146.     $w configure -activerelief sunken
  147.     set element [$w identify $x $y]
  148.     if {[string equal $element "slider"]} {
  149.     tkScrollStartDrag $w $x $y
  150.     } else {
  151.     tkScrollSelect $w $element initial
  152.     }
  153. }
  154.  
  155. # tkScrollButtonUp --
  156. # This procedure is invoked when a button is released in a scrollbar.
  157. # It cancels scans and auto-repeats that were in progress, and restores
  158. # the way the active element is displayed.
  159. #
  160. # Arguments:
  161. # w -        The scrollbar widget.
  162. # x, y -    Mouse coordinates.
  163.  
  164. proc tkScrollButtonUp {w x y} {
  165.     global tkPriv
  166.     tkCancelRepeat
  167.     if {[info exists tkPriv(relief)]} {
  168.     # Avoid error due to spurious release events
  169.     $w configure -activerelief $tkPriv(relief)
  170.     tkScrollEndDrag $w $x $y
  171.     $w activate [$w identify $x $y]
  172.     }
  173. }
  174.  
  175. # tkScrollSelect --
  176. # This procedure is invoked when a button is pressed over the scrollbar.
  177. # It invokes one of several scrolling actions depending on where in
  178. # the scrollbar the button was pressed.
  179. #
  180. # Arguments:
  181. # w -        The scrollbar widget.
  182. # element -    The element of the scrollbar that was selected, such
  183. #        as "arrow1" or "trough2".  Shouldn't be "slider".
  184. # repeat -    Whether and how to auto-repeat the action:  "noRepeat"
  185. #        means don't auto-repeat, "initial" means this is the
  186. #        first action in an auto-repeat sequence, and "again"
  187. #        means this is the second repetition or later.
  188.  
  189. proc tkScrollSelect {w element repeat} {
  190.     global tkPriv
  191.     if {![winfo exists $w]} return
  192.     switch -- $element {
  193.     "arrow1"    {tkScrollByUnits $w hv -1}
  194.     "trough1"    {tkScrollByPages $w hv -1}
  195.     "trough2"    {tkScrollByPages $w hv 1}
  196.     "arrow2"    {tkScrollByUnits $w hv 1}
  197.     default        {return}
  198.     }
  199.     if {[string equal $repeat "again"]} {
  200.     set tkPriv(afterId) [after [$w cget -repeatinterval] \
  201.         [list tkScrollSelect $w $element again]]
  202.     } elseif {[string equal $repeat "initial"]} {
  203.     set delay [$w cget -repeatdelay]
  204.     if {$delay > 0} {
  205.         set tkPriv(afterId) [after $delay \
  206.             [list tkScrollSelect $w $element again]]
  207.     }
  208.     }
  209. }
  210.  
  211. # tkScrollStartDrag --
  212. # This procedure is called to initiate a drag of the slider.  It just
  213. # remembers the starting position of the mouse and slider.
  214. #
  215. # Arguments:
  216. # w -        The scrollbar widget.
  217. # x, y -    The mouse position at the start of the drag operation.
  218.  
  219. proc tkScrollStartDrag {w x y} {
  220.     global tkPriv
  221.  
  222.     if {[string equal [$w cget -command] ""]} {
  223.     return
  224.     }
  225.     set tkPriv(pressX) $x
  226.     set tkPriv(pressY) $y
  227.     set tkPriv(initValues) [$w get]
  228.     set iv0 [lindex $tkPriv(initValues) 0]
  229.     if {[llength $tkPriv(initValues)] == 2} {
  230.     set tkPriv(initPos) $iv0
  231.     } elseif {$iv0 == 0} {
  232.     set tkPriv(initPos) 0.0
  233.     } else {
  234.     set tkPriv(initPos) [expr {(double([lindex $tkPriv(initValues) 2])) \
  235.         / [lindex $tkPriv(initValues) 0]}]
  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 {[string equal $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 [list $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 {[string equal $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 {[string equal $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 {[string equal $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 {[string equal $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.     global tkPriv
  376.     set element [$w identify $x $y]
  377.     if {[string match *1 $element]} {
  378.     tkScrollToPos $w 0
  379.     } elseif {[string match *2 $element]} {
  380.     tkScrollToPos $w 1
  381.     }
  382.  
  383.     # Set tkPriv(relief), since it's needed by tkScrollButtonUp.
  384.  
  385.     set tkPriv(relief) [$w cget -activerelief]
  386. }
  387.  
  388. # tkScrollButton2Down
  389. # This procedure is invoked when button 2 is pressed over a scrollbar.
  390. # If the button is over the trough or slider, it sets the scrollbar to
  391. # the mouse position and starts a slider drag.  Otherwise it just
  392. # behaves the same as button 1.
  393. #
  394. # Arguments:
  395. # w -        The scrollbar widget.
  396. # x, y -    Mouse coordinates within the widget.
  397.  
  398. proc tkScrollButton2Down {w x y} {
  399.     global tkPriv
  400.     set element [$w identify $x $y]
  401.     if {[string match {arrow[12]} $element]} {
  402.     tkScrollButtonDown $w $x $y
  403.     return
  404.     }
  405.     tkScrollToPos $w [$w fraction $x $y]
  406.     set tkPriv(relief) [$w cget -activerelief]
  407.  
  408.     # Need the "update idletasks" below so that the widget calls us
  409.     # back to reset the actual scrollbar position before we start the
  410.     # slider drag.
  411.  
  412.     update idletasks
  413.     $w configure -activerelief sunken
  414.     $w activate slider
  415.     tkScrollStartDrag $w $x $y
  416. }
  417.