use Gtk2 '-init';
use Gtk2::Ex::Simple::List;
use Glib qw /TRUE FALSE/;
use Data::Dumper;

# Declare a simple list
my $slist  = Gtk2::Ex::Simple::List->new ('Mylist' => 'text',);
my @listdata;
while(<DATA>) {
	chomp;
	push @listdata, $_;
}

# Create an index for sorting later on
my $i = 0;
my %hash = map { $_ => $i++ } @listdata;

# All the records that don't match the pattern will go in here
my @REJECTS;

# Populate the list
@{$slist->{data}} = @listdata;

my $buffer = Gtk2::TextBuffer->new;
my $text = Gtk2::TextView->new_with_buffer ($buffer);

my $CHAR_COUNT;
$buffer->signal_connect( 'changed' => sub {
	my ($self) = @_;
	my $string = $self->get_text($self->get_start_iter, $self->get_end_iter,TRUE);
	if ($self->get_char_count > $CHAR_COUNT) { # Additional chars. So filter more.
		$CHAR_COUNT = $self->get_char_count;
		filter_list($string);
	} else { # Backspace. Remove the previous filter
		$CHAR_COUNT--;
		push @{$slist->{data}}, @{pop @REJECTS};
		# sort the list according to the original order
		@{$slist->{data}} =  map { $_->[0] } sort { $hash{$a->[0]} <=> $hash{$b->[0]} } @{$slist->{data}};
	}
});

# Pack the widgets and display
my $scroll = Gtk2::ScrolledWindow->new;
$scroll->set_policy('never','automatic');
$scroll->add($slist);
my $vbox = Gtk2::VBox->new(FALSE);
$vbox->pack_start($text, FALSE, TRUE, 0);
$vbox->pack_start($scroll, TRUE, TRUE, 0);
my $window = Gtk2::Window->new;
$window->signal_connect(destroy => sub { Gtk2->main_quit; });
$window->add($vbox);
$window->set_default_size(400,350);
$window->show_all;
Gtk2->main;

sub filter_list {
	my ($string) = @_;
	my @rejected_here;
	for (my $i=$#{@{$slist->{data}}}; $i>=0; $i--) {		
		next if $slist->{data}->[$i]->[0] =~ /$string/i;
		push @rejected_here, splice @{$slist->{data}}, $i, 1;
	}
	push @REJECTS, \@rejected_here;
}

__DATA__
Of Mans First Disobedience, and the Fruit
Of that Forbidden Tree, whose mortal tast
Brought Death into the World, and all our woe,
With loss of EDEN, till one greater Man
Restore us, and regain the blissful Seat,
Sing Heav'nly Muse, that on the secret top
Of OREB, or of SINAI, didst inspire
That Shepherd, who first taught the chosen Seed,
In the Beginning how the Heav'ns and Earth
Rose out of CHAOS: Or if SION Hill
Delight thee more, and SILOA'S Brook that flow'd
Fast by the Oracle of God; I thence
Invoke thy aid to my adventrous Song,
That with no middle flight intends to soar
Above th' AONIAN Mount, while it pursues
Things unattempted yet in Prose or Rhime.
And chiefly Thou O Spirit, that dost prefer
Before all Temples th' upright heart and pure,
Instruct me, for Thou know'st; Thou from the first
Wast present, and with mighty wings outspread
Dove-like satst brooding on the vast Abyss
And mad'st it pregnant: What in me is dark
Illumine, what is low raise and support;
That to the highth of this great Argument
I may assert th' Eternal Providence,
And justifie the wayes of God to men.
 Say first, for Heav'n hides nothing from thy view
Nor the deep Tract of Hell, say first what cause
Mov'd our Grand Parents in that happy State,
Favour'd of Heav'n so highly, to fall off
From their Creator, and transgress his Will
For one restraint, Lords of the World besides?
Who first seduc'd them to that fowl revolt?
Th' infernal Serpent; he it was, whose guile
Stird up with Envy and Revenge, deceiv'd
The Mother of Mankinde, what time his Pride
Had cast him out from Heav'n, with all his Host
Of Rebel Angels, by whose aid aspiring
To set himself in Glory above his Peers,
He trusted to have equal'd the most High,
If he oppos'd; and with ambitious aim
Against the Throne and Monarchy of God
Rais'd impious War in Heav'n and Battel proud
With vain attempt. Him the Almighty Power
Hurld headlong flaming from th' Ethereal Skie
With hideous ruine and combustion down
To bottomless perdition, there to dwell
