Problem setting widget cursor during drag

I have an app with a GtkFixed that contains a set of GtkButtons that can be repositioned by dragging them using GtkGestureDrag’s drag-begin, drag-update and drag-end signals.

I want to use gtk_widget_set_cursor_from_name() to set the cursor to “dragging” in my drag-begin callback and then reset it to NULL on drag-end. However, this doesn’t work as the cursor maintains “default” during dragging.

Is this expected behaviour? If there’s a correct way to accomplish what I want, a pointer in the right direction would be much appreciated!

You probably need to set the cursor also in the drag-update signal handler. And do not forget to unreference the cursor every time you’ve created it.

However, I find this combo of gdk_cursor_new... and g_object_unref a little inelegant. In the Windows API, when you load a standad resource, e.g. a system-defined cursor, you get a pointer (handle) to an already loaded (= shared) cursor. You do not need to dispose of it. You must free only custom cursors that you create explicitly. In GTK, it seems, that even standard cursors are created from scratch, with memory allocation, and you need to free them after use. Is it really so?

The GdkCursor object is just a container of data: a name, or the custom texture data; the location of the hotspot. Depending on the windowing system, the GDK backend will map names to an internal cache. Additionally, depending on the platform, named cursors can be “themed”, which requires loading texture data.

When you’re creating a new GdkCursor you’re just creating a local instance that gets associated to a device and/or a GdkSurface; the life time of the actual cursor texture data exists outside of the life time of the GdkCursor instance.

It’s easier to reason in terms of ownership if all constructor functions for types that are not initially unowned return a fully owned reference to the caller.

You probably need to set the cursor also in the drag-update signal handler.

Thanks for the tip. Unfortunately, this doesn’t work. As a further description of what’s happening: with or without also setting the cursor in drag-update the desired cursor appears once primary click is released, i.e. the cursor remains “default” when the drag starts, when it updates, but “grabbing” appears once the drag ends if I don’t reset the cursor on drag-end by passing NULL.

However, I find this combo of gdk_cursor_new... and g_object_unref a little inelegant.

Thankfully, gtk_widget_set_cursor_from_name() handles this dance for me.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.