1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Author: Alexander Larsson <alexl@redhat.com>
25 #include "gthemedicon.h"
33 * @short_description: Icon theming support
35 * @see_also: #GIcon, #GLoadableIcon
37 * #GThemedIcon is an implementation of #GIcon that supports icon themes.
38 * #GThemedIcon contains a list of all of the icons present in an icon
39 * theme, so that icons can be looked up quickly. #GThemedIcon does
40 * not provide actual pixmaps for icons, just the icon names.
41 * Ideally something like gtk_icon_theme_choose_icon() should be used to
42 * resolve the list of names so that fallback icons work nicely with
43 * themes that inherit other themes.
46 static void g_themed_icon_icon_iface_init (GIconIface
*iface
);
50 GObject parent_instance
;
54 gboolean use_default_fallbacks
;
57 struct _GThemedIconClass
59 GObjectClass parent_class
;
67 PROP_USE_DEFAULT_FALLBACKS
70 static void g_themed_icon_update_names (GThemedIcon
*themed
);
72 G_DEFINE_TYPE_WITH_CODE (GThemedIcon
, g_themed_icon
, G_TYPE_OBJECT
,
73 G_IMPLEMENT_INTERFACE (G_TYPE_ICON
,
74 g_themed_icon_icon_iface_init
))
77 g_themed_icon_get_property (GObject
*object
,
82 GThemedIcon
*icon
= G_THEMED_ICON (object
);
87 g_value_set_boxed (value
, icon
->init_names
);
90 case PROP_USE_DEFAULT_FALLBACKS
:
91 g_value_set_boolean (value
, icon
->use_default_fallbacks
);
95 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
100 g_themed_icon_set_property (GObject
*object
,
105 GThemedIcon
*icon
= G_THEMED_ICON (object
);
112 name
= g_value_get_string (value
);
117 if (icon
->init_names
)
118 g_strfreev (icon
->init_names
);
120 icon
->init_names
= g_new (char *, 2);
121 icon
->init_names
[0] = g_strdup (name
);
122 icon
->init_names
[1] = NULL
;
126 names
= g_value_dup_boxed (value
);
131 if (icon
->init_names
)
132 g_strfreev (icon
->init_names
);
134 icon
->init_names
= names
;
137 case PROP_USE_DEFAULT_FALLBACKS
:
138 icon
->use_default_fallbacks
= g_value_get_boolean (value
);
142 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
147 g_themed_icon_constructed (GObject
*object
)
149 g_themed_icon_update_names (G_THEMED_ICON (object
));
153 g_themed_icon_finalize (GObject
*object
)
157 themed
= G_THEMED_ICON (object
);
159 g_strfreev (themed
->init_names
);
160 g_strfreev (themed
->names
);
162 G_OBJECT_CLASS (g_themed_icon_parent_class
)->finalize (object
);
166 g_themed_icon_class_init (GThemedIconClass
*klass
)
168 GObjectClass
*gobject_class
= G_OBJECT_CLASS (klass
);
170 gobject_class
->finalize
= g_themed_icon_finalize
;
171 gobject_class
->constructed
= g_themed_icon_constructed
;
172 gobject_class
->set_property
= g_themed_icon_set_property
;
173 gobject_class
->get_property
= g_themed_icon_get_property
;
180 g_object_class_install_property (gobject_class
, PROP_NAME
,
181 g_param_spec_string ("name",
183 P_("The name of the icon"),
185 G_PARAM_CONSTRUCT_ONLY
| G_PARAM_WRITABLE
| G_PARAM_STATIC_NAME
| G_PARAM_STATIC_BLURB
| G_PARAM_STATIC_NICK
));
190 * A %NULL-terminated array of icon names.
192 g_object_class_install_property (gobject_class
, PROP_NAMES
,
193 g_param_spec_boxed ("names",
195 P_("An array containing the icon names"),
197 G_PARAM_CONSTRUCT_ONLY
| G_PARAM_READWRITE
| G_PARAM_STATIC_NAME
| G_PARAM_STATIC_BLURB
| G_PARAM_STATIC_NICK
));
200 * GThemedIcon:use-default-fallbacks:
202 * Whether to use the default fallbacks found by shortening the icon name
203 * at '-' characters. If the "names" array has more than one element,
204 * ignores any past the first.
206 * For example, if the icon name was "gnome-dev-cdrom-audio", the array
208 * |[<!-- language="C" -->
210 * "gnome-dev-cdrom-audio",
218 g_object_class_install_property (gobject_class
, PROP_USE_DEFAULT_FALLBACKS
,
219 g_param_spec_boolean ("use-default-fallbacks",
220 P_("use default fallbacks"),
221 P_("Whether to use default fallbacks found by shortening the name at “-” characters. Ignores names after the first if multiple names are given."),
223 G_PARAM_CONSTRUCT_ONLY
| G_PARAM_READWRITE
| G_PARAM_STATIC_NAME
| G_PARAM_STATIC_BLURB
| G_PARAM_STATIC_NICK
));
227 g_themed_icon_init (GThemedIcon
*themed
)
229 themed
->init_names
= NULL
;
230 themed
->names
= NULL
;
234 * g_themed_icon_update_names:
235 * @themed: a #GThemedIcon.
237 * Update the actual icon name list, based on the requested names (from
238 * construction, or later added with g_themed_icon_prepend_name() and
239 * g_themed_icon_append_name()).
240 * The order of the list matters, indicating priority:
241 * - The first requested icon is first in priority.
242 * - If "use-default-fallbacks" is #TRUE, then it is followed by all its
243 * fallbacks (starting from top to lower context levels).
244 * - Then next requested icons, and optionally their fallbacks, follow.
245 * - Finally all the style variants (symbolic or regular, opposite to whatever
246 * is the requested style) follow in the same order.
248 * An icon is not added twice in the list if it was previously added.
250 * For instance, if requested names are:
251 * [ "some-icon-symbolic", "some-other-icon" ]
252 * and use-default-fallbacks is TRUE, the final name list shall be:
253 * [ "some-icon-symbolic", "some-symbolic", "some-other-icon",
254 * "some-other", "some", "some-icon", "some-other-icon-symbolic",
255 * "some-other-symbolic" ]
257 * Returns: (transfer full) (type GThemedIcon): a new #GThemedIcon
260 g_themed_icon_update_names (GThemedIcon
*themed
)
263 GList
*variants
= NULL
;
267 g_return_if_fail (themed
->init_names
!= NULL
&& themed
->init_names
[0] != NULL
);
269 for (i
= 0; themed
->init_names
[i
]; i
++)
272 gboolean is_symbolic
;
274 is_symbolic
= g_str_has_suffix (themed
->init_names
[i
], "-symbolic");
276 name
= g_strndup (themed
->init_names
[i
], strlen (themed
->init_names
[i
]) - 9);
278 name
= g_strdup (themed
->init_names
[i
]);
280 if (g_list_find_custom (names
, name
, (GCompareFunc
) g_strcmp0
))
287 names
= g_list_prepend (names
, g_strdup (themed
->init_names
[i
]));
289 names
= g_list_prepend (names
, name
);
291 if (themed
->use_default_fallbacks
)
298 while ((dashp
= strrchr (last
, '-')) != NULL
)
303 last
= g_strndup (last
, dashp
- last
);
307 fallback
= g_strdup_printf ("%s-symbolic", last
);
311 if (g_list_find_custom (names
, fallback
, (GCompareFunc
) g_strcmp0
))
316 names
= g_list_prepend (names
, fallback
);
321 else if (is_symbolic
)
324 for (iter
= names
; iter
; iter
= iter
->next
)
326 gchar
*name
= (gchar
*) iter
->data
;
328 gboolean is_symbolic
;
330 is_symbolic
= g_str_has_suffix (name
, "-symbolic");
332 variant
= g_strndup (name
, strlen (name
) - 9);
334 variant
= g_strdup_printf ("%s-symbolic", name
);
335 if (g_list_find_custom (names
, variant
, (GCompareFunc
) g_strcmp0
) ||
336 g_list_find_custom (variants
, variant
, (GCompareFunc
) g_strcmp0
))
342 variants
= g_list_prepend (variants
, variant
);
344 names
= g_list_reverse (names
);
346 g_strfreev (themed
->names
);
347 themed
->names
= g_new (char *, g_list_length (names
) + g_list_length (variants
) + 1);
349 for (iter
= names
, i
= 0; iter
; iter
= iter
->next
, i
++)
350 themed
->names
[i
] = iter
->data
;
351 for (iter
= variants
; iter
; iter
= iter
->next
, i
++)
352 themed
->names
[i
] = iter
->data
;
353 themed
->names
[i
] = NULL
;
356 g_list_free (variants
);
358 g_object_notify (G_OBJECT (themed
), "names");
363 * @iconname: a string containing an icon name.
365 * Creates a new themed icon for @iconname.
367 * Returns: (transfer full) (type GThemedIcon): a new #GThemedIcon.
370 g_themed_icon_new (const char *iconname
)
372 g_return_val_if_fail (iconname
!= NULL
, NULL
);
374 return G_ICON (g_object_new (G_TYPE_THEMED_ICON
, "name", iconname
, NULL
));
378 * g_themed_icon_new_from_names:
379 * @iconnames: (array length=len): an array of strings containing icon names.
380 * @len: the length of the @iconnames array, or -1 if @iconnames is
383 * Creates a new themed icon for @iconnames.
385 * Returns: (transfer full) (type GThemedIcon): a new #GThemedIcon
388 g_themed_icon_new_from_names (char **iconnames
,
393 g_return_val_if_fail (iconnames
!= NULL
, NULL
);
400 names
= g_new (char *, len
+ 1);
402 for (i
= 0; i
< len
; i
++)
403 names
[i
] = iconnames
[i
];
407 icon
= G_ICON (g_object_new (G_TYPE_THEMED_ICON
, "names", names
, NULL
));
412 icon
= G_ICON (g_object_new (G_TYPE_THEMED_ICON
, "names", iconnames
, NULL
));
418 * g_themed_icon_new_with_default_fallbacks:
419 * @iconname: a string containing an icon name
421 * Creates a new themed icon for @iconname, and all the names
422 * that can be created by shortening @iconname at '-' characters.
424 * In the following example, @icon1 and @icon2 are equivalent:
425 * |[<!-- language="C" -->
426 * const char *names[] = {
427 * "gnome-dev-cdrom-audio",
433 * icon1 = g_themed_icon_new_from_names (names, 4);
434 * icon2 = g_themed_icon_new_with_default_fallbacks ("gnome-dev-cdrom-audio");
437 * Returns: (transfer full) (type GThemedIcon): a new #GThemedIcon.
440 g_themed_icon_new_with_default_fallbacks (const char *iconname
)
442 g_return_val_if_fail (iconname
!= NULL
, NULL
);
444 return G_ICON (g_object_new (G_TYPE_THEMED_ICON
, "name", iconname
, "use-default-fallbacks", TRUE
, NULL
));
449 * g_themed_icon_get_names:
450 * @icon: a #GThemedIcon.
452 * Gets the names of icons from within @icon.
454 * Returns: (transfer none): a list of icon names.
457 g_themed_icon_get_names (GThemedIcon
*icon
)
459 g_return_val_if_fail (G_IS_THEMED_ICON (icon
), NULL
);
460 return (const char * const *)icon
->names
;
464 * g_themed_icon_append_name:
465 * @icon: a #GThemedIcon
466 * @iconname: name of icon to append to list of icons from within @icon.
468 * Append a name to the list of icons from within @icon.
470 * Note that doing so invalidates the hash computed by prior calls
474 g_themed_icon_append_name (GThemedIcon
*icon
,
475 const char *iconname
)
479 g_return_if_fail (G_IS_THEMED_ICON (icon
));
480 g_return_if_fail (iconname
!= NULL
);
482 num_names
= g_strv_length (icon
->init_names
);
483 icon
->init_names
= g_realloc (icon
->init_names
, sizeof (char*) * (num_names
+ 2));
484 icon
->init_names
[num_names
] = g_strdup (iconname
);
485 icon
->init_names
[num_names
+ 1] = NULL
;
487 g_themed_icon_update_names (icon
);
491 * g_themed_icon_prepend_name:
492 * @icon: a #GThemedIcon
493 * @iconname: name of icon to prepend to list of icons from within @icon.
495 * Prepend a name to the list of icons from within @icon.
497 * Note that doing so invalidates the hash computed by prior calls
503 g_themed_icon_prepend_name (GThemedIcon
*icon
,
504 const char *iconname
)
510 g_return_if_fail (G_IS_THEMED_ICON (icon
));
511 g_return_if_fail (iconname
!= NULL
);
513 num_names
= g_strv_length (icon
->init_names
);
514 names
= g_new (char*, num_names
+ 2);
515 for (i
= 0; icon
->init_names
[i
]; i
++)
516 names
[i
+ 1] = icon
->init_names
[i
];
517 names
[0] = g_strdup (iconname
);
518 names
[num_names
+ 1] = NULL
;
520 g_free (icon
->init_names
);
521 icon
->init_names
= names
;
523 g_themed_icon_update_names (icon
);
527 g_themed_icon_hash (GIcon
*icon
)
529 GThemedIcon
*themed
= G_THEMED_ICON (icon
);
535 for (i
= 0; themed
->names
[i
] != NULL
; i
++)
536 hash
^= g_str_hash (themed
->names
[i
]);
542 g_themed_icon_equal (GIcon
*icon1
,
545 GThemedIcon
*themed1
= G_THEMED_ICON (icon1
);
546 GThemedIcon
*themed2
= G_THEMED_ICON (icon2
);
549 for (i
= 0; themed1
->names
[i
] != NULL
&& themed2
->names
[i
] != NULL
; i
++)
551 if (!g_str_equal (themed1
->names
[i
], themed2
->names
[i
]))
555 return themed1
->names
[i
] == NULL
&& themed2
->names
[i
] == NULL
;
560 g_themed_icon_to_tokens (GIcon
*icon
,
564 GThemedIcon
*themed_icon
= G_THEMED_ICON (icon
);
567 g_return_val_if_fail (out_version
!= NULL
, FALSE
);
571 for (n
= 0; themed_icon
->names
[n
] != NULL
; n
++)
572 g_ptr_array_add (tokens
,
573 g_strdup (themed_icon
->names
[n
]));
579 g_themed_icon_from_tokens (gchar
**tokens
,
594 G_IO_ERROR_INVALID_ARGUMENT
,
595 _("Can’t handle version %d of GThemedIcon encoding"),
600 names
= g_new0 (gchar
*, num_tokens
+ 1);
601 for (n
= 0; n
< num_tokens
; n
++)
602 names
[n
] = tokens
[n
];
605 icon
= g_themed_icon_new_from_names (names
, num_tokens
);
613 g_themed_icon_serialize (GIcon
*icon
)
615 GThemedIcon
*themed_icon
= G_THEMED_ICON (icon
);
617 return g_variant_new ("(sv)", "themed", g_variant_new ("^as", themed_icon
->names
));
621 g_themed_icon_icon_iface_init (GIconIface
*iface
)
623 iface
->hash
= g_themed_icon_hash
;
624 iface
->equal
= g_themed_icon_equal
;
625 iface
->to_tokens
= g_themed_icon_to_tokens
;
626 iface
->from_tokens
= g_themed_icon_from_tokens
;
627 iface
->serialize
= g_themed_icon_serialize
;