]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathFrac.cpp
1babb9768e14a22187edaada7dc31ef4beb01b43
[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 = 0;
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 = 0;
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         int m = x + dim.wid / 2;
144         if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
145                 if (nargs() == 1) {
146                         ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE);
147                         cell(0).draw(pi, x + 1, y);
148                 } else if (nargs() == 2) {
149                         cell(0).draw(pi, x + 1, y);
150                         ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE);
151                         cell(1).draw(pi, x + dim0.width() + 5, y);
152                 } else {
153                         cell(2).draw(pi, x + 1, y);
154                         ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE);
155                         FracChanger dummy(pi.base);
156                         Dimension const dim1 = cell(1).dimension(*pi.base.bv);
157                         Dimension const dim2 = cell(2).dimension(*pi.base.bv);
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                 Dimension const dim1 = cell(1).dimension(*pi.base.bv);
167                 if (kind_ == NICEFRAC) {
168                         cell(0).draw(pi, x + 2,
169                                         y - dim0.des - 5);
170                         cell(1).draw(pi, x + dim0.width() + 5,
171                                         y + dim1.asc / 2);
172                 } else if (kind_ == UNITFRAC) {
173                         ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE);
174                         cell(0).draw(pi, x + 2,
175                                         y - dim0.des - 5);
176                         cell(1).draw(pi, x + dim0.width() + 5,
177                                         y + dim1.asc / 2);
178                 } else {
179                         // Classical fraction
180                         cell(0).draw(pi, m - dim0.width() / 2,
181                                         y - dim0.des - 2 - 5);
182                         cell(1).draw(pi, m - dim1.wid / 2,
183                                         y + dim1.asc  + 2 - 5);
184                 }
185         }
186         if (kind_ == NICEFRAC || kind_ == UNITFRAC) {
187                 // Diag line:
188                 int xx = x;
189                 Dimension const dim2 = cell(2).dimension(*pi.base.bv);
190                 if (nargs() == 3)
191                         xx += dim2.wid + 5;
192                 pi.pain.line(xx + dim0.wid,
193                                 y + dim.des - 2,
194                                 xx + dim0.wid + 5,
195                                 y - dim.asc + 2, Color::math);
196         }
197         if (kind_ == FRAC || kind_ == OVER)
198                 pi.pain.line(x + 1, y - 5,
199                                 x + dim.wid - 2, y - 5, Color::math);
200         drawMarkers(pi, x, y);
201 }
202
203
204 void InsetMathFrac::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
205 {
206         Dimension dim0, dim1;
207         cell(0).metricsT(mi, dim0);
208         cell(1).metricsT(mi, dim1);
209         dim.wid = std::max(dim0.width(), dim1.wid);
210         dim.asc = dim0.height() + 1;
211         dim.des = dim1.height();
212 }
213
214
215 void InsetMathFrac::drawT(TextPainter & pain, int x, int y) const
216 {
217         // FIXME: BROKEN!
218         /*
219         Dimension dim;
220         int m = x + dim.width() / 2;
221         cell(0).drawT(pain, m - dim0.width() / 2, y - dim0.des - 1);
222         cell(1).drawT(pain, m - dim1.wid / 2, y + dim1.asc);
223         // ASCII art: ignore niceties
224         if (kind_ == FRAC || kind_ == OVER || kind_ == NICEFRAC || kind_ == UNITFRAC)
225                 pain.horizontalLine(x, y, dim.width());
226         */
227 }
228
229
230 void InsetMathFrac::write(WriteStream & os) const
231 {
232         switch (kind_) {
233         case ATOP:
234                 os << '{' << cell(0) << "\\atop " << cell(1) << '}';
235                 break;
236         case OVER:
237                 // \\over is only for compatibility, normalize this to \\frac
238                 os << "\\frac{" << cell(0) << "}{" << cell(1) << '}';
239                 break;
240         case FRAC:
241         case NICEFRAC:
242         case UNITFRAC:
243                 if (nargs() == 2)
244                         InsetMathNest::write(os);
245                 else
246                         os << "\\unitfrac[" << cell(2) << "]{" << cell(0) << "}{" << cell(1) << '}';
247                 break;
248         case UNIT:
249                 if (nargs() == 2)
250                         os << "\\unit[" << cell(0) << "]{" << cell(1) << '}';
251                 else
252                         os << "\\unit{" << cell(0) << '}';
253                 break;
254         }
255 }
256
257
258 docstring InsetMathFrac::name() const
259 {
260         switch (kind_) {
261         case FRAC:
262                 return from_ascii("frac");
263         case OVER:
264                 return from_ascii("over");
265         case NICEFRAC:
266                 return from_ascii("nicefrac");
267         case UNITFRAC:
268                 return from_ascii("unitfrac");
269         case UNIT:
270                 return from_ascii("unit");
271         case ATOP:
272                 return from_ascii("atop");
273         }
274         // shut up stupid compiler
275         return docstring();
276 }
277
278
279 bool InsetMathFrac::extraBraces() const
280 {
281         return kind_ == ATOP || kind_ == OVER;
282 }
283
284
285 void InsetMathFrac::maple(MapleStream & os) const
286 {
287         os << '(' << cell(0) << ")/(" << cell(1) << ')';
288 }
289
290
291 void InsetMathFrac::mathematica(MathematicaStream & os) const
292 {
293         os << '(' << cell(0) << ")/(" << cell(1) << ')';
294 }
295
296
297 void InsetMathFrac::octave(OctaveStream & os) const
298 {
299         os << '(' << cell(0) << ")/(" << cell(1) << ')';
300 }
301
302
303 void InsetMathFrac::mathmlize(MathStream & os) const
304 {
305         os << MTag("mfrac") << cell(0) << cell(1) << ETag("mfrac");
306 }
307
308
309 void InsetMathFrac::validate(LaTeXFeatures & features) const
310 {
311         if (kind_ == NICEFRAC || kind_ == UNITFRAC || kind_ == UNIT)
312                 features.require("units");
313         InsetMathNest::validate(features);
314 }
315
316
317
318 } // namespace lyx