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 <glib/gmem.h>
22 #include <gtk/gtkiconfactory.h>
23 #include <gtk/gtkstock.h>
24 #include <gtk/gtkimage.h>
26 #include <gtkmm/image.h>
28 #include "prefs-utils.h"
29 #include "inkscape.h"
30 #include "document.h"
31 #include "sp-item.h"
32 #include "display/nr-arena.h"
33 #include "display/nr-arena-item.h"
34 #include "io/sys.h"
36 #include "icon.h"
38 static gboolean icon_prerender_task(gpointer data);
40 static void addPreRender( Inkscape::IconSize lsize, gchar const *name );
42 static void sp_icon_class_init(SPIconClass *klass);
43 static void sp_icon_init(SPIcon *icon);
44 static void sp_icon_destroy(GtkObject *object);
46 static void sp_icon_reset(SPIcon *icon);
47 static void sp_icon_clear(SPIcon *icon);
49 static void sp_icon_size_request(GtkWidget *widget, GtkRequisition *requisition);
50 static void sp_icon_size_allocate(GtkWidget *widget, GtkAllocation *allocation);
51 static int sp_icon_expose(GtkWidget *widget, GdkEventExpose *event);
53 static void sp_icon_paint(SPIcon *icon, GdkRectangle const *area);
55 static void sp_icon_screen_changed( GtkWidget *widget, GdkScreen *previous_screen );
56 static void sp_icon_style_set( GtkWidget *widget, GtkStyle *previous_style );
57 static void sp_icon_theme_changed( SPIcon *icon );
59 static guchar *sp_icon_image_load_pixmap(gchar const *name, unsigned lsize, unsigned psize);
60 static guchar *sp_icon_image_load_svg(gchar const *name, unsigned lsize, unsigned psize);
62 static guchar *sp_icon_image_load(SPIcon *icon, gchar const *name);
64 static int sp_icon_get_phys_size(int size);
66 static void sp_icon_overlay_pixels( guchar *px, int width, int height, int stride,
67 unsigned r, unsigned g, unsigned b );
69 static void injectCustomSize();
71 static GtkWidgetClass *parent_class;
73 static bool sizeDirty = true;
75 static bool sizeMapDone = false;
76 static GtkIconSize iconSizeLookup[] = {
77 GTK_ICON_SIZE_INVALID,
78 GTK_ICON_SIZE_MENU,
79 GTK_ICON_SIZE_SMALL_TOOLBAR,
80 GTK_ICON_SIZE_LARGE_TOOLBAR,
81 GTK_ICON_SIZE_BUTTON,
82 GTK_ICON_SIZE_DND,
83 GTK_ICON_SIZE_DIALOG,
84 GTK_ICON_SIZE_MENU, // for Inkscape::ICON_SIZE_DECORATION
85 };
87 GtkType
88 sp_icon_get_type()
89 {
90 static GtkType type = 0;
91 if (!type) {
92 GtkTypeInfo info = {
93 "SPIcon",
94 sizeof(SPIcon),
95 sizeof(SPIconClass),
96 (GtkClassInitFunc) sp_icon_class_init,
97 (GtkObjectInitFunc) sp_icon_init,
98 NULL, NULL, NULL
99 };
100 type = gtk_type_unique(GTK_TYPE_WIDGET, &info);
101 }
102 return type;
103 }
105 static void
106 sp_icon_class_init(SPIconClass *klass)
107 {
108 GtkObjectClass *object_class;
109 GtkWidgetClass *widget_class;
111 object_class = (GtkObjectClass *) klass;
112 widget_class = (GtkWidgetClass *) klass;
114 parent_class = (GtkWidgetClass*)g_type_class_peek_parent(klass);
116 object_class->destroy = sp_icon_destroy;
118 widget_class->size_request = sp_icon_size_request;
119 widget_class->size_allocate = sp_icon_size_allocate;
120 widget_class->expose_event = sp_icon_expose;
121 widget_class->screen_changed = sp_icon_screen_changed;
122 widget_class->style_set = sp_icon_style_set;
123 }
126 static void
127 sp_icon_init(SPIcon *icon)
128 {
129 GTK_WIDGET_FLAGS(icon) |= GTK_NO_WINDOW;
130 icon->lsize = Inkscape::ICON_SIZE_BUTTON;
131 icon->psize = 0;
132 icon->name = 0;
133 icon->pb = 0;
134 icon->pb_faded = 0;
135 }
137 static void
138 sp_icon_destroy(GtkObject *object)
139 {
140 SPIcon *icon = SP_ICON(object);
141 sp_icon_clear(icon);
142 if ( icon->name ) {
143 g_free( icon->name );
144 icon->name = 0;
145 }
147 ((GtkObjectClass *) (parent_class))->destroy(object);
148 }
150 static void sp_icon_reset( SPIcon *icon ) {
151 icon->psize = 0;
152 sp_icon_clear(icon);
153 }
155 static void sp_icon_clear( SPIcon *icon ) {
156 if (icon->pb) {
157 g_object_unref(G_OBJECT(icon->pb));
158 icon->pb = NULL;
159 }
160 if (icon->pb_faded) {
161 g_object_unref(G_OBJECT(icon->pb_faded));
162 icon->pb_faded = NULL;
163 }
164 }
166 static void
167 sp_icon_size_request(GtkWidget *widget, GtkRequisition *requisition)
168 {
169 SPIcon const *icon = SP_ICON(widget);
171 int const size = ( icon->psize
172 ? icon->psize
173 : sp_icon_get_phys_size(icon->lsize) );
174 requisition->width = size;
175 requisition->height = size;
176 }
178 static void
179 sp_icon_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
180 {
181 widget->allocation = *allocation;
183 if (GTK_WIDGET_DRAWABLE(widget)) {
184 gtk_widget_queue_draw(widget);
185 }
186 }
188 static int sp_icon_expose(GtkWidget *widget, GdkEventExpose *event)
189 {
190 if ( GTK_WIDGET_DRAWABLE(widget) ) {
191 SPIcon *icon = SP_ICON(widget);
192 if ( !icon->pb ) {
193 sp_icon_fetch_pixbuf( icon );
194 }
196 sp_icon_paint(SP_ICON(widget), &event->area);
197 }
199 return TRUE;
200 }
202 void sp_icon_fetch_pixbuf( SPIcon *icon )
203 {
204 if ( icon ) {
205 if ( !icon->pb ) {
206 guchar *pixels = 0;
208 icon->psize = sp_icon_get_phys_size(icon->lsize);
210 pixels = sp_icon_image_load( icon, icon->name );
212 if (pixels) {
213 // don't pass the g_free because we're caching the pixel
214 // space loaded through ...
215 // I just changed this. make sure sp_icon_image_load still does the right thing.
216 icon->pb = gdk_pixbuf_new_from_data(pixels, GDK_COLORSPACE_RGB, TRUE, 8,
217 icon->psize, icon->psize, icon->psize * 4,
218 /*(GdkPixbufDestroyNotify)g_free*/NULL, NULL);
219 icon->pb_faded = gdk_pixbuf_copy(icon->pb);
221 pixels = gdk_pixbuf_get_pixels(icon->pb_faded);
222 size_t stride = gdk_pixbuf_get_rowstride(icon->pb_faded);
223 pixels += 3; // alpha
224 for ( int row = 0 ; row < icon->psize ; row++ ) {
225 guchar *row_pixels = pixels;
226 for ( int column = 0 ; column < icon->psize ; column++ ) {
227 *row_pixels = *row_pixels >> 1;
228 row_pixels += 4;
229 }
230 pixels += stride;
231 }
232 } else {
233 /* TODO: We should do something more useful if we can't load the image. */
234 g_warning ("failed to load icon '%s'", icon->name);
235 }
236 }
237 }
238 }
240 static void sp_icon_screen_changed( GtkWidget *widget, GdkScreen *previous_screen )
241 {
242 if ( GTK_WIDGET_CLASS( parent_class )->screen_changed ) {
243 GTK_WIDGET_CLASS( parent_class )->screen_changed( widget, previous_screen );
244 }
245 SPIcon *icon = SP_ICON(widget);
246 sp_icon_theme_changed(icon);
247 }
249 static void sp_icon_style_set( GtkWidget *widget, GtkStyle *previous_style )
250 {
251 if ( GTK_WIDGET_CLASS( parent_class )->style_set ) {
252 GTK_WIDGET_CLASS( parent_class )->style_set( widget, previous_style );
253 }
254 SPIcon *icon = SP_ICON(widget);
255 sp_icon_theme_changed(icon);
256 }
258 static void sp_icon_theme_changed( SPIcon *icon )
259 {
260 //g_message("Got a change bump for this icon");
261 sizeDirty = true;
262 sp_icon_reset(icon);
263 gtk_widget_queue_draw( GTK_WIDGET(icon) );
264 }
267 static GtkWidget *
268 sp_icon_new_full( Inkscape::IconSize lsize, gchar const *name )
269 {
270 static gint dump = prefs_get_int_attribute_limited( "debug.icons", "dumpGtk", 0, 0, 1 );
271 static gint fallback = prefs_get_int_attribute_limited( "debug.icons", "checkNames", 0, 0, 1 );
273 addPreRender( lsize, name );
275 GtkStockItem stock;
276 gboolean tryLoad = gtk_stock_lookup( name, &stock );
277 if ( !tryLoad && fallback ) {
278 tryLoad |= strncmp("gtk-", name, 4 ) == 0;
279 }
280 if ( !tryLoad && fallback ) {
281 tryLoad |= strncmp("gnome-", name, 6 ) == 0;
282 }
284 GtkWidget *widget = 0;
285 if ( tryLoad ) {
286 gint trySize = CLAMP( static_cast<gint>(lsize), 0, static_cast<gint>(G_N_ELEMENTS(iconSizeLookup) - 1) );
288 if ( !sizeMapDone ) {
289 injectCustomSize();
290 }
292 GtkWidget *img = gtk_image_new_from_stock( name, iconSizeLookup[trySize] );
293 if ( img ) {
294 GtkImageType type = gtk_image_get_storage_type( GTK_IMAGE(img) );
295 if ( type == GTK_IMAGE_STOCK ) {
296 widget = GTK_WIDGET(img);
297 img = 0;
299 if ( dump ) {
300 g_message( "loaded gtk '%s' %d (GTK_IMAGE_STOCK)", name, lsize );
301 }
302 } else {
303 if ( dump ) {
304 g_message( "skipped gtk '%s' %d (not GTK_IMAGE_STOCK)", name, lsize );
305 }
306 g_object_unref( (GObject *)img );
307 img = 0;
308 }
309 }
310 }
312 if ( !widget ) {
313 SPIcon *icon = (SPIcon *)g_object_new(SP_TYPE_ICON, NULL);
314 icon->lsize = lsize;
315 icon->name = g_strdup(name);
316 icon->psize = sp_icon_get_phys_size(lsize);
318 widget = GTK_WIDGET(icon);
319 }
321 return widget;
322 }
324 GtkWidget *
325 sp_icon_new( Inkscape::IconSize lsize, gchar const *name )
326 {
327 return sp_icon_new_full( lsize, name );
328 }
330 Gtk::Widget *sp_icon_get_icon( Glib::ustring const &oid, Inkscape::IconSize size )
331 {
332 Gtk::Widget *result = 0;
333 GtkWidget *widget = sp_icon_new_full( size, oid.c_str() );
335 if ( widget ) {
336 if ( GTK_IS_IMAGE(widget) ) {
337 GtkImage *img = GTK_IMAGE(widget);
338 result = Glib::wrap( img );
339 } else {
340 result = Glib::wrap( widget );
341 }
342 }
344 return result;
345 }
347 // Try to load the named svg, falling back to pixmaps
348 guchar *
349 sp_icon_image_load( SPIcon *icon, gchar const *name )
350 {
351 guchar *px = sp_icon_image_load_svg( name, icon->lsize, icon->psize );
352 if (!px) {
353 px = sp_icon_image_load_pixmap(name, icon->lsize, icon->psize);
354 }
356 return px;
357 }
359 GtkIconSize
360 sp_icon_get_gtk_size(int size)
361 {
362 static GtkIconSize map[64] = {(GtkIconSize)0};
363 size = CLAMP(size, 4, 63);
364 if (!map[size]) {
365 static int count = 0;
366 char c[64];
367 g_snprintf(c, 64, "InkscapeIcon%d", count++);
368 map[size] = gtk_icon_size_register(c, size, size);
369 }
370 return map[size];
371 }
373 static void injectCustomSize()
374 {
375 // TODO - still need to handle the case of theme changes and resize, especially as we can't re-register a string.
376 if ( !sizeMapDone )
377 {
378 gint dump = prefs_get_int_attribute_limited( "debug.icons", "dumpDefault", 0, 0, 1 );
379 gint width = 0;
380 gint height = 0;
381 if ( gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &width, &height ) ) {
382 gint newWidth = ((width * 3) / 4);
383 gint newHeight = ((height * 3) / 4);
384 GtkIconSize newSizeEnum = gtk_icon_size_register( "inkscape-decoration", newWidth, newHeight );
385 if ( newSizeEnum ) {
386 if ( dump ) {
387 g_message("Registered (%d, %d) <= (%d, %d) as index %d", newWidth, newHeight, width, height, newSizeEnum);
388 }
389 guint index = static_cast<guint>(Inkscape::ICON_SIZE_DECORATION);
390 if ( index < G_N_ELEMENTS(iconSizeLookup) ) {
391 iconSizeLookup[index] = newSizeEnum;
392 } else if ( dump ) {
393 g_message("size lookup array too small to store entry");
394 }
395 }
396 }
397 sizeMapDone = true;
398 }
400 }
402 static int sp_icon_get_phys_size(int size)
403 {
404 static bool init = false;
405 static int lastSys[Inkscape::ICON_SIZE_DECORATION + 1];
406 static int vals[Inkscape::ICON_SIZE_DECORATION + 1];
408 size = CLAMP( size, GTK_ICON_SIZE_MENU, Inkscape::ICON_SIZE_DECORATION );
410 if ( !sizeMapDone ) {
411 injectCustomSize();
412 }
414 if ( sizeDirty && init ) {
415 GtkIconSize const gtkSizes[] = {
416 GTK_ICON_SIZE_MENU,
417 GTK_ICON_SIZE_SMALL_TOOLBAR,
418 GTK_ICON_SIZE_LARGE_TOOLBAR,
419 GTK_ICON_SIZE_BUTTON,
420 GTK_ICON_SIZE_DND,
421 GTK_ICON_SIZE_DIALOG,
422 static_cast<guint>(Inkscape::ICON_SIZE_DECORATION) < G_N_ELEMENTS(iconSizeLookup) ?
423 iconSizeLookup[static_cast<int>(Inkscape::ICON_SIZE_DECORATION)] :
424 GTK_ICON_SIZE_MENU
425 };
426 for (unsigned i = 0; i < G_N_ELEMENTS(gtkSizes) && init; ++i) {
427 guint const val_ix = (gtkSizes[i] <= GTK_ICON_SIZE_DIALOG) ? (guint)gtkSizes[i] : (guint)Inkscape::ICON_SIZE_DECORATION;
429 g_assert( val_ix < G_N_ELEMENTS(vals) );
431 gint width = 0;
432 gint height = 0;
433 if ( gtk_icon_size_lookup(gtkSizes[i], &width, &height ) ) {
434 init &= (lastSys[val_ix] == std::max(width, height));
435 }
436 }
437 }
439 if ( !init ) {
440 sizeDirty = false;
441 gint dump = prefs_get_int_attribute_limited( "debug.icons", "dumpDefault", 0, 0, 1 );
443 if ( dump ) {
444 g_message( "Default icon sizes:" );
445 }
446 memset( vals, 0, sizeof(vals) );
447 memset( lastSys, 0, sizeof(lastSys) );
448 GtkIconSize const gtkSizes[] = {
449 GTK_ICON_SIZE_MENU,
450 GTK_ICON_SIZE_SMALL_TOOLBAR,
451 GTK_ICON_SIZE_LARGE_TOOLBAR,
452 GTK_ICON_SIZE_BUTTON,
453 GTK_ICON_SIZE_DND,
454 GTK_ICON_SIZE_DIALOG,
455 static_cast<guint>(Inkscape::ICON_SIZE_DECORATION) < G_N_ELEMENTS(iconSizeLookup) ?
456 iconSizeLookup[static_cast<int>(Inkscape::ICON_SIZE_DECORATION)] :
457 GTK_ICON_SIZE_MENU
458 };
459 gchar const *const names[] = {
460 "GTK_ICON_SIZE_MENU",
461 "GTK_ICON_SIZE_SMALL_TOOLBAR",
462 "GTK_ICON_SIZE_LARGE_TOOLBAR",
463 "GTK_ICON_SIZE_BUTTON",
464 "GTK_ICON_SIZE_DND",
465 "GTK_ICON_SIZE_DIALOG",
466 "inkscape-decoration"
467 };
469 GtkWidget *icon = (GtkWidget *)g_object_new(SP_TYPE_ICON, NULL);
471 for (unsigned i = 0; i < G_N_ELEMENTS(gtkSizes); ++i) {
472 guint const val_ix = (gtkSizes[i] <= GTK_ICON_SIZE_DIALOG) ? (guint)gtkSizes[i] : (guint)Inkscape::ICON_SIZE_DECORATION;
474 g_assert( val_ix < G_N_ELEMENTS(vals) );
476 gint width = 0;
477 gint height = 0;
478 bool used = false;
479 if ( gtk_icon_size_lookup(gtkSizes[i], &width, &height ) ) {
480 vals[val_ix] = std::max(width, height);
481 lastSys[val_ix] = vals[val_ix];
482 used = true;
483 }
484 if (dump) {
485 g_message(" =-- %u size:%d %c(%d, %d) '%s'",
486 i, gtkSizes[i],
487 ( used ? ' ' : 'X' ), width, height, names[i]);
488 }
489 gchar const *id = GTK_STOCK_OPEN;
490 GdkPixbuf *pb = gtk_widget_render_icon( icon, id, gtkSizes[i], NULL);
491 if (pb) {
492 width = gdk_pixbuf_get_width(pb);
493 height = gdk_pixbuf_get_height(pb);
494 int newSize = std::max( width, height );
495 // TODO perhaps check a few more stock icons to get a range on sizes.
496 if ( newSize > 0 ) {
497 vals[val_ix] = newSize;
498 }
499 if (dump) {
500 g_message(" %u size:%d (%d, %d)", i, gtkSizes[i], width, height);
501 }
503 g_object_unref(G_OBJECT(pb));
504 }
505 }
506 //g_object_unref(icon);
507 init = true;
508 }
510 return vals[size];
511 }
513 static void sp_icon_paint(SPIcon *icon, GdkRectangle const *area)
514 {
515 GtkWidget &widget = *GTK_WIDGET(icon);
517 GdkPixbuf *image = GTK_WIDGET_IS_SENSITIVE(&widget) ? icon->pb : icon->pb_faded;
519 if (image) {
520 int const padx = ( ( widget.allocation.width > icon->psize )
521 ? ( widget.allocation.width - icon->psize ) / 2
522 : 0 );
523 int const pady = ( ( widget.allocation.height > icon->psize )
524 ? ( widget.allocation.height - icon->psize ) / 2
525 : 0 );
527 int const x0 = std::max(area->x, widget.allocation.x + padx);
528 int const y0 = std::max(area->y, widget.allocation.y + pady);
529 int const x1 = std::min(area->x + area->width, widget.allocation.x + padx + static_cast<int>(icon->psize) );
530 int const y1 = std::min(area->y + area->height, widget.allocation.y + pady + static_cast<int>(icon->psize) );
532 int width = x1 - x0;
533 int height = y1 - y0;
534 // Limit drawing to when we actually have something. Avoids some crashes.
535 if ( (width > 0) && (height > 0) ) {
536 gdk_draw_pixbuf(GDK_DRAWABLE(widget.window), NULL, image,
537 x0 - widget.allocation.x - padx,
538 y0 - widget.allocation.y - pady,
539 x0, y0,
540 width, height,
541 GDK_RGB_DITHER_NORMAL, x0, y0);
542 }
543 }
544 }
546 static guchar *
547 sp_icon_image_load_pixmap(gchar const *name, unsigned lsize, unsigned psize)
548 {
549 gchar *path;
550 guchar *px;
551 GdkPixbuf *pb;
553 path = (gchar *) g_strdup_printf("%s/%s.png", INKSCAPE_PIXMAPDIR, name);
554 // TODO: bulia, please look over
555 gsize bytesRead = 0;
556 gsize bytesWritten = 0;
557 GError *error = NULL;
558 gchar *localFilename = g_filename_from_utf8( path,
559 -1,
560 &bytesRead,
561 &bytesWritten,
562 &error);
563 pb = gdk_pixbuf_new_from_file(localFilename, NULL);
564 g_free(localFilename);
565 g_free(path);
566 if (!pb) {
567 path = (gchar *) g_strdup_printf("%s/%s.xpm", INKSCAPE_PIXMAPDIR, name);
568 // TODO: bulia, please look over
569 gsize bytesRead = 0;
570 gsize bytesWritten = 0;
571 GError *error = NULL;
572 gchar *localFilename = g_filename_from_utf8( path,
573 -1,
574 &bytesRead,
575 &bytesWritten,
576 &error);
577 pb = gdk_pixbuf_new_from_file(localFilename, NULL);
578 g_free(localFilename);
579 g_free(path);
580 }
581 if (pb) {
582 if (!gdk_pixbuf_get_has_alpha(pb))
583 gdk_pixbuf_add_alpha(pb, FALSE, 0, 0, 0);
584 if ( ( static_cast<unsigned>(gdk_pixbuf_get_width(pb)) != psize )
585 || ( static_cast<unsigned>(gdk_pixbuf_get_height(pb)) != psize ) ) {
586 GdkPixbuf *spb = gdk_pixbuf_scale_simple(pb, psize, psize, GDK_INTERP_HYPER);
587 g_object_unref(G_OBJECT(pb));
588 pb = spb;
589 }
590 guchar *spx = gdk_pixbuf_get_pixels(pb);
591 int srs = gdk_pixbuf_get_rowstride(pb);
592 px = g_new(guchar, 4 * psize * psize);
593 for (unsigned y = 0; y < psize; y++) {
594 memcpy(px + 4 * y * psize, spx + y * srs, 4 * psize);
595 }
596 g_object_unref(G_OBJECT(pb));
598 return px;
599 }
601 return NULL;
602 }
604 // takes doc, root, icon, and icon name to produce pixels
605 extern "C" guchar *
606 sp_icon_doc_icon( SPDocument *doc, NRArenaItem *root,
607 gchar const *name, unsigned psize )
608 {
609 gint const dump = prefs_get_int_attribute_limited( "debug.icons", "dumpSvg", 0, 0, 1 );
610 guchar *px = NULL;
612 if (doc) {
613 SPObject *object = doc->getObjectById(name);
614 if (object && SP_IS_ITEM(object)) {
615 /* Find bbox in document */
616 NR::Matrix const i2doc(sp_item_i2doc_affine(SP_ITEM(object)));
617 NR::Maybe<NR::Rect> dbox = SP_ITEM(object)->getBounds(i2doc);
619 if ( SP_OBJECT_PARENT(object) == NULL )
620 {
621 dbox = NR::Rect(NR::Point(0, 0),
622 NR::Point(sp_document_width(doc), sp_document_height(doc)));
623 }
625 /* This is in document coordinates, i.e. pixels */
626 if ( dbox && !dbox->isEmpty() ) {
627 NRGC gc(NULL);
628 /* Update to renderable state */
629 double sf = 1.0;
630 NRMatrix t;
631 nr_matrix_set_scale(&t, sf, sf);
632 nr_arena_item_set_transform(root, &t);
633 nr_matrix_set_identity(&gc.transform);
634 nr_arena_item_invoke_update( root, NULL, &gc,
635 NR_ARENA_ITEM_STATE_ALL,
636 NR_ARENA_ITEM_STATE_NONE );
637 /* Item integer bbox in points */
638 NRRectL ibox;
639 ibox.x0 = (int) floor(sf * dbox->min()[NR::X] + 0.5);
640 ibox.y0 = (int) floor(sf * dbox->min()[NR::Y] + 0.5);
641 ibox.x1 = (int) floor(sf * dbox->max()[NR::X] + 0.5);
642 ibox.y1 = (int) floor(sf * dbox->max()[NR::Y] + 0.5);
644 if ( dump ) {
645 g_message( " box --'%s' (%f,%f)-(%f,%f)", name, (double)ibox.x0, (double)ibox.y0, (double)ibox.x1, (double)ibox.y1 );
646 }
648 /* Find button visible area */
649 int width = ibox.x1 - ibox.x0;
650 int height = ibox.y1 - ibox.y0;
652 if ( dump ) {
653 g_message( " vis --'%s' (%d,%d)", name, width, height );
654 }
656 {
657 int block = std::max(width, height);
658 if (block != static_cast<int>(psize) ) {
659 if ( dump ) {
660 g_message(" resizing" );
661 }
662 sf = (double)psize / (double)block;
664 nr_matrix_set_scale(&t, sf, sf);
665 nr_arena_item_set_transform(root, &t);
666 nr_matrix_set_identity(&gc.transform);
667 nr_arena_item_invoke_update( root, NULL, &gc,
668 NR_ARENA_ITEM_STATE_ALL,
669 NR_ARENA_ITEM_STATE_NONE );
670 /* Item integer bbox in points */
671 ibox.x0 = (int) floor(sf * dbox->min()[NR::X] + 0.5);
672 ibox.y0 = (int) floor(sf * dbox->min()[NR::Y] + 0.5);
673 ibox.x1 = (int) floor(sf * dbox->max()[NR::X] + 0.5);
674 ibox.y1 = (int) floor(sf * dbox->max()[NR::Y] + 0.5);
676 if ( dump ) {
677 g_message( " box2 --'%s' (%f,%f)-(%f,%f)", name, (double)ibox.x0, (double)ibox.y0, (double)ibox.x1, (double)ibox.y1 );
678 }
680 /* Find button visible area */
681 width = ibox.x1 - ibox.x0;
682 height = ibox.y1 - ibox.y0;
683 if ( dump ) {
684 g_message( " vis2 --'%s' (%d,%d)", name, width, height );
685 }
686 }
687 }
689 int dx, dy;
690 //dx = (psize - width) / 2;
691 //dy = (psize - height) / 2;
692 dx=dy=psize;
693 dx=(dx-width)/2; // watch out for psize, since 'unsigned'-'signed' can cause problems if the result is negative
694 dy=(dy-height)/2;
695 NRRectL area;
696 area.x0 = ibox.x0 - dx;
697 area.y0 = ibox.y0 - dy;
698 area.x1 = area.x0 + psize;
699 area.y1 = area.y0 + psize;
700 /* Actual renderable area */
701 NRRectL ua;
702 ua.x0 = MAX(ibox.x0, area.x0);
703 ua.y0 = MAX(ibox.y0, area.y0);
704 ua.x1 = MIN(ibox.x1, area.x1);
705 ua.y1 = MIN(ibox.y1, area.y1);
707 if ( dump ) {
708 g_message( " area --'%s' (%f,%f)-(%f,%f)", name, (double)area.x0, (double)area.y0, (double)area.x1, (double)area.y1 );
709 g_message( " ua --'%s' (%f,%f)-(%f,%f)", name, (double)ua.x0, (double)ua.y0, (double)ua.x1, (double)ua.y1 );
710 }
711 /* Set up pixblock */
712 px = g_new(guchar, 4 * psize * psize);
713 memset(px, 0x00, 4 * psize * psize);
714 /* Render */
715 NRPixBlock B;
716 nr_pixblock_setup_extern( &B, NR_PIXBLOCK_MODE_R8G8B8A8N,
717 ua.x0, ua.y0, ua.x1, ua.y1,
718 px + 4 * psize * (ua.y0 - area.y0) +
719 4 * (ua.x0 - area.x0),
720 4 * psize, FALSE, FALSE );
721 nr_arena_item_invoke_render(NULL, root, &ua, &B,
722 NR_ARENA_ITEM_RENDER_NO_CACHE );
723 nr_pixblock_release(&B);
725 gint useOverlay = prefs_get_int_attribute_limited( "debug.icons", "overlaySvg", 0, 0, 1 );
726 if ( useOverlay ) {
727 sp_icon_overlay_pixels( px, psize, psize, 4 * psize, 0x00, 0x00, 0xff );
728 }
729 }
730 }
731 }
733 return px;
734 } // end of sp_icon_doc_icon()
738 struct svg_doc_cache_t
739 {
740 SPDocument *doc;
741 NRArenaItem *root;
742 };
744 static std::map<Glib::ustring, svg_doc_cache_t *> doc_cache;
745 static std::map<Glib::ustring, guchar *> px_cache;
747 static Glib::ustring icon_cache_key(gchar const *name,
748 unsigned lsize, unsigned psize)
749 {
750 Glib::ustring key=name;
751 key += ":";
752 key += lsize;
753 key += ":";
754 key += psize;
755 return key;
756 }
758 static guchar *get_cached_pixels(Glib::ustring const &key) {
759 std::map<Glib::ustring, guchar *>::iterator found=px_cache.find(key);
760 if ( found != px_cache.end() ) {
761 return found->second;
762 }
763 return NULL;
764 }
766 static guchar *load_svg_pixels(gchar const *name,
767 unsigned lsize, unsigned psize)
768 {
769 SPDocument *doc = NULL;
770 NRArenaItem *root = NULL;
771 svg_doc_cache_t *info = NULL;
773 // Fall back from user prefs dir into system locations.
774 Glib::ustring iconsvg = name;
775 iconsvg += ".svg";
776 std::list<gchar *> sources;
777 sources.push_back(g_build_filename(profile_path("icons"),iconsvg.c_str(), NULL));
778 sources.push_back(g_build_filename(profile_path("icons"),"icons.svg", NULL));
779 sources.push_back(g_build_filename(INKSCAPE_PIXMAPDIR, iconsvg.c_str(), NULL));
780 sources.push_back(g_build_filename(INKSCAPE_PIXMAPDIR, "icons.svg", NULL));
782 // Try each document in turn until we successfully load the icon from one
783 guchar *px=NULL;
784 while ( !sources.empty() && !px ) {
785 gchar *doc_filename = sources.front();
787 // Did we already load this doc?
788 Glib::ustring key(doc_filename);
789 info = 0;
790 {
791 std::map<Glib::ustring, svg_doc_cache_t *>::iterator i = doc_cache.find(key);
792 if ( i != doc_cache.end() ) {
793 info = i->second;
794 }
795 }
797 /* Try to load from document. */
798 if (!info &&
799 Inkscape::IO::file_test( doc_filename, G_FILE_TEST_IS_REGULAR ) &&
800 (doc = sp_document_new( doc_filename, FALSE )) ) {
802 // prep the document
803 sp_document_ensure_up_to_date(doc);
804 /* Create new arena */
805 NRArena *arena = NRArena::create();
806 /* Create ArenaItem and set transform */
807 unsigned visionkey = sp_item_display_key_new(1);
808 /* fixme: Memory manage root if needed (Lauris) */
809 root = sp_item_invoke_show( SP_ITEM(SP_DOCUMENT_ROOT(doc)),
810 arena, visionkey, SP_ITEM_SHOW_DISPLAY );
812 // store into the cache
813 info = new svg_doc_cache_t;
814 g_assert(info);
816 info->doc=doc;
817 info->root=root;
818 doc_cache[key]=info;
819 }
820 if (info) {
821 doc=info->doc;
822 root=info->root;
823 }
825 // toss the filename
826 g_free(doc_filename);
827 sources.pop_front();
829 // move on to the next document if we couldn't get anything
830 if (!info && !doc) continue;
832 px = sp_icon_doc_icon( doc, root, name, psize );
833 }
835 return px;
836 }
838 // returns true if icon needed preloading, false if nothing was done
839 static bool prerender_icon(gchar const *name, unsigned lsize, unsigned psize)
840 {
841 Glib::ustring key = icon_cache_key(name, lsize, psize);
842 guchar *px = get_cached_pixels(key);
843 if (px) {
844 return false;
845 } else {
846 px = load_svg_pixels(name, lsize, psize);
847 if (px) {
848 px_cache[key] = px;
849 }
850 return true;
851 }
852 }
854 static guchar *
855 sp_icon_image_load_svg(gchar const *name, unsigned lsize, unsigned psize)
856 {
857 Glib::ustring key = icon_cache_key(name, lsize, psize);
859 // did we already load this icon at this scale/size?
860 guchar *px = get_cached_pixels(key);
861 if (!px) {
862 px = load_svg_pixels(name, lsize, psize);
863 if (px) {
864 px_cache[key] = px;
865 }
866 }
867 return px;
868 }
870 void sp_icon_overlay_pixels(guchar *px, int width, int height, int stride,
871 unsigned r, unsigned g, unsigned b)
872 {
873 for ( int y = 0; y < height; y += 4 ) {
874 guchar *ptr = px + y * stride;
875 for ( int x = 0; x < width; x += 4 ) {
876 *(ptr++) = r;
877 *(ptr++) = g;
878 *(ptr++) = b;
879 *(ptr++) = 0xff;
881 ptr += 4 * 3;
882 }
883 }
885 if ( width > 1 && height > 1 ) {
886 // point at the last pixel
887 guchar *ptr = px + ((height-1) * stride) + ((width - 1) * 4);
889 if ( width > 2 ) {
890 px[4] = r;
891 px[5] = g;
892 px[6] = b;
893 px[7] = 0xff;
895 ptr[-12] = r;
896 ptr[-11] = g;
897 ptr[-10] = b;
898 ptr[-9] = 0xff;
899 }
901 ptr[-4] = r;
902 ptr[-3] = g;
903 ptr[-2] = b;
904 ptr[-1] = 0xff;
906 px[0 + stride] = r;
907 px[1 + stride] = g;
908 px[2 + stride] = b;
909 px[3 + stride] = 0xff;
911 ptr[0 - stride] = r;
912 ptr[1 - stride] = g;
913 ptr[2 - stride] = b;
914 ptr[3 - stride] = 0xff;
916 if ( height > 2 ) {
917 ptr[0 - stride * 3] = r;
918 ptr[1 - stride * 3] = g;
919 ptr[2 - stride * 3] = b;
920 ptr[3 - stride * 3] = 0xff;
921 }
922 }
923 }
925 class preRenderItem
926 {
927 public:
928 preRenderItem( Inkscape::IconSize lsize, gchar const *name ) :
929 _lsize( lsize ),
930 _name( name )
931 {}
932 Inkscape::IconSize _lsize;
933 Glib::ustring _name;
934 };
937 #include <queue>
939 static std::queue<preRenderItem> pendingRenders;
940 static bool callbackHooked = false;
942 static void addPreRender( Inkscape::IconSize lsize, gchar const *name )
943 {
945 if ( !callbackHooked )
946 {
947 callbackHooked = true;
948 g_idle_add_full( G_PRIORITY_LOW, &icon_prerender_task, NULL, NULL );
949 }
951 pendingRenders.push(preRenderItem(lsize, name));
952 }
954 gboolean icon_prerender_task(gpointer data) {
955 if (!pendingRenders.empty()) {
956 preRenderItem single=pendingRenders.front();
957 pendingRenders.pop();
958 int psize = sp_icon_get_phys_size(single._lsize);
959 prerender_icon(single._name.c_str(), single._lsize, psize);
960 }
962 if (!pendingRenders.empty()) {
963 return TRUE;
964 } else {
965 callbackHooked = false;
966 return FALSE;
967 }
968 }
970 /*
971 Local Variables:
972 mode:c++
973 c-file-style:"stroustrup"
974 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
975 indent-tabs-mode:nil
976 fill-column:99
977 End:
978 */
979 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :