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 void 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);
400 string MathHullInset::label(row_type row) const
402 BOOST_ASSERT(row < nrows());
407 void MathHullInset::label(row_type row, string const & label)
409 //lyxerr << "setting label '" << label << "' for row " << row << endl;
414 void MathHullInset::numbered(row_type row, bool num)
420 bool MathHullInset::numbered(row_type row) const
426 bool MathHullInset::ams() const
430 type_ == "flalign" ||
431 type_ == "multline" ||
433 type_ == "alignat" ||
434 type_ == "xalignat" ||
435 type_ == "xxalignat";
439 bool MathHullInset::display() const
441 return type_ != "simple" && type_ != "none";
445 void MathHullInset::getLabelList(Buffer const &, vector<string> & labels) const
447 for (row_type row = 0; row < nrows(); ++row)
448 if (!label_[row].empty() && nonum_[row] != 1)
449 labels.push_back(label_[row]);
453 bool MathHullInset::numberedType() const
457 if (type_ == "simple")
459 if (type_ == "xxalignat")
461 for (row_type row = 0; row < nrows(); ++row)
468 void MathHullInset::validate(LaTeXFeatures & features) const
471 features.require("amsmath");
474 // Validation is necessary only if not using AMS math.
475 // To be safe, we will always run mathedvalidate.
476 //if (features.amsstyle)
479 features.require("boldsymbol");
480 //features.binom = true;
482 MathGridInset::validate(features);
486 void MathHullInset::header_write(WriteStream & os) const
488 bool n = numberedType();
493 else if (type_ == "simple") {
499 else if (type_ == "equation") {
501 os << "\\begin{equation" << star(n) << "}\n";
506 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
507 || type_ == "gather" || type_ == "multline")
508 os << "\\begin{" << type_ << star(n) << "}\n";
510 else if (type_ == "alignat" || type_ == "xalignat")
511 os << "\\begin{" << type_ << star(n) << '}'
512 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
514 else if (type_ == "xxalignat")
515 os << "\\begin{" << type_ << '}'
516 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
519 os << "\\begin{unknown" << star(n) << '}';
523 void MathHullInset::footer_write(WriteStream & os) const
525 bool n = numberedType();
530 else if (type_ == "simple")
533 else if (type_ == "equation")
535 os << "\\end{equation" << star(n) << "}\n";
539 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
540 || type_ == "alignat" || type_ == "xalignat"
541 || type_ == "gather" || type_ == "multline")
542 os << "\\end{" << type_ << star(n) << "}\n";
544 else if (type_ == "xxalignat")
545 os << "\\end{" << type_ << "}\n";
548 os << "\\end{unknown" << star(n) << '}';
552 bool MathHullInset::rowChangeOK() const
555 type_ == "eqnarray" || type_ == "align" ||
556 type_ == "flalign" || type_ == "alignat" ||
557 type_ == "xalignat" || type_ == "xxalignat" ||
558 type_ == "gather" || type_ == "multline";
562 bool MathHullInset::colChangeOK() const
565 type_ == "align" || type_ == "flalign" ||type_ == "alignat" ||
566 type_ == "xalignat" || type_ == "xxalignat";
570 void MathHullInset::addRow(row_type row)
574 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
575 label_.insert(label_.begin() + row + 1, string());
576 MathGridInset::addRow(row);
580 void MathHullInset::swapRow(row_type row)
584 if (row + 1 == nrows())
586 swap(nonum_[row], nonum_[row + 1]);
587 swap(label_[row], label_[row + 1]);
588 MathGridInset::swapRow(row);
592 void MathHullInset::delRow(row_type row)
594 if (nrows() <= 1 || !rowChangeOK())
596 MathGridInset::delRow(row);
597 // The last dummy row has no number info nor a label.
598 // Test nrows() + 1 because we have already erased the row.
599 if (row == nrows() + 1)
601 nonum_.erase(nonum_.begin() + row);
602 label_.erase(label_.begin() + row);
606 void MathHullInset::addCol(col_type col)
610 MathGridInset::addCol(col);
614 void MathHullInset::delCol(col_type col)
616 if (ncols() <= 1 || !colChangeOK())
618 MathGridInset::delCol(col);
622 string MathHullInset::nicelabel(row_type row) const
626 if (label_[row].empty())
627 return string("(#)");
628 return '(' + label_[row] + ')';
632 void MathHullInset::glueall()
635 for (idx_type i = 0; i < nargs(); ++i)
637 *this = MathHullInset("simple");
643 void MathHullInset::splitTo2Cols()
645 BOOST_ASSERT(ncols() == 1);
646 MathGridInset::addCol(1);
647 for (row_type row = 0; row < nrows(); ++row) {
648 idx_type const i = 2 * row;
649 pos_type pos = firstRelOp(cell(i));
650 cell(i + 1) = MathArray(cell(i).begin() + pos, cell(i).end());
651 cell(i).erase(pos, cell(i).size());
656 void MathHullInset::splitTo3Cols()
658 BOOST_ASSERT(ncols() < 3);
661 MathGridInset::addCol(1);
662 for (row_type row = 0; row < nrows(); ++row) {
663 idx_type const i = 3 * row + 1;
664 if (cell(i).size()) {
665 cell(i + 1) = MathArray(cell(i).begin() + 1, cell(i).end());
666 cell(i).erase(1, cell(i).size());
672 void MathHullInset::changeCols(col_type cols)
676 else if (ncols() < cols) {
682 while (ncols() < cols)
683 MathGridInset::addCol(ncols() - 1);
689 for (row_type row = 0; row < nrows(); ++row) {
690 idx_type const i = row * ncols();
691 for (col_type col = cols; col < ncols(); ++col) {
692 cell(i + cols - 1).append(cell(i + col));
696 while (ncols() > cols) {
697 MathGridInset::delCol(ncols() - 1);
702 string const & MathHullInset::getType() const
708 void MathHullInset::setType(string const & type)
716 void MathHullInset::mutate(string const & newtype)
718 //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
720 // we try to move along the chain
721 // none <-> simple <-> equation <-> eqnarray -> *align* -> multline, gather -+
723 // +-------------------------------------+
724 // we use eqnarray as intermediate type for mutations that are not
725 // directly supported because it handles labels and numbering for
728 if (newtype == "dump") {
732 else if (newtype == type_) {
736 else if (typecode(newtype) < 0) {
740 else if (type_ == "none") {
746 else if (type_ == "simple") {
747 if (newtype == "none") {
757 else if (type_ == "equation") {
758 if (smaller(newtype, type_)) {
762 } else if (newtype == "eqnarray") {
763 // split it "nicely" on the first relop
766 } else if (newtype == "multline" || newtype == "gather") {
776 else if (type_ == "eqnarray") {
777 if (smaller(newtype, type_)) {
778 // set correct (no)numbering
779 bool allnonum = true;
780 for (row_type row = 0; row < nrows(); ++row)
784 // set first non-empty label
786 for (row_type row = 0; row < nrows(); ++row) {
787 if (!label_[row].empty()) {
794 nonum_[0] = allnonum;
797 } else { // align & Co.
804 else if (type_ == "align" || type_ == "alignat" ||
805 type_ == "xalignat" || type_ == "flalign") {
806 if (smaller(newtype, "align")) {
810 } else if (newtype == "gather" || newtype == "multline") {
813 } else if (newtype == "xxalignat") {
814 for (row_type row = 0; row < nrows(); ++row)
815 numbered(row, false);
822 else if (type_ == "xxalignat") {
823 for (row_type row = 0; row < nrows(); ++row)
824 numbered(row, false);
825 if (smaller(newtype, "align")) {
829 } else if (newtype == "gather" || newtype == "multline") {
837 else if (type_ == "multline" || type_ == "gather") {
838 if (newtype == "gather" || newtype == "multline")
840 else if (newtype == "align" || newtype == "flalign" ||
841 newtype == "alignat" || newtype == "xalignat") {
844 } else if (newtype == "xxalignat") {
846 for (row_type row = 0; row < nrows(); ++row)
847 numbered(row, false);
857 lyxerr << "mutation from '" << type_
858 << "' to '" << newtype << "' not implemented" << endl;
863 string MathHullInset::eolString(row_type row, bool emptyline, bool fragile) const
866 if (numberedType()) {
867 if (!label_[row].empty() && !nonum_[row])
868 res += "\\label{" + label_[row] + '}';
869 if (nonum_[row] && (type_ != "multline"))
870 res += "\\nonumber ";
872 return res + MathGridInset::eolString(row, emptyline, fragile);
876 void MathHullInset::write(WriteStream & os) const
879 MathGridInset::write(os);
884 void MathHullInset::normalize(NormalStream & os) const
886 os << "[formula " << type_ << ' ';
887 MathGridInset::normalize(os);
892 void MathHullInset::mathmlize(MathMLStream & os) const
894 MathGridInset::mathmlize(os);
898 void MathHullInset::infoize(ostream & os) const
900 os << "Type: " << type_;
904 void MathHullInset::check() const
906 BOOST_ASSERT(nonum_.size() == nrows());
907 BOOST_ASSERT(label_.size() == nrows());
911 void MathHullInset::doExtern(LCursor & cur, FuncRequest & func)
915 istringstream iss(func.argument);
916 iss >> lang >> extra;
921 #warning temporarily disabled
922 //if (cur.selection()) {
925 // lyxerr << "use selection: " << ar << endl;
926 // insert(pipeThroughExtern(lang, extra, ar));
932 eq.push_back(MathAtom(new MathCharInset('=')));
934 // go to first item in line
935 cur.idx() -= cur.idx() % ncols();
938 if (getType() == "simple") {
939 size_type pos = cur.cell().find_last(eq);
941 if (cur.inMathed() && cur.selection()) {
942 asArray(grabAndEraseSelection(cur), ar);
943 } else if (pos == cur.cell().size()) {
945 lyxerr << "use whole cell: " << ar << endl;
947 ar = MathArray(cur.cell().begin() + pos + 1, cur.cell().end());
948 lyxerr << "use partial cell form pos: " << pos << endl;
950 cur.cell().append(eq);
951 cur.cell().append(pipeThroughExtern(lang, extra, ar));
952 cur.pos() = cur.lastpos();
956 if (getType() == "equation") {
957 lyxerr << "use equation inset" << endl;
959 MathArray & ar = cur.cell();
960 lyxerr << "use cell: " << ar << endl;
964 cur.cell() = pipeThroughExtern(lang, extra, ar);
965 // move to end of line
966 cur.pos() = cur.lastpos();
971 lyxerr << "use eqnarray" << endl;
972 cur.idx() += 2 - cur.idx() % ncols();
974 MathArray ar = cur.cell();
975 lyxerr << "use cell: " << ar << endl;
977 #warning temporarily disabled
984 cur.cell() = pipeThroughExtern(lang, extra, ar);
985 cur.pos() = cur.lastpos();
990 void MathHullInset::doDispatch(LCursor & cur, FuncRequest & cmd)
992 //lyxerr << "action: " << cmd.action << endl;
993 switch (cmd.action) {
995 case LFUN_FINISHED_LEFT:
996 case LFUN_FINISHED_RIGHT:
997 case LFUN_FINISHED_UP:
998 case LFUN_FINISHED_DOWN:
999 //lyxerr << "action: " << cmd.action << endl;
1000 MathGridInset::doDispatch(cur, cmd);
1001 notifyCursorLeaves(cur);
1005 case LFUN_BREAK_PARAGRAPH:
1006 // just swallow this
1009 case LFUN_BREAK_LINE:
1010 // some magic for the common case
1011 if (type_ == "simple" || type_ == "equation") {
1012 recordUndoInset(cur);
1014 cur.bv().buffer()->params().use_amsmath == BufferParams::AMS_ON;
1015 mutate(align ? "align" : "eqnarray");
1017 cur.pos() = cur.lastpos();
1019 MathGridInset::doDispatch(cur, cmd);
1022 case LFUN_MATH_NUMBER:
1023 //lyxerr << "toggling all numbers" << endl;
1025 recordUndoInset(cur);
1026 bool old = numberedType();
1027 if (type_ == "multline")
1028 numbered(nrows() - 1, !old);
1030 for (row_type row = 0; row < nrows(); ++row)
1031 numbered(row, !old);
1032 cur.message(old ? _("No number") : _("Number"));
1036 case LFUN_MATH_NONUMBER:
1038 recordUndoInset(cur);
1039 row_type r = (type_ == "multline") ? nrows() - 1 : cur.row();
1040 bool old = numbered(r);
1041 cur.message(old ? _("No number") : _("Number"));
1046 case LFUN_LABEL_INSERT: {
1047 recordUndoInset(cur);
1048 row_type r = (type_ == "multline") ? nrows() - 1 : cur.row();
1049 string old_label = label(r);
1050 string const default_label =
1051 (lyxrc.label_init_length >= 0) ? "eq:" : "";
1052 if (old_label.empty())
1053 old_label = default_label;
1054 string const contents = cmd.argument.empty() ?
1055 old_label : cmd.argument;
1057 InsetCommandParams p("label", contents);
1058 string const data = InsetCommandMailer::params2string("label", p);
1060 if (cmd.argument.empty()) {
1061 cur.bv().owner()->getDialogs().show("label", data, 0);
1063 FuncRequest fr(LFUN_INSET_INSERT, data);
1069 case LFUN_INSET_INSERT: {
1070 //lyxerr << "arg: " << cmd.argument << endl;
1071 string const name = cmd.getArg(0);
1072 if (name == "label") {
1073 InsetCommandParams p;
1074 InsetCommandMailer::string2params(name, cmd.argument, p);
1075 string str = p.getContents();
1076 recordUndoInset(cur);
1077 row_type const r = (type_ == "multline") ? nrows() - 1 : cur.row();
1078 str = lyx::support::trim(str);
1081 string old = label(r);
1083 cur.bv().buffer()->changeRefsIfUnique(old, str);
1089 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
1097 case LFUN_MATH_EXTERN:
1098 recordUndoInset(cur);
1102 case LFUN_MATH_MUTATE: {
1103 recordUndoInset(cur);
1104 row_type row = cur.row();
1105 col_type col = cur.col();
1106 mutate(cmd.argument);
1107 cur.idx() = row * ncols() + col;
1108 if (cur.idx() > cur.lastidx()) {
1109 cur.idx() = cur.lastidx();
1110 cur.pos() = cur.lastpos();
1112 if (cur.pos() > cur.lastpos())
1113 cur.pos() = cur.lastpos();
1114 //cur.dispatched(FINISHED);
1118 case LFUN_MATH_DISPLAY: {
1119 recordUndoInset(cur);
1120 mutate(type_ == "simple" ? "equation" : "simple");
1122 cur.pos() = cur.lastpos();
1123 //cur.dispatched(FINISHED);
1128 MathGridInset::doDispatch(cur, cmd);
1134 bool MathHullInset::getStatus(LCursor & cur, FuncRequest const & cmd,
1135 FuncStatus & status) const
1137 switch (cmd.action) {
1138 case LFUN_FINISHED_LEFT:
1139 case LFUN_FINISHED_RIGHT:
1140 case LFUN_FINISHED_UP:
1141 case LFUN_FINISHED_DOWN:
1142 status.enabled(true);
1144 case LFUN_BREAK_LINE:
1145 case LFUN_MATH_NUMBER:
1146 case LFUN_MATH_NONUMBER:
1147 case LFUN_MATH_EXTERN:
1148 case LFUN_MATH_MUTATE:
1149 case LFUN_MATH_DISPLAY:
1151 status.enabled(true);
1153 case LFUN_LABEL_INSERT:
1154 status.enabled(type_ != "simple");
1156 case LFUN_INSET_INSERT: {
1157 // Don't test createMathInset_fromDialogStr(), since
1158 // getStatus is not called with a valid reference and the
1159 // dialog would not be applyable.
1160 string const name = cmd.getArg(0);
1161 status.enabled(name == "ref" ||
1162 (name == "label" && type_ != "simple"));
1165 case LFUN_TABULAR_FEATURE: {
1166 istringstream is(cmd.argument);
1170 && (s == "append-row"
1171 || s == "delete-row"
1172 || s == "copy-row")) {
1173 status.message(bformat(
1174 N_("Can't change number of rows in '%1$s'"),
1176 status.enabled(false);
1180 && (s == "append-column"
1181 || s == "delete-column"
1182 || s == "copy-column")) {
1183 status.message(bformat(
1184 N_("Can't change number of columns in '%1$s'"),
1186 status.enabled(false);
1189 if ((type_ == "simple"
1190 || type_ == "equation"
1191 || type_ == "none") &&
1192 (s == "add-hline-above" || s == "add-hline-below")) {
1193 status.message(bformat(
1194 N_("Can't add horizontal grid lines in '%1$s'"),
1196 status.enabled(false);
1199 if (s == "add-vline-left" || s == "add-vline-right") {
1200 status.message(bformat(
1201 N_("Can't add vertical grid lines in '%1$s'"),
1203 status.enabled(false);
1206 if (s == "valign-top" || s == "valign-middle"
1207 || s == "valign-bottom" || s == "align-left"
1208 || s == "align-center" || s == "align-right") {
1209 status.enabled(false);
1212 return MathGridInset::getStatus(cur, cmd, status);
1215 return MathGridInset::getStatus(cur, cmd, status);
1218 // This cannot really happen, but inserted to shut-up gcc
1219 return MathGridInset::getStatus(cur, cmd, status);
1223 /////////////////////////////////////////////////////////////////////
1225 #include "math_arrayinset.h"
1226 #include "math_deliminset.h"
1227 #include "math_factory.h"
1228 #include "math_parser.h"
1229 #include "math_spaceinset.h"
1230 #include "ref_inset.h"
1232 #include "bufferview_funcs.h"
1233 #include "lyxtext.h"
1235 #include "frontends/LyXView.h"
1236 #include "frontends/Dialogs.h"
1238 #include "support/lyxlib.h"
1241 // simply scrap this function if you want
1242 void MathHullInset::mutateToText()
1245 // translate to latex
1247 latex(NULL, os, false, false);
1248 string str = os.str();
1251 LyXText * lt = view_->getLyXText();
1252 string::const_iterator cit = str.begin();
1253 string::const_iterator end = str.end();
1254 for (; cit != end; ++cit)
1255 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
1258 //view_->owner()->dispatch(LFUN_ESCAPE);
1263 void MathHullInset::handleFont(LCursor & cur, string const & arg,
1264 string const & font)
1266 // this whole function is a hack and won't work for incremental font
1269 if (cur.inset().asMathInset()->name() == font)
1270 cur.handleFont(font);
1272 cur.handleNest(createMathInset(font));
1278 void MathHullInset::handleFont2(LCursor & cur, string const & arg)
1283 bv_funcs::string2font(arg, font, b);
1284 if (font.color() != LColor::inherit) {
1285 MathAtom at = MathAtom(new MathColorInset(true, font.color()));
1286 cur.handleNest(at, 0);
1291 void MathHullInset::edit(LCursor & cur, bool left)
1294 left ? idxFirst(cur) : idxLast(cur);
1298 string const MathHullInset::editMessage() const
1300 return _("Math editor mode");
1304 void MathHullInset::revealCodes(LCursor & cur) const
1306 if (!cur.inMathed())
1310 cur.message(os.str());
1312 // write something to the minibuffer
1313 // translate to latex
1317 string str = os.str();
1319 string::size_type pos = 0;
1321 for (string::iterator it = str.begin(); it != str.end(); ++it) {
1324 else if (*it == '\0') {
1326 pos = it - str.begin();
1332 res = res.substr(pos - 30);
1333 if (res.size() > 60)
1334 res = res.substr(0, 60);
1340 InsetBase::Code MathHullInset::lyxCode() const
1346 /////////////////////////////////////////////////////////////////////
1350 bool MathHullInset::searchForward(BufferView * bv, string const & str,
1353 #ifdef WITH_WARNINGS
1354 #warning completely broken
1356 static MathHullInset * lastformula = 0;
1357 static CursorBase current = DocIterator(ibegin(nucleus()));
1358 static MathArray ar;
1359 static string laststr;
1361 if (lastformula != this || laststr != str) {
1362 //lyxerr << "reset lastformula to " << this << endl;
1365 current = ibegin(nucleus());
1367 mathed_parse_cell(ar, str);
1371 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
1373 for (DocIterator it = current; it != iend(nucleus()); increment(it)) {
1374 CursorSlice & top = it.back();
1375 MathArray const & a = top.asMathInset()->cell(top.idx_);
1376 if (a.matchpart(ar, top.pos_)) {
1377 bv->cursor().setSelection(it, ar.size());
1379 top.pos_ += ar.size();
1385 //lyxerr << "not found!" << endl;
1392 void MathHullInset::write(Buffer const &, std::ostream & os) const
1394 WriteStream wi(os, false, false);
1400 void MathHullInset::read(Buffer const &, LyXLex & lex)
1403 mathed_parse_normal(at, lex);
1404 operator=(*at->asHullInset());
1408 int MathHullInset::plaintext(Buffer const &, ostream & os,
1409 OutputParams const &) const
1411 if (0 && display()) {
1415 TextPainter tpain(dim.width(), dim.height());
1416 drawT(tpain, 0, dim.ascent());
1418 // reset metrics cache to "real" values
1420 return tpain.textheight();
1422 WriteStream wi(os, false, true);
1429 int MathHullInset::linuxdoc(Buffer const & buf, ostream & os,
1430 OutputParams const & runparams) const
1432 return docbook(buf, os, runparams);
1436 int MathHullInset::docbook(Buffer const & buf, ostream & os,
1437 OutputParams const & runparams) const
1439 MathMLStream ms(os);
1442 if (getType() == "simple")
1443 name= "inlineequation";
1445 name = "informalequation";
1447 string bname = name;
1448 if (!label(0).empty())
1449 bname += " id=\"" + sgml::cleanID(buf, runparams, label(0)) + "\"";
1450 ms << MTag(bname.c_str());
1453 if (runparams.flavor == OutputParams::XML) {
1454 ms << MTag("alt role=\"tex\" ");
1455 // Workaround for db2latex: db2latex always includes equations with
1456 // \ensuremath{} or \begin{display}\end{display}
1457 // so we strip LyX' math environment
1458 WriteStream wi(ls, false, false);
1459 MathGridInset::write(wi);
1460 ms << subst(subst(ls.str(), "&", "&"), "<", "<");
1463 MathGridInset::mathmlize(ms);
1466 ms << MTag("alt role=\"tex\"");
1467 res = latex(buf, ls, runparams);
1468 ms << subst(subst(ls.str(), "&", "&"), "<", "<");
1472 ms << "<graphic fileref=\"eqn/";
1473 if ( !label(0).empty())
1474 ms << sgml::cleanID(buf, runparams, label(0));
1476 ms << sgml::uniqueID("anon");
1478 if (runparams.flavor == OutputParams::XML)
1483 ms << ETag(name.c_str());
1484 return ms.line() + res;
1488 int MathHullInset::textString(Buffer const & buf, ostream & os,
1489 OutputParams const & op) const
1491 return plaintext(buf, os, op);