Measure space available for widget

I have a GtkImage nested inside various types of containers. I need to get the size available to it, so that I can make the image as large possible without exceeding the screen size. I have tried using
allocation = self.{object}.get_allocation() but that just gets small figures which gradually increase every time I change the image.

I’ve tried all sorts. Is there another attribute I haven’t found yet, or is there another type of container that I could enclose it in which would give me useful figures? I have looked at the list of available containers and can’t find anything that is described in Gnome Developer in such a way as it looked suitable.

1 Like

Alternatively, is there a way of stopping the image expanding beyond a certain size?

You will need to subclass GtkImage, or create your own GtkBin class and pack a GtkImage into it, and override the GtkWidgetClass.measure virtual function to determine the maximum size of the widget.

@ebassi Interesting idea, but unfortunately it is far beyond my understanding of classes and GTK ones in particular. I would only be capable of using a solution which used existing GTK widgets.

Sooner or later you will have to deal with deriving widgets; GTK is never going to cater to every possible use case out of the box.

Widgets have a minimum and a natural size; additionally, container can assign as much space as available (according to the layout policy they implement) if a child widget is marked as capable of expanding. There’s no absolute “maximum” size, per se, unless you implement it in very specific circumstances.

Well, my app is complete apart from that one problem and I’m an amateur who has other priorities, so I’m not intending to write another one any time soon, so I don’t think I can agree… :grinning:

Is the layout policy an immutable part of the widget? If not is there an attribute I could change?

As an aside, what it the point of self.{object}.get_allocation()? It doesn’t seem to return numbers that mean anything.

Generally speaking, yes. You cannot make GtkBox lay out its children in a grid, for instance. At most, you can control whether a widget can expand, and what kind of alignment/fill it has within the allocated size assigned by its parent container.

The allocation is only assumed to be valid when drawing. You definitely cannot change the size of a widget outside of the allocation phase.

The GTkImage is inside a GtkGrid. I am not changing the size of the grid, just wishing to find the size of the grid so I can adjust the size of the image to fit it. Both your phrases above and this

The adjusted allocation is guaranteed to be completely contained within the gtk_widget_size_allocate() allocation, however. So a GtkContainer is guaranteed that its children stay inside the assigned bounds, but not that they have exactly the bounds the container assigned.

seem to indicate that the image inside a container should stay inside the size of the container. rather than expand it, but the first image is tiny, the second the exact size and all subsequent ones larger and larger.

I thought initially that this might have been to do with Expand as I have found that to be a minefield in the past, but I spent hours trying everything without success.

The problem was caused by taking the get_allocation() from the gtkimage, I should have been taking it from the grid containing it. The images now stay within the screen. The first one is tiny though, I’ve no idea why that is.

I should have been taking it from the main window.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.