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