Get image from cursor

Hi, ALL,

In that link the documentation says:

Note that depending on the capabilities of the windowing system and on the cursor, GDK may not be able to obtain the image data. In this case, NULL is returned.

Is there a way to query the window system capabilities to know whether the cursor will be returned or NULL will be returned?

Yes: calling gdk_cursor_get_image() will return NULL. You can’t know beforehand if that’s the case, and you will still need to handle that case gracefully.

Emmanuele,
I presume there is no other means to do what I want.

Thank you.

What is it that you want? I mean: the only way to find out if you can get an image out of the cursor is to ask for one. I don’t understand what could be gained in having a “query if I can call the API” kind of API, when the API in question is already capable of telling you that it can fail:

gboolean
gdk_cursor_can_get_image (GdkCursor *cursor)
{
  GdkPixbuf *image = gdk_cursor_get_image (cursor);

  if (image == NULL)
    return FALSE;

  g_object_unref (image);
  return TRUE;
}

Even if you had something like that, and decided to call it, you’d need to handle the case where the current platform, windowing system, or cursor theme could not give you back an image:

if (gdk_cursor_can_get_image (cursor)) {
  GdkPixbuf *image = gdk_cursor_get_image (cursor);
  // do something
  g_object_unref (image);
} else {
  // do something else to handle the case
}

Which means:

GdkPixbuf *image = gdk_cursor_get_image (cursor);
if (image != NULL) {
  // do something
  g_object_unref (image);
} else {
  // do something else to handle the case
}

Emmanuele,
What I want is:
Is there a different API that can give me a cursor bitmap in case this call returns NULL.
Or maybe some kind of API that will always give a cursor bitmap (no NULLs).

However I take it there is none.

Thank you.

If this function returns NULL it means there’s no cursor bitmap to give you that is also available to GTK.

No, that can’t be done without having GTK choose the fallback icon for you, which would not really be useful since only you can decide what constitutes a reasonable fallback.

@Emmanuele,
I understand.
In my program I have a panel where the user can choose an alternative cursor - either as a cursor file or from a set of a stock cursors.

I also want to display a selected cursor bitmap.

Now, it looks to me like this functionality is dependent on the many factors and I can’t reliably display the cursor bitmap to the user.

Or maybe there is another possibility for GTK? Maybe I can display the cursor itself and not the underlying bitmap? I mean GNOME does display the cursor at any time…

Any suggestions?

Thank you.

Themed cursors are stored as image files on disk, and GTK uses libXcursor to load them. If it is important enough to you, you could use libXcursor yourself to get images and show them.

@matthiasc,
I presume that Wayland has its own way, since this one belongs to the X?

Thank you.

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