1 |
578 |
markom |
# 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 |
|
|
# SCCS: @(#) scrlbar.tcl 1.26 96/11/30 17:19:16
|
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 {($tcl_platform(platform) != "windows") &&
|
21 |
|
|
($tcl_platform(platform) != "macintosh")} {
|
22 |
|
|
bind Scrollbar <Enter> {
|
23 |
|
|
if {$tk_strictMotif} {
|
24 |
|
|
set tkPriv(activeBg) [%W cget -activebackground]
|
25 |
|
|
%W config -activebackground [%W cget -background]
|
26 |
|
|
}
|
27 |
|
|
%W activate [%W identify %x %y]
|
28 |
|
|
}
|
29 |
|
|
bind Scrollbar <Motion> {
|
30 |
|
|
%W activate [%W identify %x %y]
|
31 |
|
|
}
|
32 |
|
|
|
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
|
36 |
|
|
# unknown reasons.
|
37 |
|
|
|
38 |
|
|
bind Scrollbar <Leave> {
|
39 |
|
|
if {$tk_strictMotif && [info exists tkPriv(activeBg)]} {
|
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 {![winfo exists $w]} return
|
188 |
|
|
if {$element == "arrow1"} {
|
189 |
|
|
tkScrollByUnits $w hv -1
|
190 |
|
|
} elseif {$element == "trough1"} {
|
191 |
|
|
tkScrollByPages $w hv -1
|
192 |
|
|
} elseif {$element == "trough2"} {
|
193 |
|
|
tkScrollByPages $w hv 1
|
194 |
|
|
} elseif {$element == "arrow2"} {
|
195 |
|
|
tkScrollByUnits $w hv 1
|
196 |
|
|
} else {
|
197 |
|
|
return
|
198 |
|
|
}
|
199 |
|
|
if {$repeat == "again"} {
|
200 |
|
|
set tkPriv(afterId) [after [$w cget -repeatinterval] \
|
201 |
|
|
tkScrollSelect $w $element again]
|
202 |
|
|
} elseif {$repeat == "initial"} {
|
203 |
|
|
set delay [$w cget -repeatdelay]
|
204 |
|
|
if {$delay > 0} {
|
205 |
|
|
set tkPriv(afterId) [after $delay tkScrollSelect $w $element again]
|
206 |
|
|
}
|
207 |
|
|
}
|
208 |
|
|
}
|
209 |
|
|
|
210 |
|
|
# tkScrollStartDrag --
|
211 |
|
|
# This procedure is called to initiate a drag of the slider. It just
|
212 |
|
|
# remembers the starting position of the mouse and slider.
|
213 |
|
|
#
|
214 |
|
|
# Arguments:
|
215 |
|
|
# w - The scrollbar widget.
|
216 |
|
|
# x, y - The mouse position at the start of the drag operation.
|
217 |
|
|
|
218 |
|
|
proc tkScrollStartDrag {w x y} {
|
219 |
|
|
global tkPriv
|
220 |
|
|
|
221 |
|
|
if {[$w cget -command] == ""} {
|
222 |
|
|
return
|
223 |
|
|
}
|
224 |
|
|
set tkPriv(pressX) $x
|
225 |
|
|
set tkPriv(pressY) $y
|
226 |
|
|
set tkPriv(initValues) [$w get]
|
227 |
|
|
set iv0 [lindex $tkPriv(initValues) 0]
|
228 |
|
|
if {[llength $tkPriv(initValues)] == 2} {
|
229 |
|
|
set tkPriv(initPos) $iv0
|
230 |
|
|
} else {
|
231 |
|
|
if {$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 |
|
|
|
240 |
|
|
# tkScrollDrag --
|
241 |
|
|
# This procedure is called for each mouse motion even when the slider
|
242 |
|
|
# is being dragged. It notifies the associated widget if we're not
|
243 |
|
|
# jump scrolling, and it just updates the scrollbar if we are jump
|
244 |
|
|
# scrolling.
|
245 |
|
|
#
|
246 |
|
|
# Arguments:
|
247 |
|
|
# w - The scrollbar widget.
|
248 |
|
|
# x, y - The current mouse position.
|
249 |
|
|
|
250 |
|
|
proc tkScrollDrag {w x y} {
|
251 |
|
|
global tkPriv
|
252 |
|
|
|
253 |
|
|
if {$tkPriv(initPos) == ""} {
|
254 |
|
|
return
|
255 |
|
|
}
|
256 |
|
|
set delta [$w delta [expr {$x - $tkPriv(pressX)}] [expr {$y - $tkPriv(pressY)}]]
|
257 |
|
|
if {[$w cget -jump]} {
|
258 |
|
|
if {[llength $tkPriv(initValues)] == 2} {
|
259 |
|
|
$w set [expr {[lindex $tkPriv(initValues) 0] + $delta}] \
|
260 |
|
|
[expr {[lindex $tkPriv(initValues) 1] + $delta}]
|
261 |
|
|
} else {
|
262 |
|
|
set delta [expr {round($delta * [lindex $tkPriv(initValues) 0])}]
|
263 |
|
|
eval $w set [lreplace $tkPriv(initValues) 2 3 \
|
264 |
|
|
[expr {[lindex $tkPriv(initValues) 2] + $delta}] \
|
265 |
|
|
[expr {[lindex $tkPriv(initValues) 3] + $delta}]]
|
266 |
|
|
}
|
267 |
|
|
} else {
|
268 |
|
|
tkScrollToPos $w [expr {$tkPriv(initPos) + $delta}]
|
269 |
|
|
}
|
270 |
|
|
}
|
271 |
|
|
|
272 |
|
|
# tkScrollEndDrag --
|
273 |
|
|
# This procedure is called to end an interactive drag of the slider.
|
274 |
|
|
# It scrolls the window if we're in jump mode, otherwise it does nothing.
|
275 |
|
|
#
|
276 |
|
|
# Arguments:
|
277 |
|
|
# w - The scrollbar widget.
|
278 |
|
|
# x, y - The mouse position at the end of the drag operation.
|
279 |
|
|
|
280 |
|
|
proc tkScrollEndDrag {w x y} {
|
281 |
|
|
global tkPriv
|
282 |
|
|
|
283 |
|
|
if {$tkPriv(initPos) == ""} {
|
284 |
|
|
return
|
285 |
|
|
}
|
286 |
|
|
if {[$w cget -jump]} {
|
287 |
|
|
set delta [$w delta [expr {$x - $tkPriv(pressX)}] \
|
288 |
|
|
[expr {$y - $tkPriv(pressY)}]]
|
289 |
|
|
tkScrollToPos $w [expr {$tkPriv(initPos) + $delta}]
|
290 |
|
|
}
|
291 |
|
|
set tkPriv(initPos) ""
|
292 |
|
|
}
|
293 |
|
|
|
294 |
|
|
# tkScrollByUnits --
|
295 |
|
|
# This procedure tells the scrollbar's associated widget to scroll up
|
296 |
|
|
# or down by a given number of units. It notifies the associated widget
|
297 |
|
|
# in different ways for old and new command syntaxes.
|
298 |
|
|
#
|
299 |
|
|
# Arguments:
|
300 |
|
|
# w - The scrollbar widget.
|
301 |
|
|
# orient - Which kinds of scrollbars this applies to: "h" for
|
302 |
|
|
# horizontal, "v" for vertical, "hv" for both.
|
303 |
|
|
# amount - How many units to scroll: typically 1 or -1.
|
304 |
|
|
|
305 |
|
|
proc tkScrollByUnits {w orient amount} {
|
306 |
|
|
set cmd [$w cget -command]
|
307 |
|
|
if {($cmd == "") || ([string first \
|
308 |
|
|
[string index [$w cget -orient] 0] $orient] < 0)} {
|
309 |
|
|
return
|
310 |
|
|
}
|
311 |
|
|
set info [$w get]
|
312 |
|
|
if {[llength $info] == 2} {
|
313 |
|
|
uplevel #0 $cmd scroll $amount units
|
314 |
|
|
} else {
|
315 |
|
|
uplevel #0 $cmd [expr [lindex $info 2] + $amount]
|
316 |
|
|
}
|
317 |
|
|
}
|
318 |
|
|
|
319 |
|
|
# tkScrollByPages --
|
320 |
|
|
# This procedure tells the scrollbar's associated widget to scroll up
|
321 |
|
|
# or down by a given number of screenfuls. It notifies the associated
|
322 |
|
|
# widget in different ways for old and new command syntaxes.
|
323 |
|
|
#
|
324 |
|
|
# Arguments:
|
325 |
|
|
# w - The scrollbar widget.
|
326 |
|
|
# orient - Which kinds of scrollbars this applies to: "h" for
|
327 |
|
|
# horizontal, "v" for vertical, "hv" for both.
|
328 |
|
|
# amount - How many screens to scroll: typically 1 or -1.
|
329 |
|
|
|
330 |
|
|
proc tkScrollByPages {w orient amount} {
|
331 |
|
|
set cmd [$w cget -command]
|
332 |
|
|
if {($cmd == "") || ([string first \
|
333 |
|
|
[string index [$w cget -orient] 0] $orient] < 0)} {
|
334 |
|
|
return
|
335 |
|
|
}
|
336 |
|
|
set info [$w get]
|
337 |
|
|
if {[llength $info] == 2} {
|
338 |
|
|
uplevel #0 $cmd scroll $amount pages
|
339 |
|
|
} else {
|
340 |
|
|
uplevel #0 $cmd [expr [lindex $info 2] + $amount*([lindex $info 1] - 1)]
|
341 |
|
|
}
|
342 |
|
|
}
|
343 |
|
|
|
344 |
|
|
# tkScrollToPos --
|
345 |
|
|
# This procedure tells the scrollbar's associated widget to scroll to
|
346 |
|
|
# a particular location, given by a fraction between 0 and 1. It notifies
|
347 |
|
|
# the associated widget in different ways for old and new command syntaxes.
|
348 |
|
|
#
|
349 |
|
|
# Arguments:
|
350 |
|
|
# w - The scrollbar widget.
|
351 |
|
|
# pos - A fraction between 0 and 1 indicating a desired position
|
352 |
|
|
# in the document.
|
353 |
|
|
|
354 |
|
|
proc tkScrollToPos {w pos} {
|
355 |
|
|
set cmd [$w cget -command]
|
356 |
|
|
if {($cmd == "")} {
|
357 |
|
|
return
|
358 |
|
|
}
|
359 |
|
|
set info [$w get]
|
360 |
|
|
if {[llength $info] == 2} {
|
361 |
|
|
uplevel #0 $cmd moveto $pos
|
362 |
|
|
} else {
|
363 |
|
|
uplevel #0 $cmd [expr round([lindex $info 0]*$pos)]
|
364 |
|
|
}
|
365 |
|
|
}
|
366 |
|
|
|
367 |
|
|
# tkScrollTopBottom
|
368 |
|
|
# Scroll to the top or bottom of the document, depending on the mouse
|
369 |
|
|
# position.
|
370 |
|
|
#
|
371 |
|
|
# Arguments:
|
372 |
|
|
# w - The scrollbar widget.
|
373 |
|
|
# x, y - Mouse coordinates within the widget.
|
374 |
|
|
|
375 |
|
|
proc tkScrollTopBottom {w x y} {
|
376 |
|
|
global tkPriv
|
377 |
|
|
set element [$w identify $x $y]
|
378 |
|
|
if {[string match *1 $element]} {
|
379 |
|
|
tkScrollToPos $w 0
|
380 |
|
|
} elseif {[string match *2 $element]} {
|
381 |
|
|
tkScrollToPos $w 1
|
382 |
|
|
}
|
383 |
|
|
|
384 |
|
|
# Set tkPriv(relief), since it's needed by tkScrollButtonUp.
|
385 |
|
|
|
386 |
|
|
set tkPriv(relief) [$w cget -activerelief]
|
387 |
|
|
}
|
388 |
|
|
|
389 |
|
|
# tkScrollButton2Down
|
390 |
|
|
# This procedure is invoked when button 2 is pressed over a scrollbar.
|
391 |
|
|
# If the button is over the trough or slider, it sets the scrollbar to
|
392 |
|
|
# the mouse position and starts a slider drag. Otherwise it just
|
393 |
|
|
# behaves the same as button 1.
|
394 |
|
|
#
|
395 |
|
|
# Arguments:
|
396 |
|
|
# w - The scrollbar widget.
|
397 |
|
|
# x, y - Mouse coordinates within the widget.
|
398 |
|
|
|
399 |
|
|
proc tkScrollButton2Down {w x y} {
|
400 |
|
|
global tkPriv
|
401 |
|
|
set element [$w identify $x $y]
|
402 |
|
|
if {($element == "arrow1") || ($element == "arrow2")} {
|
403 |
|
|
tkScrollButtonDown $w $x $y
|
404 |
|
|
return
|
405 |
|
|
}
|
406 |
|
|
tkScrollToPos $w [$w fraction $x $y]
|
407 |
|
|
set tkPriv(relief) [$w cget -activerelief]
|
408 |
|
|
|
409 |
|
|
# Need the "update idletasks" below so that the widget calls us
|
410 |
|
|
# back to reset the actual scrollbar position before we start the
|
411 |
|
|
# slider drag.
|
412 |
|
|
|
413 |
|
|
update idletasks
|
414 |
|
|
$w configure -activerelief sunken
|
415 |
|
|
$w activate slider
|
416 |
|
|
tkScrollStartDrag $w $x $y
|
417 |
|
|
}
|