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/gtkiconfactory.h>
21 #include <gtk/gtkstock.h>
22 #include <gtk/gtkimage.h>
23 #include <gtkmm/iconfactory.h>
24 #include <gtkmm/iconset.h>
25 #include <gtkmm/iconsource.h>
26 #include <gtkmm/icontheme.h>
27 #include <gtkmm/image.h>
29 #include "path-prefix.h"
30 #include "prefs-utils.h"
31 #include "inkscape.h"
32 #include "document.h"
33 #include "sp-item.h"
34 #include "display/nr-arena.h"
35 #include "display/nr-arena-item.h"
36 #include "io/sys.h"
38 #include "icon.h"
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 GdkPixbuf *sp_icon_image_load_pixmap(gchar const *name, unsigned lsize, unsigned psize);
62 static GdkPixbuf *sp_icon_image_load_svg(gchar const *name, unsigned lsize, unsigned psize);
64 static void sp_icon_overlay_pixels( guchar *px, int width, int height, int stride,
65 unsigned r, unsigned g, unsigned b );
67 static void injectCustomSize();
69 static GtkWidgetClass *parent_class;
71 static bool sizeDirty = true;
73 static bool sizeMapDone = false;
74 static GtkIconSize iconSizeLookup[] = {
75 GTK_ICON_SIZE_INVALID,
76 GTK_ICON_SIZE_MENU,
77 GTK_ICON_SIZE_SMALL_TOOLBAR,
78 GTK_ICON_SIZE_LARGE_TOOLBAR,
79 GTK_ICON_SIZE_BUTTON,
80 GTK_ICON_SIZE_DND,
81 GTK_ICON_SIZE_DIALOG,
82 GTK_ICON_SIZE_MENU, // for Inkscape::ICON_SIZE_DECORATION
83 };
85 class IconCacheItem
86 {
87 public:
88 IconCacheItem( Inkscape::IconSize lsize, GdkPixbuf* pb ) :
89 _lsize( lsize ),
90 _pb( pb )
91 {}
92 Inkscape::IconSize _lsize;
93 GdkPixbuf* _pb;
94 };
96 static Glib::RefPtr<Gtk::IconFactory> inkyIcons;
97 static std::map<Glib::ustring, std::vector<IconCacheItem> > iconSetCache;
98 static std::map<gpointer, gulong> mapHandlerMap;
100 GtkType
101 sp_icon_get_type()
102 {
103 static GtkType type = 0;
104 if (!type) {
105 GtkTypeInfo info = {
106 "SPIcon",
107 sizeof(SPIcon),
108 sizeof(SPIconClass),
109 (GtkClassInitFunc) sp_icon_class_init,
110 (GtkObjectInitFunc) sp_icon_init,
111 NULL, NULL, NULL
112 };
113 type = gtk_type_unique(GTK_TYPE_WIDGET, &info);
114 }
115 return type;
116 }
118 static void
119 sp_icon_class_init(SPIconClass *klass)
120 {
121 GtkObjectClass *object_class;
122 GtkWidgetClass *widget_class;
124 object_class = (GtkObjectClass *) klass;
125 widget_class = (GtkWidgetClass *) klass;
127 parent_class = (GtkWidgetClass*)g_type_class_peek_parent(klass);
129 object_class->destroy = sp_icon_destroy;
131 widget_class->size_request = sp_icon_size_request;
132 widget_class->size_allocate = sp_icon_size_allocate;
133 widget_class->expose_event = sp_icon_expose;
134 widget_class->screen_changed = sp_icon_screen_changed;
135 widget_class->style_set = sp_icon_style_set;
136 }
139 static void
140 sp_icon_init(SPIcon *icon)
141 {
142 GTK_WIDGET_FLAGS(icon) |= GTK_NO_WINDOW;
143 icon->lsize = Inkscape::ICON_SIZE_BUTTON;
144 icon->psize = 0;
145 icon->name = 0;
146 icon->pb = 0;
147 icon->pb_faded = 0;
148 }
150 static void
151 sp_icon_destroy(GtkObject *object)
152 {
153 SPIcon *icon = SP_ICON(object);
154 sp_icon_clear(icon);
155 if ( icon->name ) {
156 g_free( icon->name );
157 icon->name = 0;
158 }
160 ((GtkObjectClass *) (parent_class))->destroy(object);
161 }
163 static void sp_icon_reset( SPIcon *icon ) {
164 icon->psize = 0;
165 sp_icon_clear(icon);
166 }
168 static void sp_icon_clear( SPIcon *icon ) {
169 if (icon->pb) {
170 g_object_unref(G_OBJECT(icon->pb));
171 icon->pb = NULL;
172 }
173 if (icon->pb_faded) {
174 g_object_unref(G_OBJECT(icon->pb_faded));
175 icon->pb_faded = NULL;
176 }
177 }
179 static void
180 sp_icon_size_request(GtkWidget *widget, GtkRequisition *requisition)
181 {
182 SPIcon const *icon = SP_ICON(widget);
184 int const size = ( icon->psize
185 ? icon->psize
186 : sp_icon_get_phys_size(icon->lsize) );
187 requisition->width = size;
188 requisition->height = size;
189 }
191 static void
192 sp_icon_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
193 {
194 widget->allocation = *allocation;
196 if (GTK_WIDGET_DRAWABLE(widget)) {
197 gtk_widget_queue_draw(widget);
198 }
199 }
201 static int sp_icon_expose(GtkWidget *widget, GdkEventExpose *event)
202 {
203 if ( GTK_WIDGET_DRAWABLE(widget) ) {
204 SPIcon *icon = SP_ICON(widget);
205 if ( !icon->pb ) {
206 sp_icon_fetch_pixbuf( icon );
207 }
209 sp_icon_paint(SP_ICON(widget), &event->area);
210 }
212 return TRUE;
213 }
215 // PUBLIC CALL:
216 void sp_icon_fetch_pixbuf( SPIcon *icon )
217 {
218 if ( icon ) {
219 if ( !icon->pb ) {
220 icon->psize = sp_icon_get_phys_size(icon->lsize);
222 GdkPixbuf *pb = sp_icon_image_load_svg( icon->name, icon->lsize, icon->psize );
223 if (!pb) {
224 pb = sp_icon_image_load_pixmap( icon->name, icon->lsize, icon->psize );
225 }
227 if ( pb ) {
228 icon->pb = pb;
229 icon->pb_faded = gdk_pixbuf_copy(icon->pb);
230 gdk_pixbuf_saturate_and_pixelate(icon->pb, icon->pb_faded, 0.5, TRUE);
231 } else {
232 /* TODO: We should do something more useful if we can't load the image. */
233 g_warning ("failed to load icon '%s'", icon->name);
234 }
235 }
236 }
237 }
239 static void sp_icon_screen_changed( GtkWidget *widget, GdkScreen *previous_screen )
240 {
241 if ( GTK_WIDGET_CLASS( parent_class )->screen_changed ) {
242 GTK_WIDGET_CLASS( parent_class )->screen_changed( widget, previous_screen );
243 }
244 SPIcon *icon = SP_ICON(widget);
245 sp_icon_theme_changed(icon);
246 }
248 static void sp_icon_style_set( GtkWidget *widget, GtkStyle *previous_style )
249 {
250 if ( GTK_WIDGET_CLASS( parent_class )->style_set ) {
251 GTK_WIDGET_CLASS( parent_class )->style_set( widget, previous_style );
252 }
253 SPIcon *icon = SP_ICON(widget);
254 sp_icon_theme_changed(icon);
255 }
257 static void sp_icon_theme_changed( SPIcon *icon )
258 {
259 //g_message("Got a change bump for this icon");
260 sizeDirty = true;
261 sp_icon_reset(icon);
262 gtk_widget_queue_draw( GTK_WIDGET(icon) );
263 }
266 static void imageMapCB(GtkWidget* widget, gpointer user_data);
267 static void populate_placeholder_icon(gchar const* name, unsigned lsize);
268 static bool prerender_icon(gchar const *name, unsigned lsize, unsigned psize);
269 static Glib::ustring icon_cache_key(gchar const *name, unsigned lsize, unsigned psize);
270 static GdkPixbuf *get_cached_pixbuf(Glib::ustring const &key);
272 std::map<Glib::ustring, Glib::ustring> legacyNames;
274 static void setupLegacyNaming() {
275 legacyNames["view-fullscreen"] = "fullscreen";
276 legacyNames["edit-select-all"] = "selection_select_all";
277 legacyNames["window-new"] = "view_new";
278 }
280 static GtkWidget *
281 sp_icon_new_full( Inkscape::IconSize lsize, gchar const *name )
282 {
283 static gint dump = prefs_get_int_attribute_limited( "debug.icons", "dumpGtk", 0, 0, 1 );
285 GtkWidget *widget = 0;
286 gint trySize = CLAMP( static_cast<gint>(lsize), 0, static_cast<gint>(G_N_ELEMENTS(iconSizeLookup) - 1) );
288 if ( !sizeMapDone ) {
289 injectCustomSize();
290 }
292 GtkStockItem stock;
293 gboolean stockFound = gtk_stock_lookup( name, &stock );
295 GtkWidget *img = 0;
296 if ( legacyNames.empty() ) {
297 setupLegacyNaming();
298 }
300 if ( legacyNames.find(name) != legacyNames.end() ) {
301 img = gtk_image_new_from_icon_name( name, iconSizeLookup[trySize] );
302 } else {
303 img = gtk_image_new_from_stock( name, iconSizeLookup[trySize] );
304 }
305 if ( img ) {
306 GtkImageType type = gtk_image_get_storage_type( GTK_IMAGE(img) );
307 if ( type == GTK_IMAGE_STOCK ) {
308 if ( !stockFound ) {
309 // It's not showing as a stock ID, so assume it will be present internally
310 populate_placeholder_icon( name, lsize );
311 addPreRender( lsize, name );
313 // Add a hook to render if set visible before prerender is done.
314 gulong handlerId = g_signal_connect( G_OBJECT(img), "map", G_CALLBACK(imageMapCB), GINT_TO_POINTER(0) );
315 mapHandlerMap[img] = handlerId;
316 }
317 widget = GTK_WIDGET(img);
318 img = 0;
319 if ( dump ) {
320 g_message( "loaded gtk '%s' %d (GTK_IMAGE_STOCK) %s", name, lsize, (stockFound ? "STOCK" : "local") );
321 }
322 } else if ( type == GTK_IMAGE_ICON_NAME ) {
323 widget = GTK_WIDGET(img);
324 img = 0;
325 addPreRender( lsize, name );
326 } else {
327 if ( dump ) {
328 g_message( "skipped gtk '%s' %d (not GTK_IMAGE_STOCK)", name, lsize );
329 }
330 //g_object_unref( (GObject *)img );
331 img = 0;
332 }
333 }
335 if ( !widget ) {
336 //g_message("Creating an SPIcon instance for %s:%d", name, (int)lsize);
337 SPIcon *icon = (SPIcon *)g_object_new(SP_TYPE_ICON, NULL);
338 icon->lsize = lsize;
339 icon->name = g_strdup(name);
340 icon->psize = sp_icon_get_phys_size(lsize);
342 widget = GTK_WIDGET(icon);
343 }
345 return widget;
346 }
348 GtkWidget *
349 sp_icon_new( Inkscape::IconSize lsize, gchar const *name )
350 {
351 return sp_icon_new_full( lsize, name );
352 }
354 // PUBLIC CALL:
355 Gtk::Widget *sp_icon_get_icon( Glib::ustring const &oid, Inkscape::IconSize size )
356 {
357 Gtk::Widget *result = 0;
358 GtkWidget *widget = sp_icon_new_full( size, oid.c_str() );
360 if ( widget ) {
361 if ( GTK_IS_IMAGE(widget) ) {
362 GtkImage *img = GTK_IMAGE(widget);
363 result = Glib::wrap( img );
364 } else {
365 result = Glib::wrap( widget );
366 }
367 }
369 return result;
370 }
372 GtkIconSize
373 sp_icon_get_gtk_size(int size)
374 {
375 static GtkIconSize sizemap[64] = {(GtkIconSize)0};
376 size = CLAMP(size, 4, 63);
377 if (!sizemap[size]) {
378 static int count = 0;
379 char c[64];
380 g_snprintf(c, 64, "InkscapeIcon%d", count++);
381 sizemap[size] = gtk_icon_size_register(c, size, size);
382 }
383 return sizemap[size];
384 }
386 static void injectCustomSize()
387 {
388 // TODO - still need to handle the case of theme changes and resize, especially as we can't re-register a string.
389 if ( !sizeMapDone )
390 {
391 gint dump = prefs_get_int_attribute_limited( "debug.icons", "dumpDefault", 0, 0, 1 );
392 gint width = 0;
393 gint height = 0;
394 if ( gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &width, &height ) ) {
395 gint newWidth = ((width * 3) / 4);
396 gint newHeight = ((height * 3) / 4);
397 GtkIconSize newSizeEnum = gtk_icon_size_register( "inkscape-decoration", newWidth, newHeight );
398 if ( newSizeEnum ) {
399 if ( dump ) {
400 g_message("Registered (%d, %d) <= (%d, %d) as index %d", newWidth, newHeight, width, height, newSizeEnum);
401 }
402 guint index = static_cast<guint>(Inkscape::ICON_SIZE_DECORATION);
403 if ( index < G_N_ELEMENTS(iconSizeLookup) ) {
404 iconSizeLookup[index] = newSizeEnum;
405 } else if ( dump ) {
406 g_message("size lookup array too small to store entry");
407 }
408 }
409 }
410 sizeMapDone = true;
411 }
413 static bool hit = false;
414 if ( !hit ) {
415 hit = true;
416 inkyIcons = Gtk::IconFactory::create();
417 inkyIcons->add_default();
418 }
419 }
421 // PUBLIC CALL:
422 int sp_icon_get_phys_size(int size)
423 {
424 static bool init = false;
425 static int lastSys[Inkscape::ICON_SIZE_DECORATION + 1];
426 static int vals[Inkscape::ICON_SIZE_DECORATION + 1];
428 size = CLAMP( size, GTK_ICON_SIZE_MENU, Inkscape::ICON_SIZE_DECORATION );
430 if ( !sizeMapDone ) {
431 injectCustomSize();
432 }
434 if ( sizeDirty && init ) {
435 GtkIconSize const gtkSizes[] = {
436 GTK_ICON_SIZE_MENU,
437 GTK_ICON_SIZE_SMALL_TOOLBAR,
438 GTK_ICON_SIZE_LARGE_TOOLBAR,
439 GTK_ICON_SIZE_BUTTON,
440 GTK_ICON_SIZE_DND,
441 GTK_ICON_SIZE_DIALOG,
442 static_cast<guint>(Inkscape::ICON_SIZE_DECORATION) < G_N_ELEMENTS(iconSizeLookup) ?
443 iconSizeLookup[static_cast<int>(Inkscape::ICON_SIZE_DECORATION)] :
444 GTK_ICON_SIZE_MENU
445 };
446 for (unsigned i = 0; i < G_N_ELEMENTS(gtkSizes) && init; ++i) {
447 guint const val_ix = (gtkSizes[i] <= GTK_ICON_SIZE_DIALOG) ? (guint)gtkSizes[i] : (guint)Inkscape::ICON_SIZE_DECORATION;
449 g_assert( val_ix < G_N_ELEMENTS(vals) );
451 gint width = 0;
452 gint height = 0;
453 if ( gtk_icon_size_lookup(gtkSizes[i], &width, &height ) ) {
454 init &= (lastSys[val_ix] == std::max(width, height));
455 }
456 }
457 }
459 if ( !init ) {
460 sizeDirty = false;
461 gint dump = prefs_get_int_attribute_limited( "debug.icons", "dumpDefault", 0, 0, 1 );
463 if ( dump ) {
464 g_message( "Default icon sizes:" );
465 }
466 memset( vals, 0, sizeof(vals) );
467 memset( lastSys, 0, sizeof(lastSys) );
468 GtkIconSize const gtkSizes[] = {
469 GTK_ICON_SIZE_MENU,
470 GTK_ICON_SIZE_SMALL_TOOLBAR,
471 GTK_ICON_SIZE_LARGE_TOOLBAR,
472 GTK_ICON_SIZE_BUTTON,
473 GTK_ICON_SIZE_DND,
474 GTK_ICON_SIZE_DIALOG,
475 static_cast<guint>(Inkscape::ICON_SIZE_DECORATION) < G_N_ELEMENTS(iconSizeLookup) ?
476 iconSizeLookup[static_cast<int>(Inkscape::ICON_SIZE_DECORATION)] :
477 GTK_ICON_SIZE_MENU
478 };
479 gchar const *const names[] = {
480 "GTK_ICON_SIZE_MENU",
481 "GTK_ICON_SIZE_SMALL_TOOLBAR",
482 "GTK_ICON_SIZE_LARGE_TOOLBAR",
483 "GTK_ICON_SIZE_BUTTON",
484 "GTK_ICON_SIZE_DND",
485 "GTK_ICON_SIZE_DIALOG",
486 "inkscape-decoration"
487 };
489 GtkWidget *icon = (GtkWidget *)g_object_new(SP_TYPE_ICON, NULL);
491 for (unsigned i = 0; i < G_N_ELEMENTS(gtkSizes); ++i) {
492 guint const val_ix = (gtkSizes[i] <= GTK_ICON_SIZE_DIALOG) ? (guint)gtkSizes[i] : (guint)Inkscape::ICON_SIZE_DECORATION;
494 g_assert( val_ix < G_N_ELEMENTS(vals) );
496 gint width = 0;
497 gint height = 0;
498 bool used = false;
499 if ( gtk_icon_size_lookup(gtkSizes[i], &width, &height ) ) {
500 vals[val_ix] = std::max(width, height);
501 lastSys[val_ix] = vals[val_ix];
502 used = true;
503 }
504 if (dump) {
505 g_message(" =-- %u size:%d %c(%d, %d) '%s'",
506 i, gtkSizes[i],
507 ( used ? ' ' : 'X' ), width, height, names[i]);
508 }
509 gchar const *id = GTK_STOCK_OPEN;
510 GdkPixbuf *pb = gtk_widget_render_icon( icon, id, gtkSizes[i], NULL);
511 if (pb) {
512 width = gdk_pixbuf_get_width(pb);
513 height = gdk_pixbuf_get_height(pb);
514 int newSize = std::max( width, height );
515 // TODO perhaps check a few more stock icons to get a range on sizes.
516 if ( newSize > 0 ) {
517 vals[val_ix] = newSize;
518 }
519 if (dump) {
520 g_message(" %u size:%d (%d, %d)", i, gtkSizes[i], width, height);
521 }
523 g_object_unref(G_OBJECT(pb));
524 }
525 }
526 //g_object_unref(icon);
527 init = true;
528 }
530 return vals[size];
531 }
533 static void sp_icon_paint(SPIcon *icon, GdkRectangle const *area)
534 {
535 GtkWidget &widget = *GTK_WIDGET(icon);
537 GdkPixbuf *image = GTK_WIDGET_IS_SENSITIVE(&widget) ? icon->pb : icon->pb_faded;
539 if (image) {
540 int const padx = ( ( widget.allocation.width > icon->psize )
541 ? ( widget.allocation.width - icon->psize ) / 2
542 : 0 );
543 int const pady = ( ( widget.allocation.height > icon->psize )
544 ? ( widget.allocation.height - icon->psize ) / 2
545 : 0 );
547 int const x0 = std::max(area->x, widget.allocation.x + padx);
548 int const y0 = std::max(area->y, widget.allocation.y + pady);
549 int const x1 = std::min(area->x + area->width, widget.allocation.x + padx + static_cast<int>(icon->psize) );
550 int const y1 = std::min(area->y + area->height, widget.allocation.y + pady + static_cast<int>(icon->psize) );
552 int width = x1 - x0;
553 int height = y1 - y0;
554 // Limit drawing to when we actually have something. Avoids some crashes.
555 if ( (width > 0) && (height > 0) ) {
556 gdk_draw_pixbuf(GDK_DRAWABLE(widget.window), NULL, image,
557 x0 - widget.allocation.x - padx,
558 y0 - widget.allocation.y - pady,
559 x0, y0,
560 width, height,
561 GDK_RGB_DITHER_NORMAL, x0, y0);
562 }
563 }
564 }
566 GdkPixbuf *sp_icon_image_load_pixmap(gchar const *name, unsigned /*lsize*/, unsigned psize)
567 {
568 gchar *path = (gchar *) g_strdup_printf("%s/%s.png", INKSCAPE_PIXMAPDIR, name);
569 // TODO: bulia, please look over
570 gsize bytesRead = 0;
571 gsize bytesWritten = 0;
572 GError *error = NULL;
573 gchar *localFilename = g_filename_from_utf8( path,
574 -1,
575 &bytesRead,
576 &bytesWritten,
577 &error);
578 GdkPixbuf *pb = gdk_pixbuf_new_from_file(localFilename, NULL);
579 g_free(localFilename);
580 g_free(path);
581 if (!pb) {
582 path = (gchar *) g_strdup_printf("%s/%s.xpm", INKSCAPE_PIXMAPDIR, name);
583 // TODO: bulia, please look over
584 gsize bytesRead = 0;
585 gsize bytesWritten = 0;
586 GError *error = NULL;
587 gchar *localFilename = g_filename_from_utf8( path,
588 -1,
589 &bytesRead,
590 &bytesWritten,
591 &error);
592 pb = gdk_pixbuf_new_from_file(localFilename, NULL);
593 g_free(localFilename);
594 g_free(path);
595 }
597 if (pb) {
598 if (!gdk_pixbuf_get_has_alpha(pb)) {
599 gdk_pixbuf_add_alpha(pb, FALSE, 0, 0, 0);
600 }
602 if ( ( static_cast<unsigned>(gdk_pixbuf_get_width(pb)) != psize )
603 || ( static_cast<unsigned>(gdk_pixbuf_get_height(pb)) != psize ) ) {
604 GdkPixbuf *spb = gdk_pixbuf_scale_simple(pb, psize, psize, GDK_INTERP_HYPER);
605 g_object_unref(G_OBJECT(pb));
606 pb = spb;
607 }
608 }
610 return pb;
611 }
613 // takes doc, root, icon, and icon name to produce pixels
614 extern "C" guchar *
615 sp_icon_doc_icon( SPDocument *doc, NRArenaItem *root,
616 gchar const *name, unsigned psize )
617 {
618 gint const dump = prefs_get_int_attribute_limited( "debug.icons", "dumpSvg", 0, 0, 1 );
619 guchar *px = NULL;
621 if (doc) {
622 SPObject *object = doc->getObjectById(name);
623 if (object && SP_IS_ITEM(object)) {
624 /* Find bbox in document */
625 NR::Matrix const i2doc(from_2geom(sp_item_i2doc_affine(SP_ITEM(object))));
626 NR::Maybe<NR::Rect> dbox = SP_ITEM(object)->getBounds(i2doc);
628 if ( SP_OBJECT_PARENT(object) == NULL )
629 {
630 dbox = NR::Rect(NR::Point(0, 0),
631 NR::Point(sp_document_width(doc), sp_document_height(doc)));
632 }
634 /* This is in document coordinates, i.e. pixels */
635 if ( dbox && !dbox->isEmpty() ) {
636 NRGC gc(NULL);
637 /* Update to renderable state */
638 double sf = 1.0;
639 nr_arena_item_set_transform(root, NR::Matrix(NR::scale(sf, sf)));
640 gc.transform.set_identity();
641 nr_arena_item_invoke_update( root, NULL, &gc,
642 NR_ARENA_ITEM_STATE_ALL,
643 NR_ARENA_ITEM_STATE_NONE );
644 /* Item integer bbox in points */
645 NRRectL ibox;
646 ibox.x0 = (int) floor(sf * dbox->min()[NR::X] + 0.5);
647 ibox.y0 = (int) floor(sf * dbox->min()[NR::Y] + 0.5);
648 ibox.x1 = (int) floor(sf * dbox->max()[NR::X] + 0.5);
649 ibox.y1 = (int) floor(sf * dbox->max()[NR::Y] + 0.5);
651 if ( dump ) {
652 g_message( " box --'%s' (%f,%f)-(%f,%f)", name, (double)ibox.x0, (double)ibox.y0, (double)ibox.x1, (double)ibox.y1 );
653 }
655 /* Find button visible area */
656 int width = ibox.x1 - ibox.x0;
657 int height = ibox.y1 - ibox.y0;
659 if ( dump ) {
660 g_message( " vis --'%s' (%d,%d)", name, width, height );
661 }
663 {
664 int block = std::max(width, height);
665 if (block != static_cast<int>(psize) ) {
666 if ( dump ) {
667 g_message(" resizing" );
668 }
669 sf = (double)psize / (double)block;
671 nr_arena_item_set_transform(root, NR::Matrix(NR::scale(sf, sf)));
672 gc.transform.set_identity();
673 nr_arena_item_invoke_update( root, NULL, &gc,
674 NR_ARENA_ITEM_STATE_ALL,
675 NR_ARENA_ITEM_STATE_NONE );
676 /* Item integer bbox in points */
677 ibox.x0 = (int) floor(sf * dbox->min()[NR::X] + 0.5);
678 ibox.y0 = (int) floor(sf * dbox->min()[NR::Y] + 0.5);
679 ibox.x1 = (int) floor(sf * dbox->max()[NR::X] + 0.5);
680 ibox.y1 = (int) floor(sf * dbox->max()[NR::Y] + 0.5);
682 if ( dump ) {
683 g_message( " box2 --'%s' (%f,%f)-(%f,%f)", name, (double)ibox.x0, (double)ibox.y0, (double)ibox.x1, (double)ibox.y1 );
684 }
686 /* Find button visible area */
687 width = ibox.x1 - ibox.x0;
688 height = ibox.y1 - ibox.y0;
689 if ( dump ) {
690 g_message( " vis2 --'%s' (%d,%d)", name, width, height );
691 }
692 }
693 }
695 int dx, dy;
696 //dx = (psize - width) / 2;
697 //dy = (psize - height) / 2;
698 dx=dy=psize;
699 dx=(dx-width)/2; // watch out for psize, since 'unsigned'-'signed' can cause problems if the result is negative
700 dy=(dy-height)/2;
701 NRRectL area;
702 area.x0 = ibox.x0 - dx;
703 area.y0 = ibox.y0 - dy;
704 area.x1 = area.x0 + psize;
705 area.y1 = area.y0 + psize;
706 /* Actual renderable area */
707 NRRectL ua;
708 ua.x0 = MAX(ibox.x0, area.x0);
709 ua.y0 = MAX(ibox.y0, area.y0);
710 ua.x1 = MIN(ibox.x1, area.x1);
711 ua.y1 = MIN(ibox.y1, area.y1);
713 if ( dump ) {
714 g_message( " area --'%s' (%f,%f)-(%f,%f)", name, (double)area.x0, (double)area.y0, (double)area.x1, (double)area.y1 );
715 g_message( " ua --'%s' (%f,%f)-(%f,%f)", name, (double)ua.x0, (double)ua.y0, (double)ua.x1, (double)ua.y1 );
716 }
717 /* Set up pixblock */
718 px = g_new(guchar, 4 * psize * psize);
719 memset(px, 0x00, 4 * psize * psize);
720 /* Render */
721 NRPixBlock B;
722 nr_pixblock_setup_extern( &B, NR_PIXBLOCK_MODE_R8G8B8A8N,
723 ua.x0, ua.y0, ua.x1, ua.y1,
724 px + 4 * psize * (ua.y0 - area.y0) +
725 4 * (ua.x0 - area.x0),
726 4 * psize, FALSE, FALSE );
727 nr_arena_item_invoke_render(NULL, root, &ua, &B,
728 NR_ARENA_ITEM_RENDER_NO_CACHE );
729 nr_pixblock_release(&B);
731 gint useOverlay = prefs_get_int_attribute_limited( "debug.icons", "overlaySvg", 0, 0, 1 );
732 if ( useOverlay ) {
733 sp_icon_overlay_pixels( px, psize, psize, 4 * psize, 0x00, 0x00, 0xff );
734 }
735 }
736 }
737 }
739 return px;
740 } // end of sp_icon_doc_icon()
744 struct svg_doc_cache_t
745 {
746 SPDocument *doc;
747 NRArenaItem *root;
748 };
750 static std::map<Glib::ustring, svg_doc_cache_t *> doc_cache;
751 static std::map<Glib::ustring, GdkPixbuf *> pb_cache;
753 Glib::ustring icon_cache_key(gchar const *name,
754 unsigned lsize, unsigned psize)
755 {
756 Glib::ustring key=name;
757 key += ":";
758 key += lsize;
759 key += ":";
760 key += psize;
761 return key;
762 }
764 GdkPixbuf *get_cached_pixbuf(Glib::ustring const &key) {
765 std::map<Glib::ustring, GdkPixbuf *>::iterator found = pb_cache.find(key);
766 if ( found != pb_cache.end() ) {
767 return found->second;
768 }
769 return NULL;
770 }
772 static guchar *load_svg_pixels(gchar const *name,
773 unsigned /*lsize*/, unsigned psize)
774 {
775 SPDocument *doc = NULL;
776 NRArenaItem *root = NULL;
777 svg_doc_cache_t *info = NULL;
779 // Fall back from user prefs dir into system locations.
780 Glib::ustring iconsvg = name;
781 iconsvg += ".svg";
782 std::list<gchar *> sources;
783 sources.push_back(g_build_filename(profile_path("icons"),iconsvg.c_str(), NULL));
784 sources.push_back(g_build_filename(profile_path("icons"),"icons.svg", NULL));
785 sources.push_back(g_build_filename(INKSCAPE_PIXMAPDIR, iconsvg.c_str(), NULL));
786 sources.push_back(g_build_filename(INKSCAPE_PIXMAPDIR, "icons.svg", NULL));
788 // Try each document in turn until we successfully load the icon from one
789 guchar *px=NULL;
790 while ( !sources.empty() && !px ) {
791 gchar *doc_filename = sources.front();
793 // Did we already load this doc?
794 Glib::ustring key(doc_filename);
795 info = 0;
796 {
797 std::map<Glib::ustring, svg_doc_cache_t *>::iterator i = doc_cache.find(key);
798 if ( i != doc_cache.end() ) {
799 info = i->second;
800 }
801 }
803 /* Try to load from document. */
804 if (!info &&
805 Inkscape::IO::file_test( doc_filename, G_FILE_TEST_IS_REGULAR ) &&
806 (doc = sp_document_new( doc_filename, FALSE )) ) {
808 // prep the document
809 sp_document_ensure_up_to_date(doc);
810 /* Create new arena */
811 NRArena *arena = NRArena::create();
812 /* Create ArenaItem and set transform */
813 unsigned visionkey = sp_item_display_key_new(1);
814 /* fixme: Memory manage root if needed (Lauris) */
815 root = sp_item_invoke_show( SP_ITEM(SP_DOCUMENT_ROOT(doc)),
816 arena, visionkey, SP_ITEM_SHOW_DISPLAY );
818 // store into the cache
819 info = new svg_doc_cache_t;
820 g_assert(info);
822 info->doc=doc;
823 info->root=root;
824 doc_cache[key]=info;
825 }
826 if (info) {
827 doc=info->doc;
828 root=info->root;
829 }
831 // toss the filename
832 g_free(doc_filename);
833 sources.pop_front();
835 // move on to the next document if we couldn't get anything
836 if (!info && !doc) continue;
838 px = sp_icon_doc_icon( doc, root, name, psize );
839 }
841 return px;
842 }
844 static void populate_placeholder_icon(gchar const* name, unsigned lsize)
845 {
846 if ( iconSetCache.find(name) == iconSetCache.end() ) {
847 // only add a placeholder if nothing is already set
848 Gtk::IconSet icnset;
849 Gtk::IconSource src;
850 src.set_icon_name( GTK_STOCK_MISSING_IMAGE );
851 src.set_size( Gtk::IconSize(lsize) );
852 icnset.add_source(src);
853 inkyIcons->add(Gtk::StockID(name), icnset);
854 }
855 }
857 static void addToIconSet(GdkPixbuf* pb, gchar const* name, unsigned lsize, unsigned psize) {
858 GtkStockItem stock;
859 gboolean stockFound = gtk_stock_lookup( name, &stock );
860 if ( !stockFound ) {
861 Gtk::IconTheme::add_builtin_icon( name, psize, Glib::wrap(pb) );
862 //g_message(" set in a builtin for %s:%d:%d", name, lsize, psize);
863 }
865 for ( std::vector<IconCacheItem>::iterator it = iconSetCache[name].begin(); it != iconSetCache[name].end(); ++it ) {
866 if ( it->_lsize == Inkscape::IconSize(lsize) ) {
867 iconSetCache[name].erase(it);
868 break;
869 }
870 }
871 iconSetCache[name].push_back(IconCacheItem(Inkscape::IconSize(lsize), pb));
873 Gtk::IconSet icnset;
874 for ( std::vector<IconCacheItem>::iterator it = iconSetCache[name].begin(); it != iconSetCache[name].end(); ++it ) {
875 Gtk::IconSource src;
876 g_object_ref( G_OBJECT(it->_pb) );
877 src.set_pixbuf( Glib::wrap(it->_pb) );
878 src.set_size( Gtk::IconSize(it->_lsize) );
879 src.set_size_wildcarded( (it->_lsize != 1) || (iconSetCache[name].size() == 1) );
880 src.set_state_wildcarded( true );
881 icnset.add_source(src);
882 }
883 inkyIcons->add(Gtk::StockID(name), icnset);
884 }
886 // returns true if icon needed preloading, false if nothing was done
887 bool prerender_icon(gchar const *name, unsigned lsize, unsigned psize)
888 {
889 Glib::ustring key = icon_cache_key(name, lsize, psize);
890 GdkPixbuf *pb = get_cached_pixbuf(key);
891 if (pb) {
892 return false;
893 } else {
894 guchar* px = load_svg_pixels(name, lsize, psize);
895 if ( !px ) {
896 // check for a fallback name
897 if ( legacyNames.find(name) != legacyNames.end() ) {
898 px = load_svg_pixels(legacyNames[name].c_str(), lsize, psize);
899 }
900 }
902 if (px) {
903 pb = gdk_pixbuf_new_from_data(px, GDK_COLORSPACE_RGB, TRUE, 8,
904 psize, psize, psize * 4,
905 (GdkPixbufDestroyNotify)g_free, NULL);
906 pb_cache[key] = pb;
907 addToIconSet(pb, name, lsize, psize);
908 }
909 return true;
910 }
911 }
913 static GdkPixbuf *sp_icon_image_load_svg(gchar const *name, unsigned lsize, unsigned psize)
914 {
915 Glib::ustring key = icon_cache_key(name, lsize, psize);
917 // did we already load this icon at this scale/size?
918 GdkPixbuf* pb = get_cached_pixbuf(key);
919 if (!pb) {
920 guchar *px = load_svg_pixels(name, lsize, psize);
921 if (px) {
922 pb = gdk_pixbuf_new_from_data(px, GDK_COLORSPACE_RGB, TRUE, 8,
923 psize, psize, psize * 4,
924 (GdkPixbufDestroyNotify)g_free, NULL);
925 pb_cache[key] = pb;
926 addToIconSet(pb, name, lsize, psize);
927 }
928 }
930 if ( pb ) {
931 // increase refcount since we're handing out ownership
932 g_object_ref(G_OBJECT(pb));
933 }
934 return pb;
935 }
937 void sp_icon_overlay_pixels(guchar *px, int width, int height, int stride,
938 unsigned r, unsigned g, unsigned b)
939 {
940 for ( int y = 0; y < height; y += 4 ) {
941 guchar *ptr = px + y * stride;
942 for ( int x = 0; x < width; x += 4 ) {
943 *(ptr++) = r;
944 *(ptr++) = g;
945 *(ptr++) = b;
946 *(ptr++) = 0xff;
948 ptr += 4 * 3;
949 }
950 }
952 if ( width > 1 && height > 1 ) {
953 // point at the last pixel
954 guchar *ptr = px + ((height-1) * stride) + ((width - 1) * 4);
956 if ( width > 2 ) {
957 px[4] = r;
958 px[5] = g;
959 px[6] = b;
960 px[7] = 0xff;
962 ptr[-12] = r;
963 ptr[-11] = g;
964 ptr[-10] = b;
965 ptr[-9] = 0xff;
966 }
968 ptr[-4] = r;
969 ptr[-3] = g;
970 ptr[-2] = b;
971 ptr[-1] = 0xff;
973 px[0 + stride] = r;
974 px[1 + stride] = g;
975 px[2 + stride] = b;
976 px[3 + stride] = 0xff;
978 ptr[0 - stride] = r;
979 ptr[1 - stride] = g;
980 ptr[2 - stride] = b;
981 ptr[3 - stride] = 0xff;
983 if ( height > 2 ) {
984 ptr[0 - stride * 3] = r;
985 ptr[1 - stride * 3] = g;
986 ptr[2 - stride * 3] = b;
987 ptr[3 - stride * 3] = 0xff;
988 }
989 }
990 }
992 class preRenderItem
993 {
994 public:
995 preRenderItem( Inkscape::IconSize lsize, gchar const *name ) :
996 _lsize( lsize ),
997 _name( name )
998 {}
999 Inkscape::IconSize _lsize;
1000 Glib::ustring _name;
1001 };
1004 #include <queue>
1006 static std::queue<preRenderItem> pendingRenders;
1007 static bool callbackHooked = false;
1009 static void addPreRender( Inkscape::IconSize lsize, gchar const *name )
1010 {
1011 if ( !callbackHooked )
1012 {
1013 callbackHooked = true;
1014 g_idle_add_full( G_PRIORITY_LOW, &icon_prerender_task, NULL, NULL );
1015 }
1017 pendingRenders.push(preRenderItem(lsize, name));
1018 }
1020 gboolean icon_prerender_task(gpointer /*data*/) {
1021 if (!pendingRenders.empty()) {
1022 bool workDone = false;
1023 do {
1024 preRenderItem single = pendingRenders.front();
1025 pendingRenders.pop();
1026 int psize = sp_icon_get_phys_size(single._lsize);
1027 workDone = prerender_icon(single._name.c_str(), single._lsize, psize);
1028 } while (!pendingRenders.empty() && !workDone);
1029 }
1031 if (!pendingRenders.empty()) {
1032 return TRUE;
1033 } else {
1034 callbackHooked = false;
1035 return FALSE;
1036 }
1037 }
1039 void imageMapCB(GtkWidget* widget, gpointer /*user_data*/) {
1040 gchar* id = 0;
1041 GtkIconSize size = GTK_ICON_SIZE_INVALID;
1042 gtk_image_get_stock(GTK_IMAGE(widget), &id, &size);
1043 if ( id ) {
1044 int psize = sp_icon_get_phys_size(size);
1045 prerender_icon(id, size, psize);
1047 std::vector<IconCacheItem>& iconSet = iconSetCache[id];
1048 for ( std::vector<IconCacheItem>::iterator it = iconSet.begin(); it != iconSet.end(); ++it ) {
1049 GObject* obj = G_OBJECT(it->_pb);
1050 if ( obj ) {
1051 }
1052 }
1053 }
1056 std::map<gpointer, gulong>::iterator it = mapHandlerMap.find(widget);
1058 if ( it != mapHandlerMap.end() ) {
1059 gulong handlerId = it->second;
1060 if ( g_signal_handler_is_connected(widget, handlerId) ) {
1061 g_signal_handler_disconnect(widget, handlerId);
1062 }
1063 mapHandlerMap.erase(it);
1064 }
1065 }
1068 /*
1069 Local Variables:
1070 mode:c++
1071 c-file-style:"stroustrup"
1072 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1073 indent-tabs-mode:nil
1074 fill-column:99
1075 End:
1076 */
1077 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :