]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathFrac.cpp
863107403074570638dd0181e2fb938488eecada
[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         Dimension dim0, dim1, dim2;
88
89         if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
90                 if (nargs() == 1) {
91                         ShapeChanger dummy2(mi.base.font, Font::UP_SHAPE);
92                         cell(0).metrics(mi, dim0);
93                         dim.wid = dim0.width()+ 3;
94                         dim.asc = dim0.asc;
95                         dim.des = dim0.des;
96                 } else if (nargs() == 2) {
97                         cell(0).metrics(mi, dim0);
98                         ShapeChanger dummy2(mi.base.font, Font::UP_SHAPE);
99                         cell(1).metrics(mi, dim1);
100                         dim.wid = dim0.width() + dim1.wid + 5;
101                         dim.asc = std::max(dim0.asc, dim1.asc);
102                         dim.des = std::max(dim0.des, dim1.des);
103                 } else {
104                         cell(2).metrics(mi, dim2);
105                         ShapeChanger dummy2(mi.base.font, Font::UP_SHAPE);
106                         FracChanger dummy(mi.base);
107                         cell(0).metrics(mi, dim0);
108                         cell(1).metrics(mi, dim1);
109                         dim.wid = dim0.width() + dim1.wid + dim2.wid + 10;
110                         dim.asc = std::max(dim2.asc, dim0.height() + 5);
111                         dim.des = std::max(dim2.des, dim1.height() - 5);
112                 }
113         } else {
114                 FracChanger dummy(mi.base);
115                 cell(0).metrics(mi, dim0);
116                 cell(1).metrics(mi, dim1);
117                 if (kind_ == NICEFRAC) {
118                         dim.wid = dim0.width() + dim1.wid + 5;
119                         dim.asc = dim0.height() + 5;
120                         dim.des = dim1.height() - 5;
121                 } else if (kind_ == UNITFRAC) {
122                         ShapeChanger dummy2(mi.base.font, Font::UP_SHAPE);
123                         dim.wid = dim0.width() + dim1.wid + 5;
124                         dim.asc = dim0.height() + 5;
125                         dim.des = dim1.height() - 5;
126                 } else {
127                         dim.wid = std::max(dim0.width(), dim1.wid) + 2;
128                         dim.asc = dim0.height() + 2 + 5;
129                         dim.des = dim1.height() + 2 - 5;
130                 }
131         }
132         metricsMarkers(dim);
133         // Cache the inset dimension. 
134         setDimCache(mi, dim);
135 }
136
137
138 void InsetMathFrac::draw(PainterInfo & pi, int x, int y) const
139 {
140         setPosCache(pi, x, y);
141         Dimension const dim = dimension(*pi.base.bv);
142         Dimension const dim0 = cell(0).dimension(*pi.base.bv);
143         Dimension const dim1 = cell(1).dimension(*pi.base.bv);
144         Dimension const dim2 = cell(0).dimension(*pi.base.bv);
145         int m = x + dim.wid / 2;
146         if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
147                 if (nargs() == 1) {
148                         ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE);
149                         cell(0).draw(pi, x + 1, y);
150                 } else if (nargs() == 2) {
151                         cell(0).draw(pi, x + 1, y);
152                         ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE);
153                         cell(1).draw(pi, x + dim0.width() + 5, y);
154                 } else {
155                         cell(2).draw(pi, x + 1, y);
156                         ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE);
157                         FracChanger dummy(pi.base);
158                         int xx = x + dim2.wid + 5;
159                         cell(0).draw(pi, xx + 2, 
160                                          y - dim0.des - 5);
161                         cell(1).draw(pi, xx  + dim0.width() + 5, 
162                                          y + dim1.asc / 2);
163                 }
164         } else {
165                 FracChanger dummy(pi.base);
166                 if (kind_ == NICEFRAC) {
167                         cell(0).draw(pi, x + 2,
168                                         y - dim0.des - 5);
169                         cell(1).draw(pi, x + dim0.width() + 5,
170                                         y + dim1.asc / 2);
171                 } else if (kind_ == UNITFRAC) {
172                         ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE);
173                         cell(0).draw(pi, x + 2,
174                                         y - dim0.des - 5);
175                         cell(1).draw(pi, x + dim0.width() + 5,
176                                         y + dim1.asc / 2);
177                 } else {
178                         // Classical fraction
179                         cell(0).draw(pi, m - dim0.width() / 2,
180                                         y - dim0.des - 2 - 5);
181                         cell(1).draw(pi, m - dim1.wid / 2,
182                                         y + dim1.asc  + 2 - 5);
183                 }
184         }
185         if (kind_ == NICEFRAC || kind_ == UNITFRAC) {
186                 // Diag line:
187                 int xx = x;
188                 if (nargs() == 3)
189                         xx += dim2.wid + 5;
190                 pi.pain.line(xx + dim0.wid,
191                                 y + dim.des - 2,
192                                 xx + dim0.wid + 5,
193                                 y - dim.asc + 2, Color::math);
194         }
195         if (kind_ == FRAC || kind_ == OVER)
196                 pi.pain.line(x + 1, y - 5,
197                                 x + dim.wid - 2, y - 5, Color::math);
198         drawMarkers(pi, x, y);
199 }
200
201
202 void InsetMathFrac::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
203 {
204         Dimension dim0, dim1;
205         cell(0).metricsT(mi, dim0);
206         cell(1).metricsT(mi, dim1);
207         dim.wid = std::max(dim0.width(), dim1.wid);
208         dim.asc = dim0.height() + 1;
209         dim.des = dim1.height();
210 }
211
212
213 void InsetMathFrac::drawT(TextPainter & pain, int x, int y) const
214 {
215         // FIXME: BROKEN!
216         /*
217         Dimension dim;
218         int m = x + dim.width() / 2;
219         cell(0).drawT(pain, m - dim0.width() / 2, y - dim0.des - 1);
220         cell(1).drawT(pain, m - dim1.wid / 2, y + dim1.asc);
221         // ASCII art: ignore niceties
222         if (kind_ == FRAC || kind_ == OVER || kind_ == NICEFRAC || kind_ == UNITFRAC)
223                 pain.horizontalLine(x, y, dim.width());
224         */
225 }
226
227
228 void InsetMathFrac::write(WriteStream & os) const
229 {
230         switch (kind_) {
231         case ATOP:
232                 os << '{' << cell(0) << "\\atop " << cell(1) << '}';
233                 break;
234         case OVER:
235                 // \\over is only for compatibility, normalize this to \\frac
236                 os << "\\frac{" << cell(0) << "}{" << cell(1) << '}';
237                 break;
238         case FRAC:
239         case NICEFRAC:
240         case UNITFRAC:
241                 if (nargs() == 2)
242                         InsetMathNest::write(os);
243                 else
244                         os << "\\unitfrac[" << cell(2) << "]{" << cell(0) << "}{" << cell(1) << '}';
245                 break;
246         case UNIT:
247                 if (nargs() == 2)
248                         os << "\\unit[" << cell(0) << "]{" << cell(1) << '}';
249                 else
250                         os << "\\unit{" << cell(0) << '}';
251                 break;
252         }
253 }
254
255
256 docstring InsetMathFrac::name() const
257 {
258         switch (kind_) {
259         case FRAC:
260                 return from_ascii("frac");
261         case OVER:
262                 return from_ascii("over");
263         case NICEFRAC:
264                 return from_ascii("nicefrac");
265         case UNITFRAC:
266                 return from_ascii("unitfrac");
267         case UNIT:
268                 return from_ascii("unit");
269         case ATOP:
270                 return from_ascii("atop");
271         }
272         // shut up stupid compiler
273         return docstring();
274 }
275
276
277 bool InsetMathFrac::extraBraces() const
278 {
279         return kind_ == ATOP || kind_ == OVER;
280 }
281
282
283 void InsetMathFrac::maple(MapleStream & os) const
284 {
285         os << '(' << cell(0) << ")/(" << cell(1) << ')';
286 }
287
288
289 void InsetMathFrac::mathematica(MathematicaStream & os) const
290 {
291         os << '(' << cell(0) << ")/(" << cell(1) << ')';
292 }
293
294
295 void InsetMathFrac::octave(OctaveStream & os) const
296 {
297         os << '(' << cell(0) << ")/(" << cell(1) << ')';
298 }
299
300
301 void InsetMathFrac::mathmlize(MathStream & os) const
302 {
303         os << MTag("mfrac") << cell(0) << cell(1) << ETag("mfrac");
304 }
305
306
307 void InsetMathFrac::validate(LaTeXFeatures & features) const
308 {
309         if (kind_ == NICEFRAC || kind_ == UNITFRAC || kind_ == UNIT)
310                 features.require("units");
311         InsetMathNest::validate(features);
312 }
313
314
315
316 } // namespace lyx