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
248 objtype_ == LM_OT_ALIGN ||
249 objtype_ == LM_OT_MULTLINE ||
250 objtype_ == LM_OT_GATHER ||
251 objtype_ == LM_OT_ALIGNAT ||
252 objtype_ == LM_OT_XALIGNAT ||
253 objtype_ == LM_OT_XXALIGNAT;
257 bool MathMatrixInset::display() const
259 return getType() != LM_OT_SIMPLE;
263 std::vector<string> const MathMatrixInset::getLabelList() const
265 std::vector<string> res;
266 for (row_type row = 0; row < nrows(); ++row)
267 if (!label_[row].empty() && nonum_[row] != 1)
268 res.push_back(label_[row]);
273 bool MathMatrixInset::numberedType() const
275 if (getType() == LM_OT_SIMPLE || getType() == LM_OT_XXALIGNAT)
277 for (row_type row = 0; row < nrows(); ++row)
284 void MathMatrixInset::validate(LaTeXFeatures & features) const
286 features.amsstyle = ams();
288 // Validation is necessary only if not using AMS math.
289 // To be safe, we will always run mathedvalidate.
290 //if (features.amsstyle)
293 features.boldsymbol = true;
294 //features.binom = true;
296 MathNestInset::validate(features);
300 void MathMatrixInset::header_write(std::ostream & os) const
302 bool n = numberedType();
313 os << "\\begin{equation" << star(n) << "}\n";
319 os << "\\begin{eqnarray" << star(n) << "}\n";
323 os << "\\begin{align" << star(n) << "}\n";
327 os << "\\begin{alignat" << star(n) << "}" << "{" << ncols()/2 << "}\n";
331 os << "\\begin{xalignat" << star(n) << "}" << "{" << ncols()/2 << "}\n";
334 case LM_OT_XXALIGNAT:
335 os << "\\begin{xxalignat}" << "{" << ncols()/2 << "}\n";
339 os << "\\begin{multline}\n";
343 os << "\\begin{gather}\n";
347 os << "\\begin{unknown" << star(n) << "}";
352 void MathMatrixInset::footer_write(std::ostream & os) const
354 bool n = numberedType();
363 os << "\\end{equation" << star(n) << "}\n";
369 os << "\n\\end{eqnarray" << star(n) << "}\n";
373 os << "\n\\end{align" << star(n) << "}\n";
377 os << "\n\\end{alignat" << star(n) << "}\n";
381 os << "\n\\end{xalignat" << star(n) << "}\n";
384 case LM_OT_XXALIGNAT:
385 os << "\n\\end{xxalignat}\n";
389 os << "\n\\end{multline}\n";
393 os << "\n\\end{gather}\n";
397 os << "\\end{unknown" << star(n) << "}";
402 void MathMatrixInset::addRow(row_type row)
404 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
405 label_.insert(label_.begin() + row + 1, string());
406 MathGridInset::addRow(row);
410 void MathMatrixInset::appendRow()
412 nonum_.push_back(!numberedType());
413 label_.push_back(string());
414 MathGridInset::appendRow();
418 void MathMatrixInset::delRow(row_type row)
420 MathGridInset::delRow(row);
421 nonum_.erase(nonum_.begin() + row);
422 label_.erase(label_.begin() + row);
426 void MathMatrixInset::addCol(col_type col)
430 mutate(LM_OT_EQNARRAY);
439 mutate(LM_OT_ALIGNAT);
445 case LM_OT_XXALIGNAT:
446 MathGridInset::addCol(col);
447 MathGridInset::addCol(col + 1);
456 void MathMatrixInset::delCol(col_type col)
461 case LM_OT_XXALIGNAT:
462 MathGridInset::delCol(col + 1);
463 MathGridInset::delCol(col);
471 string MathMatrixInset::nicelabel(row_type row) const
475 if (label_[row].empty())
476 return string("(#)");
477 return "(" + label_[row] + ")";
481 void MathMatrixInset::mutate(string const & newtype)
483 if (newtype == "dump") {
487 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
488 mutate(typecode(newtype));
492 void MathMatrixInset::glueall()
495 for (idx_type i = 0; i < nargs(); ++i)
496 ar.push_back(cell(i));
497 *this = MathMatrixInset(LM_OT_SIMPLE);
502 MathInsetTypes MathMatrixInset::getType() const
508 void MathMatrixInset::setType(MathInsetTypes t)
516 void MathMatrixInset::mutate(MathInsetTypes newtype)
518 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
520 if (newtype == getType())
525 setType(LM_OT_EQUATION);
533 setType(LM_OT_SIMPLE);
539 case LM_OT_XXALIGNAT: {
541 MathGridInset::addCol(1);
544 pos_type pos = firstRelOp(cell(0));
546 cell(0).erase(pos, cell(0).size());
547 cell(1).erase(0, pos);
548 setType(LM_OT_ALIGN);
555 MathGridInset::addCol(1);
556 MathGridInset::addCol(1);
558 // split it "nicely" on the firest relop
559 pos_type pos = firstRelOp(cell(0));
561 cell(0).erase(pos, cell(0).size());
562 cell(1).erase(0, pos);
564 if (cell(1).size()) {
566 cell(1).erase(1, cell(1).size());
570 setType(LM_OT_EQNARRAY);
579 case LM_OT_EQUATION: {
580 // set correct (no)numbering
581 bool allnonum = true;
582 for (row_type row = 0; row < nrows(); ++row) {
587 // set first non-empty label
589 for (row_type row = 0; row < nrows(); ++row) {
590 if (!label_[row].empty()) {
598 nonum_[0] = allnonum;
607 case LM_OT_XXALIGNAT:
609 for (row_type row = 0; row < nrows(); ++row) {
610 idx_type c = 3 * row + 1;
611 cell(c).push_back(cell(c + 1));
613 MathGridInset::delCol(2);
614 setType(LM_OT_ALIGN);
626 MathGridInset::addCol(1);
627 setType(LM_OT_EQNARRAY);
633 case LM_OT_XXALIGNAT:
638 lyxerr << "mutation from '" << getType()
639 << "' to '" << newtype << "' not implemented\n";
647 setType(LM_OT_GATHER);
650 lyxerr << "mutation from '" << getType()
651 << "' to '" << newtype << "' not implemented\n";
658 setType(LM_OT_MULTLINE);
661 lyxerr << "mutation from '" << getType()
662 << "' to '" << newtype << "' not implemented\n";
667 lyxerr << "mutation from '" << getType()
668 << "' to '" << newtype << "' not implemented\n";