Code

* Lots of documentation for the Inkscape::XML namespace
[inkscape.git] / src / xml / simple-node.h
1 /** @file
2  * @brief GC-managed XML node implementation
3  */
4 /* Copyright 2004-2005 MenTaLguY <mental@rydia.net>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * See the file COPYING for details.
12  */
14 #ifndef SEEN_INKSCAPE_XML_SIMPLE_NODE_H
15 #define SEEN_INKSCAPE_XML_SIMPLE_NODE_H
17 #include <glib.h> // g_assert()
19 #include "xml/node.h"
20 #include "xml/attribute-record.h"
21 #include "xml/composite-node-observer.h"
22 #include "util/list-container.h"
24 namespace Inkscape {
26 namespace XML {
28 /**
29  * @brief Default implementation of the XML node stored in memory.
30  *
31  * @see Inkscape::XML::Node
32  */
33 class SimpleNode
34 : virtual public Node, public Inkscape::GC::Managed<>
35 {
36 public:
37     gchar const *name() const;
38     int code() const { return _name; }
39     void setCodeUnsafe(int code) {
40         _name = code;
41     }
43     Document *document() { return _document; }
44     Document const *document() const {
45         return const_cast<SimpleNode *>(this)->document();
46     }
48     Node *duplicate(Document* doc) const { return _duplicate(doc); }
50     Node *root();
51     Node const *root() const {
52         return const_cast<SimpleNode *>(this)->root();
53     }
55     Node *parent() { return _parent; }
56     Node const *parent() const { return _parent; }
58     Node *next() { return _next; }
59     Node const *next() const { return _next; }
61     Node *firstChild() { return _first_child; }
62     Node const *firstChild() const { return _first_child; }
63     Node *lastChild() { return _last_child; }
64     Node const *lastChild() const { return _last_child; }
66     unsigned childCount() const { return _child_count; }
67     Node *nthChild(unsigned index);
68     Node const *nthChild(unsigned index) const {
69         return const_cast<SimpleNode *>(this)->nthChild(index);
70     }
72     void addChild(Node *child, Node *ref);
73     void appendChild(Node *child) {
74         SimpleNode::addChild(child, _last_child);
75     }
76     void removeChild(Node *child);
77     void changeOrder(Node *child, Node *ref);
79     unsigned position() const;
80     void setPosition(int pos);
82     gchar const *attribute(gchar const *key) const;
83     void setAttribute(gchar const *key, gchar const *value, bool is_interactive=false);
84     bool matchAttributeName(gchar const *partial_name) const;
86     gchar const *content() const;
87     void setContent(gchar const *value);
89     void mergeFrom(Node const *src, gchar const *key);
91     Inkscape::Util::List<AttributeRecord const> attributeList() const {
92         return _attributes;
93     }
95     void synthesizeEvents(NodeEventVector const *vector, void *data);
96     void synthesizeEvents(NodeObserver &observer);
98     void addListener(NodeEventVector const *vector, void *data) {
99         g_assert(vector != NULL);
100         _observers.addListener(*vector, data);
101     }
102     void addObserver(NodeObserver &observer) {
103         _observers.add(observer);
104     }
105     void removeListenerByData(void *data) {
106         _observers.removeListenerByData(data);
107     }
108     void removeObserver(NodeObserver &observer) {
109         _observers.remove(observer);
110     }
112     void addSubtreeObserver(NodeObserver &observer) {
113         _subtree_observers.add(observer);
114     }
115     void removeSubtreeObserver(NodeObserver &observer) {
116         _subtree_observers.remove(observer);
117     }
119 protected:
120     SimpleNode(int code, Document *document);
121     SimpleNode(SimpleNode const &repr, Document *document);
123     virtual SimpleNode *_duplicate(Document *doc) const=0;
125 private:
126     void operator=(Node const &); // no assign
128     void _setParent(SimpleNode *parent);
129     unsigned _childPosition(SimpleNode const &child) const;
131     SimpleNode *_parent;
132     SimpleNode *_next;
133     Document *_document;
134     mutable unsigned _cached_position;
136     int _name;
138     Inkscape::Util::MutableList<AttributeRecord> _attributes;
140     Inkscape::Util::ptr_shared<char> _content;
142     unsigned _child_count;
143     mutable bool _cached_positions_valid;
144     SimpleNode *_first_child;
145     SimpleNode *_last_child;
147     CompositeNodeObserver _observers;
148     CompositeNodeObserver _subtree_observers;
149 };
155 #endif
156 /*
157   Local Variables:
158   mode:c++
159   c-file-style:"stroustrup"
160   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
161   indent-tabs-mode:nil
162   fill-column:99
163   End:
164 */
165 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :