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"
23 #include "FuncStatus.h"
25 #include "LaTeXFeatures.h"
28 #include "dispatchresult.h"
29 #include "funcrequest.h"
33 #include "outputparams.h"
35 #include "textpainter.h"
38 #include "insets/render_preview.h"
40 #include "frontends/Alert.h"
42 #include "graphics/PreviewImage.h"
43 #include "graphics/PreviewLoader.h"
45 #include "support/lstrings.h"
47 #include <boost/bind.hpp>
51 using lyx::cap::grabAndEraseSelection;
52 using lyx::support::bformat;
53 using lyx::support::subst;
60 using std::istringstream;
62 using std::ostringstream;
70 int getCols(string const & type)
72 if (type == "eqnarray")
76 if (type == "flalign")
78 if (type == "alignat")
80 if (type == "xalignat")
82 if (type == "xxalignat")
88 // returns position of first relation operator in the array
89 // used for "intelligent splitting"
90 size_t firstRelOp(MathArray const & ar)
92 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
94 return it - ar.begin();
99 char const * star(bool numbered)
101 return numbered ? "" : "*";
105 int typecode(string const & s)
107 if (s == "none") return 0;
108 if (s == "simple") return 1;
109 if (s == "equation") return 2;
110 if (s == "eqnarray") return 3;
111 if (s == "align") return 4;
112 if (s == "alignat") return 5;
113 if (s == "xalignat") return 6;
114 if (s == "xxalignat") return 7;
115 if (s == "multline") return 8;
116 if (s == "gather") return 9;
117 if (s == "flalign") return 10;
118 lyxerr << "unknown hull type '" << s << "'" << endl;
122 bool smaller(string const & s, string const & t)
124 return typecode(s) < typecode(t);
128 } // end anon namespace
132 MathHullInset::MathHullInset()
133 : MathGridInset(1, 1), type_("none"), nonum_(1), label_(1),
134 preview_(new RenderPreview(this))
136 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << endl;
137 //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl;
138 //lyxerr << "sizeof MathCharInset: " << sizeof(MathCharInset) << endl;
139 //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << endl;
144 MathHullInset::MathHullInset(string const & type)
145 : MathGridInset(getCols(type), 1), type_(type), nonum_(1), label_(1),
146 preview_(new RenderPreview(this))
152 MathHullInset::MathHullInset(MathHullInset const & other)
153 : MathGridInset(other),
154 type_(other.type_), nonum_(other.nonum_), label_(other.label_),
155 preview_(new RenderPreview(this))
159 MathHullInset::~MathHullInset()
163 auto_ptr<InsetBase> MathHullInset::doClone() const
165 return auto_ptr<InsetBase>(new MathHullInset(*this));
169 MathHullInset & MathHullInset::operator=(MathHullInset const & other)
173 *static_cast<MathGridInset*>(this) = MathGridInset(other);
175 nonum_ = other.nonum_;
176 label_ = other.label_;
177 preview_.reset(new RenderPreview(*other.preview_, this));
183 MathInset::mode_type MathHullInset::currentMode() const
186 return UNDECIDED_MODE;
187 // definitely math mode ...
192 bool MathHullInset::idxFirst(LCursor & cur) const
200 bool MathHullInset::idxLast(LCursor & cur) const
202 cur.idx() = nargs() - 1;
203 cur.pos() = cur.lastpos();
208 char MathHullInset::defaultColAlign(col_type col)
210 if (type_ == "eqnarray")
212 if (typecode(type_) >= typecode("align"))
213 return "rl"[col & 1];
218 int MathHullInset::defaultColSpace(col_type col)
220 if (type_ == "align" || type_ == "alignat")
222 if (type_ == "xalignat")
223 return (col & 1) ? 20 : 0;
224 if (type_ == "xxalignat" || type_ == "flalign")
225 return (col & 1) ? 40 : 0;
230 char const * MathHullInset::standardFont() const
233 return "lyxnochange";
238 void MathHullInset::metrics(MetricsInfo & mi, Dimension & dim) const
240 BOOST_ASSERT(mi.base.bv && mi.base.bv->buffer());
242 bool use_preview = false;
243 if (!editing(mi.base.bv) &&
244 RenderPreview::status() == LyXRC::PREVIEW_ON) {
245 lyx::graphics::PreviewImage const * pimage =
246 preview_->getPreviewImage(*mi.base.bv->buffer());
247 use_preview = pimage && pimage->image();
251 preview_->metrics(mi, dim);
252 // insert a one pixel gap in front of the formula
260 FontSetChanger dummy1(mi.base, standardFont());
261 StyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
263 // let the cells adjust themselves
264 MathGridInset::metrics(mi, dim);
271 if (numberedType()) {
272 FontSetChanger dummy(mi.base, "mathbf");
274 for (row_type row = 0; row < nrows(); ++row)
275 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
281 // make it at least as high as the current font
284 math_font_max_dim(mi.base.font, asc, des);
285 dim.asc = max(dim.asc, asc);
286 dim.des = max(dim.des, des);
292 void MathHullInset::draw(PainterInfo & pi, int x, int y) const
294 BOOST_ASSERT(pi.base.bv && pi.base.bv->buffer());
296 bool use_preview = false;
297 if (!editing(pi.base.bv) &&
298 RenderPreview::status() == LyXRC::PREVIEW_ON) {
299 lyx::graphics::PreviewImage const * pimage =
300 preview_->getPreviewImage(*pi.base.bv->buffer());
301 use_preview = pimage && pimage->image();
305 // one pixel gap in front
306 preview_->draw(pi, x + 1, y);
307 setPosCache(pi, x, y);
311 FontSetChanger dummy1(pi.base, standardFont());
312 StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
313 MathGridInset::draw(pi, x + 1, y);
315 if (numberedType()) {
316 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
317 for (row_type row = 0; row < nrows(); ++row) {
318 int const yy = y + rowinfo_[row].offset_;
319 FontSetChanger dummy(pi.base, "mathrm");
320 drawStr(pi, pi.base.font, xx, yy, nicelabel(row));
323 setPosCache(pi, x, y);
327 void MathHullInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
330 MathGridInset::metricsT(mi, dim);
333 WriteStream wi(os, false, true);
335 dim.wid = os.str().size();
342 void MathHullInset::drawT(TextPainter & pain, int x, int y) const
345 MathGridInset::drawT(pain, x, y);
348 WriteStream wi(os, false, true);
350 pain.draw(x, y, os.str().c_str());
357 string const latex_string(MathHullInset const & inset)
360 WriteStream wi(ls, false, false);
368 void MathHullInset::addPreview(lyx::graphics::PreviewLoader & ploader) const
370 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
371 string const snippet = latex_string(*this);
372 preview_->addPreview(snippet, ploader);
377 void MathHullInset::notifyCursorLeaves(LCursor & cur)
379 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
380 Buffer const & buffer = cur.buffer();
381 string const snippet = latex_string(*this);
382 preview_->addPreview(snippet, buffer);
383 preview_->startLoading(buffer);
388 string MathHullInset::label(row_type row) const
390 row_type n = nrows();
391 BOOST_ASSERT(row < n);
396 void MathHullInset::label(row_type row, string const & label)
398 //lyxerr << "setting label '" << label << "' for row " << row << endl;
403 void MathHullInset::numbered(row_type row, bool num)
409 bool MathHullInset::numbered(row_type row) const
415 bool MathHullInset::ams() const
419 type_ == "flalign" ||
420 type_ == "multline" ||
422 type_ == "alignat" ||
423 type_ == "xalignat" ||
424 type_ == "xxalignat";
428 bool MathHullInset::display() const
430 return type_ != "simple" && type_ != "none";
434 void MathHullInset::getLabelList(Buffer const &, vector<string> & labels) const
436 for (row_type row = 0; row < nrows(); ++row)
437 if (!label_[row].empty() && nonum_[row] != 1)
438 labels.push_back(label_[row]);
442 bool MathHullInset::numberedType() const
446 if (type_ == "simple")
448 if (type_ == "xxalignat")
450 for (row_type row = 0; row < nrows(); ++row)
457 void MathHullInset::validate(LaTeXFeatures & features) const
460 features.require("amsmath");
463 // Validation is necessary only if not using AMS math.
464 // To be safe, we will always run mathedvalidate.
465 //if (features.amsstyle)
468 features.require("boldsymbol");
469 //features.binom = true;
471 MathGridInset::validate(features);
475 void MathHullInset::header_write(WriteStream & os) const
477 bool n = numberedType();
482 else if (type_ == "simple") {
488 else if (type_ == "equation") {
490 os << "\\begin{equation" << star(n) << "}\n";
495 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
496 || type_ == "gather" || type_ == "multline")
497 os << "\\begin{" << type_ << star(n) << "}\n";
499 else if (type_ == "alignat" || type_ == "xalignat")
500 os << "\\begin{" << type_ << star(n) << '}'
501 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
503 else if (type_ == "xxalignat")
504 os << "\\begin{" << type_ << '}'
505 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
508 os << "\\begin{unknown" << star(n) << '}';
512 void MathHullInset::footer_write(WriteStream & os) const
514 bool n = numberedType();
519 else if (type_ == "simple")
522 else if (type_ == "equation")
524 os << "\\end{equation" << star(n) << "}\n";
528 else if (type_ == "eqnarray" || type_ == "align" || type_ == "flalign"
529 || type_ == "alignat" || type_ == "xalignat"
530 || type_ == "gather" || type_ == "multline")
531 os << "\\end{" << type_ << star(n) << "}\n";
533 else if (type_ == "xxalignat")
534 os << "\\end{" << type_ << "}\n";
537 os << "\\end{unknown" << star(n) << '}';
541 bool MathHullInset::rowChangeOK() const
544 type_ == "eqnarray" || type_ == "align" ||
545 type_ == "flalign" || type_ == "alignat" ||
546 type_ == "xalignat" || type_ == "xxalignat" ||
547 type_ == "gather" || type_ == "multline";
551 bool MathHullInset::colChangeOK() const
554 type_ == "align" || type_ == "flalign" ||type_ == "alignat" ||
555 type_ == "xalignat" || type_ == "xxalignat";
559 void MathHullInset::addRow(row_type row)
563 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
564 label_.insert(label_.begin() + row + 1, string());
565 MathGridInset::addRow(row);
569 void MathHullInset::swapRow(row_type row)
573 if (row + 1 == nrows())
575 swap(nonum_[row], nonum_[row + 1]);
576 swap(label_[row], label_[row + 1]);
577 MathGridInset::swapRow(row);
581 void MathHullInset::delRow(row_type row)
583 if (nrows() <= 1 || !rowChangeOK())
585 MathGridInset::delRow(row);
586 nonum_.erase(nonum_.begin() + row);
587 label_.erase(label_.begin() + row);
591 void MathHullInset::addCol(col_type col)
595 MathGridInset::addCol(col);
599 void MathHullInset::delCol(col_type col)
601 if (ncols() <= 1 || !colChangeOK())
603 MathGridInset::delCol(col);
607 string MathHullInset::nicelabel(row_type row) const
611 if (label_[row].empty())
612 return string("(#)");
613 return '(' + label_[row] + ')';
617 void MathHullInset::glueall()
620 for (idx_type i = 0; i < nargs(); ++i)
622 *this = MathHullInset("simple");
628 void MathHullInset::splitTo2Cols()
630 BOOST_ASSERT(ncols() == 1);
631 MathGridInset::addCol(1);
632 for (row_type row = 0; row < nrows(); ++row) {
633 idx_type const i = 2 * row;
634 pos_type pos = firstRelOp(cell(i));
635 cell(i + 1) = MathArray(cell(i).begin() + pos, cell(i).end());
636 cell(i).erase(pos, cell(i).size());
641 void MathHullInset::splitTo3Cols()
643 BOOST_ASSERT(ncols() < 3);
646 MathGridInset::addCol(1);
647 for (row_type row = 0; row < nrows(); ++row) {
648 idx_type const i = 3 * row + 1;
649 if (cell(i).size()) {
650 cell(i + 1) = MathArray(cell(i).begin() + 1, cell(i).end());
651 cell(i).erase(1, cell(i).size());
657 void MathHullInset::changeCols(col_type cols)
661 else if (ncols() < cols) {
667 while (ncols() < cols)
668 MathGridInset::addCol(ncols() - 1);
674 for (row_type row = 0; row < nrows(); ++row) {
675 idx_type const i = row * ncols();
676 for (col_type col = cols; col < ncols(); ++col) {
677 cell(i + cols - 1).append(cell(i + col));
681 while (ncols() > cols) {
682 MathGridInset::delCol(ncols() - 1);
687 string const & MathHullInset::getType() const
693 void MathHullInset::setType(string const & type)
701 void MathHullInset::mutate(string const & newtype)
703 lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
705 // we try to move along the chain
706 // none <-> simple <-> equation <-> eqnarray -> *align* -> multline, gather -+
708 // +-------------------------------------+
709 // we use eqnarray as intermediate type for mutations that are not
710 // directly supported because it handles labels and numbering for
713 if (newtype == "dump") {
717 else if (newtype == type_) {
721 else if (typecode(newtype) < 0) {
725 else if (type_ == "none") {
731 else if (type_ == "simple") {
732 if (newtype == "none") {
742 else if (type_ == "equation") {
743 if (smaller(newtype, type_)) {
747 } else if (newtype == "eqnarray") {
748 // split it "nicely" on the first relop
751 } else if (newtype == "multline" || newtype == "gather") {
761 else if (type_ == "eqnarray") {
762 if (smaller(newtype, type_)) {
763 // set correct (no)numbering
764 bool allnonum = true;
765 for (row_type row = 0; row < nrows(); ++row)
769 // set first non-empty label
771 for (row_type row = 0; row < nrows(); ++row) {
772 if (!label_[row].empty()) {
779 nonum_[0] = allnonum;
782 } else { // align & Co.
789 else if (type_ == "align" || type_ == "alignat" ||
790 type_ == "xalignat" || type_ == "flalign") {
791 if (smaller(newtype, "align")) {
795 } else if (newtype == "gather" || newtype == "multline") {
798 } else if (newtype == "xxalignat") {
799 for (row_type row = 0; row < nrows(); ++row)
800 numbered(row, false);
807 else if (type_ == "xxalignat") {
808 for (row_type row = 0; row < nrows(); ++row)
809 numbered(row, false);
810 if (smaller(newtype, "align")) {
814 } else if (newtype == "gather" || newtype == "multline") {
822 else if (type_ == "multline" || type_ == "gather") {
823 if (newtype == "gather" || newtype == "multline")
825 else if (newtype == "align" || newtype == "flalign" ||
826 newtype == "alignat" || newtype == "xalignat") {
829 } else if (newtype == "xxalignat") {
831 for (row_type row = 0; row < nrows(); ++row)
832 numbered(row, false);
842 lyxerr << "mutation from '" << type_
843 << "' to '" << newtype << "' not implemented" << endl;
848 string MathHullInset::eolString(row_type row, bool fragile) const
851 if (numberedType()) {
852 if (!label_[row].empty() && !nonum_[row])
853 res += "\\label{" + label_[row] + '}';
854 if (nonum_[row] && (type_ != "multline"))
855 res += "\\nonumber ";
857 return res + MathGridInset::eolString(row, fragile);
861 void MathHullInset::write(WriteStream & os) const
864 MathGridInset::write(os);
869 void MathHullInset::normalize(NormalStream & os) const
871 os << "[formula " << type_ << ' ';
872 MathGridInset::normalize(os);
877 void MathHullInset::mathmlize(MathMLStream & os) const
879 MathGridInset::mathmlize(os);
883 void MathHullInset::infoize(ostream & os) const
885 os << "Type: " << type_;
889 void MathHullInset::check() const
891 BOOST_ASSERT(nonum_.size() == nrows());
892 BOOST_ASSERT(label_.size() == nrows());
896 void MathHullInset::doExtern(LCursor & cur, FuncRequest & func)
900 istringstream iss(func.argument);
901 iss >> lang >> extra;
906 #warning temporarily disabled
907 //if (cur.selection()) {
910 // lyxerr << "use selection: " << ar << endl;
911 // insert(pipeThroughExtern(lang, extra, ar));
917 eq.push_back(MathAtom(new MathCharInset('=')));
919 // go to first item in line
920 cur.idx() -= cur.idx() % ncols();
923 if (getType() == "simple") {
924 size_type pos = cur.cell().find_last(eq);
926 if (cur.inMathed() && cur.selection()) {
927 asArray(grabAndEraseSelection(cur), ar);
928 } else if (pos == cur.cell().size()) {
930 lyxerr << "use whole cell: " << ar << endl;
932 ar = MathArray(cur.cell().begin() + pos + 1, cur.cell().end());
933 lyxerr << "use partial cell form pos: " << pos << endl;
935 cur.cell().append(eq);
936 cur.cell().append(pipeThroughExtern(lang, extra, ar));
937 cur.pos() = cur.lastpos();
941 if (getType() == "equation") {
942 lyxerr << "use equation inset" << endl;
944 MathArray & ar = cur.cell();
945 lyxerr << "use cell: " << ar << endl;
949 cur.cell() = pipeThroughExtern(lang, extra, ar);
950 // move to end of line
951 cur.pos() = cur.lastpos();
956 lyxerr << "use eqnarray" << endl;
957 cur.idx() += 2 - cur.idx() % ncols();
959 MathArray ar = cur.cell();
960 lyxerr << "use cell: " << ar << endl;
962 #warning temporarily disabled
969 cur.cell() = pipeThroughExtern(lang, extra, ar);
970 cur.pos() = cur.lastpos();
975 void MathHullInset::doDispatch(LCursor & cur, FuncRequest & cmd)
977 switch (cmd.action) {
979 case LFUN_FINISHED_LEFT:
980 case LFUN_FINISHED_RIGHT:
981 case LFUN_FINISHED_UP:
982 case LFUN_FINISHED_DOWN:
983 MathGridInset::doDispatch(cur, cmd);
984 notifyCursorLeaves(cur);
987 case LFUN_BREAKPARAGRAPH:
992 // some magic for the common case
993 if (type_ == "simple" || type_ == "equation") {
994 recordUndoInset(cur);
997 cur.pos() = cur.lastpos();
999 MathGridInset::doDispatch(cur, cmd);
1002 case LFUN_MATH_NUMBER:
1003 //lyxerr << "toggling all numbers" << endl;
1005 recordUndoInset(cur);
1006 bool old = numberedType();
1007 if (type_ == "multline")
1008 numbered(nrows() - 1, !old);
1010 for (row_type row = 0; row < nrows(); ++row)
1011 numbered(row, !old);
1012 cur.message(old ? _("No number") : _("Number"));
1016 case LFUN_MATH_NONUMBER:
1018 recordUndoInset(cur);
1019 row_type r = (type_ == "multline") ? nrows() - 1 : cur.row();
1020 bool old = numbered(r);
1021 cur.message(old ? _("No number") : _("Number"));
1026 case LFUN_INSERT_LABEL: {
1027 recordUndoInset(cur);
1028 row_type r = (type_ == "multline") ? nrows() - 1 : cur.row();
1029 string old_label = label(r);
1030 string new_label = cmd.argument;
1032 if (new_label.empty()) {
1033 string const default_label =
1034 (lyxrc.label_init_length >= 0) ? "eq:" : "";
1035 pair<bool, string> const res = old_label.empty()
1036 ? Alert::askForText(_("Enter new label to insert:"), default_label)
1037 : Alert::askForText(_("Enter label:"), old_label);
1039 new_label = lyx::support::trim(res.second);
1041 new_label = old_label;
1044 if (!new_label.empty())
1046 label(r, new_label);
1050 case LFUN_MATH_EXTERN:
1051 recordUndoInset(cur);
1055 case LFUN_MATH_MUTATE: {
1056 recordUndoInset(cur);
1057 row_type row = cur.row();
1058 col_type col = cur.col();
1059 mutate(cmd.argument);
1060 cur.idx() = row * ncols() + col;
1061 if (cur.idx() > cur.lastidx()) {
1062 cur.idx() = cur.lastidx();
1063 cur.pos() = cur.lastpos();
1065 if (cur.pos() > cur.lastpos())
1066 cur.pos() = cur.lastpos();
1067 //cur.dispatched(FINISHED);
1071 case LFUN_MATH_DISPLAY: {
1072 recordUndoInset(cur);
1073 mutate(type_ == "simple" ? "equation" : "simple");
1075 cur.pos() = cur.lastpos();
1076 //cur.dispatched(FINISHED);
1081 MathGridInset::doDispatch(cur, cmd);
1087 bool MathHullInset::getStatus(LCursor & cur, FuncRequest const & cmd,
1088 FuncStatus & flag) const
1090 switch (cmd.action) {
1091 case LFUN_BREAKLINE:
1092 case LFUN_MATH_NUMBER:
1093 case LFUN_MATH_NONUMBER:
1094 case LFUN_INSERT_LABEL:
1095 case LFUN_MATH_EXTERN:
1096 case LFUN_MATH_MUTATE:
1097 case LFUN_MATH_DISPLAY:
1101 case LFUN_TABULAR_FEATURE: {
1102 istringstream is(cmd.argument);
1106 && (s == "append-row"
1107 || s == "delete-row"
1108 || s == "copy-row")) {
1109 flag.message(bformat(
1110 N_("Can't change number of rows in '%1$s'"),
1112 flag.enabled(false);
1116 && (s == "append-column"
1117 || s == "delete-column"
1118 || s == "copy-column")) {
1119 flag.message(bformat(
1120 N_("Can't change number of columns in '%1$s'"),
1122 flag.enabled(false);
1125 if ((type_ == "simple"
1126 || type_ == "equation"
1127 || type_ == "none") &&
1128 (s == "add-hline-above" || s == "add-hline-below" ||
1129 s == "add-vline-left" || s == "add-vline-right")) {
1130 flag.message(bformat(
1131 N_("Can't add grid lines in '%1$s'"),
1133 flag.enabled(false);
1136 return MathGridInset::getStatus(cur, cmd, flag);
1139 return MathGridInset::getStatus(cur, cmd, flag);
1144 /////////////////////////////////////////////////////////////////////
1146 #include "math_arrayinset.h"
1147 #include "math_deliminset.h"
1148 #include "math_factory.h"
1149 #include "math_parser.h"
1150 #include "math_spaceinset.h"
1151 #include "ref_inset.h"
1153 #include "bufferview_funcs.h"
1154 #include "lyxtext.h"
1156 #include "frontends/LyXView.h"
1157 #include "frontends/Dialogs.h"
1159 #include "support/lyxlib.h"
1162 // simply scrap this function if you want
1163 void MathHullInset::mutateToText()
1166 // translate to latex
1168 latex(NULL, os, false, false);
1169 string str = os.str();
1172 LyXText * lt = view_->getLyXText();
1173 string::const_iterator cit = str.begin();
1174 string::const_iterator end = str.end();
1175 for (; cit != end; ++cit)
1176 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
1179 //view_->owner()->dispatch(LFUN_ESCAPE);
1184 void MathHullInset::handleFont(LCursor & cur, string const & arg,
1185 string const & font)
1187 // this whole function is a hack and won't work for incremental font
1190 if (cur.inset().asMathInset()->name() == font)
1191 cur.handleFont(font);
1193 cur.handleNest(createMathInset(font));
1199 void MathHullInset::handleFont2(LCursor & cur, string const & arg)
1204 bv_funcs::string2font(arg, font, b);
1205 if (font.color() != LColor::inherit) {
1206 MathAtom at = createMathInset("color");
1207 asArray(lcolor.getGUIName(font.color()), at.nucleus()->cell(0));
1208 cur.handleNest(at, 1);
1213 void MathHullInset::edit(LCursor & cur, bool left)
1216 left ? idxFirst(cur) : idxLast(cur);
1220 string const MathHullInset::editMessage() const
1222 return _("Math editor mode");
1226 void MathHullInset::getCursorDim(int & asc, int & desc) const
1230 //math_font_max_dim(font_, asc, des);
1234 void MathHullInset::revealCodes(LCursor & cur) const
1236 if (!cur.inMathed())
1240 cur.message(os.str());
1242 // write something to the minibuffer
1243 // translate to latex
1247 string str = os.str();
1249 string::size_type pos = 0;
1251 for (string::iterator it = str.begin(); it != str.end(); ++it) {
1254 else if (*it == '\0') {
1256 pos = it - str.begin();
1262 res = res.substr(pos - 30);
1263 if (res.size() > 60)
1264 res = res.substr(0, 60);
1270 InsetBase::Code MathHullInset::lyxCode() const
1276 /////////////////////////////////////////////////////////////////////
1280 bool MathHullInset::searchForward(BufferView * bv, string const & str,
1283 #ifdef WITH_WARNINGS
1284 #warning completely broken
1286 static MathHullInset * lastformula = 0;
1287 static CursorBase current = DocIterator(ibegin(nucleus()));
1288 static MathArray ar;
1289 static string laststr;
1291 if (lastformula != this || laststr != str) {
1292 //lyxerr << "reset lastformula to " << this << endl;
1295 current = ibegin(nucleus());
1297 mathed_parse_cell(ar, str);
1301 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
1303 for (DocIterator it = current; it != iend(nucleus()); increment(it)) {
1304 CursorSlice & top = it.back();
1305 MathArray const & a = top.asMathInset()->cell(top.idx_);
1306 if (a.matchpart(ar, top.pos_)) {
1307 bv->cursor().setSelection(it, ar.size());
1309 top.pos_ += ar.size();
1315 //lyxerr << "not found!" << endl;
1322 void MathHullInset::write(Buffer const &, std::ostream & os) const
1324 WriteStream wi(os, false, false);
1330 void MathHullInset::read(Buffer const &, LyXLex & lex)
1333 mathed_parse_normal(at, lex);
1334 operator=(*at->asHullInset());
1338 int MathHullInset::plaintext(Buffer const &, ostream & os,
1339 OutputParams const &) const
1341 if (0 && display()) {
1345 TextPainter tpain(dim.width(), dim.height());
1346 drawT(tpain, 0, dim.ascent());
1348 // reset metrics cache to "real" values
1350 return tpain.textheight();
1352 WriteStream wi(os, false, true);
1359 int MathHullInset::linuxdoc(Buffer const & buf, ostream & os,
1360 OutputParams const & runparams) const
1362 return docbook(buf, os, runparams);
1366 int MathHullInset::docbook(Buffer const & buf, ostream & os,
1367 OutputParams const & runparams) const
1369 MathMLStream ms(os);
1372 if (getType() == "simple")
1373 name= "inlineequation";
1375 name = "informalequation";
1377 string bname = name;
1378 if (!label(0).empty())
1379 bname += " id=\"" + sgml::cleanID(buf, runparams, label(0)) + "\"";
1380 ms << MTag(bname.c_str());
1383 if (runparams.flavor == OutputParams::XML) {
1384 ms << MTag("alt role=\"tex\" ");
1385 // Workaround for db2latex: db2latex always includes equations with
1386 // \ensuremath{} or \begin{display}\end{display}
1387 // so we strip LyX' math environment
1388 WriteStream wi(ls, false, false);
1389 MathGridInset::write(wi);
1390 ms << subst(subst(ls.str(), "&", "&"), "<", "<");
1393 MathGridInset::mathmlize(ms);
1396 ms << MTag("alt role=\"tex\"");
1397 res = latex(buf, ls, runparams);
1398 ms << subst(subst(ls.str(), "&", "&"), "<", "<");
1402 ms << "<graphic fileref=\"eqn/";
1403 if ( !label(0).empty())
1404 ms << sgml::cleanID(buf, runparams, label(0));
1406 ms << sgml::uniqueID("anon");
1408 if (runparams.flavor == OutputParams::XML)
1413 ms << ETag(name.c_str());
1414 return ms.line() + res;