7 #include "math_matrixinset.h"
9 #include "support/LOstream.h"
11 #include "LaTeXFeatures.h"
14 LyXFont WhichFont(short type, int size);
18 string const getAlign(short int type, int cols)
23 for (int i = 0; i < cols; ++i)
28 for (int i = 0; i < cols; ++i)
44 string const star(bool n)
50 int getCols(short int type)
69 // returns position of first relation operator in the array
70 // used for "intelligent splitting"
71 int firstRelOp(MathArray const & array)
73 for (int pos = 0; pos < array.size(); array.next(pos))
74 if (!array.isInset(pos) &&
75 MathIsRelOp(array.GetChar(pos), array.GetCode(pos)))
82 MathMatrixInset::MathMatrixInset(MathInsetTypes t)
83 : MathGridInset(getCols(t), 1, "formula", t), nonum_(1), label_(1)
87 MathMatrixInset::MathMatrixInset()
88 : MathGridInset(1, 1, "formula", LM_OT_SIMPLE), nonum_(1), label_(1)
91 MathInset * MathMatrixInset::clone() const
93 return new MathMatrixInset(*this);
97 void MathMatrixInset::Metrics(MathStyles /* st */, int, int)
99 size_ = (GetType() == LM_OT_SIMPLE) ? LM_ST_TEXT : LM_ST_DISPLAY;
101 //LyXFont wfont = WhichFont(LM_TC_BF, size());
102 //wfont.setLatex(LyXFont::OFF);
104 // let the cells adjust themselves
105 MathGridInset::Metrics(size_);
112 if (numberedType()) {
114 for (int row = 0; row < nrows(); ++row)
115 l = std::max(l, mathed_string_width(LM_TC_TEXTRM, size(), nicelabel(row)));
123 void MathMatrixInset::draw(Painter & pain, int x, int y)
128 MathGridInset::draw(pain, x, y);
130 if (numberedType()) {
131 LyXFont wfont = WhichFont(LM_TC_BF, size());
133 wfont.setLatex(LyXFont::OFF);
135 int xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
136 for (int row = 0; row < nrows(); ++row)
137 pain.text(xx, y + rowinfo_[row].offset_, nicelabel(row), wfont);
142 void MathMatrixInset::Write(std::ostream & os, bool fragile) const
146 bool n = numberedType();
148 for (int row = 0; row < nrows(); ++row) {
151 for (int col = 0; col < ncols(); ++col) {
154 cell(index(row, col)).Write(os, fragile);
157 if (!label_[row].empty())
158 os << "\\label{" << label_[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 MathInset::Validate(features);
240 void MathMatrixInset::header_write(std::ostream & os) const
242 bool n = numberedType();
251 os << "\\begin{equation" << star(n) << "}\n";
257 os << "\\begin{eqnarray" << star(n) << "}\n";
261 os << "\\begin{align" << star(n) << "}";
265 os << "\\begin{alignat" << star(n) << "}"
266 << "{" << ncols()/2 << "}\n";
269 os << "\\begin{unknown" << star(n) << "}";
274 void MathMatrixInset::footer_write(std::ostream & os) const
276 bool n = numberedType();
285 os << "\\end{equation" << star(n) << "}\n";
291 os << "\\end{eqnarray" << star(n) << "}\n";
295 os << "\\end{align" << star(n) << "}\n";
299 os << "\\end{alignat" << star(n) << "}\n";
303 os << "\\end{unknown" << star(n) << "}";
308 void MathMatrixInset::addRow(int row)
310 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
311 label_.insert(label_.begin() + row + 1, string());
312 MathGridInset::addRow(row);
315 void MathMatrixInset::appendRow()
317 nonum_.push_back(!numberedType());
318 label_.push_back(string());
319 MathGridInset::appendRow();
323 void MathMatrixInset::delRow(int row)
325 MathGridInset::delRow(row);
326 nonum_.erase(nonum_.begin() + row);
327 label_.erase(label_.begin() + row);
330 void MathMatrixInset::addCol(int col)
334 mutate(LM_OT_EQNARRAY);
344 MathGridInset::addCol(col);
346 MathGridInset::addCol(col);
355 void MathMatrixInset::delCol(int col)
359 MathGridInset::delCol(col);
368 string MathMatrixInset::nicelabel(int row) const
372 if (label_[row].empty())
373 return string("(#)");
374 return "(" + label_[row] + ")";
379 short typecode(string const & s)
382 return LM_OT_EQUATION;
384 return LM_OT_EQUATION;
386 return LM_OT_EQNARRAY;
392 return LM_OT_XXALIGN;
394 return LM_OT_MULTLINE;
399 void MathMatrixInset::mutate(string const & newtype)
401 if (newtype == "dump") {
405 //lyxerr << "mutating from '" << GetType() << "' to '" << newtype << "'\n";
406 mutate(typecode(newtype));
409 void MathMatrixInset::glueall()
412 for (int i = 0; i < nargs(); ++i)
413 ar.push_back(cell(i));
414 *this = MathMatrixInset(LM_OT_SIMPLE);
418 void MathMatrixInset::mutate(short newtype)
420 //lyxerr << "mutating from '" << GetType() << "' to '" << newtype << "'\n";
422 if (newtype == GetType())
427 SetType(LM_OT_EQUATION);
435 SetType(LM_OT_SIMPLE);
439 MathGridInset::addCol(1);
442 int pos = firstRelOp(cell(0));
444 cell(0).erase(pos, cell(0).size());
445 cell(1).erase(0, pos);
448 SetType(LM_OT_ALIGN);
454 MathGridInset::addCol(1);
455 MathGridInset::addCol(1);
457 // split it "nicely" on the firest relop
458 int pos1 = firstRelOp(cell(0));
460 cell(0).erase(pos1, cell(0).size());
461 cell(1).erase(0, pos1);
465 cell(1).erase(pos2, cell(1).size());
466 cell(2).erase(0, pos2);
469 SetType(LM_OT_EQNARRAY);
478 case LM_OT_EQUATION: {
479 // set correct (no)numbering
480 bool allnonum = true;
481 for (int r = 0; r < nrows(); ++r) {
486 // set first non-empty label
488 for (int r = 0; r < nrows(); ++r) {
489 if (!label_[r].empty()) {
497 nonum_[0] = allnonum;
505 for (int row = 0; row < nrows(); ++row) {
507 cell(c).push_back(cell(c + 1));
509 MathGridInset::delCol(2);
510 SetType(LM_OT_ALIGN);
523 MathGridInset::addCol(1);
524 SetType(LM_OT_EQNARRAY);
530 lyxerr << "mutation from '" << GetType()
531 << "' to '" << newtype << "' not implemented\n";
537 lyxerr << "mutation from '" << GetType()
538 << "' to '" << newtype << "' not implemented\n";