FormFactory.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« FormFactory/Window.pm ↑ Summary FormFactory/Intro.pm »
1 package Gtk2::Ex::FormFactory;
2  
3 $VERSION = "0.67";
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use base qw( Gtk2::Ex::FormFactory::Container );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 0 ▶use Gtk2;
Subroutine BEGIN: called CC=1 SCAR=2.6
10 use Gtk2::Ex::FormFactory::Loader;
11 use Scalar::Util;
12  
13 sub get_type { "form_factory" }
14  
15 use Gtk2::Ex::FormFactory::Context;
16 use Gtk2::Ex::FormFactory::Layout;
17 use Gtk2::Ex::FormFactory::Rules;
18  
19 sub get_context { shift->{context} }
20 sub get_sync { shift->{sync} }
21 sub get_layouter { shift->{layouter} }
22 sub get_rule_checker { shift->{rule_checker} }
23 sub get_gtk_size_groups { shift->{gtk_size_groups} }
24 sub get_parent_ff { shift->{parent_ff} }
25 sub get_widgets_by_name { shift->{widgets_by_name} }
26 sub get_buffered { shift->{buffered} }
27  
28 sub set_context { shift->{context} = $_[1] }
29 sub set_sync { shift->{sync} = $_[1] }
30 sub set_layouter { shift->{layouter} = $_[1] }
31 sub set_rule_checker { shift->{rule_checker} = $_[1] }
32 sub set_gtk_size_groups { shift->{gtk_size_groups} = $_[1] }
33 sub set_parent_ff { shift->{parent_ff} = $_[1] }
34 sub set_widgets_by_name { shift->{widgets_by_name} = $_[1] }
35 sub set_buffered { shift->{buffered} = $_[1] }
36  
37 sub get_form_factory { shift }
38  
39 sub new {
40 my $class = shift;
41 my %par = @_;
42 my ($context, $parent_ff, $sync, $layouter, $rule_checker) =
43 @par{'context','parent_ff','sync','layouter','rule_checker'};
44  
45 my $self = $class->SUPER::new(@_);
46  
47 $sync = 1 unless defined $sync;
48 $context ||= Gtk2::Ex::FormFactory::Context->new;
49 $layouter ||= Gtk2::Ex::FormFactory::Layout->new;
50 $rule_checker ||= Gtk2::Ex::FormFactory::Rules->new;
51  
52 $self->set_parent_ff ($parent_ff);
53 $self->set_context ($context);
54 $self->set_sync ($sync);
55 $self->set_layouter ($layouter);
56 $self->set_rule_checker ($rule_checker);
57 $self->set_gtk_size_groups ({});
58 $self->set_widgets_by_name ({});
59 $self->set_buffered (1);
60  
61 return $self;
62 }
63  
64 sub cleanup {
65 my $self = shift;
66
67 $self->SUPER::cleanup(@_);
68
69 $self->set_gtk_size_groups({});
70 $self->set_widgets_by_name({});
71  
72 1;
73 }
74  
75 sub open {
76 my $self = shift;
77 my %par = @_;
78 my ($hide) = $par{'hide'};
79  
80 #-- First build all widgets as implemented in the Widget class
81 $self->build();
82
83 #-- Now show all widgets, if we shouldn't keep the hided
84 $self->show if not $hide;
85
86 1;
87 }
88  
89 sub build {
90 my $self = shift;
91
92 return if $self->get_built;
93
94 #-- first register all widgets of this FormFactory
95 #-- to resolve cross references between Widgets
96 #-- during building.
97 $self->register_all_widgets($self);
98
99 #-- Now call Container's build
100 $self->SUPER::build();
101
102 1;
103 }
104  
105 sub show {
106 my $self = shift;
107
108 #-- Show all widgets
109 foreach my $child ( @{$self->get_content} ) {
110 $child->get_gtk_parent_widget->show_all;
111 }
112
113 #-- And finally connect the changed signals - this need to be
114 #-- done *after* showing the widgets. For containers ->show()
115 #-- could trigger updates which cause object updates. Since
116 #-- ->update() isn't called yet, this would invalidate the
117 #-- object's state.
118 $self->connect_signals;
119
120 1;
121 }
122  
123 sub update {
124 my $self = shift;
125 $self->update_all;
126 }
127  
128 sub ok {
129 my $self = shift;
130  
131 $self->apply;
132 $self->close;
133
134 1;
135 }
136  
137 sub apply {
138 my $self = shift;
139  
140 if ( $self->get_sync ) {
141 $self->commit_proxy_buffers_all;
142 } else {
143 $self->apply_changes_all;
144 }
145  
146 1;
147 }
148  
149 sub cancel {
150 my $self = shift;
151
152 $self->discard_proxy_buffers_all if $self->get_sync;
153 $self->close;
154
155 1;
156 }
157  
158 sub close {
159 my $self = shift;
160  
161 $_->get_gtk_parent_widget->destroy for @{$self->get_content};
162  
163 $self->cleanup;
164  
165 1;
166 }
167  
168 sub register_all_widgets {
169 my $self = shift;
170 my ($widget) = @_;
171
172 if ( $widget->isa("Gtk2::Ex::FormFactory::Container") ) {
173 $self->register_widget($widget);
174 foreach my $child ( @{$widget->get_content} ) {
175 $self->register_all_widgets($child);
176 }
177 } else {
178 $self->register_widget($widget);
179 }
180
181 1;
182 }
183  
184 sub register_widget {
185 my $self = shift;
186 my ($widget) = @_;
187  
188 $self->get_widgets_by_name->{$widget->get_name} = $widget;
189  
190 $self->set_buffered(0) if $widget->get_object &&
191 !$self->get_context
192 ->get_proxy($widget->get_object)
193 ->get_buffered;
194  
195 1;
196 }
197  
198 sub get_form_factory_gtk_window {
199 my $self = shift;
200
201 my $gtk_window;
202 foreach my $child ( @{$self->get_content} ) {
203 if ( $child->isa("Gtk2::Ex::FormFactory::Window") ) {
204 $gtk_window = $child->get_gtk_parent_widget;
205 last;
206 }
207 }
208
209 return $gtk_window;
210 }
211  
212 sub open_confirm_window {
213 my $self = shift;
214 my %par = @_;
215 my ($message, $yes_callback, $no_callback, $position, $with_cancel) =
216 @par{'message','yes_callback','no_callback','position','with_cancel'};
217 my ($yes_label, $no_label) =
218 @par{'yes_label','no_label'};
219  
220 $position ||= "center-on-parent";
221 $yes_label ||= "gtk-yes";
222 $no_label ||= "gtk-no";
223  
224 my $confirm = Gtk2::MessageDialog->new_with_markup (
225 $self->get_form_factory_gtk_window,
226 ["modal","destroy-with-parent"],
227 "question",
228 "none",
229 $message
230 );
231  
232 $confirm->add_buttons ("gtk-cancel", "cancel") if $with_cancel;
233 $confirm->add_buttons ($no_label, "no");
234 $confirm->add_buttons ($yes_label, "yes");
235  
236 $confirm->signal_connect("response", sub {
237 my ($widget, $answer) = @_;
238 if ( $answer eq 'yes' ) {
239 &$yes_callback() if $yes_callback;
240 }
241 if ( $answer eq 'no' ) {
242 &$no_callback() if $no_callback;
243 }
244 $widget->destroy;
245 1;
246 });
247  
248 $confirm->set_position ($position);
249 $confirm->show;
250  
251 1;
252 }
253  
254 sub open_message_window {
255 my $self = shift;
256 my %par = @_;
257 my ($type, $message, $position) =
258 @par{'type','message','position'};
259  
260 $position ||= "center-on-parent";
261 $type ||= "info";
262  
263 my $confirm = Gtk2::MessageDialog->new_with_markup (
264 $self->get_form_factory_gtk_window,
265 ["modal","destroy-with-parent"],
266 $type,
267 "ok",
268 $message
269 );
270  
271 $confirm->signal_connect("response", sub {
272 my ($widget) = @_;
273 $widget->destroy;
274 1;
275 });
276 $confirm->set_position ($position);
277 $confirm->show;
278  
279 1;
280 }
281  
282 sub get_image_path {
283 my $class = shift;
284 my ($filename) = @_;
285
286 foreach my $dir ( @INC ) {
287 return "$dir/$filename" if -f "$dir/$filename";
288 }
289
290 return;
291 }
292  
293 sub change_mouse_cursor {
294 my $self = shift;
295 my ($type, $gtk_window) = @_;
296  
297 $gtk_window ||= $self->get_form_factory_gtk_window;
298 return unless $gtk_window;
299
300 my $cursor = $type ? Gtk2::Gdk::Cursor->new($type) : undef;
301 $gtk_window->window->set_cursor($cursor);
302 Gtk2->main_iteration while Gtk2->events_pending;
303
304 1;
305 }
306  
307 1;
308  
309 __END__
« FormFactory/Window.pm ↑ Summary FormFactory/Intro.pm »