blib/lib/Tk/DynaMouseWheelBind.pm untested

filter: Statementstmt 0.0% 0 / 55 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 80 CC 1 SCAR 6.9
CC
1
Cov
0%
DynaMouseWheelBind
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package Tk::DynaMouseWheelBind;
2  
3 =head1 NAME
4  
5 Tk::DynaMouseWheelBind - Wheel scroll anything that scrolls
6  
7 =cut
8  
9 require Tk::Widget;
10 package # hide from PAUSE indexer
11 Tk::Widget;
12  
13 use strict;
14 use warnings;
15 use vars qw( $VERSION );
16 $VERSION = 0.08;
17  
18 # keep Tk::Widgets namespace clean
19 my ($motion, $do_scroll, $mousewheel_event, $setup );
20  
21 sub DynaMouseWheelBind{
22 my $w = shift;
23 my @classes = @_;
24 my $mw = $w->MainWindow;
25 $setup->($mw);
26 for my $class (@classes) {
27 eval "require $class" or die $@;
28 # initialize class bindings so the following changes
29 # won't get overridden
30 $class->InitClass($mw);
31 # replace MouseWheel bindings - these should be processed
32 # through the $mw binding only
33 my @mw_events = ('<MouseWheel>', '<4>', '<5>');
34 $mw->bind($class,$_,'') for (@mw_events);
35 $mw->bind($class,'<<DynaMouseWheel>>',$do_scroll);
36 }
37 }
38  
39 # setup two bindings to track the window under the cursor
40 # and globally receive <MouseWheel>
41  
42 $setup = sub{
43 my $mw = shift;
44 $mw->bind('all','<Enter>',$motion);
45 $mw->bind('all','<MouseWheel>',[$mousewheel_event, Tk::Ev('D')]);
46 $mw->bind('all','<4>',[$mousewheel_event, 120]);
47 $mw->bind('all','<5>',[$mousewheel_event, -120]);
48 };
49  
50 {
51 my $under_cursor ;
52 my $scrollable;
53 my $delta;
54  
55 $motion = sub {
56 my $w = shift;
57 $under_cursor = $w->XEvent->Info('W') if defined $w;
58 };
59  
60 $do_scroll = sub{
61 $scrollable->yview('scroll', -($delta/120)*3, 'units');
62 };
63  
64 $mousewheel_event = sub{
65 my $widget = shift;
66 $delta = shift;
67 # just in case, the mouse has not been moved yet:
68 my $w = $under_cursor ||= $widget;
69 my $has_binding;
70 until ($has_binding || $w->isa('Tk::Toplevel')){
71 if($w->Tk::bind(ref($w),'<<DynaMouseWheel>>')){
72 $has_binding = 1 ;
73 }else{
74 $w = $w->parent;
75 }
76 }
77 if ($has_binding) {
78 $scrollable = $w;
79 $w->eventGenerate('<<DynaMouseWheel>>');
80 }
81 };
82 } # end of scope for $under_cursor, $scrollable, $delta
83  
84 =head1 SYNOPSIS
85  
86 require Tk::DynaMouseWheelBind;
87 $mw->DynaMouseWheelBind('Tk::Canvas', 'Tk::Pane')
88
89 =head1 DESCRIPTION
90  
91 This module applies mouse wheel events to Canvases, Panes and any other widget that has scroll capabilities.
92  
93 =head1 LICENSE
94  
95 Same as Perl.
96  
97 =head1 AUTHOR
98  
99 Hans Jeuken (hanje at cpan dot org)
100  
101 =head1 TODO
102  
103 =over 4
104  
105 =back
106  
107 =head1 BUGS AND CAVEATS
108  
109 If you find any bugs, please contact the author.
110  
111 =head1 SEE ALSO
112  
113 =over 4
114  
115 =back
116  
117 =cut
118  
119 1;