]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_macrotemplate.C
make \newcommand{\bb}[1]{\mathbf{#1}} work for read/write/display.
[lyx.git] / src / mathed / math_macrotemplate.C
index c75df81ad88e8043559bcbdbe96126921d0c1a96..3fe31bc7b7477df0fb072a87e6973c080ebf6d9b 100644 (file)
-#include <config.h>
-
 #ifdef __GNUG__
 #pragma implementation
 #endif
 
 #include "math_macrotemplate.h"
-#include "math_macro.h"
-#include "macro_support.h"
-#include "support/LOstream.h"
-#include "support/LAssert.h"
-
-using std::ostream;
-
-
-MathMacroTemplate::MathMacroTemplate(string const & nm, int na):
-       MathParInset(LM_ST_TEXT, nm, LM_OT_MACRO), 
-       edit_(false),
-       nargs_(na)
-{
-       if (nargs_ > 0) {
-               tcode_ = LM_TC_ACTIVE_INSET;
-               for (int i = 0; i < nargs_; ++i) {
-                       args_.push_back(MathMacroArgument(i + 1));
-               }
-               //for (int i = 0; i < nargs_; ++i) {
-               //      MathMacroArgument * ma = new MathMacroArgument(i + 1);
-               //      args_.push_back(boost::shared_ptr<MathMacroArgument>(ma));
-               //}
-       } else {
-               tcode_ = LM_TC_INSET;
-               // Here is  nargs != args_.size()
-               //args = 0;
-       }
-}
-
-
-void  MathMacroTemplate::setTCode(MathedTextCodes t)
-{
-       tcode_ = t;
-}
+#include "math_mathmlstream.h"
+#include "Painter.h"
+#include "debug.h"
 
 
-MathedTextCodes MathMacroTemplate::getTCode() const
-{
-       return tcode_;
-}
-
-
-int MathMacroTemplate::getNoArgs() const
-{
-       return nargs_;
-}
+MathMacroTemplate::MathMacroTemplate()
+       : MathNestInset(1), numargs_(0), name_()
+{}
 
 
-void MathMacroTemplate::setEditMode(bool ed)
+MathMacroTemplate::MathMacroTemplate(string const & nm, int numargs)
+       : MathNestInset(1), numargs_(numargs), name_(nm)
 {
-       if (ed) {
-               edit_ = true;
-               for (int i = 0; i < nargs_; ++i) {
-                       args_[i].setExpand(false);
-               }
-       } else {
-               edit_ = false;
-               for (int i = 0; i < nargs_; ++i) {
-                       args_[i].setExpand(true);
-               }
-       }
+       if (numargs_ > 9)
+               lyxerr << "MathMacroTemplate::MathMacroTemplate: wrong # of arguments: "
+                       << numargs_ << std::endl;
 }
 
 
-void MathMacroTemplate::draw(Painter & pain, int x, int y)
+MathInset * MathMacroTemplate::clone() const
 {
-       int x2;
-       int y2;
-       bool expnd = (nargs_ > 0) ? args_[0].getExpand() : false;
-       if (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);
-       }
+       //lyxerr << "cloning MacroTemplate!\n";
+       return new MathMacroTemplate(*this);
 }
 
 
-void MathMacroTemplate::Metrics()
+int MathMacroTemplate::numargs() const
 {
-       bool const expnd = (nargs_ > 0) ? args_[0].getExpand() : false;
-    
-       if (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 numargs_;
 }
 
 
-void MathMacroTemplate::update(MathMacro const & macro)
+void MathMacroTemplate::numargs(int numargs)
 {
-       for (int i = 0; i < nargs_; ++i) {
-               args_[i] = macro.getArg(i);
-       }
+       numargs_ = numargs;
 }
 
 
-void MathMacroTemplate::WriteDef(ostream & os, bool fragile)
+string const & MathMacroTemplate::name() 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";
+       return name_;
 }
 
 
-void MathMacroTemplate::GetMacroXY(int i, int & x, int & y) const
+void MathMacroTemplate::metrics(MathMetricsInfo const & mi) const
 {
-       args_[i].GetXY(x, y);
+       xcell(0).metrics(mi);
+       width_   = xcell(0).width() + 4;
+       ascent_  = xcell(0).ascent() + 2;
+       descent_ = xcell(0).descent() + 2;
 }
 
 
-MathParInset * MathMacroTemplate::getMacroPar(int i) const
+void MathMacroTemplate::draw(Painter & pain, int x, int y) const
 {
-       if (i >= 0 && i < nargs_) {
-               MathParInset * p = const_cast<MathParInset *>
-                       (static_cast<MathParInset const *>(&args_[i]));
-               Assert(p);
-               return p;
-       } else 
-               return 0;
+       xcell(0).draw(pain, x + 2, y + 1);
+       pain.rectangle(x, y - ascent(), width(), height(), LColor::blue);
 }
 
 
-void MathMacroTemplate::SetMacroFocus(int &idx, int x, int y)
+void MathMacroTemplate::write(WriteStream & os) const
 {
-       for (int i = 0; i < nargs_; ++i) {
-               if (args_[i].Inside(x, y)) {
-                       idx = i;
-                       break;
-               }
-       }
+       os << "\n\\newcommand{\\" << name_.c_str() << '}';
+       if (numargs_ > 0)
+               os << '[' << numargs_ << ']';
+       os << '{' << cell(0) << "}\n";
 }