gdk: Add gdk_cursor_get_name()

Also add the GdkCursor::name property.
This commit is contained in:
Benjamin Otte 2017-11-02 20:38:07 +01:00
parent a1759a0a52
commit 8ba9ae6012
7 changed files with 77 additions and 30 deletions

View File

@ -813,6 +813,7 @@ gdk_cursor_new_from_pixbuf
gdk_cursor_new_from_surface
gdk_cursor_new_from_name
gdk_cursor_get_display
gdk_cursor_get_name
gdk_cursor_get_image
gdk_cursor_get_surface

View File

@ -126,6 +126,7 @@ _gdk_broadway_display_get_cursor_for_name (GdkDisplay *display,
private = g_object_new (GDK_TYPE_BROADWAY_CURSOR,
"display", display,
"name", name,
NULL);
return GDK_CURSOR (private);

View File

@ -65,7 +65,8 @@
enum {
PROP_0,
PROP_DISPLAY
PROP_DISPLAY,
PROP_NAME
};
G_DEFINE_ABSTRACT_TYPE (GdkCursor, gdk_cursor, G_TYPE_OBJECT)
@ -83,6 +84,9 @@ gdk_cursor_get_property (GObject *object,
case PROP_DISPLAY:
g_value_set_object (value, cursor->display);
break;
case PROP_NAME:
g_value_set_string (value, cursor->name);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
@ -104,12 +108,25 @@ gdk_cursor_set_property (GObject *object,
/* check that implementations actually provide the display when constructing */
g_assert (cursor->display != NULL);
break;
case PROP_NAME:
cursor->name = g_value_dup_string (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gdk_cursor_finalize (GObject *object)
{
GdkCursor *cursor = GDK_CURSOR (object);
g_free (cursor->name);
G_OBJECT_CLASS (gdk_cursor_parent_class)->finalize (object);
}
static void
gdk_cursor_class_init (GdkCursorClass *cursor_class)
{
@ -117,6 +134,7 @@ gdk_cursor_class_init (GdkCursorClass *cursor_class)
object_class->get_property = gdk_cursor_get_property;
object_class->set_property = gdk_cursor_set_property;
object_class->finalize = gdk_cursor_finalize;
g_object_class_install_property (object_class,
PROP_DISPLAY,
@ -125,6 +143,13 @@ gdk_cursor_class_init (GdkCursorClass *cursor_class)
P_("Display of this cursor"),
GDK_TYPE_DISPLAY,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property (object_class,
PROP_NAME,
g_param_spec_string ("name",
P_("Name"),
P_("Name of this cursor"),
NULL,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
}
static void
@ -189,6 +214,7 @@ gdk_cursor_new_from_name (GdkDisplay *display,
const gchar *name)
{
g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
g_return_val_if_fail (name != NULL, NULL);
return GDK_DISPLAY_GET_CLASS (display)->get_cursor_for_name (display, name);
}
@ -309,6 +335,7 @@ gdk_cursor_new_from_surface (GdkDisplay *display,
return GDK_DISPLAY_GET_CLASS (display)->get_cursor_for_surface (display,
surface, x, y);
}
/**
* gdk_cursor_get_display:
* @cursor: a #GdkCursor.
@ -319,7 +346,6 @@ gdk_cursor_new_from_surface (GdkDisplay *display,
*
* Since: 2.2
*/
GdkDisplay *
gdk_cursor_get_display (GdkCursor *cursor)
{
@ -328,6 +354,26 @@ gdk_cursor_get_display (GdkCursor *cursor)
return cursor->display;
}
/**
* gdk_cursor_get_name:
* @cursor: a #GdkCursor.
*
* Returns the name of the cursor. If the cursor is not a named cursor, %NULL
* will be returned and the surface property will be set.
*
* Returns: (transfer none): the name of the cursor or %NULL if it is not
* a named cursor
*
* Since: 3.94
*/
const char *
gdk_cursor_get_name (GdkCursor *cursor)
{
g_return_val_if_fail (GDK_IS_CURSOR (cursor), NULL);
return cursor->name;
}
/**
* gdk_cursor_get_image:
* @cursor: a #GdkCursor

View File

@ -60,6 +60,8 @@ GdkCursor* gdk_cursor_new_from_name (GdkDisplay *display,
const gchar *name);
GDK_AVAILABLE_IN_ALL
GdkDisplay* gdk_cursor_get_display (GdkCursor *cursor);
GDK_AVAILABLE_IN_3_94
const char *gdk_cursor_get_name (GdkCursor *cursor);
GDK_AVAILABLE_IN_ALL
GdkPixbuf* gdk_cursor_get_image (GdkCursor *cursor);
GDK_AVAILABLE_IN_3_10

View File

@ -40,6 +40,7 @@ struct _GdkCursor
GObject parent_instance;
GdkDisplay *display;
char *name;
};
struct _GdkCursorClass

View File

@ -49,7 +49,6 @@ typedef struct _GdkWaylandCursorClass GdkWaylandCursorClass;
struct _GdkWaylandCursor
{
GdkCursor cursor;
gchar *name;
struct
{
@ -144,22 +143,25 @@ _gdk_wayland_cursor_update (GdkWaylandDisplay *display_wayland,
{
struct wl_cursor *c;
struct wl_cursor_theme *theme;
const char *name;
name = gdk_cursor_get_name (GDK_CURSOR (cursor));
/* Do nothing if this is not a wl_cursor cursor. */
if (cursor->name == NULL)
if (name == NULL)
return FALSE;
theme = _gdk_wayland_display_get_scaled_cursor_theme (display_wayland,
cursor->scale);
c = wl_cursor_theme_get_cursor (theme, cursor->name);
c = wl_cursor_theme_get_cursor (theme, name);
if (!c)
{
const char *fallback;
fallback = name_fallback (cursor->name);
fallback = name_fallback (name);
if (fallback)
{
c = wl_cursor_theme_get_cursor (theme, name_fallback (cursor->name));
c = wl_cursor_theme_get_cursor (theme, fallback);
if (!c)
c = wl_cursor_theme_get_cursor (theme, "left_ptr");
}
@ -167,7 +169,7 @@ _gdk_wayland_cursor_update (GdkWaylandDisplay *display_wayland,
if (!c)
{
g_message ("Unable to load %s from the cursor theme", cursor->name);
g_message ("Unable to load %s from the cursor theme", name);
return FALSE;
}
@ -194,7 +196,6 @@ gdk_wayland_cursor_finalize (GObject *object)
{
GdkWaylandCursor *cursor = GDK_WAYLAND_CURSOR (object);
g_free (cursor->name);
if (cursor->surface.cairo_surface)
cairo_surface_destroy (cursor->surface.cairo_surface);
@ -243,7 +244,7 @@ _gdk_wayland_cursor_get_buffer (GdkCursor *cursor,
return wl_cursor_image_get_buffer (image);
}
else if (wayland_cursor->name == NULL) /* From surface */
else if (gdk_cursor_get_name (cursor) == NULL) /* From surface */
{
*hotspot_x =
wayland_cursor->surface.hotspot_x / wayland_cursor->surface.scale;
@ -309,7 +310,7 @@ _gdk_wayland_cursor_set_scale (GdkCursor *cursor,
wayland_cursor->scale = scale;
/* Blank cursor case */
if (g_strcmp0 (wayland_cursor->name, "none") == 0)
if (g_strcmp0 (gdk_cursor_get_name (cursor), "none") == 0)
return;
_gdk_wayland_cursor_update (display_wayland, wayland_cursor);
@ -347,18 +348,17 @@ _gdk_wayland_display_get_cursor_for_name_with_scale (GdkDisplay *display,
private = g_object_new (GDK_TYPE_WAYLAND_CURSOR,
"display", display,
"name", name,
NULL);
/* Blank cursor case */
if (!name || g_str_equal (name, "none") || g_str_equal (name, "blank_cursor"))
if (g_str_equal (name, "none") || g_str_equal (name, "blank_cursor"))
{
private->name = g_strdup ("none");
private->scale = scale;
return GDK_CURSOR (private);
}
private->name = g_strdup (name);
private->scale = scale;
if (!_gdk_wayland_cursor_update (display_wayland, private))
@ -369,7 +369,7 @@ _gdk_wayland_display_get_cursor_for_name_with_scale (GdkDisplay *display,
/* Insert into cache. */
g_hash_table_insert (display_wayland->cursor_cache,
private->name,
(gpointer) gdk_cursor_get_name (GDK_CURSOR (private)),
g_object_ref (private));
return GDK_CURSOR (private);
}
@ -408,7 +408,6 @@ _gdk_wayland_display_get_cursor_for_surface (GdkDisplay *display,
cursor = g_object_new (GDK_TYPE_WAYLAND_CURSOR,
"display", display_wayland,
NULL);
cursor->name = NULL;
cursor->surface.hotspot_x = x;
cursor->surface.hotspot_y = y;

View File

@ -50,7 +50,6 @@ struct _GdkX11Cursor
GdkCursor cursor;
Cursor xcursor;
gchar *name;
guint serial;
};
@ -103,7 +102,7 @@ cache_compare_func (gconstpointer listelem,
/* Elements marked as pixmap must be named cursors
* (since we don't store normal pixmap cursors
*/
return strcmp (key->name, cursor->name);
return strcmp (key->name, gdk_cursor_get_name (GDK_CURSOR (cursor)));
}
/* Returns the cursor if there is a match, NULL if not
@ -175,8 +174,6 @@ gdk_x11_cursor_finalize (GObject *object)
if (private->xcursor && !gdk_display_is_closed (display))
XFreeCursor (GDK_DISPLAY_XDISPLAY (display), private->xcursor);
g_free (private->name);
G_OBJECT_CLASS (gdk_x11_cursor_parent_class)->finalize (object);
}
@ -269,15 +266,13 @@ gdk_x11_cursor_get_surface (GdkCursor *cursor,
{
GdkDisplay *display;
Display *xdisplay;
GdkX11Cursor *private;
XcursorImages *images;
XcursorImage *image;
gint size;
cairo_surface_t *surface;
gint scale;
gchar *theme;
private = GDK_X11_CURSOR (cursor);
const char *name;
display = gdk_cursor_get_display (cursor);
xdisplay = GDK_DISPLAY_XDISPLAY (display);
@ -285,8 +280,9 @@ gdk_x11_cursor_get_surface (GdkCursor *cursor,
size = XcursorGetDefaultSize (xdisplay);
theme = XcursorGetTheme (xdisplay);
if (private->name)
images = XcursorLibraryLoadImages (private->name, theme, size);
name = gdk_cursor_get_name (cursor);
if (name)
images = XcursorLibraryLoadImages (name, theme, size);
else
images = NULL;
@ -343,8 +339,11 @@ _gdk_x11_cursor_update_theme (GdkCursor *cursor)
if (private->xcursor != None)
{
if (private->name)
new_cursor = XcursorLibraryLoadCursor (xdisplay, private->name);
const char *name;
name = gdk_cursor_get_name (cursor);
if (name)
new_cursor = XcursorLibraryLoadCursor (xdisplay, name);
if (new_cursor != None)
{
@ -532,7 +531,6 @@ _gdk_x11_display_get_cursor_for_surface (GdkDisplay *display,
"display", display,
NULL);
private->xcursor = xcursor;
private->name = NULL;
private->serial = theme_serial;
return GDK_CURSOR (private);
@ -644,9 +642,9 @@ _gdk_x11_display_get_cursor_for_name (GdkDisplay *display,
private = g_object_new (GDK_TYPE_X11_CURSOR,
"display", display,
"name", name,
NULL);
private->xcursor = xcursor;
private->name = g_strdup (name);
private->serial = theme_serial;
add_to_cache (private);
@ -709,7 +707,6 @@ gdk_cursor_new_from_pixmap (GdkDisplay *display,
"display", display,
NULL);
private->xcursor = xcursor;
private->name = NULL;
private->serial = theme_serial;
return GDK_CURSOR (private);