]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkicontheme.c
build with GTK_DISABLE_DEPRECATED.
[~andy/gtk] / gtk / gtkicontheme.c
index 885df5381a9fbe3380faeb725f847ce723eddd3f..43666a4c2a87056d15e6cc9e69f89064898bcba1 100644 (file)
@@ -33,6 +33,9 @@
 #ifndef S_ISDIR
 #define S_ISDIR(mode) ((mode)&_S_IFDIR)
 #endif
+#define WIN32_MEAN_AND_LEAN
+#include <windows.h>
+#include "win32/gdkwin32.h"
 #endif /* G_OS_WIN32 */
 
 #include "gtkicontheme.h"
@@ -40,6 +43,7 @@
 #include "gtkiconcache.h"
 #include "gtkbuiltincache.h"
 #include "gtkintl.h"
+#include "gtkmain.h"
 #include "gtksettings.h"
 #include "gtkprivate.h"
 #include "gtkalias.h"
@@ -67,15 +71,18 @@ typedef enum
 
 struct _GtkIconThemePrivate
 {
-  guint custom_theme : 1;
+  guint custom_theme        : 1;
   guint is_screen_singleton : 1;
   guint pixbuf_supports_svg : 1;
+  guint themes_valid        : 1;
+  guint check_reload        : 1;
+  guint loading_themes      : 1;
   
   char *current_theme;
+  char *fallback_theme;
   char **search_path;
   int search_path_len;
 
-  gboolean themes_valid;
   /* A list of all the themes needed to look up icons.
    * In search order, without duplicates
    */
@@ -96,8 +103,6 @@ struct _GtkIconThemePrivate
   GList *dir_mtimes;
 
   gulong reset_styles_idle;
-
-  gboolean check_reload;
 };
 
 struct _GtkIconInfo
@@ -111,6 +116,8 @@ struct _GtkIconInfo
    */
   gchar *cp_filename;
 #endif
+  GLoadableIcon *loadable;
+
   /* Cache pixbuf (if there is any) */
   GdkPixbuf *cache_pixbuf;
 
@@ -126,7 +133,8 @@ struct _GtkIconInfo
   /* Parameters influencing the scaled icon
    */
   gint desired_size;
-  gboolean raw_coordinates;
+  guint raw_coordinates : 1;
+  guint forced_size     : 1;
 
   /* Cached information if we go ahead and try to load
    * the icon.
@@ -134,6 +142,8 @@ struct _GtkIconInfo
   GdkPixbuf *pixbuf;
   GError *load_error;
   gdouble scale;
+
+  guint ref_count;
 };
 
 typedef struct
@@ -159,6 +169,7 @@ typedef struct
 
   char *dir;
   char *subdir;
+  int subdir_index;
   
   GtkIconCache *cache;
   
@@ -186,8 +197,6 @@ typedef struct
   GtkIconCache *cache;
 } IconThemeDirMtime;
 
-static void  gtk_icon_theme_class_init (GtkIconThemeClass    *klass);
-static void  gtk_icon_theme_init       (GtkIconTheme         *icon_theme);
 static void  gtk_icon_theme_finalize   (GObject              *object);
 static void  theme_dir_destroy         (IconThemeDir         *dir);
 
@@ -200,13 +209,16 @@ static GtkIconInfo *theme_lookup_icon (IconTheme        *theme,
 static void         theme_list_icons  (IconTheme        *theme,
                                       GHashTable       *icons,
                                       GQuark            context);
+static void         theme_list_contexts  (IconTheme        *theme,
+                                         GHashTable       *contexts);
 static void         theme_subdir_load (GtkIconTheme     *icon_theme,
                                       IconTheme        *theme,
                                       GKeyFile         *theme_file,
                                       char             *subdir);
 static void         do_theme_change   (GtkIconTheme     *icon_theme);
 
-static void  blow_themes               (GtkIconTheme    *icon_themes);
+static void     blow_themes               (GtkIconTheme    *icon_themes);
+static gboolean rescan_themes             (GtkIconTheme    *icon_themes);
 
 static void  icon_data_free            (GtkIconData     *icon_data);
 static void load_icon_data             (IconThemeDir    *dir,
@@ -228,8 +240,6 @@ static BuiltinIcon *find_builtin_icon (const gchar *icon_name,
                                       gint        *min_difference_p,
                                       gboolean    *has_larger_p);
 
-static GObjectClass *parent_class = NULL;
-
 static guint signal_changed = 0;
 
 static GHashTable *icon_theme_builtin_icons;
@@ -238,32 +248,7 @@ static GHashTable *icon_theme_builtin_icons;
 GtkIconCache *_builtin_cache = NULL;
 static GList *builtin_dirs = NULL;
 
-
-GType
-gtk_icon_theme_get_type (void)
-{
-  static GType type = 0;
-
-  if (type == 0)
-    {
-      static const GTypeInfo info =
-       {
-         sizeof (GtkIconThemeClass),
-         NULL,           /* base_init */
-         NULL,           /* base_finalize */
-         (GClassInitFunc) gtk_icon_theme_class_init,
-         NULL,           /* class_finalize */
-         NULL,           /* class_data */
-         sizeof (GtkIconTheme),
-         0,              /* n_preallocs */
-         (GInstanceInitFunc) gtk_icon_theme_init,
-       };
-
-      type = g_type_register_static (G_TYPE_OBJECT, I_("GtkIconTheme"), &info, 0);
-    }
-
-  return type;
-}
+G_DEFINE_TYPE (GtkIconTheme, gtk_icon_theme, G_TYPE_OBJECT)
 
 /**
  * gtk_icon_theme_new:
@@ -353,8 +338,6 @@ gtk_icon_theme_class_init (GtkIconThemeClass *klass)
 {
   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
 
-  parent_class = g_type_class_peek_parent (klass);
-
   gobject_class->finalize = gtk_icon_theme_finalize;
 
 /**
@@ -407,31 +390,53 @@ display_closed (GdkDisplay   *display,
 static void
 update_current_theme (GtkIconTheme *icon_theme)
 {
+#define theme_changed(_old, _new) \
+  ((_old && !_new) || (!_old && _new) || \
+   (_old && _new && strcmp (_old, _new) != 0))
   GtkIconThemePrivate *priv = icon_theme->priv;
 
   if (!priv->custom_theme)
     {
       gchar *theme = NULL;
+      gchar *fallback_theme = NULL;
+      gboolean changed = FALSE;
 
       if (priv->screen)
        {
          GtkSettings *settings = gtk_settings_get_for_screen (priv->screen);
-         g_object_get (settings, "gtk-icon-theme-name", &theme, NULL);
+         g_object_get (settings, 
+                       "gtk-icon-theme-name", &theme, 
+                       "gtk-fallback-icon-theme", &fallback_theme, NULL);
        }
 
-      if (!theme)
+      /* ensure that the current theme (even when just the default)
+       * is searched before any fallback theme
+       */
+      if (!theme && fallback_theme)
        theme = g_strdup (DEFAULT_THEME_NAME);
 
-      if (strcmp (priv->current_theme, theme) != 0)
+      if (theme_changed (priv->current_theme, theme))
        {
          g_free (priv->current_theme);
          priv->current_theme = theme;
-
-         do_theme_change (icon_theme);
+         changed = TRUE;
        }
       else
        g_free (theme);
