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