#!/usr/bin/perl
#

use strict;
use warnings;

use constant TRUE	=> 1;
use constant FALSE	=> 0;

use Gtk2;

Gtk2->init;

my $w = Gtk2::Window->new('toplevel');
$w->signal_connect(delete_event => sub { $_[0]->destroy;  });
$w->signal_connect(destroy      => sub { Gtk2->main_quit; });

my $vbox = Gtk2::VBox->new(FALSE, 18);
$vbox->set_border_width(12);
$w->add($vbox);

my $sw = Gtk2::ScrolledWindow->new;
$sw->set_policy('automatic', 'automatic');
$sw->set_shadow_type('etched_in');

$vbox->pack_start($sw, 0, 0, 0);

my @data = (
	'A test', 'C Test', 'b Test', 'E test', 'd test', 'F TEST',
);

our ($model, $view, $renderer);
$model = Gtk2::ListStore->new('Glib::String');
foreach my $s (@data)
{
	print $s . "\n";
	my $iter = $model->append;
	$model->set($iter, 0, $s);
}

$view = Gtk2::TreeView->new($model);

$renderer = Gtk2::CellRendererText->new;
$view->insert_column_with_attributes(-1, "Sorted", $renderer, text => 0);

$model->set_sort_column_id(0, 'ascending');
$model->set_sort_func(0, sub {
		my ($model, $iter_a, $iter_b) = @_;

		my $a = uc($model->get($iter_a, 0));
		my $b = uc($model->get($iter_b, 0));

		return $a cmp $b;
	});

$sw->add($view);

my $label = Gtk2::Label->new('Current sorting order: ascending');
$vbox->pack_start($label, 0, 0, 0);

my $button = Gtk2::Button->new('Change Sorting Order');
$button->signal_connect(clicked => sub {
		my ($button, $model) = @_;
		
		my ($id, $order) = $model->get_sort_column_id;
		if    ($order eq 'ascending')
		{
			$model->set_sort_column_id(0, 'descending');
			$label->set_text('Current sorting order: descending');
		}
		elsif ($order eq 'descending')
		{
			$model->set_sort_column_id(0, 'ascending');
			$label->set_text('Current sorting order: ascending');
		}
		
	}, $model);

$vbox->pack_end($button, 0, 0, 0);

$w->show_all;

Gtk2->main;

0;