+
+      if (theme_changed (priv->fallback_theme, fallback_theme))
+       {
+         g_free (priv->fallback_theme);
+         priv->fallback_theme = fallback_theme;
+         changed = TRUE;
+       }
+      else
+       g_free (fallback_theme);
+
+      if (changed)
+       do_theme_change (icon_theme);
     }
+#undef theme_changed
 }
 
 /* Callback when the icon theme GtkSetting changes
@@ -504,6 +509,8 @@ gtk_icon_theme_set_screen (GtkIconTheme *icon_theme,
                        G_CALLBACK (display_closed), icon_theme);
       g_signal_connect (settings, "notify::gtk-icon-theme-name",
                        G_CALLBACK (theme_changed), icon_theme);
+      g_signal_connect (settings, "notify::gtk-fallback-icon-theme-name",
+                       G_CALLBACK (theme_changed), icon_theme);
     }
 
   update_current_theme (icon_theme);
@@ -515,14 +522,16 @@ gtk_icon_theme_set_screen (GtkIconTheme *icon_theme,
 static gboolean
 pixbuf_supports_svg (void)
 {
-  GSList *formats = gdk_pixbuf_get_formats ();
+  GSList *formats;
   GSList *tmp_list;
-  static gboolean found_svg = FALSE;
-  static gboolean value_known = FALSE;
+  static gint found_svg = -1;
 
-  if (value_known)
+  if (found_svg != -1)
     return found_svg;
-  
+
+  formats = gdk_pixbuf_get_formats ();
+
+  found_svg = FALSE; 
   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);
@@ -538,7 +547,6 @@ pixbuf_supports_svg (void)
     }
 
   g_slist_free (formats);
-  value_known = TRUE;
   
   return found_svg;
 }
@@ -555,7 +563,6 @@ gtk_icon_theme_init (GtkIconTheme *icon_theme)
   icon_theme->priv = priv;
 
   priv->custom_theme = FALSE;
-  priv->current_theme = g_strdup (DEFAULT_THEME_NAME);
 
   xdg_data_dirs = g_get_system_data_dirs ();
   for (i = 0; xdg_data_dirs[i]; i++) ;
@@ -588,7 +595,7 @@ free_dir_mtime (IconThemeDirMtime *dir_mtime)
     _gtk_icon_cache_unref (dir_mtime->cache);
 
   g_free (dir_mtime->dir);
-  g_free (dir_mtime);
+  g_slice_free (IconThemeDirMtime, dir_mtime);
 
 }
 
@@ -598,8 +605,6 @@ reset_styles_idle (gpointer user_data)
   GtkIconTheme *icon_theme;
   GtkIconThemePrivate *priv;
 
-  GDK_THREADS_ENTER ();
-
   icon_theme = GTK_ICON_THEME (user_data);
   priv = icon_theme->priv;
 
@@ -611,8 +616,6 @@ reset_styles_idle (gpointer user_data)
 
   priv->reset_styles_idle = 0;
 
-  GDK_THREADS_LEAVE ();
-
   return FALSE;
 }
 
@@ -620,6 +623,9 @@ static void
 do_theme_change (GtkIconTheme *icon_theme)
 {
   GtkIconThemePrivate *priv = icon_theme->priv;
+
+  if (!priv->themes_valid)
+    return;
   
   GTK_NOTE (ICONTHEME, 
            g_print ("change to icon theme \"%s\"\n", priv->current_theme));
@@ -628,7 +634,8 @@ do_theme_change (GtkIconTheme *icon_theme)
 
   if (!priv->reset_styles_idle)
     priv->reset_styles_idle = 
-      g_idle_add (reset_styles_idle, icon_theme);
+      gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE - 2, 
+                      reset_styles_idle, icon_theme, NULL);
 }
 
 static void
@@ -673,7 +680,7 @@ gtk_icon_theme_finalize (GObject *object)
   g_free (priv->current_theme);
   priv->current_theme = NULL;
 
-  for (i=0; i < priv->search_path_len; i++)
+  for (i = 0; i < priv->search_path_len; i++)
     g_free (priv->search_path[i]);
 
   g_free (priv->search_path);
@@ -681,7 +688,7 @@ gtk_icon_theme_finalize (GObject *object)
 
   blow_themes (icon_theme);
 
-  G_OBJECT_CLASS (parent_class)->finalize (object);  
+  G_OBJECT_CLASS (gtk_icon_theme_parent_class)->finalize (object);  
 }
 
 /**
@@ -725,6 +732,7 @@ gtk_icon_theme_set_search_path (GtkIconTheme *icon_theme,
 
   priv->search_path = g_new (gchar *, n_elements);
   priv->search_path_len = n_elements;
+
   for (i = 0; i < priv->search_path_len; i++)
     priv->search_path[i] = g_strdup (path[i]);
 
@@ -790,6 +798,7 @@ gtk_icon_theme_append_search_path (GtkIconTheme *icon_theme,
   priv = icon_theme->priv;
   
   priv->search_path_len++;
+
   priv->search_path = g_renew (gchar *, priv->search_path, priv->search_path_len);
   priv->search_path[priv->search_path_len-1] = g_strdup (path);
 
@@ -857,7 +866,7 @@ gtk_icon_theme_set_custom_theme (GtkIconTheme *icon_theme,
   if (theme_name != NULL)
     {
       priv->custom_theme = TRUE;
-      if (strcmp (theme_name, priv->current_theme) != 0)
+      if (!priv->current_theme || strcmp (theme_name, priv->current_theme) != 0)
        {
          g_free (priv->current_theme);
          priv->current_theme = g_strdup (theme_name);
@@ -867,9 +876,12 @@ gtk_icon_theme_set_custom_theme (GtkIconTheme *icon_theme,
     }
   else
     {
-      priv->custom_theme = FALSE;
+      if (priv->custom_theme)
+       {
+         priv->custom_theme = FALSE;
 
-      update_current_theme (icon_theme);
+         update_current_theme (icon_theme);
+       }
     }
 }
 
@@ -881,7 +893,7 @@ insert_theme (GtkIconTheme *icon_theme, const char *theme_name)
   char **dirs;
   char **themes;
   GtkIconThemePrivate *priv;
-  IconTheme *theme;
+  IconTheme *theme = NULL;
   char *path;
   GKeyFile *theme_file;
   GError *error = NULL;
@@ -902,7 +914,7 @@ insert_theme (GtkIconTheme *icon_theme, const char *theme_name)
       path = g_build_filename (priv->search_path[i],
                               theme_name,
                               NULL);
-      dir_mtime = g_new (IconThemeDirMtime, 1);
+      dir_mtime = g_slice_new (IconThemeDirMtime);
       dir_mtime->cache = NULL;
       dir_mtime->dir = path;
       if (g_stat (path, &stat_buf) == 0 && S_ISDIR (stat_buf.st_mode))
@@ -954,7 +966,15 @@ insert_theme (GtkIconTheme *icon_theme, const char *theme_name)
 
   dirs = g_key_file_get_string_list (theme_file, "Icon Theme", "Directories", NULL, NULL);
   if (!dirs)
-    g_warning ("Theme file for %s has no directories\n", theme_name);
+    {
+      g_warning ("Theme file for %s has no directories\n", theme_name);
+      priv->themes = g_list_remove (priv->themes, theme);
+      g_free (theme->name);
+      g_free (theme->display_name);
+      g_free (theme);
+      g_key_file_free (theme_file);
+      return;
+    }
   
   theme->comment = 
     g_key_file_get_locale_string (theme_file, 
@@ -968,9 +988,9 @@ insert_theme (GtkIconTheme *icon_theme, const char *theme_name)
   theme->dirs = NULL;
   for (i = 0; dirs[i] != NULL; i++)
     theme_subdir_load (icon_theme, theme, theme_file, dirs[i]);
-  
+
   g_strfreev (dirs);
-  
+
   theme->dirs = g_list_reverse (theme->dirs);
 
   themes = g_key_file_get_string_list (theme_file,
@@ -992,11 +1012,22 @@ insert_theme (GtkIconTheme *icon_theme, const char *theme_name)
 static void
 free_unthemed_icon (UnthemedIcon *unthemed_icon)
 {
-  if (unthemed_icon->svg_filename)
-    g_free (unthemed_icon->svg_filename);
-  if (unthemed_icon->no_svg_filename)
-    g_free (unthemed_icon->no_svg_filename);
-  g_free (unthemed_icon);
+  g_free (unthemed_icon->svg_filename);
+  g_free (unthemed_icon->no_svg_filename);
+  g_slice_free (UnthemedIcon, unthemed_icon);
+}
+
+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);
 }
 
 static void
@@ -1005,9 +1036,8 @@ load_themes (GtkIconTheme *icon_theme)
   GtkIconThemePrivate *priv;
   GDir *gdir;
   int base;
-  char *dir, *base_name, *dot;
+  char *dir;
   const char *file;
-  char *abs_file;
   UnthemedIcon *unthemed_icon;
   IconSuffix old_suffix, new_suffix;
   GTimeVal tv;
@@ -1018,9 +1048,12 @@ load_themes (GtkIconTheme *icon_theme)
 
   priv->all_icons = g_hash_table_new (g_str_hash, g_str_equal);
   
-  insert_theme (icon_theme, priv->current_theme);
-  
-  /* Always look in the "default" icon theme */
+  if (priv->current_theme)
+    insert_theme (icon_theme, priv->current_theme);
+
+  /* Always look in the "default" icon theme, and in a fallback theme */
+  if (priv->fallback_theme)
+    insert_theme (icon_theme, priv->fallback_theme);
   insert_theme (icon_theme, DEFAULT_THEME_NAME);
   priv->themes = g_list_reverse (priv->themes);
 
