Re: Resize Pixbuf



On Wed, May 11, 2005 at 13:55:35 +0200, Timo Schneider wrote:
I have a Gtk2::Gtk::Image wich contains a Gtk2::Gtk::Pixbuf displaying
an Image larger than the the Gtk2::Gtk::Image. How can I resize and
update the Pixbuf whenever the Image-widget resizes?

I tried it with using the size_allocate signal, but this creates 
infinite loops because $image->set_from_pixbuf($pixbuf), which is called
from my signal handler, itself emits an size_allocate signal. 

I tried it with using a timer, it worked, but this doesn't seem to be
the best sollution.

Hm, I had this working somewhere. I should probably release it as
a Gtk::Ex::<something> (I already have it as a module, so I will just
have to give it all appropriate names).

For the unpatient, here is the idea:

You must override 2 signals, size_allocate AND size_request.

For the size allocate, is should go along:

sub _size_allocate {
    my ($self, $new) = @_;
    no warnings 'uninitialized';
    if($self->{width}  != $new->width ||
       $self->{height} != $new->height) {
        $self->{width}  = $new->width;
        $self->{height} = $new->height;
        my $pixbuf = GetTheBitmapAtSize($self->{width}, $self->{height});
        $self->set_from_pixbuf($pixbuf);
    }

    $self->signal_chain_from_overridden($new);
}

where the "GetTheBitmapAtSize" should be the right call to
Gtk::Gdk::Pixbuf->scale_simple (I have my own function there, so I don't recall
the exact arguments).

And for the size_request, simples thing to do is:

sub _size_request {
    my ($self, $req) = @_;
    $req->width(1);
    $req->height(1);
}

In the code I have I have an attribute for minimal size and minimal
scale so I compute this in quite sophisticated ways, but this works for
the basics. You want the widget to be packed with expand on.

Note that I am not blocking any signals -- I simply check whether
I should actually resize the image and don't do anything if the
dimensions did not change. That's enough.

-------------------------------------------------------------------------------
                                                 Jan 'Bulb' Hudec <bulb ucw cz>

Attachment: signature.asc
Description: Digital signature



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