Search code examples
cgtkgobject

How do I get the size of a maximized window without showing it?


I am trying to determine the size of a maximized window so I can set the window size to a value close to it. However, I don't know how to do that without first showing the maximized window. Is there a way to emit the signal generated by gtk_window_maximize before the window is displayed?

Below is my attempt so far. The problem is that I can see a flash of the maximized window before the resizing takes place.

#include <gtk/gtk.h>

int signal_id;

void resize(GtkWindow *window, GdkEvent *event, gpointer data)
{
    gint width, height;

    g_signal_handler_disconnect(G_OBJECT(window), signal_id);
    gtk_window_get_size(window, &width, &height);
    gtk_window_resize(window, width - 10, height - 10);
}


int main(int argc, char *argv[])
{
    GtkWidget *window;

    gtk_init(&argc, &argv);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    signal_id = g_signal_connect(G_OBJECT(window), "configure-event", G_CALLBACK(resize), NULL);
    gtk_window_maximize(GTK_WINDOW(window));
    gtk_widget_show_all(GTK_WIDGET(window));

    gtk_main();
    return 0;
}

Solution

  • No it is not possible, because until the window manager maps the window, the actual size is not known. But what you can do is get the screen size:

    width=gdk_screen_width();
    height=gdk_screen_height();
    gtk_widget_set_size_request(window, width/2, height/2);