Code

now that selection description includes style (filtered, clipped), we need to update...
[inkscape.git] / src / sp-object.cpp
index d260792608a50e288064ab8f1a136baa04de8dbc..1e7c5f2fe33b3d02c780a369bcc053f67a650e43 100644 (file)
@@ -85,7 +85,7 @@ static void sp_object_release(SPObject *object);
 static void sp_object_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
 
 static void sp_object_private_set(SPObject *object, unsigned int key, gchar const *value);
-static Inkscape::XML::Node *sp_object_private_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
+static Inkscape::XML::Node *sp_object_private_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
 
 /* Real handlers of repr signals */
 
@@ -817,7 +817,8 @@ sp_object_invoke_build(SPObject *object, SPDocument *document, Inkscape::XML::No
 
     object->document = document;
     object->repr = repr;
-    Inkscape::GC::anchor(repr);
+    if (!cloned)
+        Inkscape::GC::anchor(repr);
     object->cloned = cloned;
 
     if (!SP_OBJECT_IS_CLONED(object)) {
@@ -885,6 +886,8 @@ void SPObject::releaseReferences() {
         this->_default_label = NULL;
 
         this->document->bindObjectToRepr(this->repr, NULL);
+
+        Inkscape::GC::release(this->repr);
     } else {
         g_assert(!this->id);
     }
@@ -893,8 +896,6 @@ void SPObject::releaseReferences() {
         this->style = sp_style_unref(this->style);
     }
 
-    Inkscape::GC::release(this->repr);
-
     this->document = NULL;
     this->repr = NULL;
 }
@@ -1067,7 +1068,7 @@ sp_object_read_attr(SPObject *object, gchar const *key)
  * Callback for attr_changed node event.
  */
 static void
-sp_object_repr_attr_changed(Inkscape::XML::Node *repr, gchar const *key, gchar const */*oldval*/, gchar const */*newval*/, bool is_interactive, gpointer data)
+sp_object_repr_attr_changed(Inkscape::XML::Node */*repr*/, gchar const *key, gchar const */*oldval*/, gchar const */*newval*/, bool is_interactive, gpointer data)
 {
     SPObject *object = SP_OBJECT(data);
 
@@ -1076,7 +1077,7 @@ sp_object_repr_attr_changed(Inkscape::XML::Node *repr, gchar const *key, gchar c
     // manual changes to extension attributes require the normal
     // attributes, which depend on them, to be updated immediately
     if (is_interactive) {
-        object->updateRepr(repr, 0);
+        object->updateRepr(0);
     }
 }
 
@@ -1112,10 +1113,10 @@ sp_xml_get_space_string(unsigned int space)
  * Callback for write event.
  */
 static Inkscape::XML::Node *
-sp_object_private_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
+sp_object_private_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags)
 {
     if (!repr && (flags & SP_OBJECT_WRITE_BUILD)) {
-        repr = SP_OBJECT_REPR(object)->duplicate(NULL); // FIXME
+        repr = SP_OBJECT_REPR(object)->duplicate(doc);
         if (!( flags & SP_OBJECT_WRITE_EXT )) {
             repr->setAttribute("inkscape:collect", NULL);
         }
@@ -1180,7 +1181,7 @@ SPObject::updateRepr(unsigned int flags) {
     if (!SP_OBJECT_IS_CLONED(this)) {
         Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
         if (repr) {
-            return updateRepr(repr, flags);
+            return updateRepr(repr->document(), repr, flags);
         } else {
             g_critical("Attempt to update non-existent repr");
             return NULL;
@@ -1196,7 +1197,9 @@ SPObject::updateRepr(unsigned int flags) {
  *  saving as "Plain SVG"
  */
 Inkscape::XML::Node *
-SPObject::updateRepr(Inkscape::XML::Node *repr, unsigned int flags) {
+SPObject::updateRepr(Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, unsigned int flags) {
+    g_assert(doc != NULL);
+
     if (SP_OBJECT_IS_CLONED(this)) {
         /* cloned objects have no repr */
         return NULL;
@@ -1205,13 +1208,12 @@ SPObject::updateRepr(Inkscape::XML::Node *repr, unsigned int flags) {
         if (!(flags & SP_OBJECT_WRITE_BUILD) && !repr) {
             repr = SP_OBJECT_REPR(this);
         }
-        return ((SPObjectClass *) G_OBJECT_GET_CLASS(this))->write(this, repr, flags);
+        return ((SPObjectClass *) G_OBJECT_GET_CLASS(this))->write(this, doc, repr, flags);
     } else {
         g_warning("Class %s does not implement ::write", G_OBJECT_TYPE_NAME(this));
         if (!repr) {
             if (flags & SP_OBJECT_WRITE_BUILD) {
-                /// \todo FIXME:  Plumb an appropriate XML::Document into this
-                repr = SP_OBJECT_REPR(this)->duplicate(NULL);
+                repr = SP_OBJECT_REPR(this)->duplicate(doc);
             }
             /// \todo FIXME: else probably error (Lauris) */
         } else {
@@ -1293,8 +1295,20 @@ SPObject::updateDisplay(SPCtx *ctx, unsigned int flags)
         }
     }
 
-    if (((SPObjectClass *) G_OBJECT_GET_CLASS(this))->update)
-        ((SPObjectClass *) G_OBJECT_GET_CLASS(this))->update(this, ctx, flags);
+    try
+    {
+        if (((SPObjectClass *) G_OBJECT_GET_CLASS(this))->update)
+            ((SPObjectClass *) G_OBJECT_GET_CLASS(this))->update(this, ctx, flags);
+    }
+    catch(...)
+    {
+        /** \todo 
+        * in case of catching an exception we need to inform the user somehow that the document is corrupted
+        * maybe by implementing an document flag documentOk
+        * or by a modal error dialog
+        */
+        g_warning("SPObject::updateDisplay(SPCtx *ctx, unsigned int flags) : throw in ((SPObjectClass *) G_OBJECT_GET_CLASS(this))->update(this, ctx, flags);");
+    }
 
     update_in_progress --;
 }
@@ -1587,13 +1601,14 @@ SPObject::title() const
 
 /**
  * Sets the title of this object
- * A NULL or purely whitespace argument is interpreted as meaning that
- * the existing title (if any) should be deleted.
+ * A NULL first argument is interpreted as meaning that the existing title
+ * (if any) should be deleted.
+ * The second argument is optional - see setTitleOrDesc() below for details.
  */
-void
-SPObject::setTitle(gchar const *title)
+bool
+SPObject::setTitle(gchar const *title, bool verbatim)
 {
-    setTitleOrDesc(title, "svg:title");
+    return setTitleOrDesc(title, "svg:title", verbatim);
 }
 
 /**
@@ -1609,13 +1624,14 @@ SPObject::desc() const
 
 /**
  * Sets the description of this object.
- * A NULL or purely whitespace argument is interpreted as meaning that
- * the existing description (if any) should be deleted.
+ * A NULL first argument is interpreted as meaning that the existing
+ * description (if any) should be deleted.
+ * The second argument is optional - see setTitleOrDesc() below for details.
  */
-void
-SPObject::setDesc(gchar const *desc)
+bool
+SPObject::setDesc(gchar const *desc, bool verbatim)
 {
-    setTitleOrDesc(desc, "svg:desc");
+    return setTitleOrDesc(desc, "svg:desc", verbatim);
 }
 
 /**
@@ -1638,29 +1654,58 @@ SPObject::getTitleOrDesc(gchar const *svg_tagname) const
 
 /**
  * Sets or deletes the title or description of this object.
+ * A NULL 'value' argument causes the title or description to be deleted.
+ *
+ * 'verbatim' parameter:
+ * If verbatim==true, then the title or description is set to exactly the
+ * specified value.  If verbatim==false then two exceptions are made:
+ *   (1) If the specified value is just whitespace, then the title/description
+ *       is deleted.
+ *   (2) If the specified value is the same as the current value except for
+ *       mark-up, then the current value is left unchanged.
+ * This is usually the desired behaviour, so 'verbatim' defaults to false for
+ * setTitle() and setDesc().
+ *
+ * The return value is true if a change was made to the title/description,
+ * and usually false otherwise.
  */
-void
-SPObject::setTitleOrDesc(gchar const *value, gchar const *svg_tagname)
+bool
+SPObject::setTitleOrDesc(gchar const *value, gchar const *svg_tagname, bool verbatim)
 {
-    SPObject *elem = findFirstChild(svg_tagname);
-
-    // if the new title/description is NULL, or just whitespace,
-    // then delete any existing title/description
-    bool just_whitespace = true;
-    if (value)
-        for (const gchar *cp = value; *cp; ++cp) {
-            if (!std::strchr("\r\n \t", *cp)) {
-                just_whitespace = false;
-                break;
+    if (!verbatim) {
+        // If the new title/description is just whitespace,
+        // treat it as though it were NULL.
+        if (value) {
+            bool just_whitespace = true;
+            for (const gchar *cp = value; *cp; ++cp) {
+                if (!std::strchr("\r\n \t", *cp)) {
+                    just_whitespace = false;
+                    break;
+                }
+            }
+            if (just_whitespace) value = NULL;
+        }
+        // Don't stomp on mark-up if there is no real change.
+        if (value) {
+            gchar *current_value = getTitleOrDesc(svg_tagname);
+            if (current_value) {
+                bool different = std::strcmp(current_value, value);
+                g_free(current_value);
+                if (!different) return false;
             }
         }
-    if (just_whitespace) {
+    }
+
+    SPObject *elem = findFirstChild(svg_tagname);
+
+    if (value == NULL) {
+        if (elem == NULL) return false;
         // delete the title/description(s)
         while (elem) {
             elem->deleteObject();
             elem = findFirstChild(svg_tagname);
         }
-        return;
+        return true;
     }
 
     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
@@ -1671,6 +1716,7 @@ SPObject::setTitleOrDesc(gchar const *value, gchar const *svg_tagname)
         Inkscape::XML::Node *xml_elem = xml_doc->createElement(svg_tagname);
         repr->addChild(xml_elem, NULL);
         elem = document->getObjectByRepr(xml_elem);
+        Inkscape::GC::release(xml_elem);
     }
     else {
         // remove the current content of the 'text' or 'desc' element
@@ -1680,6 +1726,7 @@ SPObject::setTitleOrDesc(gchar const *value, gchar const *svg_tagname)
 
     // add the new content
     elem->appendChildRepr(xml_doc->createTextNode(value));
+    return true;
 }
 
 /**