5c502c335953f738a196565174c403c70d768376
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 // FIXME: now we create style for all objects, but per SVG, only the following can have style attribute:
178 // vg, g, defs, desc, title, symbol, use, image, switch, path, rect, circle, ellipse, line, polyline,
179 // polygon, text, tspan, tref, textPath, altGlyph, glyphRef, marker, linearGradient, radialGradient,
180 // stop, pattern, clipPath, mask, filter, feImage, a, font, glyph, missing-glyph, foreignObject
181 object->style = sp_style_new_from_object(object);
183 object->_collection_policy = SPObject::COLLECT_WITH_PARENT;
185 new (&object->_release_signal) sigc::signal<void, SPObject *>();
186 new (&object->_modified_signal) sigc::signal<void, SPObject *, unsigned int>();
187 new (&object->_delete_signal) sigc::signal<void, SPObject *>();
188 new (&object->_position_changed_signal) sigc::signal<void, SPObject *>();
189 object->_successor = NULL;
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, force unique id */
825 gchar const *id = object->repr->attribute("id");
826 gchar *realid = sp_object_get_unique_id(object, id);
827 g_assert(realid != NULL);
829 object->document->bindObjectToId(realid, object);
830 object->id = realid;
832 /* Redefine ID, if required */
833 if ((id == NULL) || (strcmp(id, realid) != 0)) {
834 bool saved = sp_document_get_undo_sensitive(document);
835 sp_document_set_undo_sensitive(document, false);
836 object->repr->setAttribute("id", realid);
837 sp_document_set_undo_sensitive(document, saved);
838 }
839 }
840 } else {
841 g_assert(object->id == NULL);
842 }
844 /* Invoke derived methods, if any */
846 if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->build) {
847 (*((SPObjectClass *) G_OBJECT_GET_CLASS(object))->build)(object, document, repr);
848 }
850 /* Signalling (should be connected AFTER processing derived methods */
851 sp_repr_add_listener(repr, &object_event_vector, object);
852 }
854 void SPObject::releaseReferences() {
855 g_assert(this->document);
856 g_assert(this->repr);
858 sp_repr_remove_listener_by_data(this->repr, this);
860 this->_release_signal.emit(this);
861 SPObjectClass *klass=(SPObjectClass *)G_OBJECT_GET_CLASS(this);
862 if (klass->release) {
863 klass->release(this);
864 }
866 /* all hrefs should be released by the "release" handlers */
867 g_assert(this->hrefcount == 0);
869 if (!SP_OBJECT_IS_CLONED(this)) {
870 if (this->id) {
871 this->document->bindObjectToId(this->id, NULL);
872 }
873 g_free(this->id);
874 this->id = NULL;
876 g_free(this->_default_label);
877 this->_default_label = NULL;
879 this->document->bindObjectToRepr(this->repr, NULL);
880 } else {
881 g_assert(!this->id);
882 }
884 if (this->style) {
885 this->style = sp_style_unref(this->style);
886 }
888 Inkscape::GC::release(this->repr);
890 this->document = NULL;
891 this->repr = NULL;
892 }
894 /**
895 * Callback for child_added node event.
896 */
897 static void
898 sp_object_repr_child_added(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, gpointer data)
899 {
900 SPObject *object = SP_OBJECT(data);
902 if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->child_added)
903 (*((SPObjectClass *)G_OBJECT_GET_CLASS(object))->child_added)(object, child, ref);
904 }
906 /**
907 * Callback for remove_child node event.
908 */
909 static void
910 sp_object_repr_child_removed(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, gpointer data)
911 {
912 SPObject *object = SP_OBJECT(data);
914 if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->remove_child) {
915 (* ((SPObjectClass *)G_OBJECT_GET_CLASS(object))->remove_child)(object, child);
916 }
917 }
919 /**
920 * Callback for order_changed node event.
921 *
922 * \todo fixme:
923 */
924 static void
925 sp_object_repr_order_changed(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *old, Inkscape::XML::Node *newer, gpointer data)
926 {
927 SPObject *object = SP_OBJECT(data);
929 if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->order_changed) {
930 (* ((SPObjectClass *)G_OBJECT_GET_CLASS(object))->order_changed)(object, child, old, newer);
931 }
932 }
934 /**
935 * Callback for set event.
936 */
937 static void
938 sp_object_private_set(SPObject *object, unsigned int key, gchar const *value)
939 {
940 g_assert(key != SP_ATTR_INVALID);
942 switch (key) {
943 case SP_ATTR_ID:
944 if ( !SP_OBJECT_IS_CLONED(object) && object->repr->type() == Inkscape::XML::ELEMENT_NODE ) {
945 SPDocument *document=object->document;
946 SPObject *conflict=NULL;
948 if (value) {
949 conflict = document->getObjectById((char const *)value);
950 }
951 if ( conflict && conflict != object ) {
952 sp_object_ref(conflict, NULL);
953 // give the conflicting object a new ID
954 gchar *new_conflict_id = sp_object_get_unique_id(conflict, NULL);
955 SP_OBJECT_REPR(conflict)->setAttribute("id", new_conflict_id);
956 g_free(new_conflict_id);
957 sp_object_unref(conflict, NULL);
958 }
960 if (object->id) {
961 document->bindObjectToId(object->id, NULL);
962 g_free(object->id);
963 }
965 if (value) {
966 object->id = g_strdup((char const*)value);
967 document->bindObjectToId(object->id, object);
968 } else {
969 object->id = NULL;
970 }
972 g_free(object->_default_label);
973 object->_default_label = NULL;
974 }
975 break;
976 case SP_ATTR_INKSCAPE_LABEL:
977 g_free(object->_label);
978 if (value) {
979 object->_label = g_strdup(value);
980 } else {
981 object->_label = NULL;
982 }
983 g_free(object->_default_label);
984 object->_default_label = NULL;
985 break;
986 case SP_ATTR_INKSCAPE_COLLECT:
987 if ( value && !strcmp(value, "always") ) {
988 object->setCollectionPolicy(SPObject::ALWAYS_COLLECT);
989 } else {
990 object->setCollectionPolicy(SPObject::COLLECT_WITH_PARENT);
991 }
992 break;
993 case SP_ATTR_XML_SPACE:
994 if (value && !strcmp(value, "preserve")) {
995 object->xml_space.value = SP_XML_SPACE_PRESERVE;
996 object->xml_space.set = TRUE;
997 } else if (value && !strcmp(value, "default")) {
998 object->xml_space.value = SP_XML_SPACE_DEFAULT;
999 object->xml_space.set = TRUE;
1000 } else if (object->parent) {
1001 SPObject *parent;
1002 parent = object->parent;
1003 object->xml_space.value = parent->xml_space.value;
1004 }
1005 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
1006 break;
1007 case SP_ATTR_STYLE:
1008 sp_style_read_from_object(object->style, object);
1009 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
1010 break;
1011 default:
1012 break;
1013 }
1014 }
1016 /**
1017 * Call virtual set() function of object.
1018 */
1019 void
1020 sp_object_set(SPObject *object, unsigned int key, gchar const *value)
1021 {
1022 g_assert(object != NULL);
1023 g_assert(SP_IS_OBJECT(object));
1025 if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->set) {
1026 ((SPObjectClass *) G_OBJECT_GET_CLASS(object))->set(object, key, value);
1027 }
1028 }
1030 /**
1031 * Read value of key attribute from XML node into object.
1032 */
1033 void
1034 sp_object_read_attr(SPObject *object, gchar const *key)
1035 {
1036 g_assert(object != NULL);
1037 g_assert(SP_IS_OBJECT(object));
1038 g_assert(key != NULL);
1040 g_assert(object->repr != NULL);
1042 unsigned int keyid = sp_attribute_lookup(key);
1043 if (keyid != SP_ATTR_INVALID) {
1044 /* Retrieve the 'key' attribute from the object's XML representation */
1045 gchar const *value = object->repr->attribute(key);
1047 sp_object_set(object, keyid, value);
1048 }
1049 }
1051 /**
1052 * Callback for attr_changed node event.
1053 */
1054 static void
1055 sp_object_repr_attr_changed(Inkscape::XML::Node *repr, gchar const *key, gchar const *oldval, gchar const *newval, bool is_interactive, gpointer data)
1056 {
1057 SPObject *object = SP_OBJECT(data);
1059 sp_object_read_attr(object, key);
1061 // manual changes to extension attributes require the normal
1062 // attributes, which depend on them, to be updated immediately
1063 if (is_interactive) {
1064 object->updateRepr(repr, 0);
1065 }
1066 }
1068 /**
1069 * Callback for content_changed node event.
1070 */
1071 static void
1072 sp_object_repr_content_changed(Inkscape::XML::Node *repr, gchar const *oldcontent, gchar const *newcontent, gpointer data)
1073 {
1074 SPObject *object = SP_OBJECT(data);
1076 if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->read_content)
1077 (*((SPObjectClass *) G_OBJECT_GET_CLASS(object))->read_content)(object);
1078 }
1080 /**
1081 * Return string representation of space value.
1082 */
1083 static gchar const*
1084 sp_xml_get_space_string(unsigned int space)
1085 {
1086 switch (space) {
1087 case SP_XML_SPACE_DEFAULT:
1088 return "default";
1089 case SP_XML_SPACE_PRESERVE:
1090 return "preserve";
1091 default:
1092 return NULL;
1093 }
1094 }
1096 /**
1097 * Callback for write event.
1098 */
1099 static Inkscape::XML::Node *
1100 sp_object_private_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
1101 {
1102 if (!repr && (flags & SP_OBJECT_WRITE_BUILD)) {
1103 repr = SP_OBJECT_REPR(object)->duplicate();
1104 if (!( flags & SP_OBJECT_WRITE_EXT )) {
1105 repr->setAttribute("inkscape:collect", NULL);
1106 }
1107 } else {
1108 repr->setAttribute("id", object->id);
1110 if (object->xml_space.set) {
1111 char const *xml_space;
1112 xml_space = sp_xml_get_space_string(object->xml_space.value);
1113 repr->setAttribute("xml:space", xml_space);
1114 }
1116 if ( flags & SP_OBJECT_WRITE_EXT &&
1117 object->collectionPolicy() == SPObject::ALWAYS_COLLECT )
1118 {
1119 repr->setAttribute("inkscape:collect", "always");
1120 } else {
1121 repr->setAttribute("inkscape:collect", NULL);
1122 }
1124 SPStyle const *const obj_style = SP_OBJECT_STYLE(object);
1125 if (obj_style) {
1126 gchar *s = sp_style_write_string(obj_style, SP_STYLE_FLAG_IFSET);
1127 repr->setAttribute("style", ( *s ? s : NULL ));
1128 g_free(s);
1129 } else {
1130 /** \todo I'm not sure what to do in this case. Bug #1165868
1131 * suggests that it can arise, but the submitter doesn't know
1132 * how to do so reliably. The main two options are either
1133 * leave repr's style attribute unchanged, or explicitly clear it.
1134 * Must also consider what to do with property attributes for
1135 * the element; see below.
1136 */
1137 char const *style_str = repr->attribute("style");
1138 if (!style_str) {
1139 style_str = "NULL";
1140 }
1141 g_warning("Item's style is NULL; repr style attribute is %s", style_str);
1142 }
1144 /** \note We treat object->style as authoritative. Its effects have
1145 * been written to the style attribute above; any properties that are
1146 * unset we take to be deliberately unset (e.g. so that clones can
1147 * override the property).
1148 *
1149 * Note that the below has an undesirable consequence of changing the
1150 * appearance on renderers that lack CSS support (e.g. SVG tiny);
1151 * possibly we should write property attributes instead of a style
1152 * attribute.
1153 */
1154 sp_style_unset_property_attrs (object);
1155 }
1157 return repr;
1158 }
1160 /**
1161 * Update this object's XML node with flags value.
1162 */
1163 Inkscape::XML::Node *
1164 SPObject::updateRepr(unsigned int flags) {
1165 if (!SP_OBJECT_IS_CLONED(this)) {
1166 Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
1167 if (repr) {
1168 return updateRepr(repr, flags);
1169 } else {
1170 g_critical("Attempt to update non-existent repr");
1171 return NULL;
1172 }
1173 } else {
1174 /* cloned objects have no repr */
1175 return NULL;
1176 }
1177 }
1179 Inkscape::XML::Node *
1180 SPObject::updateRepr(Inkscape::XML::Node *repr, unsigned int flags) {
1181 if (SP_OBJECT_IS_CLONED(this)) {
1182 /* cloned objects have no repr */
1183 return NULL;
1184 }
1185 if (((SPObjectClass *) G_OBJECT_GET_CLASS(this))->write) {
1186 if (!(flags & SP_OBJECT_WRITE_BUILD) && !repr) {
1187 repr = SP_OBJECT_REPR(this);
1188 }
1189 return ((SPObjectClass *) G_OBJECT_GET_CLASS(this))->write(this, repr, flags);
1190 } else {
1191 g_warning("Class %s does not implement ::write", G_OBJECT_TYPE_NAME(this));
1192 if (!repr) {
1193 if (flags & SP_OBJECT_WRITE_BUILD) {
1194 repr = SP_OBJECT_REPR(this)->duplicate();
1195 }
1196 /// \todo fixme: else probably error (Lauris) */
1197 } else {
1198 repr->mergeFrom(SP_OBJECT_REPR(this), "id");
1199 }
1200 return repr;
1201 }
1202 }
1204 /* Modification */
1206 /**
1207 * Add \a flags to \a object's as dirtiness flags, and
1208 * recursively add CHILD_MODIFIED flag to
1209 * parent and ancestors (as far up as necessary).
1210 */
1211 void
1212 SPObject::requestDisplayUpdate(unsigned int flags)
1213 {
1214 g_return_if_fail( this->document != NULL );
1216 if (update_in_progress) {
1217 g_print("WARNING: Requested update while update in progress, counter = %d\n", update_in_progress);
1218 }
1220 /* requestModified must be used only to set one of SP_OBJECT_MODIFIED_FLAG or
1221 * SP_OBJECT_CHILD_MODIFIED_FLAG */
1222 g_return_if_fail(!(flags & SP_OBJECT_PARENT_MODIFIED_FLAG));
1223 g_return_if_fail((flags & SP_OBJECT_MODIFIED_FLAG) || (flags & SP_OBJECT_CHILD_MODIFIED_FLAG));
1224 g_return_if_fail(!((flags & SP_OBJECT_MODIFIED_FLAG) && (flags & SP_OBJECT_CHILD_MODIFIED_FLAG)));
1226 bool already_propagated = (!(this->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG)));
1228 this->uflags |= flags;
1230 /* If requestModified has already been called on this object or one of its children, then we
1231 * don't need to set CHILD_MODIFIED on our ancestors because it's already been done.
1232 */
1233 if (already_propagated) {
1234 SPObject *parent = SP_OBJECT_PARENT(this);
1235 if (parent) {
1236 parent->requestDisplayUpdate(SP_OBJECT_CHILD_MODIFIED_FLAG);
1237 } else {
1238 sp_document_request_modified(SP_OBJECT_DOCUMENT(this));
1239 }
1240 }
1241 }
1243 /**
1244 * Update views
1245 */
1246 void
1247 SPObject::updateDisplay(SPCtx *ctx, unsigned int flags)
1248 {
1249 g_return_if_fail(!(flags & ~SP_OBJECT_MODIFIED_CASCADE));
1251 update_in_progress ++;
1253 #ifdef SP_OBJECT_DEBUG_CASCADE
1254 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);
1255 #endif
1257 /* Get this flags */
1258 flags |= this->uflags;
1259 /* Copy flags to modified cascade for later processing */
1260 this->mflags |= this->uflags;
1261 /* We have to clear flags here to allow rescheduling update */
1262 this->uflags = 0;
1264 // Merge style if we have good reasons to think that parent style is changed */
1265 /** \todo
1266 * I am not sure whether we should check only propagated
1267 * flag. We are currently assuming that style parsing is
1268 * done immediately. I think this is correct (Lauris).
1269 */
1270 if ((flags & SP_OBJECT_STYLE_MODIFIED_FLAG) && (flags & SP_OBJECT_PARENT_MODIFIED_FLAG)) {
1271 if (this->style && this->parent) {
1272 sp_style_merge_from_parent(this->style, this->parent->style);
1273 }
1274 }
1276 if (((SPObjectClass *) G_OBJECT_GET_CLASS(this))->update)
1277 ((SPObjectClass *) G_OBJECT_GET_CLASS(this))->update(this, ctx, flags);
1279 update_in_progress --;
1280 }
1282 /**
1283 * Request modified always bubbles *up* the tree, as opposed to
1284 * request display update, which trickles down and relies on the
1285 * flags set during this pass...
1286 */
1287 void
1288 SPObject::requestModified(unsigned int flags)
1289 {
1290 g_return_if_fail( this->document != NULL );
1292 /* requestModified must be used only to set one of SP_OBJECT_MODIFIED_FLAG or
1293 * SP_OBJECT_CHILD_MODIFIED_FLAG */
1294 g_return_if_fail(!(flags & SP_OBJECT_PARENT_MODIFIED_FLAG));
1295 g_return_if_fail((flags & SP_OBJECT_MODIFIED_FLAG) || (flags & SP_OBJECT_CHILD_MODIFIED_FLAG));
1296 g_return_if_fail(!((flags & SP_OBJECT_MODIFIED_FLAG) && (flags & SP_OBJECT_CHILD_MODIFIED_FLAG)));
1298 bool already_propagated = (!(this->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG)));
1300 this->mflags |= flags;
1302 /* If requestModified has already been called on this object or one of its children, then we
1303 * don't need to set CHILD_MODIFIED on our ancestors because it's already been done.
1304 */
1305 if (already_propagated) {
1306 SPObject *parent=SP_OBJECT_PARENT(this);
1307 if (parent) {
1308 parent->requestModified(SP_OBJECT_CHILD_MODIFIED_FLAG);
1309 } else {
1310 sp_document_request_modified(SP_OBJECT_DOCUMENT(this));
1311 }
1312 }
1313 }
1315 /**
1316 * Emits the MODIFIED signal with the object's flags.
1317 * The object's mflags are the original set aside during the update pass for
1318 * later delivery here. Once emitModified() is called, those flags don't
1319 * need to be stored any longer.
1320 */
1321 void
1322 SPObject::emitModified(unsigned int flags)
1323 {
1324 /* only the MODIFIED_CASCADE flag is legal here */
1325 g_return_if_fail(!(flags & ~SP_OBJECT_MODIFIED_CASCADE));
1327 #ifdef SP_OBJECT_DEBUG_CASCADE
1328 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);
1329 #endif
1331 flags |= this->mflags;
1332 /* We have to clear mflags beforehand, as signal handlers may
1333 * make changes and therefore queue new modification notifications
1334 * themselves. */
1335 this->mflags = 0;
1337 g_object_ref(G_OBJECT(this));
1338 SPObjectClass *klass=(SPObjectClass *)G_OBJECT_GET_CLASS(this);
1339 if (klass->modified) {
1340 klass->modified(this, flags);
1341 }
1342 _modified_signal.emit(this, flags);
1343 g_object_unref(G_OBJECT(this));
1344 }
1346 /*
1347 * Get and set descriptive parameters
1348 *
1349 * These are inefficent, so they are not intended to be used interactively
1350 */
1352 gchar const *
1353 sp_object_title_get(SPObject *object)
1354 {
1355 return NULL;
1356 }
1358 gchar const *
1359 sp_object_description_get(SPObject *object)
1360 {
1361 return NULL;
1362 }
1364 unsigned int
1365 sp_object_title_set(SPObject *object, gchar const *title)
1366 {
1367 return FALSE;
1368 }
1370 unsigned int
1371 sp_object_description_set(SPObject *object, gchar const *desc)
1372 {
1373 return FALSE;
1374 }
1376 gchar const *
1377 sp_object_tagName_get(SPObject const *object, SPException *ex)
1378 {
1379 /* If exception is not clear, return */
1380 if (!SP_EXCEPTION_IS_OK(ex)) {
1381 return NULL;
1382 }
1384 /// \todo fixme: Exception if object is NULL? */
1385 return object->repr->name();
1386 }
1388 gchar const *
1389 sp_object_getAttribute(SPObject const *object, gchar const *key, SPException *ex)
1390 {
1391 /* If exception is not clear, return */
1392 if (!SP_EXCEPTION_IS_OK(ex)) {
1393 return NULL;
1394 }
1396 /// \todo fixme: Exception if object is NULL? */
1397 return (gchar const *) object->repr->attribute(key);
1398 }
1400 void
1401 sp_object_setAttribute(SPObject *object, gchar const *key, gchar const *value, SPException *ex)
1402 {
1403 /* If exception is not clear, return */
1404 g_return_if_fail(SP_EXCEPTION_IS_OK(ex));
1406 /// \todo fixme: Exception if object is NULL? */
1407 if (!sp_repr_set_attr(object->repr, key, value)) {
1408 ex->code = SP_NO_MODIFICATION_ALLOWED_ERR;
1409 }
1410 }
1412 void
1413 sp_object_removeAttribute(SPObject *object, gchar const *key, SPException *ex)
1414 {
1415 /* If exception is not clear, return */
1416 g_return_if_fail(SP_EXCEPTION_IS_OK(ex));
1418 /// \todo fixme: Exception if object is NULL? */
1419 if (!sp_repr_set_attr(object->repr, key, NULL)) {
1420 ex->code = SP_NO_MODIFICATION_ALLOWED_ERR;
1421 }
1422 }
1424 /* Helper */
1426 static gchar *
1427 sp_object_get_unique_id(SPObject *object, gchar const *id)
1428 {
1429 static unsigned long count = 0;
1431 g_assert(SP_IS_OBJECT(object));
1433 count++;
1435 gchar const *name = object->repr->name();
1436 g_assert(name != NULL);
1438 gchar const *local = strchr(name, ':');
1439 if (local) {
1440 name = local + 1;
1441 }
1443 if (id != NULL) {
1444 if (object->document->getObjectById(id) == NULL) {
1445 return g_strdup(id);
1446 }
1447 }
1449 size_t const name_len = strlen(name);
1450 size_t const buflen = name_len + (sizeof(count) * 10 / 4) + 1;
1451 gchar *const buf = (gchar *) g_malloc(buflen);
1452 memcpy(buf, name, name_len);
1453 gchar *const count_buf = buf + name_len;
1454 size_t const count_buflen = buflen - name_len;
1455 do {
1456 ++count;
1457 g_snprintf(count_buf, count_buflen, "%lu", count);
1458 } while ( object->document->getObjectById(buf) != NULL );
1459 return buf;
1460 }
1462 /* Style */
1464 /**
1465 * Returns an object style property.
1466 *
1467 * \todo
1468 * fixme: Use proper CSS parsing. The current version is buggy
1469 * in a number of situations where key is a substring of the
1470 * style string other than as a property name (including
1471 * where key is a substring of a property name), and is also
1472 * buggy in its handling of inheritance for properties that
1473 * aren't inherited by default. It also doesn't allow for
1474 * the case where the property is specified but with an invalid
1475 * value (in which case I believe the CSS2 error-handling
1476 * behaviour applies, viz. behave as if the property hadn't
1477 * been specified). Also, the current code doesn't use CRSelEng
1478 * stuff to take a value from stylesheets. Also, we aren't
1479 * setting any hooks to force an update for changes in any of
1480 * the inputs (i.e., in any of the elements that this function
1481 * queries).
1482 *
1483 * \par
1484 * Given that the default value for a property depends on what
1485 * property it is (e.g., whether to inherit or not), and given
1486 * the above comment about ignoring invalid values, and that the
1487 * repr parent isn't necessarily the right element to inherit
1488 * from (e.g., maybe we need to inherit from the referencing
1489 * <use> element instead), we should probably make the caller
1490 * responsible for ascending the repr tree as necessary.
1491 */
1492 gchar const *
1493 sp_object_get_style_property(SPObject const *object, gchar const *key, gchar const *def)
1494 {
1495 g_return_val_if_fail(object != NULL, NULL);
1496 g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
1497 g_return_val_if_fail(key != NULL, NULL);
1499 gchar const *style = object->repr->attribute("style");
1500 if (style) {
1501 size_t const len = strlen(key);
1502 char const *p;
1503 while ( (p = strstr(style, key))
1504 != NULL )
1505 {
1506 p += len;
1507 while ((*p <= ' ') && *p) p++;
1508 if (*p++ != ':') break;
1509 while ((*p <= ' ') && *p) p++;
1510 size_t const inherit_len = sizeof("inherit") - 1;
1511 if (*p
1512 && !(strneq(p, "inherit", inherit_len)
1513 && (p[inherit_len] == '\0'
1514 || p[inherit_len] == ';'
1515 || g_ascii_isspace(p[inherit_len])))) {
1516 return p;
1517 }
1518 }
1519 }
1520 gchar const *val = object->repr->attribute(key);
1521 if (val && !streq(val, "inherit")) {
1522 return val;
1523 }
1524 if (object->parent) {
1525 return sp_object_get_style_property(object->parent, key, def);
1526 }
1528 return def;
1529 }
1531 /**
1532 * Lifts SVG version of all root objects to version.
1533 */
1534 void
1535 SPObject::_requireSVGVersion(Inkscape::Version version) {
1536 for ( SPObject::ParentIterator iter=this ; iter ; ++iter ) {
1537 SPObject *object=iter;
1538 if (SP_IS_ROOT(object)) {
1539 SPRoot *root=SP_ROOT(object);
1540 if ( root->version.svg < version ) {
1541 root->version.svg = version;
1542 }
1543 }
1544 }
1545 }
1547 /**
1548 * Return sodipodi version of first root ancestor or (0,0).
1549 */
1550 Inkscape::Version
1551 sp_object_get_sodipodi_version(SPObject *object)
1552 {
1553 static Inkscape::Version const zero_version(0, 0);
1555 while (object) {
1556 if (SP_IS_ROOT(object)) {
1557 return SP_ROOT(object)->version.sodipodi;
1558 }
1559 object = SP_OBJECT_PARENT(object);
1560 }
1562 return zero_version;
1563 }
1565 /**
1566 * Returns previous object in sibling list or NULL.
1567 */
1568 SPObject *
1569 sp_object_prev(SPObject *child)
1570 {
1571 SPObject *parent = SP_OBJECT_PARENT(child);
1572 for ( SPObject *i = sp_object_first_child(parent); i; i = SP_OBJECT_NEXT(i) ) {
1573 if (SP_OBJECT_NEXT(i) == child)
1574 return i;
1575 }
1576 return NULL;
1577 }
1580 /*
1581 Local Variables:
1582 mode:c++
1583 c-file-style:"stroustrup"
1584 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1585 indent-tabs-mode:nil
1586 fill-column:99
1587 End:
1588 */
1589 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :