Code

implement rendering of clips/masks in outline mode; make all outline colors stored...
[inkscape.git] / src / xml / simple-node.cpp
1 /*
2  * SimpleNode - simple XML node implementation
3  *
4  * Copyright 2003-2005 MenTaLguY <mental@rydia.net>
5  * Copyright 2003 Nathan Hurst
6  * Copyright 1999-2003 Lauris Kaplinski
7  * Copyright 2000-2002 Ximian Inc.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * See the file COPYING for details.
15  *
16  */
18 #include <glib/gstrfuncs.h>
19 #include "xml/simple-node.h"
20 #include "xml/node-event-vector.h"
21 #include "xml/node-fns.h"
22 #include "xml/repr.h"
23 #include "debug/event-tracker.h"
24 #include "debug/simple-event.h"
25 #include "util/share.h"
26 #include "util/format.h"
28 namespace Inkscape {
30 namespace XML {
32 namespace {
34 Util::ptr_shared<char> stringify_node(Node const &node) {
35     gchar *string;
36     switch (node.type()) {
37     case ELEMENT_NODE: {
38         char const *id=node.attribute("id");
39         if (id) {
40             string = g_strdup_printf("element(%p)=%s(#%s)", &node, node.name(), id);
41         } else {
42             string = g_strdup_printf("element(%p)=%s", &node, node.name());
43         }
44     } break;
45     case TEXT_NODE:
46         string = g_strdup_printf("text(%p)=%s", &node, node.content());
47         break;
48     case COMMENT_NODE:
49         string = g_strdup_printf("comment(%p)=<!--%s-->", &node, node.content());
50         break;
51     case DOCUMENT_NODE:
52         string = g_strdup_printf("document(%p)", &node);
53         break;
54     default:
55         string = g_strdup_printf("unknown(%p)", &node);
56     }
57     Util::ptr_shared<char> result=Util::share_string(string);
58     g_free(string);
59     return result;
60 }
62 typedef Debug::SimpleEvent<Debug::Event::XML> DebugXML;
64 class DebugXMLNode : public DebugXML {
65 public:
66     DebugXMLNode(Node const &node, Util::ptr_shared<char> name)
67     : DebugXML(name)
68     {
69         _addProperty("node", stringify_node(node));
70     }
71 };
73 class DebugAddChild : public DebugXMLNode {
74 public:
75     DebugAddChild(Node const &node, Node const &child, Node const *prev)
76     : DebugXMLNode(node, Util::share_static_string("add-child"))
77     {
78         _addProperty("child", stringify_node(child));
79         _addProperty("position", Util::format("%d", ( prev ? prev->position() + 1 : 0 )));
80     }
81 };
83 class DebugRemoveChild : public DebugXMLNode {
84 public:
85     DebugRemoveChild(Node const &node, Node const &child)
86     : DebugXMLNode(node, Util::share_static_string("remove-child"))
87     {
88         _addProperty("child", stringify_node(child));
89     }
90 };
92 class DebugSetChildPosition : public DebugXMLNode {
93 public:
94     DebugSetChildPosition(Node const &node, Node const &child,
95                           Node const *old_prev, Node const *new_prev)
96     : DebugXMLNode(node, Util::share_static_string("set-child-position"))
97     {
98         _addProperty("child", stringify_node(child));
100         unsigned old_position = ( old_prev ? old_prev->position() : 0 );
101         unsigned position = ( new_prev ? new_prev->position() : 0 );
102         if ( position > old_position ) {
103             --position;
104         }
106         _addProperty("position", Util::format("%d", position));
107     }
108 };
110 class DebugSetContent : public DebugXMLNode {
111 public:
112     DebugSetContent(Node const &node,
113                     Util::ptr_shared<char> content)
114     : DebugXMLNode(node, Util::share_static_string("set-content"))
115     {
116         _addProperty("content", content);
117     }
118 };
120 class DebugClearContent : public DebugXMLNode {
121 public:
122     DebugClearContent(Node const &node)
123     : DebugXMLNode(node, Util::share_static_string("clear-content"))
124     {}
125 };
127 class DebugSetAttribute : public DebugXMLNode {
128 public:
129     DebugSetAttribute(Node const &node,
130                       GQuark name,
131                       Util::ptr_shared<char> value)
132     : DebugXMLNode(node, Util::share_static_string("set-attribute"))
133     {
134         _addProperty("name", Util::share_static_string(g_quark_to_string(name)));
135         _addProperty("value", value);
136     }
137 };
139 class DebugClearAttribute : public DebugXMLNode {
140 public:
141     DebugClearAttribute(Node const &node, GQuark name)
142     : DebugXMLNode(node, Util::share_static_string("clear-attribute"))
143     {
144         _addProperty("name", Util::share_static_string(g_quark_to_string(name)));
145     }
146 };
150 using Util::ptr_shared;
151 using Util::share_string;
152 using Util::share_unsafe;
153 using Util::share_static_string;
154 using Util::List;
155 using Util::MutableList;
156 using Util::cons;
157 using Util::rest;
158 using Util::set_rest;
160 SimpleNode::SimpleNode(int code)
161 : Node(), _name(code), _attributes(), _child_count(0),
162   _cached_positions_valid(false)
164     this->_logger = NULL;
165     this->_document = NULL;
166     this->_parent = this->_next = NULL;
167     this->_first_child = this->_last_child = NULL;
170 SimpleNode::SimpleNode(SimpleNode const &node)
171 : Node(),
172   _cached_position(node._cached_position),
173   _name(node._name), _attributes(), _content(node._content),
174   _child_count(node._child_count),
175   _cached_positions_valid(node._cached_positions_valid)
177     _logger = NULL;
178     _document = NULL;
179     _parent = _next = NULL;
180     _first_child = _last_child = NULL;
182     for ( Node *child = node._first_child ;
183           child != NULL ; child = child->next() )
184     {
185         Node *child_copy=child->duplicate();
187         child_copy->_setParent(this);
188         if (_last_child) {
189             _last_child->_setNext(child_copy);
190         } else {
191             _first_child = child_copy;
192         }
193         _last_child = child_copy;
195         child_copy->release(); // release to avoid a leak
196     }
198     for ( List<AttributeRecord const> iter = node._attributes ;
199           iter ; ++iter )
200     {
201         _attributes = cons(*iter, _attributes);
202     }
205 gchar const *SimpleNode::name() const {
206     return g_quark_to_string(_name);
209 gchar const *SimpleNode::content() const {
210     return this->_content;
213 gchar const *SimpleNode::attribute(gchar const *name) const {
214     g_return_val_if_fail(name != NULL, NULL);
216     GQuark const key = g_quark_from_string(name);
218     for ( List<AttributeRecord const> iter = _attributes ;
219           iter ; ++iter )
220     {
221         if ( iter->key == key ) {
222             return iter->value;
223         }
224     }
226     return NULL;
229 unsigned SimpleNode::position() const {
230     g_return_val_if_fail(_parent != NULL, 0);
231     return _parent->_childPosition(*this);
234 unsigned SimpleNode::_childPosition(Node const &child) const {
235     if (!_cached_positions_valid) {
236         unsigned position=0;
237         for ( Node *sibling = _first_child ;
238               sibling ; sibling = sibling->next() )
239         {
240             sibling->_setCachedPosition(position);
241             position++;
242         }
243         _cached_positions_valid = true;
244     }
245     return child._cachedPosition();
248 Node *SimpleNode::nthChild(unsigned index) {
249     Node *child = _first_child;
250     for ( ; index > 0 && child ; child = child->next() ) {
251         index--;
252     }
253     return child;
256 bool SimpleNode::matchAttributeName(gchar const *partial_name) const {
257     g_return_val_if_fail(partial_name != NULL, false);
259     for ( List<AttributeRecord const> iter = _attributes ;
260           iter ; ++iter )
261     {
262         gchar const *name = g_quark_to_string(iter->key);
263         if (std::strstr(name, partial_name)) {
264             return true;
265         }
266     }
268     return false;
271 void SimpleNode::setContent(gchar const *content) {
272     ptr_shared<char> old_content=_content;
273     ptr_shared<char> new_content = ( content ? share_string(content) : ptr_shared<char>() );
275     Debug::EventTracker<> tracker;
276     if (new_content) {
277         tracker.set<DebugSetContent>(*this, new_content);
278     } else {
279         tracker.set<DebugClearContent>(*this);
280     }
282     _content = new_content;
284     if ( _content != old_content ) {
285         if (_logger) {
286             _logger->notifyContentChanged(*this, old_content, _content);
287         }
289         _observers.notifyContentChanged(*this, old_content, _content);
290     }
293 void
294 SimpleNode::setAttribute(gchar const *name, gchar const *value, bool const is_interactive)
296     g_return_if_fail(name && *name);
298     GQuark const key = g_quark_from_string(name);
300     MutableList<AttributeRecord> ref;
301     MutableList<AttributeRecord> existing;
302     for ( existing = _attributes ; existing ; ++existing ) {
303         if ( existing->key == key ) {
304             break;
305         }
306         ref = existing;
307     }
309     Debug::EventTracker<> tracker;
311     ptr_shared<char> old_value=( existing ? existing->value : ptr_shared<char>() );
313     ptr_shared<char> new_value=ptr_shared<char>();
314     if (value) {
315         new_value = share_string(value);
316         tracker.set<DebugSetAttribute>(*this, key, new_value);
317         if (!existing) {
318             if (ref) {
319                 set_rest(ref, MutableList<AttributeRecord>(AttributeRecord(key, new_value)));
320             } else {
321                 _attributes = MutableList<AttributeRecord>(AttributeRecord(key, new_value));
322             }
323         } else {
324             existing->value = new_value;
325         }
326     } else {
327         tracker.set<DebugClearAttribute>(*this, key);
328         if (existing) {
329             if (ref) {
330                 set_rest(ref, rest(existing));
331             } else {
332                 _attributes = rest(existing);
333             }
334             set_rest(existing, MutableList<AttributeRecord>());
335         }
336     }
338     if ( new_value != old_value && (!old_value || !new_value || strcmp(old_value, new_value))) {
339         if (_logger) {
340             _logger->notifyAttributeChanged(*this, key, old_value, new_value);
341         }
343         _observers.notifyAttributeChanged(*this, key, old_value, new_value);
344     }
347 void SimpleNode::addChild(Node *child, Node *ref) {
348     g_assert(child);
349     g_assert(!ref || ref->parent() == this);
350     g_assert(!child->parent());
352     Debug::EventTracker<DebugAddChild> tracker(*this, *child, ref);
354     Node *next;
355     if (ref) {
356         next = ref->next();
357         ref->_setNext(child);
358     } else {
359         next = _first_child;
360         _first_child = child;
361     }
362     if (!next) { // appending?
363         _last_child = child;
364         // set cached position if possible when appending
365         if (!ref) {
366             // if !next && !ref, child is sole child
367             child->_setCachedPosition(0);
368             _cached_positions_valid = true;
369         } else if (_cached_positions_valid) {
370             child->_setCachedPosition(ref->_cachedPosition() + 1);
371         }
372     } else {
373         // invalidate cached positions otherwise
374         _cached_positions_valid = false;
375     }
377     child->_setParent(this);
378     child->_setNext(next);
379     _child_count++;
381     if (_document) {
382         child->_bindDocument(*_document);
383     }
384     if (_logger) {
385         child->_bindLogger(*_logger);
386         _logger->notifyChildAdded(*this, *child, ref);
387     }
389     _observers.notifyChildAdded(*this, *child, ref);
392 void SimpleNode::_bindDocument(Document &document) {
393     g_assert(!_document || _document == &document);
395     if (!_document) {
396         _document = &document;
398         for ( Node *child = _first_child ; child != NULL ; child = child->next() ) {
399             child->_bindDocument(document);
400         }
401     }
404 void SimpleNode::_bindLogger(TransactionLogger &logger) {
405     g_assert(!_logger || _logger == &logger);
407     if (!_logger) {
408         _logger = &logger;
410         for ( Node *child = _first_child ; child != NULL ; child = child->next() ) {
411             child->_bindLogger(logger);
412         }
413     }
416 void SimpleNode::removeChild(Node *child) {
417     g_assert(child);
418     g_assert(child->parent() == this);
420     Node *ref = ( child != _first_child ? previous_node(child) : NULL );
422     Debug::EventTracker<DebugRemoveChild> tracker(*this, *child);
424     Node *next = child->next();
425     if (ref) {
426         ref->_setNext(next);
427     } else {
428         _first_child = next;
429     }
430     if (!next) { // removing the last child?
431         _last_child = ref;
432     } else {
433         // removing any other child invalidates the cached positions
434         _cached_positions_valid = false;
435     }
437     child->_setNext(NULL);
438     child->_setParent(NULL);
439     _child_count--;
441     if (_logger) {
442         _logger->notifyChildRemoved(*this, *child, ref);
443     }
445     _observers.notifyChildRemoved(*this, *child, ref);
448 void SimpleNode::changeOrder(Node *child, Node *ref) {
449     g_return_if_fail(child);
450     g_return_if_fail(child->parent() == this);
451     g_return_if_fail(child != ref);
452     g_return_if_fail(!ref || ref->parent() == this);
454     Node *const prev = previous_node(child);
456     Debug::EventTracker<DebugSetChildPosition> tracker(*this, *child, prev, ref);
458     if (prev == ref) { return; }
460     Node *next;
462     /* Remove from old position. */
463     next=child->next();
464     if (prev) {
465         prev->_setNext(next);
466     } else {
467         _first_child = next;
468     }
469     if (!next) {
470         _last_child = prev;
471     }
473     /* Insert at new position. */
474     if (ref) {
475         next = ref->next();
476         ref->_setNext(child);
477     } else {
478         next = _first_child;
479         _first_child = child;
480     }
481     child->_setNext(next);
482     if (!next) {
483         _last_child = child;
484     }
486     _cached_positions_valid = false;
488     if (_logger) {
489         _logger->notifyChildOrderChanged(*this, *child, prev, ref);
490     }
492     _observers.notifyChildOrderChanged(*this, *child, prev, ref);
495 void SimpleNode::setPosition(int pos) {
496     g_return_if_fail(_parent != NULL);
498     // a position beyond the end of the list means the end of the list;
499     // a negative position is the same as an infinitely large position
501     Node *ref=NULL;
502     for ( Node *sibling = _parent->firstChild() ;
503           sibling && pos ; sibling = sibling->next() )
504     {
505         if ( sibling != this ) {
506             ref = sibling;
507             pos--;
508         }
509     }
511     _parent->changeOrder(this, ref);
514 namespace {
516 void child_added(Node *node, Node *child, Node *ref, void *data) {
517     reinterpret_cast<NodeObserver *>(data)->notifyChildAdded(*node, *child, ref);
520 void child_removed(Node *node, Node *child, Node *ref, void *data) {
521     reinterpret_cast<NodeObserver *>(data)->notifyChildRemoved(*node, *child, ref);
524 void content_changed(Node *node, gchar const *old_content, gchar const *new_content, void *data) {
525     reinterpret_cast<NodeObserver *>(data)->notifyContentChanged(*node, Util::share_unsafe((const char *)old_content), Util::share_unsafe((const char *)new_content));
528 void attr_changed(Node *node, gchar const *name, gchar const *old_value, gchar const *new_value, bool is_interactive, void *data) {
529     reinterpret_cast<NodeObserver *>(data)->notifyAttributeChanged(*node, g_quark_from_string(name), Util::share_unsafe((const char *)old_value), Util::share_unsafe((const char *)new_value));
532 void order_changed(Node *node, Node *child, Node *old_ref, Node *new_ref, void *data) {
533     reinterpret_cast<NodeObserver *>(data)->notifyChildOrderChanged(*node, *child, old_ref, new_ref);
536 const NodeEventVector OBSERVER_EVENT_VECTOR = {
537     &child_added,
538     &child_removed,
539     &attr_changed,
540     &content_changed,
541     &order_changed
542 };
544 };
546 void SimpleNode::synthesizeEvents(NodeEventVector const *vector, void *data) {
547     if (vector->attr_changed) {
548         for ( List<AttributeRecord const> iter = _attributes ;
549               iter ; ++iter )
550         {
551             vector->attr_changed(this, g_quark_to_string(iter->key), NULL, iter->value, false, data);
552         }
553     }
554     if (vector->child_added) {
555         Node *ref = NULL;
556         for ( Node *child = this->_first_child ;
557               child ; child = child->next() )
558         {
559             vector->child_added(this, child, ref, data);
560             ref = child;
561         }
562     }
563     if (vector->content_changed) {
564         vector->content_changed(this, NULL, this->_content, data);
565     }
568 void SimpleNode::synthesizeEvents(NodeObserver &observer) {
569     synthesizeEvents(&OBSERVER_EVENT_VECTOR, &observer);
572 Node *SimpleNode::root() {
573     Node *parent=this;
574     while (parent->parent()) {
575         parent = parent->parent();
576     }
578     if ( parent->type() == DOCUMENT_NODE ) {
579         for ( Node *child = _document->firstChild() ;
580               child ; child = child->next() )
581         {
582             if ( child->type() == ELEMENT_NODE ) {
583                 return child;
584             }
585         }
586         return NULL;
587     } else if ( parent->type() == ELEMENT_NODE ) {
588         return parent;
589     } else {
590         return NULL;
591     }
594 void SimpleNode::mergeFrom(Node const *src, gchar const *key) {
595     g_return_if_fail(src != NULL);
596     g_return_if_fail(key != NULL);
597     g_assert(src != this);
599     setContent(src->content());
601     for ( Node const *child = src->firstChild() ; child != NULL ; child = child->next() )
602     {
603         gchar const *id = child->attribute(key);
604         if (id) {
605             Node *rch=sp_repr_lookup_child(this, key, id);
606             if (rch) {
607                 rch->mergeFrom(child, key);
608             } else {
609                 rch = child->duplicate();
610                 appendChild(rch);
611                 rch->release();
612             }
613         } else {
614             Node *rch=child->duplicate();
615             appendChild(rch);
616             rch->release();
617         }
618     }
620     for ( List<AttributeRecord const> iter = src->attributeList() ;
621           iter ; ++iter )
622     {
623         setAttribute(g_quark_to_string(iter->key), iter->value);
624     }
631 /*
632   Local Variables:
633   mode:c++
634   c-file-style:"stroustrup"
635   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
636   indent-tabs-mode:nil
637   fill-column:99
638   End:
639 */
640 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :