Code

Make curvature work again by fixing a minor omission
[inkscape.git] / src / xml / simple-node.cpp
index d4a412ae63e1fb3e803283b37fba6085e1439fa0..7ecdc1b8398b004a290fc60be21fa718e59c1290 100644 (file)
@@ -1,7 +1,7 @@
-/*
- * SimpleNode - simple XML node implementation
- *
- * Copyright 2003-2005 MenTaLguY <mental@rydia.net>
+/** @file
+ * @brief Garbage collected XML node implementation
+ */
+/* Copyright 2003-2005 MenTaLguY <mental@rydia.net>
  * Copyright 2003 Nathan Hurst
  * Copyright 1999-2003 Lauris Kaplinski
  * Copyright 2000-2002 Ximian Inc.
  * of the License, or (at your option) any later version.
  *
  * See the file COPYING for details.
- *
  */
 
 #include <cstring>
 #include <string>
 #include <glib/gstrfuncs.h>
 
+#include "xml/node.h"
 #include "xml/simple-node.h"
 #include "xml/node-event-vector.h"
 #include "xml/node-fns.h"
@@ -186,14 +186,14 @@ SimpleNode::SimpleNode(SimpleNode const &node, Document *document)
     _parent = _next = NULL;
     _first_child = _last_child = NULL;
 
