Code

Add signal for notification of object position changes
[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::shared_ptr<char> stringify(SPObject *obj) {
231     char *temp=g_strdup_printf("%p", obj);
232     Inkscape::Util::shared_ptr<char> result=Inkscape::Util::share_string(temp);
233     g_free(temp);
234     return result;
237 Inkscape::Util::shared_ptr<char> stringify(unsigned n) {
238     char *temp=g_strdup_printf("%u", n);
239     Inkscape::Util::shared_ptr<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::shared_ptr<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::shared_ptr<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     SPObject *prev=NULL;
663     for ( SPObject *child = parent->children ; child && child != object ;
664           child = child->next )
665     {
666         prev = child;
667     }
669     SPObject *next=object->next;
670     if (prev) {
671         prev->next = next;
672     } else {
673         parent->children = next;
674     }
675     if (!next) {
676         parent->_last_child = prev;
677     }
679     object->next = NULL;
680     object->parent = NULL;
682     sp_object_invoke_release(object);
683     parent->_updateTotalHRefCount(-object->_total_hrefcount);
684     sp_object_unref(object, parent);
687 /**
688  * Return object's child whose node pointer equals repr.
689  */
690 SPObject *
691 sp_object_get_child_by_repr(SPObject *object, Inkscape::XML::Node *repr)
693     g_return_val_if_fail(object != NULL, NULL);
694     g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
695     g_return_val_if_fail(repr != NULL, NULL);
697     if (object->_last_child && SP_OBJECT_REPR(object->_last_child) == repr)
698         return object->_last_child;   // optimization for common scenario
699     for ( SPObject *child = object->children ; child ; child = child->next ) {
700         if ( SP_OBJECT_REPR(child) == repr ) {
701             return child;
702         }
703     }
705     return NULL;
708 /**
709  * Callback for child_added event.
710  * Invoked whenever the given mutation event happens in the XML tree.
711  */
712 static void
713 sp_object_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
715     GType type = sp_repr_type_lookup(child);
716     if (!type) {
717         return;
718     }
719     SPObject *ochild = SP_OBJECT(g_object_new(type, 0));
720     SPObject *prev = ref ? sp_object_get_child_by_repr(object, ref) : NULL;
721     sp_object_attach(object, ochild, prev);
722     sp_object_unref(ochild, NULL);
724     sp_object_invoke_build(ochild, object->document, child, SP_OBJECT_IS_CLONED(object));
727 /**
728  * Removes, releases and unrefs all children of object.
729  *
730  * This is the opposite of build. It has to be invoked as soon as the
731  * object is removed from the tree, even if it is still alive according
732  * to reference count. The frontend unregisters the object from the
733  * document and releases the SPRepr bindings; implementations should free
734  * state data and release all child objects.  Invoking release on
735  * SPRoot destroys the whole document tree.
736  * \see sp_object_build()
737  */
738 static void sp_object_release(SPObject *object)
740     debug("id=%x, typename=%s", object, g_type_name_from_instance((GTypeInstance*)object));
741     while (object->children) {
742         sp_object_detach(object, object->children);
743     }
746 /**
747  * Remove object's child whose node equals repr, release and
748  * unref it.
749  *
750  * Invoked whenever the given mutation event happens in the XML
751  * tree, BEFORE removal from the XML tree happens, so grouping
752  * objects can safely release the child data.
753  */
754 static void
755 sp_object_remove_child(SPObject *object, Inkscape::XML::Node *child)
757     debug("id=%x, typename=%s", object, g_type_name_from_instance((GTypeInstance*)object));
758     SPObject *ochild = sp_object_get_child_by_repr(object, child);
759     g_return_if_fail(ochild != NULL);
760     sp_object_detach(object, ochild);
763 /**
764  * Move object corresponding to child after sibling object corresponding
765  * to new_ref.
766  * Invoked whenever the given mutation event happens in the XML tree.
767  * \param old_ref Ignored
768  */
769 static void sp_object_order_changed(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref,
770                                     Inkscape::XML::Node *new_ref)
772     SPObject *ochild = sp_object_get_child_by_repr(object, child);
773     g_return_if_fail(ochild != NULL);
774     SPObject *prev = new_ref ? sp_object_get_child_by_repr(object, new_ref) : NULL;
775     sp_object_reorder(ochild, prev);
776     ochild->_position_changed_signal.emit(ochild);
779 /**
780  * Virtual build callback.
781  *
782  * This has to be invoked immediately after creation of an SPObject. The
783  * frontend method ensures that the new object is properly attached to
784  * the document and repr; implementation then will parse all of the attributes,
785  * generate the children objects and so on.  Invoking build on the SPRoot
786  * object results in creation of the whole document tree (this is, what
787  * SPDocument does after the creation of the XML tree).
788  * \see sp_object_release()
789  */
790 static void
791 sp_object_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
793     /* Nothing specific here */
794     debug("id=%x, typename=%s", object, g_type_name_from_instance((GTypeInstance*)object));
796     sp_object_read_attr(object, "xml:space");
797     sp_object_read_attr(object, "inkscape:label");
798     sp_object_read_attr(object, "inkscape:collect");
800     for (Inkscape::XML::Node *rchild = repr->firstChild() ; rchild != NULL; rchild = rchild->next()) {
801         GType type = sp_repr_type_lookup(rchild);
802         if (!type) {
803             continue;
804         }
805         SPObject *child = SP_OBJECT(g_object_new(type, 0));
806         sp_object_attach(object, child, object->lastChild());
807         sp_object_unref(child, NULL);
808         sp_object_invoke_build(child, document, rchild, SP_OBJECT_IS_CLONED(object));
809     }
812 void
813 sp_object_invoke_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr, unsigned int cloned)
815     debug("id=%x, typename=%s", object, g_type_name_from_instance((GTypeInstance*)object));
817     g_assert(object != NULL);
818     g_assert(SP_IS_OBJECT(object));
819     g_assert(document != NULL);
820     g_assert(repr != NULL);
822     g_assert(object->document == NULL);
823     g_assert(object->repr == NULL);
824     g_assert(object->id == NULL);
826     /* Bookkeeping */
828     object->document = document;
829     object->repr = repr;
830     Inkscape::GC::anchor(repr);
831     object->cloned = cloned;
833     if (!SP_OBJECT_IS_CLONED(object)) {
834         object->document->bindObjectToRepr(object->repr, object);
836         if (Inkscape::XML::id_permitted(object->repr)) {
837             /* If we are not cloned, force unique id */
838             gchar const *id = object->repr->attribute("id");
839             gchar *realid = sp_object_get_unique_id(object, id);
840             g_assert(realid != NULL);
842             object->document->bindObjectToId(realid, object);
843             object->id = realid;
845             /* Redefine ID, if required */
846             if ((id == NULL) || (strcmp(id, realid) != 0)) {
847                 gboolean undo_sensitive=sp_document_get_undo_sensitive(document);
848                 sp_document_set_undo_sensitive(document, FALSE);
849                 object->repr->setAttribute("id", realid);
850                 sp_document_set_undo_sensitive(document, undo_sensitive);
851             }
852         }
853     } else {
854         g_assert(object->id == NULL);
855     }
857     /* Invoke derived methods, if any */
859     if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->build) {
860         (*((SPObjectClass *) G_OBJECT_GET_CLASS(object))->build)(object, document, repr);
861     }
863     /* Signalling (should be connected AFTER processing derived methods */
864     sp_repr_add_listener(repr, &object_event_vector, object);
867 void
868 sp_object_invoke_release(SPObject *object)
870     g_assert(object != NULL);
871     g_assert(SP_IS_OBJECT(object));
873     // we need to remember our parent
874     // g_assert(!object->parent);
875     g_assert(!object->next);
876     g_assert(object->document);
877     g_assert(object->repr);
879     sp_repr_remove_listener_by_data(object->repr, object);
881     g_signal_emit(G_OBJECT(object), object_signals[RELEASE], 0);
883     /* all hrefs should be released by the "release" handlers */
884     g_assert(object->hrefcount == 0);
886     if (!SP_OBJECT_IS_CLONED(object)) {
887         if (object->id) {
888             object->document->bindObjectToId(object->id, NULL);
889         }
890         g_free(object->id);
891         object->id = NULL;
893         g_free(object->_default_label);
894         object->_default_label = NULL;
896         object->document->bindObjectToRepr(object->repr, NULL);
897     } else {
898         g_assert(!object->id);
899     }
901     if (object->style) {
902         object->style = sp_style_unref(object->style);
903     }
905     Inkscape::GC::release(object->repr);
907     object->document = NULL;
908     object->repr = NULL;
911 /**
912  * Callback for child_added node event.
913  */
914 static void
915 sp_object_repr_child_added(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, gpointer data)
917     SPObject *object = SP_OBJECT(data);
919     if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->child_added)
920         (*((SPObjectClass *)G_OBJECT_GET_CLASS(object))->child_added)(object, child, ref);
923 /**
924  * Callback for remove_child node event.
925  */
926 static void
927 sp_object_repr_child_removed(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, gpointer data)
929     SPObject *object = SP_OBJECT(data);
931     if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->remove_child) {
932         (* ((SPObjectClass *)G_OBJECT_GET_CLASS(object))->remove_child)(object, child);
933     }
936 /**
937  * Callback for order_changed node event.
938  *
939  * \todo fixme:
940  */
941 static void
942 sp_object_repr_order_changed(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *old, Inkscape::XML::Node *newer, gpointer data)
944     SPObject *object = SP_OBJECT(data);
946     if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->order_changed) {
947         (* ((SPObjectClass *)G_OBJECT_GET_CLASS(object))->order_changed)(object, child, old, newer);
948     }
951 /**
952  * Callback for set event.
953  */
954 static void
955 sp_object_private_set(SPObject *object, unsigned int key, gchar const *value)
957     g_assert(key != SP_ATTR_INVALID);
959     switch (key) {
960         case SP_ATTR_ID:
961             if ( !SP_OBJECT_IS_CLONED(object) && object->repr->type() == Inkscape::XML::ELEMENT_NODE ) {
962                 SPDocument *document=object->document;
963                 SPObject *conflict=NULL;
965                 if (value) {
966                     conflict = document->getObjectById((char const *)value);
967                 }
968                 if ( conflict && conflict != object ) {
969                     sp_object_ref(conflict, NULL);
970                     // give the conflicting object a new ID
971                     gchar *new_conflict_id = sp_object_get_unique_id(conflict, NULL);
972                     SP_OBJECT_REPR(conflict)->setAttribute("id", new_conflict_id);
973                     g_free(new_conflict_id);
974                     sp_object_unref(conflict, NULL);
975                 }
977                 if (object->id) {
978                     document->bindObjectToId(object->id, NULL);
979                     g_free(object->id);
980                 }
982                 if (value) {
983                     object->id = g_strdup((char const*)value);
984                     document->bindObjectToId(object->id, object);
985                 } else {
986                     object->id = NULL;
987                 }
989                 g_free(object->_default_label);
990                 object->_default_label = NULL;
991             }
992             break;
993         case SP_ATTR_INKSCAPE_LABEL:
994             g_free(object->_label);
995             if (value) {
996                 object->_label = g_strdup(value);
997             } else {
998                 object->_label = NULL;
999             }
1000             g_free(object->_default_label);
1001             object->_default_label = NULL;
1002             break;
1003         case SP_ATTR_INKSCAPE_COLLECT:
1004             if ( value && !strcmp(value, "always") ) {
1005                 object->setCollectionPolicy(SPObject::ALWAYS_COLLECT);
1006             } else {
1007                 object->setCollectionPolicy(SPObject::COLLECT_WITH_PARENT);
1008             }
1009             break;
1010         case SP_ATTR_XML_SPACE:
1011             if (value && !strcmp(value, "preserve")) {
1012                 object->xml_space.value = SP_XML_SPACE_PRESERVE;
1013                 object->xml_space.set = TRUE;
1014             } else if (value && !strcmp(value, "default")) {
1015                 object->xml_space.value = SP_XML_SPACE_DEFAULT;
1016                 object->xml_space.set = TRUE;
1017             } else if (object->parent) {
1018                 SPObject *parent;
1019                 parent = object->parent;
1020                 object->xml_space.value = parent->xml_space.value;
1021             }
1022             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
1023             break;
1024         default:
1025             break;
1026     }
1029 /**
1030  * Call virtual set() function of object.
1031  */
1032 void
1033 sp_object_set(SPObject *object, unsigned int key, gchar const *value)
1035     g_assert(object != NULL);
1036     g_assert(SP_IS_OBJECT(object));
1038     if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->set) {
1039         ((SPObjectClass *) G_OBJECT_GET_CLASS(object))->set(object, key, value);
1040     }
1043 /**
1044  * Read value of key attribute from XML node into object.
1045  */
1046 void
1047 sp_object_read_attr(SPObject *object, gchar const *key)
1049     g_assert(object != NULL);
1050     g_assert(SP_IS_OBJECT(object));
1051     g_assert(key != NULL);
1053     g_assert(object->repr != NULL);
1055     unsigned int keyid = sp_attribute_lookup(key);
1056     if (keyid != SP_ATTR_INVALID) {
1057         /* Retrieve the 'key' attribute from the object's XML representation */
1058         gchar const *value = object->repr->attribute(key);
1060         sp_object_set(object, keyid, value);
1061     }
1064 /**
1065  * Callback for attr_changed node event.
1066  */
1067 static void
1068 sp_object_repr_attr_changed(Inkscape::XML::Node *repr, gchar const *key, gchar const *oldval, gchar const *newval, bool is_interactive, gpointer data)
1070     SPObject *object = SP_OBJECT(data);
1072     sp_object_read_attr(object, key);
1074     // manual changes to extension attributes require the normal
1075     // attributes, which depend on them, to be updated immediately
1076     if (is_interactive) {
1077         object->updateRepr(repr, 0);
1078     }
1081 /**
1082  * Callback for content_changed node event.
1083  */
1084 static void
1085 sp_object_repr_content_changed(Inkscape::XML::Node *repr, gchar const *oldcontent, gchar const *newcontent, gpointer data)
1087     SPObject *object = SP_OBJECT(data);
1089     if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->read_content)
1090         (*((SPObjectClass *) G_OBJECT_GET_CLASS(object))->read_content)(object);
1093 /**
1094  * Return string representation of space value.
1095  */
1096 static gchar const*
1097 sp_xml_get_space_string(unsigned int space)
1099     switch (space) {
1100         case SP_XML_SPACE_DEFAULT:
1101             return "default";
1102         case SP_XML_SPACE_PRESERVE:
1103             return "preserve";
1104         default:
1105             return NULL;
1106     }
1109 /**
1110  * Callback for write event.
1111  */
1112 static Inkscape::XML::Node *
1113 sp_object_private_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
1115     if (!repr && (flags & SP_OBJECT_WRITE_BUILD)) {
1116         repr = SP_OBJECT_REPR(object)->duplicate();
1117         if (!( flags & SP_OBJECT_WRITE_EXT )) {
1118             repr->setAttribute("inkscape:collect", NULL);
1119         }
1120     } else {
1121         repr->setAttribute("id", object->id);
1123         if (object->xml_space.set) {
1124             char const *xml_space;
1125             xml_space = sp_xml_get_space_string(object->xml_space.value);
1126             repr->setAttribute("xml:space", xml_space);
1127         }
1129         if ( flags & SP_OBJECT_WRITE_EXT &&
1130              object->collectionPolicy() == SPObject::ALWAYS_COLLECT )
1131         {
1132             repr->setAttribute("inkscape:collect", "always");
1133         } else {
1134             repr->setAttribute("inkscape:collect", NULL);
1135         }
1136     }
1138     return repr;
1141 /**
1142  * Update this object's XML node with flags value.
1143  */
1144 Inkscape::XML::Node *
1145 SPObject::updateRepr(unsigned int flags) {
1146     if (!SP_OBJECT_IS_CLONED(this)) {
1147         Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
1148         if (repr) {
1149             return updateRepr(repr, flags);
1150         } else {
1151             g_critical("Attempt to update non-existent repr");
1152             return NULL;
1153         }
1154     } else {
1155         /* cloned objects have no repr */
1156         return NULL;
1157     }
1160 Inkscape::XML::Node *
1161 SPObject::updateRepr(Inkscape::XML::Node *repr, unsigned int flags) {
1162     if (SP_OBJECT_IS_CLONED(this)) {
1163         /* cloned objects have no repr */
1164         return NULL;
1165     }
1166     if (((SPObjectClass *) G_OBJECT_GET_CLASS(this))->write) {
1167         if (!(flags & SP_OBJECT_WRITE_BUILD) && !repr) {
1168             repr = SP_OBJECT_REPR(this);
1169         }
1170         return ((SPObjectClass *) G_OBJECT_GET_CLASS(this))->write(this, repr, flags);
1171     } else {
1172         g_warning("Class %s does not implement ::write", G_OBJECT_TYPE_NAME(this));
1173         if (!repr) {
1174             if (flags & SP_OBJECT_WRITE_BUILD) {
1175                 repr = SP_OBJECT_REPR(this)->duplicate();
1176             }
1177             /// \todo fixme: else probably error (Lauris) */
1178         } else {
1179             repr->mergeFrom(SP_OBJECT_REPR(this), "id");
1180         }
1181         return repr;
1182     }
1185 /* Modification */
1187 /**
1188  * Add \a flags to \a object's as dirtiness flags, and
1189  * recursively add CHILD_MODIFIED flag to
1190  * parent and ancestors (as far up as necessary).
1191  */
1192 void
1193 SPObject::requestDisplayUpdate(unsigned int flags)
1195     if (update_in_progress) {
1196         g_print("WARNING: Requested update while update in progress, counter = %d\n", update_in_progress);
1197     }
1199     g_return_if_fail(!(flags & SP_OBJECT_PARENT_MODIFIED_FLAG));
1200     g_return_if_fail((flags & SP_OBJECT_MODIFIED_FLAG) || (flags & SP_OBJECT_CHILD_MODIFIED_FLAG));
1201     g_return_if_fail(!((flags & SP_OBJECT_MODIFIED_FLAG) && (flags & SP_OBJECT_CHILD_MODIFIED_FLAG)));
1203     /* Check for propagate before we set any flags */
1204     /* Propagate means, that this is not passed through by modification request cascade yet */
1205     unsigned int propagate = (!(this->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG)));
1207     /* Just set this flags safe even if some have been set before */
1208     this->uflags |= flags;
1210     if (propagate) {
1211         if (this->parent) {
1212             this->parent->requestDisplayUpdate(SP_OBJECT_CHILD_MODIFIED_FLAG);
1213         } else {
1214             sp_document_request_modified(this->document);
1215         }
1216     }
1219 void
1220 SPObject::updateDisplay(SPCtx *ctx, unsigned int flags)
1222     g_return_if_fail(!(flags & ~SP_OBJECT_MODIFIED_CASCADE));
1224     update_in_progress ++;
1226 #ifdef SP_OBJECT_DEBUG_CASCADE
1227     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);
1228 #endif
1230     /* Get this flags */
1231     flags |= this->uflags;
1232     /* Copy flags to modified cascade for later processing */
1233     this->mflags |= this->uflags;
1234     /* We have to clear flags here to allow rescheduling update */
1235     this->uflags = 0;
1237     // Merge style if we have good reasons to think that parent style is changed */
1238     /** \todo
1239      * I am not sure whether we should check only propagated
1240      * flag. We are currently assuming that style parsing is
1241      * done immediately. I think this is correct (Lauris).
1242      */
1243     if ((flags & SP_OBJECT_STYLE_MODIFIED_FLAG) && (flags & SP_OBJECT_PARENT_MODIFIED_FLAG)) {
1244         if (this->style && this->parent) {
1245             sp_style_merge_from_parent(this->style, this->parent->style);
1246         }
1247     }
1249     if (((SPObjectClass *) G_OBJECT_GET_CLASS(this))->update)
1250         ((SPObjectClass *) G_OBJECT_GET_CLASS(this))->update(this, ctx, flags);
1252     update_in_progress --;
1255 void
1256 SPObject::requestModified(unsigned int flags)
1258     g_return_if_fail( this->document != NULL );
1260     /* PARENT_MODIFIED is computed later on and is not intended to be
1261      * "manually" queued */
1262     g_return_if_fail(!(flags & SP_OBJECT_PARENT_MODIFIED_FLAG));
1264     /* we should be setting either MODIFIED or CHILD_MODIFIED... */
1265     g_return_if_fail((flags & SP_OBJECT_MODIFIED_FLAG) || (flags & SP_OBJECT_CHILD_MODIFIED_FLAG));
1267     /* ...but not both */
1268     g_return_if_fail(!((flags & SP_OBJECT_MODIFIED_FLAG) && (flags & SP_OBJECT_CHILD_MODIFIED_FLAG)));
1270     unsigned int old_mflags=this->mflags;
1271     this->mflags |= flags;
1273     /* If we already had MODIFIED or CHILD_MODIFIED queued, we will
1274      * have already queued CHILD_MODIFIED with our ancestors and
1275      * need not disturb them again.
1276      */
1277     if (!( old_mflags & ( SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG ) )) {
1278         SPObject *parent=SP_OBJECT_PARENT(this);
1279         if (parent) {
1280             parent->requestModified(SP_OBJECT_CHILD_MODIFIED_FLAG);
1281         } else {
1282             sp_document_request_modified(SP_OBJECT_DOCUMENT(this));
1283         }
1284     }
1287 void
1288 SPObject::emitModified(unsigned int flags)
1290     /* only the MODIFIED_CASCADE flag is legal here */
1291     g_return_if_fail(!(flags & ~SP_OBJECT_MODIFIED_CASCADE));
1293 #ifdef SP_OBJECT_DEBUG_CASCADE
1294     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);
1295 #endif
1297     flags |= this->mflags;
1298     /* We have to clear mflags beforehand, as signal handlers may
1299      * make changes and therefore queue new modification notifications
1300      * themselves. */
1301     this->mflags = 0;
1303     g_object_ref(G_OBJECT(this));
1304     g_signal_emit(G_OBJECT(this), object_signals[MODIFIED], 0, flags);
1305     g_object_unref(G_OBJECT(this));
1308 /*
1309  * Get and set descriptive parameters
1310  *
1311  * These are inefficent, so they are not intended to be used interactively
1312  */
1314 gchar const *
1315 sp_object_title_get(SPObject *object)
1317     return NULL;
1320 gchar const *
1321 sp_object_description_get(SPObject *object)
1323     return NULL;
1326 unsigned int
1327 sp_object_title_set(SPObject *object, gchar const *title)
1329     return FALSE;
1332 unsigned int
1333 sp_object_description_set(SPObject *object, gchar const *desc)
1335     return FALSE;
1338 gchar const *
1339 sp_object_tagName_get(SPObject const *object, SPException *ex)
1341     /* If exception is not clear, return */
1342     if (!SP_EXCEPTION_IS_OK(ex)) {
1343         return NULL;
1344     }
1346     /// \todo fixme: Exception if object is NULL? */
1347     return object->repr->name();
1350 gchar const *
1351 sp_object_getAttribute(SPObject const *object, gchar const *key, SPException *ex)
1353     /* If exception is not clear, return */
1354     if (!SP_EXCEPTION_IS_OK(ex)) {
1355         return NULL;
1356     }
1358     /// \todo fixme: Exception if object is NULL? */
1359     return (gchar const *) object->repr->attribute(key);
1362 void
1363 sp_object_setAttribute(SPObject *object, gchar const *key, gchar const *value, SPException *ex)
1365     /* If exception is not clear, return */
1366     g_return_if_fail(SP_EXCEPTION_IS_OK(ex));
1368     /// \todo fixme: Exception if object is NULL? */
1369     if (!sp_repr_set_attr(object->repr, key, value)) {
1370         ex->code = SP_NO_MODIFICATION_ALLOWED_ERR;
1371     }
1374 void
1375 sp_object_removeAttribute(SPObject *object, gchar const *key, SPException *ex)
1377     /* If exception is not clear, return */
1378     g_return_if_fail(SP_EXCEPTION_IS_OK(ex));
1380     /// \todo fixme: Exception if object is NULL? */
1381     if (!sp_repr_set_attr(object->repr, key, NULL)) {
1382         ex->code = SP_NO_MODIFICATION_ALLOWED_ERR;
1383     }
1386 /* Helper */
1388 static gchar *
1389 sp_object_get_unique_id(SPObject *object, gchar const *id)
1391     static unsigned long count = 0;
1393     g_assert(SP_IS_OBJECT(object));
1395     count++;
1397     gchar const *name = object->repr->name();
1398     g_assert(name != NULL);
1400     gchar const *local = strchr(name, ':');
1401     if (local) {
1402         name = local + 1;
1403     }
1405     if (id != NULL) {
1406         if (object->document->getObjectById(id) == NULL) {
1407             return g_strdup(id);
1408         }
1409     }
1411     size_t const name_len = strlen(name);
1412     size_t const buflen = name_len + (sizeof(count) * 10 / 4) + 1;
1413     gchar *const buf = (gchar *) g_malloc(buflen);
1414     memcpy(buf, name, name_len);
1415     gchar *const count_buf = buf + name_len;
1416     size_t const count_buflen = buflen - name_len;
1417     do {
1418         ++count;
1419         g_snprintf(count_buf, count_buflen, "%lu", count);
1420     } while ( object->document->getObjectById(buf) != NULL );
1421     return buf;
1424 /* Style */
1426 /**
1427  * Returns an object style property.
1428  *
1429  * \todo
1430  * fixme: Use proper CSS parsing.  The current version is buggy
1431  * in a number of situations where key is a substring of the
1432  * style string other than as a property name (including
1433  * where key is a substring of a property name), and is also
1434  * buggy in its handling of inheritance for properties that
1435  * aren't inherited by default.  It also doesn't allow for
1436  * the case where the property is specified but with an invalid
1437  * value (in which case I believe the CSS2 error-handling
1438  * behaviour applies, viz. behave as if the property hadn't
1439  * been specified).  Also, the current code doesn't use CRSelEng
1440  * stuff to take a value from stylesheets.  Also, we aren't
1441  * setting any hooks to force an update for changes in any of
1442  * the inputs (i.e., in any of the elements that this function
1443  * queries).
1444  *
1445  * \par
1446  * Given that the default value for a property depends on what
1447  * property it is (e.g., whether to inherit or not), and given
1448  * the above comment about ignoring invalid values, and that the
1449  * repr parent isn't necessarily the right element to inherit
1450  * from (e.g., maybe we need to inherit from the referencing
1451  * <use> element instead), we should probably make the caller
1452  * responsible for ascending the repr tree as necessary.
1453  */
1454 gchar const *
1455 sp_object_get_style_property(SPObject const *object, gchar const *key, gchar const *def)
1457     g_return_val_if_fail(object != NULL, NULL);
1458     g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
1459     g_return_val_if_fail(key != NULL, NULL);
1461     gchar const *style = object->repr->attribute("style");
1462     if (style) {
1463         size_t const len = strlen(key);
1464         char const *p;
1465         while ( (p = strstr(style, key))
1466                 != NULL )
1467         {
1468             p += len;
1469             while ((*p <= ' ') && *p) p++;
1470             if (*p++ != ':') break;
1471             while ((*p <= ' ') && *p) p++;
1472             size_t const inherit_len = sizeof("inherit") - 1;
1473             if (*p
1474                 && !(strneq(p, "inherit", inherit_len)
1475                      && (p[inherit_len] == '\0'
1476                          || p[inherit_len] == ';'
1477                          || g_ascii_isspace(p[inherit_len])))) {
1478                 return p;
1479             }
1480         }
1481     }
1482     gchar const *val = object->repr->attribute(key);
1483     if (val && !streq(val, "inherit")) {
1484         return val;
1485     }
1486     if (object->parent) {
1487         return sp_object_get_style_property(object->parent, key, def);
1488     }
1490     return def;
1493 /**
1494  * Lifts SVG version of all root objects to version.
1495  */
1496 void
1497 SPObject::_requireSVGVersion(Inkscape::Version version) {
1498     for ( SPObject::ParentIterator iter=this ; iter ; ++iter ) {
1499         SPObject *object=iter;
1500         if (SP_IS_ROOT(object)) {
1501             SPRoot *root=SP_ROOT(object);
1502             if ( root->version.svg < version ) {
1503                 root->version.svg = version;
1504             }
1505         }
1506     }
1509 /**
1510  * Return sodipodi version of first root ancestor or (0,0).
1511  */
1512 Inkscape::Version
1513 sp_object_get_sodipodi_version(SPObject *object)
1515     static Inkscape::Version const zero_version(0, 0);
1517     while (object) {
1518         if (SP_IS_ROOT(object)) {
1519             return SP_ROOT(object)->version.sodipodi;
1520         }
1521         object = SP_OBJECT_PARENT(object);
1522     }
1524     return zero_version;
1527 /**
1528  * Returns previous object in sibling list or NULL.
1529  */
1530 SPObject *
1531 sp_object_prev(SPObject *child)
1533     SPObject *parent = SP_OBJECT_PARENT(child);
1534     for ( SPObject *i = sp_object_first_child(parent); i; i = SP_OBJECT_NEXT(i) ) {
1535         if (SP_OBJECT_NEXT(i) == child)
1536             return i;
1537     }
1538     return NULL;
1542 /*
1543   Local Variables:
1544   mode:c++
1545   c-file-style:"stroustrup"
1546   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1547   indent-tabs-mode:nil
1548   fill-column:99
1549   End:
1550 */
1551 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :