]> git.lyx.org Git - features.git/commitdiff
more mathed cleanup
authorLars Gullik Bjønnes <larsbj@gullik.org>
Tue, 13 Feb 2001 17:08:51 +0000 (17:08 +0000)
committerLars Gullik Bjønnes <larsbj@gullik.org>
Tue, 13 Feb 2001 17:08:51 +0000 (17:08 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@1499 a592a061-630c-0410-9148-cb99ea01b6c8

15 files changed:
src/mathed/ChangeLog
src/mathed/Makefile.am
src/mathed/formulamacro.C
src/mathed/macro_support.C [new file with mode: 0644]
src/mathed/macro_support.h [new file with mode: 0644]
src/mathed/math_cursor.C
src/mathed/math_macro.C
src/mathed/math_macro.h
src/mathed/math_macroarg.C [new file with mode: 0644]
src/mathed/math_macroarg.h [new file with mode: 0644]
src/mathed/math_macrotable.C [new file with mode: 0644]
src/mathed/math_macrotable.h [new file with mode: 0644]
src/mathed/math_macrotemplate.C [new file with mode: 0644]
src/mathed/math_macrotemplate.h [new file with mode: 0644]
src/mathed/math_parser.C

index bb5b4b539c7c73eac3d47e7c49aaff5eb37fa43e..f05e4cf95c09b6a9a92f1174878fc93babb5eff2 100644 (file)
@@ -1,3 +1,23 @@
+2001-02-13  Lars Gullik Bjønnes  <larsbj@lyx.org>
+
+       * math_parser.C: include macrotable and macrotemplate
+
+       * math_macrotemplate.[Ch]: new files
+
+       * math_macrotable.[Ch]: new files
+
+       * math_macroarg.[Ch]: new files
+
+       * math_macro.C: move stuff to the new files.
+
+       * math_cursor.C: include math_macrotable.h
+
+       * macro_support.[Ch]: new files
+
+       * formulamacro.C: add more includes
+
+       * Makefile.am (libmathed_la_SOURCES): add new files.
+
 2001-02-13  Jean-Marc Lasgouttes  <Jean-Marc.Lasgouttes@inria.fr>
 
        * Makefile.am (libmathed_la_SOURCES): remove duplicate
index 04fabc232e1af0a18efe7ec86ac13eabb6638417..07c84fe53bfadc6130f7fa244f4a59b9b09efb24 100644 (file)
@@ -14,6 +14,8 @@ libmathed_la_SOURCES = \
        formula.h \
        formulamacro.C \
        formulamacro.h \
+       macro_support.C \
+       macro_support.h \
        math_sqrtinset.C \
        math_sqrtinset.h \
        math_accentinset.C \
@@ -43,6 +45,12 @@ libmathed_la_SOURCES = \
        math_iter.h \
        math_macro.C \
        math_macro.h \
+       math_macroarg.C \
+       math_macroarg.h \
+       math_macrotemplate.C \
+       math_macrotemplate.h \
+       math_macrotable.C \
+       math_macrotable.h \
        math_matrixinset.C \
        math_matrixinset.h \
        math_panel.C \
index 5c9dbc16a309cc628b8ec2132d400568364926e1..73af819f5e45f8815660ec44edcf53d8e6c5907f 100644 (file)
@@ -25,6 +25,8 @@
 #include "math_cursor.h"
 #include "math_parser.h"
 #include "math_macro.h"
+#include "math_macrotable.h"
+#include "math_macrotemplate.h"
 #include "lyx_main.h"
 #include "BufferView.h"
 #include "gettext.h"
diff --git a/src/mathed/macro_support.C b/src/mathed/macro_support.C
new file mode 100644 (file)
index 0000000..56c398d
--- /dev/null
@@ -0,0 +1,11 @@
+#include <config.h>
+
+#include "support/LOstream.h"
+#include "macro_support.h"
+
+using std::ostream;
+
+ostream & operator<<(ostream & o, MathedMacroFlag mmf)
+{
+       return o << int(mmf);
+}
diff --git a/src/mathed/macro_support.h b/src/mathed/macro_support.h
new file mode 100644 (file)
index 0000000..b9c40e8
--- /dev/null
@@ -0,0 +1,15 @@
+// -*- C++ -*-
+#ifndef MACROSUPPORT_H
+#define MACROSUPPORT_H
+
+#include <iosfwd>
+
+enum MathedMacroFlag {
+       MMF_Env = 1,
+       MMF_Exp = 2,
+       MMF_Edit = 4
+};
+
+extern
+std::ostream & operator<<(std::ostream & o, MathedMacroFlag mmf);
+#endif
index bd8cd8e5097e10b1add78e99ed17fc8515a4ba78..e91201eb7b321716d8a361fd2997c6bb3630f6e1 100644 (file)
@@ -25,6 +25,7 @@
 #include "math_parser.h"
 #include "math_cursor.h"
 #include "math_macro.h"
+#include "math_macrotable.h"
 #include "math_root.h"
 #include "support/lstrings.h"
 #include "debug.h"
index 5274462b061420ef92df776b3875a929bce19523..632c3d76763742c3da7d4da66cc00912bf5ce817 100644 (file)
@@ -33,6 +33,9 @@
 #include "math_rowst.h"
 #include "support/lstrings.h"
 #include "debug.h"
+#include "mathed/support.h"
+#include "math_macrotemplate.h"
+#include "macro_support.h"
 
 using std::ostream;
 using std::endl;
@@ -42,23 +45,12 @@ ostream & operator<<(ostream & o, MathedTextCodes mtc)
        return o << int(mtc);
 }
 
