2 * \file InsetMathFrac.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alejandro Aguilar Sierra
9 * Full author contact details are available in file CREDITS.
14 #include "InsetMathFrac.h"
16 #include "MathStream.h"
17 #include "TextPainter.h"
18 #include "LaTeXFeatures.h"
20 #include "frontends/Painter.h"
31 InsetMathFrac::InsetMathFrac(Kind kind)
36 auto_ptr<InsetBase> InsetMathFrac::doClone() const
38 return auto_ptr<InsetBase>(new InsetMathFrac(*this));
42 InsetMathFrac * InsetMathFrac::asFracInset()
44 return kind_ == ATOP ? 0 : this;
48 InsetMathFrac const * InsetMathFrac::asFracInset() const
50 return kind_ == ATOP ? 0 : this;
54 void InsetMathFrac::metrics(MetricsInfo & mi, Dimension & dim) const
56 FracChanger dummy(mi.base);
59 if (kind_ == NICEFRAC) {
60 dim.wid = cell(0).width() + cell(1).width() + 5;
61 dim.asc = cell(0).height() + 5;
62 dim.des = cell(1).height() - 5;
64 dim.wid = max(cell(0).width(), cell(1).width()) + 2;
65 dim.asc = cell(0).height() + 2 + 5;
66 dim.des = cell(1).height() + 2 - 5;
73 void InsetMathFrac::draw(PainterInfo & pi, int x, int y) const
75 setPosCache(pi, x, y);
76 int m = x + dim_.wid / 2;
77 FracChanger dummy(pi.base);
78 if (kind_ == NICEFRAC) {
79 cell(0).draw(pi, x + 2,
80 y - cell(0).descent() - 5);
81 cell(1).draw(pi, x + cell(0).width() + 5,
82 y + cell(1).ascent() / 2);
83 pi.pain.line(x + cell(0).width(),
85 x + cell(0).width() + 5,
86 y - dim_.asc + 2, LColor::math);
88 cell(0).draw(pi, m - cell(0).width() / 2,
89 y - cell(0).descent() - 2 - 5);
90 cell(1).draw(pi, m - cell(1).width() / 2,
91 y + cell(1).ascent() + 2 - 5);
93 if (kind_ == FRAC || kind_ == OVER)
94 pi.pain.line(x + 1, y - 5,
95 x + dim_.wid - 2, y - 5, LColor::math);
96 drawMarkers(pi, x, y);
100 void InsetMathFrac::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
102 cell(0).metricsT(mi, dim);
103 cell(1).metricsT(mi, dim);
104 dim.wid = max(cell(0).width(), cell(1).width());
105 dim.asc = cell(0).height() + 1;
106 dim.des = cell(1).height();
111 void InsetMathFrac::drawT(TextPainter & pain, int x, int y) const
113 int m = x + dim_.width() / 2;
114 cell(0).drawT(pain, m - cell(0).width() / 2, y - cell(0).descent() - 1);
115 cell(1).drawT(pain, m - cell(1).width() / 2, y + cell(1).ascent());
116 // ASCII art: ignore niceties
117 if (kind_ == FRAC || kind_ == OVER || kind_ == NICEFRAC)
118 pain.horizontalLine(x, y, dim_.width());
122 void InsetMathFrac::write(WriteStream & os) const
126 os << '{' << cell(0) << "\\atop " << cell(1) << '}';
129 // \\over is only for compatibility, normalize this to \\frac
130 os << "\\frac{" << cell(0) << "}{" << cell(1) << '}';
134 InsetMathNest::write(os);
140 docstring InsetMathFrac::name() const
144 return from_ascii("frac");
146 return from_ascii("over");
148 return from_ascii("nicefrac");
150 return from_ascii("atop");
152 // shut up stupid compiler
157 bool InsetMathFrac::extraBraces() const
159 return kind_ == ATOP || kind_ == OVER;
163 void InsetMathFrac::maple(MapleStream & os) const
165 os << '(' << cell(0) << ")/(" << cell(1) << ')';
169 void InsetMathFrac::mathematica(MathematicaStream & os) const
171 os << '(' << cell(0) << ")/(" << cell(1) << ')';
175 void InsetMathFrac::octave(OctaveStream & os) const
177 os << '(' << cell(0) << ")/(" << cell(1) << ')';
181 void InsetMathFrac::mathmlize(MathStream & os) const
183 os << MTag("mfrac") << cell(0) << cell(1) << ETag("mfrac");
187 void InsetMathFrac::validate(LaTeXFeatures & features) const
189 if (kind_ == NICEFRAC)
190 features.require("nicefrac");
191 InsetMathNest::validate(features);