]> 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 c4d8bfddac9c30e8e9f533457b62f39aa899e208..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.
  *
@@ -30,6 +30,8 @@
 #include "support/lstrings.h"
 #include "debug.h"
 
+using std::ostream;
+using std::endl;
 
 ostream & operator<<(ostream & o, MathedTextCodes mtc)
 {
@@ -47,7 +49,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&);
 
@@ -119,14 +120,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);
 }
@@ -158,29 +158,20 @@ void MathMacro::SetFocus(int x, int y)
 }
 
 
-void MathMacro::Write(ostream & os)
-{
-   string output;
-   MathMacro::Write(output);
-   os << output;
-}
-
-
-void MathMacro::Write(string &file)
+void MathMacro::Write(ostream & os, bool fragile)
 {
     if (tmplate->flags & MMF_Exp) {
            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 += '[';
@@ -189,29 +180,28 @@ void MathMacro::Write(string &file)
 //      }
        
        if (!(tmplate->flags & MMF_Env) && nargs > 0) 
-         file += '{';
+               os << '{';
        
        for (int i = 0; i < nargs; ++i) {
            array = args[i].array;
-           MathParInset::Write(file);
+           MathParInset::Write(os, fragile);
            if (i < nargs - 1)  
-             file += "}{";
+                   os << "}{";
        }   
        if (tmplate->flags & MMF_Env) {
-           file += "\\end{";
-           file += name;
-           file += '}';
+               os << "\\end{"
+                  << name
+                  << '}';
        } else {
            if (nargs > 0) 
-               file += '}';
+                   os << '}';
            else
-               file += ' ';
+                   os << ' ';
        }
     }
 }
 
 
-
 /*---------------  Macro argument -----------------------------------*/
 
 MathMacroArgument::MathMacroArgument(int n)
@@ -222,14 +212,23 @@ 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(reinterpret_cast<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
     }
 }
 
@@ -239,30 +238,35 @@ void MathMacroArgument::Metrics()
     if (expnd_mode) {
        MathParInset::Metrics();
     } else {
-       unsigned char s[3];
-       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);
+#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(ostream & os)
-{
-   string output;
-   MathMacroArgument::Write(output);
-   os << output;
-}
-
-
-void MathMacroArgument::Write(string & file)
+void MathMacroArgument::Write(ostream & os, bool fragile)
 {
     if (expnd_mode) {
-       MathParInset::Write(file);
+       MathParInset::Write(os, fragile);
     } else {
-       file += '#';
-       file += tostr(number);
-       file += ' ';
+           os << '#' << number << ' ';
     }
 }
 
@@ -310,10 +314,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 +329,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) {
@@ -371,7 +375,7 @@ void MathMacroTemplate::update(MathMacro * macro)
 }
     
 
-void MathMacroTemplate::WriteDef(ostream & os)
+void MathMacroTemplate::WriteDef(ostream & os, bool fragile)
 {
        os << "\n\\newcommand{\\" << name << "}";
       
@@ -383,33 +387,11 @@ void MathMacroTemplate::WriteDef(ostream & os)
     for (int i = 0; i < nargs; ++i) {
        args[i].setExpand(false);
     }   
-    Write(os); 
+    Write(os, fragile); 
     os << "}\n";
 }
 
 
-void MathMacroTemplate::WriteDef(string &file)
-{
-    file += "\n\\newcommand{\\";
-    file += name;
-    file += '}';
-      
-    if (nargs > 0 ) {
-      file += '[';
-      file += tostr(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)
 {
     args[i].SetData(a);
@@ -441,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;