gtkicontheme.c 94.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* GtkIconTheme - a loader for icon themes
 * gtk-icon-theme.c Copyright (C) 2002, 2003 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

20 21
#include "config.h"

22 23
#include <sys/types.h>
#include <sys/stat.h>
24
#ifdef HAVE_UNISTD_H
25
#include <unistd.h>
26
#endif
27 28 29
#include <string.h>
#include <stdlib.h>
#include <glib.h>
30
#include <glib/gstdio.h>
31

32 33 34 35
#ifdef G_OS_WIN32
#ifndef S_ISDIR
#define S_ISDIR(mode) ((mode)&_S_IFDIR)
#endif
36 37 38
#define WIN32_MEAN_AND_LEAN
#include <windows.h>
#include "win32/gdkwin32.h"
39 40
#endif /* G_OS_WIN32 */

41
#include "gtkicontheme.h"
42
#include "gtkdebug.h"
43
#include "gtkiconfactory.h"
44
#include "gtkiconcache.h"
45
#include "gtkbuiltincache.h"
46
#include "gtkintl.h"
47
#include "gtkmain.h"
48
#include "gtksettings.h"
49
#include "gtkprivate.h"
50

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#define DEFAULT_THEME_NAME "hicolor"

typedef enum
{
  ICON_THEME_DIR_FIXED,  
  ICON_THEME_DIR_SCALABLE,  
  ICON_THEME_DIR_THRESHOLD,
  ICON_THEME_DIR_UNTHEMED
} IconThemeDirType;

/* In reverse search order: */
typedef enum
{
  ICON_SUFFIX_NONE = 0,
  ICON_SUFFIX_XPM = 1 << 0,
  ICON_SUFFIX_SVG = 1 << 1,
67 68
  ICON_SUFFIX_PNG = 1 << 2,
  HAS_ICON_FILE = 1 << 3
69 70 71 72 73
} IconSuffix;


struct _GtkIconThemePrivate
{
74
  guint custom_theme        : 1;
75 76
  guint is_screen_singleton : 1;
  guint pixbuf_supports_svg : 1;
77 78
  guint themes_valid        : 1;
  guint check_reload        : 1;
79
  guint loading_themes      : 1;
80 81
  
  char *current_theme;
82
  char *fallback_theme;
83 84 85 86 87 88 89 90
  char **search_path;
  int search_path_len;

  /* A list of all the themes needed to look up icons.
   * In search order, without duplicates
   */
  GList *themes;
  GHashTable *unthemed_icons;
91
  
92 93 94 95 96 97 98 99 100 101 102 103
  /* Note: The keys of this hashtable are owned by the
   * themedir and unthemed hashtables.
   */
  GHashTable *all_icons;

  /* GdkScreen for the icon theme (may be NULL)
   */
  GdkScreen *screen;
  
  /* time when we last stat:ed for theme changes */
  long last_stat_time;
  GList *dir_mtimes;
104 105

  gulong reset_styles_idle;
106 107 108 109 110 111 112
};

struct _GtkIconInfo
{
  /* Information about the source
   */
  gchar *filename;
113
  GLoadableIcon *loadable;
114
  GSList *emblem_infos;
115

Anders Carlsson's avatar
Anders Carlsson committed
116 117 118
  /* Cache pixbuf (if there is any) */
  GdkPixbuf *cache_pixbuf;

119 120 121 122 123 124 125 126 127 128 129 130
  GtkIconData *data;
  
  /* Information about the directory where
   * the source was found
   */
  IconThemeDirType dir_type;
  gint dir_size;
  gint threshold;

  /* Parameters influencing the scaled icon
   */
  gint desired_size;
131 132
  guint raw_coordinates : 1;
  guint forced_size     : 1;
133 134 135 136 137 138 139

  /* Cached information if we go ahead and try to load
   * the icon.
   */
  GdkPixbuf *pixbuf;
  GError *load_error;
  gdouble scale;
140 141

  guint ref_count;
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
};

typedef struct
{
  char *name;
  char *display_name;
  char *comment;
  char *example;

  /* In search order */
  GList *dirs;
} IconTheme;

typedef struct
{
  IconThemeDirType type;
  GQuark context;

  int size;
  int min_size;
  int max_size;
  int threshold;

  char *dir;
166
  char *subdir;
167
  int subdir_index;
168 169
  
  GtkIconCache *cache;
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
  
  GHashTable *icons;
  GHashTable *icon_data;
} IconThemeDir;

typedef struct
{
  char *svg_filename;
  char *no_svg_filename;
} UnthemedIcon;

typedef struct
{
  gint size;
  GdkPixbuf *pixbuf;
} BuiltinIcon;

typedef struct 
{
  char *dir;
  time_t mtime; /* 0 == not existing or not a dir */
191 192

  GtkIconCache *cache;
193 194 195 196 197 198 199 200 201 202 203 204 205 206
} IconThemeDirMtime;

static void  gtk_icon_theme_finalize   (GObject              *object);
static void  theme_dir_destroy         (IconThemeDir         *dir);

static void         theme_destroy     (IconTheme        *theme);
static GtkIconInfo *theme_lookup_icon (IconTheme        *theme,
				       const char       *icon_name,
				       int               size,
				       gboolean          allow_svg,
				       gboolean          use_default_icons);
static void         theme_list_icons  (IconTheme        *theme,
				       GHashTable       *icons,
				       GQuark            context);
207 208
static void         theme_list_contexts  (IconTheme        *theme,
					  GHashTable       *contexts);
209 210
static void         theme_subdir_load (GtkIconTheme     *icon_theme,
				       IconTheme        *theme,
211
				       GKeyFile         *theme_file,
212 213 214
				       char             *subdir);
static void         do_theme_change   (GtkIconTheme     *icon_theme);

215 216
static void     blow_themes               (GtkIconTheme    *icon_themes);
static gboolean rescan_themes             (GtkIconTheme    *icon_themes);
217

218 219 220 221
static void  icon_data_free            (GtkIconData     *icon_data);
static void load_icon_data             (IconThemeDir    *dir,
			                const char      *path,
			                const char      *name);
222 223 224 225 226

static IconSuffix theme_dir_get_icon_suffix (IconThemeDir *dir,
					     const gchar  *icon_name,
					     gboolean     *has_icon_file);

227 228 229 230 231 232 233

static GtkIconInfo *icon_info_new             (void);
static GtkIconInfo *icon_info_new_builtin     (BuiltinIcon *icon);

static IconSuffix suffix_from_name (const char *name);

static BuiltinIcon *find_builtin_icon (const gchar *icon_name,
234
				       gint        size,
235 236 237 238 239 240 241
				       gint        *min_difference_p,
				       gboolean    *has_larger_p);

static guint signal_changed = 0;

static GHashTable *icon_theme_builtin_icons;

242 243 244 245
/* also used in gtkiconfactory.c */
GtkIconCache *_builtin_cache = NULL;
static GList *builtin_dirs = NULL;

Matthias Clasen's avatar
Matthias Clasen committed
246
G_DEFINE_TYPE (GtkIconTheme, gtk_icon_theme, G_TYPE_OBJECT)
247 248 249 250 251 252 253 254 255 256 257

/**
 * gtk_icon_theme_new:
 * 
 * Creates a new icon theme object. Icon theme objects are used
 * to lookup up an icon by name in a particular icon theme.
 * Usually, you'll want to use gtk_icon_theme_get_default()
 * or gtk_icon_theme_get_for_screen() rather than creating
 * a new icon theme object for scratch.
 * 
 * Return value: the newly created #GtkIconTheme object.
Matthias Clasen's avatar
Matthias Clasen committed
258 259
 *
 * Since: 2.4
260 261 262 263 264 265 266 267 268 269 270 271
 **/