-    for ( Node *child = node._first_child ;
-          child != NULL ; child = child->next() )
+    for ( SimpleNode *child = node._first_child ;
+          child != NULL ; child = child->_next )
     {
-        Node *child_copy=child->duplicate(document);
+        SimpleNode *child_copy=dynamic_cast<SimpleNode *>(child->duplicate(document));
 
         child_copy->_setParent(this);
         if (_last_child) {
-            _last_child->_setNext(child_copy);
+            _last_child->_next = child_copy;
         } else {
             _first_child = child_copy;
         }
@@ -240,23 +240,23 @@ unsigned SimpleNode::position() const {
     return _parent->_childPosition(*this);
 }
 
-unsigned SimpleNode::_childPosition(Node const &child) const {
+unsigned SimpleNode::_childPosition(SimpleNode const &child) const {
     if (!_cached_positions_valid) {
         unsigned position=0;
-        for ( Node *sibling = _first_child ;
-              sibling ; sibling = sibling->next() )
+        for ( SimpleNode *sibling = _first_child ;
+              sibling ; sibling = sibling->_next )
         {
-            sibling->_setCachedPosition(position);
+            sibling->_cached_position = position;
             position++;
         }
         _cached_positions_valid = true;
     }
-    return child._cachedPosition();
+    return child._cached_position;
 }
 
 Node *SimpleNode::nthChild(unsigned index) {
-    Node *child = _first_child;
-    for ( ; index > 0 && child ; child = child->next() ) {
+    SimpleNode *child = _first_child;
+    for ( ; index > 0 && child ; child = child->_next ) {
         index--;
     }
     return child;
@@ -277,13 +277,13 @@ bool SimpleNode::matchAttributeName(gchar const *partial_name) const {
     return false;
 }
 
-void SimpleNode::_setParent(Node *parent) {
-   if (_parent) {
-        _subtree_observers.remove(_parent->_subtreeObservers());
+void SimpleNode::_setParent(SimpleNode *parent) {
+    if (_parent) {
+        _subtree_observers.remove(_parent->_subtree_observers);
     }
     _parent = parent;
     if (parent) {
-        _subtree_observers.add(parent->_subtreeObservers());
+        _subtree_observers.add(parent->_subtree_observers);
     }
 }
 
@@ -301,10 +301,7 @@ void SimpleNode::setContent(gchar const *content) {
     _content = new_content;
 
     if ( _content != old_content ) {
-        if (_document) {
-            _document->logger()->notifyContentChanged(*this, old_content, _content);
-        }
-
+        _document->logger()->notifyContentChanged(*this, old_content, _content);
         _observers.notifyContentChanged(*this, old_content, _content);
     }
 }
@@ -355,25 +352,28 @@ SimpleNode::setAttribute(gchar const *name, gchar const *value, bool const /*is_
     }
 
     if ( new_value != old_value && (!old_value || !new_value || strcmp(old_value, new_value))) {
-        if (_document) {
-            _document->logger()->notifyAttributeChanged(*this, key, old_value, new_value);
-        }
-
+        _document->logger()->notifyAttributeChanged(*this, key, old_value, new_value);
         _observers.notifyAttributeChanged(*this, key, old_value, new_value);
     }
 }
 
-void SimpleNode::addChild(Node *child, Node *ref) {
-    g_assert(child);
-    g_assert(!ref || ref->parent() == this);
-    g_assert(!child->parent());
+void SimpleNode::addChild(Node *generic_child, Node *generic_ref) {
+    g_assert(generic_child);
+    g_assert(generic_child->document() == _document);
+    g_assert(!generic_ref || generic_ref->document() == _document);
+
+    SimpleNode *child=dynamic_cast<SimpleNode *>(generic_child);
+    SimpleNode *ref=dynamic_cast<SimpleNode *>(generic_ref);
+
+    g_assert(!ref || ref->_parent == this);
+    g_assert(!child->_parent);
 
     Debug::EventTracker<DebugAddChild> tracker(*this, *child, ref);
 
-    Node *next;
+    SimpleNode *next;
     if (ref) {
-        next = ref->next();
-        ref->_setNext(child);
+        next = ref->_next;
+        ref->_next = child;
     } else {
         next = _first_child;
         _first_child = child;
@@ -383,10 +383,10 @@ void SimpleNode::addChild(Node *child, Node *ref) {
         // set cached position if possible when appending
         if (!ref) {
             // if !next && !ref, child is sole child
-            child->_setCachedPosition(0);
+            child->_cached_position = 0;
             _cached_positions_valid = true;
         } else if (_cached_positions_valid) {
-            child->_setCachedPosition(ref->_cachedPosition() + 1);
+            child->_cached_position = ref->_cached_position + 1;
         }
     } else {
         // invalidate cached positions otherwise
@@ -394,40 +394,27 @@ void SimpleNode::addChild(Node *child, Node *ref) {
     }
 
     child->_setParent(this);
-    child->_setNext(next);
+    child->_next = next;
     _child_count++;
 
-    if (_document) {
-        child->_bindDocument(*_document);
-        _document->logger()->notifyChildAdded(*this, *child, ref);
-    }
-
+    _document->logger()->notifyChildAdded(*this, *child, ref);
     _observers.notifyChildAdded(*this, *child, ref);
 }
 
-void SimpleNode::_bindDocument(Document &document) {
-    g_assert(!_document || _document == &document);
+void SimpleNode::removeChild(Node *generic_child) {
+    g_assert(generic_child);
+    g_assert(generic_child->document() == _document);
 
-    if (!_document) {
-        _document = &document;
+    SimpleNode *child=dynamic_cast<SimpleNode *>(generic_child);
+    SimpleNode *ref=dynamic_cast<SimpleNode *>(previous_node(child));
 
-        for ( Node *child = _first_child ; child != NULL ; child = child->next() ) {
-            child->_bindDocument(document);
-        }
-    }
-}
-
-void SimpleNode::removeChild(Node *child) {
-    g_assert(child);
-    g_assert(child->parent() == this);
-
-    Node *ref = ( child != _first_child ? previous_node(child) : NULL );
+    g_assert(child->_parent == this);
 
     Debug::EventTracker<DebugRemoveChild> tracker(*this, *child);
 
-    Node *next = child->next();
+    SimpleNode *next = child->_next;
     if (ref) {
-        ref->_setNext(next);
+        ref->_next = next;
     } else {
         _first_child = next;
     }
@@ -438,35 +425,38 @@ void SimpleNode::removeChild(Node *child) {
         _cached_positions_valid = false;
     }
 
-    child->_setNext(NULL);
+    child->_next = NULL;
     child->_setParent(NULL);
     _child_count--;
 
-    if (_document) {
-        _document->logger()->notifyChildRemoved(*this, *child, ref);
-    }
-
+    _document->logger()->notifyChildRemoved(*this, *child, ref);
     _observers.notifyChildRemoved(*this, *child, ref);
 }
 
-void SimpleNode::changeOrder(Node *child, Node *ref) {
-    g_return_if_fail(child);
+void SimpleNode::changeOrder(Node *generic_child, Node *generic_ref) {
+    g_assert(generic_child);
+    g_assert(generic_child->document() == this->_document);
+    g_assert(!generic_ref || generic_ref->document() == this->_document);
+
+    SimpleNode *const child=dynamic_cast<SimpleNode *>(generic_child);
+    SimpleNode *const ref=dynamic_cast<SimpleNode *>(generic_ref);
+
     g_return_if_fail(child->parent() == this);
     g_return_if_fail(child != ref);
     g_return_if_fail(!ref || ref->parent() == this);
 
-    Node *const prev = previous_node(child);
+    SimpleNode *const prev=dynamic_cast<SimpleNode *>(previous_node(child));
 
     Debug::EventTracker<DebugSetChildPosition> tracker(*this, *child, prev, ref);
 
     if (prev == ref) { return; }
 
-    Node *next;
+    SimpleNode *next;
 
     /* Remove from old position. */
-    next=child->next();
+    next = child->_next;
     if (prev) {
-        prev->_setNext(next);
+        prev->_next = next;
     } else {
         _first_child = next;
     }
@@ -476,23 +466,20 @@ void SimpleNode::changeOrder(Node *child, Node *ref) {
 
     /* Insert at new position. */
     if (ref) {
-        next = ref->next();
-        ref->_setNext(child);
+        next = ref->_next;
+        ref->_next = child;
     } else {
         next = _first_child;
         _first_child = child;
     }
-    child->_setNext(next);
+    child->_next = next;
     if (!next) {
         _last_child = child;
     }
 
     _cached_positions_valid = false;
 
-    if (_document) {
-        _document->logger()->notifyChildOrderChanged(*this, *child, prev, ref);
-    }
-
+    _document->logger()->notifyChildOrderChanged(*this, *child, prev, ref);
     _observers.notifyChildOrderChanged(*this, *child, prev, ref);
 }
 
@@ -502,9 +489,9 @@ void SimpleNode::setPosition(int pos) {
     // a position beyond the end of the list means the end of the list;
     // a negative position is the same as an infinitely large position
 
-    Node *ref=NULL;
-    for ( Node *sibling = _parent->firstChild() ;
-          sibling && pos ; sibling = sibling->next() )
+    SimpleNode *ref=NULL;
+    for ( SimpleNode *sibling = _parent->_first_child ;
+          sibling && pos ; sibling = sibling->_next )
     {
         if ( sibling != this ) {
             ref = sibling;
@@ -556,9 +543,9 @@ void SimpleNode::synthesizeEvents(NodeEventVector const *vector, void *data) {
         }
     }
     if (vector->child_added) {
-        Node *ref = NULL;
-        for ( Node *child = this->_first_child ;
-              child ; child = child->next() )
+        SimpleNode *ref = NULL;
+        for ( SimpleNode *child = this->_first_child ;
+              child ; child = child->_next )
         {
             vector->child_added(this, child, ref, data);
             ref = child;
@@ -641,4 +628,4 @@ void SimpleNode::mergeFrom(Node const *src, gchar const *key) {
   fill-column:99
   End:
 */
-// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :