Code

init matrix variable, removes compiler warnings
[inkscape.git] / src / widgets / icon.cpp
index 32ebb4c5798ed92702aa6f52aa21fcd464b2ae02..cab03fa503890783235b81d0a94d76c724bef1b8 100644 (file)
@@ -36,7 +36,7 @@
 
 static gboolean icon_prerender_task(gpointer data);
 
-static void addPreRender( GtkIconSize lsize, gchar const *name );
+static void addPreRender( Inkscape::IconSize lsize, gchar const *name );
 
 static void sp_icon_class_init(SPIconClass *klass);
 static void sp_icon_init(SPIcon *icon);
@@ -65,10 +65,24 @@ 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 );
 
+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
+};
+
 GtkType
 sp_icon_get_type()
 {
@@ -112,7 +126,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;
@@ -242,7 +256,7 @@ static void sp_icon_theme_changed( SPIcon *icon )
 
 
 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 );
@@ -260,7 +274,13 @@ sp_icon_new_full( GtkIconSize lsize, gchar const *name )
 
     GtkWidget *widget = 0;
     if ( tryLoad ) {
-        GtkWidget *img = gtk_image_new_from_stock( name, lsize );
+        gint trySize = CLAMP( static_cast<gint>(lsize), 0, static_cast<gint>(G_N_ELEMENTS(iconSizeLookup) - 1) );
+
+        if ( !sizeMapDone ) {
+            injectCustomSize();
+        }
+
+        GtkWidget *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 ) {
@@ -293,12 +313,12 @@ 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 )
+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() );
@@ -341,13 +361,46 @@ sp_icon_get_gtk_size(int size)
     return map[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 )
+    {
+        gint dump = prefs_get_int_attribute_limited( "debug.icons", "dumpDefault", 0, 0, 1 );
+        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 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 +409,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;
@@ -373,6 +430,7 @@ 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 );
+
         if ( dump ) {
             g_message( "Default icon sizes:" );
         }
@@ -384,7 +442,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 +453,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;
@@ -848,11 +911,11 @@ void sp_icon_overlay_pixels(guchar *px, int width, int height, int stride,
 class preRenderItem
 {
 public:
-    preRenderItem( GtkIconSize lsize, gchar const *name ) :
+    preRenderItem( Inkscape::IconSize lsize, gchar const *name ) :
         _lsize( lsize ),
         _name( name )
     {}
-    GtkIconSize _lsize;
+    Inkscape::IconSize _lsize;
     Glib::ustring _name;
 };
 
@@ -861,15 +924,12 @@ public:
 
 static std::queue<preRenderItem> pendingRenders;
 static bool callbackHooked = false;
-static Glib::Timer *prerender_timer=NULL;
 
-static void addPreRender( GtkIconSize lsize, gchar const *name )
+static void addPreRender( Inkscape::IconSize 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 );
     }
@@ -877,36 +937,17 @@ static void addPreRender( GtkIconSize lsize, gchar const *name )
     pendingRenders.push(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;
-
     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));
-        }
     }
 
     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;
     }