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 */)
91 size_ = (GetType() == LM_OT_SIMPLE) ? LM_ST_TEXT : LM_ST_DISPLAY;
93 //LyXFont wfont = WhichFont(LM_TC_BF, size());
94 //wfont.setLatex(LyXFont::OFF);
96 // let the cells adjust themselves
97 MathGridInset::Metrics(size_);
104 if (numberedType()) {
106 for (int row = 0; row < nrows(); ++row)
107 l = std::max(l, mathed_string_width(LM_TC_TEXTRM, size(), nicelabel(row)));
115 void MathMatrixInset::draw(Painter & pain, int x, int y)
120 MathGridInset::draw(pain, x, y);
122 if (numberedType()) {
123 LyXFont wfont = WhichFont(LM_TC_BF, size());
125 wfont.setLatex(LyXFont::OFF);
127 int xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
128 for (int row = 0; row < nrows(); ++row)
129 pain.text(xx, y + rowinfo_[row].offset_, nicelabel(row), wfont);
134 void MathMatrixInset::Write(std::ostream & os, bool fragile) const
138 bool n = numberedType();
140 for (int row = 0; row < nrows(); ++row) {
143 for (int col = 0; col < ncols(); ++col) {
146 cell(index(row, col)).Write(os, fragile);
149 if (!label_[row].empty())
150 os << "\\label{" << label_[row] << "}";
160 string MathMatrixInset::label(int row) const
165 void MathMatrixInset::label(int row, string const & label)
171 void MathMatrixInset::numbered(int row, bool num)
177 bool MathMatrixInset::numbered(int row) const
183 bool MathMatrixInset::ams() const
189 bool MathMatrixInset::display() const
191 return GetType() != LM_OT_SIMPLE;
195 std::vector<string> const MathMatrixInset::getLabelList() const
197 std::vector<string> res;
198 for (int row = 0; row < nrows(); ++row)
199 if (!label_[row].empty() && nonum_[row] != 1)
200 res.push_back(label_[row]);
205 bool MathMatrixInset::numberedType() const
207 if (GetType() == LM_OT_SIMPLE)
209 for (int row = 0; row < nrows(); ++row)
216 void MathMatrixInset::Validate(LaTeXFeatures & features)
218 features.amsstyle = ams();
220 // Validation is necessary only if not using AMS math.
221 // To be safe, we will always run mathedValidate.
222 //if (features.amsstyle)
225 //Validate1(features);
227 features.boldsymbol = true;
228 features.binom = true;
232 void MathMatrixInset::Validate1(LaTeXFeatures & features)
236 while (it.OK() && !(features.binom && features.boldsymbol)) {
237 MathInset * p = it.GetInset();
239 p = it.GetActiveInset();
241 if (!features.binom && p->GetType() == LM_OT_MACRO &&
242 p->name() == "binom") {
243 features.binom = true;
245 for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
246 p->setArgumentIdx(i);
247 Validate1(features, p);
251 if (!features.boldsymbol && p->name() == "boldsymbol")
252 features.boldsymbol = true;
261 void MathMatrixInset::header_write(std::ostream & os) const
263 bool n = numberedType();
272 os << "\\begin{equation" << star(n) << "}\n";
278 os << "\\begin{eqnarray" << star(n) << "}\n";
282 os << "\\begin{align" << star(n) << "}";
286 os << "\\begin{alignat" << star(n) << "}"
287 << "{" << ncols()/2 << "}\n";
290 os << "\\begin{unknown" << star(n) << "}";
295 void MathMatrixInset::footer_write(std::ostream & os) const
297 bool n = numberedType();
306 os << "\\end{equation" << star(n) << "}\n";
312 os << "\\end{eqnarray" << star(n) << "}\n";
316 os << "\\end{align" << star(n) << "}\n";
320 os << "\\end{alignat" << star(n) << "}\n";
324 os << "\\end{unknown" << star(n) << "}";
329 void MathMatrixInset::addRow(int row)
331 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
332 label_.insert(label_.begin() + row + 1, string());
333 MathGridInset::addRow(row);
336 void MathMatrixInset::appendRow()
338 nonum_.push_back(!numberedType());
339 label_.push_back(string());
340 MathGridInset::appendRow();
344 void MathMatrixInset::delRow(int row)
346 MathGridInset::delRow(row);
347 nonum_.erase(nonum_.begin() + row);
348 label_.erase(label_.begin() + row);
351 void MathMatrixInset::addCol(int col)
355 mutate(LM_OT_EQNARRAY);
365 MathGridInset::addCol(col);
367 MathGridInset::addCol(col);
376 void MathMatrixInset::delCol(int col)
380 MathGridInset::delCol(col);
389 string MathMatrixInset::nicelabel(int row) const
393 if (label_[row].empty())
394 return string("(#)");
395 return "(" + label_[row] + ")";
400 short typecode(string const & s)
403 return LM_OT_EQUATION;
405 return LM_OT_EQUATION;
407 return LM_OT_EQNARRAY;
413 return LM_OT_XXALIGN;
415 return LM_OT_MULTLINE;
420 void MathMatrixInset::mutate(string const & newtype)
422 if (newtype == "dump") {
426 //lyxerr << "mutating from '" << GetType() << "' to '" << newtype << "'\n";
427 mutate(typecode(newtype));
430 void MathMatrixInset::glueall()
433 for (int i = 0; i < nargs(); ++i)
434 ar.push_back(cell(i));
435 *this = MathMatrixInset(LM_OT_SIMPLE);
439 void MathMatrixInset::mutate(short newtype)
441 //lyxerr << "mutating from '" << GetType() << "' to '" << newtype << "'\n";
443 if (newtype == GetType())
448 SetType(LM_OT_EQUATION);
456 SetType(LM_OT_SIMPLE);
460 MathGridInset::addCol(1);
462 SetType(LM_OT_ALIGN);
466 MathGridInset::addCol(1);
467 MathGridInset::addCol(1);
469 SetType(LM_OT_EQNARRAY);
478 case LM_OT_EQUATION: {
479 bool allnonum = true;
480 for (int r = 0; r < nrows(); ++r) {
486 for (int r = 0; r < nrows(); ++r) {
487 if (!label_[r].empty()) {
496 nonum_[0] = allnonum;
502 for (int row = 0; row < nrows(); ++row) {
504 cell(c).push_back(cell(c + 1));
506 MathGridInset::delCol(2);
507 SetType(LM_OT_ALIGN);
519 MathGridInset::addCol(1);
520 SetType(LM_OT_EQNARRAY);
526 lyxerr << "mutation from '" << GetType()
527 << "' to '" << newtype << "' not implemented\n";
533 lyxerr << "mutation from '" << GetType()
534 << "' to '" << newtype << "' not implemented\n";