]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/formula.C
first go at mathed file cleanup
[lyx.git] / src / mathed / formula.C
index 386be2062b7c1280178b55652f9d49c79abdb062..67363caa0c79af950aa495617fd1e062865befb7 100644 (file)
 #include "font.h"
 #include "support/lyxlib.h"
 #include "lyxrc.h"
+#include "math_defs.h"
+#include "math_inset.h"
+#include "math_parinset.h"
+#include "math_matrixinset.h"
+#include "math_rowst.h"
+#include "math_spaceinset.h"
+#include "math_deliminset.h"
+#include "support.h"
 
 using std::ostream;
 using std::istream;
@@ -72,10 +80,7 @@ static bool sel_flag;
 
 MathedCursor * InsetFormula::mathcursor = 0; 
 
-
-int MathedInset::df_asc;
-int MathedInset::df_des;
-int MathedInset::df_width;
+void mathed_init_fonts();
 
 
 static
@@ -84,70 +89,70 @@ void mathedValidate(LaTeXFeatures & features, MathParInset * par);
 
 LyXFont WhichFont(short type, int size)
 {
-    LyXFont f;
+       LyXFont f;
     
-      if (!Math_Fonts)
-       mathed_init_fonts();
-   
-   switch (type) {
-    case LM_TC_SYMB:        
-      f = Math_Fonts[2];
-      break;
-    case LM_TC_BSYM:        
-      f = Math_Fonts[2];
-      break;
-    case LM_TC_VAR:
-    case LM_TC_IT:
-      f = Math_Fonts[0];
-      break;
-    case LM_TC_BF:
-      f = Math_Fonts[3];
-      break;
-    case LM_TC_SF:
-      f = Math_Fonts[7];
-      break;
-    case LM_TC_CAL:
-      f = Math_Fonts[4];
-      break;
-    case LM_TC_TT:
-      f = Math_Fonts[5];
-      break;
-    case LM_TC_SPECIAL: //f = Math_Fonts[0]; break;
-    case LM_TC_TEXTRM:
-    case LM_TC_RM:    
-      f = Math_Fonts[6];
-      break;
-    default:
-      f = Math_Fonts[1];
-      break;   
-   }
-    
-    f.setSize(lfont_size);
-    
-    switch (size) {
-     case LM_ST_DISPLAY:     
-       if (type == LM_TC_BSYM) {
-           f.incSize();
-           f.incSize();
+       if (!Math_Fonts)
+               mathed_init_fonts();
+       
+       switch (type) {
+       case LM_TC_SYMB:             
+               f = Math_Fonts[2];
+               break;
+       case LM_TC_BSYM:             
+               f = Math_Fonts[2];
+               break;
+       case LM_TC_VAR:
+       case LM_TC_IT:
+               f = Math_Fonts[0];
+               break;
+       case LM_TC_BF:
+               f = Math_Fonts[3];
+               break;
+       case LM_TC_SF:
+               f = Math_Fonts[7];
+               break;
+       case LM_TC_CAL:
+               f = Math_Fonts[4];
+               break;
+       case LM_TC_TT:
+               f = Math_Fonts[5];
+               break;
+       case LM_TC_SPECIAL: //f = Math_Fonts[0]; break;
+       case LM_TC_TEXTRM:
+       case LM_TC_RM:    
+               f = Math_Fonts[6];
+               break;
+       default:
+               f = Math_Fonts[1];
+               break;   
        }
+       
+       f.setSize(lfont_size);
+       
+       switch (size) {
+       case LM_ST_DISPLAY:     
+               if (type == LM_TC_BSYM) {
+                       f.incSize();
+                       f.incSize();
+               }
        break;
-     case LM_ST_TEXT:
-       break;
-     case LM_ST_SCRIPT:
-       f.decSize();
-       break;
-     case LM_ST_SCRIPTSCRIPT:
-       f.decSize();
-       f.decSize();
-       break;
-     default:
-            lyxerr << "Mathed Error: wrong font size: " << size << endl;
+       case LM_ST_TEXT:
+               break;
+       case LM_ST_SCRIPT:
+               f.decSize();
+               break;
+       case LM_ST_SCRIPTSCRIPT:
+               f.decSize();
+               f.decSize();
        break;
-    }
-
-    if (type != LM_TC_TEXTRM) 
-      f.setColor(LColor::math);
-    return f;
+       default:
+               lyxerr << "Mathed Error: wrong font size: " << size << endl;
+               break;
+       }
+       
+       if (type != LM_TC_TEXTRM) 
+               f.setColor(LColor::math);
+       return f;
 }
 
 
@@ -187,112 +192,34 @@ void mathed_init_fonts() //removed 'static' because DEC cxx does not
 }
 
 
-LyXFont mathed_get_font(short type, int size)
-{
-       LyXFont f = WhichFont(type, size);
-       if (type == LM_TC_TEX) {
-               f.setLatex(LyXFont::ON);
-       }
-       return f;
-}
 
 
-int mathed_string_width(short type, int size, byte const * s, int ls)
-{
-       string st;
-       if (MathIsBinary(type))
-               for (int i = 0; i < ls; ++i) {
-                       st += ' ';
-                       st += s[i];
-                       st += ' ';
-               }
-       else
-               st = string(reinterpret_cast<char const *>(s), ls);
 
-       LyXFont const f = WhichFont(type, size);
-       return lyxfont::width(st, f);
-}
 
-int mathed_string_width(short type, int size, string const & str)
-{
-       return mathed_string_width(type, size, reinterpret_cast<unsigned char const *>(str.c_str()), str.length());
-}
 
 
-int mathed_char_width(short type, int size, byte c)
-{
-    int t = (MathIsBinary(type)) ? mathed_string_width(type, size, &c, 1) :
-           lyxfont::width(c, WhichFont(type, size));
-    return t;
-}
-
-
-int mathed_string_height(short type, int size, byte const * s,
-                        int ls, int & asc, int & des)
-{
-   LyXFont font = WhichFont(type, size);
-   asc = des = 0;
-   for (int i = 0; i < ls; ++i) {
-      des = max(des, lyxfont::descent(s[i], font));
-      asc = max(asc, lyxfont::ascent(s[i], font));
-   }
-   return asc + des;
-}
-
-
-int mathed_string_height(short type, int size, string const & str,
-                        int & asc, int & des)
-{
-       return mathed_string_height(type, size,
-                                   reinterpret_cast<unsigned char const *>(str.c_str()), str.length(),
-                                   asc, des);
-}
-
-
-int mathed_char_height(short type, int size, byte c, int & asc, int & des)
-{
-   LyXFont font = WhichFont(type, size);
-   des = lyxfont::descent(c, font);
-   asc = lyxfont::ascent(c, font);
-   return asc + des;
-}
 
 
-// In a near future maybe we use a better fonts renderer
-void MathedInset::drawStr(Painter & pain, short type, int siz,
-                         int x, int y, byte const * s, int ls)
-{
-       string st;
-       if (MathIsBinary(type))
-               for (int i = 0; i < ls; ++i) {
-                       st += ' ';
-                       st += char(s[i]);
-                       st += ' ';
-               }
-       else
-               st = string(reinterpret_cast<char const *>(s), ls);
-
-       LyXFont const mf = mathed_get_font(type, siz);
-       pain.text(x, y, st, mf);
-}
 
 
 InsetFormula::InsetFormula(bool display)
 {
-  par = new MathParInset; // this leaks
-  //   mathcursor = 0;
-  disp_flag = display;
-  //label = 0;
-  if (disp_flag) {
-    par->SetType(LM_OT_PAR);
-    par->SetStyle(LM_ST_DISPLAY);
-  }
+       par = new MathParInset; // this leaks
+       //   mathcursor = 0;
+       disp_flag = display;
+       //label = 0;
+       if (disp_flag) {
+               par->SetType(LM_OT_PAR);
+               par->SetStyle(LM_ST_DISPLAY);
+       }
 }
 
 
 InsetFormula::InsetFormula(MathParInset * p)
 {
-   par = (p->GetType()>= LM_OT_MPAR) ? 
+   if (is_matrix_type(p->GetType()))
+          lyxerr << "InsetFormula::InsetFormula: This shouldn't happen" << endl; 
+   par = is_multiline(p->GetType()) ? 
          new MathMatrixInset(static_cast<MathMatrixInset*>(p)): 
          new MathParInset(p);
 //   mathcursor = 0;
@@ -355,9 +282,13 @@ int InsetFormula::DocBook(Buffer const * buf, ostream & os) const
 // Check if uses AMS macros 
 void InsetFormula::Validate(LaTeXFeatures & features) const
 {
-    // Validation only necesary if not using an AMS Style
-    if (!features.amsstyle)
-      mathedValidate(features, par);
+       if (is_ams(par->GetType()))
+               features.amsstyle = true;
+
+       // Validation is necessary only if not using AMS math.
+       // To be safe, we will always run mathedValidate.
+       //if (!features.amsstyle)
+       mathedValidate(features, par);
 }
 
 
@@ -412,8 +343,9 @@ int InsetFormula::descent(BufferView *, LyXFont const &) const
 }
 
 
-int InsetFormula::width(BufferView *, LyXFont const & f) const
+int InsetFormula::width(BufferView * bv, LyXFont const & f) const
 {
+    MathedInset::workWidth = bv->workWidth();
     lfont_size = f.size();
     par->Metrics();
     return par->Width(); //+2;
@@ -423,6 +355,7 @@ int InsetFormula::width(BufferView *, LyXFont const & f) const
 void InsetFormula::draw(BufferView * bv, LyXFont const & f,
                        int baseline, float & x, bool) const
 {
+       MathedInset::workWidth = bv->workWidth();
        Painter & pain = bv->painter();
        // Seems commenting out solves a problem.
        LyXFont font = mathed_get_font(LM_TC_TEXTRM, LM_ST_TEXT);
@@ -446,19 +379,19 @@ void InsetFormula::draw(BufferView * bv, LyXFont const & f,
                par->draw(pain, int(x), baseline);
        }
        x += float(width(bv, font));
-       
-       if (par->GetType() == LM_OT_PARN || par->GetType() == LM_OT_MPARN) {
+
+       if (is_numbered(par->GetType())) {
                LyXFont wfont = WhichFont(LM_TC_BF, par->size);
                wfont.setLatex(LyXFont::OFF);
                
-               if (par->GetType() == LM_OT_PARN) {
+               if (is_singlely_numbered(par->GetType())) {
                        string str;
                        if (!label.empty())
                                str = string("(") + label + ")";
                        else
                                str = string("(#)");
                        pain.text(int(x + 20), baseline, str, wfont);
-               } else if (par->GetType() == LM_OT_MPARN) {
+               } else {
                        MathMatrixInset * mt =
                                static_cast<MathMatrixInset*>(par);
                        int y;
@@ -606,7 +539,7 @@ void InsetFormula::display(bool dspf)
         par->SetType(LM_OT_PAR);
         par->SetStyle(LM_ST_DISPLAY);
       } else {
-        if (par->GetType() >= LM_OT_MPAR) { 
+        if (is_multiline(par->GetType())) { 
            MathParInset * p = new MathParInset(par);
            delete par;
            par = p;
@@ -615,7 +548,7 @@ void InsetFormula::display(bool dspf)
         }
         par->SetType(LM_OT_MIN);
         par->SetStyle(LM_ST_TEXT);
-        if (!label.empty() && par->GetType() != LM_OT_MPARN) {
+        if (!label.empty()) {
                 label.erase();
         }
       }
@@ -631,7 +564,7 @@ vector<string> const InsetFormula::getLabelList() const
 
        vector<string> label_list;
 
-       if (par->GetType() == LM_OT_MPARN) {
+       if (is_multi_numbered(par->GetType())) {
                MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
                MathedRowSt const * crow = mt->getRowSt();
                while (crow) {
@@ -719,7 +652,7 @@ bool InsetFormula::SetNumber(bool numbf)
 {
    if (disp_flag) {
       short type = par->GetType();
-      bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
+      bool oldf = is_numbered(type);
       if (numbf && !oldf) ++type;
       if (!numbf && oldf) --type;
       par->SetType(type);
@@ -793,8 +726,10 @@ InsetFormula::LocalDispatch(BufferView * bv,
       UpdateLocal(bv);
       break;
     case LFUN_BREAKLINE:
+    {
       bv->lockedInsetStoreUndo(Undo::INSERT);
-      mathcursor->Insert(' ', LM_TC_CR);
+      byte c = arg.empty() ? '1' : arg[0];
+      mathcursor->Insert(c, LM_TC_CR);
       if (!label.empty()) {
         mathcursor->setLabel(label);
         label.erase();
@@ -802,6 +737,7 @@ InsetFormula::LocalDispatch(BufferView * bv,
       par = mathcursor->GetPar();
       UpdateLocal(bv);
       break;
+    }
     case LFUN_TAB:
       bv->lockedInsetStoreUndo(Undo::INSERT);
       mathcursor->Insert(0, LM_TC_TAB);
@@ -915,8 +851,7 @@ InsetFormula::LocalDispatch(BufferView * bv,
       bv->lockedInsetStoreUndo(Undo::INSERT);
        if (disp_flag) {
          short type = par->GetType();
-         bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
-         if (oldf) {
+         if (is_numbered(type)) {
             --type;
             if (!label.empty()) {
                     label.erase();
@@ -934,7 +869,7 @@ InsetFormula::LocalDispatch(BufferView * bv,
     
     case LFUN_MATH_NONUMBER:
     { 
-       if (par->GetType() == LM_OT_MPARN) {
+       if (is_multi_numbered(par->GetType())) {
 //        MathMatrixInset *mt = (MathMatrixInset*)par;
           //BUG 
 //        mt->SetNumbered(!mt->IsNumbered());
@@ -1060,8 +995,7 @@ InsetFormula::LocalDispatch(BufferView * bv,
        if (par->GetType() < LM_OT_PAR)
              break;
 
-       string old_label = (par->GetType() == LM_OT_MPARN ||
-                          par->GetType() == LM_OT_MPAR)
+       string old_label = is_multiline(par->GetType())
               ?  mathcursor->getLabel() : label;
 
 #warning This is a terrible hack! We should find a better solution.
@@ -1094,7 +1028,7 @@ InsetFormula::LocalDispatch(BufferView * bv,
        if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
              bv->redraw();
 
-       if (par->GetType() == LM_OT_MPARN)
+       if (is_multi_numbered(par->GetType()))
          mathcursor->setLabel(new_label);
 //       MathMatrixInset *mt = (MathMatrixInset*)par;
 //       mt->SetLabel(new_label);
@@ -1272,34 +1206,6 @@ InsetFormula::LocalDispatch(BufferView * bv,
 }
 
 
-void
-MathFuncInset::draw(Painter & pain, int x, int y)
-{ 
-       if (!name.empty() && name[0] > ' ') {
-               LyXFont font = WhichFont(LM_TC_TEXTRM, size);
-               font.setLatex(LyXFont::ON);
-               x += (lyxfont::width('I', font) + 3) / 4;
-               pain.text(x, y, name, font);
-       }
-}
-
-
-void MathFuncInset::Metrics() 
-{
-       //ln = (name) ? strlen(name): 0;
-       LyXFont  font = WhichFont(LM_TC_TEXTRM, size);
-       font.setLatex(LyXFont::ON);
-       if (name.empty()) {
-               width = df_width;
-               descent = df_des;
-               ascent = df_asc;
-       } else {
-               width = lyxfont::width(name, font)
-                       + lyxfont::width('I', font) / 2;
-               mathed_string_height(LM_TC_TEXTRM, size, name, ascent, descent);
-       }
-}
-
 
 static
 void mathedValidate(LaTeXFeatures & features, MathParInset * par)