2003-08-19 13:00:56 +00:00
|
|
|
|
/**
|
|
|
|
|
* \file math_macro.C
|
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
|
* Licence details can be found in the file COPYING.
|
1999-09-27 18:44:28 +00:00
|
|
|
|
*
|
2003-08-19 13:00:56 +00:00
|
|
|
|
* \author Alejandro Aguilar Sierra
|
|
|
|
|
* \author Andr<EFBFBD> P<EFBFBD>nitz
|
1999-09-27 18:44:28 +00:00
|
|
|
|
*
|
2003-08-19 13:00:56 +00:00
|
|
|
|
* Full author contact details are available in file CREDITS.
|
1999-09-27 18:44:28 +00:00
|
|
|
|
*/
|
|
|
|
|
|
2003-08-02 11:30:30 +00:00
|
|
|
|
#include <config.h>
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
#include "math_macro.h"
|
2001-11-08 12:15:12 +00:00
|
|
|
|
#include "math_support.h"
|
2001-11-16 08:26:41 +00:00
|
|
|
|
#include "math_extern.h"
|
2001-11-08 12:06:56 +00:00
|
|
|
|
#include "math_mathmlstream.h"
|
2004-04-13 06:27:29 +00:00
|
|
|
|
|
|
|
|
|
#include "buffer.h"
|
2004-01-20 14:25:24 +00:00
|
|
|
|
#include "cursor.h"
|
2001-11-16 08:26:41 +00:00
|
|
|
|
#include "debug.h"
|
2004-04-13 06:27:29 +00:00
|
|
|
|
#include "BufferView.h"
|
2001-07-13 14:54:56 +00:00
|
|
|
|
#include "LaTeXFeatures.h"
|
2005-07-17 10:31:44 +00:00
|
|
|
|
#include "frontends/Painter.h"
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
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;
|
2003-08-02 11:30:30 +00:00
|
|
|
|
using std::endl;
|
2002-02-16 15:59:55 +00:00
|
|
|
|
|
|
|
|
|
|
2004-04-13 13:54:58 +00:00
|
|
|
|
MathMacro::MathMacro(string const & name, int numargs)
|
|
|
|
|
: MathNestInset(numargs), name_(name)
|
2001-07-12 11:55:57 +00:00
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
2004-11-23 23:04:52 +00:00
|
|
|
|
auto_ptr<InsetBase> MathMacro::doClone() const
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2003-07-25 17:11:25 +00:00
|
|
|
|
return auto_ptr<InsetBase>(new MathMacro(*this));
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-08-09 09:19:18 +00:00
|
|
|
|
|
2002-08-01 15:53:46 +00:00
|
|
|
|
string MathMacro::name() const
|
2001-08-08 17:26:30 +00:00
|
|
|
|
{
|
2004-04-13 06:27:29 +00:00
|
|
|
|
return name_;
|
2001-08-08 17:26:30 +00:00
|
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2001-08-09 09:19:18 +00:00
|
|
|
|
|
2005-10-05 21:19:32 +00:00
|
|
|
|
void MathMacro::cursorPos(CursorSlice const & sl, bool boundary, int & x,
|
|
|
|
|
int & y) const
|
|
|
|
|
{
|
|
|
|
|
// We may have 0 arguments, but MathNestInset requires at least one.
|
|
|
|
|
if (nargs() > 0)
|
|
|
|
|
MathNestInset::cursorPos(sl, boundary, x, y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-04-13 06:27:29 +00:00
|
|
|
|
void MathMacro::metrics(MetricsInfo & mi, Dimension & dim) const
|
2002-03-25 12:11:25 +00:00
|
|
|
|
{
|
2004-04-13 13:54:58 +00:00
|
|
|
|
if (!MacroTable::globalMacros().has(name())) {
|
|
|
|
|
mathed_string_dim(mi.base.font, "Unknown: " + name(), dim);
|
|
|
|
|
} else if (editing(mi.base.bv)) {
|
|
|
|
|
asArray(MacroTable::globalMacros().get(name()).def(), tmpl_);
|
|
|
|
|
LyXFont font = mi.base.font;
|
|
|
|
|
augmentFont(font, "lyxtex");
|
|
|
|
|
tmpl_.metrics(mi, dim);
|
|
|
|
|
dim.wid += mathed_string_width(font, name()) + 10;
|
|
|
|
|
int ww = mathed_string_width(font, "#1: ");
|
|
|
|
|
for (idx_type i = 0; i < nargs(); ++i) {
|
|
|
|
|
MathArray const & c = cell(i);
|
|
|
|
|
c.metrics(mi);
|
|
|
|
|
dim.wid = max(dim.wid, c.width() + ww);
|
|
|
|
|
dim.des += c.height() + 10;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
MacroTable::globalMacros().get(name()).expand(cells_, expanded_);
|
|
|
|
|
expanded_.metrics(mi, dim);
|
|
|
|
|
}
|
|
|
|
|
metricsMarkers2(dim);
|
2004-01-30 11:41:12 +00:00
|
|
|
|
dim_ = dim;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-03-21 14:20:48 +00:00
|
|
|
|
void MathMacro::draw(PainterInfo & pi, int x, int y) const
|
2000-02-10 17:53:36 +00:00
|
|
|
|
{
|
2004-04-13 13:54:58 +00:00
|
|
|
|
if (!MacroTable::globalMacros().has(name())) {
|
|
|
|
|
drawStrRed(pi, x, y, "Unknown: " + name());
|
|
|
|
|
} else if (editing(pi.base.bv)) {
|
|
|
|
|
LyXFont font = pi.base.font;
|
|
|
|
|
augmentFont(font, "lyxtex");
|
|
|
|
|
int h = y - dim_.ascent() + 2 + tmpl_.ascent();
|
2005-07-17 10:31:44 +00:00
|
|
|
|
pi.pain.text(x + 3, h, name(), font);
|
2004-04-13 13:54:58 +00:00
|
|
|
|
int const w = mathed_string_width(font, name());
|
|
|
|
|
tmpl_.draw(pi, x + w + 12, h);
|
|
|
|
|
h += tmpl_.descent();
|
|
|
|
|
Dimension ldim;
|
|
|
|
|
mathed_string_dim(font, "#1: ", ldim);
|
|
|
|
|
for (idx_type i = 0; i < nargs(); ++i) {
|
|
|
|
|
MathArray const & c = cell(i);
|
|
|
|
|
h += max(c.ascent(), ldim.asc) + 5;
|
|
|
|
|
c.draw(pi, x + ldim.wid, h);
|
|
|
|
|
char str[] = "#1:";
|
|
|
|
|
str[1] += static_cast<char>(i);
|
2005-07-17 10:31:44 +00:00
|
|
|
|
pi.pain.text(x + 3, h, str, font);
|
2004-04-13 13:54:58 +00:00
|
|
|
|
h += max(c.descent(), ldim.des) + 5;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
expanded_.draw(pi, x, y);
|
|
|
|
|
}
|
2004-04-13 06:27:29 +00:00
|
|
|
|
drawMarkers2(pi, x, y);
|
2001-04-24 16:13:38 +00:00
|
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2001-04-25 15:43:57 +00:00
|
|
|
|
|
2005-10-05 21:19:32 +00:00
|
|
|
|
void MathMacro::drawSelection(PainterInfo & pi, int x, int y) const
|
|
|
|
|
{
|
|
|
|
|
// We may have 0 arguments, but MathNestInset requires at least one.
|
|
|
|
|
if (nargs() > 0)
|
|
|
|
|
MathNestInset::drawSelection(pi, x, y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2001-07-26 06:56:43 +00:00
|
|
|
|
void MathMacro::validate(LaTeXFeatures & features) const
|
2001-07-13 14:54:56 +00:00
|
|
|
|
{
|
2002-04-03 10:45:32 +00:00
|
|
|
|
if (name() == "binom" || name() == "mathcircumflex")
|
|
|
|
|
features.require(name());
|
2001-07-13 14:54:56 +00:00
|
|
|
|
}
|
2001-11-16 08:26:41 +00:00
|
|
|
|
|
|
|
|
|
|
2005-10-05 21:19:32 +00:00
|
|
|
|
InsetBase * MathMacro::editXY(LCursor & cur, int x, int y)
|
|
|
|
|
{
|
|
|
|
|
// We may have 0 arguments, but MathNestInset requires at least one.
|
2006-02-14 10:41:34 +00:00
|
|
|
|
if (nargs() > 0) {
|
|
|
|
|
// Prevent crash due to cold coordcache
|
|
|
|
|
// FIXME: This is only a workaround, the call of
|
|
|
|
|
// MathNestInset::editXY is correct. The correct fix would
|
|
|
|
|
// ensure that the coordcache of the arguments is valid.
|
|
|
|
|
if (!editing(&cur.bv())) {
|
|
|
|
|
edit(cur, true);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2005-10-05 21:19:32 +00:00
|
|
|
|
return MathNestInset::editXY(cur, x, y);
|
2006-02-14 10:41:34 +00:00
|
|
|
|
}
|
2005-10-05 21:19:32 +00:00
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-02-14 14:30:09 +00:00
|
|
|
|
void MathMacro::maple(MapleStream & os) const
|
2001-11-16 08:26:41 +00:00
|
|
|
|
{
|
2001-12-11 11:33:43 +00:00
|
|
|
|
updateExpansion();
|
2003-02-14 14:30:09 +00:00
|
|
|
|
::maple(expanded_, os);
|
2001-11-16 08:26:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2001-11-16 08:29:11 +00:00
|
|
|
|
void MathMacro::mathmlize(MathMLStream & os) const
|
|
|
|
|
{
|
2001-12-11 11:33:43 +00:00
|
|
|
|
updateExpansion();
|
2002-08-02 14:29:42 +00:00
|
|
|
|
::mathmlize(expanded_, os);
|
2001-11-16 08:29:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-02-14 14:30:09 +00:00
|
|
|
|
void MathMacro::octave(OctaveStream & os) const
|
2001-11-16 08:29:11 +00:00
|
|
|
|
{
|
2001-12-11 11:33:43 +00:00
|
|
|
|
updateExpansion();
|
2003-02-14 14:30:09 +00:00
|
|
|
|
::octave(expanded_, os);
|
2001-11-16 08:26:41 +00:00
|
|
|
|
}
|
2001-12-11 11:33:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void MathMacro::updateExpansion() const
|
|
|
|
|
{
|
2004-04-13 06:27:29 +00:00
|
|
|
|
//expanded_.substitute(*this);
|
2001-12-11 11:33:43 +00:00
|
|
|
|
}
|
2003-01-07 11:24:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void MathMacro::infoize(std::ostream & os) const
|
|
|
|
|
{
|
|
|
|
|
os << "Macro: " << name();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void MathMacro::infoize2(std::ostream & os) const
|
|
|
|
|
{
|
|
|
|
|
os << "Macro: " << name();
|
2004-04-13 06:27:29 +00:00
|
|
|
|
|
2003-01-07 11:24:43 +00:00
|
|
|
|
}
|