Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to change cursor with GTK3

Tags:

c

gtk

gtk3

gdk

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.

like image 710
I . Avatar asked Sep 21 '25 02:09

I .


1 Answers

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);
}
like image 72
I . Avatar answered Sep 22 '25 17:09

I .