]> 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 749f0aafa4ddbd0a2c020729e2f92e269a80f3d8..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&);
 
@@ -66,7 +65,7 @@ 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;
@@ -119,14 +118,13 @@ 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);
+    tmplate->draw(pain, x, y);
     for (int i = 0; i < nargs; ++i)
       tmplate->GetMacroXY(i, args[i].x, args[i].y);
 }
@@ -160,12 +158,55 @@ void MathMacro::SetFocus(int x, int y)
 
 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);
    os << output;
+#endif
 }
 
 
+#ifndef USE_OSTREAM_ONLY
 void MathMacro::Write(string &file)
 {
     if (tmplate->flags & MMF_Exp) {
@@ -209,7 +250,7 @@ void MathMacro::Write(string &file)
        }
     }
 }
-
+#endif
 
 
 /*---------------  Macro argument -----------------------------------*/
@@ -222,14 +263,14 @@ MathMacroArgument::MathMacroArgument(int n)
 }
 
 
-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,7 +281,7 @@ 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);
     }
@@ -249,12 +290,21 @@ void MathMacroArgument::Metrics()
 
 void MathMacroArgument::Write(ostream & os)
 {
+#ifdef USE_OSTREAM_ONLY
+    if (expnd_mode) {
+       MathParInset::Write(os);
+    } else {
+           os << '#' << number << ' ';
+    }
+#else
    string output;
    MathMacroArgument::Write(output);
    os << output;
+#endif
 }
 
 
+#ifndef USE_OSTREAM_ONLY
 void MathMacroArgument::Write(string & file)
 {
     if (expnd_mode) {
@@ -265,6 +315,7 @@ void MathMacroArgument::Write(string & file)
        file += ' ';
     }
 }
+#endif
 
 
 /* --------------------- MathMacroTemplate ---------------------------*/
@@ -273,11 +324,11 @@ 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;
@@ -287,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;
@@ -310,10 +361,10 @@ void MathMacroTemplate::setEditMode(bool ed)
 }
 
 
-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) {
            args[i].setExpand(false);
@@ -325,7 +376,7 @@ void MathMacroTemplate::Draw(int x, int y)
        }
       x2 = xo; y2 = yo;
     }
-    MathParInset::Draw(x, y);
+    MathParInset::draw(pain, x, y);
     xo = x2; yo = y2;
     
     for (int i = 0; i < nargs; ++i) {
@@ -339,17 +390,17 @@ 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);
     }
 }
@@ -358,7 +409,7 @@ void MathMacroTemplate::Metrics()
 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());
@@ -388,7 +439,8 @@ void MathMacroTemplate::WriteDef(ostream & os)
 }
 
 
-void MathMacroTemplate::WriteDef(string &file)
+#ifndef USE_OSTREAM_ONLY
+void MathMacroTemplate::WriteDef(string & file)
 {
     file += "\n\\newcommand{\\";
     file += name;
@@ -408,6 +460,7 @@ void MathMacroTemplate::WriteDef(string &file)
     Write(file); 
     file += "}\n";
 }
+#endif
 
 
 void MathMacroTemplate::setArgument(LyxArrayBase * a, int i)
@@ -424,7 +477,7 @@ void MathMacroTemplate::GetMacroXY(int i, int & x, int & y) 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;
 }
 
 
@@ -441,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;