Resize window while maintaining aspect ratio

19 Views Asked by At

I have a GTK calculator app. Until now, its main window has been non-resizable, and I would like to make it resizable. The main window contains a GtkMenuBar and a GtkDrawingArea, arranged vertically in a GtkVBox, with appropriate constraints to let the GtkDrawingArea expand into the vertical space.

Since the drawing area displays a calculator skin, I would like to maintain that drawing area's aspect ratio as the main window is being resized. This is what I've come up with:

GdkGeometry geom;
geom.min_width = 160;
geom.max_width = 32767;
geom.min_height = 160;
geom.max_height = 32767;
geom.min_aspect = ((double) skin_width) / skin_height;
geom.max_aspect = geom.min_aspect;
gtk_window_set_geometry_hints(GTK_WINDOW(mainwindow), calc_widget, &geom,
        GdkWindowHints(GDK_HINT_MIN_SIZE | GDK_HINT_MAX_SIZE | GDK_HINT_ASPECT));

In this, 'mainwindow' is the top-level window (GtkWindow), and calc_widget is the GtkDrawingArea.

As I understood the GTK3 documentation, this should set the aspect ratio on the GtkDrawingArea, and constrain the geometry of the top-level window in such a way that the aspect ratio of the drawing area is satisfied. But that is not what is happening; in reality, it appears to be enforcing the aspect ratio on the top level window.

This means that I end up with a strip of white to the right of the skin, with a width equal to the height of the menu bar divided by the aspect ratio. I have tried providing base_height to get it to take the height of the menu bar into account, but to no avail.

Is it possible to use gtk_window_set_geometry_hints() to enforce an aspect ratio on a widget instead of the whole window?

0

There are 0 best solutions below