[Gtk-sharp-list] Event problems for resizing

Michael Hutchinson m.j.hutchinson at gmail.com
Mon May 31 14:46:44 EDT 2010

On Mon, May 31, 2010 at 11:31 AM, Indrid Cold <indrid.warm at gmail.com> wrote:
> Thanks for the reply.
> In order to try overriding, I used this simple piece of code as a test:
> protected override void OnSizeAllocated (Rectangle allocation)
> {
>        base.OnSizeAllocated (allocation);
>        if (_pixbuf != null)
>        {
>                imageShown.Pixbuf = _pixbuf.ScaleSimple (allocation.Width,
> allocation.Height, InterpType.Bilinear);
>        }
> }
> This, however, causes the program (and, if I leave it long enough, the
> system) to hang. Am I using it in the wrong way?

When you allocate a new pixbuf to the Image widget. it will queue a
resize request, which will propagate up to its parent widgets and
cause new size request and allocations to propagate down. This is
causing your OnSizeAllocated to assign a new scaled pixbuf, and so on
- an infinite loop. One possible solution would be only to assign the
scaled image if the allocation has actually changed, though depending
on how the parent container allocates space there might still be loops
that could arise. Also, you should probably dispose your scaled pixbuf
after assigning it to the image, to release the managed handle.
Pixbufs are reference-counted, and the Image will hold a reference to
it anyway.

A better (though slightly more complex) solution would be not to have
an child widget at all. Override OnSizeAllocated and use it to
allocate a scaled pixbuf; override OnExpose and render the pixbuf
directly to the exposed region of the screen; override OnSizeRequested
and have it request the real size of the image.

Michael Hutchinson

More information about the Gtk-sharp-list mailing list