@@ -1032,37 +1065,36 @@ load_themes (GtkIconTheme *icon_theme)
     {
       dir = icon_theme->priv->search_path[base];
 
-      dir_mtime = g_new (IconThemeDirMtime, 1);
-      dir_mtime->cache = _gtk_icon_cache_new_for_path (dir);
+      dir_mtime = g_slice_new (IconThemeDirMtime);
+      priv->dir_mtimes = g_list_append (priv->dir_mtimes, dir_mtime);
+      
       dir_mtime->dir = g_strdup (dir);
-      if (g_stat (dir, &stat_buf) == 0 && S_ISDIR (stat_buf.st_mode))
-       dir_mtime->mtime = stat_buf.st_mtime;
-      else
-       dir_mtime->mtime = 0;
+      dir_mtime->mtime = 0;
+      dir_mtime->cache = NULL;
 
-      priv->dir_mtimes = g_list_append (priv->dir_mtimes, dir_mtime);
+      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);
       if (dir_mtime->cache != NULL)
        continue;
 
       gdir = g_dir_open (dir, 0, NULL);
-
       if (gdir == NULL)
        continue;
-      
+
       while ((file = g_dir_read_name (gdir)))
        {
          new_suffix = suffix_from_name (file);
-
+         
          if (new_suffix != ICON_SUFFIX_NONE)
            {
-             abs_file = g_build_filename (dir, file, NULL);
+             char *abs_file;
+             char *base_name;
 
-             base_name = g_strdup (file);
-                 
-             dot = strrchr (base_name, '.');
-             if (dot)
-               *dot = 0;
+             abs_file = g_build_filename (dir, file, NULL);
+             base_name = strip_suffix (file);
 
              if ((unthemed_icon = g_hash_table_lookup (priv->unthemed_icons,
                                                        base_name)))
@@ -1095,7 +1127,7 @@ load_themes (GtkIconTheme *icon_theme)
                }
              else
                {
-                 unthemed_icon = g_new0 (UnthemedIcon, 1);
+                 unthemed_icon = g_slice_new0 (UnthemedIcon);
                  
                  if (new_suffix == ICON_SUFFIX_SVG)
                    unthemed_icon->svg_filename = abs_file;
@@ -1119,13 +1151,19 @@ load_themes (GtkIconTheme *icon_theme)
   priv->last_stat_time = tv.tv_sec;
 }
 
-static void
+void
 _gtk_icon_theme_ensure_builtin_cache (void)
 {
   static gboolean initialized = FALSE;
   IconThemeDir *dir;
-  gint sizes[5] = { 16, 20, 24, 32, 48 };
-  gint n_sizes = G_N_ELEMENTS (sizes);
+  static IconThemeDir dirs[5] = 
+    {
+      { 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 }
+    };
   gint i;
 
   if (!initialized)
@@ -1134,19 +1172,11 @@ _gtk_icon_theme_ensure_builtin_cache (void)
 
       _builtin_cache = _gtk_icon_cache_new ((gchar *)builtin_icons);
 
-      for (i = 0; i < n_sizes; i++)
+      for (i = 0; i < G_N_ELEMENTS (dirs); i++)
        {
-         dir = g_new (IconThemeDir, 1);
-         dir->type = ICON_THEME_DIR_THRESHOLD;
-         dir->context = 0;
-         dir->size = sizes[i];
-         dir->min_size = sizes[i];
-         dir->max_size = sizes[i];
-         dir->threshold = 2;
-         dir->dir = NULL;
-         dir->icon_data = NULL;
-         dir->subdir = g_strdup_printf ("%d", sizes[i]);
+         dir = &(dirs[i]);
          dir->cache = _gtk_icon_cache_ref (_builtin_cache);
+          dir->subdir_index = _gtk_icon_cache_get_directory_index (dir->cache, dir->subdir);
 
          builtin_dirs = g_list_append (builtin_dirs, dir);
        }
@@ -1160,80 +1190,67 @@ ensure_valid_themes (GtkIconTheme *icon_theme)
   GTimeVal tv;
   gboolean was_valid = priv->themes_valid;
 
+  if (priv->loading_themes)
+    return;
+  priv->loading_themes = TRUE;
+
   _gtk_icon_theme_ensure_builtin_cache ();
 
   if (priv->themes_valid)
     {
       g_get_current_time (&tv);
 
-      if (ABS (tv.tv_sec - priv->last_stat_time) > 5)
-       gtk_icon_theme_rescan_if_needed (icon_theme);
+      if (ABS (tv.tv_sec - priv->last_stat_time) > 5 &&
+         rescan_themes (icon_theme))
+       blow_themes (icon_theme);
     }
   
   if (!priv->themes_valid)
     {
       load_themes (icon_theme);
-      
-      if (!priv->check_reload && was_valid && 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");
+      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");
 
-         for (i = 0; i < 5; i++)
-           event->client.data.l[i] = 0;
-         event->client.data_format = 32;
-         event->client.message_type = atom_iconthemes;
+             for (i = 0; i < 5; i++)
+               event->client.data.l[i] = 0;
+             event->client.data_format = 32;
+             event->client.message_type = atom_iconthemes;
 
-         gdk_screen_broadcast_client_message (priv->screen, event);
+             gdk_screen_broadcast_client_message (priv->screen, event);
+           }
        }
     }
+
+  priv->loading_themes = FALSE;
 }
 
