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>
58 using lyx::cap::grabAndEraseSelection;
59 using lyx::support::bformat;
60 using lyx::support::subst;
67 using std::istringstream;
69 using std::ostringstream;
77 int getCols(string const & type)
79 if (type == "eqnarray")
83 if (type == "flalign")
85 if (type == "alignat")
87 if (type == "xalignat")
89 if (type == "xxalignat")
95 // returns position of first relation operator in the array
96 // used for "intelligent splitting"
97 size_t firstRelOp(MathArray const & ar)
99 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
100 if ((*it)->isRelOp())
101 return it - ar.begin();
106 char const * star(bool numbered)
108 return numbered ? "" : "*";
112 int typecode(string const & s)
114 if (s == "none") return 0;
115 if (s == "simple") return 1;
116 if (s == "equation") return 2;
117 if (s == "eqnarray") return 3;
118 if (s == "align") return 4;
119 if (s == "alignat") return 5;
120 if (s == "xalignat") return 6;
121 if (s == "xxalignat") return 7;
122 if (s == "multline") return 8;
123 if (s == "gather") return 9;
124 if (s == "flalign") return 10;
125 lyxerr << "unknown hull type '" << s << "'" << endl;
129 bool smaller(string const & s, string const & t)
131 return typecode(s) < typecode(t);
135 } // end anon namespace
139 MathHullInset::MathHullInset()
140 : MathGridInset(1, 1), type_("none"), nonum_(1), label_(1),
141 preview_(new RenderPreview(this))
143 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << endl;
144 //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl;
145 //lyxerr << "sizeof MathCharInset: " << sizeof(MathCharInset) << endl;
146 //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << endl;
152 MathHullInset::MathHullInset(string const & type)
153 : MathGridInset(getCols(type), 1), type_(type), nonum_(1), label_(1),
154 preview_(new RenderPreview(this))
161 MathHullInset::MathHullInset(MathHullInset const & other)
162 : MathGridInset(other),
163 type_(other.type_), nonum_(other.nonum_), label_(other.label_),
164 preview_(new RenderPreview(this))
168 MathHullInset::~MathHullInset()
172 auto_ptr<InsetBase> MathHullInset::doClone() const
174 return auto_ptr<InsetBase>(new MathHullInset(*this));
178 MathHullInset & MathHullInset::operator=(MathHullInset const & other)
182 *static_cast<MathGridInset*>(this) = MathGridInset(other);
184 nonum_ = other.nonum_;
185 label_ = other.label_;
186 preview_.reset(new RenderPreview(*other.preview_, this));
192 InsetBase * MathHullInset::editXY(LCursor & cur, int x, int y)
198 return MathNestInset::editXY(cur, x, y);
202 MathInset::mode_type MathHullInset::currentMode() const
205 return UNDECIDED_MODE;
206 // definitely math mode ...
211 bool MathHullInset::idxFirst(LCursor & cur) const
219 bool MathHullInset::idxLast(LCursor & cur) const
221 cur.idx() = nargs() - 1;
222 cur.pos() = cur.lastpos();
227 char MathHullInset::defaultColAlign(col_type col)
229 if (type_ == "eqnarray")
231 if (typecode(type_) >= typecode("align"))
232 return "rl"[col & 1];
237 int MathHullInset::defaultColSpace(col_type col)
239 if (type_ == "align" || type_ == "alignat")
241 if (type_ == "xalignat")
242 return (col & 1) ? 20 : 0;
243 if (type_ == "xxalignat" || type_ == "flalign")
244 return (col & 1) ? 40 : 0;
249 char const * MathHullInset::standardFont() const
251 return type_ == "none" ? "lyxnochange" : "mathnormal";
255 bool MathHullInset::previewState(BufferView * bv) const
257 if (!editing(bv) && RenderPreview::status() == LyXRC::PREVIEW_ON) {
258 lyx::graphics::PreviewImage const * pimage =
259 preview_->getPreviewImage(*bv->buffer());
260 return pimage && pimage->image();
266 void MathHullInset::metrics(MetricsInfo & mi, Dimension & dim) const
268 if (previewState(mi.base.bv)) {
269 preview_->metrics(mi, dim);
270 // insert a one pixel gap in front of the formula
278 FontSetChanger dummy1(mi.base, standardFont());
279 StyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
281 // let the cells adjust themselves
282 MathGridInset::metrics(mi, dim);
289 if (numberedType()) {
290 FontSetChanger dummy(mi.base, "mathbf");
292 for (row_type row = 0; row < nrows(); ++row)
293 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
299 // make it at least as high as the current font
302 math_font_max_dim(mi.base.font, asc, des);
303 dim.asc = max(dim.asc, asc);
304 dim.des = max(dim.des, des);
310 void MathHullInset::draw(PainterInfo & pi, int x, int y) const
312 use_preview_ = previewState(pi.base.bv);
315 // one pixel gap in front
316 preview_->draw(pi, x + 1, y);
317 setPosCache(pi, x, y);
321 FontSetChanger dummy1(pi.base, standardFont());
322 StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
323 MathGridInset::draw(pi, x + 1, y);
325 if (numberedType()) {
326 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
327 for (row_type row = 0; row < nrows(); ++row) {
328 int const yy = y + rowinfo_[row].offset_;
329 FontSetChanger dummy(pi.base, "mathrm");
330 string const nl = nicelabel(row);
331 docstring const dnl(nl.begin(), nl.end());
332 pi.draw(xx, yy, dnl);
335 setPosCache(pi, x, y);
339 void MathHullInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
342 MathGridInset::metricsT(mi, dim);
345 WriteStream wi(os, false, true);
347 dim.wid = os.str().size();
354 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
357 MathGridInset::drawT(pain, x, y);
360 WriteStream wi(os, false, true);
362 pain.draw(x, y, os.str().c_str());
369 string const latex_string(MathHullInset const & inset)
372 WriteStream wi(ls, false, false);
380 void MathHullInset::addPreview(lyx::graphics::PreviewLoader & ploader) const
382 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
383 string const snippet = latex_string(*this);
384 preview_->addPreview(snippet, ploader);
389 bool MathHullInset::notifyCursorLeaves(LCursor & cur)
391 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
392 Buffer const & buffer = cur.buffer();
393 string const snippet = latex_string(*this);
394 preview_->addPreview(snippet, buffer);
395 preview_->startLoading(buffer);
401 string MathHullInset::label(row_type row) const
403 BOOST_ASSERT(row < nrows());
408 void MathHullInset::label(row_type row, string const & label)
410 //lyxerr << "setting label '" << label << "' for row " << row << endl;
415 void MathHullInset::numbered(row_type row, bool num)
421 bool MathHullInset::numbered(row_type row) const
427 bool MathHullInset::ams() const
431 type_ == "flalign" ||
432 type_ == "multline" ||
434 type_ == "alignat" ||
435 type_ == "xalignat" ||
436 type_ == "xxalignat";
440 bool MathHullInset::display() const
442 return type_ != "simple" && type_ != "none";
446 void MathHullInset::getLabelList(Buffer const &, vector<string> & labels) const
448 for (row_type row = 0; row < nrows(); ++row)
449 if (!label_[row].empty() && nonum_[row] != 1)
450 labels.push_back(label_[row]);
454 bool MathHullInset::numberedType() const
458 if (type_ == "simple")
460 if (type_ == "xxalignat")
462 for (row_type row = 0; row < nrows(); ++row)
469 void MathHullInset::validate(LaTeXFeatures & features) const
472 features.require("amsmath");
475 // Validation is necessary only if not using AMS math.
476 // To be safe, we will always run mathedvalidate.
477 //if (features.amsstyle)
480 features.require("boldsymbol");
481 //features.binom = true;
483 MathGridInset::validate(features);
487 void MathHullInset::header_write(WriteStream & os) const
489 bool n = numberedType();
494 else if (type_ == "simple") {
500 else if (type_ == "equation") {
502 os << "\\begin{equation" << star(n) << "}\n";
507 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
508 || type_ == "gather" || type_ == "multline")
509 os << "\\begin{" << type_ << star(n) << "}\n";
511 else if (type_ == "alignat" || type_ == "xalignat")
512 os << "\\begin{" << type_ << star(n) << '}'
513 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
515 else if (type_ == "xxalignat")
516 os << "\\begin{" << type_ << '}'
517 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
520 os << "\\begin{unknown" << star(n) << '}';
524 void MathHullInset::footer_write(WriteStream & os) const
526 bool n = numberedType();
531 else if (type_ == "simple")
534 else if (type_ == "equation")
536 os << "\\end{equation" << star(n) << "}\n";
540 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
541 || type_ == "alignat" || type_ == "xalignat"
542 || type_ == "gather" || type_ == "multline")
543 os << "\\end{" << type_ << star(n) << "}\n";
545 else if (type_ == "xxalignat")
546 os << "\\end{" << type_ << "}\n";
549 os << "\\end{unknown" << star(n) << '}';
553 bool MathHullInset::rowChangeOK() const
556 type_ == "eqnarray" || type_ == "align" ||
557 type_ == "flalign" || type_ == "alignat" ||
558 type_ == "xalignat" || type_ == "xxalignat" ||
559 type_ == "gather" || type_ == "multline";
563 bool MathHullInset::colChangeOK() const
566 type_ == "align" || type_ == "flalign" ||type_ == "alignat" ||
567 type_ == "xalignat" || type_ == "xxalignat";
571 void MathHullInset::addRow(row_type row)
575 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
576 label_.insert(label_.begin() + row + 1, string());
577 MathGridInset::addRow(row);
581 void MathHullInset::swapRow(row_type row)
585 if (row + 1 == nrows())
587 swap(nonum_[row], nonum_[row + 1]);
588 swap(label_[row], label_[row + 1]);
589 MathGridInset::swapRow(row);
593 void MathHullInset::delRow(row_type row)
595 if (nrows() <= 1 || !rowChangeOK())
597 MathGridInset::delRow(row);
598 // The last dummy row has no number info nor a label.
599 // Test nrows() + 1 because we have already erased the row.
600 if (row == nrows() + 1)
602 nonum_.erase(nonum_.begin() + row);
603 label_.erase(label_.begin() + row);
607 void MathHullInset::addCol(col_type col)
611 MathGridInset::addCol(col);
615 void MathHullInset::delCol(col_type col)
617 if (ncols() <= 1 || !colChangeOK())
619 MathGridInset::delCol(col);
623 string MathHullInset::nicelabel(row_type row) const
627 if (label_[row].empty())
628 return string("(#)");
629 return '(' + label_[row] + ')';
633 void MathHullInset::glueall()
636 for (idx_type i = 0; i < nargs(); ++i)
638 *this = MathHullInset("simple");
644 void MathHullInset::splitTo2Cols()
646 BOOST_ASSERT(ncols() == 1);
647 MathGridInset::addCol(1);
648 for (row_type row = 0; row < nrows(); ++row) {
649 idx_type const i = 2 * row;
650 pos_type pos = firstRelOp(cell(i));
651 cell(i + 1) = MathArray(cell(i).begin() + pos, cell(i).end());
652 cell(i).erase(pos, cell(i).size());
657 void MathHullInset::splitTo3Cols()
659 BOOST_ASSERT(ncols() < 3);
662 MathGridInset::addCol(1);
663 for (row_type row = 0; row < nrows(); ++row) {
664 idx_type const i = 3 * row + 1;
665 if (cell(i).size()) {
666 cell(i + 1) = MathArray(cell(i).begin() + 1, cell(i).end());
667 cell(i).erase(1, cell(i).size());
673 void MathHullInset::changeCols(col_type cols)
677 else if (ncols() < cols) {
683 while (ncols() < cols)
684 MathGridInset::addCol(ncols() - 1);
690 for (row_type row = 0; row < nrows(); ++row) {
691 idx_type const i = row * ncols();
692 for (col_type col = cols; col < ncols(); ++col) {
693 cell(i + cols - 1).append(cell(i + col));
697 while (ncols() > cols) {
698 MathGridInset::delCol(ncols() - 1);
703 string const & MathHullInset::getType() const
709 void MathHullInset::setType(string const & type)
717 void MathHullInset::mutate(string const & newtype)
719 //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
721 // we try to move along the chain
722 // none <-> simple <-> equation <-> eqnarray -> *align* -> multline, gather -+
724 // +-------------------------------------+
725 // we use eqnarray as intermediate type for mutations that are not
726 // directly supported because it handles labels and numbering for
729 if (newtype == "dump") {
733 else if (newtype == type_) {
737 else if (typecode(newtype) < 0) {
741 else if (type_ == "none") {
747 else if (type_ == "simple") {
748 if (newtype == "none") {
758 else if (type_ == "equation") {
759 if (smaller(newtype, type_)) {
763 } else if (newtype == "eqnarray") {
764 // split it "nicely" on the first relop
767 } else if (newtype == "multline" || newtype == "gather") {
777 else if (type_ == "eqnarray") {
778 if (smaller(newtype, type_)) {
779 // set correct (no)numbering
780 bool allnonum = true;
781 for (row_type row = 0; row < nrows(); ++row)
785 // set first non-empty label
787 for (row_type row = 0; row < nrows(); ++row) {
788 if (!label_[row].empty()) {
795 nonum_[0] = allnonum;
798 } else { // align & Co.
805 else if (type_ == "align" || type_ == "alignat" ||
806 type_ == "xalignat" || type_ == "flalign") {
807 if (smaller(newtype, "align")) {
811 } else if (newtype == "gather" || newtype == "multline") {
814 } else if (newtype == "xxalignat") {
815 for (row_type row = 0; row < nrows(); ++row)
816 numbered(row, false);
823 else if (type_ == "xxalignat") {
824 for (row_type row = 0; row < nrows(); ++row)
825 numbered(row, false);
826 if (smaller(newtype, "align")) {
830 } else if (newtype == "gather" || newtype == "multline") {
838 else if (type_ == "multline" || type_ == "gather") {
839 if (newtype == "gather" || newtype == "multline")
841 else if (newtype == "align" || newtype == "flalign" ||
842 newtype == "alignat" || newtype == "xalignat") {
845 } else if (newtype == "xxalignat") {
847 for (row_type row = 0; row < nrows(); ++row)
848 numbered(row, false);
858 lyxerr << "mutation from '" << type_
859 << "' to '" << newtype << "' not implemented" << endl;
864 string MathHullInset::eolString(row_type row, bool emptyline, bool fragile) const
867 if (numberedType()) {
868 if (!label_[row].empty() && !nonum_[row])
869 res += "\\label{" + label_[row] + '}';
870 if (nonum_[row] && (type_ != "multline"))
871 res += "\\nonumber ";
873 return res + MathGridInset::eolString(row, emptyline, fragile);
877 void MathHullInset::write(WriteStream & os) const
880 MathGridInset::write(os);
885 void MathHullInset::normalize(NormalStream & os) const
887 os << "[formula " << type_ << ' ';
888 MathGridInset::normalize(os);
893 void MathHullInset::mathmlize(MathMLStream & os) const
895 MathGridInset::mathmlize(os);
899 void MathHullInset::infoize(ostream & os) const
901 os << "Type: " << type_;
905 void MathHullInset::check() const
907 BOOST_ASSERT(nonum_.size() == nrows());
908 BOOST_ASSERT(label_.size() == nrows());
912 void MathHullInset::doExtern(LCursor & cur, FuncRequest & func)
916 istringstream iss(func.argument);
917 iss >> lang >> extra;
922 #warning temporarily disabled
923 //if (cur.selection()) {
926 // lyxerr << "use selection: " << ar << endl;
927 // insert(pipeThroughExtern(lang, extra, ar));
933 eq.push_back(MathAtom(new MathCharInset('=')));
935 // go to first item in line
936 cur.idx() -= cur.idx() % ncols();
939 if (getType() == "simple") {
940 size_type pos = cur.cell().find_last(eq);
942 if (cur.inMathed() && cur.selection()) {
943 asArray(grabAndEraseSelection(cur), ar);
944 } else if (pos == cur.cell().size()) {
946 lyxerr << "use whole cell: " << ar << endl;
948 ar = MathArray(cur.cell().begin() + pos + 1, cur.cell().end());
949 lyxerr << "use partial cell form pos: " << pos << endl;
951 cur.cell().append(eq);
952 cur.cell().append(pipeThroughExtern(lang, extra, ar));
953 cur.pos() = cur.lastpos();
957 if (getType() == "equation") {
958 lyxerr << "use equation inset" << endl;
960 MathArray & ar = cur.cell();
961 lyxerr << "use cell: " << ar << endl;
965 cur.cell() = pipeThroughExtern(lang, extra, ar);
966 // move to end of line
967 cur.pos() = cur.lastpos();
972 lyxerr << "use eqnarray" << endl;
973 cur.idx() += 2 - cur.idx() % ncols();
975 MathArray ar = cur.cell();
976 lyxerr << "use cell: " << ar << endl;
978 #warning temporarily disabled
985 cur.cell() = pipeThroughExtern(lang, extra, ar);
986 cur.pos() = cur.lastpos();
991 void MathHullInset::doDispatch(LCursor & cur, FuncRequest & cmd)
993 //lyxerr << "action: " << cmd.action << endl;
994 switch (cmd.action) {
996 case LFUN_FINISHED_LEFT:
997 case LFUN_FINISHED_RIGHT:
998 case LFUN_FINISHED_UP:
999 case LFUN_FINISHED_DOWN:
1000 //lyxerr << "action: " << cmd.action << endl;
1001 MathGridInset::doDispatch(cur, cmd);
1002 notifyCursorLeaves(cur);
1006 case LFUN_BREAK_PARAGRAPH:
1007 // just swallow this
1010 case LFUN_BREAK_LINE:
1011 // some magic for the common case
1012 if (type_ == "simple" || type_ == "equation") {
1013 recordUndoInset(cur);
1015 cur.bv().buffer()->params().use_amsmath == BufferParams::AMS_ON;
1016 mutate(align ? "align" : "eqnarray");
1018 cur.pos() = cur.lastpos();
1020 MathGridInset::doDispatch(cur, cmd);
1023 case LFUN_MATH_NUMBER:
1024 //lyxerr << "toggling all numbers" << endl;
1026 recordUndoInset(cur);
1027 bool old = numberedType();
1028 if (type_ == "multline")
1029 numbered(nrows() - 1, !old);
1031 for (row_type row = 0; row < nrows(); ++row)
1032 numbered(row, !old);
1033 cur.message(old ? _("No number") : _("Number"));
1037 case LFUN_MATH_NONUMBER:
1039 recordUndoInset(cur);
1040 row_type r = (type_ == "multline") ? nrows() - 1 : cur.row();
1041 bool old = numbered(r);
1042 cur.message(old ? _("No number") : _("Number"));
1047 case LFUN_LABEL_INSERT: {
1048 recordUndoInset(cur);
1049 row_type r = (type_ == "multline") ? nrows() - 1 : cur.row();
1050 string old_label = label(r);
1051 string const default_label =
1052 (lyxrc.label_init_length >= 0) ? "eq:" : "";
1053 if (old_label.empty())
1054 old_label = default_label;
1055 string const contents = cmd.argument.empty() ?
1056 old_label : cmd.argument;
1058 InsetCommandParams p("label", contents);
1059 string const data = InsetCommandMailer::params2string("label", p);
1061 if (cmd.argument.empty()) {
1062 cur.bv().owner()->getDialogs().show("label", data, 0);
1064 FuncRequest fr(LFUN_INSET_INSERT, data);
1070 case LFUN_INSET_INSERT: {
1071 //lyxerr << "arg: " << cmd.argument << endl;
1072 string const name = cmd.getArg(0);
1073 if (name == "label") {
1074 InsetCommandParams p;
1075 InsetCommandMailer::string2params(name, cmd.argument, p);
1076 string str = p.getContents();
1077 recordUndoInset(cur);
1078 row_type const r = (type_ == "multline") ? nrows() - 1 : cur.row();
1079 str = lyx::support::trim(str);
1082 string old = label(r);
1084 cur.bv().buffer()->changeRefsIfUnique(old, str);
1090 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
1098 case LFUN_MATH_EXTERN:
1099 recordUndoInset(cur);
1103 case LFUN_MATH_MUTATE: {
1104 recordUndoInset(cur);
1105 row_type row = cur.row();
1106 col_type col = cur.col();
1107 mutate(cmd.argument);
1108 cur.idx() = row * ncols() + col;
1109 if (cur.idx() > cur.lastidx()) {
1110 cur.idx() = cur.lastidx();
1111 cur.pos() = cur.lastpos();
1113 if (cur.pos() > cur.lastpos())
1114 cur.pos() = cur.lastpos();
1115 //cur.dispatched(FINISHED);
1119 case LFUN_MATH_DISPLAY: {
1120 recordUndoInset(cur);
1121 mutate(type_ == "simple" ? "equation" : "simple");
1123 cur.pos() = cur.lastpos();
1124 //cur.dispatched(FINISHED);
1129 MathGridInset::doDispatch(cur, cmd);
1135 bool MathHullInset::getStatus(LCursor & cur, FuncRequest const & cmd,
1136 FuncStatus & status) const
1138 switch (cmd.action) {
1139 case LFUN_FINISHED_LEFT:
1140 case LFUN_FINISHED_RIGHT:
1141 case LFUN_FINISHED_UP:
1142 case LFUN_FINISHED_DOWN:
1143 status.enabled(true);
1145 case LFUN_BREAK_LINE:
1146 case LFUN_MATH_NUMBER:
1147 case LFUN_MATH_NONUMBER:
1148 case LFUN_MATH_EXTERN:
1149 case LFUN_MATH_MUTATE:
1150 case LFUN_MATH_DISPLAY:
1152 status.enabled(true);
1154 case LFUN_LABEL_INSERT:
1155 status.enabled(type_ != "simple");
1157 case LFUN_INSET_INSERT: {
1158 // Don't test createMathInset_fromDialogStr(), since
1159 // getStatus is not called with a valid reference and the
1160 // dialog would not be applyable.
1161 string const name = cmd.getArg(0);
1162 status.enabled(name == "ref" ||
1163 (name == "label" && type_ != "simple"));
1166 case LFUN_TABULAR_FEATURE: {
1167 istringstream is(cmd.argument);
1171 && (s == "append-row"
1172 || s == "delete-row"
1173 || s == "copy-row")) {
1174 status.message(bformat(
1175 N_("Can't change number of rows in '%1$s'"),
1177 status.enabled(false);
1181 && (s == "append-column"
1182 || s == "delete-column"
1183 || s == "copy-column")) {
1184 status.message(bformat(
1185 N_("Can't change number of columns in '%1$s'"),
1187 status.enabled(false);
1190 if ((type_ == "simple"
1191 || type_ == "equation"
1192 || type_ == "none") &&
1193 (s == "add-hline-above" || s == "add-hline-below")) {
1194 status.message(bformat(
1195 N_("Can't add horizontal grid lines in '%1$s'"),
1197 status.enabled(false);
1200 if (s == "add-vline-left" || s == "add-vline-right") {
1201 status.message(bformat(
1202 N_("Can't add vertical grid lines in '%1$s'"),
1204 status.enabled(false);
1207 if (s == "valign-top" || s == "valign-middle"
1208 || s == "valign-bottom" || s == "align-left"
1209 || s == "align-center" || s == "align-right") {
1210 status.enabled(false);
1213 return MathGridInset::getStatus(cur, cmd, status);
1216 return MathGridInset::getStatus(cur, cmd, status);
1219 // This cannot really happen, but inserted to shut-up gcc
1220 return MathGridInset::getStatus(cur, cmd, status);
1224 /////////////////////////////////////////////////////////////////////
1226 #include "math_arrayinset.h"
1227 #include "math_deliminset.h"
1228 #include "math_factory.h"
1229 #include "math_parser.h"
1230 #include "math_spaceinset.h"
1231 #include "ref_inset.h"
1233 #include "bufferview_funcs.h"
1234 #include "lyxtext.h"
1236 #include "frontends/LyXView.h"
1237 #include "frontends/Dialogs.h"
1239 #include "support/lyxlib.h"
1242 // simply scrap this function if you want
1243 void MathHullInset::mutateToText()
1246 // translate to latex
1248 latex(NULL, os, false, false);
1249 string str = os.str();
1252 LyXText * lt = view_->getLyXText();
1253 string::const_iterator cit = str.begin();
1254 string::const_iterator end = str.end();
1255 for (; cit != end; ++cit)
1256 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
1259 //view_->owner()->dispatch(LFUN_ESCAPE);
1264 void MathHullInset::handleFont(LCursor & cur, string const & arg,
1265 string const & font)
1267 // this whole function is a hack and won't work for incremental font
1270 if (cur.inset().asMathInset()->name() == font)
1271 cur.handleFont(font);
1273 cur.handleNest(createMathInset(font));
1279 void MathHullInset::handleFont2(LCursor & cur, string const & arg)
1284 bv_funcs::string2font(arg, font, b);
1285 if (font.color() != LColor::inherit) {
1286 MathAtom at = MathAtom(new MathColorInset(true, font.color()));
1287 cur.handleNest(at, 0);
1292 void MathHullInset::edit(LCursor & cur, bool left)
1295 left ? idxFirst(cur) : idxLast(cur);
1299 string const MathHullInset::editMessage() const
1301 return _("Math editor mode");
1305 void MathHullInset::revealCodes(LCursor & cur) const
1307 if (!cur.inMathed())
1311 cur.message(os.str());
1313 // write something to the minibuffer
1314 // translate to latex
1318 string str = os.str();
1320 string::size_type pos = 0;
1322 for (string::iterator it = str.begin(); it != str.end(); ++it) {
1325 else if (*it == '\0') {
1327 pos = it - str.begin();
1333 res = res.substr(pos - 30);
1334 if (res.size() > 60)
1335 res = res.substr(0, 60);
1341 InsetBase::Code MathHullInset::lyxCode() const
1347 /////////////////////////////////////////////////////////////////////
1351 bool MathHullInset::searchForward(BufferView * bv, string const & str,
1354 #ifdef WITH_WARNINGS
1355 #warning completely broken
1357 static MathHullInset * lastformula = 0;
1358 static CursorBase current = DocIterator(ibegin(nucleus()));
1359 static MathArray ar;
1360 static string laststr;
1362 if (lastformula != this || laststr != str) {
1363 //lyxerr << "reset lastformula to " << this << endl;
1366 current = ibegin(nucleus());
1368 mathed_parse_cell(ar, str);
1372 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
1374 for (DocIterator it = current; it != iend(nucleus()); increment(it)) {
1375 CursorSlice & top = it.back();
1376 MathArray const & a = top.asMathInset()->cell(top.idx_);
1377 if (a.matchpart(ar, top.pos_)) {
1378 bv->cursor().setSelection(it, ar.size());
1380 top.pos_ += ar.size();
1386 //lyxerr << "not found!" << endl;
1393 void MathHullInset::write(Buffer const &, std::ostream & os) const
1395 WriteStream wi(os, false, false);
1401 void MathHullInset::read(Buffer const &, LyXLex & lex)
1404 mathed_parse_normal(at, lex);
1405 operator=(*at->asHullInset());
1409 int MathHullInset::plaintext(Buffer const &, ostream & os,
1410 OutputParams const &) const
1412 if (0 && display()) {
1416 TextPainter tpain(dim.width(), dim.height());
1417 drawT(tpain, 0, dim.ascent());
1419 // reset metrics cache to "real" values
1421 return tpain.textheight();
1423 WriteStream wi(os, false, true);
1430 int MathHullInset::docbook(Buffer const & buf, ostream & os,
1431 OutputParams const & runparams) const
1433 MathMLStream ms(os);
1436 if (getType() == "simple")
1437 name= "inlineequation";
1439 name = "informalequation";
1441 string bname = name;
1442 if (!label(0).empty())
1443 bname += " id=\"" + sgml::cleanID(buf, runparams, label(0)) + "\"";
1444 ms << MTag(bname.c_str());
1447 if (runparams.flavor == OutputParams::XML) {
1448 ms << MTag("alt role=\"tex\" ");
1449 // Workaround for db2latex: db2latex always includes equations with
1450 // \ensuremath{} or \begin{display}\end{display}
1451 // so we strip LyX' math environment
1452 WriteStream wi(ls, false, false);
1453 MathGridInset::write(wi);
1454 ms << subst(subst(ls.str(), "&", "&"), "<", "<");
1457 MathGridInset::mathmlize(ms);
1460 ms << MTag("alt role=\"tex\"");
1461 res = latex(buf, ls, runparams);
1462 ms << subst(subst(ls.str(), "&", "&"), "<", "<");
1466 ms << "<graphic fileref=\"eqn/";
1467 if ( !label(0).empty())
1468 ms << sgml::cleanID(buf, runparams, label(0));
1470 ms << sgml::uniqueID("anon");
1472 if (runparams.flavor == OutputParams::XML)
1477 ms << ETag(name.c_str());
1478 return ms.line() + res;
1482 int MathHullInset::textString(Buffer const & buf, ostream & os,
1483 OutputParams const & op) const
1485 return plaintext(buf, os, op);