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;
34 int getCols(string const & type)
36 if (type == "eqnarray")
40 if (type == "flalign")
42 if (type == "alignat")
44 if (type == "xalignat")
46 if (type == "xxalignat")
52 // returns position of first relation operator in the array
53 // used for "intelligent splitting"
54 MathArray::size_type firstRelOp(MathArray const & ar)
56 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
58 return it - ar.begin();
63 char const * star(bool numbered)
65 return numbered ? "" : "*";
69 int typecode(string const & s)
71 if (s == "none") return 0;
72 if (s == "simple") return 1;
73 if (s == "equation") return 2;
74 if (s == "eqnarray") return 3;
75 if (s == "align") return 4;
76 if (s == "alignat") return 5;
77 if (s == "xalignat") return 6;
78 if (s == "xxalignat") return 7;
79 if (s == "multline") return 8;
80 if (s == "gather") return 9;
81 if (s == "flalign") return 10;
82 lyxerr << "unknown hull type '" << s << "'\n";
86 bool smaller(string const & s, string const & t)
88 return typecode(s) < typecode(t);
92 } // end anon namespace
95 MathHullInset::MathHullInset()
96 : MathGridInset(1, 1), type_("none"), nonum_(1), label_(1)
102 MathHullInset::MathHullInset(string const & type)
103 : MathGridInset(getCols(type), 1), type_(type), nonum_(1), label_(1)
109 InsetBase * MathHullInset::clone() const
111 return new MathHullInset(*this);
115 MathInset::mode_type MathHullInset::currentMode() const
118 return UNDECIDED_MODE;
119 // definitely math mode ...
124 bool MathHullInset::idxFirst(idx_type & idx, pos_type & pos) const
132 bool MathHullInset::idxLast(idx_type & idx, pos_type & pos) const
135 pos = cell(idx).size();
140 char MathHullInset::defaultColAlign(col_type col)
142 if (type_ == "eqnarray")
144 if (typecode(type_) >= typecode("align"))
145 return "rl"[col & 1];
150 int MathHullInset::defaultColSpace(col_type col)
152 if (type_ == "align" || type_ == "alignat")
154 if (type_ == "xalignat")
155 return (col & 1) ? 20 : 0;
156 if (type_ == "xxalignat" || type_ == "flalign")
157 return (col & 1) ? 40 : 0;
162 char const * MathHullInset::standardFont() const
165 return "lyxnochange";
170 void MathHullInset::metrics(MetricsInfo & mi, Dimension & dim) const
172 FontSetChanger dummy1(mi.base, standardFont());
173 StyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
175 // let the cells adjust themselves
176 MathGridInset::metrics(mi);
183 if (numberedType()) {
184 FontSetChanger dummy(mi.base, "mathbf");
186 for (row_type row = 0; row < nrows(); ++row)
187 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
193 // make it at least as high as the current font
196 math_font_max_dim(mi.base.font, asc, des);
197 dim_.asc = max(dim_.asc, asc);
198 dim_.des = max(dim_.des, des);
206 void MathHullInset::draw(PainterInfo & pi, int x, int y) const
208 FontSetChanger dummy1(pi.base, standardFont());
209 StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
210 MathGridInset::draw(pi, x + 1, y);
212 if (numberedType()) {
213 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
214 for (row_type row = 0; row < nrows(); ++row) {
215 int const yy = y + rowinfo_[row].offset_;
216 FontSetChanger dummy(pi.base, "mathrm");
217 drawStr(pi, pi.base.font, xx, yy, nicelabel(row));
221 drawMarkers2(pi, x, y);
225 void MathHullInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
228 MathGridInset::metricsT(mi, dim);
231 WriteStream wi(os, false, true);
233 dim.wid = os.str().size();
240 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
243 MathGridInset::drawT(pain, x, y);
246 WriteStream wi(os, false, true);
248 pain.draw(x, y, os.str().c_str());
253 string MathHullInset::label(row_type row) const
255 row_type n = nrows();
261 void MathHullInset::label(row_type row, string const & label)
263 //lyxerr << "setting label '" << label << "' for row " << row << endl;
268 void MathHullInset::numbered(row_type row, bool num)
274 bool MathHullInset::numbered(row_type row) const
280 bool MathHullInset::ams() const
284 type_ == "flalign" ||
285 type_ == "multline" ||
287 type_ == "alignat" ||
288 type_ == "xalignat" ||
289 type_ == "xxalignat";
293 bool MathHullInset::display() const
295 return type_ != "simple" && type_ != "none";
299 void MathHullInset::getLabelList(std::vector<string> & labels) const
301 for (row_type row = 0; row < nrows(); ++row)
302 if (!label_[row].empty() && nonum_[row] != 1)
303 labels.push_back(label_[row]);
307 bool MathHullInset::numberedType() const
311 if (type_ == "simple")
313 if (type_ == "xxalignat")
315 for (row_type row = 0; row < nrows(); ++row)
322 void MathHullInset::validate(LaTeXFeatures & features) const
325 features.require("amsmath");
328 // Validation is necessary only if not using AMS math.
329 // To be safe, we will always run mathedvalidate.
330 //if (features.amsstyle)
333 features.require("boldsymbol");
334 //features.binom = true;
336 MathGridInset::validate(features);
340 void MathHullInset::header_write(WriteStream & os) const
342 bool n = numberedType();
347 else if (type_ == "simple") {
353 else if (type_ == "equation") {
355 os << "\\begin{equation" << star(n) << "}\n";
360 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
361 || type_ == "gather" || type_ == "multline")
362 os << "\\begin{" << type_ << star(n) << "}\n";
364 else if (type_ == "alignat" || type_ == "xalignat")
365 os << "\\begin{" << type_ << star(n) << '}'
366 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
368 else if (type_ == "xxalignat")
369 os << "\\begin{" << type_ << '}'
370 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
373 os << "\\begin{unknown" << star(n) << '}';
377 void MathHullInset::footer_write(WriteStream & os) const
379 bool n = numberedType();
384 else if (type_ == "simple")
387 else if (type_ == "equation")
389 os << "\\end{equation" << star(n) << "}\n";
393 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
394 || type_ == "alignat" || type_ == "xalignat"
395 || type_ == "gather" || type_ == "multline")
396 os << "\\end{" << type_ << star(n) << "}\n";
398 else if (type_ == "xxalignat")
399 os << "\\end{" << type_ << "}\n";
402 os << "\\end{unknown" << star(n) << '}';
406 bool MathHullInset::colChangeOK() const
409 type_ == "align" || type_ == "flalign" ||type_ == "alignat" ||
410 type_ == "xalignat" || type_ == "xxalignat";
414 void MathHullInset::addRow(row_type row)
416 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
417 label_.insert(label_.begin() + row + 1, string());
418 MathGridInset::addRow(row);
422 void MathHullInset::delRow(row_type row)
426 MathGridInset::delRow(row);
427 nonum_.erase(nonum_.begin() + row);
428 label_.erase(label_.begin() + row);
432 void MathHullInset::addCol(col_type col)
435 MathGridInset::addCol(col);
437 lyxerr << "Can't change number of columns in '" << type_ << "'\n";
441 void MathHullInset::delCol(col_type col)
444 MathGridInset::delCol(col);
446 lyxerr << "Can't change number of columns in '" << type_ << "'\n";
450 string MathHullInset::nicelabel(row_type row) const
454 if (label_[row].empty())
455 return string("(#)");
456 return '(' + label_[row] + ')';
460 void MathHullInset::glueall()
463 for (idx_type i = 0; i < nargs(); ++i)
465 *this = MathHullInset("simple");
471 string const & MathHullInset::getType() const
477 void MathHullInset::setType(string const & type)
485 void MathHullInset::mutate(string const & newtype)
487 //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'\n";
489 // we try to move along the chain
490 // none <-> simple <-> equation <-> eqnarray
492 if (newtype == "dump") {
496 else if (newtype == type_) {
500 else if (type_ == "none") {
506 else if (type_ == "simple") {
507 if (newtype == "none") {
516 else if (type_ == "equation") {
517 if (smaller(newtype, type_)) {
520 } else if (newtype == "eqnarray") {
521 MathGridInset::addCol(1);
522 MathGridInset::addCol(1);
524 // split it "nicely" on the firest relop
525 pos_type pos = firstRelOp(cell(0));
526 cell(1) = MathArray(cell(0).begin() + pos, cell(0).end());
527 cell(0).erase(pos, cell(0).size());
529 if (cell(1).size()) {
530 cell(2) = MathArray(cell(1).begin() + 1, cell(1).end());
531 cell(1).erase(1, cell(1).size());
535 } else if (newtype == "multline" || newtype == "gather") {
539 MathGridInset::addCol(1);
541 pos_type pos = firstRelOp(cell(0));
543 cell(0).erase(pos, cell(0).size());
544 cell(1).erase(0, pos);
550 else if (type_ == "eqnarray") {
551 if (smaller(newtype, type_)) {
552 // set correct (no)numbering
553 bool allnonum = true;
554 for (row_type row = 0; row < nrows(); ++row)
558 // set first non-empty label
560 for (row_type row = 0; row < nrows(); ++row) {
561 if (!label_[row].empty()) {
568 nonum_[0] = allnonum;
571 } else { // align & Co.
572 for (row_type row = 0; row < nrows(); ++row) {
573 idx_type c = 3 * row + 1;
574 cell(c).append(cell(c + 1));
576 MathGridInset::delCol(2);
582 else if (type_ == "align") {
583 if (smaller(newtype, type_)) {
584 MathGridInset::addCol(1);
592 else if (type_ == "multline") {
593 if (newtype == "gather" || newtype == "align" ||
594 newtype == "xalignat" || newtype == "xxalignat" || newtype == "flalign")
596 else if (newtype == "eqnarray") {
597 MathGridInset::addCol(1);
598 MathGridInset::addCol(1);
601 lyxerr << "mutation from '" << type_
602 << "' to '" << newtype << "' not implemented" << endl;
606 else if (type_ == "gather") {
607 if (newtype == "multline") {
610 lyxerr << "mutation from '" << type_
611 << "' to '" << newtype << "' not implemented" << endl;
616 lyxerr << "mutation from '" << type_
617 << "' to '" << newtype << "' not implemented" << endl;
622 string MathHullInset::eolString(row_type row, bool fragile) const
625 if (numberedType()) {
626 if (!label_[row].empty() && !nonum_[row])
627 res += "\\label{" + label_[row] + '}';
628 if (nonum_[row] && (type_ != "multline"))
629 res += "\\nonumber ";
631 return res + MathGridInset::eolString(row, fragile);
635 void MathHullInset::write(WriteStream & os) const
638 MathGridInset::write(os);
643 void MathHullInset::normalize(NormalStream & os) const
645 os << "[formula " << type_ << ' ';
646 MathGridInset::normalize(os);
651 void MathHullInset::mathmlize(MathMLStream & os) const
653 MathGridInset::mathmlize(os);
657 void MathHullInset::infoize(std::ostream & os) const
659 os << "Type: " << type_;
663 void MathHullInset::check() const
665 Assert(nonum_.size() == nrows());
666 Assert(label_.size() == nrows());
670 void MathHullInset::doExtern
671 (FuncRequest const & func, idx_type & idx, pos_type & pos)
675 istringstream iss(func.argument.c_str());
676 iss >> lang >> extra;
681 #warning temporarily disabled
685 // lyxerr << "use selection: " << ar << "\n";
686 // insert(pipeThroughExtern(lang, extra, ar));
692 eq.push_back(MathAtom(new MathCharInset('=')));
694 // go to first item in line
695 idx -= idx % ncols();
698 if (getType() == "simple") {
699 size_type pos = cell(idx).find_last(eq);
701 if (mathcursor && mathcursor->selection()) {
702 asArray(mathcursor->grabAndEraseSelection(), ar);
703 } else if (pos == cell(idx).size()) {
705 lyxerr << "use whole cell: " << ar << "\n";
707 ar = MathArray(cell(idx).begin() + pos + 1, cell(idx).end());
708 lyxerr << "use partial cell form pos: " << pos << "\n";
710 cell(idx).append(eq);
711 cell(idx).append(pipeThroughExtern(lang, extra, ar));
712 pos = cell(idx).size();
716 if (getType() == "equation") {
717 lyxerr << "use equation inset\n";
719 MathArray & ar = cell(idx);
720 lyxerr << "use cell: " << ar << "\n";
722 cell(idx + 2) = pipeThroughExtern(lang, extra, ar);
723 // move to end of line
725 pos = cell(idx).size();
730 lyxerr << "use eqnarray\n";
731 idx -= idx % ncols();
734 MathArray ar = cell(idx);
735 lyxerr << "use cell: " << ar << "\n";
737 #warning temporarily disabled
741 cell(idx + 3) = pipeThroughExtern(lang, extra, ar);
743 pos = cell(idx).size();
748 dispatch_result MathHullInset::dispatch
749 (FuncRequest const & cmd, idx_type & idx, pos_type & pos)
751 switch (cmd.action) {
754 if (type_ == "simple" || type_ == "equation") {
758 return DISPATCHED_POP;
760 return MathGridInset::dispatch(cmd, idx, pos);
762 case LFUN_MATH_NUMBER:
763 //lyxerr << "toggling all numbers\n";
765 //bv->lockedInsetStoreUndo(Undo::INSERT);
766 bool old = numberedType();
767 if (type_ == "multline")
768 numbered(nrows() - 1, !old);
770 for (row_type row = 0; row < nrows(); ++row)
772 //bv->owner()->message(old ? _("No number") : _("Number"));
776 case LFUN_MATH_NONUMBER:
778 row_type r = (type_ == "multline") ? nrows() - 1 : row(idx);
779 //bv->lockedInsetStoreUndo(Undo::INSERT);
780 bool old = numbered(r);
781 //bv->owner()->message(old ? _("No number") : _("Number"));
786 case LFUN_INSERT_LABEL: {
787 row_type r = (type_ == "multline") ? nrows() - 1 : row(idx);
788 string old_label = label(r);
789 string new_label = cmd.argument;
791 if (new_label.empty()) {
792 string const default_label =
793 (lyxrc.label_init_length >= 0) ? "eq:" : "";
794 pair<bool, string> const res = old_label.empty()
795 ? Alert::askForText(_("Enter new label to insert:"), default_label)
796 : Alert::askForText(_("Enter label:"), old_label);
799 new_label = trim(res.second);
802 //if (new_label == old_label)
803 // break; // Nothing to do
805 if (!new_label.empty())
811 case LFUN_MATH_EXTERN:
812 doExtern(cmd, idx, pos);
813 return DISPATCHED_POP;
815 case LFUN_MATH_MUTATE: {
816 row_type r = row(idx);
817 col_type c = col(idx);
818 mutate(cmd.argument);
819 idx = r * ncols() + c;
822 if (pos > cell(idx).size())
823 pos = cell(idx).size();
824 return DISPATCHED_POP;
827 case LFUN_MATH_DISPLAY: {
828 mutate(type_ == "simple" ? "equation" : "simple");
830 pos = cell(idx).size();
831 return DISPATCHED_POP;
835 return MathGridInset::dispatch(cmd, idx, pos);
840 string MathHullInset::fileInsetLabel() const