| 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 widget position in root coords, with watching for reparenting | ||
| 20 | # | ||
| 21 | |||
| 22 | |||
| 23 | package App::Chart::Gtk2::Ex::WidgetPositionTracker; | ||
| 24 | use 5.008; | ||
| 25 | use strict; | ||
| 26 | use warnings; | ||
| 27 | use Gtk2; | ||
| 28 | use Glib::Ex::SignalIds; | ||
| 29 | use Scalar::Util 1.18 'refaddr'; # 1.18 for pure-perl refaddr() fix | ||
| 30 | |||
| 31 | use App::Chart::Glib::Ex::MoreUtils; | ||
| 32 | use App::Chart::Glib::Ex::TieWeakNotify; | ||
| 33 | |||
| 34 | # uncomment this to run the ### lines | ||
| 35 | #use Smart::Comments; | ||
| 36 | |||
| 37 | use Glib::Object::Subclass | ||
| 38 | 'Glib::Object', | ||
| 39 | properties => [Glib::ParamSpec->object | ||
| 40 | ('widget', | ||
| 41 | 'widget', | ||
| 42 | 'Blurb.', | ||
| 43 | 'Gtk2::Widget', | ||
| 44 | Glib::G_PARAM_READWRITE), | ||
| 45 | ], | ||
| 46 | signals => { moved => { param_types => [], | ||
| 47 | return_type => undef }, | ||
| 48 | }; | ||
| 49 | |||
| 50 | my %realized_instances; | ||
| 51 | my $configure_event_hook_id; | ||
| 52 | |||
| 53 | sub INIT_INSTANCE { | ||
| 54 | my ($self) = @_; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub FINALIZE_INSTANCE { | ||
| 58 | my ($self) = @_; | ||
| 59 | delete $realized_instances{refaddr($self)}; | ||
| 60 | delete $self->{'alloc_ids'}; | ||
| 61 | } | ||
| 62 | |||
| 63 | sub SET_PROPERTY { | ||
| 64 | my ($self, $pspec, $newval) = @_; | ||
| 65 | my $pname = $pspec->get_name; | ||
| 66 | |||
| 67 | if ($pname eq 'widget') { | ||
| 68 | FINALIZE_INSTANCE ($self); | ||
| 69 | delete $self->{'realize_ids'}; | ||
| 70 | App::Chart::Glib::Ex::TieWeakNotify->set ($self, $pname, $newval); | ||
| 71 | |||
| 72 | if ($newval) { | ||
| 73 | if ($newval->flags & 'no-window') { | ||
| 74 | $self->{'alloc_ids'} = Glib::Ex::SignalIds->new | ||
| 75 | ($newval, | ||
| 76 | $newval->signal_connect (size_allocate => \&_do_size_allocate, | ||
| 77 | App::Chart::Glib::Ex::MoreUtils::ref_weak($self))); | ||
| 78 | } | ||
| 79 | ### realized: $newval->realized | ||
| 80 | if ($newval->realized) { | ||
| 81 | _do_realize ($newval, \$self); | ||
| 82 | } else { | ||
| 83 | _connect_realize ($self, $newval); | ||
| 84 | } | ||
| 85 | } | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | # called for size changes as well as position changes, and by the time reach | ||
| 90 | # here the old size is apparently not available any more: $allocation in | ||
| 91 | # $parameter->[1] is the same as $widget->allocation. | ||
| 92 | # | ||
| 93 | sub _do_size_allocate { | ||
| 94 | my ($widget, $alloc, $ref_weak_self) = @_; | ||
| 95 | ### size_allocate signal: $widget | ||
| 96 | my $self = $$ref_weak_self || return; | ||
| 97 | $self->signal_emit ('moved'); | ||
| 98 | } | ||
| 99 | |||
| 100 | sub _connect_realize { | ||
| 101 | my ($self, $widget) = @_; | ||
| 102 | ### connect realize | ||
| 103 | $self->{'realize_ids'} = Glib::Ex::SignalIds->new | ||
| 104 | ($widget, | ||
| 105 | $widget->signal_connect (realize => \&_do_realize, | ||
| 106 | App::Chart::Glib::Ex::MoreUtils::ref_weak($self))); | ||
| 107 | ### ids: $self->{'realize_ids'} | ||
| 108 | } | ||
| 109 | |||
| 110 | sub _do_realize { | ||
| 111 | my ($widget, $ref_weak_self) = @_; | ||
| 112 | ### realize signal, window: $widget->window | ||
| 113 | my $self = $$ref_weak_self || return; | ||
| 114 | delete $self->{'realize_ids'}; | ||
| 115 | Scalar::Util::weaken ($realized_instances{refaddr($self)} = $self); | ||
| 116 | $configure_event_hook_id ||= Gtk2::Widget->signal_add_emission_hook | ||
| 117 | (configure_event => \&_do_configure_event); | ||
| 118 | } | ||
| 119 | |||
| 120 | sub _do_configure_event { | ||
| 121 | my ($invocation_hint, $parameters) = @_; | ||
| 122 | my $changed_widget = $parameters->[0]; | ||
| 123 | ### configure event: $changed_widget | ||
| 124 | |||
| 125 | foreach my $self (values %realized_instances) { | ||
| 126 | if (my $widget = $self->{'widget'}) { | ||
| 127 | if ($widget->window) { | ||
| 128 | if ($changed_widget == $widget | ||
| 129 | || $widget->is_ancestor ($changed_widget)) { | ||
| 130 | $self->signal_emit ('moved'); | ||
| 131 | } | ||
| 132 | } else { | ||
| 133 | # was unrealized | ||
| 134 | FINALIZE_INSTANCE ($self); | ||
| 135 | _connect_realize ($self, $widget); | ||
| 136 | } | ||
| 137 | } else { | ||
| 138 | # widget weakened away | ||
| 139 | FINALIZE_INSTANCE ($self); | ||
| 140 | } | ||
| 141 | } | ||
| 142 | if (%realized_instances) { | ||
| 143 | return 1; # stay connected | ||
| 144 | } else { | ||
| 145 | ### disconnect hook | ||
| 146 | undef $configure_event_hook_id; | ||
| 147 | return 0; # disconnect | ||
| 148 | } | ||
| 149 | } | ||
| 150 | |||
| 151 | 1; | ||
| 152 | __END__ |