3 #include "math_hullinset.h"
4 #include "math_mathmlstream.h"
5 #include "math_streamstr.h"
6 #include "math_cursor.h"
7 #include "math_support.h"
8 #include "math_extern.h"
9 #include "math_charinset.h"
11 #include "textpainter.h"
12 #include "funcrequest.h"
14 #include "LaTeXFeatures.h"
15 #include "support/LAssert.h"
16 #include "frontends/Painter.h"
18 #include "frontends/Alert.h"
21 #include "BufferView.h"
25 using namespace lyx::support;
35 int getCols(string const & type)
37 if (type == "eqnarray")
41 if (type == "flalign")
43 if (type == "alignat")
45 if (type == "xalignat")
47 if (type == "xxalignat")
53 // returns position of first relation operator in the array
54 // used for "intelligent splitting"
55 MathArray::size_type firstRelOp(MathArray const & ar)
57 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
59 return it - ar.begin();
64 char const * star(bool numbered)
66 return numbered ? "" : "*";
70 int typecode(string const & s)
72 if (s == "none") return 0;
73 if (s == "simple") return 1;
74 if (s == "equation") return 2;
75 if (s == "eqnarray") return 3;
76 if (s == "align") return 4;
77 if (s == "alignat") return 5;
78 if (s == "xalignat") return 6;
79 if (s == "xxalignat") return 7;
80 if (s == "multline") return 8;
81 if (s == "gather") return 9;
82 if (s == "flalign") return 10;
83 lyxerr << "unknown hull type '" << s << "'\n";
87 bool smaller(string const & s, string const & t)
89 return typecode(s) < typecode(t);
93 } // end anon namespace
96 MathHullInset::MathHullInset()
97 : MathGridInset(1, 1), type_("none"), nonum_(1), label_(1)
103 MathHullInset::MathHullInset(string const & type)
104 : MathGridInset(getCols(type), 1), type_(type), nonum_(1), label_(1)
110 auto_ptr<InsetBase> MathHullInset::clone() const
112 return auto_ptr<InsetBase>(new MathHullInset(*this));
116 MathInset::mode_type MathHullInset::currentMode() const
119 return UNDECIDED_MODE;
120 // definitely math mode ...
125 bool MathHullInset::idxFirst(idx_type & idx, pos_type & pos) const
133 bool MathHullInset::idxLast(idx_type & idx, pos_type & pos) const
136 pos = cell(idx).size();
141 char MathHullInset::defaultColAlign(col_type col)
143 if (type_ == "eqnarray")
145 if (typecode(type_) >= typecode("align"))
146 return "rl"[col & 1];
151 int MathHullInset::defaultColSpace(col_type col)
153 if (type_ == "align" || type_ == "alignat")
155 if (type_ == "xalignat")
156 return (col & 1) ? 20 : 0;
157 if (type_ == "xxalignat" || type_ == "flalign")
158 return (col & 1) ? 40 : 0;
163 char const * MathHullInset::standardFont() const
166 return "lyxnochange";
171 void MathHullInset::metrics(MetricsInfo & mi, Dimension & dim) const
173 FontSetChanger dummy1(mi.base, standardFont());
174 StyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
176 // let the cells adjust themselves
177 MathGridInset::metrics(mi);
184 if (numberedType()) {
185 FontSetChanger dummy(mi.base, "mathbf");
187 for (row_type row = 0; row < nrows(); ++row)
188 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
194 // make it at least as high as the current font
197 math_font_max_dim(mi.base.font, asc, des);
198 dim_.asc = max(dim_.asc, asc);
199 dim_.des = max(dim_.des, des);
207 void MathHullInset::draw(PainterInfo & pi, int x, int y) const
209 FontSetChanger dummy1(pi.base, standardFont());
210 StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
211 MathGridInset::draw(pi, x + 1, y);
213 if (numberedType()) {
214 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
215 for (row_type row = 0; row < nrows(); ++row) {
216 int const yy = y + rowinfo_[row].offset_;
217 FontSetChanger dummy(pi.base, "mathrm");
218 drawStr(pi, pi.base.font, xx, yy, nicelabel(row));
222 drawMarkers2(pi, x, y);
226 void MathHullInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
229 MathGridInset::metricsT(mi, dim);
232 WriteStream wi(os, false, true);
234 dim.wid = os.str().size();
241 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
244 MathGridInset::drawT(pain, x, y);
247 WriteStream wi(os, false, true);
249 pain.draw(x, y, os.str().c_str());
254 string MathHullInset::label(row_type row) const
256 row_type n = nrows();
262 void MathHullInset::label(row_type row, string const & label)
264 //lyxerr << "setting label '" << label << "' for row " << row << endl;
269 void MathHullInset::numbered(row_type row, bool num)
275 bool MathHullInset::numbered(row_type row) const
281 bool MathHullInset::ams() const
285 type_ == "flalign" ||
286 type_ == "multline" ||
288 type_ == "alignat" ||
289 type_ == "xalignat" ||
290 type_ == "xxalignat";
294 bool MathHullInset::display() const
296 return type_ != "simple" && type_ != "none";
300 void MathHullInset::getLabelList(std::vector<string> & labels) const
302 for (row_type row = 0; row < nrows(); ++row)
303 if (!label_[row].empty() && nonum_[row] != 1)
304 labels.push_back(label_[row]);
308 bool MathHullInset::numberedType() const
312 if (type_ == "simple")
314 if (type_ == "xxalignat")
316 for (row_type row = 0; row < nrows(); ++row)
323 void MathHullInset::validate(LaTeXFeatures & features) const
326 features.require("amsmath");
329 // Validation is necessary only if not using AMS math.
330 // To be safe, we will always run mathedvalidate.
331 //if (features.amsstyle)
334 features.require("boldsymbol");
335 //features.binom = true;
337 MathGridInset::validate(features);
341 void MathHullInset::header_write(WriteStream & os) const
343 bool n = numberedType();
348 else if (type_ == "simple") {
354 else if (type_ == "equation") {
356 os << "\\begin{equation" << star(n) << "}\n";
361 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
362 || type_ == "gather" || type_ == "multline")
363 os << "\\begin{" << type_ << star(n) << "}\n";
365 else if (type_ == "alignat" || type_ == "xalignat")
366 os << "\\begin{" << type_ << star(n) << '}'
367 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
369 else if (type_ == "xxalignat")
370 os << "\\begin{" << type_ << '}'
371 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
374 os << "\\begin{unknown" << star(n) << '}';
378 void MathHullInset::footer_write(WriteStream & os) const
380 bool n = numberedType();
385 else if (type_ == "simple")
388 else if (type_ == "equation")
390 os << "\\end{equation" << star(n) << "}\n";
394 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
395 || type_ == "alignat" || type_ == "xalignat"
396 || type_ == "gather" || type_ == "multline")
397 os << "\\end{" << type_ << star(n) << "}\n";
399 else if (type_ == "xxalignat")
400 os << "\\end{" << type_ << "}\n";
403 os << "\\end{unknown" << star(n) << '}';
407 bool MathHullInset::colChangeOK() const
410 type_ == "align" || type_ == "flalign" ||type_ == "alignat" ||
411 type_ == "xalignat" || type_ == "xxalignat";
415 void MathHullInset::addRow(row_type row)
417 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
418 label_.insert(label_.begin() + row + 1, string());
419 MathGridInset::addRow(row);
423 void MathHullInset::delRow(row_type row)
427 MathGridInset::delRow(row);
428 nonum_.erase(nonum_.begin() + row);
429 label_.erase(label_.begin() + row);
433 void MathHullInset::addCol(col_type col)
436 MathGridInset::addCol(col);
438 lyxerr << "Can't change number of columns in '" << type_ << "'\n";
442 void MathHullInset::delCol(col_type col)
445 MathGridInset::delCol(col);
447 lyxerr << "Can't change number of columns in '" << type_ << "'\n";
451 string MathHullInset::nicelabel(row_type row) const
455 if (label_[row].empty())
456 return string("(#)");
457 return '(' + label_[row] + ')';
461 void MathHullInset::glueall()
464 for (idx_type i = 0; i < nargs(); ++i)
466 *this = MathHullInset("simple");
472 string const & MathHullInset::getType() const
478 void MathHullInset::setType(string const & type)
486 void MathHullInset::mutate(string const & newtype)
488 //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'\n";
490 // we try to move along the chain
491 // none <-> simple <-> equation <-> eqnarray
493 if (newtype == "dump") {
497 else if (newtype == type_) {
501 else if (type_ == "none") {
507 else if (type_ == "simple") {
508 if (newtype == "none") {
517 else if (type_ == "equation") {
518 if (smaller(newtype, type_)) {
521 } else if (newtype == "eqnarray") {
522 MathGridInset::addCol(1);
523 MathGridInset::addCol(1);
525 // split it "nicely" on the firest relop
526 pos_type pos = firstRelOp(cell(0));
527 cell(1) = MathArray(cell(0).begin() + pos, cell(0).end());
528 cell(0).erase(pos, cell(0).size());
530 if (cell(1).size()) {
531 cell(2) = MathArray(cell(1).begin() + 1, cell(1).end());
532 cell(1).erase(1, cell(1).size());
536 } else if (newtype == "multline" || newtype == "gather") {
540 MathGridInset::addCol(1);
542 pos_type pos = firstRelOp(cell(0));
544 cell(0).erase(pos, cell(0).size());
545 cell(1).erase(0, pos);
551 else if (type_ == "eqnarray") {
552 if (smaller(newtype, type_)) {
553 // set correct (no)numbering
554 bool allnonum = true;
555 for (row_type row = 0; row < nrows(); ++row)
559 // set first non-empty label
561 for (row_type row = 0; row < nrows(); ++row) {
562 if (!label_[row].empty()) {
569 nonum_[0] = allnonum;
572 } else { // align & Co.
573 for (row_type row = 0; row < nrows(); ++row) {
574 idx_type c = 3 * row + 1;
575 cell(c).append(cell(c + 1));
577 MathGridInset::delCol(2);
583 else if (type_ == "align") {
584 if (smaller(newtype, type_)) {
585 MathGridInset::addCol(1);
593 else if (type_ == "multline") {
594 if (newtype == "gather" || newtype == "align" ||
595 newtype == "xalignat" || newtype == "xxalignat" || newtype == "flalign")
597 else if (newtype == "eqnarray") {
598 MathGridInset::addCol(1);
599 MathGridInset::addCol(1);
602 lyxerr << "mutation from '" << type_
603 << "' to '" << newtype << "' not implemented" << endl;
607 else if (type_ == "gather") {
608 if (newtype == "multline") {
611 lyxerr << "mutation from '" << type_
612 << "' to '" << newtype << "' not implemented" << endl;
617 lyxerr << "mutation from '" << type_
618 << "' to '" << newtype << "' not implemented" << endl;
623 string MathHullInset::eolString(row_type row, bool fragile) const
626 if (numberedType()) {
627 if (!label_[row].empty() && !nonum_[row])
628 res += "\\label{" + label_[row] + '}';
629 if (nonum_[row] && (type_ != "multline"))
630 res += "\\nonumber ";
632 return res + MathGridInset::eolString(row, fragile);
636 void MathHullInset::write(WriteStream & os) const
639 MathGridInset::write(os);
644 void MathHullInset::normalize(NormalStream & os) const
646 os << "[formula " << type_ << ' ';
647 MathGridInset::normalize(os);
652 void MathHullInset::mathmlize(MathMLStream & os) const
654 MathGridInset::mathmlize(os);
658 void MathHullInset::infoize(std::ostream & os) const
660 os << "Type: " << type_;
664 void MathHullInset::check() const
666 Assert(nonum_.size() == nrows());
667 Assert(label_.size() == nrows());
671 void MathHullInset::doExtern
672 (FuncRequest const & func, idx_type & idx, pos_type & pos)
676 istringstream iss(func.argument.c_str());
677 iss >> lang >> extra;
682 #warning temporarily disabled
686 // lyxerr << "use selection: " << ar << "\n";
687 // insert(pipeThroughExtern(lang, extra, ar));
693 eq.push_back(MathAtom(new MathCharInset('=')));
695 // go to first item in line
696 idx -= idx % ncols();
699 if (getType() == "simple") {
700 size_type pos = cell(idx).find_last(eq);
702 if (mathcursor && mathcursor->selection()) {
703 asArray(mathcursor->grabAndEraseSelection(), ar);
704 } else if (pos == cell(idx).size()) {
706 lyxerr << "use whole cell: " << ar << "\n";
708 ar = MathArray(cell(idx).begin() + pos + 1, cell(idx).end());
709 lyxerr << "use partial cell form pos: " << pos << "\n";
711 cell(idx).append(eq);
712 cell(idx).append(pipeThroughExtern(lang, extra, ar));
713 pos = cell(idx).size();
717 if (getType() == "equation") {
718 lyxerr << "use equation inset\n";
720 MathArray & ar = cell(idx);
721 lyxerr << "use cell: " << ar << "\n";
723 cell(idx + 2) = pipeThroughExtern(lang, extra, ar);
724 // move to end of line
726 pos = cell(idx).size();
731 lyxerr << "use eqnarray\n";
732 idx -= idx % ncols();
735 MathArray ar = cell(idx);
736 lyxerr << "use cell: " << ar << "\n";
738 #warning temporarily disabled
742 cell(idx + 3) = pipeThroughExtern(lang, extra, ar);
744 pos = cell(idx).size();
749 dispatch_result MathHullInset::dispatch
750 (FuncRequest const & cmd, idx_type & idx, pos_type & pos)
752 switch (cmd.action) {
755 if (type_ == "simple" || type_ == "equation") {
759 return DISPATCHED_POP;
761 return MathGridInset::dispatch(cmd, idx, pos);
763 case LFUN_MATH_NUMBER:
764 //lyxerr << "toggling all numbers\n";
766 //bv->lockedInsetStoreUndo(Undo::INSERT);
767 bool old = numberedType();
768 if (type_ == "multline")
769 numbered(nrows() - 1, !old);
771 for (row_type row = 0; row < nrows(); ++row)
773 //bv->owner()->message(old ? _("No number") : _("Number"));
777 case LFUN_MATH_NONUMBER:
779 row_type r = (type_ == "multline") ? nrows() - 1 : row(idx);
780 //bv->lockedInsetStoreUndo(Undo::INSERT);
781 bool old = numbered(r);
782 //bv->owner()->message(old ? _("No number") : _("Number"));
787 case LFUN_INSERT_LABEL: {
788 row_type r = (type_ == "multline") ? nrows() - 1 : row(idx);
789 string old_label = label(r);
790 string new_label = cmd.argument;
792 if (new_label.empty()) {
793 string const default_label =
794 (lyxrc.label_init_length >= 0) ? "eq:" : "";
795 pair<bool, string> const res = old_label.empty()
796 ? Alert::askForText(_("Enter new label to insert:"), default_label)
797 : Alert::askForText(_("Enter label:"), old_label);
800 new_label = trim(res.second);
803 //if (new_label == old_label)
804 // break; // Nothing to do
806 if (!new_label.empty())
812 case LFUN_MATH_EXTERN:
813 doExtern(cmd, idx, pos);
814 return DISPATCHED_POP;
816 case LFUN_MATH_MUTATE: {
817 row_type r = row(idx);
818 col_type c = col(idx);
819 mutate(cmd.argument);
820 idx = r * ncols() + c;
823 if (pos > cell(idx).size())
824 pos = cell(idx).size();
825 return DISPATCHED_POP;
828 case LFUN_MATH_DISPLAY: {
829 mutate(type_ == "simple" ? "equation" : "simple");
831 pos = cell(idx).size();
832 return DISPATCHED_POP;
836 return MathGridInset::dispatch(cmd, idx, pos);
841 string MathHullInset::fileInsetLabel() const