Mark msysGit as obsolete
[msysgit.git] / mingw / lib / tk8.5 / scrlbar.tcl
blob4cb95bd605a7a4c6fd77c9deaf5a1f05d5ad1456
1 # scrlbar.tcl --
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"} {
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
34 # unknown reasons.
36 bind Scrollbar <Leave> {
37 if {$tk_strictMotif && [info exists tk::Priv(activeBg)]} {
38 %W configure -activebackground $tk::Priv(activeBg)
40 %W activate {}
42 bind Scrollbar <1> {
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.
60 bind Scrollbar <2> {
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 <Up> {
95 tk::ScrollByUnits %W v -1
97 bind Scrollbar <Down> {
98 tk::ScrollByUnits %W v 1
100 bind Scrollbar <Control-Up> {
101 tk::ScrollByPages %W v -1
103 bind Scrollbar <Control-Down> {
104 tk::ScrollByPages %W v 1
106 bind Scrollbar <Left> {
107 tk::ScrollByUnits %W h -1
109 bind Scrollbar <Right> {
110 tk::ScrollByUnits %W h 1
112 bind Scrollbar <Control-Left> {
113 tk::ScrollByPages %W h -1
115 bind Scrollbar <Control-Right> {
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 <Home> {
125 tk::ScrollToPos %W 0
127 bind Scrollbar <End> {
128 tk::ScrollToPos %W 1
131 if {[tk windowingsystem] eq "aqua"} {
132 bind Scrollbar <MouseWheel> {
133 tk::ScrollByUnits %W v [expr {- (%D)}]
135 bind Scrollbar <Option-MouseWheel> {
136 tk::ScrollByUnits %W v [expr {-10 * (%D)}]
138 bind Scrollbar <Shift-MouseWheel> {
139 tk::ScrollByUnits %W h [expr {- (%D)}]
141 bind Scrollbar <Shift-Option-MouseWheel> {
142 tk::ScrollByUnits %W h [expr {-10 * (%D)}]
145 # tk::ScrollButtonDown --
146 # This procedure is invoked when a button is pressed in a scrollbar.
147 # It changes the way the scrollbar is displayed and takes actions
148 # depending on where the mouse is.
150 # Arguments:
151 # w - The scrollbar widget.
152 # x, y - Mouse coordinates.
154 proc tk::ScrollButtonDown {w x y} {
155 variable ::tk::Priv
156 set Priv(relief) [$w cget -activerelief]
157 $w configure -activerelief sunken
158 set element [$w identify $x $y]
159 if {$element eq "slider"} {
160 ScrollStartDrag $w $x $y
161 } else {
162 ScrollSelect $w $element initial
166 # ::tk::ScrollButtonUp --
167 # This procedure is invoked when a button is released in a scrollbar.
168 # It cancels scans and auto-repeats that were in progress, and restores
169 # the way the active element is displayed.
171 # Arguments:
172 # w - The scrollbar widget.
173 # x, y - Mouse coordinates.
175 proc ::tk::ScrollButtonUp {w x y} {
176 variable ::tk::Priv
177 tk::CancelRepeat
178 if {[info exists Priv(relief)]} {
179 # Avoid error due to spurious release events
180 $w configure -activerelief $Priv(relief)
181 ScrollEndDrag $w $x $y
182 $w activate [$w identify $x $y]
186 # ::tk::ScrollSelect --
187 # This procedure is invoked when a button is pressed over the scrollbar.
188 # It invokes one of several scrolling actions depending on where in
189 # the scrollbar the button was pressed.
191 # Arguments:
192 # w - The scrollbar widget.
193 # element - The element of the scrollbar that was selected, such
194 # as "arrow1" or "trough2". Shouldn't be "slider".
195 # repeat - Whether and how to auto-repeat the action: "noRepeat"
196 # means don't auto-repeat, "initial" means this is the
197 # first action in an auto-repeat sequence, and "again"
198 # means this is the second repetition or later.
200 proc ::tk::ScrollSelect {w element repeat} {
201 variable ::tk::Priv
202 if {![winfo exists $w]} return
203 switch -- $element {
204 "arrow1" {ScrollByUnits $w hv -1}
205 "trough1" {ScrollByPages $w hv -1}
206 "trough2" {ScrollByPages $w hv 1}
207 "arrow2" {ScrollByUnits $w hv 1}
208 default {return}
210 if {$repeat eq "again"} {
211 set Priv(afterId) [after [$w cget -repeatinterval] \
212 [list tk::ScrollSelect $w $element again]]
213 } elseif {$repeat eq "initial"} {
214 set delay [$w cget -repeatdelay]
215 if {$delay > 0} {
216 set Priv(afterId) [after $delay \
217 [list tk::ScrollSelect $w $element again]]
222 # ::tk::ScrollStartDrag --
223 # This procedure is called to initiate a drag of the slider. It just
224 # remembers the starting position of the mouse and slider.
226 # Arguments:
227 # w - The scrollbar widget.
228 # x, y - The mouse position at the start of the drag operation.
230 proc ::tk::ScrollStartDrag {w x y} {
231 variable ::tk::Priv
233 if {[$w cget -command] eq ""} {
234 return
236 set Priv(pressX) $x
237 set Priv(pressY) $y
238 set Priv(initValues) [$w get]
239 set iv0 [lindex $Priv(initValues) 0]
240 if {[llength $Priv(initValues)] == 2} {
241 set Priv(initPos) $iv0
242 } elseif {$iv0 == 0} {
243 set Priv(initPos) 0.0
244 } else {
245 set Priv(initPos) [expr {(double([lindex $Priv(initValues) 2])) \
246 / [lindex $Priv(initValues) 0]}]
250 # ::tk::ScrollDrag --
251 # This procedure is called for each mouse motion even when the slider
252 # is being dragged. It notifies the associated widget if we're not
253 # jump scrolling, and it just updates the scrollbar if we are jump
254 # scrolling.
256 # Arguments:
257 # w - The scrollbar widget.
258 # x, y - The current mouse position.
260 proc ::tk::ScrollDrag {w x y} {
261 variable ::tk::Priv
263 if {$Priv(initPos) eq ""} {
264 return
266 set delta [$w delta [expr {$x - $Priv(pressX)}] [expr {$y - $Priv(pressY)}]]
267 if {[$w cget -jump]} {
268 if {[llength $Priv(initValues)] == 2} {
269 $w set [expr {[lindex $Priv(initValues) 0] + $delta}] \
270 [expr {[lindex $Priv(initValues) 1] + $delta}]
271 } else {
272 set delta [expr {round($delta * [lindex $Priv(initValues) 0])}]
273 eval [list $w] set [lreplace $Priv(initValues) 2 3 \
274 [expr {[lindex $Priv(initValues) 2] + $delta}] \
275 [expr {[lindex $Priv(initValues) 3] + $delta}]]
277 } else {
278 ScrollToPos $w [expr {$Priv(initPos) + $delta}]
282 # ::tk::ScrollEndDrag --
283 # This procedure is called to end an interactive drag of the slider.
284 # It scrolls the window if we're in jump mode, otherwise it does nothing.
286 # Arguments:
287 # w - The scrollbar widget.
288 # x, y - The mouse position at the end of the drag operation.
290 proc ::tk::ScrollEndDrag {w x y} {
291 variable ::tk::Priv
293 if {$Priv(initPos) eq ""} {
294 return
296 if {[$w cget -jump]} {
297 set delta [$w delta [expr {$x - $Priv(pressX)}] \
298 [expr {$y - $Priv(pressY)}]]
299 ScrollToPos $w [expr {$Priv(initPos) + $delta}]
301 set Priv(initPos) ""
304 # ::tk::ScrollByUnits --
305 # This procedure tells the scrollbar's associated widget to scroll up
306 # or down by a given number of units. It notifies the associated widget
307 # in different ways for old and new command syntaxes.
309 # Arguments:
310 # w - The scrollbar widget.
311 # orient - Which kinds of scrollbars this applies to: "h" for
312 # horizontal, "v" for vertical, "hv" for both.
313 # amount - How many units to scroll: typically 1 or -1.
315 proc ::tk::ScrollByUnits {w orient amount} {
316 set cmd [$w cget -command]
317 if {$cmd eq "" || ([string first \
318 [string index [$w cget -orient] 0] $orient] < 0)} {
319 return
321 set info [$w get]
322 if {[llength $info] == 2} {
323 uplevel #0 $cmd scroll $amount units
324 } else {
325 uplevel #0 $cmd [expr {[lindex $info 2] + $amount}]
329 # ::tk::ScrollByPages --
330 # This procedure tells the scrollbar's associated widget to scroll up
331 # or down by a given number of screenfuls. It notifies the associated
332 # widget in different ways for old and new command syntaxes.
334 # Arguments:
335 # w - The scrollbar widget.
336 # orient - Which kinds of scrollbars this applies to: "h" for
337 # horizontal, "v" for vertical, "hv" for both.
338 # amount - How many screens to scroll: typically 1 or -1.
340 proc ::tk::ScrollByPages {w orient amount} {
341 set cmd [$w cget -command]
342 if {$cmd eq "" || ([string first \
343 [string index [$w cget -orient] 0] $orient] < 0)} {
344 return
346 set info [$w get]
347 if {[llength $info] == 2} {
348 uplevel #0 $cmd scroll $amount pages
349 } else {
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.
359 # Arguments:
360 # w - The scrollbar widget.
361 # pos - A fraction between 0 and 1 indicating a desired position
362 # in the document.
364 proc ::tk::ScrollToPos {w pos} {
365 set cmd [$w cget -command]
366 if {$cmd eq ""} {
367 return
369 set info [$w get]
370 if {[llength $info] == 2} {
371 uplevel #0 $cmd moveto $pos
372 } else {
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
379 # position.
381 # Arguments:
382 # w - The scrollbar widget.
383 # x, y - Mouse coordinates within the widget.
385 proc ::tk::ScrollTopBottom {w x y} {
386 variable ::tk::Priv
387 set element [$w identify $x $y]
388 if {[string match *1 $element]} {
389 ScrollToPos $w 0
390 } elseif {[string match *2 $element]} {
391 ScrollToPos $w 1
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.
405 # Arguments:
406 # w - The scrollbar widget.
407 # x, y - Mouse coordinates within the widget.
409 proc ::tk::ScrollButton2Down {w x y} {
410 variable ::tk::Priv
411 set element [$w identify $x $y]
412 if {[string match {arrow[12]} $element]} {
413 ScrollButtonDown $w $x $y
414 return
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
421 # slider drag.
423 update idletasks
424 $w configure -activerelief sunken
425 $w activate slider
426 ScrollStartDrag $w $x $y