1999-09-27 18:44:28 +00:00
|
|
|
/*
|
|
|
|
* File: math_macro.C
|
|
|
|
* Purpose: Implementation of macro class for mathed
|
|
|
|
* Author: Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
|
|
|
|
* 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-02-13 13:28:32 +00:00
|
|
|
#include "array.h"
|
1999-10-02 16:21:10 +00:00
|
|
|
#include "support/lstrings.h"
|
2001-06-25 00:06:33 +00:00
|
|
|
#include "support/LAssert.h"
|
1999-10-07 18:44:17 +00:00
|
|
|
#include "debug.h"
|
2001-02-13 17:08:51 +00:00
|
|
|
#include "mathed/support.h"
|
2001-06-25 00:06:33 +00:00
|
|
|
#include "mathed/math_cursor.h"
|
|
|
|
#include "math_macrotable.h"
|
2001-02-13 17:08:51 +00:00
|
|
|
#include "math_macrotemplate.h"
|
2001-04-24 16:13:38 +00:00
|
|
|
#include "Painter.h"
|
2001-07-13 14:54:56 +00:00
|
|
|
#include "LaTeXFeatures.h"
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2001-06-27 15:57:57 +00:00
|
|
|
using std::endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2001-04-24 16:13:38 +00:00
|
|
|
MathMacro::MathMacro(MathMacroTemplate const & t)
|
2001-08-08 17:26:30 +00:00
|
|
|
: MathNestInset(t.numargs()), tmplate_(&t)
|
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-07-12 11:55:57 +00:00
|
|
|
MathMacro::MathMacro(MathMacro const & t)
|
2001-08-03 17:10:22 +00:00
|
|
|
: MathNestInset(t), tmplate_(t.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
|
|
|
|
|
|
|
const char * MathMacro::name() const
|
2001-08-08 17:26:30 +00:00
|
|
|
{
|
2001-08-09 09:19:18 +00:00
|
|
|
return tmplate_->name().c_str();
|
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-08-06 17:20:26 +00:00
|
|
|
void MathMacro::metrics(MathStyles st) const
|
2001-04-24 16:13:38 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
if (mathcursor && mathcursor->isInside(this)) {
|
|
|
|
expanded_ = tmplate_->xcell(0);
|
2001-07-26 06:56:43 +00:00
|
|
|
expanded_.metrics(st);
|
2001-06-25 00:06:33 +00:00
|
|
|
size_ = st;
|
|
|
|
width_ = expanded_.width() + 4;
|
|
|
|
ascent_ = expanded_.ascent() + 2;
|
|
|
|
descent_ = expanded_.descent() + 2;
|
2001-04-24 16:13:38 +00:00
|
|
|
|
2001-08-08 17:26:30 +00:00
|
|
|
width_ += mathed_string_width(LM_TC_TEXTRM, size_, name()) + 10;
|
2001-04-24 16:13:38 +00:00
|
|
|
|
2001-07-06 12:09:32 +00:00
|
|
|
int lasc;
|
|
|
|
int ldes;
|
|
|
|
int lwid;
|
|
|
|
mathed_string_dim(LM_TC_TEXTRM, size_, "#1: ", lasc, ldes, lwid);
|
|
|
|
|
2001-04-24 16:13:38 +00:00
|
|
|
for (int i = 0; i < nargs(); ++i) {
|
2001-08-06 17:20:26 +00:00
|
|
|
MathXArray const & c = xcell(i);
|
2001-07-26 06:56:43 +00:00
|
|
|
c.metrics(st);
|
2001-07-06 12:09:32 +00:00
|
|
|
width_ = std::max(width_, c.width() + lwid);
|
|
|
|
descent_ += std::max(c.ascent(), lasc) + 5;
|
|
|
|
descent_ += std::max(c.descent(), ldes) + 5;
|
2001-04-24 16:13:38 +00:00
|
|
|
}
|
|
|
|
} else {
|
2001-06-25 00:06:33 +00:00
|
|
|
expanded_ = tmplate_->xcell(0);
|
|
|
|
expanded_.data_.substitute(*this);
|
2001-07-26 06:56:43 +00:00
|
|
|
expanded_.metrics(st);
|
2001-06-25 00:06:33 +00:00
|
|
|
size_ = st;
|
|
|
|
width_ = expanded_.width() + 6;
|
|
|
|
ascent_ = expanded_.ascent() + 3;
|
|
|
|
descent_ = expanded_.descent() + 3;
|
2001-04-24 16:13:38 +00:00
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-08-06 17:20:26 +00:00
|
|
|
void MathMacro::draw(Painter & pain, int x, int y) const
|
2000-02-10 17:53:36 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
xo(x);
|
|
|
|
yo(y);
|
|
|
|
|
2001-07-26 06:56:43 +00:00
|
|
|
metrics(size());
|
2001-07-12 11:55:57 +00:00
|
|
|
|
2001-04-24 16:13:38 +00:00
|
|
|
LColor::color col;
|
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
if (mathcursor && mathcursor->isInside(this)) {
|
|
|
|
|
|
|
|
int h = y - ascent() + 2 + expanded_.ascent();
|
2001-08-08 17:26:30 +00:00
|
|
|
drawStr(pain, LM_TC_TEXTRM, size(), x + 3, h, name());
|
2001-06-25 00:06:33 +00:00
|
|
|
|
2001-08-08 17:26:30 +00:00
|
|
|
int const w = mathed_string_width(LM_TC_TEXTRM, size(), name());
|
2001-06-25 00:06:33 +00:00
|
|
|
expanded_.draw(pain, x + w + 12, h);
|
|
|
|
h += expanded_.descent();
|
|
|
|
|
2001-07-06 12:09:32 +00:00
|
|
|
int lasc;
|
|
|
|
int ldes;
|
|
|
|
int lwid;
|
|
|
|
mathed_string_dim(LM_TC_TEXTRM, size_, "#1: ", lasc, ldes, lwid);
|
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
for (int i = 0; i < nargs(); ++i) {
|
2001-08-06 17:20:26 +00:00
|
|
|
MathXArray const & c = xcell(i);
|
2001-07-06 12:09:32 +00:00
|
|
|
h += std::max(c.ascent(), lasc) + 5;
|
|
|
|
c.draw(pain, x + lwid, h);
|
2001-04-24 16:13:38 +00:00
|
|
|
char str[] = "#1:";
|
|
|
|
str[1] += i;
|
2001-04-25 15:43:57 +00:00
|
|
|
drawStr(pain, LM_TC_TEX, size(), x + 3, h, str);
|
2001-07-06 12:09:32 +00:00
|
|
|
h += std::max(c.descent(), ldes) + 5;
|
2001-04-24 16:13:38 +00:00
|
|
|
}
|
|
|
|
col = LColor::red;
|
|
|
|
} else {
|
2001-06-25 00:06:33 +00:00
|
|
|
expanded_.draw(pain, x + 3, y);
|
2001-04-24 16:13:38 +00:00
|
|
|
col = LColor::black;
|
|
|
|
}
|
|
|
|
|
2001-07-12 11:55:57 +00:00
|
|
|
if (nargs() > 0)
|
|
|
|
pain.rectangle(x + 1, y - ascent() + 1, width() - 2, height() - 2, col);
|
2001-04-24 16:13:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-27 18:29:18 +00:00
|
|
|
void MathMacro::dump(std::ostream & os) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-06-25 00:06:33 +00:00
|
|
|
MathMacroTable::dump();
|
|
|
|
os << "\n macro: '" << this << "'\n";
|
2001-08-08 17:26:30 +00:00
|
|
|
os << " name: '" << name() << "'\n";
|
2001-06-25 00:06:33 +00:00
|
|
|
os << " template: '" << tmplate_ << "'\n";
|
|
|
|
os << " template: '" << *tmplate_ << "'\n";
|
|
|
|
os << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
2001-07-26 06:56:43 +00:00
|
|
|
void MathMacro::write(std::ostream & os, bool fragile) const
|
2000-02-10 17:53:36 +00:00
|
|
|
{
|
2001-08-08 17:26:30 +00:00
|
|
|
os << '\\' << name();
|
2001-06-25 00:06:33 +00:00
|
|
|
for (int i = 0; i < nargs(); ++i) {
|
|
|
|
os << '{';
|
2001-07-26 06:56:43 +00:00
|
|
|
cell(i).write(os, fragile);
|
2001-06-25 00:06:33 +00:00
|
|
|
os << '}';
|
|
|
|
}
|
|
|
|
if (nargs() == 0)
|
|
|
|
os << ' ';
|
2000-02-10 17:53:36 +00:00
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
1999-12-07 00:44:53 +00:00
|
|
|
|
2001-07-26 06:56:43 +00:00
|
|
|
void MathMacro::writeNormal(std::ostream & os) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-08-08 17:26:30 +00:00
|
|
|
os << "[macro " << name() << " ";
|
2001-04-24 16:13:38 +00:00
|
|
|
for (int i = 0; i < nargs(); ++i) {
|
2001-07-26 06:56:43 +00:00
|
|
|
cell(i).writeNormal(os);
|
2001-06-25 00:06:33 +00:00
|
|
|
os << ' ';
|
2001-04-24 16:13:38 +00:00
|
|
|
}
|
2001-06-25 00:06:33 +00:00
|
|
|
os << "] ";
|
2001-04-24 16:13:38 +00:00
|
|
|
}
|
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
|
|
|
|
bool MathMacro::idxUp(int & idx, int & pos) const
|
2001-04-24 16:13:38 +00:00
|
|
|
{
|
2001-08-03 17:10:22 +00:00
|
|
|
return MathNestInset::idxLeft(idx, pos);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
1999-12-07 00:44:53 +00:00
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
bool MathMacro::idxDown(int & idx, int & pos) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-08-03 17:10:22 +00:00
|
|
|
return MathNestInset::idxRight(idx, pos);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
bool MathMacro::idxLeft(int &, int &) 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-06-25 00:06:33 +00:00
|
|
|
bool MathMacro::idxRight(int &, int &) 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
|
|
|
{
|
2001-08-08 17:26:30 +00:00
|
|
|
if (name() == "binom")
|
2001-07-13 14:54:56 +00:00
|
|
|
features.binom = true;
|
2001-08-06 17:20:26 +00:00
|
|
|
//MathInset::validate(features);
|
2001-07-13 14:54:56 +00:00
|
|
|
}
|