GtkIconTheme *
gtk_icon_theme_new (void)
{
  return g_object_new (GTK_TYPE_ICON_THEME, NULL);
}

/**
 * gtk_icon_theme_get_default:
 * 
 * Gets the icon theme for the default screen. See
 * gtk_icon_theme_get_for_screen().
272 273
 *
 * Return value: (transfer none): A unique #GtkIconTheme associated with
274 275
 *  the default screen. This icon theme is associated with
 *  the screen and can be used as long as the screen
276
 *  is open. Do not ref or unref it.
Matthias Clasen's avatar
Matthias Clasen committed
277 278
 *
 * Since: 2.4
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
 **/
GtkIconTheme *
gtk_icon_theme_get_default (void)
{
  return gtk_icon_theme_get_for_screen (gdk_screen_get_default ());
}

/**
 * gtk_icon_theme_get_for_screen:
 * @screen: a #GdkScreen
 * 
 * Gets the icon theme object associated with @screen; if this
 * function has not previously been called for the given
 * screen, a new icon theme object will be created and
 * associated with the screen. Icon theme objects are
 * fairly expensive to create, so using this function
 * is usually a better choice than calling than gtk_icon_theme_new()
 * and setting the screen yourself; by using this function
 * a single icon theme object will be shared between users.
298 299
 *
 * Return value: (transfer none): A unique #GtkIconTheme associated with
300 301
 *  the given screen. This icon theme is associated with
 *  the screen and can be used as long as the screen
302
 *  is open. Do not ref or unref it.
Matthias Clasen's avatar
Matthias Clasen committed
303 304
 *
 * Since: 2.4
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
 **/
GtkIconTheme *
gtk_icon_theme_get_for_screen (GdkScreen *screen)
{
  GtkIconTheme *icon_theme;

  g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
  g_return_val_if_fail (!screen->closed, NULL);

  icon_theme = g_object_get_data (G_OBJECT (screen), "gtk-icon-theme");
  if (!icon_theme)
    {
      GtkIconThemePrivate *priv;

      icon_theme = gtk_icon_theme_new ();
      gtk_icon_theme_set_screen (icon_theme, screen);

      priv = icon_theme->priv;
      priv->is_screen_singleton = TRUE;

325
      g_object_set_data (G_OBJECT (screen), I_("gtk-icon-theme"), icon_theme);
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
    }

  return icon_theme;
}

static void
gtk_icon_theme_class_init (GtkIconThemeClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->finalize = gtk_icon_theme_finalize;

/**
 * GtkIconTheme::changed
 * @icon_theme: the icon theme
 * 
 * Emitted when the current icon theme is switched or GTK+ detects
 * that a change has occurred in the contents of the current
 * icon theme.
 **/
346
  signal_changed = g_signal_new (I_("changed"),
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
				 G_TYPE_FROM_CLASS (klass),
				 G_SIGNAL_RUN_LAST,
				 G_STRUCT_OFFSET (GtkIconThemeClass, changed),
				 NULL, NULL,
				 g_cclosure_marshal_VOID__VOID,
				 G_TYPE_NONE, 0);

  g_type_class_add_private (klass, sizeof (GtkIconThemePrivate));
}


/* Callback when the display that the icon theme is attached to
 * is closed; unset the screen, and if it's the unique theme
 * for the screen, drop the reference
 */
static void
display_closed (GdkDisplay   *display,
		gboolean      is_error,
		GtkIconTheme *icon_theme)
{
  GtkIconThemePrivate *priv = icon_theme->priv;
  GdkScreen *screen = priv->screen;
  gboolean was_screen_singleton = priv->is_screen_singleton;

  if (was_screen_singleton)
    {
373
      g_object_set_data (G_OBJECT (screen), I_("gtk-icon-theme"), NULL);
374 375 376 377 378 379 380 381 382 383 384 385 386 387
      priv->is_screen_singleton = FALSE;
    }

  gtk_icon_theme_set_screen (icon_theme, NULL);

  if (was_screen_singleton)
    {
      g_object_unref (icon_theme);
    }
}

static void
update_current_theme (GtkIconTheme *icon_theme)
{
388 389 390
#define theme_changed(_old, _new) \
  ((_old && !_new) || (!_old && _new) || \
   (_old && _new && strcmp (_old, _new) != 0))
391 392 393 394 395
  GtkIconThemePrivate *priv = icon_theme->priv;

  if (!priv->custom_theme)
    {
      gchar *theme = NULL;
396 397
      gchar *fallback_theme = NULL;
      gboolean changed = FALSE;
398 399 400 401

      if (priv->screen)
	{
	  GtkSettings *settings = gtk_settings_get_for_screen (priv->screen);
402 403 404
	  g_object_get (settings, 
			"gtk-icon-theme-name", &theme, 
			"gtk-fallback-icon-theme", &fallback_theme, NULL);
405 406
	}

407 408 409 410
      /* ensure that the current theme (even when just the default)
       * is searched before any fallback theme
       */
      if (!theme && fallback_theme)
411 412
	theme = g_strdup (DEFAULT_THEME_NAME);

413
      if (theme_changed (priv->current_theme, theme))
414 415 416
	{
	  g_free (priv->current_theme);
	  priv->current_theme = theme;
417
	  changed = TRUE;
418 419 420
	}
      else
	g_free (theme);
421

422
      if (theme_changed (priv->fallback_theme, fallback_theme))
423 424 425 426 427 428 429 430 431 432
	{
	  g_free (priv->fallback_theme);
	  priv->fallback_theme = fallback_theme;
	  changed = TRUE;
	}
      else
	g_free (fallback_theme);

      if (changed)
	do_theme_change (icon_theme);
433
    }
434
#undef theme_changed
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
}

/* Callback when the icon theme GtkSetting changes
 */
static void
theme_changed (GtkSettings  *settings,
	       GParamSpec   *pspec,
	       GtkIconTheme *icon_theme)
{
  update_current_theme (icon_theme);
}

static void
unset_screen (GtkIconTheme *icon_theme)
{
  GtkIconThemePrivate *priv = icon_theme->priv;
  GtkSettings *settings;
  GdkDisplay *display;
  
  if (priv->screen)
    {
      settings = gtk_settings_get_for_screen (priv->screen);
      display = gdk_screen_get_display (priv->screen);
      
      g_signal_handlers_disconnect_by_func (display,
					    (gpointer) display_closed,
					    icon_theme);
      g_signal_handlers_disconnect_by_func (settings,
					    (gpointer) theme_changed,
					    icon_theme);

      priv->screen = NULL;
    }
}

/**
 * gtk_icon_theme_set_screen:
 * @icon_theme: a #GtkIconTheme
 * @screen: a #GdkScreen
 * 
 * Sets the screen for an icon theme; the screen is used
 * to track the user's currently configured icon theme,
 * which might be different for different screens.
Matthias Clasen's avatar
Matthias Clasen committed
478 479
 *
 * Since: 2.4
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
 **/
void
gtk_icon_theme_set_screen (GtkIconTheme *icon_theme,
			   GdkScreen    *screen)
{
  GtkIconThemePrivate *priv;
  GtkSettings *settings;
  GdkDisplay *display;

  g_return_if_fail (GTK_ICON_THEME (icon_theme));
  g_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));

  priv = icon_theme->priv;

  unset_screen (icon_theme);
  
  if (screen)
    {
      display = gdk_screen_get_display (screen);
      settings = gtk_settings_get_for_screen (screen);
      
      priv->screen = screen;
      
      g_signal_connect (display, "closed",
			G_CALLBACK (display_closed), icon_theme);
      g_signal_connect (settings, "notify::gtk-icon-theme-name",
			G_CALLBACK (theme_changed), icon_theme);
507 508
      g_signal_connect (settings, "notify::gtk-fallback-icon-theme-name",
			G_CALLBACK (theme_changed), icon_theme);
509 510 511 512 513 514 515 516 517
    }

  update_current_theme (icon_theme);
}

