]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/mathml_editor/test/aux.cc
Initial revision
[helm.git] / helm / DEVEL / mathml_editor / test / aux.cc
1 // Copyright (C) 2000-2002, Luca Padovani <luca.padovani@cs.unibo.it>.
2 //
3 // This file is part of GtkMathView, a Gtk widget for MathML.
4 // 
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.
9 //
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.
14 //
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.
18 // 
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>
22
23 #include <config.h>
24 #include <assert.h>
25
26 #include <gdome.h>
27 #include <gdome-util.h>
28 #include <GdomeSmartDOM.hh>
29
30 //#include "gmetadom.hh"
31
32 namespace DOM = GdomeSmartDOM;
33
34 static unsigned
35 getDepth(const DOM::Element& elem)
36 {
37   unsigned length = 0;
38   DOM::Element p = elem;
39
40   while (p)
41     {
42       p = p.get_parentNode();
43       length++;
44     }
45
46   return length;
47 }
48
49 static DOM::Element
50 findCommonAncestor(const DOM::Element& first, const DOM::Element& last)
51 {
52   assert(first);
53   assert(last);
54
55   DOM::Element p(first);
56   DOM::Element q(last);
57
58   if (p != q)
59     {
60       unsigned pDepth = getDepth(p);
61       unsigned qDepth  = getDepth(q);
62
63       while (p && pDepth > qDepth)
64         {
65           p = p.get_parentNode();
66           pDepth--;
67         }
68
69       while (q && qDepth > pDepth)
70         {
71           q = q.get_parentNode();
72           qDepth--;
73         }
74
75       assert(pDepth == qDepth);
76
77       while (p && q && p != q)
78         {
79           p = p.get_parentNode();
80           q = q.get_parentNode();
81         }
82     }
83   
84   return p;
85 }
86
87 static void
88 findCommonSiblings(const DOM::Element& first, const DOM::Element& last,
89                    DOM::Element& firstS, DOM::Element& lastS)
90 {
91   assert(first);
92   assert(last);
93
94   DOM::Element p(first);
95   DOM::Element q(last);
96
97   if (p != q)
98     {
99       unsigned pDepth = getDepth(p);
100       unsigned qDepth  = getDepth(q);
101
102       while (p && pDepth > qDepth)
103         {
104           p = p.get_parentNode();
105           pDepth--;
106         }
107
108       while (q && qDepth > pDepth)
109         {
110           q = q.get_parentNode();
111           qDepth--;
112         }
113
114       assert(pDepth == qDepth);
115
116       while (p && q && p.get_parentNode() != q.get_parentNode())
117         {
118           p = p.get_parentNode();
119           q = q.get_parentNode();
120         }
121     }
122
123   firstS = p;
124   lastS = q;
125 }
126
127 static DOM::Node
128 leftmostChild(const DOM::Node& node)
129 {
130   if (!node) return node;
131
132   DOM::Node firstChild = node.get_firstChild();
133   if (!firstChild) return node;
134
135   return leftmostChild(firstChild);
136 }
137
138 static DOM::Node
139 rightmostChild(const DOM::Node& node)
140 {
141   if (!node) return node;
142
143   DOM::Node lastChild = node.get_lastChild();
144   if (!lastChild) return node;
145
146   return rightmostChild(lastChild);
147 }
148
149 static DOM::Node
150 leftSibling(const DOM::Node& node)
151 {
152   DOM::Node p = node;
153
154   if (!p) return p;
155
156   while (p.get_parentNode() && p.get_parentNode().get_firstChild() == p)
157     p = p.get_parentNode();
158
159   if (!p.get_parentNode()) return DOM::Node(0);
160
161   DOM::Node prevSibling = p.get_previousSibling();
162   assert(prevSibling);
163
164   return rightmostChild(prevSibling);
165 }
166
167 static DOM::Node
168 rightSibling(const DOM::Node& node)
169 {
170   DOM::Node p = node;
171
172   if (!p) return p;
173
174   DOM::Node firstChild = p.get_firstChild();
175   if (firstChild) return firstChild;
176
177   while (p.get_parentNode() && p.get_parentNode().get_lastChild() == p)
178     p = p.get_parentNode();
179
180   if (!p.get_parentNode()) return DOM::Node(0);
181
182   DOM::Node nextSibling = p.get_nextSibling();
183   assert(nextSibling);
184
185   return leftmostChild(nextSibling);
186 }
187
188 extern "C" GdomeElement*
189 find_common_ancestor(GdomeElement* first, GdomeElement* last)
190 {
191   DOM::Element p(first);
192   DOM::Element q(last);
193   if (GdomeElement* res = gdome_cast_el(findCommonAncestor(p, q).gdome_object()))
194     {
195       GdomeException exc = 0;
196       gdome_el_ref(res, &exc);
197       assert(exc == 0);
198       return res;
199     }
200   else
201     return 0;
202 }
203
204 extern "C" void
205 find_common_siblings(GdomeElement* first, GdomeElement* last,
206                      GdomeElement** firstS, GdomeElement** lastS)
207 {
208   DOM::Element fs(0);
209   DOM::Element ls(0);
210
211   findCommonSiblings(DOM::Element(first), DOM::Element(last), fs, ls);
212
213   if (firstS != NULL) *firstS = gdome_cast_el(fs.gdome_object());
214   if (lastS != NULL) *lastS = gdome_cast_el(ls.gdome_object());
215 }
216
217 extern "C" void
218 delete_element(GdomeElement* elem)
219 {
220   DOM::Element p(elem);
221
222   DOM::Element parent = p.get_parentNode();
223   assert(parent);
224
225   parent.removeChild(p);
226 }
227