Code

Merge and cleanup of GSoC C++-ification project.
[inkscape.git] / src / widgets / icon.cpp
index 5e47bd8e4620e2a26d34734dde38eef23650211d..15900d340c1e27969a35cb6d1dba2f2abc78710e 100644 (file)
@@ -5,6 +5,7 @@
  * Author:
  *   Lauris Kaplinski <lauris@kaplinski.com>
  *   Jon A. Cruz <jon@joncruz.org>
+ *   Abhishek Sharma
  *
  * Copyright (C) 2002 Lauris Kaplinski
  *
@@ -219,7 +220,7 @@ void sp_icon_fetch_pixbuf( SPIcon *icon )
     }
 }
 
-static GdkPixbuf* renderup( gchar const* name, Inkscape::IconSize lsize, unsigned psize ) {
+GdkPixbuf* renderup( gchar const* name, Inkscape::IconSize lsize, unsigned psize ) {
     GtkIconTheme *theme = gtk_icon_theme_get_default();
 
     GdkPixbuf *pb = 0;
@@ -381,7 +382,7 @@ static void setupLegacyNaming() {
     legacyNames["text-unkern"] ="remove_manual_kerns";
     legacyNames["help-keyboard-shortcuts"] ="help_keys";
     legacyNames["help-contents"] ="help_tutorials";
-    legacyNames["inkscape"] ="inkscape_options";
+    legacyNames["inkscape-logo"] ="inkscape_options";
     legacyNames["dialog-memory"] ="about_memory";
     legacyNames["tool-pointer"] ="draw_select";
     legacyNames["tool-node-editor"] ="draw_node";
@@ -714,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<int>(GTK_ICON_SIZE_MENU), static_cast<int>(Inkscape::ICON_SIZE_DECORATION) );
 
     if ( !sizeMapDone ) {
         injectCustomSize();
@@ -908,7 +909,7 @@ GdkPixbuf *sp_icon_image_load_pixmap(gchar const *name, unsigned /*lsize*/, unsi
 
 // takes doc, root, icon, and icon name to produce pixels
 extern "C" guchar *
-sp_icon_doc_icon( Document *doc, NRArenaItem *root,
+sp_icon_doc_icon( SPDocument *doc, NRArenaItem *root,
                   gchar const *name, unsigned psize )
 {
     bool const dump = Inkscape::Preferences::get()->getBool("/debug/icons/dumpSvg");
@@ -918,13 +919,13 @@ sp_icon_doc_icon( Document *doc, NRArenaItem *root,
         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 */
@@ -1038,7 +1039,7 @@ sp_icon_doc_icon( Document *doc, NRArenaItem *root,
 
 struct svg_doc_cache_t
 {
-    Document *doc;
+    SPDocument *doc;
     NRArenaItem *root;
 };
 
@@ -1081,7 +1082,7 @@ static std::list<gchar*> &icons_svg_paths()
 static guchar *load_svg_pixels(gchar const *name,
                                unsigned /*lsize*/, unsigned psize)
 {
-    Document *doc = NULL;
+    SPDocument *doc = NULL;
     NRArenaItem *root = NULL;
     svg_doc_cache_t *info = NULL;
 
@@ -1105,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<Shape::point_data>::allocate(unsigned long, void const*) (new_allocator.h:89)
+            ==7014==    by 0x4F02AC: std::_Vector_base<Shape::point_data, std::allocator<Shape::point_data> >::_M_allocate(unsigned long) (stl_vector.h:140)
+            ==7014==    by 0xCF62D7: std::vector<Shape::point_data, std::allocator<Shape::point_data> >::_M_fill_insert(__gnu_cxx::__normal_iterator<Shape::point_data*, std::vector<Shape::point_data, std::allocator<Shape::point_data> > >, unsigned long, Shape::point_data const&) (vector.tcc:414)
+            ==7014==    by 0xCF4D45: std::vector<Shape::point_data, std::allocator<Shape::point_data> >::insert(__gnu_cxx::__normal_iterator<Shape::point_data*, std::vector<Shape::point_data, std::allocator<Shape::point_data> > >, unsigned long, Shape::point_data const&) (stl_vector.h:851)
+            ==7014==    by 0xCF3DCD: std::vector<Shape::point_data, std::allocator<Shape::point_data> >::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;
@@ -1164,7 +1184,8 @@ void Inkscape::queueIconPrerender( Glib::ustring const &name, Inkscape::IconSize
 {
     GtkStockItem stock;
     gboolean stockFound = gtk_stock_lookup( name.c_str(), &stock );
-    if (!stockFound && !gtk_icon_theme_has_icon(gtk_icon_theme_get_default(), name.c_str()) ) {
+    gboolean themedFound = gtk_icon_theme_has_icon(gtk_icon_theme_get_default(), name.c_str());
+    if (!stockFound && !themedFound ) {
         gint trySize = CLAMP( static_cast<gint>(lsize), 0, static_cast<gint>(G_N_ELEMENTS(iconSizeLookup) - 1) );
         if ( !sizeMapDone ) {
             injectCustomSize();
@@ -1329,7 +1350,9 @@ static void addPreRender( GtkIconSize lsize, gchar const *name )
 }
 
 gboolean icon_prerender_task(gpointer /*data*/) {
-    if (!pendingRenders.empty()) {
+    if ( inkscapeIsCrashing() ) {
+        // stop
+    } else if (!pendingRenders.empty()) {
         bool workDone = false;
         do {
             preRenderItem single = pendingRenders.front();
@@ -1339,7 +1362,7 @@ gboolean icon_prerender_task(gpointer /*data*/) {
         } while (!pendingRenders.empty() && !workDone);
     }
 
-    if (!pendingRenders.empty()) {
+    if (!inkscapeIsCrashing() && !pendingRenders.empty()) {
         return TRUE;
     } else {
         callbackHooked = false;
@@ -1420,4 +1443,4 @@ static void imageMapNamedCB(GtkWidget* widget, gpointer user_data) {
   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 :