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"
14 #include "funcrequest.h"
16 #include "LaTeXFeatures.h"
17 #include "support/LAssert.h"
27 int getCols(string const & type)
29 if (type == "eqnarray")
33 if (type == "alignat")
35 if (type == "xalignat")
37 if (type == "xxalignat")
43 // returns position of first relation operator in the array
44 // used for "intelligent splitting"
45 MathArray::size_type firstRelOp(MathArray const & ar)
47 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
49 return it - ar.begin();
54 char const * star(bool numbered)
56 return numbered ? "" : "*";
60 int typecode(string const & s)
62 if (s == "none") return 0;
63 if (s == "simple") return 1;
64 if (s == "equation") return 2;
65 if (s == "eqnarray") return 3;
66 if (s == "align") return 4;
67 if (s == "alignat") return 5;
68 if (s == "xalignat") return 6;
69 if (s == "xxalignat") return 7;
70 if (s == "multline") return 8;
71 if (s == "gather") return 9;
72 lyxerr << "unknown hull type '" << s << "'\n";
76 bool smaller(string const & s, string const & t)
78 return typecode(s) < typecode(t);
82 } // end anon namespace
85 MathHullInset::MathHullInset()
86 : MathGridInset(1, 1), type_("none"), nonum_(1), label_(1)
92 MathHullInset::MathHullInset(string const & type)
93 : MathGridInset(getCols(type), 1), type_(type), nonum_(1), label_(1)
99 MathInset * MathHullInset::clone() const
101 return new MathHullInset(*this);
105 MathInset::mode_type MathHullInset::currentMode() const
108 return UNDECIDED_MODE;
109 // definitely math mode ...
114 bool MathHullInset::idxFirst(idx_type & idx, pos_type & pos) const
122 bool MathHullInset::idxLast(idx_type & idx, pos_type & pos) const
125 pos = cell(idx).size();
130 char MathHullInset::defaultColAlign(col_type col)
132 if (type_ == "eqnarray")
134 if (typecode(type_) >= typecode("align"))
135 return "rl"[col & 1];
140 int MathHullInset::defaultColSpace(col_type col)
142 if (type_ == "align" || type_ == "alignat")
144 if (type_ == "xalignat")
145 return (col & 1) ? 20 : 0;
146 if (type_ == "xxalignat")
147 return (col & 1) ? 40 : 0;
152 char const * MathHullInset::standardFont() const
155 return "lyxnochange";
160 void MathHullInset::metrics(MathMetricsInfo & mi) const
162 MathFontSetChanger dummy1(mi.base, standardFont());
163 MathStyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
165 // let the cells adjust themselves
166 MathGridInset::metrics(mi);
173 if (numberedType()) {
174 MathFontSetChanger dummy(mi.base, "mathbf");
176 for (row_type row = 0; row < nrows(); ++row)
177 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
183 // make it at least as high as the current font
186 math_font_max_dim(mi.base.font, asc, des);
187 dim_.a = max(dim_.a, asc);
188 dim_.d = max(dim_.d, des);
195 void MathHullInset::draw(MathPainterInfo & pi, int x, int y) const
197 MathFontSetChanger dummy1(pi.base, standardFont());
198 MathStyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
199 MathGridInset::draw(pi, x + 1, y);
201 if (numberedType()) {
202 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
203 for (row_type row = 0; row < nrows(); ++row) {
204 int const yy = y + rowinfo_[row].offset_;
205 MathFontSetChanger dummy(pi.base, "mathrm");
206 drawStr(pi, pi.base.font, xx, yy, nicelabel(row));
210 drawMarkers2(pi, x, y);
214 void MathHullInset::metricsT(TextMetricsInfo const & mi) const
217 MathGridInset::metricsT(mi);
220 WriteStream wi(os, false, true);
222 dim_.w = os.str().size();
229 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
232 MathGridInset::drawT(pain, x, y);
235 WriteStream wi(os, false, true);
237 pain.draw(x, y, os.str().c_str());
242 string MathHullInset::label(row_type row) const
244 row_type n = nrows();
245 lyx::Assert(row < n);
250 void MathHullInset::label(row_type row, string const & label)
252 //lyxerr << "setting label '" << label << "' for row " << row << endl;
257 void MathHullInset::numbered(row_type row, bool num)
263 bool MathHullInset::numbered(row_type row) const
269 bool MathHullInset::ams() const
273 type_ == "multline" ||
275 type_ == "alignat" ||
276 type_ == "xalignat" ||
277 type_ == "xxalignat";
281 bool MathHullInset::display() const
283 return type_ != "simple" && type_ != "none";
287 void MathHullInset::getLabelList(std::vector<string> & labels) const
289 for (row_type row = 0; row < nrows(); ++row)
290 if (!label_[row].empty() && nonum_[row] != 1)
291 labels.push_back(label_[row]);
295 bool MathHullInset::numberedType() const
299 if (type_ == "simple")
301 if (type_ == "xxalignat")
303 for (row_type row = 0; row < nrows(); ++row)
310 void MathHullInset::validate(LaTeXFeatures & features) const
313 features.require("amsmath");
316 // Validation is necessary only if not using AMS math.
317 // To be safe, we will always run mathedvalidate.
318 //if (features.amsstyle)
321 features.require("boldsymbol");
322 //features.binom = true;
324 MathNestInset::validate(features);
328 void MathHullInset::header_write(WriteStream & os) const
330 bool n = numberedType();
335 else if (type_ == "simple") {
341 else if (type_ == "equation") {
343 os << "\\begin{equation" << star(n) << "}\n";
348 else if (type_ == "eqnarray" || type_ == "align")
349 os << "\\begin{" << type_ << star(n) << "}\n";
351 else if (type_ == "alignat" || type_ == "xalignat")
352 os << "\\begin{" << type_ << star(n) << "}"
353 << "{" << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
355 else if (type_ == "xxalignat")
356 os << "\\begin{" << type_ << "}"
357 << "{" << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
359 else if (type_ == "multline" || type_ == "gather")
360 os << "\\begin{" << type_ << "}\n";
363 os << "\\begin{unknown" << star(n) << "}";
367 void MathHullInset::footer_write(WriteStream & os) const
369 bool n = numberedType();
374 else if (type_ == "simple")
377 else if (type_ == "equation")
379 os << "\\end{equation" << star(n) << "}\n";
383 else if (type_ == "eqnarray" || type_ == "align" || type_ == "alignat"
384 || type_ == "xalignat")
385 os << "\n\\end{" << type_ << star(n) << "}\n";
387 else if (type_ == "xxalignat" || type_ == "multline" || type_ == "gather")
388 os << "\n\\end{" << type_ << "}\n";
391 os << "\\end{unknown" << star(n) << "}";
395 void MathHullInset::addRow(row_type row)
397 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
398 label_.insert(label_.begin() + row + 1, string());
399 MathGridInset::addRow(row);
403 void MathHullInset::delRow(row_type row)
405 MathGridInset::delRow(row);
406 nonum_.erase(nonum_.begin() + row);
407 label_.erase(label_.begin() + row);
411 void MathHullInset::addFancyCol(col_type col)
413 if (type_ == "equation")
416 else if (type_ == "eqnarray") {
421 else if (type_ == "align" || type_ == "alignat"
422 || type_ == "xalignat" || type_ == "xxalignat")
423 MathGridInset::addCol(col);
427 void MathHullInset::delFancyCol(col_type col)
429 if (type_ == "alignat" || type_ == "xalignat" || type_ == "xxalignat")
430 MathGridInset::delCol(col);
434 string MathHullInset::nicelabel(row_type row) const
438 if (label_[row].empty())
439 return string("(#)");
440 return "(" + label_[row] + ")";
444 void MathHullInset::glueall()
447 for (idx_type i = 0; i < nargs(); ++i)
449 *this = MathHullInset("simple");
455 string const & MathHullInset::getType() const
461 void MathHullInset::setType(string const & type)
469 void MathHullInset::mutate(string const & newtype)
471 //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'\n";
473 // we try to move along the chain
474 // none <-> simple <-> equation <-> eqnarray
476 if (newtype == "dump") {
480 else if (newtype == type_) {
484 else if (type_ == "none") {
490 else if (type_ == "simple") {
491 if (newtype == "none") {
500 else if (type_ == "equation") {
501 if (smaller(newtype, type_)) {
504 } else if (newtype == "eqnarray") {
505 MathGridInset::addCol(1);
506 MathGridInset::addCol(1);
508 // split it "nicely" on the firest relop
509 pos_type pos = firstRelOp(cell(0));
510 cell(1) = MathArray(cell(0).begin() + pos, cell(0).end());
511 cell(0).erase(pos, cell(0).size());
513 if (cell(1).size()) {
514 cell(2) = MathArray(cell(1).begin() + 1, cell(1).end());
515 cell(1).erase(1, cell(1).size());
519 } else if (newtype == "multline" || newtype == "gather") {
523 MathGridInset::addCol(1);
525 pos_type pos = firstRelOp(cell(0));
527 cell(0).erase(pos, cell(0).size());
528 cell(1).erase(0, pos);
534 else if (type_ == "eqnarray") {
535 if (smaller(newtype, type_)) {
536 // set correct (no)numbering
537 bool allnonum = true;
538 for (row_type row = 0; row < nrows(); ++row)
542 // set first non-empty label
544 for (row_type row = 0; row < nrows(); ++row) {
545 if (!label_[row].empty()) {
552 nonum_[0] = allnonum;
555 } else { // align & Co.
556 for (row_type row = 0; row < nrows(); ++row) {
557 idx_type c = 3 * row + 1;
558 cell(c).append(cell(c + 1));
560 MathGridInset::delCol(2);
566 else if (type_ == "align") {
567 if (smaller(newtype, type_)) {
568 MathGridInset::addCol(1);
576 else if (type_ == "multline") {
577 if (newtype == "gather") {
580 lyxerr << "mutation from '" << type_
581 << "' to '" << newtype << "' not implemented"
586 else if (type_ == "gather") {
587 if (newtype == "multline") {
590 lyxerr << "mutation from '" << type_
591 << "' to '" << newtype << "' not implemented" << endl;
596 lyxerr << "mutation from '" << type_
597 << "' to '" << newtype << "' not implemented" << endl;
602 void MathHullInset::write(WriteStream & os) const
606 bool n = numberedType();
608 for (row_type row = 0; row < nrows(); ++row) {
609 for (col_type col = 0; col < ncols(); ++col)
610 os << cell(index(row, col)) << eocString(col);
612 if (!label_[row].empty())
613 os << "\\label{" << label_[row] << "}";
617 os << eolString(row);
624 void MathHullInset::normalize(NormalStream & os) const
626 os << "[formula " << type_ << " ";
627 MathGridInset::normalize(os);
632 void MathHullInset::mathmlize(MathMLStream & os) const
634 MathGridInset::mathmlize(os);
638 void MathHullInset::infoize(std::ostream & os) const
640 os << "Type: " << type_;
644 void MathHullInset::check() const
646 lyx::Assert(nonum_.size() == nrows());
647 lyx::Assert(label_.size() == nrows());
651 MathInset::result_type MathHullInset::dispatch
652 (FuncRequest const & cmd, idx_type & idx, pos_type & pos)
654 switch (cmd.action) {
657 if (type_ == "simple" || type_ == "equation") {
661 return DISPATCHED_POP;
663 return MathGridInset::dispatch(cmd, idx, pos);
665 case LFUN_MATH_NUMBER:
666 //lyxerr << "toggling all numbers\n";
668 //bv->lockedInsetStoreUndo(Undo::INSERT);
669 bool old = numberedType();
670 for (row_type row = 0; row < nrows(); ++row)
672 //bv->owner()->message(old ? _("No number") : _("Number"));
673 //updateLocal(bv, true);
677 case LFUN_MATH_NONUMBER:
679 //bv->lockedInsetStoreUndo(Undo::INSERT);
680 bool old = numbered(row(idx));
681 //bv->owner()->message(old ? _("No number") : _("Number"));
682 numbered(row(idx), !old);
683 //updateLocal(bv, true);