/* Checks whether a loader for SVG files has been registered
 * with GdkPixbuf.
 */
static gboolean
518
pixbuf_supports_svg (void)
519
{
520
  GSList *formats;
521
  GSList *tmp_list;
Matthias Clasen's avatar
Matthias Clasen committed
522
  static gint found_svg = -1;
523

Matthias Clasen's avatar
Matthias Clasen committed
524
  if (found_svg != -1)
525
    return found_svg;
526 527 528

  formats = gdk_pixbuf_get_formats ();

Matthias Clasen's avatar
Matthias Clasen committed
529
  found_svg = FALSE; 
530 531 532 533 534 535 536 537 538 539 540
  for (tmp_list = formats; tmp_list && !found_svg; tmp_list = tmp_list->next)
    {
      gchar **mime_types = gdk_pixbuf_format_get_mime_types (tmp_list->data);
      gchar **mime_type;
      
      for (mime_type = mime_types; *mime_type && !found_svg; mime_type++)
	{
	  if (strcmp (*mime_type, "image/svg") == 0)
	    found_svg = TRUE;
	}

541
      g_strfreev (mime_types);
542 543 544
    }

  g_slist_free (formats);
545
  
546 547 548 549 550 551 552
  return found_svg;
}

static void
gtk_icon_theme_init (GtkIconTheme *icon_theme)
{
  GtkIconThemePrivate *priv;
553
  const gchar * const *xdg_data_dirs;
554
  int i, j;
555 556 557 558

  priv = G_TYPE_INSTANCE_GET_PRIVATE (icon_theme,
                                      GTK_TYPE_ICON_THEME,
                                      GtkIconThemePrivate);
559 560 561
  icon_theme->priv = priv;

  priv->custom_theme = FALSE;
562 563 564 565

  xdg_data_dirs = g_get_system_data_dirs ();
  for (i = 0; xdg_data_dirs[i]; i++) ;

566
  priv->search_path_len = 2 * i + 2;
567 568 569 570 571 572
  
  priv->search_path = g_new (char *, priv->search_path_len);
  
  i = 0;
  priv->search_path[i++] = g_build_filename (g_get_home_dir (), ".icons", NULL);
  priv->search_path[i++] = g_build_filename (g_get_user_data_dir (), "icons", NULL);
573
  
574 575
  for (j = 0; xdg_data_dirs[j]; j++) 
    priv->search_path[i++] = g_build_filename (xdg_data_dirs[j], "icons", NULL);
576

577 578
  for (j = 0; xdg_data_dirs[j]; j++) 
    priv->search_path[i++] = g_build_filename (xdg_data_dirs[j], "pixmaps", NULL);
579 580 581 582

  priv->themes_valid = FALSE;
  priv->themes = NULL;
  priv->unthemed_icons = NULL;
583
  
584 585 586 587 588 589
  priv->pixbuf_supports_svg = pixbuf_supports_svg ();
}

static void
free_dir_mtime (IconThemeDirMtime *dir_mtime)
{
590 591 592
  if (dir_mtime->cache)
    _gtk_icon_cache_unref (dir_mtime->cache);

593
  g_free (dir_mtime->dir);
Matthias Clasen's avatar
Matthias Clasen committed
594
  g_slice_free (IconThemeDirMtime, dir_mtime);
595

596 597
}

598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
static gboolean
reset_styles_idle (gpointer user_data)
{
  GtkIconTheme *icon_theme;
  GtkIconThemePrivate *priv;

  icon_theme = GTK_ICON_THEME (user_data);
  priv = icon_theme->priv;

  if (priv->screen && priv->is_screen_singleton)
    {
      GtkSettings *settings = gtk_settings_get_for_screen (priv->screen);
      gtk_rc_reset_styles (settings);
    }

  priv->reset_styles_idle = 0;

  return FALSE;
}

618 619 620 621
static void
do_theme_change (GtkIconTheme *icon_theme)
{
  GtkIconThemePrivate *priv = icon_theme->priv;
622 623 624

  if (!priv->themes_valid)
    return;
625
  
626 627
  GTK_NOTE (ICONTHEME, 
	    g_print ("change to icon theme \"%s\"\n", priv->current_theme));
628
  blow_themes (icon_theme);
629
  g_signal_emit (icon_theme, signal_changed, 0);
630 631 632

  if (!priv->reset_styles_idle)
    priv->reset_styles_idle = 
633
      gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE - 2, 
634
		       reset_styles_idle, icon_theme, NULL);
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
}

static void
blow_themes (GtkIconTheme *icon_theme)
{
  GtkIconThemePrivate *priv = icon_theme->priv;
  
  if (priv->themes_valid)
    {
      g_hash_table_destroy (priv->all_icons);
      g_list_foreach (priv->themes, (GFunc)theme_destroy, NULL);
      g_list_free (priv->themes);
      g_list_foreach (priv->dir_mtimes, (GFunc)free_dir_mtime, NULL);
      g_list_free (priv->dir_mtimes);
      g_hash_table_destroy (priv->unthemed_icons);
    }
  priv->themes = NULL;
  priv->unthemed_icons = NULL;
  priv->dir_mtimes = NULL;
  priv->all_icons = NULL;
  priv->themes_valid = FALSE;
}

static void
gtk_icon_theme_finalize (GObject *object)
{
  GtkIconTheme *icon_theme;
  GtkIconThemePrivate *priv;
  int i;

  icon_theme = GTK_ICON_THEME (object);
  priv = icon_theme->priv;

668 669 670 671 672 673
  if (priv->reset_styles_idle)
    {
      g_source_remove (priv->reset_styles_idle);
      priv->reset_styles_idle = 0;
    }

674 675 676 677 678
  unset_screen (icon_theme);

  g_free (priv->current_theme);
  priv->current_theme = NULL;

679
  for (i = 0; i < priv->search_path_len; i++)
680 681 682 683 684 685
    g_free (priv->search_path[i]);

  g_free (priv->search_path);
  priv->search_path = NULL;

  blow_themes (icon_theme);
686

Matthias Clasen's avatar
Matthias Clasen committed
687
  G_OBJECT_CLASS (gtk_icon_theme_parent_class)->finalize (object);  
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
}

/**
 * gtk_icon_theme_set_search_path:
 * @icon_theme: a #GtkIconTheme
 * @path: array of directories that are searched for icon themes
 * @n_elements: number of elements in @path.
 * 
 * Sets the search path for the icon theme object. When looking
 * for an icon theme, GTK+ will search for a subdirectory of
 * one or more of the directories in @path with the same name
 * as the icon theme. (Themes from multiple of the path elements
 * are combined to allow themes to be extended by adding icons
 * in the user's home directory.)
 *
 * In addition if an icon found isn't found either in the current
 * icon theme or the default icon theme, and an image file with
 * the right name is found directly in one of the elements of
 * @path, then that image will be used for the icon name.
 * (This is legacy feature, and new icons should be put
708 709
 * into the default icon theme, which is called DEFAULT_THEME_NAME,
 * rather than directly on the icon path.)
Matthias Clasen's avatar
Matthias Clasen committed
710 711
 *
 * Since: 2.4
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
 **/
void
gtk_icon_theme_set_search_path (GtkIconTheme *icon_theme,
				const gchar  *path[],
				gint          n_elements)
{
  GtkIconThemePrivate *priv;
  gint i;

  g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));

  priv = icon_theme->priv;
  for (i = 0; i < priv->search_path_len; i++)
    g_free (priv->search_path[i]);

  g_free (priv->search_path);

  priv->search_path = g_new (gchar *, n_elements);
  priv->search_path_len = n_elements;
731

