-// -*- C++ -*-
/*
* File: math_macro.C
- * Purpose: Implementation of macro class for mathed
- * Author: Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
+ * Purpose: Implementation of macro class for mathed
+ * Author: Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
* 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_extern.h"
+#include "math_macrotable.h"
+#include "math_macrotemplate.h"
+#include "math_mathmlstream.h"
+#include "math_streamstr.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 "LaTeXFeatures.h"
-using std::ostream;
+
+using std::max;
+using std::auto_ptr;
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(boost::shared_ptr<MathMacroTemplate> const & t)
- : MathParInset(LM_ST_TEXT, "", LM_OT_MACRO),
- tmplate_(t)
-{
- //nargs_ = tmplate_->getNoArgs();
- int const n = tmplate_->getNoArgs();
-
- tcode_ = tmplate_->getTCode();
- for (int i = 0; i < n; ++i) {
- args_.push_back(MathMacroArgument(t->args_[i]));
- }
- //for (int i = 0; i < nargs_; ++i) {
- // MathMacroArgument * ma = new MathMacroArgument(*t->args_[i]);
- // args_.push_back(boost::shared_ptr<MathMacroArgument>(ma));
- //}
-
- idx_ = 0;
- SetName(tmplate_->GetName());
-}
+MathMacro::MathMacro(MathMacro const & m)
+ : MathNestInset(m),
+ tmplate_(m.tmplate_) // don't copy 'expanded_'!
+{}
-MathedInset * MathMacro::Clone()
+
+auto_ptr<InsetBase> MathMacro::clone() const
{
- return new MathMacro(*this);
+ return auto_ptr<InsetBase>(new MathMacro(*this));
}
-void MathMacro::Metrics()
+string MathMacro::name() const
{
- for (unsigned int i = 0; i < args_.size(); ++i)
- tmplate_->args_[i] = getArg(i);
- tmplate_->SetStyle(size());
- tmplate_->Metrics();
- width = tmplate_->Width();
- ascent = tmplate_->Ascent();
- descent = tmplate_->Descent();
+ return tmplate_->asMacroTemplate()->name();
}
-void MathMacro::draw(Painter & pain, int x, int y)
+bool MathMacro::defining() const
{
- xo(x);
- yo(y);
- Metrics();
- tmplate_->SetStyle(size());
- tmplate_->draw(pain, x, y);
+ return 0;
+ //return mathcursor && mathcursor->formula()->getInsetName() == name();
}
-bool MathMacro::setArgumentIdx(int i)
+void MathMacro::expand() const
{
- if (i >= 0 && 0 < (args_.size() - i)) {
- idx_ = i;
- return true;
- } else
- return false;
+ expanded_ = tmplate_->cell(tmplate_->cell(1).empty() ? 0 : 1);
}
-int MathMacro::getArgumentIdx() const
-{
- return idx_;
+void MathMacro::metrics(MetricsInfo & mi, Dimension & dim) const
+{
+ augmentFont(font_, "lyxtex");
+ mi_ = mi;
+
+ if (defining()) {
+
+ mathed_string_dim(font_, name(), dim_);
+
+ } else if (editing()) {
+
+ expand();
+ expanded_.metrics(mi_, dim_);
+ metricsMarkers();
+
+ dim_.wid += mathed_string_width(font_, name()) + 10;
+
+ int ww = mathed_string_width(font_, "#1: ");
+
+ for (idx_type i = 0; i < nargs(); ++i) {
+ MathArray const & c = cell(i);
+ c.metrics(mi_);
+ dim_.wid = max(dim_.wid, c.width() + ww);
+ dim_.des += max(c.ascent(), dim_.asc) + 5;
+ dim_.des += max(c.descent(), dim_.des) + 5;
+ }
+
+ } else {
+
+ expand();
+ expanded_.substitute(*this);
+ expanded_.metrics(mi_, dim_);
+
+ }
+
+ dim = dim_;
}
-int MathMacro::getMaxArgumentIdx() const
-{
- return args_.size() - 1;
-}
+void MathMacro::draw(PainterInfo & pi, int x, int y) const
+{
+ metrics(mi_, dim_);
+ LyXFont texfont;
+ augmentFont(texfont, "lyxtex");
-MathedArray & MathMacro::GetData()
-{
- return args_[idx_].GetData();
-}
+ if (defining()) {
+ drawStr(pi, texfont, x, y, name());
+ return;
+ }
+ if (editing()) {
+ int h = y - dim_.ascent() + 2 + expanded_.ascent();
+ drawStr(pi, font_, x + 3, h, name());
+
+ int const w = mathed_string_width(font_, name());
+ expanded_.draw(pi, x + w + 12, h);
+ h += expanded_.descent();
+
+ Dimension ldim;
+ mathed_string_dim(font_, "#1: ", ldim);
+
+ for (idx_type i = 0; i < nargs(); ++i) {
+ MathArray const & c = cell(i);
+ h += max(c.ascent(), ldim.asc) + 5;
+ c.draw(pi, x + ldim.wid, h);
+ char str[] = "#1:";
+ str[1] += static_cast<char>(i);
+ drawStr(pi, texfont, x + 3, h, str);
+ h += max(c.descent(), ldim.des) + 5;
+ }
+ return;
+ }
-MathedArray const & MathMacro::GetData() const
-{
- return args_[idx_].GetData();
-}
+ expanded_.draw(pi, x, y);
+}
+
+
+void MathMacro::dump() const
+{
+ MathMacroTable::dump();
+ lyxerr << "\n macro: '" << this << "'\n"
+ << " name: '" << name() << "'\n"
+ << " template: '";
+ WriteStream wi(lyxerr);
+ tmplate_->write(wi);
+ lyxerr << "'" << endl;
+}
-int MathMacro::GetColumns() const
+bool MathMacro::idxUpDown(idx_type & idx, pos_type &, bool up, int x) const
{
- return tmplate_->getMacroPar(idx_)->GetColumns();
+ pos_type pos;
+ if (up) {
+ if (!MathNestInset::idxLeft(idx, pos))
+ return false;
+ pos = cell(idx).x2pos(x);
+ return true;
+ } else {
+ if (!MathNestInset::idxRight(idx, pos))
+ return false;
+ pos = cell(idx).x2pos(x);
+ return true;
+ }
}
-void MathMacro::GetXY(int & x, int & y) const
+bool MathMacro::idxLeft(idx_type &, pos_type &) const
{
- const_cast<MathMacro*>(this)->Metrics();
- tmplate_->GetMacroXY(idx_, x, y);
+ return false;
}
-bool MathMacro::Permit(short f) const
+bool MathMacro::idxRight(idx_type &, pos_type &) const
{
- return (args_.size() > 0) ?
- tmplate_->getMacroPar(idx_)->Permit(f) :
- MathParInset::Permit(f);
+ return false;
}
-void MathMacro::SetFocus(int x, int y)
+void MathMacro::validate(LaTeXFeatures & features) const
{
- Metrics();
- tmplate_->SetMacroFocus(idx_, x, y);
+ if (name() == "binom" || name() == "mathcircumflex")
+ features.require(name());
+ //MathInset::validate(features);
}
-void MathMacro::setData(MathedArray const & a)
+void MathMacro::maple(MapleStream & os) const
{
- args_[idx_].setData(a);
+ updateExpansion();
+ ::maple(expanded_, os);
}
-MathedTextCodes MathMacro::getTCode() const
+void MathMacro::mathmlize(MathMLStream & os) const
{
- return tcode_;
+ updateExpansion();
+ ::mathmlize(expanded_, os);
}
-
-void MathMacro::Write(ostream & os, bool fragile)
+
+void MathMacro::octave(OctaveStream & os) const
{
- os << '\\' << name;
+ updateExpansion();
+ ::octave(expanded_, os);
+}
- int const n = args_.size();
-
- if (n > 0) {
- os << '{';
-
- for (int i = 0; i < n; ++i) {
- array = args_[i].GetData();
- MathParInset::Write(os, fragile);
- if (i < n - 1)
- os << "}{";
- }
- os << '}';
- } else
- os << ' ';
+
+void MathMacro::updateExpansion() const
+{
+ expand();
+ expanded_.substitute(*this);
}
-MathMacroArgument const & MathMacro::getArg(int i) const
+void MathMacro::infoize(std::ostream & os) const
{
- return args_[i];
+ os << "Macro: " << name();
}
-//boost::shared_ptr<MathMacroArgument> MathMacro::getArg(int i)
-//{
-// return args_[i];
-//}
+
+void MathMacro::infoize2(std::ostream & os) const
+{
+ os << "Macro: " << name();
+}