3 # This file defines the default bindings for Tk scrollbar widgets.
4 # It also provides procedures that help in implementing the bindings.
6 # RCS: @(#) $Id: scrlbar.tcl,v 1.10.2.3 2006/03/17 10:50:11 patthoyts Exp $
8 # Copyright (c) 1994 The Regents of the University of California.
9 # Copyright (c) 1994-1996 Sun Microsystems, Inc.
11 # See the file "license.terms" for information on usage and redistribution
12 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
15 #-------------------------------------------------------------------------
16 # The code below creates the default class bindings for scrollbars.
17 #-------------------------------------------------------------------------
19 # Standard Motif bindings:
20 if {[tk windowingsystem
] eq
"x11"} {
22 bind Scrollbar
<Enter
> {
23 if {$tk_strictMotif} {
24 set tk::Priv(activeBg
) [%W cget
-activebackground]
25 %W configure
-activebackground [%W cget
-background]
27 %W activate
[%W identify
%x
%y
]
29 bind Scrollbar
<Motion
> {
30 %W activate
[%W identify
%x
%y
]
33 # The "info exists" command in the following binding handles the
34 # situation where a Leave event occurs for a scrollbar without the Enter
35 # event. This seems to happen on some systems (such as Solaris 2.4) for
38 bind Scrollbar
<Leave
> {
39 if {$tk_strictMotif && [info exists
tk::Priv(activeBg
)]} {
40 %W configure
-activebackground $tk::Priv(activeBg
)
45 tk::ScrollButtonDown %W
%x
%y
47 bind Scrollbar
<B1-Motion
> {
48 tk::ScrollDrag %W
%x
%y
50 bind Scrollbar
<B1-B2-Motion
> {
51 tk::ScrollDrag %W
%x
%y
53 bind Scrollbar
<ButtonRelease-1
> {
54 tk::ScrollButtonUp %W
%x
%y
56 bind Scrollbar
<B1-Leave
> {
57 # Prevents <Leave> binding from being invoked.
59 bind Scrollbar
<B1-Enter
> {
60 # Prevents <Enter> binding from being invoked.
63 tk::ScrollButton2Down %W
%x
%y
65 bind Scrollbar
<B1-2
> {
66 # Do nothing, since button 1 is already down.
68 bind Scrollbar
<B2-1
> {
69 # Do nothing, since button 2 is already down.
71 bind Scrollbar
<B2-Motion
> {
72 tk::ScrollDrag %W
%x
%y
74 bind Scrollbar
<ButtonRelease-2
> {
75 tk::ScrollButtonUp %W
%x
%y
77 bind Scrollbar
<B1-ButtonRelease-2
> {
78 # Do nothing: B1 release will handle it.
80 bind Scrollbar
<B2-ButtonRelease-1
> {
81 # Do nothing: B2 release will handle it.
83 bind Scrollbar
<B2-Leave
> {
84 # Prevents <Leave> binding from being invoked.
86 bind Scrollbar
<B2-Enter
> {
87 # Prevents <Enter> binding from being invoked.
89 bind Scrollbar
<Control-1
> {
90 tk::ScrollTopBottom %W
%x
%y
92 bind Scrollbar
<Control-2
> {
93 tk::ScrollTopBottom %W
%x
%y
97 tk::ScrollByUnits %W v
-1
99 bind Scrollbar
<Down
> {
100 tk::ScrollByUnits %W v
1
102 bind Scrollbar
<Control-Up
> {
103 tk::ScrollByPages %W v
-1
105 bind Scrollbar
<Control-Down
> {
106 tk::ScrollByPages %W v
1
108 bind Scrollbar
<Left
> {
109 tk::ScrollByUnits %W h
-1
111 bind Scrollbar
<Right
> {
112 tk::ScrollByUnits %W h
1
114 bind Scrollbar
<Control-Left
> {
115 tk::ScrollByPages %W h
-1
117 bind Scrollbar
<Control-Right
> {
118 tk::ScrollByPages %W h
1
120 bind Scrollbar
<Prior
> {
121 tk::ScrollByPages %W hv
-1
123 bind Scrollbar
<Next
> {
124 tk::ScrollByPages %W hv
1
126 bind Scrollbar
<Home
> {
129 bind Scrollbar
<End
> {
133 if {[tk windowingsystem
] eq
"classic" ||
[tk windowingsystem
] eq
"aqua"} {
134 bind Scrollbar
<MouseWheel
> {
135 tk::ScrollByUnits %W v
[expr {- (%D
)}]
137 bind Scrollbar
<Option-MouseWheel
> {
138 tk::ScrollByUnits %W v
[expr {-10 * (%D
)}]
140 bind Scrollbar
<Shift-MouseWheel
> {
141 tk::ScrollByUnits %W h
[expr {- (%D
)}]
143 bind Scrollbar
<Shift-Option-MouseWheel
> {
144 tk::ScrollByUnits %W h
[expr {-10 * (%D
)}]
147 # tk::ScrollButtonDown --
148 # This procedure is invoked when a button is pressed in a scrollbar.
149 # It changes the way the scrollbar is displayed and takes actions
150 # depending on where the mouse is.
153 # w - The scrollbar widget.
154 # x, y - Mouse coordinates.
156 proc tk::ScrollButtonDown {w x y
} {
158 set Priv
(relief
) [$w cget
-activerelief]
159 $w configure
-activerelief sunken
160 set element
[$w identify
$x $y]
161 if {$element eq
"slider"} {
162 ScrollStartDrag
$w $x $y
164 ScrollSelect
$w $element initial
168 # ::tk::ScrollButtonUp --
169 # This procedure is invoked when a button is released in a scrollbar.
170 # It cancels scans and auto-repeats that were in progress, and restores
171 # the way the active element is displayed.
174 # w - The scrollbar widget.
175 # x, y - Mouse coordinates.
177 proc ::tk::ScrollButtonUp {w x y
} {
180 if {[info exists Priv
(relief
)]} {
181 # Avoid error due to spurious release events
182 $w configure
-activerelief $Priv(relief
)
183 ScrollEndDrag
$w $x $y
184 $w activate
[$w identify
$x $y]
188 # ::tk::ScrollSelect --
189 # This procedure is invoked when a button is pressed over the scrollbar.
190 # It invokes one of several scrolling actions depending on where in
191 # the scrollbar the button was pressed.
194 # w - The scrollbar widget.
195 # element - The element of the scrollbar that was selected, such
196 # as "arrow1" or "trough2". Shouldn't be "slider".
197 # repeat - Whether and how to auto-repeat the action: "noRepeat"
198 # means don't auto-repeat, "initial" means this is the
199 # first action in an auto-repeat sequence, and "again"
200 # means this is the second repetition or later.
202 proc ::tk::ScrollSelect {w element repeat
} {
204 if {![winfo exists
$w]} return
206 "arrow1" {ScrollByUnits
$w hv
-1}
207 "trough1" {ScrollByPages
$w hv
-1}
208 "trough2" {ScrollByPages
$w hv
1}
209 "arrow2" {ScrollByUnits
$w hv
1}
212 if {$repeat eq
"again"} {
213 set Priv
(afterId
) [after [$w cget
-repeatinterval] \
214 [list tk::ScrollSelect $w $element again
]]
215 } elseif
{$repeat eq
"initial"} {
216 set delay
[$w cget
-repeatdelay]
218 set Priv
(afterId
) [after $delay \
219 [list tk::ScrollSelect $w $element again
]]
224 # ::tk::ScrollStartDrag --
225 # This procedure is called to initiate a drag of the slider. It just
226 # remembers the starting position of the mouse and slider.
229 # w - The scrollbar widget.
230 # x, y - The mouse position at the start of the drag operation.
232 proc ::tk::ScrollStartDrag {w x y
} {
235 if {[$w cget
-command] eq
""} {
240 set Priv
(initValues
) [$w get
]
241 set iv0
[lindex $Priv(initValues
) 0]
242 if {[llength $Priv(initValues
)] == 2} {
243 set Priv
(initPos
) $iv0
244 } elseif
{$iv0 == 0} {
245 set Priv
(initPos
) 0.0
247 set Priv
(initPos
) [expr {(double
([lindex $Priv(initValues
) 2])) \
248 / [lindex $Priv(initValues
) 0]}]
252 # ::tk::ScrollDrag --
253 # This procedure is called for each mouse motion even when the slider
254 # is being dragged. It notifies the associated widget if we're not
255 # jump scrolling, and it just updates the scrollbar if we are jump
259 # w - The scrollbar widget.
260 # x, y - The current mouse position.
262 proc ::tk::ScrollDrag {w x y
} {
265 if {$Priv(initPos
) eq
""} {
268 set delta
[$w delta
[expr {$x - $Priv(pressX
)}] [expr {$y - $Priv(pressY
)}]]
269 if {[$w cget
-jump]} {
270 if {[llength $Priv(initValues
)] == 2} {
271 $w set [expr {[lindex $Priv(initValues
) 0] + $delta}] \
272 [expr {[lindex $Priv(initValues
) 1] + $delta}]
274 set delta
[expr {round
($delta * [lindex $Priv(initValues
) 0])}]
275 eval [list $w] set [lreplace $Priv(initValues
) 2 3 \
276 [expr {[lindex $Priv(initValues
) 2] + $delta}] \
277 [expr {[lindex $Priv(initValues
) 3] + $delta}]]
280 ScrollToPos
$w [expr {$Priv(initPos
) + $delta}]
284 # ::tk::ScrollEndDrag --
285 # This procedure is called to end an interactive drag of the slider.
286 # It scrolls the window if we're in jump mode, otherwise it does nothing.
289 # w - The scrollbar widget.
290 # x, y - The mouse position at the end of the drag operation.
292 proc ::tk::ScrollEndDrag {w x y
} {
295 if {$Priv(initPos
) eq
""} {
298 if {[$w cget
-jump]} {
299 set delta
[$w delta
[expr {$x - $Priv(pressX
)}] \
300 [expr {$y - $Priv(pressY
)}]]
301 ScrollToPos
$w [expr {$Priv(initPos
) + $delta}]
306 # ::tk::ScrollByUnits --
307 # This procedure tells the scrollbar's associated widget to scroll up
308 # or down by a given number of units. It notifies the associated widget
309 # in different ways for old and new command syntaxes.
312 # w - The scrollbar widget.
313 # orient - Which kinds of scrollbars this applies to: "h" for
314 # horizontal, "v" for vertical, "hv" for both.
315 # amount - How many units to scroll: typically 1 or -1.
317 proc ::tk::ScrollByUnits {w orient amount
} {
318 set cmd
[$w cget
-command]
319 if {$cmd eq
"" ||
([string first
[string index
[$w cget
-orient] 0] $orient] < 0)} {
323 if {[llength $info] == 2} {
324 uplevel #0 $cmd scroll $amount units
326 uplevel #0 $cmd [expr {[lindex $info 2] + $amount}]
330 # ::tk::ScrollByPages --
331 # This procedure tells the scrollbar's associated widget to scroll up
332 # or down by a given number of screenfuls. It notifies the associated
333 # widget in different ways for old and new command syntaxes.
336 # w - The scrollbar widget.
337 # orient - Which kinds of scrollbars this applies to: "h" for
338 # horizontal, "v" for vertical, "hv" for both.
339 # amount - How many screens to scroll: typically 1 or -1.
341 proc ::tk::ScrollByPages {w orient amount
} {
342 set cmd
[$w cget
-command]
343 if {$cmd eq
"" ||
([string first
[string index
[$w cget
-orient] 0] $orient] < 0)} {
347 if {[llength $info] == 2} {
348 uplevel #0 $cmd scroll $amount pages
350 uplevel #0 $cmd [expr {[lindex $info 2] + $amount*([lindex $info 1] - 1)}]
354 # ::tk::ScrollToPos --
355 # This procedure tells the scrollbar's associated widget to scroll to
356 # a particular location, given by a fraction between 0 and 1. It notifies
357 # the associated widget in different ways for old and new command syntaxes.
360 # w - The scrollbar widget.
361 # pos - A fraction between 0 and 1 indicating a desired position
364 proc ::tk::ScrollToPos {w pos
} {
365 set cmd
[$w cget
-command]
370 if {[llength $info] == 2} {
371 uplevel #0 $cmd moveto $pos
373 uplevel #0 $cmd [expr {round([lindex $info 0]*$pos)}]
377 # ::tk::ScrollTopBottom
378 # Scroll to the top or bottom of the document, depending on the mouse
382 # w - The scrollbar widget.
383 # x, y - Mouse coordinates within the widget.
385 proc ::tk::ScrollTopBottom {w x y
} {
387 set element
[$w identify
$x $y]
388 if {[string match
*1 $element]} {
390 } elseif
{[string match
*2 $element]} {
394 # Set Priv(relief), since it's needed by tk::ScrollButtonUp.
396 set Priv
(relief
) [$w cget
-activerelief]
399 # ::tk::ScrollButton2Down
400 # This procedure is invoked when button 2 is pressed over a scrollbar.
401 # If the button is over the trough or slider, it sets the scrollbar to
402 # the mouse position and starts a slider drag. Otherwise it just
403 # behaves the same as button 1.
406 # w - The scrollbar widget.
407 # x, y - Mouse coordinates within the widget.
409 proc ::tk::ScrollButton2Down {w x y
} {
411 set element
[$w identify
$x $y]
412 if {[string match
{arrow
[12]} $element]} {
413 ScrollButtonDown
$w $x $y
416 ScrollToPos
$w [$w fraction
$x $y]
417 set Priv
(relief
) [$w cget
-activerelief]
419 # Need the "update idletasks" below so that the widget calls us
420 # back to reset the actual scrollbar position before we start the
424 $w configure
-activerelief sunken
426 ScrollStartDrag
$w $x $y