]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_macro.C
Hopefully fix the problem with stateText() in lyxfont.C
[lyx.git] / src / mathed / math_macro.C
index 52f629666c5a492db8e4bfa3735fbfec1e2a78e8..a68705933dd2997f19462f4a2e88fa6a6fa4e0f7 100644 (file)
@@ -8,7 +8,7 @@
  *
  *  Dependencies: Mathed
  *
- *  Copyright: (c) 1996, 1997 Alejandro Aguilar Sierra
+ *  Copyright: 1996, 1997 Alejandro Aguilar Sierra
  *
  *  Version: 0.2, Mathed & Lyx project.
  *
 #include "math_macro.h"
 #include "math_iter.h"
 #include "math_inset.h"
-#include "error.h"
+#include "support/lstrings.h"
+#include "debug.h"
 
+using std::ostream;
+using std::endl;
+
+ostream & operator<<(ostream & o, MathedTextCodes mtc)
+{
+       return o << int(mtc);
+}
 
 enum MathedMacroFlag {
-    MMF_Env=1,
-    MMF_Exp=2,
-    MMF_Edit=4
+    MMF_Env= 1,
+    MMF_Exp= 2,
+    MMF_Edit= 4
 };
 
+ostream & operator<<(ostream & o, MathedMacroFlag mmf)
+{
+       return o << int(mmf);
+}
 
-extern GC mathGC, mathFrameGC, latexGC;
 extern int mathed_string_width(short type, int style, byte const* s, int ls);
 extern int mathed_string_height(short, int, byte const*, int, int&, int&);
 
@@ -48,7 +59,7 @@ MathMacro::MathMacro(MathMacroTemplate* t):
     nargs = tmplate->getNoArgs();
     tcode = tmplate->getTCode();
     args = new MacroArgumentBase[nargs];
-    for (int i=0; i<nargs; i++) {
+    for (int i = 0; i < nargs; ++i) {
 //     if (tmplate->getMacroPar(i)->Permit(LMPF_ALLOW_CR))
 //       args[i].row = new MathedRowSt(tmplate->getMacroPar(i)->GetColumns());
 //     else 
@@ -56,14 +67,15 @@ MathMacro::MathMacro(MathMacroTemplate* t):
 /*     int k = tmplate->getMacroPar(i)->GetColumns();
        if (k>0) {
            args[i].array = new LyxArrayBase;
-           for (int j=0; j<k-1; j++) args[i].array->Insert(j, LM_TC_TAB);
+           for (int j= 0; j<k-1; ++j) args[i].array->Insert(j, LM_TC_TAB);
        }*/
     }
     idx = 0;
     SetName(tmplate->GetName());
 }
 
