]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathFrac.cpp
50bfbb4ade3b75a342981c5675273c471ba3e07f
[lyx.git] / src / mathed / InsetMathFrac.cpp
1 /**
2  * \file InsetMathFracBase.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alejandro Aguilar Sierra
7  * \author André Pönitz
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetMathFrac.h"
15
16 #include "Cursor.h"
17 #include "LaTeXFeatures.h"
18 #include "MathData.h"
19 #include "MathStream.h"
20 #include "MathSupport.h"
21 #include "TextPainter.h"
22
23 #include "frontends/Painter.h"
24
25
26 namespace lyx {
27
28 /////////////////////////////////////////////////////////////////////
29 //
30 // InsetMathFracBase
31 //
32 /////////////////////////////////////////////////////////////////////
33
34
35 InsetMathFracBase::InsetMathFracBase(idx_type ncells)
36         : InsetMathNest(ncells)
37 {}
38
39
40 bool InsetMathFracBase::idxUpDown(Cursor & cur, bool up) const
41 {
42         InsetMath::idx_type target = !up; // up ? 0 : 1, since upper cell has idx 0
43         if (cur.idx() == target)
44                 return false;
45         cur.idx() = target;
46         cur.pos() = cell(target).x2pos(cur.x_target());
47         return true;
48 }
49
50
51
52 /////////////////////////////////////////////////////////////////////
53 //
54 // InsetMathFrac
55 //
56 /////////////////////////////////////////////////////////////////////
57
58
59 InsetMathFrac::InsetMathFrac(Kind kind, InsetMath::idx_type ncells)
60         : InsetMathFracBase(ncells), kind_(kind)
61 {}
62
63
64 Inset * InsetMathFrac::clone() const
65 {
66         return new InsetMathFrac(*this);
67 }
68
69
70 InsetMathFrac * InsetMathFrac::asFracInset()
71 {
72         return kind_ == ATOP ? 0 : this;
73 }
74
75
76 InsetMathFrac const * InsetMathFrac::asFracInset() const
77 {
78         return kind_ == ATOP ? 0 : this;
79 }
80
81
82 bool InsetMathFrac::idxRight(Cursor & cur) const
83 {
84         InsetMath::idx_type target = 0;
85         if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
86                 if (nargs() == 3)
87                         target = 0;
88                 else if (nargs() == 2)
89                         target = 1;
90         } else
91                 return false;
92         if (cur.idx() == target)
93                 return false;
94         cur.idx() = target;
95         cur.pos() = cell(target).x2pos(cur.x_target());
96         return true;
97 }
98
99
100 bool InsetMathFrac::idxLeft(Cursor & cur) const
101 {
102         InsetMath::idx_type target = 0;
103         if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
104                 if (nargs() == 3)
105                         target = 2;
106                 else if (nargs() == 2)
107                         target = 0;
108         } else
109                 return false;
110         if (cur.idx() == target)
111                 return false;
112         cur.idx() = target;
113         cur.pos() = cell(target).x2pos(cur.x_target());
114         return true;
115 }
116
117
118 void InsetMathFrac::metrics(MetricsInfo & mi, Dimension & dim) const
119 {
120         Dimension dim0, dim1, dim2;
121
122         if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
123                 if (nargs() == 1) {
124                         ShapeChanger dummy2(mi.base.font, UP_SHAPE);
125                         cell(0).metrics(mi, dim0);
126                         dim.wid = dim0.width()+ 3;
127                         dim.asc = dim0.asc;
128                         dim.des = dim0.des;
129                 } else if (nargs() == 2) {
130                         cell(0).metrics(mi, dim0);
131                         ShapeChanger dummy2(mi.base.font, UP_SHAPE);
132                         cell(1).metrics(mi, dim1);
133                         dim.wid = dim0.width() + dim1.wid + 5;
134                         dim.asc = std::max(dim0.asc, dim1.asc);
135                         dim.des = std::max(dim0.des, dim1.des);
136                 } else {
137                         cell(2).metrics(mi, dim2);
138                         ShapeChanger dummy2(mi.base.font, UP_SHAPE);
139                         FracChanger dummy(mi.base);
140                         cell(0).metrics(mi, dim0);
141                         cell(1).metrics(mi, dim1);
142                         dim.wid = dim0.width() + dim1.wid + dim2.wid + 10;
143                         dim.asc = std::max(dim2.asc, dim0.height() + 5);
144                         dim.des = std::max(dim2.des, dim1.height() - 5);
145                 }
146         } else {
147                 FracChanger dummy(mi.base);
148                 cell(0).metrics(mi, dim0);
149                 cell(1).metrics(mi, dim1);
150                 if (nargs() == 3)
151                         cell(2).metrics(mi, dim2);
152
153                 if (kind_ == NICEFRAC) {
154                         dim.wid = dim0.width() + dim1.wid + 5;
155                         dim.asc = dim0.height() + 5;
156                         dim.des = dim1.height() - 5;
157                 } else if (kind_ == UNITFRAC) {
158                         ShapeChanger dummy2(mi.base.font, UP_SHAPE);
159                         dim.wid = dim0.width() + dim1.wid + 5;
160                         dim.asc = dim0.height() + 5;
161                         dim.des = dim1.height() - 5;
162                 } else {
163                         dim.wid = std::max(dim0.width(), dim1.wid) + 2;
164                         dim.asc = dim0.height() + 2 + 5;
165                         dim.des = dim1.height() + 2 - 5;
166                 }
167         }
168         metricsMarkers(dim);
169         // Cache the inset dimension. 
170         setDimCache(mi, dim);
171 }
172
173
174 void InsetMathFrac::draw(PainterInfo & pi, int x, int y) const
175 {
176         setPosCache(pi, x, y);
177         Dimension const dim = dimension(*pi.base.bv);
178         Dimension const dim0 = cell(0).dimension(*pi.base.bv);
179         int m = x + dim.wid / 2;
180         if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
181                 if (nargs() == 1) {
182                         ShapeChanger dummy2(pi.base.font, UP_SHAPE);
183                         cell(0).draw(pi, x + 1, y);
184                 } else if (nargs() == 2) {
185                         cell(0).draw(pi, x + 1, y);
186                         ShapeChanger dummy2(pi.base.font, UP_SHAPE);
187                         cell(1).draw(pi, x + dim0.width() + 5, y);
188                 } else {
189                         cell(2).draw(pi, x + 1, y);
190                         ShapeChanger dummy2(pi.base.font, UP_SHAPE);
191                         FracChanger dummy(pi.base);
192                         Dimension const dim1 = cell(1).dimension(*pi.base.bv);
193                         Dimension const dim2 = cell(2).dimension(*pi.base.bv);
194                         int xx = x + dim2.wid + 5;
195                         cell(0).draw(pi, xx + 2, 
196                                          y - dim0.des - 5);
197                         cell(1).draw(pi, xx  + dim0.width() + 5, 
198                                          y + dim1.asc / 2);
199                 }
200         } else {
201                 FracChanger dummy(pi.base);
202                 Dimension const dim1 = cell(1).dimension(*pi.base.bv);
203                 if (kind_ == NICEFRAC) {
204                         cell(0).draw(pi, x + 2,
205                                         y - dim0.des - 5);
206                         cell(1).draw(pi, x + dim0.width() + 5,
207                                         y + dim1.asc / 2);
208                 } else if (kind_ == UNITFRAC) {
209                         ShapeChanger dummy2(pi.base.font, UP_SHAPE);
210                         cell(0).draw(pi, x + 2,
211                                         y - dim0.des - 5);
212                         cell(1).draw(pi, x + dim0.width() + 5,
213                                         y + dim1.asc / 2);
214                 } else {
215                         // Classical fraction
216                         cell(0).draw(pi, m - dim0.width() / 2,
217                                         y - dim0.des - 2 - 5);
218                         cell(1).draw(pi, m - dim1.wid / 2,
219                                         y + dim1.asc  + 2 - 5);
220                 }
221         }
222         if (kind_ == NICEFRAC || kind_ == UNITFRAC) {
223                 // Diag line:
224                 int xx = x;
225                 if (nargs() == 3)
226                         xx += cell(2).dimension(*pi.base.bv).wid + 5;
227
228                 pi.pain.line(xx + dim0.wid,
229                                 y + dim.des - 2,
230                                 xx + dim0.wid + 5,
231                                 y - dim.asc + 2, Color_math);
232         }
233         if (kind_ == FRAC || kind_ == OVER)
234                 pi.pain.line(x + 1, y - 5,
235                                 x + dim.wid - 2, y - 5, Color_math);
236         drawMarkers(pi, x, y);
237 }
238
239
240 void InsetMathFrac::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
241 {
242         Dimension dim0, dim1;
243         cell(0).metricsT(mi, dim0);
244         cell(1).metricsT(mi, dim1);
245         dim.wid = std::max(dim0.width(), dim1.wid);
246         dim.asc = dim0.height() + 1;
247         dim.des = dim1.height();
248 }
249
250
251 void InsetMathFrac::drawT(TextPainter & pain, int x, int y) const
252 {
253         // FIXME: BROKEN!
254         /*
255         Dimension dim;
256         int m = x + dim.width() / 2;
257         cell(0).drawT(pain, m - dim0.width() / 2, y - dim0.des - 1);
258         cell(1).drawT(pain, m - dim1.wid / 2, y + dim1.asc);
259         // ASCII art: ignore niceties
260         if (kind_ == FRAC || kind_ == OVER || kind_ == NICEFRAC || kind_ == UNITFRAC)
261                 pain.horizontalLine(x, y, dim.width());
262         */
263 }
264
265
266 void InsetMathFrac::write(WriteStream & os) const
267 {
268         switch (kind_) {
269         case ATOP:
270                 os << '{' << cell(0) << "\\atop " << cell(1) << '}';
271                 break;
272         case OVER:
273                 // \\over is only for compatibility, normalize this to \\frac
274                 os << "\\frac{" << cell(0) << "}{" << cell(1) << '}';
275                 break;
276         case FRAC:
277         case NICEFRAC:
278         case UNITFRAC:
279                 if (nargs() == 2)
280                         InsetMathNest::write(os);
281                 else
282                         os << "\\unitfrac[" << cell(2) << "]{" << cell(0) << "}{" << cell(1) << '}';
283                 break;
284         case UNIT:
285                 if (nargs() == 2)
286                         os << "\\unit[" << cell(0) << "]{" << cell(1) << '}';
287                 else
288                         os << "\\unit{" << cell(0) << '}';
289                 break;
290         }
291 }
292
293
294 docstring InsetMathFrac::name() const
295 {
296         switch (kind_) {
297         case FRAC:
298                 return from_ascii("frac");
299         case OVER:
300                 return from_ascii("over");
301         case NICEFRAC:
302                 return from_ascii("nicefrac");
303         case UNITFRAC:
304                 return from_ascii("unitfrac");
305         case UNIT:
306                 return from_ascii("unit");
307         case ATOP:
308                 return from_ascii("atop");
309         }
310         // shut up stupid compiler
311         return docstring();
312 }
313
314
315 bool InsetMathFrac::extraBraces() const
316 {
317         return kind_ == ATOP || kind_ == OVER;
318 }
319
320
321 void InsetMathFrac::maple(MapleStream & os) const
322 {
323         os << '(' << cell(0) << ")/(" << cell(1) << ')';
324 }
325
326
327 void InsetMathFrac::mathematica(MathematicaStream & os) const
328 {
329         os << '(' << cell(0) << ")/(" << cell(1) << ')';
330 }
331
332
333 void InsetMathFrac::octave(OctaveStream & os) const
334 {
335         os << '(' << cell(0) << ")/(" << cell(1) << ')';
336 }
337
338
339 void InsetMathFrac::mathmlize(MathStream & os) const
340 {
341         os << MTag("mfrac") << cell(0) << cell(1) << ETag("mfrac");
342 }
343
344
345 void InsetMathFrac::validate(LaTeXFeatures & features) const
346 {
347         if (kind_ == NICEFRAC || kind_ == UNITFRAC || kind_ == UNIT)
348                 features.require("units");
349         InsetMathNest::validate(features);
350 }
351
352
353 /////////////////////////////////////////////////////////////////////
354 //
355 // InsetMathDFrac
356 //
357 /////////////////////////////////////////////////////////////////////
358
359
360 Inset * InsetMathDFrac::clone() const
361 {
362         return new InsetMathDFrac(*this);
363 }
364
365
366 void InsetMathDFrac::metrics(MetricsInfo & mi, Dimension & dim) const
367 {
368         Dimension dim0, dim1;
369         cell(0).metrics(mi, dim0);
370         cell(1).metrics(mi, dim1);
371         dim.wid = std::max(dim0.wid, dim1.wid) + 2;
372         dim.asc = dim0.height() + 2 + 5;
373         dim.des = dim1.height() + 2 - 5;
374         // Cache the inset dimension. 
375         setDimCache(mi, dim);
376 }
377
378
379 void InsetMathDFrac::draw(PainterInfo & pi, int x, int y) const
380 {
381         Dimension const dim = dimension(*pi.base.bv);
382         Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
383         Dimension const & dim1 = cell(1).dimension(*pi.base.bv);
384         int m = x + dim.wid / 2;
385         cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 2 - 5);
386         cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc  + 2 - 5);
387         pi.pain.line(x + 1, y - 5, x + dim.wid - 2, y - 5, Color_math);
388         setPosCache(pi, x, y);
389 }
390
391
392 docstring InsetMathDFrac::name() const
393 {
394         return from_ascii("dfrac");
395 }
396
397
398 void InsetMathDFrac::mathmlize(MathStream & os) const
399 {
400         os << MTag("mdfrac") << cell(0) << cell(1) << ETag("mdfrac");
401 }
402
403
404 void InsetMathDFrac::validate(LaTeXFeatures & features) const
405 {
406         features.require("amsmath");
407         InsetMathNest::validate(features);
408 }
409
410
411 /////////////////////////////////////////////////////////////////////
412 //
413 // InsetMathTFrac
414 //
415 /////////////////////////////////////////////////////////////////////
416
417
418 Inset * InsetMathTFrac::clone() const
419 {
420         return new InsetMathTFrac(*this);
421 }
422
423
424 void InsetMathTFrac::metrics(MetricsInfo & mi, Dimension & dim) const
425 {
426         StyleChanger dummy(mi.base, LM_ST_SCRIPT);
427         Dimension dim0;
428         cell(0).metrics(mi, dim0);
429         Dimension dim1;
430         cell(1).metrics(mi, dim1);
431         dim.wid = std::max(dim0.width(), dim1.width()) + 2;
432         dim.asc = dim0.height() + 2 + 5;
433         dim.des = dim1.height() + 2 - 5;
434         // Cache the inset dimension. 
435         setDimCache(mi, dim);
436 }
437
438
439 void InsetMathTFrac::draw(PainterInfo & pi, int x, int y) const
440 {
441         StyleChanger dummy(pi.base, LM_ST_SCRIPT);
442         Dimension const dim = dimension(*pi.base.bv);
443         Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
444         Dimension const & dim1 = cell(1).dimension(*pi.base.bv);
445         int m = x + dim.wid / 2;
446         cell(0).draw(pi, m - dim0.width() / 2, y - dim0.descent() - 2 - 5);
447         cell(1).draw(pi, m - dim1.width() / 2, y + dim1.ascent()  + 2 - 5);
448         pi.pain.line(x + 1, y - 5, x + dim.wid - 2, y - 5, Color_math);
449         setPosCache(pi, x, y);
450 }
451
452
453 docstring InsetMathTFrac::name() const
454 {
455         return from_ascii("tfrac");
456 }
457
458
459 void InsetMathTFrac::mathmlize(MathStream & os) const
460 {
461         os << MTag("mtfrac") << cell(0) << cell(1) << ETag("mtfrac");
462 }
463
464
465 void InsetMathTFrac::validate(LaTeXFeatures & features) const
466 {
467         features.require("amsmath");
468         InsetMathNest::validate(features);
469 }
470
471
472 /////////////////////////////////////////////////////////////////////
473 //
474 // InsetMathBinom
475 //
476 /////////////////////////////////////////////////////////////////////
477
478
479 InsetMathBinom::InsetMathBinom(bool choose)
480         : choose_(choose)
481 {}
482
483
484 Inset * InsetMathBinom::clone() const
485 {
486         return new InsetMathBinom(*this);
487 }
488
489
490 int InsetMathBinom::dw(int height) const
491 {
492         int w = height / 5;
493         if (w > 15)
494                 w = 15;
495         if (w < 6)
496                 w = 6;
497         return w;
498 }
499
500
501 void InsetMathBinom::metrics(MetricsInfo & mi, Dimension & dim) const
502 {
503         FracChanger dummy(mi.base);
504         Dimension dim0, dim1;
505         cell(0).metrics(mi, dim0);
506         cell(1).metrics(mi, dim1);
507         dim.asc = dim0.height() + 4 + 5;
508         dim.des = dim1.height() + 4 - 5;
509         dim.wid = std::max(dim0.width(), dim1.wid) + 2 * dw(dim.height()) + 4;
510         metricsMarkers2(dim);
511         // Cache the inset dimension. 
512         setDimCache(mi, dim);
513 }
514
515
516 void InsetMathBinom::draw(PainterInfo & pi, int x, int y) const
517 {
518         Dimension const dim = dimension(*pi.base.bv);
519         Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
520         Dimension const & dim1 = cell(1).dimension(*pi.base.bv);
521         int m = x + dim.width() / 2;
522         FracChanger dummy(pi.base);
523         cell(0).draw(pi, m - dim0.width() / 2, y - dim0.des - 3 - 5);
524         cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc  + 3 - 5);
525         mathed_draw_deco(pi, x, y - dim.ascent(), dw(dim.height()), dim.height(), from_ascii("("));
526         mathed_draw_deco(pi, x + dim.width() - dw(dim.height()), y - dim.ascent(),
527                 dw(dim.height()), dim.height(), from_ascii(")"));
528         drawMarkers2(pi, x, y);
529 }
530
531
532 bool InsetMathBinom::extraBraces() const
533 {
534         return choose_;
535 }
536
537
538 void InsetMathBinom::write(WriteStream & os) const
539 {
540         if (choose_)
541                 os << '{' << cell(0) << " \\choose " << cell(1) << '}';
542         else
543                 os << "\\binom{" << cell(0) << "}{" << cell(1) << '}';
544 }
545
546
547 void InsetMathBinom::normalize(NormalStream & os) const
548 {
549         os << "[binom " << cell(0) << ' ' << cell(1) << ']';
550 }
551
552
553 /////////////////////////////////////////////////////////////////////
554 //
555 // InsetMathDBinom
556 //
557 /////////////////////////////////////////////////////////////////////
558
559 Inset * InsetMathDBinom::clone() const
560 {
561         return new InsetMathDBinom(*this);
562 }
563
564
565 int InsetMathDBinom::dw(int height) const
566 {
567         int w = height / 5;
568         if (w > 15)
569                 w = 15;
570         if (w < 6)
571                 w = 6;
572         return w;
573 }
574
575
576 void InsetMathDBinom::metrics(MetricsInfo & mi, Dimension & dim) const
577 {
578         Dimension dim0, dim1;
579         cell(0).metrics(mi, dim0);
580         cell(1).metrics(mi, dim1);
581         dim.asc = dim0.height() + 4 + 5;
582         dim.des = dim1.height() + 4 - 5;
583         dim.wid = std::max(dim0.width(), dim1.wid) + 2 * dw(dim.height()) + 4;
584         metricsMarkers2(dim);
585         // Cache the inset dimension. 
586         setDimCache(mi, dim);
587 }
588
589
590 void InsetMathDBinom::draw(PainterInfo & pi, int x, int y) const
591 {
592         Dimension const dim = dimension(*pi.base.bv);
593         Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
594         Dimension const & dim1 = cell(1).dimension(*pi.base.bv);
595         int m = x + dim.width() / 2;
596         cell(0).draw(pi, m - dim0.width() / 2, y - dim0.des - 3 - 5);
597         cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc  + 3 - 5);
598         mathed_draw_deco(pi, x, y - dim.ascent(), dw(dim.height()), dim.height(), from_ascii("("));
599         mathed_draw_deco(pi, x + dim.width() - dw(dim.height()), y - dim.ascent(),
600                 dw(dim.height()), dim.height(), from_ascii(")"));
601         drawMarkers2(pi, x, y);
602 }
603
604
605 docstring InsetMathDBinom::name() const
606 {
607         return from_ascii("dbinom");
608 }
609
610 void InsetMathDBinom::mathmlize(MathStream & os) const
611 {
612         os << MTag("mdbinom") << cell(0) << cell(1) << ETag("mdbinom");
613 }
614
615 void InsetMathDBinom::validate(LaTeXFeatures & features) const
616 {
617         features.require("amsmath");
618         InsetMathNest::validate(features);
619 }
620
621
622 /////////////////////////////////////////////////////////////////////
623 //
624 // InsetMathTBinom
625 //
626 /////////////////////////////////////////////////////////////////////
627
628 Inset * InsetMathTBinom::clone() const
629 {
630         return new InsetMathTBinom(*this);
631 }
632
633
634 int InsetMathTBinom::dw(int height) const
635 {
636         int w = height / 5;
637         if (w > 15)
638                 w = 15;
639         if (w < 6)
640                 w = 6;
641         return w;
642 }
643
644
645 void InsetMathTBinom::metrics(MetricsInfo & mi, Dimension & dim) const
646 {
647         StyleChanger dummy(mi.base, LM_ST_SCRIPT);
648         Dimension dim0, dim1;
649         cell(0).metrics(mi, dim0);
650         cell(1).metrics(mi, dim1);
651         dim.asc = dim0.height() + 4 + 5;
652         dim.des = dim1.height() + 4 - 5;
653         dim.wid = std::max(dim0.width(), dim1.wid) + 2 * dw(dim.height()) + 4;
654         metricsMarkers2(dim);
655         // Cache the inset dimension. 
656         setDimCache(mi, dim);
657 }
658
659
660 void InsetMathTBinom::draw(PainterInfo & pi, int x, int y) const
661 {
662         StyleChanger dummy(pi.base, LM_ST_SCRIPT);
663         Dimension const dim = dimension(*pi.base.bv);
664         Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
665         Dimension const & dim1 = cell(1).dimension(*pi.base.bv);
666         int m = x + dim.width() / 2;
667         cell(0).draw(pi, m - dim0.width() / 2, y - dim0.des - 3 - 5);
668         cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc  + 3 - 5);
669         mathed_draw_deco(pi, x, y - dim.ascent(), dw(dim.height()), dim.height(), from_ascii("("));
670         mathed_draw_deco(pi, x + dim.width() - dw(dim.height()), y - dim.ascent(),
671                 dw(dim.height()), dim.height(), from_ascii(")"));
672         drawMarkers2(pi, x, y);
673 }
674
675
676 docstring InsetMathTBinom::name() const
677 {
678         return from_ascii("tbinom");
679 }
680
681 void InsetMathTBinom::mathmlize(MathStream & os) const
682 {
683         os << MTag("mtbinom") << cell(0) << cell(1) << ETag("mtbinom");
684 }
685
686 void InsetMathTBinom::validate(LaTeXFeatures & features) const
687 {
688         features.require("amsmath");
689         InsetMathNest::validate(features);
690 }
691
692 } // namespace lyx