1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
|
# scrlbar.tcl --
#
# This file defines the default bindings for Tk scrollbar widgets.
# It also provides procedures that help in implementing the bindings.
#
# RCS: @(#) $Id: scrlbar.tcl,v 1.1.4.4 1999/04/06 03:52:58 stanton Exp $
#
# Copyright (c) 1994 The Regents of the University of California.
# Copyright (c) 1994-1996 Sun Microsystems, Inc.
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
#-------------------------------------------------------------------------
# The code below creates the default class bindings for scrollbars.
#-------------------------------------------------------------------------
# Standard Motif bindings:
if {[string compare $tcl_platform(platform) "windows"] &&
[string compare $tcl_platform(platform) "macintosh"]} {
bind Scrollbar <Enter> {
if {$tk_strictMotif} {
set tkPriv(activeBg) [%W cget -activebackground]
%W config -activebackground [%W cget -background]
}
%W activate [%W identify %x %y]
}
bind Scrollbar <Motion> {
%W activate [%W identify %x %y]
}
# The "info exists" command in the following binding handles the
# situation where a Leave event occurs for a scrollbar without the Enter
# event. This seems to happen on some systems (such as Solaris 2.4) for
# unknown reasons.
bind Scrollbar <Leave> {
if {$tk_strictMotif && [info exists tkPriv(activeBg)]} {
%W config -activebackground $tkPriv(activeBg)
}
%W activate {}
}
bind Scrollbar <1> {
tkScrollButtonDown %W %x %y
}
bind Scrollbar <B1-Motion> {
tkScrollDrag %W %x %y
}
bind Scrollbar <B1-B2-Motion> {
tkScrollDrag %W %x %y
}
bind Scrollbar <ButtonRelease-1> {
tkScrollButtonUp %W %x %y
}
bind Scrollbar <B1-Leave> {
# Prevents <Leave> binding from being invoked.
}
bind Scrollbar <B1-Enter> {
# Prevents <Enter> binding from being invoked.
}
bind Scrollbar <2> {
tkScrollButton2Down %W %x %y
}
bind Scrollbar <B1-2> {
# Do nothing, since button 1 is already down.
}
bind Scrollbar <B2-1> {
# Do nothing, since button 2 is already down.
}
bind Scrollbar <B2-Motion> {
tkScrollDrag %W %x %y
}
bind Scrollbar <ButtonRelease-2> {
tkScrollButtonUp %W %x %y
}
bind Scrollbar <B1-ButtonRelease-2> {
# Do nothing: B1 release will handle it.
}
bind Scrollbar <B2-ButtonRelease-1> {
# Do nothing: B2 release will handle it.
}
bind Scrollbar <B2-Leave> {
# Prevents <Leave> binding from being invoked.
}
bind Scrollbar <B2-Enter> {
# Prevents <Enter> binding from being invoked.
}
bind Scrollbar <Control-1> {
tkScrollTopBottom %W %x %y
}
bind Scrollbar <Control-2> {
tkScrollTopBottom %W %x %y
}
bind Scrollbar <Up> {
tkScrollByUnits %W v -1
}
bind Scrollbar <Down> {
tkScrollByUnits %W v 1
}
bind Scrollbar <Control-Up> {
tkScrollByPages %W v -1
}
bind Scrollbar <Control-Down> {
tkScrollByPages %W v 1
}
bind Scrollbar <Left> {
tkScrollByUnits %W h -1
}
bind Scrollbar <Right> {
tkScrollByUnits %W h 1
}
bind Scrollbar <Control-Left> {
tkScrollByPages %W h -1
}
bind Scrollbar <Control-Right> {
tkScrollByPages %W h 1
}
bind Scrollbar <Prior> {
tkScrollByPages %W hv -1
}
bind Scrollbar <Next> {
tkScrollByPages %W hv 1
}
bind Scrollbar <Home> {
tkScrollToPos %W 0
}
bind Scrollbar <End> {
tkScrollToPos %W 1
}
}
# tkScrollButtonDown --
# This procedure is invoked when a button is pressed in a scrollbar.
# It changes the way the scrollbar is displayed and takes actions
# depending on where the mouse is.
#
# Arguments:
# w - The scrollbar widget.
# x, y - Mouse coordinates.
proc tkScrollButtonDown {w x y} {
global tkPriv
set tkPriv(relief) [$w cget -activerelief]
$w configure -activerelief sunken
set element [$w identify $x $y]
if {![string compare $element "slider"]} {
tkScrollStartDrag $w $x $y
} else {
tkScrollSelect $w $element initial
}
}
# tkScrollButtonUp --
# This procedure is invoked when a button is released in a scrollbar.
# It cancels scans and auto-repeats that were in progress, and restores
# the way the active element is displayed.
#
# Arguments:
# w - The scrollbar widget.
# x, y - Mouse coordinates.
proc tkScrollButtonUp {w x y} {
global tkPriv
tkCancelRepeat
$w configure -activerelief $tkPriv(relief)
tkScrollEndDrag $w $x $y
$w activate [$w identify $x $y]
}
# tkScrollSelect --
# This procedure is invoked when a button is pressed over the scrollbar.
# It invokes one of several scrolling actions depending on where in
# the scrollbar the button was pressed.
#
# Arguments:
# w - The scrollbar widget.
# element - The element of the scrollbar that was selected, such
# as "arrow1" or "trough2". Shouldn't be "slider".
# repeat - Whether and how to auto-repeat the action: "noRepeat"
# means don't auto-repeat, "initial" means this is the
# first action in an auto-repeat sequence, and "again"
# means this is the second repetition or later.
proc tkScrollSelect {w element repeat} {
global tkPriv
if {![winfo exists $w]} return
switch -- $element {
"arrow1" {tkScrollByUnits $w hv -1}
"trough1" {tkScrollByPages $w hv -1}
"trough2" {tkScrollByPages $w hv 1}
"arrow2" {tkScrollByUnits $w hv 1}
default {return}
}
if {![string compare $repeat "again"]} {
set tkPriv(afterId) [after [$w cget -repeatinterval] \
tkScrollSelect $w $element again]
} elseif {![string compare $repeat "initial"]} {
set delay [$w cget -repeatdelay]
if {$delay > 0} {
set tkPriv(afterId) [after $delay tkScrollSelect $w $element again]
}
}
}
# tkScrollStartDrag --
# This procedure is called to initiate a drag of the slider. It just
# remembers the starting position of the mouse and slider.
#
# Arguments:
# w - The scrollbar widget.
# x, y - The mouse position at the start of the drag operation.
proc tkScrollStartDrag {w x y} {
global tkPriv
if {![string compare [$w cget -command] ""]} {
return
}
set tkPriv(pressX) $x
set tkPriv(pressY) $y
set tkPriv(initValues) [$w get]
set iv0 [lindex $tkPriv(initValues) 0]
if {[llength $tkPriv(initValues)] == 2} {
set tkPriv(initPos) $iv0
} else {
if {$iv0 == 0} {
set tkPriv(initPos) 0.0
} else {
set tkPriv(initPos) [expr {(double([lindex $tkPriv(initValues) 2])) \
/ [lindex $tkPriv(initValues) 0]}]
}
}
}
# tkScrollDrag --
# This procedure is called for each mouse motion even when the slider
# is being dragged. It notifies the associated widget if we're not
# jump scrolling, and it just updates the scrollbar if we are jump
# scrolling.
#
# Arguments:
# w - The scrollbar widget.
# x, y - The current mouse position.
proc tkScrollDrag {w x y} {
global tkPriv
if {![string compare $tkPriv(initPos) ""]} {
return
}
set delta [$w delta [expr {$x - $tkPriv(pressX)}] [expr {$y - $tkPriv(pressY)}]]
if {[$w cget -jump]} {
if {[llength $tkPriv(initValues)] == 2} {
$w set [expr {[lindex $tkPriv(initValues) 0] + $delta}] \
[expr {[lindex $tkPriv(initValues) 1] + $delta}]
} else {
set delta [expr {round($delta * [lindex $tkPriv(initValues) 0])}]
eval $w set [lreplace $tkPriv(initValues) 2 3 \
[expr {[lindex $tkPriv(initValues) 2] + $delta}] \
[expr {[lindex $tkPriv(initValues) 3] + $delta}]]
}
} else {
tkScrollToPos $w [expr {$tkPriv(initPos) + $delta}]
}
}
# tkScrollEndDrag --
# This procedure is called to end an interactive drag of the slider.
# It scrolls the window if we're in jump mode, otherwise it does nothing.
#
# Arguments:
# w - The scrollbar widget.
# x, y - The mouse position at the end of the drag operation.
proc tkScrollEndDrag {w x y} {
global tkPriv
if {![string compare $tkPriv(initPos) ""]} {
return
}
if {[$w cget -jump]} {
set delta [$w delta [expr {$x - $tkPriv(pressX)}] \
[expr {$y - $tkPriv(pressY)}]]
tkScrollToPos $w [expr {$tkPriv(initPos) + $delta}]
}
set tkPriv(initPos) ""
}
# tkScrollByUnits --
# This procedure tells the scrollbar's associated widget to scroll up
# or down by a given number of units. It notifies the associated widget
# in different ways for old and new command syntaxes.
#
# Arguments:
# w - The scrollbar widget.
# orient - Which kinds of scrollbars this applies to: "h" for
# horizontal, "v" for vertical, "hv" for both.
# amount - How many units to scroll: typically 1 or -1.
proc tkScrollByUnits {w orient amount} {
set cmd [$w cget -command]
if {![string compare $cmd ""] || ([string first \
[string index [$w cget -orient] 0] $orient] < 0)} {
return
}
set info [$w get]
if {[llength $info] == 2} {
uplevel #0 $cmd scroll $amount units
} else {
uplevel #0 $cmd [expr {[lindex $info 2] + $amount}]
}
}
# tkScrollByPages --
# This procedure tells the scrollbar's associated widget to scroll up
# or down by a given number of screenfuls. It notifies the associated
# widget in different ways for old and new command syntaxes.
#
# Arguments:
# w - The scrollbar widget.
# orient - Which kinds of scrollbars this applies to: "h" for
# horizontal, "v" for vertical, "hv" for both.
# amount - How many screens to scroll: typically 1 or -1.
proc tkScrollByPages {w orient amount} {
set cmd [$w cget -command]
if {![string compare $cmd ""] || ([string first \
[string index [$w cget -orient] 0] $orient] < 0)} {
return
}
set info [$w get]
if {[llength $info] == 2} {
uplevel #0 $cmd scroll $amount pages
} else {
uplevel #0 $cmd [expr {[lindex $info 2] + $amount*([lindex $info 1] - 1)}]
}
}
# tkScrollToPos --
# This procedure tells the scrollbar's associated widget to scroll to
# a particular location, given by a fraction between 0 and 1. It notifies
# the associated widget in different ways for old and new command syntaxes.
#
# Arguments:
# w - The scrollbar widget.
# pos - A fraction between 0 and 1 indicating a desired position
# in the document.
proc tkScrollToPos {w pos} {
set cmd [$w cget -command]
if {![string compare $cmd ""]} {
return
}
set info [$w get]
if {[llength $info] == 2} {
uplevel #0 $cmd moveto $pos
} else {
uplevel #0 $cmd [expr {round([lindex $info 0]*$pos)}]
}
}
# tkScrollTopBottom
# Scroll to the top or bottom of the document, depending on the mouse
# position.
#
# Arguments:
# w - The scrollbar widget.
# x, y - Mouse coordinates within the widget.
proc tkScrollTopBottom {w x y} {
global tkPriv
set element [$w identify $x $y]
if {[string match *1 $element]} {
tkScrollToPos $w 0
} elseif {[string match *2 $element]} {
tkScrollToPos $w 1
}
# Set tkPriv(relief), since it's needed by tkScrollButtonUp.
set tkPriv(relief) [$w cget -activerelief]
}
# tkScrollButton2Down
# This procedure is invoked when button 2 is pressed over a scrollbar.
# If the button is over the trough or slider, it sets the scrollbar to
# the mouse position and starts a slider drag. Otherwise it just
# behaves the same as button 1.
#
# Arguments:
# w - The scrollbar widget.
# x, y - Mouse coordinates within the widget.
proc tkScrollButton2Down {w x y} {
global tkPriv
set element [$w identify $x $y]
if {![string compare $element "arrow1"]
|| ![string compare $element "arrow2"]} {
tkScrollButtonDown $w $x $y
return
}
tkScrollToPos $w [$w fraction $x $y]
set tkPriv(relief) [$w cget -activerelief]
# Need the "update idletasks" below so that the widget calls us
# back to reset the actual scrollbar position before we start the
# slider drag.
update idletasks
$w configure -activerelief sunken
$w activate slider
tkScrollStartDrag $w $x $y
}
|