deb3e73017356f1c5c41f9cff0e81c45a093a847
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
18 #include <cstring>
19 #include <glib/gmem.h>
20 #include <gtk/gtk.h>
21 #include <gtkmm.h>
23 #include "path-prefix.h"
24 #include "preferences.h"
25 #include "inkscape.h"
26 #include "document.h"
27 #include "sp-item.h"
28 #include "display/nr-arena.h"
29 #include "display/nr-arena-item.h"
30 #include "io/sys.h"
32 #include "icon.h"
34 static void sp_icon_class_init(SPIconClass *klass);
35 static void sp_icon_init(SPIcon *icon);
36 static void sp_icon_dispose(GObject *object);
38 static void sp_icon_reset(SPIcon *icon);
39 static void sp_icon_clear(SPIcon *icon);
41 static void sp_icon_size_request(GtkWidget *widget, GtkRequisition *requisition);
42 static void sp_icon_size_allocate(GtkWidget *widget, GtkAllocation *allocation);
43 static int sp_icon_expose(GtkWidget *widget, GdkEventExpose *event);
45 static void sp_icon_paint(SPIcon *icon, GdkRectangle const *area);
47 static void sp_icon_screen_changed( GtkWidget *widget, GdkScreen *previous_screen );
48 static void sp_icon_style_set( GtkWidget *widget, GtkStyle *previous_style );
49 static void sp_icon_theme_changed( SPIcon *icon );
51 static GdkPixbuf *sp_icon_image_load_pixmap(gchar const *name, unsigned lsize, unsigned psize);
52 static GdkPixbuf *sp_icon_image_load_svg(gchar const *name, GtkIconSize lsize, unsigned psize);
54 static void sp_icon_overlay_pixels( guchar *px, int width, int height, int stride,
55 unsigned r, unsigned g, unsigned b );
57 static void injectCustomSize();
59 static GtkWidgetClass *parent_class;
61 static bool sizeDirty = true;
63 static bool sizeMapDone = false;
64 static GtkIconSize iconSizeLookup[] = {
65 GTK_ICON_SIZE_INVALID,
66 GTK_ICON_SIZE_MENU,
67 GTK_ICON_SIZE_SMALL_TOOLBAR,
68 GTK_ICON_SIZE_LARGE_TOOLBAR,
69 GTK_ICON_SIZE_BUTTON,
70 GTK_ICON_SIZE_DND,
71 GTK_ICON_SIZE_DIALOG,
72 GTK_ICON_SIZE_MENU, // for Inkscape::ICON_SIZE_DECORATION
73 };
75 class IconCacheItem
76 {
77 public:
78 IconCacheItem( GtkIconSize lsize, GdkPixbuf* pb ) :
79 _lsize( lsize ),
80 _pb( pb )
81 {}
82 GtkIconSize _lsize;
83 GdkPixbuf* _pb;
84 };
86 static Glib::RefPtr<Gtk::IconFactory> inkyIcons;
88 GType
89 sp_icon_get_type()
90 {
91 //TODO: switch to GObject
92 // GtkType and such calls were deprecated a while back with the
93 // introduction of GObject as a separate layer, with GType instead. --JonCruz
95 static GType type = 0;
96 if (!type) {
97 GTypeInfo info = {
98 sizeof(SPIconClass),
99 NULL,
100 NULL,
101 (GClassInitFunc) sp_icon_class_init,
102 NULL,
103 NULL,
104 sizeof(SPIcon),
105 0,
106 (GInstanceInitFunc) sp_icon_init,
107 NULL
108 };
109 type = g_type_register_static(GTK_TYPE_WIDGET, "SPIcon", &info, (GTypeFlags)0);
110 }
111 return type;
112 }
114 static void
115 sp_icon_class_init(SPIconClass *klass)
116 {
117 GObjectClass *object_class;
118 GtkWidgetClass *widget_class;
120 object_class = (GObjectClass *) klass;
121 widget_class = (GtkWidgetClass *) klass;
123 parent_class = (GtkWidgetClass*)g_type_class_peek_parent(klass);
125 object_class->dispose = sp_icon_dispose;
127 widget_class->size_request = sp_icon_size_request;
128 widget_class->size_allocate = sp_icon_size_allocate;
129 widget_class->expose_event = sp_icon_expose;
130 widget_class->screen_changed = sp_icon_screen_changed;
131 widget_class->style_set = sp_icon_style_set;
132 }
135 static void
136 sp_icon_init(SPIcon *icon)
137 {
138 GTK_WIDGET_FLAGS(icon) |= GTK_NO_WINDOW;
139 icon->lsize = Inkscape::ICON_SIZE_BUTTON;
140 icon->psize = 0;
141 icon->name = 0;
142 icon->pb = 0;
143 icon->pb_faded = 0;
144 }
146 static void
147 sp_icon_dispose(GObject *object)
148 {
149 SPIcon *icon = SP_ICON(object);
150 sp_icon_clear(icon);
151 if ( icon->name ) {
152 g_free( icon->name );
153 icon->name = 0;
154 }
156 ((GObjectClass *) (parent_class))->dispose(object);
157 }
159 static void sp_icon_reset( SPIcon *icon ) {
160 icon->psize = 0;
161 sp_icon_clear(icon);
162 }
164 static void sp_icon_clear( SPIcon *icon ) {
165 if (icon->pb) {
166 g_object_unref(G_OBJECT(icon->pb));
167 icon->pb = NULL;
168 }
169 if (icon->pb_faded) {
170 g_object_unref(G_OBJECT(icon->pb_faded));
171 icon->pb_faded = NULL;
172 }
173 }
175 static void
176 sp_icon_size_request(GtkWidget *widget, GtkRequisition *requisition)
177 {
178 SPIcon const *icon = SP_ICON(widget);
180 int const size = ( icon->psize
181 ? icon->psize
182 : sp_icon_get_phys_size(icon->lsize) );
183 requisition->width = size;
184 requisition->height = size;
185 }
187 static void
188 sp_icon_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
189 {
190 widget->allocation = *allocation;
192 if (GTK_WIDGET_DRAWABLE(widget)) {
193 gtk_widget_queue_draw(widget);
194 }
195 }
197 static int sp_icon_expose(GtkWidget *widget, GdkEventExpose *event)
198 {
199 if ( GTK_WIDGET_DRAWABLE(widget) ) {
200 SPIcon *icon = SP_ICON(widget);
201 if ( !icon->pb ) {
202 sp_icon_fetch_pixbuf( icon );
203 }
205 sp_icon_paint(SP_ICON(widget), &event->area);
206 }
208 return TRUE;
209 }
211 // PUBLIC CALL:
212 void sp_icon_fetch_pixbuf( SPIcon *icon )
213 {
214 if ( icon ) {
215 if ( !icon->pb ) {
216 icon->psize = sp_icon_get_phys_size(icon->lsize);
217 GtkIconTheme *theme = gtk_icon_theme_get_default();
219 GdkPixbuf *pb = NULL;
220 if (gtk_icon_theme_has_icon(theme, icon->name)) {
221 pb = gtk_icon_theme_load_icon(theme, icon->name, icon->psize, (GtkIconLookupFlags) 0, NULL);
222 }
223 if (!pb) {
224 pb = sp_icon_image_load_svg( icon->name, Inkscape::getRegisteredIconSize(icon->lsize), icon->psize );
225 // if this was loaded from SVG, add it as a builtin icon
226 if (pb) gtk_icon_theme_add_builtin_icon(icon->name, icon->psize, pb);
227 }
228 if (!pb) {
229 pb = sp_icon_image_load_pixmap( icon->name, icon->lsize, icon->psize );
230 }
231 if ( pb ) {
232 icon->pb = pb;
233 icon->pb_faded = gdk_pixbuf_copy(icon->pb);
234 gdk_pixbuf_saturate_and_pixelate(icon->pb, icon->pb_faded, 0.5, TRUE);
235 } else {
236 /* TODO: We should do something more useful if we can't load the image. */
237 g_warning ("failed to load icon '%s'", icon->name);
238 }
239 }
240 }
241 }
243 static void sp_icon_screen_changed( GtkWidget *widget, GdkScreen *previous_screen )
244 {
245 if ( GTK_WIDGET_CLASS( parent_class )->screen_changed ) {
246 GTK_WIDGET_CLASS( parent_class )->screen_changed( widget, previous_screen );
247 }
248 SPIcon *icon = SP_ICON(widget);
249 sp_icon_theme_changed(icon);
250 }
252 static void sp_icon_style_set( GtkWidget *widget, GtkStyle *previous_style )
253 {
254 if ( GTK_WIDGET_CLASS( parent_class )->style_set ) {
255 GTK_WIDGET_CLASS( parent_class )->style_set( widget, previous_style );
256 }
257 SPIcon *icon = SP_ICON(widget);
258 sp_icon_theme_changed(icon);
259 }
261 static void sp_icon_theme_changed( SPIcon *icon )
262 {
263 //g_message("Got a change bump for this icon");
264 sizeDirty = true;
265 sp_icon_reset(icon);
266 gtk_widget_queue_draw( GTK_WIDGET(icon) );
267 }
270 static Glib::ustring icon_cache_key(gchar const *name, unsigned psize);
271 static GdkPixbuf *get_cached_pixbuf(Glib::ustring const &key);
273 static GtkWidget *
274 sp_icon_new_full( Inkscape::IconSize lsize, gchar const *name )
275 {
276 GtkWidget *widget = 0;
277 gint trySize = CLAMP( static_cast<gint>(lsize), 0, static_cast<gint>(G_N_ELEMENTS(iconSizeLookup) - 1) );
278 if ( !sizeMapDone ) {
279 injectCustomSize();
280 }
281 GtkIconSize mappedSize = iconSizeLookup[trySize];
283 GtkStockItem stock;
284 gboolean stockFound = gtk_stock_lookup( name, &stock );
286 if ( stockFound ) {
287 GtkWidget *img = gtk_image_new_from_stock( name, mappedSize );
288 widget = GTK_WIDGET(img);
289 }
291 if ( !widget ) {
292 //g_message("Creating an SPIcon instance for %s:%d", name, (int)lsize);
293 SPIcon *icon = (SPIcon *)g_object_new(SP_TYPE_ICON, NULL);
294 icon->lsize = lsize;
295 icon->name = g_strdup(name);
296 icon->psize = sp_icon_get_phys_size(lsize);
298 widget = GTK_WIDGET(icon);
299 }
301 return widget;
302 }
304 GtkWidget *
305 sp_icon_new( Inkscape::IconSize lsize, gchar const *name )
306 {
307 return sp_icon_new_full( lsize, name );
308 }
310 // PUBLIC CALL:
311 Gtk::Widget *sp_icon_get_icon( Glib::ustring const &oid, Inkscape::IconSize size )
312 {
313 Gtk::Widget *result = 0;
314 GtkWidget *widget = sp_icon_new_full( static_cast<Inkscape::IconSize>(Inkscape::getRegisteredIconSize(size)), oid.c_str() );
316 if ( widget ) {
317 if ( GTK_IS_IMAGE(widget) ) {
318 GtkImage *img = GTK_IMAGE(widget);
319 result = Glib::wrap( img );
320 } else {
321 result = Glib::wrap( widget );
322 }
323 }
325 return result;
326 }
328 GtkIconSize
329 sp_icon_get_gtk_size(int size)
330 {
331 static GtkIconSize sizemap[64] = {(GtkIconSize)0};
332 size = CLAMP(size, 4, 63);
333 if (!sizemap[size]) {
334 static int count = 0;
335 char c[64];
336 g_snprintf(c, 64, "InkscapeIcon%d", count++);
337 sizemap[size] = gtk_icon_size_register(c, size, size);
338 }
339 return sizemap[size];
340 }
343 static void injectCustomSize()
344 {
345 // TODO - still need to handle the case of theme changes and resize, especially as we can't re-register a string.
346 if ( !sizeMapDone )
347 {
348 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
349 bool dump = prefs->getBool( "/debug/icons/dumpDefault");
350 gint width = 0;
351 gint height = 0;
352 if ( gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &width, &height ) ) {
353 gint newWidth = ((width * 3) / 4);
354 gint newHeight = ((height * 3) / 4);
355 GtkIconSize newSizeEnum = gtk_icon_size_register( "inkscape-decoration", newWidth, newHeight );
356 if ( newSizeEnum ) {
357 if ( dump ) {
358 g_message("Registered (%d, %d) <= (%d, %d) as index %d", newWidth, newHeight, width, height, newSizeEnum);
359 }
360 guint index = static_cast<guint>(Inkscape::ICON_SIZE_DECORATION);
361 if ( index < G_N_ELEMENTS(iconSizeLookup) ) {
362 iconSizeLookup[index] = newSizeEnum;
363 } else if ( dump ) {
364 g_message("size lookup array too small to store entry");
365 }
366 }
367 }
368 sizeMapDone = true;
369 }
371 static bool hit = false;
372 if ( !hit ) {
373 hit = true;
374 inkyIcons = Gtk::IconFactory::create();
375 inkyIcons->add_default();
376 }
377 }
379 GtkIconSize Inkscape::getRegisteredIconSize( Inkscape::IconSize size )
380 {
381 GtkIconSize other = GTK_ICON_SIZE_MENU;
382 injectCustomSize();
383 size = CLAMP( size, Inkscape::ICON_SIZE_MENU, Inkscape::ICON_SIZE_DECORATION );
384 if ( size == Inkscape::ICON_SIZE_DECORATION ) {
385 other = gtk_icon_size_from_name("inkscape-decoration");
386 } else {
387 other = static_cast<GtkIconSize>(size);
388 }
390 return other;
391 }
394 // PUBLIC CALL:
395 int sp_icon_get_phys_size(int size)
396 {
397 static bool init = false;
398 static int lastSys[Inkscape::ICON_SIZE_DECORATION + 1];
399 static int vals[Inkscape::ICON_SIZE_DECORATION + 1];
401 size = CLAMP( size, GTK_ICON_SIZE_MENU, Inkscape::ICON_SIZE_DECORATION );
403 if ( !sizeMapDone ) {
404 injectCustomSize();
405 }
407 if ( sizeDirty && init ) {
408 GtkIconSize const gtkSizes[] = {
409 GTK_ICON_SIZE_MENU,
410 GTK_ICON_SIZE_SMALL_TOOLBAR,
411 GTK_ICON_SIZE_LARGE_TOOLBAR,
412 GTK_ICON_SIZE_BUTTON,
413 GTK_ICON_SIZE_DND,
414 GTK_ICON_SIZE_DIALOG,
415 static_cast<guint>(Inkscape::ICON_SIZE_DECORATION) < G_N_ELEMENTS(iconSizeLookup) ?
416 iconSizeLookup[static_cast<int>(Inkscape::ICON_SIZE_DECORATION)] :
417 GTK_ICON_SIZE_MENU
418 };
419 for (unsigned i = 0; i < G_N_ELEMENTS(gtkSizes) && init; ++i) {
420 guint const val_ix = (gtkSizes[i] <= GTK_ICON_SIZE_DIALOG) ? (guint)gtkSizes[i] : (guint)Inkscape::ICON_SIZE_DECORATION;
422 g_assert( val_ix < G_N_ELEMENTS(vals) );
424 gint width = 0;
425 gint height = 0;
426 if ( gtk_icon_size_lookup(gtkSizes[i], &width, &height ) ) {
427 init &= (lastSys[val_ix] == std::max(width, height));
428 }
429 }
430 }
432 if ( !init ) {
433 sizeDirty = false;
434 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
435 bool dump = prefs->getBool("/debug/icons/dumpDefault");
437 if ( dump ) {
438 g_message( "Default icon sizes:" );
439 }
440 memset( vals, 0, sizeof(vals) );
441 memset( lastSys, 0, sizeof(lastSys) );
442 GtkIconSize const gtkSizes[] = {
443 GTK_ICON_SIZE_MENU,
444 GTK_ICON_SIZE_SMALL_TOOLBAR,
445 GTK_ICON_SIZE_LARGE_TOOLBAR,
446 GTK_ICON_SIZE_BUTTON,
447 GTK_ICON_SIZE_DND,
448 GTK_ICON_SIZE_DIALOG,
449 static_cast<guint>(Inkscape::ICON_SIZE_DECORATION) < G_N_ELEMENTS(iconSizeLookup) ?
450 iconSizeLookup[static_cast<int>(Inkscape::ICON_SIZE_DECORATION)] :
451 GTK_ICON_SIZE_MENU
452 };
453 gchar const *const names[] = {
454 "GTK_ICON_SIZE_MENU",
455 "GTK_ICON_SIZE_SMALL_TOOLBAR",
456 "GTK_ICON_SIZE_LARGE_TOOLBAR",
457 "GTK_ICON_SIZE_BUTTON",
458 "GTK_ICON_SIZE_DND",
459 "GTK_ICON_SIZE_DIALOG",
460 "inkscape-decoration"
461 };
463 GtkWidget *icon = (GtkWidget *)g_object_new(SP_TYPE_ICON, NULL);
465 for (unsigned i = 0; i < G_N_ELEMENTS(gtkSizes); ++i) {
466 guint const val_ix = (gtkSizes[i] <= GTK_ICON_SIZE_DIALOG) ? (guint)gtkSizes[i] : (guint)Inkscape::ICON_SIZE_DECORATION;
468 g_assert( val_ix < G_N_ELEMENTS(vals) );
470 gint width = 0;
471 gint height = 0;
472 bool used = false;
473 if ( gtk_icon_size_lookup(gtkSizes[i], &width, &height ) ) {
474 vals[val_ix] = std::max(width, height);
475 lastSys[val_ix] = vals[val_ix];
476 used = true;
477 }
478 if (dump) {
479 g_message(" =-- %u size:%d %c(%d, %d) '%s'",
480 i, gtkSizes[i],
481 ( used ? ' ' : 'X' ), width, height, names[i]);
482 }
483 gchar const *id = GTK_STOCK_OPEN;
484 GdkPixbuf *pb = gtk_widget_render_icon( icon, id, gtkSizes[i], NULL);
485 if (pb) {
486 width = gdk_pixbuf_get_width(pb);
487 height = gdk_pixbuf_get_height(pb);
488 int newSize = std::max( width, height );
489 // TODO perhaps check a few more stock icons to get a range on sizes.
490 if ( newSize > 0 ) {
491 vals[val_ix] = newSize;
492 }
493 if (dump) {
494 g_message(" %u size:%d (%d, %d)", i, gtkSizes[i], width, height);
495 }
497 g_object_unref(G_OBJECT(pb));
498 }
499 }
500 //g_object_unref(icon);
501 init = true;
502 }
504 // Fixup workaround
505 if ((size == GTK_ICON_SIZE_MENU) || (size == GTK_ICON_SIZE_SMALL_TOOLBAR) || (size == GTK_ICON_SIZE_LARGE_TOOLBAR)) {
506 gint width = 0;
507 gint height = 0;
508 if ( gtk_icon_size_lookup( static_cast<GtkIconSize>(size), &width, &height ) ) {
509 vals[size] = std::max( width, height );
510 }
511 }
513 return vals[size];
514 }
516 static void sp_icon_paint(SPIcon *icon, GdkRectangle const *area)
517 {
518 GtkWidget &widget = *GTK_WIDGET(icon);
520 GdkPixbuf *image = GTK_WIDGET_IS_SENSITIVE(&widget) ? icon->pb : icon->pb_faded;
522 if (image) {
523 int const padx = ( ( widget.allocation.width > icon->psize )
524 ? ( widget.allocation.width - icon->psize ) / 2
525 : 0 );
526 int const pady = ( ( widget.allocation.height > icon->psize )
527 ? ( widget.allocation.height - icon->psize ) / 2
528 : 0 );
530 int const x0 = std::max(area->x, widget.allocation.x + padx);
531 int const y0 = std::max(area->y, widget.allocation.y + pady);
532 int const x1 = std::min(area->x + area->width, widget.allocation.x + padx + static_cast<int>(icon->psize) );
533 int const y1 = std::min(area->y + area->height, widget.allocation.y + pady + static_cast<int>(icon->psize) );
535 int width = x1 - x0;
536 int height = y1 - y0;
537 // Limit drawing to when we actually have something. Avoids some crashes.
538 if ( (width > 0) && (height > 0) ) {
539 gdk_draw_pixbuf(GDK_DRAWABLE(widget.window), NULL, image,
540 x0 - widget.allocation.x - padx,
541 y0 - widget.allocation.y - pady,
542 x0, y0,
543 width, height,
544 GDK_RGB_DITHER_NORMAL, x0, y0);
545 }
546 }
547 }
549 GdkPixbuf *sp_icon_image_load_pixmap(gchar const *name, unsigned /*lsize*/, unsigned psize)
550 {
551 gchar *path = (gchar *) g_strdup_printf("%s/%s.png", INKSCAPE_PIXMAPDIR, name);
552 // TODO: bulia, please look over
553 gsize bytesRead = 0;
554 gsize bytesWritten = 0;
555 GError *error = NULL;
556 gchar *localFilename = g_filename_from_utf8( path,
557 -1,
558 &bytesRead,
559 &bytesWritten,
560 &error);
561 GdkPixbuf *pb = gdk_pixbuf_new_from_file(localFilename, NULL);
562 g_free(localFilename);
563 g_free(path);
564 if (!pb) {
565 path = (gchar *) g_strdup_printf("%s/%s.xpm", INKSCAPE_PIXMAPDIR, name);
566 // TODO: bulia, please look over
567 gsize bytesRead = 0;
568 gsize bytesWritten = 0;
569 GError *error = NULL;
570 gchar *localFilename = g_filename_from_utf8( path,
571 -1,
572 &bytesRead,
573 &bytesWritten,
574 &error);
575 pb = gdk_pixbuf_new_from_file(localFilename, NULL);
576 g_free(localFilename);
577 g_free(path);
578 }
580 if (pb) {
581 if (!gdk_pixbuf_get_has_alpha(pb)) {
582 gdk_pixbuf_add_alpha(pb, FALSE, 0, 0, 0);
583 }
585 if ( ( static_cast<unsigned>(gdk_pixbuf_get_width(pb)) != psize )
586 || ( static_cast<unsigned>(gdk_pixbuf_get_height(pb)) != psize ) ) {
587 GdkPixbuf *spb = gdk_pixbuf_scale_simple(pb, psize, psize, GDK_INTERP_HYPER);
588 g_object_unref(G_OBJECT(pb));
589 pb = spb;
590 }
591 }
593 return pb;
594 }
596 // takes doc, root, icon, and icon name to produce pixels
597 extern "C" guchar *
598 sp_icon_doc_icon( SPDocument *doc, NRArenaItem *root,
599 gchar const *name, unsigned psize )
600 {
601 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
602 bool const dump = prefs->getBool("/debug/icons/dumpSvg");
603 guchar *px = NULL;
605 if (doc) {
606 SPObject *object = doc->getObjectById(name);
607 if (object && SP_IS_ITEM(object)) {
608 /* Find bbox in document */
609 Geom::Matrix const i2doc(sp_item_i2doc_affine(SP_ITEM(object)));
610 Geom::OptRect dbox = SP_ITEM(object)->getBounds(i2doc);
612 if ( SP_OBJECT_PARENT(object) == NULL )
613 {
614 dbox = Geom::Rect(Geom::Point(0, 0),
615 Geom::Point(sp_document_width(doc), sp_document_height(doc)));
616 }
618 /* This is in document coordinates, i.e. pixels */
619 if ( dbox ) {
620 NRGC gc(NULL);
621 /* Update to renderable state */
622 double sf = 1.0;
623 nr_arena_item_set_transform(root, (Geom::Matrix)Geom::Scale(sf, sf));
624 gc.transform.setIdentity();
625 nr_arena_item_invoke_update( root, NULL, &gc,
626 NR_ARENA_ITEM_STATE_ALL,
627 NR_ARENA_ITEM_STATE_NONE );
628 /* Item integer bbox in points */
629 NRRectL ibox;
630 ibox.x0 = (int) floor(sf * dbox->min()[Geom::X] + 0.5);
631 ibox.y0 = (int) floor(sf * dbox->min()[Geom::Y] + 0.5);
632 ibox.x1 = (int) floor(sf * dbox->max()[Geom::X] + 0.5);
633 ibox.y1 = (int) floor(sf * dbox->max()[Geom::Y] + 0.5);
635 if ( dump ) {
636 g_message( " box --'%s' (%f,%f)-(%f,%f)", name, (double)ibox.x0, (double)ibox.y0, (double)ibox.x1, (double)ibox.y1 );
637 }
639 /* Find button visible area */
640 int width = ibox.x1 - ibox.x0;
641 int height = ibox.y1 - ibox.y0;
643 if ( dump ) {
644 g_message( " vis --'%s' (%d,%d)", name, width, height );
645 }
647 {
648 int block = std::max(width, height);
649 if (block != static_cast<int>(psize) ) {
650 if ( dump ) {
651 g_message(" resizing" );
652 }
653 sf = (double)psize / (double)block;
655 nr_arena_item_set_transform(root, (Geom::Matrix)Geom::Scale(sf, sf));
656 gc.transform.setIdentity();
657 nr_arena_item_invoke_update( root, NULL, &gc,
658 NR_ARENA_ITEM_STATE_ALL,
659 NR_ARENA_ITEM_STATE_NONE );
660 /* Item integer bbox in points */
661 ibox.x0 = (int) floor(sf * dbox->min()[Geom::X] + 0.5);
662 ibox.y0 = (int) floor(sf * dbox->min()[Geom::Y] + 0.5);
663 ibox.x1 = (int) floor(sf * dbox->max()[Geom::X] + 0.5);
664 ibox.y1 = (int) floor(sf * dbox->max()[Geom::Y] + 0.5);
666 if ( dump ) {
667 g_message( " box2 --'%s' (%f,%f)-(%f,%f)", name, (double)ibox.x0, (double)ibox.y0, (double)ibox.x1, (double)ibox.y1 );
668 }
670 /* Find button visible area */
671 width = ibox.x1 - ibox.x0;
672 height = ibox.y1 - ibox.y0;
673 if ( dump ) {
674 g_message( " vis2 --'%s' (%d,%d)", name, width, height );
675 }
676 }
677 }
679 int dx, dy;
680 //dx = (psize - width) / 2;
681 //dy = (psize - height) / 2;
682 dx=dy=psize;
683 dx=(dx-width)/2; // watch out for psize, since 'unsigned'-'signed' can cause problems if the result is negative
684 dy=(dy-height)/2;
685 NRRectL area;
686 area.x0 = ibox.x0 - dx;
687 area.y0 = ibox.y0 - dy;
688 area.x1 = area.x0 + psize;
689 area.y1 = area.y0 + psize;
690 /* Actual renderable area */
691 NRRectL ua;
692 ua.x0 = MAX(ibox.x0, area.x0);
693 ua.y0 = MAX(ibox.y0, area.y0);
694 ua.x1 = MIN(ibox.x1, area.x1);
695 ua.y1 = MIN(ibox.y1, area.y1);
697 if ( dump ) {
698 g_message( " area --'%s' (%f,%f)-(%f,%f)", name, (double)area.x0, (double)area.y0, (double)area.x1, (double)area.y1 );
699 g_message( " ua --'%s' (%f,%f)-(%f,%f)", name, (double)ua.x0, (double)ua.y0, (double)ua.x1, (double)ua.y1 );
700 }
701 /* Set up pixblock */
702 px = g_new(guchar, 4 * psize * psize);
703 memset(px, 0x00, 4 * psize * psize);
704 /* Render */
705 NRPixBlock B;
706 nr_pixblock_setup_extern( &B, NR_PIXBLOCK_MODE_R8G8B8A8N,
707 ua.x0, ua.y0, ua.x1, ua.y1,
708 px + 4 * psize * (ua.y0 - area.y0) +
709 4 * (ua.x0 - area.x0),
710 4 * psize, FALSE, FALSE );
711 nr_arena_item_invoke_render(NULL, root, &ua, &B,
712 NR_ARENA_ITEM_RENDER_NO_CACHE );
713 nr_pixblock_release(&B);
715 bool useOverlay = prefs->getBool("/debug/icons/overlaySvg");
716 if ( useOverlay ) {
717 sp_icon_overlay_pixels( px, psize, psize, 4 * psize, 0x00, 0x00, 0xff );
718 }
719 }
720 }
721 }
723 return px;
724 } // end of sp_icon_doc_icon()
728 struct svg_doc_cache_t
729 {
730 SPDocument *doc;
731 NRArenaItem *root;
732 };
734 static std::map<Glib::ustring, svg_doc_cache_t *> doc_cache;
735 static std::map<Glib::ustring, GdkPixbuf *> pb_cache;
737 Glib::ustring icon_cache_key(gchar const *name, unsigned psize)
738 {
739 Glib::ustring key=name;
740 key += ":";
741 key += psize;
742 return key;
743 }
745 GdkPixbuf *get_cached_pixbuf(Glib::ustring const &key) {
746 std::map<Glib::ustring, GdkPixbuf *>::iterator found = pb_cache.find(key);
747 if ( found != pb_cache.end() ) {
748 return found->second;
749 }
750 return NULL;
751 }
753 static std::list<gchar*> &icons_svg_paths()
754 {
755 static std::list<gchar *> sources;
756 static bool initialized = false;
757 if (!initialized) {
758 // Fall back from user prefs dir into system locations.
759 gchar *userdir = profile_path("icons");
760 sources.push_back(g_build_filename(userdir,"icons.svg", NULL));
761 sources.push_back(g_build_filename(INKSCAPE_PIXMAPDIR, "icons.svg", NULL));
762 g_free(userdir);
763 initialized = true;
764 }
765 return sources;
766 }
768 // this function renders icons from icons.svg and returns the pixels.
769 static guchar *load_svg_pixels(gchar const *name,
770 unsigned /*lsize*/, unsigned psize)
771 {
772 SPDocument *doc = NULL;
773 NRArenaItem *root = NULL;
774 svg_doc_cache_t *info = NULL;
776 std::list<gchar *> &sources = icons_svg_paths();
778 // Try each document in turn until we successfully load the icon from one
779 guchar *px=NULL;
780 for (std::list<gchar*>::iterator i = sources.begin(); i != sources.end() && !px; ++i) {
781 gchar *doc_filename = *i;
783 // Did we already load this doc?
784 Glib::ustring key(doc_filename);
785 info = 0;
786 {
787 std::map<Glib::ustring, svg_doc_cache_t *>::iterator i = doc_cache.find(key);
788 if ( i != doc_cache.end() ) {
789 info = i->second;
790 }
791 }
793 /* Try to load from document. */
794 if (!info &&
795 Inkscape::IO::file_test( doc_filename, G_FILE_TEST_IS_REGULAR ) &&
796 (doc = sp_document_new( doc_filename, FALSE )) ) {
798 //g_message("Loaded icon file %s", doc_filename);
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 // move on to the next document if we couldn't get anything
823 if (!info && !doc) continue;
825 px = sp_icon_doc_icon( doc, root, name, psize );
826 //if (px) g_message("Found icon %s in %s", name, doc_filename);
827 }
829 //if (!px) g_message("Not found icon %s", name);
830 return px;
831 }
833 static GdkPixbuf *sp_icon_image_load_svg(gchar const *name, GtkIconSize lsize, unsigned psize)
834 {
835 Glib::ustring key = icon_cache_key(name, psize);
837 // did we already load this icon at this scale/size?
838 GdkPixbuf* pb = get_cached_pixbuf(key);
839 if (!pb) {
840 guchar *px = load_svg_pixels(name, lsize, psize);
841 if (px) {
842 pb = gdk_pixbuf_new_from_data(px, GDK_COLORSPACE_RGB, TRUE, 8,
843 psize, psize, psize * 4,
844 (GdkPixbufDestroyNotify)g_free, NULL);
845 pb_cache[key] = pb;
846 }
847 }
849 if ( pb ) {
850 // increase refcount since we're handing out ownership
851 g_object_ref(G_OBJECT(pb));
852 }
853 return pb;
854 }
856 void sp_icon_overlay_pixels(guchar *px, int width, int height, int stride,
857 unsigned r, unsigned g, unsigned b)
858 {
859 int bytesPerPixel = 4;
860 int spacing = 4;
861 for ( int y = 0; y < height; y += spacing ) {
862 guchar *ptr = px + y * stride;
863 for ( int x = 0; x < width; x += spacing ) {
864 *(ptr++) = r;
865 *(ptr++) = g;
866 *(ptr++) = b;
867 *(ptr++) = 0xff;
869 ptr += bytesPerPixel * (spacing - 1);
870 }
871 }
873 if ( width > 1 && height > 1 ) {
874 // point at the last pixel
875 guchar *ptr = px + ((height-1) * stride) + ((width - 1) * bytesPerPixel);
877 if ( width > 2 ) {
878 px[4] = r;
879 px[5] = g;
880 px[6] = b;
881 px[7] = 0xff;
883 ptr[-12] = r;
884 ptr[-11] = g;
885 ptr[-10] = b;
886 ptr[-9] = 0xff;
887 }
889 ptr[-4] = r;
890 ptr[-3] = g;
891 ptr[-2] = b;
892 ptr[-1] = 0xff;
894 px[0 + stride] = r;
895 px[1 + stride] = g;
896 px[2 + stride] = b;
897 px[3 + stride] = 0xff;
899 ptr[0 - stride] = r;
900 ptr[1 - stride] = g;
901 ptr[2 - stride] = b;
902 ptr[3 - stride] = 0xff;
904 if ( height > 2 ) {
905 ptr[0 - stride * 3] = r;
906 ptr[1 - stride * 3] = g;
907 ptr[2 - stride * 3] = b;
908 ptr[3 - stride * 3] = 0xff;
909 }
910 }
911 }
914 /*
915 Local Variables:
916 mode:c++
917 c-file-style:"stroustrup"
918 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
919 indent-tabs-mode:nil
920 fill-column:99
921 End:
922 */
923 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :