App/Chart/Gtk2/Ex/BindingBits.pm untested

filter: Statementstmt 0.0% 0 / 29 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 8 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 49 CC 3 SCAR 16.7
CC
3
Cov
0%
activate_button_event
12.0
BEGIN
2.0
BEGIN
2.0
CRAP 5.3
1 # binding set replaced by an rc parse_string in program,
2 #
3 # lookup through widget bindings won't take Pointer_Button1 etc
4  
5  
6  
7 # Copyright 2009, 2010, 2011 Kevin Ryde
8  
9 # This file is part of Chart.
10 #
11 # Chart is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by the
13 # Free Software Foundation; either version 3, or (at your option) any later
14 # version.
15 #
16 # Chart is distributed in the hope that it will be useful, but
17 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 # for more details.
20 #
21 # You should have received a copy of the GNU General Public License along
22 # with Chart. If not, see <http://www.gnu.org/licenses/>.
23  
24 package App::Chart::Gtk2::Ex::BindingBits;
25 use 5.008;
26 use strict;
27 use warnings;
28 use Carp;
29 use Gtk2;
30 use Scalar::Util;
31  
32 # uncomment this to run the ### lines
33 #use Smart::Comments;
34  
35  
36 # Think it's better to croak for an unknown bindingset name than to return 0
37 # for not activated, on the basis it picks up typos or bad names.
38 #
39 sub activate_button_event {
40 my ($bindings, $event, $object) = @_;
41  
42 unless (Scalar::Util::blessed($bindings)
43 && $bindings->isa('Gtk2::BindingSet')) {
44 $bindings = Gtk2::BindingSet->find ($bindings)
45 || croak 'No BindingSet called ',$bindings;
46 }
47 my $keyval = Gtk2::Gdk->keyval_from_name('Pointer_Button' . $event->button)
48 || do {
49 ### activate_button_event() unknown: 'Pointer_Button' . $event->button
50 return 0;
51 };
52  
53 my $modifiers = $event->get_state;
54 if ($event->type eq 'button-release') { $modifiers |= 'release-mask'; }
55 ### type : $event->type
56 ### modifiers: "$modifiers"
57  
58 my $found = $bindings->activate ($keyval, $modifiers, $object);
59 ### activate_button() found: $found
60 return $found;
61 }
62  
63 1;
64 __END__
65  
66 =for stopwords Ryde Chart
67  
68 =head1 NAME
69  
70 App::Chart::Gtk2::Ex::BindingBits -- helpers for Gtk2::BindingSet etc
71  
72 =head1 SYNOPSIS
73  
74 use App::Chart::Gtk2::Ex::BindingBits;
75  
76 =head1 FUNCTIONS
77  
78 =over 4
79  
80 =item C<< $bool = App::Chart::Gtk2::Ex::BindingBits::activate_button_event ($bindings, $event, $object) >>
81  
82 Activate bindings for a button press C<$event>. The return is true if a
83 binding was found and activated or false if not.
84  
85 C<$event> should be a C<Gtk2::Gdk::Event::Button>. The button number is
86 turned into keyval C<Pointer_Button1> etc, modifiers get C<release-mask>
87 added if it's a C<button-release> event, then simply
88  
89 $bindings->activate ($keyval, $modifiers, $object)
90  
91 C<$bindings> can be either a C<Gtk2::BindingSet> object or a string name of
92 a binding set for C<< Gtk2::BindingSet->find >>. As of Gtk 2.18 there's
93 keyvals for buttons 1 through 5. If C<< $event->button >> is somehow
94 another value then C<activate_button_event> returns 0 as there cannot be any
95 bindings for it.
96  
97 See F<examples/binding-button-event.pl> in the sources for a sample program.
98  
99 =back
100  
101 =head1 OTHER NOTES
102  
103 For reference, as of Gtk 2.18 the C<Gtk2::Object> method
104 C<< $object->bindings_activate >> is no good for C<Pointer_Button1> etc as
105 that C<bindings_activate> trys to go from keyval to hardware keycode then
106 back to keyvals, but there's normally no keycode for the pointer buttons, so
107 it doesn't lookup anything in the BindingSet.
108  
109 =head1 SEE ALSO
110  
111 L<Gtk2::BindingSet>, L<Gtk2::Ex::WidgetBits>, L<Gtk2::Gdk::Event::Button>
112  
113 =head1 HOME PAGE
114  
115 L<http://user42.tuxfamily.org/chart/index.html>
116  
117 =head1 LICENSE
118  
119 Copyright 2009, 2010 Kevin Ryde
120  
121 Chart is free software; you can redistribute it and/or modify
122 it under the terms of the GNU General Public License as published by the
123 Free Software Foundation; either version 3, or (at your option) any later
124 version.
125  
126 Chart is distributed in the hope that it will be useful, but
127 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
128 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
129 more details.
130  
131 You should have received a copy of the GNU General Public License along with
132 Chart. If not, see L<http://www.gnu.org/licenses/>.
133  
134 =cut
135  
136 # junk, probably
137 #
138 # sub activate_event {
139 # my ($bindings, $event, $object) = @_;
140 # unless (Scalar::Util::blessed($bindings)
141 # && $bindings->isa('Gtk2::BindingSet')) {
142 # $bindings = Gtk2::BindingSet->find ($bindings)
143 # || croak "No BindingSet called '$bindings'";
144 # }
145 #
146 # my $keyval;
147 # if ($event->can('keyval')) {
148 # $keyval = $event->keyval;
149 # } elsif ($event->can('button')) {
150 # my $button = $event->button;
151 # $keyval = Gtk2::Gdk->keyval_from_name("Pointer_Button$button")
152 # || do { warn "BindingBits ignoring unrecognised button number $button";
153 # return 0; };
154 # } else {
155 # croak "Unrecognised event type $event";
156 # }
157 #
158 # my $found = $bindings->activate ($keyval, $event->get_state, $object);
159 # ### activate_event_keyval() found: $found
160 # return $found;
161 # }