blib/lib/Games/Axmud/Obj/Simple.pm untested

filter: Statementstmt 0.0% 0 / 257 Branchbran 0.0% 0 / 74 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 50 Podpod 0.0% 0 / 40 totaltotal 0.0% 0 / 452 CC 38 SCAR 31.9
CC
38
Cov
0%
new_from_treeview
132.0
SPLICE
42.0
select
12.0
CRAP 24.2
1 # Copyright 2004 by the Gtk3-Perl team
2 #
3 # This program is free software: you can redistribute it and/or modify it under the terms of the GNU
4 # Lesser Public License as published by the Free Software Foundation, either version 3 of the
5 # License, or (at your option) any later version.
6 #
7 # This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
8 # even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 # Lesser Public License for more details.
10 #
11 # You should have received a copy of the GNU Lesser Public License along with this program. If not,
12 # see <http://www.gnu.org/licenses/>.
13 #
14 #
15 # Games::Axmud::Obj::SimpleList, a modification of Gtk3::SimpleList (v0.18) by Thierry Vignaud
16 #
17 # Packaged along with Axmud, and renamed, so that MS Windows users can use it, and so that CPAN
18 # doesn't complain about unauthorised packages
19 # List of changes (besides cosmetic ones):
20 # - Added 'use warnings' as the lack of it causes Kwalitee errors
21 # - Commented out $VERSION as it causes Kwalitee errors
22 # - Added a hack to ->new_from_treeview to prevent Gtk auto-selecting the first row in the simple
23 # list (sometimes, but not all the time)
24 # - Modified ->new_from_treeview to set the arguments in Gtk3::ListStore->new() to a list
25 # reference, rather than a straight list (as required by Gtk3, and without which we get Perl
26 # errors)
27 # - Modified ->text_cell_edited, ->new_from_treeview and GA::Obj::SimpleList::TiedRow->STORE to
28 # transform the arguments in ->set($iter, $cols, $val) to ->set($iter, [$cols], [$val]), as
29 # required by Gtk3, and without which we get Perl errors
30 # - Commented out Data::Dumper usage in ->get_selected_indices because it creates useless Perl
31 # warnings
32  
33 package Games::Axmud::Obj::SimpleList;
34  
35 use strict;
36 use warnings;
37 use Carp;
38 use Gtk3;
39  
40 our @ISA = 'Gtk3::TreeView';
41  
42 #our $VERSION = '0.18';
43  
44 our %column_types = (
45 'hidden' => {type=>'Glib::String', attr=>'hidden'},
46 'text' => {type=>'Glib::String', renderer=>'Gtk3::CellRendererText', attr=>'text'},
47 'markup' => {type=>'Glib::String', renderer=>'Gtk3::CellRendererText', attr=>'markup'},
48 'int' => {type=>'Glib::Int', renderer=>'Gtk3::CellRendererText', attr=>'text'},
49 'double' => {type=>'Glib::Double', renderer=>'Gtk3::CellRendererText', attr=>'text'},
50 'bool' => {type=>'Glib::Boolean', renderer=>'Gtk3::CellRendererToggle', attr=>'active'},
51 'scalar' => {type=>'Glib::Scalar', renderer=>'Gtk3::CellRendererText',
52 attr=> sub {
53 my ($tree_column, $cell, $model, $iter, $i) = @_;
54 my ($info) = $model->get ($iter, $i);
55 $cell->set (text => $info || '' );
56 } },
57 'pixbuf' => {type=>'Gtk3::Gdk::Pixbuf', renderer=>'Gtk3::CellRendererPixbuf', attr=>'pixbuf'},
58 );
59  
60 # this is some cool shit
61 sub add_column_type
62 {
63 shift; # don't want/need classname
64 my $name = shift;
65 $column_types{$name} = { @_ };
66 }
67  
68 sub text_cell_edited {
69 my ($cell_renderer, $text_path, $new_text, $slist) = @_;
70 my $path = Gtk3::TreePath->new_from_string ($text_path);
71 my $model = $slist->get_model;
72 my $iter = $model->get_iter ($path);
73 # $model->set ($iter, $cell_renderer->{column}, $new_text);
74 $model->set ($iter, [$cell_renderer->{column}], [$new_text]);
75 }
76  
77 sub new {
78 croak "Usage: $_[0]\->new (title => type, ...)\n"
79 . " expecting a list of column title and type name pairs.\n"
80 . " can't create a SimpleList with no columns"
81 unless @_ >= 3; # class, key1, val1
82 return shift->new_from_treeview (Gtk3::TreeView->new (), @_);
83 }
84  
85 sub new_from_treeview {
86 my $class = shift;
87 my $view = shift;
88 croak "treeview is not a Gtk3::TreeView"
89 unless defined ($view)
90 and UNIVERSAL::isa ($view, 'Gtk3::TreeView');
91 croak "Usage: $class\->new_from_treeview (treeview, title => type, ...)\n"
92 . " expecting a treeview reference and list of column title and type name pairs.\n"
93 . " can't create a SimpleList with no columns"
94 unless @_ >= 2; # key1, val1
95 my @column_info = ();
96 for (my $i = 0; $i < @_ ; $i+=2) {
97 my $typekey = $_[$i+1];
98 croak "expecting pairs of title=>type"
99 unless $typekey;
100 croak "unknown column type $typekey, use one of "
101 . join(", ", keys %column_types)
102 unless exists $column_types{$typekey};
103 my $type = $column_types{$typekey}{type};
104 if (not defined $type) {
105 $type = 'Glib::String';
106 carp "column type $typekey has no type field; did you"
107 . " create a custom column type incorrectly?\n"
108 . "limping along with $type";
109 }
110 push @column_info, {
111 title => $_[$i],
112 type => $type,
113 rtype => $column_types{$_[$i+1]}{renderer},
114 attr => $column_types{$_[$i+1]}{attr},
115 };
116 }
117 # my $model = Gtk3::ListStore->new (map { $_->{type} } @column_info);
118 my $model = Gtk3::ListStore->new ([map { $_->{type} } @column_info]);
119 # just in case, 'cause i'm paranoid like that.
120 map { $view->remove_column ($_) } $view->get_columns;
121 $view->set_model ($model);
122 for (my $i = 0; $i < @column_info ; $i++) {
123 if( 'CODE' eq ref $column_info[$i]{attr} )
124 {
125 $view->insert_column_with_data_func (-1,
126 $column_info[$i]{title},
127 $column_info[$i]{rtype}->new,
128 $column_info[$i]{attr}, $i);
129 }
130 elsif ('hidden' eq $column_info[$i]{attr})
131 {
132 # skip hidden column
133 }
134 else
135 {
136 my $column = Gtk3::TreeViewColumn->new_with_attributes (
137 $column_info[$i]{title},
138 $column_info[$i]{rtype}->new,
139 $column_info[$i]{attr} => $i,
140 );
141 $view->append_column ($column);
142  
143 if ($column_info[$i]{attr} eq 'active') {
144 # make boolean columns respond to editing.
145 my $r = $column->get_cells;
146 $r->set (activatable => 1);
147 $r->{column_index} = $i;
148 $r->signal_connect (toggled => sub {
149 my ($renderer, $row, $slist) = @_;
150 my $col = $renderer->{column_index};
151 my $model = $slist->get_model;
152 my $iter = $model->iter_nth_child (undef, $row);
153 my $val = $model->get ($iter, $col);
154 # $model->set ($iter, $col, !$val);
155 $model->set ($iter, [$col], [!$val]);
156 }, $view);
157  
158 } elsif ($column_info[$i]{attr} eq 'text') {
159 # attach a decent 'edited' callback to any
160 # columns using a text renderer. we do NOT
161 # turn on editing by default.
162 my $r = $column->get_cells;
163 $r->{column} = $i;
164 $r->signal_connect
165 (edited => \&text_cell_edited, $view);
166 }
167 }
168 }
169  
170 my @a;
171 tie @a, 'Games::Axmud::Obj::SimpleList::TiedList', $model;
172  
173 $view->{data} = \@a;
174 # return bless $view, $class;
175 bless $view, $class;
176  
177 # In Axmud simple lists, the first item is sometimes automatically selected, and sometimes not.
178 # This is not what happened in Gtk2, so the Axmud code doesn't expect it
179 # This down-and-dirty hack solves the problem by clearing a row that's auto-selected, while
180 # still allowing the user to select a row by clicking on it
181 my $flag;
182 $view->add_events(['pointer-motion-mask']);
183 $view->signal_connect ('motion-notify-event' => sub { $flag = 1; });
184 $view->signal_connect('cursor-changed' => sub {
185  
186 if (! $flag) {
187  
188 $view->get_selection->unselect_all();
189 $flag = 1;
190 }
191 });
192  
193 return $view;
194 }
195  
196 sub set_column_editable {
197 my ($self, $index, $editable) = @_;
198 my $column = $self->get_column ($index);
199 croak "invalid column index $index"
200 unless defined $column;
201 my $cell_renderer = $column->get_cells;
202 $cell_renderer->set (editable => $editable);
203 }
204  
205 sub get_column_editable {
206 my ($self, $index, $editable) = @_;
207 my $column = $self->get_column ($index);
208 croak "invalid column index $index"
209 unless defined $column;
210 my $cell_renderer = $column->get_cells;
211 return $cell_renderer->get ('editable');
212 }
213  
214 sub get_selected_indices {
215 my $self = shift;
216 my $selection = $self->get_selection;
217 return () unless $selection;
218 # warning: this assumes that the TreeModel is actually a ListStore.
219 # if the model is a TreeStore, get_indices will return more than one
220 # index, which tells you how to get all the way down into the tree,
221 # but all the indices will be squashed into one array... so, ah,
222 # don't use this for TreeStores!
223 my ($indices) = $selection->get_selected_rows;
224 # use Data::Dumper; warn Dumper $indices;
225 map { $_->get_indices } @$indices;
226 }
227  
228 sub select {
229 my $self = shift;
230 my $selection = $self->get_selection;
231 my @inds = (@_ > 1 && $selection->get_mode ne 'multiple')
232 ? $_[0]
233 : @_;
234 my $model = $self->get_model;
235 foreach my $i (@inds) {
236 my $iter = $model->iter_nth_child (undef, $i);
237 next unless $iter;
238 $selection->select_iter ($iter);
239 }
240 }
241  
242 sub unselect {
243 my $self = shift;
244 my $selection = $self->get_selection;
245 my @inds = (@_ > 1 && $selection->get_mode ne 'multiple')
246 ? $_[0]
247 : @_;
248 my $model = $self->get_model;
249 foreach my $i (@inds) {
250 my $iter = $model->iter_nth_child (undef, $i);
251 next unless $iter;
252 $selection->unselect_iter ($iter);
253 }
254 }
255  
256 sub set_data_array
257 {
258 @{$_[0]->{data}} = @{$_[1]};
259 }
260  
261 sub get_row_data_from_path
262 {
263 my ($self, $path) = @_;
264  
265 # $path->get_depth always 1 for SimpleList
266 # my $depth = $path->get_depth;
267  
268 # array has only one member for SimpleList
269 my @indices = $path->get_indices;
270 my $index = $indices[0];
271  
272 return $self->{data}->[$index];
273 }
274  
275 ##################################
276 package Games::Axmud::Obj::SimpleList::TiedRow;
277  
278 use strict;
279 use Gtk3;
280 use Carp;
281  
282 # TiedRow is the lowest-level tie, allowing you to treat a row as an array
283 # of column data.
284  
285 sub TIEARRAY {
286 my $class = shift;
287 my $model = shift;
288 my $iter = shift;
289  
290 croak "usage tie (\@ary, 'class', model, iter)"
291 unless $model && UNIVERSAL::isa ($model, 'Gtk3::TreeModel');
292  
293 return bless {
294 model => $model,
295 iter => $iter,
296 }, $class;
297 }
298  
299 sub FETCH { # this, index
300 return $_[0]->{model}->get ($_[0]->{iter}, $_[1]);
301 }
302  
303 sub STORE { # this, index, value
304 # return $_[0]->{model}->set ($_[0]->{iter}, $_[1], $_[2])
305 return $_[0]->{model}->set ($_[0]->{iter}, [$_[1]], [$_[2]])
306 if defined $_[2]; # allow 0, but not undef
307 }
308  
309 sub FETCHSIZE { # this
310 return $_[0]{model}->get_n_columns;
311 }
312  
313 sub EXISTS {
314 return( $_[1] < $_[0]{model}->get_n_columns );
315 }
316  
317 sub EXTEND { } # can't change the length, ignore
318 sub CLEAR { } # can't change the length, ignore
319  
320 sub new {
321 my ($class, $model, $iter) = @_;
322 my @a;
323 tie @a, __PACKAGE__, $model, $iter;
324 return \@a;
325 }
326  
327 sub POP { croak "pop called on a TiedRow, but you can't change its size"; }
328 sub PUSH { croak "push called on a TiedRow, but you can't change its size"; }
329 sub SHIFT { croak "shift called on a TiedRow, but you can't change its size"; }
330 sub UNSHIFT { croak "unshift called on a TiedRow, but you can't change its size"; }
331 sub SPLICE { croak "splice called on a TiedRow, but you can't change its size"; }
332 #sub DELETE { croak "delete called on a TiedRow, but you can't change its size"; }
333 sub STORESIZE { carp "STORESIZE operation not supported"; }
334  
335  
336 ###################################
337 package Games::Axmud::Obj::SimpleList::TiedList;
338  
339 use strict;
340 use Gtk3;
341 use Carp;
342  
343 # TiedList is an array in which each element is a row in the liststore.
344  
345 sub TIEARRAY {
346 my $class = shift;
347 my $model = shift;
348  
349 croak "usage tie (\@ary, 'class', model)"
350 unless $model && UNIVERSAL::isa ($model, 'Gtk3::TreeModel');
351  
352 return bless {
353 model => $model,
354 }, $class;
355 }
356  
357 sub FETCH { # this, index
358 my $iter = $_[0]->{model}->iter_nth_child (undef, $_[1]);
359 return undef unless defined $iter;
360 my @row;
361 tie @row, 'Games::Axmud::Obj::SimpleList::TiedRow', $_[0]->{model}, $iter;
362 return \@row;
363 }
364  
365 sub STORE { # this, index, value
366 my $iter = $_[0]->{model}->iter_nth_child (undef, $_[1]);
367 $iter = $_[0]->{model}->insert ($_[1])
368 if not defined $iter;
369 my @row;
370 tie @row, 'Games::Axmud::Obj::SimpleList::TiedRow', $_[0]->{model}, $iter;
371 if ('ARRAY' eq ref $_[2]) {
372 @row = @{$_[2]};
373 } else {
374 $row[0] = $_[2];
375 }
376  
377 return $_[2];
378 }
379  
380 sub FETCHSIZE { # this
381 return $_[0]->{model}->iter_n_children (undef);
382 }
383  
384 sub PUSH { # this, list
385 my $model = shift()->{model};
386 my $iter;
387 foreach (@_)
388 {
389 $iter = $model->append;
390 my @row;
391 tie @row, 'Games::Axmud::Obj::SimpleList::TiedRow', $model, $iter;
392 if ('ARRAY' eq ref $_) {
393 @row = @$_;
394 } else {
395 $row[0] = $_;
396 }
397 }
398 return $model->iter_n_children (undef);
399 }
400  
401 sub POP { # this
402 my $model = $_[0]->{model};
403 my $index = $model->iter_n_children-1;
404 my $iter = $model->iter_nth_child(undef, $index);
405 return undef unless $iter;
406 my $ret = [ $model->get ($iter) ];
407 $model->remove($iter) if( $index >= 0 );
408 return $ret;
409 }
410  
411 sub SHIFT { # this
412 my $model = $_[0]->{model};
413 my $iter = $model->iter_nth_child(undef, 0);
414 return undef unless $iter;
415 my $ret = [ $model->get ($iter) ];
416 $model->remove($iter) if( $model->iter_n_children );
417 return $ret;
418 }
419  
420 sub UNSHIFT { # this, list
421 my $model = shift()->{model};
422 my $iter;
423 foreach (@_)
424 {
425 $iter = $model->prepend;
426 my @row;
427 tie @row, 'Games::Axmud::Obj::SimpleList::TiedRow', $model, $iter;
428 if ('ARRAY' eq ref $_) {
429 @row = @$_;
430 } else {
431 $row[0] = $_;
432 }
433 }
434 return $model->iter_n_children (undef);
435 }
436  
437 # note: really, arrays aren't supposed to support the delete operator this
438 # way, but we don't want to break existing code.
439 sub DELETE { # this, key
440 my $model = $_[0]->{model};
441 my $ret;
442 if ($_[1] < $model->iter_n_children (undef)) {
443 my $iter = $model->iter_nth_child (undef, $_[1]);
444 return undef unless $iter;
445 $ret = [ $model->get ($iter) ];
446 $model->remove ($iter);
447 }
448 return $ret;
449 }
450  
451 sub CLEAR { # this
452 $_[0]->{model}->clear;
453 }
454  
455 # note: arrays aren't supposed to support exists, either.
456 sub EXISTS { # this, key
457 return( $_[1] < $_[0]->{model}->iter_n_children );
458 }
459  
460 # we can't really, reasonably, extend the tree store in one go, it will be
461 # extend as items are added
462 sub EXTEND {}
463  
464 sub get_model {
465 return $_[0]{model};
466 }
467  
468 sub STORESIZE { carp "STORESIZE: operation not supported"; }
469  
470 sub SPLICE { # this, offset, length, list
471 my $self = shift;
472 # get the model and the number of rows
473 my $model = $self->{model};
474 # get the offset
475 my $offset = shift || 0;
476 # if offset is neg, invert it
477 $offset = $model->iter_n_children (undef) + $offset if ($offset < 0);
478 # get the number of elements to remove
479 my $length = shift;
480 # if len was undef, not just false, calculate it
481 $length = $self->FETCHSIZE() - $offset unless (defined ($length));
482 # get any elements we need to insert into their place
483 my @list = @_;
484  
485 # place to store any returns
486 my @ret = ();
487  
488 # remove the desired elements
489 my $ret;
490 for (my $i = $offset; $i < $offset+$length; $i++)
491 {
492 # things will be shifting forward, so always delete at offset
493 $ret = $self->DELETE ($offset);
494 push @ret, $ret if defined $ret;
495 }
496  
497 # insert the passed list at offset in reverse order, so the will
498 # be in the correct order
499 foreach (reverse @list)
500 {
501 # insert a new row
502 $model->insert ($offset);
503 # and put the data in it
504 $self->STORE ($offset, $_);
505 }
506  
507 # return deleted rows in array context, the last row otherwise
508 # if nothing deleted return empty
509 return (@ret ? (wantarray ? @ret : $ret[-1]) : ());
510 }
511  
512 1;
513