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"
26 #include "frontends/Painter.h"
28 #include "frontends/Alert.h"
31 #include "BufferView.h"
35 using namespace lyx::support;
45 int getCols(string const & type)
47 if (type == "eqnarray")
51 if (type == "flalign")
53 if (type == "alignat")
55 if (type == "xalignat")
57 if (type == "xxalignat")
63 // returns position of first relation operator in the array
64 // used for "intelligent splitting"
65 MathArray::size_type firstRelOp(MathArray const & ar)
67 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
69 return it - ar.begin();
74 char const * star(bool numbered)
76 return numbered ? "" : "*";
80 int typecode(string const & s)
82 if (s == "none") return 0;
83 if (s == "simple") return 1;
84 if (s == "equation") return 2;
85 if (s == "eqnarray") return 3;
86 if (s == "align") return 4;
87 if (s == "alignat") return 5;
88 if (s == "xalignat") return 6;
89 if (s == "xxalignat") return 7;
90 if (s == "multline") return 8;
91 if (s == "gather") return 9;
92 if (s == "flalign") return 10;
93 lyxerr << "unknown hull type '" << s << "'" << endl;
97 bool smaller(string const & s, string const & t)
99 return typecode(s) < typecode(t);
103 } // end anon namespace
106 MathHullInset::MathHullInset()
107 : MathGridInset(1, 1), type_("none"), nonum_(1), label_(1)
113 MathHullInset::MathHullInset(string const & type)
114 : MathGridInset(getCols(type), 1), type_(type), nonum_(1), label_(1)
120 auto_ptr<InsetBase> MathHullInset::clone() const
122 return auto_ptr<InsetBase>(new MathHullInset(*this));
126 MathInset::mode_type MathHullInset::currentMode() const
129 return UNDECIDED_MODE;
130 // definitely math mode ...
135 bool MathHullInset::idxFirst(idx_type & idx, pos_type & pos) const
143 bool MathHullInset::idxLast(idx_type & idx, pos_type & pos) const
146 pos = cell(idx).size();
151 char MathHullInset::defaultColAlign(col_type col)
153 if (type_ == "eqnarray")
155 if (typecode(type_) >= typecode("align"))
156 return "rl"[col & 1];
161 int MathHullInset::defaultColSpace(col_type col)
163 if (type_ == "align" || type_ == "alignat")
165 if (type_ == "xalignat")
166 return (col & 1) ? 20 : 0;
167 if (type_ == "xxalignat" || type_ == "flalign")
168 return (col & 1) ? 40 : 0;
173 char const * MathHullInset::standardFont() const
176 return "lyxnochange";
181 void MathHullInset::metrics(MetricsInfo & mi, Dimension & dim) const
183 FontSetChanger dummy1(mi.base, standardFont());
184 StyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
186 // let the cells adjust themselves
187 MathGridInset::metrics(mi);
194 if (numberedType()) {
195 FontSetChanger dummy(mi.base, "mathbf");
197 for (row_type row = 0; row < nrows(); ++row)
198 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
204 // make it at least as high as the current font
207 math_font_max_dim(mi.base.font, asc, des);
208 dim_.asc = max(dim_.asc, asc);
209 dim_.des = max(dim_.des, des);
217 void MathHullInset::draw(PainterInfo & pi, int x, int y) const
219 FontSetChanger dummy1(pi.base, standardFont());
220 StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
221 MathGridInset::draw(pi, x + 1, y);
223 if (numberedType()) {
224 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
225 for (row_type row = 0; row < nrows(); ++row) {
226 int const yy = y + rowinfo_[row].offset_;
227 FontSetChanger dummy(pi.base, "mathrm");
228 drawStr(pi, pi.base.font, xx, yy, nicelabel(row));
232 drawMarkers2(pi, x, y);
236 void MathHullInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
239 MathGridInset::metricsT(mi, dim);
242 WriteStream wi(os, false, true);
244 dim.wid = os.str().size();
251 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
254 MathGridInset::drawT(pain, x, y);
257 WriteStream wi(os, false, true);
259 pain.draw(x, y, os.str().c_str());
264 string MathHullInset::label(row_type row) const
266 row_type n = nrows();
272 void MathHullInset::label(row_type row, string const & label)
274 //lyxerr << "setting label '" << label << "' for row " << row << endl;
279 void MathHullInset::numbered(row_type row, bool num)
285 bool MathHullInset::numbered(row_type row) const
291 bool MathHullInset::ams() const
295 type_ == "flalign" ||
296 type_ == "multline" ||
298 type_ == "alignat" ||
299 type_ == "xalignat" ||
300 type_ == "xxalignat";
304 bool MathHullInset::display() const
306 return type_ != "simple" && type_ != "none";
310 void MathHullInset::getLabelList(std::vector<string> & labels) const
312 for (row_type row = 0; row < nrows(); ++row)
313 if (!label_[row].empty() && nonum_[row] != 1)
314 labels.push_back(label_[row]);
318 bool MathHullInset::numberedType() const
322 if (type_ == "simple")
324 if (type_ == "xxalignat")
326 for (row_type row = 0; row < nrows(); ++row)
333 void MathHullInset::validate(LaTeXFeatures & features) const
336 features.require("amsmath");
339 // Validation is necessary only if not using AMS math.
340 // To be safe, we will always run mathedvalidate.
341 //if (features.amsstyle)
344 features.require("boldsymbol");
345 //features.binom = true;
347 MathGridInset::validate(features);
351 void MathHullInset::header_write(WriteStream & os) const
353 bool n = numberedType();
358 else if (type_ == "simple") {
364 else if (type_ == "equation") {
366 os << "\\begin{equation" << star(n) << "}\n";
371 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
372 || type_ == "gather" || type_ == "multline")
373 os << "\\begin{" << type_ << star(n) << "}\n";
375 else if (type_ == "alignat" || type_ == "xalignat")
376 os << "\\begin{" << type_ << star(n) << '}'
377 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
379 else if (type_ == "xxalignat")
380 os << "\\begin{" << type_ << '}'
381 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
384 os << "\\begin{unknown" << star(n) << '}';
388 void MathHullInset::footer_write(WriteStream & os) const
390 bool n = numberedType();
395 else if (type_ == "simple")
398 else if (type_ == "equation")
400 os << "\\end{equation" << star(n) << "}\n";
404 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
405 || type_ == "alignat" || type_ == "xalignat"
406 || type_ == "gather" || type_ == "multline")
407 os << "\\end{" << type_ << star(n) << "}\n";
409 else if (type_ == "xxalignat")
410 os << "\\end{" << type_ << "}\n";
413 os << "\\end{unknown" << star(n) << '}';
417 bool MathHullInset::colChangeOK() const
420 type_ == "align" || type_ == "flalign" ||type_ == "alignat" ||
421 type_ == "xalignat" || type_ == "xxalignat";
425 void MathHullInset::addRow(row_type row)
427 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
428 label_.insert(label_.begin() + row + 1, string());
429 MathGridInset::addRow(row);
433 void MathHullInset::delRow(row_type row)
437 MathGridInset::delRow(row);
438 nonum_.erase(nonum_.begin() + row);
439 label_.erase(label_.begin() + row);
443 void MathHullInset::addCol(col_type col)
446 MathGridInset::addCol(col);
448 lyxerr << "Can't change number of columns in '" << type_ << "'" << endl;
452 void MathHullInset::delCol(col_type col)
455 MathGridInset::delCol(col);
457 lyxerr << "Can't change number of columns in '" << type_ << "'" << endl;
461 string MathHullInset::nicelabel(row_type row) const
465 if (label_[row].empty())
466 return string("(#)");
467 return '(' + label_[row] + ')';
471 void MathHullInset::glueall()
474 for (idx_type i = 0; i < nargs(); ++i)
476 *this = MathHullInset("simple");
482 string const & MathHullInset::getType() const
488 void MathHullInset::setType(string const & type)
496 void MathHullInset::mutate(string const & newtype)
498 //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
500 // we try to move along the chain
501 // none <-> simple <-> equation <-> eqnarray
503 if (newtype == "dump") {
507 else if (newtype == type_) {
511 else if (type_ == "none") {
517 else if (type_ == "simple") {
518 if (newtype == "none") {
527 else if (type_ == "equation") {
528 if (smaller(newtype, type_)) {
531 } else if (newtype == "eqnarray") {
532 MathGridInset::addCol(1);
533 MathGridInset::addCol(1);
535 // split it "nicely" on the firest relop
536 pos_type pos = firstRelOp(cell(0));
537 cell(1) = MathArray(cell(0).begin() + pos, cell(0).end());
538 cell(0).erase(pos, cell(0).size());
540 if (cell(1).size()) {
541 cell(2) = MathArray(cell(1).begin() + 1, cell(1).end());
542 cell(1).erase(1, cell(1).size());
546 } else if (newtype == "multline" || newtype == "gather") {
550 MathGridInset::addCol(1);
552 pos_type pos = firstRelOp(cell(0));
554 cell(0).erase(pos, cell(0).size());
555 cell(1).erase(0, pos);
561 else if (type_ == "eqnarray") {
562 if (smaller(newtype, type_)) {
563 // set correct (no)numbering
564 bool allnonum = true;
565 for (row_type row = 0; row < nrows(); ++row)
569 // set first non-empty label
571 for (row_type row = 0; row < nrows(); ++row) {
572 if (!label_[row].empty()) {
579 nonum_[0] = allnonum;
582 } else { // align & Co.
583 for (row_type row = 0; row < nrows(); ++row) {
584 idx_type c = 3 * row + 1;
585 cell(c).append(cell(c + 1));
587 MathGridInset::delCol(2);
593 else if (type_ == "align") {
594 if (smaller(newtype, type_)) {
595 MathGridInset::addCol(1);
603 else if (type_ == "multline") {
604 if (newtype == "gather" || newtype == "align" ||
605 newtype == "xalignat" || newtype == "xxalignat" || newtype == "flalign")
607 else if (newtype == "eqnarray") {
608 MathGridInset::addCol(1);
609 MathGridInset::addCol(1);
612 lyxerr << "mutation from '" << type_
613 << "' to '" << newtype << "' not implemented" << endl;
617 else if (type_ == "gather") {
618 if (newtype == "multline") {
621 lyxerr << "mutation from '" << type_
622 << "' to '" << newtype << "' not implemented" << endl;
627 lyxerr << "mutation from '" << type_
628 << "' to '" << newtype << "' not implemented" << endl;
633 string MathHullInset::eolString(row_type row, bool fragile) const
636 if (numberedType()) {
637 if (!label_[row].empty() && !nonum_[row])
638 res += "\\label{" + label_[row] + '}';
639 if (nonum_[row] && (type_ != "multline"))
640 res += "\\nonumber ";
642 return res + MathGridInset::eolString(row, fragile);
646 void MathHullInset::write(WriteStream & os) const
649 MathGridInset::write(os);
654 void MathHullInset::normalize(NormalStream & os) const
656 os << "[formula " << type_ << ' ';
657 MathGridInset::normalize(os);
662 void MathHullInset::mathmlize(MathMLStream & os) const
664 MathGridInset::mathmlize(os);
668 void MathHullInset::infoize(std::ostream & os) const
670 os << "Type: " << type_;
674 void MathHullInset::check() const
676 Assert(nonum_.size() == nrows());
677 Assert(label_.size() == nrows());
681 void MathHullInset::doExtern
682 (FuncRequest const & func, idx_type & idx, pos_type & pos)
686 istringstream iss(func.argument.c_str());
687 iss >> lang >> extra;
692 #warning temporarily disabled
696 // lyxerr << "use selection: " << ar << endl;
697 // insert(pipeThroughExtern(lang, extra, ar));
703 eq.push_back(MathAtom(new MathCharInset('=')));
705 // go to first item in line
706 idx -= idx % ncols();
709 if (getType() == "simple") {
710 size_type pos = cell(idx).find_last(eq);
712 if (mathcursor && mathcursor->selection()) {
713 asArray(mathcursor->grabAndEraseSelection(), ar);
714 } else if (pos == cell(idx).size()) {
716 lyxerr << "use whole cell: " << ar << endl;
718 ar = MathArray(cell(idx).begin() + pos + 1, cell(idx).end());
719 lyxerr << "use partial cell form pos: " << pos << endl;
721 cell(idx).append(eq);
722 cell(idx).append(pipeThroughExtern(lang, extra, ar));
723 pos = cell(idx).size();
727 if (getType() == "equation") {
728 lyxerr << "use equation inset" << endl;
730 MathArray & ar = cell(idx);
731 lyxerr << "use cell: " << ar << endl;
733 cell(idx + 2) = pipeThroughExtern(lang, extra, ar);
734 // move to end of line
736 pos = cell(idx).size();
741 lyxerr << "use eqnarray" << endl;
742 idx -= idx % ncols();
745 MathArray ar = cell(idx);
746 lyxerr << "use cell: " << ar << endl;
748 #warning temporarily disabled
752 cell(idx + 3) = pipeThroughExtern(lang, extra, ar);
754 pos = cell(idx).size();
759 dispatch_result MathHullInset::dispatch
760 (FuncRequest const & cmd, idx_type & idx, pos_type & pos)
762 switch (cmd.action) {
765 if (type_ == "simple" || type_ == "equation") {
769 return DISPATCHED_POP;
771 return MathGridInset::dispatch(cmd, idx, pos);
773 case LFUN_MATH_NUMBER:
774 //lyxerr << "toggling all numbers" << endl;
776 //recordUndo(bv, Undo::INSERT);
777 bool old = numberedType();
778 if (type_ == "multline")
779 numbered(nrows() - 1, !old);
781 for (row_type row = 0; row < nrows(); ++row)
783 //bv->owner()->message(old ? _("No number") : _("Number"));
787 case LFUN_MATH_NONUMBER:
789 row_type r = (type_ == "multline") ? nrows() - 1 : row(idx);
790 //recordUndo(bv, Undo::INSERT);
791 bool old = numbered(r);
792 //bv->owner()->message(old ? _("No number") : _("Number"));
797 case LFUN_INSERT_LABEL: {
798 row_type r = (type_ == "multline") ? nrows() - 1 : row(idx);
799 string old_label = label(r);
800 string new_label = cmd.argument;
802 if (new_label.empty()) {
803 string const default_label =
804 (lyxrc.label_init_length >= 0) ? "eq:" : "";
805 pair<bool, string> const res = old_label.empty()
806 ? Alert::askForText(_("Enter new label to insert:"), default_label)
807 : Alert::askForText(_("Enter label:"), old_label);
810 new_label = trim(res.second);
813 //if (new_label == old_label)
814 // break; // Nothing to do
816 if (!new_label.empty())
822 case LFUN_MATH_EXTERN:
823 doExtern(cmd, idx, pos);
824 return DISPATCHED_POP;
826 case LFUN_MATH_MUTATE: {
827 row_type r = row(idx);
828 col_type c = col(idx);
829 mutate(cmd.argument);
830 idx = r * ncols() + c;
833 if (pos > cell(idx).size())
834 pos = cell(idx).size();
835 return DISPATCHED_POP;
838 case LFUN_MATH_DISPLAY: {
839 mutate(type_ == "simple" ? "equation" : "simple");
841 pos = cell(idx).size();
842 return DISPATCHED_POP;
846 return MathGridInset::dispatch(cmd, idx, pos);
851 string MathHullInset::fileInsetLabel() const