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