user8695089
user8695089

Reputation:

(Stack:7229): Gdk-CRITICAL **: 11:09:22.221: gdk_window_get_origin: assertion 'GDK_IS_WINDOW (window)' failed

I am trying to build an App in linux Ubuntu with C and GTK3 and I get an Error about gdk_window_get_origin and I can not figure out what I am doing wrong.

The app runs and the Window is indeed the size of my Screen, but there is this warning which I cannot fix it:

(Stack:7229): Gdk-CRITICAL **: 11:09:22.221: gdk_window_get_origin: assertion 'GDK_IS_WINDOW (window)' failed Screen max: 1600W X 900H

Here is a sample code:

#include <gtk/gtk.h>

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

    gtk_init(&argc, &argv);
    ///     Creating the Window
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

    ///     Get window Size
    GdkDisplay *dpy = gtk_widget_get_display( window );
    GdkWindow *win = gtk_widget_get_window(window);
    GdkMonitor *monitor = gdk_display_get_monitor_at_window(dpy, win);

    ///     Get window Geometry
    GdkRectangle geometry;
    gdk_monitor_get_geometry(monitor, &geometry);
    width = geometry.width;
    height = geometry.height;
    g_print("Screen max: %dW X %dH\n", width, height);

    //  Setting the default size of the Window
    gtk_window_set_default_size(GTK_WINDOW(window), width, height );

    //  Destroy the Window
    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
    gtk_widget_show_all(window);
    gtk_main();
}

Upvotes: 2

Views: 7715

Answers (1)

pan-mroku
pan-mroku

Reputation: 891

TL;DR: You are trying to access GdkWindow before GtkWindow is realized.

From gtk_widget_get_window documentation:

Returns the widget’s window if it is realized, NULL otherwise

And gtk_widget_realize:

Creates the GDK (windowing system) resources associated with a widget. For example, widget->window will be created when a widget is realized. Normally realization happens implicitly; if you show a widget and all its parent containers, then the widget will be realized and mapped automatically.

You need to either call gtk_widget_realize before trying to access GdkWindow with gtk_widget_get_display or postpone the access until the window is realized (after the start of the Gtk main loop) using callback to realize signal:

#include <gtk/gtk.h>

void print_geometry(GtkWidget* widget, GdkRectangle* geometry)
{
    GdkDisplay *dpy = gtk_widget_get_display(widget);
    GdkWindow *win = gtk_widget_get_window(widget);
    GdkMonitor *monitor = gdk_display_get_monitor_at_window(dpy, win);

    gdk_monitor_get_geometry(monitor, geometry);
    gint width = geometry->width;
    gint height = geometry->height;
    g_print("Screen max: %dW X %dH\n", width, height);

    gtk_window_set_default_size(GTK_WINDOW(widget), width, height );
}

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

    gtk_init(&argc, &argv);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

    GdkRectangle geometry;
    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
    g_signal_connect(window, "realize", G_CALLBACK(print_geometry), &geometry);
    gtk_widget_show_all(window);
    gtk_main();
}

Upvotes: 1

Related Questions