Code

remove many unnecessary to_2geom and from_2geom calls
[inkscape.git] / src / widgets / icon.cpp
index 2c89f010b36271d4fbd04edd1aa9e7bafd92ecf7..cdd356871c80d5d840c695a3d05b199ac28c297a 100644 (file)
@@ -23,6 +23,7 @@
 #include <gtkmm/iconfactory.h>
 #include <gtkmm/iconset.h>
 #include <gtkmm/iconsource.h>
+#include <gtkmm/icontheme.h>
 #include <gtkmm/image.h>
 
 #include "path-prefix.h"
@@ -264,6 +265,17 @@ static void sp_icon_theme_changed( SPIcon *icon )
 
 static void imageMapCB(GtkWidget* widget, gpointer user_data);
 static void populate_placeholder_icon(gchar const* name, unsigned lsize);
+static bool prerender_icon(gchar const *name, unsigned 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( Inkscape::IconSize lsize, gchar const *name )
@@ -277,12 +289,22 @@ sp_icon_new_full( Inkscape::IconSize lsize, gchar const *name )
             injectCustomSize();
         }
 
-        GtkWidget *img = gtk_image_new_from_stock( name, iconSizeLookup[trySize] );
+        GtkStockItem stock;
+        gboolean stockFound = gtk_stock_lookup( name, &stock );
+
+        GtkWidget *img = 0;
+        if ( legacyNames.empty() ) {
+            setupLegacyNaming();
+        }
+
+        if ( legacyNames.find(name) != legacyNames.end() ) {
+            img = gtk_image_new_from_icon_name( name, iconSizeLookup[trySize] );
+        } else {
+            img = gtk_image_new_from_stock( name, iconSizeLookup[trySize] );
+        }
         if ( img ) {
             GtkImageType type = gtk_image_get_storage_type( GTK_IMAGE(img) );
             if ( type == GTK_IMAGE_STOCK ) {
-                GtkStockItem stock;
-                gboolean stockFound = gtk_stock_lookup( name, &stock );
                 if ( !stockFound ) {
                     // It's not showing as a stock ID, so assume it will be present internally
                     populate_placeholder_icon( name, lsize );
@@ -297,11 +319,15 @@ sp_icon_new_full( Inkscape::IconSize lsize, gchar const *name )
                 if ( dump ) {
                     g_message( "loaded gtk  '%s' %d  (GTK_IMAGE_STOCK) %s", name, lsize, (stockFound ? "STOCK" : "local") );
                 }
+            } else if ( type == GTK_IMAGE_ICON_NAME ) {
+                widget = GTK_WIDGET(img);
+                img = 0;
+                addPreRender( lsize, 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;
             }
         }
@@ -596,13 +622,17 @@ sp_icon_doc_icon( SPDocument *doc, NRArenaItem *root,
         SPObject *object = doc->getObjectById(name);
         if (object && SP_IS_ITEM(object)) {
             /* Find bbox in document */
-            NR::Matrix const i2doc(from_2geom(sp_item_i2doc_affine(SP_ITEM(object))));
-            NR::Maybe<NR::Rect> dbox = SP_ITEM(object)->getBounds(i2doc);
+            Geom::Matrix const i2doc(sp_item_i2doc_affine(SP_ITEM(object)));
+            boost::optional<NR::Rect> nrdbox = SP_ITEM(object)->getBounds(i2doc);
+            boost::optional<Geom::Rect> dbox;
+            if (nrdbox) {
+                dbox = to_2geom(*nrdbox);
+            }
 
             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 */
@@ -610,17 +640,17 @@ sp_icon_doc_icon( SPDocument *doc, NRArenaItem *root,
                 NRGC gc(NULL);
                 /* Update to renderable state */
                 double sf = 1.0;
-                nr_arena_item_set_transform(root, NR::Matrix(NR::scale(sf, sf)));
+                nr_arena_item_set_transform(root, (Geom::Matrix)Geom::Scale(sf, sf));
                 gc.transform.set_identity();
                 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 );
@@ -642,16 +672,16 @@ sp_icon_doc_icon( SPDocument *doc, NRArenaItem *root,
                         }
                         sf = (double)psize / (double)block;
 
-                        nr_arena_item_set_transform(root, NR::Matrix(NR::scale(sf, sf)));
+                        nr_arena_item_set_transform(root, (Geom::Matrix)Geom::Scale(sf, sf));
                         gc.transform.set_identity();
                         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 );
@@ -724,8 +754,8 @@ struct svg_doc_cache_t
 static std::map<Glib::ustring, svg_doc_cache_t *> doc_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 += ":";
@@ -735,7 +765,7 @@ static Glib::ustring icon_cache_key(gchar const *name,
     return key;
 }
 
-static GdkPixbuf *get_cached_pixbuf(Glib::ustring const &key) {
+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;
@@ -828,7 +858,14 @@ static void populate_placeholder_icon(gchar const* name, unsigned lsize)
     }
 }
 
-static void addToIconSet(GdkPixbuf* pb, gchar const* name, unsigned lsize, unsigned /*psize*/) {
+static void addToIconSet(GdkPixbuf* pb, gchar const* name, unsigned lsize, unsigned psize) {
+    GtkStockItem stock;
+    gboolean stockFound = gtk_stock_lookup( name, &stock );
+    if ( !stockFound ) {
+        Gtk::IconTheme::add_builtin_icon( name, psize, Glib::wrap(pb) );
+        //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 == Inkscape::IconSize(lsize) ) {
             iconSetCache[name].erase(it);
@@ -851,7 +888,7 @@ static void addToIconSet(GdkPixbuf* pb, gchar const* name, unsigned lsize, unsig
 }
 
 // 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, unsigned lsize, unsigned psize)
 {
     Glib::ustring key = icon_cache_key(name, lsize, psize);
     GdkPixbuf *pb = get_cached_pixbuf(key);
@@ -859,6 +896,13 @@ static bool prerender_icon(gchar const *name, unsigned lsize, unsigned psize)
         return false;
     } else {
         guchar* px = load_svg_pixels(name, lsize, psize);
+        if ( !px ) {
+            // check for a fallback name
+            if ( legacyNames.find(name) != legacyNames.end() ) {
+                px = load_svg_pixels(legacyNames[name].c_str(), lsize, psize);
+            }
+        }
+
         if (px) {
             pb = gdk_pixbuf_new_from_data(px, GDK_COLORSPACE_RGB, TRUE, 8,
                                           psize, psize, psize * 4,
@@ -887,8 +931,10 @@ static GdkPixbuf *sp_icon_image_load_svg(gchar const *name, unsigned lsize, unsi
         }
     }
 
-    // increase refcount since we're hading out ownership
-    g_object_ref(G_OBJECT(pb));
+    if ( pb ) {
+        // increase refcount since we're handing out ownership
+        g_object_ref(G_OBJECT(pb));
+    }
     return pb;
 }
 
@@ -966,7 +1012,6 @@ static bool callbackHooked = false;
 
 static void addPreRender( Inkscape::IconSize lsize, gchar const *name )
 {
-
     if ( !callbackHooked )
     {
         callbackHooked = true;
@@ -995,7 +1040,7 @@ gboolean icon_prerender_task(gpointer /*data*/) {
     }
 }
 
-void imageMapCB(GtkWidget* widget, gpointer user_data) {
+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);