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 & mi) const
146 mi_.style = (getType() == LM_OT_SIMPLE) ? LM_ST_TEXT : LM_ST_DISPLAY;
148 // let the cells adjust themselves
149 MathGridInset::metrics(mi_);
156 if (numberedType()) {
158 for (row_type row = 0; row < nrows(); ++row)
159 l = std::max(l, mathed_string_width(LM_TC_BF, mi_, nicelabel(row)));
165 // make it at least as high as the current font
168 math_font_max_dim(LM_TC_TEXTRM, mi_, 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
176 MathGridInset::draw(pain, x, y);
178 if (numberedType()) {
179 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
180 for (row_type row = 0; row < nrows(); ++row) {
181 int const yy = y + rowinfo_[row].offset_;
182 drawStr(pain, LM_TC_BF, mi_, xx, yy, nicelabel(row));
188 void MathMatrixInset::write(MathWriteInfo & os) const
192 bool n = numberedType();
194 for (row_type row = 0; row < nrows(); ++row) {
195 for (col_type col = 0; col < ncols(); ++col)
196 os << cell(index(row, col)) << eocString(col);
198 if (!label_[row].empty())
199 os << "\\label{" << label_[row] << "}";
203 os << eolString(row);
210 void MathMatrixInset::writeNormal(std::ostream & os) const
212 os << "[formula " << normalName(getType()) << " ";
213 MathGridInset::writeNormal(os);
219 string MathMatrixInset::label(row_type row) const
225 void MathMatrixInset::label(row_type row, string const & label)
231 void MathMatrixInset::numbered(row_type row, bool num)
237 bool MathMatrixInset::numbered(row_type row) const
243 bool MathMatrixInset::ams() const
249 bool MathMatrixInset::display() const
251 return getType() != LM_OT_SIMPLE;
255 std::vector<string> const MathMatrixInset::getLabelList() const
257 std::vector<string> res;
258 for (row_type row = 0; row < nrows(); ++row)
259 if (!label_[row].empty() && nonum_[row] != 1)
260 res.push_back(label_[row]);
265 bool MathMatrixInset::numberedType() const
267 if (getType() == LM_OT_SIMPLE || getType() == LM_OT_XXALIGNAT)
269 for (row_type row = 0; row < nrows(); ++row)
276 void MathMatrixInset::validate(LaTeXFeatures & features) const
278 features.amsstyle = ams();
280 // Validation is necessary only if not using AMS math.
281 // To be safe, we will always run mathedvalidate.
282 //if (features.amsstyle)
285 features.boldsymbol = true;
286 //features.binom = true;
288 MathNestInset::validate(features);
292 void MathMatrixInset::header_write(std::ostream & os) const
294 bool n = numberedType();
305 os << "\\begin{equation" << star(n) << "}\n";
311 os << "\\begin{eqnarray" << star(n) << "}\n";
315 os << "\\begin{align" << star(n) << "}\n";
319 os << "\\begin{alignat" << star(n) << "}" << "{" << ncols()/2 << "}\n";
323 os << "\\begin{xalignat" << star(n) << "}" << "{" << ncols()/2 << "}\n";
326 case LM_OT_XXALIGNAT:
327 os << "\\begin{xxalignat}" << "{" << ncols()/2 << "}\n";
331 os << "\\begin{multline}\n";
335 os << "\\begin{gather}\n";
339 os << "\\begin{unknown" << star(n) << "}";
344 void MathMatrixInset::footer_write(std::ostream & os) const
346 bool n = numberedType();
355 os << "\\end{equation" << star(n) << "}\n";
361 os << "\n\\end{eqnarray" << star(n) << "}\n";
365 os << "\n\\end{align" << star(n) << "}\n";
369 os << "\n\\end{alignat" << star(n) << "}\n";
373 os << "\n\\end{xalignat" << star(n) << "}\n";
376 case LM_OT_XXALIGNAT:
377 os << "\n\\end{xxalignat}\n";
381 os << "\n\\end{multline}\n";
385 os << "\n\\end{gather}\n";
389 os << "\\end{unknown" << star(n) << "}";
394 void MathMatrixInset::addRow(row_type row)
396 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
397 label_.insert(label_.begin() + row + 1, string());
398 MathGridInset::addRow(row);
402 void MathMatrixInset::appendRow()
404 nonum_.push_back(!numberedType());
405 label_.push_back(string());
406 MathGridInset::appendRow();
410 void MathMatrixInset::delRow(row_type row)
412 MathGridInset::delRow(row);
413 nonum_.erase(nonum_.begin() + row);
414 label_.erase(label_.begin() + row);
418 void MathMatrixInset::addCol(col_type col)
422 mutate(LM_OT_EQNARRAY);
431 mutate(LM_OT_ALIGNAT);
437 case LM_OT_XXALIGNAT:
438 MathGridInset::addCol(col);
439 MathGridInset::addCol(col + 1);
448 void MathMatrixInset::delCol(col_type col)
453 case LM_OT_XXALIGNAT:
454 MathGridInset::delCol(col + 1);
455 MathGridInset::delCol(col);
463 string MathMatrixInset::nicelabel(row_type row) const
467 if (label_[row].empty())
468 return string("(#)");
469 return "(" + label_[row] + ")";
473 void MathMatrixInset::mutate(string const & newtype)
475 if (newtype == "dump") {
479 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
480 mutate(typecode(newtype));
484 void MathMatrixInset::glueall()
487 for (idx_type i = 0; i < nargs(); ++i)
488 ar.push_back(cell(i));
489 *this = MathMatrixInset(LM_OT_SIMPLE);
494 MathInsetTypes MathMatrixInset::getType() const
500 void MathMatrixInset::setType(MathInsetTypes t)
508 void MathMatrixInset::mutate(MathInsetTypes newtype)
510 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
512 if (newtype == getType())
517 setType(LM_OT_EQUATION);
525 setType(LM_OT_SIMPLE);
531 case LM_OT_XXALIGNAT: {
533 MathGridInset::addCol(1);
536 pos_type pos = firstRelOp(cell(0));
538 cell(0).erase(pos, cell(0).size());
539 cell(1).erase(0, pos);
540 setType(LM_OT_ALIGN);
547 MathGridInset::addCol(1);
548 MathGridInset::addCol(1);
550 // split it "nicely" on the firest relop
551 pos_type pos = firstRelOp(cell(0));
553 cell(0).erase(pos, cell(0).size());
554 cell(1).erase(0, pos);
556 if (cell(1).size()) {
558 cell(1).erase(1, cell(1).size());
562 setType(LM_OT_EQNARRAY);
571 case LM_OT_EQUATION: {
572 // set correct (no)numbering
573 bool allnonum = true;
574 for (row_type row = 0; row < nrows(); ++row) {
579 // set first non-empty label
581 for (row_type row = 0; row < nrows(); ++row) {
582 if (!label_[row].empty()) {
590 nonum_[0] = allnonum;
599 case LM_OT_XXALIGNAT:
601 for (row_type row = 0; row < nrows(); ++row) {
602 idx_type c = 3 * row + 1;
603 cell(c).push_back(cell(c + 1));
605 MathGridInset::delCol(2);
606 setType(LM_OT_ALIGN);
618 MathGridInset::addCol(1);
619 setType(LM_OT_EQNARRAY);
625 case LM_OT_XXALIGNAT:
630 lyxerr << "mutation from '" << getType()
631 << "' to '" << newtype << "' not implemented\n";
639 setType(LM_OT_GATHER);
642 lyxerr << "mutation from '" << getType()
643 << "' to '" << newtype << "' not implemented\n";
650 setType(LM_OT_MULTLINE);
653 lyxerr << "mutation from '" << getType()
654 << "' to '" << newtype << "' not implemented\n";
659 lyxerr << "mutation from '" << getType()
660 << "' to '" << newtype << "' not implemented\n";