2003-08-19 13:00:56 +00:00
|
|
|
|
/**
|
2007-04-25 03:01:35 +00:00
|
|
|
|
* \file InsetMathFrac.cpp
|
2003-08-19 13:00:56 +00:00
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
|
*
|
|
|
|
|
* \author Alejandro Aguilar Sierra
|
|
|
|
|
* \author Andr<EFBFBD> P<EFBFBD>nitz
|
|
|
|
|
*
|
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
2006-09-17 09:14:18 +00:00
|
|
|
|
#include "InsetMathFrac.h"
|
2007-04-26 16:05:57 +00:00
|
|
|
|
#include "MathData.h"
|
2006-10-22 10:15:23 +00:00
|
|
|
|
#include "MathStream.h"
|
2006-09-17 09:14:18 +00:00
|
|
|
|
#include "TextPainter.h"
|
2006-04-06 09:46:01 +00:00
|
|
|
|
#include "LaTeXFeatures.h"
|
2007-04-26 17:34:20 +00:00
|
|
|
|
#include "Color.h"
|
2003-09-07 21:25:37 +00:00
|
|
|
|
#include "frontends/Painter.h"
|
2001-02-14 15:00:50 +00:00
|
|
|
|
|
2001-02-13 13:28:32 +00:00
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
namespace lyx {
|
|
|
|
|
|
|
|
|
|
|
2003-10-06 15:43:21 +00:00
|
|
|
|
using std::string;
|
2002-02-16 15:59:55 +00:00
|
|
|
|
using std::max;
|
2003-07-25 17:11:25 +00:00
|
|
|
|
using std::auto_ptr;
|
2002-02-16 15:59:55 +00:00
|
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
|
InsetMathFrac::InsetMathFrac(Kind kind)
|
2006-04-06 09:46:01 +00:00
|
|
|
|
: kind_(kind)
|
2001-07-09 10:19:50 +00:00
|
|
|
|
{}
|
2001-02-13 13:28:32 +00:00
|
|
|
|
|
|
|
|
|
|
2007-04-29 13:39:47 +00:00
|
|
|
|
auto_ptr<Inset> InsetMathFrac::doClone() const
|
2002-03-21 17:42:56 +00:00
|
|
|
|
{
|
2007-04-29 13:39:47 +00:00
|
|
|
|
return auto_ptr<Inset>(new InsetMathFrac(*this));
|
2001-02-13 13:28:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
|
InsetMathFrac * InsetMathFrac::asFracInset()
|
2001-11-13 18:33:48 +00:00
|
|
|
|
{
|
2006-04-06 09:46:01 +00:00
|
|
|
|
return kind_ == ATOP ? 0 : this;
|
2001-11-13 18:33:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
|
InsetMathFrac const * InsetMathFrac::asFracInset() const
|
2002-08-08 16:08:11 +00:00
|
|
|
|
{
|
2006-04-06 09:46:01 +00:00
|
|
|
|
return kind_ == ATOP ? 0 : this;
|
2002-08-08 16:08:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-11-28 15:15:49 +00:00
|
|
|
|
bool InsetMathFrac::metrics(MetricsInfo & mi, Dimension & dim) const
|
2001-02-13 13:28:32 +00:00
|
|
|
|
{
|
2003-03-21 14:20:48 +00:00
|
|
|
|
FracChanger dummy(mi.base);
|
2002-08-02 14:29:42 +00:00
|
|
|
|
cell(0).metrics(mi);
|
|
|
|
|
cell(1).metrics(mi);
|
2006-04-06 09:46:01 +00:00
|
|
|
|
if (kind_ == NICEFRAC) {
|
2007-03-29 22:01:21 +00:00
|
|
|
|
dim.wid = cell(0).width() + cell(1).width() + 5;
|
2006-04-06 09:46:01 +00:00
|
|
|
|
dim.asc = cell(0).height() + 5;
|
|
|
|
|
dim.des = cell(1).height() - 5;
|
|
|
|
|
} else {
|
|
|
|
|
dim.wid = max(cell(0).width(), cell(1).width()) + 2;
|
|
|
|
|
dim.asc = cell(0).height() + 2 + 5;
|
|
|
|
|
dim.des = cell(1).height() + 2 - 5;
|
|
|
|
|
}
|
2004-04-07 16:54:15 +00:00
|
|
|
|
metricsMarkers(dim);
|
2006-11-28 15:15:49 +00:00
|
|
|
|
if (dim_ == dim)
|
|
|
|
|
return false;
|
2004-04-07 16:54:15 +00:00
|
|
|
|
dim_ = dim;
|
2006-11-28 15:15:49 +00:00
|
|
|
|
return true;
|
2001-02-13 13:28:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
|
void InsetMathFrac::draw(PainterInfo & pi, int x, int y) const
|
2001-02-13 13:28:32 +00:00
|
|
|
|
{
|
2004-01-30 11:41:12 +00:00
|
|
|
|
setPosCache(pi, x, y);
|
2003-05-28 13:22:36 +00:00
|
|
|
|
int m = x + dim_.wid / 2;
|
2003-03-21 14:20:48 +00:00
|
|
|
|
FracChanger dummy(pi.base);
|
2006-04-06 09:46:01 +00:00
|
|
|
|
if (kind_ == NICEFRAC) {
|
2007-05-28 22:27:45 +00:00
|
|
|
|
cell(0).draw(pi, x + 2,
|
2006-04-06 09:46:01 +00:00
|
|
|
|
y - cell(0).descent() - 5);
|
|
|
|
|
cell(1).draw(pi, x + cell(0).width() + 5,
|
|
|
|
|
y + cell(1).ascent() / 2);
|
2007-05-28 22:27:45 +00:00
|
|
|
|
pi.pain.line(x + cell(0).width(),
|
|
|
|
|
y + dim_.des - 2,
|
|
|
|
|
x + cell(0).width() + 5,
|
2007-04-26 17:34:20 +00:00
|
|
|
|
y - dim_.asc + 2, Color::math);
|
2006-04-06 09:46:01 +00:00
|
|
|
|
} else {
|
2007-05-28 22:27:45 +00:00
|
|
|
|
cell(0).draw(pi, m - cell(0).width() / 2,
|
2006-04-06 09:46:01 +00:00
|
|
|
|
y - cell(0).descent() - 2 - 5);
|
|
|
|
|
cell(1).draw(pi, m - cell(1).width() / 2,
|
|
|
|
|
y + cell(1).ascent() + 2 - 5);
|
|
|
|
|
}
|
2006-07-19 10:50:18 +00:00
|
|
|
|
if (kind_ == FRAC || kind_ == OVER)
|
2007-05-28 22:27:45 +00:00
|
|
|
|
pi.pain.line(x + 1, y - 5,
|
2007-04-26 17:34:20 +00:00
|
|
|
|
x + dim_.wid - 2, y - 5, Color::math);
|
2004-04-07 16:54:15 +00:00
|
|
|
|
drawMarkers(pi, x, y);
|
2001-02-13 13:28:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
|
void InsetMathFrac::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
|
2002-03-19 16:55:58 +00:00
|
|
|
|
{
|
2003-05-28 13:22:36 +00:00
|
|
|
|
cell(0).metricsT(mi, dim);
|
|
|
|
|
cell(1).metricsT(mi, dim);
|
|
|
|
|
dim.wid = max(cell(0).width(), cell(1).width());
|
|
|
|
|
dim.asc = cell(0).height() + 1;
|
|
|
|
|
dim.des = cell(1).height();
|
2003-06-02 10:03:27 +00:00
|
|
|
|
//dim = dim_;
|
2002-03-19 16:55:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
|
void InsetMathFrac::drawT(TextPainter & pain, int x, int y) const
|
2002-03-19 16:55:58 +00:00
|
|
|
|
{
|
2003-05-28 13:22:36 +00:00
|
|
|
|
int m = x + dim_.width() / 2;
|
2002-08-02 14:29:42 +00:00
|
|
|
|
cell(0).drawT(pain, m - cell(0).width() / 2, y - cell(0).descent() - 1);
|
|
|
|
|
cell(1).drawT(pain, m - cell(1).width() / 2, y + cell(1).ascent());
|
2006-04-06 09:46:01 +00:00
|
|
|
|
// ASCII art: ignore niceties
|
2006-07-19 10:50:18 +00:00
|
|
|
|
if (kind_ == FRAC || kind_ == OVER || kind_ == NICEFRAC)
|
2003-05-28 13:22:36 +00:00
|
|
|
|
pain.horizontalLine(x, y, dim_.width());
|
2002-03-19 16:55:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
|
void InsetMathFrac::write(WriteStream & os) const
|
2001-02-13 13:28:32 +00:00
|
|
|
|
{
|
2006-07-19 10:50:18 +00:00
|
|
|
|
switch (kind_) {
|
|
|
|
|
case ATOP:
|
2001-10-19 11:25:48 +00:00
|
|
|
|
os << '{' << cell(0) << "\\atop " << cell(1) << '}';
|
2006-07-19 10:50:18 +00:00
|
|
|
|
break;
|
|
|
|
|
case OVER:
|
|
|
|
|
// \\over is only for compatibility, normalize this to \\frac
|
|
|
|
|
os << "\\frac{" << cell(0) << "}{" << cell(1) << '}';
|
|
|
|
|
break;
|
|
|
|
|
case FRAC:
|
|
|
|
|
case NICEFRAC:
|
2006-09-16 18:11:38 +00:00
|
|
|
|
InsetMathNest::write(os);
|
2006-07-19 10:50:18 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2001-02-13 13:28:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-10-22 10:15:23 +00:00
|
|
|
|
docstring InsetMathFrac::name() const
|
2001-02-13 13:28:32 +00:00
|
|
|
|
{
|
2006-04-06 09:46:01 +00:00
|
|
|
|
switch (kind_) {
|
|
|
|
|
case FRAC:
|
2006-10-22 10:15:23 +00:00
|
|
|
|
return from_ascii("frac");
|
2006-07-19 10:50:18 +00:00
|
|
|
|
case OVER:
|
2006-10-22 10:15:23 +00:00
|
|
|
|
return from_ascii("over");
|
2006-04-06 09:46:01 +00:00
|
|
|
|
case NICEFRAC:
|
2006-10-22 10:15:23 +00:00
|
|
|
|
return from_ascii("nicefrac");
|
2006-04-06 09:46:01 +00:00
|
|
|
|
case ATOP:
|
2006-10-22 10:15:23 +00:00
|
|
|
|
return from_ascii("atop");
|
2006-04-06 09:46:01 +00:00
|
|
|
|
}
|
2006-07-31 17:44:50 +00:00
|
|
|
|
// shut up stupid compiler
|
2006-10-22 10:15:23 +00:00
|
|
|
|
return docstring();
|
2001-02-13 13:28:32 +00:00
|
|
|
|
}
|
2001-11-07 08:51:35 +00:00
|
|
|
|
|
2002-08-01 15:53:46 +00:00
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
|
bool InsetMathFrac::extraBraces() const
|
2006-07-16 17:19:05 +00:00
|
|
|
|
{
|
2006-07-19 10:50:18 +00:00
|
|
|
|
return kind_ == ATOP || kind_ == OVER;
|
2006-07-16 17:19:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
|
void InsetMathFrac::maple(MapleStream & os) const
|
2001-11-07 08:51:35 +00:00
|
|
|
|
{
|
2001-11-13 16:27:06 +00:00
|
|
|
|
os << '(' << cell(0) << ")/(" << cell(1) << ')';
|
2001-11-07 17:30:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-08-01 15:53:46 +00:00
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
|
void InsetMathFrac::mathematica(MathematicaStream & os) const
|
2002-07-01 11:17:14 +00:00
|
|
|
|
{
|
|
|
|
|
os << '(' << cell(0) << ")/(" << cell(1) << ')';
|
|
|
|
|
}
|
2001-11-07 17:30:26 +00:00
|
|
|
|
|
2002-08-01 15:53:46 +00:00
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
|
void InsetMathFrac::octave(OctaveStream & os) const
|
2002-04-25 15:37:10 +00:00
|
|
|
|
{
|
|
|
|
|
os << '(' << cell(0) << ")/(" << cell(1) << ')';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-10-22 10:15:23 +00:00
|
|
|
|
void InsetMathFrac::mathmlize(MathStream & os) const
|
2001-11-07 17:30:26 +00:00
|
|
|
|
{
|
2001-11-08 12:06:56 +00:00
|
|
|
|
os << MTag("mfrac") << cell(0) << cell(1) << ETag("mfrac");
|
2001-11-07 08:51:35 +00:00
|
|
|
|
}
|
2006-04-06 09:46:01 +00:00
|
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
|
void InsetMathFrac::validate(LaTeXFeatures & features) const
|
2006-04-06 09:46:01 +00:00
|
|
|
|
{
|
|
|
|
|
if (kind_ == NICEFRAC)
|
|
|
|
|
features.require("nicefrac");
|
2006-09-16 18:11:38 +00:00
|
|
|
|
InsetMathNest::validate(features);
|
2006-04-06 09:46:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|