Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Gtk2 ListStore Thumbnail/Image Viewer

by zentara (Archbishop)
on Sep 30, 2008 at 18:53 UTC ( [id://714609]=sourcecode: print w/replies, xml ) Need Help??
Category: GUI Programming
Author/Contact Info zentara of perlmonks
Description: A simple ListStore method of showing thumbnails and images. Useful for single directory browsing of images. Shows (one way) how to make nice Lists with pictures and text with Gtk2.
#!/usr/bin/perl -w
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){
   my $pixbuf = Gtk2::Gdk::Pixbuf->new_from_file_at_scale($file,100,10
+0,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();
}

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://714609]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2024-04-19 08:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found