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