2 * \file InsetMathFrac.cpp
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 bool 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;
76 void InsetMathFrac::draw(PainterInfo & pi, int x, int y) const
78 setPosCache(pi, x, y);
79 int m = x + dim_.wid / 2;
80 FracChanger dummy(pi.base);
81 if (kind_ == NICEFRAC) {
82 cell(0).draw(pi, x + 2,
83 y - cell(0).descent() - 5);
84 cell(1).draw(pi, x + cell(0).width() + 5,
85 y + cell(1).ascent() / 2);
86 pi.pain.line(x + cell(0).width(),
88 x + cell(0).width() + 5,
89 y - dim_.asc + 2, LColor::math);
91 cell(0).draw(pi, m - cell(0).width() / 2,
92 y - cell(0).descent() - 2 - 5);
93 cell(1).draw(pi, m - cell(1).width() / 2,
94 y + cell(1).ascent() + 2 - 5);
96 if (kind_ == FRAC || kind_ == OVER)
97 pi.pain.line(x + 1, y - 5,
98 x + dim_.wid - 2, y - 5, LColor::math);
99 drawMarkers(pi, x, y);
103 void InsetMathFrac::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
105 cell(0).metricsT(mi, dim);
106 cell(1).metricsT(mi, dim);
107 dim.wid = max(cell(0).width(), cell(1).width());
108 dim.asc = cell(0).height() + 1;
109 dim.des = cell(1).height();
114 void InsetMathFrac::drawT(TextPainter & pain, int x, int y) const
116 int m = x + dim_.width() / 2;
117 cell(0).drawT(pain, m - cell(0).width() / 2, y - cell(0).descent() - 1);
118 cell(1).drawT(pain, m - cell(1).width() / 2, y + cell(1).ascent());
119 // ASCII art: ignore niceties
120 if (kind_ == FRAC || kind_ == OVER || kind_ == NICEFRAC)
121 pain.horizontalLine(x, y, dim_.width());
125 void InsetMathFrac::write(WriteStream & os) const
129 os << '{' << cell(0) << "\\atop " << cell(1) << '}';
132 // \\over is only for compatibility, normalize this to \\frac
133 os << "\\frac{" << cell(0) << "}{" << cell(1) << '}';
137 InsetMathNest::write(os);
143 docstring InsetMathFrac::name() const
147 return from_ascii("frac");
149 return from_ascii("over");
151 return from_ascii("nicefrac");
153 return from_ascii("atop");
155 // shut up stupid compiler
160 bool InsetMathFrac::extraBraces() const
162 return kind_ == ATOP || kind_ == OVER;
166 void InsetMathFrac::maple(MapleStream & os) const
168 os << '(' << cell(0) << ")/(" << cell(1) << ')';
172 void InsetMathFrac::mathematica(MathematicaStream & os) const
174 os << '(' << cell(0) << ")/(" << cell(1) << ')';
178 void InsetMathFrac::octave(OctaveStream & os) const
180 os << '(' << cell(0) << ")/(" << cell(1) << ')';
184 void InsetMathFrac::mathmlize(MathStream & os) const
186 os << MTag("mfrac") << cell(0) << cell(1) << ETag("mfrac");
190 void InsetMathFrac::validate(LaTeXFeatures & features) const
192 if (kind_ == NICEFRAC)
193 features.require("nicefrac");
194 InsetMathNest::validate(features);