| 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__ | ||
| 76 | |||
| 77 | =for stopwords Gdk WidgetPointerGrab | ||
| 78 | |||
| 79 | =head1 NAME | ||
| 80 | |||
| 81 | App::Chart::Gtk2::Ex::WidgetPointerGrab -- active pointer grab for widget | ||
| 82 | |||
| 83 | =head1 SYNOPSIS | ||
| 84 | |||
| 85 | use App::Chart::Gtk2::Ex::WidgetPointerGrab; | ||
| 86 | |||
| 87 | # grab as a perl object | ||
| 88 | my $grab = App::Chart::Gtk2::Ex::WidgetPointerGrab->new_pointer_grab | ||
| 89 | # (many args ...); | ||
| 90 | |||
| 91 | =head1 DESCRIPTION | ||
| 92 | |||
| 93 | C<App::Chart::Gtk2::Ex::WidgetPointerGrab> is an object-oriented wrapper around the Gdk | ||
| 94 | pointer grab mechanism. It automatically removes a grab when the | ||
| 95 | WidgetPointerGrab object is destroyed. | ||
| 96 | |||
| 97 | =head1 FUNCTIONS | ||
| 98 | |||
| 99 | =over 4 | ||
| 100 | |||
| 101 | =item C<< App::Chart::Gtk2::Ex::WidgetPointerGrab->new_pointer_grab (...) >> | ||
| 102 | |||
| 103 | =back | ||
| 104 | |||
| 105 | =head1 SEE ALSO | ||
| 106 | |||
| 107 | L<Gtk2::Gdk::Window> | ||
| 108 | |||
| 109 | =cut |