Re: Scaling an image with Gtk2::Gdk::Pixbuf->scale




Try inserting a widget into the textview and capture the clicks on the widget. In this case, an event box with an image widget and pixbuf.

Eric

#!/usr/bin/perl
package scaleme;
 
use strict;
use diagnostics;
use warnings;
 
use Gtk2 '-init';
use Glib qw(TRUE FALSE);
 
# Display this image
my $path = 'cat.png';
 
# Open a Gtk2 window with a Gtk2::TextView
my $window = Gtk2::Window->new('toplevel');
$window->set_title('scaleme');
$window->set_position('center');
$window->set_default_size(400, 400);
$window->signal_connect('delete-event' => sub {
    Gtk2->main_quit();
    exit;
});
           
my $frame = Gtk2::Frame->new();
$window->add($frame);
 
my $scrollWin = Gtk2::ScrolledWindow->new(undef, undef);
$frame->add($scrollWin);
$scrollWin->set_policy('automatic', 'automatic');    
$scrollWin->set_border_width(0);
 
my $textView = Gtk2::TextView->new;
$scrollWin->add_with_viewport($textView);
 
if (-e $path) {
 
    # Display a photo of a cat face
    my $pixbuf = Gtk2::Gdk::Pixbuf->new_from_file($path);
 
    my $w = $pixbuf->get_width();
    my $h = $pixbuf->get_height();
    my $scale_w1 = $w * 0.3;
    my $scale_h1 = $h * 0.3;
 
    my $pixbuf2 = $pixbuf->scale_simple($scale_w1, $scale_h1, 'GDK_INTERP_BILINEAR');

    my $image = Gtk2::Image->new_from_pixbuf($pixbuf2);
    my $ebox = Gtk2::EventBox->new();
    $ebox->signal_connect('button-press-event' => \&click_pixbuf);
    $ebox->add($image);

    my $buffer = $textView->get_buffer();
    my $anchor = $buffer->create_child_anchor($buffer->get_end_iter());
    $textView->add_child_at_anchor($ebox, $anchor);

}
 
$window->show_all();  
Gtk2->main();

sub click_pixbuf
{
  print "Click Pixbuf\n";
}




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]