43611e9edeaed74adfeafd632bd12f24cb665a24
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->_release_signal) sigc::signal<void, SPObject *>();
199 new (&object->_modified_signal) sigc::signal<void, SPObject *, unsigned int>();
200 new (&object->_delete_signal) sigc::signal<void, SPObject *>();
201 new (&object->_position_changed_signal) sigc::signal<void, SPObject *>();
202 object->_successor = NULL;
204 object->_label = NULL;
205 object->_default_label = NULL;
206 }
208 /**
209 * Callback to destroy all members and connections of object and itself.
210 */
211 static void
212 sp_object_finalize(GObject *object)
213 {
214 SPObject *spobject = (SPObject *)object;
216 g_free(spobject->_label);
217 g_free(spobject->_default_label);
218 spobject->_label = NULL;
219 spobject->_default_label = NULL;
221 if (spobject->_successor) {
222 sp_object_unref(spobject->_successor, NULL);
223 spobject->_successor = NULL;
224 }
226 if (((GObjectClass *) (parent_class))->finalize) {
227 (* ((GObjectClass *) (parent_class))->finalize)(object);
228 }
230 spobject->_release_signal.~signal();
231 spobject->_modified_signal.~signal();
232 spobject->_delete_signal.~signal();
233 spobject->_position_changed_signal.~signal();
234 }
236 namespace {
238 namespace Debug = Inkscape::Debug;
239 namespace Util = Inkscape::Util;
241 typedef Debug::SimpleEvent<Debug::Event::REFCOUNT> BaseRefCountEvent;
243 class RefCountEvent : public BaseRefCountEvent {
244 public:
245 RefCountEvent(SPObject *object, int bias, Util::ptr_shared<char> name)
246 : BaseRefCountEvent(name)
247 {
248 _addProperty("object", Util::format("%p", object));
249 _addProperty("class", Debug::demangle(g_type_name(G_TYPE_FROM_INSTANCE(object))));
250 _addProperty("new-refcount", Util::format("%d", G_OBJECT(object)->ref_count + bias));
251 }
252 };
254 class RefEvent : public RefCountEvent {
255 public:
256 RefEvent(SPObject *object)
257 : RefCountEvent(object, 1, Util::share_static_string("sp-object-ref"))
258 {}
259 };
261 class UnrefEvent : public RefCountEvent {
262 public:
263 UnrefEvent(SPObject *object)
264 : RefCountEvent(object, -1, Util::share_static_string("sp-object-unref"))
265 {}
266 };
268 }
270 /**
271 * Increase reference count of object, with possible debugging.
272 *
273 * \param owner If non-NULL, make debug log entry.
274 * \return object, NULL is error.
275 * \pre object points to real object
276 */
277 SPObject *
278 sp_object_ref(SPObject *object, SPObject *owner)
279 {
280 g_return_val_if_fail(object != NULL, NULL);
281 g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
282 g_return_val_if_fail(!owner || SP_IS_OBJECT(owner), NULL);
284 Inkscape::Debug::EventTracker<RefEvent> tracker(object);
285 g_object_ref(G_OBJECT(object));
286 return object;
287 }
289 /**
290 * Decrease reference count of object, with possible debugging and
291 * finalization.
292 *
293 * \param owner If non-NULL, make debug log entry.
294 * \return always NULL
295 * \pre object points to real object
296 */
297 SPObject *
298 sp_object_unref(SPObject *object, SPObject *owner)
299 {
300 g_return_val_if_fail(object != NULL, NULL);
301 g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
302 g_return_val_if_fail(!owner || SP_IS_OBJECT(owner), NULL);
304 Inkscape::Debug::EventTracker<UnrefEvent> tracker(object);
305 g_object_unref(G_OBJECT(object));
306 return NULL;
307 }
309 /**
310 * Increase weak refcount.
311 *
312 * Hrefcount is used for weak references, for example, to
313 * determine whether any graphical element references a certain gradient
314 * node.
315 * \param owner Ignored.
316 * \return object, NULL is error
317 * \pre object points to real object
318 */
319 SPObject *
320 sp_object_href(SPObject *object, gpointer owner)
321 {
322 g_return_val_if_fail(object != NULL, NULL);
323 g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
325 object->hrefcount++;
326 object->_updateTotalHRefCount(1);
328 return object;
329 }
331 /**
332 * Decrease weak refcount.
333 *
334 * Hrefcount is used for weak references, for example, to determine whether
335 * any graphical element references a certain gradient node.
336 * \param owner Ignored.
337 * \return always NULL
338 * \pre object points to real object and hrefcount>0
339 */
340 SPObject *
341 sp_object_hunref(SPObject *object, gpointer owner)
342 {
343 g_return_val_if_fail(object != NULL, NULL);
344 g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
345 g_return_val_if_fail(object->hrefcount > 0, NULL);
347 object->hrefcount--;
348 object->_updateTotalHRefCount(-1);
350 return NULL;
351 }
353 /**
354 * Adds increment to _total_hrefcount of object and its parents.
355 */
356 void
357 SPObject::_updateTotalHRefCount(int increment) {
358 SPObject *topmost_collectable = NULL;
359 for ( SPObject *iter = this ; iter ; iter = SP_OBJECT_PARENT(iter) ) {
360 iter->_total_hrefcount += increment;
361 if ( iter->_total_hrefcount < iter->hrefcount ) {
362 g_critical("HRefs overcounted");
363 }
364 if ( iter->_total_hrefcount == 0 &&
365 iter->_collection_policy != COLLECT_WITH_PARENT )
366 {
367 topmost_collectable = iter;
368 }
369 }
370 if (topmost_collectable) {
371 topmost_collectable->requestOrphanCollection();
372 }
373 }
375 /**
376 * True if object is non-NULL and this is some in/direct parent of object.
377 */
378 bool
379 SPObject::isAncestorOf(SPObject const *object) const {
380 g_return_val_if_fail(object != NULL, false);
381 object = SP_OBJECT_PARENT(object);
382 while (object) {
383 if ( object == this ) {
384 return true;
385 }
386 object = SP_OBJECT_PARENT(object);
387 }
388 return false;
389 }
391 namespace {
393 bool same_objects(SPObject const &a, SPObject const &b) {
394 return &a == &b;
395 }
397 }
399 /**
400 * Returns youngest object being parent to this and object.
401 */
402 SPObject const *
403 SPObject::nearestCommonAncestor(SPObject const *object) const {
404 g_return_val_if_fail(object != NULL, NULL);
406 using Inkscape::Algorithms::longest_common_suffix;
407 return longest_common_suffix<SPObject::ConstParentIterator>(this, object, NULL, &same_objects);
408 }
410 SPObject const *AncestorSon(SPObject const *obj, SPObject const *ancestor) {
411 if (obj == NULL || ancestor == NULL)
412 return NULL;
413 if (SP_OBJECT_PARENT(obj) == ancestor)
414 return obj;
415 return AncestorSon(SP_OBJECT_PARENT(obj), ancestor);
416 }
418 /**
419 * Compares height of objects in tree.
420 *
421 * Works for different-parent objects, so long as they have a common ancestor.
422 * \return \verbatim
423 * 0 positions are equivalent
424 * 1 first object's position is greater than the second
425 * -1 first object's position is less than the second \endverbatim
426 */
427 int
428 sp_object_compare_position(SPObject const *first, SPObject const *second)
429 {
430 if (first == second) return 0;
432 SPObject const *ancestor = first->nearestCommonAncestor(second);
433 if (ancestor == NULL) return 0; // cannot compare, no common ancestor!
435 // we have an object and its ancestor (should not happen when sorting selection)
436 if (ancestor == first)
437 return 1;
438 if (ancestor == second)
439 return -1;
441 SPObject const *to_first = AncestorSon(first, ancestor);
442 SPObject const *to_second = AncestorSon(second, ancestor);
444 g_assert(SP_OBJECT_PARENT(to_second) == SP_OBJECT_PARENT(to_first));
446 return sp_repr_compare_position(SP_OBJECT_REPR(to_first), SP_OBJECT_REPR(to_second));
447 }
450 /**
451 * Append repr as child of this object.
452 * \pre this is not a cloned object
453 */
454 SPObject *
455 SPObject::appendChildRepr(Inkscape::XML::Node *repr) {
456 if (!SP_OBJECT_IS_CLONED(this)) {
457 SP_OBJECT_REPR(this)->appendChild(repr);
458 return SP_OBJECT_DOCUMENT(this)->getObjectByRepr(repr);
459 } else {
460 g_critical("Attempt to append repr as child of cloned object");
461 return NULL;
462 }
463 }
465 /**
466 * Retrieves the children as a GSList object, optionally ref'ing the children
467 * in the process, if add_ref is specified.
468 */
469 GSList *SPObject::childList(bool add_ref, Action) {
470 GSList *l = NULL;
471 for (SPObject *child = sp_object_first_child(this) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
472 if (add_ref)
473 g_object_ref (G_OBJECT (child));
475 l = g_slist_prepend (l, child);
476 }
477 return l;
479 }
481 /** Gets the label property for the object or a default if no label
482 * is defined.
483 */
484 gchar const *
485 SPObject::label() const {
486 return _label;
487 }
489 /** Returns a default label property for the object. */
490 gchar const *
491 SPObject::defaultLabel() const {
492 if (_label) {
493 return _label;
494 } else {
495 if (!_default_label) {
496 gchar const *id=SP_OBJECT_ID(this);
497 if (id) {
498 _default_label = g_strdup_printf("#%s", id);
499 } else {
500 _default_label = g_strdup_printf("<%s>", SP_OBJECT_REPR(this)->name());
501 }
502 }
503 return _default_label;
504 }
505 }
507 /** Sets the label property for the object */
508 void
509 SPObject::setLabel(gchar const *label) {
510 SP_OBJECT_REPR(this)->setAttribute("inkscape:label", label, false);
511 }
514 /** Queues the object for orphan collection */
515 void
516 SPObject::requestOrphanCollection() {
517 g_return_if_fail(document != NULL);
518 document->queueForOrphanCollection(this);
520 /** \todo
521 * This is a temporary hack added to make fill&stroke rebuild its
522 * gradient list when the defs are vacuumed. gradient-vector.cpp
523 * listens to the modified signal on defs, and now we give it that
524 * signal. Mental says that this should be made automatic by
525 * merging SPObjectGroup with SPObject; SPObjectGroup would issue
526 * this signal automatically. Or maybe just derive SPDefs from
527 * SPObjectGroup?
528 */
530 this->requestModified(SP_OBJECT_CHILD_MODIFIED_FLAG);
531 }
533 /** Sends the delete signal to all children of this object recursively */
534 void
535 SPObject::_sendDeleteSignalRecursive() {
536 for (SPObject *child = sp_object_first_child(this); child; child = SP_OBJECT_NEXT(child)) {
537 child->_delete_signal.emit(child);
538 child->_sendDeleteSignalRecursive();
539 }
540 }
542 /**
543 * Deletes the object reference, unparenting it from its parent.
544 *
545 * If the \a propagate parameter is set to true, it emits a delete
546 * signal. If the \a propagate_descendants parameter is true, it
547 * recursively sends the delete signal to children.
548 */
549 void
550 SPObject::deleteObject(bool propagate, bool propagate_descendants)
551 {
552 sp_object_ref(this, NULL);
553 if (propagate) {
554 _delete_signal.emit(this);
555 }
556 if (propagate_descendants) {
557 this->_sendDeleteSignalRecursive();
558 }
560 Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
561 if (repr && sp_repr_parent(repr)) {
562 sp_repr_unparent(repr);
563 }
565 if (_successor) {
566 _successor->deleteObject(propagate, propagate_descendants);
567 }
568 sp_object_unref(this, NULL);
569 }
571 /**
572 * Put object into object tree, under parent, and behind prev;
573 * also update object's XML space.
574 */
575 void
576 sp_object_attach(SPObject *parent, SPObject *object, SPObject *prev)
577 {
578 g_return_if_fail(parent != NULL);
579 g_return_if_fail(SP_IS_OBJECT(parent));
580 g_return_if_fail(object != NULL);
581 g_return_if_fail(SP_IS_OBJECT(object));
582 g_return_if_fail(!prev || SP_IS_OBJECT(prev));
583 g_return_if_fail(!prev || prev->parent == parent);
584 g_return_if_fail(!object->parent);
586 sp_object_ref(object, parent);
587 object->parent = parent;
588 parent->_updateTotalHRefCount(object->_total_hrefcount);
590 SPObject *next;
591 if (prev) {
592 next = prev->next;
593 prev->next = object;
594 } else {
595 next = parent->children;
596 parent->children = object;
597 }
598 object->next = next;
599 if (!next) {
600 parent->_last_child = object;
601 }
602 if (!object->xml_space.set)
603 object->xml_space.value = parent->xml_space.value;
604 }
606 /**
607 * In list of object's siblings, move object behind prev.
608 */
609 void
610 sp_object_reorder(SPObject *object, SPObject *prev) {
611 g_return_if_fail(object != NULL);
612 g_return_if_fail(SP_IS_OBJECT(object));
613 g_return_if_fail(object->parent != NULL);
614 g_return_if_fail(object != prev);
615 g_return_if_fail(!prev || SP_IS_OBJECT(prev));
616 g_return_if_fail(!prev || prev->parent == object->parent);
618 SPObject *const parent=object->parent;
620 SPObject *old_prev=NULL;
621 for ( SPObject *child = parent->children ; child && child != object ;
622 child = child->next )
623 {
624 old_prev = child;
625 }
627 SPObject *next=object->next;
628 if (old_prev) {
629 old_prev->next = next;
630 } else {
631 parent->children = next;
632 }
633 if (!next) {
634 parent->_last_child = old_prev;
635 }
636 if (prev) {
637 next = prev->next;
638 prev->next = object;
639 } else {
640 next = parent->children;
641 parent->children = object;
642 }
643 object->next = next;
644 if (!next) {
645 parent->_last_child = object;
646 }
647 }
649 /**
650 * Remove object from parent's children, release and unref it.
651 */
652 void
653 sp_object_detach(SPObject *parent, SPObject *object) {
654 g_return_if_fail(parent != NULL);
655 g_return_if_fail(SP_IS_OBJECT(parent));
656 g_return_if_fail(object != NULL);
657 g_return_if_fail(SP_IS_OBJECT(object));
658 g_return_if_fail(object->parent == parent);
660 object->releaseReferences();
662 SPObject *prev=NULL;
663 for ( SPObject *child = parent->children ; child && child != object ;
664 child = child->next )
665 {
666 prev = child;
667 }
669 SPObject *next=object->next;
670 if (prev) {
671 prev->next = next;
672 } else {
673 parent->children = next;
674 }
675 if (!next) {
676 parent->_last_child = prev;
677 }
679 object->next = NULL;
680 object->parent = NULL;
682 parent->_updateTotalHRefCount(-object->_total_hrefcount);
683 sp_object_unref(object, parent);
684 }
686 /**
687 * Return object's child whose node pointer equals repr.
688 */
689 SPObject *
690 sp_object_get_child_by_repr(SPObject *object, Inkscape::XML::Node *repr)
691 {
692 g_return_val_if_fail(object != NULL, NULL);
693 g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
694 g_return_val_if_fail(repr != NULL, NULL);
696 if (object->_last_child && SP_OBJECT_REPR(object->_last_child) == repr)
697 return object->_last_child; // optimization for common scenario
698 for ( SPObject *child = object->children ; child ; child = child->next ) {
699 if ( SP_OBJECT_REPR(child) == repr ) {
700 return child;
701 }
702 }
704 return NULL;
705 }
707 /**
708 * Callback for child_added event.
709 * Invoked whenever the given mutation event happens in the XML tree.
710 */
711 static void
712 sp_object_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
713 {
714 GType type = sp_repr_type_lookup(child);
715 if (!type) {
716 return;
717 }
718 SPObject *ochild = SP_OBJECT(g_object_new(type, 0));
719 SPObject *prev = ref ? sp_object_get_child_by_repr(object, ref) : NULL;
720 sp_object_attach(object, ochild, prev);
721 sp_object_unref(ochild, NULL);
723 sp_object_invoke_build(ochild, object->document, child, SP_OBJECT_IS_CLONED(object));
724 }
726 /**
727 * Removes, releases and unrefs all children of object.
728 *
729 * This is the opposite of build. It has to be invoked as soon as the
730 * object is removed from the tree, even if it is still alive according
731 * to reference count. The frontend unregisters the object from the
732 * document and releases the SPRepr bindings; implementations should free
733 * state data and release all child objects. Invoking release on
734 * SPRoot destroys the whole document tree.
735 * \see sp_object_build()
736 */
737 static void sp_object_release(SPObject *object)
738 {
739 debug("id=%x, typename=%s", object, g_type_name_from_instance((GTypeInstance*)object));
740 while (object->children) {
741 sp_object_detach(object, object->children);
742 }
743 }
745 /**
746 * Remove object's child whose node equals repr, release and
747 * unref it.
748 *
749 * Invoked whenever the given mutation event happens in the XML
750 * tree, BEFORE removal from the XML tree happens, so grouping
751 * objects can safely release the child data.
752 */
753 static void
754 sp_object_remove_child(SPObject *object, Inkscape::XML::Node *child)
755 {
756 debug("id=%x, typename=%s", object, g_type_name_from_instance((GTypeInstance*)object));
757 SPObject *ochild = sp_object_get_child_by_repr(object, child);
758 g_return_if_fail(ochild != NULL);
759 sp_object_detach(object, ochild);
760 }
762 /**
763 * Move object corresponding to child after sibling object corresponding
764 * to new_ref.
765 * Invoked whenever the given mutation event happens in the XML tree.
766 * \param old_ref Ignored
767 */
768 static void sp_object_order_changed(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref,
769 Inkscape::XML::Node *new_ref)
770 {
771 SPObject *ochild = sp_object_get_child_by_repr(object, child);
772 g_return_if_fail(ochild != NULL);
773 SPObject *prev = new_ref ? sp_object_get_child_by_repr(object, new_ref) : NULL;
774 sp_object_reorder(ochild, prev);
775 ochild->_position_changed_signal.emit(ochild);
776 }
778 /**
779 * Virtual build callback.
780 *
781 * This has to be invoked immediately after creation of an SPObject. The
782 * frontend method ensures that the new object is properly attached to
783 * the document and repr; implementation then will parse all of the attributes,
784 * generate the children objects and so on. Invoking build on the SPRoot
785 * object results in creation of the whole document tree (this is, what
786 * SPDocument does after the creation of the XML tree).
787 * \see sp_object_release()
788 */
789 static void
790 sp_object_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
791 {
792 /* Nothing specific here */
793 debug("id=%x, typename=%s", object, g_type_name_from_instance((GTypeInstance*)object));
795 sp_object_read_attr(object, "xml:space");
796 sp_object_read_attr(object, "inkscape:label");
797 sp_object_read_attr(object, "inkscape:collect");
799 for (Inkscape::XML::Node *rchild = repr->firstChild() ; rchild != NULL; rchild = rchild->next()) {
800 GType type = sp_repr_type_lookup(rchild);
801 if (!type) {
802 continue;
803 }
804 SPObject *child = SP_OBJECT(g_object_new(type, 0));
805 sp_object_attach(object, child, object->lastChild());
806 sp_object_unref(child, NULL);
807 sp_object_invoke_build(child, document, rchild, SP_OBJECT_IS_CLONED(object));
808 }
809 }
811 void
812 sp_object_invoke_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr, unsigned int cloned)
813 {
814 debug("id=%x, typename=%s", object, g_type_name_from_instance((GTypeInstance*)object));
816 g_assert(object != NULL);
817 g_assert(SP_IS_OBJECT(object));
818 g_assert(document != NULL);
819 g_assert(repr != NULL);
821 g_assert(object->document == NULL);
822 g_assert(object->repr == NULL);
823 g_assert(object->id == NULL);
825 /* Bookkeeping */
827 object->document = document;
828 object->repr = repr;
829 Inkscape::GC::anchor(repr);
830 object->cloned = cloned;
832 if (!SP_OBJECT_IS_CLONED(object)) {
833 object->document->bindObjectToRepr(object->repr, object);
835 if (Inkscape::XML::id_permitted(object->repr)) {
836 /* If we are not cloned, force unique id */
837 gchar const *id = object->repr->attribute("id");
838 gchar *realid = sp_object_get_unique_id(object, id);
839 g_assert(realid != NULL);
841 object->document->bindObjectToId(realid, object);
842 object->id = realid;
844 /* Redefine ID, if required */
845 if ((id == NULL) || (strcmp(id, realid) != 0)) {
846 gboolean undo_sensitive=sp_document_get_undo_sensitive(document);
847 sp_document_set_undo_sensitive(document, FALSE);
848 object->repr->setAttribute("id", realid);
849 sp_document_set_undo_sensitive(document, undo_sensitive);
850 }
851 }
852 } else {
853 g_assert(object->id == NULL);
854 }
856 /* Invoke derived methods, if any */
858 if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->build) {
859 (*((SPObjectClass *) G_OBJECT_GET_CLASS(object))->build)(object, document, repr);
860 }
862 /* Signalling (should be connected AFTER processing derived methods */
863 sp_repr_add_listener(repr, &object_event_vector, object);
864 }
866 void SPObject::releaseReferences() {
867 g_assert(this->document);
868 g_assert(this->repr);
870 sp_repr_remove_listener_by_data(this->repr, this);
872 g_signal_emit(G_OBJECT(this), object_signals[RELEASE], 0);
873 this->_release_signal.emit(this);
875 /* all hrefs should be released by the "release" handlers */
876 g_assert(this->hrefcount == 0);
878 if (!SP_OBJECT_IS_CLONED(this)) {
879 if (this->id) {
880 this->document->bindObjectToId(this->id, NULL);
881 }
882 g_free(this->id);
883 this->id = NULL;
885 g_free(this->_default_label);
886 this->_default_label = NULL;
888 this->document->bindObjectToRepr(this->repr, NULL);
889 } else {
890 g_assert(!this->id);
891 }
893 if (this->style) {
894 this->style = sp_style_unref(this->style);
895 }
897 Inkscape::GC::release(this->repr);
899 this->document = NULL;
900 this->repr = NULL;
901 }
903 /**
904 * Callback for child_added node event.
905 */
906 static void
907 sp_object_repr_child_added(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, gpointer data)
908 {
909 SPObject *object = SP_OBJECT(data);
911 if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->child_added)
912 (*((SPObjectClass *)G_OBJECT_GET_CLASS(object))->child_added)(object, child, ref);
913 }
915 /**
916 * Callback for remove_child node event.
917 */
918 static void
919 sp_object_repr_child_removed(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, gpointer data)
920 {
921 SPObject *object = SP_OBJECT(data);
923 if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->remove_child) {
924 (* ((SPObjectClass *)G_OBJECT_GET_CLASS(object))->remove_child)(object, child);
925 }
926 }
928 /**
929 * Callback for order_changed node event.
930 *
931 * \todo fixme:
932 */
933 static void
934 sp_object_repr_order_changed(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *old, Inkscape::XML::Node *newer, gpointer data)
935 {
936 SPObject *object = SP_OBJECT(data);
938 if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->order_changed) {
939 (* ((SPObjectClass *)G_OBJECT_GET_CLASS(object))->order_changed)(object, child, old, newer);
940 }
941 }
943 /**
944 * Callback for set event.
945 */
946 static void
947 sp_object_private_set(SPObject *object, unsigned int key, gchar const *value)
948 {
949 g_assert(key != SP_ATTR_INVALID);
951 switch (key) {
952 case SP_ATTR_ID:
953 if ( !SP_OBJECT_IS_CLONED(object) && object->repr->type() == Inkscape::XML::ELEMENT_NODE ) {
954 SPDocument *document=object->document;
955 SPObject *conflict=NULL;
957 if (value) {
958 conflict = document->getObjectById((char const *)value);
959 }
960 if ( conflict && conflict != object ) {
961 sp_object_ref(conflict, NULL);
962 // give the conflicting object a new ID
963 gchar *new_conflict_id = sp_object_get_unique_id(conflict, NULL);
964 SP_OBJECT_REPR(conflict)->setAttribute("id", new_conflict_id);
965 g_free(new_conflict_id);
966 sp_object_unref(conflict, NULL);
967 }
969 if (object->id) {
970 document->bindObjectToId(object->id, NULL);
971 g_free(object->id);
972 }
974 if (value) {
975 object->id = g_strdup((char const*)value);
976 document->bindObjectToId(object->id, object);
977 } else {
978 object->id = NULL;
979 }
981 g_free(object->_default_label);
982 object->_default_label = NULL;
983 }
984 break;
985 case SP_ATTR_INKSCAPE_LABEL:
986 g_free(object->_label);
987 if (value) {
988 object->_label = g_strdup(value);
989 } else {
990 object->_label = NULL;
991 }
992 g_free(object->_default_label);
993 object->_default_label = NULL;
994 break;
995 case SP_ATTR_INKSCAPE_COLLECT:
996 if ( value && !strcmp(value, "always") ) {
997 object->setCollectionPolicy(SPObject::ALWAYS_COLLECT);
998 } else {
999 object->setCollectionPolicy(SPObject::COLLECT_WITH_PARENT);
1000 }
1001 break;
1002 case SP_ATTR_XML_SPACE:
1003 if (value && !strcmp(value, "preserve")) {
1004 object->xml_space.value = SP_XML_SPACE_PRESERVE;
1005 object->xml_space.set = TRUE;
1006 } else if (value && !strcmp(value, "default")) {
1007 object->xml_space.value = SP_XML_SPACE_DEFAULT;
1008 object->xml_space.set = TRUE;
1009 } else if (object->parent) {
1010 SPObject *parent;
1011 parent = object->parent;
1012 object->xml_space.value = parent->xml_space.value;
1013 }
1014 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
1015 break;
1016 default:
1017 break;
1018 }
1019 }
1021 /**
1022 * Call virtual set() function of object.
1023 */
1024 void
1025 sp_object_set(SPObject *object, unsigned int key, gchar const *value)
1026 {
1027 g_assert(object != NULL);
1028 g_assert(SP_IS_OBJECT(object));
1030 if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->set) {
1031 ((SPObjectClass *) G_OBJECT_GET_CLASS(object))->set(object, key, value);
1032 }
1033 }
1035 /**
1036 * Read value of key attribute from XML node into object.
1037 */
1038 void
1039 sp_object_read_attr(SPObject *object, gchar const *key)
1040 {
1041 g_assert(object != NULL);
1042 g_assert(SP_IS_OBJECT(object));
1043 g_assert(key != NULL);
1045 g_assert(object->repr != NULL);
1047 unsigned int keyid = sp_attribute_lookup(key);
1048 if (keyid != SP_ATTR_INVALID) {
1049 /* Retrieve the 'key' attribute from the object's XML representation */
1050 gchar const *value = object->repr->attribute(key);
1052 sp_object_set(object, keyid, value);
1053 }
1054 }
1056 /**
1057 * Callback for attr_changed node event.
1058 */
1059 static void
1060 sp_object_repr_attr_changed(Inkscape::XML::Node *repr, gchar const *key, gchar const *oldval, gchar const *newval, bool is_interactive, gpointer data)
1061 {
1062 SPObject *object = SP_OBJECT(data);
1064 sp_object_read_attr(object, key);
1066 // manual changes to extension attributes require the normal
1067 // attributes, which depend on them, to be updated immediately
1068 if (is_interactive) {
1069 object->updateRepr(repr, 0);
1070 }
1071 }
1073 /**
1074 * Callback for content_changed node event.
1075 */
1076 static void
1077 sp_object_repr_content_changed(Inkscape::XML::Node *repr, gchar const *oldcontent, gchar const *newcontent, gpointer data)
1078 {
1079 SPObject *object = SP_OBJECT(data);
1081 if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->read_content)
1082 (*((SPObjectClass *) G_OBJECT_GET_CLASS(object))->read_content)(object);
1083 }
1085 /**
1086 * Return string representation of space value.
1087 */
1088 static gchar const*
1089 sp_xml_get_space_string(unsigned int space)
1090 {
1091 switch (space) {
1092 case SP_XML_SPACE_DEFAULT:
1093 return "default";
1094 case SP_XML_SPACE_PRESERVE:
1095 return "preserve";
1096 default:
1097 return NULL;
1098 }
1099 }
1101 /**
1102 * Callback for write event.
1103 */
1104 static Inkscape::XML::Node *
1105 sp_object_private_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
1106 {
1107 if (!repr && (flags & SP_OBJECT_WRITE_BUILD)) {
1108 repr = SP_OBJECT_REPR(object)->duplicate();
1109 if (!( flags & SP_OBJECT_WRITE_EXT )) {
1110 repr->setAttribute("inkscape:collect", NULL);
1111 }
1112 } else {
1113 repr->setAttribute("id", object->id);
1115 if (object->xml_space.set) {
1116 char const *xml_space;
1117 xml_space = sp_xml_get_space_string(object->xml_space.value);
1118 repr->setAttribute("xml:space", xml_space);
1119 }
1121 if ( flags & SP_OBJECT_WRITE_EXT &&
1122 object->collectionPolicy() == SPObject::ALWAYS_COLLECT )
1123 {
1124 repr->setAttribute("inkscape:collect", "always");
1125 } else {
1126 repr->setAttribute("inkscape:collect", NULL);
1127 }
1128 }
1130 return repr;
1131 }
1133 /**
1134 * Update this object's XML node with flags value.
1135 */
1136 Inkscape::XML::Node *
1137 SPObject::updateRepr(unsigned int flags) {
1138 if (!SP_OBJECT_IS_CLONED(this)) {
1139 Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
1140 if (repr) {
1141 return updateRepr(repr, flags);
1142 } else {
1143 g_critical("Attempt to update non-existent repr");
1144 return NULL;
1145 }
1146 } else {
1147 /* cloned objects have no repr */
1148 return NULL;
1149 }
1150 }
1152 Inkscape::XML::Node *
1153 SPObject::updateRepr(Inkscape::XML::Node *repr, unsigned int flags) {
1154 if (SP_OBJECT_IS_CLONED(this)) {
1155 /* cloned objects have no repr */
1156 return NULL;
1157 }
1158 if (((SPObjectClass *) G_OBJECT_GET_CLASS(this))->write) {
1159 if (!(flags & SP_OBJECT_WRITE_BUILD) && !repr) {
1160 repr = SP_OBJECT_REPR(this);
1161 }
1162 return ((SPObjectClass *) G_OBJECT_GET_CLASS(this))->write(this, repr, flags);
1163 } else {
1164 g_warning("Class %s does not implement ::write", G_OBJECT_TYPE_NAME(this));
1165 if (!repr) {
1166 if (flags & SP_OBJECT_WRITE_BUILD) {
1167 repr = SP_OBJECT_REPR(this)->duplicate();
1168 }
1169 /// \todo fixme: else probably error (Lauris) */
1170 } else {
1171 repr->mergeFrom(SP_OBJECT_REPR(this), "id");
1172 }
1173 return repr;
1174 }
1175 }
1177 /* Modification */
1179 /**
1180 * Add \a flags to \a object's as dirtiness flags, and
1181 * recursively add CHILD_MODIFIED flag to
1182 * parent and ancestors (as far up as necessary).
1183 */
1184 void
1185 SPObject::requestDisplayUpdate(unsigned int flags)
1186 {
1187 g_return_if_fail( this->document != NULL );
1189 if (update_in_progress) {
1190 g_print("WARNING: Requested update while update in progress, counter = %d\n", update_in_progress);
1191 }
1193 /* requestModified must be used only to set one of SP_OBJECT_MODIFIED_FLAG or
1194 * SP_OBJECT_CHILD_MODIFIED_FLAG */
1195 g_return_if_fail(!(flags & SP_OBJECT_PARENT_MODIFIED_FLAG));
1196 g_return_if_fail((flags & SP_OBJECT_MODIFIED_FLAG) || (flags & SP_OBJECT_CHILD_MODIFIED_FLAG));
1197 g_return_if_fail(!((flags & SP_OBJECT_MODIFIED_FLAG) && (flags & SP_OBJECT_CHILD_MODIFIED_FLAG)));
1199 bool already_propagated = (!(this->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG)));
1201 this->uflags |= flags;
1203 /* If requestModified has already been called on this object or one of its children, then we
1204 * don't need to set CHILD_MODIFIED on our ancestors because it's already been done.
1205 */
1206 if (already_propagated) {
1207 SPObject *parent = SP_OBJECT_PARENT(this);
1208 if (parent) {
1209 parent->requestDisplayUpdate(SP_OBJECT_CHILD_MODIFIED_FLAG);
1210 } else {
1211 sp_document_request_modified(SP_OBJECT_DOCUMENT(this));
1212 }
1213 }
1214 }
1216 void
1217 SPObject::updateDisplay(SPCtx *ctx, unsigned int flags)
1218 {
1219 g_return_if_fail(!(flags & ~SP_OBJECT_MODIFIED_CASCADE));
1221 update_in_progress ++;
1223 #ifdef SP_OBJECT_DEBUG_CASCADE
1224 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);
1225 #endif
1227 /* Get this flags */
1228 flags |= this->uflags;
1229 /* Copy flags to modified cascade for later processing */
1230 this->mflags |= this->uflags;
1231 /* We have to clear flags here to allow rescheduling update */
1232 this->uflags = 0;
1234 // Merge style if we have good reasons to think that parent style is changed */
1235 /** \todo
1236 * I am not sure whether we should check only propagated
1237 * flag. We are currently assuming that style parsing is
1238 * done immediately. I think this is correct (Lauris).
1239 */
1240 if ((flags & SP_OBJECT_STYLE_MODIFIED_FLAG) && (flags & SP_OBJECT_PARENT_MODIFIED_FLAG)) {
1241 if (this->style && this->parent) {
1242 sp_style_merge_from_parent(this->style, this->parent->style);
1243 }
1244 }
1246 if (((SPObjectClass *) G_OBJECT_GET_CLASS(this))->update)
1247 ((SPObjectClass *) G_OBJECT_GET_CLASS(this))->update(this, ctx, flags);
1249 update_in_progress --;
1250 }
1252 /**
1253 * Request modified always bubbles *up* the tree, as opposed to
1254 * request display update, which trickles down and relies on the
1255 * flags set during this pass...
1256 */
1257 void
1258 SPObject::requestModified(unsigned int flags)
1259 {
1260 g_return_if_fail( this->document != NULL );
1262 /* requestModified must be used only to set one of SP_OBJECT_MODIFIED_FLAG or
1263 * SP_OBJECT_CHILD_MODIFIED_FLAG */
1264 g_return_if_fail(!(flags & SP_OBJECT_PARENT_MODIFIED_FLAG));
1265 g_return_if_fail((flags & SP_OBJECT_MODIFIED_FLAG) || (flags & SP_OBJECT_CHILD_MODIFIED_FLAG));
1266 g_return_if_fail(!((flags & SP_OBJECT_MODIFIED_FLAG) && (flags & SP_OBJECT_CHILD_MODIFIED_FLAG)));
1268 bool already_propagated = (!(this->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG)));
1270 this->mflags |= flags;
1272 /* If requestModified has already been called on this object or one of its children, then we
1273 * don't need to set CHILD_MODIFIED on our ancestors because it's already been done.
1274 */
1275 if (already_propagated) {
1276 SPObject *parent=SP_OBJECT_PARENT(this);
1277 if (parent) {
1278 parent->requestModified(SP_OBJECT_CHILD_MODIFIED_FLAG);
1279 } else {
1280 sp_document_request_modified(SP_OBJECT_DOCUMENT(this));
1281 }
1282 }
1283 }
1285 /**
1286 * This is what actually delivers the modified signals
1287 */
1288 void
1289 SPObject::emitModified(unsigned int flags)
1290 {
1291 /* only the MODIFIED_CASCADE flag is legal here */
1292 g_return_if_fail(!(flags & ~SP_OBJECT_MODIFIED_CASCADE));
1294 #ifdef SP_OBJECT_DEBUG_CASCADE
1295 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);
1296 #endif
1298 flags |= this->mflags;
1299 /* We have to clear mflags beforehand, as signal handlers may
1300 * make changes and therefore queue new modification notifications
1301 * themselves. */
1302 this->mflags = 0;
1304 g_object_ref(G_OBJECT(this));
1305 g_signal_emit(G_OBJECT(this), object_signals[MODIFIED], 0, flags);
1306 _modified_signal.emit(this, flags);
1307 g_object_unref(G_OBJECT(this));
1308 }
1310 /*
1311 * Get and set descriptive parameters
1312 *
1313 * These are inefficent, so they are not intended to be used interactively
1314 */
1316 gchar const *
1317 sp_object_title_get(SPObject *object)
1318 {
1319 return NULL;
1320 }
1322 gchar const *
1323 sp_object_description_get(SPObject *object)
1324 {
1325 return NULL;
1326 }
1328 unsigned int
1329 sp_object_title_set(SPObject *object, gchar const *title)
1330 {
1331 return FALSE;
1332 }
1334 unsigned int
1335 sp_object_description_set(SPObject *object, gchar const *desc)
1336 {
1337 return FALSE;
1338 }
1340 gchar const *
1341 sp_object_tagName_get(SPObject const *object, SPException *ex)
1342 {
1343 /* If exception is not clear, return */
1344 if (!SP_EXCEPTION_IS_OK(ex)) {
1345 return NULL;
1346 }
1348 /// \todo fixme: Exception if object is NULL? */
1349 return object->repr->name();
1350 }
1352 gchar const *
1353 sp_object_getAttribute(SPObject const *object, gchar const *key, SPException *ex)
1354 {
1355 /* If exception is not clear, return */
1356 if (!SP_EXCEPTION_IS_OK(ex)) {
1357 return NULL;
1358 }
1360 /// \todo fixme: Exception if object is NULL? */
1361 return (gchar const *) object->repr->attribute(key);
1362 }
1364 void
1365 sp_object_setAttribute(SPObject *object, gchar const *key, gchar const *value, SPException *ex)
1366 {
1367 /* If exception is not clear, return */
1368 g_return_if_fail(SP_EXCEPTION_IS_OK(ex));
1370 /// \todo fixme: Exception if object is NULL? */
1371 if (!sp_repr_set_attr(object->repr, key, value)) {
1372 ex->code = SP_NO_MODIFICATION_ALLOWED_ERR;
1373 }
1374 }
1376 void
1377 sp_object_removeAttribute(SPObject *object, gchar const *key, SPException *ex)
1378 {
1379 /* If exception is not clear, return */
1380 g_return_if_fail(SP_EXCEPTION_IS_OK(ex));
1382 /// \todo fixme: Exception if object is NULL? */
1383 if (!sp_repr_set_attr(object->repr, key, NULL)) {
1384 ex->code = SP_NO_MODIFICATION_ALLOWED_ERR;
1385 }
1386 }
1388 /* Helper */
1390 static gchar *
1391 sp_object_get_unique_id(SPObject *object, gchar const *id)
1392 {
1393 static unsigned long count = 0;
1395 g_assert(SP_IS_OBJECT(object));
1397 count++;
1399 gchar const *name = object->repr->name();
1400 g_assert(name != NULL);
1402 gchar const *local = strchr(name, ':');
1403 if (local) {
1404 name = local + 1;
1405 }
1407 if (id != NULL) {
1408 if (object->document->getObjectById(id) == NULL) {
1409 return g_strdup(id);
1410 }
1411 }
1413 size_t const name_len = strlen(name);
1414 size_t const buflen = name_len + (sizeof(count) * 10 / 4) + 1;
1415 gchar *const buf = (gchar *) g_malloc(buflen);
1416 memcpy(buf, name, name_len);
1417 gchar *const count_buf = buf + name_len;
1418 size_t const count_buflen = buflen - name_len;
1419 do {
1420 ++count;
1421 g_snprintf(count_buf, count_buflen, "%lu", count);
1422 } while ( object->document->getObjectById(buf) != NULL );
1423 return buf;
1424 }
1426 /* Style */
1428 /**
1429 * Returns an object style property.
1430 *
1431 * \todo
1432 * fixme: Use proper CSS parsing. The current version is buggy
1433 * in a number of situations where key is a substring of the
1434 * style string other than as a property name (including
1435 * where key is a substring of a property name), and is also
1436 * buggy in its handling of inheritance for properties that
1437 * aren't inherited by default. It also doesn't allow for
1438 * the case where the property is specified but with an invalid
1439 * value (in which case I believe the CSS2 error-handling
1440 * behaviour applies, viz. behave as if the property hadn't
1441 * been specified). Also, the current code doesn't use CRSelEng
1442 * stuff to take a value from stylesheets. Also, we aren't
1443 * setting any hooks to force an update for changes in any of
1444 * the inputs (i.e., in any of the elements that this function
1445 * queries).
1446 *
1447 * \par
1448 * Given that the default value for a property depends on what
1449 * property it is (e.g., whether to inherit or not), and given
1450 * the above comment about ignoring invalid values, and that the
1451 * repr parent isn't necessarily the right element to inherit
1452 * from (e.g., maybe we need to inherit from the referencing
1453 * <use> element instead), we should probably make the caller
1454 * responsible for ascending the repr tree as necessary.
1455 */
1456 gchar const *
1457 sp_object_get_style_property(SPObject const *object, gchar const *key, gchar const *def)
1458 {
1459 g_return_val_if_fail(object != NULL, NULL);
1460 g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
1461 g_return_val_if_fail(key != NULL, NULL);
1463 gchar const *style = object->repr->attribute("style");
1464 if (style) {
1465 size_t const len = strlen(key);
1466 char const *p;
1467 while ( (p = strstr(style, key))
1468 != NULL )
1469 {
1470 p += len;
1471 while ((*p <= ' ') && *p) p++;
1472 if (*p++ != ':') break;
1473 while ((*p <= ' ') && *p) p++;
1474 size_t const inherit_len = sizeof("inherit") - 1;
1475 if (*p
1476 && !(strneq(p, "inherit", inherit_len)
1477 && (p[inherit_len] == '\0'
1478 || p[inherit_len] == ';'
1479 || g_ascii_isspace(p[inherit_len])))) {
1480 return p;
1481 }
1482 }
1483 }
1484 gchar const *val = object->repr->attribute(key);
1485 if (val && !streq(val, "inherit")) {
1486 return val;
1487 }
1488 if (object->parent) {
1489 return sp_object_get_style_property(object->parent, key, def);
1490 }
1492 return def;
1493 }
1495 /**
1496 * Lifts SVG version of all root objects to version.
1497 */
1498 void
1499 SPObject::_requireSVGVersion(Inkscape::Version version) {
1500 for ( SPObject::ParentIterator iter=this ; iter ; ++iter ) {
1501 SPObject *object=iter;
1502 if (SP_IS_ROOT(object)) {
1503 SPRoot *root=SP_ROOT(object);
1504 if ( root->version.svg < version ) {
1505 root->version.svg = version;
1506 }
1507 }
1508 }
1509 }
1511 /**
1512 * Return sodipodi version of first root ancestor or (0,0).
1513 */
1514 Inkscape::Version
1515 sp_object_get_sodipodi_version(SPObject *object)
1516 {
1517 static Inkscape::Version const zero_version(0, 0);
1519 while (object) {
1520 if (SP_IS_ROOT(object)) {
1521 return SP_ROOT(object)->version.sodipodi;
1522 }
1523 object = SP_OBJECT_PARENT(object);
1524 }
1526 return zero_version;
1527 }
1529 /**
1530 * Returns previous object in sibling list or NULL.
1531 */
1532 SPObject *
1533 sp_object_prev(SPObject *child)
1534 {
1535 SPObject *parent = SP_OBJECT_PARENT(child);
1536 for ( SPObject *i = sp_object_first_child(parent); i; i = SP_OBJECT_NEXT(i) ) {
1537 if (SP_OBJECT_NEXT(i) == child)
1538 return i;
1539 }
1540 return NULL;
1541 }
1544 /*
1545 Local Variables:
1546 mode:c++
1547 c-file-style:"stroustrup"
1548 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1549 indent-tabs-mode:nil
1550 fill-column:99
1551 End:
1552 */
1553 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :