]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathFrac.cpp
4f95377d1bee2789e5a80610afed1800c4e8fdc5
[lyx.git] / src / mathed / InsetMathFrac.cpp
1 /**
2  * \file InsetMathFrac.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 #include "MathData.h"
16 #include "MathStream.h"
17 #include "TextPainter.h"
18 #include "LaTeXFeatures.h"
19 #include "Color.h"
20 #include "Cursor.h"
21 #include "frontends/Painter.h"
22
23
24 namespace lyx {
25
26 InsetMathFrac::InsetMathFrac(Kind kind, InsetMath::idx_type ncells)
27         : InsetMathFracBase(ncells), kind_(kind)
28 {}
29
30
31 Inset * InsetMathFrac::clone() const
32 {
33         return new InsetMathFrac(*this);
34 }
35
36
37 InsetMathFrac * InsetMathFrac::asFracInset()
38 {
39         return kind_ == ATOP ? 0 : this;
40 }
41
42
43 InsetMathFrac const * InsetMathFrac::asFracInset() const
44 {
45         return kind_ == ATOP ? 0 : this;
46 }
47
48
49 bool InsetMathFrac::idxRight(Cursor & cur) const
50 {
51         InsetMath::idx_type target;
52         if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
53                 if (nargs() == 3)
54                         target = 0;
55                 else if (nargs() == 2)
56                         target = 1;
57         } else
58                 return false;
59         if (cur.idx() == target)
60                 return false;
61         cur.idx() = target;
62         cur.pos() = cell(target).x2pos(cur.x_target());
63         return true;
64 }
65
66
67 bool InsetMathFrac::idxLeft(Cursor & cur) const
68 {
69         InsetMath::idx_type target;
70         if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
71                 if (nargs() == 3)
72                         target = 2;
73                 else if (nargs() == 2)
74                         target = 0;
75         } else
76                 return false;
77         if (cur.idx() == target)
78                 return false;
79         cur.idx() = target;
80         cur.pos() = cell(target).x2pos(cur.x_target());
81         return true;
82 }
83
84
85 void InsetMathFrac::metrics(MetricsInfo & mi, Dimension & dim) const
86 {
87         if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
88                 if (nargs() == 1) {
89                         ShapeChanger dummy2(mi.base.font, Font::UP_SHAPE);
90                         cell(0).metrics(mi);
91                         dim.wid = cell(0).width()+ 3;
92                         dim.asc = cell(0).ascent();
93                         dim.des = cell(0).descent();
94                 } else if (nargs() == 2) {
95                         cell(0).metrics(mi);
96                         ShapeChanger dummy2(mi.base.font, Font::UP_SHAPE);
97                         cell(1).metrics(mi);
98                         dim.wid = cell(0).width() + cell(1).width() + 5;
99                         dim.asc = std::max(cell(0).ascent(), cell(1).ascent());
100                         dim.des = std::max(cell(0).descent(), cell(1).descent());
101                 } else {
102                         cell(2).metrics(mi);
103                         ShapeChanger dummy2(mi.base.font, Font::UP_SHAPE);
104                         FracChanger dummy(mi.base);
105                         cell(0).metrics(mi);
106                         cell(1).metrics(mi);
107                         dim.wid = cell(0).width() + cell(1).width() + cell(2).width() + 10;
108                         dim.asc = std::max(cell(2).ascent(), cell(0).height() + 5);
109                         dim.des = std::max(cell(2).descent(), cell(1).height() - 5);
110                 }
111         } else {
112                 FracChanger dummy(mi.base);
113                 cell(0).metrics(mi);
114                 cell(1).metrics(mi);
115                 if (kind_ == NICEFRAC) {
116                         dim.wid = cell(0).width() + cell(1).width() + 5;
117                         dim.asc = cell(0).height() + 5;
118                         dim.des = cell(1).height() - 5;
119                 } else if (kind_ == UNITFRAC) {
120                         ShapeChanger dummy2(mi.base.font, Font::UP_SHAPE);
121                         dim.wid = cell(0).width() + cell(1).width() + 5;
122                         dim.asc = cell(0).height() + 5;
123                         dim.des = cell(1).height() - 5;
124                 } else {
125                         dim.wid = std::max(cell(0).width(), cell(1).width()) + 2;
126                         dim.asc = cell(0).height() + 2 + 5;
127                         dim.des = cell(1).height() + 2 - 5;
128                 }
129         }
130         metricsMarkers(dim);
131         dim_ = dim;
132 }
133
134
135 void InsetMathFrac::draw(PainterInfo & pi, int x, int y) const
136 {
137         setPosCache(pi, x, y);
138         int m = x + dim_.wid / 2;
139         if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
140                 if (nargs() == 1) {
141                         ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE);
142                         cell(0).draw(pi, x + 1, y);
143                 } else if (nargs() == 2) {
144                         cell(0).draw(pi, x + 1, y);
145                         ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE);
146                         cell(1).draw(pi, x + cell(0).width() + 5, y);
147                 } else {
148                         cell(2).draw(pi, x + 1, y);
149                         ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE);
150                         FracChanger dummy(pi.base);
151                         int xx = x + cell(2).width() + 5;
152                         cell(0).draw(pi, xx + 2, 
153                                          y - cell(0).descent() - 5);
154                         cell(1).draw(pi, xx  + cell(0).width() + 5, 
155                                          y + cell(1).ascent() / 2);
156                 }
157         } else {
158                 FracChanger dummy(pi.base);
159                 if (kind_ == NICEFRAC) {
160                         cell(0).draw(pi, x + 2,
161                                         y - cell(0).descent() - 5);
162                         cell(1).draw(pi, x + cell(0).width() + 5,
163                                         y + cell(1).ascent() / 2);
164                 } else if (kind_ == UNITFRAC) {
165                         ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE);
166                         cell(0).draw(pi, x + 2,
167                                         y - cell(0).descent() - 5);
168                         cell(1).draw(pi, x + cell(0).width() + 5,
169                                         y + cell(1).ascent() / 2);
170                 } else {
171                         // Classical fraction
172                         cell(0).draw(pi, m - cell(0).width() / 2,
173                                         y - cell(0).descent() - 2 - 5);
174                         cell(1).draw(pi, m - cell(1).width() / 2,
175                                         y + cell(1).ascent()  + 2 - 5);
176                 }
177         }
178         if (kind_ == NICEFRAC || kind_ == UNITFRAC) {
179                 // Diag line:
180                 int xx = x;
181                 if (nargs() == 3)
182                         xx += cell(2).width() + 5;
183                 pi.pain.line(xx + cell(0).width(),
184                                 y + dim_.des - 2,
185                                 xx + cell(0).width() + 5,
186                                 y - dim_.asc + 2, Color::math);
187         }
188         if (kind_ == FRAC || kind_ == OVER)
189                 pi.pain.line(x + 1, y - 5,
190                                 x + dim_.wid - 2, y - 5, Color::math);
191         drawMarkers(pi, x, y);
192 }
193
194
195 void InsetMathFrac::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
196 {
197         cell(0).metricsT(mi, dim);
198         cell(1).metricsT(mi, dim);
199         dim.wid = std::max(cell(0).width(), cell(1).width());
200         dim.asc = cell(0).height() + 1;
201         dim.des = cell(1).height();
202         //dim = dim_;
203 }
204
205
206 void InsetMathFrac::drawT(TextPainter & pain, int x, int y) const
207 {
208         int m = x + dim_.width() / 2;
209         cell(0).drawT(pain, m - cell(0).width() / 2, y - cell(0).descent() - 1);
210         cell(1).drawT(pain, m - cell(1).width() / 2, y + cell(1).ascent());
211         // ASCII art: ignore niceties
212         if (kind_ == FRAC || kind_ == OVER || kind_ == NICEFRAC || kind_ == UNITFRAC)
213                 pain.horizontalLine(x, y, dim_.width());
214 }
215
216
217 void InsetMathFrac::write(WriteStream & os) const
218 {
219         switch (kind_) {
220         case ATOP:
221                 os << '{' << cell(0) << "\\atop " << cell(1) << '}';
222                 break;
223         case OVER:
224                 // \\over is only for compatibility, normalize this to \\frac
225                 os << "\\frac{" << cell(0) << "}{" << cell(1) << '}';
226                 break;
227         case FRAC:
228         case NICEFRAC:
229         case UNITFRAC:
230                 if (nargs() == 2)
231                         InsetMathNest::write(os);
232                 else
233                         os << "\\unitfrac[" << cell(2) << "]{" << cell(0) << "}{" << cell(1) << '}';
234                 break;
235         case UNIT:
236                 if (nargs() == 2)
237                         os << "\\unit[" << cell(0) << "]{" << cell(1) << '}';
238                 else
239                         os << "\\unit{" << cell(0) << '}';
240                 break;
241         }
242 }
243
244
245 docstring InsetMathFrac::name() const
246 {
247         switch (kind_) {
248         case FRAC:
249                 return from_ascii("frac");
250         case OVER:
251                 return from_ascii("over");
252         case NICEFRAC:
253                 return from_ascii("nicefrac");
254         case UNITFRAC:
255                 return from_ascii("unitfrac");
256         case UNIT:
257                 return from_ascii("unit");
258         case ATOP:
259                 return from_ascii("atop");
260         }
261         // shut up stupid compiler
262         return docstring();
263 }
264
265
266 bool InsetMathFrac::extraBraces() const
267 {
268         return kind_ == ATOP || kind_ == OVER;
269 }
270
271
272 void InsetMathFrac::maple(MapleStream & os) const
273 {
274         os << '(' << cell(0) << ")/(" << cell(1) << ')';
275 }
276
277
278 void InsetMathFrac::mathematica(MathematicaStream & os) const
279 {
280         os << '(' << cell(0) << ")/(" << cell(1) << ')';
281 }
282
283
284 void InsetMathFrac::octave(OctaveStream & os) const
285 {
286         os << '(' << cell(0) << ")/(" << cell(1) << ')';
287 }
288
289
290 void InsetMathFrac::mathmlize(MathStream & os) const
291 {
292         os << MTag("mfrac") << cell(0) << cell(1) << ETag("mfrac");
293 }
294
295
296 void InsetMathFrac::validate(LaTeXFeatures & features) const
297 {
298         if (kind_ == NICEFRAC || kind_ == UNITFRAC || kind_ == UNIT)
299                 features.require("units");
300         InsetMathNest::validate(features);
301 }
302
303
304
305 } // namespace lyx