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(idx_type & idx, pos_type & pos) const
146 bool MathHullInset::idxLast(idx_type & idx, pos_type & pos) const
149 pos = cell(idx).size();
154 char MathHullInset::defaultColAlign(col_type col)
156 if (type_ == "eqnarray")
158 if (typecode(type_) >= typecode("align"))
159 return "rl"[col & 1];
164 int MathHullInset::defaultColSpace(col_type col)
166 if (type_ == "align" || type_ == "alignat")
168 if (type_ == "xalignat")
169 return (col & 1) ? 20 : 0;
170 if (type_ == "xxalignat" || type_ == "flalign")
171 return (col & 1) ? 40 : 0;
176 char const * MathHullInset::standardFont() const
179 return "lyxnochange";
184 void MathHullInset::metrics(MetricsInfo & mi, Dimension & dim) const
186 FontSetChanger dummy1(mi.base, standardFont());
187 StyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
189 // let the cells adjust themselves
190 MathGridInset::metrics(mi);
197 if (numberedType()) {
198 FontSetChanger dummy(mi.base, "mathbf");
200 for (row_type row = 0; row < nrows(); ++row)
201 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
207 // make it at least as high as the current font
210 math_font_max_dim(mi.base.font, asc, des);
211 dim_.asc = max(dim_.asc, asc);
212 dim_.des = max(dim_.des, des);
220 void MathHullInset::draw(PainterInfo & pi, int x, int y) const
222 FontSetChanger dummy1(pi.base, standardFont());
223 StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
224 MathGridInset::draw(pi, x + 1, y);
226 if (numberedType()) {
227 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
228 for (row_type row = 0; row < nrows(); ++row) {
229 int const yy = y + rowinfo_[row].offset_;
230 FontSetChanger dummy(pi.base, "mathrm");
231 drawStr(pi, pi.base.font, xx, yy, nicelabel(row));
235 drawMarkers2(pi, x, y);
239 void MathHullInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
242 MathGridInset::metricsT(mi, dim);
245 WriteStream wi(os, false, true);
247 dim.wid = os.str().size();
254 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
257 MathGridInset::drawT(pain, x, y);
260 WriteStream wi(os, false, true);
262 pain.draw(x, y, os.str().c_str());
267 string MathHullInset::label(row_type row) const
269 row_type n = nrows();
270 BOOST_ASSERT(row < n);
275 void MathHullInset::label(row_type row, string const & label)
277 //lyxerr << "setting label '" << label << "' for row " << row << endl;
282 void MathHullInset::numbered(row_type row, bool num)
288 bool MathHullInset::numbered(row_type row) const
294 bool MathHullInset::ams() const
298 type_ == "flalign" ||
299 type_ == "multline" ||
301 type_ == "alignat" ||
302 type_ == "xalignat" ||
303 type_ == "xxalignat";
307 bool MathHullInset::display() const
309 return type_ != "simple" && type_ != "none";
313 void MathHullInset::getLabelList(Buffer const &,
314 std::vector<string> & labels) const
316 for (row_type row = 0; row < nrows(); ++row)
317 if (!label_[row].empty() && nonum_[row] != 1)
318 labels.push_back(label_[row]);
322 bool MathHullInset::numberedType() const
326 if (type_ == "simple")
328 if (type_ == "xxalignat")
330 for (row_type row = 0; row < nrows(); ++row)
337 void MathHullInset::validate(LaTeXFeatures & features) const
340 features.require("amsmath");
343 // Validation is necessary only if not using AMS math.
344 // To be safe, we will always run mathedvalidate.
345 //if (features.amsstyle)
348 features.require("boldsymbol");
349 //features.binom = true;
351 MathGridInset::validate(features);
355 void MathHullInset::header_write(WriteStream & os) const
357 bool n = numberedType();
362 else if (type_ == "simple") {
368 else if (type_ == "equation") {
370 os << "\\begin{equation" << star(n) << "}\n";
375 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
376 || type_ == "gather" || type_ == "multline")
377 os << "\\begin{" << type_ << star(n) << "}\n";
379 else if (type_ == "alignat" || type_ == "xalignat")
380 os << "\\begin{" << type_ << star(n) << '}'
381 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
383 else if (type_ == "xxalignat")
384 os << "\\begin{" << type_ << '}'
385 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
388 os << "\\begin{unknown" << star(n) << '}';
392 void MathHullInset::footer_write(WriteStream & os) const
394 bool n = numberedType();
399 else if (type_ == "simple")
402 else if (type_ == "equation")
404 os << "\\end{equation" << star(n) << "}\n";
408 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
409 || type_ == "alignat" || type_ == "xalignat"
410 || type_ == "gather" || type_ == "multline")
411 os << "\\end{" << type_ << star(n) << "}\n";
413 else if (type_ == "xxalignat")
414 os << "\\end{" << type_ << "}\n";
417 os << "\\end{unknown" << star(n) << '}';
421 bool MathHullInset::colChangeOK() const
424 type_ == "align" || type_ == "flalign" ||type_ == "alignat" ||
425 type_ == "xalignat" || type_ == "xxalignat";
429 void MathHullInset::addRow(row_type row)
431 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
432 label_.insert(label_.begin() + row + 1, string());
433 MathGridInset::addRow(row);
437 void MathHullInset::swapRow(row_type row)
441 if (row + 1 == nrows())
443 std::swap(nonum_[row], nonum_[row + 1]);
444 std::swap(label_[row], label_[row + 1]);
445 MathGridInset::swapRow(row);
449 void MathHullInset::delRow(row_type row)
453 MathGridInset::delRow(row);
454 nonum_.erase(nonum_.begin() + row);
455 label_.erase(label_.begin() + row);
459 void MathHullInset::addCol(col_type col)
462 MathGridInset::addCol(col);
464 lyxerr << "Can't change number of columns in '" << type_ << "'" << endl;
468 void MathHullInset::delCol(col_type col)
471 MathGridInset::delCol(col);
473 lyxerr << "Can't change number of columns in '" << type_ << "'" << endl;
477 string MathHullInset::nicelabel(row_type row) const
481 if (label_[row].empty())
482 return string("(#)");
483 return '(' + label_[row] + ')';
487 void MathHullInset::glueall()
490 for (idx_type i = 0; i < nargs(); ++i)
492 *this = MathHullInset("simple");
498 string const & MathHullInset::getType() const
504 void MathHullInset::setType(string const & type)
512 void MathHullInset::mutate(string const & newtype)
514 lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
516 // we try to move along the chain
517 // none <-> simple <-> equation <-> eqnarray
519 if (newtype == "dump") {
523 else if (newtype == type_) {
527 else if (type_ == "none") {
533 else if (type_ == "simple") {
534 if (newtype == "none") {
543 else if (type_ == "equation") {
544 if (smaller(newtype, type_)) {
547 } else if (newtype == "eqnarray") {
548 MathGridInset::addCol(1);
549 MathGridInset::addCol(1);
551 // split it "nicely" on the firest relop
552 pos_type pos = firstRelOp(cell(0));
553 cell(1) = MathArray(cell(0).begin() + pos, cell(0).end());
554 cell(0).erase(pos, cell(0).size());
556 if (cell(1).size()) {
557 cell(2) = MathArray(cell(1).begin() + 1, cell(1).end());
558 cell(1).erase(1, cell(1).size());
562 } else if (newtype == "multline" || newtype == "gather") {
566 MathGridInset::addCol(1);
568 pos_type pos = firstRelOp(cell(0));
570 cell(0).erase(pos, cell(0).size());
571 cell(1).erase(0, pos);
577 else if (type_ == "eqnarray") {
578 if (smaller(newtype, type_)) {
579 // set correct (no)numbering
580 bool allnonum = true;
581 for (row_type row = 0; row < nrows(); ++row)
585 // set first non-empty label
587 for (row_type row = 0; row < nrows(); ++row) {
588 if (!label_[row].empty()) {
595 nonum_[0] = allnonum;
598 } else { // align & Co.
599 for (row_type row = 0; row < nrows(); ++row) {
600 idx_type c = 3 * row + 1;
601 cell(c).append(cell(c + 1));
603 MathGridInset::delCol(2);
609 else if (type_ == "align") {
610 if (smaller(newtype, type_)) {
611 MathGridInset::addCol(1);
619 else if (type_ == "multline") {
620 if (newtype == "gather" || newtype == "align" ||
621 newtype == "xalignat" || newtype == "xxalignat" || newtype == "flalign")
623 else if (newtype == "eqnarray") {
624 MathGridInset::addCol(1);
625 MathGridInset::addCol(1);
628 lyxerr << "mutation from '" << type_
629 << "' to '" << newtype << "' not implemented" << endl;
633 else if (type_ == "gather") {
634 if (newtype == "multline") {
637 lyxerr << "mutation from '" << type_
638 << "' to '" << newtype << "' not implemented" << endl;
643 lyxerr << "mutation from '" << type_
644 << "' to '" << newtype << "' not implemented" << endl;
649 string MathHullInset::eolString(row_type row, bool fragile) const
652 if (numberedType()) {
653 if (!label_[row].empty() && !nonum_[row])
654 res += "\\label{" + label_[row] + '}';
655 if (nonum_[row] && (type_ != "multline"))
656 res += "\\nonumber ";
658 return res + MathGridInset::eolString(row, fragile);
662 void MathHullInset::write(WriteStream & os) const
665 MathGridInset::write(os);
670 void MathHullInset::normalize(NormalStream & os) const
672 os << "[formula " << type_ << ' ';
673 MathGridInset::normalize(os);
678 void MathHullInset::mathmlize(MathMLStream & os) const
680 MathGridInset::mathmlize(os);
684 void MathHullInset::infoize(std::ostream & os) const
686 os << "Type: " << type_;
690 void MathHullInset::check() const
692 BOOST_ASSERT(nonum_.size() == nrows());
693 BOOST_ASSERT(label_.size() == nrows());
697 void MathHullInset::doExtern
698 (FuncRequest const & func, idx_type & idx, pos_type & pos)
702 istringstream iss(func.argument.c_str());
703 iss >> lang >> extra;
708 #warning temporarily disabled
712 // lyxerr << "use selection: " << ar << endl;
713 // insert(pipeThroughExtern(lang, extra, ar));
719 eq.push_back(MathAtom(new MathCharInset('=')));
721 // go to first item in line
722 idx -= idx % ncols();
725 if (getType() == "simple") {
726 size_type pos = cell(idx).find_last(eq);
728 if (mathcursor && mathcursor->selection()) {
729 asArray(mathcursor->grabAndEraseSelection(), ar);
730 } else if (pos == cell(idx).size()) {
732 lyxerr << "use whole cell: " << ar << endl;
734 ar = MathArray(cell(idx).begin() + pos + 1, cell(idx).end());
735 lyxerr << "use partial cell form pos: " << pos << endl;
737 cell(idx).append(eq);
738 cell(idx).append(pipeThroughExtern(lang, extra, ar));
739 pos = cell(idx).size();
743 if (getType() == "equation") {
744 lyxerr << "use equation inset" << endl;
746 MathArray & ar = cell(idx);
747 lyxerr << "use cell: " << ar << endl;
749 cell(idx + 2) = pipeThroughExtern(lang, extra, ar);
750 // move to end of line
752 pos = cell(idx).size();
757 lyxerr << "use eqnarray" << endl;
758 idx -= idx % ncols();
761 MathArray ar = cell(idx);
762 lyxerr << "use cell: " << ar << endl;
764 #warning temporarily disabled
768 cell(idx + 3) = pipeThroughExtern(lang, extra, ar);
770 pos = cell(idx).size();
775 DispatchResult MathHullInset::priv_dispatch
776 (FuncRequest const & cmd, idx_type & idx, pos_type & pos)
778 switch (cmd.action) {
781 if (type_ == "simple" || type_ == "equation") {
785 return DispatchResult(true, FINISHED);
787 return MathGridInset::priv_dispatch(cmd, idx, pos);
789 case LFUN_MATH_NUMBER:
790 //lyxerr << "toggling all numbers" << endl;
792 //recordUndo(bv, Undo::INSERT);
793 bool old = numberedType();
794 if (type_ == "multline")
795 numbered(nrows() - 1, !old);
797 for (row_type row = 0; row < nrows(); ++row)
799 //bv->owner()->message(old ? _("No number") : _("Number"));
801 return DispatchResult(true, true);
803 case LFUN_MATH_NONUMBER:
805 row_type r = (type_ == "multline") ? nrows() - 1 : row(idx);
806 //recordUndo(bv, Undo::INSERT);
807 bool old = numbered(r);
808 //bv->owner()->message(old ? _("No number") : _("Number"));
811 return DispatchResult(true, true);
813 case LFUN_INSERT_LABEL: {
814 row_type r = (type_ == "multline") ? nrows() - 1 : row(idx);
815 string old_label = label(r);
816 string new_label = cmd.argument;
818 if (new_label.empty()) {
819 string const default_label =
820 (lyxrc.label_init_length >= 0) ? "eq:" : "";
821 pair<bool, string> const res = old_label.empty()
822 ? Alert::askForText(_("Enter new label to insert:"), default_label)
823 : Alert::askForText(_("Enter label:"), old_label);
825 return DispatchResult(false);
826 new_label = trim(res.second);
829 //if (new_label == old_label)
830 // break; // Nothing to do
832 if (!new_label.empty())
835 return DispatchResult(true, true);
838 case LFUN_MATH_EXTERN:
839 doExtern(cmd, idx, pos);
840 return DispatchResult(true, FINISHED);
842 case LFUN_MATH_MUTATE: {
843 lyxerr << "Hull: MUTATE: " << cmd.argument << endl;
844 row_type r = row(idx);
845 col_type c = col(idx);
846 mutate(cmd.argument);
847 idx = r * ncols() + c;
850 if (pos > cell(idx).size())
851 pos = cell(idx).size();
852 return DispatchResult(true, FINISHED);
855 case LFUN_MATH_DISPLAY: {
856 mutate(type_ == "simple" ? "equation" : "simple");
858 pos = cell(idx).size();
859 return DispatchResult(true, FINISHED);
863 return MathGridInset::priv_dispatch(cmd, idx, pos);
868 string MathHullInset::fileInsetLabel() const