7 #include "math_matrixinset.h"
10 #include "support/LOstream.h"
12 #include "LaTeXFeatures.h"
18 int getCols(MathInsetTypes type)
34 // returns position of first relation operator in the array
35 // used for "intelligent splitting"
36 int firstRelOp(MathArray const & array)
38 for (MathArray::const_iterator it = array.begin(); it != array.end(); ++it)
40 return it - array.begin();
45 char const * star(bool numbered)
47 return numbered ? "" : "*";
53 MathMatrixInset::MathMatrixInset()
54 : MathGridInset(1, 1), objtype_(LM_OT_SIMPLE), nonum_(1), label_(1)
60 MathMatrixInset::MathMatrixInset(MathInsetTypes t)
61 : MathGridInset(getCols(t), 1), objtype_(t), nonum_(1), label_(1)
67 MathMatrixInset::MathMatrixInset(MathInsetTypes t, int cols)
68 : MathGridInset(cols, 1), objtype_(t), nonum_(1), label_(1)
74 MathInset * MathMatrixInset::clone() const
76 return new MathMatrixInset(*this);
80 char MathMatrixInset::defaultColAlign(int col)
95 int MathMatrixInset::defaultColSpace(int col)
102 return (col & 1) ? 20 : 0;
103 case LM_OT_XXALIGNAT:
104 return (col & 1) ? 40 : 0;
111 void MathMatrixInset::setDefaults()
113 for (int col = 0; col < ncols(); ++col) {
114 colinfo_[col].align_ = defaultColAlign(col);
115 colinfo_[col].skip_ = defaultColSpace(col);
120 void MathMatrixInset::metrics(MathStyles) const
122 size_ = (getType() == LM_OT_SIMPLE) ? LM_ST_TEXT : LM_ST_DISPLAY;
124 // let the cells adjust themselves
125 MathGridInset::metrics(size_);
132 if (numberedType()) {
134 for (int row = 0; row < nrows(); ++row)
135 l = std::max(l, mathed_string_width(LM_TC_BF, size(), nicelabel(row)));
141 // make it at least as high as the current font
144 math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, des);
145 ascent_ = std::max(ascent_, asc);
146 descent_ = std::max(descent_, des);
150 void MathMatrixInset::draw(Painter & pain, int x, int y) const
155 MathGridInset::draw(pain, x, y);
157 if (numberedType()) {
158 int xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
159 for (int row = 0; row < nrows(); ++row) {
160 int yy = y + rowinfo_[row].offset_;
161 drawStr(pain, LM_TC_BF, size(), xx, yy, nicelabel(row));
167 void MathMatrixInset::write(std::ostream & os, bool fragile) const
171 bool n = numberedType();
173 for (int row = 0; row < nrows(); ++row) {
174 for (int col = 0; col < ncols(); ++col) {
175 cell(index(row, col)).write(os, fragile);
176 os << eocString(col);
179 if (!label_[row].empty())
180 os << "\\label{" << label_[row] << "}";
184 os << eolString(row);
191 string MathMatrixInset::label(int row) const
197 void MathMatrixInset::label(int row, string const & label)
203 void MathMatrixInset::numbered(int row, bool num)
209 bool MathMatrixInset::numbered(int row) const
215 bool MathMatrixInset::ams() const
221 bool MathMatrixInset::display() const
223 return getType() != LM_OT_SIMPLE;
227 std::vector<string> const MathMatrixInset::getLabelList() const
229 std::vector<string> res;
230 for (int row = 0; row < nrows(); ++row)
231 if (!label_[row].empty() && nonum_[row] != 1)
232 res.push_back(label_[row]);
237 bool MathMatrixInset::numberedType() const
239 if (getType() == LM_OT_SIMPLE || getType() == LM_OT_XXALIGNAT)
241 for (int row = 0; row < nrows(); ++row)
248 void MathMatrixInset::validate(LaTeXFeatures & features) const
250 features.amsstyle = ams();
252 // Validation is necessary only if not using AMS math.
253 // To be safe, we will always run mathedvalidate.
254 //if (features.amsstyle)
257 features.boldsymbol = true;
258 //features.binom = true;
260 MathNestInset::validate(features);
264 void MathMatrixInset::header_write(std::ostream & os) const
266 bool n = numberedType();
277 os << "\\begin{equation" << star(n) << "}\n";
283 os << "\\begin{eqnarray" << star(n) << "}\n";
287 os << "\\begin{align" << star(n) << "}";
291 os << "\\begin{alignat" << star(n) << "}" << "{" << ncols()/2 << "}\n";
295 os << "\\begin{xalignat" << star(n) << "}" << "{" << ncols()/2 << "}\n";
298 case LM_OT_XXALIGNAT:
299 os << "\\begin{xxalignat}" << "{" << ncols()/2 << "}\n";
303 os << "\\begin{multline}\n";
307 os << "\\begin{gather}\n";
311 os << "\\begin{unknown" << star(n) << "}";
316 void MathMatrixInset::footer_write(std::ostream & os) const
318 bool n = numberedType();
327 os << "\\end{equation" << star(n) << "}\n";
333 os << "\\end{eqnarray" << star(n) << "}\n";
337 os << "\\end{align" << star(n) << "}\n";
341 os << "\\end{alignat" << star(n) << "}\n";
345 os << "\\end{xalignat" << star(n) << "}\n";
348 case LM_OT_XXALIGNAT:
349 os << "\\end{xxalignat}\n";
353 os << "\\end{multline}\n";
357 os << "\\end{gather}\n";
361 os << "\\end{unknown" << star(n) << "}";
366 void MathMatrixInset::addRow(int row)
368 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
369 label_.insert(label_.begin() + row + 1, string());
370 MathGridInset::addRow(row);
374 void MathMatrixInset::appendRow()
376 nonum_.push_back(!numberedType());
377 label_.push_back(string());
378 MathGridInset::appendRow();
382 void MathMatrixInset::delRow(int row)
384 MathGridInset::delRow(row);
385 nonum_.erase(nonum_.begin() + row);
386 label_.erase(label_.begin() + row);
390 void MathMatrixInset::addCol(int col)
394 mutate(LM_OT_EQNARRAY);
403 mutate(LM_OT_ALIGNAT);
409 case LM_OT_XXALIGNAT:
410 MathGridInset::addCol(col);
411 MathGridInset::addCol(col + 1);
420 void MathMatrixInset::delCol(int col)
425 case LM_OT_XXALIGNAT:
426 MathGridInset::delCol(col + 1);
427 MathGridInset::delCol(col);
435 string MathMatrixInset::nicelabel(int row) const
439 if (label_[row].empty())
440 return string("(#)");
441 return "(" + label_[row] + ")";
446 MathInsetTypes typecode(string const & s)
449 return LM_OT_EQUATION;
451 return LM_OT_EQUATION;
453 return LM_OT_EQNARRAY;
459 return LM_OT_XALIGNAT;
460 if (s == "xxalignat")
461 return LM_OT_XXALIGNAT;
463 return LM_OT_MULTLINE;
470 void MathMatrixInset::mutate(string const & newtype)
472 if (newtype == "dump") {
476 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
477 mutate(typecode(newtype));
480 void MathMatrixInset::glueall()
483 for (int i = 0; i < nargs(); ++i)
484 ar.push_back(cell(i));
485 *this = MathMatrixInset(LM_OT_SIMPLE);
490 MathInsetTypes MathMatrixInset::getType() const
496 void MathMatrixInset::setType(MathInsetTypes t)
504 void MathMatrixInset::mutate(MathInsetTypes newtype)
506 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
508 if (newtype == getType())
513 setType(LM_OT_EQUATION);
521 setType(LM_OT_SIMPLE);
527 case LM_OT_XXALIGNAT: {
529 MathGridInset::addCol(1);
532 int pos = firstRelOp(cell(0));
534 cell(0).erase(pos, cell(0).size());
535 cell(1).erase(0, pos);
536 setType(LM_OT_ALIGN);
543 MathGridInset::addCol(1);
544 MathGridInset::addCol(1);
546 // split it "nicely" on the firest relop
547 int pos = firstRelOp(cell(0));
549 cell(0).erase(pos, cell(0).size());
550 cell(1).erase(0, pos);
552 if (cell(1).size()) {
554 cell(1).erase(1, cell(1).size());
558 setType(LM_OT_EQNARRAY);
567 case LM_OT_EQUATION: {
568 // set correct (no)numbering
569 bool allnonum = true;
570 for (int r = 0; r < nrows(); ++r) {
575 // set first non-empty label
577 for (int r = 0; r < nrows(); ++r) {
578 if (!label_[r].empty()) {
586 nonum_[0] = allnonum;
595 case LM_OT_XXALIGNAT:
597 for (int row = 0; row < nrows(); ++row) {
599 cell(c).push_back(cell(c + 1));
601 MathGridInset::delCol(2);
602 setType(LM_OT_ALIGN);
614 MathGridInset::addCol(1);
615 setType(LM_OT_EQNARRAY);
621 case LM_OT_XXALIGNAT:
626 lyxerr << "mutation from '" << getType()
627 << "' to '" << newtype << "' not implemented\n";
635 setType(LM_OT_GATHER);
638 lyxerr << "mutation from '" << getType()
639 << "' to '" << newtype << "' not implemented\n";
646 setType(LM_OT_MULTLINE);
649 lyxerr << "mutation from '" << getType()
650 << "' to '" << newtype << "' not implemented\n";
655 lyxerr << "mutation from '" << getType()
656 << "' to '" << newtype << "' not implemented\n";