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)));
120 void MathMatrixInset::draw(Painter & pain, int x, int y) const
125 MathGridInset::draw(pain, x, y);
127 if (numberedType()) {
128 int xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
129 for (int row = 0; row < nrows(); ++row) {
130 int yy = y + rowinfo_[row].offset_;
131 drawStr(pain, LM_TC_BF, size(), xx, yy, nicelabel(row));
137 void MathMatrixInset::write(std::ostream & os, bool fragile) const
141 bool n = numberedType();
143 for (int row = 0; row < nrows(); ++row) {
144 for (int col = 0; col < ncols(); ++col) {
145 cell(index(row, col)).write(os, fragile);
146 os << eocString(col);
149 if (!label_[row].empty())
150 os << "\\label{" << label_[row] << "}";
154 os << eolString(row);
161 string MathMatrixInset::label(int row) const
166 void MathMatrixInset::label(int row, string const & label)
172 void MathMatrixInset::numbered(int row, bool num)
178 bool MathMatrixInset::numbered(int row) const
184 bool MathMatrixInset::ams() const
190 bool MathMatrixInset::display() const
192 return getType() != LM_OT_SIMPLE;
196 std::vector<string> const MathMatrixInset::getLabelList() const
198 std::vector<string> res;
199 for (int row = 0; row < nrows(); ++row)
200 if (!label_[row].empty() && nonum_[row] != 1)
201 res.push_back(label_[row]);
206 bool MathMatrixInset::numberedType() const
208 if (getType() == LM_OT_SIMPLE)
210 for (int row = 0; row < nrows(); ++row)
217 void MathMatrixInset::validate(LaTeXFeatures & features) const
219 features.amsstyle = ams();
221 // Validation is necessary only if not using AMS math.
222 // To be safe, we will always run mathedvalidate.
223 //if (features.amsstyle)
226 features.boldsymbol = true;
227 //features.binom = true;
229 MathNestInset::validate(features);
233 void MathMatrixInset::header_write(std::ostream & os) const
235 bool n = numberedType();
246 os << "\\begin{equation" << star(n) << "}\n";
252 os << "\\begin{eqnarray" << star(n) << "}\n";
256 os << "\\begin{align" << star(n) << "}";
260 os << "\\begin{alignat" << star(n) << "}"
261 << "{" << ncols()/2 << "}\n";
264 os << "\\begin{unknown" << star(n) << "}";
269 void MathMatrixInset::footer_write(std::ostream & os) const
271 bool n = numberedType();
280 os << "\\end{equation" << star(n) << "}\n";
286 os << "\\end{eqnarray" << star(n) << "}\n";
290 os << "\\end{align" << star(n) << "}\n";
294 os << "\\end{alignat" << star(n) << "}\n";
298 os << "\\end{unknown" << star(n) << "}";
303 void MathMatrixInset::addRow(int row)
305 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
306 label_.insert(label_.begin() + row + 1, string());
307 MathGridInset::addRow(row);
310 void MathMatrixInset::appendRow()
312 nonum_.push_back(!numberedType());
313 label_.push_back(string());
314 MathGridInset::appendRow();
318 void MathMatrixInset::delRow(int row)
320 MathGridInset::delRow(row);
321 nonum_.erase(nonum_.begin() + row);
322 label_.erase(label_.begin() + row);
325 void MathMatrixInset::addCol(int col)
329 mutate(LM_OT_EQNARRAY);
339 MathGridInset::addCol(col);
341 MathGridInset::addCol(col);
350 void MathMatrixInset::delCol(int col)
354 MathGridInset::delCol(col);
363 string MathMatrixInset::nicelabel(int row) const
367 if (label_[row].empty())
368 return string("(#)");
369 return "(" + label_[row] + ")";
374 short typecode(string const & s)
377 return LM_OT_EQUATION;
379 return LM_OT_EQUATION;
381 return LM_OT_EQNARRAY;
387 return LM_OT_XXALIGN;
389 return LM_OT_MULTLINE;
394 void MathMatrixInset::mutate(string const & newtype)
396 if (newtype == "dump") {
400 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
401 mutate(typecode(newtype));
404 void MathMatrixInset::glueall()
407 for (int i = 0; i < nargs(); ++i)
408 ar.push_back(cell(i));
409 *this = MathMatrixInset(LM_OT_SIMPLE);
414 MathInsetTypes MathMatrixInset::getType() const
420 void MathMatrixInset::setType(MathInsetTypes t)
427 void MathMatrixInset::mutate(short newtype)
429 //lyxerr << "mutating from '" << getType() << "' to '" << newtype << "'\n";
431 if (newtype == getType())
436 setType(LM_OT_EQUATION);
444 setType(LM_OT_SIMPLE);
448 MathGridInset::addCol(1);
451 int pos = firstRelOp(cell(0));
453 cell(0).erase(pos, cell(0).size());
454 cell(1).erase(0, pos);
457 setType(LM_OT_ALIGN);
463 MathGridInset::addCol(1);
464 MathGridInset::addCol(1);
466 // split it "nicely" on the firest relop
467 int pos = firstRelOp(cell(0));
469 cell(0).erase(pos, cell(0).size());
470 cell(1).erase(0, pos);
472 if (cell(1).size()) {
474 cell(1).erase(1, cell(1).size());
479 setType(LM_OT_EQNARRAY);
488 case LM_OT_EQUATION: {
489 // set correct (no)numbering
490 bool allnonum = true;
491 for (int r = 0; r < nrows(); ++r) {
496 // set first non-empty label
498 for (int r = 0; r < nrows(); ++r) {
499 if (!label_[r].empty()) {
507 nonum_[0] = allnonum;
515 for (int row = 0; row < nrows(); ++row) {
517 cell(c).push_back(cell(c + 1));
519 MathGridInset::delCol(2);
520 setType(LM_OT_ALIGN);
533 MathGridInset::addCol(1);
534 setType(LM_OT_EQNARRAY);
540 lyxerr << "mutation from '" << getType()
541 << "' to '" << newtype << "' not implemented\n";
547 lyxerr << "mutation from '" << getType()
548 << "' to '" << newtype << "' not implemented\n";