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