X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Fmathed%2Fmath_matrixinset.C;h=4fa1ebd1f10c906d02d9809b933d45848cc59df9;hb=dff2911bda426ad439e6475f62183cedd7044801;hp=bb830b8e42e55d911f06aaaa3a915800930de743;hpb=8b7b3a589507e2bed9777f2854ea731c454492e4;p=features.git diff --git a/src/mathed/math_matrixinset.C b/src/mathed/math_matrixinset.C index bb830b8e42..4fa1ebd1f1 100644 --- a/src/mathed/math_matrixinset.C +++ b/src/mathed/math_matrixinset.C @@ -5,6 +5,7 @@ #include #include "math_matrixinset.h" +#include "support.h" #include "debug.h" #include "support/LOstream.h" #include "Painter.h" @@ -13,92 +14,139 @@ namespace { -string getAlign(MathInsetTypes type, int cols) -{ - string align; - switch (type) { - case LM_OT_ALIGN: - for (int i = 0; i < cols; ++i) - align += "Rl"; - break; + 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; + } - case LM_OT_ALIGNAT: - for (int i = 0; i < cols; ++i) - align += "rl"; - break; - case LM_OT_MULTLINE: - align = "C"; - break; + // 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(); + } - default: - align = "rcl"; - break; + + char const * star(bool numbered) + { + return numbered ? "" : "*"; } - return align; -} + 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; + } + + + 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"; + } -string const star(bool n) +} // end anon namespace + + +MathMatrixInset::MathMatrixInset() + : MathGridInset(1, 1), objtype_(LM_OT_SIMPLE), nonum_(1), label_(1) { - return n ? "" : "*"; + setDefaults(); } -int getCols(short int type) +MathMatrixInset::MathMatrixInset(MathInsetTypes t) + : MathGridInset(getCols(t), 1), objtype_(t), nonum_(1), label_(1) { - int col; - switch (type) { - case LM_OT_EQNARRAY: - col = 3; - break; + setDefaults(); +} - case LM_OT_ALIGN: - case LM_OT_ALIGNAT: - col = 2; - break; - default: - col = 1; - } - return col; +MathMatrixInset::MathMatrixInset(MathInsetTypes t, col_type cols) + : MathGridInset(cols, 1), objtype_(t), nonum_(1), label_(1) +{ + setDefaults(); } -// returns position of first relation operator in the array -// used for "intelligent splitting" -int firstRelOp(MathArray const & array) + +MathInset * MathMatrixInset::clone() const { - for (int pos = 0; pos < array.size(); ++pos) - if (MathIsRelOp(array.getChar(pos), array.getCode(pos))) - return pos; - return array.size(); + return new MathMatrixInset(*this); } -} -MathMatrixInset::MathMatrixInset(MathInsetTypes t) - : MathGridInset(getCols(t), 1, "formula"), objtype_(t), nonum_(1), label_(1) +char MathMatrixInset::defaultColAlign(col_type col) { - halign(getAlign(t, ncols())); + 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'; } -MathMatrixInset::MathMatrixInset() - : MathGridInset(1, 1, "formula"), objtype_(LM_OT_SIMPLE), nonum_(1), label_(1) -{} - -MathInset * MathMatrixInset::clone() const +int MathMatrixInset::defaultColSpace(col_type col) { - return new MathMatrixInset(*this); + 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 +void MathMatrixInset::metrics(MathMetricsInfo const & mi) const { - size_ = (getType() == LM_OT_SIMPLE) ? LM_ST_TEXT : LM_ST_DISPLAY; + mi_ = mi; + mi_.style = (getType() == LM_OT_SIMPLE) ? LM_ST_TEXT : LM_ST_DISPLAY; // let the cells adjust themselves - MathGridInset::metrics(size_); + MathGridInset::metrics(mi_); if (display()) { ascent_ += 12; @@ -107,76 +155,86 @@ void MathMatrixInset::metrics(MathStyles) const if (numberedType()) { int l = 0; - for (int row = 0; row < nrows(); ++row) - l = std::max(l, mathed_string_width(LM_TC_BF, size(), nicelabel(row))); + for (row_type row = 0; row < nrows(); ++row) + l = std::max(l, mathed_string_width(LM_TC_BF, mi_, 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, mi_, asc, des); + ascent_ = std::max(ascent_, asc); + descent_ = std::max(descent_, des); } void MathMatrixInset::draw(Painter & pain, int x, int y) const { - xo(x); - yo(y); - MathGridInset::draw(pain, x, y); if (numberedType()) { - int xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20; - for (int row = 0; row < nrows(); ++row) { - int yy = y + rowinfo_[row].offset_; - drawStr(pain, LM_TC_BF, size(), xx, yy, nicelabel(row)); + int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20; + for (row_type row = 0; row < nrows(); ++row) { + int const yy = y + rowinfo_[row].offset_; + drawStr(pain, LM_TC_BF, mi_, xx, yy, nicelabel(row)); } } } -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) { - if (row) - os << " \\\\\n"; - for (int col = 0; col < ncols(); ++col) { - if (col) - os << " & "; - cell(index(row, col)).write(os, fragile); - } + 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] << "}"; if (nonum_[row]) os << "\\nonumber "; } + os << eolString(row); } - footer_write(os); + footer_write(os.os); +} + + +void MathMatrixInset::writeNormal(std::ostream & os) const +{ + os << "[formula " << normalName(getType()) << " "; + MathGridInset::writeNormal(os); + os << "] "; } -string MathMatrixInset::label(int row) const + +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]; } @@ -185,6 +243,14 @@ bool MathMatrixInset::numbered(int row) const bool MathMatrixInset::ams() const { return true; + + return + objtype_ == LM_OT_ALIGN || + objtype_ == LM_OT_MULTLINE || + objtype_ == LM_OT_GATHER || + objtype_ == LM_OT_ALIGNAT || + objtype_ == LM_OT_XALIGNAT || + objtype_ == LM_OT_XXALIGNAT; } @@ -197,7 +263,7 @@ bool MathMatrixInset::display() const std::vector const MathMatrixInset::getLabelList() const { std::vector 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; @@ -206,9 +272,9 @@ std::vector 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 (row_type row = 0; row < nrows(); ++row) if (!nonum_[row]) return true; return false; @@ -254,13 +320,29 @@ 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: - 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) << "}"; } @@ -284,15 +366,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 << "\n\\end{xalignat" << star(n) << "}\n"; + break; + + case LM_OT_XXALIGNAT: + os << "\n\\end{xxalignat}\n"; + break; + + case LM_OT_MULTLINE: + os << "\n\\end{multline}\n"; + break; + + case LM_OT_GATHER: + os << "\n\\end{gather}\n"; break; default: @@ -301,13 +399,14 @@ 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()); MathGridInset::addRow(row); } + void MathMatrixInset::appendRow() { nonum_.push_back(!numberedType()); @@ -316,14 +415,15 @@ void MathMatrixInset::appendRow() } -void MathMatrixInset::delRow(int row) +void MathMatrixInset::delRow(row_type row) { MathGridInset::delRow(row); nonum_.erase(nonum_.begin() + row); label_.erase(label_.begin() + row); } -void MathMatrixInset::addCol(int col) + +void MathMatrixInset::addCol(col_type col) { switch (getType()) { case LM_OT_EQUATION: @@ -336,11 +436,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: @@ -348,20 +452,23 @@ void MathMatrixInset::addCol(int col) } } -void MathMatrixInset::delCol(int col) + +void MathMatrixInset::delCol(col_type 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(row_type row) const { if (nonum_[row]) return string(); @@ -371,27 +478,6 @@ string MathMatrixInset::nicelabel(int row) const } -namespace { - short 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 == "xalign") - return LM_OT_XALIGN; - if (s == "xxalign") - return LM_OT_XXALIGN; - if (s == "multline") - return LM_OT_MULTLINE; - return LM_OT_SIMPLE; - } -} - void MathMatrixInset::mutate(string const & newtype) { if (newtype == "dump") { @@ -402,10 +488,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; @@ -421,11 +508,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"; @@ -445,17 +533,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)); + pos_type 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; } @@ -465,17 +556,17 @@ void MathMatrixInset::mutate(short newtype) MathGridInset::addCol(1); // split it "nicely" on the firest relop - int pos1 = firstRelOp(cell(0)); + pos_type 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; @@ -488,16 +579,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 (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; } } @@ -511,14 +602,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 (row_type row = 0; row < nrows(); ++row) { + idx_type c = 3 * row + 1; cell(c).push_back(cell(c + 1)); } MathGridInset::delCol(2); setType(LM_OT_ALIGN); - halign("rl"); mutate(newtype); break; } @@ -532,10 +625,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"; @@ -543,6 +641,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";