7 #include "math_matrixinset.h"
10 #include "support/LOstream.h"
12 #include "LaTeXFeatures.h"
17 string getAlign(MathInsetTypes type, int cols)
22 for (int i = 0; i < cols; ++i)
27 for (int i = 0; i < cols; ++i)
43 string const star(bool n)
49 int getCols(short int type)
68 // returns position of first relation operator in the array
69 // used for "intelligent splitting"
70 int firstRelOp(MathArray const & array)
72 for (MathArray::const_iterator it = array.begin(); it != array.end(); ++it)
74 return it - array.begin();
80 MathMatrixInset::MathMatrixInset(MathInsetTypes t)
81 : MathGridInset(getCols(t), 1), objtype_(t), nonum_(1), label_(1)
83 halign(getAlign(t, ncols()));
87 MathMatrixInset::MathMatrixInset()
88 : MathGridInset(1, 1), objtype_(LM_OT_SIMPLE), nonum_(1), label_(1)
91 MathInset * MathMatrixInset::clone() const
93 return new MathMatrixInset(*this);
97 void MathMatrixInset::metrics(MathStyles) const
99 size_ = (getType() == LM_OT_SIMPLE) ? LM_ST_TEXT : LM_ST_DISPLAY;
101 // let the cells adjust themselves
102 MathGridInset::metrics(size_);
109 if (numberedType()) {
111 for (int row = 0; row < nrows(); ++row)
112 l = std::max(l, mathed_string_width(LM_TC_BF, size(), nicelabel(row)));
118 // make it at least as high as the current font
121 math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, des);
122 ascent_ = std::max(ascent_, asc);
123 descent_ = std::max(descent_, des);
127 void MathMatrixInset::draw(Painter & pain, int x, int y) const
132 MathGridInset::draw(pain, x, y);
134 if (numberedType()) {
135 int xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
136 for (int row = 0; row < nrows(); ++row) {
137 int yy = y + rowinfo_[row].offset_;
138 drawStr(pain, LM_TC_BF, size(), xx, yy, nicelabel(row));
144 void MathMatrixInset::write(std::ostream & os, bool fragile) const
148 bool n = numberedType();
150 for (int row = 0; row < nrows(); ++row) {
151 for (int col = 0; col < ncols(); ++col) {
152 cell(index(row, col)).write(os, fragile);
153 os << eocString(col);
156 if (!label_[row].empty())
157 os << "\\label{" << label_[row] << "}";
161 os << eolString(row);
168 string MathMatrixInset::label(int row) const
173 void MathMatrixInset::label(int row, string const & label)
179 void MathMatrixInset::numbered(int row, bool num)
185 bool MathMatrixInset::numbered(int row) const
191 bool MathMatrixInset::ams() const
197 bool MathMatrixInset::display() const
199 return getType() != LM_OT_SIMPLE;
203 std::vector<string> const MathMatrixInset::getLabelList() const
205 std::vector<string> res;
206 for (int row = 0; row < nrows(); ++row)
207 if (!label_[row].empty() && nonum_[row] != 1)
208 res.push_back(label_[row]);
213 bool MathMatrixInset::numberedType() const
215 if (getType() == LM_OT_SIMPLE)
217 for (int row = 0; row < nrows(); ++row)
224 void MathMatrixInset::validate(LaTeXFeatures & features) const
226 features.amsstyle = ams();
228 // Validation is necessary only if not using AMS math.
229 // To be safe, we will always run mathedvalidate.
230 //if (features.amsstyle)
233 features.boldsymbol = true;
234 //features.binom = true;
236 MathNestInset::validate(features);
240 void MathMatrixInset::header_write(std::ostream & os) const
242 bool n = numberedType();
253 os << "\\begin{equation" << star(n) << "}\n";
259 os << "\\begin{eqnarray" << star(n) << "}\n";
263 os << "\\begin{align" << star(n) << "}";
267 os << "\\begin{alignat" << star(n) << "}"
268 << "{" << ncols()/2 << "}\n";
271 os << "\\begin{unknown" << star(n) << "}";
276 void MathMatrixInset::footer_write(std::ostream & os) const
278 bool n = numberedType();
287 os << "\\end{equation" << star(n) << "}\n";
293 os << "\\end{eqnarray" << star(n) << "}\n";
297 os << "\\end{align" << star(n) << "}\n";
301 os << "\\end{alignat" << star(n) << "}\n";
305 os << "\\end{unknown" << star(n) << "}";
310 void MathMatrixInset::addRow(int row)
312 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
313 label_.insert(label_.begin() + row + 1, string());
314 MathGridInset::addRow(row);
317 void MathMatrixInset::appendRow()
319 nonum_.push_back(!numberedType());
320 label_.push_back(string());
321 MathGridInset::appendRow();
325 void MathMatrixInset::delRow(int row)
327 MathGridInset::delRow(row);
328 nonum_.erase(nonum_.begin() + row);
329 label_.erase(label_.begin() + row);
332 void MathMatrixInset::addCol(int col)
336 mutate(LM_OT_EQNARRAY);
346 MathGridInset::addCol(col);
348 MathGridInset::addCol(col);
357 void MathMatrixInset::delCol(int col)
361 MathGridInset::delCol(col);
370 string MathMatrixInset::nicelabel(int row) const
374 if (label_[row].empty())
375 return string("(#)");
376 return "(" + label_[row] + ")";
381 short typecode(string const & s)
384 return LM_OT_EQUATION;
386 return LM_OT_EQUATION;
388 return LM_OT_EQNARRAY;
394 return LM_OT_XXALIGN;
396 return LM_OT_MULTLINE;
401 void MathMatrixInset::mutate(string const & newtype)
403 if (newtype == "dump") {
407 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
408 mutate(typecode(newtype));
411 void MathMatrixInset::glueall()
414 for (int i = 0; i < nargs(); ++i)
415 ar.push_back(cell(i));
416 *this = MathMatrixInset(LM_OT_SIMPLE);
421 MathInsetTypes MathMatrixInset::getType() const
427 void MathMatrixInset::setType(MathInsetTypes t)
434 void MathMatrixInset::mutate(short newtype)
436 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
438 if (newtype == getType())
443 setType(LM_OT_EQUATION);
451 setType(LM_OT_SIMPLE);
455 MathGridInset::addCol(1);
458 int pos = firstRelOp(cell(0));
460 cell(0).erase(pos, cell(0).size());
461 cell(1).erase(0, pos);
464 setType(LM_OT_ALIGN);
470 MathGridInset::addCol(1);
471 MathGridInset::addCol(1);
473 // split it "nicely" on the firest relop
474 int pos = firstRelOp(cell(0));
476 cell(0).erase(pos, cell(0).size());
477 cell(1).erase(0, pos);
479 if (cell(1).size()) {
481 cell(1).erase(1, cell(1).size());
486 setType(LM_OT_EQNARRAY);
495 case LM_OT_EQUATION: {
496 // set correct (no)numbering
497 bool allnonum = true;
498 for (int r = 0; r < nrows(); ++r) {
503 // set first non-empty label
505 for (int r = 0; r < nrows(); ++r) {
506 if (!label_[r].empty()) {
514 nonum_[0] = allnonum;
522 for (int row = 0; row < nrows(); ++row) {
524 cell(c).push_back(cell(c + 1));
526 MathGridInset::delCol(2);
527 setType(LM_OT_ALIGN);
540 MathGridInset::addCol(1);
541 setType(LM_OT_EQNARRAY);
547 lyxerr << "mutation from '" << getType()
548 << "' to '" << newtype << "' not implemented\n";
554 lyxerr << "mutation from '" << getType()
555 << "' to '" << newtype << "' not implemented\n";