1999-09-27 18:44:28 +00:00
|
|
|
/*
|
|
|
|
* File: math_macro.C
|
2002-03-21 17:42:56 +00:00
|
|
|
* Purpose: Implementation of macro class for mathed
|
|
|
|
* Author: Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
|
1999-09-27 18:44:28 +00:00
|
|
|
* Created: November 1996
|
|
|
|
* Description: WYSIWYG math macros
|
|
|
|
*
|
2001-06-25 00:06:33 +00:00
|
|
|
* Dependencies: Math
|
1999-09-27 18:44:28 +00:00
|
|
|
*
|
2000-03-09 03:36:48 +00:00
|
|
|
* Copyright: 1996, 1997 Alejandro Aguilar Sierra
|
1999-09-27 18:44:28 +00:00
|
|
|
*
|
2001-06-25 00:06:33 +00:00
|
|
|
* Version: 0.2, Math & Lyx project.
|
1999-09-27 18:44:28 +00:00
|
|
|
*
|
|
|
|
* This code is under the GNU General Public Licence version 2 or later.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef __GNUG__
|
2001-02-13 13:28:32 +00:00
|
|
|
#pragma implementation
|
1999-09-27 18:44:28 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "math_macro.h"
|
2001-11-08 12:15:12 +00:00
|
|
|
#include "math_support.h"
|
2001-11-16 08:26:41 +00:00
|
|
|
#include "math_extern.h"
|
2001-06-25 00:06:33 +00:00
|
|
|
#include "math_macrotable.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-04-03 10:45:32 +00:00
|
|
|
#include "math_streamstr.h"
|
2001-11-16 08:26:41 +00:00
|
|
|
#include "support/lstrings.h"
|
|
|
|
#include "support/LAssert.h"
|
|
|
|
#include "debug.h"
|
2001-07-13 14:54:56 +00:00
|
|
|
#include "LaTeXFeatures.h"
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
2002-02-16 15:59:55 +00:00
|
|
|
using std::max;
|
|
|
|
|
|
|
|
|
2001-10-12 12:02:49 +00:00
|
|
|
MathMacro::MathMacro(string const & name)
|
|
|
|
: MathNestInset(MathMacroTable::provide(name)->asMacroTemplate()->numargs()),
|
|
|
|
tmplate_(MathMacroTable::provide(name))
|
2001-06-25 00:06:33 +00:00
|
|
|
{}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
1999-12-07 00:44:53 +00:00
|
|
|
|
2001-10-12 12:02:49 +00:00
|
|
|
MathMacro::MathMacro(MathMacro const & m)
|
|
|
|
: MathNestInset(m),
|
|
|
|
tmplate_(m.tmplate_) // don't copy 'expanded_'!
|
2001-07-12 11:55:57 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-06-28 10:25:20 +00:00
|
|
|
MathInset * MathMacro::clone() const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-02-28 11:56:36 +00:00
|
|
|
return new MathMacro(*this);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
2001-08-09 09:19:18 +00:00
|
|
|
|
2002-04-03 10:45:32 +00:00
|
|
|
string const & MathMacro::name() const
|
2001-08-08 17:26:30 +00:00
|
|
|
{
|
2002-04-03 10:45:32 +00:00
|
|
|
return tmplate_->asMacroTemplate()->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
|
|
|
|
2001-10-12 16:12:32 +00:00
|
|
|
bool MathMacro::defining() const
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
//return mathcursor && mathcursor->formula()->getInsetName() == name();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-03-25 12:11:25 +00:00
|
|
|
void MathMacro::expand() const
|
|
|
|
{
|
|
|
|
expanded_ = tmplate_->xcell(tmplate_->cell(1).empty() ? 0 : 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-05-30 07:09:54 +00:00
|
|
|
void MathMacro::metrics(MathMetricsInfo & mi) const
|
2001-04-24 16:13:38 +00:00
|
|
|
{
|
2002-05-30 07:09:54 +00:00
|
|
|
augmentFont(font_, "lyxtex");
|
2001-10-22 15:37:49 +00:00
|
|
|
mi_ = mi;
|
|
|
|
|
2001-10-12 16:12:32 +00:00
|
|
|
if (defining()) {
|
2002-03-19 16:55:58 +00:00
|
|
|
mathed_string_dim(font_, name(), ascent_, descent_, width_);
|
2001-10-12 16:12:32 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (editing()) {
|
2002-03-25 12:11:25 +00:00
|
|
|
expand();
|
2001-10-22 15:37:49 +00:00
|
|
|
expanded_.metrics(mi_);
|
2001-06-25 00:06:33 +00:00
|
|
|
width_ = expanded_.width() + 4;
|
|
|
|
ascent_ = expanded_.ascent() + 2;
|
|
|
|
descent_ = expanded_.descent() + 2;
|
2001-04-24 16:13:38 +00:00
|
|
|
|
2002-03-19 16:55:58 +00:00
|
|
|
width_ += mathed_string_width(font_, name()) + 10;
|
2001-04-24 16:13:38 +00:00
|
|
|
|
2001-07-06 12:09:32 +00:00
|
|
|
int lasc;
|
|
|
|
int ldes;
|
|
|
|
int lwid;
|
2002-03-19 16:55:58 +00:00
|
|
|
mathed_string_dim(font_, "#1: ", lasc, ldes, lwid);
|
2001-07-06 12:09:32 +00:00
|
|
|
|
2001-09-26 16:52:34 +00:00
|
|
|
for (idx_type i = 0; i < nargs(); ++i) {
|
2001-08-06 17:20:26 +00:00
|
|
|
MathXArray const & c = xcell(i);
|
2001-10-22 15:37:49 +00:00
|
|
|
c.metrics(mi_);
|
2002-02-16 15:59:55 +00:00
|
|
|
width_ = max(width_, c.width() + lwid);
|
|
|
|
descent_ += max(c.ascent(), lasc) + 5;
|
|
|
|
descent_ += max(c.descent(), ldes) + 5;
|
2001-04-24 16:13:38 +00:00
|
|
|
}
|
2001-10-12 16:12:32 +00:00
|
|
|
return;
|
2002-03-21 17:42:56 +00:00
|
|
|
}
|
2001-10-12 16:12:32 +00:00
|
|
|
|
2002-03-25 12:11:25 +00:00
|
|
|
expand();
|
2002-07-08 14:57:12 +00:00
|
|
|
expanded_.data().substitute(*this);
|
2001-10-22 15:37:49 +00:00
|
|
|
expanded_.metrics(mi_);
|
2001-10-31 10:54:34 +00:00
|
|
|
width_ = expanded_.width();
|
|
|
|
ascent_ = expanded_.ascent();
|
|
|
|
descent_ = expanded_.descent();
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-24 15:37:14 +00:00
|
|
|
void MathMacro::draw(MathPainterInfo & pi, int x, int y) const
|
2000-02-10 17:53:36 +00:00
|
|
|
{
|
2001-10-22 15:37:49 +00:00
|
|
|
metrics(mi_);
|
2001-07-12 11:55:57 +00:00
|
|
|
|
2002-03-19 16:55:58 +00:00
|
|
|
LyXFont texfont;
|
2002-05-30 07:09:54 +00:00
|
|
|
augmentFont(texfont, "lyxtex");
|
2002-03-19 16:55:58 +00:00
|
|
|
|
2001-10-12 16:12:32 +00:00
|
|
|
if (defining()) {
|
2002-06-24 15:37:14 +00:00
|
|
|
drawStr(pi, texfont, x, y, name());
|
2001-10-12 16:12:32 +00:00
|
|
|
return;
|
|
|
|
}
|
2001-06-25 00:06:33 +00:00
|
|
|
|
2001-10-12 16:12:32 +00:00
|
|
|
if (editing()) {
|
2001-06-25 00:06:33 +00:00
|
|
|
int h = y - ascent() + 2 + expanded_.ascent();
|
2002-06-24 15:37:14 +00:00
|
|
|
drawStr(pi, font_, x + 3, h, name());
|
2001-06-25 00:06:33 +00:00
|
|
|
|
2002-03-19 16:55:58 +00:00
|
|
|
int const w = mathed_string_width(font_, name());
|
2002-06-24 15:37:14 +00:00
|
|
|
expanded_.draw(pi, x + w + 12, h);
|
2001-06-25 00:06:33 +00:00
|
|
|
h += expanded_.descent();
|
|
|
|
|
2001-07-06 12:09:32 +00:00
|
|
|
int lasc;
|
|
|
|
int ldes;
|
|
|
|
int lwid;
|
2002-03-19 16:55:58 +00:00
|
|
|
mathed_string_dim(font_, "#1: ", lasc, ldes, lwid);
|
2001-07-06 12:09:32 +00:00
|
|
|
|
2001-09-26 16:52:34 +00:00
|
|
|
for (idx_type i = 0; i < nargs(); ++i) {
|
2001-08-06 17:20:26 +00:00
|
|
|
MathXArray const & c = xcell(i);
|
2002-02-16 15:59:55 +00:00
|
|
|
h += max(c.ascent(), lasc) + 5;
|
2002-06-24 15:37:14 +00:00
|
|
|
c.draw(pi, x + lwid, h);
|
2001-04-24 16:13:38 +00:00
|
|
|
char str[] = "#1:";
|
2001-10-01 12:23:00 +00:00
|
|
|
str[1] += static_cast<char>(i);
|
2002-06-24 15:37:14 +00:00
|
|
|
drawStr(pi, texfont, x + 3, h, str);
|
2002-02-16 15:59:55 +00:00
|
|
|
h += max(c.descent(), ldes) + 5;
|
2001-04-24 16:13:38 +00:00
|
|
|
}
|
2001-10-12 16:12:32 +00:00
|
|
|
return;
|
2001-04-24 16:13:38 +00:00
|
|
|
}
|
|
|
|
|
2002-06-24 15:37:14 +00:00
|
|
|
expanded_.draw(pi, x, y);
|
2001-04-24 16:13:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-09-26 15:20:45 +00:00
|
|
|
void MathMacro::dump() const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
MathMacroTable::dump();
|
2001-09-26 15:20:45 +00:00
|
|
|
lyxerr << "\n macro: '" << this << "'\n";
|
|
|
|
lyxerr << " name: '" << name() << "'\n";
|
2001-10-19 11:25:48 +00:00
|
|
|
lyxerr << " template: '";
|
2001-11-09 08:35:57 +00:00
|
|
|
WriteStream wi(lyxerr);
|
2001-10-19 11:25:48 +00:00
|
|
|
tmplate_->write(wi);
|
|
|
|
lyxerr << "'\n";
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
2001-10-12 12:02:49 +00:00
|
|
|
|
2002-03-21 09:48:46 +00:00
|
|
|
bool MathMacro::idxUpDown(idx_type & idx, bool up) const
|
2001-04-24 16:13:38 +00:00
|
|
|
{
|
2001-12-12 13:11:28 +00:00
|
|
|
pos_type pos;
|
2002-03-21 09:48:46 +00:00
|
|
|
return
|
|
|
|
up ? MathNestInset::idxLeft(idx, pos) : MathNestInset::idxRight(idx, pos);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-09-26 16:52:34 +00:00
|
|
|
bool MathMacro::idxLeft(idx_type &, pos_type &) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
return false;
|
2001-04-24 16:13:38 +00:00
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2001-04-25 15:43:57 +00:00
|
|
|
|
2001-09-26 16:52:34 +00:00
|
|
|
bool MathMacro::idxRight(idx_type &, pos_type &) const
|
2001-04-25 15:43:57 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
return false;
|
2001-04-25 15:43:57 +00:00
|
|
|
}
|
2001-07-13 14:54:56 +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-08-06 17:20:26 +00:00
|
|
|
//MathInset::validate(features);
|
2001-07-13 14:54:56 +00:00
|
|
|
}
|
2001-11-16 08:26:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
void MathMacro::maplize(MapleStream & os) const
|
|
|
|
{
|
2001-12-11 11:33:43 +00:00
|
|
|
updateExpansion();
|
2002-07-08 14:57:12 +00:00
|
|
|
::maplize(expanded_.data(), 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-07-08 14:57:12 +00:00
|
|
|
::mathmlize(expanded_.data(), os);
|
2001-11-16 08:29:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MathMacro::octavize(OctaveStream & os) const
|
|
|
|
{
|
2001-12-11 11:33:43 +00:00
|
|
|
updateExpansion();
|
2002-07-08 14:57:12 +00:00
|
|
|
::octavize(expanded_.data(), os);
|
2001-11-16 08:29:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-16 08:26:41 +00:00
|
|
|
void MathMacro::normalize(NormalStream & os) const
|
|
|
|
{
|
|
|
|
os << "[macro " << name() << " ";
|
2002-03-21 17:42:56 +00:00
|
|
|
for (idx_type i = 0; i < nargs(); ++i)
|
2001-11-16 08:26:41 +00:00
|
|
|
os << cell(i) << ' ';
|
|
|
|
os << ']';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MathMacro::write(WriteStream & os) const
|
|
|
|
{
|
|
|
|
os << '\\' << name();
|
|
|
|
for (idx_type i = 0; i < nargs(); ++i)
|
|
|
|
os << '{' << cell(i) << '}';
|
2002-03-21 17:42:56 +00:00
|
|
|
if (nargs() == 0)
|
2001-11-16 08:26:41 +00:00
|
|
|
os << ' ';
|
|
|
|
}
|
2001-12-11 11:33:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
void MathMacro::updateExpansion() const
|
|
|
|
{
|
2002-03-25 12:11:25 +00:00
|
|
|
expand();
|
2002-07-08 14:57:12 +00:00
|
|
|
expanded_.data().substitute(*this);
|
2001-12-11 11:33:43 +00:00
|
|
|
}
|