Code

98d363f3487269ead181fda29094162ecf212056
[inkscape.git] / src / xml / simple-node.h
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 private:
122     void operator=(Node const &); // no assign
124     void _setParent(SimpleNode *parent);
125     unsigned _childPosition(SimpleNode const &child) const;
127     SimpleNode *_parent;
128     SimpleNode *_next;
129     Document *_document;
130     mutable unsigned _cached_position;
132     int _name;
134     Inkscape::Util::MutableList<AttributeRecord> _attributes;
136     Inkscape::Util::ptr_shared<char> _content;
138     unsigned _child_count;
139     mutable bool _cached_positions_valid;
140     SimpleNode *_first_child;
141     SimpleNode *_last_child;
143     CompositeNodeObserver _observers;
144     CompositeNodeObserver _subtree_observers;
145 };
151 #endif
152 /*
153   Local Variables:
154   mode:c++
155   c-file-style:"stroustrup"
156   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
157   indent-tabs-mode:nil
158   fill-column:99
159   End:
160 */
161 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :