Code

marker refactoring work
[inkscape.git] / src / sp-object.cpp
1 #define __SP_OBJECT_C__
2 /** \file
3  * SPObject implementation.
4  *
5  * Authors:
6  *   Lauris Kaplinski <lauris@kaplinski.com>
7  *   bulia byak <buliabyak@users.sf.net>
8  *
9  * Copyright (C) 1999-2005 authors
10  * Copyright (C) 2001-2002 Ximian, Inc.
11  *
12  * Released under GNU GPL, read the file 'COPYING' for more information
13  */
15 /** \class SPObject
16  *
17  * SPObject is an abstract base class of all of the document nodes at the
18  * SVG document level. Each SPObject subclass implements a certain SVG
19  * element node type, or is an abstract base class for different node
20  * types.  The SPObject layer is bound to the SPRepr layer, closely
21  * following the SPRepr mutations via callbacks.  During creation,
22  * SPObject parses and interprets all textual attributes and CSS style
23  * strings of the SPRepr, and later updates the internal state whenever
24  * it receives a signal about a change. The opposite is not true - there
25  * are methods manipulating SPObjects directly and such changes do not
26  * propagate to the SPRepr layer. This is important for implementation of
27  * the undo stack, animations and other features.
28  *
29  * SPObjects are bound to the higher-level container SPDocument, which
30  * provides document level functionality such as the undo stack,
31  * dictionary and so on. Source: doc/architecture.txt
32  */
35 #include "helper/sp-marshal.h"
36 #include "xml/node-event-vector.h"
37 #include "attributes.h"
38 #include "document.h"
39 #include "style.h"
40 #include "sp-object-repr.h"
41 #include "sp-root.h"
42 #include "streq.h"
43 #include "strneq.h"
44 #include "xml/repr.h"
45 #include "xml/node-fns.h"
46 #include "debug/event-tracker.h"
47 #include "debug/simple-event.h"
48 #include "debug/demangle.h"
49 #include "util/share.h"
50 #include "util/format.h"
52 #include "algorithms/longest-common-suffix.h"
53 using std::memcpy;
54 using std::strchr;
55 using std::strcmp;
56 using std::strlen;
57 using std::strstr;
59 #define noSP_OBJECT_DEBUG_CASCADE
61 #define noSP_OBJECT_DEBUG
63 #ifdef SP_OBJECT_DEBUG
64 # define debug(f, a...) { g_print("%s(%d) %s:", \
65                                   __FILE__,__LINE__,__FUNCTION__); \
66                           g_print(f, ## a); \
67                           g_print("\n"); \
68                         }
69 #else
70 # define debug(f, a...) /**/
71 #endif
73 static void sp_object_class_init(SPObjectClass *klass);
74 static void sp_object_init(SPObject *object);
75 static void sp_object_finalize(GObject *object);
77 static void sp_object_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref);
78 static void sp_object_remove_child(SPObject *object, Inkscape::XML::Node *child);
79 static void sp_object_order_changed(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref, Inkscape::XML::Node *new_ref);
81 static void sp_object_release(SPObject *object);
82 static void sp_object_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
84 static void sp_object_private_set(SPObject *object, unsigned int key, gchar const *value);
85 static Inkscape::XML::Node *sp_object_private_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
87 /* Real handlers of repr signals */
89 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);
91 static void sp_object_repr_content_changed(Inkscape::XML::Node *repr, gchar const *oldcontent, gchar const *newcontent, gpointer data);
93 static void sp_object_repr_child_added(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, gpointer data);
94 static void sp_object_repr_child_removed(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, void *data);
96 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);
98 static gchar *sp_object_get_unique_id(SPObject *object, gchar const *defid);
100 guint update_in_progress = 0; // guard against update-during-update
102 enum {RELEASE, MODIFIED, LAST_SIGNAL};
104 Inkscape::XML::NodeEventVector object_event_vector = {
105     sp_object_repr_child_added,
106     sp_object_repr_child_removed,
107     sp_object_repr_attr_changed,
108     sp_object_repr_content_changed,
109     sp_object_repr_order_changed
110 };
112 static GObjectClass *parent_class;
113 static guint object_signals[LAST_SIGNAL] = {0};
115 /**
116  * Registers the SPObject class with Gdk and returns its type number.
117  */
118 GType
119 sp_object_get_type(void)
121     static GType type = 0;
122     if (!type) {
123         GTypeInfo info = {
124             sizeof(SPObjectClass),
125             NULL, NULL,
126             (GClassInitFunc) sp_object_class_init,
127             NULL, NULL,
128             sizeof(SPObject),
129             16,
130             (GInstanceInitFunc) sp_object_init,
131             NULL
132         };
133         type = g_type_register_static(G_TYPE_OBJECT, "SPObject", &info, (GTypeFlags)0);
134     }
135     return type;
138 /**
139  * Initializes the SPObject vtable.
140  */
141 static void
142 sp_object_class_init(SPObjectClass *klass)
144     GObjectClass *object_class;
146     object_class = (GObjectClass *) klass;
148     parent_class = (GObjectClass *) g_type_class_ref(G_TYPE_OBJECT);
150     object_signals[RELEASE] =  g_signal_new("release",
151                                             G_TYPE_FROM_CLASS(klass),
152                                             (GSignalFlags)(G_SIGNAL_RUN_CLEANUP | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS),
153                                             G_STRUCT_OFFSET(SPObjectClass, release),
154                                             NULL, NULL,
155                                             sp_marshal_VOID__VOID,
156                                             G_TYPE_NONE, 0);
157     object_signals[MODIFIED] = g_signal_new("modified",
158                                             G_TYPE_FROM_CLASS(klass),
159                                             G_SIGNAL_RUN_FIRST,
160                                             G_STRUCT_OFFSET(SPObjectClass, modified),
161                                             NULL, NULL,
162                                             sp_marshal_NONE__UINT,
163                                             G_TYPE_NONE, 1, G_TYPE_UINT);
165     object_class->finalize = sp_object_finalize;
167     klass->child_added = sp_object_child_added;
168     klass->remove_child = sp_object_remove_child;
169     klass->order_changed = sp_object_order_changed;
171     klass->release = sp_object_release;
173     klass->build = sp_object_build;
175     klass->set = sp_object_private_set;
176     klass->write = sp_object_private_write;
179 /**
180  * Callback to initialize the SPObject object.
181  */
182 static void
183 sp_object_init(SPObject *object)
185     debug("id=%x, typename=%s",object, g_type_name_from_instance((GTypeInstance*)object));
187     object->hrefcount = 0;
188     object->_total_hrefcount = 0;
189     object->document = NULL;
190     object->children = object->_last_child = NULL;
191     object->parent = object->next = NULL;
192     object->repr = NULL;
193     object->id = NULL;
194     object->style = NULL;
196     object->_collection_policy = SPObject::COLLECT_WITH_PARENT;
198     new (&object->_delete_signal) sigc::signal<void, SPObject *>();
199     new (&object->_position_changed_signal) sigc::signal<void, SPObject *>();
200     object->_successor = NULL;
202     object->_label = NULL;
203     object->_default_label = NULL;
206 /**
207  * Callback to destroy all members and connections of object and itself.
208  */
209 static void
210 sp_object_finalize(GObject *object)
212     SPObject *spobject = (SPObject *)object;
214     g_free(spobject->_label);
215     g_free(spobject->_default_label);
216     spobject->_label = NULL;
217     spobject->_default_label = NULL;
219     if (spobject->_successor) {
220         sp_object_unref(spobject->_successor, NULL);
221         spobject->_successor = NULL;
222     }
224     if (((GObjectClass *) (parent_class))->finalize) {
225         (* ((GObjectClass *) (parent_class))->finalize)(object);
226     }
228     spobject->_delete_signal.~signal();
229     spobject->_position_changed_signal.~signal();
232 namespace {
234 namespace Debug = Inkscape::Debug;
235 namespace Util = Inkscape::Util;
237 typedef Debug::SimpleEvent<Debug::Event::REFCOUNT> BaseRefCountEvent;
239 class RefCountEvent : public BaseRefCountEvent {
240 public:
241     RefCountEvent(SPObject *object, int bias, Util::ptr_shared<char> name)
242     : BaseRefCountEvent(name)
243     {
244         _addProperty("object", Util::format("%p", object));
245         _addProperty("class", Debug::demangle(g_type_name(G_TYPE_FROM_INSTANCE(object))));
246         _addProperty("new-refcount", Util::format("%d", G_OBJECT(object)->ref_count + bias));
247     }
248 };
250 class RefEvent : public RefCountEvent {
251 public:
252     RefEvent(SPObject *object)
253     : RefCountEvent(object, 1, Util::share_static_string("sp-object-ref"))
254     {}
255 };
257 class UnrefEvent : public RefCountEvent {
258 public:
259     UnrefEvent(SPObject *object)
260     : RefCountEvent(object, -1, Util::share_static_string("sp-object-unref"))
261     {}
262 };
266 /**
267  * Increase reference count of object, with possible debugging.
268  *
269  * \param owner If non-NULL, make debug log entry.
270  * \return object, NULL is error.
271  * \pre object points to real object
272  */
273 SPObject *
274 sp_object_ref(SPObject *object, SPObject *owner)
276     g_return_val_if_fail(object != NULL, NULL);
277     g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
278     g_return_val_if_fail(!owner || SP_IS_OBJECT(owner), NULL);
280     Inkscape::Debug::EventTracker<RefEvent> tracker(object);
281     g_object_ref(G_OBJECT(object));
282     return object;
285 /**
286  * Decrease reference count of object, with possible debugging and
287  * finalization.
288  *
289  * \param owner If non-NULL, make debug log entry.
290  * \return always NULL
291  * \pre object points to real object
292  */
293 SPObject *
294 sp_object_unref(SPObject *object, SPObject *owner)
296     g_return_val_if_fail(object != NULL, NULL);
297     g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
298     g_return_val_if_fail(!owner || SP_IS_OBJECT(owner), NULL);
300     Inkscape::Debug::EventTracker<UnrefEvent> tracker(object);
301     g_object_unref(G_OBJECT(object));
302     return NULL;
305 /**
306  * Increase weak refcount.
307  *
308  * Hrefcount is used for weak references, for example, to
309  * determine whether any graphical element references a certain gradient
310  * node.
311  * \param owner Ignored.
312  * \return object, NULL is error
313  * \pre object points to real object
314  */
315 SPObject *
316 sp_object_href(SPObject *object, gpointer owner)
318     g_return_val_if_fail(object != NULL, NULL);
319     g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
321     object->hrefcount++;
322     object->_updateTotalHRefCount(1);
324     return object;
327 /**
328  * Decrease weak refcount.
329  *
330  * Hrefcount is used for weak references, for example, to determine whether
331  * any graphical element references a certain gradient node.
332  * \param owner Ignored.
333  * \return always NULL
334  * \pre object points to real object and hrefcount>0
335  */
336 SPObject *
337 sp_object_hunref(SPObject *object, gpointer owner)
339     g_return_val_if_fail(object != NULL, NULL);
340     g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
341     g_return_val_if_fail(object->hrefcount > 0, NULL);
343     object->hrefcount--;
344     object->_updateTotalHRefCount(-1);
346     return NULL;
349 /**
350  * Adds increment to _total_hrefcount of object and its parents.
351  */
352 void
353 SPObject::_updateTotalHRefCount(int increment) {
354     SPObject *topmost_collectable = NULL;
355     for ( SPObject *iter = this ; iter ; iter = SP_OBJECT_PARENT(iter) ) {
356         iter->_total_hrefcount += increment;
357         if ( iter->_total_hrefcount < iter->hrefcount ) {
358             g_critical("HRefs overcounted");
359         }
360         if ( iter->_total_hrefcount == 0 &&
361              iter->_collection_policy != COLLECT_WITH_PARENT )
362         {
363             topmost_collectable = iter;
364         }
365     }
366     if (topmost_collectable) {
367         topmost_collectable->requestOrphanCollection();
368     }
371 /**
372  * True if object is non-NULL and this is some in/direct parent of object.
373  */
374 bool
375 SPObject::isAncestorOf(SPObject const *object) const {
376     g_return_val_if_fail(object != NULL, false);
377     object = SP_OBJECT_PARENT(object);
378     while (object) {
379         if ( object == this ) {
380             return true;
381         }
382         object = SP_OBJECT_PARENT(object);
383     }
384     return false;
387 namespace {
389 bool same_objects(SPObject const &a, SPObject const &b) {
390     return &a == &b;
395 /**
396  * Returns youngest object being parent to this and object.
397  */
398 SPObject const *
399 SPObject::nearestCommonAncestor(SPObject const *object) const {
400     g_return_val_if_fail(object != NULL, NULL);
402     using Inkscape::Algorithms::longest_common_suffix;
403     return longest_common_suffix<SPObject::ConstParentIterator>(this, object, NULL, &same_objects);
406 SPObject const *AncestorSon(SPObject const *obj, SPObject const *ancestor) {
407     if (obj == NULL || ancestor == NULL)
408         return NULL;
409     if (SP_OBJECT_PARENT(obj) == ancestor)
410         return obj;
411     return AncestorSon(SP_OBJECT_PARENT(obj), ancestor);
414 /**
415  * Compares height of objects in tree.
416  *
417  * Works for different-parent objects, so long as they have a common ancestor.
418  * \return \verbatim
419  *    0    positions are equivalent
420  *    1    first object's position is greater than the second
421  *   -1    first object's position is less than the second   \endverbatim
422  */
423 int
424 sp_object_compare_position(SPObject const *first, SPObject const *second)
426     if (first == second) return 0;
428     SPObject const *ancestor = first->nearestCommonAncestor(second);
429     if (ancestor == NULL) return 0; // cannot compare, no common ancestor!
431     // we have an object and its ancestor (should not happen when sorting selection)
432     if (ancestor == first)
433         return 1;
434     if (ancestor == second)
435         return -1;
437     SPObject const *to_first = AncestorSon(first, ancestor);
438     SPObject const *to_second = AncestorSon(second, ancestor);
440     g_assert(SP_OBJECT_PARENT(to_second) == SP_OBJECT_PARENT(to_first));
442     return sp_repr_compare_position(SP_OBJECT_REPR(to_first), SP_OBJECT_REPR(to_second));
446 /**
447  * Append repr as child of this object.
448  * \pre this is not a cloned object
449  */
450 SPObject *
451 SPObject::appendChildRepr(Inkscape::XML::Node *repr) {
452     if (!SP_OBJECT_IS_CLONED(this)) {
453         SP_OBJECT_REPR(this)->appendChild(repr);
454         return SP_OBJECT_DOCUMENT(this)->getObjectByRepr(repr);
455     } else {
456         g_critical("Attempt to append repr as child of cloned object");
457         return NULL;
458     }
461 /**
462  * Retrieves the children as a GSList object, optionally ref'ing the children
463  * in the process, if add_ref is specified.
464  */
465 GSList *SPObject::childList(bool add_ref, Action) {
466     GSList *l = NULL;
467     for (SPObject *child = sp_object_first_child(this) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
468         if (add_ref)
469             g_object_ref (G_OBJECT (child));
471         l = g_slist_prepend (l, child);
472     }
473     return l;
477 /** Gets the label property for the object or a default if no label
478  *  is defined.
479  */
480 gchar const *
481 SPObject::label() const {
482     return _label;
485 /** Returns a default label property for the object. */
486 gchar const *
487 SPObject::defaultLabel() const {
488     if (_label) {
489         return _label;
490     } else {
491         if (!_default_label) {
492             gchar const *id=SP_OBJECT_ID(this);
493             if (id) {
494                 _default_label = g_strdup_printf("#%s", id);
495             } else {
496                 _default_label = g_strdup_printf("<%s>", SP_OBJECT_REPR(this)->name());
497             }
498         }
499         return _default_label;
500     }
503 /** Sets the label property for the object */
504 void
505 SPObject::setLabel(gchar const *label) {
506     SP_OBJECT_REPR(this)->setAttribute("inkscape:label", label, false);
510 /** Queues the object for orphan collection */
511 void
512 SPObject::requestOrphanCollection() {
513     g_return_if_fail(document != NULL);
514     document->queueForOrphanCollection(this);
516     /** \todo
517      * This is a temporary hack added to make fill&stroke rebuild its
518      * gradient list when the defs are vacuumed.  gradient-vector.cpp
519      * listens to the modified signal on defs, and now we give it that
520      * signal.  Mental says that this should be made automatic by
521      * merging SPObjectGroup with SPObject; SPObjectGroup would issue
522      * this signal automatically. Or maybe just derive SPDefs from
523      * SPObjectGroup?
524      */
526     this->requestModified(SP_OBJECT_CHILD_MODIFIED_FLAG);
529 /** Sends the delete signal to all children of this object recursively */
530 void
531 SPObject::_sendDeleteSignalRecursive() {
532     for (SPObject *child = sp_object_first_child(this); child; child = SP_OBJECT_NEXT(child)) {
533         child->_delete_signal.emit(child);
534         child->_sendDeleteSignalRecursive();
535     }
538 /**
539  * Deletes the object reference, unparenting it from its parent.
540  *
541  * If the \a propagate parameter is set to true, it emits a delete
542  * signal.  If the \a propagate_descendants parameter is true, it
543  * recursively sends the delete signal to children.
544  */
545 void
546 SPObject::deleteObject(bool propagate, bool propagate_descendants)
548     sp_object_ref(this, NULL);
549     if (propagate) {
550         _delete_signal.emit(this);
551     }
552     if (propagate_descendants) {
553         this->_sendDeleteSignalRecursive();
554     }
556     Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
557     if (repr && sp_repr_parent(repr)) {
558         sp_repr_unparent(repr);
559     }
561     if (_successor) {
562         _successor->deleteObject(propagate, propagate_descendants);
563     }
564     sp_object_unref(this, NULL);
567 /**
568  * Put object into object tree, under parent, and behind prev;
569  * also update object's XML space.
570  */
571 void
572 sp_object_attach(SPObject *parent, SPObject *object, SPObject *prev)
574     g_return_if_fail(parent != NULL);
575     g_return_if_fail(SP_IS_OBJECT(parent));
576     g_return_if_fail(object != NULL);
577     g_return_if_fail(SP_IS_OBJECT(object));
578     g_return_if_fail(!prev || SP_IS_OBJECT(prev));
579     g_return_if_fail(!prev || prev->parent == parent);
580     g_return_if_fail(!object->parent);
582     sp_object_ref(object, parent);
583     object->parent = parent;
584     parent->_updateTotalHRefCount(object->_total_hrefcount);
586     SPObject *next;
587     if (prev) {
588         next = prev->next;
589         prev->next = object;
590     } else {
591         next = parent->children;
592         parent->children = object;
593     }
594     object->next = next;
595     if (!next) {
596         parent->_last_child = object;
597     }
598     if (!object->xml_space.set)
599         object->xml_space.value = parent->xml_space.value;
602 /**
603  * In list of object's siblings, move object behind prev.
604  */
605 void
606 sp_object_reorder(SPObject *object, SPObject *prev) {
607     g_return_if_fail(object != NULL);
608     g_return_if_fail(SP_IS_OBJECT(object));
609     g_return_if_fail(object->parent != NULL);
610     g_return_if_fail(object != prev);
611     g_return_if_fail(!prev || SP_IS_OBJECT(prev));
612     g_return_if_fail(!prev || prev->parent == object->parent);
614     SPObject *const parent=object->parent;
616     SPObject *old_prev=NULL;
617     for ( SPObject *child = parent->children ; child && child != object ;
618           child = child->next )
619     {
620         old_prev = child;
621     }
623     SPObject *next=object->next;
624     if (old_prev) {
625         old_prev->next = next;
626     } else {
627         parent->children = next;
628     }
629     if (!next) {
630         parent->_last_child = old_prev;
631     }
632     if (prev) {
633         next = prev->next;
634         prev->next = object;
635     } else {
636         next = parent->children;
637         parent->children = object;
638     }
639     object->next = next;
640     if (!next) {
641         parent->_last_child = object;
642     }
645 /**
646  * Remove object from parent's children, release and unref it.
647  */
648 void
649 sp_object_detach(SPObject *parent, SPObject *object) {
650     g_return_if_fail(parent != NULL);
651     g_return_if_fail(SP_IS_OBJECT(parent));
652     g_return_if_fail(object != NULL);
653     g_return_if_fail(SP_IS_OBJECT(object));
654     g_return_if_fail(object->parent == parent);
656     sp_object_invoke_release(object);
658     SPObject *prev=NULL;
659     for ( SPObject *child = parent->children ; child && child != object ;
660           child = child->next )
661     {
662         prev = child;
663     }
665     SPObject *next=object->next;
666     if (prev) {
667         prev->next = next;
668     } else {
669         parent->children = next;
670     }
671     if (!next) {
672         parent->_last_child = prev;
673     }
675     object->next = NULL;
676     object->parent = NULL;
678     parent->_updateTotalHRefCount(-object->_total_hrefcount);
679     sp_object_unref(object, parent);
682 /**
683  * Return object's child whose node pointer equals repr.
684  */
685 SPObject *
686 sp_object_get_child_by_repr(SPObject *object, Inkscape::XML::Node *repr)
688     g_return_val_if_fail(object != NULL, NULL);
689     g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
690     g_return_val_if_fail(repr != NULL, NULL);
692     if (object->_last_child && SP_OBJECT_REPR(object->_last_child) == repr)
693         return object->_last_child;   // optimization for common scenario
694     for ( SPObject *child = object->children ; child ; child = child->next ) {
695         if ( SP_OBJECT_REPR(child) == repr ) {
696             return child;
697         }
698     }
700     return NULL;
703 /**
704  * Callback for child_added event.
705  * Invoked whenever the given mutation event happens in the XML tree.
706  */
707 static void
708 sp_object_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
710     GType type = sp_repr_type_lookup(child);
711     if (!type) {
712         return;
713     }
714     SPObject *ochild = SP_OBJECT(g_object_new(type, 0));
715     SPObject *prev = ref ? sp_object_get_child_by_repr(object, ref) : NULL;
716     sp_object_attach(object, ochild, prev);
717     sp_object_unref(ochild, NULL);
719     sp_object_invoke_build(ochild, object->document, child, SP_OBJECT_IS_CLONED(object));
722 /**
723  * Removes, releases and unrefs all children of object.
724  *
725  * This is the opposite of build. It has to be invoked as soon as the
726  * object is removed from the tree, even if it is still alive according
727  * to reference count. The frontend unregisters the object from the
728  * document and releases the SPRepr bindings; implementations should free
729  * state data and release all child objects.  Invoking release on
730  * SPRoot destroys the whole document tree.
731  * \see sp_object_build()
732  */
733 static void sp_object_release(SPObject *object)
735     debug("id=%x, typename=%s", object, g_type_name_from_instance((GTypeInstance*)object));
736     while (object->children) {
737         sp_object_detach(object, object->children);
738     }
741 /**
742  * Remove object's child whose node equals repr, release and
743  * unref it.
744  *
745  * Invoked whenever the given mutation event happens in the XML
746  * tree, BEFORE removal from the XML tree happens, so grouping
747  * objects can safely release the child data.
748  */
749 static void
750 sp_object_remove_child(SPObject *object, Inkscape::XML::Node *child)
752     debug("id=%x, typename=%s", object, g_type_name_from_instance((GTypeInstance*)object));
753     SPObject *ochild = sp_object_get_child_by_repr(object, child);
754     g_return_if_fail(ochild != NULL);
755     sp_object_detach(object, ochild);
758 /**
759  * Move object corresponding to child after sibling object corresponding
760  * to new_ref.
761  * Invoked whenever the given mutation event happens in the XML tree.
762  * \param old_ref Ignored
763  */
764 static void sp_object_order_changed(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref,
765                                     Inkscape::XML::Node *new_ref)
767     SPObject *ochild = sp_object_get_child_by_repr(object, child);
768     g_return_if_fail(ochild != NULL);
769     SPObject *prev = new_ref ? sp_object_get_child_by_repr(object, new_ref) : NULL;
770     sp_object_reorder(ochild, prev);
771     ochild->_position_changed_signal.emit(ochild);
774 /**
775  * Virtual build callback.
776  *
777  * This has to be invoked immediately after creation of an SPObject. The
778  * frontend method ensures that the new object is properly attached to
779  * the document and repr; implementation then will parse all of the attributes,
780  * generate the children objects and so on.  Invoking build on the SPRoot
781  * object results in creation of the whole document tree (this is, what
782  * SPDocument does after the creation of the XML tree).
783  * \see sp_object_release()
784  */
785 static void
786 sp_object_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
788     /* Nothing specific here */
789     debug("id=%x, typename=%s", object, g_type_name_from_instance((GTypeInstance*)object));
791     sp_object_read_attr(object, "xml:space");
792     sp_object_read_attr(object, "inkscape:label");
793     sp_object_read_attr(object, "inkscape:collect");
795     for (Inkscape::XML::Node *rchild = repr->firstChild() ; rchild != NULL; rchild = rchild->next()) {
796         GType type = sp_repr_type_lookup(rchild);
797         if (!type) {
798             continue;
799         }
800         SPObject *child = SP_OBJECT(g_object_new(type, 0));
801         sp_object_attach(object, child, object->lastChild());
802         sp_object_unref(child, NULL);
803         sp_object_invoke_build(child, document, rchild, SP_OBJECT_IS_CLONED(object));
804     }
807 void
808 sp_object_invoke_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr, unsigned int cloned)
810     debug("id=%x, typename=%s", object, g_type_name_from_instance((GTypeInstance*)object));
812     g_assert(object != NULL);
813     g_assert(SP_IS_OBJECT(object));
814     g_assert(document != NULL);
815     g_assert(repr != NULL);
817     g_assert(object->document == NULL);
818     g_assert(object->repr == NULL);
819     g_assert(object->id == NULL);
821     /* Bookkeeping */
823     object->document = document;
824     object->repr = repr;
825     Inkscape::GC::anchor(repr);
826     object->cloned = cloned;
828     if (!SP_OBJECT_IS_CLONED(object)) {
829         object->document->bindObjectToRepr(object->repr, object);
831         if (Inkscape::XML::id_permitted(object->repr)) {
832             /* If we are not cloned, force unique id */
833             gchar const *id = object->repr->attribute("id");
834             gchar *realid = sp_object_get_unique_id(object, id);
835             g_assert(realid != NULL);
837             object->document->bindObjectToId(realid, object);
838             object->id = realid;
840             /* Redefine ID, if required */
841             if ((id == NULL) || (strcmp(id, realid) != 0)) {
842                 gboolean undo_sensitive=sp_document_get_undo_sensitive(document);
843                 sp_document_set_undo_sensitive(document, FALSE);
844                 object->repr->setAttribute("id", realid);
845                 sp_document_set_undo_sensitive(document, undo_sensitive);
846             }
847         }
848     } else {
849         g_assert(object->id == NULL);
850     }
852     /* Invoke derived methods, if any */
854     if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->build) {
855         (*((SPObjectClass *) G_OBJECT_GET_CLASS(object))->build)(object, document, repr);
856     }
858     /* Signalling (should be connected AFTER processing derived methods */
859     sp_repr_add_listener(repr, &object_event_vector, object);
862 void
863 sp_object_invoke_release(SPObject *object)
865     g_assert(object != NULL);
866     g_assert(SP_IS_OBJECT(object));
868     g_assert(object->document);
869     g_assert(object->repr);
871     sp_repr_remove_listener_by_data(object->repr, object);
873     g_signal_emit(G_OBJECT(object), object_signals[RELEASE], 0);
875     /* all hrefs should be released by the "release" handlers */
876     g_assert(object->hrefcount == 0);
878     if (!SP_OBJECT_IS_CLONED(object)) {
879         if (object->id) {
880             object->document->bindObjectToId(object->id, NULL);
881         }
882         g_free(object->id);
883         object->id = NULL;
885         g_free(object->_default_label);
886         object->_default_label = NULL;
888         object->document->bindObjectToRepr(object->repr, NULL);
889     } else {
890         g_assert(!object->id);
891     }
893     if (object->style) {
894         object->style = sp_style_unref(object->style);
895     }
897     Inkscape::GC::release(object->repr);
899     object->document = NULL;
900     object->repr = NULL;
903 /**
904  * Callback for child_added node event.
905  */
906 static void
907 sp_object_repr_child_added(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, gpointer data)
909     SPObject *object = SP_OBJECT(data);
911     if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->child_added)
912         (*((SPObjectClass *)G_OBJECT_GET_CLASS(object))->child_added)(object, child, ref);
915 /**
916  * Callback for remove_child node event.
917  */
918 static void
919 sp_object_repr_child_removed(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, gpointer data)
921     SPObject *object = SP_OBJECT(data);
923     if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->remove_child) {
924         (* ((SPObjectClass *)G_OBJECT_GET_CLASS(object))->remove_child)(object, child);
925     }
928 /**
929  * Callback for order_changed node event.
930  *
931  * \todo fixme:
932  */
933 static void
934 sp_object_repr_order_changed(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *old, Inkscape::XML::Node *newer, gpointer data)
936     SPObject *object = SP_OBJECT(data);
938     if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->order_changed) {
939         (* ((SPObjectClass *)G_OBJECT_GET_CLASS(object))->order_changed)(object, child, old, newer);
940     }
943 /**
944  * Callback for set event.
945  */
946 static void
947 sp_object_private_set(SPObject *object, unsigned int key, gchar const *value)
949     g_assert(key != SP_ATTR_INVALID);
951     switch (key) {
952         case SP_ATTR_ID:
953             if ( !SP_OBJECT_IS_CLONED(object) && object->repr->type() == Inkscape::XML::ELEMENT_NODE ) {
954                 SPDocument *document=object->document;
955                 SPObject *conflict=NULL;
957                 if (value) {
958                     conflict = document->getObjectById((char const *)value);
959                 }
960                 if ( conflict && conflict != object ) {
961                     sp_object_ref(conflict, NULL);
962                     // give the conflicting object a new ID
963                     gchar *new_conflict_id = sp_object_get_unique_id(conflict, NULL);
964                     SP_OBJECT_REPR(conflict)->setAttribute("id", new_conflict_id);
965                     g_free(new_conflict_id);
966                     sp_object_unref(conflict, NULL);
967                 }
969                 if (object->id) {
970                     document->bindObjectToId(object->id, NULL);
971                     g_free(object->id);
972                 }
974                 if (value) {
975                     object->id = g_strdup((char const*)value);
976                     document->bindObjectToId(object->id, object);
977                 } else {
978                     object->id = NULL;
979                 }
981                 g_free(object->_default_label);
982                 object->_default_label = NULL;
983             }
984             break;
985         case SP_ATTR_INKSCAPE_LABEL:
986             g_free(object->_label);
987             if (value) {
988                 object->_label = g_strdup(value);
989             } else {
990                 object->_label = NULL;
991             }
992             g_free(object->_default_label);
993             object->_default_label = NULL;
994             break;
995         case SP_ATTR_INKSCAPE_COLLECT:
996             if ( value && !strcmp(value, "always") ) {
997                 object->setCollectionPolicy(SPObject::ALWAYS_COLLECT);
998             } else {
999                 object->setCollectionPolicy(SPObject::COLLECT_WITH_PARENT);
1000             }
1001             break;
1002         case SP_ATTR_XML_SPACE:
1003             if (value && !strcmp(value, "preserve")) {
1004                 object->xml_space.value = SP_XML_SPACE_PRESERVE;
1005                 object->xml_space.set = TRUE;
1006             } else if (value && !strcmp(value, "default")) {
1007                 object->xml_space.value = SP_XML_SPACE_DEFAULT;
1008                 object->xml_space.set = TRUE;
1009             } else if (object->parent) {
1010                 SPObject *parent;
1011                 parent = object->parent;
1012                 object->xml_space.value = parent->xml_space.value;
1013             }
1014             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
1015             break;
1016         default:
1017             break;
1018     }
1021 /**
1022  * Call virtual set() function of object.
1023  */
1024 void
1025 sp_object_set(SPObject *object, unsigned int key, gchar const *value)
1027     g_assert(object != NULL);
1028     g_assert(SP_IS_OBJECT(object));
1030     if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->set) {
1031         ((SPObjectClass *) G_OBJECT_GET_CLASS(object))->set(object, key, value);
1032     }
1035 /**
1036  * Read value of key attribute from XML node into object.
1037  */
1038 void
1039 sp_object_read_attr(SPObject *object, gchar const *key)
1041     g_assert(object != NULL);
1042     g_assert(SP_IS_OBJECT(object));
1043     g_assert(key != NULL);
1045     g_assert(object->repr != NULL);
1047     unsigned int keyid = sp_attribute_lookup(key);
1048     if (keyid != SP_ATTR_INVALID) {
1049         /* Retrieve the 'key' attribute from the object's XML representation */
1050         gchar const *value = object->repr->attribute(key);
1052         sp_object_set(object, keyid, value);
1053     }
1056 /**
1057  * Callback for attr_changed node event.
1058  */
1059 static void
1060 sp_object_repr_attr_changed(Inkscape::XML::Node *repr, gchar const *key, gchar const *oldval, gchar const *newval, bool is_interactive, gpointer data)
1062     SPObject *object = SP_OBJECT(data);
1064     sp_object_read_attr(object, key);
1066     // manual changes to extension attributes require the normal
1067     // attributes, which depend on them, to be updated immediately
1068     if (is_interactive) {
1069         object->updateRepr(repr, 0);
1070     }
1073 /**
1074  * Callback for content_changed node event.
1075  */
1076 static void
1077 sp_object_repr_content_changed(Inkscape::XML::Node *repr, gchar const *oldcontent, gchar const *newcontent, gpointer data)
1079     SPObject *object = SP_OBJECT(data);
1081     if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->read_content)
1082         (*((SPObjectClass *) G_OBJECT_GET_CLASS(object))->read_content)(object);
1085 /**
1086  * Return string representation of space value.
1087  */
1088 static gchar const*
1089 sp_xml_get_space_string(unsigned int space)
1091     switch (space) {
1092         case SP_XML_SPACE_DEFAULT:
1093             return "default";
1094         case SP_XML_SPACE_PRESERVE:
1095             return "preserve";
1096         default:
1097             return NULL;
1098     }
1101 /**
1102  * Callback for write event.
1103  */
1104 static Inkscape::XML::Node *
1105 sp_object_private_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
1107     if (!repr && (flags & SP_OBJECT_WRITE_BUILD)) {
1108         repr = SP_OBJECT_REPR(object)->duplicate();
1109         if (!( flags & SP_OBJECT_WRITE_EXT )) {
1110             repr->setAttribute("inkscape:collect", NULL);
1111         }
1112     } else {
1113         repr->setAttribute("id", object->id);
1115         if (object->xml_space.set) {
1116             char const *xml_space;
1117             xml_space = sp_xml_get_space_string(object->xml_space.value);
1118             repr->setAttribute("xml:space", xml_space);
1119         }
1121         if ( flags & SP_OBJECT_WRITE_EXT &&
1122              object->collectionPolicy() == SPObject::ALWAYS_COLLECT )
1123         {
1124             repr->setAttribute("inkscape:collect", "always");
1125         } else {
1126             repr->setAttribute("inkscape:collect", NULL);
1127         }
1128     }
1130     return repr;
1133 /**
1134  * Update this object's XML node with flags value.
1135  */
1136 Inkscape::XML::Node *
1137 SPObject::updateRepr(unsigned int flags) {
1138     if (!SP_OBJECT_IS_CLONED(this)) {
1139         Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
1140         if (repr) {
1141             return updateRepr(repr, flags);
1142         } else {
1143             g_critical("Attempt to update non-existent repr");
1144             return NULL;
1145         }
1146     } else {
1147         /* cloned objects have no repr */
1148         return NULL;
1149     }
1152 Inkscape::XML::Node *
1153 SPObject::updateRepr(Inkscape::XML::Node *repr, unsigned int flags) {
1154     if (SP_OBJECT_IS_CLONED(this)) {
1155         /* cloned objects have no repr */
1156         return NULL;
1157     }
1158     if (((SPObjectClass *) G_OBJECT_GET_CLASS(this))->write) {
1159         if (!(flags & SP_OBJECT_WRITE_BUILD) && !repr) {
1160             repr = SP_OBJECT_REPR(this);
1161         }
1162         return ((SPObjectClass *) G_OBJECT_GET_CLASS(this))->write(this, repr, flags);
1163     } else {
1164         g_warning("Class %s does not implement ::write", G_OBJECT_TYPE_NAME(this));
1165         if (!repr) {
1166             if (flags & SP_OBJECT_WRITE_BUILD) {
1167                 repr = SP_OBJECT_REPR(this)->duplicate();
1168             }
1169             /// \todo fixme: else probably error (Lauris) */
1170         } else {
1171             repr->mergeFrom(SP_OBJECT_REPR(this), "id");
1172         }
1173         return repr;
1174     }
1177 /* Modification */
1179 /**
1180  * Add \a flags to \a object's as dirtiness flags, and
1181  * recursively add CHILD_MODIFIED flag to
1182  * parent and ancestors (as far up as necessary).
1183  */
1184 void
1185 SPObject::requestDisplayUpdate(unsigned int flags)
1187     g_return_if_fail( this->document != NULL );
1189     if (update_in_progress) {
1190         g_print("WARNING: Requested update while update in progress, counter = %d\n", update_in_progress);
1191     }
1193     /* requestModified must be used only to set one of SP_OBJECT_MODIFIED_FLAG or
1194      * SP_OBJECT_CHILD_MODIFIED_FLAG */
1195     g_return_if_fail(!(flags & SP_OBJECT_PARENT_MODIFIED_FLAG));
1196     g_return_if_fail((flags & SP_OBJECT_MODIFIED_FLAG) || (flags & SP_OBJECT_CHILD_MODIFIED_FLAG));
1197     g_return_if_fail(!((flags & SP_OBJECT_MODIFIED_FLAG) && (flags & SP_OBJECT_CHILD_MODIFIED_FLAG)));
1199     bool already_propagated = (!(this->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG)));
1201     this->uflags |= flags;
1203     /* If requestModified has already been called on this object or one of its children, then we
1204      * don't need to set CHILD_MODIFIED on our ancestors because it's already been done.
1205      */
1206     if (already_propagated) {
1207         SPObject *parent = SP_OBJECT_PARENT(this);
1208         if (parent) {
1209             parent->requestDisplayUpdate(SP_OBJECT_CHILD_MODIFIED_FLAG);
1210         } else {
1211             sp_document_request_modified(SP_OBJECT_DOCUMENT(this));
1212         }
1213     }
1216 void
1217 SPObject::updateDisplay(SPCtx *ctx, unsigned int flags)
1219     g_return_if_fail(!(flags & ~SP_OBJECT_MODIFIED_CASCADE));
1221     update_in_progress ++;
1223 #ifdef SP_OBJECT_DEBUG_CASCADE
1224     g_print("Update %s:%s %x %x %x\n", g_type_name_from_instance((GTypeInstance *) this), SP_OBJECT_ID(this), flags, this->uflags, this->mflags);
1225 #endif
1227     /* Get this flags */
1228     flags |= this->uflags;
1229     /* Copy flags to modified cascade for later processing */
1230     this->mflags |= this->uflags;
1231     /* We have to clear flags here to allow rescheduling update */
1232     this->uflags = 0;
1234     // Merge style if we have good reasons to think that parent style is changed */
1235     /** \todo
1236      * I am not sure whether we should check only propagated
1237      * flag. We are currently assuming that style parsing is
1238      * done immediately. I think this is correct (Lauris).
1239      */
1240     if ((flags & SP_OBJECT_STYLE_MODIFIED_FLAG) && (flags & SP_OBJECT_PARENT_MODIFIED_FLAG)) {
1241         if (this->style && this->parent) {
1242             sp_style_merge_from_parent(this->style, this->parent->style);
1243         }
1244     }
1246     if (((SPObjectClass *) G_OBJECT_GET_CLASS(this))->update)
1247         ((SPObjectClass *) G_OBJECT_GET_CLASS(this))->update(this, ctx, flags);
1249     update_in_progress --;
1252 /**
1253  * Request modified always bubbles *up* the tree, as opposed to 
1254  * request display update, which trickles down and relies on the 
1255  * flags set during this pass...
1256  */
1257 void
1258 SPObject::requestModified(unsigned int flags)
1260     g_return_if_fail( this->document != NULL );
1262     /* requestModified must be used only to set one of SP_OBJECT_MODIFIED_FLAG or
1263      * SP_OBJECT_CHILD_MODIFIED_FLAG */
1264     g_return_if_fail(!(flags & SP_OBJECT_PARENT_MODIFIED_FLAG));
1265     g_return_if_fail((flags & SP_OBJECT_MODIFIED_FLAG) || (flags & SP_OBJECT_CHILD_MODIFIED_FLAG));
1266     g_return_if_fail(!((flags & SP_OBJECT_MODIFIED_FLAG) && (flags & SP_OBJECT_CHILD_MODIFIED_FLAG)));
1268     bool already_propagated = (!(this->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG)));
1270     this->mflags |= flags;
1272     /* If requestModified has already been called on this object or one of its children, then we
1273      * don't need to set CHILD_MODIFIED on our ancestors because it's already been done.
1274      */
1275     if (already_propagated) {
1276         SPObject *parent=SP_OBJECT_PARENT(this);
1277         if (parent) {
1278             parent->requestModified(SP_OBJECT_CHILD_MODIFIED_FLAG);
1279         } else {
1280             sp_document_request_modified(SP_OBJECT_DOCUMENT(this));
1281         }
1282     }
1285 /** 
1286  * This is what actually delivers the modified signals
1287  */
1288 void
1289 SPObject::emitModified(unsigned int flags)
1291     /* only the MODIFIED_CASCADE flag is legal here */
1292     g_return_if_fail(!(flags & ~SP_OBJECT_MODIFIED_CASCADE));
1294 #ifdef SP_OBJECT_DEBUG_CASCADE
1295     g_print("Modified %s:%s %x %x %x\n", g_type_name_from_instance((GTypeInstance *) this), SP_OBJECT_ID(this), flags, this->uflags, this->mflags);
1296 #endif
1298     flags |= this->mflags;
1299     /* We have to clear mflags beforehand, as signal handlers may
1300      * make changes and therefore queue new modification notifications
1301      * themselves. */
1302     this->mflags = 0;
1304     g_object_ref(G_OBJECT(this));
1305     g_signal_emit(G_OBJECT(this), object_signals[MODIFIED], 0, flags);
1306     g_object_unref(G_OBJECT(this));
1309 /*
1310  * Get and set descriptive parameters
1311  *
1312  * These are inefficent, so they are not intended to be used interactively
1313  */
1315 gchar const *
1316 sp_object_title_get(SPObject *object)
1318     return NULL;
1321 gchar const *
1322 sp_object_description_get(SPObject *object)
1324     return NULL;
1327 unsigned int
1328 sp_object_title_set(SPObject *object, gchar const *title)
1330     return FALSE;
1333 unsigned int
1334 sp_object_description_set(SPObject *object, gchar const *desc)
1336     return FALSE;
1339 gchar const *
1340 sp_object_tagName_get(SPObject const *object, SPException *ex)
1342     /* If exception is not clear, return */
1343     if (!SP_EXCEPTION_IS_OK(ex)) {
1344         return NULL;
1345     }
1347     /// \todo fixme: Exception if object is NULL? */
1348     return object->repr->name();
1351 gchar const *
1352 sp_object_getAttribute(SPObject const *object, gchar const *key, SPException *ex)
1354     /* If exception is not clear, return */
1355     if (!SP_EXCEPTION_IS_OK(ex)) {
1356         return NULL;
1357     }
1359     /// \todo fixme: Exception if object is NULL? */
1360     return (gchar const *) object->repr->attribute(key);
1363 void
1364 sp_object_setAttribute(SPObject *object, gchar const *key, gchar const *value, SPException *ex)
1366     /* If exception is not clear, return */
1367     g_return_if_fail(SP_EXCEPTION_IS_OK(ex));
1369     /// \todo fixme: Exception if object is NULL? */
1370     if (!sp_repr_set_attr(object->repr, key, value)) {
1371         ex->code = SP_NO_MODIFICATION_ALLOWED_ERR;
1372     }
1375 void
1376 sp_object_removeAttribute(SPObject *object, gchar const *key, SPException *ex)
1378     /* If exception is not clear, return */
1379     g_return_if_fail(SP_EXCEPTION_IS_OK(ex));
1381     /// \todo fixme: Exception if object is NULL? */
1382     if (!sp_repr_set_attr(object->repr, key, NULL)) {
1383         ex->code = SP_NO_MODIFICATION_ALLOWED_ERR;
1384     }
1387 /* Helper */
1389 static gchar *
1390 sp_object_get_unique_id(SPObject *object, gchar const *id)
1392     static unsigned long count = 0;
1394     g_assert(SP_IS_OBJECT(object));
1396     count++;
1398     gchar const *name = object->repr->name();
1399     g_assert(name != NULL);
1401     gchar const *local = strchr(name, ':');
1402     if (local) {
1403         name = local + 1;
1404     }
1406     if (id != NULL) {
1407         if (object->document->getObjectById(id) == NULL) {
1408             return g_strdup(id);
1409         }
1410     }
1412     size_t const name_len = strlen(name);
1413     size_t const buflen = name_len + (sizeof(count) * 10 / 4) + 1;
1414     gchar *const buf = (gchar *) g_malloc(buflen);
1415     memcpy(buf, name, name_len);
1416     gchar *const count_buf = buf + name_len;
1417     size_t const count_buflen = buflen - name_len;
1418     do {
1419         ++count;
1420         g_snprintf(count_buf, count_buflen, "%lu", count);
1421     } while ( object->document->getObjectById(buf) != NULL );
1422     return buf;
1425 /* Style */
1427 /**
1428  * Returns an object style property.
1429  *
1430  * \todo
1431  * fixme: Use proper CSS parsing.  The current version is buggy
1432  * in a number of situations where key is a substring of the
1433  * style string other than as a property name (including
1434  * where key is a substring of a property name), and is also
1435  * buggy in its handling of inheritance for properties that
1436  * aren't inherited by default.  It also doesn't allow for
1437  * the case where the property is specified but with an invalid
1438  * value (in which case I believe the CSS2 error-handling
1439  * behaviour applies, viz. behave as if the property hadn't
1440  * been specified).  Also, the current code doesn't use CRSelEng
1441  * stuff to take a value from stylesheets.  Also, we aren't
1442  * setting any hooks to force an update for changes in any of
1443  * the inputs (i.e., in any of the elements that this function
1444  * queries).
1445  *
1446  * \par
1447  * Given that the default value for a property depends on what
1448  * property it is (e.g., whether to inherit or not), and given
1449  * the above comment about ignoring invalid values, and that the
1450  * repr parent isn't necessarily the right element to inherit
1451  * from (e.g., maybe we need to inherit from the referencing
1452  * <use> element instead), we should probably make the caller
1453  * responsible for ascending the repr tree as necessary.
1454  */
1455 gchar const *
1456 sp_object_get_style_property(SPObject const *object, gchar const *key, gchar const *def)
1458     g_return_val_if_fail(object != NULL, NULL);
1459     g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
1460     g_return_val_if_fail(key != NULL, NULL);
1462     gchar const *style = object->repr->attribute("style");
1463     if (style) {
1464         size_t const len = strlen(key);
1465         char const *p;
1466         while ( (p = strstr(style, key))
1467                 != NULL )
1468         {
1469             p += len;
1470             while ((*p <= ' ') && *p) p++;
1471             if (*p++ != ':') break;
1472             while ((*p <= ' ') && *p) p++;
1473             size_t const inherit_len = sizeof("inherit") - 1;
1474             if (*p
1475                 && !(strneq(p, "inherit", inherit_len)
1476                      && (p[inherit_len] == '\0'
1477                          || p[inherit_len] == ';'
1478                          || g_ascii_isspace(p[inherit_len])))) {
1479                 return p;
1480             }
1481         }
1482     }
1483     gchar const *val = object->repr->attribute(key);
1484     if (val && !streq(val, "inherit")) {
1485         return val;
1486     }
1487     if (object->parent) {
1488         return sp_object_get_style_property(object->parent, key, def);
1489     }
1491     return def;
1494 /**
1495  * Lifts SVG version of all root objects to version.
1496  */
1497 void
1498 SPObject::_requireSVGVersion(Inkscape::Version version) {
1499     for ( SPObject::ParentIterator iter=this ; iter ; ++iter ) {
1500         SPObject *object=iter;
1501         if (SP_IS_ROOT(object)) {
1502             SPRoot *root=SP_ROOT(object);
1503             if ( root->version.svg < version ) {
1504                 root->version.svg = version;
1505             }
1506         }
1507     }
1510 /**
1511  * Return sodipodi version of first root ancestor or (0,0).
1512  */
1513 Inkscape::Version
1514 sp_object_get_sodipodi_version(SPObject *object)
1516     static Inkscape::Version const zero_version(0, 0);
1518     while (object) {
1519         if (SP_IS_ROOT(object)) {
1520             return SP_ROOT(object)->version.sodipodi;
1521         }
1522         object = SP_OBJECT_PARENT(object);
1523     }
1525     return zero_version;
1528 /**
1529  * Returns previous object in sibling list or NULL.
1530  */
1531 SPObject *
1532 sp_object_prev(SPObject *child)
1534     SPObject *parent = SP_OBJECT_PARENT(child);
1535     for ( SPObject *i = sp_object_first_child(parent); i; i = SP_OBJECT_NEXT(i) ) {
1536         if (SP_OBJECT_NEXT(i) == child)
1537             return i;
1538     }
1539     return NULL;
1543 /*
1544   Local Variables:
1545   mode:c++
1546   c-file-style:"stroustrup"
1547   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1548   indent-tabs-mode:nil
1549   fill-column:99
1550   End:
1551 */
1552 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :