2003-08-19 13:00:56 +00:00
|
|
|
|
/**
|
2006-09-17 09:14:18 +00:00
|
|
|
|
* \file InsetMathMacro.C
|
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
|
|
|
|
|
2006-09-17 09:14:18 +00:00
|
|
|
|
#include "InsetMathMacro.h"
|
|
|
|
|
#include "MathSupport.h"
|
|
|
|
|
#include "MathExtern.h"
|
|
|
|
|
#include "MathMLStream.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-08-13 22:54:59 +00:00
|
|
|
|
using lyx::docstring;
|
|
|
|
|
|
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;
|
2002-02-16 15:59:55 +00:00
|
|
|
|
|
|
|
|
|
|
2004-04-13 13:54:58 +00:00
|
|
|
|
MathMacro::MathMacro(string const & name, int numargs)
|
2006-09-16 18:11:38 +00:00
|
|
|
|
: 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
|
|
|
|
|
2002-08-01 15:53:46 +00:00
|
|
|
|
string 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
|
|
|
|
|
2005-10-05 21:19:32 +00:00
|
|
|
|
void MathMacro::cursorPos(CursorSlice const & sl, bool boundary, 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::cursorPos(sl, boundary, x, y);
|
2005-10-05 21:19:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-04-13 06:27:29 +00:00
|
|
|
|
void 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-17 14:46:45 +00:00
|
|
|
|
mathed_string_dim(mi.base.font, lyx::from_utf8("Unknown: " + name()), dim);
|
2004-04-13 13:54:58 +00:00
|
|
|
|
} else if (editing(mi.base.bv)) {
|
2006-10-17 14:46:45 +00:00
|
|
|
|
// FIXME UNICODE
|
|
|
|
|
asArray(lyx::from_utf8(MacroTable::globalMacros().get(name()).def()), tmpl_);
|
2004-04-13 13:54:58 +00:00
|
|
|
|
LyXFont font = mi.base.font;
|
|
|
|
|
augmentFont(font, "lyxtex");
|
|
|
|
|
tmpl_.metrics(mi, dim);
|
2006-10-17 14:46:45 +00:00
|
|
|
|
// FIXME UNICODE
|
|
|
|
|
dim.wid += mathed_string_width(font, lyx::from_utf8(name())) + 10;
|
|
|
|
|
// FIXME UNICODE
|
|
|
|
|
int ww = mathed_string_width(font, lyx::from_ascii("#1: "));
|
2004-04-13 13:54:58 +00:00
|
|
|
|
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 {
|
|
|
|
|
MacroTable::globalMacros().get(name()).expand(cells_, expanded_);
|
|
|
|
|
expanded_.metrics(mi, dim);
|
|
|
|
|
}
|
|
|
|
|
metricsMarkers2(dim);
|
2004-01-30 11:41:12 +00:00
|
|
|
|
dim_ = 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
|
|
|
|
|
drawStrRed(pi, x, y, lyx::from_utf8("Unknown: " + name()));
|
2004-04-13 13:54:58 +00:00
|
|
|
|
} else if (editing(pi.base.bv)) {
|
|
|
|
|
LyXFont font = pi.base.font;
|
|
|
|
|
augmentFont(font, "lyxtex");
|
|
|
|
|
int h = y - dim_.ascent() + 2 + tmpl_.ascent();
|
2006-10-17 14:46:45 +00:00
|
|
|
|
// FIXME UNICODE
|
|
|
|
|
docstring dn = lyx::from_utf8(name());
|
2006-08-13 22:54:59 +00:00
|
|
|
|
pi.pain.text(x + 3, h, dn, font);
|
2006-10-17 14:46:45 +00:00
|
|
|
|
int const w = mathed_string_width(font, dn);
|
2004-04-13 13:54:58 +00:00
|
|
|
|
tmpl_.draw(pi, x + w + 12, h);
|
|
|
|
|
h += tmpl_.descent();
|
|
|
|
|
Dimension ldim;
|
2006-10-17 14:46:45 +00:00
|
|
|
|
mathed_string_dim(font, lyx::from_ascii("#1: "), ldim);
|
2004-04-13 13:54:58 +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);
|
2006-08-13 22:54:59 +00:00
|
|
|
|
lyx::char_type str[] = { '#', '1', ':', '\0' };
|
|
|
|
|
str[1] += static_cast<lyx::char_type>(i);
|
2005-07-17 10:31:44 +00:00
|
|
|
|
pi.pain.text(x + 3, h, str, font);
|
2004-04-13 13:54:58 +00:00
|
|
|
|
h += max(c.descent(), ldim.des) + 5;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
expanded_.draw(pi, x, y);
|
|
|
|
|
}
|
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")
|
|
|
|
|
features.require(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.
|
2006-02-14 10:41:34 +00:00
|
|
|
|
if (nargs() > 0) {
|
|
|
|
|
// Prevent crash due to cold coordcache
|
|
|
|
|
// FIXME: This is only a workaround, the call of
|
2006-09-16 18:11:38 +00:00
|
|
|
|
// InsetMathNest::editXY is correct. The correct fix would
|
2006-02-14 10:41:34 +00:00
|
|
|
|
// 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);
|
2006-02-14 10:41:34 +00:00
|
|
|
|
}
|
2005-10-05 21:19:32 +00:00
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-02-14 14:30:09 +00:00
|
|
|
|
void MathMacro::maple(MapleStream & os) const
|
2001-11-16 08:26:41 +00:00
|
|
|
|
{
|
2001-12-11 11:33:43 +00:00
|
|
|
|
updateExpansion();
|
2003-02-14 14:30:09 +00:00
|
|
|
|
::maple(expanded_, os);
|
2001-11-16 08:26:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2001-11-16 08:29:11 +00:00
|
|
|
|
void MathMacro::mathmlize(MathMLStream & os) const
|
|
|
|
|
{
|
2001-12-11 11:33:43 +00:00
|
|
|
|
updateExpansion();
|
2002-08-02 14:29:42 +00:00
|
|
|
|
::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
|
|
|
|
{
|
2001-12-11 11:33:43 +00:00
|
|
|
|
updateExpansion();
|
2003-02-14 14:30:09 +00:00
|
|
|
|
::octave(expanded_, os);
|
2001-11-16 08:26:41 +00:00
|
|
|
|
}
|
2001-12-11 11:33:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void MathMacro::updateExpansion() const
|
|
|
|
|
{
|
2004-04-13 06:27:29 +00:00
|
|
|
|
//expanded_.substitute(*this);
|
2001-12-11 11:33:43 +00:00
|
|
|
|
}
|
2003-01-07 11:24:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void MathMacro::infoize(std::ostream & os) const
|
|
|
|
|
{
|
|
|
|
|
os << "Macro: " << name();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void MathMacro::infoize2(std::ostream & os) const
|
|
|
|
|
{
|
|
|
|
|
os << "Macro: " << name();
|
2004-04-13 06:27:29 +00:00
|
|
|
|
|
2003-01-07 11:24:43 +00:00
|
|
|
|
}
|