-// -*- 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 std::ostream;
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 * t):
- MathParInset(LM_ST_TEXT, "", LM_OT_MACRO), tmplate_(t)
-{
- 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());
-}
+MathMacro::MathMacro(MathMacro const & t)
+ : MathNestInset(t), tmplate_(t.tmplate_) // don't copy 'expanded_'!
+{}
+
-MathMacro::MathMacro(MathMacro * m):
- MathParInset(LM_ST_TEXT, m->GetName(), LM_OT_MACRO)
+MathInset * MathMacro::clone() const
{
- tmplate_ = m->tmplate_;
- nargs_ = tmplate_->getNoArgs();
- tcode_ = tmplate_->getTCode();
- args_.resize(nargs_);
- idx_ = 0;
- SetName(tmplate_->GetName());
- for (int i = 0; i < tmplate_->getNoArgs(); ++i) {
- m->setArgumentIdx(i);
- args_[i].row = m->args_[i].row;
- args_[i].array = *(m->GetData());
- }
+ return new MathMacro(*this);
}
-MathMacro::~MathMacro()
+const char * MathMacro::name() const
{
+ return tmplate_->name().c_str();
}
-MathedInset * MathMacro::Clone()
+void MathMacro::metrics(MathStyles st) const
{
- return new MathMacro(this);
-}
+ 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;
+ width_ += mathed_string_width(LM_TC_TEXTRM, size_, name()) + 10;
-void MathMacro::Metrics()
-{
- if (nargs_ > 0)
- tmplate_->update(this);
- tmplate_->SetStyle(size());
- tmplate_->Metrics();
- width = tmplate_->Width();
- ascent = tmplate_->Ascent();
- descent = tmplate_->Descent();
+ int lasc;
+ int ldes;
+ int lwid;
+ mathed_string_dim(LM_TC_TEXTRM, size_, "#1: ", lasc, ldes, lwid);
+
+ 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;
+ }
}
-void MathMacro::draw(Painter & pain, int x, int y)
+void MathMacro::draw(Painter & pain, int x, int y) 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);
- }
-}
+ metrics(size());
-bool MathMacro::setArgumentIdx(int i)
-{
- if (i >= 0 && i < nargs_) {
- idx_ = i;
- return true;
- } else
- return false;
-}
+ //LColor::color col;
+ if (mathcursor && mathcursor->isInside(this)) {
-int MathMacro::getArgumentIdx() const
-{
- return idx_;
-}
+ 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 MathMacro::getMaxArgumentIdx() const
-{
- return nargs_ - 1;
-}
+ int lasc;
+ int ldes;
+ int lwid;
+ mathed_string_dim(LM_TC_TEXTRM, size_, "#1: ", lasc, ldes, lwid);
+ 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 + 3, h, str);
+ h += std::max(c.descent(), ldes) + 5;
+ }
+ //col = LColor::red;
+ } else {
+ expanded_.draw(pain, x + 3, y);
+ //col = LColor::black;
+ }
-MathedArray * MathMacro::GetData()
-{
- return &args_[idx_].array;
-}
+ //if (nargs() > 0)
+ // pain.rectangle(x + 1, y - ascent() + 1, width() - 2, height() - 2, col);
+}
-int MathMacro::GetColumns() const
+void MathMacro::dump() const
{
- return tmplate_->getMacroPar(idx_)->GetColumns();
+ MathMacroTable::dump();
+ lyxerr << "\n macro: '" << this << "'\n";
+ lyxerr << " name: '" << name() << "'\n";
+ lyxerr << " template: '" << tmplate_ << "'\n";
+ lyxerr << " template: '" << *tmplate_ << "'\n";
+ lyxerr << endl;
}
-
-void MathMacro::GetXY(int & x, int & y) const
+void MathMacro::write(std::ostream & os, bool fragile) const
{
- x = args_[idx_].x;
- y = args_[idx_].y;
+ os << '\\' << name();
+ for (unsigned int i = 0; i < nargs(); ++i) {
+ os << '{';
+ cell(i).write(os, fragile);
+ os << '}';
+ }
+ if (nargs() == 0)
+ os << ' ';
}
-bool MathMacro::Permit(short f) const
+void MathMacro::writeNormal(std::ostream & os) const
{
- return (nargs_ > 0) ?
- tmplate_->getMacroPar(idx_)->Permit(f) : MathParInset::Permit(f);
+ os << "[macro " << name() << " ";
+ for (unsigned int i = 0; i < nargs(); ++i) {
+ cell(i).writeNormal(os);
+ os << ' ';
+ }
+ os << "] ";
}
-void MathMacro::SetFocus(int x, int y)
+bool MathMacro::idxUp(unsigned int & idx, unsigned int & pos) const
{
- tmplate_->update(this);
- tmplate_->SetMacroFocus(idx_, x, y);
+ return MathNestInset::idxLeft(idx, pos);
}
-void MathMacro::setData(MathedArray * a)
+bool MathMacro::idxDown(unsigned int & idx, unsigned int & pos) const
{
- args_[idx_].array = *a;
+ return MathNestInset::idxRight(idx, pos);
}
-
-
-MathedRowSt * MathMacro::getRowSt() const
+bool MathMacro::idxLeft(unsigned int &, unsigned int &) const
{
- return args_[idx_].row;
+ return false;
}
-MathedTextCodes MathMacro::getTCode() const
+bool MathMacro::idxRight(unsigned int &, unsigned int &) const
{
- return tcode_;
+ return false;
}
-
-void MathMacro::Write(ostream & os, bool fragile)
+
+void MathMacro::validate(LaTeXFeatures & features) 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 << ' ';
- }
- }
+ if (name() == "binom")
+ features.binom = true;
+ //MathInset::validate(features);
}