-// -*- 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(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();
+MathMacro::MathMacro(MathMacro const & t)
+ : MathNestInset(t), tmplate_(t.tmplate_) // don't copy 'expanded_'!
+{}
- 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());
-}
-MathedInset * MathMacro::Clone()
+MathInset * MathMacro::clone() const
{
return new MathMacro(*this);
}
-void MathMacro::Metrics()
+const char * 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_->name().c_str();
}
-void MathMacro::draw(Painter & pain, int x, int y)
+void MathMacro::metrics(MathStyles st) const
{
- xo(x);
- yo(y);
- Metrics();
- tmplate_->SetStyle(size());
- tmplate_->draw(pain, x, y);
+ 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;
+
+ 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;
+ }
}
-bool MathMacro::setArgumentIdx(int i)
+void MathMacro::draw(Painter & pain, int x, int y) const
{
- if (i >= 0 && 0 < (args_.size() - i)) {
- idx_ = i;
- return true;
- } else
- return false;
-}
+ xo(x);
+ yo(y);
+ metrics(size());
-int MathMacro::getArgumentIdx() const
-{
- return idx_;
-}
+ //LColor::color col;
+ if (mathcursor && mathcursor->isInside(this)) {
-int MathMacro::getMaxArgumentIdx() const
-{
- return args_.size() - 1;
-}
+ 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();
-MathedArray & MathMacro::GetData()
-{
- return args_[idx_].GetData();
-}
+ 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 const & MathMacro::GetData() const
-{
- return args_[idx_].GetData();
-}
+ //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
{
- const_cast<MathMacro*>(this)->Metrics();
- tmplate_->GetMacroXY(idx_, x, 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 (args_.size() > 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
{
- Metrics();
- tmplate_->SetMacroFocus(idx_, x, y);
+ return MathNestInset::idxLeft(idx, pos);
}
-void MathMacro::setData(MathedArray const & a)
+bool MathMacro::idxDown(unsigned int & idx, unsigned int & pos) const
{
- args_[idx_].setData(a);
+ return MathNestInset::idxRight(idx, pos);
}
-MathedTextCodes MathMacro::getTCode() const
+bool MathMacro::idxLeft(unsigned int &, unsigned int &) const
{
- return tcode_;
+ return false;
}
-
-void MathMacro::Write(ostream & os, bool fragile)
-{
- os << '\\' << name;
- 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 << ' ';
+bool MathMacro::idxRight(unsigned int &, unsigned int &) const
+{
+ return false;
}
-MathMacroArgument const & MathMacro::getArg(int i) const
+void MathMacro::validate(LaTeXFeatures & features) const
{
- return args_[i];
+ if (name() == "binom")
+ features.binom = true;
+ //MathInset::validate(features);
}
-//boost::shared_ptr<MathMacroArgument> MathMacro::getArg(int i)
-//{
-// return args_[i];
-//}
-