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-06-25 00:06:33 +00:00
|
|
|
|
#include "math_macrotable.h"
|
2001-02-13 17:08:51 +00:00
|
|
|
|
#include "math_macrotemplate.h"
|
2001-11-08 12:06:56 +00:00
|
|
|
|
#include "math_mathmlstream.h"
|
2004-01-20 14:25:24 +00:00
|
|
|
|
#include "cursor.h"
|
2001-11-16 08:26:41 +00:00
|
|
|
|
#include "debug.h"
|
2001-07-13 14:54:56 +00:00
|
|
|
|
#include "LaTeXFeatures.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
|
|
|
|
|
|
|
|
|
|
2001-10-12 12:02:49 +00:00
|
|
|
|
MathMacro::MathMacro(string const & name)
|
|
|
|
|
: MathNestInset(MathMacroTable::provide(name)->asMacroTemplate()->numargs()),
|
|
|
|
|
tmplate_(MathMacroTable::provide(name))
|
2001-06-25 00:06:33 +00:00
|
|
|
|
{}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
1999-12-07 00:44:53 +00:00
|
|
|
|
|
2001-10-12 12:02:49 +00:00
|
|
|
|
MathMacro::MathMacro(MathMacro const & m)
|
|
|
|
|
: MathNestInset(m),
|
|
|
|
|
tmplate_(m.tmplate_) // don't copy 'expanded_'!
|
2001-07-12 11:55:57 +00:00
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-07-25 17:11:25 +00:00
|
|
|
|
auto_ptr<InsetBase> MathMacro::clone() 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
|
|
|
|
{
|
2002-04-03 10:45:32 +00:00
|
|
|
|
return tmplate_->asMacroTemplate()->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
|
|
|
|
|
2001-10-12 16:12:32 +00:00
|
|
|
|
bool MathMacro::defining() const
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
//return mathcursor && mathcursor->formula()->getInsetName() == name();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2002-03-25 12:11:25 +00:00
|
|
|
|
void MathMacro::expand() const
|
|
|
|
|
{
|
2002-08-02 14:29:42 +00:00
|
|
|
|
expanded_ = tmplate_->cell(tmplate_->cell(1).empty() ? 0 : 1);
|
2002-03-25 12:11:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-06-02 10:03:27 +00:00
|
|
|
|
void MathMacro::metrics(MetricsInfo & mi, Dimension & dim) const
|
2001-04-24 16:13:38 +00:00
|
|
|
|
{
|
2002-05-30 07:09:54 +00:00
|
|
|
|
augmentFont(font_, "lyxtex");
|
2001-10-22 15:37:49 +00:00
|
|
|
|
mi_ = mi;
|
|
|
|
|
|
2001-10-12 16:12:32 +00:00
|
|
|
|
if (defining()) {
|
2003-05-28 13:22:36 +00:00
|
|
|
|
|
2004-01-30 11:41:12 +00:00
|
|
|
|
mathed_string_dim(font_, name(), dim);
|
2001-10-12 16:12:32 +00:00
|
|
|
|
|
2004-01-26 10:13:15 +00:00
|
|
|
|
} else if (editing(mi.base.bv)) {
|
2003-05-28 13:22:36 +00:00
|
|
|
|
|
2002-03-25 12:11:25 +00:00
|
|
|
|
expand();
|
2004-01-30 11:41:12 +00:00
|
|
|
|
expanded_.metrics(mi_, dim);
|
|
|
|
|
metricsMarkers(dim);
|
2001-04-24 16:13:38 +00:00
|
|
|
|
|
2004-01-30 11:41:12 +00:00
|
|
|
|
dim.wid += mathed_string_width(font_, name()) + 10;
|
2001-04-24 16:13:38 +00:00
|
|
|
|
|
2003-05-28 13:22:36 +00:00
|
|
|
|
int ww = mathed_string_width(font_, "#1: ");
|
2001-07-06 12:09:32 +00:00
|
|
|
|
|
2001-09-26 16:52:34 +00:00
|
|
|
|
for (idx_type i = 0; i < nargs(); ++i) {
|
2002-08-02 14:29:42 +00:00
|
|
|
|
MathArray const & c = cell(i);
|
2001-10-22 15:37:49 +00:00
|
|
|
|
c.metrics(mi_);
|
2004-01-30 11:41:12 +00:00
|
|
|
|
dim.wid = max(dim.wid, c.width() + ww);
|
|
|
|
|
dim.des += max(c.ascent(), dim.asc) + 5;
|
|
|
|
|
dim.des += max(c.descent(), dim.des) + 5;
|
2001-04-24 16:13:38 +00:00
|
|
|
|
}
|
2003-05-28 13:22:36 +00:00
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
expand();
|
|
|
|
|
expanded_.substitute(*this);
|
2004-01-30 11:41:12 +00:00
|
|
|
|
expanded_.metrics(mi_, dim);
|
2003-05-28 13:22:36 +00:00
|
|
|
|
|
2002-03-21 17:42:56 +00:00
|
|
|
|
}
|
2001-10-12 16:12:32 +00:00
|
|
|
|
|
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
|
|
|
|
{
|
2003-06-02 10:03:27 +00:00
|
|
|
|
metrics(mi_, dim_);
|
2001-07-12 11:55:57 +00:00
|
|
|
|
|
2002-03-19 16:55:58 +00:00
|
|
|
|
LyXFont texfont;
|
2002-05-30 07:09:54 +00:00
|
|
|
|
augmentFont(texfont, "lyxtex");
|
2002-03-19 16:55:58 +00:00
|
|
|
|
|
2001-10-12 16:12:32 +00:00
|
|
|
|
if (defining()) {
|
2002-06-24 15:37:14 +00:00
|
|
|
|
drawStr(pi, texfont, x, y, name());
|
2001-10-12 16:12:32 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2001-06-25 00:06:33 +00:00
|
|
|
|
|
2004-01-26 10:13:15 +00:00
|
|
|
|
if (editing(pi.base.bv)) {
|
2003-05-28 13:22:36 +00:00
|
|
|
|
int h = y - dim_.ascent() + 2 + expanded_.ascent();
|
2002-06-24 15:37:14 +00:00
|
|
|
|
drawStr(pi, font_, x + 3, h, name());
|
2001-06-25 00:06:33 +00:00
|
|
|
|
|
2002-03-19 16:55:58 +00:00
|
|
|
|
int const w = mathed_string_width(font_, name());
|
2002-06-24 15:37:14 +00:00
|
|
|
|
expanded_.draw(pi, x + w + 12, h);
|
2001-06-25 00:06:33 +00:00
|
|
|
|
h += expanded_.descent();
|
|
|
|
|
|
2002-07-11 11:27:24 +00:00
|
|
|
|
Dimension ldim;
|
|
|
|
|
mathed_string_dim(font_, "#1: ", ldim);
|
2001-07-06 12:09:32 +00:00
|
|
|
|
|
2001-09-26 16:52:34 +00:00
|
|
|
|
for (idx_type i = 0; i < nargs(); ++i) {
|
2002-08-02 14:29:42 +00:00
|
|
|
|
MathArray const & c = cell(i);
|
2003-05-27 13:55:03 +00:00
|
|
|
|
h += max(c.ascent(), ldim.asc) + 5;
|
|
|
|
|
c.draw(pi, x + ldim.wid, h);
|
2001-04-24 16:13:38 +00:00
|
|
|
|
char str[] = "#1:";
|
2001-10-01 12:23:00 +00:00
|
|
|
|
str[1] += static_cast<char>(i);
|
2002-06-24 15:37:14 +00:00
|
|
|
|
drawStr(pi, texfont, x + 3, h, str);
|
2003-05-27 13:55:03 +00:00
|
|
|
|
h += max(c.descent(), ldim.des) + 5;
|
2001-04-24 16:13:38 +00:00
|
|
|
|
}
|
2001-10-12 16:12:32 +00:00
|
|
|
|
return;
|
2001-04-24 16:13:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-06-24 15:37:14 +00:00
|
|
|
|
expanded_.draw(pi, x, y);
|
2001-04-24 16:13:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2001-09-26 15:20:45 +00:00
|
|
|
|
void MathMacro::dump() const
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
|
MathMacroTable::dump();
|
2003-08-02 11:30:30 +00:00
|
|
|
|
lyxerr << "\n macro: '" << this << "'\n"
|
|
|
|
|
<< " name: '" << name() << "'\n"
|
|
|
|
|
<< " template: '";
|
2001-11-09 08:35:57 +00:00
|
|
|
|
WriteStream wi(lyxerr);
|
2001-10-19 11:25:48 +00:00
|
|
|
|
tmplate_->write(wi);
|
2003-08-02 11:30:30 +00:00
|
|
|
|
lyxerr << "'" << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-10-12 12:02:49 +00:00
|
|
|
|
|
2004-01-26 10:13:15 +00:00
|
|
|
|
bool MathMacro::idxUpDown(LCursor & cur, bool up) const
|
2001-04-24 16:13:38 +00:00
|
|
|
|
{
|
2002-07-30 13:56:02 +00:00
|
|
|
|
if (up) {
|
2004-01-16 12:36:23 +00:00
|
|
|
|
if (!MathNestInset::idxLeft(cur))
|
2002-07-30 13:56:02 +00:00
|
|
|
|
return false;
|
|
|
|
|
} else {
|
2004-01-16 12:36:23 +00:00
|
|
|
|
if (!MathNestInset::idxRight(cur))
|
2002-07-30 13:56:02 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2004-01-26 10:13:15 +00:00
|
|
|
|
cur.pos() = cur.cell().x2pos(cur.x_target());
|
2004-01-15 17:34:44 +00:00
|
|
|
|
return true;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-01-16 12:36:23 +00:00
|
|
|
|
bool MathMacro::idxLeft(LCursor &) const
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
|
return false;
|
2001-04-24 16:13:38 +00:00
|
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2001-04-25 15:43:57 +00:00
|
|
|
|
|
2004-01-16 12:36:23 +00:00
|
|
|
|
bool MathMacro::idxRight(LCursor &) const
|
2001-04-25 15:43:57 +00:00
|
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
|
return false;
|
2001-04-25 15:43:57 +00:00
|
|
|
|
}
|
2001-07-13 14:54:56 +00:00
|
|
|
|
|
|
|
|
|
|
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-08-06 17:20:26 +00:00
|
|
|
|
//MathInset::validate(features);
|
2001-07-13 14:54:56 +00:00
|
|
|
|
}
|
2001-11-16 08:26:41 +00:00
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
{
|
2002-03-25 12:11:25 +00:00
|
|
|
|
expand();
|
2002-08-02 14:29:42 +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();
|
|
|
|
|
}
|