]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_matrixinset.C
further code uglification to make Jean-Marc's compiler happy
[lyx.git] / src / mathed / math_matrixinset.C
index a6b8c9e5a42ea4c931217cc006afb52bef52d1db..29ae980d20714d9d1e035bf9a548fde6b17b38e3 100644 (file)
@@ -5,6 +5,7 @@
 #include <vector>
 
 #include "math_matrixinset.h"
+#include "support.h"
 #include "debug.h"
 #include "support/LOstream.h"
 #include "Painter.h"
 
 namespace {
 
-string const getAlign(short int type, int cols)
+
+int getCols(MathInsetTypes type)
 {
-       string align;
        switch (type) {
+               case LM_OT_EQNARRAY:
+                       return 3;
                case LM_OT_ALIGN:
-                       for (int i = 0; i < cols; ++i)
-                               align += "Rl";
-                       break;
-
                case LM_OT_ALIGNAT:
-                       for (int i = 0; i < cols; ++i)
-                               align += "rl";
-                       break;
-
-               case LM_OT_MULTLINE:
-                       align = "C";
-                       break;
-
-               default:
-                       align = "rcl";
-                       break;
+               case LM_OT_XALIGNAT:
+               case LM_OT_XXALIGNAT:
+                       return 2;
+               default:;
        }
-       return align;
+       return 1;
 }
 
 
-string const star(bool n)
+// returns position of first relation operator in the array
+// used for "intelligent splitting"
+int firstRelOp(MathArray const & array)
 {
-       return n ? "" : "*";
+       for (MathArray::const_iterator it = array.begin(); it != array.end(); ++it)
+               if (it->nucleus()->isRelOp())
+                       return it - array.begin();
+       return array.size();
 }
 
 
-int getCols(short int type)
+char const * star(bool numbered)
 {
-       int col;
-       switch (type) {
-               case LM_OT_EQNARRAY:
-                       col = 3;
-                       break;
-
-               case LM_OT_ALIGN:
-               case LM_OT_ALIGNAT:
-                       col = 2;
-                       break;
+       return numbered ? "" : "*";
+}
 
-               default:
-                       col = 1;
-       }
-       return col;
 }
 
-// returns position of first relation operator in the array
-// used for "intelligent splitting"
-int firstRelOp(MathArray const & array)
+
+MathMatrixInset::MathMatrixInset()
+       : MathGridInset(1, 1), objtype_(LM_OT_SIMPLE), nonum_(1), label_(1)
 {
-       for (int pos = 0; pos < array.size(); array.next(pos))
-               if (!array.isInset(pos) &&
-                               MathIsRelOp(array.getChar(pos), array.getCode(pos)))
-                       return pos;
-       return array.size();
+       setDefaults();
 }
 
-}
 
 MathMatrixInset::MathMatrixInset(MathInsetTypes t)
-       : MathGridInset(getCols(t), 1, "formula"), objtype_(t), nonum_(1), label_(1)
-{}
+       : MathGridInset(getCols(t), 1), objtype_(t), nonum_(1), label_(1)
+{
+       setDefaults();
+}
 
 
-MathMatrixInset::MathMatrixInset()
-       : MathGridInset(1, 1, "formula"), objtype_(LM_OT_SIMPLE), nonum_(1), label_(1)
-{}
+MathMatrixInset::MathMatrixInset(MathInsetTypes t, unsigned int cols)
+       : MathGridInset(cols, 1), objtype_(t), nonum_(1), label_(1)
+{
+       setDefaults();
+}
+
 
 MathInset * MathMatrixInset::clone() const
 {
@@ -92,7 +77,39 @@ MathInset * MathMatrixInset::clone() const
 }
 
 
-void MathMatrixInset::metrics(MathStyles /* st */)
+char MathMatrixInset::defaultColAlign(unsigned int col)
+{
+       switch (getType()) {
+               case LM_OT_ALIGN:
+               case LM_OT_ALIGNAT:
+               case LM_OT_XALIGNAT:
+               case LM_OT_XXALIGNAT:
+                       return "rl"[col & 1];
+               case LM_OT_EQNARRAY:
+                       return "rcl"[col];
+               default:;
+       }
+       return 'c';
+}
+
+
+int MathMatrixInset::defaultColSpace(unsigned int col)
+{
+       switch (getType()) {
+               case LM_OT_ALIGN:
+               case LM_OT_ALIGNAT:
+                       return 0;
+               case LM_OT_XALIGNAT:
+                       return (col & 1) ? 20 : 0;
+               case LM_OT_XXALIGNAT:
+                       return (col & 1) ? 40 : 0;
+               default:;
+       }
+       return 10;
+}
+
+
+void MathMatrixInset::metrics(MathStyles) const
 {
        size_ = (getType() == LM_OT_SIMPLE) ? LM_ST_TEXT : LM_ST_DISPLAY;
 
@@ -106,16 +123,23 @@ void MathMatrixInset::metrics(MathStyles /* st */)
 
        if (numberedType()) {
                int l = 0;
-               for (int row = 0; row < nrows(); ++row)
+               for (unsigned int row = 0; row < nrows(); ++row)
                        l = std::max(l, mathed_string_width(LM_TC_BF, size(), nicelabel(row)));
 
                if (l)
                        width_ += 30 + l;
        }
+
+       // make it at least as high as the current font
+       int asc = 0;
+       int des = 0;
+       math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, des);
+       ascent_  = std::max(ascent_,  asc);
+       descent_ = std::max(descent_, des);
 }
 
 
