icon paintable: Replace get_filename and get_resource_path with get_file()

This returns a GFile which can represent both the above.
This commit is contained in:
Alexander Larsson 2020-02-10 12:33:17 +01:00
parent 344ad65031
commit 904eecd1ed
6 changed files with 47 additions and 45 deletions

View File

@ -423,7 +423,6 @@ get_file (GValue *value,
GtkIconTheme *icon_theme;
const char *name;
GtkIconPaintable *info;
GFile *file;
name = gtk_image_get_icon_name (GTK_IMAGE (data));
icon_theme = gtk_icon_theme_get_for_display (gtk_widget_get_display (GTK_WIDGET (data)));
@ -434,9 +433,7 @@ get_file (GValue *value,
32, 1,
gtk_widget_get_direction (GTK_WIDGET (data)),
0);
file = g_file_new_for_path (gtk_icon_paintable_get_filename (info));
g_value_set_object (value, file);
g_object_unref (file);
g_value_take_object (value, gtk_icon_paintable_get_file (info));
g_object_unref (info);
}

View File

@ -5009,8 +5009,7 @@ gtk_icon_theme_choose_icon_finish
gtk_icon_theme_lookup_by_gicon
gtk_icon_theme_list_icons
gtk_icon_theme_get_icon_sizes
gtk_icon_paintable_get_filename
gtk_icon_paintable_get_resource_path
gtk_icon_paintable_get_file
gtk_icon_paintable_get_icon_name
gtk_icon_paintable_is_symbolic
<SUBSECTION Standard>

View File

@ -3262,41 +3262,41 @@ gtk_icon_paintable_class_init (GtkIconPaintableClass *klass)
gobject_class->finalize = gtk_icon_paintable_finalize;
}
/**
* gtk_icon_paintable_get_filename:
* @self: a #GtkIcon
*
* Gets the filename for the icon.
*
* Returns: (nullable) (type filename): the filename for the icon, or %NULL
* if the icon is not represented by a filename.
*/
const gchar *
gtk_icon_paintable_get_filename (GtkIconPaintable *icon)
static GFile *
new_resource_file (const char *filename)
{
g_return_val_if_fail (icon != NULL, NULL);
char *escaped = g_uri_escape_string (filename,
G_URI_RESERVED_CHARS_ALLOWED_IN_PATH, FALSE);
char *uri = g_strconcat ("resource://", escaped, NULL);
GFile *file = g_file_new_for_uri (uri);
if (!icon->is_resource)
return icon->filename;
return NULL;
g_free (escaped);
g_free (uri);
return file;
}
/**
* gtk_icon_paintable_get_resource_path:
* gtk_icon_paintable_get_file:
* @self: a #GtkIcon
*
* Gets the resource path for the icon.
* Gets the #GFile that was used to load the icon, or %NULL if the icon was
* not loaded from a file.
*
* Returns: (nullable) (type filename): the resource for the icon, or %NULL
* if the icon is not represented by a resource.
* Returns: (nullable) (transfer full): the #GFile for the icon, or %NULL.
* Free with g_object_unref().
*/
const gchar *
gtk_icon_paintable_get_resource_path (GtkIconPaintable *icon)
GFile *
gtk_icon_paintable_get_file (GtkIconPaintable *icon)
{
g_return_val_if_fail (icon != NULL, NULL);
if (icon->filename)
{
if (icon->is_resource)
return new_resource_file (icon->filename);
else
return g_file_new_for_path (icon->filename);
}
if (icon->is_resource)
return icon->filename;
return NULL;
}

View File

@ -140,9 +140,7 @@ GDK_AVAILABLE_IN_ALL
GType gtk_icon_paintable_get_type (void) G_GNUC_CONST;
GDK_AVAILABLE_IN_ALL
const gchar * gtk_icon_paintable_get_filename (GtkIconPaintable *self);
GDK_AVAILABLE_IN_ALL
const gchar * gtk_icon_paintable_get_resource_path (GtkIconPaintable *self);
GFile * gtk_icon_paintable_get_file (GtkIconPaintable *self);
GDK_AVAILABLE_IN_ALL
const gchar * gtk_icon_paintable_get_icon_name (GtkIconPaintable *self);
GDK_AVAILABLE_IN_ALL

View File

@ -114,6 +114,8 @@ main (int argc, char *argv[])
}
else if (strcmp (argv[1], "lookup") == 0)
{
GFile *file;
if (argc < 4)
{
g_object_unref (icon_theme);
@ -128,17 +130,12 @@ main (int argc, char *argv[])
scale = atoi (argv[5]);
icon = gtk_icon_theme_lookup_icon (icon_theme, argv[3], NULL, size, scale, direction, flags);
file = gtk_icon_paintable_get_file (icon);
g_print ("icon for %s at %dx%d@%dx is %s\n", argv[3], size, size, scale,
icon ? gtk_icon_paintable_get_filename (icon) : "<none>");
file ? g_file_get_uri (file) : "<none>");
if (icon)
{
GdkPaintable *paintable = GDK_PAINTABLE (icon);
g_print ("texture size: %dx%d\n", gdk_paintable_get_intrinsic_width (paintable), gdk_paintable_get_intrinsic_height (paintable));
g_object_unref (icon);
}
g_print ("texture size: %dx%d\n", gdk_paintable_get_intrinsic_width (GDK_PAINTABLE (icon)), gdk_paintable_get_intrinsic_height (GDK_PAINTABLE (icon)));
g_object_unref (icon);
}
else
{

View File

@ -58,6 +58,8 @@ assert_icon_lookup_size (const char *icon_name,
gint pixbuf_size)
{
GtkIconPaintable *info;
GFile *file;
char *path = NULL;
if (fallbacks)
{
@ -78,21 +80,30 @@ assert_icon_lookup_size (const char *icon_name,
return;
}
file = gtk_icon_paintable_get_file (info);
if (file)
{
path = g_file_get_path (file);
g_object_unref (file);
}
if (filename)
{
if (!g_str_has_suffix (gtk_icon_paintable_get_filename (info), filename))
if (path == NULL || !g_str_has_suffix (path, filename))
{
g_error ("Icon for \"%s\" with flags %s at size %d should be \"...%s\" but is \"...%s\"",
icon_name, lookup_flags_to_string (flags), size,
filename, gtk_icon_paintable_get_filename (info) + strlen (g_get_current_dir ()));
filename, path);
return;
}
}
else
{
g_assert (gtk_icon_paintable_get_filename (info) == NULL);
g_assert (path == NULL);
}
g_free (path);
g_assert_cmpint (gdk_paintable_get_intrinsic_width (GDK_PAINTABLE (info)), ==, size);
g_object_unref (info);