Code

Fixing preview/swatch sizes.
[inkscape.git] / src / widgets / icon.cpp
index 61c6429895e49a938b328569fe993534a8a06617..8c582da83ca4ef1e4befdcf8547d60ab4accb7db 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/image.h>
 
+#include "path-prefix.h"
 #include "prefs-utils.h"
 #include "inkscape.h"
 #include "document.h"
@@ -60,8 +59,6 @@ static guchar *sp_icon_image_load_svg(gchar const *name, unsigned lsize, unsigne
 
 static guchar *sp_icon_image_load(SPIcon *icon, gchar const *name);
 
-static int sp_icon_get_phys_size(int size);
-
 static void sp_icon_overlay_pixels( guchar *px, int width, int height, int stride,
                                     unsigned r, unsigned g, unsigned b );
 
@@ -188,6 +185,19 @@ static int sp_icon_expose(GtkWidget *widget, GdkEventExpose *event)
 {
     if ( GTK_WIDGET_DRAWABLE(widget) ) {
         SPIcon *icon = SP_ICON(widget);
+        if ( !icon->pb ) {
+            sp_icon_fetch_pixbuf( icon );
+        }
+
+        sp_icon_paint(SP_ICON(widget), &event->area);
+    }
+
+    return TRUE;
+}
+
+void sp_icon_fetch_pixbuf( SPIcon *icon )
+{
+    if ( icon ) {
         if ( !icon->pb ) {
             guchar *pixels = 0;
 
@@ -196,12 +206,12 @@ static int sp_icon_expose(GtkWidget *widget, GdkEventExpose *event)
             pixels = sp_icon_image_load( icon, icon->name );
 
             if (pixels) {
-                // don't pass the nr_free because we're caching the pixel
+                // don't pass the g_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);
+                                                    /*(GdkPixbufDestroyNotify)g_free*/NULL, NULL);
                 icon->pb_faded = gdk_pixbuf_copy(icon->pb);
 
                 pixels = gdk_pixbuf_get_pixels(icon->pb_faded);
@@ -220,14 +230,9 @@ static int sp_icon_expose(GtkWidget *widget, GdkEventExpose *event)
                 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 ) {
@@ -370,8 +375,8 @@ static void injectCustomSize()
         gint width = 0;
         gint height = 0;
         if ( gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &width, &height ) ) {
-            gint newWidth = (width > 18) ? (width / 2) : ((width * 2) / 3);
-            gint newHeight = (height > 18) ? (height / 2) : ((height * 2) / 3);
+            gint newWidth = ((width * 3) / 4);
+            gint newHeight = ((height * 3) / 4);
             GtkIconSize newSizeEnum = gtk_icon_size_register( "inkscape-decoration", newWidth, newHeight );
             if ( newSizeEnum ) {
                 if ( dump ) {
@@ -390,7 +395,7 @@ static void injectCustomSize()
 
 }
 
-static int sp_icon_get_phys_size(int size)
+int sp_icon_get_phys_size(int size)
 {
     static bool init = false;
     static int lastSys[Inkscape::ICON_SIZE_DECORATION + 1];
@@ -415,7 +420,8 @@ static int sp_icon_get_phys_size(int size)
                 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;
@@ -459,7 +465,8 @@ static int sp_icon_get_phys_size(int size)
         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;
@@ -518,17 +525,22 @@ 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)
+sp_icon_image_load_pixmap(gchar const *name, unsigned /*lsize*/, unsigned psize)
 {
     gchar *path;
     guchar *px;
@@ -573,7 +585,7 @@ sp_icon_image_load_pixmap(gchar const *name, unsigned lsize, unsigned psize)
         }
         guchar *spx = gdk_pixbuf_get_pixels(pb);
         int srs = gdk_pixbuf_get_rowstride(pb);
-        px = nr_new(guchar, 4 * psize * psize);
+        px = g_new(guchar, 4 * psize * psize);
         for (unsigned y = 0; y < psize; y++) {
             memcpy(px + 4 * y * psize, spx + y * srs, 4 * psize);
         }
@@ -598,7 +610,7 @@ sp_icon_doc_icon( SPDocument *doc, NRArenaItem *root,
         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);
+            NR::Maybe<NR::Rect> dbox = SP_ITEM(object)->getBounds(i2doc);
 
             if ( SP_OBJECT_PARENT(object) == NULL )
             {
@@ -607,7 +619,7 @@ sp_icon_doc_icon( SPDocument *doc, NRArenaItem *root,
             }
 
             /* This is in document coordinates, i.e. pixels */
-            if (dbox.isEmpty() == false) {
+            if ( dbox && !dbox->isEmpty() ) {
                 NRGC gc(NULL);
                 /* Update to renderable state */
                 double sf = 1.0;
@@ -620,10 +632,10 @@ sp_icon_doc_icon( SPDocument *doc, NRArenaItem *root,
                                              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()[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);
 
                 if ( dump ) {
                     g_message( "   box    --'%s'  (%f,%f)-(%f,%f)", name, (double)ibox.x0, (double)ibox.y0, (double)ibox.x1, (double)ibox.y1 );
@@ -652,10 +664,10 @@ sp_icon_doc_icon( SPDocument *doc, NRArenaItem *root,
                                                      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()[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);
 
                         if ( dump ) {
                             g_message( "   box2   --'%s'  (%f,%f)-(%f,%f)", name, (double)ibox.x0, (double)ibox.y0, (double)ibox.x1, (double)ibox.y1 );
@@ -693,7 +705,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;
@@ -702,7 +714,7 @@ 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);
 
@@ -748,7 +760,7 @@ static guchar *get_cached_pixels(Glib::ustring const &key) {
 }
 
 static guchar *load_svg_pixels(gchar const *name,
-                               unsigned lsize, unsigned psize)
+                               unsigned /*lsize*/, unsigned psize)
 {
     SPDocument *doc = NULL;
     NRArenaItem *root = NULL;
@@ -822,8 +834,8 @@ static guchar *load_svg_pixels(gchar const *name,
 // returns true if icon needed preloading, false if nothing was done
 static bool prerender_icon(gchar const *name, unsigned lsize, unsigned psize)
 {
-    Glib::ustring key=icon_cache_key(name, lsize, psize);
-    guchar *px=get_cached_pixels(key);
+    Glib::ustring key = icon_cache_key(name, lsize, psize);
+    guchar *px = get_cached_pixels(key);
     if (px) {
         return false;
     } else {
@@ -838,10 +850,10 @@ static bool prerender_icon(gchar const *name, unsigned lsize, unsigned psize)
 static guchar *
 sp_icon_image_load_svg(gchar const *name, unsigned 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);
+    guchar *px = get_cached_pixels(key);
     if (!px) {
         px = load_svg_pixels(name, lsize, psize);
         if (px) {
@@ -935,7 +947,7 @@ static void addPreRender( Inkscape::IconSize lsize, gchar const *name )
     pendingRenders.push(preRenderItem(lsize, name));
 }
 
-gboolean icon_prerender_task(gpointer data) {
+gboolean icon_prerender_task(gpointer /*data*/) {
     if (!pendingRenders.empty()) {
         preRenderItem single=pendingRenders.front();
         pendingRenders.pop();