| 1 | # Copyright 2009, 2010 Kevin Ryde | ||
| 2 | |||
| 3 | # This file is part of Chart. | ||
| 4 | # | ||
| 5 | # Chart is free software; you can redistribute it and/or modify | ||
| 6 | # it under the terms of the GNU General Public License as published by the | ||
| 7 | # Free Software Foundation; either version 3, or (at your option) any later | ||
| 8 | # version. | ||
| 9 | # | ||
| 10 | # Chart is distributed in the hope that it will be useful, but | ||
| 11 | # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
| 12 | # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| 13 | # 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 | |||
| 19 | # tracking window position in root coords, with watching for reparenting | ||
| 20 | # | ||
| 21 | |||
| 22 | |||
| 23 | package App::Chart::Gtk2::Ex::GdkWindowTracker; | ||
| 24 | use 5.008; | ||
| 25 | use strict; | ||
| 26 | use warnings; | ||
| 27 | use Gtk2; | ||
| 28 | use Scalar::Util 1.18 'refaddr'; # 1.18 for pure-perl refaddr() fix | ||
| 29 | |||
| 30 | # uncomment this to run the ### lines | ||
| 31 | #use Smart::Comments; | ||
| 32 | |||
| 33 | use Glib::Object::Subclass | ||
| 34 | 'Glib::Object', | ||
| 35 | properties => [Glib::ParamSpec->object | ||
| 36 | ('window', | ||
| 37 | 'window', | ||
| 38 | 'Blurb.', | ||
| 39 | 'Gtk2::Window', | ||
| 40 | Glib::G_PARAM_READWRITE), | ||
| 41 | ], | ||
| 42 | signals => { moved => { param_types => [], | ||
| 43 | return_type => undef }, | ||
| 44 | }; | ||
| 45 | |||
| 46 | my %realized_instances; | ||
| 47 | my $configure_event_hook_id; | ||
| 48 | |||
| 49 | # sub INIT_INSTANCE { | ||
| 50 | # my ($self) = @_; | ||
| 51 | # } | ||
| 52 | |||
| 53 | sub FINALIZE_INSTANCE { | ||
| 54 | my ($self) = @_; | ||
| 55 | delete $realized_instances{refaddr($self)}; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub SET_PROPERTY { | ||
| 59 | my ($self, $pspec, $newval) = @_; | ||
| 60 | my $pname = $pspec->get_name; | ||
| 61 | |||
| 62 | if ($pname eq 'window') { | ||
| 63 | FINALIZE_INSTANCE ($self); | ||
| 64 | App::Chart::Glib::Ex::TieWeakNotify->set ($self, $pname, $newval); | ||
| 65 | |||
| 66 | if ($newval) { | ||
| 67 | Scalar::Util::weaken ($realized_instances{refaddr($self)} = $self); | ||
| 68 | $configure_event_hook_id ||= Gtk2::Window->signal_add_emission_hook | ||
| 69 | (configure_event => \&_do_configure_event); | ||
| 70 | } | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | sub _do_configure_event { | ||
| 75 | my ($invocation_hint, $parameters) = @_; | ||
| 76 | my $changed_window = $parameters->[0]; | ||
| 77 | ### configure event: $changed_window | ||
| 78 | |||
| 79 | foreach my $self (values %realized_instances) { | ||
| 80 | if (my $window = $self->{'window'}) { | ||
| 81 | if (_window_is_ancestor_or_self ($changed_window, $window)) { | ||
| 82 | $self->signal_emit ('moved'); | ||
| 83 | } | ||
| 84 | } else { | ||
| 85 | # window weakened away | ||
| 86 | FINALIZE_INSTANCE ($self); | ||
| 87 | } | ||
| 88 | } | ||
| 89 | if (%realized_instances) { | ||
| 90 | return 1; # stay connected | ||
| 91 | } else { | ||
| 92 | ### disconnect hook | ||
| 93 | undef $configure_event_hook_id; | ||
| 94 | return 0; # disconnect | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | sub _window_is_ancestor_or_self { | ||
| 99 | my ($window, $child) = @_; | ||
| 100 | for (;;) { | ||
| 101 | if ($window == $child) { return 1; } | ||
| 102 | $window = $window->get_parent || return 0; | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | |||
| 107 | 1; | ||
| 108 | __END__ |