2003-08-19 13:00:56 +00:00
|
|
|
|
/**
|
|
|
|
|
* \file math_macrotemplate.C
|
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
|
*
|
|
|
|
|
* \author Andr<EFBFBD> P<EFBFBD>nitz
|
|
|
|
|
*
|
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
|
*/
|
|
|
|
|
|
2003-08-02 11:30:30 +00:00
|
|
|
|
#include <config.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"
|
2002-07-12 14:24:47 +00:00
|
|
|
|
#include "math_parser.h"
|
2002-05-23 09:21:32 +00:00
|
|
|
|
#include "frontends/Painter.h"
|
2001-06-25 00:06:33 +00:00
|
|
|
|
#include "debug.h"
|
2003-09-16 09:01:15 +00:00
|
|
|
|
#include "LColor.h"
|
2001-02-14 15:00:50 +00:00
|
|
|
|
|
2003-10-06 15:43:21 +00:00
|
|
|
|
|
|
|
|
|
using std::string;
|
2003-07-25 17:11:25 +00:00
|
|
|
|
using std::auto_ptr;
|
2003-08-02 11:30:30 +00:00
|
|
|
|
using std::endl;
|
2003-07-25 17:11:25 +00:00
|
|
|
|
|
2001-02-13 17:08:51 +00:00
|
|
|
|
|
2001-08-03 09:54:48 +00:00
|
|
|
|
MathMacroTemplate::MathMacroTemplate()
|
2003-07-04 15:55:18 +00:00
|
|
|
|
: MathNestInset(2), numargs_(0), name_(), type_("newcommand")
|
2001-04-24 16:13:38 +00:00
|
|
|
|
{}
|
2001-02-13 17:08:51 +00:00
|
|
|
|
|
2001-04-25 15:43:57 +00:00
|
|
|
|
|
2002-07-12 14:24:47 +00:00
|
|
|
|
MathMacroTemplate::MathMacroTemplate(string const & nm, int numargs,
|
2003-07-04 15:55:18 +00:00
|
|
|
|
string const & type, MathArray const & ar1, MathArray const & ar2)
|
|
|
|
|
: MathNestInset(2), numargs_(numargs), name_(nm), type_(type)
|
2001-10-02 10:50:10 +00:00
|
|
|
|
{
|
2001-12-11 11:33:43 +00:00
|
|
|
|
if (numargs_ > 9)
|
2001-10-02 10:50:10 +00:00
|
|
|
|
lyxerr << "MathMacroTemplate::MathMacroTemplate: wrong # of arguments: "
|
2002-03-25 12:11:25 +00:00
|
|
|
|
<< numargs_ << std::endl;
|
2002-07-12 14:24:47 +00:00
|
|
|
|
cell(0) = ar1;
|
|
|
|
|
cell(1) = ar2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MathMacroTemplate::MathMacroTemplate(std::istream & is)
|
|
|
|
|
: MathNestInset(2), numargs_(0), name_()
|
|
|
|
|
{
|
|
|
|
|
MathArray ar;
|
|
|
|
|
mathed_parse_cell(ar, is);
|
|
|
|
|
if (ar.size() != 1 || !ar[0]->asMacroTemplate()) {
|
2003-08-02 11:30:30 +00:00
|
|
|
|
lyxerr << "cannot read macro from '" << ar << "'" << endl;
|
2002-07-12 14:24:47 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
operator=( *(ar[0]->asMacroTemplate()) );
|
2001-10-02 10:50:10 +00:00
|
|
|
|
}
|
2001-02-26 12:53:35 +00:00
|
|
|
|
|
|
|
|
|
|
2003-07-25 17:11:25 +00:00
|
|
|
|
auto_ptr<InsetBase> MathMacroTemplate::clone() const
|
2001-02-26 12:53:35 +00:00
|
|
|
|
{
|
2003-08-02 11:30:30 +00:00
|
|
|
|
//lyxerr << "cloning MacroTemplate!" << endl;
|
2003-07-25 17:11:25 +00:00
|
|
|
|
return auto_ptr<InsetBase>(new MathMacroTemplate(*this));
|
2001-02-26 12:53:35 +00:00
|
|
|
|
}
|
2001-02-13 17:08:51 +00:00
|
|
|
|
|
2001-06-27 15:33:55 +00:00
|
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
|
int MathMacroTemplate::numargs() const
|
|
|
|
|
{
|
|
|
|
|
return numargs_;
|
|
|
|
|
}
|
2001-02-13 17:08:51 +00:00
|
|
|
|
|
2001-06-27 15:33:55 +00:00
|
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
|
void MathMacroTemplate::numargs(int numargs)
|
2001-02-13 17:08:51 +00:00
|
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
|
numargs_ = numargs;
|
|
|
|
|
}
|
2001-02-13 17:08:51 +00:00
|
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
|
|
2002-08-01 15:53:46 +00:00
|
|
|
|
string MathMacroTemplate::name() const
|
2001-08-08 17:26:30 +00:00
|
|
|
|
{
|
|
|
|
|
return name_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-06-02 10:03:27 +00:00
|
|
|
|
void MathMacroTemplate::metrics(MetricsInfo & mi, Dimension & dim) const
|
2001-03-05 10:52:39 +00:00
|
|
|
|
{
|
2002-08-02 14:29:42 +00:00
|
|
|
|
cell(0).metrics(mi);
|
|
|
|
|
cell(1).metrics(mi);
|
2004-03-27 12:46:30 +00:00
|
|
|
|
dim.wid = cell(0).width() + cell(1).width() + 10;
|
|
|
|
|
dim.asc = std::max(cell(0).ascent(), cell(1).ascent()) + 2;
|
|
|
|
|
dim.des = std::max(cell(0).descent(), cell(1).descent()) + 2;
|
|
|
|
|
dim_ = dim;
|
2001-03-05 10:52:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-03-21 14:20:48 +00:00
|
|
|
|
void MathMacroTemplate::draw(PainterInfo & pi, int x, int y) const
|
2001-02-13 17:08:51 +00:00
|
|
|
|
{
|
2002-08-02 14:29:42 +00:00
|
|
|
|
int const w0 = cell(0).width();
|
|
|
|
|
int const w1 = cell(1).width();
|
|
|
|
|
cell(0).draw(pi, x + 2, y + 1);
|
2003-05-28 13:22:36 +00:00
|
|
|
|
pi.pain.rectangle(x, y - dim_.ascent() + 1, w0 + 4, dim_.height(),
|
2003-04-14 16:00:42 +00:00
|
|
|
|
LColor::mathline);
|
2002-08-02 14:29:42 +00:00
|
|
|
|
cell(1).draw(pi, x + 8 + w0, y + 1);
|
2003-05-28 13:22:36 +00:00
|
|
|
|
pi.pain.rectangle(x + w0 + 6 , y - dim_.ascent() + 1, w1 + 4,
|
|
|
|
|
dim_.height(), LColor::mathline);
|
2001-02-13 17:08:51 +00:00
|
|
|
|
}
|
2001-11-08 12:06:56 +00:00
|
|
|
|
|
|
|
|
|
|
2001-11-09 08:35:57 +00:00
|
|
|
|
void MathMacroTemplate::write(WriteStream & os) const
|
2001-11-08 12:06:56 +00:00
|
|
|
|
{
|
2003-07-04 15:55:18 +00:00
|
|
|
|
if (type_ == "def") {
|
|
|
|
|
os << "\n\\def\\" << name_.c_str();
|
2003-07-25 17:11:25 +00:00
|
|
|
|
for (int i = 1; i <= numargs_; ++i)
|
2003-07-04 15:55:18 +00:00
|
|
|
|
os << '#' << i;
|
2002-03-25 12:11:25 +00:00
|
|
|
|
} else {
|
2003-07-04 15:55:18 +00:00
|
|
|
|
// newcommand or renewcommand
|
|
|
|
|
os << "\n\\" << type_.c_str() << "{\\" << name_.c_str() << '}';
|
2002-03-25 12:11:25 +00:00
|
|
|
|
if (numargs_ > 0)
|
|
|
|
|
os << '[' << numargs_ << ']';
|
2003-07-04 15:55:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
os << '{' << cell(0) << "}";
|
|
|
|
|
|
|
|
|
|
if (os.latex()) {
|
|
|
|
|
// writing .tex. done.
|
|
|
|
|
os << "\n";
|
|
|
|
|
} else {
|
|
|
|
|
// writing .lyx, write special .tex export only if necessary
|
2002-03-25 12:11:25 +00:00
|
|
|
|
if (!cell(1).empty())
|
|
|
|
|
os << "\n{" << cell(1) << '}';
|
|
|
|
|
}
|
2001-11-08 12:06:56 +00:00
|
|
|
|
}
|