Search code examples
linuxgtkvala

Shrink window in GTK+ dynamically when content shrinks?


I have a window in a Vala application and an image inside it. This image is changed sometimes by img.set_from_pixbuf(imgdata); and so it's size changes as well. It's embedded in a Gtk.Box.

box = new Gtk.Box(Orientation.VERTICAL,5);
...
box.pack_end(img,false,false);

So if there was a big image before and I replace it with a smaller one, the window remains ridiculously big and I have not found a method to dynamically shrink it to the space required. I have tried with window.set_default_size(box.width_request,box.height_request) but it always returns -1.

So any ideas how to resize the window? Thanks!


Solution

  • If I am not mistaken the automatic resizing of windows only only happens when elements are too large to be drawn. Additionally the set_default_size method only matters when first drawing the window and unless I am wrong is never used again. I would suggest using the resize method to set the window size. (link)

    window.resize(box.width_request, box.height_request);
    

    One thing you need to remember when using resize if you can't resize it smaller than the request_size if you run into that issue use the set_request_size method.