-/**
- * 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)
+static GtkIconInfo *
+choose_icon (GtkIconTheme       *icon_theme,
+            const gchar        *icon_names[],
+            gint                size,
+            GtkIconLookupFlags  flags)
 {
   GtkIconThemePrivate *priv;
   GList *l;
   GtkIconInfo *icon_info = NULL;
-  UnthemedIcon *unthemed_icon;
+  UnthemedIcon *unthemed_icon = NULL;
   gboolean allow_svg;
   gboolean use_builtin;
-
-  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);
+  gint i;
 
   priv = icon_theme->priv;
 
-  GTK_NOTE (ICONTHEME, 
-           g_print ("gtk_icon_theme_lookup_icon %s\n", icon_name));
   if (flags & GTK_ICON_LOOKUP_NO_SVG)
     allow_svg = FALSE;
   else if (flags & GTK_ICON_LOOKUP_FORCE_SVG)
@@ -1241,20 +1258,55 @@ gtk_icon_theme_lookup_icon (GtkIconTheme       *icon_theme,
   else
     allow_svg = priv->pixbuf_supports_svg;
 
-  use_builtin = (flags & GTK_ICON_LOOKUP_USE_BUILTIN);
-
+  use_builtin = flags & GTK_ICON_LOOKUP_USE_BUILTIN;
+  
   ensure_valid_themes (icon_theme);
 
   for (l = priv->themes; l; l = l->next)
     {
       IconTheme *theme = l->data;
       
-      icon_info = theme_lookup_icon (theme, icon_name, size, allow_svg, use_builtin);
-      if (icon_info)
-       goto out;
+      for (i = 0; icon_names[i]; i++)
+        {
+          icon_info = theme_lookup_icon (theme, icon_names[i], size, allow_svg, use_builtin);
+          if (icon_info)
+            goto out;
+        }
     }
 
-  unthemed_icon = g_hash_table_lookup (priv->unthemed_icons, icon_name);
+  for (i = 0; icon_names[i]; i++)
+    {
+      unthemed_icon = g_hash_table_lookup (priv->unthemed_icons, icon_names[i]);
+      if (unthemed_icon)
+        break;
+    }
+#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
+
   if (unthemed_icon)
     {
       icon_info = icon_info_new ();
@@ -1275,11 +1327,15 @@ gtk_icon_theme_lookup_icon (GtkIconTheme       *icon_theme,
 #endif
 
       icon_info->dir_type = ICON_THEME_DIR_UNTHEMED;
+      icon_info->dir_size = size;
     }
 
  out:
-  if (icon_info)
-    icon_info->desired_size = size;
+  if (icon_info) 
+    {
+      icon_info->desired_size = size;
+      icon_info->forced_size = (flags & GTK_ICON_LOOKUP_FORCE_SIZE) != 0;
+    }
   else
     {
       static gboolean check_for_default_theme = TRUE;
@@ -1300,13 +1356,14 @@ gtk_icon_theme_lookup_icon (GtkIconTheme       *icon_theme,
              found = g_file_test (default_theme_path, G_FILE_TEST_IS_REGULAR);
              g_free (default_theme_path);
            }
+
          if (!found)
            {
              g_warning (_("Could not find the icon '%s'. The '%s' theme\n"
                           "was not found either, perhaps you need to install it.\n"
                           "You can get a copy from:\n"
                           "\t%s"),
-                        icon_name, DEFAULT_THEME_NAME, "http://icon-theme.freedesktop.org/releases");
+                        icon_names[0], DEFAULT_THEME_NAME, "http://icon-theme.freedesktop.org/releases");
            }
        }
     }
@@ -1314,15 +1371,122 @@ gtk_icon_theme_lookup_icon (GtkIconTheme       *icon_theme,
   return icon_info;
 }
 
+
+/**
+ * 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;
+      for (p = (gchar *) icon_name; *p; p++)
+        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;
+   
+      info = choose_icon (icon_theme, (const gchar **) names, size, flags);
+      
+      g_strfreev (names);
+    }
+  else 
+    {
+      const gchar *names[2];
+      
+      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);
+}
+
 /* Error quark */
 GQuark
 gtk_icon_theme_error_quark (void)
 {
-  static GQuark q = 0;
-  if (q == 0)
-    q = g_quark_from_static_string ("gtk-icon-theme-error-quark");
-
-  return q;
+  return g_quark_from_static_string ("gtk-icon-theme-error-quark");
 }
 
 /**
@@ -1370,8 +1534,8 @@ gtk_icon_theme_load_icon (GtkIconTheme         *icon_theme,
                        (flags & GTK_ICON_LOOKUP_FORCE_SVG) == 0, NULL);
   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
   
-  icon_info  = gtk_icon_theme_lookup_icon (icon_theme, icon_name, size,
-                                          flags | GTK_ICON_LOOKUP_USE_BUILTIN);
+  icon_info = gtk_icon_theme_lookup_icon (icon_theme, icon_name, size,
+                                         flags | GTK_ICON_LOOKUP_USE_BUILTIN);
   if (!icon_info)
     {
       g_set_error (error, GTK_ICON_THEME_ERROR,  GTK_ICON_THEME_NOT_FOUND,
@@ -1489,6 +1653,9 @@ gtk_icon_theme_get_icon_sizes (GtkIconTheme *icon_theme,
        {
          IconThemeDir *dir = d->data;
 
+          if (dir->type != ICON_THEME_DIR_SCALABLE && g_hash_table_lookup_extended (sizes, GINT_TO_POINTER (dir->size), NULL, NULL))
+            continue;
+
          suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);      
          if (suffix != ICON_SUFFIX_NONE)
            {
@@ -1504,6 +1671,9 @@ gtk_icon_theme_get_icon_sizes (GtkIconTheme *icon_theme,
     {
       IconThemeDir *dir = d->data;
       
+      if (dir->type != ICON_THEME_DIR_SCALABLE && g_hash_table_lookup_extended (sizes, GINT_TO_POINTER (dir->size), NULL, NULL))
+        continue;
+
       suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);         
       if (suffix != ICON_SUFFIX_NONE)
        {
@@ -1564,8 +1734,8 @@ add_key_to_list (gpointer  key,
  * 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,
- * but will typically include such values as 'apps' and
- * 'mimetypes'.
+ * but will typically include such values as "Applications" and
+ * "MimeTypes".
  * 
  * Return value: a #GList list holding the names of all the
  *  icons in the theme. You must first free each element
@@ -1622,6 +1792,51 @@ gtk_icon_theme_list_icons (GtkIconTheme *icon_theme,
   return list;
 }
 
+/**
+ * gtk_icon_theme_list_contexts:
+ * @icon_theme: a #GtkIconTheme
+ *
+ * Gets the list of contexts available within the current
+ * hierarchy of icon themes
+ *
+ * Return value: a #GList list holding the names of all the
+ *  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, contexts);
+      l = l->next;
+    }
+
+  list = NULL;
+
+  g_hash_table_foreach (contexts,
+                       add_key_to_list,
+                       &list);
+
+  g_hash_table_destroy (contexts);
+
+  return list;
+}
+
 /**
  * gtk_icon_theme_get_example_icon_name:
  * @icon_theme: a #GtkIconTheme
@@ -1661,21 +1876,9 @@ gtk_icon_theme_get_example_icon_name (GtkIconTheme *icon_theme)
   return NULL;
 }
 
-/**
- * gtk_icon_theme_rescan_if_needed:
- * @icon_theme: a #GtkIconTheme
- * 
- * Checks to see if the icon theme has changed; if it has, any
- * currently cached information is discarded and will be reloaded
- * next time @icon_theme is accessed.
- * 
- * Return value: %TRUE if the icon theme has changed and needed
- *   to be reloaded.
- *
- * Since: 2.4
- **/
-gboolean
-gtk_icon_theme_rescan_if_needed (GtkIconTheme *icon_theme)
+
+static gboolean
+rescan_themes (GtkIconTheme *icon_theme)
 {
   GtkIconThemePrivate *priv;
   IconThemeDirMtime *dir_mtime;
@@ -1684,10 +1887,8 @@ gtk_icon_theme_rescan_if_needed (GtkIconTheme *icon_theme)
   struct stat stat_buf;
   GTimeVal tv;
 
-  g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), FALSE);
-
   priv = icon_theme->priv;
