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; }
126 unsigned _childPosition(Node const &child) const;
127 unsigned _cachedPosition() const { return _cached_position; }
128 void _setCachedPosition(unsigned position) const {
129 _cached_position = position;
130 }
132 private:
133 void operator=(Node const &); // no assign
135 Node *_parent;
136 Node *_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 Node *_first_child;
149 Node *_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 :