blib/lib/Gtk2/Ex/MindMapView/HotSpot.pm untested

filter: Statementstmt 0.0% 0 / 127 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 18 totaltotal 0.0% 0 / 229 CC 26 SCAR 32.9
CC
26
Cov
0%
set
90.0
_event_handler
42.0
hotspot_engaged
20.0
CRAP 26.9
1 package Gtk2::Ex::MindMapView::HotSpot;
2  
3 our $VERSION = '0.000001';
4  
5 use warnings;
6 use strict;
7 use Carp;
8  
9 use Gtk2::Ex::MindMapView::ArgUtils;
10  
11 use Glib ':constants';
12  
13 use Gnome2::Canvas;
14  
15 sub new
16 {
17 my ($class, @attributes) = @_;
18  
19 my $self = {};
20  
21 bless $self, $class;
22  
23 my %attributes = @attributes;
24  
25 args_required(\%attributes, qw(item side));
26  
27 args_store($self, \%attributes);
28  
29 if (! grep { $_ eq $self->{side} } qw(right left))
30 {
31 croak "Unexpected side: $self->{side}. Valid are: 'right' and 'left'\n";
32 }
33  
34 if (!$self->{item}->isa('Gtk2::Ex::MindMapView::Item'))
35 {
36 croak "Item argument is not a Gtk2::Ex::MindMapView::Item.\n";
37 }
38  
39 $self->{engaged} = FALSE;
40  
41 $self->{item}->signal_connect(hotspot_adjust=>
42 sub { $self->hotspot_adjust_event_handler($_[1]); });
43  
44 $self->{item}->signal_connect(event=>\&_event_handler, $self);
45  
46 # $self->{image} = $self->hotspot_get_image();
47  
48 return $self;
49 }
50  
51  
52 # $self->hotspot_adjust_event_handler($item);
53  
54 sub hotspot_adjust_event_handler
55 {
56 my ($self, $item) = @_;
57  
58 croak "You must supply a handler for the 'hotspot_adjust' event.\n";
59 }
60  
61  
62 # $self->hotspot_button_press($item, $event);
63  
64 sub hotspot_button_press
65 {
66 my ($self, $item, $event) = @_;
67 }
68  
69  
70 # $self->hotspot_button_release($item, $event);
71  
72 sub hotspot_button_release
73 {
74 my ($self, $item, $event) = @_;
75 }
76  
77  
78 # my $flag = $self->hotspot_engaged(\@coords);
79  
80 sub hotspot_engaged
81 {
82 my ($self, $coords_ref) = @_;
83  
84 return FALSE if (!$self->{enabled});
85  
86 my @coords = @$coords_ref;
87  
88 my ($x1, $y1, $x2, $y2) = $self->{image}->get_bounds();
89  
90 return FALSE if (($coords[0] < $x1) || ($coords[0] > $x2));
91  
92 return FALSE if (($coords[1] < $y1) || ($coords[1] > $y2));
93  
94 return TRUE;
95 }
96  
97  
98 # $self->hotspot_enter_notify($item, $event);
99  
100 sub hotspot_enter_notify
101 {
102 my ($self, $item, $event) = @_;
103 }
104  
105  
106 # my $image = $self->hotspot_get_image();
107  
108 sub hotspot_get_image
109 {
110 my $self = shift(@_);
111  
112 croak "No hotspot image given. Every hotspot must \n" .
113 "have a Gnome2::Canvas::Item for an image.\n";
114 }
115  
116  
117 # $self->hotspot_leave_notify($item, $event);
118  
119 sub hotspot_leave_notify
120 {
121 my ($self, $item, $event) = @_;
122 }
123  
124  
125 # $self->hotspot_motion_notify($item, $event);
126  
127 sub hotspot_motion_notify
128 {
129 my ($self, $item, $event) = @_;
130 }
131  
132  
133 # $hotspot->set(...);
134  
135 sub set
136 {
137 my $self = shift(@_);
138  
139 my %attributes = @_;
140  
141 args_valid(\%attributes, qw(visible enabled fill_color_gdk
142 outline_color_gdk hotspot_color_gdk));
143  
144 foreach my $key (keys %attributes)
145 {
146 if ($key eq 'enabled')
147 {
148 $self->{enabled} = $attributes{$key};
149  
150 if ($self->{enabled})
151 {
152 $self->{image}->show();
153 }
154 else
155 {
156 $self->{image}->hide();
157 }
158  
159 next;
160 }
161  
162 if ($key eq 'fill_color_gdk')
163 {
164 my $fill_color_gdk = $attributes{$key};
165  
166 if (!$fill_color_gdk->isa('Gtk2::Gdk::Color'))
167 {
168 croak "set(fill_color_gdk=>...) expecting a Gtk2::Gdk::Color\n";
169 }
170  
171 $self->{fill_color_gdk} = $fill_color_gdk;
172  
173 $self->{image}->set(fill_color_gdk=>$fill_color_gdk);
174  
175 next;
176 }
177  
178 if ($key eq 'outline_color_gdk')
179 {
180 my $outline_color_gdk = $attributes{$key};
181  
182 if (!$outline_color_gdk->isa('Gtk2::Gdk::Color'))
183 {
184 croak "set(outline_color_gdk=>...) expecting a Gtk2::Gdk::Color\n";
185 }
186  
187 $self->{outline_color_gdk} = $outline_color_gdk;
188  
189 $self->{image}->set(outline_color_gdk=>$outline_color_gdk);
190  
191 next;
192 }
193  
194 if ($key eq 'hotspot_color_gdk')
195 {
196 my $hotspot_color_gdk = $attributes{$key};
197  
198 if (!$hotspot_color_gdk->isa('Gtk2::Gdk::Color'))
199 {
200 croak "set(hotspot_color_gdk=>...) expecting a Gtk2::Gdk::Color\n";
201 }
202  
203 $self->{hotspot_color_gdk} = $hotspot_color_gdk;
204  
205 next;
206 }
207 }
208 }
209  
210  
211 sub _cursor_grab
212 {
213 my ($self, $item, $time) = @_;
214  
215 $item->grab ([qw/pointer-motion-mask button-release-mask leave-notify-mask button-press-mask/],
216 Gtk2::Gdk::Cursor->new('hand2'), $time);
217  
218 $self->{image}->set(fill_color_gdk=>$self->{hotspot_color_gdk});
219 }
220  
221  
222 sub _cursor_release
223 {
224 my ($self, $item, $time) = @_;
225  
226 $item->ungrab($time);
227  
228 $self->{image}->set(fill_color_gdk=>$self->{fill_color_gdk});
229 }
230  
231  
232 sub _event_enter_notify
233 {
234 my ($self, $item, $event) = @_;
235  
236 my @coords = $event->coords; # world
237  
238 $self->{engaged} = $self->hotspot_engaged(\@coords);
239  
240 if ($self->{engaged})
241 {
242 _cursor_grab($self, $item, $event->time);
243  
244 $self->hotspot_enter_notify($item, $event);
245 }
246 }
247  
248  
249 sub _event_button_press
250 {
251 my ($self, $item, $event) = @_;
252  
253 if ($event->button == 1)
254 {
255 my @coords = $event->coords; # world
256  
257 $self->{engaged} = $self->hotspot_engaged(\@coords);
258  
259 if ($self->{engaged})
260 {
261 _cursor_grab($self, $item, $event->time);
262  
263 $self->hotspot_button_press($item, $event);
264 }
265 }
266 }
267  
268  
269 sub _event_leave_notify
270 {
271 my ($self, $item, $event) = @_;
272  
273 if ($self->{engaged})
274 {
275 _cursor_release($self, $item, $event->time);
276  
277 $self->hotspot_leave_notify($item, $event);
278 }
279 }
280  
281  
282 sub _event_button_release
283 {
284 my ($self, $item, $event) = @_;
285  
286 _cursor_release($self, $item, $event->time);
287  
288 if ($self->{engaged})
289 {
290 $self->hotspot_button_release($item, $event);
291  
292 $self->{engaged} = FALSE;
293 }
294 }
295  
296  
297 sub _event_motion_notify
298 {
299 my ($self, $item, $event) = @_;
300  
301 if ($event->state >= 'button1-mask')
302 {
303 if ($self->{engaged})
304 {
305 $self->hotspot_motion_notify($item, $event);
306 }
307 }
308 }
309  
310  
311 sub _event_handler
312 {
313 my ($item, $event, $self) = @_;
314  
315 my $event_type = $event->type;
316  
317 my @coords = $event->coords;
318  
319 # print "event_type: $event_type event_coords: @coords\n";
320  
321 if ($event_type eq 'motion-notify')
322 {
323 _event_motion_notify($self, $item, $event);
324 }
325  
326 elsif ($event_type eq 'button-press')
327 {
328 _event_button_press($self, $item, $event);
329 }
330  
331 elsif ($event_type eq 'button-release')
332 {
333 _event_button_release($self, $item, $event);
334 }
335  
336 elsif ($event_type eq 'enter-notify')
337 {
338 _event_enter_notify($self, $item, $event);
339 }
340  
341 elsif ($event_type eq 'leave-notify')
342 {
343 _event_leave_notify($self, $item, $event);
344 }
345 }
346  
347  
348  
349 1; # Magic true value required at end of module
350  
351 __END__