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 "util/share.h"
49 #include "util/format.h"
51 #include "algorithms/longest-common-suffix.h"
52 using std::memcpy;
53 using std::strchr;
54 using std::strcmp;
55 using std::strlen;
56 using std::strstr;
58 #define noSP_OBJECT_DEBUG_CASCADE
60 #define noSP_OBJECT_DEBUG
62 #ifdef SP_OBJECT_DEBUG
63 # define debug(f, a...) { g_print("%s(%d) %s:", \
64 __FILE__,__LINE__,__FUNCTION__); \
65 g_print(f, ## a); \
66 g_print("\n"); \
67 }
68 #else
69 # define debug(f, a...) /**/
70 #endif
72 static void sp_object_class_init(SPObjectClass *klass);
73 static void sp_object_init(SPObject *object);
74 static void sp_object_finalize(GObject *object);
76 static void sp_object_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref);
77 static void sp_object_remove_child(SPObject *object, Inkscape::XML::Node *child);
78 static void sp_object_order_changed(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref, Inkscape::XML::Node *new_ref);
80 static void sp_object_release(SPObject *object);
81 static void sp_object_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
83 static void sp_object_private_set(SPObject *object, unsigned int key, gchar const *value);
84 static Inkscape::XML::Node *sp_object_private_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
86 /* Real handlers of repr signals */
88 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);
90 static void sp_object_repr_content_changed(Inkscape::XML::Node *repr, gchar const *oldcontent, gchar const *newcontent, gpointer data);
92 static void sp_object_repr_child_added(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, gpointer data);
93 static void sp_object_repr_child_removed(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, void *data);
95 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);
97 static gchar *sp_object_get_unique_id(SPObject *object, gchar const *defid);
99 guint update_in_progress = 0; // guard against update-during-update
101 enum {RELEASE, MODIFIED, LAST_SIGNAL};
103 Inkscape::XML::NodeEventVector object_event_vector = {
104 sp_object_repr_child_added,
105 sp_object_repr_child_removed,
106 sp_object_repr_attr_changed,
107 sp_object_repr_content_changed,
108 sp_object_repr_order_changed
109 };
111 static GObjectClass *parent_class;
112 static guint object_signals[LAST_SIGNAL] = {0};
114 /**
115 * Registers the SPObject class with Gdk and returns its type number.
116 */
117 GType
118 sp_object_get_type(void)
119 {
120 static GType type = 0;
121 if (!type) {
122 GTypeInfo info = {
123 sizeof(SPObjectClass),
124 NULL, NULL,
125 (GClassInitFunc) sp_object_class_init,
126 NULL, NULL,
127 sizeof(SPObject),
128 16,
129 (GInstanceInitFunc) sp_object_init,
130 NULL
131 };
132 type = g_type_register_static(G_TYPE_OBJECT, "SPObject", &info, (GTypeFlags)0);
133 }
134 return type;
135 }
137 /**
138 * Initializes the SPObject vtable.
139 */
140 static void
141 sp_object_class_init(SPObjectClass *klass)
142 {
143 GObjectClass *object_class;
145 object_class = (GObjectClass *) klass;
147 parent_class = (GObjectClass *) g_type_class_ref(G_TYPE_OBJECT);
149 object_signals[RELEASE] = g_signal_new("release",
150 G_TYPE_FROM_CLASS(klass),
151 (GSignalFlags)(G_SIGNAL_RUN_CLEANUP | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS),
152 G_STRUCT_OFFSET(SPObjectClass, release),
153 NULL, NULL,
154 sp_marshal_VOID__VOID,
155 G_TYPE_NONE, 0);
156 object_signals[MODIFIED] = g_signal_new("modified",
157 G_TYPE_FROM_CLASS(klass),
158 G_SIGNAL_RUN_FIRST,
159 G_STRUCT_OFFSET(SPObjectClass, modified),
160 NULL, NULL,
161 sp_marshal_NONE__UINT,
162 G_TYPE_NONE, 1, G_TYPE_UINT);
164 object_class->finalize = sp_object_finalize;
166 klass->child_added = sp_object_child_added;
167 klass->remove_child = sp_object_remove_child;
168 klass->order_changed = sp_object_order_changed;
170 klass->release = sp_object_release;
172 klass->build = sp_object_build;
174 klass->set = sp_object_private_set;
175 klass->write = sp_object_private_write;
176 }
178 /**
179 * Callback to initialize the SPObject object.
180 */
181 static void
182 sp_object_init(SPObject *object)
183 {
184 debug("id=%x, typename=%s",object, g_type_name_from_instance((GTypeInstance*)object));
186 object->hrefcount = 0;
187 object->_total_hrefcount = 0;
188 object->document = NULL;
189 object->children = object->_last_child = NULL;
190 object->parent = object->next = NULL;
191 object->repr = NULL;
192 object->id = NULL;
193 object->style = NULL;
195 object->_collection_policy = SPObject::COLLECT_WITH_PARENT;
197 new (&object->_delete_signal) sigc::signal<void, SPObject *>();
198 new (&object->_position_changed_signal) sigc::signal<void, SPObject *>();
199 object->_successor = NULL;
201 object->_label = NULL;
202 object->_default_label = NULL;
203 }
205 /**
206 * Callback to destroy all members and connections of object and itself.
207 */
208 static void
209 sp_object_finalize(GObject *object)
210 {
211 SPObject *spobject = (SPObject *)object;
213 g_free(spobject->_label);
214 g_free(spobject->_default_label);
215 spobject->_label = NULL;
216 spobject->_default_label = NULL;
218 if (spobject->_successor) {
219 sp_object_unref(spobject->_successor, NULL);
220 spobject->_successor = NULL;
221 }
223 if (((GObjectClass *) (parent_class))->finalize) {
224 (* ((GObjectClass *) (parent_class))->finalize)(object);
225 }
227 spobject->_delete_signal.~signal();
228 spobject->_position_changed_signal.~signal();
229 }
231 namespace {
233 namespace Debug = Inkscape::Debug;
234 namespace Util = Inkscape::Util;
236 typedef Debug::SimpleEvent<Debug::Event::REFCOUNT> BaseRefCountEvent;
238 class RefCountEvent : public BaseRefCountEvent {
239 public:
240 RefCountEvent(SPObject *object, int bias, Util::ptr_shared<char> name)
241 : BaseRefCountEvent(name)
242 {
243 _addProperty("object", Util::format("%p", object));
244 _addProperty("class", Util::share_static_string(g_type_name(G_TYPE_FROM_INSTANCE(object))));
245 _addProperty("new-refcount", Util::format("%d", G_OBJECT(object)->ref_count + bias));
246 }
247 };
249 class RefEvent : public RefCountEvent {
250 public:
251 RefEvent(SPObject *object)
252 : RefCountEvent(object, 1, Util::share_static_string("sp-object-ref"))
253 {}
254 };
256 class UnrefEvent : public RefCountEvent {
257 public:
258 UnrefEvent(SPObject *object)
259 : RefCountEvent(object, -1, Util::share_static_string("sp-object-unref"))
260 {}
261 };
263 }
265 /**
266 * Increase reference count of object, with possible debugging.
267 *
268 * \param owner If non-NULL, make debug log entry.
269 * \return object, NULL is error.
270 * \pre object points to real object
271 */
272 SPObject *
273 sp_object_ref(SPObject *object, SPObject *owner)
274 {
275 g_return_val_if_fail(object != NULL, NULL);
276 g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
277 g_return_val_if_fail(!owner || SP_IS_OBJECT(owner), NULL);
279 Inkscape::Debug::EventTracker<RefEvent> tracker(object);
280 g_object_ref(G_OBJECT(object));
281 return object;
282 }
284 /**
285 * Decrease reference count of object, with possible debugging and
286 * finalization.
287 *
288 * \param owner If non-NULL, make debug log entry.
289 * \return always NULL
290 * \pre object points to real object
291 */
292 SPObject *
293 sp_object_unref(SPObject *object, SPObject *owner)
294 {
295 g_return_val_if_fail(object != NULL, NULL);
296 g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
297 g_return_val_if_fail(!owner || SP_IS_OBJECT(owner), NULL);
299 Inkscape::Debug::EventTracker<UnrefEvent> tracker(object);
300 g_object_unref(G_OBJECT(object));
301 return NULL;
302 }
304 /**
305 * Increase weak refcount.
306 *
307 * Hrefcount is used for weak references, for example, to
308 * determine whether any graphical element references a certain gradient
309 * node.
310 * \param owner Ignored.
311 * \return object, NULL is error
312 * \pre object points to real object
313 */
314 SPObject *
315 sp_object_href(SPObject *object, gpointer owner)
316 {
317 g_return_val_if_fail(object != NULL, NULL);
318 g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
320 object->hrefcount++;
321 object->_updateTotalHRefCount(1);
323 return object;
324 }
326 /**
327 * Decrease weak refcount.
328 *
329 * Hrefcount is used for weak references, for example, to determine whether
330 * any graphical element references a certain gradient node.
331 * \param owner Ignored.
332 * \return always NULL
333 * \pre object points to real object and hrefcount>0
334 */
335 SPObject *
336 sp_object_hunref(SPObject *object, gpointer owner)
337 {
338 g_return_val_if_fail(object != NULL, NULL);
339 g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
340 g_return_val_if_fail(object->hrefcount > 0, NULL);
342 object->hrefcount--;
343 object->_updateTotalHRefCount(-1);
345 return NULL;
346 }
348 /**
349 * Adds increment to _total_hrefcount of object and its parents.
350 */
351 void
352 SPObject::_updateTotalHRefCount(int increment) {
353 SPObject *topmost_collectable = NULL;
354 for ( SPObject *iter = this ; iter ; iter = SP_OBJECT_PARENT(iter) ) {
355 iter->_total_hrefcount += increment;
356 if ( iter->_total_hrefcount < iter->hrefcount ) {
357 g_critical("HRefs overcounted");
358 }
359 if ( iter->_total_hrefcount == 0 &&
360 iter->_collection_policy != COLLECT_WITH_PARENT )
361 {
362 topmost_collectable = iter;
363 }
364 }
365 if (topmost_collectable) {
366 topmost_collectable->requestOrphanCollection();
367 }
368 }
370 /**
371 * True if object is non-NULL and this is some in/direct parent of object.
372 */
373 bool
374 SPObject::isAncestorOf(SPObject const *object) const {
375 g_return_val_if_fail(object != NULL, false);
376 object = SP_OBJECT_PARENT(object);
377 while (object) {
378 if ( object == this ) {
379 return true;
380 }
381 object = SP_OBJECT_PARENT(object);
382 }
383 return false;
384 }
386 namespace {
388 bool same_objects(SPObject const &a, SPObject const &b) {
389 return &a == &b;
390 }
392 }
394 /**
395 * Returns youngest object being parent to this and object.
396 */
397 SPObject const *
398 SPObject::nearestCommonAncestor(SPObject const *object) const {
399 g_return_val_if_fail(object != NULL, NULL);
401 using Inkscape::Algorithms::longest_common_suffix;
402 return longest_common_suffix<SPObject::ConstParentIterator>(this, object, NULL, &same_objects);
403 }
405 SPObject const *AncestorSon(SPObject const *obj, SPObject const *ancestor) {
406 if (obj == NULL || ancestor == NULL)
407 return NULL;
408 if (SP_OBJECT_PARENT(obj) == ancestor)
409 return obj;
410 return AncestorSon(SP_OBJECT_PARENT(obj), ancestor);
411 }
413 /**
414 * Compares height of objects in tree.
415 *
416 * Works for different-parent objects, so long as they have a common ancestor.
417 * \return \verbatim
418 * 0 positions are equivalent
419 * 1 first object's position is greater than the second
420 * -1 first object's position is less than the second \endverbatim
421 */
422 int
423 sp_object_compare_position(SPObject const *first, SPObject const *second)
424 {
425 if (first == second) return 0;
427 SPObject const *ancestor = first->nearestCommonAncestor(second);
428 if (ancestor == NULL) return 0; // cannot compare, no common ancestor!
430 // we have an object and its ancestor (should not happen when sorting selection)
431 if (ancestor == first)
432 return 1;
433 if (ancestor == second)
434 return -1;
436 SPObject const *to_first = AncestorSon(first, ancestor);
437 SPObject const *to_second = AncestorSon(second, ancestor);
439 g_assert(SP_OBJECT_PARENT(to_second) == SP_OBJECT_PARENT(to_first));
441 return sp_repr_compare_position(SP_OBJECT_REPR(to_first), SP_OBJECT_REPR(to_second));
442 }
445 /**
446 * Append repr as child of this object.
447 * \pre this is not a cloned object
448 */
449 SPObject *
450 SPObject::appendChildRepr(Inkscape::XML::Node *repr) {
451 if (!SP_OBJECT_IS_CLONED(this)) {
452 SP_OBJECT_REPR(this)->appendChild(repr);
453 return SP_OBJECT_DOCUMENT(this)->getObjectByRepr(repr);
454 } else {
455 g_critical("Attempt to append repr as child of cloned object");
456 return NULL;
457 }
458 }
460 /** Gets the label property for the object or a default if no label
461 * is defined.
462 */
463 gchar const *
464 SPObject::label() const {
465 return _label;
466 }
468 /** Returns a default label property for the object. */
469 gchar const *
470 SPObject::defaultLabel() const {
471 if (_label) {
472 return _label;
473 } else {
474 if (!_default_label) {
475 gchar const *id=SP_OBJECT_ID(this);
476 if (id) {
477 _default_label = g_strdup_printf("#%s", id);
478 } else {
479 _default_label = g_strdup_printf("<%s>", SP_OBJECT_REPR(this)->name());
480 }
481 }
482 return _default_label;
483 }
484 }
486 /** Sets the label property for the object */
487 void
488 SPObject::setLabel(gchar const *label) {
489 SP_OBJECT_REPR(this)->setAttribute("inkscape:label", label, false);
490 }
493 /** Queues the object for orphan collection */
494 void
495 SPObject::requestOrphanCollection() {
496 g_return_if_fail(document != NULL);
497 document->queueForOrphanCollection(this);
499 /** \todo
500 * This is a temporary hack added to make fill&stroke rebuild its
501 * gradient list when the defs are vacuumed. gradient-vector.cpp
502 * listens to the modified signal on defs, and now we give it that
503 * signal. Mental says that this should be made automatic by
504 * merging SPObjectGroup with SPObject; SPObjectGroup would issue
505 * this signal automatically. Or maybe just derive SPDefs from
506 * SPObjectGroup?
507 */
509 this->requestModified(SP_OBJECT_CHILD_MODIFIED_FLAG);
510 }
512 /** Sends the delete signal to all children of this object recursively */
513 void
514 SPObject::_sendDeleteSignalRecursive() {
515 for (SPObject *child = sp_object_first_child(this); child; child = SP_OBJECT_NEXT(child)) {
516 child->_delete_signal.emit(child);
517 child->_sendDeleteSignalRecursive();
518 }
519 }
521 /**
522 * Deletes the object reference, unparenting it from its parent.
523 *
524 * If the \a propagate parameter is set to true, it emits a delete
525 * signal. If the \a propagate_descendants parameter is true, it
526 * recursively sends the delete signal to children.
527 */
528 void
529 SPObject::deleteObject(bool propagate, bool propagate_descendants)
530 {
531 sp_object_ref(this, NULL);
532 if (propagate) {
533 _delete_signal.emit(this);
534 }
535 if (propagate_descendants) {
536 this->_sendDeleteSignalRecursive();
537 }
539 Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
540 if (repr && sp_repr_parent(repr)) {
541 sp_repr_unparent(repr);
542 }
544 if (_successor) {
545 _successor->deleteObject(propagate, propagate_descendants);
546 }
547 sp_object_unref(this, NULL);
548 }
550 /**
551 * Put object into object tree, under parent, and behind prev;
552 * also update object's XML space.
553 */
554 void
555 sp_object_attach(SPObject *parent, SPObject *object, SPObject *prev)
556 {
557 g_return_if_fail(parent != NULL);
558 g_return_if_fail(SP_IS_OBJECT(parent));
559 g_return_if_fail(object != NULL);
560 g_return_if_fail(SP_IS_OBJECT(object));
561 g_return_if_fail(!prev || SP_IS_OBJECT(prev));
562 g_return_if_fail(!prev || prev->parent == parent);
563 g_return_if_fail(!object->parent);
565 sp_object_ref(object, parent);
566 object->parent = parent;
567 parent->_updateTotalHRefCount(object->_total_hrefcount);
569 SPObject *next;
570 if (prev) {
571 next = prev->next;
572 prev->next = object;
573 } else {
574 next = parent->children;
575 parent->children = object;
576 }
577 object->next = next;
578 if (!next) {
579 parent->_last_child = object;
580 }
581 if (!object->xml_space.set)
582 object->xml_space.value = parent->xml_space.value;
583 }
585 /**
586 * In list of object's siblings, move object behind prev.
587 */
588 void
589 sp_object_reorder(SPObject *object, SPObject *prev) {
590 g_return_if_fail(object != NULL);
591 g_return_if_fail(SP_IS_OBJECT(object));
592 g_return_if_fail(object->parent != NULL);
593 g_return_if_fail(object != prev);
594 g_return_if_fail(!prev || SP_IS_OBJECT(prev));
595 g_return_if_fail(!prev || prev->parent == object->parent);
597 SPObject *const parent=object->parent;
599 SPObject *old_prev=NULL;
600 for ( SPObject *child = parent->children ; child && child != object ;
601 child = child->next )
602 {
603 old_prev = child;
604 }
606 SPObject *next=object->next;
607 if (old_prev) {
608 old_prev->next = next;
609 } else {
610 parent->children = next;
611 }
612 if (!next) {
613 parent->_last_child = old_prev;
614 }
615 if (prev) {
616 next = prev->next;
617 prev->next = object;
618 } else {
619 next = parent->children;
620 parent->children = object;
621 }
622 object->next = next;
623 if (!next) {
624 parent->_last_child = object;
625 }
626 }
628 /**
629 * Remove object from parent's children, release and unref it.
630 */
631 void
632 sp_object_detach(SPObject *parent, SPObject *object) {
633 g_return_if_fail(parent != NULL);
634 g_return_if_fail(SP_IS_OBJECT(parent));
635 g_return_if_fail(object != NULL);
636 g_return_if_fail(SP_IS_OBJECT(object));
637 g_return_if_fail(object->parent == parent);
639 sp_object_invoke_release(object);
641 SPObject *prev=NULL;
642 for ( SPObject *child = parent->children ; child && child != object ;
643 child = child->next )
644 {
645 prev = child;
646 }
648 SPObject *next=object->next;
649 if (prev) {
650 prev->next = next;
651 } else {
652 parent->children = next;
653 }
654 if (!next) {
655 parent->_last_child = prev;
656 }
658 object->next = NULL;
659 object->parent = NULL;
661 parent->_updateTotalHRefCount(-object->_total_hrefcount);
662 sp_object_unref(object, parent);
663 }
665 /**
666 * Return object's child whose node pointer equals repr.
667 */
668 SPObject *
669 sp_object_get_child_by_repr(SPObject *object, Inkscape::XML::Node *repr)
670 {
671 g_return_val_if_fail(object != NULL, NULL);
672 g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
673 g_return_val_if_fail(repr != NULL, NULL);
675 if (object->_last_child && SP_OBJECT_REPR(object->_last_child) == repr)
676 return object->_last_child; // optimization for common scenario
677 for ( SPObject *child = object->children ; child ; child = child->next ) {
678 if ( SP_OBJECT_REPR(child) == repr ) {
679 return child;
680 }
681 }
683 return NULL;
684 }
686 /**
687 * Callback for child_added event.
688 * Invoked whenever the given mutation event happens in the XML tree.
689 */
690 static void
691 sp_object_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
692 {
693 GType type = sp_repr_type_lookup(child);
694 if (!type) {
695 return;
696 }
697 SPObject *ochild = SP_OBJECT(g_object_new(type, 0));
698 SPObject *prev = ref ? sp_object_get_child_by_repr(object, ref) : NULL;
699 sp_object_attach(object, ochild, prev);
700 sp_object_unref(ochild, NULL);
702 sp_object_invoke_build(ochild, object->document, child, SP_OBJECT_IS_CLONED(object));
703 }
705 /**
706 * Removes, releases and unrefs all children of object.
707 *
708 * This is the opposite of build. It has to be invoked as soon as the
709 * object is removed from the tree, even if it is still alive according
710 * to reference count. The frontend unregisters the object from the
711 * document and releases the SPRepr bindings; implementations should free
712 * state data and release all child objects. Invoking release on
713 * SPRoot destroys the whole document tree.
714 * \see sp_object_build()
715 */
716 static void sp_object_release(SPObject *object)
717 {
718 debug("id=%x, typename=%s", object, g_type_name_from_instance((GTypeInstance*)object));
719 while (object->children) {
720 sp_object_detach(object, object->children);
721 }
722 }
724 /**
725 * Remove object's child whose node equals repr, release and
726 * unref it.
727 *
728 * Invoked whenever the given mutation event happens in the XML
729 * tree, BEFORE removal from the XML tree happens, so grouping
730 * objects can safely release the child data.
731 */
732 static void
733 sp_object_remove_child(SPObject *object, Inkscape::XML::Node *child)
734 {
735 debug("id=%x, typename=%s", object, g_type_name_from_instance((GTypeInstance*)object));
736 SPObject *ochild = sp_object_get_child_by_repr(object, child);
737 g_return_if_fail(ochild != NULL);
738 sp_object_detach(object, ochild);
739 }
741 /**
742 * Move object corresponding to child after sibling object corresponding
743 * to new_ref.
744 * Invoked whenever the given mutation event happens in the XML tree.
745 * \param old_ref Ignored
746 */
747 static void sp_object_order_changed(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref,
748 Inkscape::XML::Node *new_ref)
749 {
750 SPObject *ochild = sp_object_get_child_by_repr(object, child);
751 g_return_if_fail(ochild != NULL);
752 SPObject *prev = new_ref ? sp_object_get_child_by_repr(object, new_ref) : NULL;
753 sp_object_reorder(ochild, prev);
754 ochild->_position_changed_signal.emit(ochild);
755 }
757 /**
758 * Virtual build callback.
759 *
760 * This has to be invoked immediately after creation of an SPObject. The
761 * frontend method ensures that the new object is properly attached to
762 * the document and repr; implementation then will parse all of the attributes,
763 * generate the children objects and so on. Invoking build on the SPRoot
764 * object results in creation of the whole document tree (this is, what
765 * SPDocument does after the creation of the XML tree).
766 * \see sp_object_release()
767 */
768 static void
769 sp_object_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
770 {
771 /* Nothing specific here */
772 debug("id=%x, typename=%s", object, g_type_name_from_instance((GTypeInstance*)object));
774 sp_object_read_attr(object, "xml:space");
775 sp_object_read_attr(object, "inkscape:label");
776 sp_object_read_attr(object, "inkscape:collect");
778 for (Inkscape::XML::Node *rchild = repr->firstChild() ; rchild != NULL; rchild = rchild->next()) {
779 GType type = sp_repr_type_lookup(rchild);
780 if (!type) {
781 continue;
782 }
783 SPObject *child = SP_OBJECT(g_object_new(type, 0));
784 sp_object_attach(object, child, object->lastChild());
785 sp_object_unref(child, NULL);
786 sp_object_invoke_build(child, document, rchild, SP_OBJECT_IS_CLONED(object));
787 }
788 }
790 void
791 sp_object_invoke_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr, unsigned int cloned)
792 {
793 debug("id=%x, typename=%s", object, g_type_name_from_instance((GTypeInstance*)object));
795 g_assert(object != NULL);
796 g_assert(SP_IS_OBJECT(object));
797 g_assert(document != NULL);
798 g_assert(repr != NULL);
800 g_assert(object->document == NULL);
801 g_assert(object->repr == NULL);
802 g_assert(object->id == NULL);
804 /* Bookkeeping */
806 object->document = document;
807 object->repr = repr;
808 Inkscape::GC::anchor(repr);
809 object->cloned = cloned;
811 if (!SP_OBJECT_IS_CLONED(object)) {
812 object->document->bindObjectToRepr(object->repr, object);
814 if (Inkscape::XML::id_permitted(object->repr)) {
815 /* If we are not cloned, force unique id */
816 gchar const *id = object->repr->attribute("id");
817 gchar *realid = sp_object_get_unique_id(object, id);
818 g_assert(realid != NULL);
820 object->document->bindObjectToId(realid, object);
821 object->id = realid;
823 /* Redefine ID, if required */
824 if ((id == NULL) || (strcmp(id, realid) != 0)) {
825 gboolean undo_sensitive=sp_document_get_undo_sensitive(document);
826 sp_document_set_undo_sensitive(document, FALSE);
827 object->repr->setAttribute("id", realid);
828 sp_document_set_undo_sensitive(document, undo_sensitive);
829 }
830 }
831 } else {
832 g_assert(object->id == NULL);
833 }
835 /* Invoke derived methods, if any */
837 if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->build) {
838 (*((SPObjectClass *) G_OBJECT_GET_CLASS(object))->build)(object, document, repr);
839 }
841 /* Signalling (should be connected AFTER processing derived methods */
842 sp_repr_add_listener(repr, &object_event_vector, object);
843 }
845 void
846 sp_object_invoke_release(SPObject *object)
847 {
848 g_assert(object != NULL);
849 g_assert(SP_IS_OBJECT(object));
851 g_assert(object->document);
852 g_assert(object->repr);
854 sp_repr_remove_listener_by_data(object->repr, object);
856 g_signal_emit(G_OBJECT(object), object_signals[RELEASE], 0);
858 /* all hrefs should be released by the "release" handlers */
859 g_assert(object->hrefcount == 0);
861 if (!SP_OBJECT_IS_CLONED(object)) {
862 if (object->id) {
863 object->document->bindObjectToId(object->id, NULL);
864 }
865 g_free(object->id);
866 object->id = NULL;
868 g_free(object->_default_label);
869 object->_default_label = NULL;
871 object->document->bindObjectToRepr(object->repr, NULL);
872 } else {
873 g_assert(!object->id);
874 }
876 if (object->style) {
877 object->style = sp_style_unref(object->style);
878 }
880 Inkscape::GC::release(object->repr);
882 object->document = NULL;
883 object->repr = NULL;
884 }
886 /**
887 * Callback for child_added node event.
888 */
889 static void
890 sp_object_repr_child_added(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, gpointer data)
891 {
892 SPObject *object = SP_OBJECT(data);
894 if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->child_added)
895 (*((SPObjectClass *)G_OBJECT_GET_CLASS(object))->child_added)(object, child, ref);
896 }
898 /**
899 * Callback for remove_child node event.
900 */
901 static void
902 sp_object_repr_child_removed(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, gpointer data)
903 {
904 SPObject *object = SP_OBJECT(data);
906 if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->remove_child) {
907 (* ((SPObjectClass *)G_OBJECT_GET_CLASS(object))->remove_child)(object, child);
908 }
909 }
911 /**
912 * Callback for order_changed node event.
913 *
914 * \todo fixme:
915 */
916 static void
917 sp_object_repr_order_changed(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *old, Inkscape::XML::Node *newer, gpointer data)
918 {
919 SPObject *object = SP_OBJECT(data);
921 if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->order_changed) {
922 (* ((SPObjectClass *)G_OBJECT_GET_CLASS(object))->order_changed)(object, child, old, newer);
923 }
924 }
926 /**
927 * Callback for set event.
928 */
929 static void
930 sp_object_private_set(SPObject *object, unsigned int key, gchar const *value)
931 {
932 g_assert(key != SP_ATTR_INVALID);
934 switch (key) {
935 case SP_ATTR_ID:
936 if ( !SP_OBJECT_IS_CLONED(object) && object->repr->type() == Inkscape::XML::ELEMENT_NODE ) {
937 SPDocument *document=object->document;
938 SPObject *conflict=NULL;
940 if (value) {
941 conflict = document->getObjectById((char const *)value);
942 }
943 if ( conflict && conflict != object ) {
944 sp_object_ref(conflict, NULL);
945 // give the conflicting object a new ID
946 gchar *new_conflict_id = sp_object_get_unique_id(conflict, NULL);
947 SP_OBJECT_REPR(conflict)->setAttribute("id", new_conflict_id);
948 g_free(new_conflict_id);
949 sp_object_unref(conflict, NULL);
950 }
952 if (object->id) {
953 document->bindObjectToId(object->id, NULL);
954 g_free(object->id);
955 }
957 if (value) {
958 object->id = g_strdup((char const*)value);
959 document->bindObjectToId(object->id, object);
960 } else {
961 object->id = NULL;
962 }
964 g_free(object->_default_label);
965 object->_default_label = NULL;
966 }
967 break;
968 case SP_ATTR_INKSCAPE_LABEL:
969 g_free(object->_label);
970 if (value) {
971 object->_label = g_strdup(value);
972 } else {
973 object->_label = NULL;
974 }
975 g_free(object->_default_label);
976 object->_default_label = NULL;
977 break;
978 case SP_ATTR_INKSCAPE_COLLECT:
979 if ( value && !strcmp(value, "always") ) {
980 object->setCollectionPolicy(SPObject::ALWAYS_COLLECT);
981 } else {
982 object->setCollectionPolicy(SPObject::COLLECT_WITH_PARENT);
983 }
984 break;
985 case SP_ATTR_XML_SPACE:
986 if (value && !strcmp(value, "preserve")) {
987 object->xml_space.value = SP_XML_SPACE_PRESERVE;
988 object->xml_space.set = TRUE;
989 } else if (value && !strcmp(value, "default")) {
990 object->xml_space.value = SP_XML_SPACE_DEFAULT;
991 object->xml_space.set = TRUE;
992 } else if (object->parent) {
993 SPObject *parent;
994 parent = object->parent;
995 object->xml_space.value = parent->xml_space.value;
996 }
997 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
998 break;
999 default:
1000 break;
1001 }
1002 }
1004 /**
1005 * Call virtual set() function of object.
1006 */
1007 void
1008 sp_object_set(SPObject *object, unsigned int key, gchar const *value)
1009 {
1010 g_assert(object != NULL);
1011 g_assert(SP_IS_OBJECT(object));
1013 if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->set) {
1014 ((SPObjectClass *) G_OBJECT_GET_CLASS(object))->set(object, key, value);
1015 }
1016 }
1018 /**
1019 * Read value of key attribute from XML node into object.
1020 */
1021 void
1022 sp_object_read_attr(SPObject *object, gchar const *key)
1023 {
1024 g_assert(object != NULL);
1025 g_assert(SP_IS_OBJECT(object));
1026 g_assert(key != NULL);
1028 g_assert(object->repr != NULL);
1030 unsigned int keyid = sp_attribute_lookup(key);
1031 if (keyid != SP_ATTR_INVALID) {
1032 /* Retrieve the 'key' attribute from the object's XML representation */
1033 gchar const *value = object->repr->attribute(key);
1035 sp_object_set(object, keyid, value);
1036 }
1037 }
1039 /**
1040 * Callback for attr_changed node event.
1041 */
1042 static void
1043 sp_object_repr_attr_changed(Inkscape::XML::Node *repr, gchar const *key, gchar const *oldval, gchar const *newval, bool is_interactive, gpointer data)
1044 {
1045 SPObject *object = SP_OBJECT(data);
1047 sp_object_read_attr(object, key);
1049 // manual changes to extension attributes require the normal
1050 // attributes, which depend on them, to be updated immediately
1051 if (is_interactive) {
1052 object->updateRepr(repr, 0);
1053 }
1054 }
1056 /**
1057 * Callback for content_changed node event.
1058 */
1059 static void
1060 sp_object_repr_content_changed(Inkscape::XML::Node *repr, gchar const *oldcontent, gchar const *newcontent, gpointer data)
1061 {
1062 SPObject *object = SP_OBJECT(data);
1064 if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->read_content)
1065 (*((SPObjectClass *) G_OBJECT_GET_CLASS(object))->read_content)(object);
1066 }
1068 /**
1069 * Return string representation of space value.
1070 */
1071 static gchar const*
1072 sp_xml_get_space_string(unsigned int space)
1073 {
1074 switch (space) {
1075 case SP_XML_SPACE_DEFAULT:
1076 return "default";
1077 case SP_XML_SPACE_PRESERVE:
1078 return "preserve";
1079 default:
1080 return NULL;
1081 }
1082 }
1084 /**
1085 * Callback for write event.
1086 */
1087 static Inkscape::XML::Node *
1088 sp_object_private_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
1089 {
1090 if (!repr && (flags & SP_OBJECT_WRITE_BUILD)) {
1091 repr = SP_OBJECT_REPR(object)->duplicate();
1092 if (!( flags & SP_OBJECT_WRITE_EXT )) {
1093 repr->setAttribute("inkscape:collect", NULL);
1094 }
1095 } else {
1096 repr->setAttribute("id", object->id);
1098 if (object->xml_space.set) {
1099 char const *xml_space;
1100 xml_space = sp_xml_get_space_string(object->xml_space.value);
1101 repr->setAttribute("xml:space", xml_space);
1102 }
1104 if ( flags & SP_OBJECT_WRITE_EXT &&
1105 object->collectionPolicy() == SPObject::ALWAYS_COLLECT )
1106 {
1107 repr->setAttribute("inkscape:collect", "always");
1108 } else {
1109 repr->setAttribute("inkscape:collect", NULL);
1110 }
1111 }
1113 return repr;
1114 }
1116 /**
1117 * Update this object's XML node with flags value.
1118 */
1119 Inkscape::XML::Node *
1120 SPObject::updateRepr(unsigned int flags) {
1121 if (!SP_OBJECT_IS_CLONED(this)) {
1122 Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
1123 if (repr) {
1124 return updateRepr(repr, flags);
1125 } else {
1126 g_critical("Attempt to update non-existent repr");
1127 return NULL;
1128 }
1129 } else {
1130 /* cloned objects have no repr */
1131 return NULL;
1132 }
1133 }
1135 Inkscape::XML::Node *
1136 SPObject::updateRepr(Inkscape::XML::Node *repr, unsigned int flags) {
1137 if (SP_OBJECT_IS_CLONED(this)) {
1138 /* cloned objects have no repr */
1139 return NULL;
1140 }
1141 if (((SPObjectClass *) G_OBJECT_GET_CLASS(this))->write) {
1142 if (!(flags & SP_OBJECT_WRITE_BUILD) && !repr) {
1143 repr = SP_OBJECT_REPR(this);
1144 }
1145 return ((SPObjectClass *) G_OBJECT_GET_CLASS(this))->write(this, repr, flags);
1146 } else {
1147 g_warning("Class %s does not implement ::write", G_OBJECT_TYPE_NAME(this));
1148 if (!repr) {
1149 if (flags & SP_OBJECT_WRITE_BUILD) {
1150 repr = SP_OBJECT_REPR(this)->duplicate();
1151 }
1152 /// \todo fixme: else probably error (Lauris) */
1153 } else {
1154 repr->mergeFrom(SP_OBJECT_REPR(this), "id");
1155 }
1156 return repr;
1157 }
1158 }
1160 /* Modification */
1162 /**
1163 * Add \a flags to \a object's as dirtiness flags, and
1164 * recursively add CHILD_MODIFIED flag to
1165 * parent and ancestors (as far up as necessary).
1166 */
1167 void
1168 SPObject::requestDisplayUpdate(unsigned int flags)
1169 {
1170 if (update_in_progress) {
1171 g_print("WARNING: Requested update while update in progress, counter = %d\n", update_in_progress);
1172 }
1174 g_return_if_fail(!(flags & SP_OBJECT_PARENT_MODIFIED_FLAG));
1175 g_return_if_fail((flags & SP_OBJECT_MODIFIED_FLAG) || (flags & SP_OBJECT_CHILD_MODIFIED_FLAG));
1176 g_return_if_fail(!((flags & SP_OBJECT_MODIFIED_FLAG) && (flags & SP_OBJECT_CHILD_MODIFIED_FLAG)));
1178 /* Check for propagate before we set any flags */
1179 /* Propagate means, that this is not passed through by modification request cascade yet */
1180 unsigned int propagate = (!(this->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG)));
1182 /* Just set this flags safe even if some have been set before */
1183 this->uflags |= flags;
1185 if (propagate) {
1186 if (this->parent) {
1187 this->parent->requestDisplayUpdate(SP_OBJECT_CHILD_MODIFIED_FLAG);
1188 } else {
1189 sp_document_request_modified(this->document);
1190 }
1191 }
1192 }
1194 void
1195 SPObject::updateDisplay(SPCtx *ctx, unsigned int flags)
1196 {
1197 g_return_if_fail(!(flags & ~SP_OBJECT_MODIFIED_CASCADE));
1199 update_in_progress ++;
1201 #ifdef SP_OBJECT_DEBUG_CASCADE
1202 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);
1203 #endif
1205 /* Get this flags */
1206 flags |= this->uflags;
1207 /* Copy flags to modified cascade for later processing */
1208 this->mflags |= this->uflags;
1209 /* We have to clear flags here to allow rescheduling update */
1210 this->uflags = 0;
1212 // Merge style if we have good reasons to think that parent style is changed */
1213 /** \todo
1214 * I am not sure whether we should check only propagated
1215 * flag. We are currently assuming that style parsing is
1216 * done immediately. I think this is correct (Lauris).
1217 */
1218 if ((flags & SP_OBJECT_STYLE_MODIFIED_FLAG) && (flags & SP_OBJECT_PARENT_MODIFIED_FLAG)) {
1219 if (this->style && this->parent) {
1220 sp_style_merge_from_parent(this->style, this->parent->style);
1221 }
1222 }
1224 if (((SPObjectClass *) G_OBJECT_GET_CLASS(this))->update)
1225 ((SPObjectClass *) G_OBJECT_GET_CLASS(this))->update(this, ctx, flags);
1227 update_in_progress --;
1228 }
1230 void
1231 SPObject::requestModified(unsigned int flags)
1232 {
1233 g_return_if_fail( this->document != NULL );
1235 /* PARENT_MODIFIED is computed later on and is not intended to be
1236 * "manually" queued */
1237 g_return_if_fail(!(flags & SP_OBJECT_PARENT_MODIFIED_FLAG));
1239 /* we should be setting either MODIFIED or CHILD_MODIFIED... */
1240 g_return_if_fail((flags & SP_OBJECT_MODIFIED_FLAG) || (flags & SP_OBJECT_CHILD_MODIFIED_FLAG));
1242 /* ...but not both */
1243 g_return_if_fail(!((flags & SP_OBJECT_MODIFIED_FLAG) && (flags & SP_OBJECT_CHILD_MODIFIED_FLAG)));
1245 unsigned int old_mflags=this->mflags;
1246 this->mflags |= flags;
1248 /* If we already had MODIFIED or CHILD_MODIFIED queued, we will
1249 * have already queued CHILD_MODIFIED with our ancestors and
1250 * need not disturb them again.
1251 */
1252 if (!( old_mflags & ( SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG ) )) {
1253 SPObject *parent=SP_OBJECT_PARENT(this);
1254 if (parent) {
1255 parent->requestModified(SP_OBJECT_CHILD_MODIFIED_FLAG);
1256 } else {
1257 sp_document_request_modified(SP_OBJECT_DOCUMENT(this));
1258 }
1259 }
1260 }
1262 void
1263 SPObject::emitModified(unsigned int flags)
1264 {
1265 /* only the MODIFIED_CASCADE flag is legal here */
1266 g_return_if_fail(!(flags & ~SP_OBJECT_MODIFIED_CASCADE));
1268 #ifdef SP_OBJECT_DEBUG_CASCADE
1269 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);
1270 #endif
1272 flags |= this->mflags;
1273 /* We have to clear mflags beforehand, as signal handlers may
1274 * make changes and therefore queue new modification notifications
1275 * themselves. */
1276 this->mflags = 0;
1278 g_object_ref(G_OBJECT(this));
1279 g_signal_emit(G_OBJECT(this), object_signals[MODIFIED], 0, flags);
1280 g_object_unref(G_OBJECT(this));
1281 }
1283 /*
1284 * Get and set descriptive parameters
1285 *
1286 * These are inefficent, so they are not intended to be used interactively
1287 */
1289 gchar const *
1290 sp_object_title_get(SPObject *object)
1291 {
1292 return NULL;
1293 }
1295 gchar const *
1296 sp_object_description_get(SPObject *object)
1297 {
1298 return NULL;
1299 }
1301 unsigned int
1302 sp_object_title_set(SPObject *object, gchar const *title)
1303 {
1304 return FALSE;
1305 }
1307 unsigned int
1308 sp_object_description_set(SPObject *object, gchar const *desc)
1309 {
1310 return FALSE;
1311 }
1313 gchar const *
1314 sp_object_tagName_get(SPObject const *object, SPException *ex)
1315 {
1316 /* If exception is not clear, return */
1317 if (!SP_EXCEPTION_IS_OK(ex)) {
1318 return NULL;
1319 }
1321 /// \todo fixme: Exception if object is NULL? */
1322 return object->repr->name();
1323 }
1325 gchar const *
1326 sp_object_getAttribute(SPObject const *object, gchar const *key, SPException *ex)
1327 {
1328 /* If exception is not clear, return */
1329 if (!SP_EXCEPTION_IS_OK(ex)) {
1330 return NULL;
1331 }
1333 /// \todo fixme: Exception if object is NULL? */
1334 return (gchar const *) object->repr->attribute(key);
1335 }
1337 void
1338 sp_object_setAttribute(SPObject *object, gchar const *key, gchar const *value, SPException *ex)
1339 {
1340 /* If exception is not clear, return */
1341 g_return_if_fail(SP_EXCEPTION_IS_OK(ex));
1343 /// \todo fixme: Exception if object is NULL? */
1344 if (!sp_repr_set_attr(object->repr, key, value)) {
1345 ex->code = SP_NO_MODIFICATION_ALLOWED_ERR;
1346 }
1347 }
1349 void
1350 sp_object_removeAttribute(SPObject *object, gchar const *key, SPException *ex)
1351 {
1352 /* If exception is not clear, return */
1353 g_return_if_fail(SP_EXCEPTION_IS_OK(ex));
1355 /// \todo fixme: Exception if object is NULL? */
1356 if (!sp_repr_set_attr(object->repr, key, NULL)) {
1357 ex->code = SP_NO_MODIFICATION_ALLOWED_ERR;
1358 }
1359 }
1361 /* Helper */
1363 static gchar *
1364 sp_object_get_unique_id(SPObject *object, gchar const *id)
1365 {
1366 static unsigned long count = 0;
1368 g_assert(SP_IS_OBJECT(object));
1370 count++;
1372 gchar const *name = object->repr->name();
1373 g_assert(name != NULL);
1375 gchar const *local = strchr(name, ':');
1376 if (local) {
1377 name = local + 1;
1378 }
1380 if (id != NULL) {
1381 if (object->document->getObjectById(id) == NULL) {
1382 return g_strdup(id);
1383 }
1384 }
1386 size_t const name_len = strlen(name);
1387 size_t const buflen = name_len + (sizeof(count) * 10 / 4) + 1;
1388 gchar *const buf = (gchar *) g_malloc(buflen);
1389 memcpy(buf, name, name_len);
1390 gchar *const count_buf = buf + name_len;
1391 size_t const count_buflen = buflen - name_len;
1392 do {
1393 ++count;
1394 g_snprintf(count_buf, count_buflen, "%lu", count);
1395 } while ( object->document->getObjectById(buf) != NULL );
1396 return buf;
1397 }
1399 /* Style */
1401 /**
1402 * Returns an object style property.
1403 *
1404 * \todo
1405 * fixme: Use proper CSS parsing. The current version is buggy
1406 * in a number of situations where key is a substring of the
1407 * style string other than as a property name (including
1408 * where key is a substring of a property name), and is also
1409 * buggy in its handling of inheritance for properties that
1410 * aren't inherited by default. It also doesn't allow for
1411 * the case where the property is specified but with an invalid
1412 * value (in which case I believe the CSS2 error-handling
1413 * behaviour applies, viz. behave as if the property hadn't
1414 * been specified). Also, the current code doesn't use CRSelEng
1415 * stuff to take a value from stylesheets. Also, we aren't
1416 * setting any hooks to force an update for changes in any of
1417 * the inputs (i.e., in any of the elements that this function
1418 * queries).
1419 *
1420 * \par
1421 * Given that the default value for a property depends on what
1422 * property it is (e.g., whether to inherit or not), and given
1423 * the above comment about ignoring invalid values, and that the
1424 * repr parent isn't necessarily the right element to inherit
1425 * from (e.g., maybe we need to inherit from the referencing
1426 * <use> element instead), we should probably make the caller
1427 * responsible for ascending the repr tree as necessary.
1428 */
1429 gchar const *
1430 sp_object_get_style_property(SPObject const *object, gchar const *key, gchar const *def)
1431 {
1432 g_return_val_if_fail(object != NULL, NULL);
1433 g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
1434 g_return_val_if_fail(key != NULL, NULL);
1436 gchar const *style = object->repr->attribute("style");
1437 if (style) {
1438 size_t const len = strlen(key);
1439 char const *p;
1440 while ( (p = strstr(style, key))
1441 != NULL )
1442 {
1443 p += len;
1444 while ((*p <= ' ') && *p) p++;
1445 if (*p++ != ':') break;
1446 while ((*p <= ' ') && *p) p++;
1447 size_t const inherit_len = sizeof("inherit") - 1;
1448 if (*p
1449 && !(strneq(p, "inherit", inherit_len)
1450 && (p[inherit_len] == '\0'
1451 || p[inherit_len] == ';'
1452 || g_ascii_isspace(p[inherit_len])))) {
1453 return p;
1454 }
1455 }
1456 }
1457 gchar const *val = object->repr->attribute(key);
1458 if (val && !streq(val, "inherit")) {
1459 return val;
1460 }
1461 if (object->parent) {
1462 return sp_object_get_style_property(object->parent, key, def);
1463 }
1465 return def;
1466 }
1468 /**
1469 * Lifts SVG version of all root objects to version.
1470 */
1471 void
1472 SPObject::_requireSVGVersion(Inkscape::Version version) {
1473 for ( SPObject::ParentIterator iter=this ; iter ; ++iter ) {
1474 SPObject *object=iter;
1475 if (SP_IS_ROOT(object)) {
1476 SPRoot *root=SP_ROOT(object);
1477 if ( root->version.svg < version ) {
1478 root->version.svg = version;
1479 }
1480 }
1481 }
1482 }
1484 /**
1485 * Return sodipodi version of first root ancestor or (0,0).
1486 */
1487 Inkscape::Version
1488 sp_object_get_sodipodi_version(SPObject *object)
1489 {
1490 static Inkscape::Version const zero_version(0, 0);
1492 while (object) {
1493 if (SP_IS_ROOT(object)) {
1494 return SP_ROOT(object)->version.sodipodi;
1495 }
1496 object = SP_OBJECT_PARENT(object);
1497 }
1499 return zero_version;
1500 }
1502 /**
1503 * Returns previous object in sibling list or NULL.
1504 */
1505 SPObject *
1506 sp_object_prev(SPObject *child)
1507 {
1508 SPObject *parent = SP_OBJECT_PARENT(child);
1509 for ( SPObject *i = sp_object_first_child(parent); i; i = SP_OBJECT_NEXT(i) ) {
1510 if (SP_OBJECT_NEXT(i) == child)
1511 return i;
1512 }
1513 return NULL;
1514 }
1517 /*
1518 Local Variables:
1519 mode:c++
1520 c-file-style:"stroustrup"
1521 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1522 indent-tabs-mode:nil
1523 fill-column:99
1524 End:
1525 */
1526 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :