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"
22 #include "funcrequest.h"
24 #include "LaTeXFeatures.h"
28 #include "frontends/Alert.h"
30 #include "support/std_sstream.h"
33 using lyx::support::trim;
39 using std::istringstream;
40 using std::ostringstream;
46 int getCols(string const & type)
48 if (type == "eqnarray")
52 if (type == "flalign")
54 if (type == "alignat")
56 if (type == "xalignat")
58 if (type == "xxalignat")
64 // returns position of first relation operator in the array
65 // used for "intelligent splitting"
66 MathArray::size_type firstRelOp(MathArray const & ar)
68 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
70 return it - ar.begin();
75 char const * star(bool numbered)
77 return numbered ? "" : "*";
81 int typecode(string const & s)
83 if (s == "none") return 0;
84 if (s == "simple") return 1;
85 if (s == "equation") return 2;
86 if (s == "eqnarray") return 3;
87 if (s == "align") return 4;
88 if (s == "alignat") return 5;
89 if (s == "xalignat") return 6;
90 if (s == "xxalignat") return 7;
91 if (s == "multline") return 8;
92 if (s == "gather") return 9;
93 if (s == "flalign") return 10;
94 lyxerr << "unknown hull type '" << s << "'" << endl;
98 bool smaller(string const & s, string const & t)
100 return typecode(s) < typecode(t);
104 } // end anon namespace
107 MathHullInset::MathHullInset()
108 : MathGridInset(1, 1), type_("none"), nonum_(1), label_(1)
114 MathHullInset::MathHullInset(string const & type)
115 : MathGridInset(getCols(type), 1), type_(type), nonum_(1), label_(1)
121 auto_ptr<InsetBase> MathHullInset::clone() const
123 return auto_ptr<InsetBase>(new MathHullInset(*this));
127 MathInset::mode_type MathHullInset::currentMode() const
130 return UNDECIDED_MODE;
131 // definitely math mode ...
136 bool MathHullInset::idxFirst(idx_type & idx, pos_type & pos) const
144 bool MathHullInset::idxLast(idx_type & idx, pos_type & pos) const
147 pos = cell(idx).size();
152 char MathHullInset::defaultColAlign(col_type col)
154 if (type_ == "eqnarray")
156 if (typecode(type_) >= typecode("align"))
157 return "rl"[col & 1];
162 int MathHullInset::defaultColSpace(col_type col)
164 if (type_ == "align" || type_ == "alignat")
166 if (type_ == "xalignat")
167 return (col & 1) ? 20 : 0;
168 if (type_ == "xxalignat" || type_ == "flalign")
169 return (col & 1) ? 40 : 0;
174 char const * MathHullInset::standardFont() const
177 return "lyxnochange";
178 lyxerr << "standard font: mathnormal\n";
183 void MathHullInset::metrics(MetricsInfo & mi, Dimension & dim) const
185 FontSetChanger dummy1(mi.base, standardFont());
186 StyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
188 // let the cells adjust themselves
189 MathGridInset::metrics(mi);
196 if (numberedType()) {
197 FontSetChanger dummy(mi.base, "mathbf");
199 for (row_type row = 0; row < nrows(); ++row)
200 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
206 // make it at least as high as the current font
209 math_font_max_dim(mi.base.font, asc, des);
210 dim_.asc = max(dim_.asc, asc);
211 dim_.des = max(dim_.des, des);
219 void MathHullInset::draw(PainterInfo & pi, int x, int y) const
221 FontSetChanger dummy1(pi.base, standardFont());
222 StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
223 MathGridInset::draw(pi, x + 1, y);
225 if (numberedType()) {
226 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
227 for (row_type row = 0; row < nrows(); ++row) {
228 int const yy = y + rowinfo_[row].offset_;
229 FontSetChanger dummy(pi.base, "mathrm");
230 drawStr(pi, pi.base.font, xx, yy, nicelabel(row));
234 drawMarkers2(pi, x, y);
238 void MathHullInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
241 MathGridInset::metricsT(mi, dim);
244 WriteStream wi(os, false, true);
246 dim.wid = os.str().size();
253 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
256 MathGridInset::drawT(pain, x, y);
259 WriteStream wi(os, false, true);
261 pain.draw(x, y, os.str().c_str());
266 string MathHullInset::label(row_type row) const
268 row_type n = nrows();
269 BOOST_ASSERT(row < n);
274 void MathHullInset::label(row_type row, string const & label)
276 //lyxerr << "setting label '" << label << "' for row " << row << endl;
281 void MathHullInset::numbered(row_type row, bool num)
287 bool MathHullInset::numbered(row_type row) const
293 bool MathHullInset::ams() const
297 type_ == "flalign" ||
298 type_ == "multline" ||
300 type_ == "alignat" ||
301 type_ == "xalignat" ||
302 type_ == "xxalignat";
306 bool MathHullInset::display() const
308 return type_ != "simple" && type_ != "none";
312 void MathHullInset::getLabelList(Buffer const &,
313 std::vector<string> & labels) const
315 for (row_type row = 0; row < nrows(); ++row)
316 if (!label_[row].empty() && nonum_[row] != 1)
317 labels.push_back(label_[row]);
321 bool MathHullInset::numberedType() const
325 if (type_ == "simple")
327 if (type_ == "xxalignat")
329 for (row_type row = 0; row < nrows(); ++row)
336 void MathHullInset::validate(LaTeXFeatures & features) const
339 features.require("amsmath");
342 // Validation is necessary only if not using AMS math.
343 // To be safe, we will always run mathedvalidate.
344 //if (features.amsstyle)
347 features.require("boldsymbol");
348 //features.binom = true;
350 MathGridInset::validate(features);
354 void MathHullInset::header_write(WriteStream & os) const
356 bool n = numberedType();
361 else if (type_ == "simple") {
367 else if (type_ == "equation") {
369 os << "\\begin{equation" << star(n) << "}\n";
374 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
375 || type_ == "gather" || type_ == "multline")
376 os << "\\begin{" << type_ << star(n) << "}\n";
378 else if (type_ == "alignat" || type_ == "xalignat")
379 os << "\\begin{" << type_ << star(n) << '}'
380 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
382 else if (type_ == "xxalignat")
383 os << "\\begin{" << type_ << '}'
384 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
387 os << "\\begin{unknown" << star(n) << '}';
391 void MathHullInset::footer_write(WriteStream & os) const
393 bool n = numberedType();
398 else if (type_ == "simple")
401 else if (type_ == "equation")
403 os << "\\end{equation" << star(n) << "}\n";
407 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
408 || type_ == "alignat" || type_ == "xalignat"
409 || type_ == "gather" || type_ == "multline")
410 os << "\\end{" << type_ << star(n) << "}\n";
412 else if (type_ == "xxalignat")
413 os << "\\end{" << type_ << "}\n";
416 os << "\\end{unknown" << star(n) << '}';
420 bool MathHullInset::colChangeOK() const
423 type_ == "align" || type_ == "flalign" ||type_ == "alignat" ||
424 type_ == "xalignat" || type_ == "xxalignat";
428 void MathHullInset::addRow(row_type row)
430 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
431 label_.insert(label_.begin() + row + 1, string());
432 MathGridInset::addRow(row);
436 void MathHullInset::delRow(row_type row)
440 MathGridInset::delRow(row);
441 nonum_.erase(nonum_.begin() + row);
442 label_.erase(label_.begin() + row);
446 void MathHullInset::addCol(col_type col)
449 MathGridInset::addCol(col);
451 lyxerr << "Can't change number of columns in '" << type_ << "'" << endl;
455 void MathHullInset::delCol(col_type col)
458 MathGridInset::delCol(col);
460 lyxerr << "Can't change number of columns in '" << type_ << "'" << endl;
464 string MathHullInset::nicelabel(row_type row) const
468 if (label_[row].empty())
469 return string("(#)");
470 return '(' + label_[row] + ')';
474 void MathHullInset::glueall()
477 for (idx_type i = 0; i < nargs(); ++i)
479 *this = MathHullInset("simple");
485 string const & MathHullInset::getType() const
491 void MathHullInset::setType(string const & type)
499 void MathHullInset::mutate(string const & newtype)
501 //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
503 // we try to move along the chain
504 // none <-> simple <-> equation <-> eqnarray
506 if (newtype == "dump") {
510 else if (newtype == type_) {
514 else if (type_ == "none") {
520 else if (type_ == "simple") {
521 if (newtype == "none") {
530 else if (type_ == "equation") {
531 if (smaller(newtype, type_)) {
534 } else if (newtype == "eqnarray") {
535 MathGridInset::addCol(1);
536 MathGridInset::addCol(1);
538 // split it "nicely" on the firest relop
539 pos_type pos = firstRelOp(cell(0));
540 cell(1) = MathArray(cell(0).begin() + pos, cell(0).end());
541 cell(0).erase(pos, cell(0).size());
543 if (cell(1).size()) {
544 cell(2) = MathArray(cell(1).begin() + 1, cell(1).end());
545 cell(1).erase(1, cell(1).size());
549 } else if (newtype == "multline" || newtype == "gather") {
553 MathGridInset::addCol(1);
555 pos_type pos = firstRelOp(cell(0));
557 cell(0).erase(pos, cell(0).size());
558 cell(1).erase(0, pos);
564 else if (type_ == "eqnarray") {
565 if (smaller(newtype, type_)) {
566 // set correct (no)numbering
567 bool allnonum = true;
568 for (row_type row = 0; row < nrows(); ++row)
572 // set first non-empty label
574 for (row_type row = 0; row < nrows(); ++row) {
575 if (!label_[row].empty()) {
582 nonum_[0] = allnonum;
585 } else { // align & Co.
586 for (row_type row = 0; row < nrows(); ++row) {
587 idx_type c = 3 * row + 1;
588 cell(c).append(cell(c + 1));
590 MathGridInset::delCol(2);
596 else if (type_ == "align") {
597 if (smaller(newtype, type_)) {
598 MathGridInset::addCol(1);
606 else if (type_ == "multline") {
607 if (newtype == "gather" || newtype == "align" ||
608 newtype == "xalignat" || newtype == "xxalignat" || newtype == "flalign")
610 else if (newtype == "eqnarray") {
611 MathGridInset::addCol(1);
612 MathGridInset::addCol(1);
615 lyxerr << "mutation from '" << type_
616 << "' to '" << newtype << "' not implemented" << endl;
620 else if (type_ == "gather") {
621 if (newtype == "multline") {
624 lyxerr << "mutation from '" << type_
625 << "' to '" << newtype << "' not implemented" << endl;
630 lyxerr << "mutation from '" << type_
631 << "' to '" << newtype << "' not implemented" << endl;
636 string MathHullInset::eolString(row_type row, bool fragile) const
639 if (numberedType()) {
640 if (!label_[row].empty() && !nonum_[row])
641 res += "\\label{" + label_[row] + '}';
642 if (nonum_[row] && (type_ != "multline"))
643 res += "\\nonumber ";
645 return res + MathGridInset::eolString(row, fragile);
649 void MathHullInset::write(WriteStream & os) const
652 MathGridInset::write(os);
657 void MathHullInset::normalize(NormalStream & os) const
659 os << "[formula " << type_ << ' ';
660 MathGridInset::normalize(os);
665 void MathHullInset::mathmlize(MathMLStream & os) const
667 MathGridInset::mathmlize(os);
671 void MathHullInset::infoize(std::ostream & os) const
673 os << "Type: " << type_;
677 void MathHullInset::check() const
679 BOOST_ASSERT(nonum_.size() == nrows());
680 BOOST_ASSERT(label_.size() == nrows());
684 void MathHullInset::doExtern
685 (FuncRequest const & func, idx_type & idx, pos_type & pos)
689 istringstream iss(func.argument.c_str());
690 iss >> lang >> extra;
695 #warning temporarily disabled
699 // lyxerr << "use selection: " << ar << endl;
700 // insert(pipeThroughExtern(lang, extra, ar));
706 eq.push_back(MathAtom(new MathCharInset('=')));
708 // go to first item in line
709 idx -= idx % ncols();
712 if (getType() == "simple") {
713 size_type pos = cell(idx).find_last(eq);
715 if (mathcursor && mathcursor->selection()) {
716 asArray(mathcursor->grabAndEraseSelection(), ar);
717 } else if (pos == cell(idx).size()) {
719 lyxerr << "use whole cell: " << ar << endl;
721 ar = MathArray(cell(idx).begin() + pos + 1, cell(idx).end());
722 lyxerr << "use partial cell form pos: " << pos << endl;
724 cell(idx).append(eq);
725 cell(idx).append(pipeThroughExtern(lang, extra, ar));
726 pos = cell(idx).size();
730 if (getType() == "equation") {
731 lyxerr << "use equation inset" << endl;
733 MathArray & ar = cell(idx);
734 lyxerr << "use cell: " << ar << endl;
736 cell(idx + 2) = pipeThroughExtern(lang, extra, ar);
737 // move to end of line
739 pos = cell(idx).size();
744 lyxerr << "use eqnarray" << endl;
745 idx -= idx % ncols();
748 MathArray ar = cell(idx);
749 lyxerr << "use cell: " << ar << endl;
751 #warning temporarily disabled
755 cell(idx + 3) = pipeThroughExtern(lang, extra, ar);
757 pos = cell(idx).size();
762 dispatch_result MathHullInset::dispatch
763 (FuncRequest const & cmd, idx_type & idx, pos_type & pos)
765 switch (cmd.action) {
768 if (type_ == "simple" || type_ == "equation") {
772 return DISPATCHED_POP;
774 return MathGridInset::dispatch(cmd, idx, pos);
776 case LFUN_MATH_NUMBER:
777 //lyxerr << "toggling all numbers" << endl;
779 //recordUndo(bv, Undo::INSERT);
780 bool old = numberedType();
781 if (type_ == "multline")
782 numbered(nrows() - 1, !old);
784 for (row_type row = 0; row < nrows(); ++row)
786 //bv->owner()->message(old ? _("No number") : _("Number"));
790 case LFUN_MATH_NONUMBER:
792 row_type r = (type_ == "multline") ? nrows() - 1 : row(idx);
793 //recordUndo(bv, Undo::INSERT);
794 bool old = numbered(r);
795 //bv->owner()->message(old ? _("No number") : _("Number"));
800 case LFUN_INSERT_LABEL: {
801 row_type r = (type_ == "multline") ? nrows() - 1 : row(idx);
802 string old_label = label(r);
803 string new_label = cmd.argument;
805 if (new_label.empty()) {
806 string const default_label =
807 (lyxrc.label_init_length >= 0) ? "eq:" : "";
808 pair<bool, string> const res = old_label.empty()
809 ? Alert::askForText(_("Enter new label to insert:"), default_label)
810 : Alert::askForText(_("Enter label:"), old_label);
813 new_label = trim(res.second);
816 //if (new_label == old_label)
817 // break; // Nothing to do
819 if (!new_label.empty())
825 case LFUN_MATH_EXTERN:
826 doExtern(cmd, idx, pos);
827 return DISPATCHED_POP;
829 case LFUN_MATH_MUTATE: {
830 row_type r = row(idx);
831 col_type c = col(idx);
832 mutate(cmd.argument);
833 idx = r * ncols() + c;
836 if (pos > cell(idx).size())
837 pos = cell(idx).size();
838 return DISPATCHED_POP;
841 case LFUN_MATH_DISPLAY: {
842 mutate(type_ == "simple" ? "equation" : "simple");
844 pos = cell(idx).size();
845 return DISPATCHED_POP;
849 return MathGridInset::dispatch(cmd, idx, pos);
854 string MathHullInset::fileInsetLabel() const