4

I am trying to change the cursor of a window dynamically with GTK3 but gtk_widget_get_parent_window doesn't seem to work.

Could someone please point out what I'm doing wrong? Thanks!

// https://developer.gnome.org/gtk3/stable/gtk-getting-started.html
// minimal example
#include <gtk/gtk.h>

static void
activate (GtkApplication* app,
          gpointer        user_data)
{
  GtkWidget *window;

  window = gtk_application_window_new (app);
  gtk_window_set_title (GTK_WINDOW (window), "Window");
  gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);

  // Here \/\/\/\/\/ .
  GdkWindow* w = gtk_widget_get_parent_window(window);
  GdkCursor* c = gdk_cursor_new_for_display(gdk_display_get_default(), GDK_WATCH);
  gdk_window_set_cursor(w, c);
  //      /\/\/\/\/\ .

  gtk_widget_show_all (window);
}

int
main (int    argc,
      char **argv)
{
  GtkApplication *app;
  int status;

  app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
  status = g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);

  return status;
}

(main.exe:16508): Gdk-CRITICAL **: gdk_window_set_cursor: assertion 'GDK_IS_WINDOW (window)' failed

I am using GTK 3.16 with msys2

Many thanks in advance.

I .
  • 113
  • 1
  • 9
  • 2
    A GtkWindow isn't likely going to have a parent GdkWindow. It will, however, have *its own* GdkWindow, which you want to use instead. Use `gtk_widget_get_window()` instead. – andlabs Feb 14 '16 at 20:10
  • @andlabs If I move the code to a "realize" event callback and use your tip it works flawlessly. Would you mind posting it as an answer so that other people can see? Many thanks! – I . Feb 14 '16 at 20:19

1 Answers1

3

Extending @andlabs comment

Any attempt to change the cursor needs to be done after the widget has been added to the widget hierarchy or in GTK terms realized.

Calling gtk_widget_get_parent_window() or even gtk_widget_get_window() before a realize event has been fired for the widget will result in a NULL pointers in both cases.

Like @andlabs it is safer to use gtk_widget_get_window() in combination with a GtkWindow.

The solution.

static GdkWindow* G_WINDOW = 0;
static GdkCursor* G_CURSOR = 0;

// call after WindowRealize()
void changecursor()
{
     assert(G_WINDOW != NULL);
     gdk_window_set_cursor(G_WINDOW, G_CURSOR);
}

static void WindowRealize(GtkWidget *window, gpointer data)
{
    G_CURSOR_HAND = gdk_cursor_new_for_display(gdk_display_get_default(), GDK_HAND2);
    G_WINDOW = gtk_widget_get_window(window);
}

static void activate(GtkApplication* app,gpointer user_data)
{
    GtkWidget *window = gtk_application_window_new(app);
    ...
    g_signal_connect(window, "realize", G_CALLBACK(WindowRealize), NULL);
    gtk_widget_show_all (window);
}
I .
  • 113
  • 1
  • 9
  • [You must also handle unrealizing.](https://developer.gnome.org/gtk3/stable/GtkWidget.html#GtkWidget-unrealize) – andlabs Feb 29 '16 at 18:52