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>
|
|
|
|
|
|
2006-09-17 09:14:18 +00:00
|
|
|
|
#include "MathMacroTemplate.h"
|
2006-10-22 10:15:23 +00:00
|
|
|
|
#include "MathStream.h"
|
2006-09-17 09:14:18 +00:00
|
|
|
|
#include "MathParser.h"
|
|
|
|
|
#include "MathSupport.h"
|
2004-04-13 06:27:29 +00:00
|
|
|
|
|
2007-05-01 08:26:40 +00:00
|
|
|
|
#include "Buffer.h"
|
2007-04-26 14:56:30 +00:00
|
|
|
|
#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"
|
2007-04-26 11:30:54 +00:00
|
|
|
|
#include "Lexer.h"
|
2001-02-14 15:00:50 +00:00
|
|
|
|
|
2006-10-07 16:15:06 +00:00
|
|
|
|
#include "frontends/FontMetrics.h"
|
2004-04-13 06:27:29 +00:00
|
|
|
|
#include "frontends/Painter.h"
|
|
|
|
|
|
|
|
|
|
#include "support/lstrings.h"
|
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
|
|
|
|
|
|
using support::bformat;
|
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()
|
2006-10-22 10:15:23 +00:00
|
|
|
|
: InsetMathNest(2), numargs_(0), name_(), type_(from_ascii("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
|
|
|
|
|
2006-10-22 10:15:23 +00:00
|
|
|
|
MathMacroTemplate::MathMacroTemplate(docstring const & name, int numargs,
|
2007-04-26 16:05:57 +00:00
|
|
|
|
docstring const & type, MathData const & ar1, MathData const & ar2)
|
2006-10-22 10:15:23 +00:00
|
|
|
|
: InsetMathNest(2), numargs_(numargs), name_(name), 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-10-22 14:37:32 +00:00
|
|
|
|
MathMacroTemplate::MathMacroTemplate(docstring const & str)
|
2006-09-16 18:11:38 +00:00
|
|
|
|
: InsetMathNest(2), numargs_(0), name_()
|
2002-07-12 14:24:47 +00:00
|
|
|
|
{
|
2006-01-24 11:04:30 +00:00
|
|
|
|
initMath();
|
|
|
|
|
|
2007-04-26 16:05:57 +00:00
|
|
|
|
MathData ar;
|
2006-10-22 14:37:32 +00:00
|
|
|
|
mathed_parse_cell(ar, str);
|
2002-07-12 14:24:47 +00:00
|
|
|
|
if (ar.size() != 1 || !ar[0]->asMacroTemplate()) {
|
2006-10-22 14:37:32 +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
|
|
|
|
|
|
|
|
|
|
2007-08-30 18:03:17 +00:00
|
|
|
|
Inset * MathMacroTemplate::clone() const
|
2001-02-26 12:53:35 +00:00
|
|
|
|
{
|
2007-08-30 18:03:17 +00:00
|
|
|
|
return 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
|
|
|
|
|
2007-04-26 14:56:30 +00:00
|
|
|
|
void MathMacroTemplate::edit(Cursor & cur, bool)
|
2004-04-13 06:27:29 +00:00
|
|
|
|
{
|
2006-10-22 12:07:01 +00:00
|
|
|
|
//lyxerr << "MathMacroTemplate: edit left/right" << endl;
|
2004-04-13 06:27:29 +00:00
|
|
|
|
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
|
|
|
|
|
2006-10-22 10:15:23 +00:00
|
|
|
|
docstring MathMacroTemplate::name() const
|
2001-08-08 17:26:30 +00:00
|
|
|
|
{
|
|
|
|
|
return name_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-09-03 11:54:17 +00:00
|
|
|
|
docstring MathMacroTemplate::prefix() const
|
2004-04-13 06:27:29 +00:00
|
|
|
|
{
|
2007-04-17 16:49:17 +00:00
|
|
|
|
return bformat(_(" Macro: %1$s: "), name_);
|
2004-04-13 06:27:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-09-21 20:39:47 +00:00
|
|
|
|
void MathMacroTemplate::metrics(MetricsInfo & mi, Dimension & dim) const
|
2001-03-05 10:52:39 +00:00
|
|
|
|
{
|
2007-04-17 16:52:43 +00:00
|
|
|
|
bool lockMacro = MacroTable::globalMacros().has(name_);
|
|
|
|
|
if (lockMacro)
|
|
|
|
|
MacroTable::globalMacros().get(name_).lock();
|
|
|
|
|
|
2007-09-24 13:52:04 +00:00
|
|
|
|
Dimension dim0;
|
|
|
|
|
cell(0).metrics(mi, dim0);
|
|
|
|
|
Dimension dim1;
|
|
|
|
|
cell(1).metrics(mi, dim1);
|
2006-09-03 11:54:17 +00:00
|
|
|
|
docstring dp = prefix();
|
2007-09-24 13:52:04 +00:00
|
|
|
|
dim.wid = dim0.width() + dim1.width() + 20
|
2006-10-11 17:24:46 +00:00
|
|
|
|
+ theFontMetrics(mi.base.font).width(dp);
|
2007-09-24 13:52:04 +00:00
|
|
|
|
dim.asc = std::max(dim0.ascent(), dim1.ascent()) + 7;
|
|
|
|
|
dim.des = std::max(dim0.descent(), dim1.descent()) + 7;
|
2007-05-28 22:27:45 +00:00
|
|
|
|
|
2007-04-17 16:52:43 +00:00
|
|
|
|
if (lockMacro)
|
|
|
|
|
MacroTable::globalMacros().get(name_).unlock();
|
2007-05-28 22:27:45 +00:00
|
|
|
|
|
2007-09-23 22:39:49 +00:00
|
|
|
|
// Cache the inset dimension.
|
|
|
|
|
setDimCache(mi, 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
|
|
|
|
{
|
2007-04-17 16:52:43 +00:00
|
|
|
|
bool lockMacro = MacroTable::globalMacros().has(name_);
|
|
|
|
|
if (lockMacro)
|
|
|
|
|
MacroTable::globalMacros().get(name_).lock();
|
2007-05-28 22:27:45 +00:00
|
|
|
|
|
2004-04-13 06:27:29 +00:00
|
|
|
|
setPosCache(p, x, y);
|
|
|
|
|
|
2007-09-23 22:39:49 +00:00
|
|
|
|
Dimension const dim = dimension(*p.base.bv);
|
|
|
|
|
|
2004-04-13 06:27:29 +00:00
|
|
|
|
// label
|
2007-10-28 18:51:54 +00:00
|
|
|
|
FontInfo font = p.base.font;
|
2007-10-25 12:41:02 +00:00
|
|
|
|
font.setColor(Color_math);
|
2004-04-13 06:27:29 +00:00
|
|
|
|
|
|
|
|
|
PainterInfo pi(p.base.bv, p.pain);
|
|
|
|
|
pi.base.style = LM_ST_TEXT;
|
|
|
|
|
pi.base.font = font;
|
|
|
|
|
|
2007-09-23 22:39:49 +00:00
|
|
|
|
int const a = y - dim.asc + 1;
|
|
|
|
|
int const w = dim.wid - 2;
|
|
|
|
|
int const h = dim.height() - 2;
|
2004-04-13 06:27:29 +00:00
|
|
|
|
|
2007-10-25 12:41:02 +00:00
|
|
|
|
// Color_mathbg used to be "AntiqueWhite" but is "linen" now, too
|
2004-04-13 06:27:29 +00:00
|
|
|
|
// the next line would overwrite the selection!
|
2007-10-25 12:41:02 +00:00
|
|
|
|
//pi.pain.fillRectangle(x, a, w, h, Color_mathmacrobg);
|
|
|
|
|
pi.pain.rectangle(x, a, w, h, Color_mathframe);
|
2004-04-13 06:27:29 +00:00
|
|
|
|
|
2007-08-10 11:47:12 +00:00
|
|
|
|
// FIXME:
|
2004-04-13 06:27:29 +00:00
|
|
|
|
#if 0
|
2007-04-26 14:56:30 +00:00
|
|
|
|
Cursor & cur = p.base.bv->cursor();
|
2004-04-13 06:27:29 +00:00
|
|
|
|
if (cur.isInside(this))
|
|
|
|
|
cur.drawSelection(pi);
|
|
|
|
|
#endif
|
2006-09-03 11:54:17 +00:00
|
|
|
|
docstring dp = prefix();
|
2006-08-13 22:54:59 +00:00
|
|
|
|
pi.pain.text(x + 2, y, dp, font);
|
2006-10-07 16:15:06 +00:00
|
|
|
|
// FIXME: Painter text should retain the drawn text width
|
2006-10-11 17:24:46 +00:00
|
|
|
|
x += theFontMetrics(font).width(dp) + 6;
|
2004-04-13 06:27:29 +00:00
|
|
|
|
|
2007-09-24 13:52:04 +00:00
|
|
|
|
int const w0 = cell(0).dimension(*pi.base.bv).width();
|
|
|
|
|
int const w1 = cell(1).dimension(*pi.base.bv).width();
|
2002-08-02 14:29:42 +00:00
|
|
|
|
cell(0).draw(pi, x + 2, y + 1);
|
2007-09-23 22:39:49 +00:00
|
|
|
|
pi.pain.rectangle(x, y - dim.ascent() + 3,
|
2007-10-25 12:41:02 +00:00
|
|
|
|
w0 + 4, dim.height() - 6, Color_mathline);
|
2002-08-02 14:29:42 +00:00
|
|
|
|
cell(1).draw(pi, x + 8 + w0, y + 1);
|
2007-09-23 22:39:49 +00:00
|
|
|
|
pi.pain.rectangle(x + w0 + 6, y - dim.ascent() + 3,
|
2007-10-25 12:41:02 +00:00
|
|
|
|
w1 + 4, dim.height() - 6, Color_mathline);
|
2007-05-28 22:27:45 +00:00
|
|
|
|
|
2007-04-17 16:52:43 +00:00
|
|
|
|
if (lockMacro)
|
|
|
|
|
MacroTable::globalMacros().get(name_).unlock();
|
2004-04-13 06:27:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-04-26 11:30:54 +00:00
|
|
|
|
void MathMacroTemplate::read(Buffer const &, Lexer & lex)
|
2004-04-13 06:27:29 +00:00
|
|
|
|
{
|
2007-04-26 16:05:57 +00:00
|
|
|
|
MathData ar;
|
2006-12-19 14:24:25 +00:00
|
|
|
|
mathed_parse_cell(ar, lex.getStream());
|
2004-04-13 06:27:29 +00:00
|
|
|
|
if (ar.size() != 1 || !ar[0]->asMacroTemplate()) {
|
2006-10-22 14:37:32 +00:00
|
|
|
|
lyxerr << "Cannot read macro from '" << ar << "'" << endl;
|
|
|
|
|
lyxerr << "Read: " << to_utf8(asString(ar)) << endl;
|
2004-04-13 06:27:29 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
operator=( *(ar[0]->asMacroTemplate()) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void MathMacroTemplate::write(Buffer const &, std::ostream & os) const
|
|
|
|
|
{
|
2006-10-21 00:16:43 +00:00
|
|
|
|
odocstringstream oss;
|
2006-10-19 16:51:30 +00:00
|
|
|
|
WriteStream wi(oss, false, false);
|
|
|
|
|
oss << "FormulaMacro\n";
|
2004-04-13 06:27:29 +00:00
|
|
|
|
write(wi);
|
2006-10-21 00:16:43 +00:00
|
|
|
|
os << to_utf8(oss.str());
|
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
|
|
|
|
|
|
|
|
|
|
2007-05-01 08:26:40 +00:00
|
|
|
|
int MathMacroTemplate::plaintext(Buffer const & buf, odocstream & os,
|
2007-05-28 22:27:45 +00:00
|
|
|
|
OutputParams const &) const
|
2007-02-25 09:27:11 +00:00
|
|
|
|
{
|
2007-05-01 08:26:40 +00:00
|
|
|
|
static docstring const str = '[' + buf.B_("math macro") + ']';
|
2007-02-25 09:27:11 +00:00
|
|
|
|
|
|
|
|
|
os << str;
|
|
|
|
|
return str.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-04-13 06:27:29 +00:00
|
|
|
|
MacroData MathMacroTemplate::asMacroData() const
|
|
|
|
|
{
|
2006-11-13 17:35:18 +00:00
|
|
|
|
return MacroData(asString(cell(0)), numargs(), asString(cell(1)), std::string());
|
2004-04-13 06:27:29 +00:00
|
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|