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_data.h"
15 #include "math_extern.h"
16 #include "math_hullinset.h"
17 #include "math_mathmlstream.h"
18 #include "math_streamstr.h"
19 #include "math_support.h"
21 #include "BufferView.h"
22 #include "CutAndPaste.h"
24 #include "LaTeXFeatures.h"
27 #include "dispatchresult.h"
28 #include "funcrequest.h"
32 #include "outputparams.h"
34 #include "textpainter.h"
37 #include "insets/render_preview.h"
39 #include "frontends/Alert.h"
41 #include "graphics/PreviewImage.h"
42 #include "graphics/PreviewLoader.h"
44 #include "support/lstrings.h"
46 #include <boost/bind.hpp>
50 using lyx::cap::grabAndEraseSelection;
51 using lyx::support::subst;
58 using std::istringstream;
60 using std::ostringstream;
68 int getCols(string const & type)
70 if (type == "eqnarray")
74 if (type == "flalign")
76 if (type == "alignat")
78 if (type == "xalignat")
80 if (type == "xxalignat")
86 // returns position of first relation operator in the array
87 // used for "intelligent splitting"
88 size_t firstRelOp(MathArray const & ar)
90 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
92 return it - ar.begin();
97 char const * star(bool numbered)
99 return numbered ? "" : "*";
103 int typecode(string const & s)
105 if (s == "none") return 0;
106 if (s == "simple") return 1;
107 if (s == "equation") return 2;
108 if (s == "eqnarray") return 3;
109 if (s == "align") return 4;
110 if (s == "alignat") return 5;
111 if (s == "xalignat") return 6;
112 if (s == "xxalignat") return 7;
113 if (s == "multline") return 8;
114 if (s == "gather") return 9;
115 if (s == "flalign") return 10;
116 lyxerr << "unknown hull type '" << s << "'" << endl;
120 bool smaller(string const & s, string const & t)
122 return typecode(s) < typecode(t);
126 } // end anon namespace
130 MathHullInset::MathHullInset()
131 : MathGridInset(1, 1), type_("none"), nonum_(1), label_(1),
132 preview_(new RenderPreview(this))
134 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << endl;
135 //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl;
136 //lyxerr << "sizeof MathCharInset: " << sizeof(MathCharInset) << endl;
137 //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << endl;
142 MathHullInset::MathHullInset(string const & type)
143 : MathGridInset(getCols(type), 1), type_(type), nonum_(1), label_(1),
144 preview_(new RenderPreview(this))
150 MathHullInset::MathHullInset(MathHullInset const & other)
151 : MathGridInset(other),
152 type_(other.type_), nonum_(other.nonum_), label_(other.label_),
153 preview_(new RenderPreview(this))
157 MathHullInset::~MathHullInset()
161 auto_ptr<InsetBase> MathHullInset::clone() const
163 return auto_ptr<InsetBase>(new MathHullInset(*this));
167 void MathHullInset::operator=(MathHullInset const & other)
171 *static_cast<MathGridInset*>(this) = MathGridInset(other);
173 nonum_ = other.nonum_;
174 label_ = other.label_;
175 preview_.reset(new RenderPreview(*other.preview_, this));
179 MathInset::mode_type MathHullInset::currentMode() const
182 return UNDECIDED_MODE;
183 // definitely math mode ...
188 bool MathHullInset::idxFirst(LCursor & cur) const
196 bool MathHullInset::idxLast(LCursor & cur) const
198 cur.idx() = nargs() - 1;
199 cur.pos() = cur.lastpos();
204 char MathHullInset::defaultColAlign(col_type col)
206 if (type_ == "eqnarray")
208 if (typecode(type_) >= typecode("align"))
209 return "rl"[col & 1];
214 int MathHullInset::defaultColSpace(col_type col)
216 if (type_ == "align" || type_ == "alignat")
218 if (type_ == "xalignat")
219 return (col & 1) ? 20 : 0;
220 if (type_ == "xxalignat" || type_ == "flalign")
221 return (col & 1) ? 40 : 0;
226 char const * MathHullInset::standardFont() const
229 return "lyxnochange";
234 void MathHullInset::metrics(MetricsInfo & mi, Dimension & dim) const
236 BOOST_ASSERT(mi.base.bv && mi.base.bv->buffer());
238 bool use_preview = false;
239 if (!editing(mi.base.bv) &&
240 RenderPreview::status() == LyXRC::PREVIEW_ON) {
241 lyx::graphics::PreviewImage const * pimage =
242 preview_->getPreviewImage(*mi.base.bv->buffer());
243 use_preview = pimage && pimage->image();
247 preview_->metrics(mi, dim);
248 // insert a one pixel gap in front of the formula
256 FontSetChanger dummy1(mi.base, standardFont());
257 StyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
259 // let the cells adjust themselves
260 MathGridInset::metrics(mi, dim);
267 if (numberedType()) {
268 FontSetChanger dummy(mi.base, "mathbf");
270 for (row_type row = 0; row < nrows(); ++row)
271 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
277 // make it at least as high as the current font
280 math_font_max_dim(mi.base.font, asc, des);
281 dim.asc = max(dim.asc, asc);
282 dim.des = max(dim.des, des);
288 void MathHullInset::draw(PainterInfo & pi, int x, int y) const
290 BOOST_ASSERT(pi.base.bv && pi.base.bv->buffer());
292 bool use_preview = false;
293 if (!editing(pi.base.bv) &&
294 RenderPreview::status() == LyXRC::PREVIEW_ON) {
295 lyx::graphics::PreviewImage const * pimage =
296 preview_->getPreviewImage(*pi.base.bv->buffer());
297 use_preview = pimage && pimage->image();
301 // one pixel gap in front
302 preview_->draw(pi, x + 1, y);
303 setPosCache(pi, x, y);
307 FontSetChanger dummy1(pi.base, standardFont());
308 StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
309 MathGridInset::draw(pi, x + 1, y);
311 if (numberedType()) {
312 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
313 for (row_type row = 0; row < nrows(); ++row) {
314 int const yy = y + rowinfo_[row].offset_;
315 FontSetChanger dummy(pi.base, "mathrm");
316 drawStr(pi, pi.base.font, xx, yy, nicelabel(row));
319 setPosCache(pi, x, y);
323 void MathHullInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
326 MathGridInset::metricsT(mi, dim);
329 WriteStream wi(os, false, true);
331 dim.wid = os.str().size();
338 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
341 MathGridInset::drawT(pain, x, y);
344 WriteStream wi(os, false, true);
346 pain.draw(x, y, os.str().c_str());
353 string const latex_string(MathHullInset const & inset)
356 WriteStream wi(ls, false, false);
364 void MathHullInset::addPreview(lyx::graphics::PreviewLoader & ploader) const
366 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
367 string const snippet = latex_string(*this);
368 preview_->addPreview(snippet, ploader);
373 void MathHullInset::notifyCursorLeaves(LCursor & cur)
375 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
376 Buffer const & buffer = cur.buffer();
377 string const snippet = latex_string(*this);
378 preview_->addPreview(snippet, buffer);
379 preview_->startLoading(buffer);
384 string MathHullInset::label(row_type row) const
386 row_type n = nrows();
387 BOOST_ASSERT(row < n);
392 void MathHullInset::label(row_type row, string const & label)
394 //lyxerr << "setting label '" << label << "' for row " << row << endl;
399 void MathHullInset::numbered(row_type row, bool num)
405 bool MathHullInset::numbered(row_type row) const
411 bool MathHullInset::ams() const
415 type_ == "flalign" ||
416 type_ == "multline" ||
418 type_ == "alignat" ||
419 type_ == "xalignat" ||
420 type_ == "xxalignat";
424 bool MathHullInset::display() const
426 return type_ != "simple" && type_ != "none";
430 void MathHullInset::getLabelList(Buffer const &, vector<string> & labels) const
432 for (row_type row = 0; row < nrows(); ++row)
433 if (!label_[row].empty() && nonum_[row] != 1)
434 labels.push_back(label_[row]);
438 bool MathHullInset::numberedType() const
442 if (type_ == "simple")
444 if (type_ == "xxalignat")
446 for (row_type row = 0; row < nrows(); ++row)
453 void MathHullInset::validate(LaTeXFeatures & features) const
456 features.require("amsmath");
459 // Validation is necessary only if not using AMS math.
460 // To be safe, we will always run mathedvalidate.
461 //if (features.amsstyle)
464 features.require("boldsymbol");
465 //features.binom = true;
467 MathGridInset::validate(features);
471 void MathHullInset::header_write(WriteStream & os) const
473 bool n = numberedType();
478 else if (type_ == "simple") {
484 else if (type_ == "equation") {
486 os << "\\begin{equation" << star(n) << "}\n";
491 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
492 || type_ == "gather" || type_ == "multline")
493 os << "\\begin{" << type_ << star(n) << "}\n";
495 else if (type_ == "alignat" || type_ == "xalignat")
496 os << "\\begin{" << type_ << star(n) << '}'
497 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
499 else if (type_ == "xxalignat")
500 os << "\\begin{" << type_ << '}'
501 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
504 os << "\\begin{unknown" << star(n) << '}';
508 void MathHullInset::footer_write(WriteStream & os) const
510 bool n = numberedType();
515 else if (type_ == "simple")
518 else if (type_ == "equation")
520 os << "\\end{equation" << star(n) << "}\n";
524 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
525 || type_ == "alignat" || type_ == "xalignat"
526 || type_ == "gather" || type_ == "multline")
527 os << "\\end{" << type_ << star(n) << "}\n";
529 else if (type_ == "xxalignat")
530 os << "\\end{" << type_ << "}\n";
533 os << "\\end{unknown" << star(n) << '}';
537 bool MathHullInset::rowChangeOK() const
540 type_ == "eqnarray" || type_ == "align" ||
541 type_ == "flalign" || type_ == "alignat" ||
542 type_ == "xalignat" || type_ == "xxalignat" ||
543 type_ == "gather" || type_ == "multline";
547 bool MathHullInset::colChangeOK() const
550 type_ == "align" || type_ == "flalign" ||type_ == "alignat" ||
551 type_ == "xalignat" || type_ == "xxalignat";
555 void MathHullInset::addRow(row_type row)
559 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
560 label_.insert(label_.begin() + row + 1, string());
561 MathGridInset::addRow(row);
565 void MathHullInset::swapRow(row_type row)
569 if (row + 1 == nrows())
571 swap(nonum_[row], nonum_[row + 1]);
572 swap(label_[row], label_[row + 1]);
573 MathGridInset::swapRow(row);
577 void MathHullInset::delRow(row_type row)
579 if (nrows() <= 1 || !rowChangeOK())
581 MathGridInset::delRow(row);
582 nonum_.erase(nonum_.begin() + row);
583 label_.erase(label_.begin() + row);
587 void MathHullInset::addCol(col_type col)
591 MathGridInset::addCol(col);
595 void MathHullInset::delCol(col_type col)
597 if (ncols() <= 1 || !colChangeOK())
599 MathGridInset::delCol(col);
603 string MathHullInset::nicelabel(row_type row) const
607 if (label_[row].empty())
608 return string("(#)");
609 return '(' + label_[row] + ')';
613 void MathHullInset::glueall()
616 for (idx_type i = 0; i < nargs(); ++i)
618 *this = MathHullInset("simple");
624 void MathHullInset::splitTo2Cols()
626 BOOST_ASSERT(ncols() == 1);
627 MathGridInset::addCol(1);
628 for (row_type row = 0; row < nrows(); ++row) {
629 idx_type const i = 2 * row;
630 pos_type pos = firstRelOp(cell(i));
631 cell(i + 1) = MathArray(cell(i).begin() + pos, cell(i).end());
632 cell(i).erase(pos, cell(i).size());
637 void MathHullInset::splitTo3Cols()
639 BOOST_ASSERT(ncols() < 3);
642 MathGridInset::addCol(1);
643 for (row_type row = 0; row < nrows(); ++row) {
644 idx_type const i = 3 * row + 1;
645 if (cell(i).size()) {
646 cell(i + 1) = MathArray(cell(i).begin() + 1, cell(i).end());
647 cell(i).erase(1, cell(i).size());
653 void MathHullInset::changeCols(col_type cols)
657 else if (ncols() < cols) {
663 while (ncols() < cols)
664 MathGridInset::addCol(ncols() - 1);
670 for (row_type row = 0; row < nrows(); ++row) {
671 idx_type const i = row * ncols();
672 for (col_type col = cols; col < ncols(); ++col) {
673 cell(i + cols - 1).append(cell(i + col));
677 while (ncols() > cols) {
678 MathGridInset::delCol(ncols() - 1);
683 string const & MathHullInset::getType() const
689 void MathHullInset::setType(string const & type)
697 void MathHullInset::mutate(string const & newtype)
699 lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
701 // we try to move along the chain
702 // none <-> simple <-> equation <-> eqnarray -> *align* -> multline, gather -+
704 // +-------------------------------------+
705 // we use eqnarray as intermediate type for mutations that are not
706 // directly supported because it handles labels and numbering for
709 if (newtype == "dump") {
713 else if (newtype == type_) {
717 else if (typecode(newtype) < 0) {
721 else if (type_ == "none") {
727 else if (type_ == "simple") {
728 if (newtype == "none") {
738 else if (type_ == "equation") {
739 if (smaller(newtype, type_)) {
743 } else if (newtype == "eqnarray") {
744 // split it "nicely" on the first relop
747 } else if (newtype == "multline" || newtype == "gather") {
757 else if (type_ == "eqnarray") {
758 if (smaller(newtype, type_)) {
759 // set correct (no)numbering
760 bool allnonum = true;
761 for (row_type row = 0; row < nrows(); ++row)
765 // set first non-empty label
767 for (row_type row = 0; row < nrows(); ++row) {
768 if (!label_[row].empty()) {
775 nonum_[0] = allnonum;
778 } else { // align & Co.
785 else if (type_ == "align" || type_ == "alignat" ||
786 type_ == "xalignat" || type_ == "flalign") {
787 if (smaller(newtype, "align")) {
791 } else if (newtype == "gather" || newtype == "multline") {
794 } else if (newtype == "xxalignat") {
795 for (row_type row = 0; row < nrows(); ++row)
796 numbered(row, false);
803 else if (type_ == "xxalignat") {
804 for (row_type row = 0; row < nrows(); ++row)
805 numbered(row, false);
806 if (smaller(newtype, "align")) {
810 } else if (newtype == "gather" || newtype == "multline") {
818 else if (type_ == "multline" || type_ == "gather") {
819 if (newtype == "gather" || newtype == "multline")
821 else if (newtype == "align" || newtype == "flalign" ||
822 newtype == "alignat" || newtype == "xalignat") {
825 } else if (newtype == "xxalignat") {
827 for (row_type row = 0; row < nrows(); ++row)
828 numbered(row, false);
838 lyxerr << "mutation from '" << type_
839 << "' to '" << newtype << "' not implemented" << endl;
844 string MathHullInset::eolString(row_type row, bool fragile) const
847 if (numberedType()) {
848 if (!label_[row].empty() && !nonum_[row])
849 res += "\\label{" + label_[row] + '}';
850 if (nonum_[row] && (type_ != "multline"))
851 res += "\\nonumber ";
853 return res + MathGridInset::eolString(row, fragile);
857 void MathHullInset::write(WriteStream & os) const
860 MathGridInset::write(os);
865 void MathHullInset::normalize(NormalStream & os) const
867 os << "[formula " << type_ << ' ';
868 MathGridInset::normalize(os);
873 void MathHullInset::mathmlize(MathMLStream & os) const
875 MathGridInset::mathmlize(os);
879 void MathHullInset::infoize(ostream & os) const
881 os << "Type: " << type_;
885 void MathHullInset::check() const
887 BOOST_ASSERT(nonum_.size() == nrows());
888 BOOST_ASSERT(label_.size() == nrows());
892 void MathHullInset::doExtern(LCursor & cur, FuncRequest & func)
896 istringstream iss(func.argument);
897 iss >> lang >> extra;
902 #warning temporarily disabled
903 //if (cur.selection()) {
906 // lyxerr << "use selection: " << ar << endl;
907 // insert(pipeThroughExtern(lang, extra, ar));
913 eq.push_back(MathAtom(new MathCharInset('=')));
915 // go to first item in line
916 cur.idx() -= cur.idx() % ncols();
919 if (getType() == "simple") {
920 size_type pos = cur.cell().find_last(eq);
922 if (cur.inMathed() && cur.selection()) {
923 asArray(grabAndEraseSelection(cur), ar);
924 } else if (pos == cur.cell().size()) {
926 lyxerr << "use whole cell: " << ar << endl;
928 ar = MathArray(cur.cell().begin() + pos + 1, cur.cell().end());
929 lyxerr << "use partial cell form pos: " << pos << endl;
931 cur.cell().append(eq);
932 cur.cell().append(pipeThroughExtern(lang, extra, ar));
933 cur.pos() = cur.lastpos();
937 if (getType() == "equation") {
938 lyxerr << "use equation inset" << endl;
940 MathArray & ar = cur.cell();
941 lyxerr << "use cell: " << ar << endl;
945 cur.cell() = pipeThroughExtern(lang, extra, ar);
946 // move to end of line
947 cur.pos() = cur.lastpos();
952 lyxerr << "use eqnarray" << endl;
953 cur.idx() += 2 - cur.idx() % ncols();
955 MathArray ar = cur.cell();
956 lyxerr << "use cell: " << ar << endl;
958 #warning temporarily disabled
965 cur.cell() = pipeThroughExtern(lang, extra, ar);
966 cur.pos() = cur.lastpos();
971 void MathHullInset::priv_dispatch(LCursor & cur, FuncRequest & cmd)
973 switch (cmd.action) {
975 case LFUN_FINISHED_LEFT:
976 case LFUN_FINISHED_RIGHT:
977 case LFUN_FINISHED_UP:
978 case LFUN_FINISHED_DOWN:
979 MathGridInset::priv_dispatch(cur, cmd);
980 notifyCursorLeaves(cur);
983 case LFUN_BREAKPARAGRAPH:
988 // some magic for the common case
989 if (type_ == "simple" || type_ == "equation") {
990 recordUndoInset(cur);
993 cur.pos() = cur.lastpos();
995 MathGridInset::priv_dispatch(cur, cmd);
998 case LFUN_MATH_NUMBER:
999 //lyxerr << "toggling all numbers" << endl;
1001 recordUndoInset(cur);
1002 bool old = numberedType();
1003 if (type_ == "multline")
1004 numbered(nrows() - 1, !old);
1006 for (row_type row = 0; row < nrows(); ++row)
1007 numbered(row, !old);
1008 cur.message(old ? _("No number") : _("Number"));
1012 case LFUN_MATH_NONUMBER:
1014 recordUndoInset(cur);
1015 row_type r = (type_ == "multline") ? nrows() - 1 : cur.row();
1016 bool old = numbered(r);
1017 cur.message(old ? _("No number") : _("Number"));
1022 case LFUN_INSERT_LABEL: {
1023 recordUndoInset(cur);
1024 row_type r = (type_ == "multline") ? nrows() - 1 : cur.row();
1025 string old_label = label(r);
1026 string new_label = cmd.argument;
1028 if (new_label.empty()) {
1029 string const default_label =
1030 (lyxrc.label_init_length >= 0) ? "eq:" : "";
1031 pair<bool, string> const res = old_label.empty()
1032 ? Alert::askForText(_("Enter new label to insert:"), default_label)
1033 : Alert::askForText(_("Enter label:"), old_label);
1035 new_label = lyx::support::trim(res.second);
1037 new_label = old_label;
1040 if (!new_label.empty())
1042 label(r, new_label);
1046 case LFUN_MATH_EXTERN:
1047 recordUndoInset(cur);
1051 case LFUN_MATH_MUTATE: {
1052 recordUndoInset(cur);
1053 row_type row = cur.row();
1054 col_type col = cur.col();
1055 mutate(cmd.argument);
1056 cur.idx() = row * ncols() + col;
1057 if (cur.idx() > cur.lastidx()) {
1058 cur.idx() = cur.lastidx();
1059 cur.pos() = cur.lastpos();
1061 if (cur.pos() > cur.lastpos())
1062 cur.pos() = cur.lastpos();
1063 //cur.dispatched(FINISHED);
1067 case LFUN_MATH_DISPLAY: {
1068 recordUndoInset(cur);
1069 mutate(type_ == "simple" ? "equation" : "simple");
1071 cur.pos() = cur.lastpos();
1072 //cur.dispatched(FINISHED);
1077 MathGridInset::priv_dispatch(cur, cmd);
1083 bool MathHullInset::getStatus(LCursor & cur, FuncRequest const & cmd,
1084 FuncStatus & flag) const
1086 switch (cmd.action) {
1087 case LFUN_BREAKLINE:
1088 case LFUN_MATH_NUMBER:
1089 case LFUN_MATH_NONUMBER:
1090 case LFUN_INSERT_LABEL:
1091 case LFUN_MATH_EXTERN:
1092 case LFUN_MATH_MUTATE:
1093 case LFUN_MATH_DISPLAY:
1096 case LFUN_TABULAR_FEATURE: {
1097 istringstream is(cmd.argument);
1101 && (s == "append-row"
1102 || s == "delete-row"
1103 || s == "copy-row"))
1106 && (s == "delete-row" || s == "swap-row"))
1109 && (s == "append-column"
1110 || s == "delete-column"
1111 || s == "copy-column"))
1114 && (s == "delete-column" || s == "swap-column"))
1116 return MathGridInset::getStatus(cur, cmd, flag);
1119 return MathGridInset::getStatus(cur, cmd, flag);
1124 /////////////////////////////////////////////////////////////////////
1126 #include "math_arrayinset.h"
1127 #include "math_deliminset.h"
1128 #include "math_factory.h"
1129 #include "math_parser.h"
1130 #include "math_spaceinset.h"
1131 #include "ref_inset.h"
1133 #include "bufferview_funcs.h"
1134 #include "lyxtext.h"
1136 #include "frontends/LyXView.h"
1137 #include "frontends/Dialogs.h"
1139 #include "support/lyxlib.h"
1142 // simply scrap this function if you want
1143 void MathHullInset::mutateToText()
1146 // translate to latex
1148 latex(NULL, os, false, false);
1149 string str = os.str();
1152 LyXText * lt = view_->getLyXText();
1153 string::const_iterator cit = str.begin();
1154 string::const_iterator end = str.end();
1155 for (; cit != end; ++cit)
1156 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
1159 //view_->owner()->dispatch(LFUN_ESCAPE);
1164 void MathHullInset::handleFont(LCursor & cur, string const & arg,
1165 string const & font)
1167 // this whole function is a hack and won't work for incremental font
1170 if (cur.inset().asMathInset()->name() == font)
1171 cur.handleFont(font);
1173 cur.handleNest(createMathInset(font));
1179 void MathHullInset::handleFont2(LCursor & cur, string const & arg)
1184 bv_funcs::string2font(arg, font, b);
1185 if (font.color() != LColor::inherit) {
1186 MathAtom at = createMathInset("color");
1187 asArray(lcolor.getGUIName(font.color()), at.nucleus()->cell(0));
1188 cur.handleNest(at, 1);
1193 void MathHullInset::edit(LCursor & cur, bool left)
1196 left ? idxFirst(cur) : idxLast(cur);
1200 string const MathHullInset::editMessage() const
1202 return _("Math editor mode");
1206 void MathHullInset::getCursorDim(int & asc, int & desc) const
1210 //math_font_max_dim(font_, asc, des);
1214 void MathHullInset::revealCodes(LCursor & cur) const
1216 if (!cur.inMathed())
1220 cur.message(os.str());
1222 // write something to the minibuffer
1223 // translate to latex
1227 string str = os.str();
1229 string::size_type pos = 0;
1231 for (string::iterator it = str.begin(); it != str.end(); ++it) {
1234 else if (*it == '\0') {
1236 pos = it - str.begin();
1242 res = res.substr(pos - 30);
1243 if (res.size() > 60)
1244 res = res.substr(0, 60);
1250 InsetBase::Code MathHullInset::lyxCode() const
1256 /////////////////////////////////////////////////////////////////////
1260 bool MathHullInset::searchForward(BufferView * bv, string const & str,
1263 #ifdef WITH_WARNINGS
1264 #warning completely broken
1266 static MathHullInset * lastformula = 0;
1267 static CursorBase current = DocIterator(ibegin(nucleus()));
1268 static MathArray ar;
1269 static string laststr;
1271 if (lastformula != this || laststr != str) {
1272 //lyxerr << "reset lastformula to " << this << endl;
1275 current = ibegin(nucleus());
1277 mathed_parse_cell(ar, str);
1281 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
1283 for (DocIterator it = current; it != iend(nucleus()); increment(it)) {
1284 CursorSlice & top = it.back();
1285 MathArray const & a = top.asMathInset()->cell(top.idx_);
1286 if (a.matchpart(ar, top.pos_)) {
1287 bv->cursor().setSelection(it, ar.size());
1289 top.pos_ += ar.size();
1295 //lyxerr << "not found!" << endl;
1302 void MathHullInset::write(Buffer const &, std::ostream & os) const
1304 WriteStream wi(os, false, false);
1310 void MathHullInset::read(Buffer const &, LyXLex & lex)
1313 mathed_parse_normal(at, lex);
1314 operator=(*at->asHullInset());
1318 int MathHullInset::plaintext(Buffer const &, ostream & os,
1319 OutputParams const &) const
1321 if (0 && display()) {
1325 TextPainter tpain(dim.width(), dim.height());
1326 drawT(tpain, 0, dim.ascent());
1328 // reset metrics cache to "real" values
1330 return tpain.textheight();
1332 WriteStream wi(os, false, true);
1339 int MathHullInset::linuxdoc(Buffer const & buf, ostream & os,
1340 OutputParams const & runparams) const
1342 return docbook(buf, os, runparams);
1346 int MathHullInset::docbook(Buffer const & buf, ostream & os,
1347 OutputParams const & runparams) const
1349 MathMLStream ms(os);
1352 if (getType() == "simple")
1353 name= "inlineequation";
1355 name = "informalequation";
1357 string bname = name;
1358 if (!label(0).empty())
1359 bname += " id=\"" + sgml::cleanID(buf, runparams, label(0)) + "\"";
1360 ms << MTag(bname.c_str());
1363 if (runparams.flavor == OutputParams::XML) {
1364 ms << MTag("alt role=\"tex\" ");
1365 // Workaround for db2latex: db2latex always includes equations with
1366 // \ensuremath{} or \begin{display}\end{display}
1367 // so we strip LyX' math environment
1368 WriteStream wi(ls, false, false);
1369 MathGridInset::write(wi);
1370 ms << subst(subst(ls.str(), "&", "&"), "<", "<");
1373 MathGridInset::mathmlize(ms);
1376 ms << MTag("alt role=\"tex\"");
1377 res = latex(buf, ls, runparams);
1378 ms << subst(subst(ls.str(), "&", "&"), "<", "<");
1382 ms << "<graphic fileref=\"eqn/";
1383 if ( !label(0).empty())
1384 ms << sgml::cleanID(buf, runparams, label(0));
1386 ms << sgml::uniqueID("anon");
1388 if (runparams.flavor == OutputParams::XML)
1393 ms << ETag(name.c_str());
1394 return ms.line() + res;