7 #include "math_matrixinset.h"
10 #include "support/LOstream.h"
12 #include "LaTeXFeatures.h"
17 int getCols(MathInsetTypes type)
33 // returns position of first relation operator in the array
34 // used for "intelligent splitting"
35 int firstRelOp(MathArray const & ar)
37 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
39 return it - ar.begin();
44 char const * star(bool numbered)
46 return numbered ? "" : "*";
49 MathInsetTypes typecode(string const & s)
51 if (s == "equation") return LM_OT_EQUATION;
52 if (s == "display") return LM_OT_EQUATION;
53 if (s == "eqnarray") return LM_OT_EQNARRAY;
54 if (s == "align") return LM_OT_ALIGN;
55 if (s == "alignat") return LM_OT_ALIGNAT;
56 if (s == "xalignat") return LM_OT_XALIGNAT;
57 if (s == "xxalignat") return LM_OT_XXALIGNAT;
58 if (s == "multline") return LM_OT_MULTLINE;
59 if (s == "gather") return LM_OT_GATHER;
64 string normalName(MathInsetTypes t)
67 case LM_OT_EQUATION: return "equation";
68 case LM_OT_EQNARRAY: return "eqnarray";
69 case LM_OT_ALIGN: return "align";
70 case LM_OT_ALIGNAT: return "alignat";
71 case LM_OT_XALIGNAT: return "xalignat";
72 case LM_OT_XXALIGNAT: return "xxalignat";
73 case LM_OT_MULTLINE: return "multline";
74 case LM_OT_GATHER: return "gather";
75 case LM_OT_SIMPLE: return "simple";
81 } // end anon namespace
84 MathMatrixInset::MathMatrixInset()
85 : MathGridInset(1, 1), objtype_(LM_OT_SIMPLE), nonum_(1), label_(1)
91 MathMatrixInset::MathMatrixInset(MathInsetTypes t)
92 : MathGridInset(getCols(t), 1), objtype_(t), nonum_(1), label_(1)
98 MathMatrixInset::MathMatrixInset(MathInsetTypes t, col_type cols)
99 : MathGridInset(cols, 1), objtype_(t), nonum_(1), label_(1)
105 MathInset * MathMatrixInset::clone() const
107 return new MathMatrixInset(*this);
111 char MathMatrixInset::defaultColAlign(col_type col)
117 case LM_OT_XXALIGNAT:
118 return "rl"[col & 1];
127 int MathMatrixInset::defaultColSpace(col_type col)
134 return (col & 1) ? 20 : 0;
135 case LM_OT_XXALIGNAT:
136 return (col & 1) ? 40 : 0;
143 void MathMatrixInset::metrics(MathStyles) const
145 size_ = (getType() == LM_OT_SIMPLE) ? LM_ST_TEXT : LM_ST_DISPLAY;
147 // let the cells adjust themselves
148 MathGridInset::metrics(size_);
155 if (numberedType()) {
157 for (row_type row = 0; row < nrows(); ++row)
158 l = std::max(l, mathed_string_width(LM_TC_BF, size(), nicelabel(row)));
164 // make it at least as high as the current font
167 math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, des);
168 ascent_ = std::max(ascent_, asc);
169 descent_ = std::max(descent_, des);
173 void MathMatrixInset::draw(Painter & pain, int x, int y) const
178 MathGridInset::draw(pain, x, y);
180 if (numberedType()) {
181 int xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
182 for (row_type row = 0; row < nrows(); ++row) {
183 int yy = y + rowinfo_[row].offset_;
184 drawStr(pain, LM_TC_BF, size(), xx, yy, nicelabel(row));
190 void MathMatrixInset::write(std::ostream & os, bool fragile) const
194 bool n = numberedType();
196 for (row_type row = 0; row < nrows(); ++row) {
197 for (col_type col = 0; col < ncols(); ++col) {
198 cell(index(row, col)).write(os, fragile);
199 os << eocString(col);
202 if (!label_[row].empty())
203 os << "\\label{" << label_[row] << "}";
207 os << eolString(row);
214 void MathMatrixInset::writeNormal(std::ostream & os) const
216 os << "[formula " << normalName(getType()) << " ";
217 MathGridInset::writeNormal(os);
223 string MathMatrixInset::label(row_type row) const
229 void MathMatrixInset::label(row_type row, string const & label)
235 void MathMatrixInset::numbered(row_type row, bool num)
241 bool MathMatrixInset::numbered(row_type row) const
247 bool MathMatrixInset::ams() const
253 bool MathMatrixInset::display() const
255 return getType() != LM_OT_SIMPLE;
259 std::vector<string> const MathMatrixInset::getLabelList() const
261 std::vector<string> res;
262 for (row_type row = 0; row < nrows(); ++row)
263 if (!label_[row].empty() && nonum_[row] != 1)
264 res.push_back(label_[row]);
269 bool MathMatrixInset::numberedType() const
271 if (getType() == LM_OT_SIMPLE || getType() == LM_OT_XXALIGNAT)
273 for (row_type row = 0; row < nrows(); ++row)
280 void MathMatrixInset::validate(LaTeXFeatures & features) const
282 features.amsstyle = ams();
284 // Validation is necessary only if not using AMS math.
285 // To be safe, we will always run mathedvalidate.
286 //if (features.amsstyle)
289 features.boldsymbol = true;
290 //features.binom = true;
292 MathNestInset::validate(features);
296 void MathMatrixInset::header_write(std::ostream & os) const
298 bool n = numberedType();
309 os << "\\begin{equation" << star(n) << "}\n";
315 os << "\\begin{eqnarray" << star(n) << "}\n";
319 os << "\\begin{align" << star(n) << "}\n";
323 os << "\\begin{alignat" << star(n) << "}" << "{" << ncols()/2 << "}\n";
327 os << "\\begin{xalignat" << star(n) << "}" << "{" << ncols()/2 << "}\n";
330 case LM_OT_XXALIGNAT:
331 os << "\\begin{xxalignat}" << "{" << ncols()/2 << "}\n";
335 os << "\\begin{multline}\n";
339 os << "\\begin{gather}\n";
343 os << "\\begin{unknown" << star(n) << "}";
348 void MathMatrixInset::footer_write(std::ostream & os) const
350 bool n = numberedType();
359 os << "\\end{equation" << star(n) << "}\n";
365 os << "\n\\end{eqnarray" << star(n) << "}\n";
369 os << "\n\\end{align" << star(n) << "}\n";
373 os << "\n\\end{alignat" << star(n) << "}\n";
377 os << "\n\\end{xalignat" << star(n) << "}\n";
380 case LM_OT_XXALIGNAT:
381 os << "\n\\end{xxalignat}\n";
385 os << "\n\\end{multline}\n";
389 os << "\n\\end{gather}\n";
393 os << "\\end{unknown" << star(n) << "}";
398 void MathMatrixInset::addRow(row_type row)
400 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
401 label_.insert(label_.begin() + row + 1, string());
402 MathGridInset::addRow(row);
406 void MathMatrixInset::appendRow()
408 nonum_.push_back(!numberedType());
409 label_.push_back(string());
410 MathGridInset::appendRow();
414 void MathMatrixInset::delRow(row_type row)
416 MathGridInset::delRow(row);
417 nonum_.erase(nonum_.begin() + row);
418 label_.erase(label_.begin() + row);
422 void MathMatrixInset::addCol(col_type col)
426 mutate(LM_OT_EQNARRAY);
435 mutate(LM_OT_ALIGNAT);
441 case LM_OT_XXALIGNAT:
442 MathGridInset::addCol(col);
443 MathGridInset::addCol(col + 1);
452 void MathMatrixInset::delCol(col_type col)
457 case LM_OT_XXALIGNAT:
458 MathGridInset::delCol(col + 1);
459 MathGridInset::delCol(col);
467 string MathMatrixInset::nicelabel(row_type row) const
471 if (label_[row].empty())
472 return string("(#)");
473 return "(" + label_[row] + ")";
477 void MathMatrixInset::mutate(string const & newtype)
479 if (newtype == "dump") {
483 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
484 mutate(typecode(newtype));
488 void MathMatrixInset::glueall()
491 for (idx_type i = 0; i < nargs(); ++i)
492 ar.push_back(cell(i));
493 *this = MathMatrixInset(LM_OT_SIMPLE);
498 MathInsetTypes MathMatrixInset::getType() const
504 void MathMatrixInset::setType(MathInsetTypes t)
512 void MathMatrixInset::mutate(MathInsetTypes newtype)
514 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
516 if (newtype == getType())
521 setType(LM_OT_EQUATION);
529 setType(LM_OT_SIMPLE);
535 case LM_OT_XXALIGNAT: {
537 MathGridInset::addCol(1);
540 pos_type pos = firstRelOp(cell(0));
542 cell(0).erase(pos, cell(0).size());
543 cell(1).erase(0, pos);
544 setType(LM_OT_ALIGN);
551 MathGridInset::addCol(1);
552 MathGridInset::addCol(1);
554 // split it "nicely" on the firest relop
555 pos_type pos = firstRelOp(cell(0));
557 cell(0).erase(pos, cell(0).size());
558 cell(1).erase(0, pos);
560 if (cell(1).size()) {
562 cell(1).erase(1, cell(1).size());
566 setType(LM_OT_EQNARRAY);
575 case LM_OT_EQUATION: {
576 // set correct (no)numbering
577 bool allnonum = true;
578 for (row_type row = 0; row < nrows(); ++row) {
583 // set first non-empty label
585 for (row_type row = 0; row < nrows(); ++row) {
586 if (!label_[row].empty()) {
594 nonum_[0] = allnonum;
603 case LM_OT_XXALIGNAT:
605 for (row_type row = 0; row < nrows(); ++row) {
606 idx_type c = 3 * row + 1;
607 cell(c).push_back(cell(c + 1));
609 MathGridInset::delCol(2);
610 setType(LM_OT_ALIGN);
622 MathGridInset::addCol(1);
623 setType(LM_OT_EQNARRAY);
629 case LM_OT_XXALIGNAT:
634 lyxerr << "mutation from '" << getType()
635 << "' to '" << newtype << "' not implemented\n";
643 setType(LM_OT_GATHER);
646 lyxerr << "mutation from '" << getType()
647 << "' to '" << newtype << "' not implemented\n";
654 setType(LM_OT_MULTLINE);
657 lyxerr << "mutation from '" << getType()
658 << "' to '" << newtype << "' not implemented\n";
663 lyxerr << "mutation from '" << getType()
664 << "' to '" << newtype << "' not implemented\n";