Question

I can't find how to do that :( there's gtk_window_set_resizable, but it disables resizing at all and i still want my window to resize horizontally. Any ideas?

Was it helpful?

Solution

I believe you can try using gtk_window_set_geometry_hints function and specify max and min height for your window. In this case you would still allow width change whereas height will stay constant. Pls, check if an example below would work for you:

int main(int argc, char * argv[])
{
    gtk_init(&argc, &argv);
    GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

    GdkGeometry hints;
    hints.min_width = 0;
    hints.max_width = gdk_screen_get_width(gtk_widget_get_screen(window));;
    hints.min_height = 300;
    hints.max_height = 300;

    gtk_window_set_geometry_hints(
        GTK_WINDOW(window),
        window,
        &hints,
        (GdkWindowHints)(GDK_HINT_MIN_SIZE | GDK_HINT_MAX_SIZE));

    gtk_widget_show_all(window);
    gtk_main();
    return 0;
}

hope this helps, regards

OTHER TIPS

Here's my implementation in GTK#

public static void SetFixedDimensions (
    Window window, bool vertical, bool horizontal)
{
    int width, height;
    window.GetSize(out width, out height);

    var hintGeometry = new Gdk.Geometry();

    hintGeometry.MaxHeight = vertical ? height : Int32.MaxValue;
    hintGeometry.MinHeight = vertical ? height : 0;

    hintGeometry.MaxWidth = horizontal ? width : Int32.MaxValue;
    hintGeometry.MinWidth = horizontal ? width : 0;

    window.SetGeometryHints(window, hintGeometry, Gdk.WindowHints.MaxSize);
}

Building upon @serge_gubenko's answer, if you want to forbid vertical resize only after the initial layout, you will need to setup a callback for signal size-allocate.

Example:

static gint signal_connect_id_cb_dialog_size_allocate;

static void
cb_dialog_size_allocate (GtkWidget    *window,
                         GdkRectangle *allocation,
                         gpointer      user_data)
{
        GdkGeometry hints;

        g_signal_handler_disconnect (G_OBJECT (dialog),
                                     signal_connect_id_cb_dialog_size_allocate);

        /* dummy values for min/max_width to not restrict horizontal resizing */
        hints.min_width = 0;
        hints.max_width = G_MAXINT;
        /* do not allow vertial resizing */
        hints.min_height = allocation->height;
        hints.max_height = allocation->height;
        gtk_window_set_geometry_hints (GTK_WINDOW (window), (GtkWidget *) NULL,
                                       &hints,
                                       (GdkWindowHints) (GDK_HINT_MIN_SIZE | GDK_HINT_MAX_SIZE));
}

int main(int argc, char * argv[])
{
    gtk_init(&argc, &argv);
    GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

    signal_connect_id_cb_dialog_size_allocate =
       g_signal_connect (G_OBJECT (state->dialog),
                         "size-allocate",
                         G_CALLBACK (cb_dialog_size_allocate),
                         (gpointer) NULL /* user_data */);

    gtk_widget_show_all(window);
    gtk_main();
    return 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top