7 #include "math_hullinset.h"
8 #include "math_mathmlstream.h"
9 #include "math_streamstr.h"
10 #include "math_support.h"
11 #include "math_extern.h"
12 #include "math_charinset.h"
14 #include "textpainter.h"
15 #include "funcrequest.h"
17 #include "LaTeXFeatures.h"
18 #include "support/LAssert.h"
19 #include "frontends/Painter.h"
21 #include "frontends/Alert.h"
24 #include "BufferView.h"
35 int getCols(string const & type)
37 if (type == "eqnarray")
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 lyxerr << "unknown hull type '" << s << "'\n";
84 bool smaller(string const & s, string const & t)
86 return typecode(s) < typecode(t);
90 } // end anon namespace
93 MathHullInset::MathHullInset()
94 : MathGridInset(1, 1), type_("none"), nonum_(1), label_(1)
100 MathHullInset::MathHullInset(string const & type)
101 : MathGridInset(getCols(type), 1), type_(type), nonum_(1), label_(1)
107 MathInset * MathHullInset::clone() const
109 return new MathHullInset(*this);
113 MathInset::mode_type MathHullInset::currentMode() const
116 return UNDECIDED_MODE;
117 // definitely math mode ...
122 bool MathHullInset::idxFirst(idx_type & idx, pos_type & pos) const
130 bool MathHullInset::idxLast(idx_type & idx, pos_type & pos) const
133 pos = cell(idx).size();
138 char MathHullInset::defaultColAlign(col_type col)
140 if (type_ == "eqnarray")
142 if (typecode(type_) >= typecode("align"))
143 return "rl"[col & 1];
148 int MathHullInset::defaultColSpace(col_type col)
150 if (type_ == "align" || type_ == "alignat")
152 if (type_ == "xalignat")
153 return (col & 1) ? 20 : 0;
154 if (type_ == "xxalignat")
155 return (col & 1) ? 40 : 0;
160 char const * MathHullInset::standardFont() const
163 return "lyxnochange";
168 void MathHullInset::metrics(MathMetricsInfo & mi) const
170 MathFontSetChanger dummy1(mi.base, standardFont());
171 MathStyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
173 // let the cells adjust themselves
174 MathGridInset::metrics(mi);
181 if (numberedType()) {
182 MathFontSetChanger dummy(mi.base, "mathbf");
184 for (row_type row = 0; row < nrows(); ++row)
185 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
191 // make it at least as high as the current font
194 math_font_max_dim(mi.base.font, asc, des);
195 dim_.a = max(dim_.a, asc);
196 dim_.d = max(dim_.d, des);
203 void MathHullInset::draw(MathPainterInfo & pi, int x, int y) const
205 MathFontSetChanger dummy1(pi.base, standardFont());
206 MathStyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
207 MathGridInset::draw(pi, x + 1, y);
209 if (numberedType()) {
210 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
211 for (row_type row = 0; row < nrows(); ++row) {
212 int const yy = y + rowinfo_[row].offset_;
213 MathFontSetChanger dummy(pi.base, "mathrm");
214 drawStr(pi, pi.base.font, xx, yy, nicelabel(row));
218 drawMarkers2(pi, x, y);
222 void MathHullInset::metricsT(TextMetricsInfo const & mi) const
225 MathGridInset::metricsT(mi);
228 WriteStream wi(os, false, true);
230 dim_.w = os.str().size();
237 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
240 MathGridInset::drawT(pain, x, y);
243 WriteStream wi(os, false, true);
245 pain.draw(x, y, os.str().c_str());
250 string MathHullInset::label(row_type row) const
252 row_type n = nrows();
253 lyx::Assert(row < n);
258 void MathHullInset::label(row_type row, string const & label)
260 //lyxerr << "setting label '" << label << "' for row " << row << endl;
265 void MathHullInset::numbered(row_type row, bool num)
271 bool MathHullInset::numbered(row_type row) const
277 bool MathHullInset::ams() const
281 type_ == "multline" ||
283 type_ == "alignat" ||
284 type_ == "xalignat" ||
285 type_ == "xxalignat";
289 bool MathHullInset::display() const
291 return type_ != "simple" && type_ != "none";
295 void MathHullInset::getLabelList(std::vector<string> & labels) const
297 for (row_type row = 0; row < nrows(); ++row)
298 if (!label_[row].empty() && nonum_[row] != 1)
299 labels.push_back(label_[row]);
303 bool MathHullInset::numberedType() const
307 if (type_ == "simple")
309 if (type_ == "xxalignat")
311 for (row_type row = 0; row < nrows(); ++row)
318 void MathHullInset::validate(LaTeXFeatures & features) const
321 features.require("amsmath");
324 // Validation is necessary only if not using AMS math.
325 // To be safe, we will always run mathedvalidate.
326 //if (features.amsstyle)
329 features.require("boldsymbol");
330 //features.binom = true;
332 MathNestInset::validate(features);
336 void MathHullInset::header_write(WriteStream & os) const
338 bool n = numberedType();
343 else if (type_ == "simple") {
349 else if (type_ == "equation") {
351 os << "\\begin{equation" << star(n) << "}\n";
356 else if (type_ == "eqnarray" || type_ == "align")
357 os << "\\begin{" << type_ << star(n) << "}\n";
359 else if (type_ == "alignat" || type_ == "xalignat")
360 os << "\\begin{" << type_ << star(n) << "}"
361 << "{" << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
363 else if (type_ == "xxalignat")
364 os << "\\begin{" << type_ << "}"
365 << "{" << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
367 else if (type_ == "multline" || type_ == "gather")
368 os << "\\begin{" << type_ << "}\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_ == "alignat"
392 || type_ == "xalignat")
393 os << "\n\\end{" << type_ << star(n) << "}\n";
395 else if (type_ == "xxalignat" || type_ == "multline" || type_ == "gather")
396 os << "\n\\end{" << type_ << "}\n";
399 os << "\\end{unknown" << star(n) << "}";
403 void MathHullInset::addRow(row_type row)
405 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
406 label_.insert(label_.begin() + row + 1, string());
407 MathGridInset::addRow(row);
411 void MathHullInset::delRow(row_type row)
413 MathGridInset::delRow(row);
414 nonum_.erase(nonum_.begin() + row);
415 label_.erase(label_.begin() + row);
419 void MathHullInset::addFancyCol(col_type col)
421 if (type_ == "equation")
424 else if (type_ == "eqnarray") {
429 else if (type_ == "align" || type_ == "alignat"
430 || type_ == "xalignat" || type_ == "xxalignat")
431 MathGridInset::addCol(col);
435 void MathHullInset::delFancyCol(col_type col)
437 if (type_ == "alignat" || type_ == "xalignat" || type_ == "xxalignat")
438 MathGridInset::delCol(col);
442 string MathHullInset::nicelabel(row_type row) const
446 if (label_[row].empty())
447 return string("(#)");
448 return "(" + label_[row] + ")";
452 void MathHullInset::glueall()
455 for (idx_type i = 0; i < nargs(); ++i)
457 *this = MathHullInset("simple");
463 string const & MathHullInset::getType() const
469 void MathHullInset::setType(string const & type)
477 void MathHullInset::mutate(string const & newtype)
479 //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'\n";
481 // we try to move along the chain
482 // none <-> simple <-> equation <-> eqnarray
484 if (newtype == "dump") {
488 else if (newtype == type_) {
492 else if (type_ == "none") {
498 else if (type_ == "simple") {
499 if (newtype == "none") {
508 else if (type_ == "equation") {
509 if (smaller(newtype, type_)) {
512 } else if (newtype == "eqnarray") {
513 MathGridInset::addCol(1);
514 MathGridInset::addCol(1);
516 // split it "nicely" on the firest relop
517 pos_type pos = firstRelOp(cell(0));
518 cell(1) = MathArray(cell(0).begin() + pos, cell(0).end());
519 cell(0).erase(pos, cell(0).size());
521 if (cell(1).size()) {
522 cell(2) = MathArray(cell(1).begin() + 1, cell(1).end());
523 cell(1).erase(1, cell(1).size());
527 } else if (newtype == "multline" || newtype == "gather") {
531 MathGridInset::addCol(1);
533 pos_type pos = firstRelOp(cell(0));
535 cell(0).erase(pos, cell(0).size());
536 cell(1).erase(0, pos);
542 else if (type_ == "eqnarray") {
543 if (smaller(newtype, type_)) {
544 // set correct (no)numbering
545 bool allnonum = true;
546 for (row_type row = 0; row < nrows(); ++row)
550 // set first non-empty label
552 for (row_type row = 0; row < nrows(); ++row) {
553 if (!label_[row].empty()) {
560 nonum_[0] = allnonum;
563 } else { // align & Co.
564 for (row_type row = 0; row < nrows(); ++row) {
565 idx_type c = 3 * row + 1;
566 cell(c).append(cell(c + 1));
568 MathGridInset::delCol(2);
574 else if (type_ == "align") {
575 if (smaller(newtype, type_)) {
576 MathGridInset::addCol(1);
584 else if (type_ == "multline") {
585 if (newtype == "gather") {
588 lyxerr << "mutation from '" << type_
589 << "' to '" << newtype << "' not implemented"
594 else if (type_ == "gather") {
595 if (newtype == "multline") {
598 lyxerr << "mutation from '" << type_
599 << "' to '" << newtype << "' not implemented" << endl;
604 lyxerr << "mutation from '" << type_
605 << "' to '" << newtype << "' not implemented" << endl;
610 string MathHullInset::eolString(row_type row, bool fragile) const
613 if (numberedType()) {
614 if (!label_[row].empty())
615 res += "\\label{" + label_[row] + "}";
617 res += "\\nonumber ";
619 return res + MathGridInset::eolString(row, fragile);
623 void MathHullInset::write(WriteStream & os) const
626 MathGridInset::write(os);
631 void MathHullInset::normalize(NormalStream & os) const
633 os << "[formula " << type_ << " ";
634 MathGridInset::normalize(os);
639 void MathHullInset::mathmlize(MathMLStream & os) const
641 MathGridInset::mathmlize(os);
645 void MathHullInset::infoize(std::ostream & os) const
647 os << "Type: " << type_;
651 void MathHullInset::check() const
653 lyx::Assert(nonum_.size() == nrows());
654 lyx::Assert(label_.size() == nrows());
658 void MathHullInset::doExtern
659 (FuncRequest const & func, idx_type & idx, pos_type & pos)
663 istringstream iss(func.argument.c_str());
664 iss >> lang >> extra;
669 #warning temporarily disabled
673 // lyxerr << "use selection: " << ar << "\n";
674 // insert(pipeThroughExtern(lang, extra, ar));
680 eq.push_back(MathAtom(new MathCharInset('=')));
682 // go to first item in line
683 idx -= idx % ncols();
686 if (getType() == "simple") {
687 size_type pos = cell(idx).find_last(eq);
689 if (pos == cell(idx).size()) {
691 lyxerr << "use whole cell: " << ar << "\n";
693 ar = MathArray(cell(idx).begin() + pos + 1, cell(idx).end());
694 lyxerr << "use partial cell form pos: " << pos << "\n";
696 cell(idx).append(eq);
697 cell(idx).append(pipeThroughExtern(lang, extra, ar));
698 pos = cell(idx).size();
702 if (getType() == "equation") {
703 lyxerr << "use equation inset\n";
705 MathArray & ar = cell(idx);
706 lyxerr << "use cell: " << ar << "\n";
708 cell(idx + 2) = pipeThroughExtern(lang, extra, ar);
709 // move to end of line
711 pos = cell(idx).size();
716 lyxerr << "use eqnarray\n";
717 idx -= idx % ncols();
720 MathArray ar = cell(idx);
721 lyxerr << "use cell: " << ar << "\n";
723 #warning temporarily disabled
727 cell(idx + 3) = pipeThroughExtern(lang, extra, ar);
729 pos = cell(idx).size();
734 MathInset::result_type MathHullInset::dispatch
735 (FuncRequest const & cmd, idx_type & idx, pos_type & pos)
737 switch (cmd.action) {
740 if (type_ == "simple" || type_ == "equation") {
744 return DISPATCHED_POP;
746 return MathGridInset::dispatch(cmd, idx, pos);
748 case LFUN_MATH_NUMBER:
749 //lyxerr << "toggling all numbers\n";
751 //bv->lockedInsetStoreUndo(Undo::INSERT);
752 bool old = numberedType();
753 for (row_type row = 0; row < nrows(); ++row)
755 //bv->owner()->message(old ? _("No number") : _("Number"));
756 //updateLocal(bv, true);
760 case LFUN_MATH_NONUMBER:
762 //bv->lockedInsetStoreUndo(Undo::INSERT);
763 bool old = numbered(row(idx));
764 //bv->owner()->message(old ? _("No number") : _("Number"));
765 numbered(row(idx), !old);
766 //updateLocal(bv, true);
770 case LFUN_INSERT_LABEL: {
771 row_type r = row(idx);
772 string old_label = label(r);
773 string new_label = cmd.argument;
775 if (new_label.empty()) {
776 string const default_label =
777 (lyxrc.label_init_length >= 0) ? "eq:" : "";
778 pair<bool, string> const res = old_label.empty()
779 ? Alert::askForText(_("Enter new label to insert:"), default_label)
780 : Alert::askForText(_("Enter label:"), old_label);
783 new_label = trim(res.second);
786 //if (new_label == old_label)
787 // break; // Nothing to do
789 if (!new_label.empty())
792 #warning FIXME: please check you really mean repaint() ... is it needed,
793 #warning and if so, should it be update() instead ?
794 if (!new_label.empty()
795 && cmd.view()->ChangeRefsIfUnique(old_label, new_label))
796 cmd.view()->repaint();
802 case LFUN_MATH_HALIGN:
803 case LFUN_MATH_VALIGN:
804 // we explicitly don't want the default behaviour here
807 case LFUN_MATH_EXTERN:
808 doExtern(cmd, idx, pos);
809 return DISPATCHED_POP;
811 case LFUN_MATH_MUTATE: {
812 row_type r = row(idx);
813 col_type c = col(idx);
814 mutate(cmd.argument);
815 idx = r * ncols() + c;
818 if (pos > cell(idx).size())
819 pos = cell(idx).size();
820 return DISPATCHED_POP;
823 case LFUN_MATH_DISPLAY: {
824 mutate(type_ == "simple" ? "equation" : "simple");
826 pos = cell(idx).size();
827 return DISPATCHED_POP;
831 return MathGridInset::dispatch(cmd, idx, pos);