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