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 == "equation") return 2;
73 if (s == "eqnarray") return 3;
74 if (s == "align") return 4;
75 if (s == "alignat") return 5;
76 if (s == "xalignat") return 6;
77 if (s == "xxalignat") return 7;
78 if (s == "multline") return 8;
79 if (s == "gather") return 9;
80 if (s == "flalign") return 10;
81 lyxerr << "unknown hull type '" << s << "'\n";
85 bool smaller(string const & s, string const & t)
87 return typecode(s) < typecode(t);
91 } // end anon namespace
94 MathHullInset::MathHullInset()
95 : MathGridInset(1, 1), type_("none"), nonum_(1), label_(1)
101 MathHullInset::MathHullInset(string const & type)
102 : MathGridInset(getCols(type), 1), type_(type), nonum_(1), label_(1)
108 MathInset * MathHullInset::clone() const
110 return new MathHullInset(*this);
114 MathInset::mode_type MathHullInset::currentMode() const
117 return UNDECIDED_MODE;
118 // definitely math mode ...
123 bool MathHullInset::idxFirst(idx_type & idx, pos_type & pos) const
131 bool MathHullInset::idxLast(idx_type & idx, pos_type & pos) const
134 pos = cell(idx).size();
139 char MathHullInset::defaultColAlign(col_type col)
141 if (type_ == "eqnarray")
143 if (typecode(type_) >= typecode("align"))
144 return "rl"[col & 1];
149 int MathHullInset::defaultColSpace(col_type col)
151 if (type_ == "align" || type_ == "alignat")
153 if (type_ == "xalignat")
154 return (col & 1) ? 20 : 0;
155 if (type_ == "xxalignat" || type_ == "flalign")
156 return (col & 1) ? 40 : 0;
161 char const * MathHullInset::standardFont() const
164 return "lyxnochange";
169 void MathHullInset::metrics(MathMetricsInfo & mi) const
171 MathFontSetChanger dummy1(mi.base, standardFont());
172 MathStyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
174 // let the cells adjust themselves
175 MathGridInset::metrics(mi);
182 if (numberedType()) {
183 MathFontSetChanger dummy(mi.base, "mathbf");
185 for (row_type row = 0; row < nrows(); ++row)
186 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
192 // make it at least as high as the current font
195 math_font_max_dim(mi.base.font, asc, des);
196 dim_.a = max(dim_.a, asc);
197 dim_.d = max(dim_.d, des);
204 void MathHullInset::draw(MathPainterInfo & pi, int x, int y) const
206 MathFontSetChanger dummy1(pi.base, standardFont());
207 MathStyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
208 MathGridInset::draw(pi, x + 1, y);
210 if (numberedType()) {
211 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
212 for (row_type row = 0; row < nrows(); ++row) {
213 int const yy = y + rowinfo_[row].offset_;
214 MathFontSetChanger dummy(pi.base, "mathrm");
215 drawStr(pi, pi.base.font, xx, yy, nicelabel(row));
219 drawMarkers2(pi, x, y);
223 void MathHullInset::metricsT(TextMetricsInfo const & mi) const
226 MathGridInset::metricsT(mi);
229 WriteStream wi(os, false, true);
231 dim_.w = os.str().size();
238 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
241 MathGridInset::drawT(pain, x, y);
244 WriteStream wi(os, false, true);
246 pain.draw(x, y, os.str().c_str());
251 string MathHullInset::label(row_type row) const
253 row_type n = nrows();
254 lyx::Assert(row < n);
259 void MathHullInset::label(row_type row, string const & label)
261 //lyxerr << "setting label '" << label << "' for row " << row << endl;
266 void MathHullInset::numbered(row_type row, bool num)
272 bool MathHullInset::numbered(row_type row) const
278 bool MathHullInset::ams() const
282 type_ == "flalign" ||
283 type_ == "multline" ||
285 type_ == "alignat" ||
286 type_ == "xalignat" ||
287 type_ == "xxalignat";
291 bool MathHullInset::display() const
293 return type_ != "simple" && type_ != "none";
297 void MathHullInset::getLabelList(std::vector<string> & labels) const
299 for (row_type row = 0; row < nrows(); ++row)
300 if (!label_[row].empty() && nonum_[row] != 1)
301 labels.push_back(label_[row]);
305 bool MathHullInset::numberedType() const
309 if (type_ == "simple")
311 if (type_ == "xxalignat")
313 for (row_type row = 0; row < nrows(); ++row)
320 void MathHullInset::validate(LaTeXFeatures & features) const
323 features.require("amsmath");
326 // Validation is necessary only if not using AMS math.
327 // To be safe, we will always run mathedvalidate.
328 //if (features.amsstyle)
331 features.require("boldsymbol");
332 //features.binom = true;
334 MathNestInset::validate(features);
338 void MathHullInset::header_write(WriteStream & os) const
340 bool n = numberedType();
345 else if (type_ == "simple") {
351 else if (type_ == "equation") {
353 os << "\\begin{equation" << star(n) << "}\n";
358 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
359 || type_ == "gather" || type_ == "multline")
360 os << "\\begin{" << type_ << star(n) << "}\n";
362 else if (type_ == "alignat" || type_ == "xalignat")
363 os << "\\begin{" << type_ << star(n) << '}'
364 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
366 else if (type_ == "xxalignat")
367 os << "\\begin{" << type_ << '}'
368 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
371 os << "\\begin{unknown" << star(n) << '}';
375 void MathHullInset::footer_write(WriteStream & os) const
377 bool n = numberedType();
382 else if (type_ == "simple")
385 else if (type_ == "equation")
387 os << "\\end{equation" << star(n) << "}\n";
391 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
392 || type_ == "alignat" || type_ == "xalignat"
393 || type_ == "gather" || type_ == "multline")
394 os << "\\end{" << type_ << star(n) << "}\n";
396 else if (type_ == "xxalignat")
397 os << "\\end{" << type_ << "}\n";
400 os << "\\end{unknown" << star(n) << '}';
404 bool MathHullInset::colChangeOK() const
407 type_ == "align" || type_ == "flalign" ||type_ == "alignat" ||
408 type_ == "xalignat" || type_ == "xxalignat";
412 void MathHullInset::addRow(row_type row)
414 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
415 label_.insert(label_.begin() + row + 1, string());
416 MathGridInset::addRow(row);
420 void MathHullInset::delRow(row_type row)
424 MathGridInset::delRow(row);
425 nonum_.erase(nonum_.begin() + row);
426 label_.erase(label_.begin() + row);
430 void MathHullInset::addCol(col_type col)
433 MathGridInset::addCol(col);
435 lyxerr << "Can't change number of columns in '" << type_ << "'\n";
439 void MathHullInset::delCol(col_type col)
442 MathGridInset::delCol(col);
444 lyxerr << "Can't change number of columns in '" << type_ << "'\n";
448 string MathHullInset::nicelabel(row_type row) const
452 if (label_[row].empty())
453 return string("(#)");
454 return '(' + label_[row] + ')';
458 void MathHullInset::glueall()
461 for (idx_type i = 0; i < nargs(); ++i)
463 *this = MathHullInset("simple");
469 string const & MathHullInset::getType() const
475 void MathHullInset::setType(string const & type)
483 void MathHullInset::mutate(string const & newtype)
485 //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'\n";
487 // we try to move along the chain
488 // none <-> simple <-> equation <-> eqnarray
490 if (newtype == "dump") {
494 else if (newtype == type_) {
498 else if (type_ == "none") {
504 else if (type_ == "simple") {
505 if (newtype == "none") {
514 else if (type_ == "equation") {
515 if (smaller(newtype, type_)) {
518 } else if (newtype == "eqnarray") {
519 MathGridInset::addCol(1);
520 MathGridInset::addCol(1);
522 // split it "nicely" on the firest relop
523 pos_type pos = firstRelOp(cell(0));
524 cell(1) = MathArray(cell(0).begin() + pos, cell(0).end());
525 cell(0).erase(pos, cell(0).size());
527 if (cell(1).size()) {
528 cell(2) = MathArray(cell(1).begin() + 1, cell(1).end());
529 cell(1).erase(1, cell(1).size());
533 } else if (newtype == "multline" || newtype == "gather") {
537 MathGridInset::addCol(1);
539 pos_type pos = firstRelOp(cell(0));
541 cell(0).erase(pos, cell(0).size());
542 cell(1).erase(0, pos);
548 else if (type_ == "eqnarray") {
549 if (smaller(newtype, type_)) {
550 // set correct (no)numbering
551 bool allnonum = true;
552 for (row_type row = 0; row < nrows(); ++row)
556 // set first non-empty label
558 for (row_type row = 0; row < nrows(); ++row) {
559 if (!label_[row].empty()) {
566 nonum_[0] = allnonum;
569 } else { // align & Co.
570 for (row_type row = 0; row < nrows(); ++row) {
571 idx_type c = 3 * row + 1;
572 cell(c).append(cell(c + 1));
574 MathGridInset::delCol(2);
580 else if (type_ == "align") {
581 if (smaller(newtype, type_)) {
582 MathGridInset::addCol(1);
590 else if (type_ == "multline") {
591 if (newtype == "gather" || newtype == "align" ||
592 newtype == "xalignat" || newtype == "xxalignat" || newtype == "flalign")
594 else if (newtype == "eqnarray") {
595 MathGridInset::addCol(1);
596 MathGridInset::addCol(1);
599 lyxerr << "mutation from '" << type_
600 << "' to '" << newtype << "' not implemented" << endl;
604 else if (type_ == "gather") {
605 if (newtype == "multline") {
608 lyxerr << "mutation from '" << type_
609 << "' to '" << newtype << "' not implemented" << endl;
614 lyxerr << "mutation from '" << type_
615 << "' to '" << newtype << "' not implemented" << endl;
620 string MathHullInset::eolString(row_type row, bool fragile) const
623 if (numberedType()) {
624 if (!label_[row].empty() && !nonum_[row])
625 res += "\\label{" + label_[row] + '}';
626 if (nonum_[row] && (type_ != "multline"))
627 res += "\\nonumber ";
629 return res + MathGridInset::eolString(row, fragile);
633 void MathHullInset::write(WriteStream & os) const
636 MathGridInset::write(os);
641 void MathHullInset::normalize(NormalStream & os) const
643 os << "[formula " << type_ << ' ';
644 MathGridInset::normalize(os);
649 void MathHullInset::mathmlize(MathMLStream & os) const
651 MathGridInset::mathmlize(os);
655 void MathHullInset::infoize(std::ostream & os) const
657 os << "Type: " << type_;
661 void MathHullInset::check() const
663 lyx::Assert(nonum_.size() == nrows());
664 lyx::Assert(label_.size() == nrows());
668 void MathHullInset::doExtern
669 (FuncRequest const & func, idx_type & idx, pos_type & pos)
673 istringstream iss(func.argument.c_str());
674 iss >> lang >> extra;
679 #warning temporarily disabled
683 // lyxerr << "use selection: " << ar << "\n";
684 // insert(pipeThroughExtern(lang, extra, ar));
690 eq.push_back(MathAtom(new MathCharInset('=')));
692 // go to first item in line
693 idx -= idx % ncols();
696 if (getType() == "simple") {
697 size_type pos = cell(idx).find_last(eq);
699 if (mathcursor && mathcursor->selection()) {
700 ar = asArray(mathcursor->grabAndEraseSelection());
701 } else if (pos == cell(idx).size()) {
703 lyxerr << "use whole cell: " << ar << "\n";
705 ar = MathArray(cell(idx).begin() + pos + 1, cell(idx).end());
706 lyxerr << "use partial cell form pos: " << pos << "\n";
708 cell(idx).append(eq);
709 cell(idx).append(pipeThroughExtern(lang, extra, ar));
710 pos = cell(idx).size();
714 if (getType() == "equation") {
715 lyxerr << "use equation inset\n";
717 MathArray & ar = cell(idx);
718 lyxerr << "use cell: " << ar << "\n";
720 cell(idx + 2) = pipeThroughExtern(lang, extra, ar);
721 // move to end of line
723 pos = cell(idx).size();
728 lyxerr << "use eqnarray\n";
729 idx -= idx % ncols();
732 MathArray ar = cell(idx);
733 lyxerr << "use cell: " << ar << "\n";
735 #warning temporarily disabled
739 cell(idx + 3) = pipeThroughExtern(lang, extra, ar);
741 pos = cell(idx).size();
746 MathInset::result_type MathHullInset::dispatch
747 (FuncRequest const & cmd, idx_type & idx, pos_type & pos)
749 switch (cmd.action) {
752 if (type_ == "simple" || type_ == "equation") {
756 return DISPATCHED_POP;
758 return MathGridInset::dispatch(cmd, idx, pos);
760 case LFUN_MATH_NUMBER:
761 //lyxerr << "toggling all numbers\n";
763 //bv->lockedInsetStoreUndo(Undo::INSERT);
764 bool old = numberedType();
765 if (type_ == "multline")
766 numbered(nrows() - 1, !old);
768 for (row_type row = 0; row < nrows(); ++row)
770 //bv->owner()->message(old ? _("No number") : _("Number"));
771 //updateLocal(bv, true);
775 case LFUN_MATH_NONUMBER:
777 row_type r = (type_ == "multline") ? nrows() - 1 : row(idx);
778 //bv->lockedInsetStoreUndo(Undo::INSERT);
779 bool old = numbered(r);
780 //bv->owner()->message(old ? _("No number") : _("Number"));
782 //updateLocal(bv, true);
786 case LFUN_INSERT_LABEL: {
787 row_type r = (type_ == "multline") ? nrows() - 1 : 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);