-// -*- C++ -*-
/*
* File: math_macro.C
* Purpose: Implementation of macro class for mathed
* Created: November 1996
* Description: WYSIWYG math macros
*
- * Dependencies: Mathed
+ * Dependencies: Math
*
* Copyright: 1996, 1997 Alejandro Aguilar Sierra
*
- * Version: 0.2, Mathed & Lyx project.
+ * Version: 0.2, Math & Lyx project.
*
* This code is under the GNU General Public Licence version 2 or later.
*/
-#include <config.h>
-
#ifdef __GNUG__
#pragma implementation
#endif
#include "math_macro.h"
#include "array.h"
-#include "math_iter.h"
-#include "math_inset.h"
-#include "math_accentinset.h"
-#include "math_deliminset.h"
-#include "math_fracinset.h"
-#include "math_rowst.h"
#include "support/lstrings.h"
+#include "support/LAssert.h"
#include "debug.h"
#include "mathed/support.h"
+#include "mathed/math_cursor.h"
+#include "math_macrotable.h"
#include "math_macrotemplate.h"
-#include "macro_support.h"
#include "Painter.h"
+#include "LaTeXFeatures.h"
-using namespace std;
+using std::endl;
-ostream & operator<<(ostream & o, MathedTextCodes mtc)
-{
- return o << int(mtc);
-}
+MathMacro::MathMacro(MathMacroTemplate const & t)
+ : MathNestInset(t.numargs()), tmplate_(&t)
+{}
-MathMacro::MathMacro(MathMacroTemplate const & t)
- : MathParInset(LM_ST_TEXT, t.GetName(), LM_OT_MACRO),
- tmplate_(const_cast<MathMacroTemplate *>(&t)),
- args_(t.nargs()),
- idx_(-1)
-{
- array = tmplate_->GetData();
- for (int i = 0; i < nargs(); ++i)
- args_[i].reset(new MathParInset);
-}
+MathMacro::MathMacro(MathMacro const & t)
+ : MathNestInset(t), tmplate_(t.tmplate_) // don't copy 'expanded_'!
+{}
+
-MathedInset * MathMacro::Clone()
+MathInset * MathMacro::clone() const
{
return new MathMacro(*this);
}
-void MathMacro::expand()
+const char * MathMacro::name() const
{
- expanded_.reset(static_cast<MathParInset *>(tmplate_->Clone()));
- expanded_->substitute(this);
+ return tmplate_->name().c_str();
}
-
-MathParInset const * MathMacro::arg(int i) const
-{
- if (i < 0 || i >= nargs()) {
- lyxerr << "Illegal index " << i << " max: " << nargs() << endl;
- lyx::Assert(0);
- return 0;
- }
-
- return i >= 0 ? args_[i].get() : static_cast<MathParInset const *>(this);
-}
-
-MathParInset * MathMacro::arg(int i)
+void MathMacro::metrics(MathStyles st) const
{
- if (i < 0 || i >= nargs()) {
- lyxerr << "Illegal index " << i << " max: " << nargs() << endl;
- lyx::Assert(0);
- return 0;
- }
+ if (mathcursor && mathcursor->isInside(this)) {
+ expanded_ = tmplate_->xcell(0);
+ expanded_.metrics(st);
+ size_ = st;
+ width_ = expanded_.width() + 4;
+ ascent_ = expanded_.ascent() + 2;
+ descent_ = expanded_.descent() + 2;
- return i >= 0 ? args_[i].get() : static_cast<MathParInset *>(this);
-}
+ width_ += mathed_string_width(LM_TC_TEXTRM, size_, name()) + 10;
+ int lasc;
+ int ldes;
+ int lwid;
+ mathed_string_dim(LM_TC_TEXTRM, size_, "#1: ", lasc, ldes, lwid);
-MathMacroTemplate * MathMacro::tmplate() const
-{
- return const_cast<MathMacroTemplate *>(tmplate_);
+ for (unsigned int i = 0; i < nargs(); ++i) {
+ MathXArray const & c = xcell(i);
+ c.metrics(st);
+ width_ = std::max(width_, c.width() + lwid);
+ descent_ += std::max(c.ascent(), lasc) + 5;
+ descent_ += std::max(c.descent(), ldes) + 5;
+ }
+ } else {
+ expanded_ = tmplate_->xcell(0);
+ expanded_.data_.substitute(*this);
+ expanded_.metrics(st);
+ size_ = st;
+ width_ = expanded_.width() + 6;
+ ascent_ = expanded_.ascent() + 3;
+ descent_ = expanded_.descent() + 3;
+ }
}
-extern bool is_mathcursor_inside(MathParInset *);
-
-
-void MathMacro::Metrics()
+void MathMacro::draw(Painter & pain, int x, int y) const
{
+ xo(x);
+ yo(y);
- if (is_mathcursor_inside(this)) {
+ metrics(size());
- tmplate_->Metrics();
- width = tmplate_->Width() + 4;
- ascent = tmplate_->Ascent() + 2;
- descent = tmplate_->Descent() + 2;
+ //LColor::color col;
- width += mathed_string_width(LM_TC_TEXTRM, size(), GetName()) + 10;
+ if (mathcursor && mathcursor->isInside(this)) {
- for (int i = 0; i < nargs(); ++i) {
- MathParInset * p = arg(i);
- p->Metrics();
- if (p->Width() + 30 > width)
- width = p->Width() + 30;
- descent += p->Height() + 10;
- }
- } else {
- expand();
- expanded_->Metrics();
- width = expanded_->Width() + 4;
- ascent = expanded_->Ascent() + 2;
- descent = expanded_->Descent() + 2;
+ int h = y - ascent() + 2 + expanded_.ascent();
+ drawStr(pain, LM_TC_TEXTRM, size(), x + 3, h, name());
- }
-}
+ int const w = mathed_string_width(LM_TC_TEXTRM, size(), name());
+ expanded_.draw(pain, x + w + 12, h);
+ h += expanded_.descent();
+ int lasc;
+ int ldes;
+ int lwid;
+ mathed_string_dim(LM_TC_TEXTRM, size_, "#1: ", lasc, ldes, lwid);
-void MathMacro::draw(Painter & pain, int x, int y)
-{
- LColor::color col;
-
- if (is_mathcursor_inside(this)) {
- int h = y + Descent() - 2;
- for (int i = nargs() - 1; i >= 0; --i) {
- MathParInset * p = arg(i);
- h -= p->Descent() + 5;
- p->draw(pain, x + 30, h);
+ for (unsigned int i = 0; i < nargs(); ++i) {
+ MathXArray const & c = xcell(i);
+ h += std::max(c.ascent(), lasc) + 5;
+ c.draw(pain, x + lwid, h);
char str[] = "#1:";
str[1] += i;
- drawStr(pain, LM_TC_TEX, size(), x + 1, h, str);
- h -= p->Ascent() + 5;
+ drawStr(pain, LM_TC_TEX, size(), x + 3, h, str);
+ h += std::max(c.descent(), ldes) + 5;
}
-
- h -= tmplate_->Descent();
- int w = mathed_string_width(LM_TC_TEXTRM, size(), GetName());
- drawStr(pain, LM_TC_TEXTRM, size(), x + 2, h, GetName());
- tmplate_->draw(pain, x + w + 12, h);
-
- col = LColor::red;
+ //col = LColor::red;
} else {
- expanded_->draw(pain, x + 2, y - 1);
- col = LColor::black;
+ expanded_.draw(pain, x + 3, y);
+ //col = LColor::black;
}
- int w = Width();
- int a = Ascent();
- int h = Height();
- pain.rectangle(x, y - a, w, h, col);
+ //if (nargs() > 0)
+ // pain.rectangle(x + 1, y - ascent() + 1, width() - 2, height() - 2, col);
}
-bool MathMacro::setArgumentIdx(int i)
+void MathMacro::dump() const
{
- if (i >= 0 && 0 < (nargs() - i)) {
- idx_ = i;
- return true;
- } else
- return false;
- idx_ = i;
- return true;
-}
-
-
-int MathMacro::getArgumentIdx() const
-{
- //lyxerr << "MathMacro::getArgumentIdx: res: " << idx_ << endl;
- return idx_;
+ MathMacroTable::dump();
+ lyxerr << "\n macro: '" << this << "'\n";
+ lyxerr << " name: '" << name() << "'\n";
+ lyxerr << " template: '" << tmplate_ << "'\n";
+ lyxerr << " template: '" << *tmplate_ << "'\n";
+ lyxerr << endl;
}
-
-int MathMacro::getMaxArgumentIdx() const
-{
- return nargs() - 1;
-}
-
-
-
-int MathMacro::nargs() const
-{
- return args_.size();
-}
-
-
-MathedArray & MathMacro::GetData()
-{
- //lyxerr << "MathMacro::GetData: " << *this << endl;
- return idx_ >= 0 ? arg(idx_)->GetData() : MathParInset::GetData();
-}
-
-
-MathedArray const & MathMacro::GetData() const
-{
- //lyxerr << "MathMacro::GetData: " << *this << endl;
- return idx_ >= 0 ? arg(idx_)->GetData() : MathParInset::GetData();
-}
-
-
-int MathMacro::GetColumns() const
+void MathMacro::write(std::ostream & os, bool fragile) const
{
- return idx_ >= 0 ? arg(idx_)->GetColumns() : MathParInset::GetColumns();
+ os << '\\' << name();
+ for (unsigned int i = 0; i < nargs(); ++i) {
+ os << '{';
+ cell(i).write(os, fragile);
+ os << '}';
+ }
+ if (nargs() == 0)
+ os << ' ';
}
-void MathMacro::GetXY(int & x, int & y) const
+void MathMacro::writeNormal(std::ostream & os) const
{
- if (idx_ >= 0)
- arg(idx_)->GetXY(x, y);
- else
- MathParInset::GetXY(x, y);
+ os << "[macro " << name() << " ";
+ for (unsigned int i = 0; i < nargs(); ++i) {
+ cell(i).writeNormal(os);
+ os << ' ';
+ }
+ os << "] ";
}
-bool MathMacro::Permit(short f) const
+bool MathMacro::idxUp(unsigned int & idx, unsigned int & pos) const
{
- return idx_ >= 0 ? arg(idx_)->Permit(f) : MathParInset::Permit(f);
+ return MathNestInset::idxLeft(idx, pos);
}
-void MathMacro::SetFocus(int x, int y)
-{
- idx_ = -1;
- for (int i = 0; i < nargs(); ++i) {
- if (arg(i)->Inside(x, y)) {
- idx_ = i;
- break;
- }
- }
-}
-
-void MathMacro::setData(MathedArray const & a, int i)
+bool MathMacro::idxDown(unsigned int & idx, unsigned int & pos) const
{
- arg(i)->setData(a);
+ return MathNestInset::idxRight(idx, pos);
}
-void MathMacro::setData(MathedArray const & a)
+bool MathMacro::idxLeft(unsigned int &, unsigned int &) const
{
- if (idx_ >= 0)
- arg(idx_)->setData(a);
- else
- array = a;
+ return false;
}
-MathedTextCodes MathMacro::getTCode() const
+bool MathMacro::idxRight(unsigned int &, unsigned int &) const
{
- return nargs() ? LM_TC_ACTIVE_INSET : LM_TC_INSET;
- //return LM_TC_INSET;
-}
-
-void MathMacro::dump(ostream & os) const
-{
- os << "\n macro: '" << this << "'\n";
- os << " name: '" << name << "'\n";
- os << " idx: '" << idx_ << "'\n";
- os << " data: '" << array << "'\n";
- os << " nargs: '" << nargs() << "'\n";
- for (int i = 0; i < nargs(); ++i)
- os << " " << arg(i) << ": " << arg(i)->GetData() << endl;
- os << endl;
+ return false;
}
-void MathMacro::Write(ostream & os, bool fragile)
+
+void MathMacro::validate(LaTeXFeatures & features) const
{
- os << '\\' << name;
- for (int i = 0; i < nargs(); ++i) {
- os << '{';
- arg(i)->Write(os, fragile);
- os << '}';
- }
- if (nargs() == 0)
- os << ' ';
+ if (name() == "binom")
+ features.binom = true;
+ //MathInset::validate(features);
}