732 733 734 735 736 737 738 739 740 741
  for (i = 0; i < priv->search_path_len; i++)
    priv->search_path[i] = g_strdup (path[i]);

  do_theme_change (icon_theme);
}


/**
 * gtk_icon_theme_get_search_path:
 * @icon_theme: a #GtkIconTheme
742
 * @path: (allow-none): (array length=n_elements) (out): location to store a list of icon theme path directories or %NULL
743 744 745 746 747
 *        The stored value should be freed with g_strfreev().
 * @n_elements: location to store number of elements
 *              in @path, or %NULL
 * 
 * Gets the current search path. See gtk_icon_theme_set_search_path().
Matthias Clasen's avatar
Matthias Clasen committed
748 749
 *
 * Since: 2.4
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
 **/
void
gtk_icon_theme_get_search_path (GtkIconTheme      *icon_theme,
				gchar            **path[],
				gint              *n_elements)
{
  GtkIconThemePrivate *priv;
  int i;

  g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));

  priv = icon_theme->priv;

  if (n_elements)
    *n_elements = priv->search_path_len;
  
  if (path)
    {
      *path = g_new (gchar *, priv->search_path_len + 1);
      for (i = 0; i < priv->search_path_len; i++)
770
	(*path)[i] = g_strdup (priv->search_path[i]);
771 772 773 774 775 776 777 778 779
      (*path)[i] = NULL;
    }
}

/**
 * gtk_icon_theme_append_search_path:
 * @icon_theme: a #GtkIconTheme
 * @path: directory name to append to the icon path
 * 
780 781
 * Appends a directory to the search path. 
 * See gtk_icon_theme_set_search_path(). 
Matthias Clasen's avatar
Matthias Clasen committed
782 783
 *
 * Since: 2.4
784 785 786 787 788 789 790 791 792 793 794 795 796
 **/
void
gtk_icon_theme_append_search_path (GtkIconTheme *icon_theme,
				   const gchar  *path)
{
  GtkIconThemePrivate *priv;

  g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
  g_return_if_fail (path != NULL);

  priv = icon_theme->priv;
  
  priv->search_path_len++;
797

798 799 800 801 802 803 804 805 806 807 808
  priv->search_path = g_renew (gchar *, priv->search_path, priv->search_path_len);
  priv->search_path[priv->search_path_len-1] = g_strdup (path);

  do_theme_change (icon_theme);
}

/**
 * gtk_icon_theme_prepend_search_path:
 * @icon_theme: a #GtkIconTheme
 * @path: directory name to prepend to the icon path
 * 
809 810
 * Prepends a directory to the search path. 
 * See gtk_icon_theme_set_search_path().
Matthias Clasen's avatar
Matthias Clasen committed
811 812
 *
 * Since: 2.4
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
 **/
void
gtk_icon_theme_prepend_search_path (GtkIconTheme *icon_theme,
				    const gchar  *path)
{
  GtkIconThemePrivate *priv;
  int i;

  g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
  g_return_if_fail (path != NULL);

  priv = icon_theme->priv;
  
  priv->search_path_len++;
  priv->search_path = g_renew (gchar *, priv->search_path, priv->search_path_len);

829 830
  for (i = priv->search_path_len - 1; i > 0; i--)
    priv->search_path[i] = priv->search_path[i - 1];
831 832 833 834 835 836 837 838 839
  
  priv->search_path[0] = g_strdup (path);

  do_theme_change (icon_theme);
}

/**
 * gtk_icon_theme_set_custom_theme:
 * @icon_theme: a #GtkIconTheme
840 841
 * @theme_name: name of icon theme to use instead of configured theme,
 *   or %NULL to unset a previously set custom theme
842 843 844 845
 * 
 * Sets the name of the icon theme that the #GtkIconTheme object uses
 * overriding system configuration. This function cannot be called
 * on the icon theme objects returned from gtk_icon_theme_get_default()
846
 * and gtk_icon_theme_get_for_screen().
Matthias Clasen's avatar
Matthias Clasen committed
847 848
 *
 * Since: 2.4
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
 **/
void
gtk_icon_theme_set_custom_theme (GtkIconTheme *icon_theme,
				 const gchar  *theme_name)
{
  GtkIconThemePrivate *priv;

  g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));

  priv = icon_theme->priv;

  g_return_if_fail (!priv->is_screen_singleton);
  
  if (theme_name != NULL)
    {
      priv->custom_theme = TRUE;
865
      if (!priv->current_theme || strcmp (theme_name, priv->current_theme) != 0)
866 867 868 869 870 871 872 873 874
	{
	  g_free (priv->current_theme);
	  priv->current_theme = g_strdup (theme_name);

	  do_theme_change (icon_theme);
	}
    }
  else
    {
875 876 877
      if (priv->custom_theme)
	{
	  priv->custom_theme = FALSE;
878

879 880
	  update_current_theme (icon_theme);
	}
881 882 883 884 885 886 887 888 889 890 891
    }
}

static void
insert_theme (GtkIconTheme *icon_theme, const char *theme_name)
{
  int i;
  GList *l;
  char **dirs;
  char **themes;
  GtkIconThemePrivate *priv;
892
  IconTheme *theme = NULL;
893
  char *path;
894 895
  GKeyFile *theme_file;
  GError *error = NULL;
896 897 898 899
  IconThemeDirMtime *dir_mtime;
  struct stat stat_buf;
  
  priv = icon_theme->priv;
900

901 902 903 904 905 906 907 908 909 910 911 912
  for (l = priv->themes; l != NULL; l = l->next)
    {
      theme = l->data;
      if (strcmp (theme->name, theme_name) == 0)
	return;
    }
  
  for (i = 0; i < priv->search_path_len; i++)
    {
      path = g_build_filename (priv->search_path[i],
			       theme_name,
			       NULL);
Matthias Clasen's avatar
Matthias Clasen committed
913
      dir_mtime = g_slice_new (IconThemeDirMtime);
914
      dir_mtime->cache = NULL;
915
      dir_mtime->dir = path;
916
      if (g_stat (path, &stat_buf) == 0 && S_ISDIR (stat_buf.st_mode))
917 918 919 920 921 922
	dir_mtime->mtime = stat_buf.st_mtime;
      else
	dir_mtime->mtime = 0;

      priv->dir_mtimes = g_list_prepend (priv->dir_mtimes, dir_mtime);
    }
923
  priv->dir_mtimes = g_list_reverse (priv->dir_mtimes);
924 925

  theme_file = NULL;
926
  for (i = 0; i < priv->search_path_len && !theme_file; i++)
927 928 929 930 931
    {
      path = g_build_filename (priv->search_path[i],
			       theme_name,
			       "index.theme",
			       NULL);
932 933 934 935 936 937 938 939 940 941 942 943
      if (g_file_test (path, G_FILE_TEST_IS_REGULAR)) 
	{
	  theme_file = g_key_file_new ();
	  g_key_file_set_list_separator (theme_file, ',');
	  g_key_file_load_from_file (theme_file, path, 0, &error);
	  if (error)
	    {
	      g_key_file_free (theme_file);
	      theme_file = NULL;
	      g_error_free (error);
	      error = NULL;
	    }
944 945 946 947
	}
      g_free (path);
    }

948 949 950 951 952 953 954
  if (theme_file || strcmp (theme_name, DEFAULT_THEME_NAME) == 0)
    {
      theme = g_new0 (IconTheme, 1);
      theme->name = g_strdup (theme_name);
      priv->themes = g_list_prepend (priv->themes, theme);
    }

955 956
  if (theme_file == NULL)
    return;
957

958 959 960
  theme->display_name = 
    g_key_file_get_locale_string (theme_file, "Icon Theme", "Name", NULL, NULL);
  if (!theme->display_name)
961
    g_warning ("Theme file for %s has no name\n", theme_name);
962

963 964
  dirs = g_key_file_get_string_list (theme_file, "Icon Theme", "Directories", NULL, NULL);
  if (!dirs)
Matthias Clasen's avatar
Matthias Clasen committed
965 966
    {
      g_warning ("Theme file for %s has no directories\n", theme_name);
967 968
      priv->themes = g_list_remove (priv->themes, theme);
      g_free (theme->name);
Matthias Clasen's avatar
Matthias Clasen committed
969 970 971 972 973
      g_free (theme->display_name);
      g_free (theme);
      g_key_file_free (theme_file);
      return;
    }
974
  
975 976 977 978 979 980 981 982
  theme->comment = 
    g_key_file_get_locale_string (theme_file, 
				  "Icon Theme", "Comment",
				  NULL, NULL);
  theme->example = 
    g_key_file_get_string (theme_file, 
			   "Icon Theme", "Example",
			   NULL);
983 984

  theme->dirs = NULL;
Matthias Clasen's avatar
Matthias Clasen committed
985 986 987 988 989
  for (i = 0; dirs[i] != NULL; i++)
    theme_subdir_load (icon_theme, theme, theme_file, dirs[i]);

  g_strfreev (dirs);

990 991
  theme->dirs = g_list_reverse (theme->dirs);

992
  themes = g_key_file_get_string_list (theme_file,
993 994
				       "Icon Theme",
				       "Inherits",
995 996 997
				       NULL,
				       NULL);
  if (themes)
998 999 1000 1001 1002 1003 1004
    {
      for (i = 0; themes[i] != NULL; i++)
	insert_theme (icon_theme, themes[i]);
      
      g_strfreev (themes);
    }

1005
  g_key_file_free (theme_file);
1006 1007 1008 1009 1010
}

