2003-08-19 13:00:56 +00:00
|
|
|
|
/**
|
2007-04-25 16:11:45 +00:00
|
|
|
|
* \file MathMacro.cpp
|
2003-08-19 13:00:56 +00:00
|
|
|
|
* 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
|
|
|
|
|
2007-04-25 16:11:45 +00:00
|
|
|
|
#include "MathMacro.h"
|
2006-09-17 09:14:18 +00:00
|
|
|
|
#include "MathSupport.h"
|
|
|
|
|
#include "MathExtern.h"
|
2006-10-22 10:15:23 +00:00
|
|
|
|
#include "MathStream.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
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
namespace lyx {
|
2006-08-13 22:54:59 +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;
|
2006-10-21 11:15:37 +00:00
|
|
|
|
using std::vector;
|
2002-02-16 15:59:55 +00:00
|
|
|
|
|
|
|
|
|
|
2007-04-17 16:52:43 +00:00
|
|
|
|
/// This class is the value of a macro argument, technically
|
|
|
|
|
/// a wrapper of the cells of MathMacro.
|
|
|
|
|
class MathMacroArgumentValue : public InsetMathDim {
|
|
|
|
|
public:
|
|
|
|
|
///
|
|
|
|
|
MathMacroArgumentValue(MathArray const * value, docstring const & macroName)
|
|
|
|
|
: value_(value), macroName_(macroName) {}
|
|
|
|
|
///
|
|
|
|
|
bool metrics(MetricsInfo & mi, Dimension & dim) const;
|
|
|
|
|
///
|
|
|
|
|
void draw(PainterInfo &, int x, int y) const;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::auto_ptr<InsetBase> doClone() const;
|
|
|
|
|
MathArray const * value_;
|
|
|
|
|
docstring macroName_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
auto_ptr<InsetBase> MathMacroArgumentValue::doClone() const
|
|
|
|
|
{
|
|
|
|
|
return auto_ptr<InsetBase>(new MathMacroArgumentValue(*this));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool MathMacroArgumentValue::metrics(MetricsInfo & mi, Dimension & dim) const
|
|
|
|
|
{
|
|
|
|
|
// unlock outer macro in arguments, and lock it again later
|
|
|
|
|
MacroTable::globalMacros().get(macroName_).unlock();
|
|
|
|
|
value_->metrics(mi, dim);
|
|
|
|
|
MacroTable::globalMacros().get(macroName_).lock();
|
|
|
|
|
metricsMarkers2(dim);
|
|
|
|
|
if (dim_ == dim)
|
|
|
|
|
return false;
|
|
|
|
|
dim_ = dim;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void MathMacroArgumentValue::draw(PainterInfo & pi, int x, int y) const
|
|
|
|
|
{
|
|
|
|
|
// unlock outer macro in arguments, and lock it again later
|
|
|
|
|
MacroTable::globalMacros().get(macroName_).unlock();
|
|
|
|
|
value_->draw(pi, x, y);
|
|
|
|
|
MacroTable::globalMacros().get(macroName_).lock();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-04-17 16:49:17 +00:00
|
|
|
|
MathMacro::MathMacro(docstring const & name, int numargs)
|
|
|
|
|
: InsetMathNest(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
|
|
|
|
|
2006-10-22 10:15:23 +00:00
|
|
|
|
docstring 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
|
|
|
|
|
2006-10-17 16:23:27 +00:00
|
|
|
|
void MathMacro::cursorPos(BufferView const & bv,
|
|
|
|
|
CursorSlice const & sl, bool boundary, int & x, int & y) const
|
2005-10-05 21:19:32 +00:00
|
|
|
|
{
|
2006-09-16 18:11:38 +00:00
|
|
|
|
// We may have 0 arguments, but InsetMathNest requires at least one.
|
2005-10-05 21:19:32 +00:00
|
|
|
|
if (nargs() > 0)
|
2006-10-17 16:23:27 +00:00
|
|
|
|
InsetMathNest::cursorPos(bv, sl, boundary, x, y);
|
2005-10-05 21:19:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-11-28 15:15:49 +00:00
|
|
|
|
bool 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())) {
|
2006-10-22 10:15:23 +00:00
|
|
|
|
mathed_string_dim(mi.base.font, "Unknown: " + name(), dim);
|
2004-04-13 13:54:58 +00:00
|
|
|
|
} else {
|
2007-04-17 16:52:43 +00:00
|
|
|
|
MacroData const & macro = MacroTable::globalMacros().get(name());
|
|
|
|
|
if (macro.locked()) {
|
|
|
|
|
mathed_string_dim(mi.base.font, "Self reference: " + name(), dim);
|
|
|
|
|
expanded_ = MathArray();
|
|
|
|
|
} else if (editing(mi.base.bv)) {
|
|
|
|
|
// FIXME UNICODE
|
|
|
|
|
asArray(macro.def(), tmpl_);
|
|
|
|
|
LyXFont font = mi.base.font;
|
|
|
|
|
augmentFont(font, from_ascii("lyxtex"));
|
|
|
|
|
tmpl_.metrics(mi, dim);
|
|
|
|
|
// FIXME UNICODE
|
|
|
|
|
dim.wid += mathed_string_width(font, name()) + 10;
|
|
|
|
|
// FIXME UNICODE
|
|
|
|
|
int ww = mathed_string_width(font, from_ascii("#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 {
|
|
|
|
|
// create MathMacroArgumentValue object pointing to the cells of the macro
|
|
|
|
|
MacroData const & macro = MacroTable::globalMacros().get(name());
|
|
|
|
|
vector<MathArray> values(nargs());
|
|
|
|
|
for (size_t i = 0; i != nargs(); ++i)
|
|
|
|
|
values[i].insert(0, MathAtom(new MathMacroArgumentValue(&cells_[i], name())));
|
|
|
|
|
macro.expand(values, expanded_);
|
|
|
|
|
|
|
|
|
|
MacroTable::globalMacros().get(name()).lock();
|
|
|
|
|
expanded_.metrics(mi, dim);
|
|
|
|
|
MacroTable::globalMacros().get(name()).unlock();
|
|
|
|
|
}
|
2004-04-13 13:54:58 +00:00
|
|
|
|
}
|
|
|
|
|
metricsMarkers2(dim);
|
2006-11-28 15:15:49 +00:00
|
|
|
|
if (dim_ == dim)
|
|
|
|
|
return false;
|
2004-01-30 11:41:12 +00:00
|
|
|
|
dim_ = dim;
|
2006-11-28 15:15:49 +00:00
|
|
|
|
return true;
|
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())) {
|
2006-10-17 14:46:45 +00:00
|
|
|
|
// FIXME UNICODE
|
2006-10-22 10:15:23 +00:00
|
|
|
|
drawStrRed(pi, x, y, "Unknown: " + name());
|
2004-04-13 13:54:58 +00:00
|
|
|
|
} else {
|
2007-04-17 16:52:43 +00:00
|
|
|
|
MacroData const & macro = MacroTable::globalMacros().get(name());
|
|
|
|
|
if (macro.locked()) {
|
|
|
|
|
// FIXME UNICODE
|
|
|
|
|
drawStrRed(pi, x, y, "Self reference: " + name());
|
|
|
|
|
} else if (editing(pi.base.bv)) {
|
|
|
|
|
LyXFont font = pi.base.font;
|
|
|
|
|
augmentFont(font, from_ascii("lyxtex"));
|
|
|
|
|
int h = y - dim_.ascent() + 2 + tmpl_.ascent();
|
|
|
|
|
pi.pain.text(x + 3, h, name(), font);
|
|
|
|
|
int const w = mathed_string_width(font, name());
|
|
|
|
|
tmpl_.draw(pi, x + w + 12, h);
|
|
|
|
|
h += tmpl_.descent();
|
|
|
|
|
Dimension ldim;
|
2007-04-18 09:59:02 +00:00
|
|
|
|
docstring t = from_ascii("#1: ");
|
|
|
|
|
mathed_string_dim(font, t, ldim);
|
2007-04-17 16:52:43 +00:00
|
|
|
|
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_type str[] = { '#', '1', ':', '\0' };
|
|
|
|
|
str[1] += static_cast<char_type>(i);
|
|
|
|
|
pi.pain.text(x + 3, h, str, font);
|
|
|
|
|
h += max(c.descent(), ldim.des) + 5;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
MacroTable::globalMacros().get(name()).lock();
|
|
|
|
|
expanded_.draw(pi, x, y);
|
|
|
|
|
MacroTable::globalMacros().get(name()).unlock();
|
|
|
|
|
}
|
2004-04-13 13:54:58 +00:00
|
|
|
|
}
|
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
|
|
|
|
|
{
|
2006-09-16 18:11:38 +00:00
|
|
|
|
// We may have 0 arguments, but InsetMathNest requires at least one.
|
2005-10-05 21:19:32 +00:00
|
|
|
|
if (nargs() > 0)
|
2006-09-16 18:11:38 +00:00
|
|
|
|
InsetMathNest::drawSelection(pi, x, y);
|
2005-10-05 21:19:32 +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")
|
2006-10-22 10:15:23 +00:00
|
|
|
|
features.require(to_utf8(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)
|
|
|
|
|
{
|
2006-09-16 18:11:38 +00:00
|
|
|
|
// We may have 0 arguments, but InsetMathNest requires at least one.
|
2007-04-17 16:49:17 +00:00
|
|
|
|
if (nargs() > 0) {
|
|
|
|
|
// Prevent crash due to cold coordcache
|
|
|
|
|
// FIXME: This is only a workaround, the call of
|
|
|
|
|
// InsetMathNest::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;
|
|
|
|
|
}
|
2006-09-16 18:11:38 +00:00
|
|
|
|
return InsetMathNest::editXY(cur, x, y);
|
2007-04-17 16:49:17 +00:00
|
|
|
|
}
|
|
|
|
|
return this;
|
2005-10-05 21:19:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-04-16 14:42:53 +00:00
|
|
|
|
bool MathMacro::idxFirst(LCursor & cur) const
|
|
|
|
|
{
|
|
|
|
|
cur.updateFlags(Update::Force);
|
|
|
|
|
return InsetMathNest::idxFirst(cur);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool MathMacro::idxLast(LCursor & cur) const
|
|
|
|
|
{
|
|
|
|
|
cur.updateFlags(Update::Force);
|
|
|
|
|
return InsetMathNest::idxLast(cur);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool MathMacro::notifyCursorLeaves(LCursor & cur)
|
|
|
|
|
{
|
|
|
|
|
cur.updateFlags(Update::Force);
|
|
|
|
|
return InsetMathNest::notifyCursorLeaves(cur);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-02-14 14:30:09 +00:00
|
|
|
|
void MathMacro::maple(MapleStream & os) const
|
2001-11-16 08:26:41 +00:00
|
|
|
|
{
|
2007-04-17 16:49:17 +00:00
|
|
|
|
updateExpansion();
|
2006-10-21 00:16:43 +00:00
|
|
|
|
lyx::maple(expanded_, os);
|
2001-11-16 08:26:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-10-22 10:15:23 +00:00
|
|
|
|
void MathMacro::mathmlize(MathStream & os) const
|
2001-11-16 08:29:11 +00:00
|
|
|
|
{
|
2007-04-17 16:49:17 +00:00
|
|
|
|
updateExpansion();
|
2006-10-21 00:16:43 +00:00
|
|
|
|
lyx::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
|
|
|
|
{
|
2007-04-17 16:49:17 +00:00
|
|
|
|
updateExpansion();
|
2006-10-21 00:16:43 +00:00
|
|
|
|
lyx::octave(expanded_, os);
|
2001-11-16 08:26:41 +00:00
|
|
|
|
}
|
2001-12-11 11:33:43 +00:00
|
|
|
|
|
|
|
|
|
|
2007-04-17 16:49:17 +00:00
|
|
|
|
void MathMacro::updateExpansion() const
|
|
|
|
|
{
|
|
|
|
|
//expanded_.substitute(*this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-10-22 10:15:23 +00:00
|
|
|
|
void MathMacro::infoize(odocstream & os) const
|
2003-01-07 11:24:43 +00:00
|
|
|
|
{
|
|
|
|
|
os << "Macro: " << name();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-10-22 10:15:23 +00:00
|
|
|
|
void MathMacro::infoize2(odocstream & os) const
|
2003-01-07 11:24:43 +00:00
|
|
|
|
{
|
|
|
|
|
os << "Macro: " << name();
|
2007-04-17 16:49:17 +00:00
|
|
|
|
|
2003-01-07 11:24:43 +00:00
|
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|