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 "xml/node.h"
19 #include "xml/attribute-record.h"
20 #include "xml/composite-node-observer.h"
21 #include "util/list-container.h"
23 namespace Inkscape {
25 namespace XML {
27 class SimpleNode
28 : virtual public Node, public Inkscape::GC::Managed<>
29 {
30 public:
31 gchar const *name() const;
32 int code() const { return _name; }
33 void setCodeUnsafe(int code) {
34 _name = code;
35 }
37 Document *document() { return _document; }
38 Document const *document() const {
39 return const_cast<SimpleNode *>(this)->document();
40 }
42 Node *duplicate(Document* doc) const { return _duplicate(doc); }
44 Node *root();
45 Node const *root() const {
46 return const_cast<SimpleNode *>(this)->root();
47 }
49 Node *parent() { return _parent; }
50 Node const *parent() const { return _parent; }
52 Node *next() { return _next; }
53 Node const *next() const { return _next; }
55 Node *firstChild() { return _first_child; }
56 Node const *firstChild() const { return _first_child; }
57 Node *lastChild() { return _last_child; }
58 Node const *lastChild() const { return _last_child; }
60 unsigned childCount() const { return _child_count; }
61 Node *nthChild(unsigned index);
62 Node const *nthChild(unsigned index) const {
63 return const_cast<SimpleNode *>(this)->nthChild(index);
64 }
66 void addChild(Node *child, Node *ref);
67 void appendChild(Node *child) {
68 SimpleNode::addChild(child, _last_child);
69 }
70 void removeChild(Node *child);
71 void changeOrder(Node *child, Node *ref);
73 unsigned position() const;
74 void setPosition(int pos);
76 gchar const *attribute(gchar const *key) const;
77 void setAttribute(gchar const *key, gchar const *value, bool is_interactive=false);
78 bool matchAttributeName(gchar const *partial_name) const;
80 gchar const *content() const;
81 void setContent(gchar const *value);
83 void mergeFrom(Node const *src, gchar const *key);
85 Inkscape::Util::List<AttributeRecord const> attributeList() const {
86 return _attributes;
87 }
89 void synthesizeEvents(NodeEventVector const *vector, void *data);
90 void synthesizeEvents(NodeObserver &observer);
92 void addListener(NodeEventVector const *vector, void *data) {
93 g_assert(vector != NULL);
94 _observers.addListener(*vector, data);
95 }
96 void addObserver(NodeObserver &observer) {
97 _observers.add(observer);
98 }
99 void removeListenerByData(void *data) {
100 _observers.removeListenerByData(data);
101 }
102 void removeObserver(NodeObserver &observer) {
103 _observers.remove(observer);
104 }
106 protected:
107 SimpleNode(int code);
108 SimpleNode(SimpleNode const &repr);
110 virtual SimpleNode *_duplicate(Document *doc) const=0;
112 public: // ideally these should be protected somehow...
113 void _setParent(Node *parent) { _parent = parent; }
114 void _setNext(Node *next) { _next = next; }
115 void _bindDocument(Document &document);
117 unsigned _childPosition(Node const &child) const;
118 unsigned _cachedPosition() const { return _cached_position; }
119 void _setCachedPosition(unsigned position) const {
120 _cached_position = position;
121 }
123 private:
124 void operator=(Node const &); // no assign
126 Node *_parent;
127 Node *_next;
128 Document *_document;
129 mutable unsigned _cached_position;
131 int _name;
133 Inkscape::Util::MutableList<AttributeRecord> _attributes;
135 Inkscape::Util::ptr_shared<char> _content;
137 unsigned _child_count;
138 mutable bool _cached_positions_valid;
139 Node *_first_child;
140 Node *_last_child;
142 CompositeNodeObserver _observers;
143 };
145 }
147 }
149 #endif
150 /*
151 Local Variables:
152 mode:c++
153 c-file-style:"stroustrup"
154 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
155 indent-tabs-mode:nil
156 fill-column:99
157 End:
158 */
159 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :