blib/lib/Games/RolePlay/MapGen/Editor/_MForm.pm untested

filter: Statementstmt 0.0% 0 / 261 Branchbran 0.0% 0 / 114 Conditioncond 0.0% 0 / 43 MC/DCmcdc 0.0% 0 / 27 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 453 CC 54 SCAR 78.9
CC
54
Cov
0%
make_form
2970.0
BEGIN
2.0
BEGIN
2.0
CRAP 2673.2
1 # vi:syntax=perl:
2  
3 # NOTE: I'm intending to one day split this module out into something like
4 # Gtk2::Ex::SimpleOnePageForm It's not finished-enough to release that way, so
5 # I'm just storing it here until I get further with it. If you'd like to use
6 # this elsewhere, lemme know and I'll finish it.
7 #
8 # -Paul
9  
10 package Games::RolePlay::MapGen::Editor::_MForm;
11  
12 use common::sense;
13 use Gtk2;
14 use Gtk2::SimpleList;
15 use Glib qw(TRUE FALSE);
16 use Data::Dump qw(dump);
17  
18 use parent 'Exporter';
19 our @EXPORT_OK = qw(make_form $default_restore_defaults);
20 our $default_restore_defaults = sub {
21 my ($button, $reref) = @_;
22  
23 $button->signal_connect( clicked => sub {
24 for my $k (keys %$reref) {
25 my ($item, $widget) = @{ $reref->{$k} };
26  
27 next unless exists $item->{default} or exists $item->{defaults};
28  
29 my $type = $item->{type};
30 if( $type eq "choice" or $type eq "bool" ) {
31 my ($d_i) = grep {$item->{choices}[$_] eq $item->{default}} 0 .. $#{ $item->{choices} };
32 $widget->set_active( $d_i );
33  
34 } elsif( $type eq "text" ) {
35 $widget->set_text( $item->{default} );
36  
37 } elsif( $type eq "choices" ) {
38 my $def = {map {($_=>1)} @{ $item->{defaults} }};
39 my $d = $item->{choices};
40 my @s = grep {$def->{$d->[$_]}} 0 .. $#$d;
41  
42 $widget->get_selection->unselect_all;
43 $widget->select( @s );
44  
45 } else { die "no handler for default of type $type" }
46 }
47 });
48 };
49  
50 sub make_form {
51 my ($parent_window, $i, $options, $title, $extra_buttons) = @_;
52  
53 my $table = Gtk2::Table->new(scalar @{$options->[0]}*2, scalar @$options, FALSE);
54 my $dialog = new Gtk2::Dialog(($title||"Data Required"), $parent_window, [], 'gtk-cancel' => "cancel", 'gtk-ok' => "ok");
55 $dialog->set_default_response('ok');
56  
57 my $reref = {};
58  
59 my $c_n = 0;
60 my @req;
61 for my $column (@$options) {
62 my $y = 0;
63  
64 for my $item (@$column) {
65 my $x = 2*$c_n;
66  
67 my $label = Gtk2::Label->new_with_mnemonic($item->{mnemonic} || die "no mnemonic?");
68 $label->set_alignment(1, 0.5);
69 $label->set_tooltip_text( $item->{desc} ) if exists $item->{desc};
70  
71 my $my_req = @req;
72  
73 my ($widget, $attach);
74 my $z = 1;
75 my $IT = $item->{type};
76 if( $IT eq "text" ) {
77 my $d = exists $i->{$item->{name}} ? $i->{$item->{name}} : $item->{default};
78 $d = $item->{trevnoc}->($d) if ref $d and exists $item->{trevnoc};
79  
80 $attach = $widget = new Gtk2::Entry;
81 $widget->set_text($d);
82 $widget->set_activates_default(TRUE);
83  
84 $widget->set_tooltip_text( $item->{desc} ) if exists $item->{desc};
85 $widget->signal_connect(changed => sub {
86 my $text = $widget->get_text;
87 my $chg = 0;
88 for my $fix (@{ $item->{fixes} }) {
89 $chg ++ if $fix->($text);
90 }
91 $widget->set_text($text) if $chg;
92 $req[$my_req] = 1;
93 for my $match (@{ $item->{matches} }) {
94 my $r = ref $match;
95 if( $r eq "CODE" ) {
96 $req[$my_req] = 0 unless $match->($text);
97  
98 } elsif( $r eq "Regexp" ) {
99 $req[$my_req] = 0 unless $text =~ $match;
100  
101 } else {
102 warn "bad match element for $item->{name}: $match";
103 }
104 }
105  
106 $dialog->set_response_sensitive( ok => (@req == grep {$_} @req) );
107 });
108  
109 if( $item->{convert} ) {
110 $item->{extract} = sub { $item->{convert}->( $widget->get_text ) };
111  
112 } else {
113 $item->{extract} = sub { $widget->get_text };
114 }
115  
116 # $widget->signal_connect(changed => sub { warn "test!"; }); # [WORKS FINE]
117  
118 } elsif( $IT eq "color" ) {
119 my $d = exists $i->{$item->{name}} ? $i->{$item->{name}} : $item->{default};
120 $d = $item->{trevnoc}->($d) if ref $d and exists $item->{trevnoc};
121 my @c = map {(hex $_)*257} $d =~ m/([a-fA-F\d]{2})/g;
122  
123 $attach = $widget = Gtk2::ColorButton->new_with_color(Gtk2::Gdk::Color->new(@c));
124 $widget->set_tooltip_text( $item->{desc} ) if exists $item->{desc};
125  
126 my $color = sub {
127 my $c = $widget->get_color;
128 my @rgb = map {int( $c->$_() / 257 )} qw(red green blue);
129  
130 sprintf '#%02x%02x%02x', @rgb;
131 };
132  
133 if( $item->{convert} ) {
134 $item->{extract} = sub { $item->{convert}->( $color->() ) };
135  
136 } else {
137 $item->{extract} = sub { $color->() };
138 }
139  
140 } elsif( $IT eq "choice" ) {
141 $attach = $widget = Gtk2::ComboBox->new_text;
142 my $d = $i->{$item->{name}} || $item->{default} || $item->{choices}[0];
143 $d = $item->{trevnoc}->($d) if ref $d and exists $item->{trevnoc};
144  
145 my $i = 0;
146 my $d_i;
147 for(@{$item->{choices}}) {
148 $widget->append_text($_);
149 $d_i = $i if $_ eq $d;
150 $i++;
151 }
152 $widget->set_active($d_i) if defined $d_i;
153 $widget->set_tooltip_text( $item->{desc} ) if exists $item->{desc};
154  
155 if( $item->{convert} ) {
156 $item->{extract} = sub { $item->{convert}->( $widget->get_active_text ) };
157  
158 } else {
159 $item->{extract} = sub { $widget->get_active_text };
160 }
161  
162 # $widget->signal_connect(changed => sub { warn "test!"; }); # [WORKS FINE]
163  
164 if( exists $item->{descs} and exists $item->{desc} ) {
165 $widget->signal_connect(changed => my $si = sub {
166 if( exists $item->{descs}{ my $at = $widget->get_active_text } ) {
167 $widget->set_tooltip_text( "$at - $item->{descs}{$at}" );
168  
169 } else {
170 $widget->set_tooltip_text( $item->{desc} );
171 }
172 });
173  
174 $si->();
175 }
176  
177 } elsif( $IT eq "choices" ) {
178 my $def = $i->{$item->{name}};
179 $def = $item->{trevnoc}->($def) if ref $def and exists $item->{trevnoc};
180 $def = $item->{defaults} unless $def;
181 $def = {map {($_=>1)} @$def};
182  
183 my $d = $item->{choices};
184 my @s = grep {$def->{$d->[$_]}} 0 .. $#$d;
185  
186 $widget = Gtk2::SimpleList->new( plugin_name_unseen => "text" );
187 $widget->set_headers_visible(FALSE);
188 $widget->set_data_array($d);
189 $widget->get_selection->set_mode('multiple');
190 $widget->select( @s );
191  
192 # NOTE: $widget->{data} = $d -- doesn't work !!!! fuckers you
193 # have to use @{$widget->{data}} = @$d, so I chose to just use
194 # the set_data_array() why even bother trying to do the scope
195 # hack... pfft.
196  
197 $widget->set_has_tooltip(TRUE);
198 $widget->signal_connect (query_tooltip => sub {
199 my ($widget, $x, $y, $keyboard_mode, $tooltip) = @_;
200  
201 # First, find out where the pointer is:
202 my $path = $widget->get_path_at_pos($x, $y);
203  
204 # If we're not pointed at a row, then return FALSE to say
205 # "don't show a tip".
206 return FALSE unless $path;
207  
208 # Otherwise, ask the TreeView to set up the tip's area according
209 # to the row's rectangle.
210 $widget->set_tooltip_row($tooltip, $path);
211  
212 $tooltip->set_text($item->{descs}{$d->[($path->get_indices)[0]]});
213  
214 # Return true to say "show the tip".
215 return TRUE;
216 });
217  
218 $attach = Gtk2::ScrolledWindow->new;
219  
220 my $vp = Gtk2::Viewport->new;
221 $attach->set_policy('automatic', 'automatic');
222 $attach->add($vp);
223 $vp->add($widget);
224  
225 $z = (exists $item->{z} ? $item->{z} : 2);
226  
227 if( $item->{convert} ) {
228 $item->{extract} = sub { $item->{convert}->( [map {$d->[$_]} $widget->get_selected_indices] ) };
229  
230 } else {
231 $item->{extract} = sub { [map {$d->[$_]} $widget->get_selected_indices] };
232 }
233  
234 # $widget->get_selection->signal_connect(changed => sub { warn "test!"; }); # [WORKS FINE]
235  
236 } elsif( $IT eq "bool" ) {
237 $attach = $widget = new Gtk2::CheckButton;
238 $widget->set_active(exists $i->{$item->{name}} ? $i->{$item->{name}} : $item->{default})
239 if exists $item->{default} or exists $i->{$item->{name}};
240  
241 $widget->set_tooltip_text( $item->{desc} ) if exists $item->{desc};
242 $item->{extract} = sub { $widget->get_active ? 1:0 };
243 # $widget->signal_connect(toggled => sub { warn "test!"; }); # [WORKS FINE]
244 }
245  
246 else { die "unknown form type: $item->{type}" }
247  
248 $reref->{$item->{name}} = [ $item, $widget ];
249  
250 $label->set_mnemonic_widget($widget);
251 $table->attach_defaults($label, $x, $x+1, $y, $y+1); $x ++;
252 $table->attach_defaults($attach, $x, $x+1, $y, $y+$z); $y += $z;
253 }
254  
255 $c_n ++;
256 }
257  
258 for my $column (@$options) {
259 for my $item (@$column) {
260 if( my $d = $item->{disable} ) {
261 my $this_e = $reref->{$item->{name}};
262 my $this_type = $this_e->[0]{type};
263  
264 if( $this_type eq "choices" ) {
265 my %disabled; # It appears treviews don't have a row-sensitive function, so we're going to hook
266 # the changed signal and play some pretend.
267  
268 my @d = @{ $item->{choices} };
269 my $LOOPER = 0;
270  
271 $this_e->[1]->get_selection->signal_connect(changed => my $changed = sub {
272 unless( $LOOPER ) {
273 $LOOPER = 1;
274 my @s = grep {!$disabled{$d[$_]}} $this_e->[1]->get_selected_indices;
275 $this_e->[1]->get_selection->unselect_all;
276 $this_e->[1]->select(@s);
277 $LOOPER = 0;
278 }
279 });
280  
281 for my $row_name (keys %$d) { my $row = $d->{$row_name};
282 for my $k (keys %$row) {
283 my $that_e = $reref->{$k};
284 my $that_type = $that_e->[0]{type};
285  
286 if( $that_type eq "choice" or $that_type eq "text") {
287 $that_e->[1]->signal_connect( changed => my $f = sub {
288 my $sensitive = ($row->{$k}->( $that_e->[0]{extract}->() ) ? FALSE : TRUE);
289
290 $disabled{$row_name} = not $sensitive;
291 $changed->();
292 });
293  
294 $f->();
295  
296 } elsif( $that_type eq "choices" ) {
297 $that_e->[1]->get_selection->signal_connect( changed => my $f = sub {
298 my $sensitive = ($row->{$k}->( $that_e->[0]{extract}->() ) ? FALSE : TRUE);
299
300 $disabled{$row_name} = not $sensitive;
301 $changed->();
302 });
303  
304 $f->();
305 }
306
307 else { die "unhandled disabler: $k/$that_type" }
308 }}
309  
310 } else {
311 for my $k (keys %$d) {
312 my $that_e = $reref->{$k};
313 my $that_type = $that_e->[0]{type};
314  
315 if( $that_type eq "choice" or $that_type eq "text") {
316 $that_e->[1]->signal_connect( changed => my $f = sub {
317 my $sensitive = ($d->{$k}->( $that_e->[0]{extract}->() ) ? FALSE : TRUE);
318
319 $this_e->[1]->set_sensitive($sensitive)
320 });
321  
322 $f->();
323  
324 } elsif( $that_type eq "choices" ) {
325 $that_e->[1]->get_selection->signal_connect( changed => my $f = sub {
326 my $sensitive = ($d->{$k}->( $that_e->[0]{extract}->() ) ? FALSE : TRUE);
327
328 $this_e->[1]->set_sensitive($sensitive)
329 });
330  
331 $f->();
332 }
333
334 else { die "unhandled disabler: $k/$that_type" }
335 }
336 }
337 }
338 }}
339  
340 if( ref $extra_buttons ) {
341 for my $eba (@$extra_buttons) {
342 my $aa = $dialog->action_area;
343 my $button = new Gtk2::Button($eba->[0]);
344 $button->set_tooltip_text($eba->[2]) if defined $eba->[2];
345  
346 $aa->add( $button );
347 $aa->set_child_secondary( $button, TRUE );
348  
349 if( ref (my $cb = $eba->[1]) eq "CODE" ) {
350 $cb->( $button, $reref );
351 }
352 }
353 }
354  
355 $dialog->vbox->pack_start($table,0,0,4);
356 $dialog->set_response_sensitive( ok => TRUE );
357 $dialog->show_all;
358  
359 my ($ok_button) = grep {$_->can("get_label") and $_->get_label =~ m/ok/} $dialog->action_area->get_children;
360  
361 # This is better than nothing if we can't find the ok button with the grep...
362 # It makes it so OK is selected when we tab to the action area.
363 # (We're likely to always find the ok button, but for edification purposes,
364 # this is what we'd do if we didn't...)
365 # Second thought, let's just set this anyway...
366 $dialog->set_default_response('ok');
367  
368 if( $ok_button ) {
369 # set_default_response() doesn't seem to be enough oomph...
370 # It sets the ok button default, but lets the first entry in the Table get the actual focus
371 $ok_button->grab_focus;
372 }
373  
374 my $o = {};
375 my $r = $dialog->run;
376  
377 if( $r eq "ok" ) {
378 for my $c (@$options) {
379 for my $e (@$c) {
380 if( $reref->{$e->{name}}[1]->is_sensitive() ) {
381  
382 $o->{$e->{name}} = $e->{extract}->();
383 }
384 }
385 }
386 }
387  
388 ## DEBUG ## warn dump($r, $o);
389  
390 $dialog->destroy;
391 return ($r,$o);
392 }