]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_macro.C
remove unneeded member
[lyx.git] / src / mathed / math_macro.C
index 13ac9b5531d280a7815eb189d33038255a9d8619..9983cf5f78b523af9c7425a1a17867bf75460d45 100644 (file)
-// -*- 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::endl;
 
-ostream & operator<<(ostream & o, MathedTextCodes mtc)
-{
-       return o << int(mtc);
-}
+using std::max;
+
+
+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 * m): 
-       MathParInset(LM_ST_TEXT, m->GetName(), LM_OT_MACRO)
+string const & MathMacro::name() 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 tmplate_->asMacroTemplate()->name();
 }
 
 
-MathedInset * MathMacro::Clone()
+bool MathMacro::defining() const
 {
-       return new MathMacro(this);
+       return 0;
+       //return mathcursor && mathcursor->formula()->getInsetName() == name();
 }
 
 
-void MathMacro::Metrics()
+void MathMacro::expand() const
 {
-       if (nargs_ > 0)
-               tmplate_->update(this);
-       tmplate_->SetStyle(size());
-       tmplate_->Metrics();
-       width = tmplate_->Width();
-       ascent = tmplate_->Ascent();
-       descent = tmplate_->Descent();
+       expanded_ = tmplate_->xcell(tmplate_->cell(1).empty() ? 0 : 1);
 }
 
 
-void MathMacro::draw(Painter & pain, int x, int y)
+void MathMacro::metrics(MathMetricsInfo & 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);
+       augmentFont(font_, "lyxtex");
+       mi_ = mi;
+
+       if (defining()) {
+               mathed_string_dim(font_, name(), ascent_, descent_, width_);
+               return;
        }
+
+       if (editing()) {
+               expand();
+               expanded_.metrics(mi_);
+               width_   = expanded_.width()   + 4;
+               ascent_  = expanded_.ascent()  + 2;
+               descent_ = expanded_.descent() + 2;
+
+               width_ +=  mathed_string_width(font_, name()) + 10;
+
+               int lasc;
+               int ldes;
+               int lwid;
+               mathed_string_dim(font_, "#1: ", lasc, ldes, lwid);
+
+               for (idx_type i = 0; i < nargs(); ++i) {
+                       MathXArray const & c = xcell(i);
+                       c.metrics(mi_);
+                       width_    = max(width_, c.width() + lwid);
+                       descent_ += max(c.ascent(),  lasc) + 5;
+                       descent_ += max(c.descent(), ldes) + 5;
+               }
+               return;
+       }
+
+       expand();
+       expanded_.data().substitute(*this);
+       expanded_.metrics(mi_);
+       width_   = expanded_.width();
+       ascent_  = expanded_.ascent();
+       descent_ = expanded_.descent();
 }
 
 
-bool MathMacro::setArgumentIdx(int i)
+void MathMacro::draw(MathPainterInfo & pi, int x, int y) const
 {
-       if (i >= 0 && i < nargs_) {
-               idx_ = i;
-               return true;
-       } else
-               return false;
-}
+       metrics(mi_);
+
+       LyXFont texfont;
+       augmentFont(texfont, "lyxtex");
+
+       if (defining()) {
+               drawStr(pi, texfont, x, y, name());
+               return;
+       }
 
+       if (editing()) {
+               int h = y - 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();
+
+               int lasc;
+               int ldes;
+               int lwid;
+               mathed_string_dim(font_, "#1: ", lasc, ldes, lwid);
+
+               for (idx_type i = 0; i < nargs(); ++i) {
+                       MathXArray const & c = xcell(i);
+                       h += max(c.ascent(), lasc) + 5;
+                       c.draw(pi, x + lwid, h);
+                       char str[] = "#1:";
+                       str[1] += static_cast<char>(i);
+                       drawStr(pi, texfont, x + 3, h, str);
+                       h += max(c.descent(), ldes) + 5;
+               }
+               return;
+       }
 
-int MathMacro::getArgumentIdx() const 
-{ 
-       return idx_;
+       expanded_.draw(pi, 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::idxUpDown(idx_type & idx, bool up) const
+{
+       pos_type pos;
+       return
+               up ? MathNestInset::idxLeft(idx, pos) : 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" || name() == "mathcircumflex")
+               features.require(name());
+       //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 << ' ';
-               }
-       }
+       expand();
+       expanded_.data().substitute(*this);
 }