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