]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_macro.C
the DocIterator stuff
[lyx.git] / src / mathed / math_macro.C
index d157513d0d8bc5569081d47809c04d1b0bacffa5..eba161e677a7545d587b2433ea7610b7553b0d2e 100644 (file)
-// -*- C++ -*-
-/*
- *  File:        math_macro.C
- *  Purpose:     Implementation of macro class for mathed 
- *  Author:      Alejandro Aguilar Sierra <asierra@servidor.unam.mx> 
- *  Created:     November 1996
- *  Description: WYSIWYG math macros
+/**
+ * \file math_macro.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
  *
- *  Dependencies: Mathed
+ * \author Alejandro Aguilar Sierra
+ * \author André Pönitz
  *
- *  Copyright: 1996, 1997 Alejandro Aguilar Sierra
- *
- *  Version: 0.2, Mathed & Lyx project.
- *
- *  This code is under the GNU General Public Licence version 2 or later.
+ * Full author contact details are available in file CREDITS.
  */
 
 #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 "debug.h"
-#include "mathed/support.h"
+#include "math_support.h"
+#include "math_extern.h"
+#include "math_macrotable.h"
 #include "math_macrotemplate.h"
-#include "macro_support.h"
+#include "math_mathmlstream.h"
+#include "cursor.h"
+#include "debug.h"
+#include "LaTeXFeatures.h"
+
 
-using std::ostream;
+using std::string;
+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(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 & m)
+       : MathNestInset(m),
+               tmplate_(m.tmplate_) // don't copy 'expanded_'!
+{}
 
 
-MathMacro::MathMacro(MathMacro * m): 
-       MathParInset(LM_ST_TEXT, m->GetName(), LM_OT_MACRO)
+
+auto_ptr<InsetBase> 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 auto_ptr<InsetBase>(new MathMacro(*this));
 }
 
 
-MathMacro::~MathMacro()
+string MathMacro::name() const
 {
+       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_->cell(tmplate_->cell(1).empty() ? 0 : 1);
 }
 
 
-void MathMacro::draw(Painter & pain, int x, int y)
+void MathMacro::metrics(MetricsInfo & mi, Dimension & dim) 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(), dim);
+
+       } else if (editing(mi.base.bv)) {
+
+               expand();
+               expanded_.metrics(mi_, dim);
+               metricsMarkers(dim);
+
+               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;
 }
 
 
-bool MathMacro::setArgumentIdx(int i)
+void MathMacro::draw(PainterInfo & pi, int x, int y) const
 {
-       if (i >= 0 && i < nargs_) {
-               idx_ = i;
-               return true;
-       } else
-               return false;
-}
+       metrics(mi_, dim_);
+
+       LyXFont texfont;
+       augmentFont(texfont, "lyxtex");
 
+       if (defining()) {
+               drawStr(pi, texfont, x, y, name());
+               return;
+       }
+
+       if (editing(pi.base.bv)) {
+               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;
+       }
 
-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"
+              << " name: '" << name() << "'\n"
+              << " template: '";
+       WriteStream wi(lyxerr);
+       tmplate_->write(wi);
+       lyxerr << "'" << endl;
+}
 
 
-MathedArray * MathMacro::GetData() 
-{ 
-       return &args_[idx_].array; 
-} 
+bool MathMacro::idxUpDown(LCursor & cur, bool up) const
+{
+       if (up) {
+               if (!MathNestInset::idxLeft(cur))
+                       return false;
+       } else {
+               if (!MathNestInset::idxRight(cur))
+                       return false;
+       }
+       cur.pos() = cur.cell().x2pos(cur.x_target());
+       return true;
+}
 
 
-int MathMacro::GetColumns() const
+bool MathMacro::idxLeft(LCursor &) const
 {
-       return tmplate_->getMacroPar(idx_)->GetColumns();
+       return false;
 }
 
 
-void MathMacro::GetXY(int & x, int & y) const
+bool MathMacro::idxRight(LCursor &) 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::maple(MapleStream & os) const
 {
-       tmplate_->update(this);
-       tmplate_->SetMacroFocus(idx_, x, y);
+       updateExpansion();
+       ::maple(expanded_, os);
 }
 
 
-void MathMacro::setData(MathedArray * a)
+void MathMacro::mathmlize(MathMLStream & os) const
 {
-       args_[idx_].array = *a;
+       updateExpansion();
+       ::mathmlize(expanded_, os);
 }
 
 
+void MathMacro::octave(OctaveStream & os) const
+{
+       updateExpansion();
+       ::octave(expanded_, os);
+}
 
 
-MathedRowSt * MathMacro::getRowSt() const
+void MathMacro::updateExpansion() const
 {
-       return args_[idx_].row;
+       expand();
+       expanded_.substitute(*this);
 }
 
 
-MathedTextCodes MathMacro::getTCode() const
+void MathMacro::infoize(std::ostream & os) const
 {
-       return tcode_;
+       os << "Macro: " << name();
 }
-       
 
-void MathMacro::Write(ostream & os, bool fragile)
+
+void MathMacro::infoize2(std::ostream & os) 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 << ' ';
-               }
-       }
+       os << "Macro: " << name();
 }