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();
244 os << "\\begin{equation" << star(n) << "}\n";
250 os << "\\begin{eqnarray" << star(n) << "}\n";
254 os << "\\begin{align" << star(n) << "}";
258 os << "\\begin{alignat" << star(n) << "}"
259 << "{" << ncols()/2 << "}\n";
262 os << "\\begin{unknown" << star(n) << "}";
267 void MathMatrixInset::footer_write(std::ostream & os) const
269 bool n = numberedType();
278 os << "\\end{equation" << star(n) << "}\n";
284 os << "\\end{eqnarray" << star(n) << "}\n";
288 os << "\\end{align" << star(n) << "}\n";
292 os << "\\end{alignat" << star(n) << "}\n";
296 os << "\\end{unknown" << star(n) << "}";
301 void MathMatrixInset::addRow(int row)
303 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
304 label_.insert(label_.begin() + row + 1, string());
305 MathGridInset::addRow(row);
308 void MathMatrixInset::appendRow()
310 nonum_.push_back(!numberedType());
311 label_.push_back(string());
312 MathGridInset::appendRow();
316 void MathMatrixInset::delRow(int row)
318 MathGridInset::delRow(row);
319 nonum_.erase(nonum_.begin() + row);
320 label_.erase(label_.begin() + row);
323 void MathMatrixInset::addCol(int col)
327 mutate(LM_OT_EQNARRAY);
337 MathGridInset::addCol(col);
339 MathGridInset::addCol(col);
348 void MathMatrixInset::delCol(int col)
352 MathGridInset::delCol(col);
361 string MathMatrixInset::nicelabel(int row) const
365 if (label_[row].empty())
366 return string("(#)");
367 return "(" + label_[row] + ")";
372 short typecode(string const & s)
375 return LM_OT_EQUATION;
377 return LM_OT_EQUATION;
379 return LM_OT_EQNARRAY;
385 return LM_OT_XXALIGN;
387 return LM_OT_MULTLINE;
392 void MathMatrixInset::mutate(string const & newtype)
394 if (newtype == "dump") {
398 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
399 mutate(typecode(newtype));
402 void MathMatrixInset::glueall()
405 for (int i = 0; i < nargs(); ++i)
406 ar.push_back(cell(i));
407 *this = MathMatrixInset(LM_OT_SIMPLE);
411 void MathMatrixInset::mutate(short newtype)
413 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
415 if (newtype == getType())
420 setType(LM_OT_EQUATION);
428 setType(LM_OT_SIMPLE);
432 MathGridInset::addCol(1);
435 int pos = firstRelOp(cell(0));
437 cell(0).erase(pos, cell(0).size());
438 cell(1).erase(0, pos);
441 setType(LM_OT_ALIGN);
447 MathGridInset::addCol(1);
448 MathGridInset::addCol(1);
450 // split it "nicely" on the firest relop
451 int pos1 = firstRelOp(cell(0));
453 cell(0).erase(pos1, cell(0).size());
454 cell(1).erase(0, pos1);
458 cell(1).erase(pos2, cell(1).size());
459 cell(2).erase(0, pos2);
462 setType(LM_OT_EQNARRAY);
471 case LM_OT_EQUATION: {
472 // set correct (no)numbering
473 bool allnonum = true;
474 for (int r = 0; r < nrows(); ++r) {
479 // set first non-empty label
481 for (int r = 0; r < nrows(); ++r) {
482 if (!label_[r].empty()) {
490 nonum_[0] = allnonum;
498 for (int row = 0; row < nrows(); ++row) {
500 cell(c).push_back(cell(c + 1));
502 MathGridInset::delCol(2);
503 setType(LM_OT_ALIGN);
516 MathGridInset::addCol(1);
517 setType(LM_OT_EQNARRAY);
523 lyxerr << "mutation from '" << getType()
524 << "' to '" << newtype << "' not implemented\n";
530 lyxerr << "mutation from '" << getType()
531 << "' to '" << newtype << "' not implemented\n";