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(MathMetricsInfo const & st) const
146 size_.size = (getType() == LM_OT_SIMPLE) ? LM_ST_TEXT : LM_ST_DISPLAY;
148 // let the cells adjust themselves
149 MathGridInset::metrics(size_);
156 if (numberedType()) {
158 for (row_type row = 0; row < nrows(); ++row)
159 l = std::max(l, mathed_string_width(LM_TC_BF, size(), nicelabel(row)));
165 // make it at least as high as the current font
168 math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, des);
169 ascent_ = std::max(ascent_, asc);
170 descent_ = std::max(descent_, des);
174 void MathMatrixInset::draw(Painter & pain, int x, int y) const
179 MathGridInset::draw(pain, x, y);
181 if (numberedType()) {
182 int xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
183 for (row_type row = 0; row < nrows(); ++row) {
184 int yy = y + rowinfo_[row].offset_;
185 drawStr(pain, LM_TC_BF, size(), xx, yy, nicelabel(row));
191 void MathMatrixInset::write(MathWriteInfo & os) const
195 bool n = numberedType();
197 for (row_type row = 0; row < nrows(); ++row) {
198 for (col_type col = 0; col < ncols(); ++col)
199 os << cell(index(row, col)) << eocString(col);
201 if (!label_[row].empty())
202 os << "\\label{" << label_[row] << "}";
206 os << eolString(row);
213 void MathMatrixInset::writeNormal(std::ostream & os) const
215 os << "[formula " << normalName(getType()) << " ";
216 MathGridInset::writeNormal(os);
222 string MathMatrixInset::label(row_type row) const
228 void MathMatrixInset::label(row_type row, string const & label)
234 void MathMatrixInset::numbered(row_type row, bool num)
240 bool MathMatrixInset::numbered(row_type row) const
246 bool MathMatrixInset::ams() const
252 bool MathMatrixInset::display() const
254 return getType() != LM_OT_SIMPLE;
258 std::vector<string> const MathMatrixInset::getLabelList() const
260 std::vector<string> res;
261 for (row_type row = 0; row < nrows(); ++row)
262 if (!label_[row].empty() && nonum_[row] != 1)
263 res.push_back(label_[row]);
268 bool MathMatrixInset::numberedType() const
270 if (getType() == LM_OT_SIMPLE || getType() == LM_OT_XXALIGNAT)
272 for (row_type row = 0; row < nrows(); ++row)
279 void MathMatrixInset::validate(LaTeXFeatures & features) const
281 features.amsstyle = ams();
283 // Validation is necessary only if not using AMS math.
284 // To be safe, we will always run mathedvalidate.
285 //if (features.amsstyle)
288 features.boldsymbol = true;
289 //features.binom = true;
291 MathNestInset::validate(features);
295 void MathMatrixInset::header_write(std::ostream & os) const
297 bool n = numberedType();
308 os << "\\begin{equation" << star(n) << "}\n";
314 os << "\\begin{eqnarray" << star(n) << "}\n";
318 os << "\\begin{align" << star(n) << "}\n";
322 os << "\\begin{alignat" << star(n) << "}" << "{" << ncols()/2 << "}\n";
326 os << "\\begin{xalignat" << star(n) << "}" << "{" << ncols()/2 << "}\n";
329 case LM_OT_XXALIGNAT:
330 os << "\\begin{xxalignat}" << "{" << ncols()/2 << "}\n";
334 os << "\\begin{multline}\n";
338 os << "\\begin{gather}\n";
342 os << "\\begin{unknown" << star(n) << "}";
347 void MathMatrixInset::footer_write(std::ostream & os) const
349 bool n = numberedType();
358 os << "\\end{equation" << star(n) << "}\n";
364 os << "\n\\end{eqnarray" << star(n) << "}\n";
368 os << "\n\\end{align" << star(n) << "}\n";
372 os << "\n\\end{alignat" << star(n) << "}\n";
376 os << "\n\\end{xalignat" << star(n) << "}\n";
379 case LM_OT_XXALIGNAT:
380 os << "\n\\end{xxalignat}\n";
384 os << "\n\\end{multline}\n";
388 os << "\n\\end{gather}\n";
392 os << "\\end{unknown" << star(n) << "}";
397 void MathMatrixInset::addRow(row_type row)
399 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
400 label_.insert(label_.begin() + row + 1, string());
401 MathGridInset::addRow(row);
405 void MathMatrixInset::appendRow()
407 nonum_.push_back(!numberedType());
408 label_.push_back(string());
409 MathGridInset::appendRow();
413 void MathMatrixInset::delRow(row_type row)
415 MathGridInset::delRow(row);
416 nonum_.erase(nonum_.begin() + row);
417 label_.erase(label_.begin() + row);
421 void MathMatrixInset::addCol(col_type col)
425 mutate(LM_OT_EQNARRAY);
434 mutate(LM_OT_ALIGNAT);
440 case LM_OT_XXALIGNAT:
441 MathGridInset::addCol(col);
442 MathGridInset::addCol(col + 1);
451 void MathMatrixInset::delCol(col_type col)
456 case LM_OT_XXALIGNAT:
457 MathGridInset::delCol(col + 1);
458 MathGridInset::delCol(col);
466 string MathMatrixInset::nicelabel(row_type row) const
470 if (label_[row].empty())
471 return string("(#)");
472 return "(" + label_[row] + ")";
476 void MathMatrixInset::mutate(string const & newtype)
478 if (newtype == "dump") {
482 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
483 mutate(typecode(newtype));
487 void MathMatrixInset::glueall()
490 for (idx_type i = 0; i < nargs(); ++i)
491 ar.push_back(cell(i));
492 *this = MathMatrixInset(LM_OT_SIMPLE);
497 MathInsetTypes MathMatrixInset::getType() const
503 void MathMatrixInset::setType(MathInsetTypes t)
511 void MathMatrixInset::mutate(MathInsetTypes newtype)
513 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
515 if (newtype == getType())
520 setType(LM_OT_EQUATION);
528 setType(LM_OT_SIMPLE);
534 case LM_OT_XXALIGNAT: {
536 MathGridInset::addCol(1);
539 pos_type pos = firstRelOp(cell(0));
541 cell(0).erase(pos, cell(0).size());
542 cell(1).erase(0, pos);
543 setType(LM_OT_ALIGN);
550 MathGridInset::addCol(1);
551 MathGridInset::addCol(1);
553 // split it "nicely" on the firest relop
554 pos_type pos = firstRelOp(cell(0));
556 cell(0).erase(pos, cell(0).size());
557 cell(1).erase(0, pos);
559 if (cell(1).size()) {
561 cell(1).erase(1, cell(1).size());
565 setType(LM_OT_EQNARRAY);
574 case LM_OT_EQUATION: {
575 // set correct (no)numbering
576 bool allnonum = true;
577 for (row_type row = 0; row < nrows(); ++row) {
582 // set first non-empty label
584 for (row_type row = 0; row < nrows(); ++row) {
585 if (!label_[row].empty()) {
593 nonum_[0] = allnonum;
602 case LM_OT_XXALIGNAT:
604 for (row_type row = 0; row < nrows(); ++row) {
605 idx_type c = 3 * row + 1;
606 cell(c).push_back(cell(c + 1));
608 MathGridInset::delCol(2);
609 setType(LM_OT_ALIGN);
621 MathGridInset::addCol(1);
622 setType(LM_OT_EQNARRAY);
628 case LM_OT_XXALIGNAT:
633 lyxerr << "mutation from '" << getType()
634 << "' to '" << newtype << "' not implemented\n";
642 setType(LM_OT_GATHER);
645 lyxerr << "mutation from '" << getType()
646 << "' to '" << newtype << "' not implemented\n";
653 setType(LM_OT_MULTLINE);
656 lyxerr << "mutation from '" << getType()
657 << "' to '" << newtype << "' not implemented\n";
662 lyxerr << "mutation from '" << getType()
663 << "' to '" << newtype << "' not implemented\n";