-enum MathedMacroFlag {
-    MMF_Env= 1,
-    MMF_Exp= 2,
-    MMF_Edit= 4
-};
-
-ostream & operator<<(ostream & o, MathedMacroFlag mmf)
-{
-       return o << int(mmf);
-}
-
-extern int mathed_string_width(short type, int style, string const & s);
-extern int mathed_string_height(short, int, string const &, int &, int &);
 
+//extern int mathed_string_width(short type, int style, string const & s);
+//extern int mathed_string_height(short, int, string const &, int &, int &);
 
-MathMacro::MathMacro(MathMacroTemplate* t): 
-    MathParInset(LM_ST_TEXT, "", LM_OT_MACRO), tmplate(t)
+MathMacro::MathMacro(MathMacroTemplate * t): 
+       MathParInset(LM_ST_TEXT, "", LM_OT_MACRO), tmplate(t)
 {
        nargs = tmplate->getNoArgs();
        tcode = tmplate->getTCode();
@@ -72,7 +64,7 @@ MathMacro::MathMacro(MathMacroTemplate* t):
 
 
 MathMacro::MathMacro(MathMacro * m): 
-    MathParInset(LM_ST_TEXT, m->GetName(), LM_OT_MACRO)
+       MathParInset(LM_ST_TEXT, m->GetName(), LM_OT_MACRO)
 {
        tmplate = m->tmplate;
        nargs = tmplate->getNoArgs();
@@ -100,419 +92,148 @@ MathMacro::~MathMacro()
 
 MathedInset * MathMacro::Clone()
 {
-    return new MathMacro(this);
+       return new MathMacro(this);
 }
 
 
 void MathMacro::Metrics()
 {
-    if (nargs > 0)
-      tmplate->update(this);
-    tmplate->SetStyle(size);
-    tmplate->Metrics();
-    width = tmplate->Width();
-    ascent = tmplate->Ascent();
-    descent = tmplate->Descent();
+       if (nargs > 0)
+               tmplate->update(this);
+       tmplate->SetStyle(size);
+       tmplate->Metrics();
+       width = tmplate->Width();
+       ascent = tmplate->Ascent();
+       descent = tmplate->Descent();
 }
 
 
 void MathMacro::draw(Painter & pain, int x, int y)
 {
-    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);
-    }
-}
-
-
-int MathMacro::GetColumns() const
-{
-    return tmplate->getMacroPar(idx)->GetColumns();
-}
-
-
-void MathMacro::GetXY(int & x, int & y) const
-{
-    x = args_[idx].x;  y = args_[idx].y;
-}
-
-
-bool MathMacro::Permit(short f) const
-{
-    return (nargs > 0) ?
-           tmplate->getMacroPar(idx)->Permit(f) : MathParInset::Permit(f);
-}
-
-
-void MathMacro::SetFocus(int x, int y)
-{
-    tmplate->update(this);
-    tmplate->SetMacroFocus(idx, x, y);
-}
-
-
-void MathMacro::Write(ostream & os, bool fragile)
-{
-    if (tmplate->flags & MMF_Exp) {
-           lyxerr[Debug::MATHED] << "Expand " << tmplate->flags
-                                 << ' ' << MMF_Exp << endl; 
+       xo = x;  yo = y;
+       Metrics();
        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 << '{';
-       
+       tmplate->SetStyle(size);
+       tmplate->draw(pain, x, y);
        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 << ' ';
+               tmplate->GetMacroXY(i, args_[i].x, args_[i].y);
        }
-    }
 }
 
 
-/*---------------  Macro argument -----------------------------------*/
-
-MathMacroArgument::MathMacroArgument(int n)
+bool MathMacro::setArgumentIdx(int i)
 {
-    number = n;
-    expnd_mode = false;
-    SetType(LM_OT_MACRO_ARG);
+       if (i >= 0 && i < nargs) {
+               idx = i;
+               return true;
+       } else
+               return false;
 }
 
 
