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)
72 // returns position of first relation operator in the array
73 // used for "intelligent splitting"
74 int firstRelOp(MathArray const & array)
76 for (int pos = 0; pos < array.size(); array.next(pos))
77 if (!array.isInset(pos) &&
78 MathIsRelOp(array.GetChar(pos), array.GetCode(pos)))
85 MathMatrixInset::MathMatrixInset(MathInsetTypes t)
86 : MathGridInset(getCols(t), 1, "formula", t), nonum_(1), label_(1)
90 MathMatrixInset::MathMatrixInset()
91 : MathGridInset(1, 1, "formula", LM_OT_SIMPLE), nonum_(1), label_(1)
94 MathInset * MathMatrixInset::clone() const
96 return new MathMatrixInset(*this);
100 void MathMatrixInset::Metrics(MathStyles /* st */)
102 size_ = (GetType() == LM_OT_SIMPLE) ? LM_ST_TEXT : LM_ST_DISPLAY;
104 //LyXFont wfont = WhichFont(LM_TC_BF, size());
105 //wfont.setLatex(LyXFont::OFF);
107 // let the cells adjust themselves
108 MathGridInset::Metrics(size_);
115 if (numberedType()) {
117 for (int row = 0; row < nrows(); ++row)
118 l = std::max(l, mathed_string_width(LM_TC_TEXTRM, size(), nicelabel(row)));
126 void MathMatrixInset::draw(Painter & pain, int x, int y)
131 MathGridInset::draw(pain, x, y);
133 if (numberedType()) {
134 LyXFont wfont = WhichFont(LM_TC_BF, size());
136 wfont.setLatex(LyXFont::OFF);
138 int xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
139 for (int row = 0; row < nrows(); ++row)
140 pain.text(xx, y + rowinfo_[row].offset_, nicelabel(row), wfont);
145 void MathMatrixInset::Write(std::ostream & os, bool fragile) const
149 bool n = numberedType();
151 for (int row = 0; row < nrows(); ++row) {
154 for (int col = 0; col < ncols(); ++col) {
157 cell(index(row, col)).Write(os, fragile);
160 if (!label_[row].empty())
161 os << "\\label{" << label_[row] << "}";
171 string MathMatrixInset::label(int row) const
176 void MathMatrixInset::label(int row, string const & label)
182 void MathMatrixInset::numbered(int row, bool num)
188 bool MathMatrixInset::numbered(int row) const
194 bool MathMatrixInset::ams() const
200 bool MathMatrixInset::display() const
202 return GetType() != LM_OT_SIMPLE;
206 std::vector<string> const MathMatrixInset::getLabelList() const
208 std::vector<string> res;
209 for (int row = 0; row < nrows(); ++row)
210 if (!label_[row].empty() && nonum_[row] != 1)
211 res.push_back(label_[row]);
216 bool MathMatrixInset::numberedType() const
218 if (GetType() == LM_OT_SIMPLE)
220 for (int row = 0; row < nrows(); ++row)
227 void MathMatrixInset::Validate(LaTeXFeatures & features)
229 features.amsstyle = ams();
231 // Validation is necessary only if not using AMS math.
232 // To be safe, we will always run mathedValidate.
233 //if (features.amsstyle)
236 //Validate1(features);
238 features.boldsymbol = true;
239 features.binom = true;
243 void MathMatrixInset::Validate1(LaTeXFeatures & features)
247 while (it.OK() && !(features.binom && features.boldsymbol)) {
248 MathInset * p = it.GetInset();
250 p = it.GetActiveInset();
252 if (!features.binom && p->GetType() == LM_OT_MACRO &&
253 p->name() == "binom") {
254 features.binom = true;
256 for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
257 p->setArgumentIdx(i);
258 Validate1(features, p);
262 if (!features.boldsymbol && p->name() == "boldsymbol")
263 features.boldsymbol = true;
272 void MathMatrixInset::header_write(std::ostream & os) const
274 bool n = numberedType();
283 os << "\\begin{equation" << star(n) << "}\n";
289 os << "\\begin{eqnarray" << star(n) << "}\n";
293 os << "\\begin{align" << star(n) << "}";
297 os << "\\begin{alignat" << star(n) << "}"
298 << "{" << ncols()/2 << "}\n";
301 os << "\\begin{unknown" << star(n) << "}";
306 void MathMatrixInset::footer_write(std::ostream & os) const
308 bool n = numberedType();
317 os << "\\end{equation" << star(n) << "}\n";
323 os << "\\end{eqnarray" << star(n) << "}\n";
327 os << "\\end{align" << star(n) << "}\n";
331 os << "\\end{alignat" << star(n) << "}\n";
335 os << "\\end{unknown" << star(n) << "}";
340 void MathMatrixInset::addRow(int row)
342 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
343 label_.insert(label_.begin() + row + 1, string());
344 MathGridInset::addRow(row);
347 void MathMatrixInset::appendRow()
349 nonum_.push_back(!numberedType());
350 label_.push_back(string());
351 MathGridInset::appendRow();
355 void MathMatrixInset::delRow(int row)
357 MathGridInset::delRow(row);
358 nonum_.erase(nonum_.begin() + row);
359 label_.erase(label_.begin() + row);
362 void MathMatrixInset::addCol(int col)
366 mutate(LM_OT_EQNARRAY);
376 MathGridInset::addCol(col);
378 MathGridInset::addCol(col);
387 void MathMatrixInset::delCol(int col)
391 MathGridInset::delCol(col);
400 string MathMatrixInset::nicelabel(int row) const
404 if (label_[row].empty())
405 return string("(#)");
406 return "(" + label_[row] + ")";
411 short typecode(string const & s)
414 return LM_OT_EQUATION;
416 return LM_OT_EQUATION;
418 return LM_OT_EQNARRAY;
424 return LM_OT_XXALIGN;
426 return LM_OT_MULTLINE;
431 void MathMatrixInset::mutate(string const & newtype)
433 if (newtype == "dump") {
437 //lyxerr << "mutating from '" << GetType() << "' to '" << newtype << "'\n";
438 mutate(typecode(newtype));
441 void MathMatrixInset::glueall()
444 for (int i = 0; i < nargs(); ++i)
445 ar.push_back(cell(i));
446 *this = MathMatrixInset(LM_OT_SIMPLE);
450 void MathMatrixInset::mutate(short newtype)
452 //lyxerr << "mutating from '" << GetType() << "' to '" << newtype << "'\n";
454 if (newtype == GetType())
459 SetType(LM_OT_EQUATION);
467 SetType(LM_OT_SIMPLE);
471 MathGridInset::addCol(1);
474 int pos = firstRelOp(cell(0));
476 cell(0).erase(pos, cell(0).size());
477 cell(1).erase(0, pos);
480 SetType(LM_OT_ALIGN);
486 MathGridInset::addCol(1);
487 MathGridInset::addCol(1);
489 // split it "nicely" on the firest relop
490 int pos1 = firstRelOp(cell(0));
492 cell(0).erase(pos1, cell(0).size());
493 cell(1).erase(0, pos1);
497 cell(1).erase(pos2, cell(1).size());
498 cell(2).erase(0, pos2);
501 SetType(LM_OT_EQNARRAY);
510 case LM_OT_EQUATION: {
511 // set correct (no)numbering
512 bool allnonum = true;
513 for (int r = 0; r < nrows(); ++r) {
517 nonum_[0] = allnonum;
519 // set first non-empty label
521 for (int r = 0; r < nrows(); ++r) {
522 if (!label_[r].empty()) {
536 for (int row = 0; row < nrows(); ++row) {
538 cell(c).push_back(cell(c + 1));
540 MathGridInset::delCol(2);
541 SetType(LM_OT_ALIGN);
554 MathGridInset::addCol(1);
555 SetType(LM_OT_EQNARRAY);
561 lyxerr << "mutation from '" << GetType()
562 << "' to '" << newtype << "' not implemented\n";
568 lyxerr << "mutation from '" << GetType()
569 << "' to '" << newtype << "' not implemented\n";