-void MathMatrixInset::draw(Painter & pain, int x, int y)
+void MathMatrixInset::draw(Painter & pain, int x, int y) const
 {
        xo(x);
        yo(y);
@@ -124,7 +148,7 @@ void MathMatrixInset::draw(Painter & pain, int x, int y)
 
        if (numberedType()) {
                int xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
-               for (int row = 0; row < nrows(); ++row) {
+               for (unsigned int row = 0; row < nrows(); ++row) {
                        int yy = y + rowinfo_[row].offset_;
                        drawStr(pain, LM_TC_BF, size(), xx, yy, nicelabel(row));
                }
@@ -138,13 +162,10 @@ void MathMatrixInset::write(std::ostream & os, bool fragile) const
 
        bool n = numberedType();
 
-       for (int row = 0; row < nrows(); ++row) {
-               if (row)
-                       os << " \\\\\n";
-               for (int col = 0; col < ncols(); ++col) {
-                       if (col)
-                               os << " & ";
+       for (unsigned int row = 0; row < nrows(); ++row) {
+               for (unsigned int col = 0; col < ncols(); ++col) {
                        cell(index(row, col)).write(os, fragile);
+                       os << eocString(col);
                }
                if (n) {
                        if (!label_[row].empty())
@@ -152,30 +173,32 @@ void MathMatrixInset::write(std::ostream & os, bool fragile) const
                        if (nonum_[row])
                                os << "\\nonumber ";
                }
+               os << eolString(row);
        }
 
   footer_write(os);
 }
 
 
-string MathMatrixInset::label(int row) const
+string MathMatrixInset::label(unsigned int row) const
 {
        return label_[row];
 }
 
-void MathMatrixInset::label(int row, string const & label)
+
+void MathMatrixInset::label(unsigned int row, string const & label)
 {
        label_[row] = label; 
 }
 
 
-void MathMatrixInset::numbered(int row, bool num)
+void MathMatrixInset::numbered(unsigned int row, bool num)
 {
        nonum_[row] = !num; 
 }
 
 
-bool MathMatrixInset::numbered(int row) const
+bool MathMatrixInset::numbered(unsigned int row) const
 {
        return !nonum_[row];
 }
@@ -196,7 +219,7 @@ bool MathMatrixInset::display() const
 std::vector<string> const MathMatrixInset::getLabelList() const
 {
        std::vector<string> res;
-       for (int row = 0; row < nrows(); ++row)
+       for (unsigned int row = 0; row < nrows(); ++row)
                if (!label_[row].empty() && nonum_[row] != 1)
                        res.push_back(label_[row]);
        return res;
@@ -205,9 +228,9 @@ std::vector<string> const MathMatrixInset::getLabelList() const
 
 bool MathMatrixInset::numberedType() const
 {
-       if (getType() == LM_OT_SIMPLE)
+       if (getType() == LM_OT_SIMPLE || getType() == LM_OT_XXALIGNAT)
                return false;
-       for (int row = 0; row < nrows(); ++row)
+       for (unsigned int row = 0; row < nrows(); ++row)
                if (!nonum_[row])
                        return true;
        return false;
@@ -226,7 +249,7 @@ void MathMatrixInset::validate(LaTeXFeatures & features) const
        features.boldsymbol = true;
        //features.binom      = true;
 
-       MathInset::validate(features);
+       MathNestInset::validate(features);
 }
 
 
@@ -257,9 +280,25 @@ void MathMatrixInset::header_write(std::ostream & os) const
                        break;
 
                case LM_OT_ALIGNAT:
-                       os << "\\begin{alignat" << star(n) << "}"
-                          << "{" << ncols()/2 << "}\n";
+                       os << "\\begin{alignat" << star(n) << "}" << "{" << ncols()/2 << "}\n";
+                       break;
+
+               case LM_OT_XALIGNAT:
+                       os << "\\begin{xalignat" << star(n) << "}" << "{" << ncols()/2 << "}\n";
+                       break;
+
+               case LM_OT_XXALIGNAT:
+                       os << "\\begin{xxalignat}" << "{" << ncols()/2 << "}\n";
+                       break;
+
+               case LM_OT_MULTLINE:
+                       os << "\\begin{multline}\n";
+                       break;
+
+               case LM_OT_GATHER:
+                       os << "\\begin{gather}\n";
                        break;
+
                default:
                        os << "\\begin{unknown" << star(n) << "}";
        }
@@ -294,19 +333,36 @@ void MathMatrixInset::footer_write(std::ostream & os) const
                        os << "\\end{alignat" << star(n) << "}\n";
                        break;
 
+               case LM_OT_XALIGNAT:
+                       os << "\\end{xalignat" << star(n) << "}\n";
+                       break;
+
+               case LM_OT_XXALIGNAT:
+                       os << "\\end{xxalignat}\n";
+                       break;
+
+               case LM_OT_MULTLINE:
+                       os << "\\end{multline}\n";
+                       break;
+
+               case LM_OT_GATHER:
+                       os << "\\end{gather}\n";
+                       break;
+
                default:
                        os << "\\end{unknown" << star(n) << "}";
        }
 }
 
 
-void MathMatrixInset::addRow(int row) 
+void MathMatrixInset::addRow(unsigned int row) 
 {
        nonum_.insert(nonum_.begin() + row + 1, !numberedType());
        label_.insert(label_.begin() + row + 1, string());
        MathGridInset::addRow(row);
 }
 
+
 void MathMatrixInset::appendRow()
 {
        nonum_.push_back(!numberedType());
@@ -315,14 +371,15 @@ void MathMatrixInset::appendRow()
 }
 
 
-void MathMatrixInset::delRow(int row) 
+void MathMatrixInset::delRow(unsigned int row) 
 {
        MathGridInset::delRow(row);
        nonum_.erase(nonum_.begin() + row);
        label_.erase(label_.begin() + row);
 }
 
-void MathMatrixInset::addCol(int col)
+
+void MathMatrixInset::addCol(unsigned int col)
 {
        switch (getType()) {
                case LM_OT_EQUATION:
@@ -335,11 +392,15 @@ void MathMatrixInset::addCol(int col)
                        break;
 
                case LM_OT_ALIGN:
+                       mutate(LM_OT_ALIGNAT);
+                       addCol(col);
+                       break;
+
                case LM_OT_ALIGNAT:
+               case LM_OT_XALIGNAT:
+               case LM_OT_XXALIGNAT:
                        MathGridInset::addCol(col);
-                       halign(col, 'l');
-                       MathGridInset::addCol(col);
-                       halign(col, 'r');
+                       MathGridInset::addCol(col + 1);
                        break;
 
                default:
@@ -347,20 +408,23 @@ void MathMatrixInset::addCol(int col)
        }
 }
 
-void MathMatrixInset::delCol(int col)
+
+void MathMatrixInset::delCol(unsigned int col)
 {
        switch (getType()) {
-               case LM_OT_ALIGN:
+               case LM_OT_ALIGNAT:
+               case LM_OT_XALIGNAT:
+               case LM_OT_XXALIGNAT:
+                       MathGridInset::delCol(col + 1);
                        MathGridInset::delCol(col);
                        break;
-
                default:
                        break;
        }
 }
 
 
-string MathMatrixInset::nicelabel(int row) const
+string MathMatrixInset::nicelabel(unsigned int row) const
 {
        if (nonum_[row])
                return string();
@@ -371,7 +435,7 @@ string MathMatrixInset::nicelabel(int row) const
 
 
 namespace {
-       short typecode(string const & s)
+       MathInsetTypes typecode(string const & s)
        {
                if (s == "equation")
                        return LM_OT_EQUATION;
@@ -381,16 +445,21 @@ namespace {
                        return LM_OT_EQNARRAY;
                if (s == "align")
                        return LM_OT_ALIGN;
-               if (s == "xalign")
-                       return LM_OT_XALIGN;
-               if (s == "xxalign")
-                       return LM_OT_XXALIGN;
+               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") {
@@ -401,10 +470,11 @@ void MathMatrixInset::mutate(string const & newtype)
        mutate(typecode(newtype));
 }
 
+
 void MathMatrixInset::glueall()
 {
        MathArray ar;
-       for (int i = 0; i < nargs(); ++i)
+       for (unsigned int i = 0; i < nargs(); ++i)
                ar.push_back(cell(i));
        *this = MathMatrixInset(LM_OT_SIMPLE);
        cell(0) = ar;
@@ -420,11 +490,12 @@ MathInsetTypes MathMatrixInset::getType() const
 void MathMatrixInset::setType(MathInsetTypes t)
 {
        objtype_ = t;
+       setDefaults();
 }
 
 
 
-void MathMatrixInset::mutate(short newtype)
+void MathMatrixInset::mutate(MathInsetTypes newtype)
 {
        //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
 
@@ -444,17 +515,20 @@ void MathMatrixInset::mutate(short newtype)
                                        setType(LM_OT_SIMPLE);
                                        break;
 
-                               case LM_OT_ALIGN: {
+                               case LM_OT_ALIGN: 
+                               case LM_OT_ALIGNAT:
+                               case LM_OT_XALIGNAT:
+                               case LM_OT_XXALIGNAT: {
+
                                        MathGridInset::addCol(1);
 
                                        // split it "nicely"
-                                       int pos = firstRelOp(cell(0));  
+                                       unsigned int pos = firstRelOp(cell(0)); 
                                        cell(1) = cell(0);
                                        cell(0).erase(pos, cell(0).size());
                                        cell(1).erase(0, pos);
-
-                                       halign("rl");
                                        setType(LM_OT_ALIGN);
+                                       mutate(newtype);
                                        break;
                                }
 
@@ -464,17 +538,17 @@ void MathMatrixInset::mutate(short newtype)
                                        MathGridInset::addCol(1);
 
                                        // split it "nicely" on the firest relop
-                                       int pos1 = firstRelOp(cell(0)); 
+                                       unsigned int pos = firstRelOp(cell(0)); 
                                        cell(1) = cell(0);
-                                       cell(0).erase(pos1, cell(0).size());
-                                       cell(1).erase(0, pos1);
-                                       int pos2 = 0;
-                                       cell(1).next(pos2);
-                                       cell(2) = cell(1);
-                                       cell(1).erase(pos2, cell(1).size());
-                                       cell(2).erase(0, pos2);
-
-                                       halign("rcl");
+                                       cell(0).erase(pos, cell(0).size());
+                                       cell(1).erase(0, pos);
+
+                                       if (cell(1).size()) {
+                                               cell(2) = cell(1);
+                                               cell(1).erase(1, cell(1).size());
+                                               cell(2).erase(0);
+                                       }
+
                                        setType(LM_OT_EQNARRAY);
                                        mutate(newtype);
                                        break;
@@ -487,16 +561,16 @@ void MathMatrixInset::mutate(short newtype)
                                case LM_OT_EQUATION: {
                                        // set correct (no)numbering
                                        bool allnonum = true;
-                                       for (int r = 0; r < nrows(); ++r) {
-                                               if (!nonum_[r])
+                                       for (unsigned int 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 (unsigned int row = 0; row < nrows(); ++row) {
+                                               if (!label_[row].empty()) {
+                                                       label = label_[row];
                                                        break;
                                                }
                                        }
@@ -510,14 +584,16 @@ void MathMatrixInset::mutate(short newtype)
                                }
 
                                case LM_OT_ALIGN:
+                               case LM_OT_ALIGNAT:
+                               case LM_OT_XALIGNAT:
+                               case LM_OT_XXALIGNAT:
                                default: {
-                                       for (int row = 0; row < nrows(); ++row) {
-                                               int c = 3 * row + 1;
+                                       for (unsigned int row = 0; row < nrows(); ++row) {
+                                               unsigned int c = 3 * row + 1;
                                                cell(c).push_back(cell(c + 1));
                                        }
                                        MathGridInset::delCol(2);
                                        setType(LM_OT_ALIGN);
-                                       halign("rl");
                                        mutate(newtype);
                                        break;
                                }
@@ -531,10 +607,15 @@ void MathMatrixInset::mutate(short newtype)
                                case LM_OT_EQNARRAY:
                                        MathGridInset::addCol(1);
                                        setType(LM_OT_EQNARRAY);
-                                       halign("rcl");
                                        mutate(newtype);
                                        break;
                                
+                               case LM_OT_ALIGNAT:
+                               case LM_OT_XALIGNAT:
+                               case LM_OT_XXALIGNAT:
+                                       setType(newtype);
+                                       break;
+
                                default:
                                        lyxerr << "mutation from '" << getType()
                                                << "' to '" << newtype << "' not implemented\n";
@@ -542,6 +623,28 @@ void MathMatrixInset::mutate(short newtype)
                        }
                        break;
 
+               case LM_OT_MULTLINE:
+                       switch (newtype) {
+                               case LM_OT_GATHER:
+                                       setType(LM_OT_GATHER);
+                                       break;
+                               default:
+                                       lyxerr << "mutation from '" << getType()
+                                               << "' to '" << newtype << "' not implemented\n";
+                                       break;
+                       }
+
+               case LM_OT_GATHER:
+                       switch (newtype) {
+                               case LM_OT_MULTLINE:
+                                       setType(LM_OT_MULTLINE);
+                                       break;
+                               default:
+                                       lyxerr << "mutation from '" << getType()
+                                               << "' to '" << newtype << "' not implemented\n";
+                                       break;
+                       }
+
                default:
                        lyxerr << "mutation from '" << getType()
                                << "' to '" << newtype << "' not implemented\n";