#!/usr/bin/perl -w
use Gtk2 -init;

my $window = Gtk2::Window->new;
$window->signal_connect( delete_event => sub { Gtk2->main_quit; 1 } );

my $vbox = Gtk2::VBox->new;

my $model = Gtk2::ListStore->new( 'Glib::Int', 'Glib::String' );

my $no = 0;

foreach ( qw/never whistle while you're pissing/ ) {
	$model->set( $model->append, 0, $no, 1, $_ );
	$no ++;
}

my $combo = Gtk2::ComboBoxEntry->new;
$combo->set_model( $model );
$combo->set_text_column( 1 );
setup_autocompletion( $combo );

$vbox->add( $combo );

my $btn = Gtk2::Button->new_with_label( 'Change Model ' );
$btn->signal_connect( 'button-press-event', sub { change_model( @_ ) } );

$vbox->add( $btn );

$window->add( $vbox );

my $toggle_data = 0;

$window->show_all;

Gtk2->main;

sub change_model {
	print "changing model ...\n";
	my $model = Gtk2::ListStore->new( 'Glib::Int', 'Glib::String' );
	my $no = 0;
	if ( $toggle_data ) {
		foreach ( qw/a stitch in time saves nine/ ) {
			$model->set( $model->append, 0, $no, 1, $_ );
			$no ++;
		}
		print " ... adding data\n";
		$toggle_data = 0;
	} else {
		$toggle_data = 1;
		print " ... NOT adding data\n";
	}
	$combo->set_model( $model );
}

sub setup_autocompletion {
    
    my $widget = shift;
    
    my $text_column = shift || 1;
    
    my $entrycompletion = Gtk2::EntryCompletion->new();
    $entrycompletion->set_minimum_key_length(1);
    $entrycompletion->set_model($widget->get_model);
    $entrycompletion->set_text_column($text_column);
    $widget->get_child->set_completion($entrycompletion);
    
}
