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"
26 #include "frontends/Alert.h"
30 using lyx::support::trim;
36 using std::istringstream;
37 using std::ostringstream;
43 int getCols(string const & type)
45 if (type == "eqnarray")
49 if (type == "flalign")
51 if (type == "alignat")
53 if (type == "xalignat")
55 if (type == "xxalignat")
61 // returns position of first relation operator in the array
62 // used for "intelligent splitting"
63 MathArray::size_type firstRelOp(MathArray const & ar)
65 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
67 return it - ar.begin();
72 char const * star(bool numbered)
74 return numbered ? "" : "*";
78 int typecode(string const & s)
80 if (s == "none") return 0;
81 if (s == "simple") return 1;
82 if (s == "equation") return 2;
83 if (s == "eqnarray") return 3;
84 if (s == "align") return 4;
85 if (s == "alignat") return 5;
86 if (s == "xalignat") return 6;
87 if (s == "xxalignat") return 7;
88 if (s == "multline") return 8;
89 if (s == "gather") return 9;
90 if (s == "flalign") return 10;
91 lyxerr << "unknown hull type '" << s << "'" << endl;
95 bool smaller(string const & s, string const & t)
97 return typecode(s) < typecode(t);
101 } // end anon namespace
104 MathHullInset::MathHullInset()
105 : MathGridInset(1, 1), type_("none"), nonum_(1), label_(1)
111 MathHullInset::MathHullInset(string const & type)
112 : MathGridInset(getCols(type), 1), type_(type), nonum_(1), label_(1)
118 auto_ptr<InsetBase> MathHullInset::clone() const
120 return auto_ptr<InsetBase>(new MathHullInset(*this));
124 MathInset::mode_type MathHullInset::currentMode() const
127 return UNDECIDED_MODE;
128 // definitely math mode ...
133 bool MathHullInset::idxFirst(idx_type & idx, pos_type & pos) const
141 bool MathHullInset::idxLast(idx_type & idx, pos_type & pos) const
144 pos = cell(idx).size();
149 char MathHullInset::defaultColAlign(col_type col)
151 if (type_ == "eqnarray")
153 if (typecode(type_) >= typecode("align"))
154 return "rl"[col & 1];
159 int MathHullInset::defaultColSpace(col_type col)
161 if (type_ == "align" || type_ == "alignat")
163 if (type_ == "xalignat")
164 return (col & 1) ? 20 : 0;
165 if (type_ == "xxalignat" || type_ == "flalign")
166 return (col & 1) ? 40 : 0;
171 char const * MathHullInset::standardFont() const
174 return "lyxnochange";
179 void MathHullInset::metrics(MetricsInfo & mi, Dimension & dim) const
181 FontSetChanger dummy1(mi.base, standardFont());
182 StyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
184 // let the cells adjust themselves
185 MathGridInset::metrics(mi);
192 if (numberedType()) {
193 FontSetChanger dummy(mi.base, "mathbf");
195 for (row_type row = 0; row < nrows(); ++row)
196 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
202 // make it at least as high as the current font
205 math_font_max_dim(mi.base.font, asc, des);
206 dim_.asc = max(dim_.asc, asc);
207 dim_.des = max(dim_.des, des);
215 void MathHullInset::draw(PainterInfo & pi, int x, int y) const
217 FontSetChanger dummy1(pi.base, standardFont());
218 StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
219 MathGridInset::draw(pi, x + 1, y);
221 if (numberedType()) {
222 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
223 for (row_type row = 0; row < nrows(); ++row) {
224 int const yy = y + rowinfo_[row].offset_;
225 FontSetChanger dummy(pi.base, "mathrm");
226 drawStr(pi, pi.base.font, xx, yy, nicelabel(row));
230 drawMarkers2(pi, x, y);
234 void MathHullInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
237 MathGridInset::metricsT(mi, dim);
240 WriteStream wi(os, false, true);
242 dim.wid = os.str().size();
249 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
252 MathGridInset::drawT(pain, x, y);
255 WriteStream wi(os, false, true);
257 pain.draw(x, y, os.str().c_str());
262 string MathHullInset::label(row_type row) const
264 row_type n = nrows();
265 BOOST_ASSERT(row < n);
270 void MathHullInset::label(row_type row, string const & label)
272 //lyxerr << "setting label '" << label << "' for row " << row << endl;
277 void MathHullInset::numbered(row_type row, bool num)
283 bool MathHullInset::numbered(row_type row) const
289 bool MathHullInset::ams() const
293 type_ == "flalign" ||
294 type_ == "multline" ||
296 type_ == "alignat" ||
297 type_ == "xalignat" ||
298 type_ == "xxalignat";
302 bool MathHullInset::display() const
304 return type_ != "simple" && type_ != "none";
308 void MathHullInset::getLabelList(std::vector<string> & labels) const
310 for (row_type row = 0; row < nrows(); ++row)
311 if (!label_[row].empty() && nonum_[row] != 1)
312 labels.push_back(label_[row]);
316 bool MathHullInset::numberedType() const
320 if (type_ == "simple")
322 if (type_ == "xxalignat")
324 for (row_type row = 0; row < nrows(); ++row)
331 void MathHullInset::validate(LaTeXFeatures & features) const
334 features.require("amsmath");
337 // Validation is necessary only if not using AMS math.
338 // To be safe, we will always run mathedvalidate.
339 //if (features.amsstyle)
342 features.require("boldsymbol");
343 //features.binom = true;
345 MathGridInset::validate(features);
349 void MathHullInset::header_write(WriteStream & os) const
351 bool n = numberedType();
356 else if (type_ == "simple") {
362 else if (type_ == "equation") {
364 os << "\\begin{equation" << star(n) << "}\n";
369 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
370 || type_ == "gather" || type_ == "multline")
371 os << "\\begin{" << type_ << star(n) << "}\n";
373 else if (type_ == "alignat" || type_ == "xalignat")
374 os << "\\begin{" << type_ << star(n) << '}'
375 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
377 else if (type_ == "xxalignat")
378 os << "\\begin{" << type_ << '}'
379 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
382 os << "\\begin{unknown" << star(n) << '}';
386 void MathHullInset::footer_write(WriteStream & os) const
388 bool n = numberedType();
393 else if (type_ == "simple")
396 else if (type_ == "equation")
398 os << "\\end{equation" << star(n) << "}\n";
402 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
403 || type_ == "alignat" || type_ == "xalignat"
404 || type_ == "gather" || type_ == "multline")
405 os << "\\end{" << type_ << star(n) << "}\n";
407 else if (type_ == "xxalignat")
408 os << "\\end{" << type_ << "}\n";
411 os << "\\end{unknown" << star(n) << '}';
415 bool MathHullInset::colChangeOK() const
418 type_ == "align" || type_ == "flalign" ||type_ == "alignat" ||
419 type_ == "xalignat" || type_ == "xxalignat";
423 void MathHullInset::addRow(row_type row)
425 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
426 label_.insert(label_.begin() + row + 1, string());
427 MathGridInset::addRow(row);
431 void MathHullInset::delRow(row_type row)
435 MathGridInset::delRow(row);
436 nonum_.erase(nonum_.begin() + row);
437 label_.erase(label_.begin() + row);
441 void MathHullInset::addCol(col_type col)
444 MathGridInset::addCol(col);
446 lyxerr << "Can't change number of columns in '" << type_ << "'" << endl;
450 void MathHullInset::delCol(col_type col)
453 MathGridInset::delCol(col);
455 lyxerr << "Can't change number of columns in '" << type_ << "'" << endl;
459 string MathHullInset::nicelabel(row_type row) const
463 if (label_[row].empty())
464 return string("(#)");
465 return '(' + label_[row] + ')';
469 void MathHullInset::glueall()
472 for (idx_type i = 0; i < nargs(); ++i)
474 *this = MathHullInset("simple");
480 string const & MathHullInset::getType() const
486 void MathHullInset::setType(string const & type)
494 void MathHullInset::mutate(string const & newtype)
496 //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
498 // we try to move along the chain
499 // none <-> simple <-> equation <-> eqnarray
501 if (newtype == "dump") {
505 else if (newtype == type_) {
509 else if (type_ == "none") {
515 else if (type_ == "simple") {
516 if (newtype == "none") {
525 else if (type_ == "equation") {
526 if (smaller(newtype, type_)) {
529 } else if (newtype == "eqnarray") {
530 MathGridInset::addCol(1);
531 MathGridInset::addCol(1);
533 // split it "nicely" on the firest relop
534 pos_type pos = firstRelOp(cell(0));
535 cell(1) = MathArray(cell(0).begin() + pos, cell(0).end());
536 cell(0).erase(pos, cell(0).size());
538 if (cell(1).size()) {
539 cell(2) = MathArray(cell(1).begin() + 1, cell(1).end());
540 cell(1).erase(1, cell(1).size());
544 } else if (newtype == "multline" || newtype == "gather") {
548 MathGridInset::addCol(1);
550 pos_type pos = firstRelOp(cell(0));
552 cell(0).erase(pos, cell(0).size());
553 cell(1).erase(0, pos);
559 else if (type_ == "eqnarray") {
560 if (smaller(newtype, type_)) {
561 // set correct (no)numbering
562 bool allnonum = true;
563 for (row_type row = 0; row < nrows(); ++row)
567 // set first non-empty label
569 for (row_type row = 0; row < nrows(); ++row) {
570 if (!label_[row].empty()) {
577 nonum_[0] = allnonum;
580 } else { // align & Co.
581 for (row_type row = 0; row < nrows(); ++row) {
582 idx_type c = 3 * row + 1;
583 cell(c).append(cell(c + 1));
585 MathGridInset::delCol(2);
591 else if (type_ == "align") {
592 if (smaller(newtype, type_)) {
593 MathGridInset::addCol(1);
601 else if (type_ == "multline") {
602 if (newtype == "gather" || newtype == "align" ||
603 newtype == "xalignat" || newtype == "xxalignat" || newtype == "flalign")
605 else if (newtype == "eqnarray") {
606 MathGridInset::addCol(1);
607 MathGridInset::addCol(1);
610 lyxerr << "mutation from '" << type_
611 << "' to '" << newtype << "' not implemented" << endl;
615 else if (type_ == "gather") {
616 if (newtype == "multline") {
619 lyxerr << "mutation from '" << type_
620 << "' to '" << newtype << "' not implemented" << endl;
625 lyxerr << "mutation from '" << type_
626 << "' to '" << newtype << "' not implemented" << endl;
631 string MathHullInset::eolString(row_type row, bool fragile) const
634 if (numberedType()) {
635 if (!label_[row].empty() && !nonum_[row])
636 res += "\\label{" + label_[row] + '}';
637 if (nonum_[row] && (type_ != "multline"))
638 res += "\\nonumber ";
640 return res + MathGridInset::eolString(row, fragile);
644 void MathHullInset::write(WriteStream & os) const
647 MathGridInset::write(os);
652 void MathHullInset::normalize(NormalStream & os) const
654 os << "[formula " << type_ << ' ';
655 MathGridInset::normalize(os);
660 void MathHullInset::mathmlize(MathMLStream & os) const
662 MathGridInset::mathmlize(os);
666 void MathHullInset::infoize(std::ostream & os) const
668 os << "Type: " << type_;
672 void MathHullInset::check() const
674 BOOST_ASSERT(nonum_.size() == nrows());
675 BOOST_ASSERT(label_.size() == nrows());
679 void MathHullInset::doExtern
680 (FuncRequest const & func, idx_type & idx, pos_type & pos)
684 istringstream iss(func.argument.c_str());
685 iss >> lang >> extra;
690 #warning temporarily disabled
694 // lyxerr << "use selection: " << ar << endl;
695 // insert(pipeThroughExtern(lang, extra, ar));
701 eq.push_back(MathAtom(new MathCharInset('=')));
703 // go to first item in line
704 idx -= idx % ncols();
707 if (getType() == "simple") {
708 size_type pos = cell(idx).find_last(eq);
710 if (mathcursor && mathcursor->selection()) {
711 asArray(mathcursor->grabAndEraseSelection(), ar);
712 } else if (pos == cell(idx).size()) {
714 lyxerr << "use whole cell: " << ar << endl;
716 ar = MathArray(cell(idx).begin() + pos + 1, cell(idx).end());
717 lyxerr << "use partial cell form pos: " << pos << endl;
719 cell(idx).append(eq);
720 cell(idx).append(pipeThroughExtern(lang, extra, ar));
721 pos = cell(idx).size();
725 if (getType() == "equation") {
726 lyxerr << "use equation inset" << endl;
728 MathArray & ar = cell(idx);
729 lyxerr << "use cell: " << ar << endl;
731 cell(idx + 2) = pipeThroughExtern(lang, extra, ar);
732 // move to end of line
734 pos = cell(idx).size();
739 lyxerr << "use eqnarray" << endl;
740 idx -= idx % ncols();
743 MathArray ar = cell(idx);
744 lyxerr << "use cell: " << ar << endl;
746 #warning temporarily disabled
750 cell(idx + 3) = pipeThroughExtern(lang, extra, ar);
752 pos = cell(idx).size();
757 dispatch_result MathHullInset::dispatch
758 (FuncRequest const & cmd, idx_type & idx, pos_type & pos)
760 switch (cmd.action) {
763 if (type_ == "simple" || type_ == "equation") {
767 return DISPATCHED_POP;
769 return MathGridInset::dispatch(cmd, idx, pos);
771 case LFUN_MATH_NUMBER:
772 //lyxerr << "toggling all numbers" << endl;
774 //recordUndo(bv, Undo::INSERT);
775 bool old = numberedType();
776 if (type_ == "multline")
777 numbered(nrows() - 1, !old);
779 for (row_type row = 0; row < nrows(); ++row)
781 //bv->owner()->message(old ? _("No number") : _("Number"));
785 case LFUN_MATH_NONUMBER:
787 row_type r = (type_ == "multline") ? nrows() - 1 : row(idx);
788 //recordUndo(bv, Undo::INSERT);
789 bool old = numbered(r);
790 //bv->owner()->message(old ? _("No number") : _("Number"));
795 case LFUN_INSERT_LABEL: {
796 row_type r = (type_ == "multline") ? nrows() - 1 : row(idx);
797 string old_label = label(r);
798 string new_label = cmd.argument;
800 if (new_label.empty()) {
801 string const default_label =
802 (lyxrc.label_init_length >= 0) ? "eq:" : "";
803 pair<bool, string> const res = old_label.empty()
804 ? Alert::askForText(_("Enter new label to insert:"), default_label)
805 : Alert::askForText(_("Enter label:"), old_label);
808 new_label = trim(res.second);
811 //if (new_label == old_label)
812 // break; // Nothing to do
814 if (!new_label.empty())
820 case LFUN_MATH_EXTERN:
821 doExtern(cmd, idx, pos);
822 return DISPATCHED_POP;
824 case LFUN_MATH_MUTATE: {
825 row_type r = row(idx);
826 col_type c = col(idx);
827 mutate(cmd.argument);
828 idx = r * ncols() + c;
831 if (pos > cell(idx).size())
832 pos = cell(idx).size();
833 return DISPATCHED_POP;
836 case LFUN_MATH_DISPLAY: {
837 mutate(type_ == "simple" ? "equation" : "simple");
839 pos = cell(idx).size();
840 return DISPATCHED_POP;
844 return MathGridInset::dispatch(cmd, idx, pos);
849 string MathHullInset::fileInsetLabel() const