]> git.lyx.org Git - lyx.git/blob - src/mathed/math_data.C
Fix to bug 2362: Deleting superscript also deletes subscript.
[lyx.git] / src / mathed / math_data.C
1 /**
2  * \file math_data.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "math_data.h"
14 #include "math_fontinset.h"
15 #include "math_scriptinset.h"
16 #include "math_macro.h"
17 #include "math_macrotable.h"
18 #include "math_mathmlstream.h"
19 #include "math_support.h"
20 #include "math_replace.h"
21
22 #include "coordcache.h"
23 #include "LColor.h"
24 #include "BufferView.h"
25 #include "buffer.h"
26 #include "cursor.h"
27 #include "debug.h"
28
29 #include "frontends/Painter.h"
30
31 #include <boost/assert.hpp>
32
33 using std::abs;
34 using std::endl;
35 using std::min;
36 using std::ostringstream;
37 using std::string;
38 using std::vector;
39
40
41 MathArray::MathArray()
42 {}
43
44
45 MathArray::MathArray(const_iterator from, const_iterator to)
46         : base_type(from, to)
47 {}
48
49
50 MathAtom & MathArray::operator[](pos_type pos)
51 {
52         BOOST_ASSERT(pos < size());
53         return base_type::operator[](pos);
54 }
55
56
57 MathAtom const & MathArray::operator[](pos_type pos) const
58 {
59         BOOST_ASSERT(pos < size());
60         return base_type::operator[](pos);
61 }
62
63
64 void MathArray::insert(size_type pos, MathAtom const & t)
65 {
66         base_type::insert(begin() + pos, t);
67 }
68
69
70 void MathArray::insert(size_type pos, MathArray const & ar)
71 {
72         BOOST_ASSERT(pos <= size());
73         base_type::insert(begin() + pos, ar.begin(), ar.end());
74 }
75
76
77 void MathArray::append(MathArray const & ar)
78 {
79         insert(size(), ar);
80 }
81
82
83 void MathArray::erase(size_type pos)
84 {
85         if (pos < size())
86                 erase(pos, pos + 1);
87 }
88
89
90 void MathArray::erase(iterator pos1, iterator pos2)
91 {
92         base_type::erase(pos1, pos2);
93 }
94
95
96 void MathArray::erase(iterator pos)
97 {
98         base_type::erase(pos);
99 }
100
101
102 void MathArray::erase(size_type pos1, size_type pos2)
103 {
104         base_type::erase(begin() + pos1, begin() + pos2);
105 }
106
107
108 void MathArray::dump2() const
109 {
110         NormalStream ns(lyxerr);
111         for (const_iterator it = begin(); it != end(); ++it)
112                 ns << *it << ' ';
113 }
114
115
116 void MathArray::dump() const
117 {
118         NormalStream ns(lyxerr);
119         for (const_iterator it = begin(); it != end(); ++it)
120                 ns << '<' << *it << '>';
121 }
122
123
124 void MathArray::validate(LaTeXFeatures & features) const
125 {
126         for (const_iterator it = begin(); it != end(); ++it)
127                 (*it)->validate(features);
128 }
129
130
131 bool MathArray::match(MathArray const & ar) const
132 {
133         return size() == ar.size() && matchpart(ar, 0);
134 }
135
136
137 bool MathArray::matchpart(MathArray const & ar, pos_type pos) const
138 {
139         if (size() < ar.size() + pos)
140                 return false;
141         const_iterator it = begin() + pos;
142         for (const_iterator jt = ar.begin(); jt != ar.end(); ++jt, ++it)
143                 if (asString(*it) != asString(*jt))
144                         return false;
145         return true;
146 }
147
148
149 void MathArray::replace(ReplaceData & rep)
150 {
151         for (size_type i = 0; i < size(); ++i) {
152                 if (find1(rep.from, i)) {
153                         // match found
154                         lyxerr << "match found!" << endl;
155                         erase(i, i + rep.from.size());
156                         insert(i, rep.to);
157                 }
158         }
159
160 #ifdef WITH_WARNINGS
161 #warning temporarily disabled
162         // for (const_iterator it = begin(); it != end(); ++it)
163         //      it->nucleus()->replace(rep);
164 #endif
165 }
166
167
168 bool MathArray::find1(MathArray const & ar, size_type pos) const
169 {
170         lyxerr << "finding '" << ar << "' in '" << *this << "'" << endl;
171         for (size_type i = 0, n = ar.size(); i < n; ++i)
172                 if (asString(operator[](pos + i)) != asString(ar[i]))
173                         return false;
174         return true;
175 }
176
177
178 MathArray::size_type MathArray::find(MathArray const & ar) const
179 {
180         for (int i = 0, last = size() - ar.size(); i < last; ++i)
181                 if (find1(ar, i))
182                         return i;
183         return size();
184 }
185
186
187 MathArray::size_type MathArray::find_last(MathArray const & ar) const
188 {
189         for (int i = size() - ar.size(); i >= 0; --i)
190                 if (find1(ar, i))
191                         return i;
192         return size();
193 }
194
195
196 bool MathArray::contains(MathArray const & ar) const
197 {
198         if (find(ar) != size())
199                 return true;
200         for (const_iterator it = begin(); it != end(); ++it)
201                 if ((*it)->contains(ar))
202                         return true;
203         return false;
204 }
205
206
207 void MathArray::touch() const
208 {
209 }
210
211
212 void MathArray::metrics(MetricsInfo & mi, Dimension & dim) const
213 {
214         metrics(mi);
215         dim = dim_;
216 }
217
218
219 namespace {
220
221 bool isInside(DocIterator const & it, MathArray const & ar,
222         lyx::pos_type p1, lyx::pos_type p2)
223 {
224         for (size_t i = 0; i != it.depth(); ++i) {
225                 CursorSlice const & sl = it[i];
226                 if (sl.inset().inMathed() && &sl.cell() == &ar)
227                         return p1 <= sl.pos() && sl.pos() < p2;
228         }
229         return false;
230 }
231
232 }
233
234
235
236 void MathArray::metrics(MetricsInfo & mi) const
237 {
238         mathed_char_dim(mi.base.font, 'I', dim_);
239
240         if (empty())
241                 return;
242
243         dim_.wid = 0;
244         Dimension d;
245         //BufferView & bv  = *mi.base.bv;
246         //Buffer const & buf = *bv.buffer();
247         for (size_t i = 0, n = size(); i != n; ++i) {
248                 MathAtom const & at = operator[](i);
249 #if 0
250                 MathMacro const * mac = at->asMacro();
251                 if (mac && buf.hasMacro(mac->name())) {
252                         MacroData const & tmpl = buf.getMacro(mac->name());
253                         int numargs = tmpl.numargs();
254                         if (i + numargs > n)
255                                 numargs = n - i - 1;
256                         lyxerr << "metrics:found macro: " << mac->name()
257                                 << " numargs: " << numargs << endl;
258                         if (!isInside(bv.cursor(), *this, i + 1, i + numargs + 1)) {
259                                 MathArray args(begin() + i + 1, begin() + i + numargs + 1);
260                                 MathArray exp;
261                                 tmpl.expand(args, exp);
262                                 mac->setExpansion(exp, args);
263                                 mac->metricsExpanded(mi, d);
264                                 dim_.wid += mac->widthExpanded();
265                                 i += numargs;
266                                 continue;
267                         }
268                 }
269 #endif
270                 at->metrics(mi, d);
271                 dim_ += d;
272         }
273 }
274
275
276 void MathArray::draw(PainterInfo & pi, int x, int y) const
277 {
278         //lyxerr << "MathArray::draw: x: " << x << " y: " << y << endl;
279         setXY(x, y);
280
281         if (empty()) {
282                 pi.pain.rectangle(x, y - ascent(), width(), height(), LColor::mathline);
283                 return;
284         }
285
286         // don't draw outside the workarea
287         if (y + descent() <= 0
288                 || y - ascent() >= pi.pain.paperHeight()
289                 || x + width() <= 0
290                 || x >= pi.pain.paperWidth())
291                 return;
292
293         //BufferView & bv  = *pi.base.bv;
294         for (size_t i = 0, n = size(); i != n; ++i) {
295                 MathAtom const & at = operator[](i);
296 #if 0
297         Buffer const & buf = *bv.buffer();
298                 // special macro handling
299                 MathMacro const * mac = at->asMacro();
300                 if (mac && buf.hasMacro(mac->name())) {
301                         MacroData const & tmpl = buf.getMacro(mac->name());
302                         int numargs = tmpl.numargs();
303                         if (i + numargs > n)
304                                 numargs = n - i - 1;
305                         if (!isInside(bv.cursor(), *this, i + 1, i + numargs + 1)) {
306                                 mac->drawExpanded(pi, x, y);
307                                 x += mac->widthExpanded();
308                                 i += numargs;
309                                 continue;
310                         }
311                 }
312 #endif
313                 theCoords.insets().add(at.nucleus(), x, y);
314                 at->drawSelection(pi, x, y);
315                 at->draw(pi, x, y);
316                 x += at->width();
317         }
318 }
319
320
321 void MathArray::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
322 {
323         dim.clear();
324         Dimension d;
325         for (const_iterator it = begin(); it != end(); ++it) {
326                 (*it)->metricsT(mi, d);
327                 dim += d;
328         }
329 }
330
331
332 void MathArray::drawT(TextPainter & pain, int x, int y) const
333 {
334         //lyxerr << "x: " << x << " y: " << y << ' ' << pain.workAreaHeight() << endl;
335         setXY(x, y);
336
337         for (const_iterator it = begin(), et = end(); it != et; ++it) {
338                 (*it)->drawT(pain, x, y);
339                 //x += (*it)->width_;
340                 x += 2;
341         }
342 }
343
344
345 int MathArray::pos2x(size_type pos) const
346 {
347         return pos2x(pos, 0);
348 }
349
350
351 int MathArray::pos2x(size_type pos, int glue) const
352 {
353         int x = 0;
354         size_type target = min(pos, size());
355         for (size_type i = 0; i < target; ++i) {
356                 const_iterator it = begin() + i;
357                 if ((*it)->getChar() == ' ')
358                         x += glue;
359                 //lyxerr << "char: " << (*it)->getChar()
360                 //      << "width: " << (*it)->width() << std::endl;
361                 x += (*it)->width();
362         }
363         return x;
364 }
365
366
367 MathArray::size_type MathArray::x2pos(int targetx) const
368 {
369         return x2pos(targetx, 0);
370 }
371
372
373 MathArray::size_type MathArray::x2pos(int targetx, int glue) const
374 {
375         const_iterator it = begin();
376         int lastx = 0;
377         int currx = 0;
378         // find first position after targetx
379         for (; currx < targetx && it < end(); ++it) {
380                 lastx = currx;
381                 if ((*it)->getChar() == ' ')
382                         currx += glue;
383                 currx += (*it)->width();
384         }
385
386         /** 
387          * If we are not at the beginning of the array, go to the left
388          * of the inset if one of the following two condition holds:
389          * - the current inset is editable (so that the cursor tip is
390          *   deeper than us): in this case, we want all intermediate
391          *   cursor slices to be before insets;
392          * - the mouse is closer to the left side of the inset than to
393          *   the right one.
394          * See bug 1918 for details.    
395          **/
396         if (it != begin()
397             && ((*boost::prior(it))->asNestInset()
398                 || abs(lastx - targetx) < abs(currx - targetx))) {
399                 --it;
400         }
401
402         return it - begin();
403 }
404
405
406 int MathArray::dist(int x, int y) const
407 {
408         int xx = 0;
409         int yy = 0;
410
411         const int xo_ = xo();
412         const int yo_ = yo();
413
414         if (x < xo_)
415                 xx = xo_ - x;
416         else if (x > xo_ + width())
417                 xx = x - xo_ - width();
418
419         if (y < yo_ - ascent())
420                 yy = yo_ - ascent() - y;
421         else if (y > yo_ + descent())
422                 yy = y - yo_ - descent();
423
424         return xx + yy;
425 }
426
427
428 void MathArray::setXY(int x, int y) const
429 {
430         //lyxerr << "setting position cache for MathArray " << this << std::endl;
431         theCoords.arrays().add(this, x, y);
432 }
433
434
435 int MathArray::xo() const
436 {
437         return theCoords.getArrays().x(this);
438 }
439
440
441 int MathArray::yo() const
442 {
443         return theCoords.getArrays().y(this);
444 }