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/image.h>
28 #include "path-prefix.h"
29 #include "prefs-utils.h"
30 #include "inkscape.h"
31 #include "document.h"
32 #include "sp-item.h"
33 #include "display/nr-arena.h"
34 #include "display/nr-arena-item.h"
35 #include "io/sys.h"
37 #include "icon.h"
39 static gboolean icon_prerender_task(gpointer data);
41 static void addPreRender( Inkscape::IconSize lsize, gchar const *name );
43 static void sp_icon_class_init(SPIconClass *klass);
44 static void sp_icon_init(SPIcon *icon);
45 static void sp_icon_destroy(GtkObject *object);
47 static void sp_icon_reset(SPIcon *icon);
48 static void sp_icon_clear(SPIcon *icon);
50 static void sp_icon_size_request(GtkWidget *widget, GtkRequisition *requisition);
51 static void sp_icon_size_allocate(GtkWidget *widget, GtkAllocation *allocation);
52 static int sp_icon_expose(GtkWidget *widget, GdkEventExpose *event);
54 static void sp_icon_paint(SPIcon *icon, GdkRectangle const *area);
56 static void sp_icon_screen_changed( GtkWidget *widget, GdkScreen *previous_screen );
57 static void sp_icon_style_set( GtkWidget *widget, GtkStyle *previous_style );
58 static void sp_icon_theme_changed( SPIcon *icon );
60 static GdkPixbuf *sp_icon_image_load_pixmap(gchar const *name, unsigned lsize, unsigned psize);
61 static GdkPixbuf *sp_icon_image_load_svg(gchar const *name, unsigned lsize, unsigned psize);
63 static void sp_icon_overlay_pixels( guchar *px, int width, int height, int stride,
64 unsigned r, unsigned g, unsigned b );
66 static void injectCustomSize();
68 static GtkWidgetClass *parent_class;
70 static bool sizeDirty = true;
72 static bool sizeMapDone = false;
73 static GtkIconSize iconSizeLookup[] = {
74 GTK_ICON_SIZE_INVALID,
75 GTK_ICON_SIZE_MENU,
76 GTK_ICON_SIZE_SMALL_TOOLBAR,
77 GTK_ICON_SIZE_LARGE_TOOLBAR,
78 GTK_ICON_SIZE_BUTTON,
79 GTK_ICON_SIZE_DND,
80 GTK_ICON_SIZE_DIALOG,
81 GTK_ICON_SIZE_MENU, // for Inkscape::ICON_SIZE_DECORATION
82 };
84 class IconCacheItem
85 {
86 public:
87 IconCacheItem( Inkscape::IconSize lsize, GdkPixbuf* pb ) :
88 _lsize( lsize ),
89 _pb( pb )
90 {}
91 Inkscape::IconSize _lsize;
92 GdkPixbuf* _pb;
93 };
95 static Glib::RefPtr<Gtk::IconFactory> inkyIcons;
96 static std::map<Glib::ustring, std::vector<IconCacheItem> > iconSetCache;
97 static std::map<gpointer, gulong> mapHandlerMap;
99 GtkType
100 sp_icon_get_type()
101 {
102 static GtkType type = 0;
103 if (!type) {
104 GtkTypeInfo info = {
105 "SPIcon",
106 sizeof(SPIcon),
107 sizeof(SPIconClass),
108 (GtkClassInitFunc) sp_icon_class_init,
109 (GtkObjectInitFunc) sp_icon_init,
110 NULL, NULL, NULL
111 };
112 type = gtk_type_unique(GTK_TYPE_WIDGET, &info);
113 }
114 return type;
115 }
117 static void
118 sp_icon_class_init(SPIconClass *klass)
119 {
120 GtkObjectClass *object_class;
121 GtkWidgetClass *widget_class;
123 object_class = (GtkObjectClass *) klass;
124 widget_class = (GtkWidgetClass *) klass;
126 parent_class = (GtkWidgetClass*)g_type_class_peek_parent(klass);
128 object_class->destroy = sp_icon_destroy;
130 widget_class->size_request = sp_icon_size_request;
131 widget_class->size_allocate = sp_icon_size_allocate;
132 widget_class->expose_event = sp_icon_expose;
133 widget_class->screen_changed = sp_icon_screen_changed;
134 widget_class->style_set = sp_icon_style_set;
135 }
138 static void
139 sp_icon_init(SPIcon *icon)
140 {
141 GTK_WIDGET_FLAGS(icon) |= GTK_NO_WINDOW;
142 icon->lsize = Inkscape::ICON_SIZE_BUTTON;
143 icon->psize = 0;
144 icon->name = 0;
145 icon->pb = 0;
146 icon->pb_faded = 0;
147 }
149 static void
150 sp_icon_destroy(GtkObject *object)
151 {
152 SPIcon *icon = SP_ICON(object);
153 sp_icon_clear(icon);
154 if ( icon->name ) {
155 g_free( icon->name );
156 icon->name = 0;
157 }
159 ((GtkObjectClass *) (parent_class))->destroy(object);
160 }
162 static void sp_icon_reset( SPIcon *icon ) {
163 icon->psize = 0;
164 sp_icon_clear(icon);
165 }
167 static void sp_icon_clear( SPIcon *icon ) {
168 if (icon->pb) {
169 g_object_unref(G_OBJECT(icon->pb));
170 icon->pb = NULL;
171 }
172 if (icon->pb_faded) {
173 g_object_unref(G_OBJECT(icon->pb_faded));
174 icon->pb_faded = NULL;
175 }
176 }
178 static void
179 sp_icon_size_request(GtkWidget *widget, GtkRequisition *requisition)
180 {
181 SPIcon const *icon = SP_ICON(widget);
183 int const size = ( icon->psize
184 ? icon->psize
185 : sp_icon_get_phys_size(icon->lsize) );
186 requisition->width = size;
187 requisition->height = size;
188 }
190 static void
191 sp_icon_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
192 {
193 widget->allocation = *allocation;
195 if (GTK_WIDGET_DRAWABLE(widget)) {
196 gtk_widget_queue_draw(widget);
197 }
198 }
200 static int sp_icon_expose(GtkWidget *widget, GdkEventExpose *event)
201 {
202 if ( GTK_WIDGET_DRAWABLE(widget) ) {
203 SPIcon *icon = SP_ICON(widget);
204 if ( !icon->pb ) {
205 sp_icon_fetch_pixbuf( icon );
206 }
208 sp_icon_paint(SP_ICON(widget), &event->area);
209 }
211 return TRUE;
212 }
214 // PUBLIC CALL:
215 void sp_icon_fetch_pixbuf( SPIcon *icon )
216 {
217 if ( icon ) {
218 if ( !icon->pb ) {
219 icon->psize = sp_icon_get_phys_size(icon->lsize);
221 GdkPixbuf *pb = sp_icon_image_load_svg( icon->name, icon->lsize, icon->psize );
222 if (!pb) {
223 pb = sp_icon_image_load_pixmap( icon->name, icon->lsize, icon->psize );
224 }
226 if ( pb ) {
227 icon->pb = pb;
228 icon->pb_faded = gdk_pixbuf_copy(icon->pb);
229 gdk_pixbuf_saturate_and_pixelate(icon->pb, icon->pb_faded, 0.5, TRUE);
230 } else {
231 /* TODO: We should do something more useful if we can't load the image. */
232 g_warning ("failed to load icon '%s'", icon->name);
233 }
234 }
235 }
236 }
238 static void sp_icon_screen_changed( GtkWidget *widget, GdkScreen *previous_screen )
239 {
240 if ( GTK_WIDGET_CLASS( parent_class )->screen_changed ) {
241 GTK_WIDGET_CLASS( parent_class )->screen_changed( widget, previous_screen );
242 }
243 SPIcon *icon = SP_ICON(widget);
244 sp_icon_theme_changed(icon);
245 }
247 static void sp_icon_style_set( GtkWidget *widget, GtkStyle *previous_style )
248 {
249 if ( GTK_WIDGET_CLASS( parent_class )->style_set ) {
250 GTK_WIDGET_CLASS( parent_class )->style_set( widget, previous_style );
251 }
252 SPIcon *icon = SP_ICON(widget);
253 sp_icon_theme_changed(icon);
254 }
256 static void sp_icon_theme_changed( SPIcon *icon )
257 {
258 //g_message("Got a change bump for this icon");
259 sizeDirty = true;
260 sp_icon_reset(icon);
261 gtk_widget_queue_draw( GTK_WIDGET(icon) );
262 }
265 static void imageMapCB(GtkWidget* widget, gpointer user_data);
266 static void populate_placeholder_icon(gchar const* name, unsigned lsize);
268 static GtkWidget *
269 sp_icon_new_full( Inkscape::IconSize lsize, gchar const *name )
270 {
271 static gint dump = prefs_get_int_attribute_limited( "debug.icons", "dumpGtk", 0, 0, 1 );
273 GtkWidget *widget = 0;
274 gint trySize = CLAMP( static_cast<gint>(lsize), 0, static_cast<gint>(G_N_ELEMENTS(iconSizeLookup) - 1) );
276 if ( !sizeMapDone ) {
277 injectCustomSize();
278 }
280 GtkWidget *img = gtk_image_new_from_stock( name, iconSizeLookup[trySize] );
281 if ( img ) {
282 GtkImageType type = gtk_image_get_storage_type( GTK_IMAGE(img) );
283 if ( type == GTK_IMAGE_STOCK ) {
284 GtkStockItem stock;
285 gboolean stockFound = gtk_stock_lookup( name, &stock );
286 if ( !stockFound ) {
287 // It's not showing as a stock ID, so assume it will be present internally
288 populate_placeholder_icon( name, lsize );
289 addPreRender( lsize, name );
291 // Add a hook to render if set visible before prerender is done.
292 gulong handlerId = g_signal_connect( G_OBJECT(img), "map", G_CALLBACK(imageMapCB), GINT_TO_POINTER(0) );
293 mapHandlerMap[img] = handlerId;
294 }
295 widget = GTK_WIDGET(img);
296 img = 0;
297 if ( dump ) {
298 g_message( "loaded gtk '%s' %d (GTK_IMAGE_STOCK) %s", name, lsize, (stockFound ? "STOCK" : "local") );
299 }
300 } else {
301 if ( dump ) {
302 g_message( "skipped gtk '%s' %d (not GTK_IMAGE_STOCK)", name, lsize );
303 }
304 g_object_unref( (GObject *)img );
305 img = 0;
306 }
307 }
309 if ( !widget ) {
310 //g_message("Creating an SPIcon instance for %s:%d", name, (int)lsize);
311 SPIcon *icon = (SPIcon *)g_object_new(SP_TYPE_ICON, NULL);
312 icon->lsize = lsize;
313 icon->name = g_strdup(name);
314 icon->psize = sp_icon_get_phys_size(lsize);
316 widget = GTK_WIDGET(icon);
317 }
319 return widget;
320 }
322 GtkWidget *
323 sp_icon_new( Inkscape::IconSize lsize, gchar const *name )
324 {
325 return sp_icon_new_full( lsize, name );
326 }
328 // PUBLIC CALL:
329 Gtk::Widget *sp_icon_get_icon( Glib::ustring const &oid, Inkscape::IconSize size )
330 {
331 Gtk::Widget *result = 0;
332 GtkWidget *widget = sp_icon_new_full( size, oid.c_str() );
334 if ( widget ) {
335 if ( GTK_IS_IMAGE(widget) ) {
336 GtkImage *img = GTK_IMAGE(widget);
337 result = Glib::wrap( img );
338 } else {
339 result = Glib::wrap( widget );
340 }
341 }
343 return result;
344 }
346 GtkIconSize
347 sp_icon_get_gtk_size(int size)
348 {
349 static GtkIconSize sizemap[64] = {(GtkIconSize)0};
350 size = CLAMP(size, 4, 63);
351 if (!sizemap[size]) {
352 static int count = 0;
353 char c[64];
354 g_snprintf(c, 64, "InkscapeIcon%d", count++);
355 sizemap[size] = gtk_icon_size_register(c, size, size);
356 }
357 return sizemap[size];
358 }
360 static void injectCustomSize()
361 {
362 // TODO - still need to handle the case of theme changes and resize, especially as we can't re-register a string.
363 if ( !sizeMapDone )
364 {
365 gint dump = prefs_get_int_attribute_limited( "debug.icons", "dumpDefault", 0, 0, 1 );
366 gint width = 0;
367 gint height = 0;
368 if ( gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &width, &height ) ) {
369 gint newWidth = ((width * 3) / 4);
370 gint newHeight = ((height * 3) / 4);
371 GtkIconSize newSizeEnum = gtk_icon_size_register( "inkscape-decoration", newWidth, newHeight );
372 if ( newSizeEnum ) {
373 if ( dump ) {
374 g_message("Registered (%d, %d) <= (%d, %d) as index %d", newWidth, newHeight, width, height, newSizeEnum);
375 }
376 guint index = static_cast<guint>(Inkscape::ICON_SIZE_DECORATION);
377 if ( index < G_N_ELEMENTS(iconSizeLookup) ) {
378 iconSizeLookup[index] = newSizeEnum;
379 } else if ( dump ) {
380 g_message("size lookup array too small to store entry");
381 }
382 }
383 }
384 sizeMapDone = true;
385 }
387 static bool hit = false;
388 if ( !hit ) {
389 hit = true;
390 inkyIcons = Gtk::IconFactory::create();
391 inkyIcons->add_default();
392 }
393 }
395 // PUBLIC CALL:
396 int sp_icon_get_phys_size(int size)
397 {
398 static bool init = false;
399 static int lastSys[Inkscape::ICON_SIZE_DECORATION + 1];
400 static int vals[Inkscape::ICON_SIZE_DECORATION + 1];
402 size = CLAMP( size, GTK_ICON_SIZE_MENU, Inkscape::ICON_SIZE_DECORATION );
404 if ( !sizeMapDone ) {
405 injectCustomSize();
406 }
408 if ( sizeDirty && init ) {
409 GtkIconSize const gtkSizes[] = {
410 GTK_ICON_SIZE_MENU,
411 GTK_ICON_SIZE_SMALL_TOOLBAR,
412 GTK_ICON_SIZE_LARGE_TOOLBAR,
413 GTK_ICON_SIZE_BUTTON,
414 GTK_ICON_SIZE_DND,
415 GTK_ICON_SIZE_DIALOG,
416 static_cast<guint>(Inkscape::ICON_SIZE_DECORATION) < G_N_ELEMENTS(iconSizeLookup) ?
417 iconSizeLookup[static_cast<int>(Inkscape::ICON_SIZE_DECORATION)] :
418 GTK_ICON_SIZE_MENU
419 };
420 for (unsigned i = 0; i < G_N_ELEMENTS(gtkSizes) && init; ++i) {
421 guint const val_ix = (gtkSizes[i] <= GTK_ICON_SIZE_DIALOG) ? (guint)gtkSizes[i] : (guint)Inkscape::ICON_SIZE_DECORATION;
423 g_assert( val_ix < G_N_ELEMENTS(vals) );
425 gint width = 0;
426 gint height = 0;
427 if ( gtk_icon_size_lookup(gtkSizes[i], &width, &height ) ) {
428 init &= (lastSys[val_ix] == std::max(width, height));
429 }
430 }
431 }
433 if ( !init ) {
434 sizeDirty = false;
435 gint dump = prefs_get_int_attribute_limited( "debug.icons", "dumpDefault", 0, 0, 1 );
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 return vals[size];
505 }
507 static void sp_icon_paint(SPIcon *icon, GdkRectangle const *area)
508 {
509 GtkWidget &widget = *GTK_WIDGET(icon);
511 GdkPixbuf *image = GTK_WIDGET_IS_SENSITIVE(&widget) ? icon->pb : icon->pb_faded;
513 if (image) {
514 int const padx = ( ( widget.allocation.width > icon->psize )
515 ? ( widget.allocation.width - icon->psize ) / 2
516 : 0 );
517 int const pady = ( ( widget.allocation.height > icon->psize )
518 ? ( widget.allocation.height - icon->psize ) / 2
519 : 0 );
521 int const x0 = std::max(area->x, widget.allocation.x + padx);
522 int const y0 = std::max(area->y, widget.allocation.y + pady);
523 int const x1 = std::min(area->x + area->width, widget.allocation.x + padx + static_cast<int>(icon->psize) );
524 int const y1 = std::min(area->y + area->height, widget.allocation.y + pady + static_cast<int>(icon->psize) );
526 int width = x1 - x0;
527 int height = y1 - y0;
528 // Limit drawing to when we actually have something. Avoids some crashes.
529 if ( (width > 0) && (height > 0) ) {
530 gdk_draw_pixbuf(GDK_DRAWABLE(widget.window), NULL, image,
531 x0 - widget.allocation.x - padx,
532 y0 - widget.allocation.y - pady,
533 x0, y0,
534 width, height,
535 GDK_RGB_DITHER_NORMAL, x0, y0);
536 }
537 }
538 }
540 GdkPixbuf *sp_icon_image_load_pixmap(gchar const *name, unsigned /*lsize*/, unsigned psize)
541 {
542 gchar *path = (gchar *) g_strdup_printf("%s/%s.png", INKSCAPE_PIXMAPDIR, name);
543 // TODO: bulia, please look over
544 gsize bytesRead = 0;
545 gsize bytesWritten = 0;
546 GError *error = NULL;
547 gchar *localFilename = g_filename_from_utf8( path,
548 -1,
549 &bytesRead,
550 &bytesWritten,
551 &error);
552 GdkPixbuf *pb = gdk_pixbuf_new_from_file(localFilename, NULL);
553 g_free(localFilename);
554 g_free(path);
555 if (!pb) {
556 path = (gchar *) g_strdup_printf("%s/%s.xpm", INKSCAPE_PIXMAPDIR, name);
557 // TODO: bulia, please look over
558 gsize bytesRead = 0;
559 gsize bytesWritten = 0;
560 GError *error = NULL;
561 gchar *localFilename = g_filename_from_utf8( path,
562 -1,
563 &bytesRead,
564 &bytesWritten,
565 &error);
566 pb = gdk_pixbuf_new_from_file(localFilename, NULL);
567 g_free(localFilename);
568 g_free(path);
569 }
571 if (pb) {
572 if (!gdk_pixbuf_get_has_alpha(pb)) {
573 gdk_pixbuf_add_alpha(pb, FALSE, 0, 0, 0);
574 }
576 if ( ( static_cast<unsigned>(gdk_pixbuf_get_width(pb)) != psize )
577 || ( static_cast<unsigned>(gdk_pixbuf_get_height(pb)) != psize ) ) {
578 GdkPixbuf *spb = gdk_pixbuf_scale_simple(pb, psize, psize, GDK_INTERP_HYPER);
579 g_object_unref(G_OBJECT(pb));
580 pb = spb;
581 }
582 }
584 return pb;
585 }
587 // takes doc, root, icon, and icon name to produce pixels
588 extern "C" guchar *
589 sp_icon_doc_icon( SPDocument *doc, NRArenaItem *root,
590 gchar const *name, unsigned psize )
591 {
592 gint const dump = prefs_get_int_attribute_limited( "debug.icons", "dumpSvg", 0, 0, 1 );
593 guchar *px = NULL;
595 if (doc) {
596 SPObject *object = doc->getObjectById(name);
597 if (object && SP_IS_ITEM(object)) {
598 /* Find bbox in document */
599 NR::Matrix const i2doc(from_2geom(sp_item_i2doc_affine(SP_ITEM(object))));
600 NR::Maybe<NR::Rect> dbox = SP_ITEM(object)->getBounds(i2doc);
602 if ( SP_OBJECT_PARENT(object) == NULL )
603 {
604 dbox = NR::Rect(NR::Point(0, 0),
605 NR::Point(sp_document_width(doc), sp_document_height(doc)));
606 }
608 /* This is in document coordinates, i.e. pixels */
609 if ( dbox && !dbox->isEmpty() ) {
610 NRGC gc(NULL);
611 /* Update to renderable state */
612 double sf = 1.0;
613 nr_arena_item_set_transform(root, NR::Matrix(NR::scale(sf, sf)));
614 gc.transform.set_identity();
615 nr_arena_item_invoke_update( root, NULL, &gc,
616 NR_ARENA_ITEM_STATE_ALL,
617 NR_ARENA_ITEM_STATE_NONE );
618 /* Item integer bbox in points */
619 NRRectL ibox;
620 ibox.x0 = (int) floor(sf * dbox->min()[NR::X] + 0.5);
621 ibox.y0 = (int) floor(sf * dbox->min()[NR::Y] + 0.5);
622 ibox.x1 = (int) floor(sf * dbox->max()[NR::X] + 0.5);
623 ibox.y1 = (int) floor(sf * dbox->max()[NR::Y] + 0.5);
625 if ( dump ) {
626 g_message( " box --'%s' (%f,%f)-(%f,%f)", name, (double)ibox.x0, (double)ibox.y0, (double)ibox.x1, (double)ibox.y1 );
627 }
629 /* Find button visible area */
630 int width = ibox.x1 - ibox.x0;
631 int height = ibox.y1 - ibox.y0;
633 if ( dump ) {
634 g_message( " vis --'%s' (%d,%d)", name, width, height );
635 }
637 {
638 int block = std::max(width, height);
639 if (block != static_cast<int>(psize) ) {
640 if ( dump ) {
641 g_message(" resizing" );
642 }
643 sf = (double)psize / (double)block;
645 nr_arena_item_set_transform(root, NR::Matrix(NR::scale(sf, sf)));
646 gc.transform.set_identity();
647 nr_arena_item_invoke_update( root, NULL, &gc,
648 NR_ARENA_ITEM_STATE_ALL,
649 NR_ARENA_ITEM_STATE_NONE );
650 /* Item integer bbox in points */
651 ibox.x0 = (int) floor(sf * dbox->min()[NR::X] + 0.5);
652 ibox.y0 = (int) floor(sf * dbox->min()[NR::Y] + 0.5);
653 ibox.x1 = (int) floor(sf * dbox->max()[NR::X] + 0.5);
654 ibox.y1 = (int) floor(sf * dbox->max()[NR::Y] + 0.5);
656 if ( dump ) {
657 g_message( " box2 --'%s' (%f,%f)-(%f,%f)", name, (double)ibox.x0, (double)ibox.y0, (double)ibox.x1, (double)ibox.y1 );
658 }
660 /* Find button visible area */
661 width = ibox.x1 - ibox.x0;
662 height = ibox.y1 - ibox.y0;
663 if ( dump ) {
664 g_message( " vis2 --'%s' (%d,%d)", name, width, height );
665 }
666 }
667 }
669 int dx, dy;
670 //dx = (psize - width) / 2;
671 //dy = (psize - height) / 2;
672 dx=dy=psize;
673 dx=(dx-width)/2; // watch out for psize, since 'unsigned'-'signed' can cause problems if the result is negative
674 dy=(dy-height)/2;
675 NRRectL area;
676 area.x0 = ibox.x0 - dx;
677 area.y0 = ibox.y0 - dy;
678 area.x1 = area.x0 + psize;
679 area.y1 = area.y0 + psize;
680 /* Actual renderable area */
681 NRRectL ua;
682 ua.x0 = MAX(ibox.x0, area.x0);
683 ua.y0 = MAX(ibox.y0, area.y0);
684 ua.x1 = MIN(ibox.x1, area.x1);
685 ua.y1 = MIN(ibox.y1, area.y1);
687 if ( dump ) {
688 g_message( " area --'%s' (%f,%f)-(%f,%f)", name, (double)area.x0, (double)area.y0, (double)area.x1, (double)area.y1 );
689 g_message( " ua --'%s' (%f,%f)-(%f,%f)", name, (double)ua.x0, (double)ua.y0, (double)ua.x1, (double)ua.y1 );
690 }
691 /* Set up pixblock */
692 px = g_new(guchar, 4 * psize * psize);
693 memset(px, 0x00, 4 * psize * psize);
694 /* Render */
695 NRPixBlock B;
696 nr_pixblock_setup_extern( &B, NR_PIXBLOCK_MODE_R8G8B8A8N,
697 ua.x0, ua.y0, ua.x1, ua.y1,
698 px + 4 * psize * (ua.y0 - area.y0) +
699 4 * (ua.x0 - area.x0),
700 4 * psize, FALSE, FALSE );
701 nr_arena_item_invoke_render(NULL, root, &ua, &B,
702 NR_ARENA_ITEM_RENDER_NO_CACHE );
703 nr_pixblock_release(&B);
705 gint useOverlay = prefs_get_int_attribute_limited( "debug.icons", "overlaySvg", 0, 0, 1 );
706 if ( useOverlay ) {
707 sp_icon_overlay_pixels( px, psize, psize, 4 * psize, 0x00, 0x00, 0xff );
708 }
709 }
710 }
711 }
713 return px;
714 } // end of sp_icon_doc_icon()
718 struct svg_doc_cache_t
719 {
720 SPDocument *doc;
721 NRArenaItem *root;
722 };
724 static std::map<Glib::ustring, svg_doc_cache_t *> doc_cache;
725 static std::map<Glib::ustring, GdkPixbuf *> pb_cache;
727 static Glib::ustring icon_cache_key(gchar const *name,
728 unsigned lsize, unsigned psize)
729 {
730 Glib::ustring key=name;
731 key += ":";
732 key += lsize;
733 key += ":";
734 key += psize;
735 return key;
736 }
738 static GdkPixbuf *get_cached_pixbuf(Glib::ustring const &key) {
739 std::map<Glib::ustring, GdkPixbuf *>::iterator found = pb_cache.find(key);
740 if ( found != pb_cache.end() ) {
741 return found->second;
742 }
743 return NULL;
744 }
746 static guchar *load_svg_pixels(gchar const *name,
747 unsigned /*lsize*/, unsigned psize)
748 {
749 SPDocument *doc = NULL;
750 NRArenaItem *root = NULL;
751 svg_doc_cache_t *info = NULL;
753 // Fall back from user prefs dir into system locations.
754 Glib::ustring iconsvg = name;
755 iconsvg += ".svg";
756 std::list<gchar *> sources;
757 sources.push_back(g_build_filename(profile_path("icons"),iconsvg.c_str(), NULL));
758 sources.push_back(g_build_filename(profile_path("icons"),"icons.svg", NULL));
759 sources.push_back(g_build_filename(INKSCAPE_PIXMAPDIR, iconsvg.c_str(), NULL));
760 sources.push_back(g_build_filename(INKSCAPE_PIXMAPDIR, "icons.svg", NULL));
762 // Try each document in turn until we successfully load the icon from one
763 guchar *px=NULL;
764 while ( !sources.empty() && !px ) {
765 gchar *doc_filename = sources.front();
767 // Did we already load this doc?
768 Glib::ustring key(doc_filename);
769 info = 0;
770 {
771 std::map<Glib::ustring, svg_doc_cache_t *>::iterator i = doc_cache.find(key);
772 if ( i != doc_cache.end() ) {
773 info = i->second;
774 }
775 }
777 /* Try to load from document. */
778 if (!info &&
779 Inkscape::IO::file_test( doc_filename, G_FILE_TEST_IS_REGULAR ) &&
780 (doc = sp_document_new( doc_filename, FALSE )) ) {
782 // prep the document
783 sp_document_ensure_up_to_date(doc);
784 /* Create new arena */
785 NRArena *arena = NRArena::create();
786 /* Create ArenaItem and set transform */
787 unsigned visionkey = sp_item_display_key_new(1);
788 /* fixme: Memory manage root if needed (Lauris) */
789 root = sp_item_invoke_show( SP_ITEM(SP_DOCUMENT_ROOT(doc)),
790 arena, visionkey, SP_ITEM_SHOW_DISPLAY );
792 // store into the cache
793 info = new svg_doc_cache_t;
794 g_assert(info);
796 info->doc=doc;
797 info->root=root;
798 doc_cache[key]=info;
799 }
800 if (info) {
801 doc=info->doc;
802 root=info->root;
803 }
805 // toss the filename
806 g_free(doc_filename);
807 sources.pop_front();
809 // move on to the next document if we couldn't get anything
810 if (!info && !doc) continue;
812 px = sp_icon_doc_icon( doc, root, name, psize );
813 }
815 return px;
816 }
818 static void populate_placeholder_icon(gchar const* name, unsigned lsize)
819 {
820 if ( iconSetCache.find(name) == iconSetCache.end() ) {
821 // only add a placeholder if nothing is already set
822 Gtk::IconSet icnset;
823 Gtk::IconSource src;
824 src.set_icon_name( GTK_STOCK_MISSING_IMAGE );
825 src.set_size( Gtk::IconSize(lsize) );
826 icnset.add_source(src);
827 inkyIcons->add(Gtk::StockID(name), icnset);
828 }
829 }
831 static void addToIconSet(GdkPixbuf* pb, gchar const* name, unsigned lsize, unsigned /*psize*/) {
832 for ( std::vector<IconCacheItem>::iterator it = iconSetCache[name].begin(); it != iconSetCache[name].end(); ++it ) {
833 if ( it->_lsize == Inkscape::IconSize(lsize) ) {
834 iconSetCache[name].erase(it);
835 break;
836 }
837 }
838 iconSetCache[name].push_back(IconCacheItem(Inkscape::IconSize(lsize), pb));
840 Gtk::IconSet icnset;
841 for ( std::vector<IconCacheItem>::iterator it = iconSetCache[name].begin(); it != iconSetCache[name].end(); ++it ) {
842 Gtk::IconSource src;
843 g_object_ref( G_OBJECT(it->_pb) );
844 src.set_pixbuf( Glib::wrap(it->_pb) );
845 src.set_size( Gtk::IconSize(it->_lsize) );
846 src.set_size_wildcarded( (it->_lsize != 1) || (iconSetCache[name].size() == 1) );
847 src.set_state_wildcarded( true );
848 icnset.add_source(src);
849 }
850 inkyIcons->add(Gtk::StockID(name), icnset);
851 }
853 // returns true if icon needed preloading, false if nothing was done
854 static bool prerender_icon(gchar const *name, unsigned lsize, unsigned psize)
855 {
856 Glib::ustring key = icon_cache_key(name, lsize, psize);
857 GdkPixbuf *pb = get_cached_pixbuf(key);
858 if (pb) {
859 return false;
860 } else {
861 guchar* px = load_svg_pixels(name, lsize, psize);
862 if (px) {
863 pb = gdk_pixbuf_new_from_data(px, GDK_COLORSPACE_RGB, TRUE, 8,
864 psize, psize, psize * 4,
865 (GdkPixbufDestroyNotify)g_free, NULL);
866 pb_cache[key] = pb;
867 addToIconSet(pb, name, lsize, psize);
868 }
869 return true;
870 }
871 }
873 static GdkPixbuf *sp_icon_image_load_svg(gchar const *name, unsigned lsize, unsigned psize)
874 {
875 Glib::ustring key = icon_cache_key(name, lsize, psize);
877 // did we already load this icon at this scale/size?
878 GdkPixbuf* pb = get_cached_pixbuf(key);
879 if (!pb) {
880 guchar *px = load_svg_pixels(name, lsize, psize);
881 if (px) {
882 pb = gdk_pixbuf_new_from_data(px, GDK_COLORSPACE_RGB, TRUE, 8,
883 psize, psize, psize * 4,
884 (GdkPixbufDestroyNotify)g_free, NULL);
885 pb_cache[key] = pb;
886 addToIconSet(pb, name, lsize, psize);
887 }
888 }
890 // increase refcount since we're hading out ownership
891 g_object_ref(G_OBJECT(pb));
892 return pb;
893 }
895 void sp_icon_overlay_pixels(guchar *px, int width, int height, int stride,
896 unsigned r, unsigned g, unsigned b)
897 {
898 for ( int y = 0; y < height; y += 4 ) {
899 guchar *ptr = px + y * stride;
900 for ( int x = 0; x < width; x += 4 ) {
901 *(ptr++) = r;
902 *(ptr++) = g;
903 *(ptr++) = b;
904 *(ptr++) = 0xff;
906 ptr += 4 * 3;
907 }
908 }
910 if ( width > 1 && height > 1 ) {
911 // point at the last pixel
912 guchar *ptr = px + ((height-1) * stride) + ((width - 1) * 4);
914 if ( width > 2 ) {
915 px[4] = r;
916 px[5] = g;
917 px[6] = b;
918 px[7] = 0xff;
920 ptr[-12] = r;
921 ptr[-11] = g;
922 ptr[-10] = b;
923 ptr[-9] = 0xff;
924 }
926 ptr[-4] = r;
927 ptr[-3] = g;
928 ptr[-2] = b;
929 ptr[-1] = 0xff;
931 px[0 + stride] = r;
932 px[1 + stride] = g;
933 px[2 + stride] = b;
934 px[3 + stride] = 0xff;
936 ptr[0 - stride] = r;
937 ptr[1 - stride] = g;
938 ptr[2 - stride] = b;
939 ptr[3 - stride] = 0xff;
941 if ( height > 2 ) {
942 ptr[0 - stride * 3] = r;
943 ptr[1 - stride * 3] = g;
944 ptr[2 - stride * 3] = b;
945 ptr[3 - stride * 3] = 0xff;
946 }
947 }
948 }
950 class preRenderItem
951 {
952 public:
953 preRenderItem( Inkscape::IconSize lsize, gchar const *name ) :
954 _lsize( lsize ),
955 _name( name )
956 {}
957 Inkscape::IconSize _lsize;
958 Glib::ustring _name;
959 };
962 #include <queue>
964 static std::queue<preRenderItem> pendingRenders;
965 static bool callbackHooked = false;
967 static void addPreRender( Inkscape::IconSize lsize, gchar const *name )
968 {
970 if ( !callbackHooked )
971 {
972 callbackHooked = true;
973 g_idle_add_full( G_PRIORITY_LOW, &icon_prerender_task, NULL, NULL );
974 }
976 pendingRenders.push(preRenderItem(lsize, name));
977 }
979 gboolean icon_prerender_task(gpointer /*data*/) {
980 if (!pendingRenders.empty()) {
981 bool workDone = false;
982 do {
983 preRenderItem single = pendingRenders.front();
984 pendingRenders.pop();
985 int psize = sp_icon_get_phys_size(single._lsize);
986 workDone = prerender_icon(single._name.c_str(), single._lsize, psize);
987 } while (!pendingRenders.empty() && !workDone);
988 }
990 if (!pendingRenders.empty()) {
991 return TRUE;
992 } else {
993 callbackHooked = false;
994 return FALSE;
995 }
996 }
998 void imageMapCB(GtkWidget* widget, gpointer user_data) {
999 gchar* id = 0;
1000 GtkIconSize size = GTK_ICON_SIZE_INVALID;
1001 gtk_image_get_stock(GTK_IMAGE(widget), &id, &size);
1002 if ( id ) {
1003 int psize = sp_icon_get_phys_size(size);
1004 prerender_icon(id, size, psize);
1006 std::vector<IconCacheItem>& iconSet = iconSetCache[id];
1007 for ( std::vector<IconCacheItem>::iterator it = iconSet.begin(); it != iconSet.end(); ++it ) {
1008 GObject* obj = G_OBJECT(it->_pb);
1009 if ( obj ) {
1010 }
1011 }
1012 }
1015 std::map<gpointer, gulong>::iterator it = mapHandlerMap.find(widget);
1017 if ( it != mapHandlerMap.end() ) {
1018 gulong handlerId = it->second;
1019 if ( g_signal_handler_is_connected(widget, handlerId) ) {
1020 g_signal_handler_disconnect(widget, handlerId);
1021 }
1022 mapHandlerMap.erase(it);
1023 }
1024 }
1027 /*
1028 Local Variables:
1029 mode:c++
1030 c-file-style:"stroustrup"
1031 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1032 indent-tabs-mode:nil
1033 fill-column:99
1034 End:
1035 */
1036 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :