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"
21 #include "textpainter.h"
22 #include "funcrequest.h"
24 #include "LaTeXFeatures.h"
25 #include "support/LAssert.h"
27 #include "frontends/Alert.h"
32 using namespace lyx::support;
42 int getCols(string const & type)
44 if (type == "eqnarray")
48 if (type == "flalign")
50 if (type == "alignat")
52 if (type == "xalignat")
54 if (type == "xxalignat")
60 // returns position of first relation operator in the array
61 // used for "intelligent splitting"
62 MathArray::size_type firstRelOp(MathArray const & ar)
64 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
66 return it - ar.begin();
71 char const * star(bool numbered)
73 return numbered ? "" : "*";
77 int typecode(string const & s)
79 if (s == "none") return 0;
80 if (s == "simple") return 1;
81 if (s == "equation") return 2;
82 if (s == "eqnarray") return 3;
83 if (s == "align") return 4;
84 if (s == "alignat") return 5;
85 if (s == "xalignat") return 6;
86 if (s == "xxalignat") return 7;
87 if (s == "multline") return 8;
88 if (s == "gather") return 9;
89 if (s == "flalign") return 10;
90 lyxerr << "unknown hull type '" << s << "'" << endl;
94 bool smaller(string const & s, string const & t)
96 return typecode(s) < typecode(t);
100 } // end anon namespace
103 MathHullInset::MathHullInset()
104 : MathGridInset(1, 1), type_("none"), nonum_(1), label_(1)
110 MathHullInset::MathHullInset(string const & type)
111 : MathGridInset(getCols(type), 1), type_(type), nonum_(1), label_(1)
117 auto_ptr<InsetBase> MathHullInset::clone() const
119 return auto_ptr<InsetBase>(new MathHullInset(*this));
123 MathInset::mode_type MathHullInset::currentMode() const
126 return UNDECIDED_MODE;
127 // definitely math mode ...
132 bool MathHullInset::idxFirst(idx_type & idx, pos_type & pos) const
140 bool MathHullInset::idxLast(idx_type & idx, pos_type & pos) const
143 pos = cell(idx).size();
148 char MathHullInset::defaultColAlign(col_type col)
150 if (type_ == "eqnarray")
152 if (typecode(type_) >= typecode("align"))
153 return "rl"[col & 1];
158 int MathHullInset::defaultColSpace(col_type col)
160 if (type_ == "align" || type_ == "alignat")
162 if (type_ == "xalignat")
163 return (col & 1) ? 20 : 0;
164 if (type_ == "xxalignat" || type_ == "flalign")
165 return (col & 1) ? 40 : 0;
170 char const * MathHullInset::standardFont() const
173 return "lyxnochange";
178 void MathHullInset::metrics(MetricsInfo & mi, Dimension & dim) const
180 FontSetChanger dummy1(mi.base, standardFont());
181 StyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
183 // let the cells adjust themselves
184 MathGridInset::metrics(mi);
191 if (numberedType()) {
192 FontSetChanger dummy(mi.base, "mathbf");
194 for (row_type row = 0; row < nrows(); ++row)
195 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
201 // make it at least as high as the current font
204 math_font_max_dim(mi.base.font, asc, des);
205 dim_.asc = max(dim_.asc, asc);
206 dim_.des = max(dim_.des, des);
214 void MathHullInset::draw(PainterInfo & pi, int x, int y) const
216 FontSetChanger dummy1(pi.base, standardFont());
217 StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
218 MathGridInset::draw(pi, x + 1, y);
220 if (numberedType()) {
221 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
222 for (row_type row = 0; row < nrows(); ++row) {
223 int const yy = y + rowinfo_[row].offset_;
224 FontSetChanger dummy(pi.base, "mathrm");
225 drawStr(pi, pi.base.font, xx, yy, nicelabel(row));
229 drawMarkers2(pi, x, y);
233 void MathHullInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
236 MathGridInset::metricsT(mi, dim);
239 WriteStream wi(os, false, true);
241 dim.wid = os.str().size();
248 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
251 MathGridInset::drawT(pain, x, y);
254 WriteStream wi(os, false, true);
256 pain.draw(x, y, os.str().c_str());
261 string MathHullInset::label(row_type row) const
263 row_type n = nrows();
269 void MathHullInset::label(row_type row, string const & label)
271 //lyxerr << "setting label '" << label << "' for row " << row << endl;
276 void MathHullInset::numbered(row_type row, bool num)
282 bool MathHullInset::numbered(row_type row) const
288 bool MathHullInset::ams() const
292 type_ == "flalign" ||
293 type_ == "multline" ||
295 type_ == "alignat" ||
296 type_ == "xalignat" ||
297 type_ == "xxalignat";
301 bool MathHullInset::display() const
303 return type_ != "simple" && type_ != "none";
307 void MathHullInset::getLabelList(std::vector<string> & labels) const
309 for (row_type row = 0; row < nrows(); ++row)
310 if (!label_[row].empty() && nonum_[row] != 1)
311 labels.push_back(label_[row]);
315 bool MathHullInset::numberedType() const
319 if (type_ == "simple")
321 if (type_ == "xxalignat")
323 for (row_type row = 0; row < nrows(); ++row)
330 void MathHullInset::validate(LaTeXFeatures & features) const
333 features.require("amsmath");
336 // Validation is necessary only if not using AMS math.
337 // To be safe, we will always run mathedvalidate.
338 //if (features.amsstyle)
341 features.require("boldsymbol");
342 //features.binom = true;
344 MathGridInset::validate(features);
348 void MathHullInset::header_write(WriteStream & os) const
350 bool n = numberedType();
355 else if (type_ == "simple") {
361 else if (type_ == "equation") {
363 os << "\\begin{equation" << star(n) << "}\n";
368 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
369 || type_ == "gather" || type_ == "multline")
370 os << "\\begin{" << type_ << star(n) << "}\n";
372 else if (type_ == "alignat" || type_ == "xalignat")
373 os << "\\begin{" << type_ << star(n) << '}'
374 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
376 else if (type_ == "xxalignat")
377 os << "\\begin{" << type_ << '}'
378 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
381 os << "\\begin{unknown" << star(n) << '}';
385 void MathHullInset::footer_write(WriteStream & os) const
387 bool n = numberedType();
392 else if (type_ == "simple")
395 else if (type_ == "equation")
397 os << "\\end{equation" << star(n) << "}\n";
401 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
402 || type_ == "alignat" || type_ == "xalignat"
403 || type_ == "gather" || type_ == "multline")
404 os << "\\end{" << type_ << star(n) << "}\n";
406 else if (type_ == "xxalignat")
407 os << "\\end{" << type_ << "}\n";
410 os << "\\end{unknown" << star(n) << '}';
414 bool MathHullInset::colChangeOK() const
417 type_ == "align" || type_ == "flalign" ||type_ == "alignat" ||
418 type_ == "xalignat" || type_ == "xxalignat";
422 void MathHullInset::addRow(row_type row)
424 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
425 label_.insert(label_.begin() + row + 1, string());
426 MathGridInset::addRow(row);
430 void MathHullInset::delRow(row_type row)
434 MathGridInset::delRow(row);
435 nonum_.erase(nonum_.begin() + row);
436 label_.erase(label_.begin() + row);
440 void MathHullInset::addCol(col_type col)
443 MathGridInset::addCol(col);
445 lyxerr << "Can't change number of columns in '" << type_ << "'" << endl;
449 void MathHullInset::delCol(col_type col)
452 MathGridInset::delCol(col);
454 lyxerr << "Can't change number of columns in '" << type_ << "'" << endl;
458 string MathHullInset::nicelabel(row_type row) const
462 if (label_[row].empty())
463 return string("(#)");
464 return '(' + label_[row] + ')';
468 void MathHullInset::glueall()
471 for (idx_type i = 0; i < nargs(); ++i)
473 *this = MathHullInset("simple");
479 string const & MathHullInset::getType() const
485 void MathHullInset::setType(string const & type)
493 void MathHullInset::mutate(string const & newtype)
495 //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
497 // we try to move along the chain
498 // none <-> simple <-> equation <-> eqnarray
500 if (newtype == "dump") {
504 else if (newtype == type_) {
508 else if (type_ == "none") {
514 else if (type_ == "simple") {
515 if (newtype == "none") {
524 else if (type_ == "equation") {
525 if (smaller(newtype, type_)) {
528 } else if (newtype == "eqnarray") {
529 MathGridInset::addCol(1);
530 MathGridInset::addCol(1);
532 // split it "nicely" on the firest relop
533 pos_type pos = firstRelOp(cell(0));
534 cell(1) = MathArray(cell(0).begin() + pos, cell(0).end());
535 cell(0).erase(pos, cell(0).size());
537 if (cell(1).size()) {
538 cell(2) = MathArray(cell(1).begin() + 1, cell(1).end());
539 cell(1).erase(1, cell(1).size());
543 } else if (newtype == "multline" || newtype == "gather") {
547 MathGridInset::addCol(1);
549 pos_type pos = firstRelOp(cell(0));
551 cell(0).erase(pos, cell(0).size());
552 cell(1).erase(0, pos);
558 else if (type_ == "eqnarray") {
559 if (smaller(newtype, type_)) {
560 // set correct (no)numbering
561 bool allnonum = true;
562 for (row_type row = 0; row < nrows(); ++row)
566 // set first non-empty label
568 for (row_type row = 0; row < nrows(); ++row) {
569 if (!label_[row].empty()) {
576 nonum_[0] = allnonum;
579 } else { // align & Co.
580 for (row_type row = 0; row < nrows(); ++row) {
581 idx_type c = 3 * row + 1;
582 cell(c).append(cell(c + 1));
584 MathGridInset::delCol(2);
590 else if (type_ == "align") {
591 if (smaller(newtype, type_)) {
592 MathGridInset::addCol(1);
600 else if (type_ == "multline") {
601 if (newtype == "gather" || newtype == "align" ||
602 newtype == "xalignat" || newtype == "xxalignat" || newtype == "flalign")
604 else if (newtype == "eqnarray") {
605 MathGridInset::addCol(1);
606 MathGridInset::addCol(1);
609 lyxerr << "mutation from '" << type_
610 << "' to '" << newtype << "' not implemented" << endl;
614 else if (type_ == "gather") {
615 if (newtype == "multline") {
618 lyxerr << "mutation from '" << type_
619 << "' to '" << newtype << "' not implemented" << endl;
624 lyxerr << "mutation from '" << type_
625 << "' to '" << newtype << "' not implemented" << endl;
630 string MathHullInset::eolString(row_type row, bool fragile) const
633 if (numberedType()) {
634 if (!label_[row].empty() && !nonum_[row])
635 res += "\\label{" + label_[row] + '}';
636 if (nonum_[row] && (type_ != "multline"))
637 res += "\\nonumber ";
639 return res + MathGridInset::eolString(row, fragile);
643 void MathHullInset::write(WriteStream & os) const
646 MathGridInset::write(os);
651 void MathHullInset::normalize(NormalStream & os) const
653 os << "[formula " << type_ << ' ';
654 MathGridInset::normalize(os);
659 void MathHullInset::mathmlize(MathMLStream & os) const
661 MathGridInset::mathmlize(os);
665 void MathHullInset::infoize(std::ostream & os) const
667 os << "Type: " << type_;
671 void MathHullInset::check() const
673 Assert(nonum_.size() == nrows());
674 Assert(label_.size() == nrows());
678 void MathHullInset::doExtern
679 (FuncRequest const & func, idx_type & idx, pos_type & pos)
683 istringstream iss(func.argument.c_str());
684 iss >> lang >> extra;
689 #warning temporarily disabled
693 // lyxerr << "use selection: " << ar << endl;
694 // insert(pipeThroughExtern(lang, extra, ar));
700 eq.push_back(MathAtom(new MathCharInset('=')));
702 // go to first item in line
703 idx -= idx % ncols();
706 if (getType() == "simple") {
707 size_type pos = cell(idx).find_last(eq);
709 if (mathcursor && mathcursor->selection()) {
710 asArray(mathcursor->grabAndEraseSelection(), ar);
711 } else if (pos == cell(idx).size()) {
713 lyxerr << "use whole cell: " << ar << endl;
715 ar = MathArray(cell(idx).begin() + pos + 1, cell(idx).end());
716 lyxerr << "use partial cell form pos: " << pos << endl;
718 cell(idx).append(eq);
719 cell(idx).append(pipeThroughExtern(lang, extra, ar));
720 pos = cell(idx).size();
724 if (getType() == "equation") {
725 lyxerr << "use equation inset" << endl;
727 MathArray & ar = cell(idx);
728 lyxerr << "use cell: " << ar << endl;
730 cell(idx + 2) = pipeThroughExtern(lang, extra, ar);
731 // move to end of line
733 pos = cell(idx).size();
738 lyxerr << "use eqnarray" << endl;
739 idx -= idx % ncols();
742 MathArray ar = cell(idx);
743 lyxerr << "use cell: " << ar << endl;
745 #warning temporarily disabled
749 cell(idx + 3) = pipeThroughExtern(lang, extra, ar);
751 pos = cell(idx).size();
756 dispatch_result MathHullInset::dispatch
757 (FuncRequest const & cmd, idx_type & idx, pos_type & pos)
759 switch (cmd.action) {
762 if (type_ == "simple" || type_ == "equation") {
766 return DISPATCHED_POP;
768 return MathGridInset::dispatch(cmd, idx, pos);
770 case LFUN_MATH_NUMBER:
771 //lyxerr << "toggling all numbers" << endl;
773 //recordUndo(bv, Undo::INSERT);
774 bool old = numberedType();
775 if (type_ == "multline")
776 numbered(nrows() - 1, !old);
778 for (row_type row = 0; row < nrows(); ++row)
780 //bv->owner()->message(old ? _("No number") : _("Number"));
784 case LFUN_MATH_NONUMBER:
786 row_type r = (type_ == "multline") ? nrows() - 1 : row(idx);
787 //recordUndo(bv, Undo::INSERT);
788 bool old = numbered(r);
789 //bv->owner()->message(old ? _("No number") : _("Number"));
794 case LFUN_INSERT_LABEL: {
795 row_type r = (type_ == "multline") ? nrows() - 1 : row(idx);
796 string old_label = label(r);
797 string new_label = cmd.argument;
799 if (new_label.empty()) {
800 string const default_label =
801 (lyxrc.label_init_length >= 0) ? "eq:" : "";
802 pair<bool, string> const res = old_label.empty()
803 ? Alert::askForText(_("Enter new label to insert:"), default_label)
804 : Alert::askForText(_("Enter label:"), old_label);
807 new_label = trim(res.second);
810 //if (new_label == old_label)
811 // break; // Nothing to do
813 if (!new_label.empty())
819 case LFUN_MATH_EXTERN:
820 doExtern(cmd, idx, pos);
821 return DISPATCHED_POP;
823 case LFUN_MATH_MUTATE: {
824 row_type r = row(idx);
825 col_type c = col(idx);
826 mutate(cmd.argument);
827 idx = r * ncols() + c;
830 if (pos > cell(idx).size())
831 pos = cell(idx).size();
832 return DISPATCHED_POP;
835 case LFUN_MATH_DISPLAY: {
836 mutate(type_ == "simple" ? "equation" : "simple");
838 pos = cell(idx).size();
839 return DISPATCHED_POP;
843 return MathGridInset::dispatch(cmd, idx, pos);
848 string MathHullInset::fileInsetLabel() const