3 # This file defines the default bindings for Tk scrollbar widgets.
4 # It also provides procedures that help in implementing the bindings.
6 # Copyright (c) 1994 The Regents of the University of California.
7 # Copyright (c) 1994-1996 Sun Microsystems, Inc.
9 # See the file "license.terms" for information on usage and redistribution
10 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 #-------------------------------------------------------------------------
14 # The code below creates the default class bindings for scrollbars.
15 #-------------------------------------------------------------------------
17 # Standard Motif bindings:
18 if {[tk windowingsystem] eq "x11" || [tk windowingsystem] eq "aqua"} {
20 bind Scrollbar <Enter> {
21 if {$tk_strictMotif} {
22 set tk::Priv(activeBg) [%W cget -activebackground]
23 %W configure -activebackground [%W cget -background]
25 %W activate [%W identify %x %y]
27 bind Scrollbar <Motion> {
28 %W activate [%W identify %x %y]
31 # The "info exists" command in the following binding handles the
32 # situation where a Leave event occurs for a scrollbar without the Enter
33 # event. This seems to happen on some systems (such as Solaris 2.4) for
36 bind Scrollbar <Leave> {
37 if {$tk_strictMotif && [info exists tk::Priv(activeBg)]} {
38 %W configure -activebackground $tk::Priv(activeBg)
43 tk::ScrollButtonDown %W %x %y
45 bind Scrollbar <B1-Motion> {
46 tk::ScrollDrag %W %x %y
48 bind Scrollbar <B1-B2-Motion> {
49 tk::ScrollDrag %W %x %y
51 bind Scrollbar <ButtonRelease-1> {
52 tk::ScrollButtonUp %W %x %y
54 bind Scrollbar <B1-Leave> {
55 # Prevents <Leave> binding from being invoked.
57 bind Scrollbar <B1-Enter> {
58 # Prevents <Enter> binding from being invoked.
61 tk::ScrollButton2Down %W %x %y
63 bind Scrollbar <B1-2> {
64 # Do nothing, since button 1 is already down.
66 bind Scrollbar <B2-1> {
67 # Do nothing, since button 2 is already down.
69 bind Scrollbar <B2-Motion> {
70 tk::ScrollDrag %W %x %y
72 bind Scrollbar <ButtonRelease-2> {
73 tk::ScrollButtonUp %W %x %y
75 bind Scrollbar <B1-ButtonRelease-2> {
76 # Do nothing: B1 release will handle it.
78 bind Scrollbar <B2-ButtonRelease-1> {
79 # Do nothing: B2 release will handle it.
81 bind Scrollbar <B2-Leave> {
82 # Prevents <Leave> binding from being invoked.
84 bind Scrollbar <B2-Enter> {
85 # Prevents <Enter> binding from being invoked.
87 bind Scrollbar <Control-1> {
88 tk::ScrollTopBottom %W %x %y
90 bind Scrollbar <Control-2> {
91 tk::ScrollTopBottom %W %x %y
94 bind Scrollbar <<PrevLine>> {
95 tk::ScrollByUnits %W v -1
97 bind Scrollbar <<NextLine>> {
98 tk::ScrollByUnits %W v 1
100 bind Scrollbar <<PrevPara>> {
101 tk::ScrollByPages %W v -1
103 bind Scrollbar <<NextPara>> {
104 tk::ScrollByPages %W v 1
106 bind Scrollbar <<PrevChar>> {
107 tk::ScrollByUnits %W h -1
109 bind Scrollbar <<NextChar>> {
110 tk::ScrollByUnits %W h 1
112 bind Scrollbar <<PrevWord>> {
113 tk::ScrollByPages %W h -1
115 bind Scrollbar <<NextWord>> {
116 tk::ScrollByPages %W h 1
118 bind Scrollbar <Prior> {
119 tk::ScrollByPages %W hv -1
121 bind Scrollbar <Next> {
122 tk::ScrollByPages %W hv 1
124 bind Scrollbar <<LineStart>> {
127 bind Scrollbar <<LineEnd>> {
131 switch [tk windowingsystem] {
133 bind Scrollbar <MouseWheel> {
134 tk::ScrollByUnits %W v [expr {- (%D)}]
136 bind Scrollbar <Option-MouseWheel> {
137 tk::ScrollByUnits %W v [expr {-10 * (%D)}]
139 bind Scrollbar <Shift-MouseWheel> {
140 tk::ScrollByUnits %W h [expr {- (%D)}]
142 bind Scrollbar <Shift-Option-MouseWheel> {
143 tk::ScrollByUnits %W h [expr {-10 * (%D)}]
147 bind Scrollbar <MouseWheel> {
148 tk::ScrollByUnits %W v [expr {- (%D / 120) * 4}]
150 bind Scrollbar <Shift-MouseWheel> {
151 tk::ScrollByUnits %W h [expr {- (%D / 120) * 4}]
155 bind Scrollbar <4> {tk::ScrollByUnits %W v -5}
156 bind Scrollbar <5> {tk::ScrollByUnits %W v 5}
157 bind Scrollbar <Shift-4> {tk::ScrollByUnits %W h -5}
158 bind Scrollbar <Shift-5> {tk::ScrollByUnits %W h 5}
161 # tk::ScrollButtonDown --
162 # This procedure is invoked when a button is pressed in a scrollbar.
163 # It changes the way the scrollbar is displayed and takes actions
164 # depending on where the mouse is.
167 # w - The scrollbar widget.
168 # x, y - Mouse coordinates.
170 proc tk::ScrollButtonDown {w x y} {
172 set Priv(relief) [$w cget -activerelief]
173 $w configure -activerelief sunken
174 set element [$w identify $x $y]
175 if {$element eq "slider"} {
176 ScrollStartDrag $w $x $y
178 ScrollSelect $w $element initial
182 # ::tk::ScrollButtonUp --
183 # This procedure is invoked when a button is released in a scrollbar.
184 # It cancels scans and auto-repeats that were in progress, and restores
185 # the way the active element is displayed.
188 # w - The scrollbar widget.
189 # x, y - Mouse coordinates.
191 proc ::tk::ScrollButtonUp {w x y} {
194 if {[info exists Priv(relief)]} {
195 # Avoid error due to spurious release events
196 $w configure -activerelief $Priv(relief)
197 ScrollEndDrag $w $x $y
198 $w activate [$w identify $x $y]
202 # ::tk::ScrollSelect --
203 # This procedure is invoked when a button is pressed over the scrollbar.
204 # It invokes one of several scrolling actions depending on where in
205 # the scrollbar the button was pressed.
208 # w - The scrollbar widget.
209 # element - The element of the scrollbar that was selected, such
210 # as "arrow1" or "trough2". Shouldn't be "slider".
211 # repeat - Whether and how to auto-repeat the action: "noRepeat"
212 # means don't auto-repeat, "initial" means this is the
213 # first action in an auto-repeat sequence, and "again"
214 # means this is the second repetition or later.
216 proc ::tk::ScrollSelect {w element repeat} {
218 if {![winfo exists $w]} return
220 "arrow1" {ScrollByUnits $w hv -1}
221 "trough1" {ScrollByPages $w hv -1}
222 "trough2" {ScrollByPages $w hv 1}
223 "arrow2" {ScrollByUnits $w hv 1}
226 if {$repeat eq "again"} {
227 set Priv(afterId) [after [$w cget -repeatinterval] \
228 [list tk::ScrollSelect $w $element again]]
229 } elseif {$repeat eq "initial"} {
230 set delay [$w cget -repeatdelay]
232 set Priv(afterId) [after $delay \
233 [list tk::ScrollSelect $w $element again]]
238 # ::tk::ScrollStartDrag --
239 # This procedure is called to initiate a drag of the slider. It just
240 # remembers the starting position of the mouse and slider.
243 # w - The scrollbar widget.
244 # x, y - The mouse position at the start of the drag operation.
246 proc ::tk::ScrollStartDrag {w x y} {
249 if {[$w cget -command] eq ""} {
254 set Priv(initValues) [$w get]
255 set iv0 [lindex $Priv(initValues) 0]
256 if {[llength $Priv(initValues)] == 2} {
257 set Priv(initPos) $iv0
258 } elseif {$iv0 == 0} {
259 set Priv(initPos) 0.0
261 set Priv(initPos) [expr {(double([lindex $Priv(initValues) 2])) \
262 / [lindex $Priv(initValues) 0]}]
266 # ::tk::ScrollDrag --
267 # This procedure is called for each mouse motion even when the slider
268 # is being dragged. It notifies the associated widget if we're not
269 # jump scrolling, and it just updates the scrollbar if we are jump
273 # w - The scrollbar widget.
274 # x, y - The current mouse position.
276 proc ::tk::ScrollDrag {w x y} {
279 if {$Priv(initPos) eq ""} {
282 set delta [$w delta [expr {$x - $Priv(pressX)}] [expr {$y - $Priv(pressY)}]]
283 if {[$w cget -jump]} {
284 if {[llength $Priv(initValues)] == 2} {
285 $w set [expr {[lindex $Priv(initValues) 0] + $delta}] \
286 [expr {[lindex $Priv(initValues) 1] + $delta}]
288 set delta [expr {round($delta * [lindex $Priv(initValues) 0])}]
289 eval [list $w] set [lreplace $Priv(initValues) 2 3 \
290 [expr {[lindex $Priv(initValues) 2] + $delta}] \
291 [expr {[lindex $Priv(initValues) 3] + $delta}]]
294 ScrollToPos $w [expr {$Priv(initPos) + $delta}]
298 # ::tk::ScrollEndDrag --
299 # This procedure is called to end an interactive drag of the slider.
300 # It scrolls the window if we're in jump mode, otherwise it does nothing.
303 # w - The scrollbar widget.
304 # x, y - The mouse position at the end of the drag operation.
306 proc ::tk::ScrollEndDrag {w x y} {
309 if {$Priv(initPos) eq ""} {
312 if {[$w cget -jump]} {
313 set delta [$w delta [expr {$x - $Priv(pressX)}] \
314 [expr {$y - $Priv(pressY)}]]
315 ScrollToPos $w [expr {$Priv(initPos) + $delta}]
320 # ::tk::ScrollByUnits --
321 # This procedure tells the scrollbar's associated widget to scroll up
322 # or down by a given number of units. It notifies the associated widget
323 # in different ways for old and new command syntaxes.
326 # w - The scrollbar widget.
327 # orient - Which kinds of scrollbars this applies to: "h" for
328 # horizontal, "v" for vertical, "hv" for both.
329 # amount - How many units to scroll: typically 1 or -1.
331 proc ::tk::ScrollByUnits {w orient amount} {
332 set cmd [$w cget -command]
333 if {$cmd eq "" || ([string first \
334 [string index [$w cget -orient] 0] $orient] < 0)} {
338 if {[llength $info] == 2} {
339 uplevel #0 $cmd scroll $amount units
341 uplevel #0 $cmd [expr {[lindex $info 2] + $amount}]
345 # ::tk::ScrollByPages --
346 # This procedure tells the scrollbar's associated widget to scroll up
347 # or down by a given number of screenfuls. It notifies the associated
348 # widget in different ways for old and new command syntaxes.
351 # w - The scrollbar widget.
352 # orient - Which kinds of scrollbars this applies to: "h" for
353 # horizontal, "v" for vertical, "hv" for both.
354 # amount - How many screens to scroll: typically 1 or -1.
356 proc ::tk::ScrollByPages {w orient amount} {
357 set cmd [$w cget -command]
358 if {$cmd eq "" || ([string first \
359 [string index [$w cget -orient] 0] $orient] < 0)} {
363 if {[llength $info] == 2} {
364 uplevel #0 $cmd scroll $amount pages
366 uplevel #0 $cmd [expr {[lindex $info 2] + $amount*([lindex $info 1] - 1)}]
370 # ::tk::ScrollToPos --
371 # This procedure tells the scrollbar's associated widget to scroll to
372 # a particular location, given by a fraction between 0 and 1. It notifies
373 # the associated widget in different ways for old and new command syntaxes.
376 # w - The scrollbar widget.
377 # pos - A fraction between 0 and 1 indicating a desired position
380 proc ::tk::ScrollToPos {w pos} {
381 set cmd [$w cget -command]
386 if {[llength $info] == 2} {
387 uplevel #0 $cmd moveto $pos
389 uplevel #0 $cmd [expr {round([lindex $info 0]*$pos)}]
393 # ::tk::ScrollTopBottom
394 # Scroll to the top or bottom of the document, depending on the mouse
398 # w - The scrollbar widget.
399 # x, y - Mouse coordinates within the widget.
401 proc ::tk::ScrollTopBottom {w x y} {
403 set element [$w identify $x $y]
404 if {[string match *1 $element]} {
406 } elseif {[string match *2 $element]} {
410 # Set Priv(relief), since it's needed by tk::ScrollButtonUp.
412 set Priv(relief) [$w cget -activerelief]
415 # ::tk::ScrollButton2Down
416 # This procedure is invoked when button 2 is pressed over a scrollbar.
417 # If the button is over the trough or slider, it sets the scrollbar to
418 # the mouse position and starts a slider drag. Otherwise it just
419 # behaves the same as button 1.
422 # w - The scrollbar widget.
423 # x, y - Mouse coordinates within the widget.
425 proc ::tk::ScrollButton2Down {w x y} {
427 set element [$w identify $x $y]
428 if {[string match {arrow[12]} $element]} {
429 ScrollButtonDown $w $x $y
432 ScrollToPos $w [$w fraction $x $y]
433 set Priv(relief) [$w cget -activerelief]
435 # Need the "update idletasks" below so that the widget calls us
436 # back to reset the actual scrollbar position before we start the
440 $w configure -activerelief sunken
442 ScrollStartDrag $w $x $y