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)
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{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{unknown" << star(n) << "}";
350 void MathMatrixInset::addRow(int row)
352 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
353 label_.insert(label_.begin() + row + 1, string());
354 MathGridInset::addRow(row);
358 void MathMatrixInset::appendRow()
360 nonum_.push_back(!numberedType());
361 label_.push_back(string());
362 MathGridInset::appendRow();
366 void MathMatrixInset::delRow(int row)
368 MathGridInset::delRow(row);
369 nonum_.erase(nonum_.begin() + row);
370 label_.erase(label_.begin() + row);
374 void MathMatrixInset::addCol(int col)
378 mutate(LM_OT_EQNARRAY);
387 mutate(LM_OT_ALIGNAT);
393 case LM_OT_XXALIGNAT:
394 MathGridInset::addCol(col);
395 MathGridInset::addCol(col + 1);
404 void MathMatrixInset::delCol(int col)
409 case LM_OT_XXALIGNAT:
410 MathGridInset::delCol(col + 1);
411 MathGridInset::delCol(col);
419 string MathMatrixInset::nicelabel(int row) const
423 if (label_[row].empty())
424 return string("(#)");
425 return "(" + label_[row] + ")";
430 MathInsetTypes typecode(string const & s)
433 return LM_OT_EQUATION;
435 return LM_OT_EQUATION;
437 return LM_OT_EQNARRAY;
443 return LM_OT_XALIGNAT;
444 if (s == "xxalignat")
445 return LM_OT_XXALIGNAT;
447 return LM_OT_MULTLINE;
452 void MathMatrixInset::mutate(string const & newtype)
454 if (newtype == "dump") {
458 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
459 mutate(typecode(newtype));
462 void MathMatrixInset::glueall()
465 for (int i = 0; i < nargs(); ++i)
466 ar.push_back(cell(i));
467 *this = MathMatrixInset(LM_OT_SIMPLE);
472 MathInsetTypes MathMatrixInset::getType() const
478 void MathMatrixInset::setType(MathInsetTypes t)
486 void MathMatrixInset::mutate(MathInsetTypes newtype)
488 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
490 if (newtype == getType())
495 setType(LM_OT_EQUATION);
503 setType(LM_OT_SIMPLE);
509 case LM_OT_XXALIGNAT: {
511 MathGridInset::addCol(1);
514 int pos = firstRelOp(cell(0));
516 cell(0).erase(pos, cell(0).size());
517 cell(1).erase(0, pos);
518 setType(LM_OT_ALIGN);
525 MathGridInset::addCol(1);
526 MathGridInset::addCol(1);
528 // split it "nicely" on the firest relop
529 int pos = firstRelOp(cell(0));
531 cell(0).erase(pos, cell(0).size());
532 cell(1).erase(0, pos);
534 if (cell(1).size()) {
536 cell(1).erase(1, cell(1).size());
540 setType(LM_OT_EQNARRAY);
549 case LM_OT_EQUATION: {
550 // set correct (no)numbering
551 bool allnonum = true;
552 for (int r = 0; r < nrows(); ++r) {
557 // set first non-empty label
559 for (int r = 0; r < nrows(); ++r) {
560 if (!label_[r].empty()) {
568 nonum_[0] = allnonum;
577 case LM_OT_XXALIGNAT:
579 for (int row = 0; row < nrows(); ++row) {
581 cell(c).push_back(cell(c + 1));
583 MathGridInset::delCol(2);
584 setType(LM_OT_ALIGN);
596 MathGridInset::addCol(1);
597 setType(LM_OT_EQNARRAY);
603 case LM_OT_XXALIGNAT:
608 lyxerr << "mutation from '" << getType()
609 << "' to '" << newtype << "' not implemented\n";
615 lyxerr << "mutation from '" << getType()
616 << "' to '" << newtype << "' not implemented\n";