-void MathMacroArgument::draw(Painter & pain, int x, int baseline)
-{
-       if (expnd_mode) {
-               MathParInset::draw(pain, x, baseline);
-       }
-       else {
-               std::ostringstream ost;
-               ost << '#' << number;
-               drawStr(pain, LM_TC_TEX, size, x, baseline, ost.str().c_str());
-       }
+int MathMacro::getArgumentIdx() const 
+{ 
+       return idx; 
 }
 
 
-void MathMacroArgument::Metrics()
-{
-       if (expnd_mode) {
-               MathParInset::Metrics();
-       }
-       else {
-               std::ostringstream ost;
-               ost << '#' << number;
-               width = mathed_string_width(LM_TC_TEX, size, ost.str().c_str());
-               mathed_string_height(LM_TC_TEX, size, ost.str().c_str(),
-                                    ascent, descent);
-       }
-}
-
+int MathMacro::getMaxArgumentIdx() const 
+{ 
+       return nargs - 1; 
+} 
 
-void MathMacroArgument::Write(ostream & os, bool fragile)
-{
-    if (expnd_mode) {
-       MathParInset::Write(os, fragile);
-    } else {
-           os << '#' << number << ' ';
-    }
-}
 
+MathedArray * MathMacro::GetData() 
+{ 
+       return args_[idx].array; 
+} 
 
-/* --------------------- MathMacroTemplate ---------------------------*/
 
-MathMacroTemplate::MathMacroTemplate(string const & nm, int na, int flg):
-    MathParInset(LM_ST_TEXT, nm, LM_OT_MACRO), 
-    flags(flg), nargs(na)
+int MathMacro::GetColumns() const
 {
-       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;
-       }
+       return tmplate->getMacroPar(idx)->GetColumns();
 }
 
 
-MathMacroTemplate::~MathMacroTemplate()
+void MathMacro::GetXY(int & x, int & y) const
 {
-       // prevent to delete already deleted objects
-       for (int i = 0; i < nargs; ++i) {
-               args_[i].SetData(0);
-       }
+       x = args_[idx].x;  y = args_[idx].y;
 }
 
 
-void MathMacroTemplate::setEditMode(bool ed)
-{
-       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);
-               }
-       }
-}
-
-
-void MathMacroTemplate::draw(Painter & pain, int x, int y)
+bool MathMacro::Permit(short f) const
 {
-       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);
-       }
+       return (nargs > 0) ?
+               tmplate->getMacroPar(idx)->Permit(f) : MathParInset::Permit(f);
 }
 
 
-void MathMacroTemplate::Metrics()
+void MathMacro::SetFocus(int x, int y)
 {
-    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);
-    }
+       tmplate->update(this);
+       tmplate->SetMacroFocus(idx, x, y);
 }
 
 
-void MathMacroTemplate::update(MathMacro * macro)
-{
-    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);
-}
-    
-
-void MathMacroTemplate::WriteDef(ostream & os, bool fragile)
+void MathMacro::SetData(MathedArray * a)
 {
-       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";
+       args_[idx].array = a;
 }
 
 
-void MathMacroTemplate::setArgument(MathedArray * a, int i)
-{
-    args_[i].SetData(a);
-}
 
 
-void MathMacroTemplate::GetMacroXY(int i, int & x, int & y) const
+MathedRowSt * MathMacro::getRowSt() const
 {
-    args_[i].GetXY(x, y);
+       return args_[idx].row;
 }
 
 
-MathParInset * MathMacroTemplate::getMacroPar(int i) const
+MathedTextCodes MathMacro::getTCode() const
 {
-       if (i >= 0 && i < nargs) 
-               return const_cast<MathParInset *>
-                       (static_cast<MathParInset const *>(&args_[i]));
-       else 
-               return 0;
+       return tcode;
 }
+       
 
-
-void MathMacroTemplate::SetMacroFocus(int &idx, int x, int y)
+void MathMacro::Write(ostream & os, bool fragile)
 {
-       for (int i = 0; i < nargs; ++i) {
-               if (args_[i].Inside(x, y)) {
-                       idx = i;
-                       break;
+       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 << ' ';
                }
        }
 }
