1 // Copyright (C) 2000-2002, Luca Padovani <luca.padovani@cs.unibo.it>.
3 // This file is part of GtkMathView, a Gtk widget for MathML.
5 // GtkMathView is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // GtkMathView is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with GtkMathView; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 // For details, see the GtkMathView World-Wide-Web page,
20 // http://www.cs.unibo.it/helm/mml-widget, or send a mail to
21 // <luca.padovani@cs.unibo.it>
29 #include <gdome-util.h>
31 #include <GdomeSmartDOM.hh>
33 #define MATHML_NS_URI "http://www.w3.org/1998/Math/MathML"
35 namespace DOM = GdomeSmartDOM;
38 getDepth(const DOM::Element& elem)
41 DOM::Element p = elem;
45 p = p.get_parentNode();
53 findCommonAncestor(const DOM::Element& first, const DOM::Element& last)
55 if (!first || !last) return DOM::Element(0);
57 DOM::Element p(first);
62 unsigned pDepth = getDepth(p);
63 unsigned qDepth = getDepth(q);
65 while (p && pDepth > qDepth)
67 p = p.get_parentNode();
71 while (q && qDepth > pDepth)
73 q = q.get_parentNode();
77 assert(pDepth == qDepth);
79 while (p && q && p != q)
81 p = p.get_parentNode();
82 q = q.get_parentNode();
90 findCommonSiblings(const DOM::Element& first, const DOM::Element& last,
91 DOM::Element& firstS, DOM::Element& lastS)
93 DOM::Element p(first);
98 unsigned pDepth = getDepth(p);
99 unsigned qDepth = getDepth(q);
101 while (p && pDepth > qDepth)
103 p = p.get_parentNode();
107 while (q && qDepth > pDepth)
109 q = q.get_parentNode();
113 assert(pDepth == qDepth);
115 while (p && q && p.get_parentNode() != q.get_parentNode())
117 p = p.get_parentNode();
118 q = q.get_parentNode();
127 leftmostChild(const DOM::Node& node)
129 if (!node) return node;
131 DOM::Node firstChild = node.get_firstChild();
132 if (!firstChild) return node;
134 return leftmostChild(firstChild);
138 rightmostChild(const DOM::Node& node)
140 if (!node) return node;
142 DOM::Node lastChild = node.get_lastChild();
143 if (!lastChild) return node;
145 return rightmostChild(lastChild);
149 leftSibling(const DOM::Node& node)
155 while (p.get_parentNode() && p.get_parentNode().get_firstChild() == p)
156 p = p.get_parentNode();
158 if (!p.get_parentNode()) return DOM::Node(0);
160 DOM::Node prevSibling = p.get_previousSibling();
163 return rightmostChild(prevSibling);
167 rightSibling(const DOM::Node& node)
173 DOM::Node firstChild = p.get_firstChild();
174 if (firstChild) return firstChild;
176 while (p.get_parentNode() && p.get_parentNode().get_lastChild() == p)
177 p = p.get_parentNode();
179 if (!p.get_parentNode()) return DOM::Node(0);
181 DOM::Node nextSibling = p.get_nextSibling();
184 return leftmostChild(nextSibling);
187 extern "C" GdomeElement*
188 find_common_ancestor(GdomeElement* first, GdomeElement* last)
190 DOM::Element p(first);
191 DOM::Element q(last);
192 return gdome_cast_el(findCommonAncestor(p, q).gdome_object());
195 extern "C" GdomeElement*
196 find_self_or_ancestor(GdomeElement* elem, const char* uri, const char* name)
198 DOM::Element el(elem);
200 while (el && (el.get_namespaceURI() != uri || el.get_localName() != name))
201 el = el.get_parentNode();
203 return gdome_cast_el(el.gdome_object());
207 action_toggle(GdomeElement* elem)
209 DOM::Element el(elem);
210 if (el.get_namespaceURI() != MATHML_NS_URI || el.get_localName() != "maction") return;
213 if (el.hasAttribute("selection"))
214 idx = atoi(std::string(el.getAttribute("selection")).c_str());
219 std::ostringstream os;
221 el.setAttribute("selection", os.str());
225 find_common_siblings(GdomeElement* first, GdomeElement* last,
226 GdomeElement** firstS, GdomeElement** lastS)
231 findCommonSiblings(DOM::Element(first), DOM::Element(last), fs, ls);
233 if (firstS != NULL) *firstS = gdome_cast_el(fs.gdome_object());
234 if (lastS != NULL) *lastS = gdome_cast_el(ls.gdome_object());
238 findElementWithAttribute(const DOM::Element& elem, const std::string& name)
240 DOM::Element el(elem);
241 while (el && !el.hasAttribute(name)) el = el.get_parentNode();
246 findElementWithAttributeNS(const DOM::Element& elem, const std::string& ns_uri, const std::string& name)
248 DOM::Element el(elem);
249 while (el && !el.hasAttributeNS(ns_uri, name)) el = el.get_parentNode();
253 extern "C" GdomeElement*
254 find_xref_element(GdomeElement* elem)
256 DOM::Element el = findElementWithAttribute(DOM::Element(elem), "xref");
257 return gdome_cast_el(el.gdome_object());
260 extern "C" GdomeDOMString*
261 find_hyperlink(GdomeElement* elem)
263 DOM::Element el = findElementWithAttribute(DOM::Element(elem),"href");
264 if (el) return el.getAttribute("href").gdome_str();
270 delete_element(GdomeElement* elem)
272 DOM::Element p(elem);
274 DOM::Element parent = p.get_parentNode();
277 parent.removeChild(p);