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 */
34 #include <cstring>
35 #include <string>
37 #include "helper/sp-marshal.h"
38 #include "xml/node-event-vector.h"
39 #include "attributes.h"
40 #include "document.h"
41 #include "style.h"
42 #include "sp-object-repr.h"
43 #include "sp-root.h"
44 #include "streq.h"
45 #include "strneq.h"
46 #include "xml/repr.h"
47 #include "xml/node-fns.h"
48 #include "debug/event-tracker.h"
49 #include "debug/simple-event.h"
50 #include "debug/demangle.h"
51 #include "util/share.h"
52 #include "util/format.h"
54 #include "algorithms/longest-common-suffix.h"
55 using std::memcpy;
56 using std::strchr;
57 using std::strcmp;
58 using std::strlen;
59 using std::strstr;
61 #define noSP_OBJECT_DEBUG_CASCADE
63 #define noSP_OBJECT_DEBUG
65 #ifdef SP_OBJECT_DEBUG
66 # define debug(f, a...) { g_print("%s(%d) %s:", \
67 __FILE__,__LINE__,__FUNCTION__); \
68 g_print(f, ## a); \
69 g_print("\n"); \
70 }
71 #else
72 # define debug(f, a...) /**/
73 #endif
75 static void sp_object_class_init(SPObjectClass *klass);
76 static void sp_object_init(SPObject *object);
77 static void sp_object_finalize(GObject *object);
79 static void sp_object_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref);
80 static void sp_object_remove_child(SPObject *object, Inkscape::XML::Node *child);
81 static void sp_object_order_changed(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref, Inkscape::XML::Node *new_ref);
83 static void sp_object_release(SPObject *object);
84 static void sp_object_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
86 static void sp_object_private_set(SPObject *object, unsigned int key, gchar const *value);
87 static Inkscape::XML::Node *sp_object_private_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
89 /* Real handlers of repr signals */
91 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);
93 static void sp_object_repr_content_changed(Inkscape::XML::Node *repr, gchar const *oldcontent, gchar const *newcontent, gpointer data);
95 static void sp_object_repr_child_added(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, gpointer data);
96 static void sp_object_repr_child_removed(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, void *data);
98 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);
100 static gchar *sp_object_get_unique_id(SPObject *object, gchar const *defid);
102 guint update_in_progress = 0; // guard against update-during-update
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;
114 /**
115 * Registers the SPObject class with Gdk and returns its type number.
116 */
117 GType
118 sp_object_get_type(void)
119 {
120 static GType type = 0;
121 if (!type) {
122 GTypeInfo info = {
123 sizeof(SPObjectClass),
124 NULL, NULL,
125 (GClassInitFunc) sp_object_class_init,
126 NULL, NULL,
127 sizeof(SPObject),
128 16,
129 (GInstanceInitFunc) sp_object_init,
130 NULL
131 };
132 type = g_type_register_static(G_TYPE_OBJECT, "SPObject", &info, (GTypeFlags)0);
133 }
134 return type;
135 }
137 /**
138 * Initializes the SPObject vtable.
139 */
140 static void
141 sp_object_class_init(SPObjectClass *klass)
142 {
143 GObjectClass *object_class;
145 object_class = (GObjectClass *) klass;
147 parent_class = (GObjectClass *) g_type_class_ref(G_TYPE_OBJECT);
149 object_class->finalize = sp_object_finalize;
151 klass->child_added = sp_object_child_added;
152 klass->remove_child = sp_object_remove_child;
153 klass->order_changed = sp_object_order_changed;
155 klass->release = sp_object_release;
157 klass->build = sp_object_build;
159 klass->set = sp_object_private_set;
160 klass->write = sp_object_private_write;
161 }
163 /**
164 * Callback to initialize the SPObject object.
165 */
166 static void
167 sp_object_init(SPObject *object)
168 {
169 debug("id=%x, typename=%s",object, g_type_name_from_instance((GTypeInstance*)object));
171 object->hrefcount = 0;
172 object->_total_hrefcount = 0;
173 object->document = NULL;
174 object->children = object->_last_child = NULL;
175 object->parent = object->next = NULL;
176 object->repr = NULL;
177 object->id = NULL;
179 object->_collection_policy = SPObject::COLLECT_WITH_PARENT;
181 new (&object->_release_signal) sigc::signal<void, SPObject *>();
182 new (&object->_modified_signal) sigc::signal<void, SPObject *, unsigned int>();
183 new (&object->_delete_signal) sigc::signal<void, SPObject *>();
184 new (&object->_position_changed_signal) sigc::signal<void, SPObject *>();
185 object->_successor = NULL;
187 // FIXME: now we create style for all objects, but per SVG, only the following can have style attribute:
188 // vg, g, defs, desc, title, symbol, use, image, switch, path, rect, circle, ellipse, line, polyline,
189 // polygon, text, tspan, tref, textPath, altGlyph, glyphRef, marker, linearGradient, radialGradient,
190 // stop, pattern, clipPath, mask, filter, feImage, a, font, glyph, missing-glyph, foreignObject
191 object->style = sp_style_new_from_object(object);
193 object->_label = NULL;
194 object->_default_label = NULL;
195 }
197 /**
198 * Callback to destroy all members and connections of object and itself.
199 */
200 static void
201 sp_object_finalize(GObject *object)
202 {
203 SPObject *spobject = (SPObject *)object;
205 g_free(spobject->_label);
206 g_free(spobject->_default_label);
207 spobject->_label = NULL;
208 spobject->_default_label = NULL;
210 if (spobject->_successor) {
211 sp_object_unref(spobject->_successor, NULL);
212 spobject->_successor = NULL;
213 }
215 if (((GObjectClass *) (parent_class))->finalize) {
216 (* ((GObjectClass *) (parent_class))->finalize)(object);
217 }
219 spobject->_release_signal.~signal();
220 spobject->_modified_signal.~signal();
221 spobject->_delete_signal.~signal();
222 spobject->_position_changed_signal.~signal();
223 }
225 namespace {
227 namespace Debug = Inkscape::Debug;
228 namespace Util = Inkscape::Util;
230 typedef Debug::SimpleEvent<Debug::Event::REFCOUNT> BaseRefCountEvent;
232 class RefCountEvent : public BaseRefCountEvent {
233 public:
234 RefCountEvent(SPObject *object, int bias, Util::ptr_shared<char> name)
235 : BaseRefCountEvent(name)
236 {
237 _addProperty("object", Util::format("%p", object));
238 _addProperty("class", Debug::demangle(g_type_name(G_TYPE_FROM_INSTANCE(object))));
239 _addProperty("new-refcount", Util::format("%d", G_OBJECT(object)->ref_count + bias));
240 }
241 };
243 class RefEvent : public RefCountEvent {
244 public:
245 RefEvent(SPObject *object)
246 : RefCountEvent(object, 1, Util::share_static_string("sp-object-ref"))
247 {}
248 };
250 class UnrefEvent : public RefCountEvent {
251 public:
252 UnrefEvent(SPObject *object)
253 : RefCountEvent(object, -1, Util::share_static_string("sp-object-unref"))
254 {}
255 };
257 }
259 /**
260 * Increase reference count of object, with possible debugging.
261 *
262 * \param owner If non-NULL, make debug log entry.
263 * \return object, NULL is error.
264 * \pre object points to real object
265 */
266 SPObject *
267 sp_object_ref(SPObject *object, SPObject *owner)
268 {
269 g_return_val_if_fail(object != NULL, NULL);
270 g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
271 g_return_val_if_fail(!owner || SP_IS_OBJECT(owner), NULL);
273 Inkscape::Debug::EventTracker<RefEvent> tracker(object);
274 g_object_ref(G_OBJECT(object));
275 return object;
276 }
278 /**
279 * Decrease reference count of object, with possible debugging and
280 * finalization.
281 *
282 * \param owner If non-NULL, make debug log entry.
283 * \return always NULL
284 * \pre object points to real object
285 */
286 SPObject *
287 sp_object_unref(SPObject *object, SPObject *owner)
288 {
289 g_return_val_if_fail(object != NULL, NULL);
290 g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
291 g_return_val_if_fail(!owner || SP_IS_OBJECT(owner), NULL);
293 Inkscape::Debug::EventTracker<UnrefEvent> tracker(object);
294 g_object_unref(G_OBJECT(object));
295 return NULL;
296 }
298 /**
299 * Increase weak refcount.
300 *
301 * Hrefcount is used for weak references, for example, to
302 * determine whether any graphical element references a certain gradient
303 * node.
304 * \param owner Ignored.
305 * \return object, NULL is error
306 * \pre object points to real object
307 */
308 SPObject *
309 sp_object_href(SPObject *object, gpointer /*owner*/)
310 {
311 g_return_val_if_fail(object != NULL, NULL);
312 g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
314 object->hrefcount++;
315 object->_updateTotalHRefCount(1);
317 return object;
318 }
320 /**
321 * Decrease weak refcount.
322 *
323 * Hrefcount is used for weak references, for example, to determine whether
324 * any graphical element references a certain gradient node.
325 * \param owner Ignored.
326 * \return always NULL
327 * \pre object points to real object and hrefcount>0
328 */
329 SPObject *
330 sp_object_hunref(SPObject *object, gpointer /*owner*/)
331 {
332 g_return_val_if_fail(object != NULL, NULL);
333 g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
334 g_return_val_if_fail(object->hrefcount > 0, NULL);
336 object->hrefcount--;
337 object->_updateTotalHRefCount(-1);
339 return NULL;
340 }
342 /**
343 * Adds increment to _total_hrefcount of object and its parents.
344 */
345 void
346 SPObject::_updateTotalHRefCount(int increment) {
347 SPObject *topmost_collectable = NULL;
348 for ( SPObject *iter = this ; iter ; iter = SP_OBJECT_PARENT(iter) ) {
349 iter->_total_hrefcount += increment;
350 if ( iter->_total_hrefcount < iter->hrefcount ) {
351 g_critical("HRefs overcounted");
352 }
353 if ( iter->_total_hrefcount == 0 &&
354 iter->_collection_policy != COLLECT_WITH_PARENT )
355 {
356 topmost_collectable = iter;
357 }
358 }
359 if (topmost_collectable) {
360 topmost_collectable->requestOrphanCollection();
361 }
362 }
364 /**
365 * True if object is non-NULL and this is some in/direct parent of object.
366 */
367 bool
368 SPObject::isAncestorOf(SPObject const *object) const {
369 g_return_val_if_fail(object != NULL, false);
370 object = SP_OBJECT_PARENT(object);
371 while (object) {
372 if ( object == this ) {
373 return true;
374 }
375 object = SP_OBJECT_PARENT(object);
376 }
377 return false;
378 }
380 namespace {
382 bool same_objects(SPObject const &a, SPObject const &b) {
383 return &a == &b;
384 }
386 }
388 /**
389 * Returns youngest object being parent to this and object.
390 */
391 SPObject const *
392 SPObject::nearestCommonAncestor(SPObject const *object) const {
393 g_return_val_if_fail(object != NULL, NULL);
395 using Inkscape::Algorithms::longest_common_suffix;
396 return longest_common_suffix<SPObject::ConstParentIterator>(this, object, NULL, &same_objects);
397 }
399 SPObject const *AncestorSon(SPObject const *obj, SPObject const *ancestor) {
400 if (obj == NULL || ancestor == NULL)
401 return NULL;
402 if (SP_OBJECT_PARENT(obj) == ancestor)
403 return obj;
404 return AncestorSon(SP_OBJECT_PARENT(obj), ancestor);
405 }
407 /**
408 * Compares height of objects in tree.
409 *
410 * Works for different-parent objects, so long as they have a common ancestor.
411 * \return \verbatim
412 * 0 positions are equivalent
413 * 1 first object's position is greater than the second
414 * -1 first object's position is less than the second \endverbatim
415 */
416 int
417 sp_object_compare_position(SPObject const *first, SPObject const *second)
418 {
419 if (first == second) return 0;
421 SPObject const *ancestor = first->nearestCommonAncestor(second);
422 if (ancestor == NULL) return 0; // cannot compare, no common ancestor!
424 // we have an object and its ancestor (should not happen when sorting selection)
425 if (ancestor == first)
426 return 1;
427 if (ancestor == second)
428 return -1;
430 SPObject const *to_first = AncestorSon(first, ancestor);
431 SPObject const *to_second = AncestorSon(second, ancestor);
433 g_assert(SP_OBJECT_PARENT(to_second) == SP_OBJECT_PARENT(to_first));
435 return sp_repr_compare_position(SP_OBJECT_REPR(to_first), SP_OBJECT_REPR(to_second));
436 }
439 /**
440 * Append repr as child of this object.
441 * \pre this is not a cloned object
442 */
443 SPObject *
444 SPObject::appendChildRepr(Inkscape::XML::Node *repr) {
445 if (!SP_OBJECT_IS_CLONED(this)) {
446 SP_OBJECT_REPR(this)->appendChild(repr);
447 return SP_OBJECT_DOCUMENT(this)->getObjectByRepr(repr);
448 } else {
449 g_critical("Attempt to append repr as child of cloned object");
450 return NULL;
451 }
452 }
454 /**
455 * Retrieves the children as a GSList object, optionally ref'ing the children
456 * in the process, if add_ref is specified.
457 */
458 GSList *SPObject::childList(bool add_ref, Action) {
459 GSList *l = NULL;
460 for (SPObject *child = sp_object_first_child(this) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
461 if (add_ref)
462 g_object_ref (G_OBJECT (child));
464 l = g_slist_prepend (l, child);
465 }
466 return l;
468 }
470 /** Gets the label property for the object or a default if no label
471 * is defined.
472 */
473 gchar const *
474 SPObject::label() const {
475 return _label;
476 }
478 /** Returns a default label property for the object. */
479 gchar const *
480 SPObject::defaultLabel() const {
481 if (_label) {
482 return _label;
483 } else {
484 if (!_default_label) {
485 gchar const *id=SP_OBJECT_ID(this);
486 if (id) {
487 _default_label = g_strdup_printf("#%s", id);
488 } else {
489 _default_label = g_strdup_printf("<%s>", SP_OBJECT_REPR(this)->name());
490 }
491 }
492 return _default_label;
493 }
494 }
496 /** Sets the label property for the object */
497 void
498 SPObject::setLabel(gchar const *label) {
499 SP_OBJECT_REPR(this)->setAttribute("inkscape:label", label, false);
500 }
503 /** Queues the object for orphan collection */
504 void
505 SPObject::requestOrphanCollection() {
506 g_return_if_fail(document != NULL);
507 document->queueForOrphanCollection(this);
509 /** \todo
510 * This is a temporary hack added to make fill&stroke rebuild its
511 * gradient list when the defs are vacuumed. gradient-vector.cpp
512 * listens to the modified signal on defs, and now we give it that
513 * signal. Mental says that this should be made automatic by
514 * merging SPObjectGroup with SPObject; SPObjectGroup would issue
515 * this signal automatically. Or maybe just derive SPDefs from
516 * SPObjectGroup?
517 */
519 this->requestModified(SP_OBJECT_CHILD_MODIFIED_FLAG);
520 }
522 /** Sends the delete signal to all children of this object recursively */
523 void
524 SPObject::_sendDeleteSignalRecursive() {
525 for (SPObject *child = sp_object_first_child(this); child; child = SP_OBJECT_NEXT(child)) {
526 child->_delete_signal.emit(child);
527 child->_sendDeleteSignalRecursive();
528 }
529 }
531 /**
532 * Deletes the object reference, unparenting it from its parent.
533 *
534 * If the \a propagate parameter is set to true, it emits a delete
535 * signal. If the \a propagate_descendants parameter is true, it
536 * recursively sends the delete signal to children.
537 */
538 void
539 SPObject::deleteObject(bool propagate, bool propagate_descendants)
540 {
541 sp_object_ref(this, NULL);
542 if (propagate) {
543 _delete_signal.emit(this);
544 }
545 if (propagate_descendants) {
546 this->_sendDeleteSignalRecursive();
547 }
549 Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
550 if (repr && sp_repr_parent(repr)) {
551 sp_repr_unparent(repr);
552 }
554 if (_successor) {
555 _successor->deleteObject(propagate, propagate_descendants);
556 }
557 sp_object_unref(this, NULL);
558 }
560 /**
561 * Put object into object tree, under parent, and behind prev;
562 * also update object's XML space.
563 */
564 void
565 sp_object_attach(SPObject *parent, SPObject *object, SPObject *prev)
566 {
567 g_return_if_fail(parent != NULL);
568 g_return_if_fail(SP_IS_OBJECT(parent));
569 g_return_if_fail(object != NULL);
570 g_return_if_fail(SP_IS_OBJECT(object));
571 g_return_if_fail(!prev || SP_IS_OBJECT(prev));
572 g_return_if_fail(!prev || prev->parent == parent);
573 g_return_if_fail(!object->parent);
575 sp_object_ref(object, parent);
576 object->parent = parent;
577 parent->_updateTotalHRefCount(object->_total_hrefcount);
579 SPObject *next;
580 if (prev) {
581 next = prev->next;
582 prev->next = object;
583 } else {
584 next = parent->children;
585 parent->children = object;
586 }
587 object->next = next;
588 if (!next) {
589 parent->_last_child = object;
590 }
591 if (!object->xml_space.set)
592 object->xml_space.value = parent->xml_space.value;
593 }
595 /**
596 * In list of object's siblings, move object behind prev.
597 */
598 void
599 sp_object_reorder(SPObject *object, SPObject *prev) {
600 g_return_if_fail(object != NULL);
601 g_return_if_fail(SP_IS_OBJECT(object));
602 g_return_if_fail(object->parent != NULL);
603 g_return_if_fail(object != prev);
604 g_return_if_fail(!prev || SP_IS_OBJECT(prev));
605 g_return_if_fail(!prev || prev->parent == object->parent);
607 SPObject *const parent=object->parent;
609 SPObject *old_prev=NULL;
610 for ( SPObject *child = parent->children ; child && child != object ;
611 child = child->next )
612 {
613 old_prev = child;
614 }
616 SPObject *next=object->next;
617 if (old_prev) {
618 old_prev->next = next;
619 } else {
620 parent->children = next;
621 }
622 if (!next) {
623 parent->_last_child = old_prev;
624 }
625 if (prev) {
626 next = prev->next;
627 prev->next = object;
628 } else {
629 next = parent->children;
630 parent->children = object;
631 }
632 object->next = next;
633 if (!next) {
634 parent->_last_child = object;
635 }
636 }
638 /**
639 * Remove object from parent's children, release and unref it.
640 */
641 void
642 sp_object_detach(SPObject *parent, SPObject *object) {
643 g_return_if_fail(parent != NULL);
644 g_return_if_fail(SP_IS_OBJECT(parent));
645 g_return_if_fail(object != NULL);
646 g_return_if_fail(SP_IS_OBJECT(object));
647 g_return_if_fail(object->parent == parent);
649 object->releaseReferences();
651 SPObject *prev=NULL;
652 for ( SPObject *child = parent->children ; child && child != object ;
653 child = child->next )
654 {
655 prev = child;
656 }
658 SPObject *next=object->next;
659 if (prev) {
660 prev->next = next;
661 } else {
662 parent->children = next;
663 }
664 if (!next) {
665 parent->_last_child = prev;
666 }
668 object->next = NULL;
669 object->parent = NULL;
671 parent->_updateTotalHRefCount(-object->_total_hrefcount);
672 sp_object_unref(object, parent);
673 }
675 /**
676 * Return object's child whose node pointer equals repr.
677 */
678 SPObject *
679 sp_object_get_child_by_repr(SPObject *object, Inkscape::XML::Node *repr)
680 {
681 g_return_val_if_fail(object != NULL, NULL);
682 g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
683 g_return_val_if_fail(repr != NULL, NULL);
685 if (object->_last_child && SP_OBJECT_REPR(object->_last_child) == repr)
686 return object->_last_child; // optimization for common scenario
687 for ( SPObject *child = object->children ; child ; child = child->next ) {
688 if ( SP_OBJECT_REPR(child) == repr ) {
689 return child;
690 }
691 }
693 return NULL;
694 }
696 /**
697 * Callback for child_added event.
698 * Invoked whenever the given mutation event happens in the XML tree.
699 */
700 static void
701 sp_object_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
702 {
703 GType type = sp_repr_type_lookup(child);
704 if (!type) {
705 return;
706 }
707 SPObject *ochild = SP_OBJECT(g_object_new(type, 0));
708 SPObject *prev = ref ? sp_object_get_child_by_repr(object, ref) : NULL;
709 sp_object_attach(object, ochild, prev);
710 sp_object_unref(ochild, NULL);
712 sp_object_invoke_build(ochild, object->document, child, SP_OBJECT_IS_CLONED(object));
713 }
715 /**
716 * Removes, releases and unrefs all children of object.
717 *
718 * This is the opposite of build. It has to be invoked as soon as the
719 * object is removed from the tree, even if it is still alive according
720 * to reference count. The frontend unregisters the object from the
721 * document and releases the SPRepr bindings; implementations should free
722 * state data and release all child objects. Invoking release on
723 * SPRoot destroys the whole document tree.
724 * \see sp_object_build()
725 */
726 static void sp_object_release(SPObject *object)
727 {
728 debug("id=%x, typename=%s", object, g_type_name_from_instance((GTypeInstance*)object));
729 while (object->children) {
730 sp_object_detach(object, object->children);
731 }
732 }
734 /**
735 * Remove object's child whose node equals repr, release and
736 * unref it.
737 *
738 * Invoked whenever the given mutation event happens in the XML
739 * tree, BEFORE removal from the XML tree happens, so grouping
740 * objects can safely release the child data.
741 */
742 static void
743 sp_object_remove_child(SPObject *object, Inkscape::XML::Node *child)
744 {
745 debug("id=%x, typename=%s", object, g_type_name_from_instance((GTypeInstance*)object));
746 SPObject *ochild = sp_object_get_child_by_repr(object, child);
747 g_return_if_fail (ochild != NULL || !strcmp("comment", child->name())); // comments have no objects
748 if (ochild)
749 sp_object_detach(object, ochild);
750 }
752 /**
753 * Move object corresponding to child after sibling object corresponding
754 * to new_ref.
755 * Invoked whenever the given mutation event happens in the XML tree.
756 * \param old_ref Ignored
757 */
758 static void sp_object_order_changed(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node */*old_ref*/,
759 Inkscape::XML::Node *new_ref)
760 {
761 SPObject *ochild = sp_object_get_child_by_repr(object, child);
762 g_return_if_fail(ochild != NULL);
763 SPObject *prev = new_ref ? sp_object_get_child_by_repr(object, new_ref) : NULL;
764 sp_object_reorder(ochild, prev);
765 ochild->_position_changed_signal.emit(ochild);
766 }
768 /**
769 * Virtual build callback.
770 *
771 * This has to be invoked immediately after creation of an SPObject. The
772 * frontend method ensures that the new object is properly attached to
773 * the document and repr; implementation then will parse all of the attributes,
774 * generate the children objects and so on. Invoking build on the SPRoot
775 * object results in creation of the whole document tree (this is, what
776 * SPDocument does after the creation of the XML tree).
777 * \see sp_object_release()
778 */
779 static void
780 sp_object_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
781 {
782 /* Nothing specific here */
783 debug("id=%x, typename=%s", object, g_type_name_from_instance((GTypeInstance*)object));
785 sp_object_read_attr(object, "xml:space");
786 sp_object_read_attr(object, "inkscape:label");
787 sp_object_read_attr(object, "inkscape:collect");
789 for (Inkscape::XML::Node *rchild = repr->firstChild() ; rchild != NULL; rchild = rchild->next()) {
790 GType type = sp_repr_type_lookup(rchild);
791 if (!type) {
792 continue;
793 }
794 SPObject *child = SP_OBJECT(g_object_new(type, 0));
795 sp_object_attach(object, child, object->lastChild());
796 sp_object_unref(child, NULL);
797 sp_object_invoke_build(child, document, rchild, SP_OBJECT_IS_CLONED(object));
798 }
799 }
801 void
802 sp_object_invoke_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr, unsigned int cloned)
803 {
804 debug("id=%x, typename=%s", object, g_type_name_from_instance((GTypeInstance*)object));
806 g_assert(object != NULL);
807 g_assert(SP_IS_OBJECT(object));
808 g_assert(document != NULL);
809 g_assert(repr != NULL);
811 g_assert(object->document == NULL);
812 g_assert(object->repr == NULL);
813 g_assert(object->id == NULL);
815 /* Bookkeeping */
817 object->document = document;
818 object->repr = repr;
819 Inkscape::GC::anchor(repr);
820 object->cloned = cloned;
822 if (!SP_OBJECT_IS_CLONED(object)) {
823 object->document->bindObjectToRepr(object->repr, object);
825 if (Inkscape::XML::id_permitted(object->repr)) {
826 /* If we are not cloned, and not seeking, force unique id */
827 gchar const *id = object->repr->attribute("id");
828 if (!document->isSeeking()) {
829 gchar *realid = sp_object_get_unique_id(object, id);
830 g_assert(realid != NULL);
832 object->document->bindObjectToId(realid, object);
833 object->id = realid;
835 /* Redefine ID, if required */
836 if ((id == NULL) || (strcmp(id, realid) != 0)) {
837 object->repr->setAttribute("id", realid);
838 }
839 } else if (id) {
840 // bind if id, but no conflict -- otherwise, we can expect
841 // a subsequent setting of the id attribute
842 if (!object->document->getObjectById(id)) {
843 object->document->bindObjectToId(id, object);
844 object->id = g_strdup(id);
845 }
846 }
847 }
848 } else {
849 g_assert(object->id == NULL);
850 }
852 /* Invoke derived methods, if any */
853 if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->build) {
854 (*((SPObjectClass *) G_OBJECT_GET_CLASS(object))->build)(object, document, repr);
855 }
857 /* Signalling (should be connected AFTER processing derived methods */
858 sp_repr_add_listener(repr, &object_event_vector, object);
859 }
861 void SPObject::releaseReferences() {
862 g_assert(this->document);
863 g_assert(this->repr);
865 sp_repr_remove_listener_by_data(this->repr, this);
867 this->_release_signal.emit(this);
868 SPObjectClass *klass=(SPObjectClass *)G_OBJECT_GET_CLASS(this);
869 if (klass->release) {
870 klass->release(this);
871 }
873 /* all hrefs should be released by the "release" handlers */
874 g_assert(this->hrefcount == 0);
876 if (!SP_OBJECT_IS_CLONED(this)) {
877 if (this->id) {
878 this->document->bindObjectToId(this->id, NULL);
879 }
880 g_free(this->id);
881 this->id = NULL;
883 g_free(this->_default_label);
884 this->_default_label = NULL;
886 this->document->bindObjectToRepr(this->repr, NULL);
887 } else {
888 g_assert(!this->id);
889 }
891 if (this->style) {
892 this->style = sp_style_unref(this->style);
893 }
895 Inkscape::GC::release(this->repr);
897 this->document = NULL;
898 this->repr = NULL;
899 }
901 /**
902 * Callback for child_added node event.
903 */
904 static void
905 sp_object_repr_child_added(Inkscape::XML::Node */*repr*/, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, gpointer data)
906 {
907 SPObject *object = SP_OBJECT(data);
909 if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->child_added)
910 (*((SPObjectClass *)G_OBJECT_GET_CLASS(object))->child_added)(object, child, ref);
911 }
913 /**
914 * Callback for remove_child node event.
915 */
916 static void
917 sp_object_repr_child_removed(Inkscape::XML::Node */*repr*/, Inkscape::XML::Node *child, Inkscape::XML::Node */*ref*/, gpointer data)
918 {
919 SPObject *object = SP_OBJECT(data);
921 if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->remove_child) {
922 (* ((SPObjectClass *)G_OBJECT_GET_CLASS(object))->remove_child)(object, child);
923 }
924 }
926 /**
927 * Callback for order_changed node event.
928 *
929 * \todo fixme:
930 */
931 static void
932 sp_object_repr_order_changed(Inkscape::XML::Node */*repr*/, Inkscape::XML::Node *child, Inkscape::XML::Node *old, Inkscape::XML::Node *newer, gpointer data)
933 {
934 SPObject *object = SP_OBJECT(data);
936 if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->order_changed) {
937 (* ((SPObjectClass *)G_OBJECT_GET_CLASS(object))->order_changed)(object, child, old, newer);
938 }
939 }
941 /**
942 * Callback for set event.
943 */
944 static void
945 sp_object_private_set(SPObject *object, unsigned int key, gchar const *value)
946 {
947 g_assert(key != SP_ATTR_INVALID);
949 switch (key) {
950 case SP_ATTR_ID:
951 if ( !SP_OBJECT_IS_CLONED(object) && object->repr->type() == Inkscape::XML::ELEMENT_NODE ) {
952 SPDocument *document=object->document;
953 SPObject *conflict=NULL;
955 gchar const *new_id = value;
957 if (new_id) {
958 conflict = document->getObjectById((char const *)new_id);
959 }
961 if ( conflict && conflict != object ) {
962 if (!document->isSeeking()) {
963 sp_object_ref(conflict, NULL);
964 // give the conflicting object a new ID
965 gchar *new_conflict_id = sp_object_get_unique_id(conflict, NULL);
966 SP_OBJECT_REPR(conflict)->setAttribute("id", new_conflict_id);
967 g_free(new_conflict_id);
968 sp_object_unref(conflict, NULL);
969 } else {
970 new_id = NULL;
971 }
972 }
974 if (object->id) {
975 document->bindObjectToId(object->id, NULL);
976 g_free(object->id);
977 }
979 if (new_id) {
980 object->id = g_strdup((char const*)new_id);
981 document->bindObjectToId(object->id, object);
982 } else {
983 object->id = NULL;
984 }
986 g_free(object->_default_label);
987 object->_default_label = NULL;
988 }
989 break;
990 case SP_ATTR_INKSCAPE_LABEL:
991 g_free(object->_label);
992 if (value) {
993 object->_label = g_strdup(value);
994 } else {
995 object->_label = NULL;
996 }
997 g_free(object->_default_label);
998 object->_default_label = NULL;
999 break;
1000 case SP_ATTR_INKSCAPE_COLLECT:
1001 if ( value && !strcmp(value, "always") ) {
1002 object->setCollectionPolicy(SPObject::ALWAYS_COLLECT);
1003 } else {
1004 object->setCollectionPolicy(SPObject::COLLECT_WITH_PARENT);
1005 }
1006 break;
1007 case SP_ATTR_XML_SPACE:
1008 if (value && !strcmp(value, "preserve")) {
1009 object->xml_space.value = SP_XML_SPACE_PRESERVE;
1010 object->xml_space.set = TRUE;
1011 } else if (value && !strcmp(value, "default")) {
1012 object->xml_space.value = SP_XML_SPACE_DEFAULT;
1013 object->xml_space.set = TRUE;
1014 } else if (object->parent) {
1015 SPObject *parent;
1016 parent = object->parent;
1017 object->xml_space.value = parent->xml_space.value;
1018 }
1019 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
1020 break;
1021 case SP_ATTR_STYLE:
1022 sp_style_read_from_object(object->style, object);
1023 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
1024 break;
1025 default:
1026 break;
1027 }
1028 }
1030 /**
1031 * Call virtual set() function of object.
1032 */
1033 void
1034 sp_object_set(SPObject *object, unsigned int key, gchar const *value)
1035 {
1036 g_assert(object != NULL);
1037 g_assert(SP_IS_OBJECT(object));
1039 if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->set) {
1040 ((SPObjectClass *) G_OBJECT_GET_CLASS(object))->set(object, key, value);
1041 }
1042 }
1044 /**
1045 * Read value of key attribute from XML node into object.
1046 */
1047 void
1048 sp_object_read_attr(SPObject *object, gchar const *key)
1049 {
1050 g_assert(object != NULL);
1051 g_assert(SP_IS_OBJECT(object));
1052 g_assert(key != NULL);
1054 g_assert(object->repr != NULL);
1056 unsigned int keyid = sp_attribute_lookup(key);
1057 if (keyid != SP_ATTR_INVALID) {
1058 /* Retrieve the 'key' attribute from the object's XML representation */
1059 gchar const *value = object->repr->attribute(key);
1061 sp_object_set(object, keyid, value);
1062 }
1063 }
1065 /**
1066 * Callback for attr_changed node event.
1067 */
1068 static void
1069 sp_object_repr_attr_changed(Inkscape::XML::Node *repr, gchar const *key, gchar const */*oldval*/, gchar const */*newval*/, bool is_interactive, gpointer data)
1070 {
1071 SPObject *object = SP_OBJECT(data);
1073 sp_object_read_attr(object, key);
1075 // manual changes to extension attributes require the normal
1076 // attributes, which depend on them, to be updated immediately
1077 if (is_interactive) {
1078 object->updateRepr(repr, 0);
1079 }
1080 }
1082 /**
1083 * Callback for content_changed node event.
1084 */
1085 static void
1086 sp_object_repr_content_changed(Inkscape::XML::Node */*repr*/, gchar const */*oldcontent*/, gchar const */*newcontent*/, gpointer data)
1087 {
1088 SPObject *object = SP_OBJECT(data);
1090 if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->read_content)
1091 (*((SPObjectClass *) G_OBJECT_GET_CLASS(object))->read_content)(object);
1092 }
1094 /**
1095 * Return string representation of space value.
1096 */
1097 static gchar const*
1098 sp_xml_get_space_string(unsigned int space)
1099 {
1100 switch (space) {
1101 case SP_XML_SPACE_DEFAULT:
1102 return "default";
1103 case SP_XML_SPACE_PRESERVE:
1104 return "preserve";
1105 default:
1106 return NULL;
1107 }
1108 }
1110 /**
1111 * Callback for write event.
1112 */
1113 static Inkscape::XML::Node *
1114 sp_object_private_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
1115 {
1116 if (!repr && (flags & SP_OBJECT_WRITE_BUILD)) {
1117 repr = SP_OBJECT_REPR(object)->duplicate(NULL); // FIXME
1118 if (!( flags & SP_OBJECT_WRITE_EXT )) {
1119 repr->setAttribute("inkscape:collect", NULL);
1120 }
1121 } else {
1122 repr->setAttribute("id", object->id);
1124 if (object->xml_space.set) {
1125 char const *xml_space;
1126 xml_space = sp_xml_get_space_string(object->xml_space.value);
1127 repr->setAttribute("xml:space", xml_space);
1128 }
1130 if ( flags & SP_OBJECT_WRITE_EXT &&
1131 object->collectionPolicy() == SPObject::ALWAYS_COLLECT )
1132 {
1133 repr->setAttribute("inkscape:collect", "always");
1134 } else {
1135 repr->setAttribute("inkscape:collect", NULL);
1136 }
1138 SPStyle const *const obj_style = SP_OBJECT_STYLE(object);
1139 if (obj_style) {
1140 gchar *s = sp_style_write_string(obj_style, SP_STYLE_FLAG_IFSET);
1141 repr->setAttribute("style", ( *s ? s : NULL ));
1142 g_free(s);
1143 } else {
1144 /** \todo I'm not sure what to do in this case. Bug #1165868
1145 * suggests that it can arise, but the submitter doesn't know
1146 * how to do so reliably. The main two options are either
1147 * leave repr's style attribute unchanged, or explicitly clear it.
1148 * Must also consider what to do with property attributes for
1149 * the element; see below.
1150 */
1151 char const *style_str = repr->attribute("style");
1152 if (!style_str) {
1153 style_str = "NULL";
1154 }
1155 g_warning("Item's style is NULL; repr style attribute is %s", style_str);
1156 }
1158 /** \note We treat object->style as authoritative. Its effects have
1159 * been written to the style attribute above; any properties that are
1160 * unset we take to be deliberately unset (e.g. so that clones can
1161 * override the property).
1162 *
1163 * Note that the below has an undesirable consequence of changing the
1164 * appearance on renderers that lack CSS support (e.g. SVG tiny);
1165 * possibly we should write property attributes instead of a style
1166 * attribute.
1167 */
1168 sp_style_unset_property_attrs (object);
1169 }
1171 return repr;
1172 }
1174 /**
1175 * Update this object's XML node with flags value.
1176 */
1177 Inkscape::XML::Node *
1178 SPObject::updateRepr(unsigned int flags) {
1179 if (!SP_OBJECT_IS_CLONED(this)) {
1180 Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
1181 if (repr) {
1182 return updateRepr(repr, flags);
1183 } else {
1184 g_critical("Attempt to update non-existent repr");
1185 return NULL;
1186 }
1187 } else {
1188 /* cloned objects have no repr */
1189 return NULL;
1190 }
1191 }
1193 /** Used both to create reprs in the original document, and to create
1194 * reprs in another document (e.g. a temporary document used when
1195 * saving as "Plain SVG"
1196 */
1197 Inkscape::XML::Node *
1198 SPObject::updateRepr(Inkscape::XML::Node *repr, unsigned int flags) {
1199 if (SP_OBJECT_IS_CLONED(this)) {
1200 /* cloned objects have no repr */
1201 return NULL;
1202 }
1203 if (((SPObjectClass *) G_OBJECT_GET_CLASS(this))->write) {
1204 if (!(flags & SP_OBJECT_WRITE_BUILD) && !repr) {
1205 repr = SP_OBJECT_REPR(this);
1206 }
1207 return ((SPObjectClass *) G_OBJECT_GET_CLASS(this))->write(this, repr, flags);
1208 } else {
1209 g_warning("Class %s does not implement ::write", G_OBJECT_TYPE_NAME(this));
1210 if (!repr) {
1211 if (flags & SP_OBJECT_WRITE_BUILD) {
1212 /// \todo FIXME: Plumb an appropriate XML::Document into this
1213 repr = SP_OBJECT_REPR(this)->duplicate(NULL);
1214 }
1215 /// \todo FIXME: else probably error (Lauris) */
1216 } else {
1217 repr->mergeFrom(SP_OBJECT_REPR(this), "id");
1218 }
1219 return repr;
1220 }
1221 }
1223 /* Modification */
1225 /**
1226 * Add \a flags to \a object's as dirtiness flags, and
1227 * recursively add CHILD_MODIFIED flag to
1228 * parent and ancestors (as far up as necessary).
1229 */
1230 void
1231 SPObject::requestDisplayUpdate(unsigned int flags)
1232 {
1233 g_return_if_fail( this->document != NULL );
1235 if (update_in_progress) {
1236 g_print("WARNING: Requested update while update in progress, counter = %d\n", update_in_progress);
1237 }
1239 /* requestModified must be used only to set one of SP_OBJECT_MODIFIED_FLAG or
1240 * SP_OBJECT_CHILD_MODIFIED_FLAG */
1241 g_return_if_fail(!(flags & SP_OBJECT_PARENT_MODIFIED_FLAG));
1242 g_return_if_fail((flags & SP_OBJECT_MODIFIED_FLAG) || (flags & SP_OBJECT_CHILD_MODIFIED_FLAG));
1243 g_return_if_fail(!((flags & SP_OBJECT_MODIFIED_FLAG) && (flags & SP_OBJECT_CHILD_MODIFIED_FLAG)));
1245 bool already_propagated = (!(this->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG)));
1247 this->uflags |= flags;
1249 /* If requestModified has already been called on this object or one of its children, then we
1250 * don't need to set CHILD_MODIFIED on our ancestors because it's already been done.
1251 */
1252 if (already_propagated) {
1253 SPObject *parent = SP_OBJECT_PARENT(this);
1254 if (parent) {
1255 parent->requestDisplayUpdate(SP_OBJECT_CHILD_MODIFIED_FLAG);
1256 } else {
1257 sp_document_request_modified(SP_OBJECT_DOCUMENT(this));
1258 }
1259 }
1260 }
1262 /**
1263 * Update views
1264 */
1265 void
1266 SPObject::updateDisplay(SPCtx *ctx, unsigned int flags)
1267 {
1268 g_return_if_fail(!(flags & ~SP_OBJECT_MODIFIED_CASCADE));
1270 update_in_progress ++;
1272 #ifdef SP_OBJECT_DEBUG_CASCADE
1273 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);
1274 #endif
1276 /* Get this flags */
1277 flags |= this->uflags;
1278 /* Copy flags to modified cascade for later processing */
1279 this->mflags |= this->uflags;
1280 /* We have to clear flags here to allow rescheduling update */
1281 this->uflags = 0;
1283 // Merge style if we have good reasons to think that parent style is changed */
1284 /** \todo
1285 * I am not sure whether we should check only propagated
1286 * flag. We are currently assuming that style parsing is
1287 * done immediately. I think this is correct (Lauris).
1288 */
1289 if ((flags & SP_OBJECT_STYLE_MODIFIED_FLAG) && (flags & SP_OBJECT_PARENT_MODIFIED_FLAG)) {
1290 if (this->style && this->parent) {
1291 sp_style_merge_from_parent(this->style, this->parent->style);
1292 }
1293 }
1295 if (((SPObjectClass *) G_OBJECT_GET_CLASS(this))->update)
1296 ((SPObjectClass *) G_OBJECT_GET_CLASS(this))->update(this, ctx, flags);
1298 update_in_progress --;
1299 }
1301 /**
1302 * Request modified always bubbles *up* the tree, as opposed to
1303 * request display update, which trickles down and relies on the
1304 * flags set during this pass...
1305 */
1306 void
1307 SPObject::requestModified(unsigned int flags)
1308 {
1309 g_return_if_fail( this->document != NULL );
1311 /* requestModified must be used only to set one of SP_OBJECT_MODIFIED_FLAG or
1312 * SP_OBJECT_CHILD_MODIFIED_FLAG */
1313 g_return_if_fail(!(flags & SP_OBJECT_PARENT_MODIFIED_FLAG));
1314 g_return_if_fail((flags & SP_OBJECT_MODIFIED_FLAG) || (flags & SP_OBJECT_CHILD_MODIFIED_FLAG));
1315 g_return_if_fail(!((flags & SP_OBJECT_MODIFIED_FLAG) && (flags & SP_OBJECT_CHILD_MODIFIED_FLAG)));
1317 bool already_propagated = (!(this->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG)));
1319 this->mflags |= flags;
1321 /* If requestModified has already been called on this object or one of its children, then we
1322 * don't need to set CHILD_MODIFIED on our ancestors because it's already been done.
1323 */
1324 if (already_propagated) {
1325 SPObject *parent=SP_OBJECT_PARENT(this);
1326 if (parent) {
1327 parent->requestModified(SP_OBJECT_CHILD_MODIFIED_FLAG);
1328 } else {
1329 sp_document_request_modified(SP_OBJECT_DOCUMENT(this));
1330 }
1331 }
1332 }
1334 /**
1335 * Emits the MODIFIED signal with the object's flags.
1336 * The object's mflags are the original set aside during the update pass for
1337 * later delivery here. Once emitModified() is called, those flags don't
1338 * need to be stored any longer.
1339 */
1340 void
1341 SPObject::emitModified(unsigned int flags)
1342 {
1343 /* only the MODIFIED_CASCADE flag is legal here */
1344 g_return_if_fail(!(flags & ~SP_OBJECT_MODIFIED_CASCADE));
1346 #ifdef SP_OBJECT_DEBUG_CASCADE
1347 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);
1348 #endif
1350 flags |= this->mflags;
1351 /* We have to clear mflags beforehand, as signal handlers may
1352 * make changes and therefore queue new modification notifications
1353 * themselves. */
1354 this->mflags = 0;
1356 g_object_ref(G_OBJECT(this));
1357 SPObjectClass *klass=(SPObjectClass *)G_OBJECT_GET_CLASS(this);
1358 if (klass->modified) {
1359 klass->modified(this, flags);
1360 }
1361 _modified_signal.emit(this, flags);
1362 g_object_unref(G_OBJECT(this));
1363 }
1365 /*
1366 * Get and set descriptive parameters
1367 *
1368 * These are inefficent, so they are not intended to be used interactively
1369 */
1371 gchar const *
1372 sp_object_title_get(SPObject */*object*/)
1373 {
1374 return NULL;
1375 }
1377 gchar const *
1378 sp_object_description_get(SPObject */*object*/)
1379 {
1380 return NULL;
1381 }
1383 unsigned int
1384 sp_object_title_set(SPObject */*object*/, gchar const */*title*/)
1385 {
1386 return FALSE;
1387 }
1389 unsigned int
1390 sp_object_description_set(SPObject */*object*/, gchar const */*desc*/)
1391 {
1392 return FALSE;
1393 }
1395 gchar const *
1396 sp_object_tagName_get(SPObject const *object, SPException *ex)
1397 {
1398 /* If exception is not clear, return */
1399 if (!SP_EXCEPTION_IS_OK(ex)) {
1400 return NULL;
1401 }
1403 /// \todo fixme: Exception if object is NULL? */
1404 return object->repr->name();
1405 }
1407 gchar const *
1408 sp_object_getAttribute(SPObject const *object, gchar const *key, SPException *ex)
1409 {
1410 /* If exception is not clear, return */
1411 if (!SP_EXCEPTION_IS_OK(ex)) {
1412 return NULL;
1413 }
1415 /// \todo fixme: Exception if object is NULL? */
1416 return (gchar const *) object->repr->attribute(key);
1417 }
1419 void
1420 sp_object_setAttribute(SPObject *object, gchar const *key, gchar const *value, SPException *ex)
1421 {
1422 /* If exception is not clear, return */
1423 g_return_if_fail(SP_EXCEPTION_IS_OK(ex));
1425 /// \todo fixme: Exception if object is NULL? */
1426 if (!sp_repr_set_attr(object->repr, key, value)) {
1427 ex->code = SP_NO_MODIFICATION_ALLOWED_ERR;
1428 }
1429 }
1431 void
1432 sp_object_removeAttribute(SPObject *object, gchar const *key, SPException *ex)
1433 {
1434 /* If exception is not clear, return */
1435 g_return_if_fail(SP_EXCEPTION_IS_OK(ex));
1437 /// \todo fixme: Exception if object is NULL? */
1438 if (!sp_repr_set_attr(object->repr, key, NULL)) {
1439 ex->code = SP_NO_MODIFICATION_ALLOWED_ERR;
1440 }
1441 }
1443 /* Helper */
1445 static gchar *
1446 sp_object_get_unique_id(SPObject *object, gchar const *id)
1447 {
1448 static unsigned long count = 0;
1450 g_assert(SP_IS_OBJECT(object));
1452 count++;
1454 gchar const *name = object->repr->name();
1455 g_assert(name != NULL);
1457 gchar const *local = strchr(name, ':');
1458 if (local) {
1459 name = local + 1;
1460 }
1462 if (id != NULL) {
1463 if (object->document->getObjectById(id) == NULL) {
1464 return g_strdup(id);
1465 }
1466 }
1468 size_t const name_len = strlen(name);
1469 size_t const buflen = name_len + (sizeof(count) * 10 / 4) + 1;
1470 gchar *const buf = (gchar *) g_malloc(buflen);
1471 memcpy(buf, name, name_len);
1472 gchar *const count_buf = buf + name_len;
1473 size_t const count_buflen = buflen - name_len;
1474 do {
1475 ++count;
1476 g_snprintf(count_buf, count_buflen, "%lu", count);
1477 } while ( object->document->getObjectById(buf) != NULL );
1478 return buf;
1479 }
1481 /* Style */
1483 /**
1484 * Returns an object style property.
1485 *
1486 * \todo
1487 * fixme: Use proper CSS parsing. The current version is buggy
1488 * in a number of situations where key is a substring of the
1489 * style string other than as a property name (including
1490 * where key is a substring of a property name), and is also
1491 * buggy in its handling of inheritance for properties that
1492 * aren't inherited by default. It also doesn't allow for
1493 * the case where the property is specified but with an invalid
1494 * value (in which case I believe the CSS2 error-handling
1495 * behaviour applies, viz. behave as if the property hadn't
1496 * been specified). Also, the current code doesn't use CRSelEng
1497 * stuff to take a value from stylesheets. Also, we aren't
1498 * setting any hooks to force an update for changes in any of
1499 * the inputs (i.e., in any of the elements that this function
1500 * queries).
1501 *
1502 * \par
1503 * Given that the default value for a property depends on what
1504 * property it is (e.g., whether to inherit or not), and given
1505 * the above comment about ignoring invalid values, and that the
1506 * repr parent isn't necessarily the right element to inherit
1507 * from (e.g., maybe we need to inherit from the referencing
1508 * <use> element instead), we should probably make the caller
1509 * responsible for ascending the repr tree as necessary.
1510 */
1511 gchar const *
1512 sp_object_get_style_property(SPObject const *object, gchar const *key, gchar const *def)
1513 {
1514 g_return_val_if_fail(object != NULL, NULL);
1515 g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
1516 g_return_val_if_fail(key != NULL, NULL);
1518 gchar const *style = object->repr->attribute("style");
1519 if (style) {
1520 size_t const len = strlen(key);
1521 char const *p;
1522 while ( (p = strstr(style, key))
1523 != NULL )
1524 {
1525 p += len;
1526 while ((*p <= ' ') && *p) p++;
1527 if (*p++ != ':') break;
1528 while ((*p <= ' ') && *p) p++;
1529 size_t const inherit_len = sizeof("inherit") - 1;
1530 if (*p
1531 && !(strneq(p, "inherit", inherit_len)
1532 && (p[inherit_len] == '\0'
1533 || p[inherit_len] == ';'
1534 || g_ascii_isspace(p[inherit_len])))) {
1535 return p;
1536 }
1537 }
1538 }
1539 gchar const *val = object->repr->attribute(key);
1540 if (val && !streq(val, "inherit")) {
1541 return val;
1542 }
1543 if (object->parent) {
1544 return sp_object_get_style_property(object->parent, key, def);
1545 }
1547 return def;
1548 }
1550 /**
1551 * Lifts SVG version of all root objects to version.
1552 */
1553 void
1554 SPObject::_requireSVGVersion(Inkscape::Version version) {
1555 for ( SPObject::ParentIterator iter=this ; iter ; ++iter ) {
1556 SPObject *object=iter;
1557 if (SP_IS_ROOT(object)) {
1558 SPRoot *root=SP_ROOT(object);
1559 if ( root->version.svg < version ) {
1560 root->version.svg = version;
1561 }
1562 }
1563 }
1564 }
1566 /**
1567 * Return sodipodi version of first root ancestor or (0,0).
1568 */
1569 Inkscape::Version
1570 sp_object_get_sodipodi_version(SPObject *object)
1571 {
1572 static Inkscape::Version const zero_version(0, 0);
1574 while (object) {
1575 if (SP_IS_ROOT(object)) {
1576 return SP_ROOT(object)->version.sodipodi;
1577 }
1578 object = SP_OBJECT_PARENT(object);
1579 }
1581 return zero_version;
1582 }
1584 /**
1585 * Returns previous object in sibling list or NULL.
1586 */
1587 SPObject *
1588 sp_object_prev(SPObject *child)
1589 {
1590 SPObject *parent = SP_OBJECT_PARENT(child);
1591 for ( SPObject *i = sp_object_first_child(parent); i; i = SP_OBJECT_NEXT(i) ) {
1592 if (SP_OBJECT_NEXT(i) == child)
1593 return i;
1594 }
1595 return NULL;
1596 }
1599 /*
1600 Local Variables:
1601 mode:c++
1602 c-file-style:"stroustrup"
1603 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1604 indent-tabs-mode:nil
1605 fill-column:99
1606 End:
1607 */
1608 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :