-// -*- 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 "math_support.h"
+#include "math_cursor.h"
+#include "math_extern.h"
+#include "math_macrotable.h"
+#include "math_macrotemplate.h"
+#include "math_mathmlstream.h"
#include "support/lstrings.h"
+#include "support/LAssert.h"
#include "debug.h"
-#include "mathed/support.h"
-#include "math_macrotemplate.h"
-#include "macro_support.h"
+#include "Painter.h"
+#include "LaTeXFeatures.h"
-using std::ostream;
-using std::endl;
-ostream & operator<<(ostream & o, MathedTextCodes mtc)
-{
- return o << int(mtc);
-}
+MathMacro::MathMacro(string const & name)
+ : MathNestInset(MathMacroTable::provide(name)->asMacroTemplate()->numargs()),
+ tmplate_(MathMacroTable::provide(name))
+{}
+
+
+MathMacro::MathMacro(MathMacro const & m)
+ : MathNestInset(m),
+ tmplate_(m.tmplate_) // don't copy 'expanded_'!
+{}
+
-MathMacro::MathMacro(MathMacroTemplate * t)
- : MathParInset(LM_ST_TEXT, "", LM_OT_MACRO), tmplate_(t)
+MathInset * MathMacro::clone() const
{
- nargs_ = tmplate_->getNoArgs();
- tcode_ = tmplate_->getTCode();
- args_.resize(nargs_);
- for (int i = 0; i < nargs_; ++i) {
- args_[i].row = 0;
- }
- idx_ = 0;
- SetName(tmplate_->GetName());
+ return new MathMacro(*this);
}
-MathMacro::MathMacro(MathMacro const & m)
- : MathParInset(LM_ST_TEXT, m.GetName(), LM_OT_MACRO),
- tmplate_(m.tmplate_), idx_(0)
-{
- nargs_ = tmplate_->getNoArgs();
- tcode_ = tmplate_->getTCode();
- SetName(tmplate_->GetName());
-
- std::vector<MacroArgumentBase>::const_iterator cit = m.args_.begin();
- std::vector<MacroArgumentBase>::const_iterator end = m.args_.end();
-
- //args_.resize(nargs_);
- //for (int i = 0; i < tmplate_->getNoArgs(); ++i) {
- // args_[i].row = m->args_[i].row;
- // args_[i].array = m->args_[i].array;
- //}
- for (; cit != end; ++cit) {
- args_.push_back(*cit);
- }
+const char * MathMacro::name() const
+{
+ return tmplate_->asMacroTemplate()->name().c_str();
}
-MathedInset * MathMacro::Clone()
+bool MathMacro::defining() const
{
- return new MathMacro(*this);
+ return 0;
+ //return mathcursor && mathcursor->formula()->getInsetName() == name();
}
-void MathMacro::Metrics()
+bool MathMacro::editing() const
{
- if (nargs_ > 0)
- tmplate_->update(this);
- tmplate_->SetStyle(size());
- tmplate_->Metrics();
- width = tmplate_->Width();
- ascent = tmplate_->Ascent();
- descent = tmplate_->Descent();
+ return mathcursor && mathcursor->isInside(this);
}
-void MathMacro::draw(Painter & pain, int x, int y)
+void MathMacro::metrics(MathMetricsInfo const & mi) const
{
- xo(x);
- yo(y);
- Metrics();
- tmplate_->update(this);
- tmplate_->SetStyle(size());
- tmplate_->draw(pain, x, y);
- for (int i = 0; i < nargs_; ++i) {
- tmplate_->GetMacroXY(i, args_[i].x, args_[i].y);
+ mi_ = mi;
+
+ if (defining()) {
+ mathed_string_dim(LM_TC_TEX, mi_, name(), ascent_, descent_, width_);
+ return;
}
+
+ if (editing()) {
+ expanded_ = tmplate_->xcell(0);
+ expanded_.metrics(mi_);
+ width_ = expanded_.width() + 4;
+ ascent_ = expanded_.ascent() + 2;
+ descent_ = expanded_.descent() + 2;
+
+ width_ += mathed_string_width(LM_TC_TEXTRM, mi_, name()) + 10;
+
+ int lasc;
+ int ldes;
+ int lwid;
+ mathed_string_dim(LM_TC_TEXTRM, mi_, "#1: ", lasc, ldes, lwid);
+
+ for (idx_type i = 0; i < nargs(); ++i) {
+ MathXArray const & c = xcell(i);
+ c.metrics(mi_);
+ width_ = std::max(width_, c.width() + lwid);
+ descent_ += std::max(c.ascent(), lasc) + 5;
+ descent_ += std::max(c.descent(), ldes) + 5;
+ }
+ return;
+ }
+
+ expanded_ = tmplate_->xcell(0);
+ expanded_.data_.substitute(*this);
+ expanded_.metrics(mi_);
+ width_ = expanded_.width();
+ ascent_ = expanded_.ascent();
+ descent_ = expanded_.descent();
}
-bool MathMacro::setArgumentIdx(int i)
+void MathMacro::draw(Painter & pain, int x, int y) const
{
- if (i >= 0 && i < nargs_) {
- idx_ = i;
- return true;
- } else
- return false;
-}
+ metrics(mi_);
+ if (defining()) {
+ drawStr(pain, LM_TC_TEX, mi_, x, y, name());
+ return;
+ }
-int MathMacro::getArgumentIdx() const
-{
- return idx_;
+ if (editing()) {
+ int h = y - ascent() + 2 + expanded_.ascent();
+ drawStr(pain, LM_TC_TEXTRM, mi_, x + 3, h, name());
+
+ int const w = mathed_string_width(LM_TC_TEXTRM, mi_, name());
+ expanded_.draw(pain, x + w + 12, h);
+ h += expanded_.descent();
+
+ int lasc;
+ int ldes;
+ int lwid;
+ mathed_string_dim(LM_TC_TEXTRM, mi_, "#1: ", lasc, ldes, lwid);
+
+ for (idx_type 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] += static_cast<char>(i);
+ drawStr(pain, LM_TC_TEX, mi_, x + 3, h, str);
+ h += std::max(c.descent(), ldes) + 5;
+ }
+ return;
+ }
+
+ expanded_.draw(pain, x, y);
}
-int MathMacro::getMaxArgumentIdx() const
-{
- return nargs_ - 1;
-}
+void MathMacro::dump() const
+{
+ MathMacroTable::dump();
+ lyxerr << "\n macro: '" << this << "'\n";
+ lyxerr << " name: '" << name() << "'\n";
+ lyxerr << " template: '";
+ WriteStream wi(lyxerr);
+ tmplate_->write(wi);
+ lyxerr << "'\n";
+}
-MathedArray & MathMacro::GetData()
-{
- return args_[idx_].array;
-}
+bool MathMacro::idxUp(idx_type & idx) const
+{
+ pos_type pos;
+ return MathNestInset::idxLeft(idx, pos);
+}
-MathedArray const & MathMacro::GetData() const
-{
- return args_[idx_].array;
-}
+bool MathMacro::idxDown(idx_type & idx) const
+{
+ pos_type pos;
+ return MathNestInset::idxRight(idx, pos);
+}
-int MathMacro::GetColumns() const
+bool MathMacro::idxLeft(idx_type &, pos_type &) const
{
- return tmplate_->getMacroPar(idx_)->GetColumns();
+ return false;
}
-void MathMacro::GetXY(int & x, int & y) const
+bool MathMacro::idxRight(idx_type &, pos_type &) const
{
- x = args_[idx_].x;
- y = args_[idx_].y;
+ return false;
}
-bool MathMacro::Permit(short f) const
+void MathMacro::validate(LaTeXFeatures & features) const
{
- return (nargs_ > 0) ?
- tmplate_->getMacroPar(idx_)->Permit(f) :
- MathParInset::Permit(f);
+ if (name() == "binom")
+ features.require("binom");
+ //MathInset::validate(features);
}
-void MathMacro::SetFocus(int x, int y)
+void MathMacro::maplize(MapleStream & os) const
{
- tmplate_->update(this);
- tmplate_->SetMacroFocus(idx_, x, y);
+ updateExpansion();
+ ::maplize(expanded_.data_, os);
}
-void MathMacro::setData(MathedArray const & a)
+void MathMacro::mathmlize(MathMLStream & os) const
{
- args_[idx_].array = a;
+ updateExpansion();
+ ::mathmlize(expanded_.data_, os);
}
+void MathMacro::octavize(OctaveStream & os) const
+{
+ updateExpansion();
+ ::octavize(expanded_.data_, os);
+}
-MathedRowSt * MathMacro::getRowSt() const
+void MathMacro::normalize(NormalStream & os) const
{
- return args_[idx_].row;
+ os << "[macro " << name() << " ";
+ for (idx_type i = 0; i < nargs(); ++i)
+ os << cell(i) << ' ';
+ os << ']';
}
-MathedTextCodes MathMacro::getTCode() const
+void MathMacro::write(WriteStream & os) const
{
- return tcode_;
+ os << '\\' << name();
+ for (idx_type i = 0; i < nargs(); ++i)
+ os << '{' << cell(i) << '}';
+ if (nargs() == 0)
+ os << ' ';
}
-
-void MathMacro::Write(ostream & os, bool fragile)
+
+void MathMacro::updateExpansion() const
{
- if (tmplate_->flags() & MMF_Exp) {
- lyxerr[Debug::MATHED] << "Expand " << tmplate_->flags()
- << ' ' << MMF_Exp << endl;
- tmplate_->update(this);
- tmplate_->Write(os, fragile);
- } else {
- if (tmplate_->flags() & MMF_Env) {
- os << "\\begin{"
- << name
- << "} ";
- } else {
- os << '\\' << name;
- }
-// if (options) {
-// file += '[';
-// file += options;
-// file += ']';
-// }
-
- if (!(tmplate_->flags() & MMF_Env) && nargs_ > 0)
- os << '{';
-
- for (int i = 0; i < nargs_; ++i) {
- array = args_[i].array;
- MathParInset::Write(os, fragile);
- if (i < nargs_ - 1)
- os << "}{";
- }
- if (tmplate_->flags() & MMF_Env) {
- os << "\\end{"
- << name
- << '}';
- } else {
- if (nargs_ > 0)
- os << '}';
- else
- os << ' ';
- }
- }
+ expanded_ = tmplate_->xcell(0);
+ expanded_.data_.substitute(*this);
}