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_charinset.h"
14 #include "math_data.h"
15 #include "math_extern.h"
16 #include "math_hullinset.h"
17 #include "math_mathmlstream.h"
18 #include "math_streamstr.h"
19 #include "math_support.h"
21 #include "BufferView.h"
23 #include "dispatchresult.h"
25 #include "funcrequest.h"
27 #include "LaTeXFeatures.h"
30 #include "outputparams.h"
31 #include "textpainter.h"
34 #include "frontends/Alert.h"
36 #include "support/std_sstream.h"
44 using std::istringstream;
46 using std::ostringstream;
54 int getCols(string const & type)
56 if (type == "eqnarray")
60 if (type == "flalign")
62 if (type == "alignat")
64 if (type == "xalignat")
66 if (type == "xxalignat")
72 // returns position of first relation operator in the array
73 // used for "intelligent splitting"
74 size_t firstRelOp(MathArray const & ar)
76 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
78 return it - ar.begin();
83 char const * star(bool numbered)
85 return numbered ? "" : "*";
89 int typecode(string const & s)
91 if (s == "none") return 0;
92 if (s == "simple") return 1;
93 if (s == "equation") return 2;
94 if (s == "eqnarray") return 3;
95 if (s == "align") return 4;
96 if (s == "alignat") return 5;
97 if (s == "xalignat") return 6;
98 if (s == "xxalignat") return 7;
99 if (s == "multline") return 8;
100 if (s == "gather") return 9;
101 if (s == "flalign") return 10;
102 lyxerr << "unknown hull type '" << s << "'" << endl;
106 bool smaller(string const & s, string const & t)
108 return typecode(s) < typecode(t);
112 } // end anon namespace
116 MathHullInset::MathHullInset()
117 : MathGridInset(1, 1), type_("none"), nonum_(1), label_(1)
119 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << endl;
120 //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl;
121 //lyxerr << "sizeof MathCharInset: " << sizeof(MathCharInset) << endl;
122 //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << endl;
127 MathHullInset::MathHullInset(string const & type)
128 : MathGridInset(getCols(type), 1), type_(type), nonum_(1), label_(1)
134 auto_ptr<InsetBase> MathHullInset::clone() const
136 return auto_ptr<InsetBase>(new MathHullInset(*this));
140 MathInset::mode_type MathHullInset::currentMode() const
143 return UNDECIDED_MODE;
144 // definitely math mode ...
149 bool MathHullInset::idxFirst(LCursor & cur) const
157 bool MathHullInset::idxLast(LCursor & cur) const
159 cur.idx() = nargs() - 1;
160 cur.pos() = cur.lastpos();
165 char MathHullInset::defaultColAlign(col_type col)
167 if (type_ == "eqnarray")
169 if (typecode(type_) >= typecode("align"))
170 return "rl"[col & 1];
175 int MathHullInset::defaultColSpace(col_type col)
177 if (type_ == "align" || type_ == "alignat")
179 if (type_ == "xalignat")
180 return (col & 1) ? 20 : 0;
181 if (type_ == "xxalignat" || type_ == "flalign")
182 return (col & 1) ? 40 : 0;
187 char const * MathHullInset::standardFont() const
190 return "lyxnochange";
195 void MathHullInset::metrics(MetricsInfo & mi, Dimension & dim) const
197 FontSetChanger dummy1(mi.base, standardFont());
198 StyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
200 // let the cells adjust themselves
201 MathGridInset::metrics(mi, dim);
208 if (numberedType()) {
209 FontSetChanger dummy(mi.base, "mathbf");
211 for (row_type row = 0; row < nrows(); ++row)
212 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
218 // make it at least as high as the current font
221 math_font_max_dim(mi.base.font, asc, des);
222 dim.asc = max(dim.asc, asc);
223 dim.des = max(dim.des, des);
229 void MathHullInset::draw(PainterInfo & pi, int x, int y) const
231 FontSetChanger dummy1(pi.base, standardFont());
232 StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
233 MathGridInset::draw(pi, x + 1, y);
235 if (numberedType()) {
236 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
237 for (row_type row = 0; row < nrows(); ++row) {
238 int const yy = y + rowinfo_[row].offset_;
239 FontSetChanger dummy(pi.base, "mathrm");
240 drawStr(pi, pi.base.font, xx, yy, nicelabel(row));
243 setPosCache(pi, x, y);
247 void MathHullInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
250 MathGridInset::metricsT(mi, dim);
253 WriteStream wi(os, false, true);
255 dim.wid = os.str().size();
262 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
265 MathGridInset::drawT(pain, x, y);
268 WriteStream wi(os, false, true);
270 pain.draw(x, y, os.str().c_str());
275 string MathHullInset::label(row_type row) const
277 row_type n = nrows();
278 BOOST_ASSERT(row < n);
283 void MathHullInset::label(row_type row, string const & label)
285 //lyxerr << "setting label '" << label << "' for row " << row << endl;
290 void MathHullInset::numbered(row_type row, bool num)
296 bool MathHullInset::numbered(row_type row) const
302 bool MathHullInset::ams() const
306 type_ == "flalign" ||
307 type_ == "multline" ||
309 type_ == "alignat" ||
310 type_ == "xalignat" ||
311 type_ == "xxalignat";
315 bool MathHullInset::display() const
317 return type_ != "simple" && type_ != "none";
321 void MathHullInset::getLabelList(Buffer const &, vector<string> & labels) const
323 for (row_type row = 0; row < nrows(); ++row)
324 if (!label_[row].empty() && nonum_[row] != 1)
325 labels.push_back(label_[row]);
329 bool MathHullInset::numberedType() const
333 if (type_ == "simple")
335 if (type_ == "xxalignat")
337 for (row_type row = 0; row < nrows(); ++row)
344 void MathHullInset::validate(LaTeXFeatures & features) const
347 features.require("amsmath");
350 // Validation is necessary only if not using AMS math.
351 // To be safe, we will always run mathedvalidate.
352 //if (features.amsstyle)
355 features.require("boldsymbol");
356 //features.binom = true;
358 MathGridInset::validate(features);
362 void MathHullInset::header_write(WriteStream & os) const
364 bool n = numberedType();
369 else if (type_ == "simple") {
375 else if (type_ == "equation") {
377 os << "\\begin{equation" << star(n) << "}\n";
382 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
383 || type_ == "gather" || type_ == "multline")
384 os << "\\begin{" << type_ << star(n) << "}\n";
386 else if (type_ == "alignat" || type_ == "xalignat")
387 os << "\\begin{" << type_ << star(n) << '}'
388 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
390 else if (type_ == "xxalignat")
391 os << "\\begin{" << type_ << '}'
392 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
395 os << "\\begin{unknown" << star(n) << '}';
399 void MathHullInset::footer_write(WriteStream & os) const
401 bool n = numberedType();
406 else if (type_ == "simple")
409 else if (type_ == "equation")
411 os << "\\end{equation" << star(n) << "}\n";
415 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
416 || type_ == "alignat" || type_ == "xalignat"
417 || type_ == "gather" || type_ == "multline")
418 os << "\\end{" << type_ << star(n) << "}\n";
420 else if (type_ == "xxalignat")
421 os << "\\end{" << type_ << "}\n";
424 os << "\\end{unknown" << star(n) << '}';
428 bool MathHullInset::colChangeOK() const
431 type_ == "align" || type_ == "flalign" ||type_ == "alignat" ||
432 type_ == "xalignat" || type_ == "xxalignat";
436 void MathHullInset::addRow(row_type row)
438 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
439 label_.insert(label_.begin() + row + 1, string());
440 MathGridInset::addRow(row);
444 void MathHullInset::swapRow(row_type row)
448 if (row + 1 == nrows())
450 swap(nonum_[row], nonum_[row + 1]);
451 swap(label_[row], label_[row + 1]);
452 MathGridInset::swapRow(row);
456 void MathHullInset::delRow(row_type row)
460 MathGridInset::delRow(row);
461 nonum_.erase(nonum_.begin() + row);
462 label_.erase(label_.begin() + row);
466 void MathHullInset::addCol(col_type col)
469 MathGridInset::addCol(col);
471 lyxerr << "Can't change number of columns in '" << type_ << "'" << endl;
475 void MathHullInset::delCol(col_type col)
478 MathGridInset::delCol(col);
480 lyxerr << "Can't change number of columns in '" << type_ << "'" << endl;
484 string MathHullInset::nicelabel(row_type row) const
488 if (label_[row].empty())
489 return string("(#)");
490 return '(' + label_[row] + ')';
494 void MathHullInset::glueall()
497 for (idx_type i = 0; i < nargs(); ++i)
499 *this = MathHullInset("simple");
505 string const & MathHullInset::getType() const
511 void MathHullInset::setType(string const & type)
519 void MathHullInset::mutate(string const & newtype)
521 lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
523 // we try to move along the chain
524 // none <-> simple <-> equation <-> eqnarray
526 if (newtype == "dump") {
530 else if (newtype == type_) {
534 else if (type_ == "none") {
540 else if (type_ == "simple") {
541 if (newtype == "none") {
550 else if (type_ == "equation") {
551 if (smaller(newtype, type_)) {
554 } else if (newtype == "eqnarray") {
555 MathGridInset::addCol(1);
556 MathGridInset::addCol(1);
558 // split it "nicely" on the firest relop
559 pos_type pos = firstRelOp(cell(0));
560 cell(1) = MathArray(cell(0).begin() + pos, cell(0).end());
561 cell(0).erase(pos, cell(0).size());
563 if (cell(1).size()) {
564 cell(2) = MathArray(cell(1).begin() + 1, cell(1).end());
565 cell(1).erase(1, cell(1).size());
569 } else if (newtype == "multline" || newtype == "gather") {
573 MathGridInset::addCol(1);
575 pos_type pos = firstRelOp(cell(0));
577 cell(0).erase(pos, cell(0).size());
578 cell(1).erase(0, pos);
584 else if (type_ == "eqnarray") {
585 if (smaller(newtype, type_)) {
586 // set correct (no)numbering
587 bool allnonum = true;
588 for (row_type row = 0; row < nrows(); ++row)
592 // set first non-empty label
594 for (row_type row = 0; row < nrows(); ++row) {
595 if (!label_[row].empty()) {
602 nonum_[0] = allnonum;
605 } else { // align & Co.
606 for (row_type row = 0; row < nrows(); ++row) {
607 idx_type c = 3 * row + 1;
608 cell(c).append(cell(c + 1));
610 MathGridInset::delCol(2);
616 else if (type_ == "align") {
617 if (smaller(newtype, type_)) {
618 MathGridInset::addCol(1);
626 else if (type_ == "multline") {
627 if (newtype == "gather" || newtype == "align" ||
628 newtype == "xalignat" || newtype == "xxalignat" || newtype == "flalign")
630 else if (newtype == "eqnarray") {
631 MathGridInset::addCol(1);
632 MathGridInset::addCol(1);
635 lyxerr << "mutation from '" << type_
636 << "' to '" << newtype << "' not implemented" << endl;
640 else if (type_ == "gather") {
641 if (newtype == "multline") {
644 lyxerr << "mutation from '" << type_
645 << "' to '" << newtype << "' not implemented" << endl;
650 lyxerr << "mutation from '" << type_
651 << "' to '" << newtype << "' not implemented" << endl;
656 string MathHullInset::eolString(row_type row, bool fragile) const
659 if (numberedType()) {
660 if (!label_[row].empty() && !nonum_[row])
661 res += "\\label{" + label_[row] + '}';
662 if (nonum_[row] && (type_ != "multline"))
663 res += "\\nonumber ";
665 return res + MathGridInset::eolString(row, fragile);
669 void MathHullInset::write(WriteStream & os) const
672 MathGridInset::write(os);
677 void MathHullInset::normalize(NormalStream & os) const
679 os << "[formula " << type_ << ' ';
680 MathGridInset::normalize(os);
685 void MathHullInset::mathmlize(MathMLStream & os) const
687 MathGridInset::mathmlize(os);
691 void MathHullInset::infoize(ostream & os) const
693 os << "Type: " << type_;
697 void MathHullInset::check() const
699 BOOST_ASSERT(nonum_.size() == nrows());
700 BOOST_ASSERT(label_.size() == nrows());
704 void MathHullInset::doExtern(LCursor & cur, FuncRequest & func)
708 istringstream iss(func.argument.c_str());
709 iss >> lang >> extra;
714 #warning temporarily disabled
715 //if (cur.selection()) {
718 // lyxerr << "use selection: " << ar << endl;
719 // insert(pipeThroughExtern(lang, extra, ar));
725 eq.push_back(MathAtom(new MathCharInset('=')));
727 // go to first item in line
728 cur.idx() -= cur.idx() % ncols();
731 if (getType() == "simple") {
732 size_type pos = cur.cell().find_last(eq);
734 if (cur.inMathed() && cur.selection()) {
735 asArray(cur.grabAndEraseSelection(), ar);
736 } else if (pos == cur.cell().size()) {
738 lyxerr << "use whole cell: " << ar << endl;
740 ar = MathArray(cur.cell().begin() + pos + 1, cur.cell().end());
741 lyxerr << "use partial cell form pos: " << pos << endl;
743 cur.cell().append(eq);
744 cur.cell().append(pipeThroughExtern(lang, extra, ar));
745 cur.pos() = cur.lastpos();
749 if (getType() == "equation") {
750 lyxerr << "use equation inset" << endl;
752 MathArray & ar = cur.cell();
753 lyxerr << "use cell: " << ar << endl;
757 cur.cell() = pipeThroughExtern(lang, extra, ar);
758 // move to end of line
759 cur.pos() = cur.lastpos();
764 lyxerr << "use eqnarray" << endl;
765 cur.idx() += 2 - cur.idx() % ncols();
767 MathArray ar = cur.cell();
768 lyxerr << "use cell: " << ar << endl;
770 #warning temporarily disabled
777 cur.cell() = pipeThroughExtern(lang, extra, ar);
778 cur.pos() = cur.lastpos();
783 void MathHullInset::priv_dispatch(LCursor & cur, FuncRequest & cmd)
785 switch (cmd.action) {
787 case LFUN_BREAKPARAGRAPH:
792 if (type_ == "simple" || type_ == "equation") {
796 //cur.dispatched(FINISHED);
799 MathGridInset::priv_dispatch(cur, cmd);
802 case LFUN_MATH_NUMBER:
803 //lyxerr << "toggling all numbers" << endl;
806 bool old = numberedType();
807 if (type_ == "multline")
808 numbered(nrows() - 1, !old);
810 for (row_type row = 0; row < nrows(); ++row)
812 cur.message(old ? _("No number") : _("Number"));
816 case LFUN_MATH_NONUMBER:
818 row_type r = (type_ == "multline") ? nrows() - 1 : cur.row();
820 bool old = numbered(r);
821 cur.message(old ? _("No number") : _("Number"));
826 case LFUN_INSERT_LABEL: {
827 row_type r = (type_ == "multline") ? nrows() - 1 : cur.row();
828 string old_label = label(r);
829 string new_label = cmd.argument;
831 if (new_label.empty()) {
832 string const default_label =
833 (lyxrc.label_init_length >= 0) ? "eq:" : "";
834 pair<bool, string> const res = old_label.empty()
835 ? Alert::askForText(_("Enter new label to insert:"), default_label)
836 : Alert::askForText(_("Enter label:"), old_label);
837 new_label = lyx::support::trim(res.second);
840 if (!new_label.empty())
846 case LFUN_MATH_EXTERN:
848 //cur.dispatched(FINISHED);
851 case LFUN_MATH_MUTATE: {
852 lyxerr << "Hull: MUTATE: " << cmd.argument << endl;
853 row_type row = cur.row();
854 col_type col = cur.col();
855 mutate(cmd.argument);
856 cur.idx() = row * ncols() + col;
857 if (cur.idx() > cur.lastidx()) {
858 cur.idx() = cur.lastidx();
859 cur.pos() = cur.lastpos();
861 if (cur.pos() > cur.lastpos())
862 cur.pos() = cur.lastpos();
863 //cur.dispatched(FINISHED);
867 case LFUN_MATH_DISPLAY: {
868 mutate(type_ == "simple" ? "equation" : "simple");
870 cur.pos() = cur.lastpos();
871 //cur.dispatched(FINISHED);
876 MathGridInset::priv_dispatch(cur, cmd);
882 bool MathHullInset::getStatus(LCursor & cur, FuncRequest const & cmd,
883 FuncStatus & flag) const
885 switch (cmd.action) {
887 case LFUN_MATH_NUMBER:
888 case LFUN_MATH_NONUMBER:
889 case LFUN_INSERT_LABEL:
890 case LFUN_MATH_EXTERN:
891 case LFUN_MATH_MUTATE:
892 case LFUN_MATH_DISPLAY:
896 return MathGridInset::getStatus(cur, cmd, flag);
901 /////////////////////////////////////////////////////////////////////
903 #include "math_arrayinset.h"
904 #include "math_deliminset.h"
905 #include "math_factory.h"
906 #include "math_parser.h"
907 #include "math_spaceinset.h"
908 #include "ref_inset.h"
910 #include "bufferview_funcs.h"
913 #include "frontends/LyXView.h"
914 #include "frontends/Dialogs.h"
916 #include "support/lstrings.h"
917 #include "support/lyxlib.h"
920 // simply scrap this function if you want
921 void MathHullInset::mutateToText()
924 // translate to latex
926 latex(NULL, os, false, false);
927 string str = os.str();
930 LyXText * lt = view_->getLyXText();
931 string::const_iterator cit = str.begin();
932 string::const_iterator end = str.end();
933 for (; cit != end; ++cit)
934 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
937 //view_->owner()->dispatch(LFUN_ESCAPE);
942 void MathHullInset::handleFont(LCursor & cur, string const & arg,
945 // this whole function is a hack and won't work for incremental font
948 if (cur.inset().asMathInset()->name() == font)
949 cur.handleFont(font);
951 cur.handleNest(createMathInset(font));
957 void MathHullInset::handleFont2(LCursor & cur, string const & arg)
962 bv_funcs::string2font(arg, font, b);
963 if (font.color() != LColor::inherit) {
964 MathAtom at = createMathInset("color");
965 asArray(lcolor.getGUIName(font.color()), at.nucleus()->cell(0));
966 cur.handleNest(at, 1);
971 void MathHullInset::edit(LCursor & cur, bool left)
974 left ? idxFirst(cur) : idxLast(cur);
978 string const MathHullInset::editMessage() const
980 return _("Math editor mode");
984 void MathHullInset::getCursorDim(int & asc, int & desc) const
988 //math_font_max_dim(font_, asc, des);
992 void MathHullInset::revealCodes(LCursor & cur) const
998 cur.message(os.str());
1000 // write something to the minibuffer
1001 // translate to latex
1005 string str = os.str();
1007 string::size_type pos = 0;
1009 for (string::iterator it = str.begin(); it != str.end(); ++it) {
1012 else if (*it == '\0') {
1014 pos = it - str.begin();
1020 res = res.substr(pos - 30);
1021 if (res.size() > 60)
1022 res = res.substr(0, 60);
1028 InsetBase::Code MathHullInset::lyxCode() const
1034 /////////////////////////////////////////////////////////////////////
1038 bool MathHullInset::searchForward(BufferView * bv, string const & str,
1041 #warning completely broken
1042 static MathHullInset * lastformula = 0;
1043 static CursorBase current = DocIterator(ibegin(nucleus()));
1044 static MathArray ar;
1045 static string laststr;
1047 if (lastformula != this || laststr != str) {
1048 //lyxerr << "reset lastformula to " << this << endl;
1051 current = ibegin(nucleus());
1053 mathed_parse_cell(ar, str);
1057 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
1059 for (DocIterator it = current; it != iend(nucleus()); increment(it)) {
1060 CursorSlice & top = it.back();
1061 MathArray const & a = top.asMathInset()->cell(top.idx_);
1062 if (a.matchpart(ar, top.pos_)) {
1063 bv->cursor().setSelection(it, ar.size());
1065 top.pos_ += ar.size();
1071 //lyxerr << "not found!" << endl;
1078 void MathHullInset::write(Buffer const &, std::ostream & os) const
1080 WriteStream wi(os, false, false);
1086 void MathHullInset::read(Buffer const &, LyXLex & lex)
1089 mathed_parse_normal(at, lex);
1090 operator=(*at->asHullInset());
1094 int MathHullInset::plaintext(Buffer const &, ostream & os,
1095 OutputParams const &) const
1097 if (0 && display()) {
1101 TextPainter tpain(dim.width(), dim.height());
1102 drawT(tpain, 0, dim.ascent());
1104 // reset metrics cache to "real" values
1106 return tpain.textheight();
1108 WriteStream wi(os, false, true);
1109 wi << ' ' << cell(0) << ' ';
1115 int MathHullInset::linuxdoc(Buffer const & buf, ostream & os,
1116 OutputParams const & runparams) const
1118 return docbook(buf, os, runparams);
1122 int MathHullInset::docbook(Buffer const & buf, ostream & os,
1123 OutputParams const & runparams) const
1125 MathMLStream ms(os);
1126 ms << MTag("equation");
1129 int res = plaintext(buf, ms.os(), runparams);
1133 MathGridInset::mathmlize(ms);
1135 ms << ETag("equation");
1136 return ms.line() + res;