use Gtk;
use Tie::Array;
use strict;

=head1 NAME

Gtk::CListModel - A simple data model with Gtk::Clist views

=head1 SINOPSYS

	my $model = tie @data, 'Gtk::CListModel', 
		titles => ["Fruit", "Price", "Quantity"];
	# all data manipulation is done on @data now
	push @data, ["Oranges", 5, 16];
	# Create a view (a Gtk::Clist widget) to represent the data
	# Include only some of the data in the view (fruit type and price)
	# Also, do not include fruits that cost more than 6 price units.
	my $clist = $model->create_view('main', 
		titles => ['Fruit', 'Price'],
		filter => sub {$_[1] > 6? () : @_});
	
=head1 DESCRIPTION

Gtk::CListModel lets you keep your data in a perl array and easily create
a numer of different views on that data using Gtk::CList widgets.
The views can show only some of the columns, or a subset of the data or
even munge the data with user-defined filters.

All the data manipulations will be performed on a tied array and the changes
will be propagated to the views created for that data.

To create the model use C<tie>:

	my $model = tie @data, 'Gtk::CListModel', 
		titles => ["head1", "head2",...];

The C<titles> attribute should be an array reference with the titles of the
columns of data. They will be used also for the default titles in the views.

You can also provide the initial data using the C<data> attribute. Remember
that the data elements you insert and retreive from the @data array are
array references with as many items as the columns in the model. The order
is the one defined by the C<titles> attribute.

Later you can manipulate the @data array with the usual perl array operators, push, 
splice and so on.

=head1 METHODS

=over 2

=cut

package Gtk::CListModel;

@Gtk::CListModel::ISA = qw(Tie::Array);

sub TIEARRAY {
	my $class = shift;
	my $self = {@_};
	$self->{data} = [] unless $self->{data};
	die "Need titles\n" unless $self->{titles};
	return bless $self, $class;
}

sub FETCH {
	my ($self, $index) = @_;
	my $result = $self->{data}->[$index];
	# warn "FETCH($index) -> @$result\n";
	return $result;
}

sub FETCHSIZE {
	my $self = shift;
	return scalar(@{$self->{data}});
}

sub STORE {
	my ($self, $index, $data) = @_;
	my $result = $self->{data}[$index] = $data;
	# warn "STORE($index) -> @$result\n";
	foreach my $v (values %{$self->{views}}) {
		my $view = $v->[0];
		my @d = @$result;
		my @map = @{$v->[2]};
		my $i = 0;
		my $rindex = $index;
		if (exists $view->{_filter}) {
			@d = $view->{_filter}->(@d[@map]);
		} else {
			@d = @d[@map];
		}
		for ($i=0; $i < $view->rows; ++$i) {
			last if $index == $view->get_text($i, $view->{_hcol});
		}
		# warn "row $index remapped to clist row $i (total rows: ".$view->rows.")\n";
		$rindex = $i;
		if ($rindex >= $view->rows) { # not found: append
			# warn "append instead to $index\n";
			next unless @d;
			my $r = $view->append(@d, $index);
			if (exists $view->{_postfilter}) {
				$view->{_postfilter}->($view, $r, @d);
			}
			next;
		}
		$view->remove($rindex) unless @d;
		# warn "set on index $index\n";
		$i = 0;
		foreach my $d (@d) {
			$view->set_text($rindex, $i++, $d);
		}
		if (exists $view->{_postfilter}) {
			$view->{_postfilter}->($view, $rindex, @d);
		}
	}
	return $result;
}

sub STORESIZE {
	my $self = shift;
	my $count = shift;
	my $result = $#{$self->{data}} = $count-1;
	# warn "REMOVE $count\n";
	foreach my $v (values %{$self->{views}}) {
		my $view = $v->[0];
		$view->clear(), next unless $count;
		my ($i, $todo);
		$view->freeze;
		$todo = $view->rows - $count;
		# FIXME: optimize and handle autosort
		for ($i=0; $todo && $i < $view->rows;) {
			if ($view->get_text($i, $view->{_hcol}) >= $count) {
				$view->remove($i);
				$todo--;
			} else {
				$i++;
			}
		}
		$view->thaw;
	}
}

=item create_view ($name[, %options])

Create a Gtk::Clist widget that represents the data in the model.
The name can be used later to disconnect the view from the data.

Options can be one of the following:

=over 2

=item * titles

An array reference of the titles of the columns to display in the list
in the order they should appear in the view. The default is the titles
specified at the model creation.

=item * filter

A function that can manipulate the data just before it is inserted
in the Gtk::CList. The function will receive the data and can either
make a copy and modify the data or return an empty list. In the latter case
the data will not be added to the view or, if the corresponding row was
already present, it will be removed from the view.

=item * postfilter

A function that receives the view, the row and the data that was
just inserted/modified in the view. By default all the data is inserted
in the views as text. This filter can be used to display pixmaps, for
example or do any other kind of manipulations on the Gtk::CList row.

=back

=cut

