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_charinset.h"
14 #include "math_colorinset.h"
15 #include "math_data.h"
16 #include "math_extern.h"
17 #include "math_factory.h"
18 #include "math_hullinset.h"
19 #include "math_mathmlstream.h"
20 #include "math_streamstr.h"
21 #include "math_support.h"
24 #include "bufferparams.h"
25 #include "BufferView.h"
26 #include "CutAndPaste.h"
27 #include "FuncStatus.h"
29 #include "LaTeXFeatures.h"
32 #include "dispatchresult.h"
33 #include "funcrequest.h"
37 #include "outputparams.h"
39 #include "textpainter.h"
42 #include "insets/render_preview.h"
43 #include "insets/insetlabel.h"
45 #include "frontends/Dialogs.h"
46 #include "frontends/LyXView.h"
48 #include "graphics/PreviewImage.h"
49 #include "graphics/PreviewLoader.h"
51 #include "support/lstrings.h"
53 #include <boost/bind.hpp>
57 using lyx::cap::grabAndEraseSelection;
58 using lyx::support::bformat;
59 using lyx::support::subst;
66 using std::istringstream;
68 using std::ostringstream;
76 int getCols(string const & type)
78 if (type == "eqnarray")
82 if (type == "flalign")
84 if (type == "alignat")
86 if (type == "xalignat")
88 if (type == "xxalignat")
94 // returns position of first relation operator in the array
95 // used for "intelligent splitting"
96 size_t firstRelOp(MathArray const & ar)
98 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
100 return it - ar.begin();
105 char const * star(bool numbered)
107 return numbered ? "" : "*";
111 int typecode(string const & s)
113 if (s == "none") return 0;
114 if (s == "simple") return 1;
115 if (s == "equation") return 2;
116 if (s == "eqnarray") return 3;
117 if (s == "align") return 4;
118 if (s == "alignat") return 5;
119 if (s == "xalignat") return 6;
120 if (s == "xxalignat") return 7;
121 if (s == "multline") return 8;
122 if (s == "gather") return 9;
123 if (s == "flalign") return 10;
124 lyxerr << "unknown hull type '" << s << "'" << endl;
128 bool smaller(string const & s, string const & t)
130 return typecode(s) < typecode(t);
134 } // end anon namespace
138 MathHullInset::MathHullInset()
139 : MathGridInset(1, 1), type_("none"), nonum_(1), label_(1),
140 preview_(new RenderPreview(this))
142 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << endl;
143 //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl;
144 //lyxerr << "sizeof MathCharInset: " << sizeof(MathCharInset) << endl;
145 //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << endl;
150 MathHullInset::MathHullInset(string const & type)
151 : MathGridInset(getCols(type), 1), type_(type), nonum_(1), label_(1),
152 preview_(new RenderPreview(this))
158 MathHullInset::MathHullInset(MathHullInset const & other)
159 : MathGridInset(other),
160 type_(other.type_), nonum_(other.nonum_), label_(other.label_),
161 preview_(new RenderPreview(this))
165 MathHullInset::~MathHullInset()
169 auto_ptr<InsetBase> MathHullInset::doClone() const
171 return auto_ptr<InsetBase>(new MathHullInset(*this));
175 MathHullInset & MathHullInset::operator=(MathHullInset const & other)
179 *static_cast<MathGridInset*>(this) = MathGridInset(other);
181 nonum_ = other.nonum_;
182 label_ = other.label_;
183 preview_.reset(new RenderPreview(*other.preview_, this));
189 InsetBase * MathHullInset::editXY(LCursor & cur, int x, int y)
195 return MathNestInset::editXY(cur, x, y);
199 MathInset::mode_type MathHullInset::currentMode() const
202 return UNDECIDED_MODE;
203 // definitely math mode ...
208 bool MathHullInset::idxFirst(LCursor & cur) const
216 bool MathHullInset::idxLast(LCursor & cur) const
218 cur.idx() = nargs() - 1;
219 cur.pos() = cur.lastpos();
224 char MathHullInset::defaultColAlign(col_type col)
226 if (type_ == "eqnarray")
228 if (typecode(type_) >= typecode("align"))
229 return "rl"[col & 1];
234 int MathHullInset::defaultColSpace(col_type col)
236 if (type_ == "align" || type_ == "alignat")
238 if (type_ == "xalignat")
239 return (col & 1) ? 20 : 0;
240 if (type_ == "xxalignat" || type_ == "flalign")
241 return (col & 1) ? 40 : 0;
246 char const * MathHullInset::standardFont() const
248 return type_ == "none" ? "lyxnochange" : "mathnormal";
252 bool MathHullInset::previewState(BufferView * bv) const
254 if (!editing(bv) && RenderPreview::status() == LyXRC::PREVIEW_ON) {
255 lyx::graphics::PreviewImage const * pimage =
256 preview_->getPreviewImage(*bv->buffer());
257 return pimage && pimage->image();
263 void MathHullInset::metrics(MetricsInfo & mi, Dimension & dim) const
265 if (previewState(mi.base.bv)) {
266 preview_->metrics(mi, dim);
267 // insert a one pixel gap in front of the formula
275 FontSetChanger dummy1(mi.base, standardFont());
276 StyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
278 // let the cells adjust themselves
279 MathGridInset::metrics(mi, dim);
286 if (numberedType()) {
287 FontSetChanger dummy(mi.base, "mathbf");
289 for (row_type row = 0; row < nrows(); ++row)
290 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
296 // make it at least as high as the current font
299 math_font_max_dim(mi.base.font, asc, des);
300 dim.asc = max(dim.asc, asc);
301 dim.des = max(dim.des, des);
307 void MathHullInset::draw(PainterInfo & pi, int x, int y) const
309 use_preview_ = previewState(pi.base.bv);
312 // one pixel gap in front
313 preview_->draw(pi, x + 1, y);
314 setPosCache(pi, x, y);
318 FontSetChanger dummy1(pi.base, standardFont());
319 StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
320 MathGridInset::draw(pi, x + 1, y);
322 if (numberedType()) {
323 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
324 for (row_type row = 0; row < nrows(); ++row) {
325 int const yy = y + rowinfo_[row].offset_;
326 FontSetChanger dummy(pi.base, "mathrm");
327 pi.draw(xx, yy, nicelabel(row));
330 setPosCache(pi, x, y);
334 void MathHullInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
337 MathGridInset::metricsT(mi, dim);
340 WriteStream wi(os, false, true);
342 dim.wid = os.str().size();
349 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
352 MathGridInset::drawT(pain, x, y);
355 WriteStream wi(os, false, true);
357 pain.draw(x, y, os.str().c_str());
364 string const latex_string(MathHullInset const & inset)
367 WriteStream wi(ls, false, false);
375 void MathHullInset::addPreview(lyx::graphics::PreviewLoader & ploader) const
377 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
378 string const snippet = latex_string(*this);
379 preview_->addPreview(snippet, ploader);
384 void MathHullInset::notifyCursorLeaves(LCursor & cur)
386 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
387 Buffer const & buffer = cur.buffer();
388 string const snippet = latex_string(*this);
389 preview_->addPreview(snippet, buffer);
390 preview_->startLoading(buffer);
395 string MathHullInset::label(row_type row) const
397 BOOST_ASSERT(row < nrows());
402 void MathHullInset::label(row_type row, string const & label)
404 //lyxerr << "setting label '" << label << "' for row " << row << endl;
409 void MathHullInset::numbered(row_type row, bool num)
415 bool MathHullInset::numbered(row_type row) const
421 bool MathHullInset::ams() const
425 type_ == "flalign" ||
426 type_ == "multline" ||
428 type_ == "alignat" ||
429 type_ == "xalignat" ||
430 type_ == "xxalignat";
434 bool MathHullInset::display() const
436 return type_ != "simple" && type_ != "none";
440 void MathHullInset::getLabelList(Buffer const &, vector<string> & labels) const
442 for (row_type row = 0; row < nrows(); ++row)
443 if (!label_[row].empty() && nonum_[row] != 1)
444 labels.push_back(label_[row]);
448 bool MathHullInset::numberedType() const
452 if (type_ == "simple")
454 if (type_ == "xxalignat")
456 for (row_type row = 0; row < nrows(); ++row)
463 void MathHullInset::validate(LaTeXFeatures & features) const
466 features.require("amsmath");
469 // Validation is necessary only if not using AMS math.
470 // To be safe, we will always run mathedvalidate.
471 //if (features.amsstyle)
474 features.require("boldsymbol");
475 //features.binom = true;
477 MathGridInset::validate(features);
481 void MathHullInset::header_write(WriteStream & os) const
483 bool n = numberedType();
488 else if (type_ == "simple") {
494 else if (type_ == "equation") {
496 os << "\\begin{equation" << star(n) << "}\n";
501 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
502 || type_ == "gather" || type_ == "multline")
503 os << "\\begin{" << type_ << star(n) << "}\n";
505 else if (type_ == "alignat" || type_ == "xalignat")
506 os << "\\begin{" << type_ << star(n) << '}'
507 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
509 else if (type_ == "xxalignat")
510 os << "\\begin{" << type_ << '}'
511 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
514 os << "\\begin{unknown" << star(n) << '}';
518 void MathHullInset::footer_write(WriteStream & os) const
520 bool n = numberedType();
525 else if (type_ == "simple")
528 else if (type_ == "equation")
530 os << "\\end{equation" << star(n) << "}\n";
534 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
535 || type_ == "alignat" || type_ == "xalignat"
536 || type_ == "gather" || type_ == "multline")
537 os << "\\end{" << type_ << star(n) << "}\n";
539 else if (type_ == "xxalignat")
540 os << "\\end{" << type_ << "}\n";
543 os << "\\end{unknown" << star(n) << '}';
547 bool MathHullInset::rowChangeOK() const
550 type_ == "eqnarray" || type_ == "align" ||
551 type_ == "flalign" || type_ == "alignat" ||
552 type_ == "xalignat" || type_ == "xxalignat" ||
553 type_ == "gather" || type_ == "multline";
557 bool MathHullInset::colChangeOK() const
560 type_ == "align" || type_ == "flalign" ||type_ == "alignat" ||
561 type_ == "xalignat" || type_ == "xxalignat";
565 void MathHullInset::addRow(row_type row)
569 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
570 label_.insert(label_.begin() + row + 1, string());
571 MathGridInset::addRow(row);
575 void MathHullInset::swapRow(row_type row)
579 if (row + 1 == nrows())
581 swap(nonum_[row], nonum_[row + 1]);
582 swap(label_[row], label_[row + 1]);
583 MathGridInset::swapRow(row);
587 void MathHullInset::delRow(row_type row)
589 if (nrows() <= 1 || !rowChangeOK())
591 MathGridInset::delRow(row);
592 // The last dummy row has no number info nor a label.
593 // Test nrows() + 1 because we have already erased the row.
594 if (row == nrows() + 1)
596 nonum_.erase(nonum_.begin() + row);
597 label_.erase(label_.begin() + row);
601 void MathHullInset::addCol(col_type col)
605 MathGridInset::addCol(col);
609 void MathHullInset::delCol(col_type col)
611 if (ncols() <= 1 || !colChangeOK())
613 MathGridInset::delCol(col);
617 string MathHullInset::nicelabel(row_type row) const
621 if (label_[row].empty())
622 return string("(#)");
623 return '(' + label_[row] + ')';
627 void MathHullInset::glueall()
630 for (idx_type i = 0; i < nargs(); ++i)
632 *this = MathHullInset("simple");
638 void MathHullInset::splitTo2Cols()
640 BOOST_ASSERT(ncols() == 1);
641 MathGridInset::addCol(1);
642 for (row_type row = 0; row < nrows(); ++row) {
643 idx_type const i = 2 * row;
644 pos_type pos = firstRelOp(cell(i));
645 cell(i + 1) = MathArray(cell(i).begin() + pos, cell(i).end());
646 cell(i).erase(pos, cell(i).size());
651 void MathHullInset::splitTo3Cols()
653 BOOST_ASSERT(ncols() < 3);
656 MathGridInset::addCol(1);
657 for (row_type row = 0; row < nrows(); ++row) {
658 idx_type const i = 3 * row + 1;
659 if (cell(i).size()) {
660 cell(i + 1) = MathArray(cell(i).begin() + 1, cell(i).end());
661 cell(i).erase(1, cell(i).size());
667 void MathHullInset::changeCols(col_type cols)
671 else if (ncols() < cols) {
677 while (ncols() < cols)
678 MathGridInset::addCol(ncols() - 1);
684 for (row_type row = 0; row < nrows(); ++row) {
685 idx_type const i = row * ncols();
686 for (col_type col = cols; col < ncols(); ++col) {
687 cell(i + cols - 1).append(cell(i + col));
691 while (ncols() > cols) {
692 MathGridInset::delCol(ncols() - 1);
697 string const & MathHullInset::getType() const
703 void MathHullInset::setType(string const & type)
711 void MathHullInset::mutate(string const & newtype)
713 //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
715 // we try to move along the chain
716 // none <-> simple <-> equation <-> eqnarray -> *align* -> multline, gather -+
718 // +-------------------------------------+
719 // we use eqnarray as intermediate type for mutations that are not
720 // directly supported because it handles labels and numbering for
723 if (newtype == "dump") {
727 else if (newtype == type_) {
731 else if (typecode(newtype) < 0) {
735 else if (type_ == "none") {
741 else if (type_ == "simple") {
742 if (newtype == "none") {
752 else if (type_ == "equation") {
753 if (smaller(newtype, type_)) {
757 } else if (newtype == "eqnarray") {
758 // split it "nicely" on the first relop
761 } else if (newtype == "multline" || newtype == "gather") {
771 else if (type_ == "eqnarray") {
772 if (smaller(newtype, type_)) {
773 // set correct (no)numbering
774 bool allnonum = true;
775 for (row_type row = 0; row < nrows(); ++row)
779 // set first non-empty label
781 for (row_type row = 0; row < nrows(); ++row) {
782 if (!label_[row].empty()) {
789 nonum_[0] = allnonum;
792 } else { // align & Co.
799 else if (type_ == "align" || type_ == "alignat" ||
800 type_ == "xalignat" || type_ == "flalign") {
801 if (smaller(newtype, "align")) {
805 } else if (newtype == "gather" || newtype == "multline") {
808 } else if (newtype == "xxalignat") {
809 for (row_type row = 0; row < nrows(); ++row)
810 numbered(row, false);
817 else if (type_ == "xxalignat") {
818 for (row_type row = 0; row < nrows(); ++row)
819 numbered(row, false);
820 if (smaller(newtype, "align")) {
824 } else if (newtype == "gather" || newtype == "multline") {
832 else if (type_ == "multline" || type_ == "gather") {
833 if (newtype == "gather" || newtype == "multline")
835 else if (newtype == "align" || newtype == "flalign" ||
836 newtype == "alignat" || newtype == "xalignat") {
839 } else if (newtype == "xxalignat") {
841 for (row_type row = 0; row < nrows(); ++row)
842 numbered(row, false);
852 lyxerr << "mutation from '" << type_
853 << "' to '" << newtype << "' not implemented" << endl;
858 string MathHullInset::eolString(row_type row, bool emptyline, bool fragile) const
861 if (numberedType()) {
862 if (!label_[row].empty() && !nonum_[row])
863 res += "\\label{" + label_[row] + '}';
864 if (nonum_[row] && (type_ != "multline"))
865 res += "\\nonumber ";
867 return res + MathGridInset::eolString(row, emptyline, fragile);
871 void MathHullInset::write(WriteStream & os) const
874 MathGridInset::write(os);
879 void MathHullInset::normalize(NormalStream & os) const
881 os << "[formula " << type_ << ' ';
882 MathGridInset::normalize(os);
887 void MathHullInset::mathmlize(MathMLStream & os) const
889 MathGridInset::mathmlize(os);
893 void MathHullInset::infoize(ostream & os) const
895 os << "Type: " << type_;
899 void MathHullInset::check() const
901 BOOST_ASSERT(nonum_.size() == nrows());
902 BOOST_ASSERT(label_.size() == nrows());
906 void MathHullInset::doExtern(LCursor & cur, FuncRequest & func)
910 istringstream iss(func.argument);
911 iss >> lang >> extra;
916 #warning temporarily disabled
917 //if (cur.selection()) {
920 // lyxerr << "use selection: " << ar << endl;
921 // insert(pipeThroughExtern(lang, extra, ar));
927 eq.push_back(MathAtom(new MathCharInset('=')));
929 // go to first item in line
930 cur.idx() -= cur.idx() % ncols();
933 if (getType() == "simple") {
934 size_type pos = cur.cell().find_last(eq);
936 if (cur.inMathed() && cur.selection()) {
937 asArray(grabAndEraseSelection(cur), ar);
938 } else if (pos == cur.cell().size()) {
940 lyxerr << "use whole cell: " << ar << endl;
942 ar = MathArray(cur.cell().begin() + pos + 1, cur.cell().end());
943 lyxerr << "use partial cell form pos: " << pos << endl;
945 cur.cell().append(eq);
946 cur.cell().append(pipeThroughExtern(lang, extra, ar));
947 cur.pos() = cur.lastpos();
951 if (getType() == "equation") {
952 lyxerr << "use equation inset" << endl;
954 MathArray & ar = cur.cell();
955 lyxerr << "use cell: " << ar << endl;
959 cur.cell() = pipeThroughExtern(lang, extra, ar);
960 // move to end of line
961 cur.pos() = cur.lastpos();
966 lyxerr << "use eqnarray" << endl;
967 cur.idx() += 2 - cur.idx() % ncols();
969 MathArray ar = cur.cell();
970 lyxerr << "use cell: " << ar << endl;
972 #warning temporarily disabled
979 cur.cell() = pipeThroughExtern(lang, extra, ar);
980 cur.pos() = cur.lastpos();
985 void MathHullInset::doDispatch(LCursor & cur, FuncRequest & cmd)
987 lyxerr << "action: " << cmd.action << endl;
988 switch (cmd.action) {
990 case LFUN_FINISHED_LEFT:
991 case LFUN_FINISHED_RIGHT:
992 case LFUN_FINISHED_UP:
993 case LFUN_FINISHED_DOWN:
994 lyxerr << "action: " << cmd.action << endl;
995 MathGridInset::doDispatch(cur, cmd);
996 notifyCursorLeaves(cur);
1000 case LFUN_BREAKPARAGRAPH:
1001 // just swallow this
1004 case LFUN_BREAKLINE:
1005 // some magic for the common case
1006 if (type_ == "simple" || type_ == "equation") {
1007 recordUndoInset(cur);
1009 cur.bv().buffer()->params().use_amsmath == BufferParams::AMS_ON;
1010 mutate(align ? "align" : "eqnarray");
1012 cur.pos() = cur.lastpos();
1014 MathGridInset::doDispatch(cur, cmd);
1017 case LFUN_MATH_NUMBER:
1018 //lyxerr << "toggling all numbers" << endl;
1020 recordUndoInset(cur);
1021 bool old = numberedType();
1022 if (type_ == "multline")
1023 numbered(nrows() - 1, !old);
1025 for (row_type row = 0; row < nrows(); ++row)
1026 numbered(row, !old);
1027 cur.message(old ? _("No number") : _("Number"));
1031 case LFUN_MATH_NONUMBER:
1033 recordUndoInset(cur);
1034 row_type r = (type_ == "multline") ? nrows() - 1 : cur.row();
1035 bool old = numbered(r);
1036 cur.message(old ? _("No number") : _("Number"));
1041 case LFUN_INSERT_LABEL: {
1042 recordUndoInset(cur);
1043 row_type r = (type_ == "multline") ? nrows() - 1 : cur.row();
1044 string old_label = label(r);
1045 string const default_label =
1046 (lyxrc.label_init_length >= 0) ? "eq:" : "";
1047 string const contents = cmd.argument.empty() ?
1048 label(r) : cmd.argument;
1050 InsetCommandParams p("label", contents);
1051 string const data = InsetCommandMailer::params2string("label", p);
1053 if (cmd.argument.empty()) {
1054 cur.bv().owner()->getDialogs().show("label", data, 0);
1056 FuncRequest fr(LFUN_INSET_INSERT, data);
1062 case LFUN_INSET_INSERT: {
1063 //lyxerr << "arg: " << cmd.argument << endl;
1064 string const name = cmd.getArg(0);
1065 if (name == "label") {
1066 InsetCommandParams p;
1067 InsetCommandMailer::string2params(name, cmd.argument, p);
1068 string str = p.getContents();
1069 recordUndoInset(cur);
1070 row_type const r = (type_ == "multline") ? nrows() - 1 : cur.row();
1071 str = lyx::support::trim(str);
1074 string old = label(r);
1076 cur.bv().buffer()->changeRefsIfUnique(old, str);
1082 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
1090 case LFUN_MATH_EXTERN:
1091 recordUndoInset(cur);
1095 case LFUN_MATH_MUTATE: {
1096 recordUndoInset(cur);
1097 row_type row = cur.row();
1098 col_type col = cur.col();
1099 mutate(cmd.argument);
1100 cur.idx() = row * ncols() + col;
1101 if (cur.idx() > cur.lastidx()) {
1102 cur.idx() = cur.lastidx();
1103 cur.pos() = cur.lastpos();
1105 if (cur.pos() > cur.lastpos())
1106 cur.pos() = cur.lastpos();
1107 //cur.dispatched(FINISHED);
1111 case LFUN_MATH_DISPLAY: {
1112 recordUndoInset(cur);
1113 mutate(type_ == "simple" ? "equation" : "simple");
1115 cur.pos() = cur.lastpos();
1116 //cur.dispatched(FINISHED);
1121 MathGridInset::doDispatch(cur, cmd);
1127 bool MathHullInset::getStatus(LCursor & cur, FuncRequest const & cmd,
1128 FuncStatus & status) const
1130 switch (cmd.action) {
1131 case LFUN_FINISHED_LEFT:
1132 case LFUN_FINISHED_RIGHT:
1133 case LFUN_FINISHED_UP:
1134 case LFUN_FINISHED_DOWN:
1135 status.enabled(true);
1137 case LFUN_BREAKLINE:
1138 case LFUN_MATH_NUMBER:
1139 case LFUN_MATH_NONUMBER:
1140 case LFUN_MATH_EXTERN:
1141 case LFUN_MATH_MUTATE:
1142 case LFUN_MATH_DISPLAY:
1144 status.enabled(true);
1146 case LFUN_INSERT_LABEL:
1147 status.enabled(type_ != "simple");
1149 case LFUN_INSET_INSERT: {
1150 // Don't test createMathInset_fromDialogStr(), since
1151 // getStatus is not called with a valid reference and the
1152 // dialog would not be applyable.
1153 string const name = cmd.getArg(0);
1154 status.enabled(name == "ref" ||
1155 (name == "label" && type_ != "simple"));
1158 case LFUN_TABULAR_FEATURE: {
1159 istringstream is(cmd.argument);
1163 && (s == "append-row"
1164 || s == "delete-row"
1165 || s == "copy-row")) {
1166 status.message(bformat(
1167 N_("Can't change number of rows in '%1$s'"),
1169 status.enabled(false);
1173 && (s == "append-column"
1174 || s == "delete-column"
1175 || s == "copy-column")) {
1176 status.message(bformat(
1177 N_("Can't change number of columns in '%1$s'"),
1179 status.enabled(false);
1182 if ((type_ == "simple"
1183 || type_ == "equation"
1184 || type_ == "none") &&
1185 (s == "add-hline-above" || s == "add-hline-below")) {
1186 status.message(bformat(
1187 N_("Can't add horizontal grid lines in '%1$s'"),
1189 status.enabled(false);
1192 if (s == "add-vline-left" || s == "add-vline-right") {
1193 status.message(bformat(
1194 N_("Can't add vertical grid lines in '%1$s'"),
1196 status.enabled(false);
1199 if (s == "valign-top" || s == "valign-middle"
1200 || s == "valign-bottom" || s == "align-left"
1201 || s == "align-center" || s == "align-right") {
1202 status.enabled(false);
1205 return MathGridInset::getStatus(cur, cmd, status);
1208 return MathGridInset::getStatus(cur, cmd, status);
1213 /////////////////////////////////////////////////////////////////////
1215 #include "math_arrayinset.h"
1216 #include "math_deliminset.h"
1217 #include "math_factory.h"
1218 #include "math_parser.h"
1219 #include "math_spaceinset.h"
1220 #include "ref_inset.h"
1222 #include "bufferview_funcs.h"
1223 #include "lyxtext.h"
1225 #include "frontends/LyXView.h"
1226 #include "frontends/Dialogs.h"
1228 #include "support/lyxlib.h"
1231 // simply scrap this function if you want
1232 void MathHullInset::mutateToText()
1235 // translate to latex
1237 latex(NULL, os, false, false);
1238 string str = os.str();
1241 LyXText * lt = view_->getLyXText();
1242 string::const_iterator cit = str.begin();
1243 string::const_iterator end = str.end();
1244 for (; cit != end; ++cit)
1245 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
1248 //view_->owner()->dispatch(LFUN_ESCAPE);
1253 void MathHullInset::handleFont(LCursor & cur, string const & arg,
1254 string const & font)
1256 // this whole function is a hack and won't work for incremental font
1259 if (cur.inset().asMathInset()->name() == font)
1260 cur.handleFont(font);
1262 cur.handleNest(createMathInset(font));
1268 void MathHullInset::handleFont2(LCursor & cur, string const & arg)
1273 bv_funcs::string2font(arg, font, b);
1274 if (font.color() != LColor::inherit) {
1275 MathAtom at = MathAtom(new MathColorInset(true, font.color()));
1276 cur.handleNest(at, 0);
1281 void MathHullInset::edit(LCursor & cur, bool left)
1284 left ? idxFirst(cur) : idxLast(cur);
1288 string const MathHullInset::editMessage() const
1290 return _("Math editor mode");
1294 void MathHullInset::revealCodes(LCursor & cur) const
1296 if (!cur.inMathed())
1300 cur.message(os.str());
1302 // write something to the minibuffer
1303 // translate to latex
1307 string str = os.str();
1309 string::size_type pos = 0;
1311 for (string::iterator it = str.begin(); it != str.end(); ++it) {
1314 else if (*it == '\0') {
1316 pos = it - str.begin();
1322 res = res.substr(pos - 30);
1323 if (res.size() > 60)
1324 res = res.substr(0, 60);
1330 InsetBase::Code MathHullInset::lyxCode() const
1336 /////////////////////////////////////////////////////////////////////
1340 bool MathHullInset::searchForward(BufferView * bv, string const & str,
1343 #ifdef WITH_WARNINGS
1344 #warning completely broken
1346 static MathHullInset * lastformula = 0;
1347 static CursorBase current = DocIterator(ibegin(nucleus()));
1348 static MathArray ar;
1349 static string laststr;
1351 if (lastformula != this || laststr != str) {
1352 //lyxerr << "reset lastformula to " << this << endl;
1355 current = ibegin(nucleus());
1357 mathed_parse_cell(ar, str);
1361 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
1363 for (DocIterator it = current; it != iend(nucleus()); increment(it)) {
1364 CursorSlice & top = it.back();
1365 MathArray const & a = top.asMathInset()->cell(top.idx_);
1366 if (a.matchpart(ar, top.pos_)) {
1367 bv->cursor().setSelection(it, ar.size());
1369 top.pos_ += ar.size();
1375 //lyxerr << "not found!" << endl;
1382 void MathHullInset::write(Buffer const &, std::ostream & os) const
1384 WriteStream wi(os, false, false);
1390 void MathHullInset::read(Buffer const &, LyXLex & lex)
1393 mathed_parse_normal(at, lex);
1394 operator=(*at->asHullInset());
1398 int MathHullInset::plaintext(Buffer const &, ostream & os,
1399 OutputParams const &) const
1401 if (0 && display()) {
1405 TextPainter tpain(dim.width(), dim.height());
1406 drawT(tpain, 0, dim.ascent());
1408 // reset metrics cache to "real" values
1410 return tpain.textheight();
1412 WriteStream wi(os, false, true);
1419 int MathHullInset::linuxdoc(Buffer const & buf, ostream & os,
1420 OutputParams const & runparams) const
1422 return docbook(buf, os, runparams);
1426 int MathHullInset::docbook(Buffer const & buf, ostream & os,
1427 OutputParams const & runparams) const
1429 MathMLStream ms(os);
1432 if (getType() == "simple")
1433 name= "inlineequation";
1435 name = "informalequation";
1437 string bname = name;
1438 if (!label(0).empty())
1439 bname += " id=\"" + sgml::cleanID(buf, runparams, label(0)) + "\"";
1440 ms << MTag(bname.c_str());
1443 if (runparams.flavor == OutputParams::XML) {
1444 ms << MTag("alt role=\"tex\" ");
1445 // Workaround for db2latex: db2latex always includes equations with
1446 // \ensuremath{} or \begin{display}\end{display}
1447 // so we strip LyX' math environment
1448 WriteStream wi(ls, false, false);
1449 MathGridInset::write(wi);
1450 ms << subst(subst(ls.str(), "&", "&"), "<", "<");
1453 MathGridInset::mathmlize(ms);
1456 ms << MTag("alt role=\"tex\"");
1457 res = latex(buf, ls, runparams);
1458 ms << subst(subst(ls.str(), "&", "&"), "<", "<");
1462 ms << "<graphic fileref=\"eqn/";
1463 if ( !label(0).empty())
1464 ms << sgml::cleanID(buf, runparams, label(0));
1466 ms << sgml::uniqueID("anon");
1468 if (runparams.flavor == OutputParams::XML)
1473 ms << ETag(name.c_str());
1474 return ms.line() + res;