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 cxrow->setBaseline((cxrow == row_.begin()) ?
112 cxrow->ascent() + cprow->descent()
113 + MATH_ROWSEP + cprow->getBaseline());
114 h += cxrow->ascent() + cxrow->descent() + MATH_ROWSEP;
118 int const hl = descent();
121 // Compute vertical align
124 ascent = row_.begin()->getBaseline();
130 ascent = row_.begin().is_last() ? h / 2 : h - hl;
133 descent = h - ascent + 2;
135 // Increase ws_[i] for 'R' columns (except the first one)
136 for (int i = 1; i < nc_; ++i)
137 if (h_align_[i] == 'R')
138 ws_[i] += 10 * df_width;
139 // Increase ws_[i] for 'C' column
140 if (h_align_[0] == 'C')
141 if (ws_[0] < 7 * workwidth / 8)
142 ws_[0] = 7 * workwidth / 8;
148 void MathMatrixInset::draw(Painter & pain, int x, int y)
153 MathGridInset::draw(pain, x, y);
155 if (numberedType()) {
156 LyXFont wfont = WhichFont(LM_TC_BF, size());
157 wfont.setLatex(LyXFont::OFF);
158 int xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
159 for (int row = 0; row < nrows(); ++row)
160 pain.text(xx, y + rowinfo_[row].offset_, nicelabel(row), wfont);
165 void MathMatrixInset::Write(std::ostream & os, bool fragile) const
169 bool n = numberedType();
171 for (int row = 0; row < nrows(); ++row) {
174 for (int col = 0; col < ncols(); ++col) {
177 cell(index(row, col)).Write(os, fragile);
180 if (!label_[row].empty())
181 os << "\\label{" << label_[row] << "}";
191 string MathMatrixInset::label(int row) const
196 void MathMatrixInset::label(int row, string const & label)
202 void MathMatrixInset::numbered(int row, bool num)
208 bool MathMatrixInset::numbered(int row) const
214 bool MathMatrixInset::ams() const
220 bool MathMatrixInset::display() const
222 return GetType() != LM_OT_SIMPLE;
226 vector<string> const MathMatrixInset::getLabelList() const
228 std::vector<string> res;
229 for (int row = 0; row < nrows(); ++row)
230 if (!label_[row].empty() && nonum_[row] != 1)
231 res.push_back(label_[row]);
236 bool MathMatrixInset::numberedType() const
238 if (GetType() == LM_OT_SIMPLE)
240 for (int row = 0; row < nrows(); ++row)
247 void MathMatrixInset::Validate(LaTeXFeatures & features)
249 features.amsstyle = ams();
251 // Validation is necessary only if not using AMS math.
252 // To be safe, we will always run mathedValidate.
253 //if (features.amsstyle)
256 //Validate1(features);
258 features.boldsymbol = true;
259 features.binom = true;
263 void MathMatrixInset::Validate1(LaTeXFeatures & features)
267 while (it.OK() && !(features.binom && features.boldsymbol)) {
268 MathInset * p = it.GetInset();
270 p = it.GetActiveInset();
272 if (!features.binom && p->GetType() == LM_OT_MACRO &&
273 p->name() == "binom") {
274 features.binom = true;
276 for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
277 p->setArgumentIdx(i);
278 Validate1(features, p);
282 if (!features.boldsymbol && p->name() == "boldsymbol")
283 features.boldsymbol = true;
292 void MathMatrixInset::header_write(ostream & os) const
294 bool n = numberedType();
303 os << "\\begin{equation" << star(n) << "}\n";
309 os << "\\begin{eqnarray" << star(n) << "}\n";
313 os << "\\begin{align" << star(n) << "}";
317 os << "\\begin{alignat" << star(n) << "}"
318 << "{" << ncols()/2 << "}\n";
324 void MathMatrixInset::footer_write(ostream & os) const
326 bool n = numberedType();
335 os << "\\end{equation" << star(n) << "}\n";
341 os << "\\end{eqnarray" << star(n) << "}\n";
345 os << "\\end{align" << star(n) << "}\n";
349 os << "\\end{alignat" << star(n) << "}\n";
355 void MathMatrixInset::addRow(int row)
357 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
358 label_.insert(label_.begin() + row + 1, string());
359 MathGridInset::addRow(row);
362 void MathMatrixInset::appendRow()
364 nonum_.push_back(!numberedType());
365 label_.push_back(string());
366 MathGridInset::appendRow();
370 void MathMatrixInset::delRow(int row)
372 MathGridInset::delRow(row);
373 nonum_.erase(nonum_.begin() + row);
374 label_.erase(label_.begin() + row);
377 void MathMatrixInset::addCol(int col)
381 mutate(LM_OT_EQNARRAY);
391 MathGridInset::addCol(col);
393 MathGridInset::addCol(col);
402 void MathMatrixInset::delCol(int col)
406 MathGridInset::delCol(col);
414 void MathMatrixInset::breakLine()
416 if (GetType() == LM_OT_SIMPLE || GetType() == LM_OT_EQUATION)
417 mutate(LM_OT_EQNARRAY);
422 void MathMatrixInset::splitCell(int idx)
424 if (idx == nargs() - 1) {
425 lyxerr << "can't split last cell\n";
429 lyxerr << "unimplemented\n";
433 string MathMatrixInset::nicelabel(int row) const
437 if (label_[row].empty())
438 return string("(#)");
439 return "(" + label_[row] + ")";
444 short typecode(string const & s)
447 return LM_OT_EQUATION;
449 return LM_OT_EQUATION;
451 return LM_OT_EQNARRAY;
457 return LM_OT_XXALIGN;
459 return LM_OT_MULTLINE;
464 void MathMatrixInset::mutate(string const & newtype)
466 if (newtype == "dump") {
470 //lyxerr << "mutating from '" << GetType() << "' to '" << newtype << "'\n";
471 mutate(typecode(newtype));
474 void MathMatrixInset::glueall()
477 for (int i = 0; i < nargs(); ++i)
478 ar.push_back(cell(i));
479 *this = MathMatrixInset(LM_OT_SIMPLE);
483 void MathMatrixInset::mutate(short newtype)
485 //lyxerr << "mutating from '" << GetType() << "' to '" << newtype << "'\n";
487 if (newtype == GetType())
492 SetType(LM_OT_EQUATION);
500 SetType(LM_OT_SIMPLE);
504 MathGridInset::addCol(1);
506 SetType(LM_OT_ALIGN);
510 MathGridInset::addCol(1);
511 MathGridInset::addCol(1);
513 SetType(LM_OT_EQNARRAY);
527 MathGridInset::delCol(2);
528 MathGridInset::delCol(1);
529 SetType(LM_OT_EQUATION);
532 lyxerr << "need to delete rows first\n";
537 for (int row = 0; row < nrows(); ++row) {
539 cell(c).push_back(cell(c + 1));
541 MathGridInset::delCol(2);
542 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";