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);
218 void MathMatrixInset::mathmlize(MathMLStream & os) const
220 MathGridInset::mathmlize(os);
224 string MathMatrixInset::label(row_type row) const
230 void MathMatrixInset::label(row_type row, string const & label)
236 void MathMatrixInset::numbered(row_type row, bool num)
242 bool MathMatrixInset::numbered(row_type row) const
248 bool MathMatrixInset::ams() const
253 objtype_ == LM_OT_ALIGN ||
254 objtype_ == LM_OT_MULTLINE ||
255 objtype_ == LM_OT_GATHER ||
256 objtype_ == LM_OT_ALIGNAT ||
257 objtype_ == LM_OT_XALIGNAT ||
258 objtype_ == LM_OT_XXALIGNAT;
262 bool MathMatrixInset::display() const
264 return getType() != LM_OT_SIMPLE;
268 std::vector<string> const MathMatrixInset::getLabelList() const
270 std::vector<string> res;
271 for (row_type row = 0; row < nrows(); ++row)
272 if (!label_[row].empty() && nonum_[row] != 1)
273 res.push_back(label_[row]);
278 bool MathMatrixInset::numberedType() const
280 if (getType() == LM_OT_SIMPLE || getType() == LM_OT_XXALIGNAT)
282 for (row_type row = 0; row < nrows(); ++row)
289 void MathMatrixInset::validate(LaTeXFeatures & features) const
291 features.amsstyle = ams();
293 // Validation is necessary only if not using AMS math.
294 // To be safe, we will always run mathedvalidate.
295 //if (features.amsstyle)
298 features.boldsymbol = true;
299 //features.binom = true;
301 MathNestInset::validate(features);
305 void MathMatrixInset::header_write(std::ostream & os) const
307 bool n = numberedType();
318 os << "\\begin{equation" << star(n) << "}\n";
324 os << "\\begin{eqnarray" << star(n) << "}\n";
328 os << "\\begin{align" << star(n) << "}\n";
332 os << "\\begin{alignat" << star(n) << "}" << "{" << ncols()/2 << "}\n";
336 os << "\\begin{xalignat" << star(n) << "}" << "{" << ncols()/2 << "}\n";
339 case LM_OT_XXALIGNAT:
340 os << "\\begin{xxalignat}" << "{" << ncols()/2 << "}\n";
344 os << "\\begin{multline}\n";
348 os << "\\begin{gather}\n";
352 os << "\\begin{unknown" << star(n) << "}";
357 void MathMatrixInset::footer_write(std::ostream & os) const
359 bool n = numberedType();
368 os << "\\end{equation" << star(n) << "}\n";
374 os << "\n\\end{eqnarray" << star(n) << "}\n";
378 os << "\n\\end{align" << star(n) << "}\n";
382 os << "\n\\end{alignat" << star(n) << "}\n";
386 os << "\n\\end{xalignat" << star(n) << "}\n";
389 case LM_OT_XXALIGNAT:
390 os << "\n\\end{xxalignat}\n";
394 os << "\n\\end{multline}\n";
398 os << "\n\\end{gather}\n";
402 os << "\\end{unknown" << star(n) << "}";
407 void MathMatrixInset::addRow(row_type row)
409 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
410 label_.insert(label_.begin() + row + 1, string());
411 MathGridInset::addRow(row);
415 void MathMatrixInset::appendRow()
417 nonum_.push_back(!numberedType());
418 label_.push_back(string());
419 MathGridInset::appendRow();
423 void MathMatrixInset::delRow(row_type row)
425 MathGridInset::delRow(row);
426 nonum_.erase(nonum_.begin() + row);
427 label_.erase(label_.begin() + row);
431 void MathMatrixInset::addCol(col_type col)
435 mutate(LM_OT_EQNARRAY);
444 mutate(LM_OT_ALIGNAT);
450 case LM_OT_XXALIGNAT:
451 MathGridInset::addCol(col);
452 MathGridInset::addCol(col + 1);
461 void MathMatrixInset::delCol(col_type col)
466 case LM_OT_XXALIGNAT:
467 MathGridInset::delCol(col + 1);
468 MathGridInset::delCol(col);
476 string MathMatrixInset::nicelabel(row_type row) const
480 if (label_[row].empty())
481 return string("(#)");
482 return "(" + label_[row] + ")";
486 void MathMatrixInset::mutate(string const & newtype)
488 if (newtype == "dump") {
492 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
493 mutate(typecode(newtype));
497 void MathMatrixInset::glueall()
500 for (idx_type i = 0; i < nargs(); ++i)
501 ar.push_back(cell(i));
502 *this = MathMatrixInset(LM_OT_SIMPLE);
507 MathInsetTypes MathMatrixInset::getType() const
513 void MathMatrixInset::setType(MathInsetTypes t)
521 void MathMatrixInset::mutate(MathInsetTypes newtype)
523 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
525 if (newtype == getType())
530 setType(LM_OT_EQUATION);
538 setType(LM_OT_SIMPLE);
544 case LM_OT_XXALIGNAT: {
546 MathGridInset::addCol(1);
549 pos_type pos = firstRelOp(cell(0));
551 cell(0).erase(pos, cell(0).size());
552 cell(1).erase(0, pos);
553 setType(LM_OT_ALIGN);
560 MathGridInset::addCol(1);
561 MathGridInset::addCol(1);
563 // split it "nicely" on the firest relop
564 pos_type pos = firstRelOp(cell(0));
566 cell(0).erase(pos, cell(0).size());
567 cell(1).erase(0, pos);
569 if (cell(1).size()) {
571 cell(1).erase(1, cell(1).size());
575 setType(LM_OT_EQNARRAY);
584 case LM_OT_EQUATION: {
585 // set correct (no)numbering
586 bool allnonum = true;
587 for (row_type row = 0; row < nrows(); ++row) {
592 // set first non-empty label
594 for (row_type row = 0; row < nrows(); ++row) {
595 if (!label_[row].empty()) {
603 nonum_[0] = allnonum;
612 case LM_OT_XXALIGNAT:
614 for (row_type row = 0; row < nrows(); ++row) {
615 idx_type c = 3 * row + 1;
616 cell(c).push_back(cell(c + 1));
618 MathGridInset::delCol(2);
619 setType(LM_OT_ALIGN);
631 MathGridInset::addCol(1);
632 setType(LM_OT_EQNARRAY);
638 case LM_OT_XXALIGNAT:
643 lyxerr << "mutation from '" << getType()
644 << "' to '" << newtype << "' not implemented\n";
652 setType(LM_OT_GATHER);
655 lyxerr << "mutation from '" << getType()
656 << "' to '" << newtype << "' not implemented\n";
663 setType(LM_OT_MULTLINE);
666 lyxerr << "mutation from '" << getType()
667 << "' to '" << newtype << "' not implemented\n";
672 lyxerr << "mutation from '" << getType()
673 << "' to '" << newtype << "' not implemented\n";