sub create_view {
	my ($self, $name, %opts) = @_;
	my ($clist, @data, @map, @rt, $hcol, $i, @titles);
	@titles = @{$opts{titles}} if exists $opts{titles};
	@titles = @{$self->{titles}} unless @titles;
	$clist = new_with_titles Gtk::CList (@titles, '_hidden');
	$hcol = scalar(@titles);
	$clist->set_column_visibility($hcol, 0);
	$clist->set_name($name);
	$clist->{_hcol} = $hcol;
	$clist->{_filter} = $opts{filter} if exists $opts{filter};
	$clist->{_postfilter} = $opts{postfilter} if exists $opts{postfilter};

	@data = @{$self->{data}};
	# maps column names to indexes
	@rt = @{$self->{titles}};
	foreach my $t (@titles) {
		for ($i=0; $i < @rt; ++$i) {
			push (@map, $i),next if $t eq $rt[$i];
		}
	}
	$i = 0;
	foreach my $d (@data) {
		my @d = @$d;
		if (exists $clist->{_filter}) {
			@d = $clist->{_filter}->(@d[@map]);
		} else {
			@d = @d[@map];
		}
		$i++,next unless @d;
		my $r = $clist->append(@d, $i);
		if (exists $clist->{_postfilter}) {
			$clist->{_postfilter}->($clist, $r, @d);
		}
		$i++;
	}
	$self->{views}->{$name} = [$clist, [@titles], [@map]];
	return $clist;
}

=item remove_view ($name)

Disconnect the named view from the data. The current data displayed in the
view will not be affected, but changes in the model will not
propagate to this view anymore.

=cut

sub remove_view {
	my ($self, $name) = @_;
	$name = $name->get_name if ref $name; # can be the widget itself
	delete $self->{views}->{$name};
}

=item map_row ($clist, $row)

Get the index in the data array cooresponding to the row
displayed in the Gtk::CList widget.

=cut

sub map_row {
	my ($self, $clist, $row) = @_;
	return $clist->get_text($row, $clist->{_hcol});
}

=pod

=back

=head1 AUTHOR

Molaro Paolo lupus@debian.org

=cut

package main;

init Gtk;

my $mw = new Gtk::Window;
$mw->signal_connect('destroy', sub {Gtk->main_quit});
my @data;

my $d = tie @data, 'Gtk::CListModel', titles => ["Fruit", "Price", "Quantity"];
my $clist = $d->create_view('main', postfilter => \&quantity_pixmap,);
my $clist2 = $d->create_view('customer', 
	titles => ['Fruit', 'Price'],
	# increase the price for the customer
	filter => sub {my @c=@_;$c[1]*=1.3;@c},
	);
my $clist3 = $d->create_view('products', 
	titles => ['Fruit', 'Quantity'],
	filter => sub {$_[1] <= 15?@_:()});

my $table = new Gtk::Table(2, 2);
$table->attach_defaults (new Gtk::Label("Sortable"), 0, 1, 0, 1);
$table->attach_defaults ($clist, 0, 1, 1, 2);
$table->attach_defaults (new Gtk::Label("Customer view"), 1, 2, 0, 1);
$table->attach_defaults ($clist2, 1, 2, 1, 2);
$table->attach_defaults (new Gtk::Label("Low store"), 2, 3, 0, 1);
$table->attach_defaults ($clist3, 2, 3, 1, 2);

$mw->add($table);

$clist->signal_connect('button-press-event', sub {
	my ($w, $e) = @_;
	my ($row, $col) = $w->get_selection_info($e->{x}, $e->{y});;
	# NOTE: sorting requires a mapping from $row to index in data
	my $realrow = $d->map_row($w, $row);
	# warn "ROW MAP: $row -> $realrow\n";
	$row = $realrow;
	my $d = $data[$row];
	if ($e->{button} == 1) {
		# decrement quantity in store
		$d->[2]--;
		$data[$row] = $d;
	} elsif ($e->{button} == 2) {
		# remove
		splice (@data, $row, 1);
	} else {
		# add
		push (@data, ["Nuts", 12, 34]);
	}
	#check_low($clist3, 15);
	#$w->signal_emit_stop_by_name('button-press-event');
	return 1;
});

$clist->signal_connect('click-column', sub {
	my ($w, $c) = @_;
	$w->set_sort_column($c);
	$w->sort;
});
push @data, ["Apples", 3, 58];
push @data, ["Oranges", 5, 16];

#check_low($clist3, 15);
$mw->show_all;

main Gtk;

sub quantity_pixmap {
	my ($clist, $row, @d) = @_;
	my ($pix, $mask, $color, $gc, $gcm, $v, $w);
	# this is slow, but it demonstrates using a pixmap
	$v = $d[2];
	$clist->realize;
	$pix = Gtk::Gdk::Pixmap->new($clist->window, 50, 10);
	$mask = Gtk::Gdk::Pixmap->new($clist->window, 50, 10, 1);
	$gc = new Gtk::Gdk::GC $clist->window;
	$gcm = new Gtk::Gdk::GC $mask;
	$color = $clist->get_colormap->color_alloc({red => $v<20?65000:0, blue => 0, green => $v>15?45000:0});
	$gc->set_foreground($color);
	$gcm->set_foreground({pixel=>1}); # hackety hack
	$pix->draw_rectangle($clist->style->white_gc, 1, 0, 0, -1, -1);
	$pix->draw_rectangle($gc, 1, 0, 0, $v/2, -1);
	$mask->draw_rectangle($gcm, 1, 0, 0, -1, -1);
	$pix->draw_string($clist->style->font, $clist->style->black_gc, 5, 10, $v);
	$clist->set_pixmap($row, 2, $pix, $mask);
}
