sample_images_thumbnail.pl
author Peter Gervai <grin@grin.hu>
Thu, 16 Oct 2008 15:29:16 +0200
changeset 4 e1efc7395f84
permissions -rwxr-xr-x
Adding sample: image thumbnail viewer

#!/usr/bin/perl -w
#
# image thumbnails and liststore
#
use strict;
use Gtk2 -init;

my $window = Gtk2::Window->new;

$window->set_default_size( 650, 650 );

# the ListStore model that actually holds the 
#  data ..a picture and a filename
my $model = Gtk2::ListStore->new ('Gtk2::Gdk::Pixbuf', 'Glib::String');

my @files = <*.jpg *.png *.gif>;
#print "@files\n";
foreach my $file (@files){
    print "Processing $file...\n";
    my $pixbuf = Gtk2::Gdk::Pixbuf->new_from_file_at_scale($file,100,100,1);
    my $iter = $model->append;
    $model->set($iter,
                0, $pixbuf,
                1, $file,
               );
}

# now make the actual treeview renderer
my $treeview = Gtk2::TreeView->new ($model);
$treeview->set_headers_visible (0);

$treeview->insert_column_with_attributes(
                                         -1, # append
                                         "", # this won't be visible because we turned off headings
                                         Gtk2::CellRendererPixbuf->new,  # the renderer
                                         pixbuf => 0);  # get the pixbuf property from model col 0

$treeview->insert_column_with_attributes(
                                         -1, # append
                                         "", # this won't be visible because we turned off headings
                                         Gtk2::CellRendererText->new(),  # the renderer
                                         text => 1);  # get the property from model col 1

$treeview->get_selection->signal_connect (changed => sub {
                                             
                                             my $treeselection = $treeview->get_selection;
                                             #print "$treeselection\n";
                                             
                                             $treeselection->selected_foreach (sub{ 
#this sub wil receive the following:
my ($model,$path,$iter) =@_;

#we want data at the model's column 1  
#where the iter is pointing
my $value = $model->get($iter,1);
#print $value."\n";
load_image( "./$value" );

});    

                                         });


my $hbox = Gtk2::HBox->new (0, 4);
my $scwin = Gtk2::ScrolledWindow->new();
$scwin->set_size_request( 250, 250 );
$scwin->add_with_viewport($treeview);
$scwin->set_policy('always', 'always');
$hbox->pack_start($scwin,0,1,1);


my $vp = Gtk2::Viewport->new( undef, undef );
my $scwin1 = Gtk2::ScrolledWindow->new( undef, undef );
$scwin1->set_policy( 'automatic', 'automatic' );
$scwin1->add( $vp );
$hbox->pack_start($scwin1,1,1,1);

my $image = Gtk2::Image->new();
$vp->add($image);

$window->add ($hbox);
$window->show_all;
$window->signal_connect (destroy => sub {Gtk2->main_quit});
Gtk2->main;

###################################

sub load_image {
    my $file = shift  or warn "$!\n";
    $image->set_from_file ($file);
    $window->show_all();
}