]> git.lyx.org Git - lyx.git/blob - src/mathed/math_fracinset.C
Fix bug 2481
[lyx.git] / src / mathed / math_fracinset.C
1 /**
2  * \file math_fracinset.C
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 "math_fracinset.h"
15 #include "math_data.h"
16 #include "math_mathmlstream.h"
17 #include "textpainter.h"
18 #include "LaTeXFeatures.h"
19 #include "LColor.h"
20 #include "frontends/Painter.h"
21
22
23 using std::string;
24 using std::max;
25 using std::auto_ptr;
26
27
28 MathFracInset::MathFracInset(Kind kind)
29         : kind_(kind)
30 {}
31
32
33 auto_ptr<InsetBase> MathFracInset::doClone() const
34 {
35         return auto_ptr<InsetBase>(new MathFracInset(*this));
36 }
37
38
39 MathFracInset * MathFracInset::asFracInset()
40 {
41         return kind_ == ATOP ? 0 : this;
42 }
43
44
45 MathFracInset const * MathFracInset::asFracInset() const
46 {
47         return kind_ == ATOP ? 0 : this;
48 }
49
50
51 void MathFracInset::metrics(MetricsInfo & mi, Dimension & dim) const
52 {
53         FracChanger dummy(mi.base);
54         cell(0).metrics(mi);
55         cell(1).metrics(mi);
56         if (kind_ == NICEFRAC) {
57                 dim.wid =  cell(0).width() + cell(1).width() + 5;
58                 dim.asc = cell(0).height() + 5;
59                 dim.des = cell(1).height() - 5;
60         } else {
61                 dim.wid = max(cell(0).width(), cell(1).width()) + 2;
62                 dim.asc = cell(0).height() + 2 + 5;
63                 dim.des = cell(1).height() + 2 - 5;
64         }
65         metricsMarkers(dim);
66         dim_ = dim;
67 }
68
69
70 void MathFracInset::draw(PainterInfo & pi, int x, int y) const
71 {
72         setPosCache(pi, x, y);
73         int m = x + dim_.wid / 2;
74         FracChanger dummy(pi.base);
75         if (kind_ == NICEFRAC) {
76                 cell(0).draw(pi, x + 2, 
77                                 y - cell(0).descent() - 5);
78                 cell(1).draw(pi, x + cell(0).width() + 5,
79                                 y + cell(1).ascent() / 2);
80         } else {
81                 cell(0).draw(pi, m - cell(0).width() / 2, 
82                                 y - cell(0).descent() - 2 - 5);
83                 cell(1).draw(pi, m - cell(1).width() / 2,
84                                 y + cell(1).ascent()  + 2 - 5);
85         }
86         if (kind_ == NICEFRAC)
87                 pi.pain.line(x + cell(0).width(), 
88                                 y + dim_.des - 2, 
89                                 x + cell(0).width() + 5, 
90                                 y - dim_.asc + 2, LColor::math);
91         if (kind_ == FRAC)
92                 pi.pain.line(x + 1, y - 5, 
93                                 x + dim_.wid - 2, y - 5, LColor::math);
94         drawMarkers(pi, x, y);
95 }
96
97
98 void MathFracInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
99 {
100         cell(0).metricsT(mi, dim);
101         cell(1).metricsT(mi, dim);
102         dim.wid = max(cell(0).width(), cell(1).width());
103         dim.asc = cell(0).height() + 1;
104         dim.des = cell(1).height();
105         //dim = dim_;
106 }
107
108
109 void MathFracInset::drawT(TextPainter & pain, int x, int y) const
110 {
111         int m = x + dim_.width() / 2;
112         cell(0).drawT(pain, m - cell(0).width() / 2, y - cell(0).descent() - 1);
113         cell(1).drawT(pain, m - cell(1).width() / 2, y + cell(1).ascent());
114         // ASCII art: ignore niceties
115         if (kind_ == FRAC || kind_ == NICEFRAC)
116                 pain.horizontalLine(x, y, dim_.width());
117 }
118
119
120 void MathFracInset::write(WriteStream & os) const
121 {
122         if (kind_ == ATOP)
123                 os << '{' << cell(0) << "\\atop " << cell(1) << '}';
124         else // it's \\frac
125                 MathNestInset::write(os);
126 }
127
128
129 string MathFracInset::name() const
130 {
131         switch (kind_) {
132         case FRAC:
133                 return "frac";
134         case NICEFRAC:
135                 return "nicefrac";
136         case ATOP:
137                 return "atop";
138         default:
139                 return string();
140         }
141 }
142
143
144 bool MathFracInset::extraBraces() const
145 {
146         return kind_ == ATOP;
147 }
148
149
150 void MathFracInset::maple(MapleStream & os) const
151 {
152         os << '(' << cell(0) << ")/(" << cell(1) << ')';
153 }
154
155
156 void MathFracInset::mathematica(MathematicaStream & os) const
157 {
158         os << '(' << cell(0) << ")/(" << cell(1) << ')';
159 }
160
161
162 void MathFracInset::octave(OctaveStream & os) const
163 {
164         os << '(' << cell(0) << ")/(" << cell(1) << ')';
165 }
166
167
168 void MathFracInset::mathmlize(MathMLStream & os) const
169 {
170         os << MTag("mfrac") << cell(0) << cell(1) << ETag("mfrac");
171 }
172
173
174 void MathFracInset::validate(LaTeXFeatures & features) const
175 {
176         if (kind_ == NICEFRAC)
177                 features.require("nicefrac");
178         MathNestInset::validate(features);
179 }
180