10 #include "math_matrixinset.h"
12 #include "support/LOstream.h"
14 #include "LaTeXFeatures.h"
17 LyXFont WhichFont(short type, int size);
21 string const getAlign(short int type, int cols)
26 for (int i = 0; i < cols; ++i)
31 for (int i = 0; i < cols; ++i)
47 string const star(bool n)
53 int getCols(short int type)
74 MathMatrixInset::MathMatrixInset(MathInsetTypes t)
75 : MathGridInset(getCols(t), 1, "formula", t), nonum_(1), label_(1)
79 MathMatrixInset::MathMatrixInset()
80 : MathGridInset(1, 1, "formula", LM_OT_SIMPLE), nonum_(1), label_(1)
83 MathInset * MathMatrixInset::clone() const
85 return new MathMatrixInset(*this);
89 void MathMatrixInset::Metrics(MathStyles st)
92 //LyXFont wfont = WhichFont(LM_TC_BF, size());
93 //wfont.setLatex(LyXFont::OFF);
95 // let the cells adjust themselves
96 MathGridInset::Metrics(st);
103 if (numberedType()) {
105 for (int row = 0; row < nrows(); ++row)
106 l = std::max(l, mathed_string_width(LM_TC_TEXTRM, size(), nicelabel(row)));
114 void MathMatrixInset::draw(Painter & pain, int x, int y)
119 MathGridInset::draw(pain, x, y);
121 if (numberedType()) {
122 LyXFont wfont = WhichFont(LM_TC_BF, size());
124 wfont.setLatex(LyXFont::OFF);
126 int xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
127 for (int row = 0; row < nrows(); ++row)
128 pain.text(xx, y + rowinfo_[row].offset_, nicelabel(row), wfont);
133 void MathMatrixInset::Write(std::ostream & os, bool fragile) const
137 bool n = numberedType();
139 for (int row = 0; row < nrows(); ++row) {
142 for (int col = 0; col < ncols(); ++col) {
145 cell(index(row, col)).Write(os, fragile);
148 if (!label_[row].empty())
149 os << "\\label{" << label_[row] << "}";
159 string MathMatrixInset::label(int row) const
164 void MathMatrixInset::label(int row, string const & label)
170 void MathMatrixInset::numbered(int row, bool num)
176 bool MathMatrixInset::numbered(int row) const
182 bool MathMatrixInset::ams() const
188 bool MathMatrixInset::display() const
190 return GetType() != LM_OT_SIMPLE;
194 std::vector<string> const MathMatrixInset::getLabelList() const
196 std::vector<string> res;
197 for (int row = 0; row < nrows(); ++row)
198 if (!label_[row].empty() && nonum_[row] != 1)
199 res.push_back(label_[row]);
204 bool MathMatrixInset::numberedType() const
206 if (GetType() == LM_OT_SIMPLE)
208 for (int row = 0; row < nrows(); ++row)
215 void MathMatrixInset::Validate(LaTeXFeatures & features)
217 features.amsstyle = ams();
219 // Validation is necessary only if not using AMS math.
220 // To be safe, we will always run mathedValidate.
221 //if (features.amsstyle)
224 //Validate1(features);
226 features.boldsymbol = true;
227 features.binom = true;
231 void MathMatrixInset::Validate1(LaTeXFeatures & features)
235 while (it.OK() && !(features.binom && features.boldsymbol)) {
236 MathInset * p = it.GetInset();
238 p = it.GetActiveInset();
240 if (!features.binom && p->GetType() == LM_OT_MACRO &&
241 p->name() == "binom") {
242 features.binom = true;
244 for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
245 p->setArgumentIdx(i);
246 Validate1(features, p);
250 if (!features.boldsymbol && p->name() == "boldsymbol")
251 features.boldsymbol = true;
260 void MathMatrixInset::header_write(std::ostream & os) const
262 bool n = numberedType();
271 os << "\\begin{equation" << star(n) << "}\n";
277 os << "\\begin{eqnarray" << star(n) << "}\n";
281 os << "\\begin{align" << star(n) << "}";
285 os << "\\begin{alignat" << star(n) << "}"
286 << "{" << ncols()/2 << "}\n";
292 void MathMatrixInset::footer_write(std::ostream & os) const
294 bool n = numberedType();
303 os << "\\end{equation" << star(n) << "}\n";
309 os << "\\end{eqnarray" << star(n) << "}\n";
313 os << "\\end{align" << star(n) << "}\n";
317 os << "\\end{alignat" << star(n) << "}\n";
323 void MathMatrixInset::addRow(int row)
325 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
326 label_.insert(label_.begin() + row + 1, string());
327 MathGridInset::addRow(row);
330 void MathMatrixInset::appendRow()
332 nonum_.push_back(!numberedType());
333 label_.push_back(string());
334 MathGridInset::appendRow();
338 void MathMatrixInset::delRow(int row)
340 MathGridInset::delRow(row);
341 nonum_.erase(nonum_.begin() + row);
342 label_.erase(label_.begin() + row);
345 void MathMatrixInset::addCol(int col)
349 mutate(LM_OT_EQNARRAY);
359 MathGridInset::addCol(col);
361 MathGridInset::addCol(col);
370 void MathMatrixInset::delCol(int col)
374 MathGridInset::delCol(col);
382 void MathMatrixInset::breakLine()
384 if (GetType() == LM_OT_SIMPLE || GetType() == LM_OT_EQUATION)
385 mutate(LM_OT_EQNARRAY);
390 void MathMatrixInset::splitCell(int idx)
392 if (idx == nargs() - 1) {
393 lyxerr << "can't split last cell\n";
397 lyxerr << "unimplemented\n";
401 string MathMatrixInset::nicelabel(int row) const
405 if (label_[row].empty())
406 return string("(#)");
407 return "(" + label_[row] + ")";
412 short typecode(string const & s)
415 return LM_OT_EQUATION;
417 return LM_OT_EQUATION;
419 return LM_OT_EQNARRAY;
425 return LM_OT_XXALIGN;
427 return LM_OT_MULTLINE;
432 void MathMatrixInset::mutate(string const & newtype)
434 if (newtype == "dump") {
438 //lyxerr << "mutating from '" << GetType() << "' to '" << newtype << "'\n";
439 mutate(typecode(newtype));
442 void MathMatrixInset::glueall()
445 for (int i = 0; i < nargs(); ++i)
446 ar.push_back(cell(i));
447 *this = MathMatrixInset(LM_OT_SIMPLE);
451 void MathMatrixInset::mutate(short newtype)
453 //lyxerr << "mutating from '" << GetType() << "' to '" << newtype << "'\n";
455 if (newtype == GetType())
460 SetType(LM_OT_EQUATION);
468 SetType(LM_OT_SIMPLE);
472 MathGridInset::addCol(1);
474 SetType(LM_OT_ALIGN);
478 MathGridInset::addCol(1);
479 MathGridInset::addCol(1);
481 SetType(LM_OT_EQNARRAY);
495 MathGridInset::delCol(2);
496 MathGridInset::delCol(1);
497 SetType(LM_OT_EQUATION);
500 lyxerr << "need to delete rows first\n";
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);
522 MathGridInset::addCol(1);
523 SetType(LM_OT_EQNARRAY);
529 lyxerr << "mutation from '" << GetType()
530 << "' to '" << newtype << "' not implemented\n";
536 lyxerr << "mutation from '" << GetType()
537 << "' to '" << newtype << "' not implemented\n";