#! /usr/bin/perl -w

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

#create the stock item
&create_stock_item;
 
#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 return 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 create_stock_item {
#this sub will add a stockitem:

# the stock id our stock item will be accessed with
my $stock_id = 'dylan';

# add a new entry to the stock system with our id
Gtk2::Stock->add ({
		stock_id => $stock_id,
		label    => '_Dylan - Stock image sizes',
		modifier => [],
		keyval   => $Gtk2::Gdk::Keysyms{D},
		translation_domain => 'gtk2_image',
	});
	
	# create an icon set, with only one member in this particular case
	my $icon_set = Gtk2::IconSet->new_from_pixbuf (
		Gtk2::Gdk::Pixbuf->new_from_file ("./pix/dylan.jpg"));
		
# create a new icon factory to handle rendering the image at various sizes...
my $icon_factory = Gtk2::IconFactory->new;
# add our new stock icon to it...
$icon_factory->add ($stock_id, $icon_set);
# and then add this custom icon factory to the list of default places in
# which to search for stock ids, so any gtk+ code can find our stock icon.
$icon_factory->add_default;

}


sub ret_vbox {

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

#the stock images
	my $frame = Gtk2::Frame->new();
		#to prove that a Gtk2::Frame can contain something else
		
	$frame->set_shadow_type ('out');
	#method of Gtk2::Container
	$frame->set_border_width(5);
	
		my $btn_dylan = Gtk2::Button->new_from_stock('dylan');
	
	$frame->set_label_widget ($btn_dylan);
	
		#create a hbox that will contain the various stock image sizes
		my $hbox_stock = Gtk2::HBox->new(FALSE,5);
		my @sizes =qw/menu small-toolbar large-toolbar button dnd dialog /;
		foreach my $size(@sizes){
		$hbox_stock->pack_start(Gtk2::Label->new($size.":"),FALSE,FALSE,0);
			my $img_stock = Gtk2::Image->new_from_stock('dylan',$size);	
		$hbox_stock->pack_start($img_stock,FALSE,FALSE,0);
		}	
	$frame->add($hbox_stock);
	
$vbox->pack_start($frame,FALSE,FALSE,0);

#scaling the image
	$frame = Gtk2::Frame->new('Fetching the image from a file, and scaling the original:');
	$frame->set_shadow_type ('out');
	#method of Gtk2::Container
	$frame->set_border_width(5);
	
		#create a hbox that will contain the original and scaled images
		my $hbox_scale = Gtk2::HBox->new(FALSE,5);
		
			my $lbl_type = Gtk2::Label->new("original\n(100x100):");
		$hbox_scale->pack_start($lbl_type,FALSE,FALSE,0);
			
	#________
	#the Gtk2::Gdk::PixbufLoader is used in
	# the acquisition of images in "raw" format.
	#This will typically be data from a database
			my $pixbufloader = Gtk2::Gdk::PixbufLoader->new;
				my $raw_data = `cat ./pix/dylan.jpg`;
			$pixbufloader->write($raw_data);
			$pixbufloader->close;		
			my $pixbuf = $pixbufloader->get_pixbuf;
			 
			my $img_orig = Gtk2::Image->new_from_pixbuf($pixbuf);	
		$hbox_scale->pack_start($img_orig,FALSE,FALSE,0);
			
		$lbl_type = Gtk2::Label->new("Scale\n(150x150):");
		$hbox_scale->pack_start($lbl_type,FALSE,FALSE,0);
							
			my $pixbuf_larger = $pixbuf->scale_simple(150,150,'bilinear');
			my $img_larger = Gtk2::Image->new_from_pixbuf($pixbuf_larger);
		$hbox_scale->pack_start($img_larger,FALSE,FALSE,0);
		
		$lbl_type = Gtk2::Label->new("Scale\n(50x50):");
		$hbox_scale->pack_start($lbl_type,FALSE,FALSE,0);
							
			my $pixbuf_smaller = $pixbuf->scale_simple(50,50,'bilinear');
			my $img_smaller = Gtk2::Image->new_from_pixbuf($pixbuf_smaller);
		$hbox_scale->pack_start($img_smaller,FALSE,FALSE,0);
			
	$frame->add($hbox_scale);
			
$vbox->pack_start($frame,FALSE,FALSE,0);

#cut and zoom
$frame = Gtk2::Frame->new('Fetching a piece of a pixbuff, and scaling it:');
	$frame->set_shadow_type ('out');
	#method of Gtk2::Container
	$frame->set_border_width(5);
	
	my $pixbuf_head = $pixbuf->new_subpixbuf (13, 11, 50, 50);
	my $pixbuf_head_large = $pixbuf_head->scale_simple(150,150,'bilinear');
	
	#create a hbox that will contain the original and scaled head images
	my $hbox_head_scale = Gtk2::HBox->new(0,5);
		
		$lbl_type = Gtk2::Label->new("Original head:");
	$hbox_head_scale->pack_start($lbl_type,FALSE,FALSE,0);
		my $img_head = Gtk2::Image->new_from_pixbuf($pixbuf_head);
	$hbox_head_scale->pack_start($img_head,FALSE,FALSE,0);
	
	$lbl_type = Gtk2::Label->new("Scaled head:");
	$hbox_head_scale->pack_start($lbl_type,FALSE,FALSE,0);
		my $img_head_large = Gtk2::Image->new_from_pixbuf($pixbuf_head_large);
	$hbox_head_scale->pack_start($img_head_large,FALSE,FALSE,0);
			
	$frame->add($hbox_head_scale);
	
$vbox->pack_start($frame,FALSE,FALSE,0);	
	
$vbox->show_all();
return $vbox;
}

