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 "BufferView.h"
22 #include "dispatchresult.h"
24 #include "funcrequest.h"
26 #include "LaTeXFeatures.h"
30 #include "frontends/Alert.h"
32 #include "support/std_sstream.h"
35 using lyx::support::trim;
42 using std::istringstream;
43 using std::ostringstream;
49 int getCols(string const & type)
51 if (type == "eqnarray")
55 if (type == "flalign")
57 if (type == "alignat")
59 if (type == "xalignat")
61 if (type == "xxalignat")
67 // returns position of first relation operator in the array
68 // used for "intelligent splitting"
69 MathArray::size_type firstRelOp(MathArray const & ar)
71 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
73 return it - ar.begin();
78 char const * star(bool numbered)
80 return numbered ? "" : "*";
84 int typecode(string const & s)
86 if (s == "none") return 0;
87 if (s == "simple") return 1;
88 if (s == "equation") return 2;
89 if (s == "eqnarray") return 3;
90 if (s == "align") return 4;
91 if (s == "alignat") return 5;
92 if (s == "xalignat") return 6;
93 if (s == "xxalignat") return 7;
94 if (s == "multline") return 8;
95 if (s == "gather") return 9;
96 if (s == "flalign") return 10;
97 lyxerr << "unknown hull type '" << s << "'" << endl;
101 bool smaller(string const & s, string const & t)
103 return typecode(s) < typecode(t);
107 } // end anon namespace
110 MathHullInset::MathHullInset()
111 : MathGridInset(1, 1), type_("none"), nonum_(1), label_(1)
117 MathHullInset::MathHullInset(string const & type)
118 : MathGridInset(getCols(type), 1), type_(type), nonum_(1), label_(1)
124 auto_ptr<InsetBase> MathHullInset::clone() const
126 return auto_ptr<InsetBase>(new MathHullInset(*this));
130 MathInset::mode_type MathHullInset::currentMode() const
133 return UNDECIDED_MODE;
134 // definitely math mode ...
139 bool MathHullInset::idxFirst(LCursor & cur) const
147 bool MathHullInset::idxLast(LCursor & cur) const
149 cur.idx() = nargs() - 1;
150 cur.pos() = cur.lastpos();
155 char MathHullInset::defaultColAlign(col_type col)
157 if (type_ == "eqnarray")
159 if (typecode(type_) >= typecode("align"))
160 return "rl"[col & 1];
165 int MathHullInset::defaultColSpace(col_type col)
167 if (type_ == "align" || type_ == "alignat")
169 if (type_ == "xalignat")
170 return (col & 1) ? 20 : 0;
171 if (type_ == "xxalignat" || type_ == "flalign")
172 return (col & 1) ? 40 : 0;
177 char const * MathHullInset::standardFont() const
180 return "lyxnochange";
185 void MathHullInset::metrics(MetricsInfo & mi, Dimension & dim) const
187 FontSetChanger dummy1(mi.base, standardFont());
188 StyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
190 // let the cells adjust themselves
191 MathGridInset::metrics(mi);
198 if (numberedType()) {
199 FontSetChanger dummy(mi.base, "mathbf");
201 for (row_type row = 0; row < nrows(); ++row)
202 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
208 // make it at least as high as the current font
211 math_font_max_dim(mi.base.font, asc, des);
212 dim_.asc = max(dim_.asc, asc);
213 dim_.des = max(dim_.des, des);
221 void MathHullInset::draw(PainterInfo & pi, int x, int y) const
223 FontSetChanger dummy1(pi.base, standardFont());
224 StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
225 MathGridInset::draw(pi, x + 1, y);
227 if (numberedType()) {
228 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
229 for (row_type row = 0; row < nrows(); ++row) {
230 int const yy = y + rowinfo_[row].offset_;
231 FontSetChanger dummy(pi.base, "mathrm");
232 drawStr(pi, pi.base.font, xx, yy, nicelabel(row));
236 drawMarkers2(pi, x, y);
240 void MathHullInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
243 MathGridInset::metricsT(mi, dim);
246 WriteStream wi(os, false, true);
248 dim.wid = os.str().size();
255 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
258 MathGridInset::drawT(pain, x, y);
261 WriteStream wi(os, false, true);
263 pain.draw(x, y, os.str().c_str());
268 string MathHullInset::label(row_type row) const
270 row_type n = nrows();
271 BOOST_ASSERT(row < n);
276 void MathHullInset::label(row_type row, string const & label)
278 //lyxerr << "setting label '" << label << "' for row " << row << endl;
283 void MathHullInset::numbered(row_type row, bool num)
289 bool MathHullInset::numbered(row_type row) const
295 bool MathHullInset::ams() const
299 type_ == "flalign" ||
300 type_ == "multline" ||
302 type_ == "alignat" ||
303 type_ == "xalignat" ||
304 type_ == "xxalignat";
308 bool MathHullInset::display() const
310 return type_ != "simple" && type_ != "none";
314 void MathHullInset::getLabelList(Buffer const &,
315 std::vector<string> & labels) const
317 for (row_type row = 0; row < nrows(); ++row)
318 if (!label_[row].empty() && nonum_[row] != 1)
319 labels.push_back(label_[row]);
323 bool MathHullInset::numberedType() const
327 if (type_ == "simple")
329 if (type_ == "xxalignat")
331 for (row_type row = 0; row < nrows(); ++row)
338 void MathHullInset::validate(LaTeXFeatures & features) const
341 features.require("amsmath");
344 // Validation is necessary only if not using AMS math.
345 // To be safe, we will always run mathedvalidate.
346 //if (features.amsstyle)
349 features.require("boldsymbol");
350 //features.binom = true;
352 MathGridInset::validate(features);
356 void MathHullInset::header_write(WriteStream & os) const
358 bool n = numberedType();
363 else if (type_ == "simple") {
369 else if (type_ == "equation") {
371 os << "\\begin{equation" << star(n) << "}\n";
376 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
377 || type_ == "gather" || type_ == "multline")
378 os << "\\begin{" << type_ << star(n) << "}\n";
380 else if (type_ == "alignat" || type_ == "xalignat")
381 os << "\\begin{" << type_ << star(n) << '}'
382 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
384 else if (type_ == "xxalignat")
385 os << "\\begin{" << type_ << '}'
386 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
389 os << "\\begin{unknown" << star(n) << '}';
393 void MathHullInset::footer_write(WriteStream & os) const
395 bool n = numberedType();
400 else if (type_ == "simple")
403 else if (type_ == "equation")
405 os << "\\end{equation" << star(n) << "}\n";
409 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
410 || type_ == "alignat" || type_ == "xalignat"
411 || type_ == "gather" || type_ == "multline")
412 os << "\\end{" << type_ << star(n) << "}\n";
414 else if (type_ == "xxalignat")
415 os << "\\end{" << type_ << "}\n";
418 os << "\\end{unknown" << star(n) << '}';
422 bool MathHullInset::colChangeOK() const
425 type_ == "align" || type_ == "flalign" ||type_ == "alignat" ||
426 type_ == "xalignat" || type_ == "xxalignat";
430 void MathHullInset::addRow(row_type row)
432 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
433 label_.insert(label_.begin() + row + 1, string());
434 MathGridInset::addRow(row);
438 void MathHullInset::swapRow(row_type row)
442 if (row + 1 == nrows())
444 std::swap(nonum_[row], nonum_[row + 1]);
445 std::swap(label_[row], label_[row + 1]);
446 MathGridInset::swapRow(row);
450 void MathHullInset::delRow(row_type row)
454 MathGridInset::delRow(row);
455 nonum_.erase(nonum_.begin() + row);
456 label_.erase(label_.begin() + row);
460 void MathHullInset::addCol(col_type col)
463 MathGridInset::addCol(col);
465 lyxerr << "Can't change number of columns in '" << type_ << "'" << endl;
469 void MathHullInset::delCol(col_type col)
472 MathGridInset::delCol(col);
474 lyxerr << "Can't change number of columns in '" << type_ << "'" << endl;
478 string MathHullInset::nicelabel(row_type row) const
482 if (label_[row].empty())
483 return string("(#)");
484 return '(' + label_[row] + ')';
488 void MathHullInset::glueall()
491 for (idx_type i = 0; i < nargs(); ++i)
493 *this = MathHullInset("simple");
499 string const & MathHullInset::getType() const
505 void MathHullInset::setType(string const & type)
513 void MathHullInset::mutate(string const & newtype)
515 lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
517 // we try to move along the chain
518 // none <-> simple <-> equation <-> eqnarray
520 if (newtype == "dump") {
524 else if (newtype == type_) {
528 else if (type_ == "none") {
534 else if (type_ == "simple") {
535 if (newtype == "none") {
544 else if (type_ == "equation") {
545 if (smaller(newtype, type_)) {
548 } else if (newtype == "eqnarray") {
549 MathGridInset::addCol(1);
550 MathGridInset::addCol(1);
552 // split it "nicely" on the firest relop
553 pos_type pos = firstRelOp(cell(0));
554 cell(1) = MathArray(cell(0).begin() + pos, cell(0).end());
555 cell(0).erase(pos, cell(0).size());
557 if (cell(1).size()) {
558 cell(2) = MathArray(cell(1).begin() + 1, cell(1).end());
559 cell(1).erase(1, cell(1).size());
563 } else if (newtype == "multline" || newtype == "gather") {
567 MathGridInset::addCol(1);
569 pos_type pos = firstRelOp(cell(0));
571 cell(0).erase(pos, cell(0).size());
572 cell(1).erase(0, pos);
578 else if (type_ == "eqnarray") {
579 if (smaller(newtype, type_)) {
580 // set correct (no)numbering
581 bool allnonum = true;
582 for (row_type row = 0; row < nrows(); ++row)
586 // set first non-empty label
588 for (row_type row = 0; row < nrows(); ++row) {
589 if (!label_[row].empty()) {
596 nonum_[0] = allnonum;
599 } else { // align & Co.
600 for (row_type row = 0; row < nrows(); ++row) {
601 idx_type c = 3 * row + 1;
602 cell(c).append(cell(c + 1));
604 MathGridInset::delCol(2);
610 else if (type_ == "align") {
611 if (smaller(newtype, type_)) {
612 MathGridInset::addCol(1);
620 else if (type_ == "multline") {
621 if (newtype == "gather" || newtype == "align" ||
622 newtype == "xalignat" || newtype == "xxalignat" || newtype == "flalign")
624 else if (newtype == "eqnarray") {
625 MathGridInset::addCol(1);
626 MathGridInset::addCol(1);
629 lyxerr << "mutation from '" << type_
630 << "' to '" << newtype << "' not implemented" << endl;
634 else if (type_ == "gather") {
635 if (newtype == "multline") {
638 lyxerr << "mutation from '" << type_
639 << "' to '" << newtype << "' not implemented" << endl;
644 lyxerr << "mutation from '" << type_
645 << "' to '" << newtype << "' not implemented" << endl;
650 string MathHullInset::eolString(row_type row, bool fragile) const
653 if (numberedType()) {
654 if (!label_[row].empty() && !nonum_[row])
655 res += "\\label{" + label_[row] + '}';
656 if (nonum_[row] && (type_ != "multline"))
657 res += "\\nonumber ";
659 return res + MathGridInset::eolString(row, fragile);
663 void MathHullInset::write(WriteStream & os) const
666 MathGridInset::write(os);
671 void MathHullInset::normalize(NormalStream & os) const
673 os << "[formula " << type_ << ' ';
674 MathGridInset::normalize(os);
679 void MathHullInset::mathmlize(MathMLStream & os) const
681 MathGridInset::mathmlize(os);
685 void MathHullInset::infoize(std::ostream & os) const
687 os << "Type: " << type_;
691 void MathHullInset::check() const
693 BOOST_ASSERT(nonum_.size() == nrows());
694 BOOST_ASSERT(label_.size() == nrows());
698 void MathHullInset::doExtern(LCursor & cur, FuncRequest const & func)
702 istringstream iss(func.argument.c_str());
703 iss >> lang >> extra;
708 #warning temporarily disabled
709 //if (cur.selection()) {
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 cur.idx() -= cur.idx() % ncols();
725 if (getType() == "simple") {
726 size_type pos = cur.cell().find_last(eq);
728 if (inMathed() && cur.selection()) {
729 asArray(mathcursor::grabAndEraseSelection(cur), ar);
730 } else if (pos == cur.cell().size()) {
732 lyxerr << "use whole cell: " << ar << endl;
734 ar = MathArray(cur.cell().begin() + pos + 1, cur.cell().end());
735 lyxerr << "use partial cell form pos: " << pos << endl;
737 cur.cell().append(eq);
738 cur.cell().append(pipeThroughExtern(lang, extra, ar));
739 cur.pos() = cur.lastpos();
743 if (getType() == "equation") {
744 lyxerr << "use equation inset" << endl;
746 MathArray & ar = cur.cell();
747 lyxerr << "use cell: " << ar << endl;
751 cur.cell() = pipeThroughExtern(lang, extra, ar);
752 // move to end of line
753 cur.pos() = cur.lastpos();
758 lyxerr << "use eqnarray" << endl;
759 cur.idx() += 2 - cur.idx() % ncols();
761 MathArray ar = cur.cell();
762 lyxerr << "use cell: " << ar << endl;
764 #warning temporarily disabled
771 cur.cell() = pipeThroughExtern(lang, extra, ar);
772 cur.pos() = cur.lastpos();
778 MathHullInset::priv_dispatch(LCursor & cur, FuncRequest const & cmd)
780 switch (cmd.action) {
783 if (type_ == "simple" || type_ == "equation") {
787 return DispatchResult(true, FINISHED);
789 return MathGridInset::priv_dispatch(cur, cmd);
791 case LFUN_MATH_NUMBER:
792 //lyxerr << "toggling all numbers" << endl;
794 //recordUndo(cur, Undo::INSERT);
795 bool old = numberedType();
796 if (type_ == "multline")
797 numbered(nrows() - 1, !old);
799 for (row_type row = 0; row < nrows(); ++row)
801 //cur.bv()->owner()->message(old ? _("No number") : _("Number"));
803 return DispatchResult(true, true);
805 case LFUN_MATH_NONUMBER:
807 row_type r = (type_ == "multline") ? nrows() - 1 : cur.row();
808 //recordUndo(cur, Undo::INSERT);
809 bool old = numbered(r);
810 //cur.bv()->owner()->message(old ? _("No number") : _("Number"));
813 return DispatchResult(true, true);
815 case LFUN_INSERT_LABEL: {
816 row_type r = (type_ == "multline") ? nrows() - 1 : cur.row();
817 string old_label = label(r);
818 string new_label = cmd.argument;
820 if (new_label.empty()) {
821 string const default_label =
822 (lyxrc.label_init_length >= 0) ? "eq:" : "";
823 pair<bool, string> const res = old_label.empty()
824 ? Alert::askForText(_("Enter new label to insert:"), default_label)
825 : Alert::askForText(_("Enter label:"), old_label);
827 return DispatchResult(false);
828 new_label = trim(res.second);
831 //if (new_label == old_label)
832 // break; // Nothing to do
834 if (!new_label.empty())
837 return DispatchResult(true, true);
840 case LFUN_MATH_EXTERN:
842 return DispatchResult(true, FINISHED);
844 case LFUN_MATH_MUTATE: {
845 lyxerr << "Hull: MUTATE: " << cmd.argument << endl;
846 row_type r = cur.row();
847 col_type c = cur.col();
848 mutate(cmd.argument);
849 cur.idx() = r * ncols() + c;
850 if (cur.idx() >= nargs())
851 cur.idx() = nargs() - 1;
852 if (cur.pos() > cur.lastpos())
853 cur.pos() = cur.lastpos();
854 return DispatchResult(true, FINISHED);
857 case LFUN_MATH_DISPLAY: {
858 mutate(type_ == "simple" ? "equation" : "simple");
860 cur.pos() = cur.lastpos();
861 return DispatchResult(true, FINISHED);
865 return MathGridInset::priv_dispatch(cur, cmd);
870 string MathHullInset::fileInsetLabel() const