]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_macrotemplate.C
rename commandtags.h to lfuns.h and renumber/cleanup. Rebuild the tree !
[lyx.git] / src / mathed / math_macrotemplate.C
index 21e00835755a67e9dac73eb00f51162a1116f15a..f35dace5bfdc9942314b2e06b4090270b060f1c2 100644 (file)
-#include <config.h>
 
 #include "math_macrotemplate.h"
-#include "math_macro.h"
-#include "macro_support.h"
+#include "math_mathmlstream.h"
+#include "math_parser.h"
+#include "frontends/Painter.h"
+#include "debug.h"
 
 
-void  MathMacroTemplate::setTCode(MathedTextCodes t)
-{
-       tcode = t;
-}
+MathMacroTemplate::MathMacroTemplate()
+       : MathNestInset(2), numargs_(0), name_()
+{}
 
 
-MathedTextCodes MathMacroTemplate::getTCode() const
+MathMacroTemplate::MathMacroTemplate(string const & nm, int numargs,
+               MathArray const & ar1, MathArray const & ar2)
+       : MathNestInset(2), numargs_(numargs), name_(nm)
 {
-       return tcode;
+       if (numargs_ > 9)
+               lyxerr << "MathMacroTemplate::MathMacroTemplate: wrong # of arguments: "
+                       << numargs_ << std::endl;
+       cell(0) = ar1;
+       cell(1) = ar2;
 }
 
 
-int MathMacroTemplate::getNoArgs() const
-{
-       return nargs;
-}
-
 
