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"
23 #include "support/std_sstream.h"
24 #include "LaTeXFeatures.h"
25 #include "support/LAssert.h"
27 #include "frontends/Alert.h"
32 using namespace lyx::support;
34 using std::istringstream;
35 using std::ostringstream;
44 int getCols(string const & type)
46 if (type == "eqnarray")
50 if (type == "flalign")
52 if (type == "alignat")
54 if (type == "xalignat")
56 if (type == "xxalignat")
62 // returns position of first relation operator in the array
63 // used for "intelligent splitting"
64 MathArray::size_type firstRelOp(MathArray const & ar)
66 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
68 return it - ar.begin();
73 char const * star(bool numbered)
75 return numbered ? "" : "*";
79 int typecode(string const & s)
81 if (s == "none") return 0;
82 if (s == "simple") return 1;
83 if (s == "equation") return 2;
84 if (s == "eqnarray") return 3;
85 if (s == "align") return 4;
86 if (s == "alignat") return 5;
87 if (s == "xalignat") return 6;
88 if (s == "xxalignat") return 7;
89 if (s == "multline") return 8;
90 if (s == "gather") return 9;
91 if (s == "flalign") return 10;
92 lyxerr << "unknown hull type '" << s << "'" << endl;
96 bool smaller(string const & s, string const & t)
98 return typecode(s) < typecode(t);
102 } // end anon namespace
105 MathHullInset::MathHullInset()
106 : MathGridInset(1, 1), type_("none"), nonum_(1), label_(1)
112 MathHullInset::MathHullInset(string const & type)
113 : MathGridInset(getCols(type), 1), type_(type), nonum_(1), label_(1)
119 auto_ptr<InsetBase> MathHullInset::clone() const
121 return auto_ptr<InsetBase>(new MathHullInset(*this));
125 MathInset::mode_type MathHullInset::currentMode() const
128 return UNDECIDED_MODE;
129 // definitely math mode ...
134 bool MathHullInset::idxFirst(idx_type & idx, pos_type & pos) const
142 bool MathHullInset::idxLast(idx_type & idx, pos_type & pos) const
145 pos = cell(idx).size();
150 char MathHullInset::defaultColAlign(col_type col)
152 if (type_ == "eqnarray")
154 if (typecode(type_) >= typecode("align"))
155 return "rl"[col & 1];
160 int MathHullInset::defaultColSpace(col_type col)
162 if (type_ == "align" || type_ == "alignat")
164 if (type_ == "xalignat")
165 return (col & 1) ? 20 : 0;
166 if (type_ == "xxalignat" || type_ == "flalign")
167 return (col & 1) ? 40 : 0;
172 char const * MathHullInset::standardFont() const
175 return "lyxnochange";
180 void MathHullInset::metrics(MetricsInfo & mi, Dimension & dim) const
182 FontSetChanger dummy1(mi.base, standardFont());
183 StyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
185 // let the cells adjust themselves
186 MathGridInset::metrics(mi);
193 if (numberedType()) {
194 FontSetChanger dummy(mi.base, "mathbf");
196 for (row_type row = 0; row < nrows(); ++row)
197 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
203 // make it at least as high as the current font
206 math_font_max_dim(mi.base.font, asc, des);
207 dim_.asc = max(dim_.asc, asc);
208 dim_.des = max(dim_.des, des);
216 void MathHullInset::draw(PainterInfo & pi, int x, int y) const
218 FontSetChanger dummy1(pi.base, standardFont());
219 StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
220 MathGridInset::draw(pi, x + 1, y);
222 if (numberedType()) {
223 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
224 for (row_type row = 0; row < nrows(); ++row) {
225 int const yy = y + rowinfo_[row].offset_;
226 FontSetChanger dummy(pi.base, "mathrm");
227 drawStr(pi, pi.base.font, xx, yy, nicelabel(row));
231 drawMarkers2(pi, x, y);
235 void MathHullInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
238 MathGridInset::metricsT(mi, dim);
241 WriteStream wi(os, false, true);
243 dim.wid = os.str().size();
250 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
253 MathGridInset::drawT(pain, x, y);
256 WriteStream wi(os, false, true);
258 pain.draw(x, y, os.str().c_str());
263 string MathHullInset::label(row_type row) const
265 row_type n = nrows();
271 void MathHullInset::label(row_type row, string const & label)
273 //lyxerr << "setting label '" << label << "' for row " << row << endl;
278 void MathHullInset::numbered(row_type row, bool num)
284 bool MathHullInset::numbered(row_type row) const
290 bool MathHullInset::ams() const
294 type_ == "flalign" ||
295 type_ == "multline" ||
297 type_ == "alignat" ||
298 type_ == "xalignat" ||
299 type_ == "xxalignat";
303 bool MathHullInset::display() const
305 return type_ != "simple" && type_ != "none";
309 void MathHullInset::getLabelList(std::vector<string> & labels) const
311 for (row_type row = 0; row < nrows(); ++row)
312 if (!label_[row].empty() && nonum_[row] != 1)
313 labels.push_back(label_[row]);
317 bool MathHullInset::numberedType() const
321 if (type_ == "simple")
323 if (type_ == "xxalignat")
325 for (row_type row = 0; row < nrows(); ++row)
332 void MathHullInset::validate(LaTeXFeatures & features) const
335 features.require("amsmath");
338 // Validation is necessary only if not using AMS math.
339 // To be safe, we will always run mathedvalidate.
340 //if (features.amsstyle)
343 features.require("boldsymbol");
344 //features.binom = true;
346 MathGridInset::validate(features);
350 void MathHullInset::header_write(WriteStream & os) const
352 bool n = numberedType();
357 else if (type_ == "simple") {
363 else if (type_ == "equation") {
365 os << "\\begin{equation" << star(n) << "}\n";
370 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
371 || type_ == "gather" || type_ == "multline")
372 os << "\\begin{" << type_ << star(n) << "}\n";
374 else if (type_ == "alignat" || type_ == "xalignat")
375 os << "\\begin{" << type_ << star(n) << '}'
376 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
378 else if (type_ == "xxalignat")
379 os << "\\begin{" << type_ << '}'
380 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
383 os << "\\begin{unknown" << star(n) << '}';
387 void MathHullInset::footer_write(WriteStream & os) const
389 bool n = numberedType();
394 else if (type_ == "simple")
397 else if (type_ == "equation")
399 os << "\\end{equation" << star(n) << "}\n";
403 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
404 || type_ == "alignat" || type_ == "xalignat"
405 || type_ == "gather" || type_ == "multline")
406 os << "\\end{" << type_ << star(n) << "}\n";
408 else if (type_ == "xxalignat")
409 os << "\\end{" << type_ << "}\n";
412 os << "\\end{unknown" << star(n) << '}';
416 bool MathHullInset::colChangeOK() const
419 type_ == "align" || type_ == "flalign" ||type_ == "alignat" ||
420 type_ == "xalignat" || type_ == "xxalignat";
424 void MathHullInset::addRow(row_type row)
426 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
427 label_.insert(label_.begin() + row + 1, string());
428 MathGridInset::addRow(row);
432 void MathHullInset::delRow(row_type row)
436 MathGridInset::delRow(row);
437 nonum_.erase(nonum_.begin() + row);
438 label_.erase(label_.begin() + row);
442 void MathHullInset::addCol(col_type col)
445 MathGridInset::addCol(col);
447 lyxerr << "Can't change number of columns in '" << type_ << "'" << endl;
451 void MathHullInset::delCol(col_type col)
454 MathGridInset::delCol(col);
456 lyxerr << "Can't change number of columns in '" << type_ << "'" << endl;
460 string MathHullInset::nicelabel(row_type row) const
464 if (label_[row].empty())
465 return string("(#)");
466 return '(' + label_[row] + ')';
470 void MathHullInset::glueall()
473 for (idx_type i = 0; i < nargs(); ++i)
475 *this = MathHullInset("simple");
481 string const & MathHullInset::getType() const
487 void MathHullInset::setType(string const & type)
495 void MathHullInset::mutate(string const & newtype)
497 //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
499 // we try to move along the chain
500 // none <-> simple <-> equation <-> eqnarray
502 if (newtype == "dump") {
506 else if (newtype == type_) {
510 else if (type_ == "none") {
516 else if (type_ == "simple") {
517 if (newtype == "none") {
526 else if (type_ == "equation") {
527 if (smaller(newtype, type_)) {
530 } else if (newtype == "eqnarray") {
531 MathGridInset::addCol(1);
532 MathGridInset::addCol(1);
534 // split it "nicely" on the firest relop
535 pos_type pos = firstRelOp(cell(0));
536 cell(1) = MathArray(cell(0).begin() + pos, cell(0).end());
537 cell(0).erase(pos, cell(0).size());
539 if (cell(1).size()) {
540 cell(2) = MathArray(cell(1).begin() + 1, cell(1).end());
541 cell(1).erase(1, cell(1).size());
545 } else if (newtype == "multline" || newtype == "gather") {
549 MathGridInset::addCol(1);
551 pos_type pos = firstRelOp(cell(0));
553 cell(0).erase(pos, cell(0).size());
554 cell(1).erase(0, pos);
560 else if (type_ == "eqnarray") {
561 if (smaller(newtype, type_)) {
562 // set correct (no)numbering
563 bool allnonum = true;
564 for (row_type row = 0; row < nrows(); ++row)
568 // set first non-empty label
570 for (row_type row = 0; row < nrows(); ++row) {
571 if (!label_[row].empty()) {
578 nonum_[0] = allnonum;
581 } else { // align & Co.
582 for (row_type row = 0; row < nrows(); ++row) {
583 idx_type c = 3 * row + 1;
584 cell(c).append(cell(c + 1));
586 MathGridInset::delCol(2);
592 else if (type_ == "align") {
593 if (smaller(newtype, type_)) {
594 MathGridInset::addCol(1);
602 else if (type_ == "multline") {
603 if (newtype == "gather" || newtype == "align" ||
604 newtype == "xalignat" || newtype == "xxalignat" || newtype == "flalign")
606 else if (newtype == "eqnarray") {
607 MathGridInset::addCol(1);
608 MathGridInset::addCol(1);
611 lyxerr << "mutation from '" << type_
612 << "' to '" << newtype << "' not implemented" << endl;
616 else if (type_ == "gather") {
617 if (newtype == "multline") {
620 lyxerr << "mutation from '" << type_
621 << "' to '" << newtype << "' not implemented" << endl;
626 lyxerr << "mutation from '" << type_
627 << "' to '" << newtype << "' not implemented" << endl;
632 string MathHullInset::eolString(row_type row, bool fragile) const
635 if (numberedType()) {
636 if (!label_[row].empty() && !nonum_[row])
637 res += "\\label{" + label_[row] + '}';
638 if (nonum_[row] && (type_ != "multline"))
639 res += "\\nonumber ";
641 return res + MathGridInset::eolString(row, fragile);
645 void MathHullInset::write(WriteStream & os) const
648 MathGridInset::write(os);
653 void MathHullInset::normalize(NormalStream & os) const
655 os << "[formula " << type_ << ' ';
656 MathGridInset::normalize(os);
661 void MathHullInset::mathmlize(MathMLStream & os) const
663 MathGridInset::mathmlize(os);
667 void MathHullInset::infoize(std::ostream & os) const
669 os << "Type: " << type_;
673 void MathHullInset::check() const
675 Assert(nonum_.size() == nrows());
676 Assert(label_.size() == nrows());
680 void MathHullInset::doExtern
681 (FuncRequest const & func, idx_type & idx, pos_type & pos)
685 istringstream iss(func.argument.c_str());
686 iss >> lang >> extra;
691 #warning temporarily disabled
695 // lyxerr << "use selection: " << ar << endl;
696 // insert(pipeThroughExtern(lang, extra, ar));
702 eq.push_back(MathAtom(new MathCharInset('=')));
704 // go to first item in line
705 idx -= idx % ncols();
708 if (getType() == "simple") {
709 size_type pos = cell(idx).find_last(eq);
711 if (mathcursor && mathcursor->selection()) {
712 asArray(mathcursor->grabAndEraseSelection(), ar);
713 } else if (pos == cell(idx).size()) {
715 lyxerr << "use whole cell: " << ar << endl;
717 ar = MathArray(cell(idx).begin() + pos + 1, cell(idx).end());
718 lyxerr << "use partial cell form pos: " << pos << endl;
720 cell(idx).append(eq);
721 cell(idx).append(pipeThroughExtern(lang, extra, ar));
722 pos = cell(idx).size();
726 if (getType() == "equation") {
727 lyxerr << "use equation inset" << endl;
729 MathArray & ar = cell(idx);
730 lyxerr << "use cell: " << ar << endl;
732 cell(idx + 2) = pipeThroughExtern(lang, extra, ar);
733 // move to end of line
735 pos = cell(idx).size();
740 lyxerr << "use eqnarray" << endl;
741 idx -= idx % ncols();
744 MathArray ar = cell(idx);
745 lyxerr << "use cell: " << ar << endl;
747 #warning temporarily disabled
751 cell(idx + 3) = pipeThroughExtern(lang, extra, ar);
753 pos = cell(idx).size();
758 dispatch_result MathHullInset::dispatch
759 (FuncRequest const & cmd, idx_type & idx, pos_type & pos)
761 switch (cmd.action) {
764 if (type_ == "simple" || type_ == "equation") {
768 return DISPATCHED_POP;
770 return MathGridInset::dispatch(cmd, idx, pos);
772 case LFUN_MATH_NUMBER:
773 //lyxerr << "toggling all numbers" << endl;
775 //recordUndo(bv, Undo::INSERT);
776 bool old = numberedType();
777 if (type_ == "multline")
778 numbered(nrows() - 1, !old);
780 for (row_type row = 0; row < nrows(); ++row)
782 //bv->owner()->message(old ? _("No number") : _("Number"));
786 case LFUN_MATH_NONUMBER:
788 row_type r = (type_ == "multline") ? nrows() - 1 : row(idx);
789 //recordUndo(bv, Undo::INSERT);
790 bool old = numbered(r);
791 //bv->owner()->message(old ? _("No number") : _("Number"));
796 case LFUN_INSERT_LABEL: {
797 row_type r = (type_ == "multline") ? nrows() - 1 : row(idx);
798 string old_label = label(r);
799 string new_label = cmd.argument;
801 if (new_label.empty()) {
802 string const default_label =
803 (lyxrc.label_init_length >= 0) ? "eq:" : "";
804 pair<bool, string> const res = old_label.empty()
805 ? Alert::askForText(_("Enter new label to insert:"), default_label)
806 : Alert::askForText(_("Enter label:"), old_label);
809 new_label = trim(res.second);
812 //if (new_label == old_label)
813 // break; // Nothing to do
815 if (!new_label.empty())
821 case LFUN_MATH_EXTERN:
822 doExtern(cmd, idx, pos);
823 return DISPATCHED_POP;
825 case LFUN_MATH_MUTATE: {
826 row_type r = row(idx);
827 col_type c = col(idx);
828 mutate(cmd.argument);
829 idx = r * ncols() + c;
832 if (pos > cell(idx).size())
833 pos = cell(idx).size();
834 return DISPATCHED_POP;
837 case LFUN_MATH_DISPLAY: {
838 mutate(type_ == "simple" ? "equation" : "simple");
840 pos = cell(idx).size();
841 return DISPATCHED_POP;
845 return MathGridInset::dispatch(cmd, idx, pos);
850 string MathHullInset::fileInsetLabel() const