Code

fix 1654495: a comment repr node has no spobject, so we must weaken the asserts
[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 Inkscape::XML::NodeEventVector object_event_vector = {
103     sp_object_repr_child_added,
104     sp_object_repr_child_removed,
105     sp_object_repr_attr_changed,
106     sp_object_repr_content_changed,
107     sp_object_repr_order_changed
108 };
110 static GObjectClass *parent_class;
112 /**
113  * Registers the SPObject class with Gdk and returns its type number.
114  */
115 GType
116 sp_object_get_type(void)
118     static GType type = 0;
119     if (!type) {
120         GTypeInfo info = {
121             sizeof(SPObjectClass),
122             NULL, NULL,
123             (GClassInitFunc) sp_object_class_init,
124             NULL, NULL,
125             sizeof(SPObject),
126             16,
127             (GInstanceInitFunc) sp_object_init,
128             NULL
129         };
130         type = g_type_register_static(G_TYPE_OBJECT, "SPObject", &info, (GTypeFlags)0);
131     }
132     return type;
135 /**
136  * Initializes the SPObject vtable.
137  */
138 static void
139 sp_object_class_init(SPObjectClass *klass)
141     GObjectClass *object_class;
143     object_class = (GObjectClass *) klass;
145     parent_class = (GObjectClass *) g_type_class_ref(G_TYPE_OBJECT);
147     object_class->finalize = sp_object_finalize;
149     klass->child_added = sp_object_child_added;
150     klass->remove_child = sp_object_remove_child;
151     klass->order_changed = sp_object_order_changed;
153     klass->release = sp_object_release;
155     klass->build = sp_object_build;
157     klass->set = sp_object_private_set;
158     klass->write = sp_object_private_write;
161 /**
162  * Callback to initialize the SPObject object.
163  */
164 static void
165 sp_object_init(SPObject *object)
167     debug("id=%x, typename=%s",object, g_type_name_from_instance((GTypeInstance*)object));
169     object->hrefcount = 0;
170     object->_total_hrefcount = 0;
171     object->document = NULL;
172     object->children = object->_last_child = NULL;
173     object->parent = object->next = NULL;
174     object->repr = NULL;
175     object->id = NULL;
176     object->style = NULL;
178     object->_collection_policy = SPObject::COLLECT_WITH_PARENT;
180     new (&object->_release_signal) sigc::signal<void, SPObject *>();
181     new (&object->_modified_signal) sigc::signal<void, SPObject *, unsigned int>();
182     new (&object->_delete_signal) sigc::signal<void, SPObject *>();
183     new (&object->_position_changed_signal) sigc::signal<void, SPObject *>();
184     object->_successor = NULL;
186     object->_label = NULL;
187     object->_default_label = NULL;
190 /**
191  * Callback to destroy all members and connections of object and itself.
192  */
193 static void
194 sp_object_finalize(GObject *object)
196     SPObject *spobject = (SPObject *)object;
198     g_free(spobject->_label);
199     g_free(spobject->_default_label);
200     spobject->_label = NULL;
201     spobject->_default_label = NULL;
203     if (spobject->_successor) {
204         sp_object_unref(spobject->_successor, NULL);
205         spobject->_successor = NULL;
206     }
208     if (((GObjectClass *) (parent_class))->finalize) {
209         (* ((GObjectClass *) (parent_class))->finalize)(object);
210     }
212     spobject->_release_signal.~signal();
213     spobject->_modified_signal.~signal();
214     spobject->_delete_signal.~signal();
215     spobject->_position_changed_signal.~signal();
218 namespace {
220 namespace Debug = Inkscape::Debug;
221 namespace Util = Inkscape::Util;
223 typedef Debug::SimpleEvent<Debug::Event::REFCOUNT> BaseRefCountEvent;
225 class RefCountEvent : public BaseRefCountEvent {
226 public:
227     RefCountEvent(SPObject *object, int bias, Util::ptr_shared<char> name)
228     : BaseRefCountEvent(name)
229     {
230         _addProperty("object", Util::format("%p", object));
231         _addProperty("class", Debug::demangle(g_type_name(G_TYPE_FROM_INSTANCE(object))));
232         _addProperty("new-refcount", Util::format("%d", G_OBJECT(object)->ref_count + bias));
233     }
234 };
236 class RefEvent : public RefCountEvent {
237 public:
238     RefEvent(SPObject *object)
239     : RefCountEvent(object, 1, Util::share_static_string("sp-object-ref"))
240     {}
241 };
243 class UnrefEvent : public RefCountEvent {
244 public:
245     UnrefEvent(SPObject *object)
246     : RefCountEvent(object, -1, Util::share_static_string("sp-object-unref"))
247     {}
248 };
252 /**
253  * Increase reference count of object, with possible debugging.
254  *
255  * \param owner If non-NULL, make debug log entry.
256  * \return object, NULL is error.
257  * \pre object points to real object
258  */
259 SPObject *
260 sp_object_ref(SPObject *object, SPObject *owner)
262     g_return_val_if_fail(object != NULL, NULL);
263     g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
264     g_return_val_if_fail(!owner || SP_IS_OBJECT(owner), NULL);
266     Inkscape::Debug::EventTracker<RefEvent> tracker(object);
267     g_object_ref(G_OBJECT(object));
268     return object;
271 /**
272  * Decrease reference count of object, with possible debugging and
273  * finalization.
274  *
275  * \param owner If non-NULL, make debug log entry.
276  * \return always NULL
277  * \pre object points to real object
278  */
279 SPObject *
280 sp_object_unref(SPObject *object, SPObject *owner)
282     g_return_val_if_fail(object != NULL, NULL);
283     g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
284     g_return_val_if_fail(!owner || SP_IS_OBJECT(owner), NULL);
286     Inkscape::Debug::EventTracker<UnrefEvent> tracker(object);
287     g_object_unref(G_OBJECT(object));
288     return NULL;
291 /**
292  * Increase weak refcount.
293  *
294  * Hrefcount is used for weak references, for example, to
295  * determine whether any graphical element references a certain gradient
296  * node.
297  * \param owner Ignored.
298  * \return object, NULL is error
299  * \pre object points to real object
300  */
301 SPObject *
302 sp_object_href(SPObject *object, gpointer owner)
304     g_return_val_if_fail(object != NULL, NULL);
305     g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
307     object->hrefcount++;
308     object->_updateTotalHRefCount(1);
310     return object;
313 /**
314  * Decrease weak refcount.
315  *
316  * Hrefcount is used for weak references, for example, to determine whether
317  * any graphical element references a certain gradient node.
318  * \param owner Ignored.
319  * \return always NULL
320  * \pre object points to real object and hrefcount>0
321  */
322 SPObject *
323 sp_object_hunref(SPObject *object, gpointer owner)
325     g_return_val_if_fail(object != NULL, NULL);
326     g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
327     g_return_val_if_fail(object->hrefcount > 0, NULL);
329     object->hrefcount--;
330     object->_updateTotalHRefCount(-1);
332     return NULL;
335 /**
336  * Adds increment to _total_hrefcount of object and its parents.
337  */
338 void
339 SPObject::_updateTotalHRefCount(int increment) {
340     SPObject *topmost_collectable = NULL;
341     for ( SPObject *iter = this ; iter ; iter = SP_OBJECT_PARENT(iter) ) {
342         iter->_total_hrefcount += increment;
343         if ( iter->_total_hrefcount < iter->hrefcount ) {
344             g_critical("HRefs overcounted");
345         }
346         if ( iter->_total_hrefcount == 0 &&
347              iter->_collection_policy != COLLECT_WITH_PARENT )
348         {
349             topmost_collectable = iter;
350         }
351     }
352     if (topmost_collectable) {
353         topmost_collectable->requestOrphanCollection();
354     }
357 /**
358  * True if object is non-NULL and this is some in/direct parent of object.
359  */
360 bool
361 SPObject::isAncestorOf(SPObject const *object) const {
362     g_return_val_if_fail(object != NULL, false);
363     object = SP_OBJECT_PARENT(object);
364     while (object) {
365         if ( object == this ) {
366             return true;
367         }
368         object = SP_OBJECT_PARENT(object);
369     }
370     return false;
373 namespace {
375 bool same_objects(SPObject const &a, SPObject const &b) {
376     return &a == &b;
381 /**
382  * Returns youngest object being parent to this and object.
383  */
384 SPObject const *
385 SPObject::nearestCommonAncestor(SPObject const *object) const {
386     g_return_val_if_fail(object != NULL, NULL);
388     using Inkscape::Algorithms::longest_common_suffix;
389     return longest_common_suffix<SPObject::ConstParentIterator>(this, object, NULL, &same_objects);
392 SPObject const *AncestorSon(SPObject const *obj, SPObject const *ancestor) {
393     if (obj == NULL || ancestor == NULL)
394         return NULL;
395     if (SP_OBJECT_PARENT(obj) == ancestor)
396         return obj;
397     return AncestorSon(SP_OBJECT_PARENT(obj), ancestor);
400 /**
401  * Compares height of objects in tree.
402  *
403  * Works for different-parent objects, so long as they have a common ancestor.
404  * \return \verbatim
405  *    0    positions are equivalent
406  *    1    first object's position is greater than the second
407  *   -1    first object's position is less than the second   \endverbatim
408  */
409 int
410 sp_object_compare_position(SPObject const *first, SPObject const *second)
412     if (first == second) return 0;
414     SPObject const *ancestor = first->nearestCommonAncestor(second);
415     if (ancestor == NULL) return 0; // cannot compare, no common ancestor!
417     // we have an object and its ancestor (should not happen when sorting selection)
418     if (ancestor == first)
419         return 1;
420     if (ancestor == second)
421         return -1;
423     SPObject const *to_first = AncestorSon(first, ancestor);
424     SPObject const *to_second = AncestorSon(second, ancestor);
426     g_assert(SP_OBJECT_PARENT(to_second) == SP_OBJECT_PARENT(to_first));
428     return sp_repr_compare_position(SP_OBJECT_REPR(to_first), SP_OBJECT_REPR(to_second));
432 /**
433  * Append repr as child of this object.
434  * \pre this is not a cloned object
435  */
436 SPObject *
437 SPObject::appendChildRepr(Inkscape::XML::Node *repr) {
438     if (!SP_OBJECT_IS_CLONED(this)) {
439         SP_OBJECT_REPR(this)->appendChild(repr);
440         return SP_OBJECT_DOCUMENT(this)->getObjectByRepr(repr);
441     } else {
442         g_critical("Attempt to append repr as child of cloned object");
443         return NULL;
444     }
447 /**
448  * Retrieves the children as a GSList object, optionally ref'ing the children
449  * in the process, if add_ref is specified.
450  */
451 GSList *SPObject::childList(bool add_ref, Action) {
452     GSList *l = NULL;
453     for (SPObject *child = sp_object_first_child(this) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
454         if (add_ref)
455             g_object_ref (G_OBJECT (child));
457         l = g_slist_prepend (l, child);
458     }
459     return l;
463 /** Gets the label property for the object or a default if no label
464  *  is defined.
465  */
466 gchar const *
467 SPObject::label() const {
468     return _label;
471 /** Returns a default label property for the object. */
472 gchar const *
473 SPObject::defaultLabel() const {
474     if (_label) {
475         return _label;
476     } else {
477         if (!_default_label) {
478             gchar const *id=SP_OBJECT_ID(this);
479             if (id) {
480                 _default_label = g_strdup_printf("#%s", id);
481             } else {
482                 _default_label = g_strdup_printf("<%s>", SP_OBJECT_REPR(this)->name());
483             }
484         }
485         return _default_label;
486     }
489 /** Sets the label property for the object */
490 void
491 SPObject::setLabel(gchar const *label) {
492     SP_OBJECT_REPR(this)->setAttribute("inkscape:label", label, false);
496 /** Queues the object for orphan collection */
497 void
498 SPObject::requestOrphanCollection() {
499     g_return_if_fail(document != NULL);
500     document->queueForOrphanCollection(this);
502     /** \todo
503      * This is a temporary hack added to make fill&stroke rebuild its
504      * gradient list when the defs are vacuumed.  gradient-vector.cpp
505      * listens to the modified signal on defs, and now we give it that
506      * signal.  Mental says that this should be made automatic by
507      * merging SPObjectGroup with SPObject; SPObjectGroup would issue
508      * this signal automatically. Or maybe just derive SPDefs from
509      * SPObjectGroup?
510      */
512     this->requestModified(SP_OBJECT_CHILD_MODIFIED_FLAG);
515 /** Sends the delete signal to all children of this object recursively */
516 void
517 SPObject::_sendDeleteSignalRecursive() {
518     for (SPObject *child = sp_object_first_child(this); child; child = SP_OBJECT_NEXT(child)) {
519         child->_delete_signal.emit(child);
520         child->_sendDeleteSignalRecursive();
521     }
524 /**
525  * Deletes the object reference, unparenting it from its parent.
526  *
527  * If the \a propagate parameter is set to true, it emits a delete
528  * signal.  If the \a propagate_descendants parameter is true, it
529  * recursively sends the delete signal to children.
530  */
531 void
532 SPObject::deleteObject(bool propagate, bool propagate_descendants)
534     sp_object_ref(this, NULL);
535     if (propagate) {
536         _delete_signal.emit(this);
537     }
538     if (propagate_descendants) {
539         this->_sendDeleteSignalRecursive();
540     }
542     Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
543     if (repr && sp_repr_parent(repr)) {
544         sp_repr_unparent(repr);
545     }
547     if (_successor) {
548         _successor->deleteObject(propagate, propagate_descendants);
549     }
550     sp_object_unref(this, NULL);
553 /**
554  * Put object into object tree, under parent, and behind prev;
555  * also update object's XML space.
556  */
557 void
558 sp_object_attach(SPObject *parent, SPObject *object, SPObject *prev)
560     g_return_if_fail(parent != NULL);
561     g_return_if_fail(SP_IS_OBJECT(parent));
562     g_return_if_fail(object != NULL);
563     g_return_if_fail(SP_IS_OBJECT(object));
564     g_return_if_fail(!prev || SP_IS_OBJECT(prev));
565     g_return_if_fail(!prev || prev->parent == parent);
566     g_return_if_fail(!object->parent);
568     sp_object_ref(object, parent);
569     object->parent = parent;
570     parent->_updateTotalHRefCount(object->_total_hrefcount);
572     SPObject *next;
573     if (prev) {
574         next = prev->next;
575         prev->next = object;
576     } else {
577         next = parent->children;
578         parent->children = object;
579     }
580     object->next = next;
581     if (!next) {
582         parent->_last_child = object;
583     }
584     if (!object->xml_space.set)
585         object->xml_space.value = parent->xml_space.value;
588 /**
589  * In list of object's siblings, move object behind prev.
590  */
591 void
592 sp_object_reorder(SPObject *object, SPObject *prev) {
593     g_return_if_fail(object != NULL);
594     g_return_if_fail(SP_IS_OBJECT(object));
595     g_return_if_fail(object->parent != NULL);
596     g_return_if_fail(object != prev);
597     g_return_if_fail(!prev || SP_IS_OBJECT(prev));
598     g_return_if_fail(!prev || prev->parent == object->parent);
600     SPObject *const parent=object->parent;
602     SPObject *old_prev=NULL;
603     for ( SPObject *child = parent->children ; child && child != object ;
604           child = child->next )
605     {
606         old_prev = child;
607     }
609     SPObject *next=object->next;
610     if (old_prev) {
611         old_prev->next = next;
612     } else {
613         parent->children = next;
614     }
615     if (!next) {
616         parent->_last_child = old_prev;
617     }
618     if (prev) {
619         next = prev->next;
620         prev->next = object;
621     } else {
622         next = parent->children;
623         parent->children = object;
624     }
625     object->next = next;
626     if (!next) {
627         parent->_last_child = object;
628     }
631 /**
632  * Remove object from parent's children, release and unref it.
633  */
634 void
635 sp_object_detach(SPObject *parent, SPObject *object) {
636     g_return_if_fail(parent != NULL);
637     g_return_if_fail(SP_IS_OBJECT(parent));
638     g_return_if_fail(object != NULL);
639     g_return_if_fail(SP_IS_OBJECT(object));
640     g_return_if_fail(object->parent == parent);
642     object->releaseReferences();
644     SPObject *prev=NULL;
645     for ( SPObject *child = parent->children ; child && child != object ;
646           child = child->next )
647     {
648         prev = child;
649     }
651     SPObject *next=object->next;
652     if (prev) {
653         prev->next = next;
654     } else {
655         parent->children = next;
656     }
657     if (!next) {
658         parent->_last_child = prev;
659     }
661     object->next = NULL;
662     object->parent = NULL;
664     parent->_updateTotalHRefCount(-object->_total_hrefcount);
665     sp_object_unref(object, parent);
668 /**
669  * Return object's child whose node pointer equals repr.
670  */
671 SPObject *
672 sp_object_get_child_by_repr(SPObject *object, Inkscape::XML::Node *repr)
674     g_return_val_if_fail(object != NULL, NULL);
675     g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
676     g_return_val_if_fail(repr != NULL, NULL);
678     if (object->_last_child && SP_OBJECT_REPR(object->_last_child) == repr)
679         return object->_last_child;   // optimization for common scenario
680     for ( SPObject *child = object->children ; child ; child = child->next ) {
681         if ( SP_OBJECT_REPR(child) == repr ) {
682             return child;
683         }
684     }
686     return NULL;
689 /**
690  * Callback for child_added event.
691  * Invoked whenever the given mutation event happens in the XML tree.
692  */
693 static void
694 sp_object_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
696     GType type = sp_repr_type_lookup(child);
697     if (!type) {
698         return;
699     }
700     SPObject *ochild = SP_OBJECT(g_object_new(type, 0));
701     SPObject *prev = ref ? sp_object_get_child_by_repr(object, ref) : NULL;
702     sp_object_attach(object, ochild, prev);
703     sp_object_unref(ochild, NULL);
705     sp_object_invoke_build(ochild, object->document, child, SP_OBJECT_IS_CLONED(object));
708 /**
709  * Removes, releases and unrefs all children of object.
710  *
711  * This is the opposite of build. It has to be invoked as soon as the
712  * object is removed from the tree, even if it is still alive according
713  * to reference count. The frontend unregisters the object from the
714  * document and releases the SPRepr bindings; implementations should free
715  * state data and release all child objects.  Invoking release on
716  * SPRoot destroys the whole document tree.
717  * \see sp_object_build()
718  */
719 static void sp_object_release(SPObject *object)
721     debug("id=%x, typename=%s", object, g_type_name_from_instance((GTypeInstance*)object));
722     while (object->children) {
723         sp_object_detach(object, object->children);
724     }
727 /**
728  * Remove object's child whose node equals repr, release and
729  * unref it.
730  *
731  * Invoked whenever the given mutation event happens in the XML
732  * tree, BEFORE removal from the XML tree happens, so grouping
733  * objects can safely release the child data.
734  */
735 static void
736 sp_object_remove_child(SPObject *object, Inkscape::XML::Node *child)
738     debug("id=%x, typename=%s", object, g_type_name_from_instance((GTypeInstance*)object));
739     SPObject *ochild = sp_object_get_child_by_repr(object, child);
740     g_return_if_fail (ochild != NULL || !strcmp("comment", child->name())); // comments have no objects
741     if (ochild)
742         sp_object_detach(object, ochild);
745 /**
746  * Move object corresponding to child after sibling object corresponding
747  * to new_ref.
748  * Invoked whenever the given mutation event happens in the XML tree.
749  * \param old_ref Ignored
750  */
751 static void sp_object_order_changed(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref,
752                                     Inkscape::XML::Node *new_ref)
754     SPObject *ochild = sp_object_get_child_by_repr(object, child);
755     g_return_if_fail(ochild != NULL);
756     SPObject *prev = new_ref ? sp_object_get_child_by_repr(object, new_ref) : NULL;
757     sp_object_reorder(ochild, prev);
758     ochild->_position_changed_signal.emit(ochild);
761 /**
762  * Virtual build callback.
763  *
764  * This has to be invoked immediately after creation of an SPObject. The
765  * frontend method ensures that the new object is properly attached to
766  * the document and repr; implementation then will parse all of the attributes,
767  * generate the children objects and so on.  Invoking build on the SPRoot
768  * object results in creation of the whole document tree (this is, what
769  * SPDocument does after the creation of the XML tree).
770  * \see sp_object_release()
771  */
772 static void
773 sp_object_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
775     /* Nothing specific here */
776     debug("id=%x, typename=%s", object, g_type_name_from_instance((GTypeInstance*)object));
778     sp_object_read_attr(object, "xml:space");
779     sp_object_read_attr(object, "inkscape:label");
780     sp_object_read_attr(object, "inkscape:collect");
782     for (Inkscape::XML::Node *rchild = repr->firstChild() ; rchild != NULL; rchild = rchild->next()) {
783         GType type = sp_repr_type_lookup(rchild);
784         if (!type) {
785             continue;
786         }
787         SPObject *child = SP_OBJECT(g_object_new(type, 0));
788         sp_object_attach(object, child, object->lastChild());
789         sp_object_unref(child, NULL);
790         sp_object_invoke_build(child, document, rchild, SP_OBJECT_IS_CLONED(object));
791     }
794 void
795 sp_object_invoke_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr, unsigned int cloned)
797     debug("id=%x, typename=%s", object, g_type_name_from_instance((GTypeInstance*)object));
799     g_assert(object != NULL);
800     g_assert(SP_IS_OBJECT(object));
801     g_assert(document != NULL);
802     g_assert(repr != NULL);
804     g_assert(object->document == NULL);
805     g_assert(object->repr == NULL);
806     g_assert(object->id == NULL);
808     /* Bookkeeping */
810     object->document = document;
811     object->repr = repr;
812     Inkscape::GC::anchor(repr);
813     object->cloned = cloned;
815     if (!SP_OBJECT_IS_CLONED(object)) {
816         object->document->bindObjectToRepr(object->repr, object);
818         if (Inkscape::XML::id_permitted(object->repr)) {
819             /* If we are not cloned, force unique id */
820             gchar const *id = object->repr->attribute("id");
821             gchar *realid = sp_object_get_unique_id(object, id);
822             g_assert(realid != NULL);
824             object->document->bindObjectToId(realid, object);
825             object->id = realid;
827             /* Redefine ID, if required */
828             if ((id == NULL) || (strcmp(id, realid) != 0)) {
829                 bool saved = sp_document_get_undo_sensitive(document);
830                 sp_document_set_undo_sensitive(document, false);
831                 object->repr->setAttribute("id", realid);
832                 sp_document_set_undo_sensitive(document, saved);
833             }
834         }
835     } else {
836         g_assert(object->id == NULL);
837     }
839     /* Invoke derived methods, if any */
841     if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->build) {
842         (*((SPObjectClass *) G_OBJECT_GET_CLASS(object))->build)(object, document, repr);
843     }
845     /* Signalling (should be connected AFTER processing derived methods */
846     sp_repr_add_listener(repr, &object_event_vector, object);
849 void SPObject::releaseReferences() {
850     g_assert(this->document);
851     g_assert(this->repr);
853     sp_repr_remove_listener_by_data(this->repr, this);
855     this->_release_signal.emit(this);
856     SPObjectClass *klass=(SPObjectClass *)G_OBJECT_GET_CLASS(this);
857     if (klass->release) {
858         klass->release(this);
859     }
861     /* all hrefs should be released by the "release" handlers */
862     g_assert(this->hrefcount == 0);
864     if (!SP_OBJECT_IS_CLONED(this)) {
865         if (this->id) {
866             this->document->bindObjectToId(this->id, NULL);
867         }
868         g_free(this->id);
869         this->id = NULL;
871         g_free(this->_default_label);
872         this->_default_label = NULL;
874         this->document->bindObjectToRepr(this->repr, NULL);
875     } else {
876         g_assert(!this->id);
877     }
879     if (this->style) {
880         this->style = sp_style_unref(this->style);
881     }
883     Inkscape::GC::release(this->repr);
885     this->document = NULL;
886     this->repr = NULL;
889 /**
890  * Callback for child_added node event.
891  */
892 static void
893 sp_object_repr_child_added(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, gpointer data)
895     SPObject *object = SP_OBJECT(data);
897     if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->child_added)
898         (*((SPObjectClass *)G_OBJECT_GET_CLASS(object))->child_added)(object, child, ref);
901 /**
902  * Callback for remove_child node event.
903  */
904 static void
905 sp_object_repr_child_removed(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, gpointer data)
907     SPObject *object = SP_OBJECT(data);
909     if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->remove_child) {
910         (* ((SPObjectClass *)G_OBJECT_GET_CLASS(object))->remove_child)(object, child);
911     }
914 /**
915  * Callback for order_changed node event.
916  *
917  * \todo fixme:
918  */
919 static void
920 sp_object_repr_order_changed(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *old, Inkscape::XML::Node *newer, gpointer data)
922     SPObject *object = SP_OBJECT(data);
924     if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->order_changed) {
925         (* ((SPObjectClass *)G_OBJECT_GET_CLASS(object))->order_changed)(object, child, old, newer);
926     }
929 /**
930  * Callback for set event.
931  */
932 static void
933 sp_object_private_set(SPObject *object, unsigned int key, gchar const *value)
935     g_assert(key != SP_ATTR_INVALID);
937     switch (key) {
938         case SP_ATTR_ID:
939             if ( !SP_OBJECT_IS_CLONED(object) && object->repr->type() == Inkscape::XML::ELEMENT_NODE ) {
940                 SPDocument *document=object->document;
941                 SPObject *conflict=NULL;
943                 if (value) {
944                     conflict = document->getObjectById((char const *)value);
945                 }
946                 if ( conflict && conflict != object ) {
947                     sp_object_ref(conflict, NULL);
948                     // give the conflicting object a new ID
949                     gchar *new_conflict_id = sp_object_get_unique_id(conflict, NULL);
950                     SP_OBJECT_REPR(conflict)->setAttribute("id", new_conflict_id);
951                     g_free(new_conflict_id);
952                     sp_object_unref(conflict, NULL);
953                 }
955                 if (object->id) {
956                     document->bindObjectToId(object->id, NULL);
957                     g_free(object->id);
958                 }
960                 if (value) {
961                     object->id = g_strdup((char const*)value);
962                     document->bindObjectToId(object->id, object);
963                 } else {
964                     object->id = NULL;
965                 }
967                 g_free(object->_default_label);
968                 object->_default_label = NULL;
969             }
970             break;
971         case SP_ATTR_INKSCAPE_LABEL:
972             g_free(object->_label);
973             if (value) {
974                 object->_label = g_strdup(value);
975             } else {
976                 object->_label = NULL;
977             }
978             g_free(object->_default_label);
979             object->_default_label = NULL;
980             break;
981         case SP_ATTR_INKSCAPE_COLLECT:
982             if ( value && !strcmp(value, "always") ) {
983                 object->setCollectionPolicy(SPObject::ALWAYS_COLLECT);
984             } else {
985                 object->setCollectionPolicy(SPObject::COLLECT_WITH_PARENT);
986             }
987             break;
988         case SP_ATTR_XML_SPACE:
989             if (value && !strcmp(value, "preserve")) {
990                 object->xml_space.value = SP_XML_SPACE_PRESERVE;
991                 object->xml_space.set = TRUE;
992             } else if (value && !strcmp(value, "default")) {
993                 object->xml_space.value = SP_XML_SPACE_DEFAULT;
994                 object->xml_space.set = TRUE;
995             } else if (object->parent) {
996                 SPObject *parent;
997                 parent = object->parent;
998                 object->xml_space.value = parent->xml_space.value;
999             }
1000             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
1001             break;
1002         default:
1003             break;
1004     }
1007 /**
1008  * Call virtual set() function of object.
1009  */
1010 void
1011 sp_object_set(SPObject *object, unsigned int key, gchar const *value)
1013     g_assert(object != NULL);
1014     g_assert(SP_IS_OBJECT(object));
1016     if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->set) {
1017         ((SPObjectClass *) G_OBJECT_GET_CLASS(object))->set(object, key, value);
1018     }
1021 /**
1022  * Read value of key attribute from XML node into object.
1023  */
1024 void
1025 sp_object_read_attr(SPObject *object, gchar const *key)
1027     g_assert(object != NULL);
1028     g_assert(SP_IS_OBJECT(object));
1029     g_assert(key != NULL);
1031     g_assert(object->repr != NULL);
1033     unsigned int keyid = sp_attribute_lookup(key);
1034     if (keyid != SP_ATTR_INVALID) {
1035         /* Retrieve the 'key' attribute from the object's XML representation */
1036         gchar const *value = object->repr->attribute(key);
1038         sp_object_set(object, keyid, value);
1039     }
1042 /**
1043  * Callback for attr_changed node event.
1044  */
1045 static void
1046 sp_object_repr_attr_changed(Inkscape::XML::Node *repr, gchar const *key, gchar const *oldval, gchar const *newval, bool is_interactive, gpointer data)
1048     SPObject *object = SP_OBJECT(data);
1050     sp_object_read_attr(object, key);
1052     // manual changes to extension attributes require the normal
1053     // attributes, which depend on them, to be updated immediately
1054     if (is_interactive) {
1055         object->updateRepr(repr, 0);
1056     }
1059 /**
1060  * Callback for content_changed node event.
1061  */
1062 static void
1063 sp_object_repr_content_changed(Inkscape::XML::Node *repr, gchar const *oldcontent, gchar const *newcontent, gpointer data)
1065     SPObject *object = SP_OBJECT(data);
1067     if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->read_content)
1068         (*((SPObjectClass *) G_OBJECT_GET_CLASS(object))->read_content)(object);
1071 /**
1072  * Return string representation of space value.
1073  */
1074 static gchar const*
1075 sp_xml_get_space_string(unsigned int space)
1077     switch (space) {
1078         case SP_XML_SPACE_DEFAULT:
1079             return "default";
1080         case SP_XML_SPACE_PRESERVE:
1081             return "preserve";
1082         default:
1083             return NULL;
1084     }
1087 /**
1088  * Callback for write event.
1089  */
1090 static Inkscape::XML::Node *
1091 sp_object_private_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
1093     if (!repr && (flags & SP_OBJECT_WRITE_BUILD)) {
1094         repr = SP_OBJECT_REPR(object)->duplicate();
1095         if (!( flags & SP_OBJECT_WRITE_EXT )) {
1096             repr->setAttribute("inkscape:collect", NULL);
1097         }
1098     } else {
1099         repr->setAttribute("id", object->id);
1101         if (object->xml_space.set) {
1102             char const *xml_space;
1103             xml_space = sp_xml_get_space_string(object->xml_space.value);
1104             repr->setAttribute("xml:space", xml_space);
1105         }
1107         if ( flags & SP_OBJECT_WRITE_EXT &&
1108              object->collectionPolicy() == SPObject::ALWAYS_COLLECT )
1109         {
1110             repr->setAttribute("inkscape:collect", "always");
1111         } else {
1112             repr->setAttribute("inkscape:collect", NULL);
1113         }
1114     }
1116     return repr;
1119 /**
1120  * Update this object's XML node with flags value.
1121  */
1122 Inkscape::XML::Node *
1123 SPObject::updateRepr(unsigned int flags) {
1124     if (!SP_OBJECT_IS_CLONED(this)) {
1125         Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
1126         if (repr) {
1127             return updateRepr(repr, flags);
1128         } else {
1129             g_critical("Attempt to update non-existent repr");
1130             return NULL;
1131         }
1132     } else {
1133         /* cloned objects have no repr */
1134         return NULL;
1135     }
1138 Inkscape::XML::Node *
1139 SPObject::updateRepr(Inkscape::XML::Node *repr, unsigned int flags) {
1140     if (SP_OBJECT_IS_CLONED(this)) {
1141         /* cloned objects have no repr */
1142         return NULL;
1143     }
1144     if (((SPObjectClass *) G_OBJECT_GET_CLASS(this))->write) {
1145         if (!(flags & SP_OBJECT_WRITE_BUILD) && !repr) {
1146             repr = SP_OBJECT_REPR(this);
1147         }
1148         return ((SPObjectClass *) G_OBJECT_GET_CLASS(this))->write(this, repr, flags);
1149     } else {
1150         g_warning("Class %s does not implement ::write", G_OBJECT_TYPE_NAME(this));
1151         if (!repr) {
1152             if (flags & SP_OBJECT_WRITE_BUILD) {
1153                 repr = SP_OBJECT_REPR(this)->duplicate();
1154             }
1155             /// \todo fixme: else probably error (Lauris) */
1156         } else {
1157             repr->mergeFrom(SP_OBJECT_REPR(this), "id");
1158         }
1159         return repr;
1160     }
1163 /* Modification */
1165 /**
1166  * Add \a flags to \a object's as dirtiness flags, and
1167  * recursively add CHILD_MODIFIED flag to
1168  * parent and ancestors (as far up as necessary).
1169  */
1170 void
1171 SPObject::requestDisplayUpdate(unsigned int flags)
1173     g_return_if_fail( this->document != NULL );
1175     if (update_in_progress) {
1176         g_print("WARNING: Requested update while update in progress, counter = %d\n", update_in_progress);
1177     }
1179     /* requestModified must be used only to set one of SP_OBJECT_MODIFIED_FLAG or
1180      * SP_OBJECT_CHILD_MODIFIED_FLAG */
1181     g_return_if_fail(!(flags & SP_OBJECT_PARENT_MODIFIED_FLAG));
1182     g_return_if_fail((flags & SP_OBJECT_MODIFIED_FLAG) || (flags & SP_OBJECT_CHILD_MODIFIED_FLAG));
1183     g_return_if_fail(!((flags & SP_OBJECT_MODIFIED_FLAG) && (flags & SP_OBJECT_CHILD_MODIFIED_FLAG)));
1185     bool already_propagated = (!(this->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG)));
1187     this->uflags |= flags;
1189     /* If requestModified has already been called on this object or one of its children, then we
1190      * don't need to set CHILD_MODIFIED on our ancestors because it's already been done.
1191      */
1192     if (already_propagated) {
1193         SPObject *parent = SP_OBJECT_PARENT(this);
1194         if (parent) {
1195             parent->requestDisplayUpdate(SP_OBJECT_CHILD_MODIFIED_FLAG);
1196         } else {
1197             sp_document_request_modified(SP_OBJECT_DOCUMENT(this));
1198         }
1199     }
1202 /**
1203  * Update views
1204  */
1205 void
1206 SPObject::updateDisplay(SPCtx *ctx, unsigned int flags)
1208     g_return_if_fail(!(flags & ~SP_OBJECT_MODIFIED_CASCADE));
1210     update_in_progress ++;
1212 #ifdef SP_OBJECT_DEBUG_CASCADE
1213     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);
1214 #endif
1216     /* Get this flags */
1217     flags |= this->uflags;
1218     /* Copy flags to modified cascade for later processing */
1219     this->mflags |= this->uflags;
1220     /* We have to clear flags here to allow rescheduling update */
1221     this->uflags = 0;
1223     // Merge style if we have good reasons to think that parent style is changed */
1224     /** \todo
1225      * I am not sure whether we should check only propagated
1226      * flag. We are currently assuming that style parsing is
1227      * done immediately. I think this is correct (Lauris).
1228      */
1229     if ((flags & SP_OBJECT_STYLE_MODIFIED_FLAG) && (flags & SP_OBJECT_PARENT_MODIFIED_FLAG)) {
1230         if (this->style && this->parent) {
1231             sp_style_merge_from_parent(this->style, this->parent->style);
1232         }
1233     }
1235     if (((SPObjectClass *) G_OBJECT_GET_CLASS(this))->update)
1236         ((SPObjectClass *) G_OBJECT_GET_CLASS(this))->update(this, ctx, flags);
1238     update_in_progress --;
1241 /**
1242  * Request modified always bubbles *up* the tree, as opposed to 
1243  * request display update, which trickles down and relies on the 
1244  * flags set during this pass...
1245  */
1246 void
1247 SPObject::requestModified(unsigned int flags)
1249     g_return_if_fail( this->document != NULL );
1251     /* requestModified must be used only to set one of SP_OBJECT_MODIFIED_FLAG or
1252      * SP_OBJECT_CHILD_MODIFIED_FLAG */
1253     g_return_if_fail(!(flags & SP_OBJECT_PARENT_MODIFIED_FLAG));
1254     g_return_if_fail((flags & SP_OBJECT_MODIFIED_FLAG) || (flags & SP_OBJECT_CHILD_MODIFIED_FLAG));
1255     g_return_if_fail(!((flags & SP_OBJECT_MODIFIED_FLAG) && (flags & SP_OBJECT_CHILD_MODIFIED_FLAG)));
1257     bool already_propagated = (!(this->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG)));
1259     this->mflags |= flags;
1261     /* If requestModified has already been called on this object or one of its children, then we
1262      * don't need to set CHILD_MODIFIED on our ancestors because it's already been done.
1263      */
1264     if (already_propagated) {
1265         SPObject *parent=SP_OBJECT_PARENT(this);
1266         if (parent) {
1267             parent->requestModified(SP_OBJECT_CHILD_MODIFIED_FLAG);
1268         } else {
1269             sp_document_request_modified(SP_OBJECT_DOCUMENT(this));
1270         }
1271     }
1274 /** 
1275  *  Emits the MODIFIED signal with the object's flags.
1276  *  The object's mflags are the original set aside during the update pass for 
1277  *  later delivery here.  Once emitModified() is called, those flags don't
1278  *  need to be stored any longer.
1279  */
1280 void
1281 SPObject::emitModified(unsigned int flags)
1283     /* only the MODIFIED_CASCADE flag is legal here */
1284     g_return_if_fail(!(flags & ~SP_OBJECT_MODIFIED_CASCADE));
1286 #ifdef SP_OBJECT_DEBUG_CASCADE
1287     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);
1288 #endif
1290     flags |= this->mflags;
1291     /* We have to clear mflags beforehand, as signal handlers may
1292      * make changes and therefore queue new modification notifications
1293      * themselves. */
1294     this->mflags = 0;
1296     g_object_ref(G_OBJECT(this));
1297     SPObjectClass *klass=(SPObjectClass *)G_OBJECT_GET_CLASS(this);
1298     if (klass->modified) {
1299         klass->modified(this, flags);
1300     }
1301     _modified_signal.emit(this, flags);
1302     g_object_unref(G_OBJECT(this));
1305 /*
1306  * Get and set descriptive parameters
1307  *
1308  * These are inefficent, so they are not intended to be used interactively
1309  */
1311 gchar const *
1312 sp_object_title_get(SPObject *object)
1314     return NULL;
1317 gchar const *
1318 sp_object_description_get(SPObject *object)
1320     return NULL;
1323 unsigned int
1324 sp_object_title_set(SPObject *object, gchar const *title)
1326     return FALSE;
1329 unsigned int
1330 sp_object_description_set(SPObject *object, gchar const *desc)
1332     return FALSE;
1335 gchar const *
1336 sp_object_tagName_get(SPObject const *object, SPException *ex)
1338     /* If exception is not clear, return */
1339     if (!SP_EXCEPTION_IS_OK(ex)) {
1340         return NULL;
1341     }
1343     /// \todo fixme: Exception if object is NULL? */
1344     return object->repr->name();
1347 gchar const *
1348 sp_object_getAttribute(SPObject const *object, gchar const *key, SPException *ex)
1350     /* If exception is not clear, return */
1351     if (!SP_EXCEPTION_IS_OK(ex)) {
1352         return NULL;
1353     }
1355     /// \todo fixme: Exception if object is NULL? */
1356     return (gchar const *) object->repr->attribute(key);
1359 void
1360 sp_object_setAttribute(SPObject *object, gchar const *key, gchar const *value, SPException *ex)
1362     /* If exception is not clear, return */
1363     g_return_if_fail(SP_EXCEPTION_IS_OK(ex));
1365     /// \todo fixme: Exception if object is NULL? */
1366     if (!sp_repr_set_attr(object->repr, key, value)) {
1367         ex->code = SP_NO_MODIFICATION_ALLOWED_ERR;
1368     }
1371 void
1372 sp_object_removeAttribute(SPObject *object, gchar const *key, SPException *ex)
1374     /* If exception is not clear, return */
1375     g_return_if_fail(SP_EXCEPTION_IS_OK(ex));
1377     /// \todo fixme: Exception if object is NULL? */
1378     if (!sp_repr_set_attr(object->repr, key, NULL)) {
1379         ex->code = SP_NO_MODIFICATION_ALLOWED_ERR;
1380     }
1383 /* Helper */
1385 static gchar *
1386 sp_object_get_unique_id(SPObject *object, gchar const *id)
1388     static unsigned long count = 0;
1390     g_assert(SP_IS_OBJECT(object));
1392     count++;
1394     gchar const *name = object->repr->name();
1395     g_assert(name != NULL);
1397     gchar const *local = strchr(name, ':');
1398     if (local) {
1399         name = local + 1;
1400     }
1402     if (id != NULL) {
1403         if (object->document->getObjectById(id) == NULL) {
1404             return g_strdup(id);
1405         }
1406     }
1408     size_t const name_len = strlen(name);
1409     size_t const buflen = name_len + (sizeof(count) * 10 / 4) + 1;
1410     gchar *const buf = (gchar *) g_malloc(buflen);
1411     memcpy(buf, name, name_len);
1412     gchar *const count_buf = buf + name_len;
1413     size_t const count_buflen = buflen - name_len;
1414     do {
1415         ++count;
1416         g_snprintf(count_buf, count_buflen, "%lu", count);
1417     } while ( object->document->getObjectById(buf) != NULL );
1418     return buf;
1421 /* Style */
1423 /**
1424  * Returns an object style property.
1425  *
1426  * \todo
1427  * fixme: Use proper CSS parsing.  The current version is buggy
1428  * in a number of situations where key is a substring of the
1429  * style string other than as a property name (including
1430  * where key is a substring of a property name), and is also
1431  * buggy in its handling of inheritance for properties that
1432  * aren't inherited by default.  It also doesn't allow for
1433  * the case where the property is specified but with an invalid
1434  * value (in which case I believe the CSS2 error-handling
1435  * behaviour applies, viz. behave as if the property hadn't
1436  * been specified).  Also, the current code doesn't use CRSelEng
1437  * stuff to take a value from stylesheets.  Also, we aren't
1438  * setting any hooks to force an update for changes in any of
1439  * the inputs (i.e., in any of the elements that this function
1440  * queries).
1441  *
1442  * \par
1443  * Given that the default value for a property depends on what
1444  * property it is (e.g., whether to inherit or not), and given
1445  * the above comment about ignoring invalid values, and that the
1446  * repr parent isn't necessarily the right element to inherit
1447  * from (e.g., maybe we need to inherit from the referencing
1448  * <use> element instead), we should probably make the caller
1449  * responsible for ascending the repr tree as necessary.
1450  */
1451 gchar const *
1452 sp_object_get_style_property(SPObject const *object, gchar const *key, gchar const *def)
1454     g_return_val_if_fail(object != NULL, NULL);
1455     g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
1456     g_return_val_if_fail(key != NULL, NULL);
1458     gchar const *style = object->repr->attribute("style");
1459     if (style) {
1460         size_t const len = strlen(key);
1461         char const *p;
1462         while ( (p = strstr(style, key))
1463                 != NULL )
1464         {
1465             p += len;
1466             while ((*p <= ' ') && *p) p++;
1467             if (*p++ != ':') break;
1468             while ((*p <= ' ') && *p) p++;
1469             size_t const inherit_len = sizeof("inherit") - 1;
1470             if (*p
1471                 && !(strneq(p, "inherit", inherit_len)
1472                      && (p[inherit_len] == '\0'
1473                          || p[inherit_len] == ';'
1474                          || g_ascii_isspace(p[inherit_len])))) {
1475                 return p;
1476             }
1477         }
1478     }
1479     gchar const *val = object->repr->attribute(key);
1480     if (val && !streq(val, "inherit")) {
1481         return val;
1482     }
1483     if (object->parent) {
1484         return sp_object_get_style_property(object->parent, key, def);
1485     }
1487     return def;
1490 /**
1491  * Lifts SVG version of all root objects to version.
1492  */
1493 void
1494 SPObject::_requireSVGVersion(Inkscape::Version version) {
1495     for ( SPObject::ParentIterator iter=this ; iter ; ++iter ) {
1496         SPObject *object=iter;
1497         if (SP_IS_ROOT(object)) {
1498             SPRoot *root=SP_ROOT(object);
1499             if ( root->version.svg < version ) {
1500                 root->version.svg = version;
1501             }
1502         }
1503     }
1506 /**
1507  * Return sodipodi version of first root ancestor or (0,0).
1508  */
1509 Inkscape::Version
1510 sp_object_get_sodipodi_version(SPObject *object)
1512     static Inkscape::Version const zero_version(0, 0);
1514     while (object) {
1515         if (SP_IS_ROOT(object)) {
1516             return SP_ROOT(object)->version.sodipodi;
1517         }
1518         object = SP_OBJECT_PARENT(object);
1519     }
1521     return zero_version;
1524 /**
1525  * Returns previous object in sibling list or NULL.
1526  */
1527 SPObject *
1528 sp_object_prev(SPObject *child)
1530     SPObject *parent = SP_OBJECT_PARENT(child);
1531     for ( SPObject *i = sp_object_first_child(parent); i; i = SP_OBJECT_NEXT(i) ) {
1532         if (SP_OBJECT_NEXT(i) == child)
1533             return i;
1534     }
1535     return NULL;
1539 /*
1540   Local Variables:
1541   mode:c++
1542   c-file-style:"stroustrup"
1543   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1544   indent-tabs-mode:nil
1545   fill-column:99
1546   End:
1547 */
1548 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :