7 #include "math_matrixinset.h"
8 #include "math_support.h"
11 #include "LaTeXFeatures.h"
12 #include "math_mathmlstream.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::mathmlize(MathMLStream & os) const
190 MathGridInset::mathmlize(os);
194 string MathMatrixInset::label(row_type row) const
200 void MathMatrixInset::label(row_type row, string const & label)
206 void MathMatrixInset::numbered(row_type row, bool num)
212 bool MathMatrixInset::numbered(row_type row) const
218 bool MathMatrixInset::ams() const
223 objtype_ == LM_OT_ALIGN ||
224 objtype_ == LM_OT_MULTLINE ||
225 objtype_ == LM_OT_GATHER ||
226 objtype_ == LM_OT_ALIGNAT ||
227 objtype_ == LM_OT_XALIGNAT ||
228 objtype_ == LM_OT_XXALIGNAT;
232 bool MathMatrixInset::display() const
234 return getType() != LM_OT_SIMPLE;
238 std::vector<string> const MathMatrixInset::getLabelList() const
240 std::vector<string> res;
241 for (row_type row = 0; row < nrows(); ++row)
242 if (!label_[row].empty() && nonum_[row] != 1)
243 res.push_back(label_[row]);
248 bool MathMatrixInset::numberedType() const
250 if (getType() == LM_OT_SIMPLE || getType() == LM_OT_XXALIGNAT)
252 for (row_type row = 0; row < nrows(); ++row)
259 void MathMatrixInset::validate(LaTeXFeatures & features) const
261 features.amsstyle = ams();
263 // Validation is necessary only if not using AMS math.
264 // To be safe, we will always run mathedvalidate.
265 //if (features.amsstyle)
268 features.boldsymbol = true;
269 //features.binom = true;
271 MathNestInset::validate(features);
275 void MathMatrixInset::header_write(std::ostream & os) const
277 bool n = numberedType();
288 os << "\\begin{equation" << star(n) << "}\n";
294 os << "\\begin{eqnarray" << star(n) << "}\n";
298 os << "\\begin{align" << star(n) << "}\n";
302 os << "\\begin{alignat" << star(n) << "}" << "{" << ncols()/2 << "}\n";
306 os << "\\begin{xalignat" << star(n) << "}" << "{" << ncols()/2 << "}\n";
309 case LM_OT_XXALIGNAT:
310 os << "\\begin{xxalignat}" << "{" << ncols()/2 << "}\n";
314 os << "\\begin{multline}\n";
318 os << "\\begin{gather}\n";
322 os << "\\begin{unknown" << star(n) << "}";
327 void MathMatrixInset::footer_write(std::ostream & os) const
329 bool n = numberedType();
338 os << "\\end{equation" << star(n) << "}\n";
344 os << "\n\\end{eqnarray" << star(n) << "}\n";
348 os << "\n\\end{align" << star(n) << "}\n";
352 os << "\n\\end{alignat" << star(n) << "}\n";
356 os << "\n\\end{xalignat" << star(n) << "}\n";
359 case LM_OT_XXALIGNAT:
360 os << "\n\\end{xxalignat}\n";
364 os << "\n\\end{multline}\n";
368 os << "\n\\end{gather}\n";
372 os << "\\end{unknown" << star(n) << "}";
377 void MathMatrixInset::addRow(row_type row)
379 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
380 label_.insert(label_.begin() + row + 1, string());
381 MathGridInset::addRow(row);
385 void MathMatrixInset::appendRow()
387 nonum_.push_back(!numberedType());
388 label_.push_back(string());
389 MathGridInset::appendRow();
393 void MathMatrixInset::delRow(row_type row)
395 MathGridInset::delRow(row);
396 nonum_.erase(nonum_.begin() + row);
397 label_.erase(label_.begin() + row);
401 void MathMatrixInset::addCol(col_type col)
405 mutate(LM_OT_EQNARRAY);
414 mutate(LM_OT_ALIGNAT);
420 case LM_OT_XXALIGNAT:
421 MathGridInset::addCol(col);
422 MathGridInset::addCol(col + 1);
431 void MathMatrixInset::delCol(col_type col)
436 case LM_OT_XXALIGNAT:
437 MathGridInset::delCol(col + 1);
438 MathGridInset::delCol(col);
446 string MathMatrixInset::nicelabel(row_type row) const
450 if (label_[row].empty())
451 return string("(#)");
452 return "(" + label_[row] + ")";
456 void MathMatrixInset::mutate(string const & newtype)
458 if (newtype == "dump") {
462 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
463 mutate(typecode(newtype));
467 void MathMatrixInset::glueall()
470 for (idx_type i = 0; i < nargs(); ++i)
471 ar.push_back(cell(i));
472 *this = MathMatrixInset(LM_OT_SIMPLE);
477 MathInsetTypes MathMatrixInset::getType() const
483 void MathMatrixInset::setType(MathInsetTypes t)
491 void MathMatrixInset::mutate(MathInsetTypes newtype)
493 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
495 if (newtype == getType())
500 setType(LM_OT_EQUATION);
508 setType(LM_OT_SIMPLE);
514 case LM_OT_XXALIGNAT: {
516 MathGridInset::addCol(1);
519 pos_type pos = firstRelOp(cell(0));
521 cell(0).erase(pos, cell(0).size());
522 cell(1).erase(0, pos);
523 setType(LM_OT_ALIGN);
530 MathGridInset::addCol(1);
531 MathGridInset::addCol(1);
533 // split it "nicely" on the firest relop
534 pos_type pos = firstRelOp(cell(0));
536 cell(0).erase(pos, cell(0).size());
537 cell(1).erase(0, pos);
539 if (cell(1).size()) {
541 cell(1).erase(1, cell(1).size());
545 setType(LM_OT_EQNARRAY);
554 case LM_OT_EQUATION: {
555 // set correct (no)numbering
556 bool allnonum = true;
557 for (row_type row = 0; row < nrows(); ++row) {
562 // set first non-empty label
564 for (row_type row = 0; row < nrows(); ++row) {
565 if (!label_[row].empty()) {
573 nonum_[0] = allnonum;
582 case LM_OT_XXALIGNAT:
584 for (row_type row = 0; row < nrows(); ++row) {
585 idx_type c = 3 * row + 1;
586 cell(c).push_back(cell(c + 1));
588 MathGridInset::delCol(2);
589 setType(LM_OT_ALIGN);
601 MathGridInset::addCol(1);
602 setType(LM_OT_EQNARRAY);
608 case LM_OT_XXALIGNAT:
613 lyxerr << "mutation from '" << getType()
614 << "' to '" << newtype << "' not implemented\n";
622 setType(LM_OT_GATHER);
625 lyxerr << "mutation from '" << getType()
626 << "' to '" << newtype << "' not implemented\n";
633 setType(LM_OT_MULTLINE);
636 lyxerr << "mutation from '" << getType()
637 << "' to '" << newtype << "' not implemented\n";
642 lyxerr << "mutation from '" << getType()
643 << "' to '" << newtype << "' not implemented\n";
648 void MathMatrixInset::write(MathWriteInfo & os) const
652 bool n = numberedType();
654 for (row_type row = 0; row < nrows(); ++row) {
655 for (col_type col = 0; col < ncols(); ++col)
656 os << cell(index(row, col)) << eocString(col).c_str();
658 if (!label_[row].empty())
659 os << "\\label{" << label_[row].c_str() << "}";
663 os << eolString(row).c_str();
670 void MathMatrixInset::writeNormal(NormalStream & os) const
672 os << "[formula " << normalName(getType()).c_str() << " ";
673 MathGridInset::writeNormal(os);