summaryrefslogtreecommitdiffstats
path: root/src/xml/simple-node.h
blob: 367516ee797fbbf93f3ee5d68af855fe64aaf45f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 * SimpleNode - generic XML node implementation
 *
 * Copyright 2004-2005 MenTaLguY <mental@rydia.net>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * See the file COPYING for details.
 *
 */

#ifndef SEEN_INKSCAPE_XML_SIMPLE_NODE_H
#define SEEN_INKSCAPE_XML_SIMPLE_NODE_H

#include "xml/node.h"
#include "xml/attribute-record.h"
#include "xml/transaction-logger.h"
#include "xml/composite-node-observer.h"
#include "util/list-container.h"

namespace Inkscape {

namespace XML {

class SimpleNode
: virtual public Node, public Inkscape::GC::Managed<>
{
public:
    Session *session() {
        return ( _logger ? &_logger->session() : NULL );
    }

    gchar const *name() const;
    int code() const { return _name; }
    void setCodeUnsafe(int code) {
        g_assert(_logger == NULL);
        _name = code;
    }

    Document *document() { return _document; }
    Document const *document() const {
        return const_cast<SimpleNode *>(this)->document();
    }

    Node *duplicate() const { return _duplicate(); }

    Node *root();
    Node const *root() const {
        return const_cast<SimpleNode *>(this)->root();
    }

    Node *parent() { return _parent; }
    Node const *parent() const { return _parent; }

    Node *next() { return _next; }
    Node const *next() const { return _next; }

    Node *firstChild() { return _first_child; }
    Node const *firstChild() const { return _first_child; }
    Node *lastChild() { return _last_child; }
    Node const *lastChild() const { return _last_child; }

    unsigned childCount() const { return _child_count; }
    Node *nthChild(unsigned index);
    Node const *nthChild(unsigned index) const {
        return const_cast<SimpleNode *>(this)->nthChild(index);
    }

    void addChild(Node *child, Node *ref);
    void appendChild(Node *child) {
        SimpleNode::addChild(child, _last_child);
    }
    void removeChild(Node *child);
    void changeOrder(Node *child, Node *ref);

    unsigned position() const;
    void setPosition(int pos);

    gchar const *attribute(gchar const *key) const;
    void setAttribute(gchar const *key, gchar const *value, bool is_interactive=false);
    bool matchAttributeName(gchar const *partial_name) const;

    gchar const *content() const;
    void setContent(gchar const *value);

    void mergeFrom(Node const *src, gchar const *key);

    Inkscape::Util::List<AttributeRecord const> attributeList() const {
        return _attributes;
    }

    void synthesizeEvents(NodeEventVector const *vector, void *data);
    void synthesizeEvents(NodeObserver &observer);

    void addListener(NodeEventVector const *vector, void *data) {
        g_assert(vector != NULL);
        _observers.addListener(*vector, data);
    }
    void addObserver(NodeObserver &observer) {
        _observers.add(observer);
    }
    void removeListenerByData(void *data) {
        _observers.removeListenerByData(data);
    }
    void removeObserver(NodeObserver &observer) {
        _observers.remove(observer);
    }

protected:
    SimpleNode(int code);
    SimpleNode(SimpleNode const &repr);

    virtual SimpleNode *_duplicate() const=0;

public: // ideally these should be protected somehow...
    void _setParent(Node *parent) { _parent = parent; }
    void _setNext(Node *next) { _next = next; }
    void _bindDocument(Document &document);
    void _bindLogger(TransactionLogger &logger);

    unsigned _childPosition(Node const &child) const;
    unsigned _cachedPosition() const { return _cached_position; }
    void _setCachedPosition(unsigned position) const {
        _cached_position = position;
    }

private:
    void operator=(Node const &); // no assign

    Node *_parent;
    Node *_next;
    Document *_document;
    TransactionLogger *_logger;
    mutable unsigned _cached_position;

    int _name;

    Inkscape::Util::MutableList<AttributeRecord> _attributes;

    Inkscape::Util::ptr_shared<char> _content;

    unsigned _child_count;
    mutable bool _cached_positions_valid;
    Node *_first_child;
    Node *_last_child;

    CompositeNodeObserver _observers;
};

}

}

#endif
/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :