#! /usr/bin/perl -w

use strict;
use Gtk2 '-init';
use Glib qw/TRUE FALSE/;

#Declare our columns
use constant C_MARKUP		=> 0;
use constant C_BACKGROUND	=> 1;
use constant C_STOCK		=> 2;
use constant C_TOGGLE		=> 3;

#the data for the model
my @data = (
  
{markup => '<b>This is Bold</b>',
 background => 'red',
 stock => 'gtk-open',
 toggle => TRUE},
 
{markup => '<span foreground="orange" size="15000"><i>Some Value</i></span>', 
background => undef,
stock => 'gtk-close',
toggle => FALSE	},

{ markup => '<span foreground="blue" size="30000"><u>Blue text</u></span>',
 background => 'green',
 stock => 'gtk-cdrom',
 toggle => TRUE},

);

#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->set_size_request (500, 150);

#get the model
my $store = &create_model;
	
	#create a treeview with the model.
	my $treeview = Gtk2::TreeView->new_with_model($store);
	
		#------------------------------------------------------------------
		#create a treeview column and set its title
		my $tv_clmn_pix_text = Gtk2::TreeViewColumn->new;
		$tv_clmn_pix_text->set_title("Pix and Markup");
			#pixbuf renderer
			my $ren_stock = Gtk2::CellRendererPixbuf->new();
		#pack it into the column
		$tv_clmn_pix_text->pack_start ($ren_stock, FALSE);
		#set its atributes
		$tv_clmn_pix_text->set_attributes($ren_stock,
		'stock-id' => C_STOCK,
		'cell-background'=> C_BACKGROUND,
		);
			#text renderer
			my $ren_text = Gtk2::CellRendererText->new();
		#pack it into the column	
		$tv_clmn_pix_text->pack_start ($ren_text, FALSE);
		#set its atributes		
		$tv_clmn_pix_text->set_attributes($ren_text,
		'markup' => C_MARKUP,
		'cell-background' => C_BACKGROUND,
		);
	#append this column to the treeview
	$treeview->append_column($tv_clmn_pix_text);
	
		#------------------------------------------------------------------
		#create a treeview column and set its title
		my $tv_clmn_toggle_text = Gtk2::TreeViewColumn->new;
		$tv_clmn_toggle_text->set_title("Toggle and Markup");
			#toggle renderer
			my $ren_toggle = Gtk2::CellRendererToggle->new();
			$ren_toggle->signal_connect('toggled' => \&toggled,$store);
		#pack it into the column	
		$tv_clmn_toggle_text->pack_start ($ren_toggle, FALSE);
		#set its atributes
		$tv_clmn_toggle_text->set_attributes($ren_toggle,
		'active' => C_TOGGLE,
		);
			#text renderer
			my $ren_tgl_text = Gtk2::CellRendererText->new();
		#pack it into the column	
		$tv_clmn_toggle_text->pack_start ($ren_tgl_text, FALSE);
		#set its atributes		
		$tv_clmn_toggle_text->set_attributes($ren_tgl_text,
		'markup' => C_MARKUP,
		);
	#append this column to the treeview	
	$treeview->append_column($tv_clmn_toggle_text);	
		
	#------------------------------------------------------------------		
$vbox->pack_start($treeview,TRUE,TRUE,0);
$vbox->show_all();
return $vbox;
}


sub create_model {
  # create list store
  my $list_store = Gtk2::ListStore->new(qw/Glib::String Glib::String Glib::String Glib::Boolean/);

  # add data to the list store
  foreach my $d (@data) {
      my $iter = $list_store->append;
      $list_store->set ($iter,
      		   C_MARKUP, $d->{markup},
		   C_BACKGROUND, $d->{background},
		   C_STOCK, $d->{stock},
		   C_TOGGLE, $d->{toggle},
      );
  }

  return $list_store;
}

sub toggled {
  my ($cell, $path_str, $model) = @_;
  
my $path = Gtk2::TreePath->new_from_string ($path_str);
 
	# get toggled iter
  	my $iter = $model->get_iter ($path);
  	my ($fixed) = $model->get ($iter, C_TOGGLE);

  # do something with the value
  $fixed =!$fixed;

# set new value
$model->set ($iter, C_TOGGLE, $fixed);
}