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)
39 if (it->nucleus()->isRelOp())
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)
96 int MathMatrixInset::defaultColSpace(int col)
103 return (col & 1) ? 20 : 0;
104 case LM_OT_XXALIGNAT:
105 return (col & 1) ? 40 : 0;
112 void MathMatrixInset::metrics(MathStyles) const
114 size_ = (getType() == LM_OT_SIMPLE) ? LM_ST_TEXT : LM_ST_DISPLAY;
116 // let the cells adjust themselves
117 MathGridInset::metrics(size_);
124 if (numberedType()) {
126 for (int row = 0; row < nrows(); ++row)
127 l = std::max(l, mathed_string_width(LM_TC_BF, size(), nicelabel(row)));
133 // make it at least as high as the current font
136 math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, des);
137 ascent_ = std::max(ascent_, asc);
138 descent_ = std::max(descent_, des);
142 void MathMatrixInset::draw(Painter & pain, int x, int y) const
147 MathGridInset::draw(pain, x, y);
149 if (numberedType()) {
150 int xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
151 for (int row = 0; row < nrows(); ++row) {
152 int yy = y + rowinfo_[row].offset_;
153 drawStr(pain, LM_TC_BF, size(), xx, yy, nicelabel(row));
159 void MathMatrixInset::write(std::ostream & os, bool fragile) const
163 bool n = numberedType();
165 for (int row = 0; row < nrows(); ++row) {
166 for (int col = 0; col < ncols(); ++col) {
167 cell(index(row, col)).write(os, fragile);
168 os << eocString(col);
171 if (!label_[row].empty())
172 os << "\\label{" << label_[row] << "}";
176 os << eolString(row);
183 string MathMatrixInset::label(int row) const
189 void MathMatrixInset::label(int row, string const & label)
195 void MathMatrixInset::numbered(int row, bool num)
201 bool MathMatrixInset::numbered(int row) const
207 bool MathMatrixInset::ams() const
213 bool MathMatrixInset::display() const
215 return getType() != LM_OT_SIMPLE;
219 std::vector<string> const MathMatrixInset::getLabelList() const
221 std::vector<string> res;
222 for (int row = 0; row < nrows(); ++row)
223 if (!label_[row].empty() && nonum_[row] != 1)
224 res.push_back(label_[row]);
229 bool MathMatrixInset::numberedType() const
231 if (getType() == LM_OT_SIMPLE || getType() == LM_OT_XXALIGNAT)
233 for (int row = 0; row < nrows(); ++row)
240 void MathMatrixInset::validate(LaTeXFeatures & features) const
242 features.amsstyle = ams();
244 // Validation is necessary only if not using AMS math.
245 // To be safe, we will always run mathedvalidate.
246 //if (features.amsstyle)
249 features.boldsymbol = true;
250 //features.binom = true;
252 MathNestInset::validate(features);
256 void MathMatrixInset::header_write(std::ostream & os) const
258 bool n = numberedType();
269 os << "\\begin{equation" << star(n) << "}\n";
275 os << "\\begin{eqnarray" << star(n) << "}\n";
279 os << "\\begin{align" << star(n) << "}";
283 os << "\\begin{alignat" << star(n) << "}" << "{" << ncols()/2 << "}\n";
287 os << "\\begin{xalignat" << star(n) << "}" << "{" << ncols()/2 << "}\n";
290 case LM_OT_XXALIGNAT:
291 os << "\\begin{xxalignat}" << "{" << ncols()/2 << "}\n";
295 os << "\\begin{multline}\n";
299 os << "\\begin{gather}\n";
303 os << "\\begin{unknown" << star(n) << "}";
308 void MathMatrixInset::footer_write(std::ostream & os) const
310 bool n = numberedType();
319 os << "\\end{equation" << star(n) << "}\n";
325 os << "\\end{eqnarray" << star(n) << "}\n";
329 os << "\\end{align" << star(n) << "}\n";
333 os << "\\end{alignat" << star(n) << "}\n";
337 os << "\\end{xalignat" << star(n) << "}\n";
340 case LM_OT_XXALIGNAT:
341 os << "\\end{xxalignat}\n";
345 os << "\\end{multline}\n";
349 os << "\\end{gather}\n";
353 os << "\\end{unknown" << star(n) << "}";
358 void MathMatrixInset::addRow(int row)
360 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
361 label_.insert(label_.begin() + row + 1, string());
362 MathGridInset::addRow(row);
366 void MathMatrixInset::appendRow()
368 nonum_.push_back(!numberedType());
369 label_.push_back(string());
370 MathGridInset::appendRow();
374 void MathMatrixInset::delRow(int row)
376 MathGridInset::delRow(row);
377 nonum_.erase(nonum_.begin() + row);
378 label_.erase(label_.begin() + row);
382 void MathMatrixInset::addCol(int col)
386 mutate(LM_OT_EQNARRAY);
395 mutate(LM_OT_ALIGNAT);
401 case LM_OT_XXALIGNAT:
402 MathGridInset::addCol(col);
403 MathGridInset::addCol(col + 1);
412 void MathMatrixInset::delCol(int col)
417 case LM_OT_XXALIGNAT:
418 MathGridInset::delCol(col + 1);
419 MathGridInset::delCol(col);
427 string MathMatrixInset::nicelabel(int row) const
431 if (label_[row].empty())
432 return string("(#)");
433 return "(" + label_[row] + ")";
438 MathInsetTypes typecode(string const & s)
441 return LM_OT_EQUATION;
443 return LM_OT_EQUATION;
445 return LM_OT_EQNARRAY;
451 return LM_OT_XALIGNAT;
452 if (s == "xxalignat")
453 return LM_OT_XXALIGNAT;
455 return LM_OT_MULTLINE;
462 void MathMatrixInset::mutate(string const & newtype)
464 if (newtype == "dump") {
468 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
469 mutate(typecode(newtype));
472 void MathMatrixInset::glueall()
475 for (int i = 0; i < nargs(); ++i)
476 ar.push_back(cell(i));
477 *this = MathMatrixInset(LM_OT_SIMPLE);
482 MathInsetTypes MathMatrixInset::getType() const
488 void MathMatrixInset::setType(MathInsetTypes t)
496 void MathMatrixInset::mutate(MathInsetTypes newtype)
498 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
500 if (newtype == getType())
505 setType(LM_OT_EQUATION);
513 setType(LM_OT_SIMPLE);
519 case LM_OT_XXALIGNAT: {
521 MathGridInset::addCol(1);
524 int pos = firstRelOp(cell(0));
526 cell(0).erase(pos, cell(0).size());
527 cell(1).erase(0, pos);
528 setType(LM_OT_ALIGN);
535 MathGridInset::addCol(1);
536 MathGridInset::addCol(1);
538 // split it "nicely" on the firest relop
539 int pos = firstRelOp(cell(0));
541 cell(0).erase(pos, cell(0).size());
542 cell(1).erase(0, pos);
544 if (cell(1).size()) {
546 cell(1).erase(1, cell(1).size());
550 setType(LM_OT_EQNARRAY);
559 case LM_OT_EQUATION: {
560 // set correct (no)numbering
561 bool allnonum = true;
562 for (int r = 0; r < nrows(); ++r) {
567 // set first non-empty label
569 for (int r = 0; r < nrows(); ++r) {
570 if (!label_[r].empty()) {
578 nonum_[0] = allnonum;
587 case LM_OT_XXALIGNAT:
589 for (int row = 0; row < nrows(); ++row) {
591 cell(c).push_back(cell(c + 1));
593 MathGridInset::delCol(2);
594 setType(LM_OT_ALIGN);
606 MathGridInset::addCol(1);
607 setType(LM_OT_EQNARRAY);
613 case LM_OT_XXALIGNAT:
618 lyxerr << "mutation from '" << getType()
619 << "' to '" << newtype << "' not implemented\n";
627 setType(LM_OT_GATHER);
630 lyxerr << "mutation from '" << getType()
631 << "' to '" << newtype << "' not implemented\n";
638 setType(LM_OT_MULTLINE);
641 lyxerr << "mutation from '" << getType()
642 << "' to '" << newtype << "' not implemented\n";
647 lyxerr << "mutation from '" << getType()
648 << "' to '" << newtype << "' not implemented\n";