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
|
|
|
|
|
2007-04-26 04:41:58 +00:00
|
|
|
|
#include "Buffer.h"
|
2007-04-26 14:56:30 +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;
|
|
|
|
|
|
|
|
|
|
|
2007-05-28 22:27:45 +00:00
|
|
|
|
/// This class is the value of a macro argument, technically
|
2007-04-17 16:52:43 +00:00
|
|
|
|
/// a wrapper of the cells of MathMacro.
|
2007-04-30 10:31:51 +00:00
|
|
|
|
class MathMacroArgumentValue : public InsetMath {
|
2007-04-17 16:52:43 +00:00
|
|
|
|
public:
|
|
|
|
|
///
|
2007-05-28 22:27:45 +00:00
|
|
|
|
MathMacroArgumentValue(MathMacro const & mathMacro, size_t idx)
|
2007-05-24 16:29:40 +00:00
|
|
|
|
: mathMacro_(mathMacro), idx_(idx) {}
|
2007-04-17 16:52:43 +00:00
|
|
|
|
///
|
2007-09-21 20:39:47 +00:00
|
|
|
|
void metrics(MetricsInfo & mi, Dimension & dim) const;
|
2007-04-17 16:52:43 +00:00
|
|
|
|
///
|
|
|
|
|
void draw(PainterInfo &, int x, int y) const;
|
2007-06-15 18:26:35 +00:00
|
|
|
|
///
|
|
|
|
|
int kerning() const { return mathMacro_.cell(idx_).kerning(); }
|
2007-05-28 22:27:45 +00:00
|
|
|
|
|
2007-04-17 16:52:43 +00:00
|
|
|
|
private:
|
2007-08-30 18:03:17 +00:00
|
|
|
|
Inset * clone() const;
|
2007-05-24 16:29:40 +00:00
|
|
|
|
MathMacro const & mathMacro_;
|
|
|
|
|
size_t idx_;
|
2007-04-17 16:52:43 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2007-08-30 18:03:17 +00:00
|
|
|
|
Inset * MathMacroArgumentValue::clone() const
|
2007-04-17 16:52:43 +00:00
|
|
|
|
{
|
2007-08-30 18:03:17 +00:00
|
|
|
|
return new MathMacroArgumentValue(*this);
|
2007-04-17 16:52:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-09-21 20:39:47 +00:00
|
|
|
|
void MathMacroArgumentValue::metrics(MetricsInfo & mi, Dimension & dim) const
|
2007-04-17 16:52:43 +00:00
|
|
|
|
{
|
|
|
|
|
// unlock outer macro in arguments, and lock it again later
|
2007-05-24 16:29:40 +00:00
|
|
|
|
MacroData const & macro = MacroTable::globalMacros().get(mathMacro_.name());
|
|
|
|
|
macro.unlock();
|
|
|
|
|
mathMacro_.cell(idx_).metrics(mi, dim);
|
|
|
|
|
macro.lock();
|
2007-04-17 16:52:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-05-28 22:27:45 +00:00
|
|
|
|
void MathMacroArgumentValue::draw(PainterInfo & pi, int x, int y) const
|
2007-04-17 16:52:43 +00:00
|
|
|
|
{
|
|
|
|
|
// unlock outer macro in arguments, and lock it again later
|
2007-05-24 16:29:40 +00:00
|
|
|
|
MacroData const & macro = MacroTable::globalMacros().get(mathMacro_.name());
|
|
|
|
|
macro.unlock();
|
|
|
|
|
mathMacro_.cell(idx_).draw(pi, x, y);
|
|
|
|
|
macro.lock();
|
2007-04-17 16:52:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-04-17 16:49:17 +00:00
|
|
|
|
MathMacro::MathMacro(docstring const & name, int numargs)
|
2007-05-24 16:29:40 +00:00
|
|
|
|
: InsetMathNest(numargs), name_(name), editing_(false)
|
2001-07-12 11:55:57 +00:00
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
2007-08-30 18:03:17 +00:00
|
|
|
|
Inset * MathMacro::clone() const
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2007-05-24 16:29:40 +00:00
|
|
|
|
MathMacro * x = new MathMacro(*this);
|
|
|
|
|
x->expanded_ = MathData();
|
|
|
|
|
x->macroBackup_ = MacroData();
|
2007-08-30 18:03:17 +00:00
|
|
|
|
return x;
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-09-21 20:39:47 +00:00
|
|
|
|
void MathMacro::metrics(MetricsInfo & mi, Dimension & dim) const
|
2002-03-25 12:11:25 +00:00
|
|
|
|
{
|
2007-06-15 18:26:35 +00:00
|
|
|
|
kerning_ = 0;
|
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());
|
2007-05-28 22:27:45 +00:00
|
|
|
|
|
2007-05-24 16:29:40 +00:00
|
|
|
|
if (macroBackup_ != macro)
|
|
|
|
|
updateExpansion();
|
2007-05-28 22:27:45 +00:00
|
|
|
|
|
2007-04-17 16:52:43 +00:00
|
|
|
|
if (macro.locked()) {
|
|
|
|
|
mathed_string_dim(mi.base.font, "Self reference: " + name(), dim);
|
|
|
|
|
} else if (editing(mi.base.bv)) {
|
2007-04-29 18:17:15 +00:00
|
|
|
|
Font font = mi.base.font;
|
2007-04-17 16:52:43 +00:00
|
|
|
|
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) {
|
2007-04-26 16:05:57 +00:00
|
|
|
|
MathData const & c = cell(i);
|
2007-04-17 16:52:43 +00:00
|
|
|
|
c.metrics(mi);
|
|
|
|
|
dim.wid = max(dim.wid, c.width() + ww);
|
|
|
|
|
dim.des += c.height() + 10;
|
|
|
|
|
}
|
2007-05-24 16:29:40 +00:00
|
|
|
|
editing_ = true;
|
2007-04-17 16:52:43 +00:00
|
|
|
|
} else {
|
2007-05-24 16:29:40 +00:00
|
|
|
|
macro.lock();
|
2007-04-17 16:52:43 +00:00
|
|
|
|
expanded_.metrics(mi, dim);
|
2007-05-24 16:29:40 +00:00
|
|
|
|
macro.unlock();
|
2007-06-15 18:26:35 +00:00
|
|
|
|
kerning_ = expanded_.kerning();
|
2007-05-24 16:29:40 +00:00
|
|
|
|
editing_ = false;
|
2007-04-17 16:52:43 +00:00
|
|
|
|
}
|
2004-04-13 13:54:58 +00:00
|
|
|
|
}
|
2007-09-23 22:39:49 +00:00
|
|
|
|
// Cache the inset dimension.
|
|
|
|
|
setDimCache(mi, 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
|
|
|
|
{
|
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());
|
2007-05-28 22:27:45 +00:00
|
|
|
|
|
2007-05-24 16:29:40 +00:00
|
|
|
|
// warm up cache
|
|
|
|
|
for (size_t i = 0; i < nargs(); ++i)
|
|
|
|
|
cell(i).setXY(*pi.base.bv, x, y);
|
2007-05-28 22:27:45 +00:00
|
|
|
|
|
2007-04-17 16:52:43 +00:00
|
|
|
|
if (macro.locked()) {
|
|
|
|
|
// FIXME UNICODE
|
|
|
|
|
drawStrRed(pi, x, y, "Self reference: " + name());
|
2007-05-24 16:29:40 +00:00
|
|
|
|
} else if (editing_) {
|
2007-04-29 18:17:15 +00:00
|
|
|
|
Font font = pi.base.font;
|
2007-04-17 16:52:43 +00:00
|
|
|
|
augmentFont(font, from_ascii("lyxtex"));
|
2007-09-23 22:39:49 +00:00
|
|
|
|
Dimension const dim = dimension(*pi.base.bv);
|
|
|
|
|
int h = y - dim.ascent() + 2 + tmpl_.ascent();
|
2007-04-17 16:52:43 +00:00
|
|
|
|
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) {
|
2007-04-26 16:05:57 +00:00
|
|
|
|
MathData const & c = cell(i);
|
2007-04-17 16:52:43 +00:00
|
|
|
|
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 {
|
2007-05-24 16:29:40 +00:00
|
|
|
|
macro.lock();
|
2007-04-17 16:52:43 +00:00
|
|
|
|
expanded_.draw(pi, x, y);
|
2007-05-24 16:29:40 +00:00
|
|
|
|
macro.unlock();
|
2007-04-17 16:52:43 +00:00
|
|
|
|
}
|
2007-05-28 22:27:45 +00:00
|
|
|
|
|
|
|
|
|
// edit mode changed?
|
2007-05-24 16:29:40 +00:00
|
|
|
|
if (editing_ != editing(pi.base.bv) || macroBackup_ != macro)
|
|
|
|
|
pi.base.bv->cursor().updateFlags(Update::Force);
|
2004-04-13 13:54:58 +00:00
|
|
|
|
}
|
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
|
|
|
|
{
|
2007-05-04 15:30:27 +00:00
|
|
|
|
string const require = MacroTable::globalMacros().get(name()).requires();
|
|
|
|
|
if (!require.empty())
|
|
|
|
|
features.require(require);
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
2007-04-29 13:39:47 +00:00
|
|
|
|
Inset * MathMacro::editXY(Cursor & cur, int x, int y)
|
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.
|
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-05-28 22:27:45 +00:00
|
|
|
|
bool MathMacro::idxFirst(Cursor & cur) const
|
2007-04-16 14:42:53 +00:00
|
|
|
|
{
|
|
|
|
|
cur.updateFlags(Update::Force);
|
|
|
|
|
return InsetMathNest::idxFirst(cur);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-05-28 22:27:45 +00:00
|
|
|
|
bool MathMacro::idxLast(Cursor & cur) const
|
2007-04-16 14:42:53 +00:00
|
|
|
|
{
|
|
|
|
|
cur.updateFlags(Update::Force);
|
|
|
|
|
return InsetMathNest::idxLast(cur);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-06-14 20:57:56 +00:00
|
|
|
|
bool MathMacro::idxUpDown(Cursor & cur, bool up) const
|
|
|
|
|
{
|
|
|
|
|
if (up) {
|
|
|
|
|
if (cur.idx() == 0)
|
|
|
|
|
return false;
|
|
|
|
|
--cur.idx();
|
|
|
|
|
} else {
|
|
|
|
|
if (cur.idx() + 1 >= nargs())
|
|
|
|
|
return false;
|
|
|
|
|
++cur.idx();
|
|
|
|
|
}
|
|
|
|
|
cur.pos() = cell(cur.idx()).x2pos(cur.x_target());
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-04-26 14:56:30 +00:00
|
|
|
|
bool MathMacro::notifyCursorLeaves(Cursor & cur)
|
2007-04-16 14:42:53 +00:00
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
{
|
2007-05-24 16:29:40 +00:00
|
|
|
|
MacroData const & macro = MacroTable::globalMacros().get(name());
|
2007-05-28 22:27:45 +00:00
|
|
|
|
|
2007-05-24 16:29:40 +00:00
|
|
|
|
// create MathMacroArgumentValue object pointing to the cells of the macro
|
2007-08-30 18:03:17 +00:00
|
|
|
|
std::vector<MathData> values(nargs());
|
2007-05-28 22:27:45 +00:00
|
|
|
|
for (size_t i = 0; i != nargs(); ++i)
|
2007-05-24 16:29:40 +00:00
|
|
|
|
values[i].insert(0, MathAtom(new MathMacroArgumentValue(*this, i)));
|
|
|
|
|
macro.expand(values, expanded_);
|
|
|
|
|
asArray(macro.def(), tmpl_);
|
|
|
|
|
macroBackup_ = macro;
|
2007-04-17 16:49:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
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
|