Custom widget filling maximal available width

I am trying to create a widget derived from Gtk::DrawingArea. It should always expand to the full width of the parent, and it should have a height which is computed from the width (let’s say with a function my_height(int width). However, running my_height(int width) is expensive, so I can only run this for the particular width available to the widget. The content does not have a concept of “natural height” or “minimal height”.

How do I go about doing this? I have tried various ways of overriding get_preferred_width_for_height_vfunc, on_size_allocate and others, but I can’t get this to work. Thanks!

(For completeness: I have looked at the documentation at Custom Containers but that seems to do far more than I can do with my single my_height(int width) function, not to mention that it is rather complex for what I think is a rather simple requirement on widget size).

So this is with GTK 3.

“Filling maximal available width” is usually done with the GtkWidget hexpand property (horizontal expand).

I’m not sure if you could simply set this property instead of creating a subclass of GtkDrawingArea.

Yes, GTK3, sorry for not mentioning that.

If I just set the hexpand property, GTK will not know how high to make the widget. So then in on_size_allocate, I get passed the maximal width (good) and a height of 1 (bad). If I do a set_size_request inside the on_size_allocate, nothing happens, and neither does adding a queue_resize call in there.

So in other words: the problem is to make GTK understand how high the widget should be, it can indeed figure out the required width by itself already.

::size-allocate is when GTK has already decided which size the widget will have, so it’s too late.

In the docs, the GtkWidget class description has the “Height-for-width Geometry Management” section.

You can return the same height for both the “minimum height” and the “natural height”, so GTK won’t have the choice once it has chosen the width.

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