Code

Cmake: Improve Gtkmm dependency checking, add new files to CMakeLists.txts, remove...
[inkscape.git] / src / sp-object.cpp
index 62a442d0a6483c48de2c2e49eb5b54df4014fad2..f6ddf27dfa0aae1414a756e7ddf5c2c0ac260e63 100644 (file)
@@ -31,6 +31,8 @@
  * dictionary and so on. Source: doc/architecture.txt
  */
 
+#include <cstring>
+#include <string>
 
 #include "helper/sp-marshal.h"
 #include "xml/node-event-vector.h"
@@ -173,7 +175,6 @@ sp_object_init(SPObject *object)
     object->parent = object->next = NULL;
     object->repr = NULL;
     object->id = NULL;
-    object->style = NULL;
 
     object->_collection_policy = SPObject::COLLECT_WITH_PARENT;
 
@@ -183,6 +184,12 @@ sp_object_init(SPObject *object)
     new (&object->_position_changed_signal) sigc::signal<void, SPObject *>();
     object->_successor = NULL;
 
+    // FIXME: now we create style for all objects, but per SVG, only the following can have style attribute:
+    // vg, g, defs, desc, title, symbol, use, image, switch, path, rect, circle, ellipse, line, polyline, 
+    // polygon, text, tspan, tref, textPath, altGlyph, glyphRef, marker, linearGradient, radialGradient, 
+    // stop, pattern, clipPath, mask, filter, feImage, a, font, glyph, missing-glyph, foreignObject
+    object->style = sp_style_new_from_object(object);
+
     object->_label = NULL;
     object->_default_label = NULL;
 }
@@ -299,7 +306,7 @@ sp_object_unref(SPObject *object, SPObject *owner)
  * \pre object points to real object
  */
 SPObject *
