2 * \file math_hullinset.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "math_hullinset.h"
14 #include "math_mathmlstream.h"
15 #include "math_streamstr.h"
16 #include "math_cursor.h"
17 #include "math_support.h"
18 #include "math_extern.h"
19 #include "math_charinset.h"
20 #include "textpainter.h"
21 #include "dispatchresult.h"
23 #include "funcrequest.h"
25 #include "LaTeXFeatures.h"
29 #include "frontends/Alert.h"
31 #include "support/std_sstream.h"
34 using lyx::support::trim;
41 using std::istringstream;
42 using std::ostringstream;
48 int getCols(string const & type)
50 if (type == "eqnarray")
54 if (type == "flalign")
56 if (type == "alignat")
58 if (type == "xalignat")
60 if (type == "xxalignat")
66 // returns position of first relation operator in the array
67 // used for "intelligent splitting"
68 MathArray::size_type firstRelOp(MathArray const & ar)
70 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
72 return it - ar.begin();
77 char const * star(bool numbered)
79 return numbered ? "" : "*";
83 int typecode(string const & s)
85 if (s == "none") return 0;
86 if (s == "simple") return 1;
87 if (s == "equation") return 2;
88 if (s == "eqnarray") return 3;
89 if (s == "align") return 4;
90 if (s == "alignat") return 5;
91 if (s == "xalignat") return 6;
92 if (s == "xxalignat") return 7;
93 if (s == "multline") return 8;
94 if (s == "gather") return 9;
95 if (s == "flalign") return 10;
96 lyxerr << "unknown hull type '" << s << "'" << endl;
100 bool smaller(string const & s, string const & t)
102 return typecode(s) < typecode(t);
106 } // end anon namespace
109 MathHullInset::MathHullInset()
110 : MathGridInset(1, 1), type_("none"), nonum_(1), label_(1)
116 MathHullInset::MathHullInset(string const & type)
117 : MathGridInset(getCols(type), 1), type_(type), nonum_(1), label_(1)
123 auto_ptr<InsetBase> MathHullInset::clone() const
125 return auto_ptr<InsetBase>(new MathHullInset(*this));
129 MathInset::mode_type MathHullInset::currentMode() const
132 return UNDECIDED_MODE;
133 // definitely math mode ...
138 bool MathHullInset::idxFirst(BufferView & bv) const
140 CursorSlice & cur = cursorTip(bv);
147 bool MathHullInset::idxLast(BufferView & bv) const
149 CursorSlice & cur = cursorTip(bv);
150 cur.idx() = nargs() - 1;
151 cur.pos() = cur.lastpos();
156 char MathHullInset::defaultColAlign(col_type col)
158 if (type_ == "eqnarray")
160 if (typecode(type_) >= typecode("align"))
161 return "rl"[col & 1];
166 int MathHullInset::defaultColSpace(col_type col)
168 if (type_ == "align" || type_ == "alignat")
170 if (type_ == "xalignat")
171 return (col & 1) ? 20 : 0;
172 if (type_ == "xxalignat" || type_ == "flalign")
173 return (col & 1) ? 40 : 0;
178 char const * MathHullInset::standardFont() const
181 return "lyxnochange";
186 void MathHullInset::metrics(MetricsInfo & mi, Dimension & dim) const
188 FontSetChanger dummy1(mi.base, standardFont());
189 StyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
191 // let the cells adjust themselves
192 MathGridInset::metrics(mi);
199 if (numberedType()) {
200 FontSetChanger dummy(mi.base, "mathbf");
202 for (row_type row = 0; row < nrows(); ++row)
203 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
209 // make it at least as high as the current font
212 math_font_max_dim(mi.base.font, asc, des);
213 dim_.asc = max(dim_.asc, asc);
214 dim_.des = max(dim_.des, des);
222 void MathHullInset::draw(PainterInfo & pi, int x, int y) const
224 FontSetChanger dummy1(pi.base, standardFont());
225 StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
226 MathGridInset::draw(pi, x + 1, y);
228 if (numberedType()) {
229 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
230 for (row_type row = 0; row < nrows(); ++row) {
231 int const yy = y + rowinfo_[row].offset_;
232 FontSetChanger dummy(pi.base, "mathrm");
233 drawStr(pi, pi.base.font, xx, yy, nicelabel(row));
237 drawMarkers2(pi, x, y);
241 void MathHullInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
244 MathGridInset::metricsT(mi, dim);
247 WriteStream wi(os, false, true);
249 dim.wid = os.str().size();
256 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
259 MathGridInset::drawT(pain, x, y);
262 WriteStream wi(os, false, true);
264 pain.draw(x, y, os.str().c_str());
269 string MathHullInset::label(row_type row) const
271 row_type n = nrows();
272 BOOST_ASSERT(row < n);
277 void MathHullInset::label(row_type row, string const & label)
279 //lyxerr << "setting label '" << label << "' for row " << row << endl;
284 void MathHullInset::numbered(row_type row, bool num)
290 bool MathHullInset::numbered(row_type row) const
296 bool MathHullInset::ams() const
300 type_ == "flalign" ||
301 type_ == "multline" ||
303 type_ == "alignat" ||
304 type_ == "xalignat" ||
305 type_ == "xxalignat";
309 bool MathHullInset::display() const
311 return type_ != "simple" && type_ != "none";
315 void MathHullInset::getLabelList(Buffer const &,
316 std::vector<string> & labels) const
318 for (row_type row = 0; row < nrows(); ++row)
319 if (!label_[row].empty() && nonum_[row] != 1)
320 labels.push_back(label_[row]);
324 bool MathHullInset::numberedType() const
328 if (type_ == "simple")
330 if (type_ == "xxalignat")
332 for (row_type row = 0; row < nrows(); ++row)
339 void MathHullInset::validate(LaTeXFeatures & features) const
342 features.require("amsmath");
345 // Validation is necessary only if not using AMS math.
346 // To be safe, we will always run mathedvalidate.
347 //if (features.amsstyle)
350 features.require("boldsymbol");
351 //features.binom = true;
353 MathGridInset::validate(features);
357 void MathHullInset::header_write(WriteStream & os) const
359 bool n = numberedType();
364 else if (type_ == "simple") {
370 else if (type_ == "equation") {
372 os << "\\begin{equation" << star(n) << "}\n";
377 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
378 || type_ == "gather" || type_ == "multline")
379 os << "\\begin{" << type_ << star(n) << "}\n";
381 else if (type_ == "alignat" || type_ == "xalignat")
382 os << "\\begin{" << type_ << star(n) << '}'
383 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
385 else if (type_ == "xxalignat")
386 os << "\\begin{" << type_ << '}'
387 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
390 os << "\\begin{unknown" << star(n) << '}';
394 void MathHullInset::footer_write(WriteStream & os) const
396 bool n = numberedType();
401 else if (type_ == "simple")
404 else if (type_ == "equation")
406 os << "\\end{equation" << star(n) << "}\n";
410 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
411 || type_ == "alignat" || type_ == "xalignat"
412 || type_ == "gather" || type_ == "multline")
413 os << "\\end{" << type_ << star(n) << "}\n";
415 else if (type_ == "xxalignat")
416 os << "\\end{" << type_ << "}\n";
419 os << "\\end{unknown" << star(n) << '}';
423 bool MathHullInset::colChangeOK() const
426 type_ == "align" || type_ == "flalign" ||type_ == "alignat" ||
427 type_ == "xalignat" || type_ == "xxalignat";
431 void MathHullInset::addRow(row_type row)
433 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
434 label_.insert(label_.begin() + row + 1, string());
435 MathGridInset::addRow(row);
439 void MathHullInset::swapRow(row_type row)
443 if (row + 1 == nrows())
445 std::swap(nonum_[row], nonum_[row + 1]);
446 std::swap(label_[row], label_[row + 1]);
447 MathGridInset::swapRow(row);
451 void MathHullInset::delRow(row_type row)
455 MathGridInset::delRow(row);
456 nonum_.erase(nonum_.begin() + row);
457 label_.erase(label_.begin() + row);
461 void MathHullInset::addCol(col_type col)
464 MathGridInset::addCol(col);
466 lyxerr << "Can't change number of columns in '" << type_ << "'" << endl;
470 void MathHullInset::delCol(col_type col)
473 MathGridInset::delCol(col);
475 lyxerr << "Can't change number of columns in '" << type_ << "'" << endl;
479 string MathHullInset::nicelabel(row_type row) const
483 if (label_[row].empty())
484 return string("(#)");
485 return '(' + label_[row] + ')';
489 void MathHullInset::glueall()
492 for (idx_type i = 0; i < nargs(); ++i)
494 *this = MathHullInset("simple");
500 string const & MathHullInset::getType() const
506 void MathHullInset::setType(string const & type)
514 void MathHullInset::mutate(string const & newtype)
516 lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
518 // we try to move along the chain
519 // none <-> simple <-> equation <-> eqnarray
521 if (newtype == "dump") {
525 else if (newtype == type_) {
529 else if (type_ == "none") {
535 else if (type_ == "simple") {
536 if (newtype == "none") {
545 else if (type_ == "equation") {
546 if (smaller(newtype, type_)) {
549 } else if (newtype == "eqnarray") {
550 MathGridInset::addCol(1);
551 MathGridInset::addCol(1);
553 // split it "nicely" on the firest relop
554 pos_type pos = firstRelOp(cell(0));
555 cell(1) = MathArray(cell(0).begin() + pos, cell(0).end());
556 cell(0).erase(pos, cell(0).size());
558 if (cell(1).size()) {
559 cell(2) = MathArray(cell(1).begin() + 1, cell(1).end());
560 cell(1).erase(1, cell(1).size());
564 } else if (newtype == "multline" || newtype == "gather") {
568 MathGridInset::addCol(1);
570 pos_type pos = firstRelOp(cell(0));
572 cell(0).erase(pos, cell(0).size());
573 cell(1).erase(0, pos);
579 else if (type_ == "eqnarray") {
580 if (smaller(newtype, type_)) {
581 // set correct (no)numbering
582 bool allnonum = true;
583 for (row_type row = 0; row < nrows(); ++row)
587 // set first non-empty label
589 for (row_type row = 0; row < nrows(); ++row) {
590 if (!label_[row].empty()) {
597 nonum_[0] = allnonum;
600 } else { // align & Co.
601 for (row_type row = 0; row < nrows(); ++row) {
602 idx_type c = 3 * row + 1;
603 cell(c).append(cell(c + 1));
605 MathGridInset::delCol(2);
611 else if (type_ == "align") {
612 if (smaller(newtype, type_)) {
613 MathGridInset::addCol(1);
621 else if (type_ == "multline") {
622 if (newtype == "gather" || newtype == "align" ||
623 newtype == "xalignat" || newtype == "xxalignat" || newtype == "flalign")
625 else if (newtype == "eqnarray") {
626 MathGridInset::addCol(1);
627 MathGridInset::addCol(1);
630 lyxerr << "mutation from '" << type_
631 << "' to '" << newtype << "' not implemented" << endl;
635 else if (type_ == "gather") {
636 if (newtype == "multline") {
639 lyxerr << "mutation from '" << type_
640 << "' to '" << newtype << "' not implemented" << endl;
645 lyxerr << "mutation from '" << type_
646 << "' to '" << newtype << "' not implemented" << endl;
651 string MathHullInset::eolString(row_type row, bool fragile) const
654 if (numberedType()) {
655 if (!label_[row].empty() && !nonum_[row])
656 res += "\\label{" + label_[row] + '}';
657 if (nonum_[row] && (type_ != "multline"))
658 res += "\\nonumber ";
660 return res + MathGridInset::eolString(row, fragile);
664 void MathHullInset::write(WriteStream & os) const
667 MathGridInset::write(os);
672 void MathHullInset::normalize(NormalStream & os) const
674 os << "[formula " << type_ << ' ';
675 MathGridInset::normalize(os);
680 void MathHullInset::mathmlize(MathMLStream & os) const
682 MathGridInset::mathmlize(os);
686 void MathHullInset::infoize(std::ostream & os) const
688 os << "Type: " << type_;
692 void MathHullInset::check() const
694 BOOST_ASSERT(nonum_.size() == nrows());
695 BOOST_ASSERT(label_.size() == nrows());
699 void MathHullInset::doExtern(FuncRequest const & func, BufferView & bv)
701 CursorSlice & cur = cursorTip(bv);
704 istringstream iss(func.argument.c_str());
705 iss >> lang >> extra;
710 #warning temporarily disabled
714 // lyxerr << "use selection: " << ar << endl;
715 // insert(pipeThroughExtern(lang, extra, ar));
721 eq.push_back(MathAtom(new MathCharInset('=')));
723 // go to first item in line
724 cur.idx() -= cur.idx() % ncols();
727 if (getType() == "simple") {
728 size_type pos = cur.cell().find_last(eq);
730 if (mathcursor && mathcursor->selection()) {
731 asArray(mathcursor->grabAndEraseSelection(bv), ar);
732 } else if (pos == cur.cell().size()) {
734 lyxerr << "use whole cell: " << ar << endl;
736 ar = MathArray(cur.cell().begin() + pos + 1, cur.cell().end());
737 lyxerr << "use partial cell form pos: " << pos << endl;
739 cur.cell().append(eq);
740 cur.cell().append(pipeThroughExtern(lang, extra, ar));
741 cur.pos() = cur.lastpos();
745 if (getType() == "equation") {
746 lyxerr << "use equation inset" << endl;
748 MathArray & ar = cur.cell();
749 lyxerr << "use cell: " << ar << endl;
753 cur.cell() = pipeThroughExtern(lang, extra, ar);
754 // move to end of line
755 cur.pos() = cur.lastpos();
760 lyxerr << "use eqnarray" << endl;
761 cur.idx() += 2 - cur.idx() % ncols();
763 MathArray ar = cur.cell();
764 lyxerr << "use cell: " << ar << endl;
766 #warning temporarily disabled
773 cur.cell() = pipeThroughExtern(lang, extra, ar);
774 cur.pos() = cur.lastpos();
780 MathHullInset::priv_dispatch(BufferView & bv, FuncRequest const & cmd)
782 CursorSlice & cur = cursorTip(bv);
783 switch (cmd.action) {
786 if (type_ == "simple" || type_ == "equation") {
790 return DispatchResult(true, FINISHED);
792 return MathGridInset::priv_dispatch(bv, cmd);
794 case LFUN_MATH_NUMBER:
795 //lyxerr << "toggling all numbers" << endl;
797 //recordUndo(bv, Undo::INSERT);
798 bool old = numberedType();
799 if (type_ == "multline")
800 numbered(nrows() - 1, !old);
802 for (row_type row = 0; row < nrows(); ++row)
804 //bv->owner()->message(old ? _("No number") : _("Number"));
806 return DispatchResult(true, true);
808 case LFUN_MATH_NONUMBER:
810 row_type r = (type_ == "multline") ? nrows() - 1 : cur.row();
811 //recordUndo(bv, Undo::INSERT);
812 bool old = numbered(r);
813 //bv->owner()->message(old ? _("No number") : _("Number"));
816 return DispatchResult(true, true);
818 case LFUN_INSERT_LABEL: {
819 row_type r = (type_ == "multline") ? nrows() - 1 : cur.row();
820 string old_label = label(r);
821 string new_label = cmd.argument;
823 if (new_label.empty()) {
824 string const default_label =
825 (lyxrc.label_init_length >= 0) ? "eq:" : "";
826 pair<bool, string> const res = old_label.empty()
827 ? Alert::askForText(_("Enter new label to insert:"), default_label)
828 : Alert::askForText(_("Enter label:"), old_label);
830 return DispatchResult(false);
831 new_label = trim(res.second);
834 //if (new_label == old_label)
835 // break; // Nothing to do
837 if (!new_label.empty())
840 return DispatchResult(true, true);
843 case LFUN_MATH_EXTERN:
845 return DispatchResult(true, FINISHED);
847 case LFUN_MATH_MUTATE: {
848 lyxerr << "Hull: MUTATE: " << cmd.argument << endl;
849 row_type r = cur.row();
850 col_type c = cur.col();
851 mutate(cmd.argument);
852 cur.idx() = r * ncols() + c;
853 if (cur.idx() >= nargs())
854 cur.idx() = nargs() - 1;
855 if (cur.pos() > cur.lastpos())
856 cur.pos() = cur.lastpos();
857 return DispatchResult(true, FINISHED);
860 case LFUN_MATH_DISPLAY: {
861 mutate(type_ == "simple" ? "equation" : "simple");
863 cur.pos() = cur.lastpos();
864 return DispatchResult(true, FINISHED);
868 return MathGridInset::priv_dispatch(bv, cmd);
873 string MathHullInset::fileInsetLabel() const