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(); ++pos)
72 if (MathIsRelOp(array.getChar(pos), array.getCode(pos)))
79 MathMatrixInset::MathMatrixInset(MathInsetTypes t)
80 : MathGridInset(getCols(t), 1), objtype_(t), nonum_(1), label_(1)
82 halign(getAlign(t, ncols()));
86 MathMatrixInset::MathMatrixInset()
87 : MathGridInset(1, 1), objtype_(LM_OT_SIMPLE), nonum_(1), label_(1)
90 MathInset * MathMatrixInset::clone() const
92 return new MathMatrixInset(*this);
96 void MathMatrixInset::metrics(MathStyles) const
98 size_ = (getType() == LM_OT_SIMPLE) ? LM_ST_TEXT : LM_ST_DISPLAY;
100 // let the cells adjust themselves
101 MathGridInset::metrics(size_);
108 if (numberedType()) {
110 for (int row = 0; row < nrows(); ++row)
111 l = std::max(l, mathed_string_width(LM_TC_BF, size(), nicelabel(row)));
119 void MathMatrixInset::draw(Painter & pain, int x, int y) const
124 MathGridInset::draw(pain, x, y);
126 if (numberedType()) {
127 int xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
128 for (int row = 0; row < nrows(); ++row) {
129 int yy = y + rowinfo_[row].offset_;
130 drawStr(pain, LM_TC_BF, size(), xx, yy, nicelabel(row));
136 void MathMatrixInset::write(std::ostream & os, bool fragile) const
140 bool n = numberedType();
142 for (int row = 0; row < nrows(); ++row) {
145 for (int col = 0; col < ncols(); ++col) {
148 cell(index(row, col)).write(os, fragile);
151 if (!label_[row].empty())
152 os << "\\label{" << label_[row] << "}";
162 string MathMatrixInset::label(int row) const
167 void MathMatrixInset::label(int row, string const & label)
173 void MathMatrixInset::numbered(int row, bool num)
179 bool MathMatrixInset::numbered(int row) const
185 bool MathMatrixInset::ams() const
191 bool MathMatrixInset::display() const
193 return getType() != LM_OT_SIMPLE;
197 std::vector<string> const MathMatrixInset::getLabelList() const
199 std::vector<string> res;
200 for (int row = 0; row < nrows(); ++row)
201 if (!label_[row].empty() && nonum_[row] != 1)
202 res.push_back(label_[row]);
207 bool MathMatrixInset::numberedType() const
209 if (getType() == LM_OT_SIMPLE)
211 for (int row = 0; row < nrows(); ++row)
218 void MathMatrixInset::validate(LaTeXFeatures & features) const
220 features.amsstyle = ams();
222 // Validation is necessary only if not using AMS math.
223 // To be safe, we will always run mathedvalidate.
224 //if (features.amsstyle)
227 features.boldsymbol = true;
228 //features.binom = true;
230 MathNestInset::validate(features);
234 void MathMatrixInset::header_write(std::ostream & os) const
236 bool n = numberedType();
247 os << "\\begin{equation" << star(n) << "}\n";
253 os << "\\begin{eqnarray" << star(n) << "}\n";
257 os << "\\begin{align" << star(n) << "}";
261 os << "\\begin{alignat" << star(n) << "}"
262 << "{" << ncols()/2 << "}\n";
265 os << "\\begin{unknown" << star(n) << "}";
270 void MathMatrixInset::footer_write(std::ostream & os) const
272 bool n = numberedType();
281 os << "\\end{equation" << star(n) << "}\n";
287 os << "\\end{eqnarray" << star(n) << "}\n";
291 os << "\\end{align" << star(n) << "}\n";
295 os << "\\end{alignat" << star(n) << "}\n";
299 os << "\\end{unknown" << star(n) << "}";
304 void MathMatrixInset::addRow(int row)
306 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
307 label_.insert(label_.begin() + row + 1, string());
308 MathGridInset::addRow(row);
311 void MathMatrixInset::appendRow()
313 nonum_.push_back(!numberedType());
314 label_.push_back(string());
315 MathGridInset::appendRow();
319 void MathMatrixInset::delRow(int row)
321 MathGridInset::delRow(row);
322 nonum_.erase(nonum_.begin() + row);
323 label_.erase(label_.begin() + row);
326 void MathMatrixInset::addCol(int col)
330 mutate(LM_OT_EQNARRAY);
340 MathGridInset::addCol(col);
342 MathGridInset::addCol(col);
351 void MathMatrixInset::delCol(int col)
355 MathGridInset::delCol(col);
364 string MathMatrixInset::nicelabel(int row) const
368 if (label_[row].empty())
369 return string("(#)");
370 return "(" + label_[row] + ")";
375 short typecode(string const & s)
378 return LM_OT_EQUATION;
380 return LM_OT_EQUATION;
382 return LM_OT_EQNARRAY;
388 return LM_OT_XXALIGN;
390 return LM_OT_MULTLINE;
395 void MathMatrixInset::mutate(string const & newtype)
397 if (newtype == "dump") {
401 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
402 mutate(typecode(newtype));
405 void MathMatrixInset::glueall()
408 for (int i = 0; i < nargs(); ++i)
409 ar.push_back(cell(i));
410 *this = MathMatrixInset(LM_OT_SIMPLE);
415 MathInsetTypes MathMatrixInset::getType() const
421 void MathMatrixInset::setType(MathInsetTypes t)
428 void MathMatrixInset::mutate(short newtype)
430 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
432 if (newtype == getType())
437 setType(LM_OT_EQUATION);
445 setType(LM_OT_SIMPLE);
449 MathGridInset::addCol(1);
452 int pos = firstRelOp(cell(0));
454 cell(0).erase(pos, cell(0).size());
455 cell(1).erase(0, pos);
458 setType(LM_OT_ALIGN);
464 MathGridInset::addCol(1);
465 MathGridInset::addCol(1);
467 // split it "nicely" on the firest relop
468 int pos = firstRelOp(cell(0));
470 cell(0).erase(pos, cell(0).size());
471 cell(1).erase(0, pos);
473 if (cell(1).size()) {
475 cell(1).erase(1, cell(1).size());
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";