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 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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
27 #include "gthemedicon.h"
36 * @short_description: Icon theming support
38 * @see_also: #GIcon, #GLoadableIcon
40 * #GThemedIcon is an implementation of #GIcon that supports icon themes.
41 * #GThemedIcon contains a list of all of the icons present in an icon
42 * theme, so that icons can be looked up quickly. #GThemedIcon does
43 * not provide actual pixmaps for icons, just the icon names.
44 * Ideally something like gtk_icon_theme_choose_icon() should be used to
45 * resolve the list of names so that fallback icons work nicely with
46 * themes that inherit other themes.
49 static void g_themed_icon_icon_iface_init (GIconIface
*iface
);
53 GObject parent_instance
;
56 gboolean use_default_fallbacks
;
59 struct _GThemedIconClass
61 GObjectClass parent_class
;
69 PROP_USE_DEFAULT_FALLBACKS
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
->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
);
118 g_strfreev (icon
->names
);
120 icon
->names
= g_new (char *, 2);
121 icon
->names
[0] = g_strdup (name
);
122 icon
->names
[1] = NULL
;
126 names
= g_value_dup_boxed (value
);
132 g_strfreev (icon
->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 GThemedIcon
*themed
= G_THEMED_ICON (object
);
151 g_return_if_fail (themed
->names
!= NULL
&& themed
->names
[0] != NULL
);
153 if (themed
->use_default_fallbacks
)
155 int i
= 0, dashes
= 0;
160 p
= themed
->names
[0];
168 last
= g_strdup (themed
->names
[0]);
170 g_strfreev (themed
->names
);
172 themed
->names
= g_new (char *, dashes
+ 1 + 1);
173 themed
->names
[i
++] = last
;
175 while ((dashp
= strrchr (last
, '-')) != NULL
)
176 themed
->names
[i
++] = last
= g_strndup (last
, dashp
- last
);
178 themed
->names
[i
++] = NULL
;
183 g_themed_icon_finalize (GObject
*object
)
187 themed
= G_THEMED_ICON (object
);
189 g_strfreev (themed
->names
);
191 G_OBJECT_CLASS (g_themed_icon_parent_class
)->finalize (object
);
195 g_themed_icon_class_init (GThemedIconClass
*klass
)
197 GObjectClass
*gobject_class
= G_OBJECT_CLASS (klass
);
199 gobject_class
->finalize
= g_themed_icon_finalize
;
200 gobject_class
->constructed
= g_themed_icon_constructed
;
201 gobject_class
->set_property
= g_themed_icon_set_property
;
202 gobject_class
->get_property
= g_themed_icon_get_property
;
209 g_object_class_install_property (gobject_class
, PROP_NAME
,
210 g_param_spec_string ("name",
212 _("The name of the icon"),
214 G_PARAM_CONSTRUCT_ONLY
| G_PARAM_WRITABLE
| G_PARAM_STATIC_NAME
| G_PARAM_STATIC_BLURB
| G_PARAM_STATIC_NICK
));
219 * A %NULL-terminated array of icon names.
221 g_object_class_install_property (gobject_class
, PROP_NAMES
,
222 g_param_spec_boxed ("names",
224 _("An array containing the icon names"),
226 G_PARAM_CONSTRUCT_ONLY
| G_PARAM_READWRITE
| G_PARAM_STATIC_NAME
| G_PARAM_STATIC_BLURB
| G_PARAM_STATIC_NICK
));
229 * GThemedIcon:use-default-fallbacks:
231 * Whether to use the default fallbacks found by shortening the icon name
232 * at '-' characters. If the "names" array has more than one element,
233 * ignores any past the first.
235 * For example, if the icon name was "gnome-dev-cdrom-audio", the array
239 * "gnome-dev-cdrom-audio",
247 g_object_class_install_property (gobject_class
, PROP_USE_DEFAULT_FALLBACKS
,
248 g_param_spec_boolean ("use-default-fallbacks",
249 _("use default fallbacks"),
250 _("Whether to use default fallbacks found by shortening the name at '-' characters. Ignores names after the first if multiple names are given."),
252 G_PARAM_CONSTRUCT_ONLY
| G_PARAM_READWRITE
| G_PARAM_STATIC_NAME
| G_PARAM_STATIC_BLURB
| G_PARAM_STATIC_NICK
));
256 g_themed_icon_init (GThemedIcon
*themed
)
258 themed
->names
= NULL
;
263 * @iconname: a string containing an icon name.
265 * Creates a new themed icon for @iconname.
267 * Returns: a new #GThemedIcon.
270 g_themed_icon_new (const char *iconname
)
272 g_return_val_if_fail (iconname
!= NULL
, NULL
);
274 return G_ICON (g_object_new (G_TYPE_THEMED_ICON
, "name", iconname
, NULL
));
278 * g_themed_icon_new_from_names:
279 * @iconnames: an array of strings containing icon names.
280 * @len: the length of the @iconnames array, or -1 if @iconnames is
283 * Creates a new themed icon for @iconnames.
285 * Returns: a new #GThemedIcon
288 g_themed_icon_new_from_names (char **iconnames
,
293 g_return_val_if_fail (iconnames
!= NULL
, NULL
);
300 names
= g_new (char *, len
+ 1);
302 for (i
= 0; i
< len
; i
++)
303 names
[i
] = iconnames
[i
];
307 icon
= G_ICON (g_object_new (G_TYPE_THEMED_ICON
, "names", names
, NULL
));
312 icon
= G_ICON (g_object_new (G_TYPE_THEMED_ICON
, "names", iconnames
, NULL
));
318 * g_themed_icon_new_with_default_fallbacks:
319 * @iconname: a string containing an icon name
321 * Creates a new themed icon for @iconname, and all the names
322 * that can be created by shortening @iconname at '-' characters.
324 * In the following example, @icon1 and @icon2 are equivalent:
326 * const char *names[] = {
327 * "gnome-dev-cdrom-audio",
333 * icon1 = g_themed_icon_new_from_names (names, 4);
334 * icon2 = g_themed_icon_new_with_default_fallbacks ("gnome-dev-cdrom-audio");
337 * Returns: a new #GThemedIcon.
340 g_themed_icon_new_with_default_fallbacks (const char *iconname
)
342 g_return_val_if_fail (iconname
!= NULL
, NULL
);
344 return G_ICON (g_object_new (G_TYPE_THEMED_ICON
, "name", iconname
, "use-default-fallbacks", TRUE
, NULL
));
349 * g_themed_icon_get_names:
350 * @icon: a #GThemedIcon.
352 * Gets the names of icons from within @icon.
354 * Returns: a list of icon names.
357 g_themed_icon_get_names (GThemedIcon
*icon
)
359 g_return_val_if_fail (G_IS_THEMED_ICON (icon
), NULL
);
360 return (const char * const *)icon
->names
;
364 * g_themed_icon_append_name:
365 * @icon: a #GThemedIcon
366 * @iconname: name of icon to append to list of icons from within @icon.
368 * Append a name to the list of icons from within @icon.
371 * Note that doing so invalidates the hash computed by prior calls
376 g_themed_icon_append_name (GThemedIcon
*icon
,
377 const char *iconname
)
381 g_return_if_fail (G_IS_THEMED_ICON (icon
));
382 g_return_if_fail (iconname
!= NULL
);
384 num_names
= g_strv_length (icon
->names
);
385 icon
->names
= g_realloc (icon
->names
, sizeof (char*) * (num_names
+ 2));
386 icon
->names
[num_names
] = g_strdup (iconname
);
387 icon
->names
[num_names
+ 1] = NULL
;
389 g_object_notify (G_OBJECT (icon
), "names");
393 * g_themed_icon_prepend_name:
394 * @icon: a #GThemedIcon
395 * @iconname: name of icon to prepend to list of icons from within @icon.
397 * Prepend a name to the list of icons from within @icon.
400 * Note that doing so invalidates the hash computed by prior calls
407 g_themed_icon_prepend_name (GThemedIcon
*icon
,
408 const char *iconname
)
414 g_return_if_fail (G_IS_THEMED_ICON (icon
));
415 g_return_if_fail (iconname
!= NULL
);
417 num_names
= g_strv_length (icon
->names
);
418 names
= g_new (char*, num_names
+ 2);
419 for (i
= 0; icon
->names
[i
]; i
++)
420 names
[i
+ 1] = icon
->names
[i
];
421 names
[0] = g_strdup (iconname
);
422 names
[num_names
+ 1] = NULL
;
424 g_free (icon
->names
);
427 g_object_notify (G_OBJECT (icon
), "names");
431 g_themed_icon_hash (GIcon
*icon
)
433 GThemedIcon
*themed
= G_THEMED_ICON (icon
);
439 for (i
= 0; themed
->names
[i
] != NULL
; i
++)
440 hash
^= g_str_hash (themed
->names
[i
]);
446 g_themed_icon_equal (GIcon
*icon1
,
449 GThemedIcon
*themed1
= G_THEMED_ICON (icon1
);
450 GThemedIcon
*themed2
= G_THEMED_ICON (icon2
);
453 for (i
= 0; themed1
->names
[i
] != NULL
&& themed2
->names
[i
] != NULL
; i
++)
455 if (!g_str_equal (themed1
->names
[i
], themed2
->names
[i
]))
459 return themed1
->names
[i
] == NULL
&& themed2
->names
[i
] == NULL
;
464 g_themed_icon_to_tokens (GIcon
*icon
,
468 GThemedIcon
*themed_icon
= G_THEMED_ICON (icon
);
471 g_return_val_if_fail (out_version
!= NULL
, FALSE
);
475 for (n
= 0; themed_icon
->names
[n
] != NULL
; n
++)
476 g_ptr_array_add (tokens
,
477 g_strdup (themed_icon
->names
[n
]));
483 g_themed_icon_from_tokens (gchar
**tokens
,
498 G_IO_ERROR_INVALID_ARGUMENT
,
499 _("Can't handle version %d of GThemedIcon encoding"),
504 names
= g_new0 (gchar
*, num_tokens
+ 1);
505 for (n
= 0; n
< num_tokens
; n
++)
506 names
[n
] = tokens
[n
];
509 icon
= g_themed_icon_new_from_names (names
, num_tokens
);
517 g_themed_icon_icon_iface_init (GIconIface
*iface
)
519 iface
->hash
= g_themed_icon_hash
;
520 iface
->equal
= g_themed_icon_equal
;
521 iface
->to_tokens
= g_themed_icon_to_tokens
;
522 iface
->from_tokens
= g_themed_icon_from_tokens
;
525 #define __G_THEMED_ICON_C__
526 #include "gioaliasdef.c"