]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/mathml_editor/src/TNode.cc
* added license and copyright to every source file
[helm.git] / helm / DEVEL / mathml_editor / src / TNode.cc
1 /* Copyright (C) 2002-2003, Luca Padovani <luca.padovani@cs.unibo.it>,
2  *                    2003, Paolo Marinelli <pmarinel@cs.unibo.it>.
3  *
4  * This file is part of EdiTeX, an editor of mathematical
5  * expressions based on TeX syntax
6  * 
7  * EdiTeX is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * EdiTeX 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
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with EdiTeX; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20  * 
21  * For details, see the EdiTeX World-Wide-Web page,
22  * http://helm.cs.unibo.it/editex, or send a mail to
23  * <luca.padovani@cs.unibo.it>
24  */
25
26 #include "globals.hh"
27 #include "TNode.hh"
28
29 TNode
30 TNode::next() const
31 {
32   assert(node);
33   DOM::Node p = node.get_nextSibling();
34   while (p && p.get_nodeType() != DOM::Node::ELEMENT_NODE) p = p.get_nextSibling();
35   return p;
36 }
37
38 TNode
39 TNode::nextL() const
40 {
41   assert(node);
42   if (TNode n = next())
43     if (n.isG()) return n.firstL();
44     else return n;
45   else return TNode();
46 }
47
48 TNode
49 TNode::prev() const
50 {
51   assert(node);
52   DOM::Node p = node.get_previousSibling();
53   while (p && p.get_nodeType() != DOM::Node::ELEMENT_NODE) p = p.get_previousSibling();
54   return p;
55 }
56
57 TNode
58 TNode::prevL() const
59 {
60   assert(node);
61   if (TNode n = prev())
62     if (n.isG()) return n.lastL();
63     else return n;
64   else return TNode();
65 }
66
67 TNode
68 TNode::last() const
69 {
70   assert(node);
71   DOM::Node p = node.get_lastChild();
72   while (p && p.get_nodeType() != DOM::Node::ELEMENT_NODE) p = p.get_previousSibling();
73   return p;
74 }
75
76 TNode
77 TNode::lastL() const
78 {
79   assert(node);
80   if (TNode n = last())
81     if (n.isG()) return n.lastL();
82     else return n;
83   else
84     return TNode();
85 }
86
87 TNode
88 TNode::first() const
89 {
90   assert(node);
91   DOM::Node p = node.get_firstChild();
92   while (p && p.get_nodeType() != DOM::Node::ELEMENT_NODE) p = p.get_nextSibling();
93   return p;
94 }
95
96 TNode
97 TNode::firstL() const
98 {
99   assert(node);
100   if (TNode n = first())
101     if (n.isG()) return n.firstL();
102     else return n;
103   else
104     return TNode();
105 }
106
107 TNode
108 TNode::core() const
109 {
110   assert(node);
111   // check also if there is a macro embellishment (\not)
112   if (isSb() || isSp()) return first().core();
113   else return *this;
114 }
115
116 TNode
117 TNode::parent() const
118 {
119   assert(node);
120   DOM::Node p = node.get_parentNode();
121   assert(!p || p.get_nodeType() == DOM::Node::ELEMENT_NODE);
122   return p;
123 }
124
125 unsigned
126 TNode::size() const
127 {
128   assert(node);
129   unsigned size = 0;
130   TNode p = first();
131   while (p)
132     {
133       p = p.next();
134       size++;
135     }
136
137   return size;
138 }
139
140 void
141 TNode::remove() const
142 {
143   assert(node);
144   DOM::Node parent = node.get_parentNode();
145   parent.removeChild(node);
146 }
147
148 void
149 TNode::replace(const TNode& newNode) const
150 {
151   assert(node);
152   DOM::Node parent = node.get_parentNode();
153   parent.replaceChild(newNode.node, node);
154 }
155
156 void
157 TNode::replace(const TNode& first, const TNode& last) const
158 {
159   assert(node);
160   assert(first);
161
162   TNode p = first;
163   while (p != last)
164     {
165       TNode next = p.next();
166       insert(p);
167       p = next;
168     }
169   remove();
170 }
171
172 void
173 TNode::insert(const TNode& newNode) const
174 {
175   assert(node);
176   DOM::Node parent = node.get_parentNode();
177   parent.insertBefore(newNode.node, node);
178 }
179
180 void
181 TNode::append(const TNode& newNode) const
182 {
183   assert(node);
184   node.appendChild(newNode.node);
185 }
186
187 void
188 TNode::append(const TNode& first, const TNode& last) const
189 {
190   assert(node);
191   assert(first);
192   assert(last);
193
194   TNode p = first;
195   while (p != last)
196     {
197       TNode next = p.next();
198       append(p);
199       p = next;
200     }
201 }
202
203 void
204 TNode::prepend(const TNode& newNode) const
205 {
206   assert(node);
207   DOM::Node parent = node.get_parentNode();
208   parent.insertBefore(newNode.node, parent.get_firstChild());
209 }
210
211 #if 0
212 #endif
213
214 TNode
215 TNode::child(unsigned pos) const
216 {
217   assert(node);
218   TNode p = first();
219   while (p && pos-- > 0) p = p.next();
220   return p;
221 }
222
223 std::string
224 TNode::get(const std::string& name) const
225 {
226   assert(node);
227   return node.getAttribute(name);
228 }
229
230 void
231 TNode::set(const std::string& name, const std::string& value) const
232 {
233   assert(node);
234   node.setAttribute(name, value);
235 }