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)
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
247 return type_ == "none" ? "lyxnochange" : "mathnormal";
251 bool MathHullInset::previewState(BufferView * bv) const
253 if (!editing(bv) && RenderPreview::status() == LyXRC::PREVIEW_ON) {
254 lyx::graphics::PreviewImage const * pimage =
255 preview_->getPreviewImage(*bv->buffer());
256 return pimage && pimage->image();
262 void MathHullInset::metrics(MetricsInfo & mi, Dimension & dim) const
264 if (previewState(mi.base.bv)) {
265 preview_->metrics(mi, dim);
266 // insert a one pixel gap in front of the formula
274 FontSetChanger dummy1(mi.base, standardFont());
275 StyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
277 // let the cells adjust themselves
278 MathGridInset::metrics(mi, dim);
285 if (numberedType()) {
286 FontSetChanger dummy(mi.base, "mathbf");
288 for (row_type row = 0; row < nrows(); ++row)
289 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
295 // make it at least as high as the current font
298 math_font_max_dim(mi.base.font, asc, des);
299 dim.asc = max(dim.asc, asc);
300 dim.des = max(dim.des, des);
306 void MathHullInset::draw(PainterInfo & pi, int x, int y) const
308 use_preview_ = previewState(pi.base.bv);
311 // one pixel gap in front
312 preview_->draw(pi, x + 1, y);
313 setPosCache(pi, x, y);
317 FontSetChanger dummy1(pi.base, standardFont());
318 StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
319 MathGridInset::draw(pi, x + 1, y);
321 if (numberedType()) {
322 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
323 for (row_type row = 0; row < nrows(); ++row) {
324 int const yy = y + rowinfo_[row].offset_;
325 FontSetChanger dummy(pi.base, "mathrm");
326 pi.draw(xx, yy, nicelabel(row));
329 setPosCache(pi, x, y);
333 void MathHullInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
336 MathGridInset::metricsT(mi, dim);
339 WriteStream wi(os, false, true);
341 dim.wid = os.str().size();
348 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
351 MathGridInset::drawT(pain, x, y);
354 WriteStream wi(os, false, true);
356 pain.draw(x, y, os.str().c_str());
363 string const latex_string(MathHullInset const & inset)
366 WriteStream wi(ls, false, false);
374 void MathHullInset::addPreview(lyx::graphics::PreviewLoader & ploader) const
376 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
377 string const snippet = latex_string(*this);
378 preview_->addPreview(snippet, ploader);
383 void MathHullInset::notifyCursorLeaves(LCursor & cur)
385 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
386 Buffer const & buffer = cur.buffer();
387 string const snippet = latex_string(*this);
388 preview_->addPreview(snippet, buffer);
389 preview_->startLoading(buffer);
394 string MathHullInset::label(row_type row) const
396 BOOST_ASSERT(row < nrows());
401 void MathHullInset::label(row_type row, string const & label)
403 //lyxerr << "setting label '" << label << "' for row " << row << endl;
408 void MathHullInset::numbered(row_type row, bool num)
414 bool MathHullInset::numbered(row_type row) const
420 bool MathHullInset::ams() const
424 type_ == "flalign" ||
425 type_ == "multline" ||
427 type_ == "alignat" ||
428 type_ == "xalignat" ||
429 type_ == "xxalignat";
433 bool MathHullInset::display() const
435 return type_ != "simple" && type_ != "none";
439 void MathHullInset::getLabelList(Buffer const &, vector<string> & labels) const
441 for (row_type row = 0; row < nrows(); ++row)
442 if (!label_[row].empty() && nonum_[row] != 1)
443 labels.push_back(label_[row]);
447 bool MathHullInset::numberedType() const
451 if (type_ == "simple")
453 if (type_ == "xxalignat")
455 for (row_type row = 0; row < nrows(); ++row)
462 void MathHullInset::validate(LaTeXFeatures & features) const
465 features.require("amsmath");
468 // Validation is necessary only if not using AMS math.
469 // To be safe, we will always run mathedvalidate.
470 //if (features.amsstyle)
473 features.require("boldsymbol");
474 //features.binom = true;
476 MathGridInset::validate(features);
480 void MathHullInset::header_write(WriteStream & os) const
482 bool n = numberedType();
487 else if (type_ == "simple") {
493 else if (type_ == "equation") {
495 os << "\\begin{equation" << star(n) << "}\n";
500 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
501 || type_ == "gather" || type_ == "multline")
502 os << "\\begin{" << type_ << star(n) << "}\n";
504 else if (type_ == "alignat" || type_ == "xalignat")
505 os << "\\begin{" << type_ << star(n) << '}'
506 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
508 else if (type_ == "xxalignat")
509 os << "\\begin{" << type_ << '}'
510 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
513 os << "\\begin{unknown" << star(n) << '}';
517 void MathHullInset::footer_write(WriteStream & os) const
519 bool n = numberedType();
524 else if (type_ == "simple")
527 else if (type_ == "equation")
529 os << "\\end{equation" << star(n) << "}\n";
533 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
534 || type_ == "alignat" || type_ == "xalignat"
535 || type_ == "gather" || type_ == "multline")
536 os << "\\end{" << type_ << star(n) << "}\n";
538 else if (type_ == "xxalignat")
539 os << "\\end{" << type_ << "}\n";
542 os << "\\end{unknown" << star(n) << '}';
546 bool MathHullInset::rowChangeOK() const
549 type_ == "eqnarray" || type_ == "align" ||
550 type_ == "flalign" || type_ == "alignat" ||
551 type_ == "xalignat" || type_ == "xxalignat" ||
552 type_ == "gather" || type_ == "multline";
556 bool MathHullInset::colChangeOK() const
559 type_ == "align" || type_ == "flalign" ||type_ == "alignat" ||
560 type_ == "xalignat" || type_ == "xxalignat";
564 void MathHullInset::addRow(row_type row)
568 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
569 label_.insert(label_.begin() + row + 1, string());
570 MathGridInset::addRow(row);
574 void MathHullInset::swapRow(row_type row)
578 if (row + 1 == nrows())
580 swap(nonum_[row], nonum_[row + 1]);
581 swap(label_[row], label_[row + 1]);
582 MathGridInset::swapRow(row);
586 void MathHullInset::delRow(row_type row)
588 if (nrows() <= 1 || !rowChangeOK())
590 MathGridInset::delRow(row);
591 nonum_.erase(nonum_.begin() + row);
592 label_.erase(label_.begin() + row);
596 void MathHullInset::addCol(col_type col)
600 MathGridInset::addCol(col);
604 void MathHullInset::delCol(col_type col)
606 if (ncols() <= 1 || !colChangeOK())
608 MathGridInset::delCol(col);
612 string MathHullInset::nicelabel(row_type row) const
616 if (label_[row].empty())
617 return string("(#)");
618 return '(' + label_[row] + ')';
622 void MathHullInset::glueall()
625 for (idx_type i = 0; i < nargs(); ++i)
627 *this = MathHullInset("simple");
633 void MathHullInset::splitTo2Cols()
635 BOOST_ASSERT(ncols() == 1);
636 MathGridInset::addCol(1);
637 for (row_type row = 0; row < nrows(); ++row) {
638 idx_type const i = 2 * row;
639 pos_type pos = firstRelOp(cell(i));
640 cell(i + 1) = MathArray(cell(i).begin() + pos, cell(i).end());
641 cell(i).erase(pos, cell(i).size());
646 void MathHullInset::splitTo3Cols()
648 BOOST_ASSERT(ncols() < 3);
651 MathGridInset::addCol(1);
652 for (row_type row = 0; row < nrows(); ++row) {
653 idx_type const i = 3 * row + 1;
654 if (cell(i).size()) {
655 cell(i + 1) = MathArray(cell(i).begin() + 1, cell(i).end());
656 cell(i).erase(1, cell(i).size());
662 void MathHullInset::changeCols(col_type cols)
666 else if (ncols() < cols) {
672 while (ncols() < cols)
673 MathGridInset::addCol(ncols() - 1);
679 for (row_type row = 0; row < nrows(); ++row) {
680 idx_type const i = row * ncols();
681 for (col_type col = cols; col < ncols(); ++col) {
682 cell(i + cols - 1).append(cell(i + col));
686 while (ncols() > cols) {
687 MathGridInset::delCol(ncols() - 1);
692 string const & MathHullInset::getType() const
698 void MathHullInset::setType(string const & type)
706 void MathHullInset::mutate(string const & newtype)
708 //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
710 // we try to move along the chain
711 // none <-> simple <-> equation <-> eqnarray -> *align* -> multline, gather -+
713 // +-------------------------------------+
714 // we use eqnarray as intermediate type for mutations that are not
715 // directly supported because it handles labels and numbering for
718 if (newtype == "dump") {
722 else if (newtype == type_) {
726 else if (typecode(newtype) < 0) {
730 else if (type_ == "none") {
736 else if (type_ == "simple") {
737 if (newtype == "none") {
747 else if (type_ == "equation") {
748 if (smaller(newtype, type_)) {
752 } else if (newtype == "eqnarray") {
753 // split it "nicely" on the first relop
756 } else if (newtype == "multline" || newtype == "gather") {
766 else if (type_ == "eqnarray") {
767 if (smaller(newtype, type_)) {
768 // set correct (no)numbering
769 bool allnonum = true;
770 for (row_type row = 0; row < nrows(); ++row)
774 // set first non-empty label
776 for (row_type row = 0; row < nrows(); ++row) {
777 if (!label_[row].empty()) {
784 nonum_[0] = allnonum;
787 } else { // align & Co.
794 else if (type_ == "align" || type_ == "alignat" ||
795 type_ == "xalignat" || type_ == "flalign") {
796 if (smaller(newtype, "align")) {
800 } else if (newtype == "gather" || newtype == "multline") {
803 } else if (newtype == "xxalignat") {
804 for (row_type row = 0; row < nrows(); ++row)
805 numbered(row, false);
812 else if (type_ == "xxalignat") {
813 for (row_type row = 0; row < nrows(); ++row)
814 numbered(row, false);
815 if (smaller(newtype, "align")) {
819 } else if (newtype == "gather" || newtype == "multline") {
827 else if (type_ == "multline" || type_ == "gather") {
828 if (newtype == "gather" || newtype == "multline")
830 else if (newtype == "align" || newtype == "flalign" ||
831 newtype == "alignat" || newtype == "xalignat") {
834 } else if (newtype == "xxalignat") {
836 for (row_type row = 0; row < nrows(); ++row)
837 numbered(row, false);
847 lyxerr << "mutation from '" << type_
848 << "' to '" << newtype << "' not implemented" << endl;
853 string MathHullInset::eolString(row_type row, bool fragile) const
856 if (numberedType()) {
857 if (!label_[row].empty() && !nonum_[row])
858 res += "\\label{" + label_[row] + '}';
859 if (nonum_[row] && (type_ != "multline"))
860 res += "\\nonumber ";
862 return res + MathGridInset::eolString(row, fragile);
866 void MathHullInset::write(WriteStream & os) const
869 MathGridInset::write(os);
874 void MathHullInset::normalize(NormalStream & os) const
876 os << "[formula " << type_ << ' ';
877 MathGridInset::normalize(os);
882 void MathHullInset::mathmlize(MathMLStream & os) const
884 MathGridInset::mathmlize(os);
888 void MathHullInset::infoize(ostream & os) const
890 os << "Type: " << type_;
894 void MathHullInset::check() const
896 BOOST_ASSERT(nonum_.size() == nrows());
897 BOOST_ASSERT(label_.size() == nrows());
901 void MathHullInset::doExtern(LCursor & cur, FuncRequest & func)
905 istringstream iss(func.argument);
906 iss >> lang >> extra;
911 #warning temporarily disabled
912 //if (cur.selection()) {
915 // lyxerr << "use selection: " << ar << endl;
916 // insert(pipeThroughExtern(lang, extra, ar));
922 eq.push_back(MathAtom(new MathCharInset('=')));
924 // go to first item in line
925 cur.idx() -= cur.idx() % ncols();
928 if (getType() == "simple") {
929 size_type pos = cur.cell().find_last(eq);
931 if (cur.inMathed() && cur.selection()) {
932 asArray(grabAndEraseSelection(cur), ar);
933 } else if (pos == cur.cell().size()) {
935 lyxerr << "use whole cell: " << ar << endl;
937 ar = MathArray(cur.cell().begin() + pos + 1, cur.cell().end());
938 lyxerr << "use partial cell form pos: " << pos << endl;
940 cur.cell().append(eq);
941 cur.cell().append(pipeThroughExtern(lang, extra, ar));
942 cur.pos() = cur.lastpos();
946 if (getType() == "equation") {
947 lyxerr << "use equation inset" << endl;
949 MathArray & ar = cur.cell();
950 lyxerr << "use cell: " << ar << endl;
954 cur.cell() = pipeThroughExtern(lang, extra, ar);
955 // move to end of line
956 cur.pos() = cur.lastpos();
961 lyxerr << "use eqnarray" << endl;
962 cur.idx() += 2 - cur.idx() % ncols();
964 MathArray ar = cur.cell();
965 lyxerr << "use cell: " << ar << endl;
967 #warning temporarily disabled
974 cur.cell() = pipeThroughExtern(lang, extra, ar);
975 cur.pos() = cur.lastpos();
980 void MathHullInset::doDispatch(LCursor & cur, FuncRequest & cmd)
982 switch (cmd.action) {
984 case LFUN_FINISHED_LEFT:
985 case LFUN_FINISHED_RIGHT:
986 case LFUN_FINISHED_UP:
987 case LFUN_FINISHED_DOWN:
988 MathGridInset::doDispatch(cur, cmd);
989 notifyCursorLeaves(cur);
992 case LFUN_BREAKPARAGRAPH:
997 // some magic for the common case
998 if (type_ == "simple" || type_ == "equation") {
999 recordUndoInset(cur);
1001 cur.bv().buffer()->params().use_amsmath == BufferParams::AMS_ON;
1002 mutate(align ? "align" : "eqnarray");
1004 cur.pos() = cur.lastpos();
1006 MathGridInset::doDispatch(cur, cmd);
1009 case LFUN_MATH_NUMBER:
1010 //lyxerr << "toggling all numbers" << endl;
1012 recordUndoInset(cur);
1013 bool old = numberedType();
1014 if (type_ == "multline")
1015 numbered(nrows() - 1, !old);
1017 for (row_type row = 0; row < nrows(); ++row)
1018 numbered(row, !old);
1019 cur.message(old ? _("No number") : _("Number"));
1023 case LFUN_MATH_NONUMBER:
1025 recordUndoInset(cur);
1026 row_type r = (type_ == "multline") ? nrows() - 1 : cur.row();
1027 bool old = numbered(r);
1028 cur.message(old ? _("No number") : _("Number"));
1033 case LFUN_INSERT_LABEL: {
1034 recordUndoInset(cur);
1035 row_type r = (type_ == "multline") ? nrows() - 1 : cur.row();
1036 string old_label = label(r);
1037 string const default_label =
1038 (lyxrc.label_init_length >= 0) ? "eq:" : "";
1039 string const contents = cmd.argument.empty() ?
1040 label(r) : cmd.argument;
1042 InsetCommandParams p("label", contents);
1043 string const data = InsetCommandMailer::params2string("label", p);
1045 if (cmd.argument.empty()) {
1046 cur.bv().owner()->getDialogs().show("label", data, 0);
1048 FuncRequest fr(LFUN_INSET_INSERT, data);
1054 case LFUN_INSET_INSERT: {
1055 //lyxerr << "arg: " << cmd.argument << endl;
1056 string const name = cmd.getArg(0);
1057 if (name == "label") {
1058 InsetCommandParams p;
1059 InsetCommandMailer::string2params(name, cmd.argument, p);
1060 string str = p.getContents();
1061 recordUndoInset(cur);
1062 row_type const r = (type_ == "multline") ? nrows() - 1 : cur.row();
1063 str = lyx::support::trim(str);
1066 string old = label(r);
1068 cur.bv().buffer()->changeRefsIfUnique(old, str);
1075 case LFUN_MATH_EXTERN:
1076 recordUndoInset(cur);
1080 case LFUN_MATH_MUTATE: {
1081 recordUndoInset(cur);
1082 row_type row = cur.row();
1083 col_type col = cur.col();
1084 mutate(cmd.argument);
1085 cur.idx() = row * ncols() + col;
1086 if (cur.idx() > cur.lastidx()) {
1087 cur.idx() = cur.lastidx();
1088 cur.pos() = cur.lastpos();
1090 if (cur.pos() > cur.lastpos())
1091 cur.pos() = cur.lastpos();
1092 //cur.dispatched(FINISHED);
1096 case LFUN_MATH_DISPLAY: {
1097 recordUndoInset(cur);
1098 mutate(type_ == "simple" ? "equation" : "simple");
1100 cur.pos() = cur.lastpos();
1101 //cur.dispatched(FINISHED);
1106 MathGridInset::doDispatch(cur, cmd);
1112 bool MathHullInset::getStatus(LCursor & cur, FuncRequest const & cmd,
1113 FuncStatus & status) const
1115 switch (cmd.action) {
1116 // These are only enabled inside tabular
1117 case LFUN_BREAKLINE:
1118 case LFUN_MATH_NUMBER:
1119 case LFUN_MATH_NONUMBER:
1120 case LFUN_MATH_EXTERN:
1121 case LFUN_MATH_MUTATE:
1122 case LFUN_MATH_DISPLAY:
1124 status.enabled(true);
1126 case LFUN_INSERT_LABEL:
1127 status.enabled(type_ != "simple");
1129 case LFUN_TABULAR_FEATURE: {
1130 istringstream is(cmd.argument);
1134 && (s == "append-row"
1135 || s == "delete-row"
1136 || s == "copy-row")) {
1137 status.message(bformat(
1138 N_("Can't change number of rows in '%1$s'"),
1140 status.enabled(false);
1144 && (s == "append-column"
1145 || s == "delete-column"
1146 || s == "copy-column")) {
1147 status.message(bformat(
1148 N_("Can't change number of columns in '%1$s'"),
1150 status.enabled(false);
1153 if ((type_ == "simple"
1154 || type_ == "equation"
1155 || type_ == "none") &&
1156 (s == "add-hline-above" || s == "add-hline-below")) {
1157 status.message(bformat(
1158 N_("Can't add horizontal grid lines in '%1$s'"),
1160 status.enabled(false);
1163 if (s == "add-vline-left" || s == "add-vline-right") {
1164 status.message(bformat(
1165 N_("Can't add vertical grid lines in '%1$s'"),
1167 status.enabled(false);
1170 if (s == "valign-top" || s == "valign-middle"
1171 || s == "valign-bottom" || s == "align-left"
1172 || s == "align-center" || s == "align-right") {
1173 status.enabled(false);
1176 return MathGridInset::getStatus(cur, cmd, status);
1179 return MathGridInset::getStatus(cur, cmd, status);
1184 /////////////////////////////////////////////////////////////////////
1186 #include "math_arrayinset.h"
1187 #include "math_deliminset.h"
1188 #include "math_factory.h"
1189 #include "math_parser.h"
1190 #include "math_spaceinset.h"
1191 #include "ref_inset.h"
1193 #include "bufferview_funcs.h"
1194 #include "lyxtext.h"
1196 #include "frontends/LyXView.h"
1197 #include "frontends/Dialogs.h"
1199 #include "support/lyxlib.h"
1202 // simply scrap this function if you want
1203 void MathHullInset::mutateToText()
1206 // translate to latex
1208 latex(NULL, os, false, false);
1209 string str = os.str();
1212 LyXText * lt = view_->getLyXText();
1213 string::const_iterator cit = str.begin();
1214 string::const_iterator end = str.end();
1215 for (; cit != end; ++cit)
1216 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
1219 //view_->owner()->dispatch(LFUN_ESCAPE);
1224 void MathHullInset::handleFont(LCursor & cur, string const & arg,
1225 string const & font)
1227 // this whole function is a hack and won't work for incremental font
1230 if (cur.inset().asMathInset()->name() == font)
1231 cur.handleFont(font);
1233 cur.handleNest(createMathInset(font));
1239 void MathHullInset::handleFont2(LCursor & cur, string const & arg)
1244 bv_funcs::string2font(arg, font, b);
1245 if (font.color() != LColor::inherit) {
1246 MathAtom at = MathAtom(new MathColorInset(true, font.color()));
1247 cur.handleNest(at, 0);
1252 void MathHullInset::edit(LCursor & cur, bool left)
1255 left ? idxFirst(cur) : idxLast(cur);
1259 string const MathHullInset::editMessage() const
1261 return _("Math editor mode");
1265 void MathHullInset::revealCodes(LCursor & cur) const
1267 if (!cur.inMathed())
1271 cur.message(os.str());
1273 // write something to the minibuffer
1274 // translate to latex
1278 string str = os.str();
1280 string::size_type pos = 0;
1282 for (string::iterator it = str.begin(); it != str.end(); ++it) {
1285 else if (*it == '\0') {
1287 pos = it - str.begin();
1293 res = res.substr(pos - 30);
1294 if (res.size() > 60)
1295 res = res.substr(0, 60);
1301 InsetBase::Code MathHullInset::lyxCode() const
1307 /////////////////////////////////////////////////////////////////////
1311 bool MathHullInset::searchForward(BufferView * bv, string const & str,
1314 #ifdef WITH_WARNINGS
1315 #warning completely broken
1317 static MathHullInset * lastformula = 0;
1318 static CursorBase current = DocIterator(ibegin(nucleus()));
1319 static MathArray ar;
1320 static string laststr;
1322 if (lastformula != this || laststr != str) {
1323 //lyxerr << "reset lastformula to " << this << endl;
1326 current = ibegin(nucleus());
1328 mathed_parse_cell(ar, str);
1332 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
1334 for (DocIterator it = current; it != iend(nucleus()); increment(it)) {
1335 CursorSlice & top = it.back();
1336 MathArray const & a = top.asMathInset()->cell(top.idx_);
1337 if (a.matchpart(ar, top.pos_)) {
1338 bv->cursor().setSelection(it, ar.size());
1340 top.pos_ += ar.size();
1346 //lyxerr << "not found!" << endl;
1353 void MathHullInset::write(Buffer const &, std::ostream & os) const
1355 WriteStream wi(os, false, false);
1361 void MathHullInset::read(Buffer const &, LyXLex & lex)
1364 mathed_parse_normal(at, lex);
1365 operator=(*at->asHullInset());
1369 int MathHullInset::plaintext(Buffer const &, ostream & os,
1370 OutputParams const &) const
1372 if (0 && display()) {
1376 TextPainter tpain(dim.width(), dim.height());
1377 drawT(tpain, 0, dim.ascent());
1379 // reset metrics cache to "real" values
1381 return tpain.textheight();
1383 WriteStream wi(os, false, true);
1390 int MathHullInset::linuxdoc(Buffer const & buf, ostream & os,
1391 OutputParams const & runparams) const
1393 return docbook(buf, os, runparams);
1397 int MathHullInset::docbook(Buffer const & buf, ostream & os,
1398 OutputParams const & runparams) const
1400 MathMLStream ms(os);
1403 if (getType() == "simple")
1404 name= "inlineequation";
1406 name = "informalequation";
1408 string bname = name;
1409 if (!label(0).empty())
1410 bname += " id=\"" + sgml::cleanID(buf, runparams, label(0)) + "\"";
1411 ms << MTag(bname.c_str());
1414 if (runparams.flavor == OutputParams::XML) {
1415 ms << MTag("alt role=\"tex\" ");
1416 // Workaround for db2latex: db2latex always includes equations with
1417 // \ensuremath{} or \begin{display}\end{display}
1418 // so we strip LyX' math environment
1419 WriteStream wi(ls, false, false);
1420 MathGridInset::write(wi);
1421 ms << subst(subst(ls.str(), "&", "&"), "<", "<");
1424 MathGridInset::mathmlize(ms);
1427 ms << MTag("alt role=\"tex\"");
1428 res = latex(buf, ls, runparams);
1429 ms << subst(subst(ls.str(), "&", "&"), "<", "<");
1433 ms << "<graphic fileref=\"eqn/";
1434 if ( !label(0).empty())
1435 ms << sgml::cleanID(buf, runparams, label(0));
1437 ms << sgml::uniqueID("anon");
1439 if (runparams.flavor == OutputParams::XML)
1444 ms << ETag(name.c_str());
1445 return ms.line() + res;