static void
free_unthemed_icon (UnthemedIcon *unthemed_icon)
{
1011 1012
  g_free (unthemed_icon->svg_filename);
  g_free (unthemed_icon->no_svg_filename);
1013
  g_slice_free (UnthemedIcon, unthemed_icon);
1014 1015
}

1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
static char *
strip_suffix (const char *filename)
{
  const char *dot;

  dot = strrchr (filename, '.');

  if (dot == NULL)
    return g_strdup (filename);

  return g_strndup (filename, dot - filename);
}

1029 1030 1031 1032 1033 1034
static void
load_themes (GtkIconTheme *icon_theme)
{
  GtkIconThemePrivate *priv;
  GDir *gdir;
  int base;
1035
  char *dir;
1036 1037 1038
  const char *file;
  UnthemedIcon *unthemed_icon;
  IconSuffix old_suffix, new_suffix;
1039
  GTimeVal tv;
1040 1041
  IconThemeDirMtime *dir_mtime;
  struct stat stat_buf;
1042 1043 1044 1045 1046
  
  priv = icon_theme->priv;

  priv->all_icons = g_hash_table_new (g_str_hash, g_str_equal);
  
1047 1048
  if (priv->current_theme)
    insert_theme (icon_theme, priv->current_theme);
1049 1050 1051 1052

  /* Always look in the "default" icon theme, and in a fallback theme */
  if (priv->fallback_theme)
    insert_theme (icon_theme, priv->fallback_theme);
1053 1054
  insert_theme (icon_theme, DEFAULT_THEME_NAME);
  priv->themes = g_list_reverse (priv->themes);
1055 1056


1057 1058 1059 1060 1061 1062
  priv->unthemed_icons = g_hash_table_new_full (g_str_hash, g_str_equal,
						g_free, (GDestroyNotify)free_unthemed_icon);

  for (base = 0; base < icon_theme->priv->search_path_len; base++)
    {
      dir = icon_theme->priv->search_path[base];
1063

Matthias Clasen's avatar
Matthias Clasen committed
1064
      dir_mtime = g_slice_new (IconThemeDirMtime);
1065
      priv->dir_mtimes = g_list_append (priv->dir_mtimes, dir_mtime);
1066
      
1067 1068 1069 1070 1071 1072 1073 1074 1075
      dir_mtime->dir = g_strdup (dir);
      dir_mtime->mtime = 0;
      dir_mtime->cache = NULL;

      if (g_stat (dir, &stat_buf) != 0 || !S_ISDIR (stat_buf.st_mode))
	continue;
      dir_mtime->mtime = stat_buf.st_mtime;

      dir_mtime->cache = _gtk_icon_cache_new_for_path (dir);
1076 1077
      if (dir_mtime->cache != NULL)
	continue;
1078

1079 1080 1081
      gdir = g_dir_open (dir, 0, NULL);
      if (gdir == NULL)
	continue;
1082

1083 1084 1085
      while ((file = g_dir_read_name (gdir)))
	{
	  new_suffix = suffix_from_name (file);
1086
	  
1087 1088
	  if (new_suffix != ICON_SUFFIX_NONE)
	    {
1089 1090 1091
	      char *abs_file;
	      char *base_name;

1092
	      abs_file = g_build_filename (dir, file, NULL);
1093 1094
	      base_name = strip_suffix (file);

1095 1096 1097 1098 1099
	      if ((unthemed_icon = g_hash_table_lookup (priv->unthemed_icons,
							base_name)))
		{
		  if (new_suffix == ICON_SUFFIX_SVG)
		    {
1100
		      if (unthemed_icon->svg_filename)
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
			g_free (abs_file);
		      else
			unthemed_icon->svg_filename = abs_file;
		    }
		  else
		    {
		      if (unthemed_icon->no_svg_filename)
			{
			  old_suffix = suffix_from_name (unthemed_icon->no_svg_filename);
			  if (new_suffix > old_suffix)
			    {
			      g_free (unthemed_icon->no_svg_filename);
			      unthemed_icon->no_svg_filename = abs_file;			      
			    }
			  else
			    g_free (abs_file);
			}
		      else
			unthemed_icon->no_svg_filename = abs_file;			      
		    }

		  g_free (base_name);
		}
	      else
		{
1126
		  unthemed_icon = g_slice_new0 (UnthemedIcon);
1127 1128 1129 1130
		  
		  if (new_suffix == ICON_SUFFIX_SVG)
		    unthemed_icon->svg_filename = abs_file;
		  else
1131
		    unthemed_icon->no_svg_filename = abs_file;
1132

1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
		  g_hash_table_insert (priv->unthemed_icons,
				       base_name,
				       unthemed_icon);
		  g_hash_table_insert (priv->all_icons,
				       base_name, NULL);
		}
	    }
	}
      g_dir_close (gdir);
    }

  priv->themes_valid = TRUE;
  
1146
  g_get_current_time(&tv);
1147 1148 1149
  priv->last_stat_time = tv.tv_sec;
}

Matthias Clasen's avatar
Matthias Clasen committed
1150
void
1151 1152 1153 1154
_gtk_icon_theme_ensure_builtin_cache (void)
{
  static gboolean initialized = FALSE;
  IconThemeDir *dir;
1155 1156
  static IconThemeDir dirs[5] = 
    {
1157 1158 1159 1160 1161
      { ICON_THEME_DIR_THRESHOLD, 0, 16, 16, 16, 2, NULL, "16", -1, NULL, NULL, NULL },
      { ICON_THEME_DIR_THRESHOLD, 0, 20, 20, 20, 2, NULL, "20", -1,  NULL, NULL, NULL },
      { ICON_THEME_DIR_THRESHOLD, 0, 24, 24, 24, 2, NULL, "24", -1, NULL, NULL, NULL },
      { ICON_THEME_DIR_THRESHOLD, 0, 32, 32, 32, 2, NULL, "32", -1, NULL, NULL, NULL },
      { ICON_THEME_DIR_THRESHOLD, 0, 48, 48, 48, 2, NULL, "48", -1, NULL, NULL, NULL }
1162
    };
1163 1164 1165 1166 1167 1168 1169 1170
  gint i;

  if (!initialized)
    {
      initialized = TRUE;

      _builtin_cache = _gtk_icon_cache_new ((gchar *)builtin_icons);

1171
      for (i = 0; i < G_N_ELEMENTS (dirs); i++)
1172
	{
1173
	  dir = &(dirs[i]);
1174
	  dir->cache = _gtk_icon_cache_ref (_builtin_cache);
1175
          dir->subdir_index = _gtk_icon_cache_get_directory_index (dir->cache, dir->subdir);
1176 1177 1178 1179 1180 1181

	  builtin_dirs = g_list_append (builtin_dirs, dir);
	}
    }
}