-MathMacroTemplate::MathMacroTemplate(string const & nm, int na, int flg):
-       MathParInset(LM_ST_TEXT, nm, LM_OT_MACRO), 
-       flags(flg), nargs(na)
+MathMacroTemplate::MathMacroTemplate(std::istream & is)
+       : MathNestInset(2), numargs_(0), name_()
 {
-       if (nargs > 0) {
-               tcode = LM_TC_ACTIVE_INSET;
-               args_.resize(nargs);
-               for (int i = 0; i < nargs; ++i) {
-                       args_[i].setNumber(i + 1);
-               }
-       } else {
-               tcode = LM_TC_INSET;
-               // Here is  nargs != args_.size()
-               //args = 0;
+       MathArray ar;
+       mathed_parse_cell(ar, is);
+       if (ar.size() != 1 || !ar[0]->asMacroTemplate()) {
+               lyxerr << "cannot read macro from '" << ar << "'\n";
+               return;
        }
+       operator=( *(ar[0]->asMacroTemplate()) );
 }
 
 
-MathMacroTemplate::~MathMacroTemplate()
+MathInset * MathMacroTemplate::clone() const
 {
-       // prevent to delete already deleted objects
-       for (int i = 0; i < nargs; ++i) {
-               args_[i].SetData(0);
-       }
+       //lyxerr << "cloning MacroTemplate!\n";
+       return new MathMacroTemplate(*this);
 }
 
 
-void MathMacroTemplate::setEditMode(bool ed)
+int MathMacroTemplate::numargs() const
 {
-       if (ed) {
-               flags |= MMF_Edit;
-               for (int i = 0; i < nargs; ++i) {
-                       args_[i].setExpand(false);
-               }
-       }
-       else {
-               flags &= ~MMF_Edit;
-               for (int i = 0; i < nargs; ++i) {
-                       args_[i].setExpand(true);
-               }
-       }
+       return numargs_;
 }
 
 
-void MathMacroTemplate::draw(Painter & pain, int x, int y)
+void MathMacroTemplate::numargs(int numargs)
 {
-       int x2, y2;
-       bool expnd = (nargs > 0) ? args_[0].getExpand(): false;
-       if (flags & MMF_Edit) {
-               for (int i = 0; i < nargs; ++i) {
-                       args_[i].setExpand(false);
-               }
-               x2 = x; y2 = y;
-       } else {
-               for (int i = 0; i < nargs; ++i) {
-                       args_[i].setExpand(true);
-               }
-               x2 = xo; y2 = yo;
-       }
-       MathParInset::draw(pain, x, y);
-       xo = x2; yo = y2;
-
-       for (int i = 0; i < nargs; ++i) {
-               args_[i].setExpand(expnd);
-       }
+       numargs_ = numargs;
 }
 
 
-void MathMacroTemplate::Metrics()
+string MathMacroTemplate::name() const
 {
-       bool expnd = (nargs > 0) ? args_[0].getExpand(): false;
-    
-       if (flags & MMF_Edit) {
-               for (int i = 0; i < nargs; ++i) {
-                       args_[i].setExpand(false);
-               }
-       } else {
-               for (int i = 0; i < nargs; ++i) {
-                       args_[i].setExpand(true);
-               }
-       }
-       MathParInset::Metrics();
-    
-       for (int i = 0; i < nargs; ++i) {
-               args_[i].setExpand(expnd);
-       }
+       return name_;
 }
 
 
-void MathMacroTemplate::update(MathMacro * macro)
+void MathMacroTemplate::metrics(MathMetricsInfo & mi) const
 {
-       int idx = (macro) ? macro->getArgumentIdx() : 0;
-       for (int i = 0; i < nargs; ++i) {
-               if (macro) {
-                       macro->setArgumentIdx(i);
-                       args_[i].SetData(macro->GetData());
-                       MathedRowSt * row = macro->getRowSt();
-                       args_[i].setRowSt(row);
-               }
-       }       
-       if (macro)
-               macro->setArgumentIdx(idx);
+       cell(0).metrics(mi);
+       cell(1).metrics(mi);
+       dim_.w = cell(0).width() + cell(1).width() + 10;
+       dim_.a = std::max(cell(0).ascent(),  cell(1).ascent())  + 2;
+       dim_.d = std::max(cell(0).descent(), cell(1).descent()) + 2;
 }
 
 
-void MathMacroTemplate::WriteDef(ostream & os, bool fragile)
+void MathMacroTemplate::draw(MathPainterInfo & pi, int x, int y) const
 {
-       os << "\n\\newcommand{\\" << name << "}";
-
-       if (nargs > 0 ) 
-               os << "[" << nargs << "]";
-
-       os << "{";
-
-       for (int i = 0; i < nargs; ++i) {
-               args_[i].setExpand(false);
-       }        
-       Write(os, fragile); 
-       os << "}\n";
+       int const w0 = cell(0).width();
+       int const w1 = cell(1).width();
+       cell(0).draw(pi, x + 2, y + 1);
+       pi.pain.rectangle(x, y - ascent() + 1, w0 + 4, height(),
+                       LColor::blue);
+       cell(1).draw(pi, x + 8 + w0, y + 1);
+       pi.pain.rectangle(x + w0 + 6 , y - ascent() + 1, w1 + 4,
+                       height(), LColor::blue);
 }
 
 
-void MathMacroTemplate::setArgument(MathedArray * a, int i)
-{
-       args_[i].SetData(a);
-}
-
 
-void MathMacroTemplate::GetMacroXY(int i, int & x, int & y) const
+void MathMacroTemplate::write(WriteStream & os) const
 {
-       args_[i].GetXY(x, y);
-}
-
-
-MathParInset * MathMacroTemplate::getMacroPar(int i) const
-{
-       if (i >= 0 && i < nargs) 
-               return const_cast<MathParInset *>
-                       (static_cast<MathParInset const *>(&args_[i]));
-       else 
-               return 0;
-}
-
-
-void MathMacroTemplate::SetMacroFocus(int &idx, int x, int y)
-{
-       for (int i = 0; i < nargs; ++i) {
-               if (args_[i].Inside(x, y)) {
-                       idx = i;
-                       break;
-               }
+       if (os.latex()) {
+               os << "\n\\newcommand{\\" << name_.c_str() << '}';
+               if (numargs_ > 0)
+                       os << '[' << numargs_ << ']';
+               os << '{' << cell(0) << "}\n";
+       } else {
+               // writing .lyx
+               os << "\n\\newcommand{\\" << name_.c_str() << '}';
+               if (numargs_ > 0)
+                       os << '[' << numargs_ << ']';
+               os << '{' << cell(0) << '}';
+               // write special .tex export only if necessary
+               if (!cell(1).empty())
+                       os << "\n{" << cell(1) << '}';
        }
 }