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