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