7 #include "math_matrixinset.h"
10 #include "support/LOstream.h"
12 #include "LaTeXFeatures.h"
18 int getCols(MathInsetTypes type)
34 // returns position of first relation operator in the array
35 // used for "intelligent splitting"
36 int firstRelOp(MathArray const & array)
38 for (MathArray::const_iterator it = array.begin(); it != array.end(); ++it)
40 return it - array.begin();
45 char const * star(bool numbered)
47 return numbered ? "" : "*";
53 MathMatrixInset::MathMatrixInset()
54 : MathGridInset(1, 1), objtype_(LM_OT_SIMPLE), nonum_(1), label_(1)
60 MathMatrixInset::MathMatrixInset(MathInsetTypes t)
61 : MathGridInset(getCols(t), 1), objtype_(t), nonum_(1), label_(1)
67 MathMatrixInset::MathMatrixInset(MathInsetTypes t, int cols)
68 : MathGridInset(cols, 1), objtype_(t), nonum_(1), label_(1)
74 MathInset * MathMatrixInset::clone() const
76 return new MathMatrixInset(*this);
80 char MathMatrixInset::defaultColAlign(int col)
95 int MathMatrixInset::defaultColSpace(int col)
102 return (col & 1) ? 20 : 0;
103 case LM_OT_XXALIGNAT:
104 return (col & 1) ? 40 : 0;
111 void MathMatrixInset::metrics(MathStyles) const
113 size_ = (getType() == LM_OT_SIMPLE) ? LM_ST_TEXT : LM_ST_DISPLAY;
115 // let the cells adjust themselves
116 MathGridInset::metrics(size_);
123 if (numberedType()) {
125 for (int row = 0; row < nrows(); ++row)
126 l = std::max(l, mathed_string_width(LM_TC_BF, size(), nicelabel(row)));
132 // make it at least as high as the current font
135 math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, des);
136 ascent_ = std::max(ascent_, asc);
137 descent_ = std::max(descent_, des);
141 void MathMatrixInset::draw(Painter & pain, int x, int y) const
146 MathGridInset::draw(pain, x, y);
148 if (numberedType()) {
149 int xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
150 for (int row = 0; row < nrows(); ++row) {
151 int yy = y + rowinfo_[row].offset_;
152 drawStr(pain, LM_TC_BF, size(), xx, yy, nicelabel(row));
158 void MathMatrixInset::write(std::ostream & os, bool fragile) const
162 bool n = numberedType();
164 for (int row = 0; row < nrows(); ++row) {
165 for (int col = 0; col < ncols(); ++col) {
166 cell(index(row, col)).write(os, fragile);
167 os << eocString(col);
170 if (!label_[row].empty())
171 os << "\\label{" << label_[row] << "}";
175 os << eolString(row);
182 string MathMatrixInset::label(int row) const
188 void MathMatrixInset::label(int row, string const & label)
194 void MathMatrixInset::numbered(int row, bool num)
200 bool MathMatrixInset::numbered(int row) const
206 bool MathMatrixInset::ams() const
212 bool MathMatrixInset::display() const
214 return getType() != LM_OT_SIMPLE;
218 std::vector<string> const MathMatrixInset::getLabelList() const
220 std::vector<string> res;
221 for (int row = 0; row < nrows(); ++row)
222 if (!label_[row].empty() && nonum_[row] != 1)
223 res.push_back(label_[row]);
228 bool MathMatrixInset::numberedType() const
230 if (getType() == LM_OT_SIMPLE || getType() == LM_OT_XXALIGNAT)
232 for (int row = 0; row < nrows(); ++row)
239 void MathMatrixInset::validate(LaTeXFeatures & features) const
241 features.amsstyle = ams();
243 // Validation is necessary only if not using AMS math.
244 // To be safe, we will always run mathedvalidate.
245 //if (features.amsstyle)
248 features.boldsymbol = true;
249 //features.binom = true;
251 MathNestInset::validate(features);
255 void MathMatrixInset::header_write(std::ostream & os) const
257 bool n = numberedType();
268 os << "\\begin{equation" << star(n) << "}\n";
274 os << "\\begin{eqnarray" << star(n) << "}\n";
278 os << "\\begin{align" << star(n) << "}";
282 os << "\\begin{alignat" << star(n) << "}" << "{" << ncols()/2 << "}\n";
286 os << "\\begin{xalignat" << star(n) << "}" << "{" << ncols()/2 << "}\n";
289 case LM_OT_XXALIGNAT:
290 os << "\\begin{xxalignat}" << "{" << ncols()/2 << "}\n";
294 os << "\\begin{multline}\n";
298 os << "\\begin{gather}\n";
302 os << "\\begin{unknown" << star(n) << "}";
307 void MathMatrixInset::footer_write(std::ostream & os) const
309 bool n = numberedType();
318 os << "\\end{equation" << star(n) << "}\n";
324 os << "\\end{eqnarray" << star(n) << "}\n";
328 os << "\\end{align" << star(n) << "}\n";
332 os << "\\end{alignat" << star(n) << "}\n";
336 os << "\\end{xalignat" << star(n) << "}\n";
339 case LM_OT_XXALIGNAT:
340 os << "\\end{xxalignat}\n";
344 os << "\\end{multline}\n";
348 os << "\\end{gather}\n";
352 os << "\\end{unknown" << star(n) << "}";
357 void MathMatrixInset::addRow(int row)
359 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
360 label_.insert(label_.begin() + row + 1, string());
361 MathGridInset::addRow(row);
365 void MathMatrixInset::appendRow()
367 nonum_.push_back(!numberedType());
368 label_.push_back(string());
369 MathGridInset::appendRow();
373 void MathMatrixInset::delRow(int row)
375 MathGridInset::delRow(row);
376 nonum_.erase(nonum_.begin() + row);
377 label_.erase(label_.begin() + row);
381 void MathMatrixInset::addCol(int col)
385 mutate(LM_OT_EQNARRAY);
394 mutate(LM_OT_ALIGNAT);
400 case LM_OT_XXALIGNAT:
401 MathGridInset::addCol(col);
402 MathGridInset::addCol(col + 1);
411 void MathMatrixInset::delCol(int col)
416 case LM_OT_XXALIGNAT:
417 MathGridInset::delCol(col + 1);
418 MathGridInset::delCol(col);
426 string MathMatrixInset::nicelabel(int row) const
430 if (label_[row].empty())
431 return string("(#)");
432 return "(" + label_[row] + ")";
437 MathInsetTypes typecode(string const & s)
440 return LM_OT_EQUATION;
442 return LM_OT_EQUATION;
444 return LM_OT_EQNARRAY;
450 return LM_OT_XALIGNAT;
451 if (s == "xxalignat")
452 return LM_OT_XXALIGNAT;
454 return LM_OT_MULTLINE;
461 void MathMatrixInset::mutate(string const & newtype)
463 if (newtype == "dump") {
467 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
468 mutate(typecode(newtype));
471 void MathMatrixInset::glueall()
474 for (int i = 0; i < nargs(); ++i)
475 ar.push_back(cell(i));
476 *this = MathMatrixInset(LM_OT_SIMPLE);
481 MathInsetTypes MathMatrixInset::getType() const
487 void MathMatrixInset::setType(MathInsetTypes t)
495 void MathMatrixInset::mutate(MathInsetTypes newtype)
497 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
499 if (newtype == getType())
504 setType(LM_OT_EQUATION);
512 setType(LM_OT_SIMPLE);
518 case LM_OT_XXALIGNAT: {
520 MathGridInset::addCol(1);
523 int pos = firstRelOp(cell(0));
525 cell(0).erase(pos, cell(0).size());
526 cell(1).erase(0, pos);
527 setType(LM_OT_ALIGN);
534 MathGridInset::addCol(1);
535 MathGridInset::addCol(1);
537 // split it "nicely" on the firest relop
538 int pos = firstRelOp(cell(0));
540 cell(0).erase(pos, cell(0).size());
541 cell(1).erase(0, pos);
543 if (cell(1).size()) {
545 cell(1).erase(1, cell(1).size());
549 setType(LM_OT_EQNARRAY);
558 case LM_OT_EQUATION: {
559 // set correct (no)numbering
560 bool allnonum = true;
561 for (int r = 0; r < nrows(); ++r) {
566 // set first non-empty label
568 for (int r = 0; r < nrows(); ++r) {
569 if (!label_[r].empty()) {
577 nonum_[0] = allnonum;
586 case LM_OT_XXALIGNAT:
588 for (int row = 0; row < nrows(); ++row) {
590 cell(c).push_back(cell(c + 1));
592 MathGridInset::delCol(2);
593 setType(LM_OT_ALIGN);
605 MathGridInset::addCol(1);
606 setType(LM_OT_EQNARRAY);
612 case LM_OT_XXALIGNAT:
617 lyxerr << "mutation from '" << getType()
618 << "' to '" << newtype << "' not implemented\n";
626 setType(LM_OT_GATHER);
629 lyxerr << "mutation from '" << getType()
630 << "' to '" << newtype << "' not implemented\n";
637 setType(LM_OT_MULTLINE);
640 lyxerr << "mutation from '" << getType()
641 << "' to '" << newtype << "' not implemented\n";
646 lyxerr << "mutation from '" << getType()
647 << "' to '" << newtype << "' not implemented\n";