1182 1183 1184 1185
static void
ensure_valid_themes (GtkIconTheme *icon_theme)
{
  GtkIconThemePrivate *priv = icon_theme->priv;
1186
  GTimeVal tv;
1187
  gboolean was_valid = priv->themes_valid;
1188

1189 1190 1191 1192
  if (priv->loading_themes)
    return;
  priv->loading_themes = TRUE;

1193 1194
  _gtk_icon_theme_ensure_builtin_cache ();

1195 1196
  if (priv->themes_valid)
    {
Matthias Clasen's avatar
Matthias Clasen committed
1197
      g_get_current_time (&tv);
1198

1199 1200 1201
      if (ABS (tv.tv_sec - priv->last_stat_time) > 5 &&
	  rescan_themes (icon_theme))
	blow_themes (icon_theme);
1202 1203 1204
    }
  
  if (!priv->themes_valid)
1205 1206 1207
    {
      load_themes (icon_theme);

1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
      if (was_valid)
	{
	  g_signal_emit (icon_theme, signal_changed, 0);

	  if (!priv->check_reload && priv->screen)
	    {	  
	      static GdkAtom atom_iconthemes = GDK_NONE;
	      GdkEvent *event = gdk_event_new (GDK_CLIENT_EVENT);
	      int i;

	      if (!atom_iconthemes)
		atom_iconthemes = gdk_atom_intern_static_string ("_GTK_LOAD_ICONTHEMES");
1220

1221 1222 1223 1224
	      for (i = 0; i < 5; i++)
		event->client.data.l[i] = 0;
	      event->client.data_format = 32;
	      event->client.message_type = atom_iconthemes;
1225

1226 1227
	      gdk_screen_broadcast_client_message (priv->screen, event);
	    }
1228 1229
	}
    }
1230 1231

  priv->loading_themes = FALSE;
1232 1233
}

1234 1235 1236 1237 1238
static GtkIconInfo *
choose_icon (GtkIconTheme       *icon_theme,
	     const gchar        *icon_names[],
	     gint                size,
	     GtkIconLookupFlags  flags)
1239 1240 1241 1242
{
  GtkIconThemePrivate *priv;
  GList *l;
  GtkIconInfo *icon_info = NULL;
1243
  UnthemedIcon *unthemed_icon = NULL;
1244 1245
  gboolean allow_svg;
  gboolean use_builtin;
1246
  gint i;
Matthias Clasen's avatar
Matthias Clasen committed
1247

1248 1249 1250 1251 1252 1253 1254 1255 1256
  priv = icon_theme->priv;

  if (flags & GTK_ICON_LOOKUP_NO_SVG)
    allow_svg = FALSE;
  else if (flags & GTK_ICON_LOOKUP_FORCE_SVG)
    allow_svg = TRUE;
  else
    allow_svg = priv->pixbuf_supports_svg;

1257 1258
  use_builtin = flags & GTK_ICON_LOOKUP_USE_BUILTIN;
  
1259 1260
  ensure_valid_themes (icon_theme);

1261
  for (l = priv->themes; l; l = l->next)
1262
    {
Matthias Clasen's avatar
Matthias Clasen committed
1263
      IconTheme *theme = l->data;
1264
      
1265
      for (i = 0; icon_names[i]; i++)
1266
        {
1267 1268 1269
          icon_info = theme_lookup_icon (theme, icon_names[i], size, allow_svg, use_builtin);
          if (icon_info)
            goto out;
1270
        }
1271
    }
1272

1273 1274 1275 1276 1277
  for (i = 0; icon_names[i]; i++)
    {
      unthemed_icon = g_hash_table_lookup (priv->unthemed_icons, icon_names[i]);
      if (unthemed_icon)
        break;
1278
    }
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
#ifdef G_OS_WIN32
  /* Still not found an icon, check if reference to a Win32 resource */
  if (!unthemed_icon)
    {
      gchar **resources;
      HICON hIcon = NULL;
      
      resources = g_strsplit (icon_names[0], ",", 0);
      if (resources[0])
	{
	  wchar_t *wfile = g_utf8_to_utf16 (resources[0], -1, NULL, NULL, NULL);
	  ExtractIconExW (wfile, resources[1] ? atoi (resources[1]) : 0, &hIcon, NULL, 1);
	  g_free (wfile);
	}
      
      if (hIcon)
	{
	  icon_info = icon_info_new ();
	  icon_info->cache_pixbuf = gdk_win32_icon_to_pixbuf_libgtk_only (hIcon);
	  DestroyIcon (hIcon);
          icon_info->dir_type = ICON_THEME_DIR_UNTHEMED;
          icon_info->dir_size = size;
	}
      g_strfreev (resources);
    }
#endif
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321

  if (unthemed_icon)
    {
      icon_info = icon_info_new ();

      /* A SVG icon, when allowed, beats out a XPM icon, but not
       * a PNG icon
       */
      if (allow_svg &&
	  unthemed_icon->svg_filename &&
	  (!unthemed_icon->no_svg_filename ||
	   suffix_from_name (unthemed_icon->no_svg_filename) != ICON_SUFFIX_PNG))
	icon_info->filename = g_strdup (unthemed_icon->svg_filename);
      else if (unthemed_icon->no_svg_filename)
	icon_info->filename = g_strdup (unthemed_icon->no_svg_filename);

      icon_info->dir_type = ICON_THEME_DIR_UNTHEMED;
1322
      icon_info->dir_size = size;
1323 1324 1325
    }

 out:
1326 1327 1328 1329 1330
  if (icon_info) 
    {
      icon_info->desired_size = size;
      icon_info->forced_size = (flags & GTK_ICON_LOOKUP_FORCE_SIZE) != 0;
    }
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
  else
    {
      static gboolean check_for_default_theme = TRUE;
      char *default_theme_path;
      gboolean found = FALSE;
      unsigned i;

      if (check_for_default_theme)
	{
	  check_for_default_theme = FALSE;

	  for (i = 0; !found && i < priv->search_path_len; i++)
	    {
	      default_theme_path = g_build_filename (priv->search_path[i],
						     DEFAULT_THEME_NAME,
						     "index.theme",
						     NULL);
	      found = g_file_test (default_theme_path, G_FILE_TEST_IS_REGULAR);
	      g_free (default_theme_path);
	    }
1351

1352 1353
	  if (!found)
	    {
1354
	      g_warning (_("Could not find the icon '%s'. The '%s' theme\n"
1355
			   "was not found either, perhaps you need to install it.\n"
1356 1357
			   "You can get a copy from:\n"
			   "\t%s"),
1358
			 icon_names[0], DEFAULT_THEME_NAME, "http://icon-theme.freedesktop.org/releases");
1359 1360 1361
	    }
	}
    }
1362 1363 1364 1365

  return icon_info;
}

1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408

