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_hullinset.h"
18 #include "math_mathmlstream.h"
19 #include "math_streamstr.h"
20 #include "math_support.h"
23 #include "bufferparams.h"
24 #include "BufferView.h"
25 #include "CutAndPaste.h"
26 #include "FuncStatus.h"
28 #include "LaTeXFeatures.h"
31 #include "dispatchresult.h"
32 #include "funcrequest.h"
36 #include "outputparams.h"
38 #include "textpainter.h"
41 #include "insets/render_preview.h"
42 #include "insets/insetlabel.h"
44 #include "frontends/Dialogs.h"
45 #include "frontends/LyXView.h"
47 #include "graphics/PreviewImage.h"
48 #include "graphics/PreviewLoader.h"
50 #include "support/lstrings.h"
52 #include <boost/bind.hpp>
56 using lyx::cap::grabAndEraseSelection;
57 using lyx::support::bformat;
58 using lyx::support::subst;
65 using std::istringstream;
67 using std::ostringstream;
75 int getCols(string const & type)
77 if (type == "eqnarray")
81 if (type == "flalign")
83 if (type == "alignat")
85 if (type == "xalignat")
87 if (type == "xxalignat")
93 // returns position of first relation operator in the array
94 // used for "intelligent splitting"
95 size_t firstRelOp(MathArray const & ar)
97 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
99 return it - ar.begin();
104 char const * star(bool numbered)
106 return numbered ? "" : "*";
110 int typecode(string const & s)
112 if (s == "none") return 0;
113 if (s == "simple") return 1;
114 if (s == "equation") return 2;
115 if (s == "eqnarray") return 3;
116 if (s == "align") return 4;
117 if (s == "alignat") return 5;
118 if (s == "xalignat") return 6;
119 if (s == "xxalignat") return 7;
120 if (s == "multline") return 8;
121 if (s == "gather") return 9;
122 if (s == "flalign") return 10;
123 lyxerr << "unknown hull type '" << s << "'" << endl;
127 bool smaller(string const & s, string const & t)
129 return typecode(s) < typecode(t);
133 } // end anon namespace
137 MathHullInset::MathHullInset()
138 : MathGridInset(1, 1), type_("none"), nonum_(1), label_(1),
139 preview_(new RenderPreview(this))
141 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << endl;
142 //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl;
143 //lyxerr << "sizeof MathCharInset: " << sizeof(MathCharInset) << endl;
144 //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << endl;
149 MathHullInset::MathHullInset(string const & type)
150 : MathGridInset(getCols(type), 1), type_(type), nonum_(1), label_(1),
151 preview_(new RenderPreview(this))
157 MathHullInset::MathHullInset(MathHullInset const & other)
158 : MathGridInset(other),
159 type_(other.type_), nonum_(other.nonum_), label_(other.label_),
160 preview_(new RenderPreview(this))
164 MathHullInset::~MathHullInset()
168 auto_ptr<InsetBase> MathHullInset::doClone() const
170 return auto_ptr<InsetBase>(new MathHullInset(*this));
174 MathHullInset & MathHullInset::operator=(MathHullInset const & other)
178 *static_cast<MathGridInset*>(this) = MathGridInset(other);
180 nonum_ = other.nonum_;
181 label_ = other.label_;
182 preview_.reset(new RenderPreview(*other.preview_, this));
188 InsetBase * MathHullInset::editXY(LCursor & cur, int x, int y)
190 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
194 return MathNestInset::editXY(cur, x, y);
198 MathInset::mode_type MathHullInset::currentMode() const
201 return UNDECIDED_MODE;
202 // definitely math mode ...
207 bool MathHullInset::idxFirst(LCursor & cur) const
215 bool MathHullInset::idxLast(LCursor & cur) const
217 cur.idx() = nargs() - 1;
218 cur.pos() = cur.lastpos();
223 char MathHullInset::defaultColAlign(col_type col)
225 if (type_ == "eqnarray")
227 if (typecode(type_) >= typecode("align"))
228 return "rl"[col & 1];
233 int MathHullInset::defaultColSpace(col_type col)
235 if (type_ == "align" || type_ == "alignat")
237 if (type_ == "xalignat")
238 return (col & 1) ? 20 : 0;
239 if (type_ == "xxalignat" || type_ == "flalign")
240 return (col & 1) ? 40 : 0;
245 char const * MathHullInset::standardFont() const
248 return "lyxnochange";
253 void MathHullInset::metrics(MetricsInfo & mi, Dimension & dim) const
255 BOOST_ASSERT(mi.base.bv && mi.base.bv->buffer());
257 bool use_preview = false;
258 if (!editing(mi.base.bv) &&
259 RenderPreview::status() == LyXRC::PREVIEW_ON) {
260 lyx::graphics::PreviewImage const * pimage =
261 preview_->getPreviewImage(*mi.base.bv->buffer());
262 use_preview = pimage && pimage->image();
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 BOOST_ASSERT(pi.base.bv && pi.base.bv->buffer());
311 bool use_preview = false;
312 if (!editing(pi.base.bv) &&
313 RenderPreview::status() == LyXRC::PREVIEW_ON) {
314 lyx::graphics::PreviewImage const * pimage =
315 preview_->getPreviewImage(*pi.base.bv->buffer());
316 use_preview = pimage && pimage->image();
320 // one pixel gap in front
321 preview_->draw(pi, x + 1, y);
322 setPosCache(pi, x, y);
326 FontSetChanger dummy1(pi.base, standardFont());
327 StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
328 MathGridInset::draw(pi, x + 1, y);
330 if (numberedType()) {
331 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
332 for (row_type row = 0; row < nrows(); ++row) {
333 int const yy = y + rowinfo_[row].offset_;
334 FontSetChanger dummy(pi.base, "mathrm");
335 pi.draw(xx, yy, nicelabel(row));
338 setPosCache(pi, x, y);
342 void MathHullInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
345 MathGridInset::metricsT(mi, dim);
348 WriteStream wi(os, false, true);
350 dim.wid = os.str().size();
357 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
360 MathGridInset::drawT(pain, x, y);
363 WriteStream wi(os, false, true);
365 pain.draw(x, y, os.str().c_str());
372 string const latex_string(MathHullInset const & inset)
375 WriteStream wi(ls, false, false);
383 void MathHullInset::addPreview(lyx::graphics::PreviewLoader & ploader) const
385 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
386 string const snippet = latex_string(*this);
387 preview_->addPreview(snippet, ploader);
392 void MathHullInset::notifyCursorLeaves(LCursor & cur)
394 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
395 Buffer const & buffer = cur.buffer();
396 string const snippet = latex_string(*this);
397 preview_->addPreview(snippet, buffer);
398 preview_->startLoading(buffer);
403 string MathHullInset::label(row_type row) const
405 BOOST_ASSERT(row < nrows());
410 void MathHullInset::label(row_type row, string const & label)
412 //lyxerr << "setting label '" << label << "' for row " << row << endl;
417 void MathHullInset::numbered(row_type row, bool num)
423 bool MathHullInset::numbered(row_type row) const
429 bool MathHullInset::ams() const
433 type_ == "flalign" ||
434 type_ == "multline" ||
436 type_ == "alignat" ||
437 type_ == "xalignat" ||
438 type_ == "xxalignat";
442 bool MathHullInset::display() const
444 return type_ != "simple" && type_ != "none";
448 void MathHullInset::getLabelList(Buffer const &, vector<string> & labels) const
450 for (row_type row = 0; row < nrows(); ++row)
451 if (!label_[row].empty() && nonum_[row] != 1)
452 labels.push_back(label_[row]);
456 bool MathHullInset::numberedType() const
460 if (type_ == "simple")
462 if (type_ == "xxalignat")
464 for (row_type row = 0; row < nrows(); ++row)
471 void MathHullInset::validate(LaTeXFeatures & features) const
474 features.require("amsmath");
477 // Validation is necessary only if not using AMS math.
478 // To be safe, we will always run mathedvalidate.
479 //if (features.amsstyle)
482 features.require("boldsymbol");
483 //features.binom = true;
485 MathGridInset::validate(features);
489 void MathHullInset::header_write(WriteStream & os) const
491 bool n = numberedType();
496 else if (type_ == "simple") {
502 else if (type_ == "equation") {
504 os << "\\begin{equation" << star(n) << "}\n";
509 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
510 || type_ == "gather" || type_ == "multline")
511 os << "\\begin{" << type_ << star(n) << "}\n";
513 else if (type_ == "alignat" || type_ == "xalignat")
514 os << "\\begin{" << type_ << star(n) << '}'
515 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
517 else if (type_ == "xxalignat")
518 os << "\\begin{" << type_ << '}'
519 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
522 os << "\\begin{unknown" << star(n) << '}';
526 void MathHullInset::footer_write(WriteStream & os) const
528 bool n = numberedType();
533 else if (type_ == "simple")
536 else if (type_ == "equation")
538 os << "\\end{equation" << star(n) << "}\n";
542 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
543 || type_ == "alignat" || type_ == "xalignat"
544 || type_ == "gather" || type_ == "multline")
545 os << "\\end{" << type_ << star(n) << "}\n";
547 else if (type_ == "xxalignat")
548 os << "\\end{" << type_ << "}\n";
551 os << "\\end{unknown" << star(n) << '}';
555 bool MathHullInset::rowChangeOK() const
558 type_ == "eqnarray" || type_ == "align" ||
559 type_ == "flalign" || type_ == "alignat" ||
560 type_ == "xalignat" || type_ == "xxalignat" ||
561 type_ == "gather" || type_ == "multline";
565 bool MathHullInset::colChangeOK() const
568 type_ == "align" || type_ == "flalign" ||type_ == "alignat" ||
569 type_ == "xalignat" || type_ == "xxalignat";
573 void MathHullInset::addRow(row_type row)
577 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
578 label_.insert(label_.begin() + row + 1, string());
579 MathGridInset::addRow(row);
583 void MathHullInset::swapRow(row_type row)
587 if (row + 1 == nrows())
589 swap(nonum_[row], nonum_[row + 1]);
590 swap(label_[row], label_[row + 1]);
591 MathGridInset::swapRow(row);
595 void MathHullInset::delRow(row_type row)
597 if (nrows() <= 1 || !rowChangeOK())
599 MathGridInset::delRow(row);
600 nonum_.erase(nonum_.begin() + row);
601 label_.erase(label_.begin() + row);
605 void MathHullInset::addCol(col_type col)
609 MathGridInset::addCol(col);
613 void MathHullInset::delCol(col_type col)
615 if (ncols() <= 1 || !colChangeOK())
617 MathGridInset::delCol(col);
621 string MathHullInset::nicelabel(row_type row) const
625 if (label_[row].empty())
626 return string("(#)");
627 return '(' + label_[row] + ')';
631 void MathHullInset::glueall()
634 for (idx_type i = 0; i < nargs(); ++i)
636 *this = MathHullInset("simple");
642 void MathHullInset::splitTo2Cols()
644 BOOST_ASSERT(ncols() == 1);
645 MathGridInset::addCol(1);
646 for (row_type row = 0; row < nrows(); ++row) {
647 idx_type const i = 2 * row;
648 pos_type pos = firstRelOp(cell(i));
649 cell(i + 1) = MathArray(cell(i).begin() + pos, cell(i).end());
650 cell(i).erase(pos, cell(i).size());
655 void MathHullInset::splitTo3Cols()
657 BOOST_ASSERT(ncols() < 3);
660 MathGridInset::addCol(1);
661 for (row_type row = 0; row < nrows(); ++row) {
662 idx_type const i = 3 * row + 1;
663 if (cell(i).size()) {
664 cell(i + 1) = MathArray(cell(i).begin() + 1, cell(i).end());
665 cell(i).erase(1, cell(i).size());
671 void MathHullInset::changeCols(col_type cols)
675 else if (ncols() < cols) {
681 while (ncols() < cols)
682 MathGridInset::addCol(ncols() - 1);
688 for (row_type row = 0; row < nrows(); ++row) {
689 idx_type const i = row * ncols();
690 for (col_type col = cols; col < ncols(); ++col) {
691 cell(i + cols - 1).append(cell(i + col));
695 while (ncols() > cols) {
696 MathGridInset::delCol(ncols() - 1);
701 string const & MathHullInset::getType() const
707 void MathHullInset::setType(string const & type)
715 void MathHullInset::mutate(string const & newtype)
717 //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
719 // we try to move along the chain
720 // none <-> simple <-> equation <-> eqnarray -> *align* -> multline, gather -+
722 // +-------------------------------------+
723 // we use eqnarray as intermediate type for mutations that are not
724 // directly supported because it handles labels and numbering for
727 if (newtype == "dump") {
731 else if (newtype == type_) {
735 else if (typecode(newtype) < 0) {
739 else if (type_ == "none") {
745 else if (type_ == "simple") {
746 if (newtype == "none") {
756 else if (type_ == "equation") {
757 if (smaller(newtype, type_)) {
761 } else if (newtype == "eqnarray") {
762 // split it "nicely" on the first relop
765 } else if (newtype == "multline" || newtype == "gather") {
775 else if (type_ == "eqnarray") {
776 if (smaller(newtype, type_)) {
777 // set correct (no)numbering
778 bool allnonum = true;
779 for (row_type row = 0; row < nrows(); ++row)
783 // set first non-empty label
785 for (row_type row = 0; row < nrows(); ++row) {
786 if (!label_[row].empty()) {
793 nonum_[0] = allnonum;
796 } else { // align & Co.
803 else if (type_ == "align" || type_ == "alignat" ||
804 type_ == "xalignat" || type_ == "flalign") {
805 if (smaller(newtype, "align")) {
809 } else if (newtype == "gather" || newtype == "multline") {
812 } else if (newtype == "xxalignat") {
813 for (row_type row = 0; row < nrows(); ++row)
814 numbered(row, false);
821 else if (type_ == "xxalignat") {
822 for (row_type row = 0; row < nrows(); ++row)
823 numbered(row, false);
824 if (smaller(newtype, "align")) {
828 } else if (newtype == "gather" || newtype == "multline") {
836 else if (type_ == "multline" || type_ == "gather") {
837 if (newtype == "gather" || newtype == "multline")
839 else if (newtype == "align" || newtype == "flalign" ||
840 newtype == "alignat" || newtype == "xalignat") {
843 } else if (newtype == "xxalignat") {
845 for (row_type row = 0; row < nrows(); ++row)
846 numbered(row, false);
856 lyxerr << "mutation from '" << type_
857 << "' to '" << newtype << "' not implemented" << endl;
862 string MathHullInset::eolString(row_type row, bool fragile) const
865 if (numberedType()) {
866 if (!label_[row].empty() && !nonum_[row])
867 res += "\\label{" + label_[row] + '}';
868 if (nonum_[row] && (type_ != "multline"))
869 res += "\\nonumber ";
871 return res + MathGridInset::eolString(row, fragile);
875 void MathHullInset::write(WriteStream & os) const
878 MathGridInset::write(os);
883 void MathHullInset::normalize(NormalStream & os) const
885 os << "[formula " << type_ << ' ';
886 MathGridInset::normalize(os);
891 void MathHullInset::mathmlize(MathMLStream & os) const
893 MathGridInset::mathmlize(os);
897 void MathHullInset::infoize(ostream & os) const
899 os << "Type: " << type_;
903 void MathHullInset::check() const
905 BOOST_ASSERT(nonum_.size() == nrows());
906 BOOST_ASSERT(label_.size() == nrows());
910 void MathHullInset::doExtern(LCursor & cur, FuncRequest & func)
914 istringstream iss(func.argument);
915 iss >> lang >> extra;
920 #warning temporarily disabled
921 //if (cur.selection()) {
924 // lyxerr << "use selection: " << ar << endl;
925 // insert(pipeThroughExtern(lang, extra, ar));
931 eq.push_back(MathAtom(new MathCharInset('=')));
933 // go to first item in line
934 cur.idx() -= cur.idx() % ncols();
937 if (getType() == "simple") {
938 size_type pos = cur.cell().find_last(eq);
940 if (cur.inMathed() && cur.selection()) {
941 asArray(grabAndEraseSelection(cur), ar);
942 } else if (pos == cur.cell().size()) {
944 lyxerr << "use whole cell: " << ar << endl;
946 ar = MathArray(cur.cell().begin() + pos + 1, cur.cell().end());
947 lyxerr << "use partial cell form pos: " << pos << endl;
949 cur.cell().append(eq);
950 cur.cell().append(pipeThroughExtern(lang, extra, ar));
951 cur.pos() = cur.lastpos();
955 if (getType() == "equation") {
956 lyxerr << "use equation inset" << endl;
958 MathArray & ar = cur.cell();
959 lyxerr << "use cell: " << ar << endl;
963 cur.cell() = pipeThroughExtern(lang, extra, ar);
964 // move to end of line
965 cur.pos() = cur.lastpos();
970 lyxerr << "use eqnarray" << endl;
971 cur.idx() += 2 - cur.idx() % ncols();
973 MathArray ar = cur.cell();
974 lyxerr << "use cell: " << ar << endl;
976 #warning temporarily disabled
983 cur.cell() = pipeThroughExtern(lang, extra, ar);
984 cur.pos() = cur.lastpos();
989 void MathHullInset::doDispatch(LCursor & cur, FuncRequest & cmd)
991 switch (cmd.action) {
993 case LFUN_FINISHED_LEFT:
994 case LFUN_FINISHED_RIGHT:
995 case LFUN_FINISHED_UP:
996 case LFUN_FINISHED_DOWN:
997 MathGridInset::doDispatch(cur, cmd);
998 notifyCursorLeaves(cur);
1001 case LFUN_BREAKPARAGRAPH:
1002 // just swallow this
1005 case LFUN_BREAKLINE:
1006 // some magic for the common case
1007 if (type_ == "simple" || type_ == "equation") {
1008 recordUndoInset(cur);
1010 cur.bv().buffer()->params().use_amsmath == BufferParams::AMS_ON;
1011 mutate(align ? "align" : "eqnarray");
1013 cur.pos() = cur.lastpos();
1015 MathGridInset::doDispatch(cur, cmd);
1018 case LFUN_MATH_NUMBER:
1019 //lyxerr << "toggling all numbers" << endl;
1021 recordUndoInset(cur);
1022 bool old = numberedType();
1023 if (type_ == "multline")
1024 numbered(nrows() - 1, !old);
1026 for (row_type row = 0; row < nrows(); ++row)
1027 numbered(row, !old);
1028 cur.message(old ? _("No number") : _("Number"));
1032 case LFUN_MATH_NONUMBER:
1034 recordUndoInset(cur);
1035 row_type r = (type_ == "multline") ? nrows() - 1 : cur.row();
1036 bool old = numbered(r);
1037 cur.message(old ? _("No number") : _("Number"));
1042 case LFUN_INSERT_LABEL: {
1043 recordUndoInset(cur);
1044 row_type r = (type_ == "multline") ? nrows() - 1 : cur.row();
1045 string old_label = label(r);
1046 string const default_label =
1047 (lyxrc.label_init_length >= 0) ? "eq:" : "";
1048 string const contents = cmd.argument.empty() ?
1049 label(r) : cmd.argument;
1051 InsetCommandParams p("label", contents);
1052 string const data = InsetCommandMailer::params2string("label", p);
1054 if (cmd.argument.empty()) {
1055 cur.bv().owner()->getDialogs().show("label", data, 0);
1057 FuncRequest fr(LFUN_INSET_INSERT, data);
1063 case LFUN_INSET_INSERT: {
1064 //lyxerr << "arg: " << cmd.argument << endl;
1065 string const name = cmd.getArg(0);
1066 if (name == "label") {
1067 InsetCommandParams p;
1068 InsetCommandMailer::string2params(name, cmd.argument, p);
1069 string str = p.getContents();
1070 recordUndoInset(cur);
1071 row_type const r = (type_ == "multline") ? nrows() - 1 : cur.row();
1072 str = lyx::support::trim(str);
1075 string old = label(r);
1077 cur.bv().buffer()->changeRefsIfUnique(old, str);
1084 case LFUN_MATH_EXTERN:
1085 recordUndoInset(cur);
1089 case LFUN_MATH_MUTATE: {
1090 recordUndoInset(cur);
1091 row_type row = cur.row();
1092 col_type col = cur.col();
1093 mutate(cmd.argument);
1094 cur.idx() = row * ncols() + col;
1095 if (cur.idx() > cur.lastidx()) {
1096 cur.idx() = cur.lastidx();
1097 cur.pos() = cur.lastpos();
1099 if (cur.pos() > cur.lastpos())
1100 cur.pos() = cur.lastpos();
1101 //cur.dispatched(FINISHED);
1105 case LFUN_MATH_DISPLAY: {
1106 recordUndoInset(cur);
1107 mutate(type_ == "simple" ? "equation" : "simple");
1109 cur.pos() = cur.lastpos();
1110 //cur.dispatched(FINISHED);
1115 MathGridInset::doDispatch(cur, cmd);
1121 bool MathHullInset::getStatus(LCursor & cur, FuncRequest const & cmd,
1122 FuncStatus & flag) const
1124 switch (cmd.action) {
1125 case LFUN_BREAKLINE:
1126 case LFUN_MATH_NUMBER:
1127 case LFUN_MATH_NONUMBER:
1128 case LFUN_MATH_EXTERN:
1129 case LFUN_MATH_MUTATE:
1130 case LFUN_MATH_DISPLAY:
1134 case LFUN_INSERT_LABEL:
1135 flag.enabled(type_ != "simple");
1137 case LFUN_TABULAR_FEATURE: {
1138 istringstream is(cmd.argument);
1142 && (s == "append-row"
1143 || s == "delete-row"
1144 || s == "copy-row")) {
1145 flag.message(bformat(
1146 N_("Can't change number of rows in '%1$s'"),
1148 flag.enabled(false);
1152 && (s == "append-column"
1153 || s == "delete-column"
1154 || s == "copy-column")) {
1155 flag.message(bformat(
1156 N_("Can't change number of columns in '%1$s'"),
1158 flag.enabled(false);
1161 if ((type_ == "simple"
1162 || type_ == "equation"
1163 || type_ == "none") &&
1164 (s == "add-hline-above" || s == "add-hline-below")) {
1165 flag.message(bformat(
1166 N_("Can't add horizontal grid lines in '%1$s'"),
1168 flag.enabled(false);
1171 if (s == "add-vline-left" || s == "add-vline-right") {
1172 flag.message(bformat(
1173 N_("Can't add vertical grid lines in '%1$s'"),
1175 flag.enabled(false);
1178 if (s == "valign-top" || s == "valign-middle"
1179 || s == "valign-bottom" || s == "align-left"
1180 || s == "align-center" || s == "align-right") {
1181 flag.enabled(false);
1184 return MathGridInset::getStatus(cur, cmd, flag);
1187 return MathGridInset::getStatus(cur, cmd, flag);
1192 /////////////////////////////////////////////////////////////////////
1194 #include "math_arrayinset.h"
1195 #include "math_deliminset.h"
1196 #include "math_factory.h"
1197 #include "math_parser.h"
1198 #include "math_spaceinset.h"
1199 #include "ref_inset.h"
1201 #include "bufferview_funcs.h"
1202 #include "lyxtext.h"
1204 #include "frontends/LyXView.h"
1205 #include "frontends/Dialogs.h"
1207 #include "support/lyxlib.h"
1210 // simply scrap this function if you want
1211 void MathHullInset::mutateToText()
1214 // translate to latex
1216 latex(NULL, os, false, false);
1217 string str = os.str();
1220 LyXText * lt = view_->getLyXText();
1221 string::const_iterator cit = str.begin();
1222 string::const_iterator end = str.end();
1223 for (; cit != end; ++cit)
1224 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
1227 //view_->owner()->dispatch(LFUN_ESCAPE);
1232 void MathHullInset::handleFont(LCursor & cur, string const & arg,
1233 string const & font)
1235 // this whole function is a hack and won't work for incremental font
1238 if (cur.inset().asMathInset()->name() == font)
1239 cur.handleFont(font);
1241 cur.handleNest(createMathInset(font));
1247 void MathHullInset::handleFont2(LCursor & cur, string const & arg)
1252 bv_funcs::string2font(arg, font, b);
1253 if (font.color() != LColor::inherit) {
1254 MathAtom at = MathAtom(new MathColorInset(true, font.color()));
1255 cur.handleNest(at, 0);
1260 void MathHullInset::edit(LCursor & cur, bool left)
1263 left ? idxFirst(cur) : idxLast(cur);
1267 string const MathHullInset::editMessage() const
1269 return _("Math editor mode");
1273 void MathHullInset::revealCodes(LCursor & cur) const
1275 if (!cur.inMathed())
1279 cur.message(os.str());
1281 // write something to the minibuffer
1282 // translate to latex
1286 string str = os.str();
1288 string::size_type pos = 0;
1290 for (string::iterator it = str.begin(); it != str.end(); ++it) {
1293 else if (*it == '\0') {
1295 pos = it - str.begin();
1301 res = res.substr(pos - 30);
1302 if (res.size() > 60)
1303 res = res.substr(0, 60);
1309 InsetBase::Code MathHullInset::lyxCode() const
1315 /////////////////////////////////////////////////////////////////////
1319 bool MathHullInset::searchForward(BufferView * bv, string const & str,
1322 #ifdef WITH_WARNINGS
1323 #warning completely broken
1325 static MathHullInset * lastformula = 0;
1326 static CursorBase current = DocIterator(ibegin(nucleus()));
1327 static MathArray ar;
1328 static string laststr;
1330 if (lastformula != this || laststr != str) {
1331 //lyxerr << "reset lastformula to " << this << endl;
1334 current = ibegin(nucleus());
1336 mathed_parse_cell(ar, str);
1340 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
1342 for (DocIterator it = current; it != iend(nucleus()); increment(it)) {
1343 CursorSlice & top = it.back();
1344 MathArray const & a = top.asMathInset()->cell(top.idx_);
1345 if (a.matchpart(ar, top.pos_)) {
1346 bv->cursor().setSelection(it, ar.size());
1348 top.pos_ += ar.size();
1354 //lyxerr << "not found!" << endl;
1361 void MathHullInset::write(Buffer const &, std::ostream & os) const
1363 WriteStream wi(os, false, false);
1369 void MathHullInset::read(Buffer const &, LyXLex & lex)
1372 mathed_parse_normal(at, lex);
1373 operator=(*at->asHullInset());
1377 int MathHullInset::plaintext(Buffer const &, ostream & os,
1378 OutputParams const &) const
1380 if (0 && display()) {
1384 TextPainter tpain(dim.width(), dim.height());
1385 drawT(tpain, 0, dim.ascent());
1387 // reset metrics cache to "real" values
1389 return tpain.textheight();
1391 WriteStream wi(os, false, true);
1398 int MathHullInset::linuxdoc(Buffer const & buf, ostream & os,
1399 OutputParams const & runparams) const
1401 return docbook(buf, os, runparams);
1405 int MathHullInset::docbook(Buffer const & buf, ostream & os,
1406 OutputParams const & runparams) const
1408 MathMLStream ms(os);
1411 if (getType() == "simple")
1412 name= "inlineequation";
1414 name = "informalequation";
1416 string bname = name;
1417 if (!label(0).empty())
1418 bname += " id=\"" + sgml::cleanID(buf, runparams, label(0)) + "\"";
1419 ms << MTag(bname.c_str());
1422 if (runparams.flavor == OutputParams::XML) {
1423 ms << MTag("alt role=\"tex\" ");
1424 // Workaround for db2latex: db2latex always includes equations with
1425 // \ensuremath{} or \begin{display}\end{display}
1426 // so we strip LyX' math environment
1427 WriteStream wi(ls, false, false);
1428 MathGridInset::write(wi);
1429 ms << subst(subst(ls.str(), "&", "&"), "<", "<");
1432 MathGridInset::mathmlize(ms);
1435 ms << MTag("alt role=\"tex\"");
1436 res = latex(buf, ls, runparams);
1437 ms << subst(subst(ls.str(), "&", "&"), "<", "<");
1441 ms << "<graphic fileref=\"eqn/";
1442 if ( !label(0).empty())
1443 ms << sgml::cleanID(buf, runparams, label(0));
1445 ms << sgml::uniqueID("anon");
1447 if (runparams.flavor == OutputParams::XML)
1452 ms << ETag(name.c_str());
1453 return ms.line() + res;