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"
|
2004-04-13 06:27:29 +00:00
|
|
|
|
#include "math_support.h"
|
|
|
|
|
|
|
|
|
|
#include "cursor.h"
|
2001-06-25 00:06:33 +00:00
|
|
|
|
#include "debug.h"
|
2004-04-13 06:27:29 +00:00
|
|
|
|
#include "gettext.h"
|
|
|
|
|
#include "lyxlex.h"
|
2003-09-16 09:01:15 +00:00
|
|
|
|
#include "LColor.h"
|
2001-02-14 15:00:50 +00:00
|
|
|
|
|
2004-04-13 06:27:29 +00:00
|
|
|
|
#include "frontends/Painter.h"
|
|
|
|
|
#include "frontends/font_metrics.h"
|
|
|
|
|
|
|
|
|
|
#include "support/lstrings.h"
|
|
|
|
|
|
2006-08-13 22:54:59 +00:00
|
|
|
|
using lyx::docstring;
|
2004-04-13 06:27:29 +00:00
|
|
|
|
using lyx::support::bformat;
|
|
|
|
|
|
2003-10-06 15:43:21 +00:00
|
|
|
|
using std::string;
|
2003-07-25 17:11:25 +00:00
|
|
|
|
using std::auto_ptr;
|
2004-04-13 06:27:29 +00:00
|
|
|
|
using std::ostream;
|
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")
|
2006-01-24 11:04:30 +00:00
|
|
|
|
{
|
|
|
|
|
initMath();
|
|
|
|
|
}
|
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
|
|
|
|
{
|
2006-01-24 11:04:30 +00:00
|
|
|
|
initMath();
|
|
|
|
|
|
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_()
|
|
|
|
|
{
|
2006-01-24 11:04:30 +00:00
|
|
|
|
initMath();
|
|
|
|
|
|
2002-07-12 14:24:47 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
2004-11-23 23:04:52 +00:00
|
|
|
|
auto_ptr<InsetBase> MathMacroTemplate::doClone() const
|
2001-02-26 12:53:35 +00:00
|
|
|
|
{
|
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
|
|
|
|
|
2004-04-13 13:54:58 +00:00
|
|
|
|
void MathMacroTemplate::edit(LCursor & cur, bool)
|
2004-04-13 06:27:29 +00:00
|
|
|
|
{
|
|
|
|
|
lyxerr << "MathMacroTemplate: edit left/right" << endl;
|
|
|
|
|
cur.push(*this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
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_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-04-13 06:27:29 +00:00
|
|
|
|
string MathMacroTemplate::prefix() const
|
|
|
|
|
{
|
|
|
|
|
return bformat(_(" Macro: %1$s: "), 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);
|
2006-08-13 22:54:59 +00:00
|
|
|
|
docstring dp(prefix().begin(), prefix().end());
|
2004-04-13 06:27:29 +00:00
|
|
|
|
dim.wid = cell(0).width() + cell(1).width() + 20
|
2006-08-13 22:54:59 +00:00
|
|
|
|
+ font_metrics::width(dp, mi.base.font);
|
2004-04-13 06:27:29 +00:00
|
|
|
|
dim.asc = std::max(cell(0).ascent(), cell(1).ascent()) + 7;
|
|
|
|
|
dim.des = std::max(cell(0).descent(), cell(1).descent()) + 7;
|
2004-03-27 12:46:30 +00:00
|
|
|
|
dim_ = dim;
|
2001-03-05 10:52:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-04-13 06:27:29 +00:00
|
|
|
|
void MathMacroTemplate::draw(PainterInfo & p, int x, int y) const
|
2001-02-13 17:08:51 +00:00
|
|
|
|
{
|
2004-04-13 06:27:29 +00:00
|
|
|
|
setPosCache(p, x, y);
|
|
|
|
|
|
|
|
|
|
// label
|
|
|
|
|
LyXFont font = p.base.font;
|
|
|
|
|
font.setColor(LColor::math);
|
|
|
|
|
|
|
|
|
|
PainterInfo pi(p.base.bv, p.pain);
|
|
|
|
|
pi.base.style = LM_ST_TEXT;
|
|
|
|
|
pi.base.font = font;
|
|
|
|
|
|
|
|
|
|
int const a = y - dim_.asc + 1;
|
|
|
|
|
int const w = dim_.wid - 2;
|
|
|
|
|
int const h = dim_.height() - 2;
|
|
|
|
|
|
|
|
|
|
// LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
|
|
|
|
|
// the next line would overwrite the selection!
|
|
|
|
|
//pi.pain.fillRectangle(x, a, w, h, LColor::mathmacrobg);
|
|
|
|
|
pi.pain.rectangle(x, a, w, h, LColor::mathframe);
|
|
|
|
|
|
|
|
|
|
#ifdef WITH_WARNINGS
|
|
|
|
|
#warning FIXME
|
|
|
|
|
#endif
|
|
|
|
|
#if 0
|
|
|
|
|
LCursor & cur = p.base.bv->cursor();
|
|
|
|
|
if (cur.isInside(this))
|
|
|
|
|
cur.drawSelection(pi);
|
|
|
|
|
#endif
|
2006-08-13 22:54:59 +00:00
|
|
|
|
docstring dp(prefix().begin(), prefix().end());
|
|
|
|
|
pi.pain.text(x + 2, y, dp, font);
|
|
|
|
|
x += font_metrics::width(dp, pi.base.font) + 6;
|
2004-04-13 06:27:29 +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);
|
2004-04-13 06:27:29 +00:00
|
|
|
|
pi.pain.rectangle(x, y - dim_.ascent() + 3,
|
|
|
|
|
w0 + 4, dim_.height() - 6, LColor::mathline);
|
2002-08-02 14:29:42 +00:00
|
|
|
|
cell(1).draw(pi, x + 8 + w0, y + 1);
|
2004-04-13 06:27:29 +00:00
|
|
|
|
pi.pain.rectangle(x + w0 + 6, y - dim_.ascent() + 3,
|
|
|
|
|
w1 + 4, dim_.height() - 6, LColor::mathline);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void MathMacroTemplate::read(Buffer const &, LyXLex & lex)
|
|
|
|
|
{
|
|
|
|
|
MathArray ar;
|
|
|
|
|
mathed_parse_cell(ar, lex.getStream());
|
|
|
|
|
if (ar.size() != 1 || !ar[0]->asMacroTemplate()) {
|
|
|
|
|
lyxerr << "cannot read macro from '" << ar << "'" << endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
operator=( *(ar[0]->asMacroTemplate()) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void MathMacroTemplate::write(Buffer const &, std::ostream & os) const
|
|
|
|
|
{
|
|
|
|
|
WriteStream wi(os, false, false);
|
2004-08-16 11:27:51 +00:00
|
|
|
|
os << "FormulaMacro\n";
|
2004-04-13 06:27:29 +00:00
|
|
|
|
write(wi);
|
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") {
|
2004-08-16 11:27:51 +00:00
|
|
|
|
os << "\\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
|
2004-08-16 11:27:51 +00:00
|
|
|
|
os << "\\" << 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
|
|
|
|
}
|
2004-04-13 06:27:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MacroData MathMacroTemplate::asMacroData() const
|
|
|
|
|
{
|
|
|
|
|
return MacroData(asString(cell(0)), numargs(), asString(cell(1)));
|
|
|
|
|
}
|