7 #include "math_hullinset.h"
8 #include "math_mathmlstream.h"
9 #include "math_streamstr.h"
10 #include "math_support.h"
12 #include "frontends/Painter.h"
13 #include "textpainter.h"
15 #include "LaTeXFeatures.h"
16 #include "support/LAssert.h"
26 int getCols(string const & type)
28 if (type == "eqnarray")
32 if (type == "alignat")
34 if (type == "xalignat")
36 if (type == "xxalignat")
42 // returns position of first relation operator in the array
43 // used for "intelligent splitting"
44 MathArray::size_type firstRelOp(MathArray const & ar)
46 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
48 return it - ar.begin();
53 char const * star(bool numbered)
55 return numbered ? "" : "*";
59 int typecode(string const & s)
61 if (s == "none") return 0;
62 if (s == "simple") return 1;
63 if (s == "equation") return 2;
64 if (s == "eqnarray") return 3;
65 if (s == "align") return 4;
66 if (s == "alignat") return 5;
67 if (s == "xalignat") return 6;
68 if (s == "xxalignat") return 7;
69 if (s == "multline") return 8;
70 if (s == "gather") return 9;
71 lyxerr << "unknown hull type '" << s << "'\n";
75 bool smaller(string const & s, string const & t)
77 return typecode(s) < typecode(t);
81 } // end anon namespace
84 MathHullInset::MathHullInset()
85 : MathGridInset(1, 1), type_("none"), nonum_(1), label_(1)
91 MathHullInset::MathHullInset(string const & type)
92 : MathGridInset(getCols(type), 1), type_(type), nonum_(1), label_(1)
98 MathInset * MathHullInset::clone() const
100 return new MathHullInset(*this);
104 MathInset::mode_type MathHullInset::currentMode() const
107 return UNDECIDED_MODE;
108 // definitely math mode ...
113 bool MathHullInset::idxFirst(idx_type & idx, pos_type & pos) const
121 bool MathHullInset::idxLast(idx_type & idx, pos_type & pos) const
124 pos = cell(idx).size();
129 char MathHullInset::defaultColAlign(col_type col)
131 if (type_ == "eqnarray")
133 if (typecode(type_) >= typecode("align"))
134 return "rl"[col & 1];
139 int MathHullInset::defaultColSpace(col_type col)
141 if (type_ == "align" || type_ == "alignat")
143 if (type_ == "xalignat")
144 return (col & 1) ? 20 : 0;
145 if (type_ == "xxalignat")
146 return (col & 1) ? 40 : 0;
151 char const * MathHullInset::standardFont() const
154 return "lyxnochange";
159 void MathHullInset::metrics(MathMetricsInfo & mi) const
161 MathFontSetChanger dummy(mi.base, standardFont());
163 // let the cells adjust themselves
164 MathGridInset::metrics(mi);
171 if (numberedType()) {
172 MathFontSetChanger dummy(mi.base, "mathbf");
174 for (row_type row = 0; row < nrows(); ++row)
175 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
181 // make it at least as high as the current font
184 math_font_max_dim(mi.base.font, asc, des);
185 dim_.a = max(dim_.a, asc);
186 dim_.d = max(dim_.d, des);
193 void MathHullInset::draw(MathPainterInfo & pi, int x, int y) const
195 MathFontSetChanger dummy(pi.base, standardFont());
196 MathGridInset::draw(pi, x + 1, y);
198 if (numberedType()) {
199 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
200 for (row_type row = 0; row < nrows(); ++row) {
201 int const yy = y + rowinfo_[row].offset_;
202 MathFontSetChanger dummy(pi.base, "mathrm");
203 drawStr(pi, pi.base.font, xx, yy, nicelabel(row));
207 drawMarkers2(pi, x, y);
211 void MathHullInset::metricsT(TextMetricsInfo const & mi) const
214 MathGridInset::metricsT(mi);
217 WriteStream wi(os, false, true);
219 dim_.w = os.str().size();
226 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
229 MathGridInset::drawT(pain, x, y);
232 WriteStream wi(os, false, true);
234 pain.draw(x, y, os.str().c_str());
239 string MathHullInset::label(row_type row) const
241 row_type n = nrows();
242 lyx::Assert(row < n);
247 void MathHullInset::label(row_type row, string const & label)
249 lyxerr << "setting label '" << label << "' for row " << row << endl;
254 void MathHullInset::numbered(row_type row, bool num)
260 bool MathHullInset::numbered(row_type row) const
266 bool MathHullInset::ams() const
270 type_ == "multline" ||
272 type_ == "alignat" ||
273 type_ == "xalignat" ||
274 type_ == "xxalignat";
278 bool MathHullInset::display() const
280 return type_ != "simple" && type_ != "none";
284 vector<string> MathHullInset::getLabelList() const
287 for (row_type row = 0; row < nrows(); ++row)
288 if (!label_[row].empty() && nonum_[row] != 1)
289 res.push_back(label_[row]);
294 bool MathHullInset::numberedType() const
298 if (type_ == "simple")
300 if (type_ == "xxalignat")
302 for (row_type row = 0; row < nrows(); ++row)
309 void MathHullInset::validate(LaTeXFeatures & features) const
312 features.require("amsmath");
315 // Validation is necessary only if not using AMS math.
316 // To be safe, we will always run mathedvalidate.
317 //if (features.amsstyle)
320 features.require("boldsymbol");
321 //features.binom = true;
323 MathNestInset::validate(features);
327 void MathHullInset::header_write(WriteStream & os) const
329 bool n = numberedType();
334 else if (type_ == "simple") {
340 else if (type_ == "equation") {
342 os << "\\begin{equation" << star(n) << "}\n";
347 else if (type_ == "eqnarray" || type_ == "align")
348 os << "\\begin{" << type_ << star(n) << "}\n";
350 else if (type_ == "alignat" || type_ == "xalignat")
351 os << "\\begin{" << type_ << star(n) << "}"
352 << "{" << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
354 else if (type_ == "xxalignat")
355 os << "\\begin{" << type_ << "}"
356 << "{" << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
358 else if (type_ == "multline" || type_ == "gather")
359 os << "\\begin{" << type_ << "}\n";
362 os << "\\begin{unknown" << star(n) << "}";
366 void MathHullInset::footer_write(WriteStream & os) const
368 bool n = numberedType();
373 else if (type_ == "simple")
376 else if (type_ == "equation")
378 os << "\\end{equation" << star(n) << "}\n";
382 else if (type_ == "eqnarray" || type_ == "align" || type_ == "alignat"
383 || type_ == "xalignat")
384 os << "\n\\end{" << type_ << star(n) << "}\n";
386 else if (type_ == "xxalignat" || type_ == "multline" || type_ == "gather")
387 os << "\n\\end{" << type_ << "}\n";
390 os << "\\end{unknown" << star(n) << "}";
394 void MathHullInset::addRow(row_type row)
396 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
397 label_.insert(label_.begin() + row + 1, string());
398 MathGridInset::addRow(row);
402 void MathHullInset::delRow(row_type row)
404 MathGridInset::delRow(row);
405 nonum_.erase(nonum_.begin() + row);
406 label_.erase(label_.begin() + row);
410 void MathHullInset::addFancyCol(col_type col)
412 if (type_ == "equation")
415 else if (type_ == "eqnarray") {
420 else if (type_ == "align" || type_ == "alignat"
421 || type_ == "xalignat" || type_ == "xxalignat")
422 MathGridInset::addCol(col);
426 void MathHullInset::delFancyCol(col_type col)
428 if (type_ == "alignat" || type_ == "xalignat" || type_ == "xxalignat")
429 MathGridInset::delCol(col);
433 string MathHullInset::nicelabel(row_type row) const
437 if (label_[row].empty())
438 return string("(#)");
439 return "(" + label_[row] + ")";
443 void MathHullInset::glueall()
446 for (idx_type i = 0; i < nargs(); ++i)
447 ar.push_back(cell(i));
448 *this = MathHullInset("simple");
454 string const & MathHullInset::getType() const
460 void MathHullInset::setType(string const & type)
468 void MathHullInset::mutate(string const & newtype)
470 //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'\n";
472 // we try to move along the chain
473 // none <-> simple <-> equation <-> eqnarray
475 if (newtype == "dump") {
479 else if (newtype == type_) {
483 else if (type_ == "none") {
489 else if (type_ == "simple") {
490 if (newtype == "none") {
499 else if (type_ == "equation") {
500 if (smaller(newtype, type_)) {
503 } else if (newtype == "eqnarray") {
504 MathGridInset::addCol(1);
505 MathGridInset::addCol(1);
507 // split it "nicely" on the firest relop
508 pos_type pos = firstRelOp(cell(0));
509 cell(1) = MathArray(cell(0), pos, cell(0).size());
510 cell(0).erase(pos, cell(0).size());
512 if (cell(1).size()) {
513 cell(2) = MathArray(cell(1), 1, cell(1).size());
514 cell(1).erase(1, cell(1).size());
519 MathGridInset::addCol(1);
521 pos_type pos = firstRelOp(cell(0));
523 cell(0).erase(pos, cell(0).size());
524 cell(1).erase(0, pos);
530 else if (type_ == "eqnarray") {
531 if (smaller(newtype, type_)) {
532 // set correct (no)numbering
533 bool allnonum = true;
534 for (row_type row = 0; row < nrows(); ++row)
538 // set first non-empty label
540 for (row_type row = 0; row < nrows(); ++row) {
541 if (!label_[row].empty()) {
548 nonum_[0] = allnonum;
551 } else { // align & Co.
552 for (row_type row = 0; row < nrows(); ++row) {
553 idx_type c = 3 * row + 1;
554 cell(c).push_back(cell(c + 1));
556 MathGridInset::delCol(2);
562 else if (type_ == "align") {
563 if (smaller(newtype, type_)) {
564 MathGridInset::addCol(1);
572 else if (type_ == "multline") {
573 if (newtype == "gather") {
576 lyxerr << "mutation from '" << type_
577 << "' to '" << newtype << "' not implemented"
582 else if (type_ == "gather") {
583 if (newtype == "multline") {
586 lyxerr << "mutation from '" << type_
587 << "' to '" << newtype << "' not implemented" << endl;
592 lyxerr << "mutation from '" << type_
593 << "' to '" << newtype << "' not implemented" << endl;
598 void MathHullInset::write(WriteStream & os) const
602 bool n = numberedType();
604 for (row_type row = 0; row < nrows(); ++row) {
605 for (col_type col = 0; col < ncols(); ++col)
606 os << cell(index(row, col)) << eocString(col);
608 if (!label_[row].empty())
609 os << "\\label{" << label_[row] << "}";
613 os << eolString(row);
620 void MathHullInset::normalize(NormalStream & os) const
622 os << "[formula " << type_ << " ";
623 MathGridInset::normalize(os);
628 void MathHullInset::mathmlize(MathMLStream & os) const
630 MathGridInset::mathmlize(os);
634 void MathHullInset::infoize(std::ostream & os) const
636 os << "Type: " << type_;
640 void MathHullInset::check() const
642 lyx::Assert(nonum_.size() == nrows());
643 lyx::Assert(label_.size() == nrows());