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