| 1 | # Copyright 2008, 2009, 2010, 2011 Kevin Ryde | ||
| 2 | |||
| 3 | # This file is part of Chart. | ||
| 4 | # | ||
| 5 | # Chart is free software; you can redistribute it | ||
| 6 | # and/or modify it under the terms of the GNU General Public License as | ||
| 7 | # published by the Free Software Foundation; either version 3, or (at your | ||
| 8 | # option) any later version. | ||
| 9 | # | ||
| 10 | # Chart is distributed in the hope that it will be | ||
| 11 | # useful, but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General | ||
| 13 | # Public License for more details. | ||
| 14 | # | ||
| 15 | # You should have received a copy of the GNU General Public License along | ||
| 16 | # with Chart. If not, see <http://www.gnu.org/licenses/>. | ||
| 17 | |||
| 18 | package App::Chart::Gtk2::Ex::WidgetPointerGrab; | ||
| 19 | use 5.008; | ||
| 20 | use strict; | ||
| 21 | use warnings; | ||
| 22 | use Carp; | ||
| 23 | use App::Chart::Glib::Ex::MoreUtils; | ||
| 24 | |||
| 25 | sub new_pointer_grab { | ||
| 26 | my ($class, $widget, $owner_events, $event_mask, $confine_to, $cursor, $time) | ||
| 27 | = @_; | ||
| 28 | my $window = $widget->window | ||
| 29 | || croak "Cannot grab pointer for unrealized widget\n"; | ||
| 30 | |||
| 31 | my $status = $window->pointer_grab | ||
| 32 | ($window, $owner_events, $event_mask, $confine_to, $cursor, $time); | ||
| 33 | if ($status ne 'success') { | ||
| 34 | return $status; | ||
| 35 | } | ||
| 36 | |||
| 37 | my $self = bless { widget => $widget, | ||
| 38 | time => $time | ||
| 39 | }, $class; | ||
| 40 | require Scalar::Util; | ||
| 41 | Scalar::Util::weaken ($self->{'widget'}); | ||
| 42 | |||
| 43 | require Glib::Ex::SignalIds; | ||
| 44 | $self->{'broken_id'} = Glib::Ex::SignalIds->new | ||
| 45 | ($widget, | ||
| 46 | $widget->signal_connect ('grab_broken_event', \&_do_grab_broken, | ||
| 47 | App::Chart::Glib::Ex::MoreUtils::ref_weak($self))); | ||
| 48 | |||
| 49 | return $self; | ||
| 50 | } | ||
| 51 | |||
| 52 | sub _do_grab_broken { | ||
| 53 | my ($widget, $event, $ref_weak_self) = @_; | ||
| 54 | my $self = $$ref_weak_self || return; | ||
| 55 | delete $self->{'widget'}; | ||
| 56 | delete $self->{'broken_ids'}; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub ungrab { | ||
| 60 | my ($self) = @_; | ||
| 61 | if (my $widget = delete $self->{'widget'}) { | ||
| 62 | my $display = $widget->get_display; | ||
| 63 | $display->pointer_ungrab ($self->{'time'}); | ||
| 64 | |||
| 65 | delete $self->{'broken_ids'}; | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | sub DESTROY { | ||
| 70 | my ($self) = @_; | ||
| 71 | ungrab ($self); | ||
| 72 | } | ||
| 73 | |||
| 74 | 1; | ||
| 75 | __END__ |