-
-
-/* -------------------------- MathMacroTable -----------------------*/
-
-// The search is currently linear but will be binary or hash, later.
-MathMacroTemplate * MathMacroTable::getTemplate(string const & name) const
-{
-       for (size_type i = 0; i < macro_table.size(); ++i) {
-               if (name == macro_table[i]->GetName()) 
-                       return macro_table[i];
-       }
-
-       return 0;
-}
-
-void MathMacroTable::addTemplate(MathMacroTemplate * m)
-{
-       macro_table.push_back(m);
-}
-
-
-// All this stuff aparently leaks because it's created here and is not 
-// deleted never, but it have to live all the LyX sesion. OK, would not
-// so hard to do it in the MacroTable destructor, but this doesn't harm
-// seriously, so don't bother me with purify results here.   ;-)
-
-void MathMacroTable::builtinMacros()
-{
-    MathedIter iter;
-    MathParInset * inset;// *arg;
-    MathedArray * array2;
-    
-    built = true;
-    
-    lyxerr[Debug::MATHED] << "Building macros" << endl;
-    
-    // This macro doesn't have arguments
-    MathMacroTemplate * m = new MathMacroTemplate("notin");  // this leaks
-    addTemplate(m);
-    MathedArray * array = new MathedArray; // this leaks
-    iter.SetData(array);
-    iter.Insert(new MathAccentInset(LM_in, LM_TC_BOPS, LM_not)); // this leaks
-    m->SetData(array);
-    
-    // These two are only while we are still with LyX 2.x
-    m = new MathMacroTemplate("emptyset"); // this leaks
-    addTemplate(m);
-    array = new MathedArray; // this leaks
-    iter.SetData(array);
-    iter.Insert(new MathAccentInset('O', LM_TC_RM, LM_not)); // this leaks
-    m->SetData(array);
-    
-    m = new MathMacroTemplate("perp"); // this leaks
-    addTemplate(m);
-    array = new MathedArray; // this leaks
-    iter.SetData(array);
-    iter.Insert(LM_bot, LM_TC_BOP);
-    m->SetData(array);
-
-    // binom has two arguments
-    m = new MathMacroTemplate("binom", 2);
-    addTemplate(m);
-    array = new MathedArray; 
-    m->SetData(array);
-    iter.SetData(array);
-    inset = new MathDelimInset('(', ')');
-    iter.Insert(inset, LM_TC_ACTIVE_INSET);
-    array = new MathedArray; 
-    iter.SetData(array);
-    MathFracInset * frac = new MathFracInset(LM_OT_ATOP);
-    iter.Insert(frac, LM_TC_ACTIVE_INSET);
-    inset->SetData(array);
-    array = new MathedArray;
-    array2 = new MathedArray;  
-    iter.SetData(array);
-    iter.Insert(m->getMacroPar(0));
-    iter.SetData(array2);
-    iter.Insert(m->getMacroPar(1));
-    frac->SetData(array, array2);
-
-/*
-    // Cases has 1 argument
-    m = new MathMacroTemplate("cases", 1, MMF_Env); // this leaks
-    addTemplate(m);
-    array = new MathedArray; // this leaks
-    iter.SetData(array);
-    arg = new MathMatrixInset(2, 1); // this leaks
-
-    m->setArgument(arg);
-    arg->SetAlign('c', "ll");
-    iter.Insert(arg, LM_TC_ACTIVE_INSET);
-    inset = new MathDelimInset('{', '.'); // this leaks
-    inset->SetData(array);
-    array = new MathedArray; // this leaks
-    iter.SetData(array);
-    iter.Insert(inset, LM_TC_ACTIVE_INSET);
-    m->SetData(array);
-  
-
-    // the environment substack has 1 argument
-    m = new MathMacroTemplate("substack", 1, MMF_Env); // this leaks
-    addTemplate(m);     
-    arg = new MathMatrixInset(1, 1); // this leaks
-    m->setArgument(arg);
-    arg->SetType(LM_OT_MACRO);
-    array = new MathedArray; // this leaks
-    iter.SetData(array);
-    iter.Insert(arg, LM_TC_ACTIVE_INSET);
-    m->SetData(array);*/
-}
-
-
-MathMacroTable MathMacroTable::mathMTable;
-bool MathMacroTable::built = false;
index 6f0de9d645ed08e7b33f5973e6420f9408858b9c..f63466cd0b53808e374da0e1c47619eec1fd5b50 100644 (file)
@@ -33,232 +33,73 @@ class MathMacroTemplate;
 class MathMacro : public MathParInset
 {
 public:
-    /// A macro can only be builded from an existing template
-    explicit
-    MathMacro(MathMacroTemplate *);
-    /// or from another macro.
-    explicit
-    MathMacro(MathMacro *);
-    ///
-    ~MathMacro();
-    ///
-    void draw(Painter &, int, int);
-    ///
-    void Metrics();
-    ///
-    MathedInset * Clone();
-    ///
-    void Write(std::ostream &, bool fragile);
-    ///
-    bool setArgumentIdx(int);
-    ///
-    int getArgumentIdx() const;
-    ///
-    int getMaxArgumentIdx() const;
-    ///
-    int GetColumns() const;
-    ///
-    void GetXY(int &, int &) const;
-    ///
-    void SetFocus(int, int);
-    ///
-    MathedArray * GetData();
-    ///
-    MathedRowSt * getRowSt() const { return args_[idx].row; }
-    ///
-    void SetData(MathedArray *);
-    ///
-    MathedTextCodes getTCode() const { return tcode; }
-    ///
-    bool Permit(short) const;
+       /// A macro can only be builded from an existing template
+       explicit
+       MathMacro(MathMacroTemplate *);
+       /// or from another macro.
+       explicit
+       MathMacro(MathMacro *);
+       ///
+       ~MathMacro();
+       ///
+       void draw(Painter &, int, int);
+       ///
+       void Metrics();
+       ///
+       MathedInset * Clone();
+       ///
+       void Write(std::ostream &, bool fragile);
+       ///
+       bool setArgumentIdx(int);
+       ///
+       int getArgumentIdx() const;
+       ///
+       int getMaxArgumentIdx() const;
+       ///
+       int GetColumns() const;
+       ///
+       void GetXY(int &, int &) const;
+       ///
+       void SetFocus(int, int);
+       ///
+       MathedArray * GetData();
+       ///
+       MathedRowSt * getRowSt() const;
+       ///
+       void SetData(MathedArray *);
+       ///
+       MathedTextCodes getTCode() const;
+       ///
+       bool Permit(short) const;
     
 private:
-    ///
-    MathMacroTemplate * tmplate;
-    ///
-    struct MacroArgumentBase {
-       /// Position of the macro
-       int x, y;
        ///
-       MathedRowSt * row;
+       MathMacroTemplate * tmplate;
        ///
-       MathedArray * array;
+       struct MacroArgumentBase {
+               /// Position of the macro
+               int x, y;
+               ///
+               MathedRowSt * row;
+               ///
+               MathedArray * array;
+               ///
+               MacroArgumentBase() { x = y = 0;  array = 0; row = 0; }
+       };
+       std::vector<MacroArgumentBase> args_;
        ///
-       MacroArgumentBase() { x = y = 0;  array = 0; row = 0; }
-    };
-    std::vector<MacroArgumentBase> args_;
-    ///
-    int idx;
-    ///
-    int nargs;
-    ///
-    MathedTextCodes tcode;
-    ///
-    friend class MathMacroTemplate;
-};
-
-
-/// An argument
-class MathMacroArgument: public MathParInset {
-public:
-    ///
-    MathMacroArgument() {
-           expnd_mode = false;
-           number = 1;
-           SetType(LM_OT_MACRO_ARG);
-    }
-    ///
-    explicit
-    MathMacroArgument(int);
-    ///
-    ~MathMacroArgument() { lyxerr << "help, destroyme!" << std::endl; }
-    ///
-    MathedInset * Clone() { return this; }
-    ///
-    void Metrics();
-    ///
-    void draw(Painter &, int x, int baseline);
-    ///
-    void Write(std::ostream &, bool fragile);
-    ///
-    void setNumber(int n) { number = n; }
-    /// Is expanded or not
-    void setExpand(bool e) { expnd_mode = e; }
-    /// Is expanded or not
-    bool getExpand() const { return expnd_mode; }
-private:
-    ///
-    bool expnd_mode;
-    ///
-    int number;
-};
-
-
-/// This class contains the macro definition
-class MathMacroTemplate: public MathParInset {
-public:
-    /// A template constructor needs all the data
-    explicit
-    MathMacroTemplate(string const &, int na = 0, int f = 0);
-    ///
-    ~MathMacroTemplate();
-    ///
-    void draw(Painter &, int, int);
-    ///
-    void Metrics();
-    ///
-    void WriteDef(std::ostream &, bool fragile);
-    /// useful for special insets
-    void  setTCode(MathedTextCodes t) { tcode = t; }
-    ///
-    MathedTextCodes getTCode() const { return tcode; }
-    /// 
-    void setArgument(MathedArray *, int i= 0);
-    /// Number of arguments
-    int getNoArgs() const { return nargs; }
-    ///
-    void GetMacroXY(int, int &, int &) const;
-    ///
-    MathParInset * getMacroPar(int) const;
-    ///
-    void SetMacroFocus(int &, int, int);
-    ///
-    void setEditMode(bool);
-
-    /// Replace the appropriate arguments with a specific macro's data
-    void update(MathMacro * m = 0);
-      
-private:
-    ///
-    short flags;
-    ///
-    MathedTextCodes tcode;
-    ///
-    std::vector<MathMacroArgument> args_;
-    ///
-    int nargs;
-    ///
-    friend class MathMacro;
+       int idx;
+       ///
+       int nargs;
+       ///
+       MathedTextCodes tcode;
+       ///
+       friend class MathMacroTemplate;
 };
