X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fwidgets%2Ficon.cpp;h=d8d68461440cdd645711b0c514bbe62c62fa1476;hb=b947b86feae33a7df233d6818a0b121611127cad;hp=ad0646e5a2d821b8922153eadf62c0057aa9ebf9;hpb=3ae65f587538152711e15a44012a30cb385e3443;p=inkscape.git diff --git a/src/widgets/icon.cpp b/src/widgets/icon.cpp index ad0646e5a..d8d684614 100644 --- a/src/widgets/icon.cpp +++ b/src/widgets/icon.cpp @@ -5,6 +5,7 @@ * Author: * Lauris Kaplinski * Jon A. Cruz + * Abhishek Sharma * * Copyright (C) 2002 Lauris Kaplinski * @@ -19,6 +20,7 @@ #include #include #include +#include #include "path-prefix.h" #include "preferences.h" @@ -31,6 +33,10 @@ #include "icon.h" +static gboolean icon_prerender_task(gpointer data); + +static void addPreRender( GtkIconSize lsize, gchar const *name ); + static void sp_icon_class_init(SPIconClass *klass); static void sp_icon_init(SPIcon *icon); static void sp_icon_dispose(GObject *object); @@ -72,6 +78,8 @@ static GtkIconSize iconSizeLookup[] = { GTK_ICON_SIZE_MENU, // for Inkscape::ICON_SIZE_DECORATION }; +static std::map legacyNames; + class IconCacheItem { public: @@ -83,15 +91,12 @@ public: GdkPixbuf* _pb; }; -static Glib::RefPtr inkyIcons; +static std::map > iconSetCache; +static std::set internalNames; GType sp_icon_get_type() { - //TODO: switch to GObject - // GtkType and such calls were deprecated a while back with the - // introduction of GObject as a separate layer, with GType instead. --JonCruz - static GType type = 0; if (!type) { GTypeInfo info = { @@ -203,34 +208,48 @@ static int sp_icon_expose(GtkWidget *widget, GdkEventExpose *event) return TRUE; } +static GdkPixbuf* renderup( gchar const* name, Inkscape::IconSize lsize, unsigned psize ); + // PUBLIC CALL: void sp_icon_fetch_pixbuf( SPIcon *icon ) { if ( icon ) { if ( !icon->pb ) { icon->psize = sp_icon_get_phys_size(icon->lsize); - GtkIconTheme *theme = gtk_icon_theme_get_default(); + icon->pb = renderup(icon->name, icon->lsize, icon->psize); + } + } +} - GdkPixbuf *pb = NULL; - if (gtk_icon_theme_has_icon(theme, icon->name)) { - pb = gtk_icon_theme_load_icon(theme, icon->name, icon->psize, (GtkIconLookupFlags) 0, NULL); - } - if (!pb) { - pb = sp_icon_image_load_svg( icon->name, Inkscape::getRegisteredIconSize(icon->lsize), icon->psize ); - // if this was loaded from SVG, add it as a builtin icon - if (pb) gtk_icon_theme_add_builtin_icon(icon->name, icon->psize, pb); - } - if (!pb) { - pb = sp_icon_image_load_pixmap( icon->name, icon->lsize, icon->psize ); - } - if ( pb ) { - icon->pb = pb; - } else { - /* TODO: We should do something more useful if we can't load the image. */ - g_warning ("failed to load icon '%s'", icon->name); +GdkPixbuf* renderup( gchar const* name, Inkscape::IconSize lsize, unsigned psize ) { + GtkIconTheme *theme = gtk_icon_theme_get_default(); + + GdkPixbuf *pb = 0; + if (gtk_icon_theme_has_icon(theme, name)) { + pb = gtk_icon_theme_load_icon(theme, name, psize, (GtkIconLookupFlags) 0, NULL); + } + if (!pb) { + pb = sp_icon_image_load_svg( name, Inkscape::getRegisteredIconSize(lsize), psize ); + if (!pb && (legacyNames.find(name) != legacyNames.end())) { + if ( Inkscape::Preferences::get()->getBool("/debug/icons/dumpSvg") ) { + g_message("Checking fallback [%s]->[%s]", name, legacyNames[name].c_str()); } + pb = sp_icon_image_load_svg( legacyNames[name].c_str(), Inkscape::getRegisteredIconSize(lsize), psize ); + } + + // if this was loaded from SVG, add it as a builtin icon + if (pb) { + gtk_icon_theme_add_builtin_icon(name, psize, pb); } } + if (!pb) { + pb = sp_icon_image_load_pixmap( name, lsize, psize ); + } + if ( !pb ) { + // TODO: We should do something more useful if we can't load the image. + g_warning ("failed to load icon '%s'", name); + } + return pb; } static void sp_icon_screen_changed( GtkWidget *widget, GdkScreen *previous_screen ) @@ -253,19 +272,281 @@ static void sp_icon_style_set( GtkWidget *widget, GtkStyle *previous_style ) static void sp_icon_theme_changed( SPIcon *icon ) { - //g_message("Got a change bump for this icon"); + bool const dump = Inkscape::Preferences::get()->getBool("/debug/icons/dumpSvg"); + if ( dump ) { + g_message("Got a change bump for this icon"); + } sizeDirty = true; sp_icon_reset(icon); gtk_widget_queue_draw( GTK_WIDGET(icon) ); } +static void imageMapCB(GtkWidget* widget, gpointer user_data); +static void imageMapNamedCB(GtkWidget* widget, gpointer user_data); +static bool prerender_icon(gchar const *name, GtkIconSize lsize, unsigned psize); static Glib::ustring icon_cache_key(gchar const *name, unsigned psize); static GdkPixbuf *get_cached_pixbuf(Glib::ustring const &key); -static GtkWidget * -sp_icon_new_full( Inkscape::IconSize lsize, gchar const *name ) +static void setupLegacyNaming() { + legacyNames["document-import"] ="file_import"; + legacyNames["document-export"] ="file_export"; + legacyNames["document-import-ocal"] ="ocal_import"; + legacyNames["document-export-ocal"] ="ocal_export"; + legacyNames["document-metadata"] ="document_metadata"; + legacyNames["dialog-input-devices"] ="input_devices"; + legacyNames["edit-duplicate"] ="edit_duplicate"; + legacyNames["edit-clone"] ="edit_clone"; + legacyNames["edit-clone-unlink"] ="edit_unlink_clone"; + legacyNames["edit-select-original"] ="edit_select_original"; + legacyNames["edit-undo-history"] ="edit_undo_history"; + legacyNames["edit-paste-in-place"] ="selection_paste_in_place"; + legacyNames["edit-paste-style"] ="selection_paste_style"; + legacyNames["selection-make-bitmap-copy"] ="selection_bitmap"; + legacyNames["edit-select-all"] ="selection_select_all"; + legacyNames["edit-select-all-layers"] ="selection_select_all_in_all_layers"; + legacyNames["edit-select-invert"] ="selection_invert"; + legacyNames["edit-select-none"] ="selection_deselect"; + legacyNames["dialog-xml-editor"] ="xml_editor"; + legacyNames["zoom-original"] ="zoom_1_to_1"; + legacyNames["zoom-half-size"] ="zoom_1_to_2"; + legacyNames["zoom-double-size"] ="zoom_2_to_1"; + legacyNames["zoom-fit-selection"] ="zoom_select"; + legacyNames["zoom-fit-drawing"] ="zoom_draw"; + legacyNames["zoom-fit-page"] ="zoom_page"; + legacyNames["zoom-fit-width"] ="zoom_pagewidth"; + legacyNames["zoom-previous"] ="zoom_previous"; + legacyNames["zoom-next"] ="zoom_next"; + legacyNames["zoom-in"] ="zoom_in"; + legacyNames["zoom-out"] ="zoom_out"; + legacyNames["show-grid"] ="grid"; + legacyNames["show-guides"] ="guides"; + legacyNames["color-management"] ="color_management"; + legacyNames["show-dialogs"] ="dialog_toggle"; + legacyNames["dialog-messages"] ="messages"; + legacyNames["dialog-scripts"] ="scripts"; + legacyNames["window-previous"] ="window_previous"; + legacyNames["window-next"] ="window_next"; + legacyNames["dialog-icon-preview"] ="view_icon_preview"; + legacyNames["window-new"] ="view_new"; + legacyNames["view-fullscreen"] ="fullscreen"; + legacyNames["layer-new"] ="new_layer"; + legacyNames["layer-rename"] ="rename_layer"; + legacyNames["layer-previous"] ="switch_to_layer_above"; + legacyNames["layer-next"] ="switch_to_layer_below"; + legacyNames["selection-move-to-layer-above"] ="move_selection_above"; + legacyNames["selection-move-to-layer-below"] ="move_selection_below"; + legacyNames["layer-raise"] ="raise_layer"; + legacyNames["layer-lower"] ="lower_layer"; + legacyNames["layer-top"] ="layer_to_top"; + legacyNames["layer-bottom"] ="layer_to_bottom"; + legacyNames["layer-delete"] ="delete_layer"; + legacyNames["dialog-layers"] ="layers"; + legacyNames["dialog-fill-and-stroke"] ="fill_and_stroke"; + legacyNames["dialog-object-properties"] ="dialog_item_properties"; + legacyNames["object-group"] ="selection_group"; + legacyNames["object-ungroup"] ="selection_ungroup"; + legacyNames["selection-raise"] ="selection_up"; + legacyNames["selection-lower"] ="selection_down"; + legacyNames["selection-top"] ="selection_top"; + legacyNames["selection-bottom"] ="selection_bot"; + legacyNames["object-rotate-left"] ="object_rotate_90_CCW"; + legacyNames["object-rotate-right"] ="object_rotate_90_CW"; + legacyNames["object-flip-horizontal"] ="object_flip_hor"; + legacyNames["object-flip-vertical"] ="object_flip_ver"; + legacyNames["dialog-transform"] ="object_trans"; + legacyNames["dialog-align-and-distribute"] ="object_align"; + legacyNames["dialog-rows-and-columns"] ="grid_arrange"; + legacyNames["object-to-path"] ="object_tocurve"; + legacyNames["stroke-to-path"] ="stroke_tocurve"; + legacyNames["bitmap-trace"] ="selection_trace"; + legacyNames["path-union"] ="union"; + legacyNames["path-difference"] ="difference"; + legacyNames["path-intersection"] ="intersection"; + legacyNames["path-exclusion"] ="exclusion"; + legacyNames["path-division"] ="division"; + legacyNames["path-cut"] ="cut_path"; + legacyNames["path-combine"] ="selection_combine"; + legacyNames["path-break-apart"] ="selection_break"; + legacyNames["path-outset"] ="outset_path"; + legacyNames["path-inset"] ="inset_path"; + legacyNames["path-offset-dynamic"] ="dynamic_offset"; + legacyNames["path-offset-linked"] ="linked_offset"; + legacyNames["path-simplify"] ="simplify"; + legacyNames["path-reverse"] ="selection_reverse"; + legacyNames["dialog-text-and-font"] ="object_font"; + legacyNames["text-put-on-path"] ="put_on_path"; + legacyNames["text-remove-from-path"] ="remove_from_path"; + legacyNames["text-flow-into-frame"] ="flow_into_frame"; + legacyNames["text-unflow"] ="unflow"; + legacyNames["text-convert-to-regular"] ="convert_to_text"; + legacyNames["text-unkern"] ="remove_manual_kerns"; + legacyNames["help-keyboard-shortcuts"] ="help_keys"; + legacyNames["help-contents"] ="help_tutorials"; + legacyNames["inkscape-logo"] ="inkscape_options"; + legacyNames["dialog-memory"] ="about_memory"; + legacyNames["tool-pointer"] ="draw_select"; + legacyNames["tool-node-editor"] ="draw_node"; + legacyNames["tool-tweak"] ="draw_tweak"; + legacyNames["zoom"] ="draw_zoom"; + legacyNames["draw-rectangle"] ="draw_rect"; + legacyNames["draw-cuboid"] ="draw_3dbox"; + legacyNames["draw-ellipse"] ="draw_arc"; + legacyNames["draw-polygon-star"] ="draw_star"; + legacyNames["draw-spiral"] ="draw_spiral"; + legacyNames["draw-freehand"] ="draw_freehand"; + legacyNames["draw-path"] ="draw_pen"; + legacyNames["draw-calligraphic"] ="draw_calligraphic"; + legacyNames["draw-eraser"] ="draw_erase"; + legacyNames["color-fill"] ="draw_paintbucket"; + legacyNames["draw-text"] ="draw_text"; + legacyNames["draw-connector"] ="draw_connector"; + legacyNames["color-gradient"] ="draw_gradient"; + legacyNames["color-picker"] ="draw_dropper"; + legacyNames["transform-affect-stroke"] ="transform_stroke"; + legacyNames["transform-affect-rounded-corners"] ="transform_corners"; + legacyNames["transform-affect-gradient"] ="transform_gradient"; + legacyNames["transform-affect-pattern"] ="transform_pattern"; + legacyNames["node-add"] ="node_insert"; + legacyNames["node-delete"] ="node_delete"; + legacyNames["node-join"] ="node_join"; + legacyNames["node-break"] ="node_break"; + legacyNames["node-join-segment"] ="node_join_segment"; + legacyNames["node-delete-segment"] ="node_delete_segment"; + legacyNames["node-type-cusp"] ="node_cusp"; + legacyNames["node-type-smooth"] ="node_smooth"; + legacyNames["node-type-symmetric"] ="node_symmetric"; + legacyNames["node-type-auto-smooth"] ="node_auto"; + legacyNames["node-segment-curve"] ="node_curve"; + legacyNames["node-segment-line"] ="node_line"; + legacyNames["show-node-handles"] ="nodes_show_handles"; + legacyNames["path-effect-parameter-next"] ="edit_next_parameter"; + legacyNames["show-path-outline"] ="nodes_show_helperpath"; + legacyNames["path-clip-edit"] ="nodeedit-clippath"; + legacyNames["path-mask-edit"] ="nodeedit-mask"; + legacyNames["node-type-cusp"] ="node_cusp"; + legacyNames["object-tweak-push"] ="tweak_move_mode"; + legacyNames["object-tweak-attract"] ="tweak_move_mode_inout"; + legacyNames["object-tweak-randomize"] ="tweak_move_mode_jitter"; + legacyNames["object-tweak-shrink"] ="tweak_scale_mode"; + legacyNames["object-tweak-rotate"] ="tweak_rotate_mode"; + legacyNames["object-tweak-duplicate"] ="tweak_moreless_mode"; + legacyNames["object-tweak-push"] ="tweak_move_mode"; + legacyNames["path-tweak-push"] ="tweak_push_mode"; + legacyNames["path-tweak-shrink"] ="tweak_shrink_mode"; + legacyNames["path-tweak-attract"] ="tweak_attract_mode"; + legacyNames["path-tweak-roughen"] ="tweak_roughen_mode"; + legacyNames["object-tweak-paint"] ="tweak_colorpaint_mode"; + legacyNames["object-tweak-jitter-color"] ="tweak_colorjitter_mode"; + legacyNames["object-tweak-blur"] ="tweak_blur_mode"; + legacyNames["rectangle-make-corners-sharp"] ="squared_corner"; + legacyNames["perspective-parallel"] ="toggle_vp_x"; + legacyNames["draw-ellipse-whole"] ="reset_circle"; + legacyNames["draw-ellipse-segment"] ="circle_closed_arc"; + legacyNames["draw-ellipse-arc"] ="circle_open_arc"; + legacyNames["draw-polygon"] ="star_flat"; + legacyNames["draw-star"] ="star_angled"; + legacyNames["path-mode-bezier"] ="bezier_mode"; + legacyNames["path-mode-spiro"] ="spiro_splines_mode"; + legacyNames["path-mode-polyline"] ="polylines_mode"; + legacyNames["path-mode-polyline-paraxial"] ="paraxial_lines_mode"; + legacyNames["draw-use-tilt"] ="guse_tilt"; + legacyNames["draw-use-pressure"] ="guse_pressure"; + legacyNames["draw-trace-background"] ="trace_background"; + legacyNames["draw-eraser-delete-objects"] ="delete_object"; + legacyNames["format-text-direction-vertical"] ="writing_mode_tb"; + legacyNames["format-text-direction-horizontal"] ="writing_mode_lr"; + legacyNames["connector-avoid"] ="connector_avoid"; + legacyNames["connector-ignore"] ="connector_ignore"; + legacyNames["object-fill"] ="controls_fill"; + legacyNames["object-stroke"] ="controls_stroke"; + legacyNames["snap"] ="toggle_snap_global"; + legacyNames["snap-bounding-box"] ="toggle_snap_bbox"; + legacyNames["snap-bounding-box-edges"] ="toggle_snap_to_bbox_path"; + legacyNames["snap-bounding-box-corners"] ="toggle_snap_to_bbox_node"; + legacyNames["snap-bounding-box-midpoints"] ="toggle_snap_to_bbox_edge_midpoints"; + legacyNames["snap-bounding-box-center"] ="toggle_snap_to_bbox_midpoints"; + legacyNames["snap-nodes"] ="toggle_snap_nodes"; + legacyNames["snap-nodes-path"] ="toggle_snap_to_paths"; + legacyNames["snap-nodes-cusp"] ="toggle_snap_to_nodes"; + legacyNames["snap-nodes-smooth"] ="toggle_snap_to_smooth_nodes"; + legacyNames["snap-nodes-midpoint"] ="toggle_snap_to_midpoints"; + legacyNames["snap-nodes-intersection"] ="toggle_snap_to_path_intersections"; + legacyNames["snap-nodes-center"] ="toggle_snap_to_bbox_midpoints-3"; + legacyNames["snap-nodes-rotation-center"] ="toggle_snap_center"; + legacyNames["snap-page"] ="toggle_snap_page_border"; + legacyNames["snap-grid-guide-intersections"] ="toggle_snap_grid_guide_intersections"; + legacyNames["align-horizontal-right-to-anchor"] ="al_left_out"; + legacyNames["align-horizontal-left"] ="al_left_in"; + legacyNames["align-horizontal-center"] ="al_center_hor"; + legacyNames["align-horizontal-right"] ="al_right_in"; + legacyNames["align-horizontal-left-to-anchor"] ="al_right_out"; + legacyNames["align-horizontal-baseline"] ="al_baselines_vert"; + legacyNames["align-vertical-bottom-to-anchor"] ="al_top_out"; + legacyNames["align-vertical-top"] ="al_top_in"; + legacyNames["align-vertical-center"] ="al_center_ver"; + legacyNames["align-vertical-bottom"] ="al_bottom_in"; + legacyNames["align-vertical-top-to-anchor"] ="al_bottom_out"; + legacyNames["align-vertical-baseline"] ="al_baselines_hor"; + legacyNames["distribute-horizontal-left"] ="distribute_left"; + legacyNames["distribute-horizontal-center"] ="distribute_hcentre"; + legacyNames["distribute-horizontal-right"] ="distribute_right"; + legacyNames["distribute-horizontal-baseline"] ="distribute_baselines_hor"; + legacyNames["distribute-vertical-bottom"] ="distribute_bottom"; + legacyNames["distribute-vertical-center"] ="distribute_vcentre"; + legacyNames["distribute-vertical-top"] ="distribute_top"; + legacyNames["distribute-vertical-baseline"] ="distribute_baselines_vert"; + legacyNames["distribute-randomize"] ="distribute_randomize"; + legacyNames["distribute-unclump"] ="unclump"; + legacyNames["distribute-graph"] ="graph_layout"; + legacyNames["distribute-graph-directed"] ="directed_graph"; + legacyNames["distribute-remove-overlaps"] ="remove_overlaps"; + legacyNames["align-horizontal-node"] ="node_valign"; + legacyNames["align-vertical-node"] ="node_halign"; + legacyNames["distribute-vertical-node"] ="node_vdistribute"; + legacyNames["distribute-horizontal-node"] ="node_hdistribute"; + legacyNames["xml-element-new"] ="add_xml_element_node"; + legacyNames["xml-text-new"] ="add_xml_text_node"; + legacyNames["xml-node-delete"] ="delete_xml_node"; + legacyNames["xml-node-duplicate"] ="duplicate_xml_node"; + legacyNames["xml-attribute-delete"] ="delete_xml_attribute"; + legacyNames["transform-move-horizontal"] ="arrows_hor"; + legacyNames["transform-move-vertical"] ="arrows_ver"; + legacyNames["transform-scale-horizontal"] ="transform_scale_hor"; + legacyNames["transform-scale-vertical"] ="transform_scale_ver"; + legacyNames["transform-skew-horizontal"] ="transform_scew_hor"; + legacyNames["transform-skew-vertical"] ="transform_scew_ver"; + legacyNames["object-fill"] ="properties_fill"; + legacyNames["object-stroke"] ="properties_stroke_paint"; + legacyNames["object-stroke-style"] ="properties_stroke"; + legacyNames["paint-none"] ="fill_none"; + legacyNames["paint-solid"] ="fill_solid"; + legacyNames["paint-gradient-linear"] ="fill_gradient"; + legacyNames["paint-gradient-radial"] ="fill_radial"; + legacyNames["paint-pattern"] ="fill_pattern"; + legacyNames["paint-unknown"] ="fill_unset"; + legacyNames["fill-rule-even-odd"] ="fillrule_evenodd"; + legacyNames["fill-rule-nonzero"] ="fillrule_nonzero"; + legacyNames["stroke-join-miter"] ="join_miter"; + legacyNames["stroke-join-bevel"] ="join_bevel"; + legacyNames["stroke-join-round"] ="join_round"; + legacyNames["stroke-cap-butt"] ="cap_butt"; + legacyNames["stroke-cap-square"] ="cap_square"; + legacyNames["stroke-cap-round"] ="cap_round"; + legacyNames["guides"] ="guide"; + legacyNames["grid-rectangular"] ="grid_xy"; + legacyNames["grid-axonometric"] ="grid_axonom"; + legacyNames["object-visible"] ="visible"; + legacyNames["object-hidden"] ="hidden"; + legacyNames["object-unlocked"] ="lock_unlocked"; + legacyNames["object-locked"] ="width_height_lock"; + legacyNames["zoom"] ="sticky_zoom"; +} + +static GtkWidget *sp_icon_new_full( Inkscape::IconSize lsize, gchar const *name ) { + static bool dump = Inkscape::Preferences::get()->getBool("/debug/icons/dumpGtk"); + GtkWidget *widget = 0; gint trySize = CLAMP( static_cast(lsize), 0, static_cast(G_N_ELEMENTS(iconSizeLookup) - 1) ); if ( !sizeMapDone ) { @@ -276,9 +557,60 @@ sp_icon_new_full( Inkscape::IconSize lsize, gchar const *name ) GtkStockItem stock; gboolean stockFound = gtk_stock_lookup( name, &stock ); + GtkWidget *img = 0; + if ( legacyNames.empty() ) { + setupLegacyNaming(); + } + if ( stockFound ) { - GtkWidget *img = gtk_image_new_from_stock( name, mappedSize ); - widget = GTK_WIDGET(img); + img = gtk_image_new_from_stock( name, mappedSize ); + } else { + img = gtk_image_new_from_icon_name( name, mappedSize ); + if ( dump ) { + g_message("gtk_image_new_from_icon_name( '%s', %d ) = %p", name, mappedSize, img); + GtkImageType thing = gtk_image_get_storage_type(GTK_IMAGE(img)); + g_message(" Type is %d %s", (int)thing, (thing == GTK_IMAGE_EMPTY ? "Empty" : "ok")); + } + } + + if ( img ) { + GtkImageType type = gtk_image_get_storage_type( GTK_IMAGE(img) ); + if ( type == GTK_IMAGE_STOCK ) { + if ( !stockFound ) { + // It's not showing as a stock ID, so assume it will be present internally + addPreRender( mappedSize, name ); + + // Add a hook to render if set visible before prerender is done. + g_signal_connect( G_OBJECT(img), "map", G_CALLBACK(imageMapCB), GINT_TO_POINTER(static_cast(mappedSize)) ); + if ( dump ) { + g_message(" connecting %p for imageMapCB for [%s] %d", img, name, (int)mappedSize); + } + } + widget = GTK_WIDGET(img); + img = 0; + if ( dump ) { + g_message( "loaded gtk '%s' %d (GTK_IMAGE_STOCK) %s on %p", name, mappedSize, (stockFound ? "STOCK" : "local"), widget ); + } + } else if ( type == GTK_IMAGE_ICON_NAME ) { + widget = GTK_WIDGET(img); + img = 0; + + // Add a hook to render if set visible before prerender is done. + g_signal_connect( G_OBJECT(widget), "map", G_CALLBACK(imageMapNamedCB), GINT_TO_POINTER(0) ); + + if ( Inkscape::Preferences::get()->getBool("/options/iconrender/named_nodelay") ) { + int psize = sp_icon_get_phys_size(lsize); + prerender_icon(name, mappedSize, psize); + } else { + addPreRender( mappedSize, name ); + } + } else { + if ( dump ) { + g_message( "skipped gtk '%s' %d (not GTK_IMAGE_STOCK)", name, lsize ); + } + //g_object_unref( (GObject *)img ); + img = 0; + } } if ( !widget ) { @@ -338,8 +670,7 @@ static void injectCustomSize() // TODO - still need to handle the case of theme changes and resize, especially as we can't re-register a string. if ( !sizeMapDone ) { - Inkscape::Preferences *prefs = Inkscape::Preferences::get(); - bool dump = prefs->getBool( "/debug/icons/dumpDefault"); + bool dump = Inkscape::Preferences::get()->getBool("/debug/icons/dumpDefault"); gint width = 0; gint height = 0; if ( gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &width, &height ) ) { @@ -360,13 +691,6 @@ static void injectCustomSize() } sizeMapDone = true; } - - static bool hit = false; - if ( !hit ) { - hit = true; - inkyIcons = Gtk::IconFactory::create(); - inkyIcons->add_default(); - } } GtkIconSize Inkscape::getRegisteredIconSize( Inkscape::IconSize size ) @@ -391,7 +715,7 @@ int sp_icon_get_phys_size(int size) static int lastSys[Inkscape::ICON_SIZE_DECORATION + 1]; static int vals[Inkscape::ICON_SIZE_DECORATION + 1]; - size = CLAMP( size, GTK_ICON_SIZE_MENU, Inkscape::ICON_SIZE_DECORATION ); + size = CLAMP( size, static_cast(GTK_ICON_SIZE_MENU), static_cast(Inkscape::ICON_SIZE_DECORATION) ); if ( !sizeMapDone ) { injectCustomSize(); @@ -424,9 +748,7 @@ int sp_icon_get_phys_size(int size) if ( !init ) { sizeDirty = false; - Inkscape::Preferences *prefs = Inkscape::Preferences::get(); - bool dump = prefs->getBool("/debug/icons/dumpDefault"); - + bool dump = Inkscape::Preferences::get()->getBool("/debug/icons/dumpDefault"); if ( dump ) { g_message( "Default icon sizes:" ); } @@ -473,6 +795,11 @@ int sp_icon_get_phys_size(int size) i, gtkSizes[i], ( used ? ' ' : 'X' ), width, height, names[i]); } + + // The following is needed due to this documented behavior of gtk_icon_size_lookup: + // "The rendered pixbuf may not even correspond to the width/height returned by + // gtk_icon_size_lookup(), because themes are free to render the pixbuf however + // they like, including changing the usual size." gchar const *id = GTK_STOCK_OPEN; GdkPixbuf *pb = gtk_widget_render_icon( icon, id, gtkSizes[i], NULL); if (pb) { @@ -494,15 +821,6 @@ int sp_icon_get_phys_size(int size) init = true; } - // Fixup workaround - if ((size == GTK_ICON_SIZE_MENU) || (size == GTK_ICON_SIZE_SMALL_TOOLBAR) || (size == GTK_ICON_SIZE_LARGE_TOOLBAR)) { - gint width = 0; - gint height = 0; - if ( gtk_icon_size_lookup( static_cast(size), &width, &height ) ) { - vals[size] = std::max( width, height ); - } - } - return vals[size]; } @@ -537,7 +855,9 @@ static void sp_icon_paint(SPIcon *icon, GdkRectangle const */*area*/) } } - if (unref_image) g_object_unref(G_OBJECT(image)); + if (unref_image) { + g_object_unref(G_OBJECT(image)); + } } GdkPixbuf *sp_icon_image_load_pixmap(gchar const *name, unsigned /*lsize*/, unsigned psize) @@ -592,21 +912,20 @@ extern "C" guchar * sp_icon_doc_icon( SPDocument *doc, NRArenaItem *root, gchar const *name, unsigned psize ) { - Inkscape::Preferences *prefs = Inkscape::Preferences::get(); - bool const dump = prefs->getBool("/debug/icons/dumpSvg"); + bool const dump = Inkscape::Preferences::get()->getBool("/debug/icons/dumpSvg"); guchar *px = NULL; if (doc) { SPObject *object = doc->getObjectById(name); if (object && SP_IS_ITEM(object)) { /* Find bbox in document */ - Geom::Matrix const i2doc(sp_item_i2doc_affine(SP_ITEM(object))); + Geom::Matrix const i2doc(SP_ITEM(object)->i2doc_affine()); Geom::OptRect dbox = SP_ITEM(object)->getBounds(i2doc); if ( SP_OBJECT_PARENT(object) == NULL ) { dbox = Geom::Rect(Geom::Point(0, 0), - Geom::Point(sp_document_width(doc), sp_document_height(doc))); + Geom::Point(doc->getWidth(), doc->getHeight())); } /* This is in document coordinates, i.e. pixels */ @@ -706,8 +1025,7 @@ sp_icon_doc_icon( SPDocument *doc, NRArenaItem *root, NR_ARENA_ITEM_RENDER_NO_CACHE ); nr_pixblock_release(&B); - bool useOverlay = prefs->getBool("/debug/icons/overlaySvg"); - if ( useOverlay ) { + if ( Inkscape::Preferences::get()->getBool("/debug/icons/overlaySvg") ) { sp_icon_overlay_pixels( px, psize, psize, 4 * psize, 0x00, 0x00, 0xff ); } } @@ -737,11 +1055,12 @@ Glib::ustring icon_cache_key(gchar const *name, unsigned psize) } GdkPixbuf *get_cached_pixbuf(Glib::ustring const &key) { + GdkPixbuf* pb = 0; std::map::iterator found = pb_cache.find(key); if ( found != pb_cache.end() ) { - return found->second; + pb = found->second; } - return NULL; + return pb; } static std::list &icons_svg_paths() @@ -787,18 +1106,37 @@ static guchar *load_svg_pixels(gchar const *name, /* Try to load from document. */ if (!info && Inkscape::IO::file_test( doc_filename, G_FILE_TEST_IS_REGULAR ) && - (doc = sp_document_new( doc_filename, FALSE )) ) { + (doc = SPDocument::createNewDoc( doc_filename, FALSE )) ) { //g_message("Loaded icon file %s", doc_filename); // prep the document - sp_document_ensure_up_to_date(doc); + doc->ensureUpToDate(); /* Create new arena */ NRArena *arena = NRArena::create(); /* Create ArenaItem and set transform */ - unsigned visionkey = sp_item_display_key_new(1); + unsigned visionkey = SPItem::display_key_new(1); /* fixme: Memory manage root if needed (Lauris) */ - root = sp_item_invoke_show( SP_ITEM(SP_DOCUMENT_ROOT(doc)), - arena, visionkey, SP_ITEM_SHOW_DISPLAY ); + // This needs to be fixed indeed; this leads to a memory leak of a few megabytes these days + // because shapes are being rendered which are not being freed + // Valgrind output: + /*==7014== 1,548,344 bytes in 599 blocks are possibly lost in loss record 20,361 of 20,362 + ==7014== at 0x4A05974: operator new(unsigned long) (vg_replace_malloc.c:220) + ==7014== by 0x4F1015: __gnu_cxx::new_allocator::allocate(unsigned long, void const*) (new_allocator.h:89) + ==7014== by 0x4F02AC: std::_Vector_base >::_M_allocate(unsigned long) (stl_vector.h:140) + ==7014== by 0xCF62D7: std::vector >::_M_fill_insert(__gnu_cxx::__normal_iterator > >, unsigned long, Shape::point_data const&) (vector.tcc:414) + ==7014== by 0xCF4D45: std::vector >::insert(__gnu_cxx::__normal_iterator > >, unsigned long, Shape::point_data const&) (stl_vector.h:851) + ==7014== by 0xCF3DCD: std::vector >::resize(unsigned long, Shape::point_data) (stl_vector.h:557) + ==7014== by 0xCEA771: Shape::AddPoint(Geom::Point) (Shape.cpp:326) + ==7014== by 0xD0F413: Shape::ConvertToShape(Shape*, fill_typ, bool) (ShapeSweep.cpp:257) + ==7014== by 0x5ECD4F: nr_arena_shape_update_stroke(NRArenaShape*, NRGC*, NRRectL*) (nr-arena-shape.cpp:651) + ==7014== by 0x5EE0DA: nr_arena_shape_render(_cairo*, NRArenaItem*, NRRectL*, NRPixBlock*, unsigned int) (nr-arena-shape.cpp:862) + ==7014== by 0x5E72FB: nr_arena_item_invoke_render(_cairo*, NRArenaItem*, NRRectL const*, NRPixBlock*, unsigned int) (nr-arena-item.cpp:578) + ==7014== by 0x5E9DDE: nr_arena_group_render(_cairo*, NRArenaItem*, NRRectL*, NRPixBlock*, unsigned int) (nr-arena-group.cpp:228) + ==7014== by 0x5E72FB: nr_arena_item_invoke_render(_cairo*, NRArenaItem*, NRRectL const*, NRPixBlock*, unsigned int) (nr-arena-item.cpp:578) + ==7014== by 0x5E9DDE: nr_arena_group_render(_cairo*, NRArenaItem*, NRRectL*, NRPixBlock*, unsigned int) (nr-arena-group.cpp:228) + ==7014== by 0x5E72FB: nr_arena_item_invoke_render(_cairo*, NRArenaItem*, NRRectL const*, NRPixBlock*, unsigned int) (nr-arena-item.cpp:578) + */ + root = SP_ITEM(doc->getRoot())->invoke_show(arena, visionkey, SP_ITEM_SHOW_DISPLAY ); // store into the cache info = new svg_doc_cache_t; @@ -814,16 +1152,131 @@ static guchar *load_svg_pixels(gchar const *name, } // move on to the next document if we couldn't get anything - if (!info && !doc) continue; + if (!info && !doc) { + continue; + } px = sp_icon_doc_icon( doc, root, name, psize ); - //if (px) g_message("Found icon %s in %s", name, doc_filename); +// if (px) { +// g_message("Found icon %s in %s", name, doc_filename); +// } } - //if (!px) g_message("Not found icon %s", name); +// if (!px) { +// g_message("Not found icon %s", name); +// } return px; } +static void addToIconSet(GdkPixbuf* pb, gchar const* name, GtkIconSize lsize, unsigned psize) { + static bool dump = Inkscape::Preferences::get()->getBool("/debug/icons/dumpGtk"); + GtkStockItem stock; + gboolean stockFound = gtk_stock_lookup( name, &stock ); + if ( !stockFound ) { + Gtk::IconTheme::add_builtin_icon( name, psize, Glib::wrap(pb) ); + if (dump) { + g_message(" set in a builtin for %s:%d:%d", name, lsize, psize); + } + } +} + +void Inkscape::queueIconPrerender( Glib::ustring const &name, Inkscape::IconSize lsize ) +{ + GtkStockItem stock; + gboolean stockFound = gtk_stock_lookup( name.c_str(), &stock ); + gboolean themedFound = gtk_icon_theme_has_icon(gtk_icon_theme_get_default(), name.c_str()); + if (!stockFound && !themedFound ) { + gint trySize = CLAMP( static_cast(lsize), 0, static_cast(G_N_ELEMENTS(iconSizeLookup) - 1) ); + if ( !sizeMapDone ) { + injectCustomSize(); + } + GtkIconSize mappedSize = iconSizeLookup[trySize]; + + int psize = sp_icon_get_phys_size(lsize); + // TODO place in a queue that is triggered by other map events + prerender_icon(name.c_str(), mappedSize, psize); + } +} + +// returns true if icon needed preloading, false if nothing was done +bool prerender_icon(gchar const *name, GtkIconSize lsize, unsigned psize) +{ + bool loadNeeded = false; + static bool dump = Inkscape::Preferences::get()->getBool("/debug/icons/dumpGtk"); + static bool useCache = Inkscape::Preferences::get()->getBool("/debug/icons/useCache"); + + Glib::ustring key = icon_cache_key(name, psize); + if ( !get_cached_pixbuf(key) ) { + if ((internalNames.find(name) != internalNames.end()) + || (!gtk_icon_theme_has_icon(gtk_icon_theme_get_default(), name))) { + if (dump) { + g_message("prerender_icon [%s] %d:%d", name, lsize, psize); + } + // In file encoding: + std::string iconCacheDir = Glib::build_filename(Glib::build_filename(Glib::get_user_cache_dir(), "inkscape"), "icons"); + std::string potentialFile = Glib::build_filename( iconCacheDir, name ); + potentialFile += ".png"; + + bool dataLoaded = false; + if ( useCache && Glib::file_test(potentialFile, Glib::FILE_TEST_EXISTS) && Glib::file_test(potentialFile, Glib::FILE_TEST_IS_REGULAR) ) { + Glib::RefPtr pb = Gdk::Pixbuf::create_from_file(potentialFile); + if (pb) { + dataLoaded = true; + GdkPixbuf *obj = pb->gobj(); + g_object_ref(obj); + pb_cache[key] = obj; + addToIconSet(obj, name, lsize, psize); + loadNeeded = true; + if (internalNames.find(name) == internalNames.end()) { + internalNames.insert(name); + } + } + } + + if (!dataLoaded) { + guchar* px = load_svg_pixels(name, lsize, psize); + if ( !px ) { + // check for a fallback name + if ( legacyNames.find(name) != legacyNames.end() ) { + if ( dump ) { + g_message("load_svg_pixels([%s]=%s, %d, %d)", name, legacyNames[name].c_str(), lsize, psize); + } + px = load_svg_pixels(legacyNames[name].c_str(), lsize, psize); + } + } + if (px) { + GdkPixbuf* pb = gdk_pixbuf_new_from_data( px, GDK_COLORSPACE_RGB, TRUE, 8, + psize, psize, psize * 4, + reinterpret_cast(g_free), NULL ); + pb_cache[key] = pb; + addToIconSet(pb, name, lsize, psize); + loadNeeded = true; + if (internalNames.find(name) == internalNames.end()) { + internalNames.insert(name); + } + if (useCache) { + g_object_ref(pb); + Glib::RefPtr ppp = Glib::wrap(pb); + try { + ppp->save( potentialFile, "png" ); + } catch ( Glib::FileError &ex ) { + g_warning("FileError [%s]", ex.what().c_str()); + } catch ( Gdk::PixbufError &ex ) { + g_warning("PixbufError [%s]", ex.what().c_str()); + } + } + } else if (dump) { + g_message("XXXXXXXXXXXXXXXXXXXXXXXXXXXXX error!!! pixels not found for '%s'", name); + } + } + } + else if (dump) { + g_message("prerender_icon [%s] %d NOT!!!!!!", name, psize); + } + } + return loadNeeded; +} + static GdkPixbuf *sp_icon_image_load_svg(gchar const *name, GtkIconSize lsize, unsigned psize) { Glib::ustring key = icon_cache_key(name, psize); @@ -831,12 +1284,14 @@ static GdkPixbuf *sp_icon_image_load_svg(gchar const *name, GtkIconSize lsize, u // did we already load this icon at this scale/size? GdkPixbuf* pb = get_cached_pixbuf(key); if (!pb) { + g_message("YYYYYYYYYYYYY YYYYYYYYYYYYYYY two load_svg_pixels(%s, %d, %d)", name, lsize, psize); guchar *px = load_svg_pixels(name, lsize, psize); if (px) { pb = gdk_pixbuf_new_from_data(px, GDK_COLORSPACE_RGB, TRUE, 8, psize, psize, psize * 4, (GdkPixbufDestroyNotify)g_free, NULL); pb_cache[key] = pb; + addToIconSet(pb, name, lsize, psize); } } @@ -904,6 +1359,116 @@ void sp_icon_overlay_pixels(guchar *px, int width, int height, int stride, } } +class preRenderItem +{ +public: + preRenderItem( GtkIconSize lsize, gchar const *name ) : + _lsize( lsize ), + _name( name ) + {} + GtkIconSize _lsize; + Glib::ustring _name; +}; + + +static std::vector pendingRenders; +static bool callbackHooked = false; + +static void addPreRender( GtkIconSize lsize, gchar const *name ) +{ + if ( !callbackHooked ) + { + callbackHooked = true; + g_idle_add_full( G_PRIORITY_LOW, &icon_prerender_task, NULL, NULL ); + } + + pendingRenders.push_back(preRenderItem(lsize, name)); +} + +gboolean icon_prerender_task(gpointer /*data*/) { + if ( inkscapeIsCrashing() ) { + // stop + } else if (!pendingRenders.empty()) { + bool workDone = false; + do { + preRenderItem single = pendingRenders.front(); + pendingRenders.erase(pendingRenders.begin()); + int psize = sp_icon_get_phys_size(single._lsize); + workDone = prerender_icon(single._name.c_str(), single._lsize, psize); + } while (!pendingRenders.empty() && !workDone); + } + + if (!inkscapeIsCrashing() && !pendingRenders.empty()) { + return TRUE; + } else { + callbackHooked = false; + return FALSE; + } +} + + +void imageMapCB(GtkWidget* widget, gpointer user_data) { + gchar* id = 0; + GtkIconSize size = GTK_ICON_SIZE_INVALID; + gtk_image_get_stock(GTK_IMAGE(widget), &id, &size); + GtkIconSize lsize = static_cast(GPOINTER_TO_INT(user_data)); + if ( id ) { + int psize = sp_icon_get_phys_size(lsize); + g_message("imageMapCB(%p) for [%s]:%d:%d", widget, id, lsize, psize); + for ( std::vector::iterator it = pendingRenders.begin(); it != pendingRenders.end(); ++it ) { + if ( (it->_name == id) && (it->_lsize == lsize) ) { + prerender_icon(id, lsize, psize); + pendingRenders.erase(it); + g_message(" prerender for %s:%d:%d", id, lsize, psize); + if (lsize != size) { + int psize = sp_icon_get_phys_size(size); + prerender_icon(id, size, psize); + } + break; + } + } + } + + g_signal_handlers_disconnect_by_func(widget, (gpointer)imageMapCB, user_data); +} + +static void imageMapNamedCB(GtkWidget* widget, gpointer user_data) { + GtkImage* img = GTK_IMAGE(widget); + gchar const* iconName = 0; + GtkIconSize size = GTK_ICON_SIZE_INVALID; + gtk_image_get_icon_name(img, &iconName, &size); + if ( iconName ) { + GtkImageType type = gtk_image_get_storage_type( GTK_IMAGE(img) ); + if ( type == GTK_IMAGE_ICON_NAME ) { + + gint iconSize = 0; + gchar* iconName = 0; + { + g_object_get(G_OBJECT(widget), + "icon-name", &iconName, + "icon-size", &iconSize, + NULL); + } + + for ( std::vector::iterator it = pendingRenders.begin(); it != pendingRenders.end(); ++it ) { + if ( (it->_name == iconName) && (it->_lsize == size) ) { + int psize = sp_icon_get_phys_size(size); + prerender_icon(iconName, size, psize); + pendingRenders.erase(it); + break; + } + } + + gtk_image_set_from_icon_name(img, "", (GtkIconSize)iconSize); + gtk_image_set_from_icon_name(img, iconName, (GtkIconSize)iconSize); + } else { + g_warning("UNEXPECTED TYPE of %d", (int)type); + } + } + + g_signal_handlers_disconnect_by_func(widget, (gpointer)imageMapNamedCB, user_data); +} + /* Local Variables: @@ -914,4 +1479,4 @@ void sp_icon_overlay_pixels(guchar *px, int width, int height, int stride, fill-column:99 End: */ -// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 : +// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :