]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_matrixinset.C
use stream-like syntax for LaTeX output
[lyx.git] / src / mathed / math_matrixinset.C
index 0401b8ba174d5f01b70b3e133df79acaa836cab0..690dc0acad3fe3c4c1d9ca5a1e8018e0cf6a6834 100644 (file)
 
 namespace {
 
+       int getCols(MathInsetTypes type)
+       {
+               switch (type) {
+                       case LM_OT_EQNARRAY:
+                               return 3;
+                       case LM_OT_ALIGN:
+                       case LM_OT_ALIGNAT:
+                       case LM_OT_XALIGNAT:
+                       case LM_OT_XXALIGNAT:
+                               return 2;
+                       default:;
+               }
+               return 1;
+       }
 
-int getCols(MathInsetTypes type)
-{
-       switch (type) {
-               case LM_OT_EQNARRAY:
-                       return 3;
-               case LM_OT_ALIGN:
-               case LM_OT_ALIGNAT:
-               case LM_OT_XALIGNAT:
-               case LM_OT_XXALIGNAT:
-                       return 2;
-               default:;
+
+       // returns position of first relation operator in the array
+       // used for "intelligent splitting"
+       int firstRelOp(MathArray const & ar)
+       {
+               for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
+                       if ((*it)->isRelOp())
+                               return it - ar.begin();
+               return ar.size();
        }
-       return 1;
-}
 
 
-// returns position of first relation operator in the array
-// used for "intelligent splitting"
-int firstRelOp(MathArray const & array)
-{
-       for (MathArray::const_iterator it = array.begin(); it != array.end(); ++it)
-               if ((*it)->isRelOp())
-                       return it - array.begin();
-       return array.size();
-}
+       char const * star(bool numbered)
+       {
+               return numbered ? "" : "*";
+       }
 
+       MathInsetTypes typecode(string const & s)
+       {
+               if (s == "equation")  return LM_OT_EQUATION;
+               if (s == "display")   return LM_OT_EQUATION;
+               if (s == "eqnarray")  return LM_OT_EQNARRAY;
+               if (s == "align")     return LM_OT_ALIGN;
+               if (s == "alignat")   return LM_OT_ALIGNAT;
+               if (s == "xalignat")  return LM_OT_XALIGNAT;
+               if (s == "xxalignat") return LM_OT_XXALIGNAT;
+               if (s == "multline")  return LM_OT_MULTLINE;
+               if (s == "gather")    return LM_OT_GATHER;
+               return LM_OT_SIMPLE;
+       }       
 
-char const * star(bool numbered)
-{
-       return numbered ? "" : "*";
-}
 
-}
+       string normalName(MathInsetTypes t)
+       {
+               switch (t) {
+                       case LM_OT_EQUATION:  return "equation";
+                       case LM_OT_EQNARRAY:  return "eqnarray";
+                       case LM_OT_ALIGN:     return "align";
+                       case LM_OT_ALIGNAT:   return "alignat";
+                       case LM_OT_XALIGNAT:  return "xalignat";
+                       case LM_OT_XXALIGNAT: return "xxalignat";
+                       case LM_OT_MULTLINE:  return "multline";
+                       case LM_OT_GATHER:    return "gather";
+                       case LM_OT_SIMPLE:    return "simple";
+                       default: break;
+               }
+               return "unknown";
+       }       
+
+} // end anon namespace
 
 
 MathMatrixInset::MathMatrixInset()
@@ -64,7 +95,7 @@ MathMatrixInset::MathMatrixInset(MathInsetTypes t)
 }
 
 
-MathMatrixInset::MathMatrixInset(MathInsetTypes t, int cols)
+MathMatrixInset::MathMatrixInset(MathInsetTypes t, col_type cols)
        : MathGridInset(cols, 1), objtype_(t), nonum_(1), label_(1)
 {
        setDefaults();
@@ -77,7 +108,7 @@ MathInset * MathMatrixInset::clone() const
 }
 
 
-char MathMatrixInset::defaultColAlign(int col)
+char MathMatrixInset::defaultColAlign(col_type col)
 {
        switch (getType()) {
                case LM_OT_ALIGN:
@@ -92,7 +123,8 @@ char MathMatrixInset::defaultColAlign(int col)
        return 'c';
 }
 
-int MathMatrixInset::defaultColSpace(int col)
+
+int MathMatrixInset::defaultColSpace(col_type col)
 {
        switch (getType()) {
                case LM_OT_ALIGN:
@@ -108,9 +140,10 @@ int MathMatrixInset::defaultColSpace(int col)
 }
 
 
-void MathMatrixInset::metrics(MathStyles) const
+void MathMatrixInset::metrics(MathMetricsInfo const & st) const
 {
-       size_ = (getType() == LM_OT_SIMPLE) ? LM_ST_TEXT : LM_ST_DISPLAY;
+       size_ = st;
+       size_.size = (getType() == LM_OT_SIMPLE) ? LM_ST_TEXT : LM_ST_DISPLAY;
 
        // let the cells adjust themselves
        MathGridInset::metrics(size_);
@@ -122,7 +155,7 @@ void MathMatrixInset::metrics(MathStyles) const
 
        if (numberedType()) {
                int l = 0;
-               for (int row = 0; row < nrows(); ++row)
+               for (row_type row = 0; row < nrows(); ++row)
                        l = std::max(l, mathed_string_width(LM_TC_BF, size(), nicelabel(row)));
 
                if (l)
@@ -147,7 +180,7 @@ void MathMatrixInset::draw(Painter & pain, int x, int y) const
 
        if (numberedType()) {
                int xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
-               for (int row = 0; row < nrows(); ++row) {
+               for (row_type row = 0; row < nrows(); ++row) {
                        int yy = y + rowinfo_[row].offset_;
                        drawStr(pain, LM_TC_BF, size(), xx, yy, nicelabel(row));
                }
@@ -155,17 +188,15 @@ void MathMatrixInset::draw(Painter & pain, int x, int y) const
 }
 
 
-void MathMatrixInset::write(std::ostream & os, bool fragile) const
+void MathMatrixInset::write(MathWriteInfo & os) const
 {
-  header_write(os);
+  header_write(os.os);
 
        bool n = numberedType();
 
-       for (int row = 0; row < nrows(); ++row) {
-               for (int col = 0; col < ncols(); ++col) {
-                       cell(index(row, col)).write(os, fragile);
-                       os << eocString(col);
-               }
+       for (row_type row = 0; row < nrows(); ++row) {
+               for (col_type col = 0; col < ncols(); ++col) 
+                       os << cell(index(row, col)) << eocString(col);
                if (n) {
                        if (!label_[row].empty())
                                os << "\\label{" << label_[row] << "}";
@@ -175,29 +206,38 @@ void MathMatrixInset::write(std::ostream & os, bool fragile) const
                os << eolString(row);
        }
 
-  footer_write(os);
+  footer_write(os.os);
 }
 
 
-string MathMatrixInset::label(int row) const
+void MathMatrixInset::writeNormal(std::ostream & os) const
+{
+       os << "[formula " << normalName(getType()) << " ";
+       MathGridInset::writeNormal(os);
+       os << "] ";
+}
+
+
+
+string MathMatrixInset::label(row_type row) const
 {
        return label_[row];
 }
 
 
-void MathMatrixInset::label(int row, string const & label)
+void MathMatrixInset::label(row_type row, string const & label)
 {
        label_[row] = label; 
 }
 
 
-void MathMatrixInset::numbered(int row, bool num)
+void MathMatrixInset::numbered(row_type row, bool num)
 {
        nonum_[row] = !num; 
 }
 
 
-bool MathMatrixInset::numbered(int row) const
+bool MathMatrixInset::numbered(row_type row) const
 {
        return !nonum_[row];
 }
@@ -218,7 +258,7 @@ bool MathMatrixInset::display() const
 std::vector<string> const MathMatrixInset::getLabelList() const
 {
        std::vector<string> res;
-       for (int row = 0; row < nrows(); ++row)
+       for (row_type row = 0; row < nrows(); ++row)
                if (!label_[row].empty() && nonum_[row] != 1)
                        res.push_back(label_[row]);
        return res;
@@ -229,7 +269,7 @@ bool MathMatrixInset::numberedType() const
 {
        if (getType() == LM_OT_SIMPLE || getType() == LM_OT_XXALIGNAT)
                return false;
-       for (int row = 0; row < nrows(); ++row)
+       for (row_type row = 0; row < nrows(); ++row)
                if (!nonum_[row])
                        return true;
        return false;
@@ -275,7 +315,7 @@ void MathMatrixInset::header_write(std::ostream & os) const
                        break;
 
                case LM_OT_ALIGN:
-                       os << "\\begin{align" << star(n) << "}";
+                       os << "\\begin{align" << star(n) << "}\n";
                        break;
 
                case LM_OT_ALIGNAT:
@@ -321,31 +361,31 @@ void MathMatrixInset::footer_write(std::ostream & os) const
                        break;
 
                case LM_OT_EQNARRAY:
-                       os << "\\end{eqnarray" << star(n) << "}\n";
+                       os << "\n\\end{eqnarray" << star(n) << "}\n";
                        break;
 
                case LM_OT_ALIGN:
-                       os << "\\end{align" << star(n) << "}\n";
+                       os << "\n\\end{align" << star(n) << "}\n";
                        break;
 
                case LM_OT_ALIGNAT:
-                       os << "\\end{alignat" << star(n) << "}\n";
+                       os << "\n\\end{alignat" << star(n) << "}\n";
                        break;
 
                case LM_OT_XALIGNAT:
-                       os << "\\end{xalignat" << star(n) << "}\n";
+                       os << "\n\\end{xalignat" << star(n) << "}\n";
                        break;
 
                case LM_OT_XXALIGNAT:
-                       os << "\\end{xxalignat}\n";
+                       os << "\n\\end{xxalignat}\n";
                        break;
 
                case LM_OT_MULTLINE:
-                       os << "\\end{multline}\n";
+                       os << "\n\\end{multline}\n";
                        break;
 
                case LM_OT_GATHER:
-                       os << "\\end{gather}\n";
+                       os << "\n\\end{gather}\n";
                        break;
 
                default:
@@ -354,7 +394,7 @@ void MathMatrixInset::footer_write(std::ostream & os) const
 }
 
 
-void MathMatrixInset::addRow(int row) 
+void MathMatrixInset::addRow(row_type row) 
 {
        nonum_.insert(nonum_.begin() + row + 1, !numberedType());
        label_.insert(label_.begin() + row + 1, string());
@@ -370,7 +410,7 @@ void MathMatrixInset::appendRow()
 }
 
 
-void MathMatrixInset::delRow(int row) 
+void MathMatrixInset::delRow(row_type row) 
 {
        MathGridInset::delRow(row);
        nonum_.erase(nonum_.begin() + row);
@@ -378,7 +418,7 @@ void MathMatrixInset::delRow(int row)
 }
 
 
-void MathMatrixInset::addCol(int col)
+void MathMatrixInset::addCol(col_type col)
 {
        switch (getType()) {
                case LM_OT_EQUATION:
@@ -408,7 +448,7 @@ void MathMatrixInset::addCol(int col)
 }
 
 
-void MathMatrixInset::delCol(int col)
+void MathMatrixInset::delCol(col_type col)
 {
        switch (getType()) {
                case LM_OT_ALIGNAT:
@@ -423,7 +463,7 @@ void MathMatrixInset::delCol(int col)
 }
 
 
-string MathMatrixInset::nicelabel(int row) const
+string MathMatrixInset::nicelabel(row_type row) const
 {
        if (nonum_[row])
                return string();
@@ -433,31 +473,6 @@ string MathMatrixInset::nicelabel(int row) const
 }
 
 
-namespace {
-       MathInsetTypes typecode(string const & s)
-       {
-               if (s == "equation")
-                       return LM_OT_EQUATION;
-               if (s == "display")
-                       return LM_OT_EQUATION;
-               if (s == "eqnarray")
-                       return LM_OT_EQNARRAY;
-               if (s == "align")
-                       return LM_OT_ALIGN;
-               if (s == "alignat")
-                       return LM_OT_ALIGN;
-               if (s == "xalignat")
-                       return LM_OT_XALIGNAT;
-               if (s == "xxalignat")
-                       return LM_OT_XXALIGNAT;
-               if (s == "multline")
-                       return LM_OT_MULTLINE;
-               if (s == "gather")
-                       return LM_OT_GATHER;
-               return LM_OT_SIMPLE;
-       }       
-}
-
 void MathMatrixInset::mutate(string const & newtype)
 {
        if (newtype == "dump") {
@@ -468,10 +483,11 @@ void MathMatrixInset::mutate(string const & newtype)
        mutate(typecode(newtype));
 }
 
+
 void MathMatrixInset::glueall()
 {
        MathArray ar;
-       for (int i = 0; i < nargs(); ++i)
+       for (idx_type i = 0; i < nargs(); ++i)
                ar.push_back(cell(i));
        *this = MathMatrixInset(LM_OT_SIMPLE);
        cell(0) = ar;
@@ -520,7 +536,7 @@ void MathMatrixInset::mutate(MathInsetTypes newtype)
                                        MathGridInset::addCol(1);
 
                                        // split it "nicely"
-                                       int pos = firstRelOp(cell(0));  
+                                       pos_type pos = firstRelOp(cell(0));     
                                        cell(1) = cell(0);
                                        cell(0).erase(pos, cell(0).size());
                                        cell(1).erase(0, pos);
@@ -535,7 +551,7 @@ void MathMatrixInset::mutate(MathInsetTypes newtype)
                                        MathGridInset::addCol(1);
 
                                        // split it "nicely" on the firest relop
-                                       int pos = firstRelOp(cell(0));  
+                                       pos_type pos = firstRelOp(cell(0));     
                                        cell(1) = cell(0);
                                        cell(0).erase(pos, cell(0).size());
                                        cell(1).erase(0, pos);
@@ -558,16 +574,16 @@ void MathMatrixInset::mutate(MathInsetTypes newtype)
                                case LM_OT_EQUATION: {
                                        // set correct (no)numbering
                                        bool allnonum = true;
-                                       for (int r = 0; r < nrows(); ++r) {
-                                               if (!nonum_[r])
+                                       for (row_type row = 0; row < nrows(); ++row) {
+                                               if (!nonum_[row])
                                                        allnonum = false;
                                        }
 
                                        // set first non-empty label
                                        string label;
-                                       for (int r = 0; r < nrows(); ++r) {
-                                               if (!label_[r].empty()) {
-                                                       label = label_[r];
+                                       for (row_type row = 0; row < nrows(); ++row) {
+                                               if (!label_[row].empty()) {
+                                                       label = label_[row];
                                                        break;
                                                }
                                        }
@@ -585,8 +601,8 @@ void MathMatrixInset::mutate(MathInsetTypes newtype)
                                case LM_OT_XALIGNAT:
                                case LM_OT_XXALIGNAT:
                                default: {
-                                       for (int row = 0; row < nrows(); ++row) {
-                                               int c = 3 * row + 1;
+                                       for (row_type row = 0; row < nrows(); ++row) {
+                                               idx_type c = 3 * row + 1;
                                                cell(c).push_back(cell(c + 1));
                                        }
                                        MathGridInset::delCol(2);