-     
 
 ///
-typedef MathMacro * MathMacroP;
-///
-typedef MathMacroTemplate * MathMacroTemplateP;
-
+//typedef MathMacro * MathMacroP;
 ///
-class MathMacroTable {
-public:
-    ///
-    void addTemplate(MathMacroTemplate *);
-    ///
-    MathMacro * getMacro(string const &) const;
-    ///
-    MathMacroTemplate * getTemplate(string const &) const;
-    ///
-    void builtinMacros();
-    ///
-    static MathMacroTable mathMTable;
-    ///
-    static bool built;
-    
-private:
-    ///
-    typedef std::vector<MathMacroTemplateP> table_type;
-               ///
-    typedef table_type::size_type size_type;
-    ///
-    table_type macro_table;
-};
-
-
-
-/*-----------------------  inlines  -------------------------*/
-
-inline
-bool MathMacro::setArgumentIdx(int i)
-{
-    if (i >= 0 && i < nargs) {
-       idx = i;
-       return true;
-    } else
-      return false;
-}
-
-inline
-int MathMacro::getArgumentIdx() const 
-{ 
-    return idx; 
-}
-
-inline
-int MathMacro::getMaxArgumentIdx() const 
-{ 
-    return nargs - 1; 
-} 
-
-
-inline
-MathedArray * MathMacro::GetData() 
-{ 
-    return args_[idx].array; 
-} 
-
-
-inline
-void MathMacro::SetData(MathedArray * a)
-{
-   args_[idx].array = a;
-}
-
-
-inline 
-MathMacro * MathMacroTable::getMacro(string const & name) const
-{
-       MathMacroTemplate * mt = getTemplate(name);
-       return (mt) ? new MathMacro(mt): 0;
-}
-
+//typedef MathMacroTemplate * MathMacroTemplateP;
 
 #endif
