Code

Ok, committed msgloan's patch to convert gbooleans to bools thus completing
[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);
741     sp_object_detach(object, ochild);
744 /**
745  * Move object corresponding to child after sibling object corresponding
746  * to new_ref.
747  * Invoked whenever the given mutation event happens in the XML tree.
748  * \param old_ref Ignored
749  */
750 static void sp_object_order_changed(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref,
751                                     Inkscape::XML::Node *new_ref)
753     SPObject *ochild = sp_object_get_child_by_repr(object, child);
754     g_return_if_fail(ochild != NULL);
755     SPObject *prev = new_ref ? sp_object_get_child_by_repr(object, new_ref) : NULL;
756     sp_object_reorder(ochild, prev);
757     ochild->_position_changed_signal.emit(ochild);
760 /**
761  * Virtual build callback.
762  *
763  * This has to be invoked immediately after creation of an SPObject. The
764  * frontend method ensures that the new object is properly attached to
765  * the document and repr; implementation then will parse all of the attributes,
766  * generate the children objects and so on.  Invoking build on the SPRoot
767  * object results in creation of the whole document tree (this is, what
768  * SPDocument does after the creation of the XML tree).
769  * \see sp_object_release()
770  */
771 static void
772 sp_object_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
774     /* Nothing specific here */
775     debug("id=%x, typename=%s", object, g_type_name_from_instance((GTypeInstance*)object));
777     sp_object_read_attr(object, "xml:space");
778     sp_object_read_attr(object, "inkscape:label");
779     sp_object_read_attr(object, "inkscape:collect");
781     for (Inkscape::XML::Node *rchild = repr->firstChild() ; rchild != NULL; rchild = rchild->next()) {
782         GType type = sp_repr_type_lookup(rchild);
783         if (!type) {
784             continue;
785         }
786         SPObject *child = SP_OBJECT(g_object_new(type, 0));
787         sp_object_attach(object, child, object->lastChild());
788         sp_object_unref(child, NULL);
789         sp_object_invoke_build(child, document, rchild, SP_OBJECT_IS_CLONED(object));
790     }
793 void
794 sp_object_invoke_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr, unsigned int cloned)
796     debug("id=%x, typename=%s", object, g_type_name_from_instance((GTypeInstance*)object));
798     g_assert(object != NULL);
799     g_assert(SP_IS_OBJECT(object));
800     g_assert(document != NULL);
801     g_assert(repr != NULL);
803     g_assert(object->document == NULL);
804     g_assert(object->repr == NULL);
805     g_assert(object->id == NULL);
807     /* Bookkeeping */
809     object->document = document;
810     object->repr = repr;
811     Inkscape::GC::anchor(repr);
812     object->cloned = cloned;
814     if (!SP_OBJECT_IS_CLONED(object)) {
815         object->document->bindObjectToRepr(object->repr, object);
817         if (Inkscape::XML::id_permitted(object->repr)) {
818             /* If we are not cloned, force unique id */
819             gchar const *id = object->repr->attribute("id");
820             gchar *realid = sp_object_get_unique_id(object, id);
821             g_assert(realid != NULL);
823             object->document->bindObjectToId(realid, object);
824             object->id = realid;
826             /* Redefine ID, if required */
827             if ((id == NULL) || (strcmp(id, realid) != 0)) {
828                 bool undo_sensitive=sp_document_get_undo_sensitive(document);
829                 sp_document_set_undo_sensitive(document, FALSE);
830                 object->repr->setAttribute("id", realid);
831                 sp_document_set_undo_sensitive(document, undo_sensitive);
832             }
833         }
834     } else {
835         g_assert(object->id == NULL);
836     }
838     /* Invoke derived methods, if any */
840     if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->build) {
841         (*((SPObjectClass *) G_OBJECT_GET_CLASS(object))->build)(object, document, repr);
842     }
844     /* Signalling (should be connected AFTER processing derived methods */
845     sp_repr_add_listener(repr, &object_event_vector, object);
848 void SPObject::releaseReferences() {
849     g_assert(this->document);
850     g_assert(this->repr);
852     sp_repr_remove_listener_by_data(this->repr, this);
854     SPObjectClass *klass=(SPObjectClass *)G_OBJECT_GET_CLASS(this);
855     if (klass->release) {
856         klass->release(this);
857     }
858     this->_release_signal.emit(this);
860     /* all hrefs should be released by the "release" handlers */
861     g_assert(this->hrefcount == 0);
863     if (!SP_OBJECT_IS_CLONED(this)) {
864         if (this->id) {
865             this->document->bindObjectToId(this->id, NULL);
866         }
867         g_free(this->id);
868         this->id = NULL;
870         g_free(this->_default_label);
871         this->_default_label = NULL;
873         this->document->bindObjectToRepr(this->repr, NULL);
874     } else {
875         g_assert(!this->id);
876     }
878     if (this->style) {
879         this->style = sp_style_unref(this->style);
880     }
882     Inkscape::GC::release(this->repr);
884     this->document = NULL;
885     this->repr = NULL;
888 /**
889  * Callback for child_added node event.
890  */
891 static void
892 sp_object_repr_child_added(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, gpointer data)
894     SPObject *object = SP_OBJECT(data);
896     if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->child_added)
897         (*((SPObjectClass *)G_OBJECT_GET_CLASS(object))->child_added)(object, child, ref);
900 /**
901  * Callback for remove_child node event.
902  */
903 static void
904 sp_object_repr_child_removed(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, gpointer data)
906     SPObject *object = SP_OBJECT(data);
908     if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->remove_child) {
909         (* ((SPObjectClass *)G_OBJECT_GET_CLASS(object))->remove_child)(object, child);
910     }
913 /**
914  * Callback for order_changed node event.
915  *
916  * \todo fixme:
917  */
918 static void
919 sp_object_repr_order_changed(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *old, Inkscape::XML::Node *newer, gpointer data)
921     SPObject *object = SP_OBJECT(data);
923     if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->order_changed) {
924         (* ((SPObjectClass *)G_OBJECT_GET_CLASS(object))->order_changed)(object, child, old, newer);
925     }
928 /**
929  * Callback for set event.
930  */
931 static void
932 sp_object_private_set(SPObject *object, unsigned int key, gchar const *value)
934     g_assert(key != SP_ATTR_INVALID);
936     switch (key) {
937         case SP_ATTR_ID:
938             if ( !SP_OBJECT_IS_CLONED(object) && object->repr->type() == Inkscape::XML::ELEMENT_NODE ) {
939                 SPDocument *document=object->document;
940                 SPObject *conflict=NULL;
942                 if (value) {
943                     conflict = document->getObjectById((char const *)value);
944                 }
945                 if ( conflict && conflict != object ) {
946                     sp_object_ref(conflict, NULL);
947                     // give the conflicting object a new ID
948                     gchar *new_conflict_id = sp_object_get_unique_id(conflict, NULL);
949                     SP_OBJECT_REPR(conflict)->setAttribute("id", new_conflict_id);
950                     g_free(new_conflict_id);
951                     sp_object_unref(conflict, NULL);
952                 }
954                 if (object->id) {
955                     document->bindObjectToId(object->id, NULL);
956                     g_free(object->id);
957                 }
959                 if (value) {
960                     object->id = g_strdup((char const*)value);
961                     document->bindObjectToId(object->id, object);
962                 } else {
963                     object->id = NULL;
964                 }
966                 g_free(object->_default_label);
967                 object->_default_label = NULL;
968             }
969             break;
970         case SP_ATTR_INKSCAPE_LABEL:
971             g_free(object->_label);
972             if (value) {
973                 object->_label = g_strdup(value);
974             } else {
975                 object->_label = NULL;
976             }
977             g_free(object->_default_label);
978             object->_default_label = NULL;
979             break;
980         case SP_ATTR_INKSCAPE_COLLECT:
981             if ( value && !strcmp(value, "always") ) {
982                 object->setCollectionPolicy(SPObject::ALWAYS_COLLECT);
983             } else {
984                 object->setCollectionPolicy(SPObject::COLLECT_WITH_PARENT);
985             }
986             break;
987         case SP_ATTR_XML_SPACE:
988             if (value && !strcmp(value, "preserve")) {
989                 object->xml_space.value = SP_XML_SPACE_PRESERVE;
990                 object->xml_space.set = TRUE;
991             } else if (value && !strcmp(value, "default")) {
992                 object->xml_space.value = SP_XML_SPACE_DEFAULT;
993                 object->xml_space.set = TRUE;
994             } else if (object->parent) {
995                 SPObject *parent;
996                 parent = object->parent;
997                 object->xml_space.value = parent->xml_space.value;
998             }
999             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
1000             break;
1001         default:
1002             break;
1003     }
1006 /**
1007  * Call virtual set() function of object.
1008  */
1009 void
1010 sp_object_set(SPObject *object, unsigned int key, gchar const *value)
1012     g_assert(object != NULL);
1013     g_assert(SP_IS_OBJECT(object));
1015     if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->set) {
1016         ((SPObjectClass *) G_OBJECT_GET_CLASS(object))->set(object, key, value);
1017     }
1020 /**
1021  * Read value of key attribute from XML node into object.
1022  */
1023 void
1024 sp_object_read_attr(SPObject *object, gchar const *key)
1026     g_assert(object != NULL);
1027     g_assert(SP_IS_OBJECT(object));
1028     g_assert(key != NULL);
1030     g_assert(object->repr != NULL);
1032     unsigned int keyid = sp_attribute_lookup(key);
1033     if (keyid != SP_ATTR_INVALID) {
1034         /* Retrieve the 'key' attribute from the object's XML representation */
1035         gchar const *value = object->repr->attribute(key);
1037         sp_object_set(object, keyid, value);
1038     }
1041 /**
1042  * Callback for attr_changed node event.
1043  */
1044 static void
1045 sp_object_repr_attr_changed(Inkscape::XML::Node *repr, gchar const *key, gchar const *oldval, gchar const *newval, bool is_interactive, gpointer data)
1047     SPObject *object = SP_OBJECT(data);
1049     sp_object_read_attr(object, key);
1051     // manual changes to extension attributes require the normal
1052     // attributes, which depend on them, to be updated immediately
1053     if (is_interactive) {
1054         object->updateRepr(repr, 0);
1055     }
1058 /**
1059  * Callback for content_changed node event.
1060  */
1061 static void
1062 sp_object_repr_content_changed(Inkscape::XML::Node *repr, gchar const *oldcontent, gchar const *newcontent, gpointer data)
1064     SPObject *object = SP_OBJECT(data);
1066     if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->read_content)
1067         (*((SPObjectClass *) G_OBJECT_GET_CLASS(object))->read_content)(object);
1070 /**
1071  * Return string representation of space value.
1072  */
1073 static gchar const*
1074 sp_xml_get_space_string(unsigned int space)
1076     switch (space) {
1077         case SP_XML_SPACE_DEFAULT:
1078             return "default";
1079         case SP_XML_SPACE_PRESERVE:
1080             return "preserve";
1081         default:
1082             return NULL;
1083     }
1086 /**
1087  * Callback for write event.
1088  */
1089 static Inkscape::XML::Node *
1090 sp_object_private_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
1092     if (!repr && (flags & SP_OBJECT_WRITE_BUILD)) {
1093         repr = SP_OBJECT_REPR(object)->duplicate();
1094         if (!( flags & SP_OBJECT_WRITE_EXT )) {
1095             repr->setAttribute("inkscape:collect", NULL);
1096         }
1097     } else {
1098         repr->setAttribute("id", object->id);
1100         if (object->xml_space.set) {
1101             char const *xml_space;
1102             xml_space = sp_xml_get_space_string(object->xml_space.value);
1103             repr->setAttribute("xml:space", xml_space);
1104         }
1106         if ( flags & SP_OBJECT_WRITE_EXT &&
1107              object->collectionPolicy() == SPObject::ALWAYS_COLLECT )
1108         {
1109             repr->setAttribute("inkscape:collect", "always");
1110         } else {
1111             repr->setAttribute("inkscape:collect", NULL);
1112         }
1113     }
1115     return repr;
1118 /**
1119  * Update this object's XML node with flags value.
1120  */
1121 Inkscape::XML::Node *
1122 SPObject::updateRepr(unsigned int flags) {
1123     if (!SP_OBJECT_IS_CLONED(this)) {
1124         Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
1125         if (repr) {
1126             return updateRepr(repr, flags);
1127         } else {
1128             g_critical("Attempt to update non-existent repr");
1129             return NULL;
1130         }
1131     } else {
1132         /* cloned objects have no repr */
1133         return NULL;
1134     }
1137 Inkscape::XML::Node *
1138 SPObject::updateRepr(Inkscape::XML::Node *repr, unsigned int flags) {
1139     if (SP_OBJECT_IS_CLONED(this)) {
1140         /* cloned objects have no repr */
1141         return NULL;
1142     }
1143     if (((SPObjectClass *) G_OBJECT_GET_CLASS(this))->write) {
1144         if (!(flags & SP_OBJECT_WRITE_BUILD) && !repr) {
1145             repr = SP_OBJECT_REPR(this);
1146         }
1147         return ((SPObjectClass *) G_OBJECT_GET_CLASS(this))->write(this, repr, flags);
1148     } else {
1149         g_warning("Class %s does not implement ::write", G_OBJECT_TYPE_NAME(this));
1150         if (!repr) {
1151             if (flags & SP_OBJECT_WRITE_BUILD) {
1152                 repr = SP_OBJECT_REPR(this)->duplicate();
1153             }
1154             /// \todo fixme: else probably error (Lauris) */
1155         } else {
1156             repr->mergeFrom(SP_OBJECT_REPR(this), "id");
1157         }
1158         return repr;
1159     }
1162 /* Modification */
1164 /**
1165  * Add \a flags to \a object's as dirtiness flags, and
1166  * recursively add CHILD_MODIFIED flag to
1167  * parent and ancestors (as far up as necessary).
1168  */
1169 void
1170 SPObject::requestDisplayUpdate(unsigned int flags)
1172     g_return_if_fail( this->document != NULL );
1174     if (update_in_progress) {
1175         g_print("WARNING: Requested update while update in progress, counter = %d\n", update_in_progress);
1176     }
1178     /* requestModified must be used only to set one of SP_OBJECT_MODIFIED_FLAG or
1179      * SP_OBJECT_CHILD_MODIFIED_FLAG */
1180     g_return_if_fail(!(flags & SP_OBJECT_PARENT_MODIFIED_FLAG));
1181     g_return_if_fail((flags & SP_OBJECT_MODIFIED_FLAG) || (flags & SP_OBJECT_CHILD_MODIFIED_FLAG));
1182     g_return_if_fail(!((flags & SP_OBJECT_MODIFIED_FLAG) && (flags & SP_OBJECT_CHILD_MODIFIED_FLAG)));
1184     bool already_propagated = (!(this->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG)));
1186     this->uflags |= flags;
1188     /* If requestModified has already been called on this object or one of its children, then we
1189      * don't need to set CHILD_MODIFIED on our ancestors because it's already been done.
1190      */
1191     if (already_propagated) {
1192         SPObject *parent = SP_OBJECT_PARENT(this);
1193         if (parent) {
1194             parent->requestDisplayUpdate(SP_OBJECT_CHILD_MODIFIED_FLAG);
1195         } else {
1196             sp_document_request_modified(SP_OBJECT_DOCUMENT(this));
1197         }
1198     }
1201 void
1202 SPObject::updateDisplay(SPCtx *ctx, unsigned int flags)
1204     g_return_if_fail(!(flags & ~SP_OBJECT_MODIFIED_CASCADE));
1206     update_in_progress ++;
1208 #ifdef SP_OBJECT_DEBUG_CASCADE
1209     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);
1210 #endif
1212     /* Get this flags */
1213     flags |= this->uflags;
1214     /* Copy flags to modified cascade for later processing */
1215     this->mflags |= this->uflags;
1216     /* We have to clear flags here to allow rescheduling update */
1217     this->uflags = 0;
1219     // Merge style if we have good reasons to think that parent style is changed */
1220     /** \todo
1221      * I am not sure whether we should check only propagated
1222      * flag. We are currently assuming that style parsing is
1223      * done immediately. I think this is correct (Lauris).
1224      */
1225     if ((flags & SP_OBJECT_STYLE_MODIFIED_FLAG) && (flags & SP_OBJECT_PARENT_MODIFIED_FLAG)) {
1226         if (this->style && this->parent) {
1227             sp_style_merge_from_parent(this->style, this->parent->style);
1228         }
1229     }
1231     if (((SPObjectClass *) G_OBJECT_GET_CLASS(this))->update)
1232         ((SPObjectClass *) G_OBJECT_GET_CLASS(this))->update(this, ctx, flags);
1234     update_in_progress --;
1237 /**
1238  * Request modified always bubbles *up* the tree, as opposed to 
1239  * request display update, which trickles down and relies on the 
1240  * flags set during this pass...
1241  */
1242 void
1243 SPObject::requestModified(unsigned int flags)
1245     g_return_if_fail( this->document != NULL );
1247     /* requestModified must be used only to set one of SP_OBJECT_MODIFIED_FLAG or
1248      * SP_OBJECT_CHILD_MODIFIED_FLAG */
1249     g_return_if_fail(!(flags & SP_OBJECT_PARENT_MODIFIED_FLAG));
1250     g_return_if_fail((flags & SP_OBJECT_MODIFIED_FLAG) || (flags & SP_OBJECT_CHILD_MODIFIED_FLAG));
1251     g_return_if_fail(!((flags & SP_OBJECT_MODIFIED_FLAG) && (flags & SP_OBJECT_CHILD_MODIFIED_FLAG)));
1253     bool already_propagated = (!(this->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG)));
1255     this->mflags |= flags;
1257     /* If requestModified has already been called on this object or one of its children, then we
1258      * don't need to set CHILD_MODIFIED on our ancestors because it's already been done.
1259      */
1260     if (already_propagated) {
1261         SPObject *parent=SP_OBJECT_PARENT(this);
1262         if (parent) {
1263             parent->requestModified(SP_OBJECT_CHILD_MODIFIED_FLAG);
1264         } else {
1265             sp_document_request_modified(SP_OBJECT_DOCUMENT(this));
1266         }
1267     }
1270 /** 
1271  * This is what actually delivers the modified signals
1272  */
1273 void
1274 SPObject::emitModified(unsigned int flags)
1276     /* only the MODIFIED_CASCADE flag is legal here */
1277     g_return_if_fail(!(flags & ~SP_OBJECT_MODIFIED_CASCADE));
1279 #ifdef SP_OBJECT_DEBUG_CASCADE
1280     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);
1281 #endif
1283     flags |= this->mflags;
1284     /* We have to clear mflags beforehand, as signal handlers may
1285      * make changes and therefore queue new modification notifications
1286      * themselves. */
1287     this->mflags = 0;
1289     g_object_ref(G_OBJECT(this));
1290     SPObjectClass *klass=(SPObjectClass *)G_OBJECT_GET_CLASS(this);
1291     if (klass->modified) {
1292         klass->modified(this, flags);
1293     }
1294     _modified_signal.emit(this, flags);
1295     g_object_unref(G_OBJECT(this));
1298 /*
1299  * Get and set descriptive parameters
1300  *
1301  * These are inefficent, so they are not intended to be used interactively
1302  */
1304 gchar const *
1305 sp_object_title_get(SPObject *object)
1307     return NULL;
1310 gchar const *
1311 sp_object_description_get(SPObject *object)
1313     return NULL;
1316 unsigned int
1317 sp_object_title_set(SPObject *object, gchar const *title)
1319     return FALSE;
1322 unsigned int
1323 sp_object_description_set(SPObject *object, gchar const *desc)
1325     return FALSE;
1328 gchar const *
1329 sp_object_tagName_get(SPObject const *object, SPException *ex)
1331     /* If exception is not clear, return */
1332     if (!SP_EXCEPTION_IS_OK(ex)) {
1333         return NULL;
1334     }
1336     /// \todo fixme: Exception if object is NULL? */
1337     return object->repr->name();
1340 gchar const *
1341 sp_object_getAttribute(SPObject const *object, gchar const *key, SPException *ex)
1343     /* If exception is not clear, return */
1344     if (!SP_EXCEPTION_IS_OK(ex)) {
1345         return NULL;
1346     }
1348     /// \todo fixme: Exception if object is NULL? */
1349     return (gchar const *) object->repr->attribute(key);
1352 void
1353 sp_object_setAttribute(SPObject *object, gchar const *key, gchar const *value, SPException *ex)
1355     /* If exception is not clear, return */
1356     g_return_if_fail(SP_EXCEPTION_IS_OK(ex));
1358     /// \todo fixme: Exception if object is NULL? */
1359     if (!sp_repr_set_attr(object->repr, key, value)) {
1360         ex->code = SP_NO_MODIFICATION_ALLOWED_ERR;
1361     }
1364 void
1365 sp_object_removeAttribute(SPObject *object, gchar const *key, SPException *ex)
1367     /* If exception is not clear, return */
1368     g_return_if_fail(SP_EXCEPTION_IS_OK(ex));
1370     /// \todo fixme: Exception if object is NULL? */
1371     if (!sp_repr_set_attr(object->repr, key, NULL)) {
1372         ex->code = SP_NO_MODIFICATION_ALLOWED_ERR;
1373     }
1376 /* Helper */
1378 static gchar *
1379 sp_object_get_unique_id(SPObject *object, gchar const *id)
1381     static unsigned long count = 0;
1383     g_assert(SP_IS_OBJECT(object));
1385     count++;
1387     gchar const *name = object->repr->name();
1388     g_assert(name != NULL);
1390     gchar const *local = strchr(name, ':');
1391     if (local) {
1392         name = local + 1;
1393     }
1395     if (id != NULL) {
1396         if (object->document->getObjectById(id) == NULL) {
1397             return g_strdup(id);
1398         }
1399     }
1401     size_t const name_len = strlen(name);
1402     size_t const buflen = name_len + (sizeof(count) * 10 / 4) + 1;
1403     gchar *const buf = (gchar *) g_malloc(buflen);
1404     memcpy(buf, name, name_len);
1405     gchar *const count_buf = buf + name_len;
1406     size_t const count_buflen = buflen - name_len;
1407     do {
1408         ++count;
1409         g_snprintf(count_buf, count_buflen, "%lu", count);
1410     } while ( object->document->getObjectById(buf) != NULL );
1411     return buf;
1414 /* Style */
1416 /**
1417  * Returns an object style property.
1418  *
1419  * \todo
1420  * fixme: Use proper CSS parsing.  The current version is buggy
1421  * in a number of situations where key is a substring of the
1422  * style string other than as a property name (including
1423  * where key is a substring of a property name), and is also
1424  * buggy in its handling of inheritance for properties that
1425  * aren't inherited by default.  It also doesn't allow for
1426  * the case where the property is specified but with an invalid
1427  * value (in which case I believe the CSS2 error-handling
1428  * behaviour applies, viz. behave as if the property hadn't
1429  * been specified).  Also, the current code doesn't use CRSelEng
1430  * stuff to take a value from stylesheets.  Also, we aren't
1431  * setting any hooks to force an update for changes in any of
1432  * the inputs (i.e., in any of the elements that this function
1433  * queries).
1434  *
1435  * \par
1436  * Given that the default value for a property depends on what
1437  * property it is (e.g., whether to inherit or not), and given
1438  * the above comment about ignoring invalid values, and that the
1439  * repr parent isn't necessarily the right element to inherit
1440  * from (e.g., maybe we need to inherit from the referencing
1441  * <use> element instead), we should probably make the caller
1442  * responsible for ascending the repr tree as necessary.
1443  */
1444 gchar const *
1445 sp_object_get_style_property(SPObject const *object, gchar const *key, gchar const *def)
1447     g_return_val_if_fail(object != NULL, NULL);
1448     g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
1449     g_return_val_if_fail(key != NULL, NULL);
1451     gchar const *style = object->repr->attribute("style");
1452     if (style) {
1453         size_t const len = strlen(key);
1454         char const *p;
1455         while ( (p = strstr(style, key))
1456                 != NULL )
1457         {
1458             p += len;
1459             while ((*p <= ' ') && *p) p++;
1460             if (*p++ != ':') break;
1461             while ((*p <= ' ') && *p) p++;
1462             size_t const inherit_len = sizeof("inherit") - 1;
1463             if (*p
1464                 && !(strneq(p, "inherit", inherit_len)
1465                      && (p[inherit_len] == '\0'
1466                          || p[inherit_len] == ';'
1467                          || g_ascii_isspace(p[inherit_len])))) {
1468                 return p;
1469             }
1470         }
1471     }
1472     gchar const *val = object->repr->attribute(key);
1473     if (val && !streq(val, "inherit")) {
1474         return val;
1475     }
1476     if (object->parent) {
1477         return sp_object_get_style_property(object->parent, key, def);
1478     }
1480     return def;
1483 /**
1484  * Lifts SVG version of all root objects to version.
1485  */
1486 void
1487 SPObject::_requireSVGVersion(Inkscape::Version version) {
1488     for ( SPObject::ParentIterator iter=this ; iter ; ++iter ) {
1489         SPObject *object=iter;
1490         if (SP_IS_ROOT(object)) {
1491             SPRoot *root=SP_ROOT(object);
1492             if ( root->version.svg < version ) {
1493                 root->version.svg = version;
1494             }
1495         }
1496     }
1499 /**
1500  * Return sodipodi version of first root ancestor or (0,0).
1501  */
1502 Inkscape::Version
1503 sp_object_get_sodipodi_version(SPObject *object)
1505     static Inkscape::Version const zero_version(0, 0);
1507     while (object) {
1508         if (SP_IS_ROOT(object)) {
1509             return SP_ROOT(object)->version.sodipodi;
1510         }
1511         object = SP_OBJECT_PARENT(object);
1512     }
1514     return zero_version;
1517 /**
1518  * Returns previous object in sibling list or NULL.
1519  */
1520 SPObject *
1521 sp_object_prev(SPObject *child)
1523     SPObject *parent = SP_OBJECT_PARENT(child);
1524     for ( SPObject *i = sp_object_first_child(parent); i; i = SP_OBJECT_NEXT(i) ) {
1525         if (SP_OBJECT_NEXT(i) == child)
1526             return i;
1527     }
1528     return NULL;
1532 /*
1533   Local Variables:
1534   mode:c++
1535   c-file-style:"stroustrup"
1536   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1537   indent-tabs-mode:nil
1538   fill-column:99
1539   End:
1540 */
1541 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :