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