/**
 * gtk_icon_theme_lookup_icon:
 * @icon_theme: a #GtkIconTheme
 * @icon_name: the name of the icon to lookup
 * @size: desired icon size
 * @flags: flags modifying the behavior of the icon lookup
 * 
 * Looks up a named icon and returns a structure containing
 * information such as the filename of the icon. The icon
 * can then be rendered into a pixbuf using
 * gtk_icon_info_load_icon(). (gtk_icon_theme_load_icon()
 * combines these two steps if all you need is the pixbuf.)
 * 
 * Return value: a #GtkIconInfo structure containing information
 * about the icon, or %NULL if the icon wasn't found. Free with
 * gtk_icon_info_free()
 *
 * Since: 2.4
 */
GtkIconInfo *
gtk_icon_theme_lookup_icon (GtkIconTheme       *icon_theme,
			    const gchar        *icon_name,
			    gint                size,
			    GtkIconLookupFlags  flags)
{
  GtkIconInfo *info;

  g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
  g_return_val_if_fail (icon_name != NULL, NULL);
  g_return_val_if_fail ((flags & GTK_ICON_LOOKUP_NO_SVG) == 0 ||
			(flags & GTK_ICON_LOOKUP_FORCE_SVG) == 0, NULL);

  GTK_NOTE (ICONTHEME, 
	    g_print ("gtk_icon_theme_lookup_icon %s\n", icon_name));

  if (flags & GTK_ICON_LOOKUP_GENERIC_FALLBACK)
    {
      gchar **names;
      gint dashes, i;
      gchar *p;
 
      dashes = 0;
1409
      for (p = (gchar *) icon_name; *p; p++)
1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422
        if (*p == '-')
          dashes++;

      names = g_new (gchar *, dashes + 2);
      names[0] = g_strdup (icon_name);
      for (i = 1; i <= dashes; i++)
        {
          names[i] = g_strdup (names[i - 1]);
          p = strrchr (names[i], '-');
          *p = '\0';
        }
      names[dashes + 1] = NULL;
   
1423
      info = choose_icon (icon_theme, (const gchar **) names, size, flags);
1424 1425 1426 1427 1428
      
      g_strfreev (names);
    }
  else 
    {
1429
      const gchar *names[2];
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476
      
      names[0] = icon_name;
      names[1] = NULL;

      info = choose_icon (icon_theme, names, size, flags);
    }

  return info;
}

/**
 * gtk_icon_theme_choose_icon:
 * @icon_theme: a #GtkIconTheme
 * @icon_names: %NULL-terminated array of icon names to lookup
 * @size: desired icon size
 * @flags: flags modifying the behavior of the icon lookup
 * 
 * Looks up a named icon and returns a structure containing
 * information such as the filename of the icon. The icon
 * can then be rendered into a pixbuf using
 * gtk_icon_info_load_icon(). (gtk_icon_theme_load_icon()
 * combines these two steps if all you need is the pixbuf.)
 *
 * If @icon_names contains more than one name, this function 
 * tries them all in the given order before falling back to 
 * inherited icon themes.
 * 
 * Return value: a #GtkIconInfo structure containing information
 * about the icon, or %NULL if the icon wasn't found. Free with
 * gtk_icon_info_free()
 *
 * Since: 2.12
 */
GtkIconInfo *
gtk_icon_theme_choose_icon (GtkIconTheme       *icon_theme,
			    const gchar        *icon_names[],
			    gint                size,
			    GtkIconLookupFlags  flags)
{
  g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
  g_return_val_if_fail (icon_names != NULL, NULL);
  g_return_val_if_fail ((flags & GTK_ICON_LOOKUP_NO_SVG) == 0 ||
			(flags & GTK_ICON_LOOKUP_FORCE_SVG) == 0, NULL);

  return choose_icon (icon_theme, icon_names, size, flags);
}

1477 1478 1479 1480
/* Error quark */
GQuark
gtk_icon_theme_error_quark (void)
{
1481
  return g_quark_from_static_string ("gtk-icon-theme-error-quark");
1482 1483 1484 1485 1486 1487 1488
}

/**
 * gtk_icon_theme_load_icon:
 * @icon_theme: a #GtkIconTheme
 * @icon_name: the name of the icon to lookup
 * @size: the desired icon size. The resulting icon may not be
1489
 *     exactly this size; see gtk_icon_info_load_icon().
1490
 * @flags: flags modifying the behavior of the icon lookup
1491 1492 1493
 * @error: (allow-none): Location to store error information on failure,
 *     or %NULL.
 *
1494 1495 1496 1497
 * Looks up an icon in an icon theme, scales it to the given size
 * and renders it into a pixbuf. This is a convenience function;
 * if more details about the icon are needed, use
 * gtk_icon_theme_lookup_icon() followed by gtk_icon_info_load_icon().
1498 1499
 *
 * Note that you probably want to listen for icon theme changes and
1500
 * update the icon. This is usually done by connecting to the
1501 1502 1503
 * GtkWidget::style-set signal. If for some reason you do not want to
 * update the icon when the icon theme changes, you should consider
 * using gdk_pixbuf_copy() to make a private copy of the pixbuf
1504
 * returned by this function. Otherwise GTK+ may need to keep the old
1505
 * icon theme loaded, which would be a waste of memory.
1506 1507 1508 1509 1510
 *
 * Return value: (transfer full): the rendered icon; this may be a
 *     newly created icon or a new reference to an internal icon, so
 *     you must not modify the icon. Use g_object_unref() to release
 *     your reference to the icon. %NULL if the icon isn't found.
Matthias Clasen's avatar
Matthias Clasen committed
1511 1512
 *
 * Since: 2.4
1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
 **/
GdkPixbuf *
gtk_icon_theme_load_icon (GtkIconTheme         *icon_theme,
			  const gchar          *icon_name,
			  gint                  size,
			  GtkIconLookupFlags    flags,
			  GError              **error)
{
  GtkIconInfo *icon_info;
  GdkPixbuf *pixbuf = NULL;
  
  g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
  g_return_val_if_fail (icon_name != NULL, NULL);
  g_return_val_if_fail ((flags & GTK_ICON_LOOKUP_NO_SVG) == 0 ||
			(flags & GTK_ICON_LOOKUP_FORCE_SVG) == 0, NULL);
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
  
Matthias Clasen's avatar
Matthias Clasen committed
1530 1531
  icon_info = gtk_icon_theme_lookup_icon (icon_theme, icon_name, size,
				          flags | GTK_ICON_LOOKUP_USE_BUILTIN);
1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554
  if (!icon_info)
    {
      g_set_error (error, GTK_ICON_THEME_ERROR,  GTK_ICON_THEME_NOT_FOUND,
		   _("Icon '%s' not present in theme"), icon_name);
      return NULL;
    }

  pixbuf = gtk_icon_info_load_icon (icon_info, error);
  gtk_icon_info_free (icon_info);

  return pixbuf;
}

/**
 * gtk_icon_theme_has_icon:
 * @icon_theme: a #GtkIconTheme
 * @icon_name: the name of an icon
 * 
 * Checks whether an icon theme includes an icon
 * for a particular name.
 * 
 * Return value: %TRUE if @icon_theme includes an
 *  icon for @icon_name.
Matthias Clasen's avatar
Matthias Clasen committed
1555 1556
 *
 * Since: 2.4
1557 1558 1559 1560 1561 1562
 **/
gboolean 
gtk_icon_theme_has_icon (GtkIconTheme *icon_theme,
			 const char   *icon_name)
{
  GtkIconThemePrivate *priv;
1563
  GList *l;
1564 1565

  g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), FALSE);
1566 1567
  g_return_val_if_fail (icon_name != NULL, FALSE);

1568 1569 1570 1571
  priv = icon_theme->priv;
  
  ensure_valid_themes (icon_theme);

1572
  for (l = priv->dir_mtimes; l; l = l->next)
1573
    {
1574 1575 1576 1577
      IconThemeDirMtime *dir_mtime = l->data;
      GtkIconCache *cache = dir_mtime->cache;
      
      if (cache && _gtk_icon_cache_has_icon (cache, icon_name))
1578 1579 1580
	return TRUE;
    }

