7 #include "math_matrixinset.h"
9 #include "support/LOstream.h"
11 #include "LaTeXFeatures.h"
16 string const getAlign(short int type, int cols)
21 for (int i = 0; i < cols; ++i)
26 for (int i = 0; i < cols; ++i)
42 string const star(bool n)
48 int getCols(short int type)
67 // returns position of first relation operator in the array
68 // used for "intelligent splitting"
69 int firstRelOp(MathArray const & array)
71 for (int pos = 0; pos < array.size(); array.next(pos))
72 if (!array.isInset(pos) &&
73 MathIsRelOp(array.getChar(pos), array.getCode(pos)))
80 MathMatrixInset::MathMatrixInset(MathInsetTypes t)
81 : MathGridInset(getCols(t), 1, "formula", t), nonum_(1), label_(1)
85 MathMatrixInset::MathMatrixInset()
86 : MathGridInset(1, 1, "formula", LM_OT_SIMPLE), nonum_(1), label_(1)
89 MathInset * MathMatrixInset::clone() const
91 return new MathMatrixInset(*this);
95 void MathMatrixInset::metrics(MathStyles /* st */)
97 size_ = (getType() == LM_OT_SIMPLE) ? LM_ST_TEXT : LM_ST_DISPLAY;
99 // let the cells adjust themselves
100 MathGridInset::metrics(size_);
107 if (numberedType()) {
109 for (int row = 0; row < nrows(); ++row)
110 l = std::max(l, mathed_string_width(LM_TC_BF, size(), nicelabel(row)));
118 void MathMatrixInset::draw(Painter & pain, int x, int y)
123 MathGridInset::draw(pain, x, y);
125 if (numberedType()) {
126 int xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
127 for (int row = 0; row < nrows(); ++row) {
128 int yy = y + rowinfo_[row].offset_;
129 drawStr(pain, LM_TC_BF, size(), xx, yy, nicelabel(row));
135 void MathMatrixInset::write(std::ostream & os, bool fragile) const
139 bool n = numberedType();
141 for (int row = 0; row < nrows(); ++row) {
144 for (int col = 0; col < ncols(); ++col) {
147 cell(index(row, col)).write(os, fragile);
150 if (!label_[row].empty())
151 os << "\\label{" << label_[row] << "}";
161 string MathMatrixInset::label(int row) const
166 void MathMatrixInset::label(int row, string const & label)
172 void MathMatrixInset::numbered(int row, bool num)
178 bool MathMatrixInset::numbered(int row) const
184 bool MathMatrixInset::ams() const
190 bool MathMatrixInset::display() const
192 return getType() != LM_OT_SIMPLE;
196 std::vector<string> const MathMatrixInset::getLabelList() const
198 std::vector<string> res;
199 for (int row = 0; row < nrows(); ++row)
200 if (!label_[row].empty() && nonum_[row] != 1)
201 res.push_back(label_[row]);
206 bool MathMatrixInset::numberedType() const
208 if (getType() == LM_OT_SIMPLE)
210 for (int row = 0; row < nrows(); ++row)
217 void MathMatrixInset::validate(LaTeXFeatures & features) const
219 features.amsstyle = ams();
221 // Validation is necessary only if not using AMS math.
222 // To be safe, we will always run mathedvalidate.
223 //if (features.amsstyle)
226 features.boldsymbol = true;
227 //features.binom = true;
229 MathInset::validate(features);
233 void MathMatrixInset::header_write(std::ostream & os) const
235 bool n = numberedType();
246 os << "\\begin{equation" << star(n) << "}\n";
252 os << "\\begin{eqnarray" << star(n) << "}\n";
256 os << "\\begin{align" << star(n) << "}";
260 os << "\\begin{alignat" << star(n) << "}"
261 << "{" << ncols()/2 << "}\n";
264 os << "\\begin{unknown" << star(n) << "}";
269 void MathMatrixInset::footer_write(std::ostream & os) const
271 bool n = numberedType();
280 os << "\\end{equation" << star(n) << "}\n";
286 os << "\\end{eqnarray" << star(n) << "}\n";
290 os << "\\end{align" << star(n) << "}\n";
294 os << "\\end{alignat" << star(n) << "}\n";
298 os << "\\end{unknown" << star(n) << "}";
303 void MathMatrixInset::addRow(int row)
305 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
306 label_.insert(label_.begin() + row + 1, string());
307 MathGridInset::addRow(row);
310 void MathMatrixInset::appendRow()
312 nonum_.push_back(!numberedType());
313 label_.push_back(string());
314 MathGridInset::appendRow();
318 void MathMatrixInset::delRow(int row)
320 MathGridInset::delRow(row);
321 nonum_.erase(nonum_.begin() + row);
322 label_.erase(label_.begin() + row);
325 void MathMatrixInset::addCol(int col)
329 mutate(LM_OT_EQNARRAY);
339 MathGridInset::addCol(col);
341 MathGridInset::addCol(col);
350 void MathMatrixInset::delCol(int col)
354 MathGridInset::delCol(col);
363 string MathMatrixInset::nicelabel(int row) const
367 if (label_[row].empty())
368 return string("(#)");
369 return "(" + label_[row] + ")";
374 short typecode(string const & s)
377 return LM_OT_EQUATION;
379 return LM_OT_EQUATION;
381 return LM_OT_EQNARRAY;
387 return LM_OT_XXALIGN;
389 return LM_OT_MULTLINE;
394 void MathMatrixInset::mutate(string const & newtype)
396 if (newtype == "dump") {
400 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
401 mutate(typecode(newtype));
404 void MathMatrixInset::glueall()
407 for (int i = 0; i < nargs(); ++i)
408 ar.push_back(cell(i));
409 *this = MathMatrixInset(LM_OT_SIMPLE);
413 void MathMatrixInset::mutate(short newtype)
415 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
417 if (newtype == getType())
422 setType(LM_OT_EQUATION);
430 setType(LM_OT_SIMPLE);
434 MathGridInset::addCol(1);
437 int pos = firstRelOp(cell(0));
439 cell(0).erase(pos, cell(0).size());
440 cell(1).erase(0, pos);
443 setType(LM_OT_ALIGN);
449 MathGridInset::addCol(1);
450 MathGridInset::addCol(1);
452 // split it "nicely" on the firest relop
453 int pos1 = firstRelOp(cell(0));
455 cell(0).erase(pos1, cell(0).size());
456 cell(1).erase(0, pos1);
460 cell(1).erase(pos2, cell(1).size());
461 cell(2).erase(0, pos2);
464 setType(LM_OT_EQNARRAY);
473 case LM_OT_EQUATION: {
474 // set correct (no)numbering
475 bool allnonum = true;
476 for (int r = 0; r < nrows(); ++r) {
481 // set first non-empty label
483 for (int r = 0; r < nrows(); ++r) {
484 if (!label_[r].empty()) {
492 nonum_[0] = allnonum;
500 for (int row = 0; row < nrows(); ++row) {
502 cell(c).push_back(cell(c + 1));
504 MathGridInset::delCol(2);
505 setType(LM_OT_ALIGN);
518 MathGridInset::addCol(1);
519 setType(LM_OT_EQNARRAY);
525 lyxerr << "mutation from '" << getType()
526 << "' to '" << newtype << "' not implemented\n";
532 lyxerr << "mutation from '" << getType()
533 << "' to '" << newtype << "' not implemented\n";