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"), objtype_(t), nonum_(1), label_(1)
85 MathMatrixInset::MathMatrixInset()
86 : MathGridInset(1, 1, "formula"), objtype_(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);
414 MathInsetTypes MathMatrixInset::getType() const
420 void MathMatrixInset::setType(MathInsetTypes t)
427 void MathMatrixInset::mutate(short newtype)
429 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
431 if (newtype == getType())
436 setType(LM_OT_EQUATION);
444 setType(LM_OT_SIMPLE);
448 MathGridInset::addCol(1);
451 int pos = firstRelOp(cell(0));
453 cell(0).erase(pos, cell(0).size());
454 cell(1).erase(0, pos);
457 setType(LM_OT_ALIGN);
463 MathGridInset::addCol(1);
464 MathGridInset::addCol(1);
466 // split it "nicely" on the firest relop
467 int pos1 = firstRelOp(cell(0));
469 cell(0).erase(pos1, cell(0).size());
470 cell(1).erase(0, pos1);
474 cell(1).erase(pos2, cell(1).size());
475 cell(2).erase(0, pos2);
478 setType(LM_OT_EQNARRAY);
487 case LM_OT_EQUATION: {
488 // set correct (no)numbering
489 bool allnonum = true;
490 for (int r = 0; r < nrows(); ++r) {
495 // set first non-empty label
497 for (int r = 0; r < nrows(); ++r) {
498 if (!label_[r].empty()) {
506 nonum_[0] = allnonum;
514 for (int row = 0; row < nrows(); ++row) {
516 cell(c).push_back(cell(c + 1));
518 MathGridInset::delCol(2);
519 setType(LM_OT_ALIGN);
532 MathGridInset::addCol(1);
533 setType(LM_OT_EQNARRAY);
539 lyxerr << "mutation from '" << getType()
540 << "' to '" << newtype << "' not implemented\n";
546 lyxerr << "mutation from '" << getType()
547 << "' to '" << newtype << "' not implemented\n";