Auto-adjusting image



Hi!

I have a Gtk2::Image in my app that I want to automatically resize to
swallow all remaining space in the window. For most widgets it would be
easy, but for images it seems you have to rescale the underlying pixbuf
manually. I found a post describing how to do it in Python:

http://www.daa.com.au/pipermail/pygtk/2005-July/010671.html

The idea is to catch the signal size_allocate on the image and rescale
the pixbuf to the presently available space in the callback.

However, when I did the same thing in Perl (I don't know if the Python
example actually works), it only works when enlarging the window. When
you enlarge it, the pixbuf is enlarged to fill out the newly available
space, but this space is also reserved for the image, so you can never
shrink the window! How do I solve this?

A simple program showing the (failed) principle is attached below.

best regards, Pelle



#!/usr/bin/perl

use Glib qw/TRUE FALSE/;
use Gtk2 'init';
use Gtk2::SimpleList;
use Gtk2::Helper;

sub video_resize_image
{
    my $rectangle = @_[1];
    $available_width = $rectangle->width();
    $available_height = $rectangle->height();

    if ($available_width != $pixbuf_scaled->get_width()) {

        my $orig_width = $pixbuf->get_width();
        my $orig_height = $pixbuf->get_height();
        my $scale;

        $pixbuf_scaled = $pixbuf->scale_simple($available_width,
                         $available_height, 'bilinear');

        $image->set_from_pixbuf($pixbuf_scaled);

    }

    return FALSE;
}


sub quit_program
{
    Gtk2->main_quit();
}

$window = Gtk2::Window->new('toplevel');
$window->set_border_width(10);
$window->signal_connect(destroy => \&quit_program);

$hbox = Gtk2::HBox->new(FALSE, 10);

$label = Gtk2::Label->new('Test');

$pixbuf = Gtk2::Gdk::Pixbuf->new_from_file('test_image.png');
$pixbuf_scaled = $pixbuf->copy();
$image = Gtk2::Image->new_from_pixbuf($pixbuf_scaled);
$image->signal_connect(size_allocate => \&video_resize_image);

$hbox->pack_start($label, FALSE, FALSE, 10);
$hbox->pack_start($image, TRUE, TRUE, 10);

$window->add($hbox);
$window->show_all();

Gtk2->main();





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