| 1 | package Gapp::Gtk2::Model::SimpleList; | ||
| 2 | { | ||
| 3 | $Gapp::Gtk2::Model::SimpleList::VERSION = '0.60'; | ||
| 4 | } | ||
| 5 | |||
| 6 | 0 | ▶ | use Glib qw(TRUE FALSE); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | use Gtk2; | ||
| 8 | use Carp; | ||
| 9 | use Data::Dumper; | ||
| 10 | use strict; | ||
| 11 | use warnings; | ||
| 12 | |||
| 13 | use Glib::Object::Subclass | ||
| 14 | Glib::Object::, | ||
| 15 | interfaces => [ Gtk2::TreeModel::, Gtk2::TreeDragDest:: ], | ||
| 16 | ; | ||
| 17 | |||
| 18 | sub INIT_INSTANCE { | ||
| 19 | my $self = shift; | ||
| 20 | $self->{n_columns} = 1; | ||
| 21 | $self->{column_types} = [ 'Glib::Scalar' ]; | ||
| 22 | $self->{rows} = []; | ||
| 23 | $self->{stamp} = sprintf '%d', rand (1<<31); | ||
| 24 | } | ||
| 25 | |||
| 26 | sub FINALIZE_INSTANCE { | ||
| 27 | my $self = shift; | ||
| 28 | $self->{rows} = undef; | ||
| 29 | $self->{stamp} = undef; | ||
| 30 | $self->{column_types} = undef; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub GET_FLAGS { [qw/list-only/] } | ||
| 34 | sub GET_N_COLUMNS { shift->{n_columns}; } | ||
| 35 | |||
| 36 | sub GET_COLUMN_TYPE { | ||
| 37 | my ($self, $index) = @_; | ||
| 38 | return $self->{column_types}[$index]; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub GET_ITER { | ||
| 42 | my ($self, $path) = @_; | ||
| 43 | |||
| 44 | die "no path" unless $path; | ||
| 45 | |||
| 46 | my @indices = $path->get_indices; | ||
| 47 | my $depth = $path->get_depth; | ||
| 48 | |||
| 49 | # no children, this is a list | ||
| 50 | die "depth != 1" unless $depth == 1; | ||
| 51 | |||
| 52 | my $n = $indices[0]; | ||
| 53 | return undef if $n >= @{$self->{rows}} || $n < 0; | ||
| 54 | |||
| 55 | my $record = $self->{rows}[$n]; | ||
| 56 | |||
| 57 | die "no record" unless $record; | ||
| 58 | die "bad record" unless $record->{pos} == $n; | ||
| 59 | |||
| 60 | return [ $self->{stamp}, $n, $record, undef ]; | ||
| 61 | } | ||
| 62 | |||
| 63 | |||
| 64 | sub GET_PATH { | ||
| 65 | my ($self, $iter) = @_; | ||
| 66 | die "no iter" unless $iter; | ||
| 67 | |||
| 68 | my $record = $iter->[2]; | ||
| 69 | |||
| 70 | my $path = Gtk2::TreePath->new; | ||
| 71 | $path->append_index ($record->{pos}); | ||
| 72 | return $path; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub GET_VALUE { | ||
| 76 | my ($self, $iter, $column) = @_; | ||
| 77 | |||
| 78 | die "bad iter" unless $iter; | ||
| 79 | |||
| 80 | my $record = $iter->[2]; | ||
| 81 | |||
| 82 | return undef unless $record; | ||
| 83 | |||
| 84 | die "bad iter" if $record->{pos} >= @{$self->{rows}}; | ||
| 85 | |||
| 86 | return $record->{value}; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub ITER_NEXT { | ||
| 90 | my ($self, $iter) = @_; | ||
| 91 | |||
| 92 | return undef unless $iter && $iter->[2]; | ||
| 93 | |||
| 94 | my $record = $iter->[2]; | ||
| 95 | |||
| 96 | # Is this the last record in the list? | ||
| 97 | return undef if $record->{pos} >= @{ $self->{rows} }; | ||
| 98 | |||
| 99 | my $nextrecord = $self->{rows}[$record->{pos} + 1]; | ||
| 100 | |||
| 101 | return undef unless $nextrecord; | ||
| 102 | die "invalid record" unless $nextrecord->{pos} == ($record->{pos} + 1); | ||
| 103 | |||
| 104 | return [ $self->{stamp}, $nextrecord->{pos}, $nextrecord, undef ]; | ||
| 105 | } | ||
| 106 | |||
| 107 | sub ITER_CHILDREN { | ||
| 108 | my ($self, $parent) = @_; | ||
| 109 | return undef if $parent; # this is a list, nodes have no children | ||
| 110 | return undef unless @{ $self->{rows} }; # No rows => no first row | ||
| 111 | return [ $self->{stamp}, 0, $self->{rows}[0] ]; # Set iter to first item in list | ||
| 112 | } | ||
| 113 | |||
| 114 | |||
| 115 | sub ITER_HAS_CHILD { FALSE } | ||
| 116 | |||
| 117 | |||
| 118 | sub ITER_N_CHILDREN { | ||
| 119 | my ($self, $iter) = @_; | ||
| 120 | # special case: if iter == NULL, return number of top-level rows | ||
| 121 | return scalar @{$self->{rows}} if ! $iter; | ||
| 122 | |||
| 123 | return 0; # otherwise, this is easy again for a list | ||
| 124 | } | ||
| 125 | |||
| 126 | |||
| 127 | sub ITER_NTH_CHILD { | ||
| 128 | my ($self, $parent, $n) = @_; | ||
| 129 | |||
| 130 | # a list has only top-level rows | ||
| 131 | return undef if $parent; | ||
| 132 | |||
| 133 | # special case: if parent == NULL, set iter to n-th top-level row | ||
| 134 | |||
| 135 | return undef if ! defined $self->{rows} || $n >= @{$self->{rows}}; | ||
| 136 | |||
| 137 | my $record = $self->{rows}[$n]; | ||
| 138 | |||
| 139 | die "no record" unless $record; | ||
| 140 | die "bad record" unless $record->{pos} == $n; | ||
| 141 | |||
| 142 | return [ $self->{stamp}, $n, $record ]; | ||
| 143 | } | ||
| 144 | |||
| 145 | |||
| 146 | sub ITER_PARENT { FALSE } | ||
| 147 | |||
| 148 | |||
| 149 | |||
| 150 | sub append { | ||
| 151 | my ( $self, $object ) = @_; | ||
| 152 | |||
| 153 | my $record = { value => $object }; | ||
| 154 | |||
| 155 | push @{ $self->{rows} }, $record; | ||
| 156 | $record->{pos} = @{$self->{rows}} - 1; | ||
| 157 | |||
| 158 | my $path = Gtk2::TreePath->new; | ||
| 159 | $path->append_index( $record->{pos} ); | ||
| 160 | |||
| 161 | my $iter = $self->get_iter( $path ); | ||
| 162 | $self->row_inserted( $path, $iter ); | ||
| 163 | return $self->get_iter( $path ); | ||
| 164 | } | ||
| 165 | |||
| 166 | |||
| 167 | sub set { | ||
| 168 | my ( $self, $treeiter, $value ) = @_; | ||
| 169 | |||
| 170 | my $iter = $treeiter->to_arrayref($self->{stamp}); | ||
| 171 | |||
| 172 | my $record = $iter->[2]; | ||
| 173 | $record->{value} = $value; | ||
| 174 | |||
| 175 | $self->row_changed ($self->get_path ($treeiter), $treeiter); | ||
| 176 | } | ||
| 177 | |||
| 178 | sub clear { | ||
| 179 | my ( $self ) = @_; | ||
| 180 | |||
| 181 | while ( @{$self->{rows}} ) { | ||
| 182 | my $record = pop @{ $self->{rows} }; | ||
| 183 | |||
| 184 | my $path = Gtk2::TreePath->new; | ||
| 185 | $path->append_index( $record->{pos} ); | ||
| 186 | |||
| 187 | $self->row_deleted ( $path ); | ||
| 188 | } | ||
| 189 | |||
| 190 | } | ||
| 191 | |||
| 192 | |||
| 193 | sub remove { | ||
| 194 | my ( $self, $treeiter ) = @_; | ||
| 195 | |||
| 196 | my $iter = $treeiter->to_arrayref($self->{stamp}); | ||
| 197 | my $n = $iter->[1]; | ||
| 198 | |||
| 199 | # splice the record from the rows | ||
| 200 | my ( $record ) = splice @{$self->{rows}}, $n, 1; | ||
| 201 | |||
| 202 | # update the position field of affected records | ||
| 203 | for my $i ( $n..$#{$self->{rows}} ) { | ||
| 204 | $self->{rows}[$i]{pos} = $i; | ||
| 205 | } | ||
| 206 | |||
| 207 | # find the path and notify the model of the row removal | ||
| 208 | my $path = Gtk2::TreePath->new; | ||
| 209 | $path->append_index( $record->{pos} ); | ||
| 210 | $self->row_deleted ( $path ); | ||
| 211 | |||
| 212 | # update the iter to point new row | ||
| 213 | $iter->[0] = $self->{stamp}; | ||
| 214 | $iter->[2] = $self->{rows}[$n]; | ||
| 215 | |||
| 216 | return Gtk2::TreeIter->new_from_arrayref( $iter ); | ||
| 217 | } | ||
| 218 | |||
| 219 | |||
| 220 | |||
| 221 | |||
| 222 | 1; | ||
| 223 | |||
| 224 | |||
| 225 | |||
| 226 | __END__ | ||