]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_matrixinset.C
fix pullArg when pressing <Delete> at the end of an cell
[lyx.git] / src / mathed / math_matrixinset.C
index 96a4c0db74aa544adfedd9bac80419ca9ad47325..d552e20242c35a77f4e4c3fce6a0f920abb728e7 100644 (file)
@@ -1,9 +1,9 @@
-#include <config.h>
-
 #ifdef __GNUG__
 #pragma implementation
 #endif
 
+#include <vector>
+
 #include "math_matrixinset.h"
 #include "debug.h"
 #include "support/LOstream.h"
 #include "LaTeXFeatures.h"
 
 
-LyXFont WhichFont(short type, int size);
-
 namespace {
 
-string getAlign(short int type, int cols)
+string const getAlign(short int type, int cols)
 {
        string align;
        switch (type) {
@@ -41,11 +39,12 @@ string getAlign(short int type, int cols)
 }
 
 
-string star(bool n)
+string const star(bool n)
 {
        return n ? "" : "*";
 }
 
+
 int getCols(short int type)
 {
        int col;
@@ -65,6 +64,17 @@ int getCols(short int type)
        return col;
 }
 
+// returns position of first relation operator in the array
+// used for "intelligent splitting"
+int firstRelOp(MathArray const & array)
+{
+       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();
+}
+
 }
 
 MathMatrixInset::MathMatrixInset(MathInsetTypes t)
@@ -76,20 +86,18 @@ MathMatrixInset::MathMatrixInset()
        : MathGridInset(1, 1, "formula", LM_OT_SIMPLE), nonum_(1), label_(1)
 {}
 
-MathInset * MathMatrixInset::Clone() const
+MathInset * MathMatrixInset::clone() const
 {
        return new MathMatrixInset(*this);
 }
 
 
-void MathMatrixInset::Metrics(MathStyles st)
+void MathMatrixInset::Metrics(MathStyles /* st */, int, int)
 {
-       size_ = st;
-       //LyXFont wfont = WhichFont(LM_TC_BF, size());
-       //wfont.setLatex(LyXFont::OFF);
+       size_ = (GetType() == LM_OT_SIMPLE) ? LM_ST_TEXT : LM_ST_DISPLAY;
 
        // let the cells adjust themselves
-       MathGridInset::Metrics(st);
+       MathGridInset::Metrics(size_);
 
        if (display()) {
                ascent_  += 12;
@@ -99,7 +107,7 @@ void MathMatrixInset::Metrics(MathStyles st)
        if (numberedType()) {
                int l = 0;
                for (int row = 0; row < nrows(); ++row)
-                       l = max(l, mathed_string_width(LM_TC_TEXTRM, size(), nicelabel(row)));
+                       l = std::max(l, mathed_string_width(LM_TC_BF, size(), nicelabel(row)));
 
                if (l)
                        width_ += 30 + l;
@@ -115,13 +123,11 @@ void MathMatrixInset::draw(Painter & pain, int x, int y)
        MathGridInset::draw(pain, x, y);
 
        if (numberedType()) {
-               LyXFont wfont = WhichFont(LM_TC_BF, size());
-#ifndef NO_LATEX
-               wfont.setLatex(LyXFont::OFF);
-#endif
                int xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
-               for (int row = 0; row < nrows(); ++row) 
-                       pain.text(xx, y + rowinfo_[row].offset_, nicelabel(row), wfont);
+               for (int row = 0; row < nrows(); ++row) {
+                       int yy = y + rowinfo_[row].offset_;
+                       drawStr(pain, LM_TC_BF, size(), xx, yy, nicelabel(row));
+               }
        }
 }
 
@@ -187,7 +193,7 @@ bool MathMatrixInset::display() const
 }
 
 
-vector<string> const MathMatrixInset::getLabelList() const
+std::vector<string> const MathMatrixInset::getLabelList() const
 {
        std::vector<string> res;
        for (int row = 0; row < nrows(); ++row)
@@ -208,7 +214,7 @@ bool MathMatrixInset::numberedType() const
 }
 
 
-void MathMatrixInset::Validate(LaTeXFeatures & features)
+void MathMatrixInset::Validate(LaTeXFeatures & features) const
 {
        features.amsstyle = ams();
 
@@ -217,43 +223,14 @@ void MathMatrixInset::Validate(LaTeXFeatures & features)
        //if (features.amsstyle)
        //  return;
 
-       //Validate1(features);
-
        features.boldsymbol = true;
-       features.binom      = true;
-}
+       //features.binom      = true;
 
-/*
-void MathMatrixInset::Validate1(LaTeXFeatures & features)
-{
-       MathIter it(cell());
-
-       while (it.OK() && !(features.binom && features.boldsymbol)) {
-               MathInset * p = it.GetInset();
-               if (p) {
-                       p = it.GetActiveInset();
-                       if (p) {
-                               if (!features.binom && p->GetType() == LM_OT_MACRO &&
-                                   p->name() == "binom") {
-                                       features.binom = true;
-                               } else {
-                                       for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
-                                               p->setArgumentIdx(i);
-                                               Validate1(features, p);
-                                       }
-                               }
-                       } else {
-                               if (!features.boldsymbol && p->name() == "boldsymbol") 
-                                       features.boldsymbol = true;
-                       }
-               }
-               it.Next();
-       }
+       MathInset::Validate(features);
 }
-*/
 
 
-void MathMatrixInset::header_write(ostream & os) const
+void MathMatrixInset::header_write(std::ostream & os) const
 {
        bool n = numberedType();
 
@@ -281,11 +258,13 @@ void MathMatrixInset::header_write(ostream & os) const
                        os << "\\begin{alignat" << star(n) << "}"
                           << "{" << ncols()/2 << "}\n";
                        break;
+               default:
+                       os << "\\begin{unknown" << star(n) << "}";
        }
 }
 
 
-void MathMatrixInset::footer_write(ostream & os) const
+void MathMatrixInset::footer_write(std::ostream & os) const
 {
        bool n = numberedType();
 
@@ -312,6 +291,9 @@ void MathMatrixInset::footer_write(ostream & os) const
                case LM_OT_ALIGNAT:
                        os << "\\end{alignat" << star(n) << "}\n";
                        break;
+
+               default:
+                       os << "\\end{unknown" << star(n) << "}";
        }
 }
 
