7 #include "math_matrixinset.h"
9 #include "support/LOstream.h"
11 #include "LaTeXFeatures.h"
16 string getAlign(MathInsetTypes 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"), objtype_(t), nonum_(1), label_(1)
83 halign(getAlign(t, ncols()));
87 MathMatrixInset::MathMatrixInset()
88 : MathGridInset(1, 1, "formula"), objtype_(LM_OT_SIMPLE), nonum_(1), label_(1)
91 MathInset * MathMatrixInset::clone() const
93 return new MathMatrixInset(*this);
97 void MathMatrixInset::metrics(MathStyles /* st */)
99 size_ = (getType() == LM_OT_SIMPLE) ? LM_ST_TEXT : LM_ST_DISPLAY;
101 // let the cells adjust themselves
102 MathGridInset::metrics(size_);
109 if (numberedType()) {
111 for (int row = 0; row < nrows(); ++row)
112 l = std::max(l, mathed_string_width(LM_TC_BF, size(), nicelabel(row)));
120 void MathMatrixInset::draw(Painter & pain, int x, int y)
125 MathGridInset::draw(pain, x, y);
127 if (numberedType()) {
128 int xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
129 for (int row = 0; row < nrows(); ++row) {
130 int yy = y + rowinfo_[row].offset_;
131 drawStr(pain, LM_TC_BF, size(), xx, yy, nicelabel(row));
137 void MathMatrixInset::write(std::ostream & os, bool fragile) const
141 bool n = numberedType();
143 for (int row = 0; row < nrows(); ++row) {
146 for (int col = 0; col < ncols(); ++col) {
149 cell(index(row, col)).write(os, fragile);
152 if (!label_[row].empty())
153 os << "\\label{" << label_[row] << "}";
163 string MathMatrixInset::label(int row) const
168 void MathMatrixInset::label(int row, string const & label)
174 void MathMatrixInset::numbered(int row, bool num)
180 bool MathMatrixInset::numbered(int row) const
186 bool MathMatrixInset::ams() const
192 bool MathMatrixInset::display() const
194 return getType() != LM_OT_SIMPLE;
198 std::vector<string> const MathMatrixInset::getLabelList() const
200 std::vector<string> res;
201 for (int row = 0; row < nrows(); ++row)
202 if (!label_[row].empty() && nonum_[row] != 1)
203 res.push_back(label_[row]);
208 bool MathMatrixInset::numberedType() const
210 if (getType() == LM_OT_SIMPLE)
212 for (int row = 0; row < nrows(); ++row)
219 void MathMatrixInset::validate(LaTeXFeatures & features) const
221 features.amsstyle = ams();
223 // Validation is necessary only if not using AMS math.
224 // To be safe, we will always run mathedvalidate.
225 //if (features.amsstyle)
228 features.boldsymbol = true;
229 //features.binom = true;
231 MathInset::validate(features);
235 void MathMatrixInset::header_write(std::ostream & os) const
237 bool n = numberedType();
248 os << "\\begin{equation" << star(n) << "}\n";
254 os << "\\begin{eqnarray" << star(n) << "}\n";
258 os << "\\begin{align" << star(n) << "}";
262 os << "\\begin{alignat" << star(n) << "}"
263 << "{" << ncols()/2 << "}\n";
266 os << "\\begin{unknown" << star(n) << "}";
271 void MathMatrixInset::footer_write(std::ostream & os) const
273 bool n = numberedType();
282 os << "\\end{equation" << star(n) << "}\n";
288 os << "\\end{eqnarray" << star(n) << "}\n";
292 os << "\\end{align" << star(n) << "}\n";
296 os << "\\end{alignat" << star(n) << "}\n";
300 os << "\\end{unknown" << star(n) << "}";
305 void MathMatrixInset::addRow(int row)
307 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
308 label_.insert(label_.begin() + row + 1, string());
309 MathGridInset::addRow(row);
312 void MathMatrixInset::appendRow()
314 nonum_.push_back(!numberedType());
315 label_.push_back(string());
316 MathGridInset::appendRow();
320 void MathMatrixInset::delRow(int row)
322 MathGridInset::delRow(row);
323 nonum_.erase(nonum_.begin() + row);
324 label_.erase(label_.begin() + row);
327 void MathMatrixInset::addCol(int col)
331 mutate(LM_OT_EQNARRAY);
341 MathGridInset::addCol(col);
343 MathGridInset::addCol(col);
352 void MathMatrixInset::delCol(int col)
356 MathGridInset::delCol(col);
365 string MathMatrixInset::nicelabel(int row) const
369 if (label_[row].empty())
370 return string("(#)");
371 return "(" + label_[row] + ")";
376 short typecode(string const & s)
379 return LM_OT_EQUATION;
381 return LM_OT_EQUATION;
383 return LM_OT_EQNARRAY;
389 return LM_OT_XXALIGN;
391 return LM_OT_MULTLINE;
396 void MathMatrixInset::mutate(string const & newtype)
398 if (newtype == "dump") {
402 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
403 mutate(typecode(newtype));
406 void MathMatrixInset::glueall()
409 for (int i = 0; i < nargs(); ++i)
410 ar.push_back(cell(i));
411 *this = MathMatrixInset(LM_OT_SIMPLE);
416 MathInsetTypes MathMatrixInset::getType() const
422 void MathMatrixInset::setType(MathInsetTypes t)
429 void MathMatrixInset::mutate(short newtype)
431 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
433 if (newtype == getType())
438 setType(LM_OT_EQUATION);
446 setType(LM_OT_SIMPLE);
450 MathGridInset::addCol(1);
453 int pos = firstRelOp(cell(0));
455 cell(0).erase(pos, cell(0).size());
456 cell(1).erase(0, pos);
459 setType(LM_OT_ALIGN);
465 MathGridInset::addCol(1);
466 MathGridInset::addCol(1);
468 // split it "nicely" on the firest relop
469 int pos1 = firstRelOp(cell(0));
471 cell(0).erase(pos1, cell(0).size());
472 cell(1).erase(0, pos1);
476 cell(1).erase(pos2, cell(1).size());
477 cell(2).erase(0, pos2);
480 setType(LM_OT_EQNARRAY);
489 case LM_OT_EQUATION: {
490 // set correct (no)numbering
491 bool allnonum = true;
492 for (int r = 0; r < nrows(); ++r) {
497 // set first non-empty label
499 for (int r = 0; r < nrows(); ++r) {
500 if (!label_[r].empty()) {
508 nonum_[0] = allnonum;
516 for (int row = 0; row < nrows(); ++row) {
518 cell(c).push_back(cell(c + 1));
520 MathGridInset::delCol(2);
521 setType(LM_OT_ALIGN);
534 MathGridInset::addCol(1);
535 setType(LM_OT_EQNARRAY);
541 lyxerr << "mutation from '" << getType()
542 << "' to '" << newtype << "' not implemented\n";
548 lyxerr << "mutation from '" << getType()
549 << "' to '" << newtype << "' not implemented\n";