-  
+
   for (d = priv->dir_mtimes; d != NULL; d = d->next)
     {
       dir_mtime = d->data;
@@ -1695,7 +1896,7 @@ gtk_icon_theme_rescan_if_needed (GtkIconTheme *icon_theme)
       stat_res = g_stat (dir_mtime->dir, &stat_buf);
 
       /* dir mtime didn't change */
-      if (stat_res == 0 && 
+      if (stat_res == 0 &&
          S_ISDIR (stat_buf.st_mode) &&
          dir_mtime->mtime == stat_buf.st_mtime)
        continue;
@@ -1703,17 +1904,43 @@ gtk_icon_theme_rescan_if_needed (GtkIconTheme *icon_theme)
       if (dir_mtime->mtime == 0 &&
          (stat_res != 0 || !S_ISDIR (stat_buf.st_mode)))
        continue;
-         
-      do_theme_change (icon_theme);
+
       return TRUE;
     }
-  
+
   g_get_current_time (&tv);
   priv->last_stat_time = tv.tv_sec;
 
   return FALSE;
 }
 
+/**
+ * gtk_icon_theme_rescan_if_needed:
+ * @icon_theme: a #GtkIconTheme
+ * 
+ * Checks to see if the icon theme has changed; if it has, any
+ * currently cached information is discarded and will be reloaded
+ * next time @icon_theme is accessed.
+ * 
+ * Return value: %TRUE if the icon theme has changed and needed
+ *   to be reloaded.
+ *
+ * Since: 2.4
+ **/
+gboolean
+gtk_icon_theme_rescan_if_needed (GtkIconTheme *icon_theme)
+{
+  gboolean retval;
+
+  g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), FALSE);
+
+  retval = rescan_themes (icon_theme);
+  if (retval)
+      do_theme_change (icon_theme);
+
+  return retval;
+}
+
 static void
 theme_destroy (IconTheme *theme)
 {
@@ -1839,7 +2066,7 @@ theme_dir_get_icon_suffix (IconThemeDir *dir,
     {
       suffix = (IconSuffix)_gtk_icon_cache_get_icon_flags (dir->cache,
                                                           icon_name,
-                                                          dir->subdir);
+                                                          dir->subdir_index);
 
       if (has_icon_file)
        *has_icon_file = suffix & HAS_ICON_FILE;
@@ -1850,7 +2077,7 @@ theme_dir_get_icon_suffix (IconThemeDir *dir,
     suffix = GPOINTER_TO_UINT (g_hash_table_lookup (dir->icons, icon_name));
 
   GTK_NOTE (ICONTHEME, 
-           g_print ("get_icon_suffix%s %d\n", dir->cache ? " (cached)" : "", suffix));
+           g_print ("get_icon_suffix%s %u\n", dir->cache ? " (cached)" : "", suffix));
 
   return suffix;
 }
@@ -1867,17 +2094,18 @@ theme_lookup_icon (IconTheme          *theme,
   char *file;
   int min_difference, difference;
   BuiltinIcon *closest_builtin = NULL;
-  gboolean smaller, has_larger;
+  gboolean smaller, has_larger, match;
   IconSuffix suffix;
 
   min_difference = G_MAXINT;
   min_dir = NULL;
   has_larger = FALSE;
+  match = FALSE;
 
   /* Builtin icons are logically part of the default theme and
    * are searched before other subdirectories of the default theme.
    */
-  if (strcmp (theme->name, DEFAULT_THEME_NAME) == 0 && use_builtin)
+  if (use_builtin && strcmp (theme->name, DEFAULT_THEME_NAME) == 0)
     {
       closest_builtin = find_builtin_icon (icon_name, 
                                           size,
@@ -1897,7 +2125,7 @@ theme_lookup_icon (IconTheme          *theme,
     {
       dir = l->data;
 
-      GTK_NOTE (ICONTHEME, 
+      GTK_NOTE (ICONTHEME,
                g_print ("theme_lookup_icon dir %s\n", dir->dir));
       suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);
       if (best_suffix (suffix, allow_svg) != ICON_SUFFIX_NONE)
@@ -1906,31 +2134,55 @@ theme_lookup_icon (IconTheme          *theme,
 
          if (difference == 0)
            {
-             min_dir = dir;
-             break;
-           }
-
-         if (!has_larger)
-           {
-             if (difference < min_difference || smaller)
-               {
-                 min_difference = difference;
-                 min_dir = dir;
-                 closest_builtin = NULL;
-                 has_larger = smaller;
-               }
-           }
-         else
-           {
-             if (difference < min_difference && smaller)
-               {
-                 min_difference = difference;
-                 min_dir = dir;
-                 closest_builtin = NULL;
-               }
+              if (dir->type == ICON_THEME_DIR_SCALABLE)
+                {
+                  /* don't pick scalable if we already found
+                   * a matching non-scalable dir
+                   */
+                  if (!match)
+                    {
+                     min_dir = dir;
+                     break;
+                    }
+                }
+              else
+                {
+                  /* for a matching non-scalable dir keep
+                   * going and look for a closer match
+                   */             
+                  difference = abs (size - dir->size);
+                  if (!match || difference < min_difference)
+                    {
+                      match = TRUE;
+                      min_difference = difference;
+                     min_dir = dir;
+                    }
+                  if (difference == 0)
+                    break;
+                }
+           } 
+  
+          if (!match)
+            {
+             if (!has_larger)
+               {
+                 if (difference < min_difference || smaller)
+                   {
+                     min_difference = difference;
+                     min_dir = dir;
+                     has_larger = smaller;
+                   }
+               }
+             else
+               {
+                 if (difference < min_difference && smaller)
+                   {
+                     min_difference = difference;
+                     min_dir = dir;
+                   }
+               }
            }
-
-       }
+        }
 
       l = l->next;
 
@@ -1941,9 +2193,6 @@ theme_lookup_icon (IconTheme          *theme,
        }
     }
 
-  if (closest_builtin)
-    return icon_info_new_builtin (closest_builtin);
-  
   if (min_dir)
     {
       GtkIconInfo *icon_info = icon_info_new ();
@@ -1953,20 +2202,30 @@ theme_lookup_icon (IconTheme          *theme,
       suffix = best_suffix (suffix, allow_svg);
       g_assert (suffix != ICON_SUFFIX_NONE);
       
-      file = g_strconcat (icon_name, string_from_suffix (suffix), NULL);
-      icon_info->filename = g_build_filename (min_dir->dir, file, NULL);
-      g_free (file);
+      if (min_dir->dir)
+        {
+          file = g_strconcat (icon_name, string_from_suffix (suffix), NULL);
+          icon_info->filename = g_build_filename (min_dir->dir, file, NULL);
+          g_free (file);
 #ifdef G_OS_WIN32
-      icon_info->cp_filename = g_locale_from_utf8 (icon_info->filename,
+          icon_info->cp_filename = g_locale_from_utf8 (icon_info->filename,
                                                   -1, NULL, NULL, NULL);
 #endif
+        }
+      else
+        {
+          icon_info->filename = NULL;
+#ifdef G_OS_WIN32
+          icon_info->cp_filename = NULL;
+#endif
+        }
       
       if (min_dir->icon_data != NULL)
        icon_info->data = g_hash_table_lookup (min_dir->icon_data, icon_name);
 
       if (icon_info->data == NULL && min_dir->cache != NULL)
        {
-         icon_info->data = _gtk_icon_cache_get_icon_data (min_dir->cache, icon_name, min_dir->subdir);
+         icon_info->data = _gtk_icon_cache_get_icon_data (min_dir->cache, icon_name, min_dir->subdir_index);
          if (icon_info->data)
            {
              if (min_dir->icon_data == NULL)
@@ -2000,7 +2259,7 @@ theme_lookup_icon (IconTheme          *theme,
       if (min_dir->cache)
        {
          icon_info->cache_pixbuf = _gtk_icon_cache_get_icon (min_dir->cache, icon_name,
-                                                             min_dir->subdir);
+                                                             min_dir->subdir_index);
        }
 
       icon_info->dir_type = min_dir->type;
@@ -2009,13 +2268,17 @@ theme_lookup_icon (IconTheme          *theme,
       
       return icon_info;
     }
+
+  if (closest_builtin)
+    return icon_info_new_builtin (closest_builtin);
+  
   return NULL;
 }
 
 static void
-theme_list_icons (IconTheme *theme, GHashTable *icons,
-                 GQuark context)
+theme_list_icons (IconTheme  *theme, 
+                 GHashTable *icons,
+                 GQuark      context)
 {
   GList *l = theme->dirs;
   IconThemeDir *dir;
@@ -2045,6 +2308,25 @@ theme_list_icons (IconTheme *theme, GHashTable *icons,
     }
 }
 
+static void
+theme_list_contexts (IconTheme  *theme, 
+                    GHashTable *contexts)
+{
+  GList *l = theme->dirs;
+  IconThemeDir *dir;
+  const char *context;
+
+  while (l != NULL)
+    {
+      dir = l->data;
+
+      context = g_quark_to_string (dir->context);
+      g_hash_table_replace (contexts, (gpointer) context, NULL);
+
+      l = l->next;
+    }
+}
+
 static void
 load_icon_data (IconThemeDir *dir, const char *path, const char *name)
 {
@@ -2052,7 +2334,6 @@ load_icon_data (IconThemeDir *dir, const char *path, const char *name)
   char *base_name;
   char **split;
   gsize length;
-  char *dot;
   char *str;
   char *split_point;
   int i;
@@ -2067,15 +2348,14 @@ load_icon_data (IconThemeDir *dir, const char *path, const char *name)
   if (error)
     {
       g_error_free (error);
+      g_key_file_free (icon_file);      
       return;
     }
   else
     {
-      base_name = g_strdup (name);
-      dot = strrchr (base_name, '.');
-      *dot = 0;
+      base_name = strip_suffix (name);
       
-      data = g_new0 (GtkIconData, 1);
+      data = g_slice_new0 (GtkIconData);
       g_hash_table_replace (dir->icon_data, base_name, data);
       
       ivalues = g_key_file_get_integer_list (icon_file, 
@@ -2101,7 +2381,7 @@ load_icon_data (IconThemeDir *dir, const char *path, const char *name)
          split = g_strsplit (str, "|", -1);
          
          data->n_attach_points = g_strv_length (split);
-         data->attach_points = g_malloc (sizeof (GdkPoint) * data->n_attach_points);
+         data->attach_points = g_new (GdkPoint, data->n_attach_points);
 
          i = 0;
          while (split[i] != NULL && i < data->n_attach_points)
@@ -2134,9 +2414,6 @@ scan_directory (GtkIconThemePrivate *icon_theme,
 {
   GDir *gdir;
   const char *name;
-  char *base_name, *dot;
-  char *path;
-  IconSuffix suffix, hash_suffix;
 
   GTK_NOTE (ICONTHEME, 
            g_print ("scanning directory %s\n", full_dir));
@@ -2150,6 +2427,10 @@ scan_directory (GtkIconThemePrivate *icon_theme,
 
   while ((name = g_dir_read_name (gdir)))
     {
+      char *path;
+      char *base_name;
+      IconSuffix suffix, hash_suffix;
+
       if (g_str_has_suffix (name, ".icon"))
        {
          if (dir->icon_data == NULL)
@@ -2168,14 +2449,12 @@ scan_directory (GtkIconThemePrivate *icon_theme,
       suffix = suffix_from_name (name);
       if (suffix == ICON_SUFFIX_NONE)
        continue;
-      
-      base_name = g_strdup (name);
-      dot = strrchr (base_name, '.');
-      *dot = 0;
-      
+
+      base_name = strip_suffix (name);
+
       hash_suffix = GPOINTER_TO_INT (g_hash_table_lookup (dir->icons, base_name));
+      g_hash_table_replace (icon_theme->all_icons, base_name, NULL);
       g_hash_table_replace (dir->icons, base_name, GUINT_TO_POINTER (hash_suffix| suffix));
-      g_hash_table_insert (icon_theme->all_icons, base_name, NULL);
     }
   
   g_dir_close (gdir);
@@ -2288,10 +2567,14 @@ theme_subdir_load (GtkIconTheme *icon_theme,
          dir->icon_data = NULL;
          dir->subdir = g_strdup (subdir);
          if (dir_mtime->cache != NULL)
-           dir->cache = _gtk_icon_cache_ref (dir_mtime->cache);
+            {
+             dir->cache = _gtk_icon_cache_ref (dir_mtime->cache);
+              dir->subdir_index = _gtk_icon_cache_get_directory_index (dir->cache, dir->subdir);
+            }
          else
            {
              dir->cache = NULL;
+              dir->subdir_index = -1;
              scan_directory (icon_theme->priv, dir, full_dir);
            }
 
@@ -2307,7 +2590,7 @@ icon_data_free (GtkIconData *icon_data)
 {
   g_free (icon_data->attach_points);
   g_free (icon_data->display_name);
-  g_free (icon_data);
+  g_slice_free (GtkIconData, icon_data);
 }
 
 /*
@@ -2323,15 +2606,17 @@ gtk_icon_info_get_type (void)
                                             (GBoxedCopyFunc) gtk_icon_info_copy,
                                             (GBoxedFreeFunc) gtk_icon_info_free);
 
+
   return our_type;
 }
 
 static GtkIconInfo *
 icon_info_new (void)
 {
-  GtkIconInfo *icon_info = g_new0 (GtkIconInfo, 1);
+  GtkIconInfo *icon_info = g_slice_new0 (GtkIconInfo);
 
   icon_info->scale = -1.;
+  icon_info->ref_count = 1;
 
   return icon_info;
 }
@@ -2345,7 +2630,7 @@ icon_info_new_builtin (BuiltinIcon *icon)
   icon_info->dir_type = ICON_THEME_DIR_THRESHOLD;
   icon_info->dir_size = icon->size;
   icon_info->threshold = 2;
-  
+
   return icon_info;
 }
 
@@ -2362,25 +2647,12 @@ icon_info_new_builtin (BuiltinIcon *icon)
 GtkIconInfo *
 gtk_icon_info_copy (GtkIconInfo *icon_info)
 {
-  GtkIconInfo *copy;
   
   g_return_val_if_fail (icon_info != NULL, NULL);
 
-  copy = g_memdup (icon_info, sizeof (GtkIconInfo));
-  if (copy->cache_pixbuf)
-    g_object_ref (copy->cache_pixbuf);
-  if (copy->pixbuf)
-    g_object_ref (copy->pixbuf);
-  if (copy->load_error)
-    copy->load_error = g_error_copy (copy->load_error);
-  if (copy->filename)
-    copy->filename = g_strdup (copy->filename);
-#ifdef G_OS_WIN32
-  if (copy->cp_filename)
-    copy->cp_filename = g_strdup (copy->cp_filename);
-#endif
+  icon_info->ref_count++;
 
-  return copy;
+  return icon_info;
 }
 
 /**
@@ -2396,18 +2668,22 @@ gtk_icon_info_free (GtkIconInfo *icon_info)
 {
   g_return_if_fail (icon_info != NULL);
 
-  if (icon_info->filename)
-    g_free (icon_info->filename);
+  icon_info->ref_count--;
+  if (icon_info->ref_count > 0)
+    return;
+  g_free (icon_info->filename);
 #ifdef G_OS_WIN32
-  if (icon_info->cp_filename)
-    g_free (icon_info->cp_filename);
+  g_free (icon_info->cp_filename);
 #endif
+  if (icon_info->loadable)
+    g_object_unref (icon_info->loadable);
   if (icon_info->pixbuf)
     g_object_unref (icon_info->pixbuf);
   if (icon_info->cache_pixbuf)
     g_object_unref (icon_info->cache_pixbuf);
 
-  g_free (icon_info);
+  g_slice_free (GtkIconInfo, icon_info);
 }
 
 /**
@@ -2488,56 +2764,17 @@ gtk_icon_info_get_builtin_pixbuf (GtkIconInfo *icon_info)
   return icon_info->cache_pixbuf;
 }
 
-static GdkPixbuf *
-load_svg_at_size (const gchar *filename,
-                 gint         size,
-                 GError      **error)
-{
-  GdkPixbuf *pixbuf = NULL;
-  GdkPixbufLoader *loader = NULL;
-  gchar *contents = NULL;
-  gsize length;
-  
-  if (!g_file_get_contents (filename,
-                           &contents, &length, error))
-    goto bail;
-  
-  loader = gdk_pixbuf_loader_new_with_type ("svg", error);
-  if (loader == NULL)
-    goto bail;
-
-  gdk_pixbuf_loader_set_size (loader, size, size);
-  
-  if (!gdk_pixbuf_loader_write (loader, contents, length, error))
-    {
-      gdk_pixbuf_loader_close (loader, NULL);
-      goto bail;
-    }
-  
-  if (!gdk_pixbuf_loader_close (loader, error))
-    goto bail;
-  
-  pixbuf = g_object_ref (gdk_pixbuf_loader_get_pixbuf (loader));
-  
- bail:
-  if (loader)
-    g_object_unref (loader);
-  if (contents)
-    g_free (contents);
-  
-  return pixbuf;
-}
-
-/* This function contains the complicatd logic for deciding
+/* This function contains the complicated logic for deciding
  * on the size at which to load the icon and loading it at
  * that size.
  */
 static gboolean
-icon_info_ensure_scale_and_pixbuf (GtkIconInfo *icon_info,
-                                  gboolean     scale_only)
+icon_info_ensure_scale_and_pixbuf (GtkIconInfo  *icon_info,
+                                  gboolean      scale_only)
 {
   int image_width, image_height;
   GdkPixbuf *source_pixbuf;
+  gboolean is_svg;
 
   /* First check if we already succeeded have the necessary
    * information (or failed earlier)
@@ -2554,16 +2791,61 @@ icon_info_ensure_scale_and_pixbuf (GtkIconInfo *icon_info,
   /* SVG icons are a special case - we just immediately scale them
    * to the desired size
    */
-  if (icon_info->filename && g_str_has_suffix (icon_info->filename, ".svg"))
+  if (icon_info->filename && !icon_info->loadable) 
+    {
+      GFile *file;
+
+      file = g_file_new_for_path (icon_info->filename);
+      icon_info->loadable = G_LOADABLE_ICON (g_file_icon_new (file));
+      g_object_unref (file);
+    }
+
+  is_svg = FALSE;
+  if (G_IS_FILE_ICON (icon_info->loadable))
+    {
+      GFile *file;
+      GFileInfo *file_info;
+      const gchar *content_type;
+
+      file = g_file_icon_get_file (G_FILE_ICON (icon_info->loadable));
+      file_info = g_file_query_info (file, 
+                                     G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
+                                     G_FILE_QUERY_INFO_NONE,
+                                     NULL, NULL);
+      if (file_info) 
+        {
+          content_type = g_file_info_get_content_type (file_info);
+
+          if (content_type && strcmp (content_type, "image/svg+xml") == 0)
+            is_svg = TRUE;
+
+          g_object_unref (file_info);
+       }
+    }
+
+  if (is_svg)
     {
+      GInputStream *stream;
+
       icon_info->scale = icon_info->desired_size / 1000.;
 
       if (scale_only)
        return TRUE;
       
-      icon_info->pixbuf = load_svg_at_size (icon_info->filename,
-                                           icon_info->desired_size,
-                                           &icon_info->load_error);
+      stream = g_loadable_icon_load (icon_info->loadable,
+                                     icon_info->desired_size,
+                                     NULL, NULL,
+                                     &icon_info->load_error);
+      if (stream)
+        {
+          icon_info->pixbuf = gdk_pixbuf_new_from_stream_at_scale (stream,
+                                                                   icon_info->desired_size,
+                                                                   icon_info->desired_size,
+                                                                   TRUE,
+                                                                   NULL,
+                                                                   &icon_info->load_error);
+          g_object_unref (stream);
+        }
 
       return icon_info->pixbuf != NULL;
     }
@@ -2573,7 +2855,9 @@ icon_info_ensure_scale_and_pixbuf (GtkIconInfo *icon_info,
    * for the directory where the icon is; the image size doesn't
    * matter in that case.
    */
-  if (icon_info->dir_type == ICON_THEME_DIR_FIXED)
+  if (icon_info->forced_size)
+    icon_info->scale = -1;
+  else if (icon_info->dir_type == ICON_THEME_DIR_FIXED)
     icon_info->scale = 1.0;
   else if (icon_info->dir_type == ICON_THEME_DIR_THRESHOLD)
     {
@@ -2593,19 +2877,31 @@ icon_info_ensure_scale_and_pixbuf (GtkIconInfo *icon_info,
     return TRUE;
 
   /* At this point, we need to actually get the icon; either from the
-   * builting image or by loading the file
+   * builtin image or by loading the file
    */
+  source_pixbuf = NULL;
   if (icon_info->cache_pixbuf)
     source_pixbuf = g_object_ref (icon_info->cache_pixbuf);
   else
     {
+      GInputStream *stream;
 
-      source_pixbuf = gdk_pixbuf_new_from_file (icon_info->filename,
-                                               &icon_info->load_error);
-      if (!source_pixbuf)
-       return FALSE;
+      stream = g_loadable_icon_load (icon_info->loadable,
+                                     icon_info->desired_size,
+                                     NULL, NULL,
+                                     &icon_info->load_error);
+      if (stream)
+        {
+          source_pixbuf = gdk_pixbuf_new_from_stream (stream,
+                                                      NULL,
+                                                      &icon_info->load_error);
+          g_object_unref (stream);
+        }
     }
 
+  if (!source_pixbuf)
+    return FALSE;
+
   /* Do scale calculations that depend on the image size
    */
   image_width = gdk_pixbuf_get_width (source_pixbuf);
@@ -2615,11 +2911,12 @@ icon_info_ensure_scale_and_pixbuf (GtkIconInfo *icon_info,
     {
       gint image_size = MAX (image_width, image_height);
       if (image_size > 0)
-       icon_info->scale = icon_info->desired_size / (gdouble)image_size;
+       icon_info->scale = (gdouble)icon_info->desired_size / (gdouble)image_size;
       else
        icon_info->scale = 1.0;
       
-      if (icon_info->dir_type == ICON_THEME_DIR_UNTHEMED)
+      if (icon_info->dir_type == ICON_THEME_DIR_UNTHEMED && 
+          !icon_info->forced_size)
        icon_info->scale = MIN (icon_info->scale, 1.0);
     }
 
@@ -2628,9 +2925,8 @@ icon_info_ensure_scale_and_pixbuf (GtkIconInfo *icon_info,
    * the job. This is a bit of a waste when we scale here
    * and never get the final pixbuf; at the cost of a bit of
    * extra complexity, we could keep the source pixbuf around
-   * but not actually scale it until neede.
+   * but not actually scale it until needed.
    */
-    
   if (icon_info->scale == 1.0)
     icon_info->pixbuf = source_pixbuf;
   else
@@ -2657,7 +2953,10 @@ icon_info_ensure_scale_and_pixbuf (GtkIconInfo *icon_info,
  * that differ slightly from their nominal sizes, and in addition GTK+
  * will avoid scaling icons that it considers sufficiently close to the
  * requested size or for which the source image would have to be scaled
- * up too far. (This maintains sharpness.) 
+ * up too far. (This maintains sharpness.). This behaviour can be changed
+ * by passing the %GTK_ICON_LOOKUP_FORCE_SIZE flag when obtaining
+ * the #GtkIconInfo. If this flag has been specified, the pixbuf
+ * returned by this function will be scaled to the exact size. 
  * 
  * Return value: the rendered icon; this may be a newly created icon
  *  or a new reference to an internal icon, so you must not modify
@@ -2670,16 +2969,19 @@ GdkPixbuf *
 gtk_icon_info_load_icon (GtkIconInfo *icon_info,
                         GError     **error)
 {
-  g_return_val_if_fail (icon_info != NULL, NULL);
-
   g_return_val_if_fail (icon_info != NULL, NULL);
   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
 
-  icon_info_ensure_scale_and_pixbuf (icon_info, FALSE);
-
-  if (icon_info->load_error)
+  if (!icon_info_ensure_scale_and_pixbuf (icon_info, FALSE))
     {
-      g_propagate_error (error, icon_info->load_error);
+      if (icon_info->load_error)
+        g_propagate_error (error, icon_info->load_error);
+      else
+        g_set_error (error,  
+                     GTK_ICON_THEME_ERROR,  
+                     GTK_ICON_THEME_NOT_FOUND,
+                     _("Failed to load icon"));
       return NULL;
     }
 
@@ -2865,7 +3167,7 @@ gtk_icon_info_get_attach_points (GtkIconInfo *icon_info,
  * Since: 2.4
  **/
 G_CONST_RETURN gchar *
-gtk_icon_info_get_display_name  (GtkIconInfo *icon_info)
+gtk_icon_info_get_display_name (GtkIconInfo *icon_info)
 {
   g_return_val_if_fail (icon_info != NULL, NULL);
 
@@ -3032,6 +3334,89 @@ _gtk_icon_theme_check_reload (GdkDisplay *display)
     }
 }
 
+/**
+ * gtk_icon_theme_lookup_by_gicon:
+ * @icon_theme: a #GtkIconTheme
+ * @icon: the #GIcon to look up
+ * @size: desired icon size
+ * @flags: flags modifying the behavior of the icon lookup
+ * 
+ * Looks up an 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().
+ *
+ * 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.14
+ */
+GtkIconInfo *
+gtk_icon_theme_lookup_by_gicon (GtkIconTheme       *icon_theme,
+                                GIcon              *icon,
+                                gint                size,
+                                GtkIconLookupFlags  flags)
+{
+  GtkIconInfo *info;
+
+  g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
+  g_return_val_if_fail (G_IS_ICON (icon), NULL);
+
+  if (G_IS_LOADABLE_ICON (icon))
+    {
+      info = icon_info_new ();
+      info->loadable = G_LOADABLE_ICON (g_object_ref (icon));
+
+      info->dir_type = ICON_THEME_DIR_UNTHEMED;
+      info->dir_size = size;
+      info->desired_size = size;
+      info->threshold = 2;
+      info->forced_size = (flags & GTK_ICON_LOOKUP_FORCE_SIZE) != 0;
+
+      return info;
+    }
+  else if (G_IS_THEMED_ICON (icon))
+    {
+      const gchar **names;
+
+      names = (const gchar **)g_themed_icon_get_names (G_THEMED_ICON (icon));
+      info = gtk_icon_theme_choose_icon (icon_theme, names, size, flags);
+
+      return info;
+    }
+
+  return NULL;
+}
+
+/**
+ * gtk_icon_info_new_for_pixbuf:
+ * @icon_theme: a #GtkIconTheme
+ * @pixbuf: the pixbuf to wrap in a #GtkIconInfo
+ *
+ * Creates a #GtkIconInfo for a #GtkPixbuf. 
+ *
+ * Returns: a #GtkIconInfo
+ *
+ * Since: 2.14
+ */
+GtkIconInfo *
+gtk_icon_info_new_for_pixbuf (GtkIconTheme *icon_theme,
+                              GdkPixbuf    *pixbuf)
+{
+  GtkIconInfo *info;
+
+  g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
+  g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
+
+  info = icon_info_new ();
+  info->pixbuf = g_object_ref (pixbuf);
+  info->scale = 1.0;
+  info->dir_type = ICON_THEME_DIR_UNTHEMED;
+
+  return info;
+}
+
 #ifdef G_OS_WIN32
 
 /* DLL ABI stability backward compatibility versions */