X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Fmathed%2Fmath_matrixinset.C;h=f1343b47362f52326f42b1c13c21dc6d57cbf230;hb=57501b93064a6deed43e415beed45606054d86ad;hp=0ec2f7faa31eff1e400d78d2b3f67b5b7c7273bd;hpb=d1182f17daa1a164d9527ccbe6500840d7ac6bc8;p=lyx.git diff --git a/src/mathed/math_matrixinset.C b/src/mathed/math_matrixinset.C index 0ec2f7faa3..f1343b4736 100644 --- a/src/mathed/math_matrixinset.C +++ b/src/mathed/math_matrixinset.C @@ -1,571 +1,91 @@ -#include +/** + * \file math_matrixinset.C + * This file is part of LyX, the document processor. + * Licence details can be found in the file COPYING. + * + * \author André Pönitz + * + * Full author contact details are available in file CREDITS. + */ -#ifdef __GNUG__ -#pragma implementation -#endif +#include #include "math_matrixinset.h" -#include "debug.h" -#include "support/LOstream.h" -#include "Painter.h" -#include "LaTeXFeatures.h" - - -LyXFont WhichFont(short type, int size); - -namespace { +#include "math_data.h" +#include "math_mathmlstream.h" -string getAlign(short int type, int cols) -{ - string align; - switch (type) { - 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; +using std::auto_ptr; - default: - align = "rcl"; - break; - } - return align; -} +MathMatrixInset::MathMatrixInset(MathGridInset const & p) + : MathGridInset(p) +{} -string star(bool n) -{ - return n ? "" : "*"; -} -int getCols(short int type) +auto_ptr MathMatrixInset::clone() const { - int col; - switch (type) { - case LM_OT_EQNARRAY: - col = 3; - break; - - case LM_OT_ALIGN: - case LM_OT_ALIGNAT: - col = 2; - break; - - default: - col = 1; - } - return col; -} - + return auto_ptr(new MathMatrixInset(*this)); } -MathMatrixInset::MathMatrixInset(MathInsetTypes t) - : MathGridInset(getCols(t), 1, "formula", t), nonum_(1), label_(1) -{} - -MathMatrixInset::MathMatrixInset() - : MathGridInset(1, 1, "formula", LM_OT_SIMPLE), nonum_(1), label_(1) -{} - -MathInset * MathMatrixInset::Clone() const +void MathMatrixInset::write(WriteStream & os) const { - return new MathMatrixInset(*this); + MathGridInset::write(os); } -void MathMatrixInset::Metrics(MathStyles st) +void MathMatrixInset::normalize(NormalStream & os) const { - size_ = st; - //LyXFont wfont = WhichFont(LM_TC_BF, size()); - //wfont.setLatex(LyXFont::OFF); - - // let the cells adjust themselves - MathGridInset::Metrics(st); - - if (display()) { - ascent_ += 12; - descent_ += 12; - } - - if (numberedType()) { - int l = 0; - for (int row = 0; row < nrows(); ++row) - l = max(l, mathed_string_width(LM_TC_TEXTRM, size(), nicelabel(row))); - - if (l) - width_ += 30 + l; - } - -/* - { - cxrow->setBaseline((cxrow == row_.begin()) ? - cxrow->ascent() : - cxrow->ascent() + cprow->descent() - + MATH_ROWSEP + cprow->getBaseline()); - h += cxrow->ascent() + cxrow->descent() + MATH_ROWSEP; - cprow = cxrow; - } - - int const hl = descent(); - h -= MATH_ROWSEP; - - // Compute vertical align - switch (v_align_) { - case 't': - ascent = row_.begin()->getBaseline(); - break; - case 'b': - ascent = h - hl; - break; - default: - ascent = row_.begin().is_last() ? h / 2 : h - hl; - break; - } - descent = h - ascent + 2; - - // Increase ws_[i] for 'R' columns (except the first one) - for (int i = 1; i < nc_; ++i) - if (h_align_[i] == 'R') - ws_[i] += 10 * df_width; - // Increase ws_[i] for 'C' column - if (h_align_[0] == 'C') - if (ws_[0] < 7 * workwidth / 8) - ws_[0] = 7 * workwidth / 8; - -*/ - + MathGridInset::normalize(os); } -void MathMatrixInset::draw(Painter & pain, int x, int y) -{ - xo(x); - yo(y); - - MathGridInset::draw(pain, x, y); - if (numberedType()) { - LyXFont wfont = WhichFont(LM_TC_BF, size()); - wfont.setLatex(LyXFont::OFF); - 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); +void MathMatrixInset::maple(MapleStream & os) const +{ + os << "matrix(" << int(nrows()) << ',' << int(ncols()) << ",["; + for (idx_type idx = 0; idx < nargs(); ++idx) { + if (idx) + os << ','; + os << cell(idx); } + os << "])"; } -void MathMatrixInset::Write(std::ostream & os, bool fragile) const +void MathMatrixInset::maxima(MaximaStream & os) const { - header_write(os); - - bool n = numberedType(); - - for (int row = 0; row < nrows(); ++row) { + os << "matrix("; + for (row_type row = 0; row < nrows(); ++row) { if (row) - os << " \\\\\n"; - for (int col = 0; col < ncols(); ++col) { + os << ','; + os << '['; + for (col_type col = 0; col < ncols(); ++col) { if (col) - os << " & "; - cell(index(row, col)).Write(os, fragile); - } - if (n) { - if (!label_[row].empty()) - os << "\\label{" << label_[row] << "}"; - if (nonum_[row]) - os << "\\nonumber "; + os << ','; + os << cell(index(row, col)); } + os << ']'; } - - footer_write(os); + os << ')'; } -string MathMatrixInset::label(int row) const +void MathMatrixInset::mathmlize(MathMLStream & os) const { - return label_[row]; -} - -void MathMatrixInset::label(int row, string const & label) -{ - label_[row] = label; -} - - -void MathMatrixInset::numbered(int row, bool num) -{ - nonum_[row] = !num; -} - - -bool MathMatrixInset::numbered(int row) const -{ - return !nonum_[row]; -} - - -bool MathMatrixInset::ams() const -{ - return true; -} - - -bool MathMatrixInset::display() const -{ - return GetType() != LM_OT_SIMPLE; -} - - -vector const MathMatrixInset::getLabelList() const -{ - std::vector res; - for (int row = 0; row < nrows(); ++row) - if (!label_[row].empty() && nonum_[row] != 1) - res.push_back(label_[row]); - return res; -} - - -bool MathMatrixInset::numberedType() const -{ - if (GetType() == LM_OT_SIMPLE) - return false; - for (int row = 0; row < nrows(); ++row) - if (!nonum_[row]) - return true; - return false; -} - - -void MathMatrixInset::Validate(LaTeXFeatures & features) -{ - features.amsstyle = ams(); - - // Validation is necessary only if not using AMS math. - // To be safe, we will always run mathedValidate. - //if (features.amsstyle) - // return; - - //Validate1(features); - - features.boldsymbol = 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(); - } + MathGridInset::mathmlize(os); } -*/ -void MathMatrixInset::header_write(ostream & os) const +void MathMatrixInset::octave(OctaveStream & os) const { - bool n = numberedType(); - - switch (GetType()) { - case LM_OT_SIMPLE: - os << "\\("; - break; - - case LM_OT_EQUATION: - if (n) - os << "\\begin{equation" << star(n) << "}\n"; - else - os << "\\[\n"; - break; - - case LM_OT_EQNARRAY: - os << "\\begin{eqnarray" << star(n) << "}\n"; - break; - - case LM_OT_ALIGN: - os << "\\begin{align" << star(n) << "}"; - break; - - case LM_OT_ALIGNAT: - os << "\\begin{alignat" << star(n) << "}" - << "{" << ncols()/2 << "}\n"; - break; - } -} - - -void MathMatrixInset::footer_write(ostream & os) const -{ - bool n = numberedType(); - - switch (GetType()) { - case LM_OT_SIMPLE: - os << "\\)"; - break; - - case LM_OT_EQUATION: - if (n) - os << "\\end{equation" << star(n) << "}\n"; - else - os << "\\]\n"; - break; - - case LM_OT_EQNARRAY: - os << "\\end{eqnarray" << star(n) << "}\n"; - break; - - case LM_OT_ALIGN: - os << "\\end{align" << star(n) << "}\n"; - break; - - case LM_OT_ALIGNAT: - os << "\\end{alignat" << star(n) << "}\n"; - break; - } -} - - -void MathMatrixInset::addRow(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()); - label_.push_back(string()); - MathGridInset::appendRow(); -} - - -void MathMatrixInset::delRow(int row) -{ - MathGridInset::delRow(row); - nonum_.erase(nonum_.begin() + row); - label_.erase(label_.begin() + row); -} - -void MathMatrixInset::addCol(int col) -{ - switch (GetType()) { - case LM_OT_EQUATION: - mutate(LM_OT_EQNARRAY); - break; - - case LM_OT_EQNARRAY: - mutate(LM_OT_ALIGN); - addCol(col); - break; - - case LM_OT_ALIGN: - case LM_OT_ALIGNAT: - MathGridInset::addCol(col); - halign(col, 'l'); - MathGridInset::addCol(col); - halign(col, 'r'); - break; - - default: - break; - } -} - -void MathMatrixInset::delCol(int col) -{ - switch (GetType()) { - case LM_OT_ALIGN: - MathGridInset::delCol(col); - break; - - default: - break; - } -} - -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 -{ - if (nonum_[row]) - return string(); - if (label_[row].empty()) - return string("(#)"); - return "(" + label_[row] + ")"; -} - - -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") { - dump(); - return; - } - //lyxerr << "mutating from '" << GetType() << "' to '" << newtype << "'\n"; - mutate(typecode(newtype)); -} - -void MathMatrixInset::glueall() -{ - MathArray ar; - for (int i = 0; i < nargs(); ++i) - ar.push_back(cell(i)); - *this = MathMatrixInset(LM_OT_SIMPLE); - cell(0) = ar; -} - -void MathMatrixInset::mutate(short newtype) -{ - //lyxerr << "mutating from '" << GetType() << "' to '" << newtype << "'\n"; - - if (newtype == GetType()) - return; - - switch (GetType()) { - case LM_OT_SIMPLE: - SetType(LM_OT_EQUATION); - numbered(false); - mutate(newtype); - break; - - case LM_OT_EQUATION: - switch (newtype) { - case LM_OT_SIMPLE: - SetType(LM_OT_SIMPLE); - break; - - case LM_OT_ALIGN: - MathGridInset::addCol(1); - halign("rl"); - SetType(LM_OT_ALIGN); - break; - - default: - MathGridInset::addCol(1); - MathGridInset::addCol(1); - halign("rcl"); - SetType(LM_OT_EQNARRAY); - mutate(newtype); - break; - } - break; - - case LM_OT_EQNARRAY: - switch (newtype) { - case LM_OT_SIMPLE: - 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"; - break; - - case LM_OT_ALIGN: - default: - for (int row = 0; row < nrows(); ++row) { - int c = 3 * row + 1; - cell(c).push_back(cell(c + 1)); - } - MathGridInset::delCol(2); - SetType(LM_OT_ALIGN); - halign("rl"); - mutate(newtype); - break; - } - break; - - case LM_OT_ALIGN: - switch (newtype) { - case LM_OT_SIMPLE: - case LM_OT_EQUATION: - case LM_OT_EQNARRAY: - MathGridInset::addCol(1); - SetType(LM_OT_EQNARRAY); - halign("lrl"); - mutate(newtype); - break; - - default: - lyxerr << "mutation from '" << GetType() - << "' to '" << newtype << "' not implemented\n"; - break; - } - break; - - default: - lyxerr << "mutation from '" << GetType() - << "' to '" << newtype << "' not implemented\n"; + os << '['; + for (row_type row = 0; row < nrows(); ++row) { + if (row) + os << ';'; + os << '['; + for (col_type col = 0; col < ncols(); ++col) + os << cell(index(row, col)) << ' '; + os << ']'; } + os << ']'; }