]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathScript.C
delete unneeded BufferView.h declaration.
[lyx.git] / src / mathed / InsetMathScript.C
1 /**
2  * \file InsetMathScript.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 "InsetMathScript.h"
14 #include "MathData.h"
15 #include "MathMLStream.h"
16 #include "MathSupport.h"
17 #include "InsetMathSymbol.h"
18 #include "dispatchresult.h"
19 #include "cursor.h"
20 #include "debug.h"
21 #include "funcrequest.h"
22 #include "undo.h"
23
24 #include <boost/assert.hpp>
25
26 using lyx::char_type;
27
28 using std::string;
29 using std::max;
30 using std::auto_ptr;
31 using std::endl;
32
33
34
35 InsetMathScript::InsetMathScript()
36         : InsetMathNest(1), cell_1_is_up_(false), limits_(0)
37 {}
38
39
40 InsetMathScript::InsetMathScript(bool up)
41         : InsetMathNest(2), cell_1_is_up_(up), limits_(0)
42 {}
43
44
45 InsetMathScript::InsetMathScript(MathAtom const & at, bool up)
46         : InsetMathNest(2), cell_1_is_up_(up), limits_(0)
47 {
48         BOOST_ASSERT(nargs() >= 1);
49         cell(0).push_back(at);
50 }
51
52
53 auto_ptr<InsetBase> InsetMathScript::doClone() const
54 {
55         return auto_ptr<InsetBase>(new InsetMathScript(*this));
56 }
57
58
59 InsetMathScript const * InsetMathScript::asScriptInset() const
60 {
61         return this;
62 }
63
64
65 InsetMathScript * InsetMathScript::asScriptInset()
66 {
67         return this;
68 }
69
70
71 bool InsetMathScript::idxFirst(LCursor & cur) const
72 {
73         cur.idx() = 0;
74         cur.pos() = 0;
75         return true;
76 }
77
78
79 bool InsetMathScript::idxLast(LCursor & cur) const
80 {
81         cur.idx() = 0;
82         cur.pos() = nuc().size();
83         return true;
84 }
85
86
87 MathArray const & InsetMathScript::down() const
88 {
89         if (nargs() == 3)
90                 return cell(2);
91         BOOST_ASSERT(nargs() > 1);
92         return cell(1);
93 }
94
95
96 MathArray & InsetMathScript::down()
97 {
98         if (nargs() == 3)
99                 return cell(2);
100         BOOST_ASSERT(nargs() > 1);
101         return cell(1);
102 }
103
104
105 MathArray const & InsetMathScript::up() const
106 {
107         BOOST_ASSERT(nargs() > 1);
108         return cell(1);
109 }
110
111
112 MathArray & InsetMathScript::up()
113 {
114         BOOST_ASSERT(nargs() > 1);
115         return cell(1);
116 }
117
118
119 void InsetMathScript::ensure(bool up)
120 {
121         if (nargs() == 1) {
122                 // just nucleus so far
123                 cells_.push_back(MathArray());
124                 cell_1_is_up_ = up;
125         } else if (nargs() == 2 && !has(up)) {
126                 if (up) {
127                         cells_.push_back(cell(1));
128                         cell(1).clear();
129                 } else {
130                         cells_.push_back(MathArray());
131                 }
132         }
133 }
134
135
136 MathArray const & InsetMathScript::nuc() const
137 {
138         return cell(0);
139 }
140
141
142 MathArray & InsetMathScript::nuc()
143 {
144         return cell(0);
145 }
146
147
148 int InsetMathScript::dy0() const
149 {
150         int nd = ndes();
151         if (!hasDown())
152                 return nd;
153         int des = down().ascent();
154         if (hasLimits())
155                 des += nd + 2;
156         else
157                 des = max(des, nd);
158         return des;
159 }
160
161
162 int InsetMathScript::dy1() const
163 {
164         int na = nasc();
165         if (!hasUp())
166                 return na;
167         int asc = up().descent();
168         if (hasLimits())
169                 asc += na + 2;
170         else
171                 asc = max(asc, na);
172         asc = max(asc, 5);
173         return asc;
174 }
175
176
177 int InsetMathScript::dx0() const
178 {
179         BOOST_ASSERT(hasDown());
180         return hasLimits() ? (dim_.wid - down().width()) / 2 : nwid();
181 }
182
183
184 int InsetMathScript::dx1() const
185 {
186         BOOST_ASSERT(hasUp());
187         return hasLimits() ? (dim_.wid - up().width()) / 2 : nwid();
188 }
189
190
191 int InsetMathScript::dxx() const
192 {
193         return hasLimits() ? (dim_.wid - nwid()) / 2  :  0;
194 }
195
196
197 int InsetMathScript::nwid() const
198 {
199         return nuc().size() ? nuc().width() : 2;
200 }
201
202
203 int InsetMathScript::nasc() const
204 {
205         return nuc().size() ? nuc().ascent() : 5;
206 }
207
208
209 int InsetMathScript::ndes() const
210 {
211         return nuc().size() ? nuc().descent() : 0;
212 }
213
214
215 void InsetMathScript::metrics(MetricsInfo & mi, Dimension & dim) const
216 {
217         cell(0).metrics(mi);
218         ScriptChanger dummy(mi.base);
219         if (nargs() > 1)
220                 cell(1).metrics(mi);
221         if (nargs() > 2)
222                 cell(2).metrics(mi);
223         dim.wid = 0;
224         if (hasLimits()) {
225                 dim.wid = nwid();
226                 if (hasUp())
227                         dim.wid = max(dim.wid, up().width());
228                 if (hasDown())
229                         dim.wid = max(dim.wid, down().width());
230         } else {
231                 if (hasUp())
232                         dim.wid = max(dim.wid, up().width());
233                 if (hasDown())
234                         dim.wid = max(dim.wid, down().width());
235                 dim.wid += nwid();
236         }
237         dim.asc = dy1() + (hasUp() ? up().ascent() : 0);
238         dim.des = dy0() + (hasDown() ? down().descent() : 0);
239         metricsMarkers(dim);
240         dim_ = dim;
241 }
242
243
244 void InsetMathScript::draw(PainterInfo & pi, int x, int y) const
245 {
246         if (nuc().size())
247                 nuc().draw(pi, x + dxx(), y);
248         else {
249                 nuc().setXY(x + dxx(), y);
250                 if (editing(pi.base.bv))
251                         pi.draw(x + dxx(), y, char_type('.'));
252         }
253         ScriptChanger dummy(pi.base);
254         if (hasUp())
255                 up().draw(pi, x + dx1(), y - dy1());
256         if (hasDown())
257                 down().draw(pi, x + dx0(), y + dy0());
258         drawMarkers(pi, x, y);
259 }
260
261
262 void InsetMathScript::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
263 {
264         if (hasUp())
265                 up().metricsT(mi, dim);
266         if (hasDown())
267                 down().metricsT(mi, dim);
268         nuc().metricsT(mi, dim);
269 }
270
271
272 void InsetMathScript::drawT(TextPainter & pain, int x, int y) const
273 {
274         if (nuc().size())
275                 nuc().drawT(pain, x + dxx(), y);
276         if (hasUp())
277                 up().drawT(pain, x + dx1(), y - dy1());
278         if (hasDown())
279                 down().drawT(pain, x + dx0(), y + dy0());
280 }
281
282
283
284 bool InsetMathScript::hasLimits() const
285 {
286         // obvious cases
287         if (limits_ == 1)
288                 return true;
289         if (limits_ == -1)
290                 return false;
291
292         // we can only display limits if the nucleus wants some
293         if (!nuc().size())
294                 return false;
295         if (!nuc().back()->isScriptable())
296                 return false;
297
298         if (nuc().back()->asSymbolInset()) {
299                 // \intop is an alias for \int\limits, \ointop == \oint\limits
300                 if (nuc().back()->asSymbolInset()->name().find("intop") != string::npos)
301                         return true;
302                 // per default \int has limits beside the \int even in displayed formulas
303                 if (nuc().back()->asSymbolInset()->name().find("int") != string::npos)
304                         return false;
305         }
306
307         // assume "real" limits for everything else
308         return true;
309 }
310
311
312 void InsetMathScript::removeScript(bool up)
313 {
314         if (nargs() == 2) {
315                 if (up == cell_1_is_up_)
316                         cells_.pop_back();
317         } else if (nargs() == 3) {
318                 if (up == true) {
319                         swap(cells_[1], cells_[2]);
320                         cell_1_is_up_ = false;
321                 } else {
322                         cell_1_is_up_ = true;
323                 }
324                 cells_.pop_back();
325         }
326 }
327
328
329 bool InsetMathScript::has(bool up) const
330 {
331         return idxOfScript(up);
332 }
333
334
335 bool InsetMathScript::hasUp() const
336 {
337         //lyxerr << "1up: " << bool(cell_1_is_up_) << endl;
338         //lyxerr << "hasUp: " << bool(idxOfScript(true)) << endl;
339         return idxOfScript(true);
340 }
341
342
343 bool InsetMathScript::hasDown() const
344 {
345         //lyxerr << "1up: " << bool(cell_1_is_up_) << endl;
346         //lyxerr << "hasDown: " << bool(idxOfScript(false)) << endl;
347         return idxOfScript(false);
348 }
349
350
351 InsetBase::idx_type InsetMathScript::idxOfScript(bool up) const
352 {
353         if (nargs() == 1)
354                 return 0;
355         if (nargs() == 2)
356                 return (cell_1_is_up_ == up) ? 1 : 0;
357         if (nargs() == 3)
358                 return up ? 1 : 2;
359         BOOST_ASSERT(false);
360         // Silence compiler
361         return 0;
362 }
363
364
365 bool InsetMathScript::idxRight(LCursor &) const
366 {
367         return false;
368 }
369
370
371 bool InsetMathScript::idxLeft(LCursor &) const
372 {
373         return false;
374 }
375
376
377 bool InsetMathScript::idxUpDown(LCursor & cur, bool up) const
378 {
379         // in nucleus?
380         if (cur.idx() == 0) {
381                 // don't go up/down if there is no cell in this direction
382                 if (!has(up))
383                         return false;
384                 // go up/down only if in the last position
385                 // or in the first position of something with displayed limits
386                 if (cur.pos() == cur.lastpos() || (cur.pos() == 0 && hasLimits())) {
387                         cur.idx() = idxOfScript(up);
388                         cur.pos() = 0;
389                         return true;
390                 }
391                 return false;
392         }
393
394         // Are we 'up'?
395         if (has(up) && cur.idx() == idxOfScript(true)) {
396                 // can't go further up
397                 if (up)
398                         return false;
399                 // otherwise go to last position in the nucleus
400                 cur.idx() = 0;
401                 cur.pos() = cur.lastpos();
402                 return true;
403         }
404
405         // Are we 'down'?
406         if (has(up) && cur.idx() == idxOfScript(false)) {
407                 // can't go further down
408                 if (!up)
409                         return false;
410                 // otherwise go to last position in the nucleus
411                 cur.idx() = 0;
412                 cur.pos() = cur.lastpos();
413                 return true;
414         }
415
416         return false;
417 }
418
419
420 void InsetMathScript::write(WriteStream & os) const
421 {
422         if (nuc().size()) {
423                 if (nuc().size() == 1 
424                     && ! nuc().begin()->nucleus()->asScriptInset())
425                         os << nuc();
426                 else
427                         os << '{' << nuc() << '}';
428                 //if (nuc().back()->takesLimits()) {
429                         if (limits_ == -1)
430                                 os << "\\nolimits ";
431                         if (limits_ == 1)
432                                 os << "\\limits ";
433                 //}
434         } else {
435                 if (os.firstitem())
436                         lyxerr[Debug::MATHED] << "suppressing {} when writing"
437                                               << endl;
438                 else
439                         os << "{}";
440         }
441
442         if (hasDown() /*&& down().size()*/)
443                 os << "_{" << down() << '}';
444
445         if (hasUp() /*&& up().size()*/)
446                 os << "^{" << up() << '}';
447
448         if (lock_ && !os.latex())
449                 os << "\\lyxlock ";
450 }
451
452
453 void InsetMathScript::normalize(NormalStream & os) const
454 {
455         bool d = hasDown() && down().size();
456         bool u = hasUp() && up().size();
457
458         if (u && d)
459                 os << "[subsup ";
460         else if (u)
461                 os << "[sup ";
462         else if (d)
463                 os << "[sub ";
464
465         if (nuc().size())
466                 os << nuc() << ' ';
467         else
468                 os << "[par]";
469
470         if (u && d)
471                 os << down() << ' ' << up() << ']';
472         else if (d)
473                 os << down() << ']';
474         else if (u)
475                 os << up() << ']';
476 }
477
478
479 void InsetMathScript::maple(MapleStream & os) const
480 {
481         if (nuc().size())
482                 os << nuc();
483         if (hasDown() && down().size())
484                 os << '[' << down() << ']';
485         if (hasUp() && up().size())
486                 os << "^(" << up() << ')';
487 }
488
489
490 void InsetMathScript::mathematica(MathematicaStream & os) const
491 {
492         bool d = hasDown() && down().size();
493         bool u = hasUp() && up().size();
494
495         if (nuc().size()) {
496                 if (d)
497                         os << "Subscript[" << nuc();
498                 else
499                         os << nuc();
500         }
501
502         if (u)
503                 os << "^(" << up() << ')';
504
505         if (nuc().size()) {
506                 if (d)
507                         os << ',' << down() << ']';
508         }
509 }
510
511
512 void InsetMathScript::mathmlize(MathMLStream & os) const
513 {
514         bool d = hasDown() && down().size();
515         bool u = hasUp() && up().size();
516
517         if (u && d)
518                 os << MTag("msubsup");
519         else if (u)
520                 os << MTag("msup");
521         else if (d)
522                 os << MTag("msub");
523
524         if (nuc().size())
525                 os << nuc();
526         else
527                 os << "<mrow/>";
528
529         if (u && d)
530                 os << down() << up() << ETag("msubsup");
531         else if (u)
532                 os << up() << ETag("msup");
533         else if (d)
534                 os << down() << ETag("msub");
535 }
536
537
538 void InsetMathScript::octave(OctaveStream & os) const
539 {
540         if (nuc().size())
541                 os << nuc();
542         if (hasDown() && down().size())
543                 os << '[' << down() << ']';
544         if (hasUp() && up().size())
545                 os << "^(" << up() << ')';
546 }
547
548
549 void InsetMathScript::infoize(std::ostream & os) const
550 {
551         os << "Scripts";
552 }
553
554
555 void InsetMathScript::infoize2(std::ostream & os) const
556 {
557         if (limits_)
558                 os << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
559 }
560
561
562 bool InsetMathScript::notifyCursorLeaves(LCursor & cur)
563 {
564         InsetMathNest::notifyCursorLeaves(cur);
565
566         //lyxerr << "InsetMathScript::notifyCursorLeaves: 1 " << cur << endl;
567
568         // remove empty scripts if possible
569         if (nargs() > 2) {
570                 // Case of two scripts. In this case, 1 = super, 2 = sub
571                 if (cur.idx() == 2 && cell(2).empty()) {
572                         // must be a subscript...
573                         recordUndoInset(cur);
574                         removeScript(false);
575                         return true;
576                 } else if (cur.idx() == 1 && cell(1).empty()) {
577                         // must be a superscript...
578                         recordUndoInset(cur);
579                         removeScript(true);
580                         return true;
581                 }
582         } else if (nargs() > 1 && cur.idx() == 1 && cell(1).empty()) {
583                 // could be either subscript or super script
584                 recordUndoInset(cur);
585                 removeScript(cell_1_is_up_);
586                 // Let the script inset commit suicide. This is
587                 // modelled on LCursor.pullArg(), but tries not to
588                 // invoke notifyCursorLeaves again and does not touch
589                 // cur (since the top slice will be deleted
590                 // afterwards))
591                 MathArray ar = cell(0);
592                 LCursor tmpcur = cur;
593                 tmpcur.pop();
594                 tmpcur.cell().erase(tmpcur.pos());
595                 tmpcur.cell().insert(tmpcur.pos(), ar);
596                 return true;
597         }
598
599         //lyxerr << "InsetMathScript::notifyCursorLeaves: 2 " << cur << endl;
600         return false;
601 }
602
603
604 void InsetMathScript::doDispatch(LCursor & cur, FuncRequest & cmd)
605 {
606         //lyxerr << "InsetMathScript: request: " << cmd << std::endl;
607
608         if (cmd.action == LFUN_MATH_LIMITS) {
609                 if (!cmd.argument().empty()) {
610                         if (cmd.argument() == "limits")
611                                 limits_ = 1;
612                         else if (cmd.argument() == "nolimits")
613                                 limits_ = -1;
614                         else
615                                 limits_ = 0;
616                 } else if (limits_ == 0)
617                         limits_ = hasLimits() ? -1 : 1;
618                 else
619                         limits_ = 0;
620                 return;
621         }
622
623         InsetMathNest::doDispatch(cur, cmd);
624 }