1581 1582 1583
  if (g_hash_table_lookup_extended (priv->all_icons,
				    icon_name, NULL, NULL))
    return TRUE;
1584

1585 1586 1587 1588
  if (_builtin_cache &&
      _gtk_icon_cache_has_icon (_builtin_cache, icon_name))
    return TRUE;

1589 1590
  if (icon_theme_builtin_icons &&
      g_hash_table_lookup_extended (icon_theme_builtin_icons,
1591 1592 1593 1594 1595 1596
				    icon_name, NULL, NULL))
    return TRUE;

  return FALSE;
}

1597 1598 1599 1600 1601 1602 1603 1604 1605
static void
add_size (gpointer  key,
	  gpointer  value,
	  gpointer  user_data)
{
  gint **res_p = user_data;

  **res_p = GPOINTER_TO_INT (key);

1606
  (*res_p)++;
1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619
}

/**
 * gtk_icon_theme_get_icon_sizes:
 * @icon_theme: a #GtkIconTheme
 * @icon_name: the name of an icon
 * 
 * Returns an array of integers describing the sizes at which
 * the icon is available without scaling. A size of -1 means 
 * that the icon is available in a scalable format. The array 
 * is zero-terminated.
 * 
 * Return value: An newly allocated array describing the sizes at
1620 1621
 * which the icon is available. The array should be freed with g_free()
 * when it is no longer needed.
1622 1623 1624 1625 1626 1627 1628
 *
 * Since: 2.6
 **/
gint *
gtk_icon_theme_get_icon_sizes (GtkIconTheme *icon_theme,
			       const char   *icon_name)
{
1629
  GList *l, *d, *icons;
1630
  GHashTable *sizes;
Matthias Clasen's avatar
Matthias Clasen committed
1631
  gint *result, *r;
1632
  guint suffix;  
1633 1634
  GtkIconThemePrivate *priv;

1635
  g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648
  
  priv = icon_theme->priv;

  ensure_valid_themes (icon_theme);

  sizes = g_hash_table_new (g_direct_hash, g_direct_equal);

  for (l = priv->themes; l; l = l->next)
    {
      IconTheme *theme = l->data;
      for (d = theme->dirs; d; d = d->next)
	{
	  IconThemeDir *dir = d->data;
1649

1650 1651 1652
          if (dir->type != ICON_THEME_DIR_SCALABLE && g_hash_table_lookup_extended (sizes, GINT_TO_POINTER (dir->size), NULL, NULL))
            continue;

1653
	  suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);	  
1654 1655 1656 1657 1658 1659 1660 1661 1662 1663
	  if (suffix != ICON_SUFFIX_NONE)
	    {
	      if (suffix == ICON_SUFFIX_SVG)
		g_hash_table_insert (sizes, GINT_TO_POINTER (-1), NULL);
	      else
		g_hash_table_insert (sizes, GINT_TO_POINTER (dir->size), NULL);
	    }
	}
    }

1664 1665 1666 1667
  for (d = builtin_dirs; d; d = d->next)
    {
      IconThemeDir *dir = d->data;
      
1668 1669 1670
      if (dir->type != ICON_THEME_DIR_SCALABLE && g_hash_table_lookup_extended (sizes, GINT_TO_POINTER (dir->size), NULL, NULL))
        continue;

1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684
      suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);	  
      if (suffix != ICON_SUFFIX_NONE)
	{
	  if (suffix == ICON_SUFFIX_SVG)
	    g_hash_table_insert (sizes, GINT_TO_POINTER (-1), NULL);
	  else
	    g_hash_table_insert (sizes, GINT_TO_POINTER (dir->size), NULL);
	}
    }

  if (icon_theme_builtin_icons)
    {
      icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
      
Matthias Clasen's avatar
Matthias Clasen committed
1685 1686 1687
      while (icons)
        {
	  BuiltinIcon *icon = icons->data;
1688
	
Matthias Clasen's avatar
Matthias Clasen committed
1689 1690 1691
	  g_hash_table_insert (sizes, GINT_TO_POINTER (icon->size), NULL);
          icons = icons->next;
        }      
1692 1693
    }

Matthias Clasen's avatar
Matthias Clasen committed
1694
  r = result = g_new0 (gint, g_hash_table_size (sizes) + 1);
1695

Matthias Clasen's avatar
Matthias Clasen committed
1696
  g_hash_table_foreach (sizes, add_size, &r);
1697 1698 1699 1700
  g_hash_table_destroy (sizes);
  
  return result;
}
1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730

static void
add_key_to_hash (gpointer  key,
		 gpointer  value,
		 gpointer  user_data)
{
  GHashTable *hash = user_data;

  g_hash_table_insert (hash, key, NULL);
}

static void
add_key_to_list (gpointer  key,
		 gpointer  value,
		 gpointer  user_data)
{
  GList **list = user_data;

  *list = g_list_prepend (*list, g_strdup (key));
}

/**
 * gtk_icon_theme_list_icons:
 * @icon_theme: a #GtkIconTheme
 * @context: a string identifying a particular type of icon,
 *           or %NULL to list all icons.
 * 
 * Lists the icons in the current icon theme. Only a subset
 * of the icons can be listed by providing a context string.
 * The set of values for the context string is system dependent,
1731 1732
 * but will typically include such values as "Applications" and
 * "MimeTypes".
1733 1734
 *
 * Return value: (element-type utf8) (transfer none): a #GList list holding the names of all the
1735 1736 1737
 *  icons in the theme. You must first free each element
 *  in the list with g_free(), then free the list itself
 *  with g_list_free().
Matthias Clasen's avatar
Matthias Clasen committed
1738 1739
 *
 * Since: 2.4
1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777
 **/
GList *
gtk_icon_theme_list_icons (GtkIconTheme *icon_theme,
			   const char   *context)
{
  GtkIconThemePrivate *priv;
  GHashTable *icons;
  GList *list, *l;
  GQuark context_quark;
  
  priv = icon_theme->priv;
  
  ensure_valid_themes (icon_theme);

  if (context)
    {
      context_quark = g_quark_try_string (context);

      if (!context_quark)
	return NULL;
    }
  else
    context_quark = 0;

  icons = g_hash_table_new (g_str_hash, g_str_equal);
  
  l = priv->themes;
  while (l != NULL)
    {
      theme_list_icons (l->data, icons, context_quark);
      l = l->next;
    }

  if (context_quark == 0)
    g_hash_table_foreach (priv->unthemed_icons,
			  add_key_to_hash,
			  icons);

1778
  list = NULL;
1779 1780 1781 1782 1783 1784 1785 1786 1787 1788
  
  g_hash_table_foreach (icons,
			add_key_to_list,
			&list);

  g_hash_table_destroy (icons);
  
  return list;
}

1789 1790 1791 1792 1793 1794 1795
/**
 * gtk_icon_theme_list_contexts:
 * @icon_theme: a #GtkIconTheme
 *
 * Gets the list of contexts available within the current
 * hierarchy of icon themes
 *
1796
 * Return value: (element-type utf8) (transfer full): a #GList list holding the names of all the
1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833
 *  contexts in the theme. You must first free each element
 *  in the list with g_free(), then free the list itself
 *  with g_list_free().
 *
 * Since: 2.12
 **/
GList *
gtk_icon_theme_list_contexts (GtkIconTheme *icon_theme)
{
  GtkIconThemePrivate *priv;
  GHashTable *contexts;
  GList *list, *l;

  priv = icon_theme->priv;
  
  ensure_valid_themes (icon_theme);

  contexts = g_hash_table_new (g_str_hash, g_str_equal);

  l = priv->themes;
  while (l != NULL)
    {
      theme_list_contexts (l->data