@@ -375,24 +357,6 @@ void MathMatrixInset::delCol(int col)
        }
 }
 
-void MathMatrixInset::breakLine() 
-{
-       if (GetType() == LM_OT_SIMPLE || GetType() == LM_OT_EQUATION) 
-               mutate(LM_OT_EQNARRAY);
-       addRow(nrows() - 1);
-}
-
-
-void MathMatrixInset::splitCell(int idx)
-{
-       if (idx == nargs() - 1) {
-               lyxerr << "can't split last cell\n";
-               return;
-       }
-
-       lyxerr << "unimplemented\n";
-}
-
 
 string MathMatrixInset::nicelabel(int row) const
 {
@@ -454,7 +418,7 @@ void MathMatrixInset::mutate(short newtype)
        switch (GetType()) {
                case LM_OT_SIMPLE:
                        SetType(LM_OT_EQUATION);
-                       numbered(false);
+                       numbered(0, false);
                        mutate(newtype);
                        break;
 
@@ -464,15 +428,36 @@ void MathMatrixInset::mutate(short newtype)
                                        SetType(LM_OT_SIMPLE);
                                        break;
 
-                               case LM_OT_ALIGN:
+                               case LM_OT_ALIGN: {
                                        MathGridInset::addCol(1);
+
+                                       // split it "nicely"
+                                       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);
                                        break;
+                               }
 
+                               case LM_OT_EQNARRAY:
                                default:
                                        MathGridInset::addCol(1);
                                        MathGridInset::addCol(1);
+
+                                       // split it "nicely" on the firest relop
+                                       int pos1 = 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");
                                        SetType(LM_OT_EQNARRAY);
                                        mutate(newtype);
@@ -483,21 +468,33 @@ void MathMatrixInset::mutate(short newtype)
                case LM_OT_EQNARRAY:
                        switch (newtype) {
                                case LM_OT_SIMPLE:
+                               case LM_OT_EQUATION: {
+                                       // set correct (no)numbering
+                                       bool allnonum = true;
+                                       for (int r = 0; r < nrows(); ++r) {
+                                               if (!nonum_[r])
+                                                       allnonum = false;
+                                       }
+
+                                       // set first non-empty label
+                                       string label;
+                                       for (int r = 0; r < nrows(); ++r) {
+                                               if (!label_[r].empty()) {
+                                                       label = label_[r];
+                                                       break;
+                                               }
+                                       }
+
                                        glueall();
-                                       break;
 
-                               case LM_OT_EQUATION:
-                                       if (nrows() == 1) {
-                                               MathGridInset::delCol(2);
-                                               MathGridInset::delCol(1);
-                                               SetType(LM_OT_EQUATION);
-                                               mutate(newtype);
-                                       } else 
-                                               lyxerr << "need to delete rows first\n";
+                                       nonum_[0] = allnonum;
+                                       label_[0] = label;
+                                       mutate(newtype);
                                        break;
+                               }
 
                                case LM_OT_ALIGN:
-                               default:
+                               default: {
                                        for (int row = 0; row < nrows(); ++row) {
                                                int c = 3 * row + 1;
                                                cell(c).push_back(cell(c + 1));
@@ -507,6 +504,7 @@ void MathMatrixInset::mutate(short newtype)
                                        halign("rl");
                                        mutate(newtype);
                                        break;
+                               }
                        }
                        break;
 
@@ -517,7 +515,7 @@ void MathMatrixInset::mutate(short newtype)
                                case LM_OT_EQNARRAY:
                                        MathGridInset::addCol(1);
                                        SetType(LM_OT_EQNARRAY);
-                                       halign("lrl");
+                                       halign("rcl");
                                        mutate(newtype);
                                        break;