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_NODE_H
15 #error You have included xml/simple-node.h in your document, which is an implementation. Chances are that you want xml/node.h. Please fix that.
16 #endif
18 #ifndef SEEN_INKSCAPE_XML_SIMPLE_NODE_H
19 #define SEEN_INKSCAPE_XML_SIMPLE_NODE_H
21 #include <glib.h> // g_assert()
23 #include "xml/node.h"
24 #include "xml/attribute-record.h"
25 #include "xml/composite-node-observer.h"
26 #include "util/list-container.h"
28 namespace Inkscape {
30 namespace XML {
32 /**
33 * @brief Default implementation of the XML node stored in memory.
34 *
35 * @see Inkscape::XML::Node
36 */
37 class SimpleNode
38 : virtual public Node, public Inkscape::GC::Managed<>
39 {
40 public:
41 gchar const *name() const;
42 int code() const { return _name; }
43 void setCodeUnsafe(int code) {
44 _name = code;
45 }
47 Document *document() { return _document; }
48 Document const *document() const {
49 return const_cast<SimpleNode *>(this)->document();
50 }
52 Node *duplicate(Document* doc) const { return _duplicate(doc); }
54 Node *root();
55 Node const *root() const {
56 return const_cast<SimpleNode *>(this)->root();
57 }
59 Node *parent() { return _parent; }
60 Node const *parent() const { return _parent; }
62 Node *next() { return _next; }
63 Node const *next() const { return _next; }
65 Node *firstChild() { return _first_child; }
66 Node const *firstChild() const { return _first_child; }
67 Node *lastChild() { return _last_child; }
68 Node const *lastChild() const { return _last_child; }
70 unsigned childCount() const { return _child_count; }
71 Node *nthChild(unsigned index);
72 Node const *nthChild(unsigned index) const {
73 return const_cast<SimpleNode *>(this)->nthChild(index);
74 }
76 void addChild(Node *child, Node *ref);
77 void appendChild(Node *child) {
78 SimpleNode::addChild(child, _last_child);
79 }
80 void removeChild(Node *child);
81 void changeOrder(Node *child, Node *ref);
83 unsigned position() const;
84 void setPosition(int pos);
86 gchar const *attribute(gchar const *key) const;
87 void setAttribute(gchar const *key, gchar const *value, bool is_interactive=false);
88 bool matchAttributeName(gchar const *partial_name) const;
90 gchar const *content() const;
91 void setContent(gchar const *value);
93 void mergeFrom(Node const *src, gchar const *key);
95 Inkscape::Util::List<AttributeRecord const> attributeList() const {
96 return _attributes;
97 }
99 void synthesizeEvents(NodeEventVector const *vector, void *data);
100 void synthesizeEvents(NodeObserver &observer);
102 void addListener(NodeEventVector const *vector, void *data) {
103 g_assert(vector != NULL);
104 _observers.addListener(*vector, data);
105 }
106 void addObserver(NodeObserver &observer) {
107 _observers.add(observer);
108 }
109 void removeListenerByData(void *data) {
110 _observers.removeListenerByData(data);
111 }
112 void removeObserver(NodeObserver &observer) {
113 _observers.remove(observer);
114 }
116 void addSubtreeObserver(NodeObserver &observer) {
117 _subtree_observers.add(observer);
118 }
119 void removeSubtreeObserver(NodeObserver &observer) {
120 _subtree_observers.remove(observer);
121 }
123 protected:
124 SimpleNode(int code, Document *document);
125 SimpleNode(SimpleNode const &repr, Document *document);
127 virtual SimpleNode *_duplicate(Document *doc) const=0;
129 private:
130 void operator=(Node const &); // no assign
132 void _setParent(SimpleNode *parent);
133 unsigned _childPosition(SimpleNode const &child) const;
135 SimpleNode *_parent;
136 SimpleNode *_next;
137 Document *_document;
138 mutable unsigned _cached_position;
140 int _name;
142 Inkscape::Util::MutableList<AttributeRecord> _attributes;
144 Inkscape::Util::ptr_shared<char> _content;
146 unsigned _child_count;
147 mutable bool _cached_positions_valid;
148 SimpleNode *_first_child;
149 SimpleNode *_last_child;
151 CompositeNodeObserver _observers;
152 CompositeNodeObserver _subtree_observers;
153 };
155 }
157 }
159 #endif
160 /*
161 Local Variables:
162 mode:c++
163 c-file-style:"stroustrup"
164 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
165 indent-tabs-mode:nil
166 fill-column:99
167 End:
168 */
169 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :