Code

Merge from fe-moved
[inkscape.git] / src / widgets / icon.cpp
index 32ebb4c5798ed92702aa6f52aa21fcd464b2ae02..5aad614b4e60aec2478e32cc2a7bac4aae15befb 100644 (file)
 #ifdef HAVE_CONFIG_H
 # include "config.h"
 #endif
-#include "path-prefix.h"
-
-
 
+#include <cstring>
+#include <glib/gmem.h>
 #include <gtk/gtkiconfactory.h>
 #include <gtk/gtkstock.h>
 #include <gtk/gtkimage.h>
-
+#include <gtkmm/iconfactory.h>
+#include <gtkmm/iconset.h>
+#include <gtkmm/iconsource.h>
+#include <gtkmm/icontheme.h>
 #include <gtkmm/image.h>
 
-#include "prefs-utils.h"
+#include "path-prefix.h"
+#include "preferences.h"
 #include "inkscape.h"
 #include "document.h"
 #include "sp-item.h"
@@ -55,20 +58,44 @@ static void sp_icon_screen_changed( GtkWidget *widget, GdkScreen *previous_scree
 static void sp_icon_style_set( GtkWidget *widget, GtkStyle *previous_style );
 static void sp_icon_theme_changed( SPIcon *icon );
 
-static guchar *sp_icon_image_load_pixmap(gchar const *name, unsigned lsize, unsigned psize);
-static guchar *sp_icon_image_load_svg(gchar const *name, unsigned lsize, unsigned psize);
-
-static guchar *sp_icon_image_load(SPIcon *icon, gchar const *name);
-
-static int sp_icon_get_phys_size(int size);
+static GdkPixbuf *sp_icon_image_load_pixmap(gchar const *name, unsigned lsize, unsigned psize);
+static GdkPixbuf *sp_icon_image_load_svg(gchar const *name, GtkIconSize lsize, unsigned psize);
 
 static void sp_icon_overlay_pixels( guchar *px, int width, int height, int stride,
                                     unsigned r, unsigned g, unsigned b );
 
+static void injectCustomSize();
+
 static GtkWidgetClass *parent_class;
 
 static bool sizeDirty = true;
 
+static bool sizeMapDone = false;
+static GtkIconSize iconSizeLookup[] = {
+    GTK_ICON_SIZE_INVALID,
+    GTK_ICON_SIZE_MENU,
+    GTK_ICON_SIZE_SMALL_TOOLBAR,
+    GTK_ICON_SIZE_LARGE_TOOLBAR,
+    GTK_ICON_SIZE_BUTTON,
+    GTK_ICON_SIZE_DND,
+    GTK_ICON_SIZE_DIALOG,
+    GTK_ICON_SIZE_MENU, // for Inkscape::ICON_SIZE_DECORATION
+};
+
+class IconCacheItem
+{
+public:
+    IconCacheItem( GtkIconSize lsize, GdkPixbuf* pb ) :
+        _lsize( lsize ),
+        _pb( pb )
+    {}
+    GtkIconSize _lsize;
+    GdkPixbuf* _pb;
+};
+
+static Glib::RefPtr<Gtk::IconFactory> inkyIcons;
+static std::map<Glib::ustring, std::vector<IconCacheItem> > iconSetCache;
+
 GtkType
 sp_icon_get_type()
 {
@@ -112,7 +139,7 @@ static void
 sp_icon_init(SPIcon *icon)
 {
     GTK_WIDGET_FLAGS(icon) |= GTK_NO_WINDOW;
-    icon->lsize = GTK_ICON_SIZE_BUTTON;
+    icon->lsize = Inkscape::ICON_SIZE_BUTTON;
     icon->psize = 0;
     icon->name = 0;
     icon->pb = 0;
@@ -175,45 +202,39 @@ static int sp_icon_expose(GtkWidget *widget, GdkEventExpose *event)
     if ( GTK_WIDGET_DRAWABLE(widget) ) {
         SPIcon *icon = SP_ICON(widget);
         if ( !icon->pb ) {
-            guchar *pixels = 0;
+            sp_icon_fetch_pixbuf( icon );
+        }
 
+        sp_icon_paint(SP_ICON(widget), &event->area);
+    }
+
+    return TRUE;
+}
+
+// PUBLIC CALL:
+void sp_icon_fetch_pixbuf( SPIcon *icon )
+{
+    if ( icon ) {
+        if ( !icon->pb ) {
             icon->psize = sp_icon_get_phys_size(icon->lsize);
 
-            pixels = sp_icon_image_load( icon, icon->name );
+            GdkPixbuf *pb = sp_icon_image_load_svg( icon->name, Inkscape::getRegisteredIconSize(icon->lsize), icon->psize );
+            if (!pb) {
+                pb = sp_icon_image_load_pixmap( icon->name, icon->lsize, icon->psize );
+            }
 
-            if (pixels) {
-                // don't pass the nr_free because we're caching the pixel
-                // space loaded through ...
-                // I just changed this. make sure sp_icon_image_load still does the right thing.
-                icon->pb = gdk_pixbuf_new_from_data(pixels, GDK_COLORSPACE_RGB, TRUE, 8,
-                                                    icon->psize, icon->psize, icon->psize * 4,
-                                                    /*(GdkPixbufDestroyNotify)nr_free*/NULL, NULL);
+            if ( pb ) {
+                icon->pb = pb;
                 icon->pb_faded = gdk_pixbuf_copy(icon->pb);
-
-                pixels = gdk_pixbuf_get_pixels(icon->pb_faded);
-                size_t stride = gdk_pixbuf_get_rowstride(icon->pb_faded);
-                pixels += 3; // alpha
-                for ( int row = 0 ; row < icon->psize ; row++ ) {
-                    guchar *row_pixels = pixels;
-                    for ( int column = 0 ; column < icon->psize ; column++ ) {
-                        *row_pixels = *row_pixels >> 1;
-                        row_pixels += 4;
-                    }
-                    pixels += stride;
-                }
+                gdk_pixbuf_saturate_and_pixelate(icon->pb, icon->pb_faded, 0.5, TRUE);
             } else {
                 /* TODO: We should do something more useful if we can't load the image. */
                 g_warning ("failed to load icon '%s'", icon->name);
             }
         }
-
-        sp_icon_paint(SP_ICON(widget), &event->area);
     }
-
-    return TRUE;
 }
 
-
 static void sp_icon_screen_changed( GtkWidget *widget, GdkScreen *previous_screen )
 {
     if ( GTK_WIDGET_CLASS( parent_class )->screen_changed ) {
@@ -241,46 +262,98 @@ static void sp_icon_theme_changed( SPIcon *icon )
 }
 
 
+static void imageMapCB(GtkWidget* widget, gpointer user_data);
+static void imageMapNamedCB(GtkWidget* widget, gpointer user_data);
+static void populate_placeholder_icon(gchar const* name, GtkIconSize size);
+static bool prerender_icon(gchar const *name, GtkIconSize lsize, unsigned psize);
+static Glib::ustring icon_cache_key(gchar const *name, unsigned lsize, unsigned psize);
+static GdkPixbuf *get_cached_pixbuf(Glib::ustring const &key);
+
+std::map<Glib::ustring, Glib::ustring> legacyNames;
+
+static void setupLegacyNaming() {
+    legacyNames["view-fullscreen"] = "fullscreen";
+    legacyNames["edit-select-all"] = "selection_select_all";
+    legacyNames["window-new"] = "view_new";
+}
+
 static GtkWidget *
-sp_icon_new_full( GtkIconSize lsize, gchar const *name )
+sp_icon_new_full( Inkscape::IconSize lsize, gchar const *name )
 {
-    static gint dump = prefs_get_int_attribute_limited( "debug.icons", "dumpGtk", 0, 0, 1 );
-    static gint fallback = prefs_get_int_attribute_limited( "debug.icons", "checkNames", 0, 0, 1 );
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    static bool dump = prefs->getBool( "/debug/icons/dumpGtk");
 
-    addPreRender( lsize, name );
+    GtkWidget *widget = 0;
+        gint trySize = CLAMP( static_cast<gint>(lsize), 0, static_cast<gint>(G_N_ELEMENTS(iconSizeLookup) - 1) );
+        if ( !sizeMapDone ) {
+            injectCustomSize();
+        }
+        GtkIconSize mappedSize = iconSizeLookup[trySize];
 
-    GtkStockItem stock;
-    gboolean tryLoad = gtk_stock_lookup( name, &stock );
-    if ( !tryLoad && fallback ) {
-        tryLoad |= strncmp("gtk-", name, 4 ) == 0;
-    }
-    if ( !tryLoad && fallback ) {
-        tryLoad |= strncmp("gnome-", name, 6 ) == 0;
-    }
+        GtkStockItem stock;
+        gboolean stockFound = gtk_stock_lookup( name, &stock );
+
+        GtkWidget *img = 0;
+        if ( legacyNames.empty() ) {
+            setupLegacyNaming();
+        }
+
+        bool flagMe = false;
+        if ( legacyNames.find(name) != legacyNames.end() ) {
+            img = gtk_image_new_from_icon_name( name, mappedSize );
+            flagMe = true;
+            if ( dump ) {
+                g_message("gtk_image_new_from_icon_name( '%s', %d ) = %p", name, mappedSize, img);
+                GtkImageType thing = gtk_image_get_storage_type(GTK_IMAGE(img));
+                g_message("      Type is %d  %s", (int)thing, (thing == GTK_IMAGE_EMPTY ? "Empty" : "ok"));
+            }
+        } else {
+            img = gtk_image_new_from_stock( name, mappedSize );
+        }
 
-    GtkWidget *widget = 0;
-    if ( tryLoad ) {
-        GtkWidget *img = gtk_image_new_from_stock( name, lsize );
         if ( img ) {
             GtkImageType type = gtk_image_get_storage_type( GTK_IMAGE(img) );
             if ( type == GTK_IMAGE_STOCK ) {
+                if ( !stockFound ) {
+                    // It's not showing as a stock ID, so assume it will be present internally
+                    populate_placeholder_icon( name, mappedSize );
+                    addPreRender( mappedSize, name );
+
+                    // Add a hook to render if set visible before prerender is done.
+                    g_signal_connect( G_OBJECT(img), "map", G_CALLBACK(imageMapCB), GINT_TO_POINTER(static_cast<int>(mappedSize)) );
+                    if ( dump ) {
+                        g_message("      connecting %p for imageMapCB for [%s] %d", img, name, (int)mappedSize);
+                    }
+                }
                 widget = GTK_WIDGET(img);
                 img = 0;
-
                 if ( dump ) {
-                    g_message( "loaded gtk  '%s' %d  (GTK_IMAGE_STOCK)", name, lsize );
+                    g_message( "loaded gtk  '%s' %d  (GTK_IMAGE_STOCK) %s  on %p", name, mappedSize, (stockFound ? "STOCK" : "local"), widget );
+                }
+            } else if ( type == GTK_IMAGE_ICON_NAME ) {
+                widget = GTK_WIDGET(img);
+                img = 0;
+
+                // Add a hook to render if set visible before prerender is done.
+                g_signal_connect( G_OBJECT(widget), "map", G_CALLBACK(imageMapNamedCB), GINT_TO_POINTER(0) );
+
+                if ( prefs->getBool("/options/iconrender/named_nodelay") ) {
+                    int psize = sp_icon_get_phys_size(lsize);
+                    prerender_icon(name, mappedSize, psize);
+                } else {
+                    addPreRender( mappedSize, name );
                 }
             } else {
                 if ( dump ) {
                     g_message( "skipped gtk '%s' %d  (not GTK_IMAGE_STOCK)", name, lsize );
                 }
-                g_object_unref( (GObject *)img );
+                //g_object_unref( (GObject *)img );
                 img = 0;
             }
         }
-    }
 
     if ( !widget ) {
+        //g_message("Creating an SPIcon instance for %s:%d", name, (int)lsize);
         SPIcon *icon = (SPIcon *)g_object_new(SP_TYPE_ICON, NULL);
         icon->lsize = lsize;
         icon->name = g_strdup(name);
@@ -293,15 +366,16 @@ sp_icon_new_full( GtkIconSize lsize, gchar const *name )
 }
 
 GtkWidget *
-sp_icon_new( GtkIconSize lsize, gchar const *name )
+sp_icon_new( Inkscape::IconSize lsize, gchar const *name )
 {
     return sp_icon_new_full( lsize, name );
 }
 
-Gtk::Widget *sp_icon_get_icon( Glib::ustring const &oid, GtkIconSize size )
+// PUBLIC CALL:
+Gtk::Widget *sp_icon_get_icon( Glib::ustring const &oid, Inkscape::IconSize size )
 {
     Gtk::Widget *result = 0;
-    GtkWidget *widget = sp_icon_new_full( size, oid.c_str() );
+    GtkWidget *widget = sp_icon_new_full( static_cast<Inkscape::IconSize>(Inkscape::getRegisteredIconSize(size)), oid.c_str() );
 
     if ( widget ) {
         if ( GTK_IS_IMAGE(widget) ) {
@@ -315,39 +389,84 @@ Gtk::Widget *sp_icon_get_icon( Glib::ustring const &oid, GtkIconSize size )
     return result;
 }
 
-// Try to load the named svg, falling back to pixmaps
-guchar *
-sp_icon_image_load( SPIcon *icon, gchar const *name )
-{
-    guchar *px = sp_icon_image_load_svg( name, icon->lsize, icon->psize );
-    if (!px) {
-        px = sp_icon_image_load_pixmap(name, icon->lsize, icon->psize);
-    }
-
-    return px;
-}
-
 GtkIconSize
 sp_icon_get_gtk_size(int size)
 {
-    static GtkIconSize map[64] = {(GtkIconSize)0};
+    static GtkIconSize sizemap[64] = {(GtkIconSize)0};
     size = CLAMP(size, 4, 63);
-    if (!map[size]) {
+    if (!sizemap[size]) {
         static int count = 0;
         char c[64];
         g_snprintf(c, 64, "InkscapeIcon%d", count++);
-        map[size] = gtk_icon_size_register(c, size, size);
+        sizemap[size] = gtk_icon_size_register(c, size, size);
+    }
+    return sizemap[size];
+}
+
+
+static void injectCustomSize()
+{
+    // TODO - still need to handle the case of theme changes and resize, especially as we can't re-register a string.
+    if ( !sizeMapDone )
+    {
+        Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+        bool dump = prefs->getBool( "/debug/icons/dumpDefault");
+        gint width = 0;
+        gint height = 0;
+        if ( gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &width, &height ) ) {
+            gint newWidth = ((width * 3) / 4);
+            gint newHeight = ((height * 3) / 4);
+            GtkIconSize newSizeEnum = gtk_icon_size_register( "inkscape-decoration", newWidth, newHeight );
+            if ( newSizeEnum ) {
+                if ( dump ) {
+                    g_message("Registered (%d, %d) <= (%d, %d) as index %d", newWidth, newHeight, width, height, newSizeEnum);
+                }
+                guint index = static_cast<guint>(Inkscape::ICON_SIZE_DECORATION);
+                if ( index < G_N_ELEMENTS(iconSizeLookup) ) {
+                    iconSizeLookup[index] = newSizeEnum;
+                } else if ( dump ) {
+                    g_message("size lookup array too small to store entry");
+                }
+            }
+        }
+        sizeMapDone = true;
+    }
+
+    static bool hit = false;
+    if ( !hit ) {
+        hit = true;
+        inkyIcons = Gtk::IconFactory::create();
+        inkyIcons->add_default();
     }
-    return map[size];
 }
 
-static int sp_icon_get_phys_size(int size)
+GtkIconSize Inkscape::getRegisteredIconSize( Inkscape::IconSize size )
+{
+    GtkIconSize other = GTK_ICON_SIZE_MENU;
+    injectCustomSize();
+    size = CLAMP( size, Inkscape::ICON_SIZE_MENU, Inkscape::ICON_SIZE_DECORATION );
+    if ( size == Inkscape::ICON_SIZE_DECORATION ) {
+        other = gtk_icon_size_from_name("inkscape-decoration");
+    } else {
+        other = static_cast<GtkIconSize>(size);
+    }
+
+    return other;
+}
+
+
+// PUBLIC CALL:
+int sp_icon_get_phys_size(int size)
 {
     static bool init = false;
-    static int lastSys[GTK_ICON_SIZE_DIALOG + 1];
-    static int vals[GTK_ICON_SIZE_DIALOG + 1];
+    static int lastSys[Inkscape::ICON_SIZE_DECORATION + 1];
+    static int vals[Inkscape::ICON_SIZE_DECORATION + 1];
 
-    size = CLAMP( size, GTK_ICON_SIZE_MENU, GTK_ICON_SIZE_DIALOG );
+    size = CLAMP( size, GTK_ICON_SIZE_MENU, Inkscape::ICON_SIZE_DECORATION );
+
+    if ( !sizeMapDone ) {
+        injectCustomSize();
+    }
 
     if ( sizeDirty && init ) {
         GtkIconSize const gtkSizes[] = {
@@ -356,10 +475,14 @@ static int sp_icon_get_phys_size(int size)
             GTK_ICON_SIZE_LARGE_TOOLBAR,
             GTK_ICON_SIZE_BUTTON,
             GTK_ICON_SIZE_DND,
-            GTK_ICON_SIZE_DIALOG
+            GTK_ICON_SIZE_DIALOG,
+            static_cast<guint>(Inkscape::ICON_SIZE_DECORATION) < G_N_ELEMENTS(iconSizeLookup) ?
+                iconSizeLookup[static_cast<int>(Inkscape::ICON_SIZE_DECORATION)] :
+                GTK_ICON_SIZE_MENU
         };
         for (unsigned i = 0; i < G_N_ELEMENTS(gtkSizes) && init; ++i) {
-            unsigned const val_ix(gtkSizes[i]);
+            guint const val_ix = (gtkSizes[i] <= GTK_ICON_SIZE_DIALOG) ? (guint)gtkSizes[i] : (guint)Inkscape::ICON_SIZE_DECORATION;
+
             g_assert( val_ix < G_N_ELEMENTS(vals) );
 
             gint width = 0;
@@ -372,7 +495,9 @@ static int sp_icon_get_phys_size(int size)
 
     if ( !init ) {
         sizeDirty = false;
-        gint dump = prefs_get_int_attribute_limited( "debug.icons", "dumpDefault", 0, 0, 1 );
+        Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+        bool dump = prefs->getBool("/debug/icons/dumpDefault");
+
         if ( dump ) {
             g_message( "Default icon sizes:" );
         }
@@ -384,7 +509,10 @@ static int sp_icon_get_phys_size(int size)
             GTK_ICON_SIZE_LARGE_TOOLBAR,
             GTK_ICON_SIZE_BUTTON,
             GTK_ICON_SIZE_DND,
-            GTK_ICON_SIZE_DIALOG
+            GTK_ICON_SIZE_DIALOG,
+            static_cast<guint>(Inkscape::ICON_SIZE_DECORATION) < G_N_ELEMENTS(iconSizeLookup) ?
+                iconSizeLookup[static_cast<int>(Inkscape::ICON_SIZE_DECORATION)] :
+                GTK_ICON_SIZE_MENU
         };
         gchar const *const names[] = {
             "GTK_ICON_SIZE_MENU",
@@ -392,13 +520,15 @@ static int sp_icon_get_phys_size(int size)
             "GTK_ICON_SIZE_LARGE_TOOLBAR",
             "GTK_ICON_SIZE_BUTTON",
             "GTK_ICON_SIZE_DND",
-            "GTK_ICON_SIZE_DIALOG"
+            "GTK_ICON_SIZE_DIALOG",
+            "inkscape-decoration"
         };
 
         GtkWidget *icon = (GtkWidget *)g_object_new(SP_TYPE_ICON, NULL);
 
         for (unsigned i = 0; i < G_N_ELEMENTS(gtkSizes); ++i) {
-            unsigned const val_ix(gtkSizes[i]);
+            guint const val_ix = (gtkSizes[i] <= GTK_ICON_SIZE_DIALOG) ? (guint)gtkSizes[i] : (guint)Inkscape::ICON_SIZE_DECORATION;
+
             g_assert( val_ix < G_N_ELEMENTS(vals) );
 
             gint width = 0;
@@ -457,23 +587,23 @@ static void sp_icon_paint(SPIcon *icon, GdkRectangle const *area)
         int const x1 = std::min(area->x + area->width,  widget.allocation.x + padx + static_cast<int>(icon->psize) );
         int const y1 = std::min(area->y + area->height, widget.allocation.y + pady + static_cast<int>(icon->psize) );
 
-        gdk_draw_pixbuf(GDK_DRAWABLE(widget.window), NULL, image,
-                        x0 - widget.allocation.x - padx,
-                        y0 - widget.allocation.y - pady,
-                        x0, y0,
-                        x1 - x0, y1 - y0,
-                        GDK_RGB_DITHER_NORMAL, x0, y0);
+        int width = x1 - x0;
+        int height = y1 - y0;
+        // Limit drawing to when we actually have something. Avoids some crashes.
+        if ( (width > 0) && (height > 0) ) {
+            gdk_draw_pixbuf(GDK_DRAWABLE(widget.window), NULL, image,
+                            x0 - widget.allocation.x - padx,
+                            y0 - widget.allocation.y - pady,
+                            x0, y0,
+                            width, height,
+                            GDK_RGB_DITHER_NORMAL, x0, y0);
+        }
     }
 }
 
-static guchar *
-sp_icon_image_load_pixmap(gchar const *name, unsigned lsize, unsigned psize)
+GdkPixbuf *sp_icon_image_load_pixmap(gchar const *name, unsigned /*lsize*/, unsigned psize)
 {
-    gchar *path;
-    guchar *px;
-    GdkPixbuf *pb;
-
-    path = (gchar *) g_strdup_printf("%s/%s.png", INKSCAPE_PIXMAPDIR, name);
+    gchar *path = (gchar *) g_strdup_printf("%s/%s.png", INKSCAPE_PIXMAPDIR, name);
     // TODO: bulia, please look over
     gsize bytesRead = 0;
     gsize bytesWritten = 0;
@@ -483,7 +613,7 @@ sp_icon_image_load_pixmap(gchar const *name, unsigned lsize, unsigned psize)
                                                  &bytesRead,
                                                  &bytesWritten,
                                                  &error);
-    pb = gdk_pixbuf_new_from_file(localFilename, NULL);
+    GdkPixbuf *pb = gdk_pixbuf_new_from_file(localFilename, NULL);
     g_free(localFilename);
     g_free(path);
     if (!pb) {
@@ -501,27 +631,21 @@ sp_icon_image_load_pixmap(gchar const *name, unsigned lsize, unsigned psize)
         g_free(localFilename);
         g_free(path);
     }
+
     if (pb) {
-        if (!gdk_pixbuf_get_has_alpha(pb))
+        if (!gdk_pixbuf_get_has_alpha(pb)) {
             gdk_pixbuf_add_alpha(pb, FALSE, 0, 0, 0);
+        }
+
         if ( ( static_cast<unsigned>(gdk_pixbuf_get_width(pb)) != psize )
              || ( static_cast<unsigned>(gdk_pixbuf_get_height(pb)) != psize ) ) {
             GdkPixbuf *spb = gdk_pixbuf_scale_simple(pb, psize, psize, GDK_INTERP_HYPER);
             g_object_unref(G_OBJECT(pb));
             pb = spb;
         }
-        guchar *spx = gdk_pixbuf_get_pixels(pb);
-        int srs = gdk_pixbuf_get_rowstride(pb);
-        px = nr_new(guchar, 4 * psize * psize);
-        for (unsigned y = 0; y < psize; y++) {
-            memcpy(px + 4 * y * psize, spx + y * srs, 4 * psize);
-        }
-        g_object_unref(G_OBJECT(pb));
-
-        return px;
     }
 
-    return NULL;
+    return pb;
 }
 
 // takes doc, root, icon, and icon name to produce pixels
@@ -529,40 +653,39 @@ extern "C" guchar *
 sp_icon_doc_icon( SPDocument *doc, NRArenaItem *root,
                   gchar const *name, unsigned psize )
 {
-    gint const dump = prefs_get_int_attribute_limited( "debug.icons", "dumpSvg", 0, 0, 1 );
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    bool const dump = prefs->getBool("/debug/icons/dumpSvg");
     guchar *px = NULL;
 
     if (doc) {
         SPObject *object = doc->getObjectById(name);
         if (object && SP_IS_ITEM(object)) {
             /* Find bbox in document */
-            NR::Matrix const i2doc(sp_item_i2doc_affine(SP_ITEM(object)));
-            NR::Rect dbox = SP_ITEM(object)->invokeBbox(i2doc);
+            Geom::Matrix const i2doc(sp_item_i2doc_affine(SP_ITEM(object)));
+            Geom::OptRect dbox = SP_ITEM(object)->getBounds(i2doc);
 
             if ( SP_OBJECT_PARENT(object) == NULL )
             {
-                dbox = NR::Rect(NR::Point(0, 0),
-                                NR::Point(sp_document_width(doc), sp_document_height(doc)));
+                dbox = Geom::Rect(Geom::Point(0, 0),
+                                Geom::Point(sp_document_width(doc), sp_document_height(doc)));
             }
 
             /* This is in document coordinates, i.e. pixels */
-            if (dbox.isEmpty() == false) {
+            if ( dbox ) {
                 NRGC gc(NULL);
                 /* Update to renderable state */
                 double sf = 1.0;
-                NRMatrix t;
-                nr_matrix_set_scale(&t, sf, sf);
-                nr_arena_item_set_transform(root, &t);
-                nr_matrix_set_identity(&gc.transform);
+                nr_arena_item_set_transform(root, (Geom::Matrix)Geom::Scale(sf, sf));
+                gc.transform.setIdentity();
                 nr_arena_item_invoke_update( root, NULL, &gc,
                                              NR_ARENA_ITEM_STATE_ALL,
                                              NR_ARENA_ITEM_STATE_NONE );
                 /* Item integer bbox in points */
                 NRRectL ibox;
-                ibox.x0 = (int) floor(sf * dbox.min()[NR::X] + 0.5);
-                ibox.y0 = (int) floor(sf * dbox.min()[NR::Y] + 0.5);
-                ibox.x1 = (int) floor(sf * dbox.max()[NR::X] + 0.5);
-                ibox.y1 = (int) floor(sf * dbox.max()[NR::Y] + 0.5);
+                ibox.x0 = (int) floor(sf * dbox->min()[Geom::X] + 0.5);
+                ibox.y0 = (int) floor(sf * dbox->min()[Geom::Y] + 0.5);
+                ibox.x1 = (int) floor(sf * dbox->max()[Geom::X] + 0.5);
+                ibox.y1 = (int) floor(sf * dbox->max()[Geom::Y] + 0.5);
 
                 if ( dump ) {
                     g_message( "   box    --'%s'  (%f,%f)-(%f,%f)", name, (double)ibox.x0, (double)ibox.y0, (double)ibox.x1, (double)ibox.y1 );
@@ -584,17 +707,16 @@ sp_icon_doc_icon( SPDocument *doc, NRArenaItem *root,
                         }
                         sf = (double)psize / (double)block;
 
-                        nr_matrix_set_scale(&t, sf, sf);
-                        nr_arena_item_set_transform(root, &t);
-                        nr_matrix_set_identity(&gc.transform);
+                        nr_arena_item_set_transform(root, (Geom::Matrix)Geom::Scale(sf, sf));
+                        gc.transform.setIdentity();
                         nr_arena_item_invoke_update( root, NULL, &gc,
                                                      NR_ARENA_ITEM_STATE_ALL,
                                                      NR_ARENA_ITEM_STATE_NONE );
                         /* Item integer bbox in points */
-                        ibox.x0 = (int) floor(sf * dbox.min()[NR::X] + 0.5);
-                        ibox.y0 = (int) floor(sf * dbox.min()[NR::Y] + 0.5);
-                        ibox.x1 = (int) floor(sf * dbox.max()[NR::X] + 0.5);
-                        ibox.y1 = (int) floor(sf * dbox.max()[NR::Y] + 0.5);
+                        ibox.x0 = (int) floor(sf * dbox->min()[Geom::X] + 0.5);
+                        ibox.y0 = (int) floor(sf * dbox->min()[Geom::Y] + 0.5);
+                        ibox.x1 = (int) floor(sf * dbox->max()[Geom::X] + 0.5);
+                        ibox.y1 = (int) floor(sf * dbox->max()[Geom::Y] + 0.5);
 
                         if ( dump ) {
                             g_message( "   box2   --'%s'  (%f,%f)-(%f,%f)", name, (double)ibox.x0, (double)ibox.y0, (double)ibox.x1, (double)ibox.y1 );
@@ -632,7 +754,7 @@ sp_icon_doc_icon( SPDocument *doc, NRArenaItem *root,
                     g_message( "   ua     --'%s'  (%f,%f)-(%f,%f)", name, (double)ua.x0, (double)ua.y0, (double)ua.x1, (double)ua.y1 );
                 }
                 /* Set up pixblock */
-                px = nr_new(guchar, 4 * psize * psize);
+                px = g_new(guchar, 4 * psize * psize);
                 memset(px, 0x00, 4 * psize * psize);
                 /* Render */
                 NRPixBlock B;
@@ -641,11 +763,11 @@ sp_icon_doc_icon( SPDocument *doc, NRArenaItem *root,
                                           px + 4 * psize * (ua.y0 - area.y0) +
                                           4 * (ua.x0 - area.x0),
                                           4 * psize, FALSE, FALSE );
-                nr_arena_item_invoke_render( root, &ua, &B,
+                nr_arena_item_invoke_render(NULL, root, &ua, &B,
                                              NR_ARENA_ITEM_RENDER_NO_CACHE );
                 nr_pixblock_release(&B);
 
-                gint useOverlay = prefs_get_int_attribute_limited( "debug.icons", "overlaySvg", 0, 0, 1 );
+                bool useOverlay = prefs->getBool("/debug/icons/overlaySvg");
                 if ( useOverlay ) {
                     sp_icon_overlay_pixels( px, psize, psize, 4 * psize, 0x00, 0x00, 0xff );
                 }
@@ -665,10 +787,10 @@ struct svg_doc_cache_t
 };
 
 static std::map<Glib::ustring, svg_doc_cache_t *> doc_cache;
-static std::map<Glib::ustring, guchar *> px_cache;
+static std::map<Glib::ustring, GdkPixbuf *> pb_cache;
 
-static Glib::ustring icon_cache_key(gchar const *name,
-                                    unsigned lsize, unsigned psize)
+Glib::ustring icon_cache_key(gchar const *name,
+                             unsigned lsize, unsigned psize)
 {
     Glib::ustring key=name;
     key += ":";
@@ -678,16 +800,16 @@ static Glib::ustring icon_cache_key(gchar const *name,
     return key;
 }
 
-static guchar *get_cached_pixels(Glib::ustring const &key) {
-    std::map<Glib::ustring, guchar *>::iterator found=px_cache.find(key);
-    if ( found != px_cache.end() ) {
+GdkPixbuf *get_cached_pixbuf(Glib::ustring const &key) {
+    std::map<Glib::ustring, GdkPixbuf *>::iterator found = pb_cache.find(key);
+    if ( found != pb_cache.end() ) {
         return found->second;
     }
     return NULL;
 }
 
 static guchar *load_svg_pixels(gchar const *name,
-                               unsigned lsize, unsigned psize)
+                               unsigned /*lsize*/, unsigned psize)
 {
     SPDocument *doc = NULL;
     NRArenaItem *root = NULL;
@@ -758,36 +880,108 @@ static guchar *load_svg_pixels(gchar const *name,
     return px;
 }
 
+static void populate_placeholder_icon(gchar const* name, GtkIconSize size)
+{
+    if ( iconSetCache.find(name) == iconSetCache.end() ) {
+        // only add a placeholder if nothing is already set
+        Gtk::IconSet icnset;
+        Gtk::IconSource src;
+        src.set_icon_name( GTK_STOCK_MISSING_IMAGE );
+        src.set_size( Gtk::IconSize(size) );
+        icnset.add_source(src);
+        inkyIcons->add(Gtk::StockID(name), icnset);
+    }
+}
+
+static void addToIconSet(GdkPixbuf* pb, gchar const* name, GtkIconSize lsize, unsigned psize) {
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    static bool dump = prefs->getBool("/debug/icons/dumpGtk");
+    GtkStockItem stock;
+    gboolean stockFound = gtk_stock_lookup( name, &stock );
+    if ( !stockFound ) {
+        Gtk::IconTheme::add_builtin_icon( name, psize, Glib::wrap(pb) );
+        if (dump) {
+            g_message("    set in a builtin for %s:%d:%d", name, lsize, psize);
+        }
+    }
+
+    for ( std::vector<IconCacheItem>::iterator it = iconSetCache[name].begin(); it != iconSetCache[name].end(); ++it ) {
+        if ( it->_lsize == lsize ) {
+            iconSetCache[name].erase(it);
+            break;
+        }
+    }
+    iconSetCache[name].push_back(IconCacheItem(lsize, pb));
+
+    Gtk::IconSet icnset;
+    for ( std::vector<IconCacheItem>::iterator it = iconSetCache[name].begin(); it != iconSetCache[name].end(); ++it ) {
+        Gtk::IconSource src;
+        g_object_ref( G_OBJECT(it->_pb) );
+        src.set_pixbuf( Glib::wrap(it->_pb) );
+        src.set_size( Gtk::IconSize(it->_lsize) );
+        src.set_size_wildcarded( (it->_lsize != 1) || (iconSetCache[name].size() == 1) );
+        src.set_state_wildcarded( true );
+        icnset.add_source(src);
+    }
+    inkyIcons->add(Gtk::StockID(name), icnset);
+}
+
 // returns true if icon needed preloading, false if nothing was done
-static bool prerender_icon(gchar const *name, unsigned lsize, unsigned psize)
+bool prerender_icon(gchar const *name, GtkIconSize lsize, unsigned psize)
 {
-    Glib::ustring key=icon_cache_key(name, lsize, psize);
-    guchar *px=get_cached_pixels(key);
-    if (px) {
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    static bool dump = prefs->getBool("/debug/icons/dumpGtk");
+    Glib::ustring key = icon_cache_key(name, lsize, psize);
+    GdkPixbuf *pb = get_cached_pixbuf(key);
+    if (pb) {
         return false;
     } else {
-        px = load_svg_pixels(name, lsize, psize);
+        guchar* px = load_svg_pixels(name, lsize, psize);
+        if ( !px ) {
+            // check for a fallback name
+            if ( legacyNames.find(name) != legacyNames.end() ) {
+                if ( dump ) {
+                    g_message("load_svg_pixels([%s]=%s, %d, %d)", name, legacyNames[name].c_str(), lsize, psize);
+                }
+                px = load_svg_pixels(legacyNames[name].c_str(), lsize, psize);
+            }
+        }
+
         if (px) {
-            px_cache[key] = px;
+            pb = gdk_pixbuf_new_from_data(px, GDK_COLORSPACE_RGB, TRUE, 8,
+                                          psize, psize, psize * 4,
+                                          (GdkPixbufDestroyNotify)g_free, NULL);
+            pb_cache[key] = pb;
+            addToIconSet(pb, name, lsize, psize);
+        } else if (dump) {
+            g_message("XXXXXXXXXXXXXXXXXXXXXXXXXXXXX  error!!! pixels not found for '%s'", name);
         }
         return true;
     }
 }
 
-static guchar *
-sp_icon_image_load_svg(gchar const *name, unsigned lsize, unsigned psize)
+static GdkPixbuf *sp_icon_image_load_svg(gchar const *name, GtkIconSize lsize, unsigned psize)
 {
-    Glib::ustring key=icon_cache_key(name, lsize, psize);
+    Glib::ustring key = icon_cache_key(name, lsize, psize);
 
     // did we already load this icon at this scale/size?
-    guchar *px=get_cached_pixels(key);
-    if (!px) {
-        px = load_svg_pixels(name, lsize, psize);
+    GdkPixbuf* pb = get_cached_pixbuf(key);
+    if (!pb) {
+        guchar *px = load_svg_pixels(name, lsize, psize);
         if (px) {
-            px_cache[key] = px;
+            pb = gdk_pixbuf_new_from_data(px, GDK_COLORSPACE_RGB, TRUE, 8,
+                                          psize, psize, psize * 4,
+                                          (GdkPixbufDestroyNotify)g_free, NULL);
+            pb_cache[key] = pb;
+            addToIconSet(pb, name, lsize, psize);
         }
     }
-    return px;
+
+    if ( pb ) {
+        // increase refcount since we're handing out ownership
+        g_object_ref(G_OBJECT(pb));
+    }
+    return pb;
 }
 
 void sp_icon_overlay_pixels(guchar *px, int width, int height, int stride,
@@ -857,61 +1051,91 @@ public:
 };
 
 
-#include <queue>
-
-static std::queue<preRenderItem> pendingRenders;
+static std::vector<preRenderItem> pendingRenders;
 static bool callbackHooked = false;
-static Glib::Timer *prerender_timer=NULL;
 
 static void addPreRender( GtkIconSize lsize, gchar const *name )
 {
-
     if ( !callbackHooked )
     {
-        g_message("Beginning icon prerendering");
-        prerender_timer = new Glib::Timer();
         callbackHooked = true;
         g_idle_add_full( G_PRIORITY_LOW, &icon_prerender_task, NULL, NULL );
     }
 
-    pendingRenders.push(preRenderItem(lsize, name));
+    pendingRenders.push_back(preRenderItem(lsize, name));
 }
 
-// in seconds; 10msec is roughly the threshold for human-perceptible lag,
-// but up to 60-70msec is tolerable
-#define INTERACTIVE_LIMIT 0.07
-
-static inline int seconds_to_msec(double seconds) {
-    return (int)(seconds * 1000 + 0.5);
-}
-
-gboolean icon_prerender_task(gpointer data) {
-    Glib::Timer timer;
-
+gboolean icon_prerender_task(gpointer /*data*/) {
     if (!pendingRenders.empty()) {
-        preRenderItem single=pendingRenders.front();
-        pendingRenders.pop();
-        int psize = sp_icon_get_phys_size(single._lsize);
-        prerender_icon(single._name.c_str(), single._lsize, psize);
-
-        double elapsed=timer.elapsed();
-        if ( elapsed > INTERACTIVE_LIMIT ) {
-            g_warning("Prerendering of icon \"%s\" at %dx%d pixels exceeded %dmsec (%dmsec)", single._name.c_str(), psize, psize, seconds_to_msec(INTERACTIVE_LIMIT), seconds_to_msec(elapsed));
-        }
+        bool workDone = false;
+        do {
+            preRenderItem single = pendingRenders.front();
+            pendingRenders.erase(pendingRenders.begin());
+            int psize = sp_icon_get_phys_size(single._lsize);
+            workDone = prerender_icon(single._name.c_str(), single._lsize, psize);
+        } while (!pendingRenders.empty() && !workDone);
     }
 
     if (!pendingRenders.empty()) {
         return TRUE;
     } else {
-        prerender_timer->stop();
-        g_message("Icon prerendering complete after %g seconds", prerender_timer->elapsed());
-        delete prerender_timer;
-        prerender_timer = NULL;
         callbackHooked = false;
         return FALSE;
     }
 }
 
+
+void imageMapCB(GtkWidget* widget, gpointer user_data) {
+    gchar* id = 0;
+    GtkIconSize size = GTK_ICON_SIZE_INVALID;
+    gtk_image_get_stock(GTK_IMAGE(widget), &id, &size);
+    GtkIconSize lsize = static_cast<GtkIconSize>(GPOINTER_TO_INT(user_data));
+    if ( id ) {
+        int psize = sp_icon_get_phys_size(lsize);
+        //g_message("imageMapCB(%p) for %s:%d:%d", widget, id, lsize, psize);
+        for ( std::vector<preRenderItem>::iterator it = pendingRenders.begin(); it != pendingRenders.end(); ++it ) {
+            if ( (it->_name == id) && (it->_lsize == lsize) ) {
+                prerender_icon(id, lsize, psize);
+                pendingRenders.erase(it);
+                //g_message("    prerender for %s:%d:%d", id, lsize, psize);
+                if (lsize != size) {
+                    int psize = sp_icon_get_phys_size(size);
+                    prerender_icon(id, size, psize);
+                }
+                break;
+            }
+        }
+    }
+
+    g_signal_handlers_disconnect_by_func(widget, (gpointer)imageMapCB, user_data);
+}
+
+static void imageMapNamedCB(GtkWidget* widget, gpointer user_data) {
+    GtkImage* img = GTK_IMAGE(widget);
+    gchar const* iconName = 0;
+    GtkIconSize size = GTK_ICON_SIZE_INVALID;
+    gtk_image_get_icon_name(img, &iconName, &size);
+    if ( iconName ) {
+        GtkImageType type = gtk_image_get_storage_type( GTK_IMAGE(img) );
+        if ( type == GTK_IMAGE_ICON_NAME ) {
+            for ( std::vector<preRenderItem>::iterator it = pendingRenders.begin(); it != pendingRenders.end(); ++it ) {
+                if ( (it->_name == iconName) && (it->_lsize == size) ) {
+                    int psize = sp_icon_get_phys_size(size);
+                    prerender_icon(iconName, size, psize);
+                    pendingRenders.erase(it);
+                    break;
+                }
+            }
+
+        } else {
+            g_warning("UNEXPECTED TYPE of %d", (int)type);
+        }
+    }
+
+    g_signal_handlers_disconnect_by_func(widget, (gpointer)imageMapNamedCB, user_data);
+}
+
+
 /*
   Local Variables:
   mode:c++