diff --git a/src/mathed/math_macroarg.C b/src/mathed/math_macroarg.C
new file mode 100644 (file)
index 0000000..fa25bf4
--- /dev/null
@@ -0,0 +1,85 @@
+#include <config.h>
+
+#include "math_macroarg.h"
+#include "mathed/support.h"
+#include "debug.h"
+
+
+MathMacroArgument::MathMacroArgument()
+       : expnd_mode_(false), number_(1)
+{
+       SetType(LM_OT_MACRO_ARG);
+}
+
+
+MathMacroArgument::MathMacroArgument(int n)
+       : expnd_mode_(false), number_(n)
+{
+       SetType(LM_OT_MACRO_ARG);
+}
+
+
+MathMacroArgument::~MathMacroArgument()
+{
+       lyxerr << "help, destroyme!" << std::endl;
+}
+
+
+MathedInset * MathMacroArgument::Clone()
+{
+       return this;
+}
+
+
+void MathMacroArgument::setNumber(int n)
+{
+       number_ = n;
+}
+
+
+void MathMacroArgument::setExpand(bool e)
+{
+       expnd_mode_ = e;
+}
+
+
+bool MathMacroArgument::getExpand() const
+{
+       return expnd_mode_;
+}
+
+
+void MathMacroArgument::draw(Painter & pain, int x, int baseline)
+{
+       if (expnd_mode_) {
+               MathParInset::draw(pain, x, baseline);
+       }
+       else {
+               std::ostringstream ost;
+               ost << '#' << number_;
+               drawStr(pain, LM_TC_TEX, size, x, baseline, ost.str().c_str());
+       }
+}
+
+void MathMacroArgument::Metrics()
+{
+       if (expnd_mode_) {
+               MathParInset::Metrics();
+       } else {
+               std::ostringstream ost;
+               ost << '#' << number_;
+               width = mathed_string_width(LM_TC_TEX, size, ost.str().c_str());
+               mathed_string_height(LM_TC_TEX, size, ost.str().c_str(),
+                                    ascent, descent);
+       }
+}
+
+
+void MathMacroArgument::Write(ostream & os, bool fragile)
+{
+       if (expnd_mode_) {
+               MathParInset::Write(os, fragile);
+       } else {
+               os << '#' << number_ << ' ';
+       }
+}
diff --git a/src/mathed/math_macroarg.h b/src/mathed/math_macroarg.h
new file mode 100644 (file)
index 0000000..be95ec0
--- /dev/null
@@ -0,0 +1,37 @@
+// -*- C++ -*-
+#ifndef MATHMACROARGUMENT_H
+#define MATHMACROARGUMENT_H
+
+#include "math_parinset.h"
+
+/// An argument
+class MathMacroArgument: public MathParInset {
+public:
+    ///
+    MathMacroArgument();
+    ///
+    explicit
+    MathMacroArgument(int);
+    ///
+    ~MathMacroArgument();
+    ///
+    MathedInset * Clone();
+    ///
+    void Metrics();
+    ///
+    void draw(Painter &, int x, int baseline);
+    ///
+    void Write(std::ostream &, bool fragile);
+    ///
+    void setNumber(int n);
+    /// Is expanded or not
+    void setExpand(bool e);
+    /// Is expanded or not
+    bool getExpand() const;
+private:
+    ///
+    bool expnd_mode_;
+    ///
+    int number_;
+};
+#endif
diff --git a/src/mathed/math_macrotable.C b/src/mathed/math_macrotable.C
new file mode 100644 (file)
index 0000000..f397994
--- /dev/null
@@ -0,0 +1,131 @@
+#include <config.h>
+
+#include "math_macrotable.h"
+#include "math_macro.h"
+#include "math_macrotemplate.h"
+#include "math_iter.h"
+#include "array.h"
+#include "math_accentinset.h"
+#include "math_deliminset.h"
+#include "math_fracinset.h"
+#include "math_parinset.h"
+
+
+MathMacroTable MathMacroTable::mathMTable;
+
+bool MathMacroTable::built = false;
+
+
+MathMacro * MathMacroTable::getMacro(string const & name) const
+{
+       MathMacroTemplate * mt = getTemplate(name);
+       return (mt) ? new MathMacro(mt): 0;
+}
+
+
+// The search is currently linear but will be binary or hash, later.
+MathMacroTemplate * MathMacroTable::getTemplate(string const & name) const
+{
+       for (size_type i = 0; i < macro_table.size(); ++i) {
+               if (name == macro_table[i]->GetName()) 
+                       return macro_table[i];
+       }
+
+       return 0;
+}
+
+void MathMacroTable::addTemplate(MathMacroTemplate * m)
+{
+       macro_table.push_back(m);
+}
+
+
+// All this stuff aparently leaks because it's created here and is not 
+// deleted never, but it have to live all the LyX sesion. OK, would not
+// so hard to do it in the MacroTable destructor, but this doesn't harm
+// seriously, so don't bother me with purify results here.   ;-)
+
+void MathMacroTable::builtinMacros()
+{
+       MathedIter iter;
+       MathParInset * inset;// *arg;
+       MathedArray * array2;
+    
+       built = true;
+    
+       lyxerr[Debug::MATHED] << "Building macros" << endl;
+    
+       // This macro doesn't have arguments
+       MathMacroTemplate * m = new MathMacroTemplate("notin");  // this leaks
+       addTemplate(m);
+       MathedArray * array = new MathedArray; // this leaks
+       iter.SetData(array);
+       iter.Insert(new MathAccentInset(LM_in, LM_TC_BOPS, LM_not)); // this leaks
+       m->SetData(array);
+    
+       // These two are only while we are still with LyX 2.x
+       m = new MathMacroTemplate("emptyset"); // this leaks
+       addTemplate(m);
+       array = new MathedArray; // this leaks
+       iter.SetData(array);
+       iter.Insert(new MathAccentInset('O', LM_TC_RM, LM_not)); // this leaks
+       m->SetData(array);
+    
+       m = new MathMacroTemplate("perp"); // this leaks
+       addTemplate(m);
+       array = new MathedArray; // this leaks
+       iter.SetData(array);
+       iter.Insert(LM_bot, LM_TC_BOP);
+       m->SetData(array);
+
+       // binom has two arguments
+       m = new MathMacroTemplate("binom", 2);
+       addTemplate(m);
+       array = new MathedArray; 
+       m->SetData(array);
+       iter.SetData(array);
+       inset = new MathDelimInset('(', ')');
+       iter.Insert(inset, LM_TC_ACTIVE_INSET);
+       array = new MathedArray; 
+       iter.SetData(array);
+       MathFracInset * frac = new MathFracInset(LM_OT_ATOP);
+       iter.Insert(frac, LM_TC_ACTIVE_INSET);
+       inset->SetData(array);
+       array = new MathedArray;
+       array2 = new MathedArray;  
+       iter.SetData(array);
+       iter.Insert(m->getMacroPar(0));
+       iter.SetData(array2);
+       iter.Insert(m->getMacroPar(1));
+       frac->SetData(array, array2);
+
+/*
+  // Cases has 1 argument
+  m = new MathMacroTemplate("cases", 1, MMF_Env); // this leaks
+  addTemplate(m);
+  array = new MathedArray; // this leaks
+  iter.SetData(array);
+  arg = new MathMatrixInset(2, 1); // this leaks
+
+  m->setArgument(arg);
+  arg->SetAlign('c', "ll");
+  iter.Insert(arg, LM_TC_ACTIVE_INSET);
+  inset = new MathDelimInset('{', '.'); // this leaks
+  inset->SetData(array);
+  array = new MathedArray; // this leaks
+  iter.SetData(array);
+  iter.Insert(inset, LM_TC_ACTIVE_INSET);
+  m->SetData(array);
+  
+
+  // the environment substack has 1 argument
+  m = new MathMacroTemplate("substack", 1, MMF_Env); // this leaks
+  addTemplate(m);     
+  arg = new MathMatrixInset(1, 1); // this leaks
+  m->setArgument(arg);
+  arg->SetType(LM_OT_MACRO);
+  array = new MathedArray; // this leaks
+  iter.SetData(array);
+  iter.Insert(arg, LM_TC_ACTIVE_INSET);
+  m->SetData(array);*/
+}
diff --git a/src/mathed/math_macrotable.h b/src/mathed/math_macrotable.h
new file mode 100644 (file)
index 0000000..1814e3a
--- /dev/null
@@ -0,0 +1,34 @@
+// -*- C++ -*-
+#ifndef MATHMACROTABLE
+#define MATHMACROTABLE
+
+#include <vector>
+#include "LString.h"
+
+class MathMacroTemplate;
+class MathMacro;
+
+///
+class MathMacroTable {
+public:
+    ///
+    void addTemplate(MathMacroTemplate *);
+    ///
+    MathMacro * getMacro(string const &) const;
+    ///
+    MathMacroTemplate * getTemplate(string const &) const;
+    ///
+    void builtinMacros();
+    ///
+    static MathMacroTable mathMTable;
+    ///
+    static bool built;
+private:
+    ///
+    typedef std::vector<MathMacroTemplate *> table_type;
+    ///
+    typedef table_type::size_type size_type;
+    ///
+    table_type macro_table;
+};
+#endif
diff --git a/src/mathed/math_macrotemplate.C b/src/mathed/math_macrotemplate.C
new file mode 100644 (file)
index 0000000..21e0083
--- /dev/null
@@ -0,0 +1,178 @@
+#include <config.h>
+
+#include "math_macrotemplate.h"
+#include "math_macro.h"
+#include "macro_support.h"
+
+
+void  MathMacroTemplate::setTCode(MathedTextCodes t)
+{
+       tcode = t;
+}
+
+
+MathedTextCodes MathMacroTemplate::getTCode() const
+{
+       return tcode;
+}
+
+
+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)
+{
+       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;
+       }
+}
+
+
+MathMacroTemplate::~MathMacroTemplate()
+{
+       // prevent to delete already deleted objects
+       for (int i = 0; i < nargs; ++i) {
+               args_[i].SetData(0);
+       }
+}
+
+
+void MathMacroTemplate::setEditMode(bool ed)
+{
+       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);
+               }
+       }
+}
+
+
+void MathMacroTemplate::draw(Painter & pain, int x, int y)
+{
+       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);
+       }
+}
+
+
+void MathMacroTemplate::Metrics()
+{
+       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);
+       }
+}
+
+
+void MathMacroTemplate::update(MathMacro * macro)
+{
+       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);
+}
+
+
+void MathMacroTemplate::WriteDef(ostream & os, bool fragile)
+{
+       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";
+}
+
+
+void MathMacroTemplate::setArgument(MathedArray * a, int i)
+{
+       args_[i].SetData(a);
+}
+
+
+void MathMacroTemplate::GetMacroXY(int i, int & x, int & y) 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;
+               }
+       }
+}
diff --git a/src/mathed/math_macrotemplate.h b/src/mathed/math_macrotemplate.h
new file mode 100644 (file)
index 0000000..bf34520
--- /dev/null
@@ -0,0 +1,57 @@
+// -*- C++ -*-
+#ifndef MATHMACROTEMPLATE
+#define MATHMACROTEMPLATE
+
+#include <vector>
+
+#include "math_parinset.h"
+#include "math_macroarg.h"
+
+class MathMacro;
+
+/// This class contains the macro definition
+class MathMacroTemplate: public MathParInset {
+  public:
+  /// A template constructor needs all the data
+    explicit
+    MathMacroTemplate(string const &, int na = 0, int f = 0);
+    ///
+    ~MathMacroTemplate();
+    ///
+    void draw(Painter &, int, int);
+    ///
+    void Metrics();
+    ///
+    void WriteDef(std::ostream &, bool fragile);
+    /// useful for special insets
+    void  setTCode(MathedTextCodes t);
+    ///
+    MathedTextCodes getTCode() const;
+    /// 
+    void setArgument(MathedArray *, int i= 0);
+    /// Number of arguments
+    int getNoArgs() const;
+    ///
+    void GetMacroXY(int, int &, int &) const;
+    ///
+    MathParInset * getMacroPar(int) const;
+    ///
+    void SetMacroFocus(int &, int, int);
+    ///
+    void setEditMode(bool);
+
+    /// Replace the appropriate arguments with a specific macro's data
+    void update(MathMacro * m = 0);
+private:
+    ///
+    short flags;
+    ///
+    MathedTextCodes tcode;
+    ///
+    std::vector<MathMacroArgument> args_;
+    ///
+    int nargs;
+    ///
+    friend class MathMacro;
+};
+#endif
index e0ca1a79b0d808ea7836135706eb5e7e0d47e4b7..3883ecfeb34cf8f3327650dfeef19d2907452689 100644 (file)
@@ -29,6 +29,8 @@
 #include "math_iter.h"
 #include "math_inset.h"
 #include "math_macro.h"
+#include "math_macrotable.h"
+#include "math_macrotemplate.h"
 #include "math_root.h"
 #include "math_matrixinset.h"
 #include "math_accentinset.h"