Code

move style reading/writing from SPItem up to SPObject
[inkscape.git] / src / sp-object.cpp
1 #define __SP_OBJECT_C__
2 /** \file
3  * SPObject implementation.
4  *
5  * Authors:
6  *   Lauris Kaplinski <lauris@kaplinski.com>
7  *   bulia byak <buliabyak@users.sf.net>
8  *
9  * Copyright (C) 1999-2005 authors
10  * Copyright (C) 2001-2002 Ximian, Inc.
11  *
12  * Released under GNU GPL, read the file 'COPYING' for more information
13  */
15 /** \class SPObject
16  *
17  * SPObject is an abstract base class of all of the document nodes at the
18  * SVG document level. Each SPObject subclass implements a certain SVG
19  * element node type, or is an abstract base class for different node
20  * types.  The SPObject layer is bound to the SPRepr layer, closely
21  * following the SPRepr mutations via callbacks.  During creation,
22  * SPObject parses and interprets all textual attributes and CSS style
23  * strings of the SPRepr, and later updates the internal state whenever
24  * it receives a signal about a change. The opposite is not true - there
25  * are methods manipulating SPObjects directly and such changes do not
26  * propagate to the SPRepr layer. This is important for implementation of
27  * the undo stack, animations and other features.
28  *
29  * SPObjects are bound to the higher-level container SPDocument, which
30  * provides document level functionality such as the undo stack,
31  * dictionary and so on. Source: doc/architecture.txt
32  */
35 #include "helper/sp-marshal.h"
36 #include "xml/node-event-vector.h"
37 #include "attributes.h"
38 #include "document.h"
39 #include "style.h"
40 #include "sp-object-repr.h"
41 #include "sp-root.h"
42 #include "streq.h"
43 #include "strneq.h"
44 #include "xml/repr.h"
45 #include "xml/node-fns.h"
46 #include "debug/event-tracker.h"
47 #include "debug/simple-event.h"
48 #include "debug/demangle.h"
49 #include "util/share.h"
50 #include "util/format.h"
52 #include "algorithms/longest-common-suffix.h"
53 using std::memcpy;
54 using std::strchr;
55 using std::strcmp;
56 using std::strlen;
57 using std::strstr;
59 #define noSP_OBJECT_DEBUG_CASCADE
61 #define noSP_OBJECT_DEBUG
63 #ifdef SP_OBJECT_DEBUG
64 # define debug(f, a...) { g_print("%s(%d) %s:", \
65                                   __FILE__,__LINE__,__FUNCTION__); \
66                           g_print(f, ## a); \
67                           g_print("\n"); \
68                         }
69 #else
70 # define debug(f, a...) /**/
71 #endif
73 static void sp_object_class_init(SPObjectClass *klass);
74 static void sp_object_init(SPObject *object);
75 static void sp_object_finalize(GObject *object);
77 static void sp_object_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref);
78 static void sp_object_remove_child(SPObject *object, Inkscape::XML::Node *child);
79 static void sp_object_order_changed(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref, Inkscape::XML::Node *new_ref);
81 static void sp_object_release(SPObject *object);
82 static void sp_object_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
84 static void sp_object_private_set(SPObject *object, unsigned int key, gchar const *value);
85 static Inkscape::XML::Node *sp_object_private_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
87 /* Real handlers of repr signals */
89 static void sp_object_repr_attr_changed(Inkscape::XML::Node *repr, gchar const *key, gchar const *oldval, gchar const *newval, bool is_interactive, gpointer data);
91 static void sp_object_repr_content_changed(Inkscape::XML::Node *repr, gchar const *oldcontent, gchar const *newcontent, gpointer data);
93 static void sp_object_repr_child_added(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, gpointer data);
94 static void sp_object_repr_child_removed(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, void *data);
96 static void sp_object_repr_order_changed(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *old, Inkscape::XML::Node *newer, gpointer data);
98 static gchar *sp_object_get_unique_id(SPObject *object, gchar const *defid);
100 guint update_in_progress = 0; // guard against update-during-update
102 Inkscape::XML::NodeEventVector object_event_vector = {
103     sp_object_repr_child_added,
104     sp_object_repr_child_removed,
105     sp_object_repr_attr_changed,
106     sp_object_repr_content_changed,
107     sp_object_repr_order_changed
108 };
110 static GObjectClass *parent_class;
112 /**
113  * Registers the SPObject class with Gdk and returns its type number.
114  */
115 GType
116 sp_object_get_type(void)
118     static GType type = 0;
119     if (!type) {
120         GTypeInfo info = {
121             sizeof(SPObjectClass),
122             NULL, NULL,
123             (GClassInitFunc) sp_object_class_init,
124             NULL, NULL,
125             sizeof(SPObject),
126             16,
127             (GInstanceInitFunc) sp_object_init,
128             NULL
129         };
130         type = g_type_register_static(G_TYPE_OBJECT, "SPObject", &info, (GTypeFlags)0);
131     }
132     return type;
135 /**
136  * Initializes the SPObject vtable.
137  */
138 static void
139 sp_object_class_init(SPObjectClass *klass)
141     GObjectClass *object_class;
143     object_class = (GObjectClass *) klass;
145     parent_class = (GObjectClass *) g_type_class_ref(G_TYPE_OBJECT);
147     object_class->finalize = sp_object_finalize;
149     klass->child_added = sp_object_child_added;
150     klass->remove_child = sp_object_remove_child;
151     klass->order_changed = sp_object_order_changed;
153     klass->release = sp_object_release;
155     klass->build = sp_object_build;
157     klass->set = sp_object_private_set;
158     klass->write = sp_object_private_write;
161 /**
162  * Callback to initialize the SPObject object.
163  */
164 static void
165 sp_object_init(SPObject *object)
167     debug("id=%x, typename=%s",object, g_type_name_from_instance((GTypeInstance*)object));
169     object->hrefcount = 0;
170     object->_total_hrefcount = 0;
171     object->document = NULL;
172     object->children = object->_last_child = NULL;
173     object->parent = object->next = NULL;
174     object->repr = NULL;
175     object->id = NULL;
177     object->style = sp_style_new_from_object(object);
179     object->_collection_policy = SPObject::COLLECT_WITH_PARENT;
181     new (&object->_release_signal) sigc::signal<void, SPObject *>();
182     new (&object->_modified_signal) sigc::signal<void, SPObject *, unsigned int>();
183     new (&object->_delete_signal) sigc::signal<void, SPObject *>();
184     new (&object->_position_changed_signal) sigc::signal<void, SPObject *>();
185     object->_successor = NULL;
187     object->_label = NULL;
188     object->_default_label = NULL;
191 /**
192  * Callback to destroy all members and connections of object and itself.
193  */
194 static void
195 sp_object_finalize(GObject *object)
197     SPObject *spobject = (SPObject *)object;
199     g_free(spobject->_label);
200     g_free(spobject->_default_label);
201     spobject->_label = NULL;
202     spobject->_default_label = NULL;
204     if (spobject->_successor) {
205         sp_object_unref(spobject->_successor, NULL);
206         spobject->_successor = NULL;
207     }
209     if (((GObjectClass *) (parent_class))->finalize) {
210         (* ((GObjectClass *) (parent_class))->finalize)(object);
211     }
213     spobject->_release_signal.~signal();
214     spobject->_modified_signal.~signal();
215     spobject->_delete_signal.~signal();
216     spobject->_position_changed_signal.~signal();
219 namespace {
221 namespace Debug = Inkscape::Debug;
222 namespace Util = Inkscape::Util;
224 typedef Debug::SimpleEvent<Debug::Event::REFCOUNT> BaseRefCountEvent;
226 class RefCountEvent : public BaseRefCountEvent {
227 public:
228     RefCountEvent(SPObject *object, int bias, Util::ptr_shared<char> name)
229     : BaseRefCountEvent(name)
230     {
231         _addProperty("object", Util::format("%p", object));
232         _addProperty("class", Debug::demangle(g_type_name(G_TYPE_FROM_INSTANCE(object))));
233         _addProperty("new-refcount", Util::format("%d", G_OBJECT(object)->ref_count + bias));
234     }
235 };
237 class RefEvent : public RefCountEvent {
238 public:
239     RefEvent(SPObject *object)
240     : RefCountEvent(object, 1, Util::share_static_string("sp-object-ref"))
241     {}
242 };
244 class UnrefEvent : public RefCountEvent {
245 public:
246     UnrefEvent(SPObject *object)
247     : RefCountEvent(object, -1, Util::share_static_string("sp-object-unref"))
248     {}
249 };
253 /**
254  * Increase reference count of object, with possible debugging.
255  *
256  * \param owner If non-NULL, make debug log entry.
257  * \return object, NULL is error.
258  * \pre object points to real object
259  */
260 SPObject *
261 sp_object_ref(SPObject *object, SPObject *owner)
263     g_return_val_if_fail(object != NULL, NULL);
264     g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
265     g_return_val_if_fail(!owner || SP_IS_OBJECT(owner), NULL);
267     Inkscape::Debug::EventTracker<RefEvent> tracker(object);
268     g_object_ref(G_OBJECT(object));
269     return object;
272 /**
273  * Decrease reference count of object, with possible debugging and
274  * finalization.
275  *
276  * \param owner If non-NULL, make debug log entry.
277  * \return always NULL
278  * \pre object points to real object
279  */
280 SPObject *
281 sp_object_unref(SPObject *object, SPObject *owner)
283     g_return_val_if_fail(object != NULL, NULL);
284     g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
285     g_return_val_if_fail(!owner || SP_IS_OBJECT(owner), NULL);
287     Inkscape::Debug::EventTracker<UnrefEvent> tracker(object);
288     g_object_unref(G_OBJECT(object));
289     return NULL;
292 /**
293  * Increase weak refcount.
294  *
295  * Hrefcount is used for weak references, for example, to
296  * determine whether any graphical element references a certain gradient
297  * node.
298  * \param owner Ignored.
299  * \return object, NULL is error
300  * \pre object points to real object
301  */
302 SPObject *
303 sp_object_href(SPObject *object, gpointer owner)
305     g_return_val_if_fail(object != NULL, NULL);
306     g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
308     object->hrefcount++;
309     object->_updateTotalHRefCount(1);
311     return object;
314 /**
315  * Decrease weak refcount.
316  *
317  * Hrefcount is used for weak references, for example, to determine whether
318  * any graphical element references a certain gradient node.
319  * \param owner Ignored.
320  * \return always NULL
321  * \pre object points to real object and hrefcount>0
322  */
323 SPObject *
324 sp_object_hunref(SPObject *object, gpointer owner)
326     g_return_val_if_fail(object != NULL, NULL);
327     g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
328     g_return_val_if_fail(object->hrefcount > 0, NULL);
330     object->hrefcount--;
331     object->_updateTotalHRefCount(-1);
333     return NULL;
336 /**
337  * Adds increment to _total_hrefcount of object and its parents.
338  */
339 void
340 SPObject::_updateTotalHRefCount(int increment) {
341     SPObject *topmost_collectable = NULL;
342     for ( SPObject *iter = this ; iter ; iter = SP_OBJECT_PARENT(iter) ) {
343         iter->_total_hrefcount += increment;
344         if ( iter->_total_hrefcount < iter->hrefcount ) {
345             g_critical("HRefs overcounted");
346         }
347         if ( iter->_total_hrefcount == 0 &&
348              iter->_collection_policy != COLLECT_WITH_PARENT )
349         {
350             topmost_collectable = iter;
351         }
352     }
353     if (topmost_collectable) {
354         topmost_collectable->requestOrphanCollection();
355     }
358 /**
359  * True if object is non-NULL and this is some in/direct parent of object.
360  */
361 bool
362 SPObject::isAncestorOf(SPObject const *object) const {
363     g_return_val_if_fail(object != NULL, false);
364     object = SP_OBJECT_PARENT(object);
365     while (object) {
366         if ( object == this ) {
367             return true;
368         }
369         object = SP_OBJECT_PARENT(object);
370     }
371     return false;
374 namespace {
376 bool same_objects(SPObject const &a, SPObject const &b) {
377     return &a == &b;
382 /**
383  * Returns youngest object being parent to this and object.
384  */
385 SPObject const *
386 SPObject::nearestCommonAncestor(SPObject const *object) const {
387     g_return_val_if_fail(object != NULL, NULL);
389     using Inkscape::Algorithms::longest_common_suffix;
390     return longest_common_suffix<SPObject::ConstParentIterator>(this, object, NULL, &same_objects);
393 SPObject const *AncestorSon(SPObject const *obj, SPObject const *ancestor) {
394     if (obj == NULL || ancestor == NULL)
395         return NULL;
396     if (SP_OBJECT_PARENT(obj) == ancestor)
397         return obj;
398     return AncestorSon(SP_OBJECT_PARENT(obj), ancestor);
401 /**
402  * Compares height of objects in tree.
403  *
404  * Works for different-parent objects, so long as they have a common ancestor.
405  * \return \verbatim
406  *    0    positions are equivalent
407  *    1    first object's position is greater than the second
408  *   -1    first object's position is less than the second   \endverbatim
409  */
410 int
411 sp_object_compare_position(SPObject const *first, SPObject const *second)
413     if (first == second) return 0;
415     SPObject const *ancestor = first->nearestCommonAncestor(second);
416     if (ancestor == NULL) return 0; // cannot compare, no common ancestor!
418     // we have an object and its ancestor (should not happen when sorting selection)
419     if (ancestor == first)
420         return 1;
421     if (ancestor == second)
422         return -1;
424     SPObject const *to_first = AncestorSon(first, ancestor);
425     SPObject const *to_second = AncestorSon(second, ancestor);
427     g_assert(SP_OBJECT_PARENT(to_second) == SP_OBJECT_PARENT(to_first));
429     return sp_repr_compare_position(SP_OBJECT_REPR(to_first), SP_OBJECT_REPR(to_second));
433 /**
434  * Append repr as child of this object.
435  * \pre this is not a cloned object
436  */
437 SPObject *
438 SPObject::appendChildRepr(Inkscape::XML::Node *repr) {
439     if (!SP_OBJECT_IS_CLONED(this)) {
440         SP_OBJECT_REPR(this)->appendChild(repr);
441         return SP_OBJECT_DOCUMENT(this)->getObjectByRepr(repr);
442     } else {
443         g_critical("Attempt to append repr as child of cloned object");
444         return NULL;
445     }
448 /**
449  * Retrieves the children as a GSList object, optionally ref'ing the children
450  * in the process, if add_ref is specified.
451  */
452 GSList *SPObject::childList(bool add_ref, Action) {
453     GSList *l = NULL;
454     for (SPObject *child = sp_object_first_child(this) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
455         if (add_ref)
456             g_object_ref (G_OBJECT (child));
458         l = g_slist_prepend (l, child);
459     }
460     return l;
464 /** Gets the label property for the object or a default if no label
465  *  is defined.
466  */
467 gchar const *
468 SPObject::label() const {
469     return _label;
472 /** Returns a default label property for the object. */
473 gchar const *
474 SPObject::defaultLabel() const {
475     if (_label) {
476         return _label;
477     } else {
478         if (!_default_label) {
479             gchar const *id=SP_OBJECT_ID(this);
480             if (id) {
481                 _default_label = g_strdup_printf("#%s", id);
482             } else {
483                 _default_label = g_strdup_printf("<%s>", SP_OBJECT_REPR(this)->name());
484             }
485         }
486         return _default_label;
487     }
490 /** Sets the label property for the object */
491 void
492 SPObject::setLabel(gchar const *label) {
493     SP_OBJECT_REPR(this)->setAttribute("inkscape:label", label, false);
497 /** Queues the object for orphan collection */
498 void
499 SPObject::requestOrphanCollection() {
500     g_return_if_fail(document != NULL);
501     document->queueForOrphanCollection(this);
503     /** \todo
504      * This is a temporary hack added to make fill&stroke rebuild its
505      * gradient list when the defs are vacuumed.  gradient-vector.cpp
506      * listens to the modified signal on defs, and now we give it that
507      * signal.  Mental says that this should be made automatic by
508      * merging SPObjectGroup with SPObject; SPObjectGroup would issue
509      * this signal automatically. Or maybe just derive SPDefs from
510      * SPObjectGroup?
511      */
513     this->requestModified(SP_OBJECT_CHILD_MODIFIED_FLAG);
516 /** Sends the delete signal to all children of this object recursively */
517 void
518 SPObject::_sendDeleteSignalRecursive() {
519     for (SPObject *child = sp_object_first_child(this); child; child = SP_OBJECT_NEXT(child)) {
520         child->_delete_signal.emit(child);
521         child->_sendDeleteSignalRecursive();
522     }
525 /**
526  * Deletes the object reference, unparenting it from its parent.
527  *
528  * If the \a propagate parameter is set to true, it emits a delete
529  * signal.  If the \a propagate_descendants parameter is true, it
530  * recursively sends the delete signal to children.
531  */
532 void
533 SPObject::deleteObject(bool propagate, bool propagate_descendants)
535     sp_object_ref(this, NULL);
536     if (propagate) {
537         _delete_signal.emit(this);
538     }
539     if (propagate_descendants) {
540         this->_sendDeleteSignalRecursive();
541     }
543     Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
544     if (repr && sp_repr_parent(repr)) {
545         sp_repr_unparent(repr);
546     }
548     if (_successor) {
549         _successor->deleteObject(propagate, propagate_descendants);
550     }
551     sp_object_unref(this, NULL);
554 /**
555  * Put object into object tree, under parent, and behind prev;
556  * also update object's XML space.
557  */
558 void
559 sp_object_attach(SPObject *parent, SPObject *object, SPObject *prev)
561     g_return_if_fail(parent != NULL);
562     g_return_if_fail(SP_IS_OBJECT(parent));
563     g_return_if_fail(object != NULL);
564     g_return_if_fail(SP_IS_OBJECT(object));
565     g_return_if_fail(!prev || SP_IS_OBJECT(prev));
566     g_return_if_fail(!prev || prev->parent == parent);
567     g_return_if_fail(!object->parent);
569     sp_object_ref(object, parent);
570     object->parent = parent;
571     parent->_updateTotalHRefCount(object->_total_hrefcount);
573     SPObject *next;
574     if (prev) {
575         next = prev->next;
576         prev->next = object;
577     } else {
578         next = parent->children;
579         parent->children = object;
580     }
581     object->next = next;
582     if (!next) {
583         parent->_last_child = object;
584     }
585     if (!object->xml_space.set)
586         object->xml_space.value = parent->xml_space.value;
589 /**
590  * In list of object's siblings, move object behind prev.
591  */
592 void
593 sp_object_reorder(SPObject *object, SPObject *prev) {
594     g_return_if_fail(object != NULL);
595     g_return_if_fail(SP_IS_OBJECT(object));
596     g_return_if_fail(object->parent != NULL);
597     g_return_if_fail(object != prev);
598     g_return_if_fail(!prev || SP_IS_OBJECT(prev));
599     g_return_if_fail(!prev || prev->parent == object->parent);
601     SPObject *const parent=object->parent;
603     SPObject *old_prev=NULL;
604     for ( SPObject *child = parent->children ; child && child != object ;
605           child = child->next )
606     {
607         old_prev = child;
608     }
610     SPObject *next=object->next;
611     if (old_prev) {
612         old_prev->next = next;
613     } else {
614         parent->children = next;
615     }
616     if (!next) {
617         parent->_last_child = old_prev;
618     }
619     if (prev) {
620         next = prev->next;
621         prev->next = object;
622     } else {
623         next = parent->children;
624         parent->children = object;
625     }
626     object->next = next;
627     if (!next) {
628         parent->_last_child = object;
629     }
632 /**
633  * Remove object from parent's children, release and unref it.
634  */
635 void
636 sp_object_detach(SPObject *parent, SPObject *object) {
637     g_return_if_fail(parent != NULL);
638     g_return_if_fail(SP_IS_OBJECT(parent));
639     g_return_if_fail(object != NULL);
640     g_return_if_fail(SP_IS_OBJECT(object));
641     g_return_if_fail(object->parent == parent);
643     object->releaseReferences();
645     SPObject *prev=NULL;
646     for ( SPObject *child = parent->children ; child && child != object ;
647           child = child->next )
648     {
649         prev = child;
650     }
652     SPObject *next=object->next;
653     if (prev) {
654         prev->next = next;
655     } else {
656         parent->children = next;
657     }
658     if (!next) {
659         parent->_last_child = prev;
660     }
662     object->next = NULL;
663     object->parent = NULL;
665     parent->_updateTotalHRefCount(-object->_total_hrefcount);
666     sp_object_unref(object, parent);
669 /**
670  * Return object's child whose node pointer equals repr.
671  */
672 SPObject *
673 sp_object_get_child_by_repr(SPObject *object, Inkscape::XML::Node *repr)
675     g_return_val_if_fail(object != NULL, NULL);
676     g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
677     g_return_val_if_fail(repr != NULL, NULL);
679     if (object->_last_child && SP_OBJECT_REPR(object->_last_child) == repr)
680         return object->_last_child;   // optimization for common scenario
681     for ( SPObject *child = object->children ; child ; child = child->next ) {
682         if ( SP_OBJECT_REPR(child) == repr ) {
683             return child;
684         }
685     }
687     return NULL;
690 /**
691  * Callback for child_added event.
692  * Invoked whenever the given mutation event happens in the XML tree.
693  */
694 static void
695 sp_object_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
697     GType type = sp_repr_type_lookup(child);
698     if (!type) {
699         return;
700     }
701     SPObject *ochild = SP_OBJECT(g_object_new(type, 0));
702     SPObject *prev = ref ? sp_object_get_child_by_repr(object, ref) : NULL;
703     sp_object_attach(object, ochild, prev);
704     sp_object_unref(ochild, NULL);
706     sp_object_invoke_build(ochild, object->document, child, SP_OBJECT_IS_CLONED(object));
709 /**
710  * Removes, releases and unrefs all children of object.
711  *
712  * This is the opposite of build. It has to be invoked as soon as the
713  * object is removed from the tree, even if it is still alive according
714  * to reference count. The frontend unregisters the object from the
715  * document and releases the SPRepr bindings; implementations should free
716  * state data and release all child objects.  Invoking release on
717  * SPRoot destroys the whole document tree.
718  * \see sp_object_build()
719  */
720 static void sp_object_release(SPObject *object)
722     debug("id=%x, typename=%s", object, g_type_name_from_instance((GTypeInstance*)object));
723     while (object->children) {
724         sp_object_detach(object, object->children);
725     }
728 /**
729  * Remove object's child whose node equals repr, release and
730  * unref it.
731  *
732  * Invoked whenever the given mutation event happens in the XML
733  * tree, BEFORE removal from the XML tree happens, so grouping
734  * objects can safely release the child data.
735  */
736 static void
737 sp_object_remove_child(SPObject *object, Inkscape::XML::Node *child)
739     debug("id=%x, typename=%s", object, g_type_name_from_instance((GTypeInstance*)object));
740     SPObject *ochild = sp_object_get_child_by_repr(object, child);
741     g_return_if_fail (ochild != NULL || !strcmp("comment", child->name())); // comments have no objects
742     if (ochild)
743         sp_object_detach(object, ochild);
746 /**
747  * Move object corresponding to child after sibling object corresponding
748  * to new_ref.
749  * Invoked whenever the given mutation event happens in the XML tree.
750  * \param old_ref Ignored
751  */
752 static void sp_object_order_changed(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref,
753                                     Inkscape::XML::Node *new_ref)
755     SPObject *ochild = sp_object_get_child_by_repr(object, child);
756     g_return_if_fail(ochild != NULL);
757     SPObject *prev = new_ref ? sp_object_get_child_by_repr(object, new_ref) : NULL;
758     sp_object_reorder(ochild, prev);
759     ochild->_position_changed_signal.emit(ochild);
762 /**
763  * Virtual build callback.
764  *
765  * This has to be invoked immediately after creation of an SPObject. The
766  * frontend method ensures that the new object is properly attached to
767  * the document and repr; implementation then will parse all of the attributes,
768  * generate the children objects and so on.  Invoking build on the SPRoot
769  * object results in creation of the whole document tree (this is, what
770  * SPDocument does after the creation of the XML tree).
771  * \see sp_object_release()
772  */
773 static void
774 sp_object_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
776     /* Nothing specific here */
777     debug("id=%x, typename=%s", object, g_type_name_from_instance((GTypeInstance*)object));
779     sp_object_read_attr(object, "xml:space");
780     sp_object_read_attr(object, "inkscape:label");
781     sp_object_read_attr(object, "inkscape:collect");
783     for (Inkscape::XML::Node *rchild = repr->firstChild() ; rchild != NULL; rchild = rchild->next()) {
784         GType type = sp_repr_type_lookup(rchild);
785         if (!type) {
786             continue;
787         }
788         SPObject *child = SP_OBJECT(g_object_new(type, 0));
789         sp_object_attach(object, child, object->lastChild());
790         sp_object_unref(child, NULL);
791         sp_object_invoke_build(child, document, rchild, SP_OBJECT_IS_CLONED(object));
792     }
795 void
796 sp_object_invoke_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr, unsigned int cloned)
798     debug("id=%x, typename=%s", object, g_type_name_from_instance((GTypeInstance*)object));
800     g_assert(object != NULL);
801     g_assert(SP_IS_OBJECT(object));
802     g_assert(document != NULL);
803     g_assert(repr != NULL);
805     g_assert(object->document == NULL);
806     g_assert(object->repr == NULL);
807     g_assert(object->id == NULL);
809     /* Bookkeeping */
811     object->document = document;
812     object->repr = repr;
813     Inkscape::GC::anchor(repr);
814     object->cloned = cloned;
816     if (!SP_OBJECT_IS_CLONED(object)) {
817         object->document->bindObjectToRepr(object->repr, object);
819         if (Inkscape::XML::id_permitted(object->repr)) {
820             /* If we are not cloned, force unique id */
821             gchar const *id = object->repr->attribute("id");
822             gchar *realid = sp_object_get_unique_id(object, id);
823             g_assert(realid != NULL);
825             object->document->bindObjectToId(realid, object);
826             object->id = realid;
828             /* Redefine ID, if required */
829             if ((id == NULL) || (strcmp(id, realid) != 0)) {
830                 bool saved = sp_document_get_undo_sensitive(document);
831                 sp_document_set_undo_sensitive(document, false);
832                 object->repr->setAttribute("id", realid);
833                 sp_document_set_undo_sensitive(document, saved);
834             }
835         }
836     } else {
837         g_assert(object->id == NULL);
838     }
840     /* Invoke derived methods, if any */
842     if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->build) {
843         (*((SPObjectClass *) G_OBJECT_GET_CLASS(object))->build)(object, document, repr);
844     }
846     /* Signalling (should be connected AFTER processing derived methods */
847     sp_repr_add_listener(repr, &object_event_vector, object);
850 void SPObject::releaseReferences() {
851     g_assert(this->document);
852     g_assert(this->repr);
854     sp_repr_remove_listener_by_data(this->repr, this);
856     this->_release_signal.emit(this);
857     SPObjectClass *klass=(SPObjectClass *)G_OBJECT_GET_CLASS(this);
858     if (klass->release) {
859         klass->release(this);
860     }
862     /* all hrefs should be released by the "release" handlers */
863     g_assert(this->hrefcount == 0);
865     if (!SP_OBJECT_IS_CLONED(this)) {
866         if (this->id) {
867             this->document->bindObjectToId(this->id, NULL);
868         }
869         g_free(this->id);
870         this->id = NULL;
872         g_free(this->_default_label);
873         this->_default_label = NULL;
875         this->document->bindObjectToRepr(this->repr, NULL);
876     } else {
877         g_assert(!this->id);
878     }
880     if (this->style) {
881         this->style = sp_style_unref(this->style);
882     }
884     Inkscape::GC::release(this->repr);
886     this->document = NULL;
887     this->repr = NULL;
890 /**
891  * Callback for child_added node event.
892  */
893 static void
894 sp_object_repr_child_added(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, gpointer data)
896     SPObject *object = SP_OBJECT(data);
898     if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->child_added)
899         (*((SPObjectClass *)G_OBJECT_GET_CLASS(object))->child_added)(object, child, ref);
902 /**
903  * Callback for remove_child node event.
904  */
905 static void
906 sp_object_repr_child_removed(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, gpointer data)
908     SPObject *object = SP_OBJECT(data);
910     if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->remove_child) {
911         (* ((SPObjectClass *)G_OBJECT_GET_CLASS(object))->remove_child)(object, child);
912     }
915 /**
916  * Callback for order_changed node event.
917  *
918  * \todo fixme:
919  */
920 static void
921 sp_object_repr_order_changed(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *old, Inkscape::XML::Node *newer, gpointer data)
923     SPObject *object = SP_OBJECT(data);
925     if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->order_changed) {
926         (* ((SPObjectClass *)G_OBJECT_GET_CLASS(object))->order_changed)(object, child, old, newer);
927     }
930 /**
931  * Callback for set event.
932  */
933 static void
934 sp_object_private_set(SPObject *object, unsigned int key, gchar const *value)
936     g_assert(key != SP_ATTR_INVALID);
938     switch (key) {
939         case SP_ATTR_ID:
940             if ( !SP_OBJECT_IS_CLONED(object) && object->repr->type() == Inkscape::XML::ELEMENT_NODE ) {
941                 SPDocument *document=object->document;
942                 SPObject *conflict=NULL;
944                 if (value) {
945                     conflict = document->getObjectById((char const *)value);
946                 }
947                 if ( conflict && conflict != object ) {
948                     sp_object_ref(conflict, NULL);
949                     // give the conflicting object a new ID
950                     gchar *new_conflict_id = sp_object_get_unique_id(conflict, NULL);
951                     SP_OBJECT_REPR(conflict)->setAttribute("id", new_conflict_id);
952                     g_free(new_conflict_id);
953                     sp_object_unref(conflict, NULL);
954                 }
956                 if (object->id) {
957                     document->bindObjectToId(object->id, NULL);
958                     g_free(object->id);
959                 }
961                 if (value) {
962                     object->id = g_strdup((char const*)value);
963                     document->bindObjectToId(object->id, object);
964                 } else {
965                     object->id = NULL;
966                 }
968                 g_free(object->_default_label);
969                 object->_default_label = NULL;
970             }
971             break;
972         case SP_ATTR_INKSCAPE_LABEL:
973             g_free(object->_label);
974             if (value) {
975                 object->_label = g_strdup(value);
976             } else {
977                 object->_label = NULL;
978             }
979             g_free(object->_default_label);
980             object->_default_label = NULL;
981             break;
982         case SP_ATTR_INKSCAPE_COLLECT:
983             if ( value && !strcmp(value, "always") ) {
984                 object->setCollectionPolicy(SPObject::ALWAYS_COLLECT);
985             } else {
986                 object->setCollectionPolicy(SPObject::COLLECT_WITH_PARENT);
987             }
988             break;
989         case SP_ATTR_XML_SPACE:
990             if (value && !strcmp(value, "preserve")) {
991                 object->xml_space.value = SP_XML_SPACE_PRESERVE;
992                 object->xml_space.set = TRUE;
993             } else if (value && !strcmp(value, "default")) {
994                 object->xml_space.value = SP_XML_SPACE_DEFAULT;
995                 object->xml_space.set = TRUE;
996             } else if (object->parent) {
997                 SPObject *parent;
998                 parent = object->parent;
999                 object->xml_space.value = parent->xml_space.value;
1000             }
1001             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
1002             break;
1003         case SP_ATTR_STYLE:
1004             sp_style_read_from_object(object->style, object);
1005             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
1006             break;
1007         default:
1008             break;
1009     }
1012 /**
1013  * Call virtual set() function of object.
1014  */
1015 void
1016 sp_object_set(SPObject *object, unsigned int key, gchar const *value)
1018     g_assert(object != NULL);
1019     g_assert(SP_IS_OBJECT(object));
1021     if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->set) {
1022         ((SPObjectClass *) G_OBJECT_GET_CLASS(object))->set(object, key, value);
1023     }
1026 /**
1027  * Read value of key attribute from XML node into object.
1028  */
1029 void
1030 sp_object_read_attr(SPObject *object, gchar const *key)
1032     g_assert(object != NULL);
1033     g_assert(SP_IS_OBJECT(object));
1034     g_assert(key != NULL);
1036     g_assert(object->repr != NULL);
1038     unsigned int keyid = sp_attribute_lookup(key);
1039     if (keyid != SP_ATTR_INVALID) {
1040         /* Retrieve the 'key' attribute from the object's XML representation */
1041         gchar const *value = object->repr->attribute(key);
1043         sp_object_set(object, keyid, value);
1044     }
1047 /**
1048  * Callback for attr_changed node event.
1049  */
1050 static void
1051 sp_object_repr_attr_changed(Inkscape::XML::Node *repr, gchar const *key, gchar const *oldval, gchar const *newval, bool is_interactive, gpointer data)
1053     SPObject *object = SP_OBJECT(data);
1055     sp_object_read_attr(object, key);
1057     // manual changes to extension attributes require the normal
1058     // attributes, which depend on them, to be updated immediately
1059     if (is_interactive) {
1060         object->updateRepr(repr, 0);
1061     }
1064 /**
1065  * Callback for content_changed node event.
1066  */
1067 static void
1068 sp_object_repr_content_changed(Inkscape::XML::Node *repr, gchar const *oldcontent, gchar const *newcontent, gpointer data)
1070     SPObject *object = SP_OBJECT(data);
1072     if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->read_content)
1073         (*((SPObjectClass *) G_OBJECT_GET_CLASS(object))->read_content)(object);
1076 /**
1077  * Return string representation of space value.
1078  */
1079 static gchar const*
1080 sp_xml_get_space_string(unsigned int space)
1082     switch (space) {
1083         case SP_XML_SPACE_DEFAULT:
1084             return "default";
1085         case SP_XML_SPACE_PRESERVE:
1086             return "preserve";
1087         default:
1088             return NULL;
1089     }
1092 /**
1093  * Callback for write event.
1094  */
1095 static Inkscape::XML::Node *
1096 sp_object_private_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
1098     if (!repr && (flags & SP_OBJECT_WRITE_BUILD)) {
1099         repr = SP_OBJECT_REPR(object)->duplicate();
1100         if (!( flags & SP_OBJECT_WRITE_EXT )) {
1101             repr->setAttribute("inkscape:collect", NULL);
1102         }
1103     } else {
1104         repr->setAttribute("id", object->id);
1106         if (object->xml_space.set) {
1107             char const *xml_space;
1108             xml_space = sp_xml_get_space_string(object->xml_space.value);
1109             repr->setAttribute("xml:space", xml_space);
1110         }
1112         if ( flags & SP_OBJECT_WRITE_EXT &&
1113              object->collectionPolicy() == SPObject::ALWAYS_COLLECT )
1114         {
1115             repr->setAttribute("inkscape:collect", "always");
1116         } else {
1117             repr->setAttribute("inkscape:collect", NULL);
1118         }
1119  
1120         SPStyle const *const obj_style = SP_OBJECT_STYLE(object);
1121         if (obj_style) {
1122             gchar *s = sp_style_write_string(obj_style, SP_STYLE_FLAG_IFSET);
1123             repr->setAttribute("style", ( *s ? s : NULL ));
1124             g_free(s);
1125         } else {
1126             /** \todo I'm not sure what to do in this case.  Bug #1165868
1127              * suggests that it can arise, but the submitter doesn't know
1128              * how to do so reliably.  The main two options are either
1129              * leave repr's style attribute unchanged, or explicitly clear it.
1130              * Must also consider what to do with property attributes for
1131              * the element; see below.
1132              */
1133             char const *style_str = repr->attribute("style");
1134             if (!style_str) {
1135                 style_str = "NULL";
1136             }
1137             g_warning("Item's style is NULL; repr style attribute is %s", style_str);
1138         }
1140         /** \note We treat object->style as authoritative.  Its effects have
1141          * been written to the style attribute above; any properties that are
1142          * unset we take to be deliberately unset (e.g. so that clones can
1143          * override the property).
1144          *
1145          * Note that the below has an undesirable consequence of changing the
1146          * appearance on renderers that lack CSS support (e.g. SVG tiny);
1147          * possibly we should write property attributes instead of a style
1148          * attribute.
1149          */
1150         sp_style_unset_property_attrs (object);
1151     }
1153     return repr;
1156 /**
1157  * Update this object's XML node with flags value.
1158  */
1159 Inkscape::XML::Node *
1160 SPObject::updateRepr(unsigned int flags) {
1161     if (!SP_OBJECT_IS_CLONED(this)) {
1162         Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
1163         if (repr) {
1164             return updateRepr(repr, flags);
1165         } else {
1166             g_critical("Attempt to update non-existent repr");
1167             return NULL;
1168         }
1169     } else {
1170         /* cloned objects have no repr */
1171         return NULL;
1172     }
1175 Inkscape::XML::Node *
1176 SPObject::updateRepr(Inkscape::XML::Node *repr, unsigned int flags) {
1177     if (SP_OBJECT_IS_CLONED(this)) {
1178         /* cloned objects have no repr */
1179         return NULL;
1180     }
1181     if (((SPObjectClass *) G_OBJECT_GET_CLASS(this))->write) {
1182         if (!(flags & SP_OBJECT_WRITE_BUILD) && !repr) {
1183             repr = SP_OBJECT_REPR(this);
1184         }
1185         return ((SPObjectClass *) G_OBJECT_GET_CLASS(this))->write(this, repr, flags);
1186     } else {
1187         g_warning("Class %s does not implement ::write", G_OBJECT_TYPE_NAME(this));
1188         if (!repr) {
1189             if (flags & SP_OBJECT_WRITE_BUILD) {
1190                 repr = SP_OBJECT_REPR(this)->duplicate();
1191             }
1192             /// \todo fixme: else probably error (Lauris) */
1193         } else {
1194             repr->mergeFrom(SP_OBJECT_REPR(this), "id");
1195         }
1196         return repr;
1197     }
1200 /* Modification */
1202 /**
1203  * Add \a flags to \a object's as dirtiness flags, and
1204  * recursively add CHILD_MODIFIED flag to
1205  * parent and ancestors (as far up as necessary).
1206  */
1207 void
1208 SPObject::requestDisplayUpdate(unsigned int flags)
1210     g_return_if_fail( this->document != NULL );
1212     if (update_in_progress) {
1213         g_print("WARNING: Requested update while update in progress, counter = %d\n", update_in_progress);
1214     }
1216     /* requestModified must be used only to set one of SP_OBJECT_MODIFIED_FLAG or
1217      * SP_OBJECT_CHILD_MODIFIED_FLAG */
1218     g_return_if_fail(!(flags & SP_OBJECT_PARENT_MODIFIED_FLAG));
1219     g_return_if_fail((flags & SP_OBJECT_MODIFIED_FLAG) || (flags & SP_OBJECT_CHILD_MODIFIED_FLAG));
1220     g_return_if_fail(!((flags & SP_OBJECT_MODIFIED_FLAG) && (flags & SP_OBJECT_CHILD_MODIFIED_FLAG)));
1222     bool already_propagated = (!(this->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG)));
1224     this->uflags |= flags;
1226     /* If requestModified has already been called on this object or one of its children, then we
1227      * don't need to set CHILD_MODIFIED on our ancestors because it's already been done.
1228      */
1229     if (already_propagated) {
1230         SPObject *parent = SP_OBJECT_PARENT(this);
1231         if (parent) {
1232             parent->requestDisplayUpdate(SP_OBJECT_CHILD_MODIFIED_FLAG);
1233         } else {
1234             sp_document_request_modified(SP_OBJECT_DOCUMENT(this));
1235         }
1236     }
1239 /**
1240  * Update views
1241  */
1242 void
1243 SPObject::updateDisplay(SPCtx *ctx, unsigned int flags)
1245     g_return_if_fail(!(flags & ~SP_OBJECT_MODIFIED_CASCADE));
1247     update_in_progress ++;
1249 #ifdef SP_OBJECT_DEBUG_CASCADE
1250     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);
1251 #endif
1253     /* Get this flags */
1254     flags |= this->uflags;
1255     /* Copy flags to modified cascade for later processing */
1256     this->mflags |= this->uflags;
1257     /* We have to clear flags here to allow rescheduling update */
1258     this->uflags = 0;
1260     // Merge style if we have good reasons to think that parent style is changed */
1261     /** \todo
1262      * I am not sure whether we should check only propagated
1263      * flag. We are currently assuming that style parsing is
1264      * done immediately. I think this is correct (Lauris).
1265      */
1266     if ((flags & SP_OBJECT_STYLE_MODIFIED_FLAG) && (flags & SP_OBJECT_PARENT_MODIFIED_FLAG)) {
1267         if (this->style && this->parent) {
1268             sp_style_merge_from_parent(this->style, this->parent->style);
1269         }
1270     }
1272     if (((SPObjectClass *) G_OBJECT_GET_CLASS(this))->update)
1273         ((SPObjectClass *) G_OBJECT_GET_CLASS(this))->update(this, ctx, flags);
1275     update_in_progress --;
1278 /**
1279  * Request modified always bubbles *up* the tree, as opposed to 
1280  * request display update, which trickles down and relies on the 
1281  * flags set during this pass...
1282  */
1283 void
1284 SPObject::requestModified(unsigned int flags)
1286     g_return_if_fail( this->document != NULL );
1288     /* requestModified must be used only to set one of SP_OBJECT_MODIFIED_FLAG or
1289      * SP_OBJECT_CHILD_MODIFIED_FLAG */
1290     g_return_if_fail(!(flags & SP_OBJECT_PARENT_MODIFIED_FLAG));
1291     g_return_if_fail((flags & SP_OBJECT_MODIFIED_FLAG) || (flags & SP_OBJECT_CHILD_MODIFIED_FLAG));
1292     g_return_if_fail(!((flags & SP_OBJECT_MODIFIED_FLAG) && (flags & SP_OBJECT_CHILD_MODIFIED_FLAG)));
1294     bool already_propagated = (!(this->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG)));
1296     this->mflags |= flags;
1298     /* If requestModified has already been called on this object or one of its children, then we
1299      * don't need to set CHILD_MODIFIED on our ancestors because it's already been done.
1300      */
1301     if (already_propagated) {
1302         SPObject *parent=SP_OBJECT_PARENT(this);
1303         if (parent) {
1304             parent->requestModified(SP_OBJECT_CHILD_MODIFIED_FLAG);
1305         } else {
1306             sp_document_request_modified(SP_OBJECT_DOCUMENT(this));
1307         }
1308     }
1311 /** 
1312  *  Emits the MODIFIED signal with the object's flags.
1313  *  The object's mflags are the original set aside during the update pass for 
1314  *  later delivery here.  Once emitModified() is called, those flags don't
1315  *  need to be stored any longer.
1316  */
1317 void
1318 SPObject::emitModified(unsigned int flags)
1320     /* only the MODIFIED_CASCADE flag is legal here */
1321     g_return_if_fail(!(flags & ~SP_OBJECT_MODIFIED_CASCADE));
1323 #ifdef SP_OBJECT_DEBUG_CASCADE
1324     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);
1325 #endif
1327     flags |= this->mflags;
1328     /* We have to clear mflags beforehand, as signal handlers may
1329      * make changes and therefore queue new modification notifications
1330      * themselves. */
1331     this->mflags = 0;
1333     g_object_ref(G_OBJECT(this));
1334     SPObjectClass *klass=(SPObjectClass *)G_OBJECT_GET_CLASS(this);
1335     if (klass->modified) {
1336         klass->modified(this, flags);
1337     }
1338     _modified_signal.emit(this, flags);
1339     g_object_unref(G_OBJECT(this));
1342 /*
1343  * Get and set descriptive parameters
1344  *
1345  * These are inefficent, so they are not intended to be used interactively
1346  */
1348 gchar const *
1349 sp_object_title_get(SPObject *object)
1351     return NULL;
1354 gchar const *
1355 sp_object_description_get(SPObject *object)
1357     return NULL;
1360 unsigned int
1361 sp_object_title_set(SPObject *object, gchar const *title)
1363     return FALSE;
1366 unsigned int
1367 sp_object_description_set(SPObject *object, gchar const *desc)
1369     return FALSE;
1372 gchar const *
1373 sp_object_tagName_get(SPObject const *object, SPException *ex)
1375     /* If exception is not clear, return */
1376     if (!SP_EXCEPTION_IS_OK(ex)) {
1377         return NULL;
1378     }
1380     /// \todo fixme: Exception if object is NULL? */
1381     return object->repr->name();
1384 gchar const *
1385 sp_object_getAttribute(SPObject const *object, gchar const *key, SPException *ex)
1387     /* If exception is not clear, return */
1388     if (!SP_EXCEPTION_IS_OK(ex)) {
1389         return NULL;
1390     }
1392     /// \todo fixme: Exception if object is NULL? */
1393     return (gchar const *) object->repr->attribute(key);
1396 void
1397 sp_object_setAttribute(SPObject *object, gchar const *key, gchar const *value, SPException *ex)
1399     /* If exception is not clear, return */
1400     g_return_if_fail(SP_EXCEPTION_IS_OK(ex));
1402     /// \todo fixme: Exception if object is NULL? */
1403     if (!sp_repr_set_attr(object->repr, key, value)) {
1404         ex->code = SP_NO_MODIFICATION_ALLOWED_ERR;
1405     }
1408 void
1409 sp_object_removeAttribute(SPObject *object, gchar const *key, SPException *ex)
1411     /* If exception is not clear, return */
1412     g_return_if_fail(SP_EXCEPTION_IS_OK(ex));
1414     /// \todo fixme: Exception if object is NULL? */
1415     if (!sp_repr_set_attr(object->repr, key, NULL)) {
1416         ex->code = SP_NO_MODIFICATION_ALLOWED_ERR;
1417     }
1420 /* Helper */
1422 static gchar *
1423 sp_object_get_unique_id(SPObject *object, gchar const *id)
1425     static unsigned long count = 0;
1427     g_assert(SP_IS_OBJECT(object));
1429     count++;
1431     gchar const *name = object->repr->name();
1432     g_assert(name != NULL);
1434     gchar const *local = strchr(name, ':');
1435     if (local) {
1436         name = local + 1;
1437     }
1439     if (id != NULL) {
1440         if (object->document->getObjectById(id) == NULL) {
1441             return g_strdup(id);
1442         }
1443     }
1445     size_t const name_len = strlen(name);
1446     size_t const buflen = name_len + (sizeof(count) * 10 / 4) + 1;
1447     gchar *const buf = (gchar *) g_malloc(buflen);
1448     memcpy(buf, name, name_len);
1449     gchar *const count_buf = buf + name_len;
1450     size_t const count_buflen = buflen - name_len;
1451     do {
1452         ++count;
1453         g_snprintf(count_buf, count_buflen, "%lu", count);
1454     } while ( object->document->getObjectById(buf) != NULL );
1455     return buf;
1458 /* Style */
1460 /**
1461  * Returns an object style property.
1462  *
1463  * \todo
1464  * fixme: Use proper CSS parsing.  The current version is buggy
1465  * in a number of situations where key is a substring of the
1466  * style string other than as a property name (including
1467  * where key is a substring of a property name), and is also
1468  * buggy in its handling of inheritance for properties that
1469  * aren't inherited by default.  It also doesn't allow for
1470  * the case where the property is specified but with an invalid
1471  * value (in which case I believe the CSS2 error-handling
1472  * behaviour applies, viz. behave as if the property hadn't
1473  * been specified).  Also, the current code doesn't use CRSelEng
1474  * stuff to take a value from stylesheets.  Also, we aren't
1475  * setting any hooks to force an update for changes in any of
1476  * the inputs (i.e., in any of the elements that this function
1477  * queries).
1478  *
1479  * \par
1480  * Given that the default value for a property depends on what
1481  * property it is (e.g., whether to inherit or not), and given
1482  * the above comment about ignoring invalid values, and that the
1483  * repr parent isn't necessarily the right element to inherit
1484  * from (e.g., maybe we need to inherit from the referencing
1485  * <use> element instead), we should probably make the caller
1486  * responsible for ascending the repr tree as necessary.
1487  */
1488 gchar const *
1489 sp_object_get_style_property(SPObject const *object, gchar const *key, gchar const *def)
1491     g_return_val_if_fail(object != NULL, NULL);
1492     g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
1493     g_return_val_if_fail(key != NULL, NULL);
1495     gchar const *style = object->repr->attribute("style");
1496     if (style) {
1497         size_t const len = strlen(key);
1498         char const *p;
1499         while ( (p = strstr(style, key))
1500                 != NULL )
1501         {
1502             p += len;
1503             while ((*p <= ' ') && *p) p++;
1504             if (*p++ != ':') break;
1505             while ((*p <= ' ') && *p) p++;
1506             size_t const inherit_len = sizeof("inherit") - 1;
1507             if (*p
1508                 && !(strneq(p, "inherit", inherit_len)
1509                      && (p[inherit_len] == '\0'
1510                          || p[inherit_len] == ';'
1511                          || g_ascii_isspace(p[inherit_len])))) {
1512                 return p;
1513             }
1514         }
1515     }
1516     gchar const *val = object->repr->attribute(key);
1517     if (val && !streq(val, "inherit")) {
1518         return val;
1519     }
1520     if (object->parent) {
1521         return sp_object_get_style_property(object->parent, key, def);
1522     }
1524     return def;
1527 /**
1528  * Lifts SVG version of all root objects to version.
1529  */
1530 void
1531 SPObject::_requireSVGVersion(Inkscape::Version version) {
1532     for ( SPObject::ParentIterator iter=this ; iter ; ++iter ) {
1533         SPObject *object=iter;
1534         if (SP_IS_ROOT(object)) {
1535             SPRoot *root=SP_ROOT(object);
1536             if ( root->version.svg < version ) {
1537                 root->version.svg = version;
1538             }
1539         }
1540     }
1543 /**
1544  * Return sodipodi version of first root ancestor or (0,0).
1545  */
1546 Inkscape::Version
1547 sp_object_get_sodipodi_version(SPObject *object)
1549     static Inkscape::Version const zero_version(0, 0);
1551     while (object) {
1552         if (SP_IS_ROOT(object)) {
1553             return SP_ROOT(object)->version.sodipodi;
1554         }
1555         object = SP_OBJECT_PARENT(object);
1556     }
1558     return zero_version;
1561 /**
1562  * Returns previous object in sibling list or NULL.
1563  */
1564 SPObject *
1565 sp_object_prev(SPObject *child)
1567     SPObject *parent = SP_OBJECT_PARENT(child);
1568     for ( SPObject *i = sp_object_first_child(parent); i; i = SP_OBJECT_NEXT(i) ) {
1569         if (SP_OBJECT_NEXT(i) == child)
1570             return i;
1571     }
1572     return NULL;
1576 /*
1577   Local Variables:
1578   mode:c++
1579   c-file-style:"stroustrup"
1580   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1581   indent-tabs-mode:nil
1582   fill-column:99
1583   End:
1584 */
1585 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :