7c5556f27466e6a4c2de73fc41f8db569bd1d1f2
1 /*
2 * SimpleNode - generic 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 *
13 */
15 #ifndef SEEN_INKSCAPE_XML_SIMPLE_NODE_H
16 #define SEEN_INKSCAPE_XML_SIMPLE_NODE_H
18 #include <glib.h> // g_assert()
20 #include "xml/node.h"
21 #include "xml/attribute-record.h"
22 #include "xml/composite-node-observer.h"
23 #include "util/list-container.h"
25 namespace Inkscape {
27 namespace XML {
29 class SimpleNode
30 : virtual public Node, public Inkscape::GC::Managed<>
31 {
32 public:
33 gchar const *name() const;
34 int code() const { return _name; }
35 void setCodeUnsafe(int code) {
36 _name = code;
37 }
39 Document *document() { return _document; }
40 Document const *document() const {
41 return const_cast<SimpleNode *>(this)->document();
42 }
44 Node *duplicate(Document* doc) const { return _duplicate(doc); }
46 Node *root();
47 Node const *root() const {
48 return const_cast<SimpleNode *>(this)->root();
49 }
51 Node *parent() { return _parent; }
52 Node const *parent() const { return _parent; }
54 Node *next() { return _next; }
55 Node const *next() const { return _next; }
57 Node *firstChild() { return _first_child; }
58 Node const *firstChild() const { return _first_child; }
59 Node *lastChild() { return _last_child; }
60 Node const *lastChild() const { return _last_child; }
62 unsigned childCount() const { return _child_count; }
63 Node *nthChild(unsigned index);
64 Node const *nthChild(unsigned index) const {
65 return const_cast<SimpleNode *>(this)->nthChild(index);
66 }
68 void addChild(Node *child, Node *ref);
69 void appendChild(Node *child) {
70 SimpleNode::addChild(child, _last_child);
71 }
72 void removeChild(Node *child);
73 void changeOrder(Node *child, Node *ref);
75 unsigned position() const;
76 void setPosition(int pos);
78 gchar const *attribute(gchar const *key) const;
79 void setAttribute(gchar const *key, gchar const *value, bool is_interactive=false);
80 bool matchAttributeName(gchar const *partial_name) const;
82 gchar const *content() const;
83 void setContent(gchar const *value);
85 void mergeFrom(Node const *src, gchar const *key);
87 Inkscape::Util::List<AttributeRecord const> attributeList() const {
88 return _attributes;
89 }
91 void synthesizeEvents(NodeEventVector const *vector, void *data);
92 void synthesizeEvents(NodeObserver &observer);
94 void addListener(NodeEventVector const *vector, void *data) {
95 g_assert(vector != NULL);
96 _observers.addListener(*vector, data);
97 }
98 void addObserver(NodeObserver &observer) {
99 _observers.add(observer);
100 }
101 void removeListenerByData(void *data) {
102 _observers.removeListenerByData(data);
103 }
104 void removeObserver(NodeObserver &observer) {
105 _observers.remove(observer);
106 }
108 void addSubtreeObserver(NodeObserver &observer) {
109 _subtree_observers.add(observer);
110 }
111 void removeSubtreeObserver(NodeObserver &observer) {
112 _subtree_observers.remove(observer);
113 }
115 protected:
116 SimpleNode(int code, Document *document);
117 SimpleNode(SimpleNode const &repr, Document *document);
119 virtual SimpleNode *_duplicate(Document *doc) const=0;
121 public: // ideally these should be protected somehow...
122 NodeObserver &_subtreeObservers() { return _subtree_observers; }
123 void _setParent(Node *parent);
124 void _setNext(Node *next) { _next = next; }
125 void _bindDocument(Document &document);
127 unsigned _childPosition(Node const &child) const;
128 unsigned _cachedPosition() const { return _cached_position; }
129 void _setCachedPosition(unsigned position) const {
130 _cached_position = position;
131 }
133 private:
134 void operator=(Node const &); // no assign
136 Node *_parent;
137 Node *_next;
138 Document *_document;
139 mutable unsigned _cached_position;
141 int _name;
143 Inkscape::Util::MutableList<AttributeRecord> _attributes;
145 Inkscape::Util::ptr_shared<char> _content;
147 unsigned _child_count;
148 mutable bool _cached_positions_valid;
149 Node *_first_child;
150 Node *_last_child;
152 CompositeNodeObserver _observers;
153 CompositeNodeObserver _subtree_observers;
154 };
156 }
158 }
160 #endif
161 /*
162 Local Variables:
163 mode:c++
164 c-file-style:"stroustrup"
165 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
166 indent-tabs-mode:nil
167 fill-column:99
168 End:
169 */
170 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :