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