Code

shared_ptr -> ptr_shared
[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"
48 #include "algorithms/longest-common-suffix.h"
49 using std::memcpy;
50 using std::strchr;
51 using std::strcmp;
52 using std::strlen;
53 using std::strstr;
55 #define noSP_OBJECT_DEBUG_CASCADE
57 #define noSP_OBJECT_DEBUG
59 #ifdef SP_OBJECT_DEBUG
60 # define debug(f, a...) { g_print("%s(%d) %s:", \
61                                   __FILE__,__LINE__,__FUNCTION__); \
62                           g_print(f, ## a); \
63                           g_print("\n"); \
64                         }
65 #else
66 # define debug(f, a...) /**/
67 #endif
69 static void sp_object_class_init(SPObjectClass *klass);
70 static void sp_object_init(SPObject *object);
71 static void sp_object_finalize(GObject *object);
73 static void sp_object_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref);
74 static void sp_object_remove_child(SPObject *object, Inkscape::XML::Node *child);
75 static void sp_object_order_changed(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref, Inkscape::XML::Node *new_ref);
77 static void sp_object_release(SPObject *object);
78 static void sp_object_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
80 static void sp_object_private_set(SPObject *object, unsigned int key, gchar const *value);
81 static Inkscape::XML::Node *sp_object_private_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
83 /* Real handlers of repr signals */
85 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);
87 static void sp_object_repr_content_changed(Inkscape::XML::Node *repr, gchar const *oldcontent, gchar const *newcontent, gpointer data);
89 static void sp_object_repr_child_added(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, gpointer data);
90 static void sp_object_repr_child_removed(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, void *data);
92 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);
94 static gchar *sp_object_get_unique_id(SPObject *object, gchar const *defid);
96 guint update_in_progress = 0; // guard against update-during-update
98 enum {RELEASE, MODIFIED, LAST_SIGNAL};
100 Inkscape::XML::NodeEventVector object_event_vector = {
101     sp_object_repr_child_added,
102     sp_object_repr_child_removed,
103     sp_object_repr_attr_changed,
104     sp_object_repr_content_changed,
105     sp_object_repr_order_changed
106 };
108 static GObjectClass *parent_class;
109 static guint object_signals[LAST_SIGNAL] = {0};
111 /**
112  * Registers the SPObject class with Gdk and returns its type number.
113  */
114 GType
115 sp_object_get_type(void)
117     static GType type = 0;
118     if (!type) {
119         GTypeInfo info = {
120             sizeof(SPObjectClass),
121             NULL, NULL,
122             (GClassInitFunc) sp_object_class_init,
123             NULL, NULL,
124             sizeof(SPObject),
125             16,
126             (GInstanceInitFunc) sp_object_init,
127             NULL
128         };
129         type = g_type_register_static(G_TYPE_OBJECT, "SPObject", &info, (GTypeFlags)0);
130     }
131     return type;
134 /**
135  * Initializes the SPObject vtable.
136  */
137 static void
138 sp_object_class_init(SPObjectClass *klass)
140     GObjectClass *object_class;
142     object_class = (GObjectClass *) klass;
144     parent_class = (GObjectClass *) g_type_class_ref(G_TYPE_OBJECT);
146     object_signals[RELEASE] =  g_signal_new("release",
147                                             G_TYPE_FROM_CLASS(klass),
148                                             (GSignalFlags)(G_SIGNAL_RUN_CLEANUP | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS),
149                                             G_STRUCT_OFFSET(SPObjectClass, release),
150                                             NULL, NULL,
151                                             sp_marshal_VOID__VOID,
152                                             G_TYPE_NONE, 0);
153     object_signals[MODIFIED] = g_signal_new("modified",
154                                             G_TYPE_FROM_CLASS(klass),
155                                             G_SIGNAL_RUN_FIRST,
156                                             G_STRUCT_OFFSET(SPObjectClass, modified),
157                                             NULL, NULL,
158                                             sp_marshal_NONE__UINT,
159                                             G_TYPE_NONE, 1, G_TYPE_UINT);
161     object_class->finalize = sp_object_finalize;
163     klass->child_added = sp_object_child_added;
164     klass->remove_child = sp_object_remove_child;
165     klass->order_changed = sp_object_order_changed;
167     klass->release = sp_object_release;
169     klass->build = sp_object_build;
171     klass->set = sp_object_private_set;
172     klass->write = sp_object_private_write;
175 /**
176  * Callback to initialize the SPObject object.
177  */
178 static void
179 sp_object_init(SPObject *object)
181     debug("id=%x, typename=%s",object, g_type_name_from_instance((GTypeInstance*)object));
183     object->hrefcount = 0;
184     object->_total_hrefcount = 0;
185     object->document = NULL;
186     object->children = object->_last_child = NULL;
187     object->parent = object->next = NULL;
188     object->repr = NULL;
189     object->id = NULL;
190     object->style = NULL;
192     object->_collection_policy = SPObject::COLLECT_WITH_PARENT;
194     new (&object->_delete_signal) sigc::signal<void, SPObject *>();
195     new (&object->_position_changed_signal) sigc::signal<void, SPObject *>();
196     object->_successor = NULL;
198     object->_label = NULL;
199     object->_default_label = NULL;
202 /**
203  * Callback to destroy all members and connections of object and itself.
204  */
205 static void
206 sp_object_finalize(GObject *object)
208     SPObject *spobject = (SPObject *)object;
210     g_free(spobject->_label);
211     g_free(spobject->_default_label);
212     spobject->_label = NULL;
213     spobject->_default_label = NULL;
215     if (spobject->_successor) {
216         sp_object_unref(spobject->_successor, NULL);
217         spobject->_successor = NULL;
218     }
220     if (((GObjectClass *) (parent_class))->finalize) {
221         (* ((GObjectClass *) (parent_class))->finalize)(object);
222     }
224     spobject->_delete_signal.~signal();
225     spobject->_position_changed_signal.~signal();
228 namespace {
230 Inkscape::Util::ptr_shared<char> stringify(SPObject *obj) {
231     char *temp=g_strdup_printf("%p", obj);
232     Inkscape::Util::ptr_shared<char> result=Inkscape::Util::share_string(temp);
233     g_free(temp);
234     return result;
237 Inkscape::Util::ptr_shared<char> stringify(unsigned n) {
238     char *temp=g_strdup_printf("%u", n);
239     Inkscape::Util::ptr_shared<char> result=Inkscape::Util::share_string(temp);
240     g_free(temp);
241     return result;
244 class RefEvent : public Inkscape::Debug::Event {
245 public:
246     enum Type { REF, UNREF };
248     RefEvent(SPObject *object, Type type)
249         : _object(stringify(object)), _refcount(G_OBJECT(object)->ref_count),
250           _type(type)
251     {}
253     static Category category() { return REFCOUNT; }
255     Inkscape::Util::ptr_shared<char> name() const {
256         if ( _type == REF) {
257             return Inkscape::Util::share_static_string("sp-object-ref");
258         } else {
259             return Inkscape::Util::share_static_string("sp-object-unref");
260         }
261     }
262     unsigned propertyCount() const { return 2; }
263     PropertyPair property(unsigned index) const {
264         switch (index) {
265             case 0:
266                 return PropertyPair("object", _object);
267             case 1:
268                 return PropertyPair("refcount", stringify( _type == REF ? _refcount + 1 : _refcount - 1 ));
269             default:
270                 return PropertyPair();
271         }
272     }
274 private:
275     Inkscape::Util::ptr_shared<char> _object;
276     unsigned _refcount;
277     Type _type;
278 };
282 /**
283  * Increase reference count of object, with possible debugging.
284  *
285  * \param owner If non-NULL, make debug log entry.
286  * \return object, NULL is error.
287  * \pre object points to real object
288  */
289 SPObject *
290 sp_object_ref(SPObject *object, SPObject *owner)
292     g_return_val_if_fail(object != NULL, NULL);
293     g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
294     g_return_val_if_fail(!owner || SP_IS_OBJECT(owner), NULL);
296     Inkscape::Debug::EventTracker<> tracker;
297     tracker.set<RefEvent>(object, RefEvent::REF);
299     g_object_ref(G_OBJECT(object));
301     return object;
304 /**
305  * Decrease reference count of object, with possible debugging and
306  * finalization.
307  *
308  * \param owner If non-NULL, make debug log entry.
309  * \return always NULL
310  * \pre object points to real object
311  */
312 SPObject *
313 sp_object_unref(SPObject *object, SPObject *owner)
315     g_return_val_if_fail(object != NULL, NULL);
316     g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
317     g_return_val_if_fail(!owner || SP_IS_OBJECT(owner), NULL);
319     Inkscape::Debug::EventTracker<> tracker;
320     tracker.set<RefEvent>(object, RefEvent::UNREF);
322     g_object_unref(G_OBJECT(object));
324     return NULL;
327 /**
328  * Increase weak refcount.
329  *
330  * Hrefcount is used for weak references, for example, to
331  * determine whether any graphical element references a certain gradient
332  * node.
333  * \param owner Ignored.
334  * \return object, NULL is error
335  * \pre object points to real object
336  */
337 SPObject *
338 sp_object_href(SPObject *object, gpointer owner)
340     g_return_val_if_fail(object != NULL, NULL);
341     g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
343     object->hrefcount++;
344     object->_updateTotalHRefCount(1);
346     return object;
349 /**
350  * Decrease weak refcount.
351  *
352  * Hrefcount is used for weak references, for example, to determine whether
353  * any graphical element references a certain gradient node.
354  * \param owner Ignored.
355  * \return always NULL
356  * \pre object points to real object and hrefcount>0
357  */
358 SPObject *
359 sp_object_hunref(SPObject *object, gpointer owner)
361     g_return_val_if_fail(object != NULL, NULL);
362     g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
363     g_return_val_if_fail(object->hrefcount > 0, NULL);
365     object->hrefcount--;
366     object->_updateTotalHRefCount(-1);
368     return NULL;
371 /**
372  * Adds increment to _total_hrefcount of object and its parents.
373  */
374 void
375 SPObject::_updateTotalHRefCount(int increment) {
376     SPObject *topmost_collectable = NULL;
377     for ( SPObject *iter = this ; iter ; iter = SP_OBJECT_PARENT(iter) ) {
378         iter->_total_hrefcount += increment;
379         if ( iter->_total_hrefcount < iter->hrefcount ) {
380             g_critical("HRefs overcounted");
381         }
382         if ( iter->_total_hrefcount == 0 &&
383              iter->_collection_policy != COLLECT_WITH_PARENT )
384         {
385             topmost_collectable = iter;
386         }
387     }
388     if (topmost_collectable) {
389         topmost_collectable->requestOrphanCollection();
390     }
393 /**
394  * True if object is non-NULL and this is some in/direct parent of object.
395  */
396 bool
397 SPObject::isAncestorOf(SPObject const *object) const {
398     g_return_val_if_fail(object != NULL, false);
399     object = SP_OBJECT_PARENT(object);
400     while (object) {
401         if ( object == this ) {
402             return true;
403         }
404         object = SP_OBJECT_PARENT(object);
405     }
406     return false;
409 namespace {
411 bool same_objects(SPObject const &a, SPObject const &b) {
412     return &a == &b;
417 /**
418  * Returns youngest object being parent to this and object.
419  */
420 SPObject const *
421 SPObject::nearestCommonAncestor(SPObject const *object) const {
422     g_return_val_if_fail(object != NULL, NULL);
424     using Inkscape::Algorithms::longest_common_suffix;
425     return longest_common_suffix<SPObject::ConstParentIterator>(this, object, NULL, &same_objects);
428 SPObject const *AncestorSon(SPObject const *obj, SPObject const *ancestor) {
429     if (obj == NULL || ancestor == NULL)
430         return NULL;
431     if (SP_OBJECT_PARENT(obj) == ancestor)
432         return obj;
433     return AncestorSon(SP_OBJECT_PARENT(obj), ancestor);
436 /**
437  * Compares height of objects in tree.
438  *
439  * Works for different-parent objects, so long as they have a common ancestor.
440  * \return \verbatim
441  *    0    positions are equivalent
442  *    1    first object's position is greater than the second
443  *   -1    first object's position is less than the second   \endverbatim
444  */
445 int
446 sp_object_compare_position(SPObject const *first, SPObject const *second)
448     if (first == second) return 0;
450     SPObject const *ancestor = first->nearestCommonAncestor(second);
451     if (ancestor == NULL) return 0; // cannot compare, no common ancestor!
453     // we have an object and its ancestor (should not happen when sorting selection)
454     if (ancestor == first)
455         return 1;
456     if (ancestor == second)
457         return -1;
459     SPObject const *to_first = AncestorSon(first, ancestor);
460     SPObject const *to_second = AncestorSon(second, ancestor);
462     g_assert(SP_OBJECT_PARENT(to_second) == SP_OBJECT_PARENT(to_first));
464     return sp_repr_compare_position(SP_OBJECT_REPR(to_first), SP_OBJECT_REPR(to_second));
468 /**
469  * Append repr as child of this object.
470  * \pre this is not a cloned object
471  */
472 SPObject *
473 SPObject::appendChildRepr(Inkscape::XML::Node *repr) {
474     if (!SP_OBJECT_IS_CLONED(this)) {
475         SP_OBJECT_REPR(this)->appendChild(repr);
476         return SP_OBJECT_DOCUMENT(this)->getObjectByRepr(repr);
477     } else {
478         g_critical("Attempt to append repr as child of cloned object");
479         return NULL;
480     }
483 /** Gets the label property for the object or a default if no label
484  *  is defined.
485  */
486 gchar const *
487 SPObject::label() const {
488     return _label;
491 /** Returns a default label property for the object. */
492 gchar const *
493 SPObject::defaultLabel() const {
494     if (_label) {
495         return _label;
496     } else {
497         if (!_default_label) {
498             gchar const *id=SP_OBJECT_ID(this);
499             if (id) {
500                 _default_label = g_strdup_printf("#%s", id);
501             } else {
502                 _default_label = g_strdup_printf("<%s>", SP_OBJECT_REPR(this)->name());
503             }
504         }
505         return _default_label;
506     }
509 /** Sets the label property for the object */
510 void
511 SPObject::setLabel(gchar const *label) {
512     SP_OBJECT_REPR(this)->setAttribute("inkscape:label", label, false);
516 /** Queues the object for orphan collection */
517 void
518 SPObject::requestOrphanCollection() {
519     g_return_if_fail(document != NULL);
520     document->queueForOrphanCollection(this);
522     /** \todo
523      * This is a temporary hack added to make fill&stroke rebuild its
524      * gradient list when the defs are vacuumed.  gradient-vector.cpp
525      * listens to the modified signal on defs, and now we give it that
526      * signal.  Mental says that this should be made automatic by
527      * merging SPObjectGroup with SPObject; SPObjectGroup would issue
528      * this signal automatically. Or maybe just derive SPDefs from
529      * SPObjectGroup?
530      */
532     this->requestModified(SP_OBJECT_CHILD_MODIFIED_FLAG);
535 /** Sends the delete signal to all children of this object recursively */
536 void
537 SPObject::_sendDeleteSignalRecursive() {
538     for (SPObject *child = sp_object_first_child(this); child; child = SP_OBJECT_NEXT(child)) {
539         child->_delete_signal.emit(child);
540         child->_sendDeleteSignalRecursive();
541     }
544 /**
545  * Deletes the object reference, unparenting it from its parent.
546  *
547  * If the \a propagate parameter is set to true, it emits a delete
548  * signal.  If the \a propagate_descendants parameter is true, it
549  * recursively sends the delete signal to children.
550  */
551 void
552 SPObject::deleteObject(bool propagate, bool propagate_descendants)
554     sp_object_ref(this, NULL);
555     if (propagate) {
556         _delete_signal.emit(this);
557     }
558     if (propagate_descendants) {
559         this->_sendDeleteSignalRecursive();
560     }
562     Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
563     if (repr && sp_repr_parent(repr)) {
564         sp_repr_unparent(repr);
565     }
567     if (_successor) {
568         _successor->deleteObject(propagate, propagate_descendants);
569     }
570     sp_object_unref(this, NULL);
573 /**
574  * Put object into object tree, under parent, and behind prev;
575  * also update object's XML space.
576  */
577 void
578 sp_object_attach(SPObject *parent, SPObject *object, SPObject *prev)
580     g_return_if_fail(parent != NULL);
581     g_return_if_fail(SP_IS_OBJECT(parent));
582     g_return_if_fail(object != NULL);
583     g_return_if_fail(SP_IS_OBJECT(object));
584     g_return_if_fail(!prev || SP_IS_OBJECT(prev));
585     g_return_if_fail(!prev || prev->parent == parent);
586     g_return_if_fail(!object->parent);
588     sp_object_ref(object, parent);
589     object->parent = parent;
590     parent->_updateTotalHRefCount(object->_total_hrefcount);
592     SPObject *next;
593     if (prev) {
594         next = prev->next;
595         prev->next = object;
596     } else {
597         next = parent->children;
598         parent->children = object;
599     }
600     object->next = next;
601     if (!next) {
602         parent->_last_child = object;
603     }
604     if (!object->xml_space.set)
605         object->xml_space.value = parent->xml_space.value;
608 /**
609  * In list of object's siblings, move object behind prev.
610  */
611 void
612 sp_object_reorder(SPObject *object, SPObject *prev) {
613     g_return_if_fail(object != NULL);
614     g_return_if_fail(SP_IS_OBJECT(object));
615     g_return_if_fail(object->parent != NULL);
616     g_return_if_fail(object != prev);
617     g_return_if_fail(!prev || SP_IS_OBJECT(prev));
618     g_return_if_fail(!prev || prev->parent == object->parent);
620     SPObject *const parent=object->parent;
622     SPObject *old_prev=NULL;
623     for ( SPObject *child = parent->children ; child && child != object ;
624           child = child->next )
625     {
626         old_prev = child;
627     }
629     SPObject *next=object->next;
630     if (old_prev) {
631         old_prev->next = next;
632     } else {
633         parent->children = next;
634     }
635     if (!next) {
636         parent->_last_child = old_prev;
637     }
638     if (prev) {
639         next = prev->next;
640         prev->next = object;
641     } else {
642         next = parent->children;
643         parent->children = object;
644     }
645     object->next = next;
646     if (!next) {
647         parent->_last_child = object;
648     }
651 /**
652  * Remove object from parent's children, release and unref it.
653  */
654 void
655 sp_object_detach(SPObject *parent, SPObject *object) {
656     g_return_if_fail(parent != NULL);
657     g_return_if_fail(SP_IS_OBJECT(parent));
658     g_return_if_fail(object != NULL);
659     g_return_if_fail(SP_IS_OBJECT(object));
660     g_return_if_fail(object->parent == parent);
662     sp_object_invoke_release(object);
664     SPObject *prev=NULL;
665     for ( SPObject *child = parent->children ; child && child != object ;
666           child = child->next )
667     {
668         prev = child;
669     }
671     SPObject *next=object->next;
672     if (prev) {
673         prev->next = next;
674     } else {
675         parent->children = next;
676     }
677     if (!next) {
678         parent->_last_child = prev;
679     }
681     object->next = NULL;
682     object->parent = NULL;
684     parent->_updateTotalHRefCount(-object->_total_hrefcount);
685     sp_object_unref(object, parent);
688 /**
689  * Return object's child whose node pointer equals repr.
690  */
691 SPObject *
692 sp_object_get_child_by_repr(SPObject *object, Inkscape::XML::Node *repr)
694     g_return_val_if_fail(object != NULL, NULL);
695     g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
696     g_return_val_if_fail(repr != NULL, NULL);
698     if (object->_last_child && SP_OBJECT_REPR(object->_last_child) == repr)
699         return object->_last_child;   // optimization for common scenario
700     for ( SPObject *child = object->children ; child ; child = child->next ) {
701         if ( SP_OBJECT_REPR(child) == repr ) {
702             return child;
703         }
704     }
706     return NULL;
709 /**
710  * Callback for child_added event.
711  * Invoked whenever the given mutation event happens in the XML tree.
712  */
713 static void
714 sp_object_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
716     GType type = sp_repr_type_lookup(child);
717     if (!type) {
718         return;
719     }
720     SPObject *ochild = SP_OBJECT(g_object_new(type, 0));
721     SPObject *prev = ref ? sp_object_get_child_by_repr(object, ref) : NULL;
722     sp_object_attach(object, ochild, prev);
723     sp_object_unref(ochild, NULL);
725     sp_object_invoke_build(ochild, object->document, child, SP_OBJECT_IS_CLONED(object));
728 /**
729  * Removes, releases and unrefs all children of object.
730  *
731  * This is the opposite of build. It has to be invoked as soon as the
732  * object is removed from the tree, even if it is still alive according
733  * to reference count. The frontend unregisters the object from the
734  * document and releases the SPRepr bindings; implementations should free
735  * state data and release all child objects.  Invoking release on
736  * SPRoot destroys the whole document tree.
737  * \see sp_object_build()
738  */
739 static void sp_object_release(SPObject *object)
741     debug("id=%x, typename=%s", object, g_type_name_from_instance((GTypeInstance*)object));
742     while (object->children) {
743         sp_object_detach(object, object->children);
744     }
747 /**
748  * Remove object's child whose node equals repr, release and
749  * unref it.
750  *
751  * Invoked whenever the given mutation event happens in the XML
752  * tree, BEFORE removal from the XML tree happens, so grouping
753  * objects can safely release the child data.
754  */
755 static void
756 sp_object_remove_child(SPObject *object, Inkscape::XML::Node *child)
758     debug("id=%x, typename=%s", object, g_type_name_from_instance((GTypeInstance*)object));
759     SPObject *ochild = sp_object_get_child_by_repr(object, child);
760     g_return_if_fail(ochild != NULL);
761     sp_object_detach(object, ochild);
764 /**
765  * Move object corresponding to child after sibling object corresponding
766  * to new_ref.
767  * Invoked whenever the given mutation event happens in the XML tree.
768  * \param old_ref Ignored
769  */
770 static void sp_object_order_changed(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref,
771                                     Inkscape::XML::Node *new_ref)
773     SPObject *ochild = sp_object_get_child_by_repr(object, child);
774     g_return_if_fail(ochild != NULL);
775     SPObject *prev = new_ref ? sp_object_get_child_by_repr(object, new_ref) : NULL;
776     sp_object_reorder(ochild, prev);
777     ochild->_position_changed_signal.emit(ochild);
780 /**
781  * Virtual build callback.
782  *
783  * This has to be invoked immediately after creation of an SPObject. The
784  * frontend method ensures that the new object is properly attached to
785  * the document and repr; implementation then will parse all of the attributes,
786  * generate the children objects and so on.  Invoking build on the SPRoot
787  * object results in creation of the whole document tree (this is, what
788  * SPDocument does after the creation of the XML tree).
789  * \see sp_object_release()
790  */
791 static void
792 sp_object_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
794     /* Nothing specific here */
795     debug("id=%x, typename=%s", object, g_type_name_from_instance((GTypeInstance*)object));
797     sp_object_read_attr(object, "xml:space");
798     sp_object_read_attr(object, "inkscape:label");
799     sp_object_read_attr(object, "inkscape:collect");
801     for (Inkscape::XML::Node *rchild = repr->firstChild() ; rchild != NULL; rchild = rchild->next()) {
802         GType type = sp_repr_type_lookup(rchild);
803         if (!type) {
804             continue;
805         }
806         SPObject *child = SP_OBJECT(g_object_new(type, 0));
807         sp_object_attach(object, child, object->lastChild());
808         sp_object_unref(child, NULL);
809         sp_object_invoke_build(child, document, rchild, SP_OBJECT_IS_CLONED(object));
810     }
813 void
814 sp_object_invoke_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr, unsigned int cloned)
816     debug("id=%x, typename=%s", object, g_type_name_from_instance((GTypeInstance*)object));
818     g_assert(object != NULL);
819     g_assert(SP_IS_OBJECT(object));
820     g_assert(document != NULL);
821     g_assert(repr != NULL);
823     g_assert(object->document == NULL);
824     g_assert(object->repr == NULL);
825     g_assert(object->id == NULL);
827     /* Bookkeeping */
829     object->document = document;
830     object->repr = repr;
831     Inkscape::GC::anchor(repr);
832     object->cloned = cloned;
834     if (!SP_OBJECT_IS_CLONED(object)) {
835         object->document->bindObjectToRepr(object->repr, object);
837         if (Inkscape::XML::id_permitted(object->repr)) {
838             /* If we are not cloned, force unique id */
839             gchar const *id = object->repr->attribute("id");
840             gchar *realid = sp_object_get_unique_id(object, id);
841             g_assert(realid != NULL);
843             object->document->bindObjectToId(realid, object);
844             object->id = realid;
846             /* Redefine ID, if required */
847             if ((id == NULL) || (strcmp(id, realid) != 0)) {
848                 gboolean undo_sensitive=sp_document_get_undo_sensitive(document);
849                 sp_document_set_undo_sensitive(document, FALSE);
850                 object->repr->setAttribute("id", realid);
851                 sp_document_set_undo_sensitive(document, undo_sensitive);
852             }
853         }
854     } else {
855         g_assert(object->id == NULL);
856     }
858     /* Invoke derived methods, if any */
860     if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->build) {
861         (*((SPObjectClass *) G_OBJECT_GET_CLASS(object))->build)(object, document, repr);
862     }
864     /* Signalling (should be connected AFTER processing derived methods */
865     sp_repr_add_listener(repr, &object_event_vector, object);
868 void
869 sp_object_invoke_release(SPObject *object)
871     g_assert(object != NULL);
872     g_assert(SP_IS_OBJECT(object));
874     g_assert(object->document);
875     g_assert(object->repr);
877     sp_repr_remove_listener_by_data(object->repr, object);
879     g_signal_emit(G_OBJECT(object), object_signals[RELEASE], 0);
881     /* all hrefs should be released by the "release" handlers */
882     g_assert(object->hrefcount == 0);
884     if (!SP_OBJECT_IS_CLONED(object)) {
885         if (object->id) {
886             object->document->bindObjectToId(object->id, NULL);
887         }
888         g_free(object->id);
889         object->id = NULL;
891         g_free(object->_default_label);
892         object->_default_label = NULL;
894         object->document->bindObjectToRepr(object->repr, NULL);
895     } else {
896         g_assert(!object->id);
897     }
899     if (object->style) {
900         object->style = sp_style_unref(object->style);
901     }
903     Inkscape::GC::release(object->repr);
905     object->document = NULL;
906     object->repr = NULL;
909 /**
910  * Callback for child_added node event.
911  */
912 static void
913 sp_object_repr_child_added(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, gpointer data)
915     SPObject *object = SP_OBJECT(data);
917     if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->child_added)
918         (*((SPObjectClass *)G_OBJECT_GET_CLASS(object))->child_added)(object, child, ref);
921 /**
922  * Callback for remove_child node event.
923  */
924 static void
925 sp_object_repr_child_removed(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, gpointer data)
927     SPObject *object = SP_OBJECT(data);
929     if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->remove_child) {
930         (* ((SPObjectClass *)G_OBJECT_GET_CLASS(object))->remove_child)(object, child);
931     }
934 /**
935  * Callback for order_changed node event.
936  *
937  * \todo fixme:
938  */
939 static void
940 sp_object_repr_order_changed(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *old, Inkscape::XML::Node *newer, gpointer data)
942     SPObject *object = SP_OBJECT(data);
944     if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->order_changed) {
945         (* ((SPObjectClass *)G_OBJECT_GET_CLASS(object))->order_changed)(object, child, old, newer);
946     }
949 /**
950  * Callback for set event.
951  */
952 static void
953 sp_object_private_set(SPObject *object, unsigned int key, gchar const *value)
955     g_assert(key != SP_ATTR_INVALID);
957     switch (key) {
958         case SP_ATTR_ID:
959             if ( !SP_OBJECT_IS_CLONED(object) && object->repr->type() == Inkscape::XML::ELEMENT_NODE ) {
960                 SPDocument *document=object->document;
961                 SPObject *conflict=NULL;
963                 if (value) {
964                     conflict = document->getObjectById((char const *)value);
965                 }
966                 if ( conflict && conflict != object ) {
967                     sp_object_ref(conflict, NULL);
968                     // give the conflicting object a new ID
969                     gchar *new_conflict_id = sp_object_get_unique_id(conflict, NULL);
970                     SP_OBJECT_REPR(conflict)->setAttribute("id", new_conflict_id);
971                     g_free(new_conflict_id);
972                     sp_object_unref(conflict, NULL);
973                 }
975                 if (object->id) {
976                     document->bindObjectToId(object->id, NULL);
977                     g_free(object->id);
978                 }
980                 if (value) {
981                     object->id = g_strdup((char const*)value);
982                     document->bindObjectToId(object->id, object);
983                 } else {
984                     object->id = NULL;
985                 }
987                 g_free(object->_default_label);
988                 object->_default_label = NULL;
989             }
990             break;
991         case SP_ATTR_INKSCAPE_LABEL:
992             g_free(object->_label);
993             if (value) {
994                 object->_label = g_strdup(value);
995             } else {
996                 object->_label = NULL;
997             }
998             g_free(object->_default_label);
999             object->_default_label = NULL;
1000             break;
1001         case SP_ATTR_INKSCAPE_COLLECT:
1002             if ( value && !strcmp(value, "always") ) {
1003                 object->setCollectionPolicy(SPObject::ALWAYS_COLLECT);
1004             } else {
1005                 object->setCollectionPolicy(SPObject::COLLECT_WITH_PARENT);
1006             }
1007             break;
1008         case SP_ATTR_XML_SPACE:
1009             if (value && !strcmp(value, "preserve")) {
1010                 object->xml_space.value = SP_XML_SPACE_PRESERVE;
1011                 object->xml_space.set = TRUE;
1012             } else if (value && !strcmp(value, "default")) {
1013                 object->xml_space.value = SP_XML_SPACE_DEFAULT;
1014                 object->xml_space.set = TRUE;
1015             } else if (object->parent) {
1016                 SPObject *parent;
1017                 parent = object->parent;
1018                 object->xml_space.value = parent->xml_space.value;
1019             }
1020             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
1021             break;
1022         default:
1023             break;
1024     }
1027 /**
1028  * Call virtual set() function of object.
1029  */
1030 void
1031 sp_object_set(SPObject *object, unsigned int key, gchar const *value)
1033     g_assert(object != NULL);
1034     g_assert(SP_IS_OBJECT(object));
1036     if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->set) {
1037         ((SPObjectClass *) G_OBJECT_GET_CLASS(object))->set(object, key, value);
1038     }
1041 /**
1042  * Read value of key attribute from XML node into object.
1043  */
1044 void
1045 sp_object_read_attr(SPObject *object, gchar const *key)
1047     g_assert(object != NULL);
1048     g_assert(SP_IS_OBJECT(object));
1049     g_assert(key != NULL);
1051     g_assert(object->repr != NULL);
1053     unsigned int keyid = sp_attribute_lookup(key);
1054     if (keyid != SP_ATTR_INVALID) {
1055         /* Retrieve the 'key' attribute from the object's XML representation */
1056         gchar const *value = object->repr->attribute(key);
1058         sp_object_set(object, keyid, value);
1059     }
1062 /**
1063  * Callback for attr_changed node event.
1064  */
1065 static void
1066 sp_object_repr_attr_changed(Inkscape::XML::Node *repr, gchar const *key, gchar const *oldval, gchar const *newval, bool is_interactive, gpointer data)
1068     SPObject *object = SP_OBJECT(data);
1070     sp_object_read_attr(object, key);
1072     // manual changes to extension attributes require the normal
1073     // attributes, which depend on them, to be updated immediately
1074     if (is_interactive) {
1075         object->updateRepr(repr, 0);
1076     }
1079 /**
1080  * Callback for content_changed node event.
1081  */
1082 static void
1083 sp_object_repr_content_changed(Inkscape::XML::Node *repr, gchar const *oldcontent, gchar const *newcontent, gpointer data)
1085     SPObject *object = SP_OBJECT(data);
1087     if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->read_content)
1088         (*((SPObjectClass *) G_OBJECT_GET_CLASS(object))->read_content)(object);
1091 /**
1092  * Return string representation of space value.
1093  */
1094 static gchar const*
1095 sp_xml_get_space_string(unsigned int space)
1097     switch (space) {
1098         case SP_XML_SPACE_DEFAULT:
1099             return "default";
1100         case SP_XML_SPACE_PRESERVE:
1101             return "preserve";
1102         default:
1103             return NULL;
1104     }
1107 /**
1108  * Callback for write event.
1109  */
1110 static Inkscape::XML::Node *
1111 sp_object_private_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
1113     if (!repr && (flags & SP_OBJECT_WRITE_BUILD)) {
1114         repr = SP_OBJECT_REPR(object)->duplicate();
1115         if (!( flags & SP_OBJECT_WRITE_EXT )) {
1116             repr->setAttribute("inkscape:collect", NULL);
1117         }
1118     } else {
1119         repr->setAttribute("id", object->id);
1121         if (object->xml_space.set) {
1122             char const *xml_space;
1123             xml_space = sp_xml_get_space_string(object->xml_space.value);
1124             repr->setAttribute("xml:space", xml_space);
1125         }
1127         if ( flags & SP_OBJECT_WRITE_EXT &&
1128              object->collectionPolicy() == SPObject::ALWAYS_COLLECT )
1129         {
1130             repr->setAttribute("inkscape:collect", "always");
1131         } else {
1132             repr->setAttribute("inkscape:collect", NULL);
1133         }
1134     }
1136     return repr;
1139 /**
1140  * Update this object's XML node with flags value.
1141  */
1142 Inkscape::XML::Node *
1143 SPObject::updateRepr(unsigned int flags) {
1144     if (!SP_OBJECT_IS_CLONED(this)) {
1145         Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
1146         if (repr) {
1147             return updateRepr(repr, flags);
1148         } else {
1149             g_critical("Attempt to update non-existent repr");
1150             return NULL;
1151         }
1152     } else {
1153         /* cloned objects have no repr */
1154         return NULL;
1155     }
1158 Inkscape::XML::Node *
1159 SPObject::updateRepr(Inkscape::XML::Node *repr, unsigned int flags) {
1160     if (SP_OBJECT_IS_CLONED(this)) {
1161         /* cloned objects have no repr */
1162         return NULL;
1163     }
1164     if (((SPObjectClass *) G_OBJECT_GET_CLASS(this))->write) {
1165         if (!(flags & SP_OBJECT_WRITE_BUILD) && !repr) {
1166             repr = SP_OBJECT_REPR(this);
1167         }
1168         return ((SPObjectClass *) G_OBJECT_GET_CLASS(this))->write(this, repr, flags);
1169     } else {
1170         g_warning("Class %s does not implement ::write", G_OBJECT_TYPE_NAME(this));
1171         if (!repr) {
1172             if (flags & SP_OBJECT_WRITE_BUILD) {
1173                 repr = SP_OBJECT_REPR(this)->duplicate();
1174             }
1175             /// \todo fixme: else probably error (Lauris) */
1176         } else {
1177             repr->mergeFrom(SP_OBJECT_REPR(this), "id");
1178         }
1179         return repr;
1180     }
1183 /* Modification */
1185 /**
1186  * Add \a flags to \a object's as dirtiness flags, and
1187  * recursively add CHILD_MODIFIED flag to
1188  * parent and ancestors (as far up as necessary).
1189  */
1190 void
1191 SPObject::requestDisplayUpdate(unsigned int flags)
1193     if (update_in_progress) {
1194         g_print("WARNING: Requested update while update in progress, counter = %d\n", update_in_progress);
1195     }
1197     g_return_if_fail(!(flags & SP_OBJECT_PARENT_MODIFIED_FLAG));
1198     g_return_if_fail((flags & SP_OBJECT_MODIFIED_FLAG) || (flags & SP_OBJECT_CHILD_MODIFIED_FLAG));
1199     g_return_if_fail(!((flags & SP_OBJECT_MODIFIED_FLAG) && (flags & SP_OBJECT_CHILD_MODIFIED_FLAG)));
1201     /* Check for propagate before we set any flags */
1202     /* Propagate means, that this is not passed through by modification request cascade yet */
1203     unsigned int propagate = (!(this->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG)));
1205     /* Just set this flags safe even if some have been set before */
1206     this->uflags |= flags;
1208     if (propagate) {
1209         if (this->parent) {
1210             this->parent->requestDisplayUpdate(SP_OBJECT_CHILD_MODIFIED_FLAG);
1211         } else {
1212             sp_document_request_modified(this->document);
1213         }
1214     }
1217 void
1218 SPObject::updateDisplay(SPCtx *ctx, unsigned int flags)
1220     g_return_if_fail(!(flags & ~SP_OBJECT_MODIFIED_CASCADE));
1222     update_in_progress ++;
1224 #ifdef SP_OBJECT_DEBUG_CASCADE
1225     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);
1226 #endif
1228     /* Get this flags */
1229     flags |= this->uflags;
1230     /* Copy flags to modified cascade for later processing */
1231     this->mflags |= this->uflags;
1232     /* We have to clear flags here to allow rescheduling update */
1233     this->uflags = 0;
1235     // Merge style if we have good reasons to think that parent style is changed */
1236     /** \todo
1237      * I am not sure whether we should check only propagated
1238      * flag. We are currently assuming that style parsing is
1239      * done immediately. I think this is correct (Lauris).
1240      */
1241     if ((flags & SP_OBJECT_STYLE_MODIFIED_FLAG) && (flags & SP_OBJECT_PARENT_MODIFIED_FLAG)) {
1242         if (this->style && this->parent) {
1243             sp_style_merge_from_parent(this->style, this->parent->style);
1244         }
1245     }
1247     if (((SPObjectClass *) G_OBJECT_GET_CLASS(this))->update)
1248         ((SPObjectClass *) G_OBJECT_GET_CLASS(this))->update(this, ctx, flags);
1250     update_in_progress --;
1253 void
1254 SPObject::requestModified(unsigned int flags)
1256     g_return_if_fail( this->document != NULL );
1258     /* PARENT_MODIFIED is computed later on and is not intended to be
1259      * "manually" queued */
1260     g_return_if_fail(!(flags & SP_OBJECT_PARENT_MODIFIED_FLAG));
1262     /* we should be setting either MODIFIED or CHILD_MODIFIED... */
1263     g_return_if_fail((flags & SP_OBJECT_MODIFIED_FLAG) || (flags & SP_OBJECT_CHILD_MODIFIED_FLAG));
1265     /* ...but not both */
1266     g_return_if_fail(!((flags & SP_OBJECT_MODIFIED_FLAG) && (flags & SP_OBJECT_CHILD_MODIFIED_FLAG)));
1268     unsigned int old_mflags=this->mflags;
1269     this->mflags |= flags;
1271     /* If we already had MODIFIED or CHILD_MODIFIED queued, we will
1272      * have already queued CHILD_MODIFIED with our ancestors and
1273      * need not disturb them again.
1274      */
1275     if (!( old_mflags & ( SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG ) )) {
1276         SPObject *parent=SP_OBJECT_PARENT(this);
1277         if (parent) {
1278             parent->requestModified(SP_OBJECT_CHILD_MODIFIED_FLAG);
1279         } else {
1280             sp_document_request_modified(SP_OBJECT_DOCUMENT(this));
1281         }
1282     }
1285 void
1286 SPObject::emitModified(unsigned int flags)
1288     /* only the MODIFIED_CASCADE flag is legal here */
1289     g_return_if_fail(!(flags & ~SP_OBJECT_MODIFIED_CASCADE));
1291 #ifdef SP_OBJECT_DEBUG_CASCADE
1292     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);
1293 #endif
1295     flags |= this->mflags;
1296     /* We have to clear mflags beforehand, as signal handlers may
1297      * make changes and therefore queue new modification notifications
1298      * themselves. */
1299     this->mflags = 0;
1301     g_object_ref(G_OBJECT(this));
1302     g_signal_emit(G_OBJECT(this), object_signals[MODIFIED], 0, flags);
1303     g_object_unref(G_OBJECT(this));
1306 /*
1307  * Get and set descriptive parameters
1308  *
1309  * These are inefficent, so they are not intended to be used interactively
1310  */
1312 gchar const *
1313 sp_object_title_get(SPObject *object)
1315     return NULL;
1318 gchar const *
1319 sp_object_description_get(SPObject *object)
1321     return NULL;
1324 unsigned int
1325 sp_object_title_set(SPObject *object, gchar const *title)
1327     return FALSE;
1330 unsigned int
1331 sp_object_description_set(SPObject *object, gchar const *desc)
1333     return FALSE;
1336 gchar const *
1337 sp_object_tagName_get(SPObject const *object, SPException *ex)
1339     /* If exception is not clear, return */
1340     if (!SP_EXCEPTION_IS_OK(ex)) {
1341         return NULL;
1342     }
1344     /// \todo fixme: Exception if object is NULL? */
1345     return object->repr->name();
1348 gchar const *
1349 sp_object_getAttribute(SPObject const *object, gchar const *key, SPException *ex)
1351     /* If exception is not clear, return */
1352     if (!SP_EXCEPTION_IS_OK(ex)) {
1353         return NULL;
1354     }
1356     /// \todo fixme: Exception if object is NULL? */
1357     return (gchar const *) object->repr->attribute(key);
1360 void
1361 sp_object_setAttribute(SPObject *object, gchar const *key, gchar const *value, SPException *ex)
1363     /* If exception is not clear, return */
1364     g_return_if_fail(SP_EXCEPTION_IS_OK(ex));
1366     /// \todo fixme: Exception if object is NULL? */
1367     if (!sp_repr_set_attr(object->repr, key, value)) {
1368         ex->code = SP_NO_MODIFICATION_ALLOWED_ERR;
1369     }
1372 void
1373 sp_object_removeAttribute(SPObject *object, gchar const *key, SPException *ex)
1375     /* If exception is not clear, return */
1376     g_return_if_fail(SP_EXCEPTION_IS_OK(ex));
1378     /// \todo fixme: Exception if object is NULL? */
1379     if (!sp_repr_set_attr(object->repr, key, NULL)) {
1380         ex->code = SP_NO_MODIFICATION_ALLOWED_ERR;
1381     }
1384 /* Helper */
1386 static gchar *
1387 sp_object_get_unique_id(SPObject *object, gchar const *id)
1389     static unsigned long count = 0;
1391     g_assert(SP_IS_OBJECT(object));
1393     count++;
1395     gchar const *name = object->repr->name();
1396     g_assert(name != NULL);
1398     gchar const *local = strchr(name, ':');
1399     if (local) {
1400         name = local + 1;
1401     }
1403     if (id != NULL) {
1404         if (object->document->getObjectById(id) == NULL) {
1405             return g_strdup(id);
1406         }
1407     }
1409     size_t const name_len = strlen(name);
1410     size_t const buflen = name_len + (sizeof(count) * 10 / 4) + 1;
1411     gchar *const buf = (gchar *) g_malloc(buflen);
1412     memcpy(buf, name, name_len);
1413     gchar *const count_buf = buf + name_len;
1414     size_t const count_buflen = buflen - name_len;
1415     do {
1416         ++count;
1417         g_snprintf(count_buf, count_buflen, "%lu", count);
1418     } while ( object->document->getObjectById(buf) != NULL );
1419     return buf;
1422 /* Style */
1424 /**
1425  * Returns an object style property.
1426  *
1427  * \todo
1428  * fixme: Use proper CSS parsing.  The current version is buggy
1429  * in a number of situations where key is a substring of the
1430  * style string other than as a property name (including
1431  * where key is a substring of a property name), and is also
1432  * buggy in its handling of inheritance for properties that
1433  * aren't inherited by default.  It also doesn't allow for
1434  * the case where the property is specified but with an invalid
1435  * value (in which case I believe the CSS2 error-handling
1436  * behaviour applies, viz. behave as if the property hadn't
1437  * been specified).  Also, the current code doesn't use CRSelEng
1438  * stuff to take a value from stylesheets.  Also, we aren't
1439  * setting any hooks to force an update for changes in any of
1440  * the inputs (i.e., in any of the elements that this function
1441  * queries).
1442  *
1443  * \par
1444  * Given that the default value for a property depends on what
1445  * property it is (e.g., whether to inherit or not), and given
1446  * the above comment about ignoring invalid values, and that the
1447  * repr parent isn't necessarily the right element to inherit
1448  * from (e.g., maybe we need to inherit from the referencing
1449  * <use> element instead), we should probably make the caller
1450  * responsible for ascending the repr tree as necessary.
1451  */
1452 gchar const *
1453 sp_object_get_style_property(SPObject const *object, gchar const *key, gchar const *def)
1455     g_return_val_if_fail(object != NULL, NULL);
1456     g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
1457     g_return_val_if_fail(key != NULL, NULL);
1459     gchar const *style = object->repr->attribute("style");
1460     if (style) {
1461         size_t const len = strlen(key);
1462         char const *p;
1463         while ( (p = strstr(style, key))
1464                 != NULL )
1465         {
1466             p += len;
1467             while ((*p <= ' ') && *p) p++;
1468             if (*p++ != ':') break;
1469             while ((*p <= ' ') && *p) p++;
1470             size_t const inherit_len = sizeof("inherit") - 1;
1471             if (*p
1472                 && !(strneq(p, "inherit", inherit_len)
1473                      && (p[inherit_len] == '\0'
1474                          || p[inherit_len] == ';'
1475                          || g_ascii_isspace(p[inherit_len])))) {
1476                 return p;
1477             }
1478         }
1479     }
1480     gchar const *val = object->repr->attribute(key);
1481     if (val && !streq(val, "inherit")) {
1482         return val;
1483     }
1484     if (object->parent) {
1485         return sp_object_get_style_property(object->parent, key, def);
1486     }
1488     return def;
1491 /**
1492  * Lifts SVG version of all root objects to version.
1493  */
1494 void
1495 SPObject::_requireSVGVersion(Inkscape::Version version) {
1496     for ( SPObject::ParentIterator iter=this ; iter ; ++iter ) {
1497         SPObject *object=iter;
1498         if (SP_IS_ROOT(object)) {
1499             SPRoot *root=SP_ROOT(object);
1500             if ( root->version.svg < version ) {
1501                 root->version.svg = version;
1502             }
1503         }
1504     }
1507 /**
1508  * Return sodipodi version of first root ancestor or (0,0).
1509  */
1510 Inkscape::Version
1511 sp_object_get_sodipodi_version(SPObject *object)
1513     static Inkscape::Version const zero_version(0, 0);
1515     while (object) {
1516         if (SP_IS_ROOT(object)) {
1517             return SP_ROOT(object)->version.sodipodi;
1518         }
1519         object = SP_OBJECT_PARENT(object);
1520     }
1522     return zero_version;
1525 /**
1526  * Returns previous object in sibling list or NULL.
1527  */
1528 SPObject *
1529 sp_object_prev(SPObject *child)
1531     SPObject *parent = SP_OBJECT_PARENT(child);
1532     for ( SPObject *i = sp_object_first_child(parent); i; i = SP_OBJECT_NEXT(i) ) {
1533         if (SP_OBJECT_NEXT(i) == child)
1534             return i;
1535     }
1536     return NULL;
1540 /*
1541   Local Variables:
1542   mode:c++
1543   c-file-style:"stroustrup"
1544   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1545   indent-tabs-mode:nil
1546   fill-column:99
1547   End:
1548 */
1549 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :