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, col_type 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(col_type col)
96 int MathMatrixInset::defaultColSpace(col_type 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 (row_type 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 (row_type 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 (row_type row = 0; row < nrows(); ++row) {
166 for (col_type 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(row_type row) const
189 void MathMatrixInset::label(row_type row, string const & label)
195 void MathMatrixInset::numbered(row_type row, bool num)
201 bool MathMatrixInset::numbered(row_type 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 (row_type 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 (row_type 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) << "}\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 << "\n\\end{eqnarray" << star(n) << "}\n";
329 os << "\n\\end{align" << star(n) << "}\n";
333 os << "\n\\end{alignat" << star(n) << "}\n";
337 os << "\n\\end{xalignat" << star(n) << "}\n";
340 case LM_OT_XXALIGNAT:
341 os << "\n\\end{xxalignat}\n";
345 os << "\n\\end{multline}\n";
349 os << "\n\\end{gather}\n";
353 os << "\\end{unknown" << star(n) << "}";
358 void MathMatrixInset::addRow(row_type 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(row_type row)
376 MathGridInset::delRow(row);
377 nonum_.erase(nonum_.begin() + row);
378 label_.erase(label_.begin() + row);
382 void MathMatrixInset::addCol(col_type 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(col_type col)
417 case LM_OT_XXALIGNAT:
418 MathGridInset::delCol(col + 1);
419 MathGridInset::delCol(col);
427 string MathMatrixInset::nicelabel(row_type 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;
463 void MathMatrixInset::mutate(string const & newtype)
465 if (newtype == "dump") {
469 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
470 mutate(typecode(newtype));
474 void MathMatrixInset::glueall()
477 for (idx_type i = 0; i < nargs(); ++i)
478 ar.push_back(cell(i));
479 *this = MathMatrixInset(LM_OT_SIMPLE);
484 MathInsetTypes MathMatrixInset::getType() const
490 void MathMatrixInset::setType(MathInsetTypes t)
498 void MathMatrixInset::mutate(MathInsetTypes newtype)
500 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
502 if (newtype == getType())
507 setType(LM_OT_EQUATION);
515 setType(LM_OT_SIMPLE);
521 case LM_OT_XXALIGNAT: {
523 MathGridInset::addCol(1);
526 pos_type pos = firstRelOp(cell(0));
528 cell(0).erase(pos, cell(0).size());
529 cell(1).erase(0, pos);
530 setType(LM_OT_ALIGN);
537 MathGridInset::addCol(1);
538 MathGridInset::addCol(1);
540 // split it "nicely" on the firest relop
541 pos_type pos = firstRelOp(cell(0));
543 cell(0).erase(pos, cell(0).size());
544 cell(1).erase(0, pos);
546 if (cell(1).size()) {
548 cell(1).erase(1, cell(1).size());
552 setType(LM_OT_EQNARRAY);
561 case LM_OT_EQUATION: {
562 // set correct (no)numbering
563 bool allnonum = true;
564 for (row_type row = 0; row < nrows(); ++row) {
569 // set first non-empty label
571 for (row_type row = 0; row < nrows(); ++row) {
572 if (!label_[row].empty()) {
580 nonum_[0] = allnonum;
589 case LM_OT_XXALIGNAT:
591 for (row_type row = 0; row < nrows(); ++row) {
592 idx_type c = 3 * row + 1;
593 cell(c).push_back(cell(c + 1));
595 MathGridInset::delCol(2);
596 setType(LM_OT_ALIGN);
608 MathGridInset::addCol(1);
609 setType(LM_OT_EQNARRAY);
615 case LM_OT_XXALIGNAT:
620 lyxerr << "mutation from '" << getType()
621 << "' to '" << newtype << "' not implemented\n";
629 setType(LM_OT_GATHER);
632 lyxerr << "mutation from '" << getType()
633 << "' to '" << newtype << "' not implemented\n";
640 setType(LM_OT_MULTLINE);
643 lyxerr << "mutation from '" << getType()
644 << "' to '" << newtype << "' not implemented\n";
649 lyxerr << "mutation from '" << getType()
650 << "' to '" << newtype << "' not implemented\n";