Code

missed a file
[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 = Inkscape::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 = (Inkscape::IconSize)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( Inkscape::IconSize lsize, gchar const *name )
298 // TODO FIX THIS
299     return sp_icon_new_full( (GtkIconSize)lsize, name );
302 Gtk::Widget *sp_icon_get_icon( Glib::ustring const &oid, Inkscape::IconSize size )
304     Gtk::Widget *result = 0;
305     GtkWidget *widget = sp_icon_new_full( (GtkIconSize)size, oid.c_str() );
307     if ( widget ) {
308         if ( GTK_IS_IMAGE(widget) ) {
309             GtkImage *img = GTK_IMAGE(widget);
310             result = Glib::wrap( img );
311         } else {
312             result = Glib::wrap( widget );
313         }
314     }
316     return result;
319 // Try to load the named svg, falling back to pixmaps
320 guchar *
321 sp_icon_image_load( SPIcon *icon, gchar const *name )
323     guchar *px = sp_icon_image_load_svg( name, icon->lsize, icon->psize );
324     if (!px) {
325         px = sp_icon_image_load_pixmap(name, icon->lsize, icon->psize);
326     }
328     return px;
331 GtkIconSize
332 sp_icon_get_gtk_size(int size)
334     static GtkIconSize map[64] = {(GtkIconSize)0};
335     size = CLAMP(size, 4, 63);
336     if (!map[size]) {
337         static int count = 0;
338         char c[64];
339         g_snprintf(c, 64, "InkscapeIcon%d", count++);
340         map[size] = gtk_icon_size_register(c, size, size);
341     }
342     return map[size];
345 static int sp_icon_get_phys_size(int size)
347     static bool init = false;
348     static int lastSys[GTK_ICON_SIZE_DIALOG + 1];
349     static int vals[GTK_ICON_SIZE_DIALOG + 1];
351     size = CLAMP( size, GTK_ICON_SIZE_MENU, GTK_ICON_SIZE_DIALOG );
353     if ( sizeDirty && init ) {
354         GtkIconSize const gtkSizes[] = {
355             GTK_ICON_SIZE_MENU,
356             GTK_ICON_SIZE_SMALL_TOOLBAR,
357             GTK_ICON_SIZE_LARGE_TOOLBAR,
358             GTK_ICON_SIZE_BUTTON,
359             GTK_ICON_SIZE_DND,
360             GTK_ICON_SIZE_DIALOG
361         };
362         for (unsigned i = 0; i < G_N_ELEMENTS(gtkSizes) && init; ++i) {
363             unsigned const val_ix(gtkSizes[i]);
364             g_assert( val_ix < G_N_ELEMENTS(vals) );
366             gint width = 0;
367             gint height = 0;
368             if ( gtk_icon_size_lookup(gtkSizes[i], &width, &height ) ) {
369                 init &= (lastSys[val_ix] == std::max(width, height));
370             }
371         }
372     }
374     if ( !init ) {
375         sizeDirty = false;
376         gint dump = prefs_get_int_attribute_limited( "debug.icons", "dumpDefault", 0, 0, 1 );
377         if ( dump ) {
378             g_message( "Default icon sizes:" );
379         }
380         memset( vals, 0, sizeof(vals) );
381         memset( lastSys, 0, sizeof(lastSys) );
382         GtkIconSize const gtkSizes[] = {
383             GTK_ICON_SIZE_MENU,
384             GTK_ICON_SIZE_SMALL_TOOLBAR,
385             GTK_ICON_SIZE_LARGE_TOOLBAR,
386             GTK_ICON_SIZE_BUTTON,
387             GTK_ICON_SIZE_DND,
388             GTK_ICON_SIZE_DIALOG
389         };
390         gchar const *const names[] = {
391             "GTK_ICON_SIZE_MENU",
392             "GTK_ICON_SIZE_SMALL_TOOLBAR",
393             "GTK_ICON_SIZE_LARGE_TOOLBAR",
394             "GTK_ICON_SIZE_BUTTON",
395             "GTK_ICON_SIZE_DND",
396             "GTK_ICON_SIZE_DIALOG"
397         };
399         GtkWidget *icon = (GtkWidget *)g_object_new(SP_TYPE_ICON, NULL);
401         for (unsigned i = 0; i < G_N_ELEMENTS(gtkSizes); ++i) {
402             unsigned const val_ix(gtkSizes[i]);
403             g_assert( val_ix < G_N_ELEMENTS(vals) );
405             gint width = 0;
406             gint height = 0;
407             bool used = false;
408             if ( gtk_icon_size_lookup(gtkSizes[i], &width, &height ) ) {
409                 vals[val_ix] = std::max(width, height);
410                 lastSys[val_ix] = vals[val_ix];
411                 used = true;
412             }
413             if (dump) {
414                 g_message(" =--  %u  size:%d  %c(%d, %d)   '%s'",
415                           i, gtkSizes[i],
416                           ( used ? ' ' : 'X' ), width, height, names[i]);
417             }
418             gchar const *id = GTK_STOCK_OPEN;
419             GdkPixbuf *pb = gtk_widget_render_icon( icon, id, gtkSizes[i], NULL);
420             if (pb) {
421                 width = gdk_pixbuf_get_width(pb);
422                 height = gdk_pixbuf_get_height(pb);
423                 int newSize = std::max( width, height );
424                 // TODO perhaps check a few more stock icons to get a range on sizes.
425                 if ( newSize > 0 ) {
426                     vals[val_ix] = newSize;
427                 }
428                 if (dump) {
429                     g_message("      %u  size:%d   (%d, %d)", i, gtkSizes[i], width, height);
430                 }
432                 g_object_unref(G_OBJECT(pb));
433             }
434         }
435         //g_object_unref(icon);
436         init = true;
437     }
439     return vals[size];
442 static void sp_icon_paint(SPIcon *icon, GdkRectangle const *area)
444     GtkWidget &widget = *GTK_WIDGET(icon);
446     GdkPixbuf *image = GTK_WIDGET_IS_SENSITIVE(&widget) ? icon->pb : icon->pb_faded;
448     if (image) {
449         int const padx = ( ( widget.allocation.width > icon->psize )
450                            ? ( widget.allocation.width - icon->psize ) / 2
451                            : 0 );
452         int const pady = ( ( widget.allocation.height > icon->psize )
453                            ? ( widget.allocation.height - icon->psize ) / 2
454                            : 0 );
456         int const x0 = std::max(area->x, widget.allocation.x + padx);
457         int const y0 = std::max(area->y, widget.allocation.y + pady);
458         int const x1 = std::min(area->x + area->width,  widget.allocation.x + padx + static_cast<int>(icon->psize) );
459         int const y1 = std::min(area->y + area->height, widget.allocation.y + pady + static_cast<int>(icon->psize) );
461         gdk_draw_pixbuf(GDK_DRAWABLE(widget.window), NULL, image,
462                         x0 - widget.allocation.x - padx,
463                         y0 - widget.allocation.y - pady,
464                         x0, y0,
465                         x1 - x0, y1 - y0,
466                         GDK_RGB_DITHER_NORMAL, x0, y0);
467     }
470 static guchar *
471 sp_icon_image_load_pixmap(gchar const *name, unsigned lsize, unsigned psize)
473     gchar *path;
474     guchar *px;
475     GdkPixbuf *pb;
477     path = (gchar *) g_strdup_printf("%s/%s.png", INKSCAPE_PIXMAPDIR, name);
478     // TODO: bulia, please look over
479     gsize bytesRead = 0;
480     gsize bytesWritten = 0;
481     GError *error = NULL;
482     gchar *localFilename = g_filename_from_utf8( path,
483                                                  -1,
484                                                  &bytesRead,
485                                                  &bytesWritten,
486                                                  &error);
487     pb = gdk_pixbuf_new_from_file(localFilename, NULL);
488     g_free(localFilename);
489     g_free(path);
490     if (!pb) {
491         path = (gchar *) g_strdup_printf("%s/%s.xpm", INKSCAPE_PIXMAPDIR, name);
492         // TODO: bulia, please look over
493         gsize bytesRead = 0;
494         gsize bytesWritten = 0;
495         GError *error = NULL;
496         gchar *localFilename = g_filename_from_utf8( path,
497                                                      -1,
498                                                      &bytesRead,
499                                                      &bytesWritten,
500                                                      &error);
501         pb = gdk_pixbuf_new_from_file(localFilename, NULL);
502         g_free(localFilename);
503         g_free(path);
504     }
505     if (pb) {
506         if (!gdk_pixbuf_get_has_alpha(pb))
507             gdk_pixbuf_add_alpha(pb, FALSE, 0, 0, 0);
508         if ( ( static_cast<unsigned>(gdk_pixbuf_get_width(pb)) != psize )
509              || ( static_cast<unsigned>(gdk_pixbuf_get_height(pb)) != psize ) ) {
510             GdkPixbuf *spb = gdk_pixbuf_scale_simple(pb, psize, psize, GDK_INTERP_HYPER);
511             g_object_unref(G_OBJECT(pb));
512             pb = spb;
513         }
514         guchar *spx = gdk_pixbuf_get_pixels(pb);
515         int srs = gdk_pixbuf_get_rowstride(pb);
516         px = nr_new(guchar, 4 * psize * psize);
517         for (unsigned y = 0; y < psize; y++) {
518             memcpy(px + 4 * y * psize, spx + y * srs, 4 * psize);
519         }
520         g_object_unref(G_OBJECT(pb));
522         return px;
523     }
525     return NULL;
528 // takes doc, root, icon, and icon name to produce pixels
529 extern "C" guchar *
530 sp_icon_doc_icon( SPDocument *doc, NRArenaItem *root,
531                   gchar const *name, unsigned psize )
533     gint const dump = prefs_get_int_attribute_limited( "debug.icons", "dumpSvg", 0, 0, 1 );
534     guchar *px = NULL;
536     if (doc) {
537         SPObject *object = doc->getObjectById(name);
538         if (object && SP_IS_ITEM(object)) {
539             /* Find bbox in document */
540             NR::Matrix const i2doc(sp_item_i2doc_affine(SP_ITEM(object)));
541             NR::Rect dbox = SP_ITEM(object)->invokeBbox(i2doc);
543             if ( SP_OBJECT_PARENT(object) == NULL )
544             {
545                 dbox = NR::Rect(NR::Point(0, 0),
546                                 NR::Point(sp_document_width(doc), sp_document_height(doc)));
547             }
549             /* This is in document coordinates, i.e. pixels */
550             if (dbox.isEmpty() == false) {
551                 NRGC gc(NULL);
552                 /* Update to renderable state */
553                 double sf = 1.0;
554                 NRMatrix t;
555                 nr_matrix_set_scale(&t, sf, sf);
556                 nr_arena_item_set_transform(root, &t);
557                 nr_matrix_set_identity(&gc.transform);
558                 nr_arena_item_invoke_update( root, NULL, &gc,
559                                              NR_ARENA_ITEM_STATE_ALL,
560                                              NR_ARENA_ITEM_STATE_NONE );
561                 /* Item integer bbox in points */
562                 NRRectL ibox;
563                 ibox.x0 = (int) floor(sf * dbox.min()[NR::X] + 0.5);
564                 ibox.y0 = (int) floor(sf * dbox.min()[NR::Y] + 0.5);
565                 ibox.x1 = (int) floor(sf * dbox.max()[NR::X] + 0.5);
566                 ibox.y1 = (int) floor(sf * dbox.max()[NR::Y] + 0.5);
568                 if ( dump ) {
569                     g_message( "   box    --'%s'  (%f,%f)-(%f,%f)", name, (double)ibox.x0, (double)ibox.y0, (double)ibox.x1, (double)ibox.y1 );
570                 }
572                 /* Find button visible area */
573                 int width = ibox.x1 - ibox.x0;
574                 int height = ibox.y1 - ibox.y0;
576                 if ( dump ) {
577                     g_message( "   vis    --'%s'  (%d,%d)", name, width, height );
578                 }
580                 {
581                     int block = std::max(width, height);
582                     if (block != static_cast<int>(psize) ) {
583                         if ( dump ) {
584                             g_message("      resizing" );
585                         }
586                         sf = (double)psize / (double)block;
588                         nr_matrix_set_scale(&t, sf, sf);
589                         nr_arena_item_set_transform(root, &t);
590                         nr_matrix_set_identity(&gc.transform);
591                         nr_arena_item_invoke_update( root, NULL, &gc,
592                                                      NR_ARENA_ITEM_STATE_ALL,
593                                                      NR_ARENA_ITEM_STATE_NONE );
594                         /* Item integer bbox in points */
595                         ibox.x0 = (int) floor(sf * dbox.min()[NR::X] + 0.5);
596                         ibox.y0 = (int) floor(sf * dbox.min()[NR::Y] + 0.5);
597                         ibox.x1 = (int) floor(sf * dbox.max()[NR::X] + 0.5);
598                         ibox.y1 = (int) floor(sf * dbox.max()[NR::Y] + 0.5);
600                         if ( dump ) {
601                             g_message( "   box2   --'%s'  (%f,%f)-(%f,%f)", name, (double)ibox.x0, (double)ibox.y0, (double)ibox.x1, (double)ibox.y1 );
602                         }
604                         /* Find button visible area */
605                         width = ibox.x1 - ibox.x0;
606                         height = ibox.y1 - ibox.y0;
607                         if ( dump ) {
608                             g_message( "   vis2   --'%s'  (%d,%d)", name, width, height );
609                         }
610                     }
611                 }
613                 int dx, dy;
614                 //dx = (psize - width) / 2;
615                 //dy = (psize - height) / 2;
616                 dx=dy=psize;
617                 dx=(dx-width)/2; // watch out for psize, since 'unsigned'-'signed' can cause problems if the result is negative
618                 dy=(dy-height)/2;
619                 NRRectL area;
620                 area.x0 = ibox.x0 - dx;
621                 area.y0 = ibox.y0 - dy;
622                 area.x1 = area.x0 + psize;
623                 area.y1 = area.y0 + psize;
624                 /* Actual renderable area */
625                 NRRectL ua;
626                 ua.x0 = MAX(ibox.x0, area.x0);
627                 ua.y0 = MAX(ibox.y0, area.y0);
628                 ua.x1 = MIN(ibox.x1, area.x1);
629                 ua.y1 = MIN(ibox.y1, area.y1);
631                 if ( dump ) {
632                     g_message( "   area   --'%s'  (%f,%f)-(%f,%f)", name, (double)area.x0, (double)area.y0, (double)area.x1, (double)area.y1 );
633                     g_message( "   ua     --'%s'  (%f,%f)-(%f,%f)", name, (double)ua.x0, (double)ua.y0, (double)ua.x1, (double)ua.y1 );
634                 }
635                 /* Set up pixblock */
636                 px = nr_new(guchar, 4 * psize * psize);
637                 memset(px, 0x00, 4 * psize * psize);
638                 /* Render */
639                 NRPixBlock B;
640                 nr_pixblock_setup_extern( &B, NR_PIXBLOCK_MODE_R8G8B8A8N,
641                                           ua.x0, ua.y0, ua.x1, ua.y1,
642                                           px + 4 * psize * (ua.y0 - area.y0) +
643                                           4 * (ua.x0 - area.x0),
644                                           4 * psize, FALSE, FALSE );
645                 nr_arena_item_invoke_render( root, &ua, &B,
646                                              NR_ARENA_ITEM_RENDER_NO_CACHE );
647                 nr_pixblock_release(&B);
649                 gint useOverlay = prefs_get_int_attribute_limited( "debug.icons", "overlaySvg", 0, 0, 1 );
650                 if ( useOverlay ) {
651                     sp_icon_overlay_pixels( px, psize, psize, 4 * psize, 0x00, 0x00, 0xff );
652                 }
653             }
654         }
655     }
657     return px;
658 } // end of sp_icon_doc_icon()
662 struct svg_doc_cache_t
664     SPDocument *doc;
665     NRArenaItem *root;
666 };
668 static std::map<Glib::ustring, svg_doc_cache_t *> doc_cache;
669 static std::map<Glib::ustring, guchar *> px_cache;
671 static Glib::ustring icon_cache_key(gchar const *name,
672                                     unsigned lsize, unsigned psize)
674     Glib::ustring key=name;
675     key += ":";
676     key += lsize;
677     key += ":";
678     key += psize;
679     return key;
682 static guchar *get_cached_pixels(Glib::ustring const &key) {
683     std::map<Glib::ustring, guchar *>::iterator found=px_cache.find(key);
684     if ( found != px_cache.end() ) {
685         return found->second;
686     }
687     return NULL;
690 static guchar *load_svg_pixels(gchar const *name,
691                                unsigned lsize, unsigned psize)
693     SPDocument *doc = NULL;
694     NRArenaItem *root = NULL;
695     svg_doc_cache_t *info = NULL;
697     // Fall back from user prefs dir into system locations.
698     Glib::ustring iconsvg = name;
699     iconsvg += ".svg";
700     std::list<gchar *> sources;
701     sources.push_back(g_build_filename(profile_path("icons"),iconsvg.c_str(), NULL));
702     sources.push_back(g_build_filename(profile_path("icons"),"icons.svg", NULL));
703     sources.push_back(g_build_filename(INKSCAPE_PIXMAPDIR, iconsvg.c_str(), NULL));
704     sources.push_back(g_build_filename(INKSCAPE_PIXMAPDIR, "icons.svg", NULL));
706     // Try each document in turn until we successfully load the icon from one
707     guchar *px=NULL;
708     while ( !sources.empty() && !px ) {
709         gchar *doc_filename = sources.front();
711         // Did we already load this doc?
712         Glib::ustring key(doc_filename);
713         info = 0;
714         {
715             std::map<Glib::ustring, svg_doc_cache_t *>::iterator i = doc_cache.find(key);
716             if ( i != doc_cache.end() ) {
717                 info = i->second;
718             }
719         }
721         /* Try to load from document. */
722         if (!info &&
723             Inkscape::IO::file_test( doc_filename, G_FILE_TEST_IS_REGULAR ) &&
724             (doc = sp_document_new( doc_filename, FALSE )) ) {
726             // prep the document
727             sp_document_ensure_up_to_date(doc);
728             /* Create new arena */
729             NRArena *arena = NRArena::create();
730             /* Create ArenaItem and set transform */
731             unsigned visionkey = sp_item_display_key_new(1);
732             /* fixme: Memory manage root if needed (Lauris) */
733             root = sp_item_invoke_show( SP_ITEM(SP_DOCUMENT_ROOT(doc)),
734                                         arena, visionkey, SP_ITEM_SHOW_DISPLAY );
736             // store into the cache
737             info = new svg_doc_cache_t;
738             g_assert(info);
740             info->doc=doc;
741             info->root=root;
742             doc_cache[key]=info;
743         }
744         if (info) {
745             doc=info->doc;
746             root=info->root;
747         }
749         // toss the filename
750         g_free(doc_filename);
751         sources.pop_front();
753         // move on to the next document if we couldn't get anything
754         if (!info && !doc) continue;
756         px = sp_icon_doc_icon( doc, root, name, psize );
757     }
759     return px;
762 // returns true if icon needed preloading, false if nothing was done
763 static bool prerender_icon(gchar const *name, unsigned lsize, unsigned psize)
765     Glib::ustring key=icon_cache_key(name, lsize, psize);
766     guchar *px=get_cached_pixels(key);
767     if (px) {
768         return false;
769     } else {
770         px = load_svg_pixels(name, lsize, psize);
771         if (px) {
772             px_cache[key] = px;
773         }
774         return true;
775     }
778 static guchar *
779 sp_icon_image_load_svg(gchar const *name, unsigned lsize, unsigned psize)
781     Glib::ustring key=icon_cache_key(name, lsize, psize);
783     // did we already load this icon at this scale/size?
784     guchar *px=get_cached_pixels(key);
785     if (!px) {
786         px = load_svg_pixels(name, lsize, psize);
787         if (px) {
788             px_cache[key] = px;
789         }
790     }
791     return px;
794 void sp_icon_overlay_pixels(guchar *px, int width, int height, int stride,
795                             unsigned r, unsigned g, unsigned b)
797     for ( int y = 0; y < height; y += 4 ) {
798         guchar *ptr = px + y * stride;
799         for ( int x = 0; x < width; x += 4 ) {
800             *(ptr++) = r;
801             *(ptr++) = g;
802             *(ptr++) = b;
803             *(ptr++) = 0xff;
805             ptr += 4 * 3;
806         }
807     }
809     if ( width > 1 && height > 1 ) {
810         // point at the last pixel
811         guchar *ptr = px + ((height-1) * stride) + ((width - 1) * 4);
813         if ( width > 2 ) {
814             px[4] = r;
815             px[5] = g;
816             px[6] = b;
817             px[7] = 0xff;
819             ptr[-12] = r;
820             ptr[-11] = g;
821             ptr[-10] = b;
822             ptr[-9] = 0xff;
823         }
825         ptr[-4] = r;
826         ptr[-3] = g;
827         ptr[-2] = b;
828         ptr[-1] = 0xff;
830         px[0 + stride] = r;
831         px[1 + stride] = g;
832         px[2 + stride] = b;
833         px[3 + stride] = 0xff;
835         ptr[0 - stride] = r;
836         ptr[1 - stride] = g;
837         ptr[2 - stride] = b;
838         ptr[3 - stride] = 0xff;
840         if ( height > 2 ) {
841             ptr[0 - stride * 3] = r;
842             ptr[1 - stride * 3] = g;
843             ptr[2 - stride * 3] = b;
844             ptr[3 - stride * 3] = 0xff;
845         }
846     }
849 class preRenderItem
851 public:
852     preRenderItem( GtkIconSize lsize, gchar const *name ) :
853         _lsize( lsize ),
854         _name( name )
855     {}
856     GtkIconSize _lsize;
857     Glib::ustring _name;
858 };
861 #include <queue>
863 static std::queue<preRenderItem> pendingRenders;
864 static bool callbackHooked = false;
866 static void addPreRender( GtkIconSize lsize, gchar const *name )
869     if ( !callbackHooked )
870     {
871         callbackHooked = true;
872         g_idle_add_full( G_PRIORITY_LOW, &icon_prerender_task, NULL, NULL );
873     }
875     pendingRenders.push(preRenderItem(lsize, name));
878 gboolean icon_prerender_task(gpointer data) {
879     if (!pendingRenders.empty()) {
880         preRenderItem single=pendingRenders.front();
881         pendingRenders.pop();
882         int psize = sp_icon_get_phys_size(single._lsize);
883         prerender_icon(single._name.c_str(), single._lsize, psize);
884     }
886     if (!pendingRenders.empty()) {
887         return TRUE;
888     } else {
889         callbackHooked = false;
890         return FALSE;
891     }
894 /*
895   Local Variables:
896   mode:c++
897   c-file-style:"stroustrup"
898   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
899   indent-tabs-mode:nil
900   fill-column:99
901   End:
902 */
903 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :