sample_images_thumbnail.pl
changeset 4 e1efc7395f84
equal deleted inserted replaced
3:689bf4b363c3 4:e1efc7395f84
       
     1 #!/usr/bin/perl -w
       
     2 #
       
     3 # image thumbnails and liststore
       
     4 #
       
     5 use strict;
       
     6 use Gtk2 -init;
       
     7 
       
     8 my $window = Gtk2::Window->new;
       
     9 
       
    10 $window->set_default_size( 650, 650 );
       
    11 
       
    12 # the ListStore model that actually holds the 
       
    13 #  data ..a picture and a filename
       
    14 my $model = Gtk2::ListStore->new ('Gtk2::Gdk::Pixbuf', 'Glib::String');
       
    15 
       
    16 my @files = <*.jpg *.png *.gif>;
       
    17 #print "@files\n";
       
    18 foreach my $file (@files){
       
    19     print "Processing $file...\n";
       
    20     my $pixbuf = Gtk2::Gdk::Pixbuf->new_from_file_at_scale($file,100,100,1);
       
    21     my $iter = $model->append;
       
    22     $model->set($iter,
       
    23                 0, $pixbuf,
       
    24                 1, $file,
       
    25                );
       
    26 }
       
    27 
       
    28 # now make the actual treeview renderer
       
    29 my $treeview = Gtk2::TreeView->new ($model);
       
    30 $treeview->set_headers_visible (0);
       
    31 
       
    32 $treeview->insert_column_with_attributes(
       
    33                                          -1, # append
       
    34                                          "", # this won't be visible because we turned off headings
       
    35                                          Gtk2::CellRendererPixbuf->new,  # the renderer
       
    36                                          pixbuf => 0);  # get the pixbuf property from model col 0
       
    37 
       
    38 $treeview->insert_column_with_attributes(
       
    39                                          -1, # append
       
    40                                          "", # this won't be visible because we turned off headings
       
    41                                          Gtk2::CellRendererText->new(),  # the renderer
       
    42                                          text => 1);  # get the property from model col 1
       
    43 
       
    44 $treeview->get_selection->signal_connect (changed => sub {
       
    45                                              
       
    46                                              my $treeselection = $treeview->get_selection;
       
    47                                              #print "$treeselection\n";
       
    48                                              
       
    49                                              $treeselection->selected_foreach (sub{ 
       
    50 #this sub wil receive the following:
       
    51 my ($model,$path,$iter) =@_;
       
    52 
       
    53 #we want data at the model's column 1  
       
    54 #where the iter is pointing
       
    55 my $value = $model->get($iter,1);
       
    56 #print $value."\n";
       
    57 load_image( "./$value" );
       
    58 
       
    59 });    
       
    60 
       
    61                                          });
       
    62 
       
    63 
       
    64 my $hbox = Gtk2::HBox->new (0, 4);
       
    65 my $scwin = Gtk2::ScrolledWindow->new();
       
    66 $scwin->set_size_request( 250, 250 );
       
    67 $scwin->add_with_viewport($treeview);
       
    68 $scwin->set_policy('always', 'always');
       
    69 $hbox->pack_start($scwin,0,1,1);
       
    70 
       
    71 
       
    72 my $vp = Gtk2::Viewport->new( undef, undef );
       
    73 my $scwin1 = Gtk2::ScrolledWindow->new( undef, undef );
       
    74 $scwin1->set_policy( 'automatic', 'automatic' );
       
    75 $scwin1->add( $vp );
       
    76 $hbox->pack_start($scwin1,1,1,1);
       
    77 
       
    78 my $image = Gtk2::Image->new();
       
    79 $vp->add($image);
       
    80 
       
    81 $window->add ($hbox);
       
    82 $window->show_all;
       
    83 $window->signal_connect (destroy => sub {Gtk2->main_quit});
       
    84 Gtk2->main;
       
    85 
       
    86 ###################################
       
    87 
       
    88 sub load_image {
       
    89     my $file = shift  or warn "$!\n";
       
    90     $image->set_from_file ($file);
       
    91     $window->show_all();
       
    92 }