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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
|
# Conversion from Tk4.0 scrollbar.tcl competed.
package Tk::Scrollbar;
use vars qw($VERSION);
$VERSION = '4.010'; # $Id: //depot/Tkutf8/Scrollbar/Scrollbar.pm#10 $
use Tk qw($XS_VERSION Ev);
use AutoLoader;
use base qw(Tk::Widget);
#use strict;
#use vars qw($pressX $pressY @initValues $initPos $activeBg);
Construct Tk::Widget 'Scrollbar';
bootstrap Tk::Scrollbar;
sub Tk_cmd { \&Tk::scrollbar }
Tk::Methods('activate','delta','fraction','get','identify','set');
sub Needed
{
my ($sb) = @_;
my @val = $sb->get;
return 1 unless (@val == 2);
return 1 if $val[0] != 0.0;
return 1 if $val[1] != 1.0;
return 0;
}
sub ClassInit
{
my ($class,$mw) = @_;
$mw->bind($class, '<Enter>', 'Enter');
$mw->bind($class, '<Motion>', 'Motion');
$mw->bind($class, '<Leave>', 'Leave');
$mw->bind($class, '<1>', 'ButtonDown');
$mw->bind($class, '<B1-Motion>', ['Drag', Ev('x'), Ev('y')]);
$mw->bind($class, '<ButtonRelease-1>', 'ButtonUp');
$mw->bind($class, '<B1-Leave>', 'NoOp'); # prevent generic <Leave>
$mw->bind($class, '<B1-Enter>', 'NoOp'); # prevent generic <Enter>
$mw->bind($class, '<Control-1>', 'ScrlTopBottom');
$mw->bind($class, '<2>', 'ButtonDown');
$mw->bind($class, '<B2-Motion>', ['Drag', Ev('x'), Ev('y')]);
$mw->bind($class, '<ButtonRelease-2>', 'ButtonUp');
$mw->bind($class, '<B2-Leave>', 'NoOp'); # prevent generic <Leave>
$mw->bind($class, '<B2-Enter>', 'NoOp'); # prevent generic <Enter>
$mw->bind($class, '<Control-2>', 'ScrlTopBottom');
$mw->bind($class, '<Up>', ['ScrlByUnits','v',-1]);
$mw->bind($class, '<Down>', ['ScrlByUnits','v', 1]);
$mw->bind($class, '<Control-Up>', ['ScrlByPages','v',-1]);
$mw->bind($class, '<Control-Down>', ['ScrlByPages','v', 1]);
$mw->bind($class, '<Left>', ['ScrlByUnits','h',-1]);
$mw->bind($class, '<Right>', ['ScrlByUnits','h', 1]);
$mw->bind($class, '<Control-Left>', ['ScrlByPages','h',-1]);
$mw->bind($class, '<Control-Right>', ['ScrlByPages','h', 1]);
$mw->bind($class, '<Prior>', ['ScrlByPages','hv',-1]);
$mw->bind($class, '<Next>', ['ScrlByPages','hv', 1]);
# X11 mousewheel - honour for horizontal too.
$mw->bind($class, '<4>', ['ScrlByUnits','hv',-5]);
$mw->bind($class, '<5>', ['ScrlByUnits','hv', 5]);
$mw->bind($class, '<Home>', ['ScrlToPos', 0]);
$mw->bind($class, '<End>', ['ScrlToPos', 1]);
$mw->bind($class, '<4>', ['ScrlByUnits','v',-3]);
$mw->bind($class, '<5>', ['ScrlByUnits','v', 3]);
return $class;
}
1;
__END__
sub Enter
{
my $w = shift;
my $e = $w->XEvent;
if ($Tk::strictMotif)
{
my $bg = $w->cget('-background');
$activeBg = $w->cget('-activebackground');
$w->configure('-activebackground' => $bg);
}
$w->activate($w->identify($e->x,$e->y));
}
sub Leave
{
my $w = shift;
if ($Tk::strictMotif)
{
$w->configure('-activebackground' => $activeBg) if (defined $activeBg) ;
}
$w->activate('');
}
sub Motion
{
my $w = shift;
my $e = $w->XEvent;
$w->activate($w->identify($e->x,$e->y));
}
# 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.
sub ButtonDown
{my $w = shift;
my $e = $w->XEvent;
my $element = $w->identify($e->x,$e->y);
$w->configure('-activerelief' => 'sunken');
if ($e->b == 1 and
(defined($element) && $element eq 'slider'))
{
$w->StartDrag($e->x,$e->y);
}
elsif ($e->b == 2 and
(defined($element) && $element =~ /^(trough[12]|slider)$/o))
{
my $pos = $w->fraction($e->x, $e->y);
my($head, $tail) = $w->get;
my $len = $tail - $head;
$head = $pos - $len/2;
$tail = $pos + $len/2;
if ($head < 0) {
$head = 0;
$tail = $len;
}
elsif ($tail > 1) {
$head = 1 - $len;
$tail = 1;
}
$w->ScrlToPos($head);
$w->set($head, $tail);
$w->StartDrag($e->x,$e->y);
}
else
{
$w->Select($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.
sub ButtonUp
{my $w = shift;
my $e = $w->XEvent;
$w->CancelRepeat;
$w->configure('-activerelief' => 'raised');
$w->EndDrag($e->x,$e->y);
$w->activate($w->identify($e->x,$e->y));
}
# tkScrollSelect --
# This procedure is invoked when button 1 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.
sub Select
{
my $w = shift;
my $element = shift;
my $repeat = shift;
return unless defined ($element);
if ($element eq 'arrow1')
{
$w->ScrlByUnits('hv',-1);
}
elsif ($element eq 'trough1')
{
$w->ScrlByPages('hv',-1);
}
elsif ($element eq 'trough2')
{
$w->ScrlByPages('hv', 1);
}
elsif ($element eq 'arrow2')
{
$w->ScrlByUnits('hv', 1);
}
else
{
return;
}
if ($repeat eq 'again')
{
$w->RepeatId($w->after($w->cget('-repeatinterval'),['Select',$w,$element,'again']));
}
elsif ($repeat eq 'initial')
{
$w->RepeatId($w->after($w->cget('-repeatdelay'),['Select',$w,$element,'again']));
}
}
# tkScrollStartDrag --
# This procedure is called to initiate a drag of the slider. It just
# remembers the starting position of the slider.
#
# Arguments:
# w - The scrollbar widget.
# x, y - The mouse position at the start of the drag operation.
sub StartDrag
{
my($w,$x,$y) = @_;
return unless (defined ($w->cget('-command')));
$pressX = $x;
$pressY = $y;
@initValues = $w->get;
my $iv0 = $initValues[0];
if (@initValues == 2)
{
$initPos = $iv0;
}
elsif ($iv0 == 0)
{
$initPos = 0;
}
else
{
$initPos = $initValues[2]/$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.
sub Drag
{
my($w,$x,$y) = @_;
return if !defined $initPos;
my $delta = $w->delta($x-$pressX, $y-$pressY);
if ($w->cget('-jump'))
{
if (@initValues == 2)
{
$w->set($initValues[0]+$delta, $initValues[1]+$delta);
}
else
{
$delta = sprintf "%d", $delta * $initValues[0]; # round()
$initValues[2] += $delta;
$initValues[3] += $delta;
$w->set(@initValues[2,3]);
}
}
else
{
$w->ScrlToPos($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.
sub EndDrag
{
my($w,$x,$y) = @_;
return if (!defined $initPos);
if ($w->cget('-jump'))
{
my $delta = $w->delta($x-$pressX, $y-$pressY);
$w->ScrlToPos($initPos+$delta);
}
undef $initPos;
}
# tkScrlByUnits --
# 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.
sub ScrlByUnits
{my $w = shift;
my $orient = shift;
my $amount = shift;
my $cmd = $w->cget('-command');
return unless (defined $cmd);
return if (index($orient,substr($w->cget('-orient'),0,1)) < 0);
my @info = $w->get;
if (@info == 2)
{
$cmd->Call('scroll',$amount,'units');
}
else
{
$cmd->Call($info[2]+$amount);
}
}
# tkScrlByPages --
# 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.
sub ScrlByPages
{
my $w = shift;
my $orient = shift;
my $amount = shift;
my $cmd = $w->cget('-command');
return unless (defined $cmd);
return if (index($orient,substr($w->cget('-orient'),0,1)) < 0);
my @info = $w->get;
if (@info == 2)
{
$cmd->Call('scroll',$amount,'pages');
}
else
{
$cmd->Call($info[2]+$amount*($info[1]-1));
}
}
# tkScrlToPos --
# 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.
sub ScrlToPos
{
my $w = shift;
my $pos = shift;
my $cmd = $w->cget('-command');
return unless (defined $cmd);
my @info = $w->get;
if (@info == 2)
{
$cmd->Call('moveto',$pos);
}
else
{
$cmd->Call(int($info[0]*$pos));
}
}
# tkScrlTopBottom
# 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.
sub ScrlTopBottom
{
my $w = shift;
my $e = $w->XEvent;
my $element = $w->identify($e->x,$e->y);
return unless ($element);
if ($element =~ /1$/)
{
$w->ScrlToPos(0);
}
elsif ($element =~ /2$/)
{
$w->ScrlToPos(1);
}
}
|