-MathMacro::MathMacro(MathMacro* m): 
+
+MathMacro::MathMacro(MathMacro * m): 
     MathParInset(LM_ST_TEXT, m->GetName(), LM_OT_MACRO)
 {
     tmplate = m->tmplate;
@@ -72,7 +84,7 @@ MathMacro::MathMacro(MathMacro* m):
     args = new MacroArgumentBase[nargs];
     idx = 0;
     SetName(tmplate->GetName());
-    for (int i=0; i<tmplate->nargs; i++) {
+    for (int i = 0; i < tmplate->nargs; ++i) {
        m->setArgumentIdx(i);
        MathedIter it(m->GetData());
        args[i].row = m->args[i].row;
@@ -82,7 +94,7 @@ MathMacro::MathMacro(MathMacro* m):
 
 MathMacro::~MathMacro()
 {
-    for (idx=0; idx<nargs; idx++) {
+    for (idx = 0; idx < nargs; ++idx) {
        MathedIter it(args[idx].array);
        it. Clear();
        delete args[idx].row;
@@ -91,16 +103,15 @@ MathMacro::~MathMacro()
 }
 
 
-MathedInset *MathMacro::Clone()
+MathedInset * MathMacro::Clone()
 {
-    MathMacro *m = new MathMacro(this);
-    return m;
+    return new MathMacro(this);
 }
 
 
 void MathMacro::Metrics()
 {
-    if (nargs>0)
+    if (nargs > 0)
       tmplate->update(this);
     tmplate->Metrics();
     width = tmplate->Width();
@@ -109,15 +120,14 @@ void MathMacro::Metrics()
 }
 
 
-void MathMacro::Draw(int x, int y)
+void MathMacro::draw(Painter & pain, int x, int y)
 {
     xo = x;  yo = y;
     Metrics();
     tmplate->update(this);
     tmplate->SetStyle(size);
-    mathGC = latexGC;
-    tmplate->Draw(x, y);
-    for (int i=0; i<nargs; i++)
+    tmplate->draw(pain, x, y);
+    for (int i = 0; i < nargs; ++i)
       tmplate->GetMacroXY(i, args[i].x, args[i].y);
 }
 
@@ -128,7 +138,7 @@ int MathMacro::GetColumns()
 }
 
 
-void MathMacro::GetXY(int& x, int& y) const
+void MathMacro::GetXY(int & x, int & y) const
 {
     x = args[idx].x;  y = args[idx].y;
 }
@@ -136,9 +146,11 @@ void MathMacro::GetXY(int& x, int& y) const
 
 bool MathMacro::Permit(short f)
 {
-    return ((nargs>0) ? tmplate->getMacroPar(idx)->Permit(f): MathParInset::Permit(f));
+    return (nargs > 0) ?
+           tmplate->getMacroPar(idx)->Permit(f) : MathParInset::Permit(f);
 }
 
+
 void MathMacro::SetFocus(int x, int y)
 {
     tmplate->update(this);
@@ -146,30 +158,20 @@ void MathMacro::SetFocus(int x, int y)
 }
 
 
-void MathMacro::Write(FILE *file)
-{
-   LString output;
-   MathMacro::Write(output);
-   fprintf(file, "%s", output.c_str());
-}
-
-
-void MathMacro::Write(LString &file)
+void MathMacro::Write(ostream & os, bool fragile)
 {
     if (tmplate->flags & MMF_Exp) {
-       lyxerr.debug(LString("Expand ")+ int(tmplate->flags)
-                    + ' ' + int(MMF_Exp), 
-                    Error::MATHED);
+           lyxerr[Debug::MATHED] << "Expand " << tmplate->flags
+                                 << ' ' << MMF_Exp << endl; 
        tmplate->update(this);
-       tmplate->Write(file);
+       tmplate->Write(os, fragile);
     } else {
        if (tmplate->flags & MMF_Env) {
-         file += "\\begin{";
-         file += name;
-         file += "} ";
+               os << "\\begin{"
+                  << name
+                  << "} ";
        } else {
-         file += '\\';
-         file += name;
+               os << '\\' << name;
        }
 //     if (options) { 
 //       file += '[';
@@ -177,30 +179,29 @@ void MathMacro::Write(LString &file)
 //       file += ']';
 //      }
        
-       if (!(tmplate->flags & MMF_Env) && nargs>0) 
-         file += '{';
+       if (!(tmplate->flags & MMF_Env) && nargs > 0) 
+               os << '{';
        
-       for (int i=0; i<nargs; i++) {
+       for (int i = 0; i < nargs; ++i) {
            array = args[i].array;
-           MathParInset::Write(file);
-           if (i<nargs-1)  
-             file += "}{";
+           MathParInset::Write(os, fragile);
+           if (i < nargs - 1)  
+                   os << "}{";
        }   
        if (tmplate->flags & MMF_Env) {
-           file += "\\end{";
-           file += name;
-           file += '}';
+               os << "\\end{"
+                  << name
+                  << '}';
        } else {
-           if (nargs>0) 
-               file += '}';
+           if (nargs > 0) 
+                   os << '}';
            else
-               file += ' ';
+                   os << ' ';
        }
     }
 }
 
 
-
 /*---------------  Macro argument -----------------------------------*/
 
 MathMacroArgument::MathMacroArgument(int n)
@@ -210,14 +211,24 @@ MathMacroArgument::MathMacroArgument(int n)
     SetType(LM_OT_MACRO_ARG);
 }
 
-void MathMacroArgument::Draw(int x, int baseline)
+
+void MathMacroArgument::draw(Painter & pain, int x, int baseline)
 {
     if (expnd_mode) {
-       MathParInset::Draw(x, baseline);
+       MathParInset::draw(pain, x, baseline);
     } else {
-       unsigned char s[3];
-       sprintf((char*)s, "#%d", number);
-       drawStr(LM_TC_TEX, size, x, baseline, &s[0], 2);
+#ifdef HAVE_SSTREAM
+           std::ostringstream ost;
+           ost << '#' << number;
+           drawStr(pain, LM_TC_TEX, size, x, baseline, 
+                   reinterpret_cast<byte const *>(ost.str().c_str()), 2);
+#else
+           char s[3];
+           ostrstream ost(s, 3);
+           ost << '#' << number << '\0';
+           drawStr(pain, LM_TC_TEX, size, x, baseline,
+                   reinterpret_cast<byte *>(ost.str()), 2);
+#endif
     }
 }
 
@@ -227,43 +238,50 @@ void MathMacroArgument::Metrics()
     if (expnd_mode) {
        MathParInset::Metrics();
     } else {
-       unsigned char s[3];
-       sprintf((char*)s, "#%d", number);
-       width = mathed_string_width(LM_TC_TEX, size, &s[0], 2);
-       mathed_string_height(LM_TC_TEX, size, &s[0], 2, ascent, descent);
+#ifdef HAVE_SSTREAM
+           std::ostringstream ost;
+           ost << '#' << number;
+           width = mathed_string_width(LM_TC_TEX, size, 
+                                       reinterpret_cast<byte const *>(ost.str().c_str()), 2);
+           mathed_string_height(LM_TC_TEX, size,
+                                reinterpret_cast<byte const *>(ost.str().c_str()), 
+                                2, ascent, descent);
+#else
+       char s[3];
+       ostrstream ost(s, 3);
+       ost << '#' << number << '\0';
+       width = mathed_string_width(LM_TC_TEX, size,
+                                   reinterpret_cast<byte *>
+                                   (ost.str()), 2);
+       mathed_string_height(LM_TC_TEX, size,
+                            reinterpret_cast<byte *>(ost.str()),
+                            2, ascent, descent);
+#endif
     }
 }
 
-void MathMacroArgument::Write(FILE *file)
-{
-   LString output;
-   MathMacroArgument::Write(output);
-   fprintf(file, "%s", output.c_str());
-}
 
-void MathMacroArgument::Write(LString &file)
+void MathMacroArgument::Write(ostream & os, bool fragile)
 {
     if (expnd_mode) {
-       MathParInset::Write(file);
+       MathParInset::Write(os, fragile);
     } else {
-       file += '#';
-       file += number;
-       file += ' ';
+           os << '#' << number << ' ';
     }
 }
 
 
 /* --------------------- MathMacroTemplate ---------------------------*/
 
-MathMacroTemplate::MathMacroTemplate(char const *nm, int na, int flg):
+MathMacroTemplate::MathMacroTemplate(char const * nm, int na, int flg):
     MathParInset(LM_ST_TEXT, nm, LM_OT_MACRO), 
     flags(flg), nargs(na)
 {
-    if (nargs>0) {
+    if (nargs > 0) {
        tcode = LM_TC_ACTIVE_INSET;
        args = new MathMacroArgument[nargs];
-       for (int i=0; i<nargs; i++) {
-           args[i].setNumber(i+1);
+       for (int i = 0; i < nargs; ++i) {
+           args[i].setNumber(i + 1);
        }
     } else 
       tcode = LM_TC_INSET;
@@ -273,7 +291,7 @@ MathMacroTemplate::MathMacroTemplate(char const *nm, int na, int flg):
 MathMacroTemplate::~MathMacroTemplate()
 {
     // prevent to delete already deleted objects
-    for (int i=0; i<nargs; i++) {
+    for (int i = 0; i < nargs; ++i) {
        args[i].SetData(0);
     }
     delete[] args;
@@ -284,65 +302,67 @@ void MathMacroTemplate::setEditMode(bool ed)
 {
     if (ed) {
        flags |= MMF_Edit;
-       for (int i=0; i<nargs; i++) {
+       for (int i = 0; i < nargs; ++i) {
            args[i].setExpand(false);
        }
     } else {
        flags &= ~MMF_Edit;
-       for (int i=0; i<nargs; i++) {
+       for (int i = 0; i < nargs; ++i) {
            args[i].setExpand(true);
        }
     }
 }
 
 
-void MathMacroTemplate::Draw(int x, int y)
+void MathMacroTemplate::draw(Painter & pain, int x, int y)
 {
     int x2, y2;
-    bool expnd = (nargs>0) ? args[0].getExpand(): false;
+    bool expnd = (nargs > 0) ? args[0].getExpand(): false;
     if (flags & MMF_Edit) {
-       for (int i=0; i<nargs; i++) {
+       for (int i = 0; i < nargs; ++i) {
            args[i].setExpand(false);
        }
       x2 = x; y2 = y;
     } else {
-       for (int i=0; i<nargs; i++) {
+       for (int i = 0; i < nargs; ++i) {
            args[i].setExpand(true);
        }
       x2 = xo; y2 = yo;
     }
-    MathParInset::Draw(x, y);
+    MathParInset::draw(pain, x, y);
     xo = x2; yo = y2;
     
-    for (int i=0; i<nargs; i++) {
+    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++) {
+       for (int i = 0; i < nargs; ++i) {
            args[i].setExpand(false);
        }
     } else {
-       for (int i=0; i<nargs; i++) {
+       for (int i = 0; i < nargs; ++i) {
            args[i].setExpand(true);
        }
     }
     MathParInset::Metrics();
     
-    for (int i=0; i<nargs; i++) {
+    for (int i = 0; i < nargs; ++i) {
        args[i].setExpand(expnd);
     }
 }
 
-void MathMacroTemplate::update(MathMacro* macro)
+
+void MathMacroTemplate::update(MathMacro * macro)
 {
     int idx = (macro) ? macro->getArgumentIdx(): 0;
-    for (int i=0; i<nargs; i++) {
+    for (int i = 0; i < nargs; ++i) {
        if (macro) {
            macro->setArgumentIdx(i);
            args[i].SetData(macro->GetData());
@@ -355,63 +375,44 @@ void MathMacroTemplate::update(MathMacro* macro)
 }
     
 
-void MathMacroTemplate::WriteDef(FILE *file)
+void MathMacroTemplate::WriteDef(ostream & os, bool fragile)
 {
-    fprintf(file, "\n\\newcommand{\\%s}", name);
+       os << "\n\\newcommand{\\" << name << "}";
       
     if (nargs > 0 ) 
-      fprintf(file, "[%d]", nargs);
+           os << "[" << nargs << "]";
     
-    fprintf(file, "{");
+    os << "{";
     
-    for (int i=0; i<nargs; i++) {
+    for (int i = 0; i < nargs; ++i) {
        args[i].setExpand(false);
     }   
-    Write(file); 
-    fprintf(file, "}\n");
+    Write(os, fragile); 
+    os << "}\n";
 }
 
-void MathMacroTemplate::WriteDef(LString &file)
-{
-    file += "\n\\newcommand{\\";
-    file += name;
-    file += '}';
-      
-    if (nargs > 0 ) {
-      file += '[';
-      file += nargs;
-      file += ']';
-    }
-    
-    file += '{';
-    
-    for (int i=0; i<nargs; i++) {
-       args[i].setExpand(false);
-    }   
-    Write(file); 
-    file += "}\n";
-}
 
-void MathMacroTemplate::setArgument(LyxArrayBase *a, int i)
+void MathMacroTemplate::setArgument(LyxArrayBase * a, int i)
 {
     args[i].SetData(a);
 }
 
-void MathMacroTemplate::GetMacroXY(int i, int& x, int& y) const
+
+void MathMacroTemplate::GetMacroXY(int i, int & x, int & y) const
 {
     args[i].GetXY(x, y);
 }
 
 
-MathParInset *MathMacroTemplate::getMacroPar(int i) const
+MathParInset * MathMacroTemplate::getMacroPar(int i) const
 {
-    return ((i>=0 && i<nargs) ? (MathParInset*)&args[i]: 0);
+    return (i >= 0 && i < nargs) ? static_cast<MathParInset*>(&args[i]) : 0;
 }
 
 
 void MathMacroTemplate::SetMacroFocus(int &idx, int x, int y)
 {
-    for (int i=0; i<nargs; i++) {
+    for (int i = 0; i < nargs; ++i) {
        if (args[i].Inside(x, y)) {
            idx = i;
            break;
@@ -422,7 +423,7 @@ void MathMacroTemplate::SetMacroFocus(int &idx, int x, int y)
 
 /* -------------------------- MathMacroTable -----------------------*/
 
-MathMacroTable::MathMacroTable(int n): max_macros(n)
+MathMacroTable::MathMacroTable(int n) : max_macros(n)
 {
     macro_table = new MathMacroTemplateP[max_macros];
     num_macros = 0;
@@ -436,23 +437,23 @@ MathMacroTable::~MathMacroTable()
 
 
 // The search is currently linear but will be binary or hash, later.
-MathMacroTemplate *MathMacroTable::getTemplate(char const* name) const
+MathMacroTemplate * MathMacroTable::getTemplate(char const * name) const
 {
-    for (int i=0; i<num_macros; i++) {
-      if (strcmp(name, macro_table[i]->GetName())==0) 
+    for (int i = 0; i < num_macros; ++i) {
+      if (strcmp(name, macro_table[i]->GetName()) == 0) 
        return macro_table[i];
     }
     
     return 0;
 }
 
-void MathMacroTable::addTemplate(MathMacroTemplate *m)
+void MathMacroTable::addTemplate(MathMacroTemplate * m)
 {
-    if (num_macros<max_macros)
+    if (num_macros < max_macros)
       macro_table[num_macros++] = m;
     else
-      lyxerr.print("Error (MathMacroTable::addTemplate): "
-                  "Macro table exhausted!");
+           lyxerr << "Error (MathMacroTable::addTemplate): "
+                   "Macro table exhausted!" << endl;
 }
 
 
@@ -463,19 +464,18 @@ void MathMacroTable::addTemplate(MathMacroTemplate *m)
 
 void MathMacroTable::builtinMacros()
 {
-    MathMacroTemplate *m;
     MathedIter iter;
-    MathParInset* inset;// *arg;
-    LyxArrayBase *array, *array2;
+    MathParInset * inset;// *arg;
+    LyxArrayBase * array2;
     
     built = true;
     
-    lyxerr.debug("Building macros", Error::MATHED);
+    lyxerr[Debug::MATHED] << "Building macros" << endl;
     
     // This macro doesn't have arguments
-    m = new MathMacroTemplate("notin");  // this leaks
+    MathMacroTemplate * m = new MathMacroTemplate("notin");  // this leaks
     addTemplate(m);
-    array = new LyxArrayBase; // this leaks
+    LyxArrayBase * array = new LyxArrayBase; // this leaks
     iter.SetData(array);
     iter.Insert(new MathAccentInset(LM_in, LM_TC_BOPS, LM_not)); // this leaks
     m->SetData(array);
@@ -522,10 +522,10 @@ void MathMacroTable::builtinMacros()
     addTemplate(m);
     array = new LyxArrayBase; // this leaks
     iter.SetData(array);
-    arg = new MathMatrixInset(2,1); // this leaks
+    arg = new MathMatrixInset(2, 1); // this leaks
 
     m->setArgument(arg);
-    arg->SetAlign('c',"ll");
+    arg->SetAlign('c', "ll");
     iter.Insert(arg, LM_TC_ACTIVE_INSET);
     inset = new MathDelimInset('{', '.'); // this leaks
     inset->SetData(array);
@@ -538,7 +538,7 @@ void MathMacroTable::builtinMacros()
     // the environment substack has 1 argument
     m = new MathMacroTemplate("substack", 1, MMF_Env); // this leaks
     addTemplate(m);     
-    arg = new MathMatrixInset(1,1); // this leaks
+    arg = new MathMatrixInset(1, 1); // this leaks
     m->setArgument(arg);
     arg->SetType(LM_OT_MACRO);
     array = new LyxArrayBase; // this leaks
@@ -549,5 +549,4 @@ void MathMacroTable::builtinMacros()
 
 
 MathMacroTable MathMacroTable::mathMTable(255);
-bool  MathMacroTable::built = false;
-
+bool MathMacroTable::built = false;