-sp_object_href(SPObject *object, gpointer owner)
+sp_object_href(SPObject *object, gpointer /*owner*/)
 {
     g_return_val_if_fail(object != NULL, NULL);
     g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
@@ -320,7 +327,7 @@ sp_object_href(SPObject *object, gpointer owner)
  * \pre object points to real object and hrefcount>0
  */
 SPObject *
-sp_object_hunref(SPObject *object, gpointer owner)
+sp_object_hunref(SPObject *object, gpointer /*owner*/)
 {
     g_return_val_if_fail(object != NULL, NULL);
     g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
@@ -737,8 +744,9 @@ sp_object_remove_child(SPObject *object, Inkscape::XML::Node *child)
 {
     debug("id=%x, typename=%s", object, g_type_name_from_instance((GTypeInstance*)object));
     SPObject *ochild = sp_object_get_child_by_repr(object, child);
-    g_return_if_fail(ochild != NULL);
-    sp_object_detach(object, ochild);
+    g_return_if_fail (ochild != NULL || !strcmp("comment", child->name())); // comments have no objects
+    if (ochild)
+        sp_object_detach(object, ochild);
 }
 
 /**
@@ -747,7 +755,7 @@ sp_object_remove_child(SPObject *object, Inkscape::XML::Node *child)
  * Invoked whenever the given mutation event happens in the XML tree.
  * \param old_ref Ignored
  */
-static void sp_object_order_changed(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref,
+static void sp_object_order_changed(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node */*old_ref*/,
                                     Inkscape::XML::Node *new_ref)
 {
     SPObject *ochild = sp_object_get_child_by_repr(object, child);
@@ -815,20 +823,26 @@ sp_object_invoke_build(SPObject *object, SPDocument *document, Inkscape::XML::No
         object->document->bindObjectToRepr(object->repr, object);
 
         if (Inkscape::XML::id_permitted(object->repr)) {
-            /* If we are not cloned, force unique id */
+            /* If we are not cloned, and not seeking, force unique id */
             gchar const *id = object->repr->attribute("id");
-            gchar *realid = sp_object_get_unique_id(object, id);
-            g_assert(realid != NULL);
-
-            object->document->bindObjectToId(realid, object);
-            object->id = realid;
-
-            /* Redefine ID, if required */
-            if ((id == NULL) || (strcmp(id, realid) != 0)) {
-                gboolean undo_sensitive=sp_document_get_undo_sensitive(document);
-                sp_document_set_undo_sensitive(document, FALSE);
-                object->repr->setAttribute("id", realid);
-                sp_document_set_undo_sensitive(document, undo_sensitive);
+            if (!document->isSeeking()) {
+                gchar *realid = sp_object_get_unique_id(object, id);
+                g_assert(realid != NULL);
+
+                object->document->bindObjectToId(realid, object);
+                object->id = realid;
+
+                /* Redefine ID, if required */
+                if ((id == NULL) || (strcmp(id, realid) != 0)) {
+                    object->repr->setAttribute("id", realid);
+                }
+            } else if (id) {
+                // bind if id, but no conflict -- otherwise, we can expect
+                // a subsequent setting of the id attribute
+                if (!object->document->getObjectById(id)) {
+                    object->document->bindObjectToId(id, object);
+                    object->id = g_strdup(id);
+                }
             }
         }
     } else {
@@ -836,7 +850,6 @@ sp_object_invoke_build(SPObject *object, SPDocument *document, Inkscape::XML::No
     }
 
     /* Invoke derived methods, if any */
-
     if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->build) {
         (*((SPObjectClass *) G_OBJECT_GET_CLASS(object))->build)(object, document, repr);
     }
@@ -851,11 +864,11 @@ void SPObject::releaseReferences() {
 
     sp_repr_remove_listener_by_data(this->repr, this);
 
+    this->_release_signal.emit(this);
     SPObjectClass *klass=(SPObjectClass *)G_OBJECT_GET_CLASS(this);
     if (klass->release) {
         klass->release(this);
     }
-    this->_release_signal.emit(this);
 
     /* all hrefs should be released by the "release" handlers */
     g_assert(this->hrefcount == 0);
@@ -889,7 +902,7 @@ void SPObject::releaseReferences() {
  * Callback for child_added node event.
  */
 static void
-sp_object_repr_child_added(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, gpointer data)
+sp_object_repr_child_added(Inkscape::XML::Node */*repr*/, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, gpointer data)
 {
     SPObject *object = SP_OBJECT(data);
 
@@ -901,7 +914,7 @@ sp_object_repr_child_added(Inkscape::XML::Node *repr, Inkscape::XML::Node *child
  * Callback for remove_child node event.
  */
 static void
-sp_object_repr_child_removed(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, gpointer data)
+sp_object_repr_child_removed(Inkscape::XML::Node */*repr*/, Inkscape::XML::Node *child, Inkscape::XML::Node */*ref*/, gpointer data)
 {
     SPObject *object = SP_OBJECT(data);
 
@@ -916,7 +929,7 @@ sp_object_repr_child_removed(Inkscape::XML::Node *repr, Inkscape::XML::Node *chi
  * \todo fixme:
  */
 static void
-sp_object_repr_order_changed(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *old, Inkscape::XML::Node *newer, gpointer data)
+sp_object_repr_order_changed(Inkscape::XML::Node */*repr*/, Inkscape::XML::Node *child, Inkscape::XML::Node *old, Inkscape::XML::Node *newer, gpointer data)
 {
     SPObject *object = SP_OBJECT(data);
 
@@ -939,16 +952,23 @@ sp_object_private_set(SPObject *object, unsigned int key, gchar const *value)
                 SPDocument *document=object->document;
                 SPObject *conflict=NULL;
 
-                if (value) {
-                    conflict = document->getObjectById((char const *)value);
+                gchar const *new_id = value;
+
+                if (new_id) {
+                    conflict = document->getObjectById((char const *)new_id);
                 }
+
                 if ( conflict && conflict != object ) {
-                    sp_object_ref(conflict, NULL);
-                    // give the conflicting object a new ID
-                    gchar *new_conflict_id = sp_object_get_unique_id(conflict, NULL);
-                    SP_OBJECT_REPR(conflict)->setAttribute("id", new_conflict_id);
-                    g_free(new_conflict_id);
-                    sp_object_unref(conflict, NULL);
+                    if (!document->isSeeking()) {
+                        sp_object_ref(conflict, NULL);
+                        // give the conflicting object a new ID
+                        gchar *new_conflict_id = sp_object_get_unique_id(conflict, NULL);
+                        SP_OBJECT_REPR(conflict)->setAttribute("id", new_conflict_id);
+                        g_free(new_conflict_id);
+                        sp_object_unref(conflict, NULL);
+                    } else {
+                        new_id = NULL;
+                    }
                 }
 
                 if (object->id) {
@@ -956,8 +976,8 @@ sp_object_private_set(SPObject *object, unsigned int key, gchar const *value)
                     g_free(object->id);
                 }
 
-                if (value) {
-                    object->id = g_strdup((char const*)value);
+                if (new_id) {
+                    object->id = g_strdup((char const*)new_id);
                     document->bindObjectToId(object->id, object);
                 } else {
                     object->id = NULL;
@@ -998,6 +1018,10 @@ sp_object_private_set(SPObject *object, unsigned int key, gchar const *value)
             }
             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
             break;
+        case SP_ATTR_STYLE:
+            sp_style_read_from_object(object->style, object);
+            object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
+            break;
         default:
             break;
     }
@@ -1042,7 +1066,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);
 
@@ -1059,7 +1083,7 @@ sp_object_repr_attr_changed(Inkscape::XML::Node *repr, gchar const *key, gchar c
  * Callback for content_changed node event.
  */
 static void
-sp_object_repr_content_changed(Inkscape::XML::Node *repr, gchar const *oldcontent, gchar const *newcontent, gpointer data)
+sp_object_repr_content_changed(Inkscape::XML::Node */*repr*/, gchar const */*oldcontent*/, gchar const */*newcontent*/, gpointer data)
 {
     SPObject *object = SP_OBJECT(data);
 
@@ -1090,7 +1114,7 @@ static Inkscape::XML::Node *
 sp_object_private_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
 {
     if (!repr && (flags & SP_OBJECT_WRITE_BUILD)) {
-        repr = SP_OBJECT_REPR(object)->duplicate();
+        repr = SP_OBJECT_REPR(object)->duplicate(NULL); // FIXME
         if (!( flags & SP_OBJECT_WRITE_EXT )) {
             repr->setAttribute("inkscape:collect", NULL);
         }
@@ -1110,6 +1134,38 @@ sp_object_private_write(SPObject *object, Inkscape::XML::Node *repr, guint flags
         } else {
             repr->setAttribute("inkscape:collect", NULL);
         }
+        SPStyle const *const obj_style = SP_OBJECT_STYLE(object);
+        if (obj_style) {
+            gchar *s = sp_style_write_string(obj_style, SP_STYLE_FLAG_IFSET);
+            repr->setAttribute("style", ( *s ? s : NULL ));
+            g_free(s);
+        } else {
+            /** \todo I'm not sure what to do in this case.  Bug #1165868
+             * suggests that it can arise, but the submitter doesn't know
+             * how to do so reliably.  The main two options are either
+             * leave repr's style attribute unchanged, or explicitly clear it.
+             * Must also consider what to do with property attributes for
+             * the element; see below.
+             */
+            char const *style_str = repr->attribute("style");
+            if (!style_str) {
+                style_str = "NULL";
+            }
+            g_warning("Item's style is NULL; repr style attribute is %s", style_str);
+        }
+
+        /** \note We treat object->style as authoritative.  Its effects have
+         * been written to the style attribute above; any properties that are
+         * unset we take to be deliberately unset (e.g. so that clones can
+         * override the property).
+         *
+         * Note that the below has an undesirable consequence of changing the
+         * appearance on renderers that lack CSS support (e.g. SVG tiny);
+         * possibly we should write property attributes instead of a style
+         * attribute.
+         */
+        sp_style_unset_property_attrs (object);
     }
 
     return repr;
@@ -1134,6 +1190,10 @@ SPObject::updateRepr(unsigned int flags) {
     }
 }
 
+/** Used both to create reprs in the original document, and to create 
+ *  reprs in another document (e.g. a temporary document used when
+ *  saving as "Plain SVG"
+ */
 Inkscape::XML::Node *
 SPObject::updateRepr(Inkscape::XML::Node *repr, unsigned int flags) {
     if (SP_OBJECT_IS_CLONED(this)) {
@@ -1149,9 +1209,10 @@ SPObject::updateRepr(Inkscape::XML::Node *repr, unsigned int flags) {
         g_warning("Class %s does not implement ::write", G_OBJECT_TYPE_NAME(this));
         if (!repr) {
             if (flags & SP_OBJECT_WRITE_BUILD) {
-                repr = SP_OBJECT_REPR(this)->duplicate();
+                /// \todo FIXME:  Plumb an appropriate XML::Document into this
+                repr = SP_OBJECT_REPR(this)->duplicate(NULL);
             }
-            /// \todo fixme: else probably error (Lauris) */
+            /// \todo FIXME: else probably error (Lauris) */
         } else {
             repr->mergeFrom(SP_OBJECT_REPR(this), "id");
         }
@@ -1198,6 +1259,9 @@ SPObject::requestDisplayUpdate(unsigned int flags)
     }
 }
 
+/**
+ * Update views
+ */
 void
 SPObject::updateDisplay(SPCtx *ctx, unsigned int flags)
 {
@@ -1268,7 +1332,10 @@ SPObject::requestModified(unsigned int flags)
 }
 
 /** 
- * This is what actually delivers the modified signals
+ *  Emits the MODIFIED signal with the object's flags.
+ *  The object's mflags are the original set aside during the update pass for 
+ *  later delivery here.  Once emitModified() is called, those flags don't
+ *  need to be stored any longer.
  */
 void
 SPObject::emitModified(unsigned int flags)
@@ -1302,25 +1369,25 @@ SPObject::emitModified(unsigned int flags)
  */
 
 gchar const *
-sp_object_title_get(SPObject *object)
+sp_object_title_get(SPObject */*object*/)
 {
     return NULL;
 }
 
 gchar const *
-sp_object_description_get(SPObject *object)
+sp_object_description_get(SPObject */*object*/)
 {
     return NULL;
 }
 
 unsigned int
-sp_object_title_set(SPObject *object, gchar const *title)
+sp_object_title_set(SPObject */*object*/, gchar const */*title*/)
 {
     return FALSE;
 }
 
 unsigned int
-sp_object_description_set(SPObject *object, gchar const *desc)
+sp_object_description_set(SPObject */*object*/, gchar const */*desc*/)
 {
     return FALSE;
 }
@@ -1356,9 +1423,7 @@ sp_object_setAttribute(SPObject *object, gchar const *key, gchar const *value, S
     g_return_if_fail(SP_EXCEPTION_IS_OK(ex));
 
     /// \todo fixme: Exception if object is NULL? */
-    if (!sp_repr_set_attr(object->repr, key, value)) {
-        ex->code = SP_NO_MODIFICATION_ALLOWED_ERR;
-    }
+    object->repr->setAttribute(key, value, false);
 }
 
 void
@@ -1368,9 +1433,7 @@ sp_object_removeAttribute(SPObject *object, gchar const *key, SPException *ex)
     g_return_if_fail(SP_EXCEPTION_IS_OK(ex));
 
     /// \todo fixme: Exception if object is NULL? */
-    if (!sp_repr_set_attr(object->repr, key, NULL)) {
-        ex->code = SP_NO_MODIFICATION_ALLOWED_ERR;
-    }
+    object->repr->setAttribute(key, NULL, false);
 }
 
 /* Helper */