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"
43 #include "frontends/Alert.h"
45 #include "graphics/PreviewImage.h"
46 #include "graphics/PreviewLoader.h"
48 #include "support/lstrings.h"
50 #include <boost/bind.hpp>
54 using lyx::cap::grabAndEraseSelection;
55 using lyx::support::bformat;
56 using lyx::support::subst;
63 using std::istringstream;
65 using std::ostringstream;
73 int getCols(string const & type)
75 if (type == "eqnarray")
79 if (type == "flalign")
81 if (type == "alignat")
83 if (type == "xalignat")
85 if (type == "xxalignat")
91 // returns position of first relation operator in the array
92 // used for "intelligent splitting"
93 size_t firstRelOp(MathArray const & ar)
95 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
97 return it - ar.begin();
102 char const * star(bool numbered)
104 return numbered ? "" : "*";
108 int typecode(string const & s)
110 if (s == "none") return 0;
111 if (s == "simple") return 1;
112 if (s == "equation") return 2;
113 if (s == "eqnarray") return 3;
114 if (s == "align") return 4;
115 if (s == "alignat") return 5;
116 if (s == "xalignat") return 6;
117 if (s == "xxalignat") return 7;
118 if (s == "multline") return 8;
119 if (s == "gather") return 9;
120 if (s == "flalign") return 10;
121 lyxerr << "unknown hull type '" << s << "'" << endl;
125 bool smaller(string const & s, string const & t)
127 return typecode(s) < typecode(t);
131 } // end anon namespace
135 MathHullInset::MathHullInset()
136 : MathGridInset(1, 1), type_("none"), nonum_(1), label_(1),
137 preview_(new RenderPreview(this))
139 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << endl;
140 //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl;
141 //lyxerr << "sizeof MathCharInset: " << sizeof(MathCharInset) << endl;
142 //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << endl;
147 MathHullInset::MathHullInset(string const & type)
148 : MathGridInset(getCols(type), 1), type_(type), nonum_(1), label_(1),
149 preview_(new RenderPreview(this))
155 MathHullInset::MathHullInset(MathHullInset const & other)
156 : MathGridInset(other),
157 type_(other.type_), nonum_(other.nonum_), label_(other.label_),
158 preview_(new RenderPreview(this))
162 MathHullInset::~MathHullInset()
166 auto_ptr<InsetBase> MathHullInset::doClone() const
168 return auto_ptr<InsetBase>(new MathHullInset(*this));
172 MathHullInset & MathHullInset::operator=(MathHullInset const & other)
176 *static_cast<MathGridInset*>(this) = MathGridInset(other);
178 nonum_ = other.nonum_;
179 label_ = other.label_;
180 preview_.reset(new RenderPreview(*other.preview_, this));
186 InsetBase * MathHullInset::editXY(LCursor & cur, int x, int y)
188 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
192 return MathNestInset::editXY(cur, x, y);
196 MathInset::mode_type MathHullInset::currentMode() const
199 return UNDECIDED_MODE;
200 // definitely math mode ...
205 bool MathHullInset::idxFirst(LCursor & cur) const
213 bool MathHullInset::idxLast(LCursor & cur) const
215 cur.idx() = nargs() - 1;
216 cur.pos() = cur.lastpos();
221 char MathHullInset::defaultColAlign(col_type col)
223 if (type_ == "eqnarray")
225 if (typecode(type_) >= typecode("align"))
226 return "rl"[col & 1];
231 int MathHullInset::defaultColSpace(col_type col)
233 if (type_ == "align" || type_ == "alignat")
235 if (type_ == "xalignat")
236 return (col & 1) ? 20 : 0;
237 if (type_ == "xxalignat" || type_ == "flalign")
238 return (col & 1) ? 40 : 0;
243 char const * MathHullInset::standardFont() const
246 return "lyxnochange";
251 void MathHullInset::metrics(MetricsInfo & mi, Dimension & dim) const
253 BOOST_ASSERT(mi.base.bv && mi.base.bv->buffer());
255 bool use_preview = false;
256 if (!editing(mi.base.bv) &&
257 RenderPreview::status() == LyXRC::PREVIEW_ON) {
258 lyx::graphics::PreviewImage const * pimage =
259 preview_->getPreviewImage(*mi.base.bv->buffer());
260 use_preview = pimage && pimage->image();
264 preview_->metrics(mi, dim);
265 // insert a one pixel gap in front of the formula
273 FontSetChanger dummy1(mi.base, standardFont());
274 StyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
276 // let the cells adjust themselves
277 MathGridInset::metrics(mi, dim);
284 if (numberedType()) {
285 FontSetChanger dummy(mi.base, "mathbf");
287 for (row_type row = 0; row < nrows(); ++row)
288 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
294 // make it at least as high as the current font
297 math_font_max_dim(mi.base.font, asc, des);
298 dim.asc = max(dim.asc, asc);
299 dim.des = max(dim.des, des);
305 void MathHullInset::draw(PainterInfo & pi, int x, int y) const
307 BOOST_ASSERT(pi.base.bv && pi.base.bv->buffer());
309 bool use_preview = false;
310 if (!editing(pi.base.bv) &&
311 RenderPreview::status() == LyXRC::PREVIEW_ON) {
312 lyx::graphics::PreviewImage const * pimage =
313 preview_->getPreviewImage(*pi.base.bv->buffer());
314 use_preview = pimage && pimage->image();
318 // one pixel gap in front
319 preview_->draw(pi, x + 1, y);
320 setPosCache(pi, x, y);
324 FontSetChanger dummy1(pi.base, standardFont());
325 StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
326 MathGridInset::draw(pi, x + 1, y);
328 if (numberedType()) {
329 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
330 for (row_type row = 0; row < nrows(); ++row) {
331 int const yy = y + rowinfo_[row].offset_;
332 FontSetChanger dummy(pi.base, "mathrm");
333 drawStr(pi, pi.base.font, xx, yy, nicelabel(row));
336 setPosCache(pi, x, y);
340 void MathHullInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
343 MathGridInset::metricsT(mi, dim);
346 WriteStream wi(os, false, true);
348 dim.wid = os.str().size();
355 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
358 MathGridInset::drawT(pain, x, y);
361 WriteStream wi(os, false, true);
363 pain.draw(x, y, os.str().c_str());
370 string const latex_string(MathHullInset const & inset)
373 WriteStream wi(ls, false, false);
381 void MathHullInset::addPreview(lyx::graphics::PreviewLoader & ploader) const
383 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
384 string const snippet = latex_string(*this);
385 preview_->addPreview(snippet, ploader);
390 void MathHullInset::notifyCursorLeaves(LCursor & cur)
392 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
393 Buffer const & buffer = cur.buffer();
394 string const snippet = latex_string(*this);
395 preview_->addPreview(snippet, buffer);
396 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 nonum_.erase(nonum_.begin() + row);
599 label_.erase(label_.begin() + row);
603 void MathHullInset::addCol(col_type col)
607 MathGridInset::addCol(col);
611 void MathHullInset::delCol(col_type col)
613 if (ncols() <= 1 || !colChangeOK())
615 MathGridInset::delCol(col);
619 string MathHullInset::nicelabel(row_type row) const
623 if (label_[row].empty())
624 return string("(#)");
625 return '(' + label_[row] + ')';
629 void MathHullInset::glueall()
632 for (idx_type i = 0; i < nargs(); ++i)
634 *this = MathHullInset("simple");
640 void MathHullInset::splitTo2Cols()
642 BOOST_ASSERT(ncols() == 1);
643 MathGridInset::addCol(1);
644 for (row_type row = 0; row < nrows(); ++row) {
645 idx_type const i = 2 * row;
646 pos_type pos = firstRelOp(cell(i));
647 cell(i + 1) = MathArray(cell(i).begin() + pos, cell(i).end());
648 cell(i).erase(pos, cell(i).size());
653 void MathHullInset::splitTo3Cols()
655 BOOST_ASSERT(ncols() < 3);
658 MathGridInset::addCol(1);
659 for (row_type row = 0; row < nrows(); ++row) {
660 idx_type const i = 3 * row + 1;
661 if (cell(i).size()) {
662 cell(i + 1) = MathArray(cell(i).begin() + 1, cell(i).end());
663 cell(i).erase(1, cell(i).size());
669 void MathHullInset::changeCols(col_type cols)
673 else if (ncols() < cols) {
679 while (ncols() < cols)
680 MathGridInset::addCol(ncols() - 1);
686 for (row_type row = 0; row < nrows(); ++row) {
687 idx_type const i = row * ncols();
688 for (col_type col = cols; col < ncols(); ++col) {
689 cell(i + cols - 1).append(cell(i + col));
693 while (ncols() > cols) {
694 MathGridInset::delCol(ncols() - 1);
699 string const & MathHullInset::getType() const
705 void MathHullInset::setType(string const & type)
713 void MathHullInset::mutate(string const & newtype)
715 //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
717 // we try to move along the chain
718 // none <-> simple <-> equation <-> eqnarray -> *align* -> multline, gather -+
720 // +-------------------------------------+
721 // we use eqnarray as intermediate type for mutations that are not
722 // directly supported because it handles labels and numbering for
725 if (newtype == "dump") {
729 else if (newtype == type_) {
733 else if (typecode(newtype) < 0) {
737 else if (type_ == "none") {
743 else if (type_ == "simple") {
744 if (newtype == "none") {
754 else if (type_ == "equation") {
755 if (smaller(newtype, type_)) {
759 } else if (newtype == "eqnarray") {
760 // split it "nicely" on the first relop
763 } else if (newtype == "multline" || newtype == "gather") {
773 else if (type_ == "eqnarray") {
774 if (smaller(newtype, type_)) {
775 // set correct (no)numbering
776 bool allnonum = true;
777 for (row_type row = 0; row < nrows(); ++row)
781 // set first non-empty label
783 for (row_type row = 0; row < nrows(); ++row) {
784 if (!label_[row].empty()) {
791 nonum_[0] = allnonum;
794 } else { // align & Co.
801 else if (type_ == "align" || type_ == "alignat" ||
802 type_ == "xalignat" || type_ == "flalign") {
803 if (smaller(newtype, "align")) {
807 } else if (newtype == "gather" || newtype == "multline") {
810 } else if (newtype == "xxalignat") {
811 for (row_type row = 0; row < nrows(); ++row)
812 numbered(row, false);
819 else if (type_ == "xxalignat") {
820 for (row_type row = 0; row < nrows(); ++row)
821 numbered(row, false);
822 if (smaller(newtype, "align")) {
826 } else if (newtype == "gather" || newtype == "multline") {
834 else if (type_ == "multline" || type_ == "gather") {
835 if (newtype == "gather" || newtype == "multline")
837 else if (newtype == "align" || newtype == "flalign" ||
838 newtype == "alignat" || newtype == "xalignat") {
841 } else if (newtype == "xxalignat") {
843 for (row_type row = 0; row < nrows(); ++row)
844 numbered(row, false);
854 lyxerr << "mutation from '" << type_
855 << "' to '" << newtype << "' not implemented" << endl;
860 string MathHullInset::eolString(row_type row, bool fragile) const
863 if (numberedType()) {
864 if (!label_[row].empty() && !nonum_[row])
865 res += "\\label{" + label_[row] + '}';
866 if (nonum_[row] && (type_ != "multline"))
867 res += "\\nonumber ";
869 return res + MathGridInset::eolString(row, fragile);
873 void MathHullInset::write(WriteStream & os) const
876 MathGridInset::write(os);
881 void MathHullInset::normalize(NormalStream & os) const
883 os << "[formula " << type_ << ' ';
884 MathGridInset::normalize(os);
889 void MathHullInset::mathmlize(MathMLStream & os) const
891 MathGridInset::mathmlize(os);
895 void MathHullInset::infoize(ostream & os) const
897 os << "Type: " << type_;
901 void MathHullInset::check() const
903 BOOST_ASSERT(nonum_.size() == nrows());
904 BOOST_ASSERT(label_.size() == nrows());
908 void MathHullInset::doExtern(LCursor & cur, FuncRequest & func)
912 istringstream iss(func.argument);
913 iss >> lang >> extra;
918 #warning temporarily disabled
919 //if (cur.selection()) {
922 // lyxerr << "use selection: " << ar << endl;
923 // insert(pipeThroughExtern(lang, extra, ar));
929 eq.push_back(MathAtom(new MathCharInset('=')));
931 // go to first item in line
932 cur.idx() -= cur.idx() % ncols();
935 if (getType() == "simple") {
936 size_type pos = cur.cell().find_last(eq);
938 if (cur.inMathed() && cur.selection()) {
939 asArray(grabAndEraseSelection(cur), ar);
940 } else if (pos == cur.cell().size()) {
942 lyxerr << "use whole cell: " << ar << endl;
944 ar = MathArray(cur.cell().begin() + pos + 1, cur.cell().end());
945 lyxerr << "use partial cell form pos: " << pos << endl;
947 cur.cell().append(eq);
948 cur.cell().append(pipeThroughExtern(lang, extra, ar));
949 cur.pos() = cur.lastpos();
953 if (getType() == "equation") {
954 lyxerr << "use equation inset" << endl;
956 MathArray & ar = cur.cell();
957 lyxerr << "use cell: " << ar << endl;
961 cur.cell() = pipeThroughExtern(lang, extra, ar);
962 // move to end of line
963 cur.pos() = cur.lastpos();
968 lyxerr << "use eqnarray" << endl;
969 cur.idx() += 2 - cur.idx() % ncols();
971 MathArray ar = cur.cell();
972 lyxerr << "use cell: " << ar << endl;
974 #warning temporarily disabled
981 cur.cell() = pipeThroughExtern(lang, extra, ar);
982 cur.pos() = cur.lastpos();
987 void MathHullInset::doDispatch(LCursor & cur, FuncRequest & cmd)
989 switch (cmd.action) {
991 case LFUN_FINISHED_LEFT:
992 case LFUN_FINISHED_RIGHT:
993 case LFUN_FINISHED_UP:
994 case LFUN_FINISHED_DOWN:
995 MathGridInset::doDispatch(cur, cmd);
996 notifyCursorLeaves(cur);
999 case LFUN_BREAKPARAGRAPH:
1000 // just swallow this
1003 case LFUN_BREAKLINE:
1004 // some magic for the common case
1005 if (type_ == "simple" || type_ == "equation") {
1006 recordUndoInset(cur);
1008 cur.bv().buffer()->params().use_amsmath == BufferParams::AMS_ON;
1009 mutate(align ? "align" : "eqnarray");
1011 cur.pos() = cur.lastpos();
1013 MathGridInset::doDispatch(cur, cmd);
1016 case LFUN_MATH_NUMBER:
1017 //lyxerr << "toggling all numbers" << endl;
1019 recordUndoInset(cur);
1020 bool old = numberedType();
1021 if (type_ == "multline")
1022 numbered(nrows() - 1, !old);
1024 for (row_type row = 0; row < nrows(); ++row)
1025 numbered(row, !old);
1026 cur.message(old ? _("No number") : _("Number"));
1030 case LFUN_MATH_NONUMBER:
1032 recordUndoInset(cur);
1033 row_type r = (type_ == "multline") ? nrows() - 1 : cur.row();
1034 bool old = numbered(r);
1035 cur.message(old ? _("No number") : _("Number"));
1040 case LFUN_INSERT_LABEL: {
1041 recordUndoInset(cur);
1042 row_type r = (type_ == "multline") ? nrows() - 1 : cur.row();
1043 string old_label = label(r);
1044 string new_label = cmd.argument;
1046 if (new_label.empty()) {
1047 string const default_label =
1048 (lyxrc.label_init_length >= 0) ? "eq:" : "";
1049 pair<bool, string> const res = old_label.empty()
1050 ? Alert::askForText(_("Enter new label to insert:"), default_label)
1051 : Alert::askForText(_("Enter label:"), old_label);
1053 new_label = lyx::support::trim(res.second);
1055 new_label = old_label;
1058 if (!new_label.empty())
1060 label(r, new_label);
1064 case LFUN_MATH_EXTERN:
1065 recordUndoInset(cur);
1069 case LFUN_MATH_MUTATE: {
1070 recordUndoInset(cur);
1071 row_type row = cur.row();
1072 col_type col = cur.col();
1073 mutate(cmd.argument);
1074 cur.idx() = row * ncols() + col;
1075 if (cur.idx() > cur.lastidx()) {
1076 cur.idx() = cur.lastidx();
1077 cur.pos() = cur.lastpos();
1079 if (cur.pos() > cur.lastpos())
1080 cur.pos() = cur.lastpos();
1081 //cur.dispatched(FINISHED);
1085 case LFUN_MATH_DISPLAY: {
1086 recordUndoInset(cur);
1087 mutate(type_ == "simple" ? "equation" : "simple");
1089 cur.pos() = cur.lastpos();
1090 //cur.dispatched(FINISHED);
1095 MathGridInset::doDispatch(cur, cmd);
1101 bool MathHullInset::getStatus(LCursor & cur, FuncRequest const & cmd,
1102 FuncStatus & flag) const
1104 switch (cmd.action) {
1105 case LFUN_BREAKLINE:
1106 case LFUN_MATH_NUMBER:
1107 case LFUN_MATH_NONUMBER:
1108 case LFUN_INSERT_LABEL:
1109 case LFUN_MATH_EXTERN:
1110 case LFUN_MATH_MUTATE:
1111 case LFUN_MATH_DISPLAY:
1115 case LFUN_TABULAR_FEATURE: {
1116 istringstream is(cmd.argument);
1120 && (s == "append-row"
1121 || s == "delete-row"
1122 || s == "copy-row")) {
1123 flag.message(bformat(
1124 N_("Can't change number of rows in '%1$s'"),
1126 flag.enabled(false);
1130 && (s == "append-column"
1131 || s == "delete-column"
1132 || s == "copy-column")) {
1133 flag.message(bformat(
1134 N_("Can't change number of columns in '%1$s'"),
1136 flag.enabled(false);
1139 if ((type_ == "simple"
1140 || type_ == "equation"
1141 || type_ == "none") &&
1142 (s == "add-hline-above" || s == "add-hline-below")) {
1143 flag.message(bformat(
1144 N_("Can't add horizontal grid lines in '%1$s'"),
1146 flag.enabled(false);
1149 if (s == "add-vline-left" || s == "add-vline-right") {
1150 flag.message(bformat(
1151 N_("Can't add vertical grid lines in '%1$s'"),
1153 flag.enabled(false);
1156 if (s == "valign-top" || s == "valign-middle"
1157 || s == "valign-bottom" || s == "align-left"
1158 || s == "align-center" || s == "align-right") {
1159 flag.enabled(false);
1162 return MathGridInset::getStatus(cur, cmd, flag);
1165 return MathGridInset::getStatus(cur, cmd, flag);
1170 /////////////////////////////////////////////////////////////////////
1172 #include "math_arrayinset.h"
1173 #include "math_deliminset.h"
1174 #include "math_factory.h"
1175 #include "math_parser.h"
1176 #include "math_spaceinset.h"
1177 #include "ref_inset.h"
1179 #include "bufferview_funcs.h"
1180 #include "lyxtext.h"
1182 #include "frontends/LyXView.h"
1183 #include "frontends/Dialogs.h"
1185 #include "support/lyxlib.h"
1188 // simply scrap this function if you want
1189 void MathHullInset::mutateToText()
1192 // translate to latex
1194 latex(NULL, os, false, false);
1195 string str = os.str();
1198 LyXText * lt = view_->getLyXText();
1199 string::const_iterator cit = str.begin();
1200 string::const_iterator end = str.end();
1201 for (; cit != end; ++cit)
1202 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
1205 //view_->owner()->dispatch(LFUN_ESCAPE);
1210 void MathHullInset::handleFont(LCursor & cur, string const & arg,
1211 string const & font)
1213 // this whole function is a hack and won't work for incremental font
1216 if (cur.inset().asMathInset()->name() == font)
1217 cur.handleFont(font);
1219 cur.handleNest(createMathInset(font));
1225 void MathHullInset::handleFont2(LCursor & cur, string const & arg)
1230 bv_funcs::string2font(arg, font, b);
1231 if (font.color() != LColor::inherit) {
1232 MathAtom at = MathAtom(new MathColorInset(true, font.color()));
1233 cur.handleNest(at, 0);
1238 void MathHullInset::edit(LCursor & cur, bool left)
1241 left ? idxFirst(cur) : idxLast(cur);
1245 string const MathHullInset::editMessage() const
1247 return _("Math editor mode");
1251 void MathHullInset::getCursorDim(int & asc, int & desc) const
1255 //math_font_max_dim(font_, asc, des);
1259 void MathHullInset::revealCodes(LCursor & cur) const
1261 if (!cur.inMathed())
1265 cur.message(os.str());
1267 // write something to the minibuffer
1268 // translate to latex
1272 string str = os.str();
1274 string::size_type pos = 0;
1276 for (string::iterator it = str.begin(); it != str.end(); ++it) {
1279 else if (*it == '\0') {
1281 pos = it - str.begin();
1287 res = res.substr(pos - 30);
1288 if (res.size() > 60)
1289 res = res.substr(0, 60);
1295 InsetBase::Code MathHullInset::lyxCode() const
1301 /////////////////////////////////////////////////////////////////////
1305 bool MathHullInset::searchForward(BufferView * bv, string const & str,
1308 #ifdef WITH_WARNINGS
1309 #warning completely broken
1311 static MathHullInset * lastformula = 0;
1312 static CursorBase current = DocIterator(ibegin(nucleus()));
1313 static MathArray ar;
1314 static string laststr;
1316 if (lastformula != this || laststr != str) {
1317 //lyxerr << "reset lastformula to " << this << endl;
1320 current = ibegin(nucleus());
1322 mathed_parse_cell(ar, str);
1326 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
1328 for (DocIterator it = current; it != iend(nucleus()); increment(it)) {
1329 CursorSlice & top = it.back();
1330 MathArray const & a = top.asMathInset()->cell(top.idx_);
1331 if (a.matchpart(ar, top.pos_)) {
1332 bv->cursor().setSelection(it, ar.size());
1334 top.pos_ += ar.size();
1340 //lyxerr << "not found!" << endl;
1347 void MathHullInset::write(Buffer const &, std::ostream & os) const
1349 WriteStream wi(os, false, false);
1355 void MathHullInset::read(Buffer const &, LyXLex & lex)
1358 mathed_parse_normal(at, lex);
1359 operator=(*at->asHullInset());
1363 int MathHullInset::plaintext(Buffer const &, ostream & os,
1364 OutputParams const &) const
1366 if (0 && display()) {
1370 TextPainter tpain(dim.width(), dim.height());
1371 drawT(tpain, 0, dim.ascent());
1373 // reset metrics cache to "real" values
1375 return tpain.textheight();
1377 WriteStream wi(os, false, true);
1384 int MathHullInset::linuxdoc(Buffer const & buf, ostream & os,
1385 OutputParams const & runparams) const
1387 return docbook(buf, os, runparams);
1391 int MathHullInset::docbook(Buffer const & buf, ostream & os,
1392 OutputParams const & runparams) const
1394 MathMLStream ms(os);
1397 if (getType() == "simple")
1398 name= "inlineequation";
1400 name = "informalequation";
1402 string bname = name;
1403 if (!label(0).empty())
1404 bname += " id=\"" + sgml::cleanID(buf, runparams, label(0)) + "\"";
1405 ms << MTag(bname.c_str());
1408 if (runparams.flavor == OutputParams::XML) {
1409 ms << MTag("alt role=\"tex\" ");
1410 // Workaround for db2latex: db2latex always includes equations with
1411 // \ensuremath{} or \begin{display}\end{display}
1412 // so we strip LyX' math environment
1413 WriteStream wi(ls, false, false);
1414 MathGridInset::write(wi);
1415 ms << subst(subst(ls.str(), "&", "&"), "<", "<");
1418 MathGridInset::mathmlize(ms);
1421 ms << MTag("alt role=\"tex\"");
1422 res = latex(buf, ls, runparams);
1423 ms << subst(subst(ls.str(), "&", "&"), "<", "<");
1427 ms << "<graphic fileref=\"eqn/";
1428 if ( !label(0).empty())
1429 ms << sgml::cleanID(buf, runparams, label(0));
1431 ms << sgml::uniqueID("anon");
1433 if (runparams.flavor == OutputParams::XML)
1438 ms << ETag(name.c_str());
1439 return ms.line() + res;