Code

remove icon prerender time messages
[inkscape.git] / src / widgets / icon.cpp
1 /** \file
2  * SPIcon: Generic icon widget
3  */
4 /*
5  * Author:
6  *   Lauris Kaplinski <lauris@kaplinski.com>
7  *   Jon A. Cruz <jon@joncruz.org>
8  *
9  * Copyright (C) 2002 Lauris Kaplinski
10  *
11  * Released under GNU GPL, read the file 'COPYING' for more information
12  */
14 #ifdef HAVE_CONFIG_H
15 # include "config.h"
16 #endif
17 #include "path-prefix.h"
21 #include <gtk/gtkiconfactory.h>
22 #include <gtk/gtkstock.h>
23 #include <gtk/gtkimage.h>
25 #include <gtkmm/image.h>
27 #include "prefs-utils.h"
28 #include "inkscape.h"
29 #include "document.h"
30 #include "sp-item.h"
31 #include "display/nr-arena.h"
32 #include "display/nr-arena-item.h"
33 #include "io/sys.h"
35 #include "icon.h"
37 static gboolean icon_prerender_task(gpointer data);
39 static void addPreRender( GtkIconSize lsize, gchar const *name );
41 static void sp_icon_class_init(SPIconClass *klass);
42 static void sp_icon_init(SPIcon *icon);
43 static void sp_icon_destroy(GtkObject *object);
45 static void sp_icon_reset(SPIcon *icon);
46 static void sp_icon_clear(SPIcon *icon);
48 static void sp_icon_size_request(GtkWidget *widget, GtkRequisition *requisition);
49 static void sp_icon_size_allocate(GtkWidget *widget, GtkAllocation *allocation);
50 static int sp_icon_expose(GtkWidget *widget, GdkEventExpose *event);
52 static void sp_icon_paint(SPIcon *icon, GdkRectangle const *area);
54 static void sp_icon_screen_changed( GtkWidget *widget, GdkScreen *previous_screen );
55 static void sp_icon_style_set( GtkWidget *widget, GtkStyle *previous_style );
56 static void sp_icon_theme_changed( SPIcon *icon );
58 static guchar *sp_icon_image_load_pixmap(gchar const *name, unsigned lsize, unsigned psize);
59 static guchar *sp_icon_image_load_svg(gchar const *name, unsigned lsize, unsigned psize);
61 static guchar *sp_icon_image_load(SPIcon *icon, gchar const *name);
63 static int sp_icon_get_phys_size(int size);
65 static void sp_icon_overlay_pixels( guchar *px, int width, int height, int stride,
66                                     unsigned r, unsigned g, unsigned b );
68 static GtkWidgetClass *parent_class;
70 static bool sizeDirty = true;
72 GtkType
73 sp_icon_get_type()
74 {
75     static GtkType type = 0;
76     if (!type) {
77         GtkTypeInfo info = {
78             "SPIcon",
79             sizeof(SPIcon),
80             sizeof(SPIconClass),
81             (GtkClassInitFunc) sp_icon_class_init,
82             (GtkObjectInitFunc) sp_icon_init,
83             NULL, NULL, NULL
84         };
85         type = gtk_type_unique(GTK_TYPE_WIDGET, &info);
86     }
87     return type;
88 }
90 static void
91 sp_icon_class_init(SPIconClass *klass)
92 {
93     GtkObjectClass *object_class;
94     GtkWidgetClass *widget_class;
96     object_class = (GtkObjectClass *) klass;
97     widget_class = (GtkWidgetClass *) klass;
99     parent_class = (GtkWidgetClass*)g_type_class_peek_parent(klass);
101     object_class->destroy = sp_icon_destroy;
103     widget_class->size_request = sp_icon_size_request;
104     widget_class->size_allocate = sp_icon_size_allocate;
105     widget_class->expose_event = sp_icon_expose;
106     widget_class->screen_changed = sp_icon_screen_changed;
107     widget_class->style_set = sp_icon_style_set;
111 static void
112 sp_icon_init(SPIcon *icon)
114     GTK_WIDGET_FLAGS(icon) |= GTK_NO_WINDOW;
115     icon->lsize = GTK_ICON_SIZE_BUTTON;
116     icon->psize = 0;
117     icon->name = 0;
118     icon->pb = 0;
119     icon->pb_faded = 0;
122 static void
123 sp_icon_destroy(GtkObject *object)
125     SPIcon *icon = SP_ICON(object);
126     sp_icon_clear(icon);
127     if ( icon->name ) {
128         g_free( icon->name );
129         icon->name = 0;
130     }
132     ((GtkObjectClass *) (parent_class))->destroy(object);
135 static void sp_icon_reset( SPIcon *icon ) {
136     icon->psize = 0;
137     sp_icon_clear(icon);
140 static void sp_icon_clear( SPIcon *icon ) {
141     if (icon->pb) {
142         g_object_unref(G_OBJECT(icon->pb));
143         icon->pb = NULL;
144     }
145     if (icon->pb_faded) {
146         g_object_unref(G_OBJECT(icon->pb_faded));
147         icon->pb_faded = NULL;
148     }
151 static void
152 sp_icon_size_request(GtkWidget *widget, GtkRequisition *requisition)
154     SPIcon const *icon = SP_ICON(widget);
156     int const size = ( icon->psize
157                        ? icon->psize
158                        : sp_icon_get_phys_size(icon->lsize) );
159     requisition->width = size;
160     requisition->height = size;
163 static void
164 sp_icon_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
166     widget->allocation = *allocation;
168     if (GTK_WIDGET_DRAWABLE(widget)) {
169         gtk_widget_queue_draw(widget);
170     }
173 static int sp_icon_expose(GtkWidget *widget, GdkEventExpose *event)
175     if ( GTK_WIDGET_DRAWABLE(widget) ) {
176         SPIcon *icon = SP_ICON(widget);
177         if ( !icon->pb ) {
178             guchar *pixels = 0;
180             icon->psize = sp_icon_get_phys_size(icon->lsize);
182             pixels = sp_icon_image_load( icon, icon->name );
184             if (pixels) {
185                 // don't pass the nr_free because we're caching the pixel
186                 // space loaded through ...
187                 // I just changed this. make sure sp_icon_image_load still does the right thing.
188                 icon->pb = gdk_pixbuf_new_from_data(pixels, GDK_COLORSPACE_RGB, TRUE, 8,
189                                                     icon->psize, icon->psize, icon->psize * 4,
190                                                     /*(GdkPixbufDestroyNotify)nr_free*/NULL, NULL);
191                 icon->pb_faded = gdk_pixbuf_copy(icon->pb);
193                 pixels = gdk_pixbuf_get_pixels(icon->pb_faded);
194                 size_t stride = gdk_pixbuf_get_rowstride(icon->pb_faded);
195                 pixels += 3; // alpha
196                 for ( int row = 0 ; row < icon->psize ; row++ ) {
197                     guchar *row_pixels = pixels;
198                     for ( int column = 0 ; column < icon->psize ; column++ ) {
199                         *row_pixels = *row_pixels >> 1;
200                         row_pixels += 4;
201                     }
202                     pixels += stride;
203                 }
204             } else {
205                 /* TODO: We should do something more useful if we can't load the image. */
206                 g_warning ("failed to load icon '%s'", icon->name);
207             }
208         }
210         sp_icon_paint(SP_ICON(widget), &event->area);
211     }
213     return TRUE;
217 static void sp_icon_screen_changed( GtkWidget *widget, GdkScreen *previous_screen )
219     if ( GTK_WIDGET_CLASS( parent_class )->screen_changed ) {
220         GTK_WIDGET_CLASS( parent_class )->screen_changed( widget, previous_screen );
221     }
222     SPIcon *icon = SP_ICON(widget);
223     sp_icon_theme_changed(icon);
226 static void sp_icon_style_set( GtkWidget *widget, GtkStyle *previous_style )
228     if ( GTK_WIDGET_CLASS( parent_class )->style_set ) {
229         GTK_WIDGET_CLASS( parent_class )->style_set( widget, previous_style );
230     }
231     SPIcon *icon = SP_ICON(widget);
232     sp_icon_theme_changed(icon);
235 static void sp_icon_theme_changed( SPIcon *icon )
237     //g_message("Got a change bump for this icon");
238     sizeDirty = true;
239     sp_icon_reset(icon);
240     gtk_widget_queue_draw( GTK_WIDGET(icon) );
244 static GtkWidget *
245 sp_icon_new_full( GtkIconSize lsize, gchar const *name )
247     static gint dump = prefs_get_int_attribute_limited( "debug.icons", "dumpGtk", 0, 0, 1 );
248     static gint fallback = prefs_get_int_attribute_limited( "debug.icons", "checkNames", 0, 0, 1 );
250     addPreRender( lsize, name );
252     GtkStockItem stock;
253     gboolean tryLoad = gtk_stock_lookup( name, &stock );
254     if ( !tryLoad && fallback ) {
255         tryLoad |= strncmp("gtk-", name, 4 ) == 0;
256     }
257     if ( !tryLoad && fallback ) {
258         tryLoad |= strncmp("gnome-", name, 6 ) == 0;
259     }
261     GtkWidget *widget = 0;
262     if ( tryLoad ) {
263         GtkWidget *img = gtk_image_new_from_stock( name, lsize );
264         if ( img ) {
265             GtkImageType type = gtk_image_get_storage_type( GTK_IMAGE(img) );
266             if ( type == GTK_IMAGE_STOCK ) {
267                 widget = GTK_WIDGET(img);
268                 img = 0;
270                 if ( dump ) {
271                     g_message( "loaded gtk  '%s' %d  (GTK_IMAGE_STOCK)", name, lsize );
272                 }
273             } else {
274                 if ( dump ) {
275                     g_message( "skipped gtk '%s' %d  (not GTK_IMAGE_STOCK)", name, lsize );
276                 }
277                 g_object_unref( (GObject *)img );
278                 img = 0;
279             }
280         }
281     }
283     if ( !widget ) {
284         SPIcon *icon = (SPIcon *)g_object_new(SP_TYPE_ICON, NULL);
285         icon->lsize = lsize;
286         icon->name = g_strdup(name);
287         icon->psize = sp_icon_get_phys_size(lsize);
289         widget = GTK_WIDGET(icon);
290     }
292     return widget;
295 GtkWidget *
296 sp_icon_new( GtkIconSize lsize, gchar const *name )
298     return sp_icon_new_full( lsize, name );
301 Gtk::Widget *sp_icon_get_icon( Glib::ustring const &oid, GtkIconSize size )
303     Gtk::Widget *result = 0;
304     GtkWidget *widget = sp_icon_new_full( size, oid.c_str() );
306     if ( widget ) {
307         if ( GTK_IS_IMAGE(widget) ) {
308             GtkImage *img = GTK_IMAGE(widget);
309             result = Glib::wrap( img );
310         } else {
311             result = Glib::wrap( widget );
312         }
313     }
315     return result;
318 // Try to load the named svg, falling back to pixmaps
319 guchar *
320 sp_icon_image_load( SPIcon *icon, gchar const *name )
322     guchar *px = sp_icon_image_load_svg( name, icon->lsize, icon->psize );
323     if (!px) {
324         px = sp_icon_image_load_pixmap(name, icon->lsize, icon->psize);
325     }
327     return px;
330 GtkIconSize
331 sp_icon_get_gtk_size(int size)
333     static GtkIconSize map[64] = {(GtkIconSize)0};
334     size = CLAMP(size, 4, 63);
335     if (!map[size]) {
336         static int count = 0;
337         char c[64];
338         g_snprintf(c, 64, "InkscapeIcon%d", count++);
339         map[size] = gtk_icon_size_register(c, size, size);
340     }
341     return map[size];
344 static int sp_icon_get_phys_size(int size)
346     static bool init = false;
347     static int lastSys[GTK_ICON_SIZE_DIALOG + 1];
348     static int vals[GTK_ICON_SIZE_DIALOG + 1];
350     size = CLAMP( size, GTK_ICON_SIZE_MENU, GTK_ICON_SIZE_DIALOG );
352     if ( sizeDirty && init ) {
353         GtkIconSize const gtkSizes[] = {
354             GTK_ICON_SIZE_MENU,
355             GTK_ICON_SIZE_SMALL_TOOLBAR,
356             GTK_ICON_SIZE_LARGE_TOOLBAR,
357             GTK_ICON_SIZE_BUTTON,
358             GTK_ICON_SIZE_DND,
359             GTK_ICON_SIZE_DIALOG
360         };
361         for (unsigned i = 0; i < G_N_ELEMENTS(gtkSizes) && init; ++i) {
362             unsigned const val_ix(gtkSizes[i]);
363             g_assert( val_ix < G_N_ELEMENTS(vals) );
365             gint width = 0;
366             gint height = 0;
367             if ( gtk_icon_size_lookup(gtkSizes[i], &width, &height ) ) {
368                 init &= (lastSys[val_ix] == std::max(width, height));
369             }
370         }
371     }
373     if ( !init ) {
374         sizeDirty = false;
375         gint dump = prefs_get_int_attribute_limited( "debug.icons", "dumpDefault", 0, 0, 1 );
376         if ( dump ) {
377             g_message( "Default icon sizes:" );
378         }
379         memset( vals, 0, sizeof(vals) );
380         memset( lastSys, 0, sizeof(lastSys) );
381         GtkIconSize const gtkSizes[] = {
382             GTK_ICON_SIZE_MENU,
383             GTK_ICON_SIZE_SMALL_TOOLBAR,
384             GTK_ICON_SIZE_LARGE_TOOLBAR,
385             GTK_ICON_SIZE_BUTTON,
386             GTK_ICON_SIZE_DND,
387             GTK_ICON_SIZE_DIALOG
388         };
389         gchar const *const names[] = {
390             "GTK_ICON_SIZE_MENU",
391             "GTK_ICON_SIZE_SMALL_TOOLBAR",
392             "GTK_ICON_SIZE_LARGE_TOOLBAR",
393             "GTK_ICON_SIZE_BUTTON",
394             "GTK_ICON_SIZE_DND",
395             "GTK_ICON_SIZE_DIALOG"
396         };
398         GtkWidget *icon = (GtkWidget *)g_object_new(SP_TYPE_ICON, NULL);
400         for (unsigned i = 0; i < G_N_ELEMENTS(gtkSizes); ++i) {
401             unsigned const val_ix(gtkSizes[i]);
402             g_assert( val_ix < G_N_ELEMENTS(vals) );
404             gint width = 0;
405             gint height = 0;
406             bool used = false;
407             if ( gtk_icon_size_lookup(gtkSizes[i], &width, &height ) ) {
408                 vals[val_ix] = std::max(width, height);
409                 lastSys[val_ix] = vals[val_ix];
410                 used = true;
411             }
412             if (dump) {
413                 g_message(" =--  %u  size:%d  %c(%d, %d)   '%s'",
414                           i, gtkSizes[i],
415                           ( used ? ' ' : 'X' ), width, height, names[i]);
416             }
417             gchar const *id = GTK_STOCK_OPEN;
418             GdkPixbuf *pb = gtk_widget_render_icon( icon, id, gtkSizes[i], NULL);
419             if (pb) {
420                 width = gdk_pixbuf_get_width(pb);
421                 height = gdk_pixbuf_get_height(pb);
422                 int newSize = std::max( width, height );
423                 // TODO perhaps check a few more stock icons to get a range on sizes.
424                 if ( newSize > 0 ) {
425                     vals[val_ix] = newSize;
426                 }
427                 if (dump) {
428                     g_message("      %u  size:%d   (%d, %d)", i, gtkSizes[i], width, height);
429                 }
431                 g_object_unref(G_OBJECT(pb));
432             }
433         }
434         //g_object_unref(icon);
435         init = true;
436     }
438     return vals[size];
441 static void sp_icon_paint(SPIcon *icon, GdkRectangle const *area)
443     GtkWidget &widget = *GTK_WIDGET(icon);
445     GdkPixbuf *image = GTK_WIDGET_IS_SENSITIVE(&widget) ? icon->pb : icon->pb_faded;
447     if (image) {
448         int const padx = ( ( widget.allocation.width > icon->psize )
449                            ? ( widget.allocation.width - icon->psize ) / 2
450                            : 0 );
451         int const pady = ( ( widget.allocation.height > icon->psize )
452                            ? ( widget.allocation.height - icon->psize ) / 2
453                            : 0 );
455         int const x0 = std::max(area->x, widget.allocation.x + padx);
456         int const y0 = std::max(area->y, widget.allocation.y + pady);
457         int const x1 = std::min(area->x + area->width,  widget.allocation.x + padx + static_cast<int>(icon->psize) );
458         int const y1 = std::min(area->y + area->height, widget.allocation.y + pady + static_cast<int>(icon->psize) );
460         gdk_draw_pixbuf(GDK_DRAWABLE(widget.window), NULL, image,
461                         x0 - widget.allocation.x - padx,
462                         y0 - widget.allocation.y - pady,
463                         x0, y0,
464                         x1 - x0, y1 - y0,
465                         GDK_RGB_DITHER_NORMAL, x0, y0);
466     }
469 static guchar *
470 sp_icon_image_load_pixmap(gchar const *name, unsigned lsize, unsigned psize)
472     gchar *path;
473     guchar *px;
474     GdkPixbuf *pb;
476     path = (gchar *) g_strdup_printf("%s/%s.png", INKSCAPE_PIXMAPDIR, name);
477     // TODO: bulia, please look over
478     gsize bytesRead = 0;
479     gsize bytesWritten = 0;
480     GError *error = NULL;
481     gchar *localFilename = g_filename_from_utf8( path,
482                                                  -1,
483                                                  &bytesRead,
484                                                  &bytesWritten,
485                                                  &error);
486     pb = gdk_pixbuf_new_from_file(localFilename, NULL);
487     g_free(localFilename);
488     g_free(path);
489     if (!pb) {
490         path = (gchar *) g_strdup_printf("%s/%s.xpm", INKSCAPE_PIXMAPDIR, name);
491         // TODO: bulia, please look over
492         gsize bytesRead = 0;
493         gsize bytesWritten = 0;
494         GError *error = NULL;
495         gchar *localFilename = g_filename_from_utf8( path,
496                                                      -1,
497                                                      &bytesRead,
498                                                      &bytesWritten,
499                                                      &error);
500         pb = gdk_pixbuf_new_from_file(localFilename, NULL);
501         g_free(localFilename);
502         g_free(path);
503     }
504     if (pb) {
505         if (!gdk_pixbuf_get_has_alpha(pb))
506             gdk_pixbuf_add_alpha(pb, FALSE, 0, 0, 0);
507         if ( ( static_cast<unsigned>(gdk_pixbuf_get_width(pb)) != psize )
508              || ( static_cast<unsigned>(gdk_pixbuf_get_height(pb)) != psize ) ) {
509             GdkPixbuf *spb = gdk_pixbuf_scale_simple(pb, psize, psize, GDK_INTERP_HYPER);
510             g_object_unref(G_OBJECT(pb));
511             pb = spb;
512         }
513         guchar *spx = gdk_pixbuf_get_pixels(pb);
514         int srs = gdk_pixbuf_get_rowstride(pb);
515         px = nr_new(guchar, 4 * psize * psize);
516         for (unsigned y = 0; y < psize; y++) {
517             memcpy(px + 4 * y * psize, spx + y * srs, 4 * psize);
518         }
519         g_object_unref(G_OBJECT(pb));
521         return px;
522     }
524     return NULL;
527 // takes doc, root, icon, and icon name to produce pixels
528 extern "C" guchar *
529 sp_icon_doc_icon( SPDocument *doc, NRArenaItem *root,
530                   gchar const *name, unsigned psize )
532     gint const dump = prefs_get_int_attribute_limited( "debug.icons", "dumpSvg", 0, 0, 1 );
533     guchar *px = NULL;
535     if (doc) {
536         SPObject *object = doc->getObjectById(name);
537         if (object && SP_IS_ITEM(object)) {
538             /* Find bbox in document */
539             NR::Matrix const i2doc(sp_item_i2doc_affine(SP_ITEM(object)));
540             NR::Rect dbox = SP_ITEM(object)->invokeBbox(i2doc);
542             if ( SP_OBJECT_PARENT(object) == NULL )
543             {
544                 dbox = NR::Rect(NR::Point(0, 0),
545                                 NR::Point(sp_document_width(doc), sp_document_height(doc)));
546             }
548             /* This is in document coordinates, i.e. pixels */
549             if (dbox.isEmpty() == false) {
550                 NRGC gc(NULL);
551                 /* Update to renderable state */
552                 double sf = 1.0;
553                 NRMatrix t;
554                 nr_matrix_set_scale(&t, sf, sf);
555                 nr_arena_item_set_transform(root, &t);
556                 nr_matrix_set_identity(&gc.transform);
557                 nr_arena_item_invoke_update( root, NULL, &gc,
558                                              NR_ARENA_ITEM_STATE_ALL,
559                                              NR_ARENA_ITEM_STATE_NONE );
560                 /* Item integer bbox in points */
561                 NRRectL ibox;
562                 ibox.x0 = (int) floor(sf * dbox.min()[NR::X] + 0.5);
563                 ibox.y0 = (int) floor(sf * dbox.min()[NR::Y] + 0.5);
564                 ibox.x1 = (int) floor(sf * dbox.max()[NR::X] + 0.5);
565                 ibox.y1 = (int) floor(sf * dbox.max()[NR::Y] + 0.5);
567                 if ( dump ) {
568                     g_message( "   box    --'%s'  (%f,%f)-(%f,%f)", name, (double)ibox.x0, (double)ibox.y0, (double)ibox.x1, (double)ibox.y1 );
569                 }
571                 /* Find button visible area */
572                 int width = ibox.x1 - ibox.x0;
573                 int height = ibox.y1 - ibox.y0;
575                 if ( dump ) {
576                     g_message( "   vis    --'%s'  (%d,%d)", name, width, height );
577                 }
579                 {
580                     int block = std::max(width, height);
581                     if (block != static_cast<int>(psize) ) {
582                         if ( dump ) {
583                             g_message("      resizing" );
584                         }
585                         sf = (double)psize / (double)block;
587                         nr_matrix_set_scale(&t, sf, sf);
588                         nr_arena_item_set_transform(root, &t);
589                         nr_matrix_set_identity(&gc.transform);
590                         nr_arena_item_invoke_update( root, NULL, &gc,
591                                                      NR_ARENA_ITEM_STATE_ALL,
592                                                      NR_ARENA_ITEM_STATE_NONE );
593                         /* Item integer bbox in points */
594                         ibox.x0 = (int) floor(sf * dbox.min()[NR::X] + 0.5);
595                         ibox.y0 = (int) floor(sf * dbox.min()[NR::Y] + 0.5);
596                         ibox.x1 = (int) floor(sf * dbox.max()[NR::X] + 0.5);
597                         ibox.y1 = (int) floor(sf * dbox.max()[NR::Y] + 0.5);
599                         if ( dump ) {
600                             g_message( "   box2   --'%s'  (%f,%f)-(%f,%f)", name, (double)ibox.x0, (double)ibox.y0, (double)ibox.x1, (double)ibox.y1 );
601                         }
603                         /* Find button visible area */
604                         width = ibox.x1 - ibox.x0;
605                         height = ibox.y1 - ibox.y0;
606                         if ( dump ) {
607                             g_message( "   vis2   --'%s'  (%d,%d)", name, width, height );
608                         }
609                     }
610                 }
612                 int dx, dy;
613                 //dx = (psize - width) / 2;
614                 //dy = (psize - height) / 2;
615                 dx=dy=psize;
616                 dx=(dx-width)/2; // watch out for psize, since 'unsigned'-'signed' can cause problems if the result is negative
617                 dy=(dy-height)/2;
618                 NRRectL area;
619                 area.x0 = ibox.x0 - dx;
620                 area.y0 = ibox.y0 - dy;
621                 area.x1 = area.x0 + psize;
622                 area.y1 = area.y0 + psize;
623                 /* Actual renderable area */
624                 NRRectL ua;
625                 ua.x0 = MAX(ibox.x0, area.x0);
626                 ua.y0 = MAX(ibox.y0, area.y0);
627                 ua.x1 = MIN(ibox.x1, area.x1);
628                 ua.y1 = MIN(ibox.y1, area.y1);
630                 if ( dump ) {
631                     g_message( "   area   --'%s'  (%f,%f)-(%f,%f)", name, (double)area.x0, (double)area.y0, (double)area.x1, (double)area.y1 );
632                     g_message( "   ua     --'%s'  (%f,%f)-(%f,%f)", name, (double)ua.x0, (double)ua.y0, (double)ua.x1, (double)ua.y1 );
633                 }
634                 /* Set up pixblock */
635                 px = nr_new(guchar, 4 * psize * psize);
636                 memset(px, 0x00, 4 * psize * psize);
637                 /* Render */
638                 NRPixBlock B;
639                 nr_pixblock_setup_extern( &B, NR_PIXBLOCK_MODE_R8G8B8A8N,
640                                           ua.x0, ua.y0, ua.x1, ua.y1,
641                                           px + 4 * psize * (ua.y0 - area.y0) +
642                                           4 * (ua.x0 - area.x0),
643                                           4 * psize, FALSE, FALSE );
644                 nr_arena_item_invoke_render( root, &ua, &B,
645                                              NR_ARENA_ITEM_RENDER_NO_CACHE );
646                 nr_pixblock_release(&B);
648                 gint useOverlay = prefs_get_int_attribute_limited( "debug.icons", "overlaySvg", 0, 0, 1 );
649                 if ( useOverlay ) {
650                     sp_icon_overlay_pixels( px, psize, psize, 4 * psize, 0x00, 0x00, 0xff );
651                 }
652             }
653         }
654     }
656     return px;
657 } // end of sp_icon_doc_icon()
661 struct svg_doc_cache_t
663     SPDocument *doc;
664     NRArenaItem *root;
665 };
667 static std::map<Glib::ustring, svg_doc_cache_t *> doc_cache;
668 static std::map<Glib::ustring, guchar *> px_cache;
670 static Glib::ustring icon_cache_key(gchar const *name,
671                                     unsigned lsize, unsigned psize)
673     Glib::ustring key=name;
674     key += ":";
675     key += lsize;
676     key += ":";
677     key += psize;
678     return key;
681 static guchar *get_cached_pixels(Glib::ustring const &key) {
682     std::map<Glib::ustring, guchar *>::iterator found=px_cache.find(key);
683     if ( found != px_cache.end() ) {
684         return found->second;
685     }
686     return NULL;
689 static guchar *load_svg_pixels(gchar const *name,
690                                unsigned lsize, unsigned psize)
692     SPDocument *doc = NULL;
693     NRArenaItem *root = NULL;
694     svg_doc_cache_t *info = NULL;
696     // Fall back from user prefs dir into system locations.
697     Glib::ustring iconsvg = name;
698     iconsvg += ".svg";
699     std::list<gchar *> sources;
700     sources.push_back(g_build_filename(profile_path("icons"),iconsvg.c_str(), NULL));
701     sources.push_back(g_build_filename(profile_path("icons"),"icons.svg", NULL));
702     sources.push_back(g_build_filename(INKSCAPE_PIXMAPDIR, iconsvg.c_str(), NULL));
703     sources.push_back(g_build_filename(INKSCAPE_PIXMAPDIR, "icons.svg", NULL));
705     // Try each document in turn until we successfully load the icon from one
706     guchar *px=NULL;
707     while ( !sources.empty() && !px ) {
708         gchar *doc_filename = sources.front();
710         // Did we already load this doc?
711         Glib::ustring key(doc_filename);
712         info = 0;
713         {
714             std::map<Glib::ustring, svg_doc_cache_t *>::iterator i = doc_cache.find(key);
715             if ( i != doc_cache.end() ) {
716                 info = i->second;
717             }
718         }
720         /* Try to load from document. */
721         if (!info &&
722             Inkscape::IO::file_test( doc_filename, G_FILE_TEST_IS_REGULAR ) &&
723             (doc = sp_document_new( doc_filename, FALSE )) ) {
725             // prep the document
726             sp_document_ensure_up_to_date(doc);
727             /* Create new arena */
728             NRArena *arena = NRArena::create();
729             /* Create ArenaItem and set transform */
730             unsigned visionkey = sp_item_display_key_new(1);
731             /* fixme: Memory manage root if needed (Lauris) */
732             root = sp_item_invoke_show( SP_ITEM(SP_DOCUMENT_ROOT(doc)),
733                                         arena, visionkey, SP_ITEM_SHOW_DISPLAY );
735             // store into the cache
736             info = new svg_doc_cache_t;
737             g_assert(info);
739             info->doc=doc;
740             info->root=root;
741             doc_cache[key]=info;
742         }
743         if (info) {
744             doc=info->doc;
745             root=info->root;
746         }
748         // toss the filename
749         g_free(doc_filename);
750         sources.pop_front();
752         // move on to the next document if we couldn't get anything
753         if (!info && !doc) continue;
755         px = sp_icon_doc_icon( doc, root, name, psize );
756     }
758     return px;
761 // returns true if icon needed preloading, false if nothing was done
762 static bool prerender_icon(gchar const *name, unsigned lsize, unsigned psize)
764     Glib::ustring key=icon_cache_key(name, lsize, psize);
765     guchar *px=get_cached_pixels(key);
766     if (px) {
767         return false;
768     } else {
769         px = load_svg_pixels(name, lsize, psize);
770         if (px) {
771             px_cache[key] = px;
772         }
773         return true;
774     }
777 static guchar *
778 sp_icon_image_load_svg(gchar const *name, unsigned lsize, unsigned psize)
780     Glib::ustring key=icon_cache_key(name, lsize, psize);
782     // did we already load this icon at this scale/size?
783     guchar *px=get_cached_pixels(key);
784     if (!px) {
785         px = load_svg_pixels(name, lsize, psize);
786         if (px) {
787             px_cache[key] = px;
788         }
789     }
790     return px;
793 void sp_icon_overlay_pixels(guchar *px, int width, int height, int stride,
794                             unsigned r, unsigned g, unsigned b)
796     for ( int y = 0; y < height; y += 4 ) {
797         guchar *ptr = px + y * stride;
798         for ( int x = 0; x < width; x += 4 ) {
799             *(ptr++) = r;
800             *(ptr++) = g;
801             *(ptr++) = b;
802             *(ptr++) = 0xff;
804             ptr += 4 * 3;
805         }
806     }
808     if ( width > 1 && height > 1 ) {
809         // point at the last pixel
810         guchar *ptr = px + ((height-1) * stride) + ((width - 1) * 4);
812         if ( width > 2 ) {
813             px[4] = r;
814             px[5] = g;
815             px[6] = b;
816             px[7] = 0xff;
818             ptr[-12] = r;
819             ptr[-11] = g;
820             ptr[-10] = b;
821             ptr[-9] = 0xff;
822         }
824         ptr[-4] = r;
825         ptr[-3] = g;
826         ptr[-2] = b;
827         ptr[-1] = 0xff;
829         px[0 + stride] = r;
830         px[1 + stride] = g;
831         px[2 + stride] = b;
832         px[3 + stride] = 0xff;
834         ptr[0 - stride] = r;
835         ptr[1 - stride] = g;
836         ptr[2 - stride] = b;
837         ptr[3 - stride] = 0xff;
839         if ( height > 2 ) {
840             ptr[0 - stride * 3] = r;
841             ptr[1 - stride * 3] = g;
842             ptr[2 - stride * 3] = b;
843             ptr[3 - stride * 3] = 0xff;
844         }
845     }
848 class preRenderItem
850 public:
851     preRenderItem( GtkIconSize lsize, gchar const *name ) :
852         _lsize( lsize ),
853         _name( name )
854     {}
855     GtkIconSize _lsize;
856     Glib::ustring _name;
857 };
860 #include <queue>
862 static std::queue<preRenderItem> pendingRenders;
863 static bool callbackHooked = false;
865 static void addPreRender( GtkIconSize lsize, gchar const *name )
868     if ( !callbackHooked )
869     {
870         callbackHooked = true;
871         g_idle_add_full( G_PRIORITY_LOW, &icon_prerender_task, NULL, NULL );
872     }
874     pendingRenders.push(preRenderItem(lsize, name));
877 gboolean icon_prerender_task(gpointer data) {
878     if (!pendingRenders.empty()) {
879         preRenderItem single=pendingRenders.front();
880         pendingRenders.pop();
881         int psize = sp_icon_get_phys_size(single._lsize);
882         prerender_icon(single._name.c_str(), single._lsize, psize);
883     }
885     if (!pendingRenders.empty()) {
886         return TRUE;
887     } else {
888         callbackHooked = false;
889         return FALSE;
890     }
893 /*
894   Local Variables:
895   mode:c++
896   c-file-style:"stroustrup"
897   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
898   indent-tabs-mode:nil
899   fill-column:99
900   End:
901 */
902 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :