]> git.lyx.org Git - features.git/blobdiff - src/mathed/math_macro.C
apply the ostream changes to mathed, some other small related things
[features.git] / src / mathed / math_macro.C
index 0a269752cbae2b9edc4a92e6fc7da7c0806e7f55..ca3dd9f0922de63627eb9647a0cb3a3c9221e436 100644 (file)
@@ -47,7 +47,6 @@ 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&);
 
@@ -58,7 +57,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 
@@ -66,14 +65,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;
@@ -82,7 +82,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;
@@ -92,7 +92,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;
@@ -103,17 +103,13 @@ MathMacro::~MathMacro()
 
 MathedInset * MathMacro::Clone()
 {
-#if 0
-    MathMacro *m = new MathMacro(this);
-    return m;
-#endif
     return new MathMacro(this);
 }
 
 
 void MathMacro::Metrics()
 {
-    if (nargs>0)
+    if (nargs > 0)
       tmplate->update(this);
     tmplate->Metrics();
     width = tmplate->Width();
@@ -122,15 +118,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);
 }
 
@@ -141,7 +136,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;
 }
@@ -149,9 +144,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);
@@ -159,14 +156,57 @@ void MathMacro::SetFocus(int x, int y)
 }
 
 
-void MathMacro::Write(FILE *file)
+void MathMacro::Write(ostream & os)
 {
+#ifdef USE_OSTREAM_ONLY
+    if (tmplate->flags & MMF_Exp) {
+           lyxerr[Debug::MATHED] << "Expand " << tmplate->flags
+                                 << ' ' << MMF_Exp << endl; 
+       tmplate->update(this);
+       tmplate->Write(os);
+    } 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);
+           if (i < nargs - 1)  
+                   os << "}{";
+       }   
+       if (tmplate->flags & MMF_Env) {
+               os << "\\end{"
+                  << name
+                  << '}';
+       } else {
+           if (nargs > 0) 
+                   os << '}';
+           else
+                   os << ' ';
+       }
+    }
+#else
    string output;
    MathMacro::Write(output);
-   fprintf(file, "%s", output.c_str());
+   os << output;
+#endif
 }
 
 
+#ifndef USE_OSTREAM_ONLY
 void MathMacro::Write(string &file)
 {
     if (tmplate->flags & MMF_Exp) {
@@ -189,13 +229,13 @@ void MathMacro::Write(string &file)
 //       file += ']';
 //      }
        
-       if (!(tmplate->flags & MMF_Env) && nargs>0) 
+       if (!(tmplate->flags & MMF_Env) && nargs > 0) 
          file += '{';
        
-       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)  
+           if (i < nargs - 1)  
              file += "}{";
        }   
        if (tmplate->flags & MMF_Env) {
@@ -203,14 +243,14 @@ void MathMacro::Write(string &file)
            file += name;
            file += '}';
        } else {
-           if (nargs>0) 
+           if (nargs > 0) 
                file += '}';
            else
                file += ' ';
        }
     }
 }
-
+#endif
 
 
 /*---------------  Macro argument -----------------------------------*/
@@ -222,14 +262,15 @@ 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);
+       sprintf(reinterpret_cast<char*>(s), "#%d", number);
+       drawStr(pain, LM_TC_TEX, size, x, baseline, &s[0], 2);
     }
 }
 
@@ -240,20 +281,31 @@ void MathMacroArgument::Metrics()
        MathParInset::Metrics();
     } else {
        unsigned char s[3];
-       sprintf((char*)s, "#%d", number);
+       sprintf(reinterpret_cast<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);
     }
 }
 
-void MathMacroArgument::Write(FILE *file)
+
+void MathMacroArgument::Write(ostream & os)
 {
+#ifdef USE_OSTREAM_ONLY
+    if (expnd_mode) {
+       MathParInset::Write(os);
+    } else {
+           os << '#' << number << ' ';
+    }
+#else
    string output;
    MathMacroArgument::Write(output);
-   fprintf(file, "%s", output.c_str());
+   os << output;
+#endif
 }
 
-void MathMacroArgument::Write(string &file)
+
+#ifndef USE_OSTREAM_ONLY
+void MathMacroArgument::Write(string & file)
 {
     if (expnd_mode) {
        MathParInset::Write(file);
@@ -263,19 +315,20 @@ void MathMacroArgument::Write(string &file)
        file += ' ';
     }
 }
+#endif
 
 
 /* --------------------- 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;
@@ -285,7 +338,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;
@@ -296,65 +349,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());
@@ -367,23 +422,25 @@ void MathMacroTemplate::update(MathMacro* macro)
 }
     
 
-void MathMacroTemplate::WriteDef(FILE *file)
+void MathMacroTemplate::WriteDef(ostream & os)
 {
-    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); 
+    os << "}\n";
 }
 
-void MathMacroTemplate::WriteDef(string &file)
+
+#ifndef USE_OSTREAM_ONLY
+void MathMacroTemplate::WriteDef(string & file)
 {
     file += "\n\\newcommand{\\";
     file += name;
@@ -397,33 +454,36 @@ void MathMacroTemplate::WriteDef(string &file)
     
     file += '{';
     
-    for (int i= 0; i<nargs; i++) {
+    for (int i = 0; i < nargs; ++i) {
        args[i].setExpand(false);
     }   
     Write(file); 
     file += "}\n";
 }
+#endif
+
 
-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;
@@ -434,7 +494,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;
@@ -448,9 +508,9 @@ 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++) {
+    for (int i = 0; i < num_macros; ++i) {
       if (strcmp(name, macro_table[i]->GetName()) == 0) 
        return macro_table[i];
     }
@@ -458,9 +518,9 @@ MathMacroTemplate *MathMacroTable::getTemplate(char const* name) const
     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 << "Error (MathMacroTable::addTemplate): "
@@ -475,19 +535,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::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);
@@ -561,5 +620,4 @@ void MathMacroTable::builtinMacros()
 
 
 MathMacroTable MathMacroTable::mathMTable(255);
-bool  MathMacroTable::built = false;
-
+bool MathMacroTable::built = false;