1 /* This file is part of EdiTeX, an editor of mathematical
2 * expressions based on TeX syntax.
4 * Copyright (C) 2002-2003 Luca Padovani <lpadovan@cs.unibo.it>,
5 * 2003 Paolo Marinelli <pmarinel@cs.unibo.it>.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * For more information, please visit the project's home page
22 * http://helm.cs.unibo.it/editex/
23 * or send an email to <lpadovan@cs.unibo.it>
30 #include "TDocument.hh"
32 TDocument::TDocument()
34 DOM::DOMImplementation di;
36 doc = di.createDocument(TML_NS_URI, "tml:tex", dt);
37 DOM::Element root = doc.get_documentElement();
39 root.setAttributeNS(XMLNS_NS_URI, "xmlns:tml", TML_NS_URI);
41 DOM::EventTarget et(doc);
43 et.addEventListener("DOMSubtreeModified", *this, false);
46 TDocument::~TDocument()
48 //DOM::Element root = doc.get_documentElement();
49 DOM::EventTarget et(doc);
51 et.removeEventListener("DOMSubtreeModified", *this, false);
57 DOM::Element root = doc.createElementNS(TML_NS_URI, "tml:tex");
58 root.setAttributeNS(XMLNS_NS_URI, "xmlns:tml", TML_NS_URI);
59 doc.replaceChild(root, doc.get_documentElement());
64 TDocument::serialize(const char* filename) const
66 DOM::DOMImplementation di;
67 di.saveDocumentToFile(doc, filename, GDOME_SAVE_LIBXML_INDENT);
71 TDocument::makeId(unsigned id)
79 TDocument::create(const std::string& name, unsigned id) const
81 DOM::Element elem = doc.createElementNS(TML_NS_URI, "tml:" + name);
82 if (id > 0) elem.setAttribute("id", makeId(id));
87 TDocument::createC(const std::string& name, unsigned id) const
89 TNode m = create("c", id);
95 TDocument::createT(const std::string& name, const std::string& text, unsigned id) const
97 TNode t = create(name, id);
103 TDocument::nodeDepth(const DOM::Node& node)
111 n = n.get_parentNode();
117 TDocument::findCommonAncestor(const DOM::Node& node1, const DOM::Node& node2)
119 DOM::Node n1 = node1;
120 DOM::Node n2 = node2;
122 unsigned d1 = nodeDepth(n1);
123 unsigned d2 = nodeDepth(n2);
125 // cout << "finding common ancestor " << d1 << " " << d2 << endl;
130 n2 = n2.get_parentNode();
137 n1 = n1.get_parentNode();
145 n1 = n1.get_parentNode();
146 n2 = n2.get_parentNode();
153 TDocument::findIdNode(const DOM::Node& node)
158 if (n.get_nodeType() == DOM::Node::ELEMENT_NODE)
161 if (el.hasAttribute("id")) return el;
163 n = n.get_parentNode();
170 TDocument::getNodeByIdAux(const TNode& node, const std::string& id)
172 if (node.hasId(id)) return node;
174 for (TNode p = node.first(); p; p = p.next())
175 if (TNode res = getNodeByIdAux(p, id)) return res;
180 TDocument::getNodeById(unsigned id) const
182 DOM::Element root = doc.get_documentElement();
184 return getNodeByIdAux(root, makeId(id));
188 TDocument::handleEvent(const DOM::Event& ev)
190 DOM::MutationEvent me(ev);
195 cout << "TDocument::handleEvent DIRTY BEFORE = " << dirty.getAttribute("id") << endl;
197 cout << "TDocument::handleEvent DIRTY BEFORE = (nil)" << endl;
200 if (DOM::Node node = me.get_target())
202 dirty = findIdNode(findCommonAncestor(dirty, node));
204 dirty = findIdNode(node);
209 cout << "TDocument::handleEvent target = " << DOM::Node(me.get_target()).get_nodeName() << " DIRTY AFTER = "
210 << dirty.getAttribute("id") << " ME = " << DOM::Node(me.get_target()).get_nodeName() << endl;