4 #include "math_hullinset.h"
5 #include "math_mathmlstream.h"
6 #include "math_streamstr.h"
7 #include "math_cursor.h"
8 #include "math_support.h"
9 #include "math_extern.h"
10 #include "math_charinset.h"
12 #include "textpainter.h"
13 #include "funcrequest.h"
15 #include "LaTeXFeatures.h"
16 #include "support/LAssert.h"
17 #include "frontends/Painter.h"
19 #include "frontends/Alert.h"
22 #include "BufferView.h"
33 int getCols(string const & type)
35 if (type == "eqnarray")
39 if (type == "flalign")
41 if (type == "alignat")
43 if (type == "xalignat")
45 if (type == "xxalignat")
51 // returns position of first relation operator in the array
52 // used for "intelligent splitting"
53 MathArray::size_type firstRelOp(MathArray const & ar)
55 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
57 return it - ar.begin();
62 char const * star(bool numbered)
64 return numbered ? "" : "*";
68 int typecode(string const & s)
70 if (s == "none") return 0;
71 if (s == "simple") return 1;
72 if (s == "chemistry") return 2;
73 if (s == "equation") return 3;
74 if (s == "eqnarray") return 4;
75 if (s == "align") return 5;
76 if (s == "alignat") return 6;
77 if (s == "xalignat") return 7;
78 if (s == "xxalignat") return 8;
79 if (s == "multline") return 9;
80 if (s == "gather") return 10;
81 if (s == "flalign") return 11;
82 lyxerr << "unknown hull type '" << s << "'\n";
86 bool smaller(string const & s, string const & t)
88 return typecode(s) < typecode(t);
92 } // end anon namespace
95 MathHullInset::MathHullInset()
96 : MathGridInset(1, 1), type_("none"), nonum_(1), label_(1)
102 MathHullInset::MathHullInset(string const & type)
103 : MathGridInset(getCols(type), 1), type_(type), nonum_(1), label_(1)
109 MathInset * MathHullInset::clone() const
111 return new MathHullInset(*this);
115 MathInset::mode_type MathHullInset::currentMode() const
118 return UNDECIDED_MODE;
119 // definitely math mode ...
124 bool MathHullInset::idxFirst(idx_type & idx, pos_type & pos) const
132 bool MathHullInset::idxLast(idx_type & idx, pos_type & pos) const
135 pos = cell(idx).size();
140 char MathHullInset::defaultColAlign(col_type col)
142 if (type_ == "eqnarray")
144 if (typecode(type_) >= typecode("align"))
145 return "rl"[col & 1];
150 int MathHullInset::defaultColSpace(col_type col)
152 if (type_ == "align" || type_ == "alignat")
154 if (type_ == "xalignat")
155 return (col & 1) ? 20 : 0;
156 if (type_ == "xxalignat" || type_ == "flalign")
157 return (col & 1) ? 40 : 0;
162 char const * MathHullInset::standardFont() const
165 return "lyxnochange";
166 if (type_ == "chemistry")
172 void MathHullInset::metrics(MetricsInfo & mi) const
174 FontSetChanger dummy1(mi.base, standardFont());
175 StyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
177 // let the cells adjust themselves
178 MathGridInset::metrics(mi);
185 if (numberedType()) {
186 FontSetChanger dummy(mi.base, "mathbf");
188 for (row_type row = 0; row < nrows(); ++row)
189 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
195 // make it at least as high as the current font
198 math_font_max_dim(mi.base.font, asc, des);
199 dim_.a = max(dim_.a, asc);
200 dim_.d = max(dim_.d, des);
207 void MathHullInset::draw(PainterInfo & pi, int x, int y) const
209 FontSetChanger dummy1(pi.base, standardFont());
210 StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
211 MathGridInset::draw(pi, x + 1, y);
213 if (numberedType()) {
214 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
215 for (row_type row = 0; row < nrows(); ++row) {
216 int const yy = y + rowinfo_[row].offset_;
217 FontSetChanger dummy(pi.base, "mathrm");
218 drawStr(pi, pi.base.font, xx, yy, nicelabel(row));
222 drawMarkers2(pi, x, y);
226 void MathHullInset::metricsT(TextMetricsInfo const & mi) const
229 MathGridInset::metricsT(mi);
232 WriteStream wi(os, false, true);
234 dim_.w = os.str().size();
241 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
244 MathGridInset::drawT(pain, x, y);
247 WriteStream wi(os, false, true);
249 pain.draw(x, y, os.str().c_str());
254 string MathHullInset::label(row_type row) const
256 row_type n = nrows();
257 lyx::Assert(row < n);
262 void MathHullInset::label(row_type row, string const & label)
264 //lyxerr << "setting label '" << label << "' for row " << row << endl;
269 void MathHullInset::numbered(row_type row, bool num)
275 bool MathHullInset::numbered(row_type row) const
281 bool MathHullInset::ams() const
285 type_ == "flalign" ||
286 type_ == "multline" ||
288 type_ == "alignat" ||
289 type_ == "xalignat" ||
290 type_ == "xxalignat";
294 bool MathHullInset::display() const
296 return type_ != "simple" && type_ != "none" && type_ != "chemistry";
300 void MathHullInset::getLabelList(std::vector<string> & labels) const
302 for (row_type row = 0; row < nrows(); ++row)
303 if (!label_[row].empty() && nonum_[row] != 1)
304 labels.push_back(label_[row]);
308 bool MathHullInset::numberedType() const
312 if (type_ == "chemistry")
314 if (type_ == "simple")
316 if (type_ == "xxalignat")
318 for (row_type row = 0; row < nrows(); ++row)
325 void MathHullInset::validate(LaTeXFeatures & features) const
328 features.require("amsmath");
331 // Validation is necessary only if not using AMS math.
332 // To be safe, we will always run mathedvalidate.
333 //if (features.amsstyle)
336 features.require("boldsymbol");
337 //features.binom = true;
339 MathGridInset::validate(features);
343 void MathHullInset::header_write(WriteStream & os) const
345 bool n = numberedType();
350 else if (type_ == "simple") {
356 else if (type_ == "chemistry")
359 else if (type_ == "equation") {
361 os << "\\begin{equation" << star(n) << "}\n";
366 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
367 || type_ == "gather" || type_ == "multline")
368 os << "\\begin{" << type_ << star(n) << "}\n";
370 else if (type_ == "alignat" || type_ == "xalignat")
371 os << "\\begin{" << type_ << star(n) << '}'
372 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
374 else if (type_ == "xxalignat")
375 os << "\\begin{" << type_ << '}'
376 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
379 os << "\\begin{unknown" << star(n) << '}';
383 void MathHullInset::footer_write(WriteStream & os) const
385 bool n = numberedType();
390 else if (type_ == "simple")
393 else if (type_ == "chemistry")
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_ << "'\n";
450 void MathHullInset::delCol(col_type col)
453 MathGridInset::delCol(col);
455 lyxerr << "Can't change number of columns in '" << type_ << "'\n";
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 << "'\n";
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 (newtype == "none" || newtype == "chemistry") {
514 else if (newtype == "simple") {
515 if (type_ != "none" && type_ != "chemistry") {
521 else if (newtype == "equation" && smaller(type_, newtype)) {
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;
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 lyx::Assert(nonum_.size() == nrows());
677 lyx::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 << "\n";
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 ar = asArray(mathcursor->grabAndEraseSelection());
714 } else if (pos == cell(idx).size()) {
716 lyxerr << "use whole cell: " << ar << "\n";
718 ar = MathArray(cell(idx).begin() + pos + 1, cell(idx).end());
719 lyxerr << "use partial cell form pos: " << pos << "\n";
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\n";
730 MathArray & ar = cell(idx);
731 lyxerr << "use cell: " << ar << "\n";
733 cell(idx + 2) = pipeThroughExtern(lang, extra, ar);
734 // move to end of line
736 pos = cell(idx).size();
741 lyxerr << "use eqnarray\n";
742 idx -= idx % ncols();
745 MathArray ar = cell(idx);
746 lyxerr << "use cell: " << ar << "\n";
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\n";
776 //bv->lockedInsetStoreUndo(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 //bv->lockedInsetStoreUndo(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())
819 #warning FIXME: please check you really mean repaint() ... is it needed,
820 #warning and if so, should it be update() instead ?
821 if (!new_label.empty()
822 && cmd.view()->ChangeRefsIfUnique(old_label, new_label))
823 cmd.view()->repaint();
829 case LFUN_MATH_EXTERN:
830 doExtern(cmd, idx, pos);
831 return DISPATCHED_POP;
833 case LFUN_MATH_MUTATE: {
834 row_type r = row(idx);
835 col_type c = col(idx);
836 mutate(cmd.argument);
837 idx = r * ncols() + c;
840 if (pos > cell(idx).size())
841 pos = cell(idx).size();
842 return DISPATCHED_POP;
845 case LFUN_MATH_DISPLAY: {
846 mutate(type_ == "simple" ? "equation" : "simple");
848 pos = cell(idx).size();
849 return DISPATCHED_POP;
853 return MathGridInset::dispatch(cmd, idx, pos);
860 string MathHullInset::fileInsetLabel() const
862 if (type_ == "chemistry")