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 }
145 static void
146 sp_icon_dispose(GObject *object)
147 {
148 SPIcon *icon = SP_ICON(object);
149 sp_icon_clear(icon);
150 if ( icon->name ) {
151 g_free( icon->name );
152 icon->name = 0;
153 }
155 ((GObjectClass *) (parent_class))->dispose(object);
156 }
158 static void sp_icon_reset( SPIcon *icon ) {
159 icon->psize = 0;
160 sp_icon_clear(icon);
161 }
163 static void sp_icon_clear( SPIcon *icon ) {
164 if (icon->pb) {
165 g_object_unref(G_OBJECT(icon->pb));
166 icon->pb = NULL;
167 }
168 }
170 static void
171 sp_icon_size_request(GtkWidget *widget, GtkRequisition *requisition)
172 {
173 SPIcon const *icon = SP_ICON(widget);
175 int const size = ( icon->psize
176 ? icon->psize
177 : sp_icon_get_phys_size(icon->lsize) );
178 requisition->width = size;
179 requisition->height = size;
180 }
182 static void
183 sp_icon_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
184 {
185 widget->allocation = *allocation;
187 if (GTK_WIDGET_DRAWABLE(widget)) {
188 gtk_widget_queue_draw(widget);
189 }
190 }
192 static int sp_icon_expose(GtkWidget *widget, GdkEventExpose *event)
193 {
194 if ( GTK_WIDGET_DRAWABLE(widget) ) {
195 SPIcon *icon = SP_ICON(widget);
196 if ( !icon->pb ) {
197 sp_icon_fetch_pixbuf( icon );
198 }
200 sp_icon_paint(SP_ICON(widget), &event->area);
201 }
203 return TRUE;
204 }
206 // PUBLIC CALL:
207 void sp_icon_fetch_pixbuf( SPIcon *icon )
208 {
209 if ( icon ) {
210 if ( !icon->pb ) {
211 icon->psize = sp_icon_get_phys_size(icon->lsize);
212 GtkIconTheme *theme = gtk_icon_theme_get_default();
214 GdkPixbuf *pb = 0;
215 if (gtk_icon_theme_has_icon(theme, icon->name)) {
216 pb = gtk_icon_theme_load_icon(theme, icon->name, icon->psize, (GtkIconLookupFlags) 0, NULL);
217 }
218 if (!pb) {
219 pb = sp_icon_image_load_svg( icon->name, Inkscape::getRegisteredIconSize(icon->lsize), icon->psize );
220 // if this was loaded from SVG, add it as a builtin icon
221 if (pb) {
222 gtk_icon_theme_add_builtin_icon(icon->name, icon->psize, pb);
223 }
224 }
225 if (!pb) {
226 pb = sp_icon_image_load_pixmap( icon->name, icon->lsize, icon->psize );
227 }
228 if ( pb ) {
229 icon->pb = pb;
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 Glib::ustring icon_cache_key(gchar const *name, unsigned lsize, unsigned psize);
266 static GdkPixbuf *get_cached_pixbuf(Glib::ustring const &key);
268 std::map<Glib::ustring, Glib::ustring> legacyNames;
270 static void setupLegacyNaming() {
271 legacyNames["view-fullscreen"] = "fullscreen";
272 legacyNames["edit-select-all"] = "selection_select_all";
273 legacyNames["window-new"] = "view_new";
274 }
276 static GtkWidget *
277 sp_icon_new_full( Inkscape::IconSize lsize, gchar const *name )
278 {
279 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
280 static bool dump = prefs->getBool( "/debug/icons/dumpGtk");
282 GtkWidget *widget = 0;
283 gint trySize = CLAMP( static_cast<gint>(lsize), 0, static_cast<gint>(G_N_ELEMENTS(iconSizeLookup) - 1) );
284 if ( !sizeMapDone ) {
285 injectCustomSize();
286 }
287 GtkIconSize mappedSize = iconSizeLookup[trySize];
289 GtkStockItem stock;
290 gboolean stockFound = gtk_stock_lookup( name, &stock );
292 GtkWidget *img = 0;
293 if ( legacyNames.empty() ) {
294 setupLegacyNaming();
295 }
297 if ( stockFound ) {
298 img = gtk_image_new_from_stock( name, mappedSize );
299 }
300 else if ( legacyNames.find(name) != legacyNames.end() ) {
301 img = gtk_image_new_from_icon_name( name, mappedSize );
302 if ( dump ) {
303 g_message("gtk_image_new_from_icon_name( '%s', %d ) = %p", name, mappedSize, img);
304 GtkImageType thing = gtk_image_get_storage_type(GTK_IMAGE(img));
305 g_message(" Type is %d %s", (int)thing, (thing == GTK_IMAGE_EMPTY ? "Empty" : "ok"));
306 }
307 }
309 if ( img ) {
310 GtkImageType type = gtk_image_get_storage_type( GTK_IMAGE(img) );
311 if ( type == GTK_IMAGE_STOCK ) {
312 if ( !stockFound ) {
313 // It's not showing as a stock ID, so assume it will be present internally
314 // TODO restore: populate_placeholder_icon( name, mappedSize );
315 // TODO restore: addPreRender( mappedSize, name );
317 // Add a hook to render if set visible before prerender is done.
318 // TODO restore g_signal_connect( G_OBJECT(img), "map", G_CALLBACK(imageMapCB), GINT_TO_POINTER(static_cast<int>(mappedSize)) );
319 if ( dump ) {
320 g_message(" connecting %p for imageMapCB for [%s] %d", img, name, (int)mappedSize);
321 }
322 }
323 widget = GTK_WIDGET(img);
324 img = 0;
325 if ( dump ) {
326 g_message( "loaded gtk '%s' %d (GTK_IMAGE_STOCK) %s on %p", name, mappedSize, (stockFound ? "STOCK" : "local"), widget );
327 }
328 } else if ( type == GTK_IMAGE_ICON_NAME ) {
329 widget = GTK_WIDGET(img);
330 img = 0;
332 // Add a hook to render if set visible before prerender is done.
333 // TODO restore g_signal_connect( G_OBJECT(widget), "map", G_CALLBACK(imageMapNamedCB), GINT_TO_POINTER(0) );
335 if ( prefs->getBool("/options/iconrender/named_nodelay") ) {
336 // TODO restore int psize = sp_icon_get_phys_size(lsize);
337 // TODO restore prerender_icon(name, mappedSize, psize);
338 } else {
339 // TODO restore addPreRender( mappedSize, name );
340 }
341 } else {
342 if ( dump ) {
343 g_message( "skipped gtk '%s' %d (not GTK_IMAGE_STOCK)", name, lsize );
344 }
345 //g_object_unref( (GObject *)img );
346 img = 0;
347 }
348 }
350 if ( !widget ) {
351 //g_message("Creating an SPIcon instance for %s:%d", name, (int)lsize);
352 SPIcon *icon = (SPIcon *)g_object_new(SP_TYPE_ICON, NULL);
353 icon->lsize = lsize;
354 icon->name = g_strdup(name);
355 icon->psize = sp_icon_get_phys_size(lsize);
357 widget = GTK_WIDGET(icon);
358 }
360 return widget;
361 }
363 GtkWidget *
364 sp_icon_new( Inkscape::IconSize lsize, gchar const *name )
365 {
366 return sp_icon_new_full( lsize, name );
367 }
369 // PUBLIC CALL:
370 Gtk::Widget *sp_icon_get_icon( Glib::ustring const &oid, Inkscape::IconSize size )
371 {
372 Gtk::Widget *result = 0;
373 GtkWidget *widget = sp_icon_new_full( static_cast<Inkscape::IconSize>(Inkscape::getRegisteredIconSize(size)), oid.c_str() );
375 if ( widget ) {
376 if ( GTK_IS_IMAGE(widget) ) {
377 GtkImage *img = GTK_IMAGE(widget);
378 result = Glib::wrap( img );
379 } else {
380 result = Glib::wrap( widget );
381 }
382 }
384 return result;
385 }
387 GtkIconSize
388 sp_icon_get_gtk_size(int size)
389 {
390 static GtkIconSize sizemap[64] = {(GtkIconSize)0};
391 size = CLAMP(size, 4, 63);
392 if (!sizemap[size]) {
393 static int count = 0;
394 char c[64];
395 g_snprintf(c, 64, "InkscapeIcon%d", count++);
396 sizemap[size] = gtk_icon_size_register(c, size, size);
397 }
398 return sizemap[size];
399 }
402 static void injectCustomSize()
403 {
404 // TODO - still need to handle the case of theme changes and resize, especially as we can't re-register a string.
405 if ( !sizeMapDone )
406 {
407 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
408 bool dump = prefs->getBool( "/debug/icons/dumpDefault");
409 gint width = 0;
410 gint height = 0;
411 if ( gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &width, &height ) ) {
412 gint newWidth = ((width * 3) / 4);
413 gint newHeight = ((height * 3) / 4);
414 GtkIconSize newSizeEnum = gtk_icon_size_register( "inkscape-decoration", newWidth, newHeight );
415 if ( newSizeEnum ) {
416 if ( dump ) {
417 g_message("Registered (%d, %d) <= (%d, %d) as index %d", newWidth, newHeight, width, height, newSizeEnum);
418 }
419 guint index = static_cast<guint>(Inkscape::ICON_SIZE_DECORATION);
420 if ( index < G_N_ELEMENTS(iconSizeLookup) ) {
421 iconSizeLookup[index] = newSizeEnum;
422 } else if ( dump ) {
423 g_message("size lookup array too small to store entry");
424 }
425 }
426 }
427 sizeMapDone = true;
428 }
430 static bool hit = false;
431 if ( !hit ) {
432 hit = true;
433 inkyIcons = Gtk::IconFactory::create();
434 inkyIcons->add_default();
435 }
436 }
438 GtkIconSize Inkscape::getRegisteredIconSize( Inkscape::IconSize size )
439 {
440 GtkIconSize other = GTK_ICON_SIZE_MENU;
441 injectCustomSize();
442 size = CLAMP( size, Inkscape::ICON_SIZE_MENU, Inkscape::ICON_SIZE_DECORATION );
443 if ( size == Inkscape::ICON_SIZE_DECORATION ) {
444 other = gtk_icon_size_from_name("inkscape-decoration");
445 } else {
446 other = static_cast<GtkIconSize>(size);
447 }
449 return other;
450 }
453 // PUBLIC CALL:
454 int sp_icon_get_phys_size(int size)
455 {
456 static bool init = false;
457 static int lastSys[Inkscape::ICON_SIZE_DECORATION + 1];
458 static int vals[Inkscape::ICON_SIZE_DECORATION + 1];
460 size = CLAMP( size, GTK_ICON_SIZE_MENU, Inkscape::ICON_SIZE_DECORATION );
462 if ( !sizeMapDone ) {
463 injectCustomSize();
464 }
466 if ( sizeDirty && init ) {
467 GtkIconSize const gtkSizes[] = {
468 GTK_ICON_SIZE_MENU,
469 GTK_ICON_SIZE_SMALL_TOOLBAR,
470 GTK_ICON_SIZE_LARGE_TOOLBAR,
471 GTK_ICON_SIZE_BUTTON,
472 GTK_ICON_SIZE_DND,
473 GTK_ICON_SIZE_DIALOG,
474 static_cast<guint>(Inkscape::ICON_SIZE_DECORATION) < G_N_ELEMENTS(iconSizeLookup) ?
475 iconSizeLookup[static_cast<int>(Inkscape::ICON_SIZE_DECORATION)] :
476 GTK_ICON_SIZE_MENU
477 };
478 for (unsigned i = 0; i < G_N_ELEMENTS(gtkSizes) && init; ++i) {
479 guint const val_ix = (gtkSizes[i] <= GTK_ICON_SIZE_DIALOG) ? (guint)gtkSizes[i] : (guint)Inkscape::ICON_SIZE_DECORATION;
481 g_assert( val_ix < G_N_ELEMENTS(vals) );
483 gint width = 0;
484 gint height = 0;
485 if ( gtk_icon_size_lookup(gtkSizes[i], &width, &height ) ) {
486 init &= (lastSys[val_ix] == std::max(width, height));
487 }
488 }
489 }
491 if ( !init ) {
492 sizeDirty = false;
493 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
494 bool dump = prefs->getBool("/debug/icons/dumpDefault");
496 if ( dump ) {
497 g_message( "Default icon sizes:" );
498 }
499 memset( vals, 0, sizeof(vals) );
500 memset( lastSys, 0, sizeof(lastSys) );
501 GtkIconSize const gtkSizes[] = {
502 GTK_ICON_SIZE_MENU,
503 GTK_ICON_SIZE_SMALL_TOOLBAR,
504 GTK_ICON_SIZE_LARGE_TOOLBAR,
505 GTK_ICON_SIZE_BUTTON,
506 GTK_ICON_SIZE_DND,
507 GTK_ICON_SIZE_DIALOG,
508 static_cast<guint>(Inkscape::ICON_SIZE_DECORATION) < G_N_ELEMENTS(iconSizeLookup) ?
509 iconSizeLookup[static_cast<int>(Inkscape::ICON_SIZE_DECORATION)] :
510 GTK_ICON_SIZE_MENU
511 };
512 gchar const *const names[] = {
513 "GTK_ICON_SIZE_MENU",
514 "GTK_ICON_SIZE_SMALL_TOOLBAR",
515 "GTK_ICON_SIZE_LARGE_TOOLBAR",
516 "GTK_ICON_SIZE_BUTTON",
517 "GTK_ICON_SIZE_DND",
518 "GTK_ICON_SIZE_DIALOG",
519 "inkscape-decoration"
520 };
522 GtkWidget *icon = (GtkWidget *)g_object_new(SP_TYPE_ICON, NULL);
524 for (unsigned i = 0; i < G_N_ELEMENTS(gtkSizes); ++i) {
525 guint const val_ix = (gtkSizes[i] <= GTK_ICON_SIZE_DIALOG) ? (guint)gtkSizes[i] : (guint)Inkscape::ICON_SIZE_DECORATION;
527 g_assert( val_ix < G_N_ELEMENTS(vals) );
529 gint width = 0;
530 gint height = 0;
531 bool used = false;
532 if ( gtk_icon_size_lookup(gtkSizes[i], &width, &height ) ) {
533 vals[val_ix] = std::max(width, height);
534 lastSys[val_ix] = vals[val_ix];
535 used = true;
536 }
537 if (dump) {
538 g_message(" =-- %u size:%d %c(%d, %d) '%s'",
539 i, gtkSizes[i],
540 ( used ? ' ' : 'X' ), width, height, names[i]);
541 }
542 gchar const *id = GTK_STOCK_OPEN;
543 GdkPixbuf *pb = gtk_widget_render_icon( icon, id, gtkSizes[i], NULL);
544 if (pb) {
545 width = gdk_pixbuf_get_width(pb);
546 height = gdk_pixbuf_get_height(pb);
547 int newSize = std::max( width, height );
548 // TODO perhaps check a few more stock icons to get a range on sizes.
549 if ( newSize > 0 ) {
550 vals[val_ix] = newSize;
551 }
552 if (dump) {
553 g_message(" %u size:%d (%d, %d)", i, gtkSizes[i], width, height);
554 }
556 g_object_unref(G_OBJECT(pb));
557 }
558 }
559 //g_object_unref(icon);
560 init = true;
561 }
563 // Fixup workaround
564 if ((size == GTK_ICON_SIZE_MENU) || (size == GTK_ICON_SIZE_SMALL_TOOLBAR) || (size == GTK_ICON_SIZE_LARGE_TOOLBAR)) {
565 gint width = 0;
566 gint height = 0;
567 if ( gtk_icon_size_lookup( static_cast<GtkIconSize>(size), &width, &height ) ) {
568 vals[size] = std::max( width, height );
569 }
570 }
572 return vals[size];
573 }
575 static void sp_icon_paint(SPIcon *icon, GdkRectangle const */*area*/)
576 {
577 GtkWidget &widget = *GTK_WIDGET(icon);
578 GdkPixbuf *image = icon->pb;
579 bool unref_image = false;
581 /* copied from the expose function of GtkImage */
582 if (GTK_WIDGET_STATE (icon) != GTK_STATE_NORMAL && image) {
583 GtkIconSource *source = gtk_icon_source_new();
584 gtk_icon_source_set_pixbuf(source, icon->pb);
585 gtk_icon_source_set_size(source, GTK_ICON_SIZE_SMALL_TOOLBAR); // note: this is boilerplate and not used
586 gtk_icon_source_set_size_wildcarded(source, FALSE);
587 image = gtk_style_render_icon (widget.style, source, gtk_widget_get_direction(&widget),
588 (GtkStateType) GTK_WIDGET_STATE(&widget), (GtkIconSize)-1, &widget, "gtk-image");
589 gtk_icon_source_free(source);
590 unref_image = true;
591 }
593 if (image) {
594 int x = floor(widget.allocation.x + ((widget.allocation.width - widget.requisition.width) * 0.5));
595 int y = floor(widget.allocation.y + ((widget.allocation.height - widget.requisition.height) * 0.5));
596 int width = gdk_pixbuf_get_width(image);
597 int height = gdk_pixbuf_get_height(image);
598 // Limit drawing to when we actually have something. Avoids some crashes.
599 if ( (width > 0) && (height > 0) ) {
600 gdk_draw_pixbuf(GDK_DRAWABLE(widget.window), widget.style->black_gc, image,
601 0, 0, x, y, width, height,
602 GDK_RGB_DITHER_NORMAL, x, y);
603 }
604 }
606 if (unref_image) {
607 g_object_unref(G_OBJECT(image));
608 }
609 }
611 GdkPixbuf *sp_icon_image_load_pixmap(gchar const *name, unsigned /*lsize*/, unsigned psize)
612 {
613 gchar *path = (gchar *) g_strdup_printf("%s/%s.png", INKSCAPE_PIXMAPDIR, name);
614 // TODO: bulia, please look over
615 gsize bytesRead = 0;
616 gsize bytesWritten = 0;
617 GError *error = NULL;
618 gchar *localFilename = g_filename_from_utf8( path,
619 -1,
620 &bytesRead,
621 &bytesWritten,
622 &error);
623 GdkPixbuf *pb = gdk_pixbuf_new_from_file(localFilename, NULL);
624 g_free(localFilename);
625 g_free(path);
626 if (!pb) {
627 path = (gchar *) g_strdup_printf("%s/%s.xpm", INKSCAPE_PIXMAPDIR, name);
628 // TODO: bulia, please look over
629 gsize bytesRead = 0;
630 gsize bytesWritten = 0;
631 GError *error = NULL;
632 gchar *localFilename = g_filename_from_utf8( path,
633 -1,
634 &bytesRead,
635 &bytesWritten,
636 &error);
637 pb = gdk_pixbuf_new_from_file(localFilename, NULL);
638 g_free(localFilename);
639 g_free(path);
640 }
642 if (pb) {
643 if (!gdk_pixbuf_get_has_alpha(pb)) {
644 gdk_pixbuf_add_alpha(pb, FALSE, 0, 0, 0);
645 }
647 if ( ( static_cast<unsigned>(gdk_pixbuf_get_width(pb)) != psize )
648 || ( static_cast<unsigned>(gdk_pixbuf_get_height(pb)) != psize ) ) {
649 GdkPixbuf *spb = gdk_pixbuf_scale_simple(pb, psize, psize, GDK_INTERP_HYPER);
650 g_object_unref(G_OBJECT(pb));
651 pb = spb;
652 }
653 }
655 return pb;
656 }
658 // takes doc, root, icon, and icon name to produce pixels
659 extern "C" guchar *
660 sp_icon_doc_icon( SPDocument *doc, NRArenaItem *root,
661 gchar const *name, unsigned psize )
662 {
663 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
664 bool const dump = prefs->getBool("/debug/icons/dumpSvg");
665 guchar *px = NULL;
667 if (doc) {
668 SPObject *object = doc->getObjectById(name);
669 if (object && SP_IS_ITEM(object)) {
670 /* Find bbox in document */
671 Geom::Matrix const i2doc(sp_item_i2doc_affine(SP_ITEM(object)));
672 Geom::OptRect dbox = SP_ITEM(object)->getBounds(i2doc);
674 if ( SP_OBJECT_PARENT(object) == NULL )
675 {
676 dbox = Geom::Rect(Geom::Point(0, 0),
677 Geom::Point(sp_document_width(doc), sp_document_height(doc)));
678 }
680 /* This is in document coordinates, i.e. pixels */
681 if ( dbox ) {
682 NRGC gc(NULL);
683 /* Update to renderable state */
684 double sf = 1.0;
685 nr_arena_item_set_transform(root, (Geom::Matrix)Geom::Scale(sf, sf));
686 gc.transform.setIdentity();
687 nr_arena_item_invoke_update( root, NULL, &gc,
688 NR_ARENA_ITEM_STATE_ALL,
689 NR_ARENA_ITEM_STATE_NONE );
690 /* Item integer bbox in points */
691 NRRectL ibox;
692 ibox.x0 = (int) floor(sf * dbox->min()[Geom::X] + 0.5);
693 ibox.y0 = (int) floor(sf * dbox->min()[Geom::Y] + 0.5);
694 ibox.x1 = (int) floor(sf * dbox->max()[Geom::X] + 0.5);
695 ibox.y1 = (int) floor(sf * dbox->max()[Geom::Y] + 0.5);
697 if ( dump ) {
698 g_message( " box --'%s' (%f,%f)-(%f,%f)", name, (double)ibox.x0, (double)ibox.y0, (double)ibox.x1, (double)ibox.y1 );
699 }
701 /* Find button visible area */
702 int width = ibox.x1 - ibox.x0;
703 int height = ibox.y1 - ibox.y0;
705 if ( dump ) {
706 g_message( " vis --'%s' (%d,%d)", name, width, height );
707 }
709 {
710 int block = std::max(width, height);
711 if (block != static_cast<int>(psize) ) {
712 if ( dump ) {
713 g_message(" resizing" );
714 }
715 sf = (double)psize / (double)block;
717 nr_arena_item_set_transform(root, (Geom::Matrix)Geom::Scale(sf, sf));
718 gc.transform.setIdentity();
719 nr_arena_item_invoke_update( root, NULL, &gc,
720 NR_ARENA_ITEM_STATE_ALL,
721 NR_ARENA_ITEM_STATE_NONE );
722 /* Item integer bbox in points */
723 ibox.x0 = (int) floor(sf * dbox->min()[Geom::X] + 0.5);
724 ibox.y0 = (int) floor(sf * dbox->min()[Geom::Y] + 0.5);
725 ibox.x1 = (int) floor(sf * dbox->max()[Geom::X] + 0.5);
726 ibox.y1 = (int) floor(sf * dbox->max()[Geom::Y] + 0.5);
728 if ( dump ) {
729 g_message( " box2 --'%s' (%f,%f)-(%f,%f)", name, (double)ibox.x0, (double)ibox.y0, (double)ibox.x1, (double)ibox.y1 );
730 }
732 /* Find button visible area */
733 width = ibox.x1 - ibox.x0;
734 height = ibox.y1 - ibox.y0;
735 if ( dump ) {
736 g_message( " vis2 --'%s' (%d,%d)", name, width, height );
737 }
738 }
739 }
741 int dx, dy;
742 //dx = (psize - width) / 2;
743 //dy = (psize - height) / 2;
744 dx=dy=psize;
745 dx=(dx-width)/2; // watch out for psize, since 'unsigned'-'signed' can cause problems if the result is negative
746 dy=(dy-height)/2;
747 NRRectL area;
748 area.x0 = ibox.x0 - dx;
749 area.y0 = ibox.y0 - dy;
750 area.x1 = area.x0 + psize;
751 area.y1 = area.y0 + psize;
752 /* Actual renderable area */
753 NRRectL ua;
754 ua.x0 = MAX(ibox.x0, area.x0);
755 ua.y0 = MAX(ibox.y0, area.y0);
756 ua.x1 = MIN(ibox.x1, area.x1);
757 ua.y1 = MIN(ibox.y1, area.y1);
759 if ( dump ) {
760 g_message( " area --'%s' (%f,%f)-(%f,%f)", name, (double)area.x0, (double)area.y0, (double)area.x1, (double)area.y1 );
761 g_message( " ua --'%s' (%f,%f)-(%f,%f)", name, (double)ua.x0, (double)ua.y0, (double)ua.x1, (double)ua.y1 );
762 }
763 /* Set up pixblock */
764 px = g_new(guchar, 4 * psize * psize);
765 memset(px, 0x00, 4 * psize * psize);
766 /* Render */
767 NRPixBlock B;
768 nr_pixblock_setup_extern( &B, NR_PIXBLOCK_MODE_R8G8B8A8N,
769 ua.x0, ua.y0, ua.x1, ua.y1,
770 px + 4 * psize * (ua.y0 - area.y0) +
771 4 * (ua.x0 - area.x0),
772 4 * psize, FALSE, FALSE );
773 nr_arena_item_invoke_render(NULL, root, &ua, &B,
774 NR_ARENA_ITEM_RENDER_NO_CACHE );
775 nr_pixblock_release(&B);
777 bool useOverlay = prefs->getBool("/debug/icons/overlaySvg");
778 if ( useOverlay ) {
779 sp_icon_overlay_pixels( px, psize, psize, 4 * psize, 0x00, 0x00, 0xff );
780 }
781 }
782 }
783 }
785 return px;
786 } // end of sp_icon_doc_icon()
790 struct svg_doc_cache_t
791 {
792 SPDocument *doc;
793 NRArenaItem *root;
794 };
796 static std::map<Glib::ustring, svg_doc_cache_t *> doc_cache;
797 static std::map<Glib::ustring, GdkPixbuf *> pb_cache;
799 Glib::ustring icon_cache_key(gchar const *name,
800 unsigned lsize, unsigned psize)
801 {
802 Glib::ustring key=name;
803 key += ":";
804 key += lsize;
805 key += ":";
806 key += psize;
807 return key;
808 }
810 GdkPixbuf *get_cached_pixbuf(Glib::ustring const &key) {
811 std::map<Glib::ustring, GdkPixbuf *>::iterator found = pb_cache.find(key);
812 if ( found != pb_cache.end() ) {
813 return found->second;
814 }
815 return NULL;
816 }
818 static std::list<gchar*> &icons_svg_paths()
819 {
820 static std::list<gchar *> sources;
821 static bool initialized = false;
822 if (!initialized) {
823 // Fall back from user prefs dir into system locations.
824 gchar *userdir = profile_path("icons");
825 sources.push_back(g_build_filename(userdir,"icons.svg", NULL));
826 sources.push_back(g_build_filename(INKSCAPE_PIXMAPDIR, "icons.svg", NULL));
827 g_free(userdir);
828 initialized = true;
829 }
830 return sources;
831 }
833 // this function renders icons from icons.svg and returns the pixels.
834 static guchar *load_svg_pixels(gchar const *name,
835 unsigned /*lsize*/, unsigned psize)
836 {
837 SPDocument *doc = NULL;
838 NRArenaItem *root = NULL;
839 svg_doc_cache_t *info = NULL;
841 std::list<gchar *> &sources = icons_svg_paths();
843 // Try each document in turn until we successfully load the icon from one
844 guchar *px=NULL;
845 for (std::list<gchar*>::iterator i = sources.begin(); i != sources.end() && !px; ++i) {
846 gchar *doc_filename = *i;
848 // Did we already load this doc?
849 Glib::ustring key(doc_filename);
850 info = 0;
851 {
852 std::map<Glib::ustring, svg_doc_cache_t *>::iterator i = doc_cache.find(key);
853 if ( i != doc_cache.end() ) {
854 info = i->second;
855 }
856 }
858 /* Try to load from document. */
859 if (!info &&
860 Inkscape::IO::file_test( doc_filename, G_FILE_TEST_IS_REGULAR ) &&
861 (doc = sp_document_new( doc_filename, FALSE )) ) {
863 //g_message("Loaded icon file %s", doc_filename);
864 // prep the document
865 sp_document_ensure_up_to_date(doc);
866 /* Create new arena */
867 NRArena *arena = NRArena::create();
868 /* Create ArenaItem and set transform */
869 unsigned visionkey = sp_item_display_key_new(1);
870 /* fixme: Memory manage root if needed (Lauris) */
871 root = sp_item_invoke_show( SP_ITEM(SP_DOCUMENT_ROOT(doc)),
872 arena, visionkey, SP_ITEM_SHOW_DISPLAY );
874 // store into the cache
875 info = new svg_doc_cache_t;
876 g_assert(info);
878 info->doc=doc;
879 info->root=root;
880 doc_cache[key]=info;
881 }
882 if (info) {
883 doc=info->doc;
884 root=info->root;
885 }
887 // move on to the next document if we couldn't get anything
888 if (!info && !doc) {
889 continue;
890 }
892 px = sp_icon_doc_icon( doc, root, name, psize );
893 // if (px) {
894 // g_message("Found icon %s in %s", name, doc_filename);
895 // }
896 }
898 // if (!px) {
899 // g_message("Not found icon %s", name);
900 // }
901 return px;
902 }
904 static GdkPixbuf *sp_icon_image_load_svg(gchar const *name, GtkIconSize lsize, unsigned psize)
905 {
906 Glib::ustring key = icon_cache_key(name, lsize, psize);
908 // did we already load this icon at this scale/size?
909 GdkPixbuf* pb = get_cached_pixbuf(key);
910 if (!pb) {
911 guchar *px = load_svg_pixels(name, lsize, psize);
912 if (px) {
913 pb = gdk_pixbuf_new_from_data(px, GDK_COLORSPACE_RGB, TRUE, 8,
914 psize, psize, psize * 4,
915 (GdkPixbufDestroyNotify)g_free, NULL);
916 pb_cache[key] = pb;
917 }
918 }
920 if ( pb ) {
921 // increase refcount since we're handing out ownership
922 g_object_ref(G_OBJECT(pb));
923 }
924 return pb;
925 }
927 void sp_icon_overlay_pixels(guchar *px, int width, int height, int stride,
928 unsigned r, unsigned g, unsigned b)
929 {
930 int bytesPerPixel = 4;
931 int spacing = 4;
932 for ( int y = 0; y < height; y += spacing ) {
933 guchar *ptr = px + y * stride;
934 for ( int x = 0; x < width; x += spacing ) {
935 *(ptr++) = r;
936 *(ptr++) = g;
937 *(ptr++) = b;
938 *(ptr++) = 0xff;
940 ptr += bytesPerPixel * (spacing - 1);
941 }
942 }
944 if ( width > 1 && height > 1 ) {
945 // point at the last pixel
946 guchar *ptr = px + ((height-1) * stride) + ((width - 1) * bytesPerPixel);
948 if ( width > 2 ) {
949 px[4] = r;
950 px[5] = g;
951 px[6] = b;
952 px[7] = 0xff;
954 ptr[-12] = r;
955 ptr[-11] = g;
956 ptr[-10] = b;
957 ptr[-9] = 0xff;
958 }
960 ptr[-4] = r;
961 ptr[-3] = g;
962 ptr[-2] = b;
963 ptr[-1] = 0xff;
965 px[0 + stride] = r;
966 px[1 + stride] = g;
967 px[2 + stride] = b;
968 px[3 + stride] = 0xff;
970 ptr[0 - stride] = r;
971 ptr[1 - stride] = g;
972 ptr[2 - stride] = b;
973 ptr[3 - stride] = 0xff;
975 if ( height > 2 ) {
976 ptr[0 - stride * 3] = r;
977 ptr[1 - stride * 3] = g;
978 ptr[2 - stride * 3] = b;
979 ptr[3 - stride * 3] = 0xff;
980 }
981 }
982 }
985 /*
986 Local Variables:
987 mode:c++
988 c-file-style:"stroustrup"
989 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
990 indent-tabs-mode:nil
991 fill-column:99
992 End:
993 */
994 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :