Code

plumb XML::Documents in everywhere
[inkscape.git] / src / sp-object.h
1 #ifndef SP_OBJECT_H_SEEN
2 #define SP_OBJECT_H_SEEN
4 /** \file
5  * Abstract base class for all nodes
6  *
7  * Authors:
8  *   Lauris Kaplinski <lauris@kaplinski.com>
9  *
10  * Copyright (C) 1999-2002 authors
11  * Copyright (C) 2001-2002 Ximian, Inc.
12  *
13  * Released under GNU GPL, read the file 'COPYING' for more information
14  */
16 /* SPObject flags */
18 /* Async modification flags */
19 #define SP_OBJECT_MODIFIED_FLAG (1 << 0)
20 #define SP_OBJECT_CHILD_MODIFIED_FLAG (1 << 1)
21 #define SP_OBJECT_PARENT_MODIFIED_FLAG (1 << 2)
22 #define SP_OBJECT_STYLE_MODIFIED_FLAG (1 << 3)
23 #define SP_OBJECT_VIEWPORT_MODIFIED_FLAG (1 << 4)
24 #define SP_OBJECT_USER_MODIFIED_FLAG_A (1 << 5)
25 #define SP_OBJECT_USER_MODIFIED_FLAG_B (1 << 6)
26 #define SP_OBJECT_USER_MODIFIED_FLAG_C (1 << 7)
28 /* Conveneience */
29 #define SP_OBJECT_FLAGS_ALL 0xff
31 /* Flags that mark object as modified */
32 /* Object, Child, Style, Viewport, User */
33 #define SP_OBJECT_MODIFIED_STATE (SP_OBJECT_FLAGS_ALL & ~(SP_OBJECT_PARENT_MODIFIED_FLAG))
35 /* Flags that will propagate downstreams */
36 /* Parent, Style, Viewport, User */
37 #define SP_OBJECT_MODIFIED_CASCADE (SP_OBJECT_FLAGS_ALL & ~(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))
39 /* Generic */
40 #define SP_OBJECT_IS_CLONED(o) (((SPObject *) (o))->cloned)
42 /* Write flags */
43 #define SP_OBJECT_WRITE_BUILD (1 << 0)
44 #define SP_OBJECT_WRITE_EXT (1 << 1)
45 #define SP_OBJECT_WRITE_ALL (1 << 2)
47 /* Convenience stuff */
48 #define SP_OBJECT_ID(o) (((SPObject *) (o))->id)
49 #define SP_OBJECT_REPR(o) (((SPObject *) (o))->repr)
50 #define SP_OBJECT_DOCUMENT(o) (((SPObject *) (o))->document)
51 #define SP_OBJECT_PARENT(o) (((SPObject *) (o))->parent)
52 #define SP_OBJECT_NEXT(o) (((SPObject *) (o))->next)
53 #define SP_OBJECT_PREV(o) (sp_object_prev((SPObject *) (o)))
54 #define SP_OBJECT_HREFCOUNT(o) (((SPObject *) (o))->hrefcount)
55 #define SP_OBJECT_STYLE(o) (((SPObject *) (o))->style)
58 #include <glib-object.h>
59 #include <sigc++/connection.h>
60 #include <sigc++/functors/slot.h>
61 #include <sigc++/signal.h>
63 #include "forward.h"
64 #include "version.h"
65 #include "util/forward-pointer-iterator.h"
67 namespace Inkscape {
68 namespace XML {
69 class Node;
70 class Document;
71 }
72 }
75 typedef enum {
76     SP_NO_EXCEPTION,
77     SP_INDEX_SIZE_ERR,
78     SP_DOMSTRING_SIZE_ERR,
79     SP_HIERARCHY_REQUEST_ERR,
80     SP_WRONG_DOCUMENT_ERR,
81     SP_INVALID_CHARACTER_ERR,
82     SP_NO_DATA_ALLOWED_ERR,
83     SP_NO_MODIFICATION_ALLOWED_ERR,
84     SP_NOT_FOUND_ERR,
85     SP_NOT_SUPPORTED_ERR,
86     SP_INUSE_ATTRIBUTE_ERR,
87     SP_INVALID_STATE_ERR,
88     SP_SYNTAX_ERR,
89     SP_INVALID_MODIFICATION_ERR,
90     SP_NAMESPACE_ERR,
91     SP_INVALID_ACCESS_ERR
92 } SPExceptionType;
94 class SPException;
96 /// An attempt to implement exceptions, unused?
97 struct SPException {
98     SPExceptionType code;
99 };
101 #define SP_EXCEPTION_INIT(ex) {(ex)->code = SP_NO_EXCEPTION;}
102 #define SP_EXCEPTION_IS_OK(ex) (!(ex) || ((ex)->code == SP_NO_EXCEPTION))
104 class SPCtx;
106 /// Unused
107 struct SPCtx {
108     unsigned int flags;
109 };
111 enum {
112     SP_XML_SPACE_DEFAULT,
113     SP_XML_SPACE_PRESERVE
114 };
116 class SPIXmlSpace;
118 /// Internal class consisting of two bits.
119 struct SPIXmlSpace {
120     guint set : 1;
121     guint value : 1;
122 };
124 class SPObject;
126 /*
127  * Refcounting
128  *
129  * Owner is here for debug reasons, you can set it to NULL safely
130  * Ref should return object, NULL is error, unref return always NULL
131  */
133 SPObject *sp_object_ref(SPObject *object, SPObject *owner=NULL);
134 SPObject *sp_object_unref(SPObject *object, SPObject *owner=NULL);
136 SPObject *sp_object_href(SPObject *object, gpointer owner);
137 SPObject *sp_object_hunref(SPObject *object, gpointer owner);
139 /// A refcounting tree node object.
140 struct SPObject : public GObject {
141     enum CollectionPolicy {
142         COLLECT_WITH_PARENT,
143         ALWAYS_COLLECT
144     };
146     unsigned int cloned : 1;
147     unsigned int uflags : 8;
148     unsigned int mflags : 8;
149     SPIXmlSpace xml_space;
150     unsigned int hrefcount; /* number of xlink:href references */
151     unsigned int _total_hrefcount; /* our hrefcount + total descendants */
152     SPDocument *document; /* Document we are part of */
153     SPObject *parent; /* Our parent (only one allowed) */
154     SPObject *children; /* Our children */
155     SPObject *_last_child; /* Remembered last child */
156     SPObject *next; /* Next object in linked list */
157     Inkscape::XML::Node *repr; /* Our xml representation */
158     gchar *id; /* Our very own unique id */
160     /** @brief cleans up an SPObject, releasing its references and
161      *         requesting that references to it be released
162      */
163     void releaseReferences();
165     /** @brief connects to the release request signal
166      *
167      *  @param slot the slot to connect
168      *
169      *  @returns the sigc::connection formed
170      */
171     sigc::connection connectRelease(sigc::slot<void, SPObject *> slot) {
172         return _release_signal.connect(slot);
173     }
175     /**
176      * Represents the style properties, whether from presentation attributes, the <tt>style</tt>
177      * attribute, or inherited.
178      *
179      * sp_object_private_set doesn't handle SP_ATTR_STYLE or any presentation attributes at the
180      * time of writing, so this is probably NULL for all SPObject's that aren't an SPItem.
181      *
182      * However, this gives rise to the bugs mentioned in sp_object_get_style_property.
183      * Note that some non-SPItem SPObject's, such as SPStop, do need styling information,
184      * and need to inherit properties even through other non-SPItem parents like \<defs\>.
185      */
186     SPStyle *style;
188     /// Switch containing next() method.
189     struct ParentIteratorStrategy {
190         static SPObject const *next(SPObject const *object) {
191             return object->parent;
192         }
193     };
194     /// Switch containing next() method.
195     struct SiblingIteratorStrategy {
196         static SPObject const *next(SPObject const *object) {
197             return object->next;
198         }
199     };
201     typedef Inkscape::Util::ForwardPointerIterator<SPObject, ParentIteratorStrategy> ParentIterator;
202     typedef Inkscape::Util::ForwardPointerIterator<SPObject const, ParentIteratorStrategy> ConstParentIterator;
203     typedef Inkscape::Util::ForwardPointerIterator<SPObject, SiblingIteratorStrategy> SiblingIterator;
204     typedef Inkscape::Util::ForwardPointerIterator<SPObject const, SiblingIteratorStrategy> ConstSiblingIterator;
206     bool isSiblingOf(SPObject const *object) const {
207         g_return_val_if_fail(object != NULL, false);
208         return this->parent && this->parent == object->parent;
209     }
210     bool isAncestorOf(SPObject const *object) const;
212     SPObject const *nearestCommonAncestor(SPObject const *object) const;
213     /* A non-const version can be similarly constructed if you want one.
214      * (Don't just cast away the constness, which would be ill-formed.) */
216     bool hasChildren() const { return ( children != NULL ); }
218     SPObject *firstChild() { return children; }
219     SPObject const *firstChild() const { return children; }
220     SPObject *lastChild() { return _last_child; }
221     SPObject const *lastChild() const { return _last_child; }
223     enum Action { ActionGeneral, ActionBBox, ActionUpdate, ActionShow };
224     /** @brief Retrieves children as a GSList */
225     GSList *childList(bool add_ref, Action action = ActionGeneral);
227     SPObject *appendChildRepr(Inkscape::XML::Node *repr);
229     /** @brief Gets the author-visible label for this object. */
230     gchar const *label() const;
231     /** @brief Returns a default label for this object. */
232     gchar const *defaultLabel() const;
233     /** @brief Sets the author-visible label for this object.
234      *
235      * Sets the author-visible label for the object.
236      *
237      * @param label the new label
238      */
239     void setLabel(gchar const *label);
241     /** Retrieves the title of this object */
242     gchar *title() const;
243     /** Sets the title of this object */
244     void setTitle(gchar const *title);
246     /** Retrieves the description of this object */
247     gchar *desc() const;
248     /** Sets the description of this object */
249     void setDesc(gchar const *desc);
251     /** @brief Set the policy under which this object will be
252      *         orphan-collected.
253      *
254      * Orphan-collection is the process of deleting all objects which no longer have
255      * hyper-references pointing to them.  The policy determines when this happens.  Many objects
256      * should not be deleted simply because they are no longer referred to; other objects (like
257      * "intermediate" gradients) are more or less throw-away and should always be collected when no
258      * longer in use.
259      *
260      * Along these lines, there are currently two orphan-collection policies:
261      *
262      *  COLLECT_WITH_PARENT - don't worry about the object's hrefcount;
263      *                        if its parent is collected, this object
264      *                        will be too
265      *
266      *  COLLECT_ALWAYS - always collect the object as soon as its
267      *                   hrefcount reaches zero
268      *
269      * @returns the current collection policy in effect for this object
270      */
271     CollectionPolicy collectionPolicy() const { return _collection_policy; }
273     /** @brief Sets the orphan-collection policy in effect for this object.
274      *
275      * @see SPObject::collectionPolicy
276      *
277      * @param policy the new policy to adopt
278      */
279     void setCollectionPolicy(CollectionPolicy policy) {
280         _collection_policy = policy;
281     }
283     /** @brief Requests a later automatic call to collectOrphan().
284      *
285      * This method requests that collectOrphan() be called during the document update cycle,
286      * deleting the object if it is no longer used.
287      *
288      * If the current collection policy is COLLECT_WITH_PARENT, this function has no effect.
289      *
290      * @see SPObject::collectOrphan
291      */
292     void requestOrphanCollection();
294     /** @brief Unconditionally delete the object if it is not referenced.
295      *
296      * Unconditionally delete the object if there are no outstanding hyper-references to it.
297      * Observers are not notified of the object's deletion (at the SPObject level; XML tree
298      * notifications still fire).
299      *
300      * @see SPObject::deleteObject
301      */
302     void collectOrphan() {
303         if ( _total_hrefcount == 0 ) {
304             deleteObject(false);
305         }
306     }
308     /** @brief Check if object is referenced by any other object.
309      */
310     bool isReferenced() { return ( _total_hrefcount > 0 ); }
312     /** @brief Deletes an object.
313      *
314      * Detaches the object's repr, and optionally sends notification that the object has been
315      * deleted.
316      *
317      * @param propagate notify observers that the object has been deleted?
318      *
319      * @param propagate_descendants notify observers of children that they have been deleted?
320      */
321     void deleteObject(bool propagate, bool propagate_descendants);
323     /** @brief Deletes on object.
324      *
325      * @param propagate Notify observers of this object and its children that they have been
326      *                  deleted?
327      */
328     void deleteObject(bool propagate=true) {
329         deleteObject(propagate, propagate);
330     }
332     /** @brief Connects a slot to be called when an object is deleted.
333      *
334      * This connects a slot to an object's internal delete signal, which is invoked when the object
335      * is deleted
336      *
337      * The signal is mainly useful for e.g. knowing when to break hrefs or dissociate clones.
338      *
339      * @param slot the slot to connect
340      *
341      * @see SPObject::deleteObject
342      */
343     sigc::connection connectDelete(sigc::slot<void, SPObject *> slot) {
344         return _delete_signal.connect(slot);
345     }
347     sigc::connection connectPositionChanged(sigc::slot<void, SPObject *> slot) {
348         return _position_changed_signal.connect(slot);
349     }
351     /** @brief Returns the object which supercedes this one (if any).
352      *
353      * This is mainly useful for ensuring we can correctly perform a series of moves or deletes,
354      * even if the objects in question have been replaced in the middle of the sequence.
355      */
356     SPObject *successor() { return _successor; }
358     /** @brief Indicates that another object supercedes this one. */
359     void setSuccessor(SPObject *successor) {
360         g_assert(successor != NULL);
361         g_assert(_successor == NULL);
362         g_assert(successor->_successor == NULL);
363         sp_object_ref(successor, NULL);
364         _successor = successor;
365     }
367     /* modifications; all three sets of methods should probably ultimately be protected, as they
368      * are not really part of its public interface.  However, other parts of the code to
369      * occasionally use them at present. */
371     /* the no-argument version of updateRepr() is intended to be a bit more public, however -- it
372      * essentially just flushes any changes back to the backing store (the repr layer); maybe it
373      * should be called something else and made public at that point. */
375     /** @brief Updates the object's repr based on the object's state.
376      *
377      *  This method updates the the repr attached to the object to reflect the object's current
378      *  state; see the three-argument version for details.
379      *
380      *  @param flags object write flags that apply to this update
381      *
382      *  @return the updated repr
383      */
384     Inkscape::XML::Node *updateRepr(unsigned int flags=SP_OBJECT_WRITE_EXT);
386     /** @brief Updates the given repr based on the object's state.
387      *
388      *  This method updates the given repr to reflect the object's current state.  There are
389      *  several flags that affect this:
390      *
391      *   SP_OBJECT_WRITE_BUILD - create new reprs
392      *
393      *   SP_OBJECT_WRITE_EXT   - write elements and attributes
394      *                           which are not part of pure SVG
395      *                           (i.e. the Inkscape and Sodipodi
396      *                           namespaces)
397      *
398      *   SP_OBJECT_WRITE_ALL   - create all nodes and attributes,
399      *                           even those which might be redundant
400      *
401      *  @param repr the repr to update
402      *  @param flags object write flags that apply to this update
403      *
404      *  @return the updated repr
405      */
406     Inkscape::XML::Node *updateRepr(Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, unsigned int flags);
408     /** @brief Queues an deferred update of this object's display.
409      *
410      *  This method sets flags to indicate updates to be performed later, during the idle loop.
411      *
412      *  There are several flags permitted here:
413      *
414      *   SP_OBJECT_MODIFIED_FLAG - the object has been modified
415      *
416      *   SP_OBJECT_CHILD_MODIFIED_FLAG - a child of the object has been
417      *                                   modified
418      *
419      *   SP_OBJECT_STYLE_MODIFIED_FLAG - the object's style has been
420      *                                   modified
421      *
422      *  There are also some subclass-specific modified flags which are hardly ever used.
423      *
424      *  One of either MODIFIED or CHILD_MODIFIED is required.
425      *
426      *  @param flags flags indicating what to update
427      */
428     void requestDisplayUpdate(unsigned int flags);
430     /** @brief Updates the object's display immediately
431      *
432      *  This method is called during the idle loop by SPDocument in order to update the object's
433      *  display.
434      *
435      *  One additional flag is legal here:
436      *
437      *   SP_OBJECT_PARENT_MODIFIED_FLAG - the parent has been
438      *                                    modified
439      *
440      *  @param ctx an SPCtx which accumulates various state
441      *             during the recursive update -- beware! some
442      *             subclasses try to cast this to an SPItemCtx *
443      *
444      *  @param flags flags indicating what to update (in addition
445      *               to any already set flags)
446      */
447     void updateDisplay(SPCtx *ctx, unsigned int flags);
449     /** @brief Requests that a modification notification signal
450      *         be emitted later (e.g. during the idle loop)
451      *
452      *  @param flags flags indicating what has been modified
453      */
454     void requestModified(unsigned int flags);
456     /** @brief Emits a modification notification signal
457      *
458      *  @param flags indicating what has been modified
459      */
460     void emitModified(unsigned int flags);
462     /** @brief Connects to the modification notification signal
463      *
464      *  @param slot the slot to connect
465      *
466      *  @returns the connection formed thereby
467      */
468     sigc::connection connectModified(
469       sigc::slot<void, SPObject *, unsigned int> slot
470     ) {
471         return _modified_signal.connect(slot);
472     }
474     void _sendDeleteSignalRecursive();
475     void _updateTotalHRefCount(int increment);
477     void _requireSVGVersion(unsigned major, unsigned minor) {
478         _requireSVGVersion(Inkscape::Version(major, minor));
479     }
480     void _requireSVGVersion(Inkscape::Version version);
482     sigc::signal<void, SPObject *> _release_signal;
483     sigc::signal<void, SPObject *> _delete_signal;
484     sigc::signal<void, SPObject *> _position_changed_signal;
485     sigc::signal<void, SPObject *, unsigned int> _modified_signal;
486     SPObject *_successor;
487     CollectionPolicy _collection_policy;
488     gchar *_label;
489     mutable gchar *_default_label;
491 private:
492     // Private member functions used in the definitions of setTitle(),
493     // setDesc(), title() and desc().
494     void setTitleOrDesc(gchar const *value, gchar const *svg_tagname);
495     gchar * getTitleOrDesc(gchar const *svg_tagname) const;
496     SPObject * findFirstChild(gchar const *tagname) const;
497     GString * textualContent() const;
498 };
500 /// The SPObject vtable.
501 struct SPObjectClass {
502     GObjectClass parent_class;
504     void (* build) (SPObject *object, SPDocument *doc, Inkscape::XML::Node *repr);
505     void (* release) (SPObject *object);
507     /* Virtual handlers of repr signals */
508     void (* child_added) (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref);
509     void (* remove_child) (SPObject *object, Inkscape::XML::Node *child);
511     void (* order_changed) (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *old, Inkscape::XML::Node *new_repr);
513     void (* set) (SPObject *object, unsigned int key, gchar const *value);
515     void (* read_content) (SPObject *object);
517     /* Update handler */
518     void (* update) (SPObject *object, SPCtx *ctx, unsigned int flags);
519     /* Modification handler */
520     void (* modified) (SPObject *object, unsigned int flags);
522     Inkscape::XML::Node * (* write) (SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, unsigned int flags);
523 };
526 /*
527  * Attaching/detaching
528  */
530 void sp_object_attach(SPObject *parent, SPObject *object, SPObject *prev);
531 void sp_object_reorder(SPObject *object, SPObject *prev);
532 void sp_object_detach(SPObject *parent, SPObject *object);
534 inline SPObject *sp_object_first_child(SPObject *parent) {
535     return parent->firstChild();
537 SPObject *sp_object_get_child_by_repr(SPObject *object, Inkscape::XML::Node *repr);
539 void sp_object_invoke_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr, unsigned int cloned);
541 void sp_object_set(SPObject *object, unsigned int key, gchar const *value);
543 void sp_object_read_attr(SPObject *object, gchar const *key);
545 /* Public */
547 gchar const *sp_object_tagName_get(SPObject const *object, SPException *ex);
548 gchar const *sp_object_getAttribute(SPObject const *object, gchar const *key, SPException *ex);
549 void sp_object_setAttribute(SPObject *object, gchar const *key, gchar const *value, SPException *ex);
550 void sp_object_removeAttribute(SPObject *object, gchar const *key, SPException *ex);
552 /* Style */
554 gchar const *sp_object_get_style_property(SPObject const *object,
555                                           gchar const *key, gchar const *def);
557 Inkscape::Version sp_object_get_sodipodi_version(SPObject *object);
559 int sp_object_compare_position(SPObject const *first, SPObject const *second);
561 SPObject *sp_object_prev(SPObject *child);
564 #endif // SP_OBJECT_H_SEEN
567 /*
568   Local Variables:
569   mode:c++
570   c-file-style:"stroustrup"
571   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
572   indent-tabs-mode:nil
573   fill-column:99
574   End:
575 */
576 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :