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)
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 //Validate1(features);
235 features.boldsymbol = true;
236 features.binom = true;
240 void MathMatrixInset::Validate1(LaTeXFeatures & features)
244 while (it.OK() && !(features.binom && features.boldsymbol)) {
245 MathInset * p = it.nextInset();
247 p = it.GetActiveInset();
249 if (!features.binom && p->GetType() == LM_OT_MACRO &&
250 p->name() == "binom") {
251 features.binom = true;
253 for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
254 p->setArgumentIdx(i);
255 Validate1(features, p);
259 if (!features.boldsymbol && p->name() == "boldsymbol")
260 features.boldsymbol = true;
269 void MathMatrixInset::header_write(std::ostream & os) const
271 bool n = numberedType();
280 os << "\\begin{equation" << star(n) << "}\n";
286 os << "\\begin{eqnarray" << star(n) << "}\n";
290 os << "\\begin{align" << star(n) << "}";
294 os << "\\begin{alignat" << star(n) << "}"
295 << "{" << ncols()/2 << "}\n";
298 os << "\\begin{unknown" << star(n) << "}";
303 void MathMatrixInset::footer_write(std::ostream & os) const
305 bool n = numberedType();
314 os << "\\end{equation" << star(n) << "}\n";
320 os << "\\end{eqnarray" << star(n) << "}\n";
324 os << "\\end{align" << star(n) << "}\n";
328 os << "\\end{alignat" << star(n) << "}\n";
332 os << "\\end{unknown" << star(n) << "}";
337 void MathMatrixInset::addRow(int row)
339 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
340 label_.insert(label_.begin() + row + 1, string());
341 MathGridInset::addRow(row);
344 void MathMatrixInset::appendRow()
346 nonum_.push_back(!numberedType());
347 label_.push_back(string());
348 MathGridInset::appendRow();
352 void MathMatrixInset::delRow(int row)
354 MathGridInset::delRow(row);
355 nonum_.erase(nonum_.begin() + row);
356 label_.erase(label_.begin() + row);
359 void MathMatrixInset::addCol(int col)
363 mutate(LM_OT_EQNARRAY);
373 MathGridInset::addCol(col);
375 MathGridInset::addCol(col);
384 void MathMatrixInset::delCol(int col)
388 MathGridInset::delCol(col);
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);
471 int pos = firstRelOp(cell(0));
473 cell(0).erase(pos, cell(0).size());
474 cell(1).erase(0, pos);
477 SetType(LM_OT_ALIGN);
483 MathGridInset::addCol(1);
484 MathGridInset::addCol(1);
486 // split it "nicely" on the firest relop
487 int pos1 = firstRelOp(cell(0));
489 cell(0).erase(pos1, cell(0).size());
490 cell(1).erase(0, pos1);
494 cell(1).erase(pos2, cell(1).size());
495 cell(2).erase(0, pos2);
498 SetType(LM_OT_EQNARRAY);
507 case LM_OT_EQUATION: {
508 // set correct (no)numbering
509 bool allnonum = true;
510 for (int r = 0; r < nrows(); ++r) {
515 // set first non-empty label
517 for (int r = 0; r < nrows(); ++r) {
518 if (!label_[r].empty()) {
526 nonum_[0] = allnonum;
534 for (int row = 0; row < nrows(); ++row) {
536 cell(c).push_back(cell(c + 1));
538 MathGridInset::delCol(2);
539 SetType(LM_OT_ALIGN);
552 MathGridInset::addCol(1);
553 SetType(LM_OT_EQNARRAY);
559 lyxerr << "mutation from '" << GetType()
560 << "' to '" << newtype << "' not implemented\n";
566 lyxerr << "mutation from '" << GetType()
567 << "' to '" << newtype << "' not implemented\n";