7 #include "math_matrixinset.h"
9 #include "support/LOstream.h"
11 #include "LaTeXFeatures.h"
14 LyXFont WhichFont(short type, int size);
18 string getAlign(short int type, int cols)
23 for (int i = 0; i < cols; ++i)
28 for (int i = 0; i < cols; ++i)
49 int getCols(short int type)
70 MathMatrixInset::MathMatrixInset(MathInsetTypes t)
71 : MathGridInset(getCols(t), 1, "formula", t), nonum_(1), label_(1)
75 MathMatrixInset::MathMatrixInset()
76 : MathGridInset(1, 1, "formula", LM_OT_SIMPLE), nonum_(1), label_(1)
79 MathInset * MathMatrixInset::Clone() const
81 return new MathMatrixInset(*this);
85 void MathMatrixInset::Metrics(MathStyles st)
88 //LyXFont wfont = WhichFont(LM_TC_BF, size());
89 //wfont.setLatex(LyXFont::OFF);
91 // let the cells adjust themselves
92 MathGridInset::Metrics(st);
101 for (int row = 0; row < nrows(); ++row)
102 l = max(l, mathed_string_width(LM_TC_TEXTRM, size(), nicelabel(row)));
110 void MathMatrixInset::draw(Painter & pain, int x, int y)
115 MathGridInset::draw(pain, x, y);
117 if (numberedType()) {
118 LyXFont wfont = WhichFont(LM_TC_BF, size());
120 wfont.setLatex(LyXFont::OFF);
122 int xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
123 for (int row = 0; row < nrows(); ++row)
124 pain.text(xx, y + rowinfo_[row].offset_, nicelabel(row), wfont);
129 void MathMatrixInset::Write(std::ostream & os, bool fragile) const
133 bool n = numberedType();
135 for (int row = 0; row < nrows(); ++row) {
138 for (int col = 0; col < ncols(); ++col) {
141 cell(index(row, col)).Write(os, fragile);
144 if (!label_[row].empty())
145 os << "\\label{" << label_[row] << "}";
155 string MathMatrixInset::label(int row) const
160 void MathMatrixInset::label(int row, string const & label)
166 void MathMatrixInset::numbered(int row, bool num)
172 bool MathMatrixInset::numbered(int row) const
178 bool MathMatrixInset::ams() const
184 bool MathMatrixInset::display() const
186 return GetType() != LM_OT_SIMPLE;
190 vector<string> const MathMatrixInset::getLabelList() const
192 std::vector<string> res;
193 for (int row = 0; row < nrows(); ++row)
194 if (!label_[row].empty() && nonum_[row] != 1)
195 res.push_back(label_[row]);
200 bool MathMatrixInset::numberedType() const
202 if (GetType() == LM_OT_SIMPLE)
204 for (int row = 0; row < nrows(); ++row)
211 void MathMatrixInset::Validate(LaTeXFeatures & features)
213 features.amsstyle = ams();
215 // Validation is necessary only if not using AMS math.
216 // To be safe, we will always run mathedValidate.
217 //if (features.amsstyle)
220 //Validate1(features);
222 features.boldsymbol = true;
223 features.binom = true;
227 void MathMatrixInset::Validate1(LaTeXFeatures & features)
231 while (it.OK() && !(features.binom && features.boldsymbol)) {
232 MathInset * p = it.GetInset();
234 p = it.GetActiveInset();
236 if (!features.binom && p->GetType() == LM_OT_MACRO &&
237 p->name() == "binom") {
238 features.binom = true;
240 for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
241 p->setArgumentIdx(i);
242 Validate1(features, p);
246 if (!features.boldsymbol && p->name() == "boldsymbol")
247 features.boldsymbol = true;
256 void MathMatrixInset::header_write(ostream & os) const
258 bool n = numberedType();
267 os << "\\begin{equation" << star(n) << "}\n";
273 os << "\\begin{eqnarray" << star(n) << "}\n";
277 os << "\\begin{align" << star(n) << "}";
281 os << "\\begin{alignat" << star(n) << "}"
282 << "{" << ncols()/2 << "}\n";
288 void MathMatrixInset::footer_write(ostream & os) const
290 bool n = numberedType();
299 os << "\\end{equation" << star(n) << "}\n";
305 os << "\\end{eqnarray" << star(n) << "}\n";
309 os << "\\end{align" << star(n) << "}\n";
313 os << "\\end{alignat" << star(n) << "}\n";
319 void MathMatrixInset::addRow(int row)
321 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
322 label_.insert(label_.begin() + row + 1, string());
323 MathGridInset::addRow(row);
326 void MathMatrixInset::appendRow()
328 nonum_.push_back(!numberedType());
329 label_.push_back(string());
330 MathGridInset::appendRow();
334 void MathMatrixInset::delRow(int row)
336 MathGridInset::delRow(row);
337 nonum_.erase(nonum_.begin() + row);
338 label_.erase(label_.begin() + row);
341 void MathMatrixInset::addCol(int col)
345 mutate(LM_OT_EQNARRAY);
355 MathGridInset::addCol(col);
357 MathGridInset::addCol(col);
366 void MathMatrixInset::delCol(int col)
370 MathGridInset::delCol(col);
378 void MathMatrixInset::breakLine()
380 if (GetType() == LM_OT_SIMPLE || GetType() == LM_OT_EQUATION)
381 mutate(LM_OT_EQNARRAY);
386 void MathMatrixInset::splitCell(int idx)
388 if (idx == nargs() - 1) {
389 lyxerr << "can't split last cell\n";
393 lyxerr << "unimplemented\n";
397 string MathMatrixInset::nicelabel(int row) const
401 if (label_[row].empty())
402 return string("(#)");
403 return "(" + label_[row] + ")";
408 short typecode(string const & s)
411 return LM_OT_EQUATION;
413 return LM_OT_EQUATION;
415 return LM_OT_EQNARRAY;
421 return LM_OT_XXALIGN;
423 return LM_OT_MULTLINE;
428 void MathMatrixInset::mutate(string const & newtype)
430 if (newtype == "dump") {
434 //lyxerr << "mutating from '" << GetType() << "' to '" << newtype << "'\n";
435 mutate(typecode(newtype));
438 void MathMatrixInset::glueall()
441 for (int i = 0; i < nargs(); ++i)
442 ar.push_back(cell(i));
443 *this = MathMatrixInset(LM_OT_SIMPLE);
447 void MathMatrixInset::mutate(short newtype)
449 //lyxerr << "mutating from '" << GetType() << "' to '" << newtype << "'\n";
451 if (newtype == GetType())
456 SetType(LM_OT_EQUATION);
464 SetType(LM_OT_SIMPLE);
468 MathGridInset::addCol(1);
470 SetType(LM_OT_ALIGN);
474 MathGridInset::addCol(1);
475 MathGridInset::addCol(1);
477 SetType(LM_OT_EQNARRAY);
491 MathGridInset::delCol(2);
492 MathGridInset::delCol(1);
493 SetType(LM_OT_EQUATION);
496 lyxerr << "need to delete rows first\n";
501 for (int row = 0; row < nrows(); ++row) {
503 cell(c).push_back(cell(c + 1));
505 MathGridInset::delCol(2);
506 SetType(LM_OT_ALIGN);
518 MathGridInset::addCol(1);
519 SetType(LM_OT_EQNARRAY);
525 lyxerr << "mutation from '" << GetType()
526 << "' to '" << newtype << "' not implemented\n";
532 lyxerr << "mutation from '" << GetType()
533 << "' to '" << newtype << "' not implemented\n";