#! /usr/bin/perl -w

use strict;
use Gtk2 '-init';
use Glib qw/TRUE FALSE/; 
 
#standard window creation, placement, and signal connecting
my $window = Gtk2::Window->new('toplevel');
$window->signal_connect('delete_event' => sub { Gtk2->main_quit; });
$window->set_border_width(5);
$window->set_position('center_always');

#this vbox will geturn the bulk of the gui
my $vbox = &ret_vbox();

#add and show the vbox
$window->add($vbox);
$window->show();

#our main event-loop
Gtk2->main();


sub ret_vbox {

my $vbox = Gtk2::VBox->new(FALSE,5);

$vbox->pack_start(Gtk2::Label->new("Selection Mode:"),FALSE,FALSE,5);

	#------------------
	#get a list of Gtk2::SelectionModes and fill a combo box with it
	my $cmb_box = Gtk2::ComboBox->new_text;
	 
	foreach my $s_type (Glib::Type->list_values ('Gtk2::SelectionMode')){
	
		$cmb_box->append_text($s_type->{'nick'});
	}
	
	$cmb_box->set_active(0);
	#------------------
	
$vbox->pack_start($cmb_box,FALSE,FALSE,0);

$vbox->pack_start(Gtk2::HSeparator->new(),FALSE,FALSE,0);
		
	#this is one of the provided base Gtk2::TreeModel classes.
	my $tree_store = Gtk2::TreeStore->new(qw/Glib::String/);

	#fill it with arbitry data
	foreach (1..3) {
	
		my $parent_nr = $_;
		#the iter is a pointer in the treestore. We
		#use to add data.
                my $iter = $tree_store->append(undef);
		$tree_store->set ($iter,0 => "Parent $parent_nr");
		
                 foreach (1..3){
		 	#here we append child iters to the parent iter
			#and add data to those chils iters.
                      my $iter_child = $tree_store->append($iter);
		      $tree_store->set ($iter_child,0 => "Child $_ of Parent $parent_nr");
		}
	}
	
	#this will create a treeview, specify $tree_store as its model
	my $tree_view = Gtk2::TreeView->new($tree_store);
	
		#create a Gtk2::TreeViewColumn to add to $tree_view
		my $tree_column = Gtk2::TreeViewColumn->new();
		$tree_column->set_title ("Select Something");
			
			my $renderer = Gtk2::CellRendererText->new;
			
		$tree_column->pack_start ($renderer, FALSE);
		$tree_column->add_attribute($renderer, text => 0);
	
	#add $tree_column to the treeview
	$tree_view->append_column ($tree_column);
	
	$vbox->set_size_request (300, 300);
	
	
	#---------------------------------------------------
	#get the Gtk2::TreeSelection of $tree_view
	my $treeselection = $tree_view->get_selection;
	#set its initial mode
	$treeselection->set_mode($cmb_box->get_active_text);
	
	#connect the combo box's changed signal so that it will
	#change the mode of the Gtk2::TreeSelection on each change.
	$cmb_box->signal_connect('changed' => sub {
	
		$treeselection->set_mode($cmb_box->get_active_text);
	});
	#-----------------------------------------------------
	
$vbox->pack_start($tree_view,TRUE,TRUE,0);
$vbox->pack_start(Gtk2::HSeparator->new(),FALSE,FALSE,0);

	my $btn_show_sel = Gtk2::Button->new("_Show Selected");
	$btn_show_sel->signal_connect("clicked" => sub {
	
		my $treeselection = $tree_view->get_selection;
		
		$treeselection->selected_foreach (sub{ 
			#this sub wil receive the following:
			my ($model,$path,$iter) =@_;
			
			#we want data at the model's column 0  
			#where the iter is pointing
			my $value = $model->get($iter,0);
			print $value."\n";
			
			});	
	});
		
$vbox->pack_end($btn_show_sel,FALSE,FALSE,0);

$vbox->show_all();
return $vbox;
}

