7 #include "math_hullinset.h"
8 #include "math_mathmlstream.h"
9 #include "math_streamstr.h"
10 #include "math_cursor.h"
11 #include "math_support.h"
12 #include "math_extern.h"
13 #include "math_charinset.h"
15 #include "textpainter.h"
16 #include "funcrequest.h"
18 #include "LaTeXFeatures.h"
19 #include "support/LAssert.h"
20 #include "frontends/Painter.h"
22 #include "frontends/Alert.h"
25 #include "BufferView.h"
36 int getCols(string const & type)
38 if (type == "eqnarray")
42 if (type == "flalign")
44 if (type == "alignat")
46 if (type == "xalignat")
48 if (type == "xxalignat")
54 // returns position of first relation operator in the array
55 // used for "intelligent splitting"
56 MathArray::size_type firstRelOp(MathArray const & ar)
58 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
60 return it - ar.begin();
65 char const * star(bool numbered)
67 return numbered ? "" : "*";
71 int typecode(string const & s)
73 if (s == "none") return 0;
74 if (s == "simple") return 1;
75 if (s == "equation") return 2;
76 if (s == "eqnarray") return 3;
77 if (s == "align") return 4;
78 if (s == "alignat") return 5;
79 if (s == "xalignat") return 6;
80 if (s == "xxalignat") return 7;
81 if (s == "multline") return 8;
82 if (s == "gather") return 9;
83 if (s == "flalign") return 10;
84 lyxerr << "unknown hull type '" << s << "'\n";
88 bool smaller(string const & s, string const & t)
90 return typecode(s) < typecode(t);
94 } // end anon namespace
97 MathHullInset::MathHullInset()
98 : MathGridInset(1, 1), type_("none"), nonum_(1), label_(1)
104 MathHullInset::MathHullInset(string const & type)
105 : MathGridInset(getCols(type), 1), type_(type), nonum_(1), label_(1)
111 MathInset * MathHullInset::clone() const
113 return new MathHullInset(*this);
117 MathInset::mode_type MathHullInset::currentMode() const
120 return UNDECIDED_MODE;
121 // definitely math mode ...
126 bool MathHullInset::idxFirst(idx_type & idx, pos_type & pos) const
134 bool MathHullInset::idxLast(idx_type & idx, pos_type & pos) const
137 pos = cell(idx).size();
142 char MathHullInset::defaultColAlign(col_type col)
144 if (type_ == "eqnarray")
146 if (typecode(type_) >= typecode("align"))
147 return "rl"[col & 1];
152 int MathHullInset::defaultColSpace(col_type col)
154 if (type_ == "align" || type_ == "alignat")
156 if (type_ == "xalignat")
157 return (col & 1) ? 20 : 0;
158 if (type_ == "xxalignat" || type_ == "flalign")
159 return (col & 1) ? 40 : 0;
164 char const * MathHullInset::standardFont() const
167 return "lyxnochange";
172 void MathHullInset::metrics(MathMetricsInfo & mi) const
174 MathFontSetChanger dummy1(mi.base, standardFont());
175 MathStyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
177 // let the cells adjust themselves
178 MathGridInset::metrics(mi);
185 if (numberedType()) {
186 MathFontSetChanger 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(MathPainterInfo & pi, int x, int y) const
209 MathFontSetChanger dummy1(pi.base, standardFont());
210 MathStyleChanger 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 MathFontSetChanger 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";
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_ == "simple")
314 if (type_ == "xxalignat")
316 for (row_type row = 0; row < nrows(); ++row)
323 void MathHullInset::validate(LaTeXFeatures & features) const
326 features.require("amsmath");
329 // Validation is necessary only if not using AMS math.
330 // To be safe, we will always run mathedvalidate.
331 //if (features.amsstyle)
334 features.require("boldsymbol");
335 //features.binom = true;
337 MathNestInset::validate(features);
341 void MathHullInset::header_write(WriteStream & os) const
343 bool n = numberedType();
348 else if (type_ == "simple") {
354 else if (type_ == "equation") {
356 os << "\\begin{equation" << star(n) << "}\n";
361 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign")
362 os << "\\begin{" << type_ << star(n) << "}\n";
364 else if (type_ == "alignat" || type_ == "xalignat")
365 os << "\\begin{" << type_ << star(n) << '}'
366 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
368 else if (type_ == "xxalignat")
369 os << "\\begin{" << type_ << '}'
370 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
372 else if (type_ == "multline" || type_ == "gather")
373 os << "\\begin{" << type_ << "}\n";
376 os << "\\begin{unknown" << star(n) << '}';
380 void MathHullInset::footer_write(WriteStream & os) const
382 bool n = numberedType();
387 else if (type_ == "simple")
390 else if (type_ == "equation")
392 os << "\\end{equation" << star(n) << "}\n";
396 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
397 || type_ == "alignat" || type_ == "xalignat")
398 os << "\\end{" << type_ << star(n) << "}\n";
400 else if (type_ == "xxalignat" || type_ == "multline" || type_ == "gather")
401 os << "\\end{" << type_ << "}\n";
404 os << "\\end{unknown" << star(n) << '}';
408 bool MathHullInset::colChangeOK() const
411 type_ == "align" || type_ == "flalign" ||type_ == "alignat" ||
412 type_ == "xalignat" || type_ == "xxalignat";
416 void MathHullInset::addRow(row_type row)
418 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
419 label_.insert(label_.begin() + row + 1, string());
420 MathGridInset::addRow(row);
424 void MathHullInset::delRow(row_type row)
428 MathGridInset::delRow(row);
429 nonum_.erase(nonum_.begin() + row);
430 label_.erase(label_.begin() + row);
434 void MathHullInset::addCol(col_type col)
437 MathGridInset::addCol(col);
439 lyxerr << "Can't change number of columns in '" << type_ << "'\n";
443 void MathHullInset::delCol(col_type col)
446 MathGridInset::delCol(col);
448 lyxerr << "Can't change number of columns in '" << type_ << "'\n";
452 string MathHullInset::nicelabel(row_type row) const
456 if (label_[row].empty())
457 return string("(#)");
458 return '(' + label_[row] + ')';
462 void MathHullInset::glueall()
465 for (idx_type i = 0; i < nargs(); ++i)
467 *this = MathHullInset("simple");
473 string const & MathHullInset::getType() const
479 void MathHullInset::setType(string const & type)
487 void MathHullInset::mutate(string const & newtype)
489 //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'\n";
491 // we try to move along the chain
492 // none <-> simple <-> equation <-> eqnarray
494 if (newtype == "dump") {
498 else if (newtype == type_) {
502 else if (type_ == "none") {
508 else if (type_ == "simple") {
509 if (newtype == "none") {
518 else if (type_ == "equation") {
519 if (smaller(newtype, type_)) {
522 } else if (newtype == "eqnarray") {
523 MathGridInset::addCol(1);
524 MathGridInset::addCol(1);
526 // split it "nicely" on the firest relop
527 pos_type pos = firstRelOp(cell(0));
528 cell(1) = MathArray(cell(0).begin() + pos, cell(0).end());
529 cell(0).erase(pos, cell(0).size());
531 if (cell(1).size()) {
532 cell(2) = MathArray(cell(1).begin() + 1, cell(1).end());
533 cell(1).erase(1, cell(1).size());
537 } else if (newtype == "multline" || newtype == "gather") {
541 MathGridInset::addCol(1);
543 pos_type pos = firstRelOp(cell(0));
545 cell(0).erase(pos, cell(0).size());
546 cell(1).erase(0, pos);
552 else if (type_ == "eqnarray") {
553 if (smaller(newtype, type_)) {
554 // set correct (no)numbering
555 bool allnonum = true;
556 for (row_type row = 0; row < nrows(); ++row)
560 // set first non-empty label
562 for (row_type row = 0; row < nrows(); ++row) {
563 if (!label_[row].empty()) {
570 nonum_[0] = allnonum;
573 } else { // align & Co.
574 for (row_type row = 0; row < nrows(); ++row) {
575 idx_type c = 3 * row + 1;
576 cell(c).append(cell(c + 1));
578 MathGridInset::delCol(2);
584 else if (type_ == "align") {
585 if (smaller(newtype, type_)) {
586 MathGridInset::addCol(1);
594 else if (type_ == "multline") {
595 if (newtype == "gather" || newtype == "align" ||
596 newtype == "xalignat" || newtype == "xxalignat" || newtype == "flalign")
598 else if (newtype == "eqnarray") {
599 MathGridInset::addCol(1);
600 MathGridInset::addCol(1);
603 lyxerr << "mutation from '" << type_
604 << "' to '" << newtype << "' not implemented" << endl;
608 else if (type_ == "gather") {
609 if (newtype == "multline") {
612 lyxerr << "mutation from '" << type_
613 << "' to '" << newtype << "' not implemented" << endl;
618 lyxerr << "mutation from '" << type_
619 << "' to '" << newtype << "' not implemented" << endl;
624 string MathHullInset::eolString(row_type row, bool fragile) const
627 if (numberedType()) {
628 if (!label_[row].empty() && !nonum_[row])
629 res += "\\label{" + label_[row] + '}';
631 res += "\\nonumber ";
633 return res + MathGridInset::eolString(row, fragile);
637 void MathHullInset::write(WriteStream & os) const
640 MathGridInset::write(os);
645 void MathHullInset::normalize(NormalStream & os) const
647 os << "[formula " << type_ << ' ';
648 MathGridInset::normalize(os);
653 void MathHullInset::mathmlize(MathMLStream & os) const
655 MathGridInset::mathmlize(os);
659 void MathHullInset::infoize(std::ostream & os) const
661 os << "Type: " << type_;
665 void MathHullInset::check() const
667 lyx::Assert(nonum_.size() == nrows());
668 lyx::Assert(label_.size() == nrows());
672 void MathHullInset::doExtern
673 (FuncRequest const & func, idx_type & idx, pos_type & pos)
677 istringstream iss(func.argument.c_str());
678 iss >> lang >> extra;
683 #warning temporarily disabled
687 // lyxerr << "use selection: " << ar << "\n";
688 // insert(pipeThroughExtern(lang, extra, ar));
694 eq.push_back(MathAtom(new MathCharInset('=')));
696 // go to first item in line
697 idx -= idx % ncols();
700 if (getType() == "simple") {
701 size_type pos = cell(idx).find_last(eq);
703 if (mathcursor && mathcursor->selection()) {
704 ar = asArray(mathcursor->grabAndEraseSelection());
705 } else if (pos == cell(idx).size()) {
707 lyxerr << "use whole cell: " << ar << "\n";
709 ar = MathArray(cell(idx).begin() + pos + 1, cell(idx).end());
710 lyxerr << "use partial cell form pos: " << pos << "\n";
712 cell(idx).append(eq);
713 cell(idx).append(pipeThroughExtern(lang, extra, ar));
714 pos = cell(idx).size();
718 if (getType() == "equation") {
719 lyxerr << "use equation inset\n";
721 MathArray & ar = cell(idx);
722 lyxerr << "use cell: " << ar << "\n";
724 cell(idx + 2) = pipeThroughExtern(lang, extra, ar);
725 // move to end of line
727 pos = cell(idx).size();
732 lyxerr << "use eqnarray\n";
733 idx -= idx % ncols();
736 MathArray ar = cell(idx);
737 lyxerr << "use cell: " << ar << "\n";
739 #warning temporarily disabled
743 cell(idx + 3) = pipeThroughExtern(lang, extra, ar);
745 pos = cell(idx).size();
750 MathInset::result_type MathHullInset::dispatch
751 (FuncRequest const & cmd, idx_type & idx, pos_type & pos)
753 switch (cmd.action) {
756 if (type_ == "simple" || type_ == "equation") {
760 return DISPATCHED_POP;
762 return MathGridInset::dispatch(cmd, idx, pos);
764 case LFUN_MATH_NUMBER:
765 //lyxerr << "toggling all numbers\n";
767 //bv->lockedInsetStoreUndo(Undo::INSERT);
768 bool old = numberedType();
769 for (row_type row = 0; row < nrows(); ++row)
771 //bv->owner()->message(old ? _("No number") : _("Number"));
772 //updateLocal(bv, true);
776 case LFUN_MATH_NONUMBER:
778 //bv->lockedInsetStoreUndo(Undo::INSERT);
779 bool old = numbered(row(idx));
780 //bv->owner()->message(old ? _("No number") : _("Number"));
781 numbered(row(idx), !old);
782 //updateLocal(bv, true);
786 case LFUN_INSERT_LABEL: {
787 row_type r = row(idx);
788 string old_label = label(r);
789 string new_label = cmd.argument;
791 if (new_label.empty()) {
792 string const default_label =
793 (lyxrc.label_init_length >= 0) ? "eq:" : "";
794 pair<bool, string> const res = old_label.empty()
795 ? Alert::askForText(_("Enter new label to insert:"), default_label)
796 : Alert::askForText(_("Enter label:"), old_label);
799 new_label = trim(res.second);
802 //if (new_label == old_label)
803 // break; // Nothing to do
805 if (!new_label.empty())
808 #warning FIXME: please check you really mean repaint() ... is it needed,
809 #warning and if so, should it be update() instead ?
810 if (!new_label.empty()
811 && cmd.view()->ChangeRefsIfUnique(old_label, new_label))
812 cmd.view()->repaint();
818 case LFUN_MATH_EXTERN:
819 doExtern(cmd, idx, pos);
820 return DISPATCHED_POP;
822 case LFUN_MATH_MUTATE: {
823 row_type r = row(idx);
824 col_type c = col(idx);
825 mutate(cmd.argument);
826 idx = r * ncols() + c;
829 if (pos > cell(idx).size())
830 pos = cell(idx).size();
831 return DISPATCHED_POP;
834 case LFUN_MATH_DISPLAY: {
835 mutate(type_ == "simple" ? "equation" : "simple");
837 pos = cell(idx).size();
838 return DISPATCHED_POP;
842 return MathGridInset::dispatch(cmd, idx, pos);