App/Chart/Gtk2/Ex/WidgetPositionTracker.pm untested

filter: Statementstmt 0.0% 0 / 72 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 10 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 121 CC 9 SCAR 26.1
CC
9
Cov
0%
SET_PROPERTY
30.0
_do_configure_event
30.0
INIT_INSTANCE
2.0
CRAP 13.7
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__
153  
154 , values %instances_nowin
155 $size_allocate_id ||= Gtk2::Widget->signal_add_emission_hook
156 (size_allocate => \&_do_size_allocate);
157 sub _do_size_allocate {
158 my ($invocation_hint, $parameters) = @_;
159 unless (%instances_nowin) {
160 undef $size_allocate_id;
161 return 0; # disconnect
162 }
163  
164 my $changed_widget = $parameters->[0];
165 foreach my $self (values %{$changed_widget->{(__PACKAGE__)}}) {
166 $self->signal_emit ('moved');
167 }
168 return 1; # stay connected
169 }
170  
171 if (my $widget = $self->{'widget'}) {
172 if (my $href = $widget->{(__PACKAGE__)}) {
173 delete $href->{refaddr($self)};
174 if (! %$href) {
175 delete $widget->{(__PACKAGE__)};
176 }
177 }
178 }
179  
180 my $href = ($newval->{(__PACKAGE__)} ||= {});
181 $href->{refaddr($self)} = $self;
182 Scalar::Util::weaken ($href->{refaddr($self)});
183  
184 my $changed_alloc = $parameters->[1];
185 my $alloc = $widget->allocation;
186 print $alloc->width, $changed_alloc->width,"\n";
187  
188 sub _do_moved {
189 my ($widget, $allocation, $ref_weak_self) = @_;
190 my $self = $$ref_weak_self || return;
191 $self->signal_emit ('moved');
192 }
193  
194 } else {
195 delete $self->{'ids'};
196 }
197  
198 use Glib::Ex::SignalIds 5; # version 5 for ->add()
199  
200 sub _update_connections {
201 my ($self) = @_;
202 my $connections = $self->{'connections'};
203 @$connections = ();
204 my $widget = $self->{'widget'} || return;
205 my $ref_weak_self = App::Chart::Glib::Ex::MoreUtils::ref_weak($self);
206  
207 if ($widget->flags & 'no-window') {
208 push @$connections,
209 Glib::Ex::SignalIds->new
210 ($widget,
211 $widget->signal_connect (size_allocate => \&_do_moved,
212 $ref_weak_self));
213 }
214  
215 do {
216 my $ids = Glib::Ex::SignalIds->new
217 ($widget,
218 $widget->signal_connect ('notify::parent', \&_do_reparent,
219 $ref_weak_self));
220 if ($widget == $self->{'widget'}
221 || ! ($widget->flags & 'no-window')) {
222 $ids->add ($widget->signal_connect (size_allocate => \&_do_move,
223 $ref_weak_self));
224 }
225 push @$connections, $ids;
226  
227 } while ($widget = $widget->get_parent);
228 }
229  
230 sub _do_reparent {
231 my $ref_weak_self = $_[-1];
232 my $self = $$ref_weak_self || return;
233 _update_connections ($self);
234 $self->signal_emit ('moved');
235 }
236  
237 sub _do_moved {
238 my $ref_weak_self = $_[-1];
239 my $self = $$ref_weak_self || return;
240 $self->signal_emit ('moved');
241 }
242