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 "util/share.h"
49 #include "util/format.h"
51 #include "algorithms/longest-common-suffix.h"
52 using std::memcpy;
53 using std::strchr;
54 using std::strcmp;
55 using std::strlen;
56 using std::strstr;
58 #define noSP_OBJECT_DEBUG_CASCADE
60 #define noSP_OBJECT_DEBUG
62 #ifdef SP_OBJECT_DEBUG
63 # define debug(f, a...) { g_print("%s(%d) %s:", \
64 __FILE__,__LINE__,__FUNCTION__); \
65 g_print(f, ## a); \
66 g_print("\n"); \
67 }
68 #else
69 # define debug(f, a...) /**/
70 #endif
72 static void sp_object_class_init(SPObjectClass *klass);
73 static void sp_object_init(SPObject *object);
74 static void sp_object_finalize(GObject *object);
76 static void sp_object_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref);
77 static void sp_object_remove_child(SPObject *object, Inkscape::XML::Node *child);
78 static void sp_object_order_changed(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref, Inkscape::XML::Node *new_ref);
80 static void sp_object_release(SPObject *object);
81 static void sp_object_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
83 static void sp_object_private_set(SPObject *object, unsigned int key, gchar const *value);
84 static Inkscape::XML::Node *sp_object_private_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
86 /* Real handlers of repr signals */
88 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);
90 static void sp_object_repr_content_changed(Inkscape::XML::Node *repr, gchar const *oldcontent, gchar const *newcontent, gpointer data);
92 static void sp_object_repr_child_added(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, gpointer data);
93 static void sp_object_repr_child_removed(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, void *data);
95 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);
97 static gchar *sp_object_get_unique_id(SPObject *object, gchar const *defid);
99 guint update_in_progress = 0; // guard against update-during-update
101 enum {RELEASE, MODIFIED, LAST_SIGNAL};
103 Inkscape::XML::NodeEventVector object_event_vector = {
104 sp_object_repr_child_added,
105 sp_object_repr_child_removed,
106 sp_object_repr_attr_changed,
107 sp_object_repr_content_changed,
108 sp_object_repr_order_changed
109 };
111 static GObjectClass *parent_class;
112 static guint object_signals[LAST_SIGNAL] = {0};
114 /**
115 * Registers the SPObject class with Gdk and returns its type number.
116 */
117 GType
118 sp_object_get_type(void)
119 {
120 static GType type = 0;
121 if (!type) {
122 GTypeInfo info = {
123 sizeof(SPObjectClass),
124 NULL, NULL,
125 (GClassInitFunc) sp_object_class_init,
126 NULL, NULL,
127 sizeof(SPObject),
128 16,
129 (GInstanceInitFunc) sp_object_init,
130 NULL
131 };
132 type = g_type_register_static(G_TYPE_OBJECT, "SPObject", &info, (GTypeFlags)0);
133 }
134 return type;
135 }
137 /**
138 * Initializes the SPObject vtable.
139 */
140 static void
141 sp_object_class_init(SPObjectClass *klass)
142 {
143 GObjectClass *object_class;
145 object_class = (GObjectClass *) klass;
147 parent_class = (GObjectClass *) g_type_class_ref(G_TYPE_OBJECT);
149 object_signals[RELEASE] = g_signal_new("release",
150 G_TYPE_FROM_CLASS(klass),
151 (GSignalFlags)(G_SIGNAL_RUN_CLEANUP | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS),
152 G_STRUCT_OFFSET(SPObjectClass, release),
153 NULL, NULL,
154 sp_marshal_VOID__VOID,
155 G_TYPE_NONE, 0);
156 object_signals[MODIFIED] = g_signal_new("modified",
157 G_TYPE_FROM_CLASS(klass),
158 G_SIGNAL_RUN_FIRST,
159 G_STRUCT_OFFSET(SPObjectClass, modified),
160 NULL, NULL,
161 sp_marshal_NONE__UINT,
162 G_TYPE_NONE, 1, G_TYPE_UINT);
164 object_class->finalize = sp_object_finalize;
166 klass->child_added = sp_object_child_added;
167 klass->remove_child = sp_object_remove_child;
168 klass->order_changed = sp_object_order_changed;
170 klass->release = sp_object_release;
172 klass->build = sp_object_build;
174 klass->set = sp_object_private_set;
175 klass->write = sp_object_private_write;
176 }
178 /**
179 * Callback to initialize the SPObject object.
180 */
181 static void
182 sp_object_init(SPObject *object)
183 {
184 debug("id=%x, typename=%s",object, g_type_name_from_instance((GTypeInstance*)object));
186 object->hrefcount = 0;
187 object->_total_hrefcount = 0;
188 object->document = NULL;
189 object->children = object->_last_child = NULL;
190 object->parent = object->next = NULL;
191 object->repr = NULL;
192 object->id = NULL;
193 object->style = NULL;
195 object->_collection_policy = SPObject::COLLECT_WITH_PARENT;
197 new (&object->_delete_signal) sigc::signal<void, SPObject *>();
198 new (&object->_position_changed_signal) sigc::signal<void, SPObject *>();
199 object->_successor = NULL;
201 object->_label = NULL;
202 object->_default_label = NULL;
203 }
205 /**
206 * Callback to destroy all members and connections of object and itself.
207 */
208 static void
209 sp_object_finalize(GObject *object)
210 {
211 SPObject *spobject = (SPObject *)object;
213 g_free(spobject->_label);
214 g_free(spobject->_default_label);
215 spobject->_label = NULL;
216 spobject->_default_label = NULL;
218 if (spobject->_successor) {
219 sp_object_unref(spobject->_successor, NULL);
220 spobject->_successor = NULL;
221 }
223 if (((GObjectClass *) (parent_class))->finalize) {
224 (* ((GObjectClass *) (parent_class))->finalize)(object);
225 }
227 spobject->_delete_signal.~signal();
228 spobject->_position_changed_signal.~signal();
229 }
231 namespace {
233 namespace Debug = Inkscape::Debug;
234 namespace Util = Inkscape::Util;
236 typedef Debug::SimpleEvent<Debug::Event::REFCOUNT> BaseRefCountEvent;
238 class RefCountEvent : public BaseRefCountEvent {
239 public:
240 RefCountEvent(SPObject *object, int bias, Util::ptr_shared<char> name)
241 : BaseRefCountEvent(name)
242 {
243 _addProperty("object", Util::format("%p", object));
244 _addProperty("class", Util::share_static_string(g_type_name(G_TYPE_FROM_INSTANCE(object))));
245 _addProperty("new-refcount", Util::format("%d", G_OBJECT(object)->ref_count + bias));
246 }
247 };
249 RefEvent(SPObject *object, Type type)
250 : _object(stringify(object)),
251 _class_name(Inkscape::Util::share_static_string(g_type_name(G_TYPE_FROM_INSTANCE(object)))),
252 _refcount(G_OBJECT(object)->ref_count),
253 _type(type)
254 {}
255 };
257 static Category category() { return REFCOUNT; }
259 Inkscape::Util::ptr_shared<char> name() const {
260 if ( _type == REF) {
261 return Inkscape::Util::share_static_string("sp-object-ref");
262 } else {
263 return Inkscape::Util::share_static_string("sp-object-unref");
264 }
265 }
266 unsigned propertyCount() const { return 3; }
267 PropertyPair property(unsigned index) const {
268 switch (index) {
269 case 0:
270 return PropertyPair("object", _object);
271 case 1:
272 return PropertyPair("class", _class_name);
273 case 2:
274 return PropertyPair("new-refcount", stringify( _type == REF ? _refcount + 1 : _refcount - 1 ));
275 default:
276 return PropertyPair();
277 }
278 }
280 private:
281 Inkscape::Util::ptr_shared<char> _object;
282 Inkscape::Util::ptr_shared<char> _class_name;
283 unsigned _refcount;
284 Type _type;
285 };
287 }
289 /**
290 * Increase reference count of object, with possible debugging.
291 *
292 * \param owner If non-NULL, make debug log entry.
293 * \return object, NULL is error.
294 * \pre object points to real object
295 */
296 SPObject *
297 sp_object_ref(SPObject *object, SPObject *owner)
298 {
299 g_return_val_if_fail(object != NULL, NULL);
300 g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
301 g_return_val_if_fail(!owner || SP_IS_OBJECT(owner), NULL);
303 Inkscape::Debug::EventTracker<RefEvent> tracker(object, RefEvent::REF);
304 g_object_ref(G_OBJECT(object));
305 return object;
306 }
308 /**
309 * Decrease reference count of object, with possible debugging and
310 * finalization.
311 *
312 * \param owner If non-NULL, make debug log entry.
313 * \return always NULL
314 * \pre object points to real object
315 */
316 SPObject *
317 sp_object_unref(SPObject *object, SPObject *owner)
318 {
319 g_return_val_if_fail(object != NULL, NULL);
320 g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
321 g_return_val_if_fail(!owner || SP_IS_OBJECT(owner), NULL);
323 Inkscape::Debug::EventTracker<RefEvent> tracker(object, RefEvent::UNREF);
324 g_object_unref(G_OBJECT(object));
325 return NULL;
326 }
328 /**
329 * Increase weak refcount.
330 *
331 * Hrefcount is used for weak references, for example, to
332 * determine whether any graphical element references a certain gradient
333 * node.
334 * \param owner Ignored.
335 * \return object, NULL is error
336 * \pre object points to real object
337 */
338 SPObject *
339 sp_object_href(SPObject *object, gpointer owner)
340 {
341 g_return_val_if_fail(object != NULL, NULL);
342 g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
344 object->hrefcount++;
345 object->_updateTotalHRefCount(1);
347 return object;
348 }
350 /**
351 * Decrease weak refcount.
352 *
353 * Hrefcount is used for weak references, for example, to determine whether
354 * any graphical element references a certain gradient node.
355 * \param owner Ignored.
356 * \return always NULL
357 * \pre object points to real object and hrefcount>0
358 */
359 SPObject *
360 sp_object_hunref(SPObject *object, gpointer owner)
361 {
362 g_return_val_if_fail(object != NULL, NULL);
363 g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
364 g_return_val_if_fail(object->hrefcount > 0, NULL);
366 object->hrefcount--;
367 object->_updateTotalHRefCount(-1);
369 return NULL;
370 }
372 /**
373 * Adds increment to _total_hrefcount of object and its parents.
374 */
375 void
376 SPObject::_updateTotalHRefCount(int increment) {
377 SPObject *topmost_collectable = NULL;
378 for ( SPObject *iter = this ; iter ; iter = SP_OBJECT_PARENT(iter) ) {
379 iter->_total_hrefcount += increment;
380 if ( iter->_total_hrefcount < iter->hrefcount ) {
381 g_critical("HRefs overcounted");
382 }
383 if ( iter->_total_hrefcount == 0 &&
384 iter->_collection_policy != COLLECT_WITH_PARENT )
385 {
386 topmost_collectable = iter;
387 }
388 }
389 if (topmost_collectable) {
390 topmost_collectable->requestOrphanCollection();
391 }
392 }
394 /**
395 * True if object is non-NULL and this is some in/direct parent of object.
396 */
397 bool
398 SPObject::isAncestorOf(SPObject const *object) const {
399 g_return_val_if_fail(object != NULL, false);
400 object = SP_OBJECT_PARENT(object);
401 while (object) {
402 if ( object == this ) {
403 return true;
404 }
405 object = SP_OBJECT_PARENT(object);
406 }
407 return false;
408 }
410 namespace {
412 bool same_objects(SPObject const &a, SPObject const &b) {
413 return &a == &b;
414 }
416 }
418 /**
419 * Returns youngest object being parent to this and object.
420 */
421 SPObject const *
422 SPObject::nearestCommonAncestor(SPObject const *object) const {
423 g_return_val_if_fail(object != NULL, NULL);
425 using Inkscape::Algorithms::longest_common_suffix;
426 return longest_common_suffix<SPObject::ConstParentIterator>(this, object, NULL, &same_objects);
427 }
429 SPObject const *AncestorSon(SPObject const *obj, SPObject const *ancestor) {
430 if (obj == NULL || ancestor == NULL)
431 return NULL;
432 if (SP_OBJECT_PARENT(obj) == ancestor)
433 return obj;
434 return AncestorSon(SP_OBJECT_PARENT(obj), ancestor);
435 }
437 /**
438 * Compares height of objects in tree.
439 *
440 * Works for different-parent objects, so long as they have a common ancestor.
441 * \return \verbatim
442 * 0 positions are equivalent
443 * 1 first object's position is greater than the second
444 * -1 first object's position is less than the second \endverbatim
445 */
446 int
447 sp_object_compare_position(SPObject const *first, SPObject const *second)
448 {
449 if (first == second) return 0;
451 SPObject const *ancestor = first->nearestCommonAncestor(second);
452 if (ancestor == NULL) return 0; // cannot compare, no common ancestor!
454 // we have an object and its ancestor (should not happen when sorting selection)
455 if (ancestor == first)
456 return 1;
457 if (ancestor == second)
458 return -1;
460 SPObject const *to_first = AncestorSon(first, ancestor);
461 SPObject const *to_second = AncestorSon(second, ancestor);
463 g_assert(SP_OBJECT_PARENT(to_second) == SP_OBJECT_PARENT(to_first));
465 return sp_repr_compare_position(SP_OBJECT_REPR(to_first), SP_OBJECT_REPR(to_second));
466 }
469 /**
470 * Append repr as child of this object.
471 * \pre this is not a cloned object
472 */
473 SPObject *
474 SPObject::appendChildRepr(Inkscape::XML::Node *repr) {
475 if (!SP_OBJECT_IS_CLONED(this)) {
476 SP_OBJECT_REPR(this)->appendChild(repr);
477 return SP_OBJECT_DOCUMENT(this)->getObjectByRepr(repr);
478 } else {
479 g_critical("Attempt to append repr as child of cloned object");
480 return NULL;
481 }
482 }
484 /** Gets the label property for the object or a default if no label
485 * is defined.
486 */
487 gchar const *
488 SPObject::label() const {
489 return _label;
490 }
492 /** Returns a default label property for the object. */
493 gchar const *
494 SPObject::defaultLabel() const {
495 if (_label) {
496 return _label;
497 } else {
498 if (!_default_label) {
499 gchar const *id=SP_OBJECT_ID(this);
500 if (id) {
501 _default_label = g_strdup_printf("#%s", id);
502 } else {
503 _default_label = g_strdup_printf("<%s>", SP_OBJECT_REPR(this)->name());
504 }
505 }
506 return _default_label;
507 }
508 }
510 /** Sets the label property for the object */
511 void
512 SPObject::setLabel(gchar const *label) {
513 SP_OBJECT_REPR(this)->setAttribute("inkscape:label", label, false);
514 }
517 /** Queues the object for orphan collection */
518 void
519 SPObject::requestOrphanCollection() {
520 g_return_if_fail(document != NULL);
521 document->queueForOrphanCollection(this);
523 /** \todo
524 * This is a temporary hack added to make fill&stroke rebuild its
525 * gradient list when the defs are vacuumed. gradient-vector.cpp
526 * listens to the modified signal on defs, and now we give it that
527 * signal. Mental says that this should be made automatic by
528 * merging SPObjectGroup with SPObject; SPObjectGroup would issue
529 * this signal automatically. Or maybe just derive SPDefs from
530 * SPObjectGroup?
531 */
533 this->requestModified(SP_OBJECT_CHILD_MODIFIED_FLAG);
534 }
536 /** Sends the delete signal to all children of this object recursively */
537 void
538 SPObject::_sendDeleteSignalRecursive() {
539 for (SPObject *child = sp_object_first_child(this); child; child = SP_OBJECT_NEXT(child)) {
540 child->_delete_signal.emit(child);
541 child->_sendDeleteSignalRecursive();
542 }
543 }
545 /**
546 * Deletes the object reference, unparenting it from its parent.
547 *
548 * If the \a propagate parameter is set to true, it emits a delete
549 * signal. If the \a propagate_descendants parameter is true, it
550 * recursively sends the delete signal to children.
551 */
552 void
553 SPObject::deleteObject(bool propagate, bool propagate_descendants)
554 {
555 sp_object_ref(this, NULL);
556 if (propagate) {
557 _delete_signal.emit(this);
558 }
559 if (propagate_descendants) {
560 this->_sendDeleteSignalRecursive();
561 }
563 Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
564 if (repr && sp_repr_parent(repr)) {
565 sp_repr_unparent(repr);
566 }
568 if (_successor) {
569 _successor->deleteObject(propagate, propagate_descendants);
570 }
571 sp_object_unref(this, NULL);
572 }
574 /**
575 * Put object into object tree, under parent, and behind prev;
576 * also update object's XML space.
577 */
578 void
579 sp_object_attach(SPObject *parent, SPObject *object, SPObject *prev)
580 {
581 g_return_if_fail(parent != NULL);
582 g_return_if_fail(SP_IS_OBJECT(parent));
583 g_return_if_fail(object != NULL);
584 g_return_if_fail(SP_IS_OBJECT(object));
585 g_return_if_fail(!prev || SP_IS_OBJECT(prev));
586 g_return_if_fail(!prev || prev->parent == parent);
587 g_return_if_fail(!object->parent);
589 sp_object_ref(object, parent);
590 object->parent = parent;
591 parent->_updateTotalHRefCount(object->_total_hrefcount);
593 SPObject *next;
594 if (prev) {
595 next = prev->next;
596 prev->next = object;
597 } else {
598 next = parent->children;
599 parent->children = object;
600 }
601 object->next = next;
602 if (!next) {
603 parent->_last_child = object;
604 }
605 if (!object->xml_space.set)
606 object->xml_space.value = parent->xml_space.value;
607 }
609 /**
610 * In list of object's siblings, move object behind prev.
611 */
612 void
613 sp_object_reorder(SPObject *object, SPObject *prev) {
614 g_return_if_fail(object != NULL);
615 g_return_if_fail(SP_IS_OBJECT(object));
616 g_return_if_fail(object->parent != NULL);
617 g_return_if_fail(object != prev);
618 g_return_if_fail(!prev || SP_IS_OBJECT(prev));
619 g_return_if_fail(!prev || prev->parent == object->parent);
621 SPObject *const parent=object->parent;
623 SPObject *old_prev=NULL;
624 for ( SPObject *child = parent->children ; child && child != object ;
625 child = child->next )
626 {
627 old_prev = child;
628 }
630 SPObject *next=object->next;
631 if (old_prev) {
632 old_prev->next = next;
633 } else {
634 parent->children = next;
635 }
636 if (!next) {
637 parent->_last_child = old_prev;
638 }
639 if (prev) {
640 next = prev->next;
641 prev->next = object;
642 } else {
643 next = parent->children;
644 parent->children = object;
645 }
646 object->next = next;
647 if (!next) {
648 parent->_last_child = object;
649 }
650 }
652 /**
653 * Remove object from parent's children, release and unref it.
654 */
655 void
656 sp_object_detach(SPObject *parent, SPObject *object) {
657 g_return_if_fail(parent != NULL);
658 g_return_if_fail(SP_IS_OBJECT(parent));
659 g_return_if_fail(object != NULL);
660 g_return_if_fail(SP_IS_OBJECT(object));
661 g_return_if_fail(object->parent == parent);
663 sp_object_invoke_release(object);
665 SPObject *prev=NULL;
666 for ( SPObject *child = parent->children ; child && child != object ;
667 child = child->next )
668 {
669 prev = child;
670 }
672 SPObject *next=object->next;
673 if (prev) {
674 prev->next = next;
675 } else {
676 parent->children = next;
677 }
678 if (!next) {
679 parent->_last_child = prev;
680 }
682 object->next = NULL;
683 object->parent = NULL;
685 parent->_updateTotalHRefCount(-object->_total_hrefcount);
686 sp_object_unref(object, parent);
687 }
689 /**
690 * Return object's child whose node pointer equals repr.
691 */
692 SPObject *
693 sp_object_get_child_by_repr(SPObject *object, Inkscape::XML::Node *repr)
694 {
695 g_return_val_if_fail(object != NULL, NULL);
696 g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
697 g_return_val_if_fail(repr != NULL, NULL);
699 if (object->_last_child && SP_OBJECT_REPR(object->_last_child) == repr)
700 return object->_last_child; // optimization for common scenario
701 for ( SPObject *child = object->children ; child ; child = child->next ) {
702 if ( SP_OBJECT_REPR(child) == repr ) {
703 return child;
704 }
705 }
707 return NULL;
708 }
710 /**
711 * Callback for child_added event.
712 * Invoked whenever the given mutation event happens in the XML tree.
713 */
714 static void
715 sp_object_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
716 {
717 GType type = sp_repr_type_lookup(child);
718 if (!type) {
719 return;
720 }
721 SPObject *ochild = SP_OBJECT(g_object_new(type, 0));
722 SPObject *prev = ref ? sp_object_get_child_by_repr(object, ref) : NULL;
723 sp_object_attach(object, ochild, prev);
724 sp_object_unref(ochild, NULL);
726 sp_object_invoke_build(ochild, object->document, child, SP_OBJECT_IS_CLONED(object));
727 }
729 /**
730 * Removes, releases and unrefs all children of object.
731 *
732 * This is the opposite of build. It has to be invoked as soon as the
733 * object is removed from the tree, even if it is still alive according
734 * to reference count. The frontend unregisters the object from the
735 * document and releases the SPRepr bindings; implementations should free
736 * state data and release all child objects. Invoking release on
737 * SPRoot destroys the whole document tree.
738 * \see sp_object_build()
739 */
740 static void sp_object_release(SPObject *object)
741 {
742 debug("id=%x, typename=%s", object, g_type_name_from_instance((GTypeInstance*)object));
743 while (object->children) {
744 sp_object_detach(object, object->children);
745 }
746 }
748 /**
749 * Remove object's child whose node equals repr, release and
750 * unref it.
751 *
752 * Invoked whenever the given mutation event happens in the XML
753 * tree, BEFORE removal from the XML tree happens, so grouping
754 * objects can safely release the child data.
755 */
756 static void
757 sp_object_remove_child(SPObject *object, Inkscape::XML::Node *child)
758 {
759 debug("id=%x, typename=%s", object, g_type_name_from_instance((GTypeInstance*)object));
760 SPObject *ochild = sp_object_get_child_by_repr(object, child);
761 g_return_if_fail(ochild != NULL);
762 sp_object_detach(object, ochild);
763 }
765 /**
766 * Move object corresponding to child after sibling object corresponding
767 * to new_ref.
768 * Invoked whenever the given mutation event happens in the XML tree.
769 * \param old_ref Ignored
770 */
771 static void sp_object_order_changed(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *old_ref,
772 Inkscape::XML::Node *new_ref)
773 {
774 SPObject *ochild = sp_object_get_child_by_repr(object, child);
775 g_return_if_fail(ochild != NULL);
776 SPObject *prev = new_ref ? sp_object_get_child_by_repr(object, new_ref) : NULL;
777 sp_object_reorder(ochild, prev);
778 ochild->_position_changed_signal.emit(ochild);
779 }
781 /**
782 * Virtual build callback.
783 *
784 * This has to be invoked immediately after creation of an SPObject. The
785 * frontend method ensures that the new object is properly attached to
786 * the document and repr; implementation then will parse all of the attributes,
787 * generate the children objects and so on. Invoking build on the SPRoot
788 * object results in creation of the whole document tree (this is, what
789 * SPDocument does after the creation of the XML tree).
790 * \see sp_object_release()
791 */
792 static void
793 sp_object_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
794 {
795 /* Nothing specific here */
796 debug("id=%x, typename=%s", object, g_type_name_from_instance((GTypeInstance*)object));
798 sp_object_read_attr(object, "xml:space");
799 sp_object_read_attr(object, "inkscape:label");
800 sp_object_read_attr(object, "inkscape:collect");
802 for (Inkscape::XML::Node *rchild = repr->firstChild() ; rchild != NULL; rchild = rchild->next()) {
803 GType type = sp_repr_type_lookup(rchild);
804 if (!type) {
805 continue;
806 }
807 SPObject *child = SP_OBJECT(g_object_new(type, 0));
808 sp_object_attach(object, child, object->lastChild());
809 sp_object_unref(child, NULL);
810 sp_object_invoke_build(child, document, rchild, SP_OBJECT_IS_CLONED(object));
811 }
812 }
814 void
815 sp_object_invoke_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr, unsigned int cloned)
816 {
817 debug("id=%x, typename=%s", object, g_type_name_from_instance((GTypeInstance*)object));
819 g_assert(object != NULL);
820 g_assert(SP_IS_OBJECT(object));
821 g_assert(document != NULL);
822 g_assert(repr != NULL);
824 g_assert(object->document == NULL);
825 g_assert(object->repr == NULL);
826 g_assert(object->id == NULL);
828 /* Bookkeeping */
830 object->document = document;
831 object->repr = repr;
832 Inkscape::GC::anchor(repr);
833 object->cloned = cloned;
835 if (!SP_OBJECT_IS_CLONED(object)) {
836 object->document->bindObjectToRepr(object->repr, object);
838 if (Inkscape::XML::id_permitted(object->repr)) {
839 /* If we are not cloned, force unique id */
840 gchar const *id = object->repr->attribute("id");
841 gchar *realid = sp_object_get_unique_id(object, id);
842 g_assert(realid != NULL);
844 object->document->bindObjectToId(realid, object);
845 object->id = realid;
847 /* Redefine ID, if required */
848 if ((id == NULL) || (strcmp(id, realid) != 0)) {
849 gboolean undo_sensitive=sp_document_get_undo_sensitive(document);
850 sp_document_set_undo_sensitive(document, FALSE);
851 object->repr->setAttribute("id", realid);
852 sp_document_set_undo_sensitive(document, undo_sensitive);
853 }
854 }
855 } else {
856 g_assert(object->id == NULL);
857 }
859 /* Invoke derived methods, if any */
861 if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->build) {
862 (*((SPObjectClass *) G_OBJECT_GET_CLASS(object))->build)(object, document, repr);
863 }
865 /* Signalling (should be connected AFTER processing derived methods */
866 sp_repr_add_listener(repr, &object_event_vector, object);
867 }
869 void
870 sp_object_invoke_release(SPObject *object)
871 {
872 g_assert(object != NULL);
873 g_assert(SP_IS_OBJECT(object));
875 g_assert(object->document);
876 g_assert(object->repr);
878 sp_repr_remove_listener_by_data(object->repr, object);
880 g_signal_emit(G_OBJECT(object), object_signals[RELEASE], 0);
882 /* all hrefs should be released by the "release" handlers */
883 g_assert(object->hrefcount == 0);
885 if (!SP_OBJECT_IS_CLONED(object)) {
886 if (object->id) {
887 object->document->bindObjectToId(object->id, NULL);
888 }
889 g_free(object->id);
890 object->id = NULL;
892 g_free(object->_default_label);
893 object->_default_label = NULL;
895 object->document->bindObjectToRepr(object->repr, NULL);
896 } else {
897 g_assert(!object->id);
898 }
900 if (object->style) {
901 object->style = sp_style_unref(object->style);
902 }
904 Inkscape::GC::release(object->repr);
906 object->document = NULL;
907 object->repr = NULL;
908 }
910 /**
911 * Callback for child_added node event.
912 */
913 static void
914 sp_object_repr_child_added(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, gpointer data)
915 {
916 SPObject *object = SP_OBJECT(data);
918 if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->child_added)
919 (*((SPObjectClass *)G_OBJECT_GET_CLASS(object))->child_added)(object, child, ref);
920 }
922 /**
923 * Callback for remove_child node event.
924 */
925 static void
926 sp_object_repr_child_removed(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *ref, gpointer data)
927 {
928 SPObject *object = SP_OBJECT(data);
930 if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->remove_child) {
931 (* ((SPObjectClass *)G_OBJECT_GET_CLASS(object))->remove_child)(object, child);
932 }
933 }
935 /**
936 * Callback for order_changed node event.
937 *
938 * \todo fixme:
939 */
940 static void
941 sp_object_repr_order_changed(Inkscape::XML::Node *repr, Inkscape::XML::Node *child, Inkscape::XML::Node *old, Inkscape::XML::Node *newer, gpointer data)
942 {
943 SPObject *object = SP_OBJECT(data);
945 if (((SPObjectClass *) G_OBJECT_GET_CLASS(object))->order_changed) {
946 (* ((SPObjectClass *)G_OBJECT_GET_CLASS(object))->order_changed)(object, child, old, newer);
947 }
948 }
950 /**
951 * Callback for set event.
952 */
953 static void
954 sp_object_private_set(SPObject *object, unsigned int key, gchar const *value)
955 {
956 g_assert(key != SP_ATTR_INVALID);
958 switch (key) {
959 case SP_ATTR_ID:
960 if ( !SP_OBJECT_IS_CLONED(object) && object->repr->type() == Inkscape::XML::ELEMENT_NODE ) {
961 SPDocument *document=object->document;
962 SPObject *conflict=NULL;
964 if (value) {
965 conflict = document->getObjectById((char const *)value);
966 }
967 if ( conflict && conflict != object ) {
968 sp_object_ref(conflict, NULL);
969 // give the conflicting object a new ID
970 gchar *new_conflict_id = sp_object_get_unique_id(conflict, NULL);
971 SP_OBJECT_REPR(conflict)->setAttribute("id", new_conflict_id);
972 g_free(new_conflict_id);
973 sp_object_unref(conflict, NULL);
974 }
976 if (object->id) {
977 document->bindObjectToId(object->id, NULL);
978 g_free(object->id);
979 }
981 if (value) {
982 object->id = g_strdup((char const*)value);
983 document->bindObjectToId(object->id, object);
984 } else {
985 object->id = NULL;
986 }
988 g_free(object->_default_label);
989 object->_default_label = NULL;
990 }
991 break;
992 case SP_ATTR_INKSCAPE_LABEL:
993 g_free(object->_label);
994 if (value) {
995 object->_label = g_strdup(value);
996 } else {
997 object->_label = NULL;
998 }
999 g_free(object->_default_label);
1000 object->_default_label = NULL;
1001 break;
1002 case SP_ATTR_INKSCAPE_COLLECT:
1003 if ( value && !strcmp(value, "always") ) {
1004 object->setCollectionPolicy(SPObject::ALWAYS_COLLECT);
1005 } else {
1006 object->setCollectionPolicy(SPObject::COLLECT_WITH_PARENT);
1007 }
1008 break;
1009 case SP_ATTR_XML_SPACE:
1010 if (value && !strcmp(value, "preserve")) {
1011 object->xml_space.value = SP_XML_SPACE_PRESERVE;
1012 object->xml_space.set = TRUE;
1013 } else if (value && !strcmp(value, "default")) {
1014 object->xml_space.value = SP_XML_SPACE_DEFAULT;
1015 object->xml_space.set = TRUE;
1016 } else if (object->parent) {
1017 SPObject *parent;
1018 parent = object->parent;
1019 object->xml_space.value = parent->xml_space.value;
1020 }
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();
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 }
1135 }
1137 return repr;
1138 }
1140 /**
1141 * Update this object's XML node with flags value.
1142 */
1143 Inkscape::XML::Node *
1144 SPObject::updateRepr(unsigned int flags) {
1145 if (!SP_OBJECT_IS_CLONED(this)) {
1146 Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
1147 if (repr) {
1148 return updateRepr(repr, flags);
1149 } else {
1150 g_critical("Attempt to update non-existent repr");
1151 return NULL;
1152 }
1153 } else {
1154 /* cloned objects have no repr */
1155 return NULL;
1156 }
1157 }
1159 Inkscape::XML::Node *
1160 SPObject::updateRepr(Inkscape::XML::Node *repr, unsigned int flags) {
1161 if (SP_OBJECT_IS_CLONED(this)) {
1162 /* cloned objects have no repr */
1163 return NULL;
1164 }
1165 if (((SPObjectClass *) G_OBJECT_GET_CLASS(this))->write) {
1166 if (!(flags & SP_OBJECT_WRITE_BUILD) && !repr) {
1167 repr = SP_OBJECT_REPR(this);
1168 }
1169 return ((SPObjectClass *) G_OBJECT_GET_CLASS(this))->write(this, repr, flags);
1170 } else {
1171 g_warning("Class %s does not implement ::write", G_OBJECT_TYPE_NAME(this));
1172 if (!repr) {
1173 if (flags & SP_OBJECT_WRITE_BUILD) {
1174 repr = SP_OBJECT_REPR(this)->duplicate();
1175 }
1176 /// \todo fixme: else probably error (Lauris) */
1177 } else {
1178 repr->mergeFrom(SP_OBJECT_REPR(this), "id");
1179 }
1180 return repr;
1181 }
1182 }
1184 /* Modification */
1186 /**
1187 * Add \a flags to \a object's as dirtiness flags, and
1188 * recursively add CHILD_MODIFIED flag to
1189 * parent and ancestors (as far up as necessary).
1190 */
1191 void
1192 SPObject::requestDisplayUpdate(unsigned int flags)
1193 {
1194 if (update_in_progress) {
1195 g_print("WARNING: Requested update while update in progress, counter = %d\n", update_in_progress);
1196 }
1198 g_return_if_fail(!(flags & SP_OBJECT_PARENT_MODIFIED_FLAG));
1199 g_return_if_fail((flags & SP_OBJECT_MODIFIED_FLAG) || (flags & SP_OBJECT_CHILD_MODIFIED_FLAG));
1200 g_return_if_fail(!((flags & SP_OBJECT_MODIFIED_FLAG) && (flags & SP_OBJECT_CHILD_MODIFIED_FLAG)));
1202 /* Check for propagate before we set any flags */
1203 /* Propagate means, that this is not passed through by modification request cascade yet */
1204 unsigned int propagate = (!(this->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG)));
1206 /* Just set this flags safe even if some have been set before */
1207 this->uflags |= flags;
1209 if (propagate) {
1210 if (this->parent) {
1211 this->parent->requestDisplayUpdate(SP_OBJECT_CHILD_MODIFIED_FLAG);
1212 } else {
1213 sp_document_request_modified(this->document);
1214 }
1215 }
1216 }
1218 void
1219 SPObject::updateDisplay(SPCtx *ctx, unsigned int flags)
1220 {
1221 g_return_if_fail(!(flags & ~SP_OBJECT_MODIFIED_CASCADE));
1223 update_in_progress ++;
1225 #ifdef SP_OBJECT_DEBUG_CASCADE
1226 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);
1227 #endif
1229 /* Get this flags */
1230 flags |= this->uflags;
1231 /* Copy flags to modified cascade for later processing */
1232 this->mflags |= this->uflags;
1233 /* We have to clear flags here to allow rescheduling update */
1234 this->uflags = 0;
1236 // Merge style if we have good reasons to think that parent style is changed */
1237 /** \todo
1238 * I am not sure whether we should check only propagated
1239 * flag. We are currently assuming that style parsing is
1240 * done immediately. I think this is correct (Lauris).
1241 */
1242 if ((flags & SP_OBJECT_STYLE_MODIFIED_FLAG) && (flags & SP_OBJECT_PARENT_MODIFIED_FLAG)) {
1243 if (this->style && this->parent) {
1244 sp_style_merge_from_parent(this->style, this->parent->style);
1245 }
1246 }
1248 if (((SPObjectClass *) G_OBJECT_GET_CLASS(this))->update)
1249 ((SPObjectClass *) G_OBJECT_GET_CLASS(this))->update(this, ctx, flags);
1251 update_in_progress --;
1252 }
1254 void
1255 SPObject::requestModified(unsigned int flags)
1256 {
1257 g_return_if_fail( this->document != NULL );
1259 /* PARENT_MODIFIED is computed later on and is not intended to be
1260 * "manually" queued */
1261 g_return_if_fail(!(flags & SP_OBJECT_PARENT_MODIFIED_FLAG));
1263 /* we should be setting either MODIFIED or CHILD_MODIFIED... */
1264 g_return_if_fail((flags & SP_OBJECT_MODIFIED_FLAG) || (flags & SP_OBJECT_CHILD_MODIFIED_FLAG));
1266 /* ...but not both */
1267 g_return_if_fail(!((flags & SP_OBJECT_MODIFIED_FLAG) && (flags & SP_OBJECT_CHILD_MODIFIED_FLAG)));
1269 unsigned int old_mflags=this->mflags;
1270 this->mflags |= flags;
1272 /* If we already had MODIFIED or CHILD_MODIFIED queued, we will
1273 * have already queued CHILD_MODIFIED with our ancestors and
1274 * need not disturb them again.
1275 */
1276 if (!( old_mflags & ( SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG ) )) {
1277 SPObject *parent=SP_OBJECT_PARENT(this);
1278 if (parent) {
1279 parent->requestModified(SP_OBJECT_CHILD_MODIFIED_FLAG);
1280 } else {
1281 sp_document_request_modified(SP_OBJECT_DOCUMENT(this));
1282 }
1283 }
1284 }
1286 void
1287 SPObject::emitModified(unsigned int flags)
1288 {
1289 /* only the MODIFIED_CASCADE flag is legal here */
1290 g_return_if_fail(!(flags & ~SP_OBJECT_MODIFIED_CASCADE));
1292 #ifdef SP_OBJECT_DEBUG_CASCADE
1293 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);
1294 #endif
1296 flags |= this->mflags;
1297 /* We have to clear mflags beforehand, as signal handlers may
1298 * make changes and therefore queue new modification notifications
1299 * themselves. */
1300 this->mflags = 0;
1302 g_object_ref(G_OBJECT(this));
1303 g_signal_emit(G_OBJECT(this), object_signals[MODIFIED], 0, flags);
1304 g_object_unref(G_OBJECT(this));
1305 }
1307 /*
1308 * Get and set descriptive parameters
1309 *
1310 * These are inefficent, so they are not intended to be used interactively
1311 */
1313 gchar const *
1314 sp_object_title_get(SPObject *object)
1315 {
1316 return NULL;
1317 }
1319 gchar const *
1320 sp_object_description_get(SPObject *object)
1321 {
1322 return NULL;
1323 }
1325 unsigned int
1326 sp_object_title_set(SPObject *object, gchar const *title)
1327 {
1328 return FALSE;
1329 }
1331 unsigned int
1332 sp_object_description_set(SPObject *object, gchar const *desc)
1333 {
1334 return FALSE;
1335 }
1337 gchar const *
1338 sp_object_tagName_get(SPObject const *object, SPException *ex)
1339 {
1340 /* If exception is not clear, return */
1341 if (!SP_EXCEPTION_IS_OK(ex)) {
1342 return NULL;
1343 }
1345 /// \todo fixme: Exception if object is NULL? */
1346 return object->repr->name();
1347 }
1349 gchar const *
1350 sp_object_getAttribute(SPObject const *object, gchar const *key, SPException *ex)
1351 {
1352 /* If exception is not clear, return */
1353 if (!SP_EXCEPTION_IS_OK(ex)) {
1354 return NULL;
1355 }
1357 /// \todo fixme: Exception if object is NULL? */
1358 return (gchar const *) object->repr->attribute(key);
1359 }
1361 void
1362 sp_object_setAttribute(SPObject *object, gchar const *key, gchar const *value, SPException *ex)
1363 {
1364 /* If exception is not clear, return */
1365 g_return_if_fail(SP_EXCEPTION_IS_OK(ex));
1367 /// \todo fixme: Exception if object is NULL? */
1368 if (!sp_repr_set_attr(object->repr, key, value)) {
1369 ex->code = SP_NO_MODIFICATION_ALLOWED_ERR;
1370 }
1371 }
1373 void
1374 sp_object_removeAttribute(SPObject *object, gchar const *key, SPException *ex)
1375 {
1376 /* If exception is not clear, return */
1377 g_return_if_fail(SP_EXCEPTION_IS_OK(ex));
1379 /// \todo fixme: Exception if object is NULL? */
1380 if (!sp_repr_set_attr(object->repr, key, NULL)) {
1381 ex->code = SP_NO_MODIFICATION_ALLOWED_ERR;
1382 }
1383 }
1385 /* Helper */
1387 static gchar *
1388 sp_object_get_unique_id(SPObject *object, gchar const *id)
1389 {
1390 static unsigned long count = 0;
1392 g_assert(SP_IS_OBJECT(object));
1394 count++;
1396 gchar const *name = object->repr->name();
1397 g_assert(name != NULL);
1399 gchar const *local = strchr(name, ':');
1400 if (local) {
1401 name = local + 1;
1402 }
1404 if (id != NULL) {
1405 if (object->document->getObjectById(id) == NULL) {
1406 return g_strdup(id);
1407 }
1408 }
1410 size_t const name_len = strlen(name);
1411 size_t const buflen = name_len + (sizeof(count) * 10 / 4) + 1;
1412 gchar *const buf = (gchar *) g_malloc(buflen);
1413 memcpy(buf, name, name_len);
1414 gchar *const count_buf = buf + name_len;
1415 size_t const count_buflen = buflen - name_len;
1416 do {
1417 ++count;
1418 g_snprintf(count_buf, count_buflen, "%lu", count);
1419 } while ( object->document->getObjectById(buf) != NULL );
1420 return buf;
1421 }
1423 /* Style */
1425 /**
1426 * Returns an object style property.
1427 *
1428 * \todo
1429 * fixme: Use proper CSS parsing. The current version is buggy
1430 * in a number of situations where key is a substring of the
1431 * style string other than as a property name (including
1432 * where key is a substring of a property name), and is also
1433 * buggy in its handling of inheritance for properties that
1434 * aren't inherited by default. It also doesn't allow for
1435 * the case where the property is specified but with an invalid
1436 * value (in which case I believe the CSS2 error-handling
1437 * behaviour applies, viz. behave as if the property hadn't
1438 * been specified). Also, the current code doesn't use CRSelEng
1439 * stuff to take a value from stylesheets. Also, we aren't
1440 * setting any hooks to force an update for changes in any of
1441 * the inputs (i.e., in any of the elements that this function
1442 * queries).
1443 *
1444 * \par
1445 * Given that the default value for a property depends on what
1446 * property it is (e.g., whether to inherit or not), and given
1447 * the above comment about ignoring invalid values, and that the
1448 * repr parent isn't necessarily the right element to inherit
1449 * from (e.g., maybe we need to inherit from the referencing
1450 * <use> element instead), we should probably make the caller
1451 * responsible for ascending the repr tree as necessary.
1452 */
1453 gchar const *
1454 sp_object_get_style_property(SPObject const *object, gchar const *key, gchar const *def)
1455 {
1456 g_return_val_if_fail(object != NULL, NULL);
1457 g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
1458 g_return_val_if_fail(key != NULL, NULL);
1460 gchar const *style = object->repr->attribute("style");
1461 if (style) {
1462 size_t const len = strlen(key);
1463 char const *p;
1464 while ( (p = strstr(style, key))
1465 != NULL )
1466 {
1467 p += len;
1468 while ((*p <= ' ') && *p) p++;
1469 if (*p++ != ':') break;
1470 while ((*p <= ' ') && *p) p++;
1471 size_t const inherit_len = sizeof("inherit") - 1;
1472 if (*p
1473 && !(strneq(p, "inherit", inherit_len)
1474 && (p[inherit_len] == '\0'
1475 || p[inherit_len] == ';'
1476 || g_ascii_isspace(p[inherit_len])))) {
1477 return p;
1478 }
1479 }
1480 }
1481 gchar const *val = object->repr->attribute(key);
1482 if (val && !streq(val, "inherit")) {
1483 return val;
1484 }
1485 if (object->parent) {
1486 return sp_object_get_style_property(object->parent, key, def);
1487 }
1489 return def;
1490 }
1492 /**
1493 * Lifts SVG version of all root objects to version.
1494 */
1495 void
1496 SPObject::_requireSVGVersion(Inkscape::Version version) {
1497 for ( SPObject::ParentIterator iter=this ; iter ; ++iter ) {
1498 SPObject *object=iter;
1499 if (SP_IS_ROOT(object)) {
1500 SPRoot *root=SP_ROOT(object);
1501 if ( root->version.svg < version ) {
1502 root->version.svg = version;
1503 }
1504 }
1505 }
1506 }
1508 /**
1509 * Return sodipodi version of first root ancestor or (0,0).
1510 */
1511 Inkscape::Version
1512 sp_object_get_sodipodi_version(SPObject *object)
1513 {
1514 static Inkscape::Version const zero_version(0, 0);
1516 while (object) {
1517 if (SP_IS_ROOT(object)) {
1518 return SP_ROOT(object)->version.sodipodi;
1519 }
1520 object = SP_OBJECT_PARENT(object);
1521 }
1523 return zero_version;
1524 }
1526 /**
1527 * Returns previous object in sibling list or NULL.
1528 */
1529 SPObject *
1530 sp_object_prev(SPObject *child)
1531 {
1532 SPObject *parent = SP_OBJECT_PARENT(child);
1533 for ( SPObject *i = sp_object_first_child(parent); i; i = SP_OBJECT_NEXT(i) ) {
1534 if (SP_OBJECT_NEXT(i) == child)
1535 return i;
1536 }
1537 return NULL;
1538 }
1541 /*
1542 Local Variables:
1543 mode:c++
1544 c-file-style:"stroustrup"
1545 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1546 indent-tabs-mode:nil
1547 fill-column:99
1548 End:
1549 */
1550 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :