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;
151 MathHullInset::MathHullInset(string const & type)
152 : MathGridInset(getCols(type), 1), type_(type), nonum_(1), label_(1),
153 preview_(new RenderPreview(this))
160 MathHullInset::MathHullInset(MathHullInset const & other)
161 : MathGridInset(other),
162 type_(other.type_), nonum_(other.nonum_), label_(other.label_),
163 preview_(new RenderPreview(this))
167 MathHullInset::~MathHullInset()
171 auto_ptr<InsetBase> MathHullInset::doClone() const
173 return auto_ptr<InsetBase>(new MathHullInset(*this));
177 MathHullInset & MathHullInset::operator=(MathHullInset const & other)
181 *static_cast<MathGridInset*>(this) = MathGridInset(other);
183 nonum_ = other.nonum_;
184 label_ = other.label_;
185 preview_.reset(new RenderPreview(*other.preview_, this));
191 InsetBase * MathHullInset::editXY(LCursor & cur, int x, int y)
197 return MathNestInset::editXY(cur, x, y);
201 MathInset::mode_type MathHullInset::currentMode() const
204 return UNDECIDED_MODE;
205 // definitely math mode ...
210 bool MathHullInset::idxFirst(LCursor & cur) const
218 bool MathHullInset::idxLast(LCursor & cur) const
220 cur.idx() = nargs() - 1;
221 cur.pos() = cur.lastpos();
226 char MathHullInset::defaultColAlign(col_type col)
228 if (type_ == "eqnarray")
230 if (typecode(type_) >= typecode("align"))
231 return "rl"[col & 1];
236 int MathHullInset::defaultColSpace(col_type col)
238 if (type_ == "align" || type_ == "alignat")
240 if (type_ == "xalignat")
241 return (col & 1) ? 20 : 0;
242 if (type_ == "xxalignat" || type_ == "flalign")
243 return (col & 1) ? 40 : 0;
248 char const * MathHullInset::standardFont() const
250 return type_ == "none" ? "lyxnochange" : "mathnormal";
254 bool MathHullInset::previewState(BufferView * bv) const
256 if (!editing(bv) && RenderPreview::status() == LyXRC::PREVIEW_ON) {
257 lyx::graphics::PreviewImage const * pimage =
258 preview_->getPreviewImage(*bv->buffer());
259 return pimage && pimage->image();
265 void MathHullInset::metrics(MetricsInfo & mi, Dimension & dim) const
267 if (previewState(mi.base.bv)) {
268 preview_->metrics(mi, dim);
269 // insert a one pixel gap in front of the formula
277 FontSetChanger dummy1(mi.base, standardFont());
278 StyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
280 // let the cells adjust themselves
281 MathGridInset::metrics(mi, dim);
288 if (numberedType()) {
289 FontSetChanger dummy(mi.base, "mathbf");
291 for (row_type row = 0; row < nrows(); ++row)
292 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
298 // make it at least as high as the current font
301 math_font_max_dim(mi.base.font, asc, des);
302 dim.asc = max(dim.asc, asc);
303 dim.des = max(dim.des, des);
309 void MathHullInset::draw(PainterInfo & pi, int x, int y) const
311 use_preview_ = previewState(pi.base.bv);
314 // one pixel gap in front
315 preview_->draw(pi, x + 1, y);
316 setPosCache(pi, x, y);
320 FontSetChanger dummy1(pi.base, standardFont());
321 StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
322 MathGridInset::draw(pi, x + 1, y);
324 if (numberedType()) {
325 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
326 for (row_type row = 0; row < nrows(); ++row) {
327 int const yy = y + rowinfo_[row].offset_;
328 FontSetChanger dummy(pi.base, "mathrm");
329 pi.draw(xx, yy, nicelabel(row));
332 setPosCache(pi, x, y);
336 void MathHullInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
339 MathGridInset::metricsT(mi, dim);
342 WriteStream wi(os, false, true);
344 dim.wid = os.str().size();
351 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
354 MathGridInset::drawT(pain, x, y);
357 WriteStream wi(os, false, true);
359 pain.draw(x, y, os.str().c_str());
366 string const latex_string(MathHullInset const & inset)
369 WriteStream wi(ls, false, false);
377 void MathHullInset::addPreview(lyx::graphics::PreviewLoader & ploader) const
379 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
380 string const snippet = latex_string(*this);
381 preview_->addPreview(snippet, ploader);
386 void MathHullInset::notifyCursorLeaves(LCursor & cur)
388 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
389 Buffer const & buffer = cur.buffer();
390 string const snippet = latex_string(*this);
391 preview_->addPreview(snippet, buffer);
392 preview_->startLoading(buffer);
397 string MathHullInset::label(row_type row) const
399 BOOST_ASSERT(row < nrows());
404 void MathHullInset::label(row_type row, string const & label)
406 //lyxerr << "setting label '" << label << "' for row " << row << endl;
411 void MathHullInset::numbered(row_type row, bool num)
417 bool MathHullInset::numbered(row_type row) const
423 bool MathHullInset::ams() const
427 type_ == "flalign" ||
428 type_ == "multline" ||
430 type_ == "alignat" ||
431 type_ == "xalignat" ||
432 type_ == "xxalignat";
436 bool MathHullInset::display() const
438 return type_ != "simple" && type_ != "none";
442 void MathHullInset::getLabelList(Buffer const &, vector<string> & labels) const
444 for (row_type row = 0; row < nrows(); ++row)
445 if (!label_[row].empty() && nonum_[row] != 1)
446 labels.push_back(label_[row]);
450 bool MathHullInset::numberedType() const
454 if (type_ == "simple")
456 if (type_ == "xxalignat")
458 for (row_type row = 0; row < nrows(); ++row)
465 void MathHullInset::validate(LaTeXFeatures & features) const
468 features.require("amsmath");
471 // Validation is necessary only if not using AMS math.
472 // To be safe, we will always run mathedvalidate.
473 //if (features.amsstyle)
476 features.require("boldsymbol");
477 //features.binom = true;
479 MathGridInset::validate(features);
483 void MathHullInset::header_write(WriteStream & os) const
485 bool n = numberedType();
490 else if (type_ == "simple") {
496 else if (type_ == "equation") {
498 os << "\\begin{equation" << star(n) << "}\n";
503 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
504 || type_ == "gather" || type_ == "multline")
505 os << "\\begin{" << type_ << star(n) << "}\n";
507 else if (type_ == "alignat" || type_ == "xalignat")
508 os << "\\begin{" << type_ << star(n) << '}'
509 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
511 else if (type_ == "xxalignat")
512 os << "\\begin{" << type_ << '}'
513 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
516 os << "\\begin{unknown" << star(n) << '}';
520 void MathHullInset::footer_write(WriteStream & os) const
522 bool n = numberedType();
527 else if (type_ == "simple")
530 else if (type_ == "equation")
532 os << "\\end{equation" << star(n) << "}\n";
536 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
537 || type_ == "alignat" || type_ == "xalignat"
538 || type_ == "gather" || type_ == "multline")
539 os << "\\end{" << type_ << star(n) << "}\n";
541 else if (type_ == "xxalignat")
542 os << "\\end{" << type_ << "}\n";
545 os << "\\end{unknown" << star(n) << '}';
549 bool MathHullInset::rowChangeOK() const
552 type_ == "eqnarray" || type_ == "align" ||
553 type_ == "flalign" || type_ == "alignat" ||
554 type_ == "xalignat" || type_ == "xxalignat" ||
555 type_ == "gather" || type_ == "multline";
559 bool MathHullInset::colChangeOK() const
562 type_ == "align" || type_ == "flalign" ||type_ == "alignat" ||
563 type_ == "xalignat" || type_ == "xxalignat";
567 void MathHullInset::addRow(row_type row)
571 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
572 label_.insert(label_.begin() + row + 1, string());
573 MathGridInset::addRow(row);
577 void MathHullInset::swapRow(row_type row)
581 if (row + 1 == nrows())
583 swap(nonum_[row], nonum_[row + 1]);
584 swap(label_[row], label_[row + 1]);
585 MathGridInset::swapRow(row);
589 void MathHullInset::delRow(row_type row)
591 if (nrows() <= 1 || !rowChangeOK())
593 MathGridInset::delRow(row);
594 // The last dummy row has no number info nor a label.
595 // Test nrows() + 1 because we have already erased the row.
596 if (row == nrows() + 1)
598 nonum_.erase(nonum_.begin() + row);
599 label_.erase(label_.begin() + row);
603 void MathHullInset::addCol(col_type col)
607 MathGridInset::addCol(col);
611 void MathHullInset::delCol(col_type col)
613 if (ncols() <= 1 || !colChangeOK())
615 MathGridInset::delCol(col);
619 string MathHullInset::nicelabel(row_type row) const
623 if (label_[row].empty())
624 return string("(#)");
625 return '(' + label_[row] + ')';
629 void MathHullInset::glueall()
632 for (idx_type i = 0; i < nargs(); ++i)
634 *this = MathHullInset("simple");
640 void MathHullInset::splitTo2Cols()
642 BOOST_ASSERT(ncols() == 1);
643 MathGridInset::addCol(1);
644 for (row_type row = 0; row < nrows(); ++row) {
645 idx_type const i = 2 * row;
646 pos_type pos = firstRelOp(cell(i));
647 cell(i + 1) = MathArray(cell(i).begin() + pos, cell(i).end());
648 cell(i).erase(pos, cell(i).size());
653 void MathHullInset::splitTo3Cols()
655 BOOST_ASSERT(ncols() < 3);
658 MathGridInset::addCol(1);
659 for (row_type row = 0; row < nrows(); ++row) {
660 idx_type const i = 3 * row + 1;
661 if (cell(i).size()) {
662 cell(i + 1) = MathArray(cell(i).begin() + 1, cell(i).end());
663 cell(i).erase(1, cell(i).size());
669 void MathHullInset::changeCols(col_type cols)
673 else if (ncols() < cols) {
679 while (ncols() < cols)
680 MathGridInset::addCol(ncols() - 1);
686 for (row_type row = 0; row < nrows(); ++row) {
687 idx_type const i = row * ncols();
688 for (col_type col = cols; col < ncols(); ++col) {
689 cell(i + cols - 1).append(cell(i + col));
693 while (ncols() > cols) {
694 MathGridInset::delCol(ncols() - 1);
699 string const & MathHullInset::getType() const
705 void MathHullInset::setType(string const & type)
713 void MathHullInset::mutate(string const & newtype)
715 //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
717 // we try to move along the chain
718 // none <-> simple <-> equation <-> eqnarray -> *align* -> multline, gather -+
720 // +-------------------------------------+
721 // we use eqnarray as intermediate type for mutations that are not
722 // directly supported because it handles labels and numbering for
725 if (newtype == "dump") {
729 else if (newtype == type_) {
733 else if (typecode(newtype) < 0) {
737 else if (type_ == "none") {
743 else if (type_ == "simple") {
744 if (newtype == "none") {
754 else if (type_ == "equation") {
755 if (smaller(newtype, type_)) {
759 } else if (newtype == "eqnarray") {
760 // split it "nicely" on the first relop
763 } else if (newtype == "multline" || newtype == "gather") {
773 else if (type_ == "eqnarray") {
774 if (smaller(newtype, type_)) {
775 // set correct (no)numbering
776 bool allnonum = true;
777 for (row_type row = 0; row < nrows(); ++row)
781 // set first non-empty label
783 for (row_type row = 0; row < nrows(); ++row) {
784 if (!label_[row].empty()) {
791 nonum_[0] = allnonum;
794 } else { // align & Co.
801 else if (type_ == "align" || type_ == "alignat" ||
802 type_ == "xalignat" || type_ == "flalign") {
803 if (smaller(newtype, "align")) {
807 } else if (newtype == "gather" || newtype == "multline") {
810 } else if (newtype == "xxalignat") {
811 for (row_type row = 0; row < nrows(); ++row)
812 numbered(row, false);
819 else if (type_ == "xxalignat") {
820 for (row_type row = 0; row < nrows(); ++row)
821 numbered(row, false);
822 if (smaller(newtype, "align")) {
826 } else if (newtype == "gather" || newtype == "multline") {
834 else if (type_ == "multline" || type_ == "gather") {
835 if (newtype == "gather" || newtype == "multline")
837 else if (newtype == "align" || newtype == "flalign" ||
838 newtype == "alignat" || newtype == "xalignat") {
841 } else if (newtype == "xxalignat") {
843 for (row_type row = 0; row < nrows(); ++row)
844 numbered(row, false);
854 lyxerr << "mutation from '" << type_
855 << "' to '" << newtype << "' not implemented" << endl;
860 string MathHullInset::eolString(row_type row, bool emptyline, bool fragile) const
863 if (numberedType()) {
864 if (!label_[row].empty() && !nonum_[row])
865 res += "\\label{" + label_[row] + '}';
866 if (nonum_[row] && (type_ != "multline"))
867 res += "\\nonumber ";
869 return res + MathGridInset::eolString(row, emptyline, fragile);
873 void MathHullInset::write(WriteStream & os) const
876 MathGridInset::write(os);
881 void MathHullInset::normalize(NormalStream & os) const
883 os << "[formula " << type_ << ' ';
884 MathGridInset::normalize(os);
889 void MathHullInset::mathmlize(MathMLStream & os) const
891 MathGridInset::mathmlize(os);
895 void MathHullInset::infoize(ostream & os) const
897 os << "Type: " << type_;
901 void MathHullInset::check() const
903 BOOST_ASSERT(nonum_.size() == nrows());
904 BOOST_ASSERT(label_.size() == nrows());
908 void MathHullInset::doExtern(LCursor & cur, FuncRequest & func)
912 istringstream iss(func.argument);
913 iss >> lang >> extra;
918 #warning temporarily disabled
919 //if (cur.selection()) {
922 // lyxerr << "use selection: " << ar << endl;
923 // insert(pipeThroughExtern(lang, extra, ar));
929 eq.push_back(MathAtom(new MathCharInset('=')));
931 // go to first item in line
932 cur.idx() -= cur.idx() % ncols();
935 if (getType() == "simple") {
936 size_type pos = cur.cell().find_last(eq);
938 if (cur.inMathed() && cur.selection()) {
939 asArray(grabAndEraseSelection(cur), ar);
940 } else if (pos == cur.cell().size()) {
942 lyxerr << "use whole cell: " << ar << endl;
944 ar = MathArray(cur.cell().begin() + pos + 1, cur.cell().end());
945 lyxerr << "use partial cell form pos: " << pos << endl;
947 cur.cell().append(eq);
948 cur.cell().append(pipeThroughExtern(lang, extra, ar));
949 cur.pos() = cur.lastpos();
953 if (getType() == "equation") {
954 lyxerr << "use equation inset" << endl;
956 MathArray & ar = cur.cell();
957 lyxerr << "use cell: " << ar << endl;
961 cur.cell() = pipeThroughExtern(lang, extra, ar);
962 // move to end of line
963 cur.pos() = cur.lastpos();
968 lyxerr << "use eqnarray" << endl;
969 cur.idx() += 2 - cur.idx() % ncols();
971 MathArray ar = cur.cell();
972 lyxerr << "use cell: " << ar << endl;
974 #warning temporarily disabled
981 cur.cell() = pipeThroughExtern(lang, extra, ar);
982 cur.pos() = cur.lastpos();
987 void MathHullInset::doDispatch(LCursor & cur, FuncRequest & cmd)
989 //lyxerr << "action: " << cmd.action << endl;
990 switch (cmd.action) {
992 case LFUN_FINISHED_LEFT:
993 case LFUN_FINISHED_RIGHT:
994 case LFUN_FINISHED_UP:
995 case LFUN_FINISHED_DOWN:
996 //lyxerr << "action: " << cmd.action << endl;
997 MathGridInset::doDispatch(cur, cmd);
998 notifyCursorLeaves(cur);
1002 case LFUN_BREAK_PARAGRAPH:
1003 // just swallow this
1006 case LFUN_BREAK_LINE:
1007 // some magic for the common case
1008 if (type_ == "simple" || type_ == "equation") {
1009 recordUndoInset(cur);
1011 cur.bv().buffer()->params().use_amsmath == BufferParams::AMS_ON;
1012 mutate(align ? "align" : "eqnarray");
1014 cur.pos() = cur.lastpos();
1016 MathGridInset::doDispatch(cur, cmd);
1019 case LFUN_MATH_NUMBER:
1020 //lyxerr << "toggling all numbers" << endl;
1022 recordUndoInset(cur);
1023 bool old = numberedType();
1024 if (type_ == "multline")
1025 numbered(nrows() - 1, !old);
1027 for (row_type row = 0; row < nrows(); ++row)
1028 numbered(row, !old);
1029 cur.message(old ? _("No number") : _("Number"));
1033 case LFUN_MATH_NONUMBER:
1035 recordUndoInset(cur);
1036 row_type r = (type_ == "multline") ? nrows() - 1 : cur.row();
1037 bool old = numbered(r);
1038 cur.message(old ? _("No number") : _("Number"));
1043 case LFUN_LABEL_INSERT: {
1044 recordUndoInset(cur);
1045 row_type r = (type_ == "multline") ? nrows() - 1 : cur.row();
1046 string old_label = label(r);
1047 string const default_label =
1048 (lyxrc.label_init_length >= 0) ? "eq:" : "";
1049 if (old_label.empty())
1050 old_label = default_label;
1051 string const contents = cmd.argument.empty() ?
1052 old_label : cmd.argument;
1054 InsetCommandParams p("label", contents);
1055 string const data = InsetCommandMailer::params2string("label", p);
1057 if (cmd.argument.empty()) {
1058 cur.bv().owner()->getDialogs().show("label", data, 0);
1060 FuncRequest fr(LFUN_INSET_INSERT, data);
1066 case LFUN_INSET_INSERT: {
1067 //lyxerr << "arg: " << cmd.argument << endl;
1068 string const name = cmd.getArg(0);
1069 if (name == "label") {
1070 InsetCommandParams p;
1071 InsetCommandMailer::string2params(name, cmd.argument, p);
1072 string str = p.getContents();
1073 recordUndoInset(cur);
1074 row_type const r = (type_ == "multline") ? nrows() - 1 : cur.row();
1075 str = lyx::support::trim(str);
1078 string old = label(r);
1080 cur.bv().buffer()->changeRefsIfUnique(old, str);
1086 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
1094 case LFUN_MATH_EXTERN:
1095 recordUndoInset(cur);
1099 case LFUN_MATH_MUTATE: {
1100 recordUndoInset(cur);
1101 row_type row = cur.row();
1102 col_type col = cur.col();
1103 mutate(cmd.argument);
1104 cur.idx() = row * ncols() + col;
1105 if (cur.idx() > cur.lastidx()) {
1106 cur.idx() = cur.lastidx();
1107 cur.pos() = cur.lastpos();
1109 if (cur.pos() > cur.lastpos())
1110 cur.pos() = cur.lastpos();
1111 //cur.dispatched(FINISHED);
1115 case LFUN_MATH_DISPLAY: {
1116 recordUndoInset(cur);
1117 mutate(type_ == "simple" ? "equation" : "simple");
1119 cur.pos() = cur.lastpos();
1120 //cur.dispatched(FINISHED);
1125 MathGridInset::doDispatch(cur, cmd);
1131 bool MathHullInset::getStatus(LCursor & cur, FuncRequest const & cmd,
1132 FuncStatus & status) const
1134 switch (cmd.action) {
1135 case LFUN_FINISHED_LEFT:
1136 case LFUN_FINISHED_RIGHT:
1137 case LFUN_FINISHED_UP:
1138 case LFUN_FINISHED_DOWN:
1139 status.enabled(true);
1141 case LFUN_BREAK_LINE:
1142 case LFUN_MATH_NUMBER:
1143 case LFUN_MATH_NONUMBER:
1144 case LFUN_MATH_EXTERN:
1145 case LFUN_MATH_MUTATE:
1146 case LFUN_MATH_DISPLAY:
1148 status.enabled(true);
1150 case LFUN_LABEL_INSERT:
1151 status.enabled(type_ != "simple");
1153 case LFUN_INSET_INSERT: {
1154 // Don't test createMathInset_fromDialogStr(), since
1155 // getStatus is not called with a valid reference and the
1156 // dialog would not be applyable.
1157 string const name = cmd.getArg(0);
1158 status.enabled(name == "ref" ||
1159 (name == "label" && type_ != "simple"));
1162 case LFUN_TABULAR_FEATURE: {
1163 istringstream is(cmd.argument);
1167 && (s == "append-row"
1168 || s == "delete-row"
1169 || s == "copy-row")) {
1170 status.message(bformat(
1171 N_("Can't change number of rows in '%1$s'"),
1173 status.enabled(false);
1177 && (s == "append-column"
1178 || s == "delete-column"
1179 || s == "copy-column")) {
1180 status.message(bformat(
1181 N_("Can't change number of columns in '%1$s'"),
1183 status.enabled(false);
1186 if ((type_ == "simple"
1187 || type_ == "equation"
1188 || type_ == "none") &&
1189 (s == "add-hline-above" || s == "add-hline-below")) {
1190 status.message(bformat(
1191 N_("Can't add horizontal grid lines in '%1$s'"),
1193 status.enabled(false);
1196 if (s == "add-vline-left" || s == "add-vline-right") {
1197 status.message(bformat(
1198 N_("Can't add vertical grid lines in '%1$s'"),
1200 status.enabled(false);
1203 if (s == "valign-top" || s == "valign-middle"
1204 || s == "valign-bottom" || s == "align-left"
1205 || s == "align-center" || s == "align-right") {
1206 status.enabled(false);
1209 return MathGridInset::getStatus(cur, cmd, status);
1212 return MathGridInset::getStatus(cur, cmd, status);
1215 // This cannot really happen, but inserted to shut-up gcc
1216 return MathGridInset::getStatus(cur, cmd, status);
1220 /////////////////////////////////////////////////////////////////////
1222 #include "math_arrayinset.h"
1223 #include "math_deliminset.h"
1224 #include "math_factory.h"
1225 #include "math_parser.h"
1226 #include "math_spaceinset.h"
1227 #include "ref_inset.h"
1229 #include "bufferview_funcs.h"
1230 #include "lyxtext.h"
1232 #include "frontends/LyXView.h"
1233 #include "frontends/Dialogs.h"
1235 #include "support/lyxlib.h"
1238 // simply scrap this function if you want
1239 void MathHullInset::mutateToText()
1242 // translate to latex
1244 latex(NULL, os, false, false);
1245 string str = os.str();
1248 LyXText * lt = view_->getLyXText();
1249 string::const_iterator cit = str.begin();
1250 string::const_iterator end = str.end();
1251 for (; cit != end; ++cit)
1252 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
1255 //view_->owner()->dispatch(LFUN_ESCAPE);
1260 void MathHullInset::handleFont(LCursor & cur, string const & arg,
1261 string const & font)
1263 // this whole function is a hack and won't work for incremental font
1266 if (cur.inset().asMathInset()->name() == font)
1267 cur.handleFont(font);
1269 cur.handleNest(createMathInset(font));
1275 void MathHullInset::handleFont2(LCursor & cur, string const & arg)
1280 bv_funcs::string2font(arg, font, b);
1281 if (font.color() != LColor::inherit) {
1282 MathAtom at = MathAtom(new MathColorInset(true, font.color()));
1283 cur.handleNest(at, 0);
1288 void MathHullInset::edit(LCursor & cur, bool left)
1291 left ? idxFirst(cur) : idxLast(cur);
1295 string const MathHullInset::editMessage() const
1297 return _("Math editor mode");
1301 void MathHullInset::revealCodes(LCursor & cur) const
1303 if (!cur.inMathed())
1307 cur.message(os.str());
1309 // write something to the minibuffer
1310 // translate to latex
1314 string str = os.str();
1316 string::size_type pos = 0;
1318 for (string::iterator it = str.begin(); it != str.end(); ++it) {
1321 else if (*it == '\0') {
1323 pos = it - str.begin();
1329 res = res.substr(pos - 30);
1330 if (res.size() > 60)
1331 res = res.substr(0, 60);
1337 InsetBase::Code MathHullInset::lyxCode() const
1343 /////////////////////////////////////////////////////////////////////
1347 bool MathHullInset::searchForward(BufferView * bv, string const & str,
1350 #ifdef WITH_WARNINGS
1351 #warning completely broken
1353 static MathHullInset * lastformula = 0;
1354 static CursorBase current = DocIterator(ibegin(nucleus()));
1355 static MathArray ar;
1356 static string laststr;
1358 if (lastformula != this || laststr != str) {
1359 //lyxerr << "reset lastformula to " << this << endl;
1362 current = ibegin(nucleus());
1364 mathed_parse_cell(ar, str);
1368 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
1370 for (DocIterator it = current; it != iend(nucleus()); increment(it)) {
1371 CursorSlice & top = it.back();
1372 MathArray const & a = top.asMathInset()->cell(top.idx_);
1373 if (a.matchpart(ar, top.pos_)) {
1374 bv->cursor().setSelection(it, ar.size());
1376 top.pos_ += ar.size();
1382 //lyxerr << "not found!" << endl;
1389 void MathHullInset::write(Buffer const &, std::ostream & os) const
1391 WriteStream wi(os, false, false);
1397 void MathHullInset::read(Buffer const &, LyXLex & lex)
1400 mathed_parse_normal(at, lex);
1401 operator=(*at->asHullInset());
1405 int MathHullInset::plaintext(Buffer const &, ostream & os,
1406 OutputParams const &) const
1408 if (0 && display()) {
1412 TextPainter tpain(dim.width(), dim.height());
1413 drawT(tpain, 0, dim.ascent());
1415 // reset metrics cache to "real" values
1417 return tpain.textheight();
1419 WriteStream wi(os, false, true);
1426 int MathHullInset::linuxdoc(Buffer const & buf, ostream & os,
1427 OutputParams const & runparams) const
1429 return docbook(buf, os, runparams);
1433 int MathHullInset::docbook(Buffer const & buf, ostream & os,
1434 OutputParams const & runparams) const
1436 MathMLStream ms(os);
1439 if (getType() == "simple")
1440 name= "inlineequation";
1442 name = "informalequation";
1444 string bname = name;
1445 if (!label(0).empty())
1446 bname += " id=\"" + sgml::cleanID(buf, runparams, label(0)) + "\"";
1447 ms << MTag(bname.c_str());
1450 if (runparams.flavor == OutputParams::XML) {
1451 ms << MTag("alt role=\"tex\" ");
1452 // Workaround for db2latex: db2latex always includes equations with
1453 // \ensuremath{} or \begin{display}\end{display}
1454 // so we strip LyX' math environment
1455 WriteStream wi(ls, false, false);
1456 MathGridInset::write(wi);
1457 ms << subst(subst(ls.str(), "&", "&"), "<", "<");
1460 MathGridInset::mathmlize(ms);
1463 ms << MTag("alt role=\"tex\"");
1464 res = latex(buf, ls, runparams);
1465 ms << subst(subst(ls.str(), "&", "&"), "<", "<");
1469 ms << "<graphic fileref=\"eqn/";
1470 if ( !label(0).empty())
1471 ms << sgml::cleanID(buf, runparams, label(0));
1473 ms << sgml::uniqueID("anon");
1475 if (runparams.flavor == OutputParams::XML)
1480 ms << ETag(name.c_str());
1481 return ms.line() + res;
1485 int MathHullInset::textString(Buffer const & buf, ostream & os,
1486 OutputParams const & op) const
1488 return plaintext(buf, os, op);