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 // The last dummy row has no number info nor a label.
592 // Test nrows() + 1 because we have already erased the row.
593 if (row == nrows() + 1)
595 nonum_.erase(nonum_.begin() + row);
596 label_.erase(label_.begin() + row);
600 void MathHullInset::addCol(col_type col)
604 MathGridInset::addCol(col);
608 void MathHullInset::delCol(col_type col)
610 if (ncols() <= 1 || !colChangeOK())
612 MathGridInset::delCol(col);
616 string MathHullInset::nicelabel(row_type row) const
620 if (label_[row].empty())
621 return string("(#)");
622 return '(' + label_[row] + ')';
626 void MathHullInset::glueall()
629 for (idx_type i = 0; i < nargs(); ++i)
631 *this = MathHullInset("simple");
637 void MathHullInset::splitTo2Cols()
639 BOOST_ASSERT(ncols() == 1);
640 MathGridInset::addCol(1);
641 for (row_type row = 0; row < nrows(); ++row) {
642 idx_type const i = 2 * row;
643 pos_type pos = firstRelOp(cell(i));
644 cell(i + 1) = MathArray(cell(i).begin() + pos, cell(i).end());
645 cell(i).erase(pos, cell(i).size());
650 void MathHullInset::splitTo3Cols()
652 BOOST_ASSERT(ncols() < 3);
655 MathGridInset::addCol(1);
656 for (row_type row = 0; row < nrows(); ++row) {
657 idx_type const i = 3 * row + 1;
658 if (cell(i).size()) {
659 cell(i + 1) = MathArray(cell(i).begin() + 1, cell(i).end());
660 cell(i).erase(1, cell(i).size());
666 void MathHullInset::changeCols(col_type cols)
670 else if (ncols() < cols) {
676 while (ncols() < cols)
677 MathGridInset::addCol(ncols() - 1);
683 for (row_type row = 0; row < nrows(); ++row) {
684 idx_type const i = row * ncols();
685 for (col_type col = cols; col < ncols(); ++col) {
686 cell(i + cols - 1).append(cell(i + col));
690 while (ncols() > cols) {
691 MathGridInset::delCol(ncols() - 1);
696 string const & MathHullInset::getType() const
702 void MathHullInset::setType(string const & type)
710 void MathHullInset::mutate(string const & newtype)
712 //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
714 // we try to move along the chain
715 // none <-> simple <-> equation <-> eqnarray -> *align* -> multline, gather -+
717 // +-------------------------------------+
718 // we use eqnarray as intermediate type for mutations that are not
719 // directly supported because it handles labels and numbering for
722 if (newtype == "dump") {
726 else if (newtype == type_) {
730 else if (typecode(newtype) < 0) {
734 else if (type_ == "none") {
740 else if (type_ == "simple") {
741 if (newtype == "none") {
751 else if (type_ == "equation") {
752 if (smaller(newtype, type_)) {
756 } else if (newtype == "eqnarray") {
757 // split it "nicely" on the first relop
760 } else if (newtype == "multline" || newtype == "gather") {
770 else if (type_ == "eqnarray") {
771 if (smaller(newtype, type_)) {
772 // set correct (no)numbering
773 bool allnonum = true;
774 for (row_type row = 0; row < nrows(); ++row)
778 // set first non-empty label
780 for (row_type row = 0; row < nrows(); ++row) {
781 if (!label_[row].empty()) {
788 nonum_[0] = allnonum;
791 } else { // align & Co.
798 else if (type_ == "align" || type_ == "alignat" ||
799 type_ == "xalignat" || type_ == "flalign") {
800 if (smaller(newtype, "align")) {
804 } else if (newtype == "gather" || newtype == "multline") {
807 } else if (newtype == "xxalignat") {
808 for (row_type row = 0; row < nrows(); ++row)
809 numbered(row, false);
816 else if (type_ == "xxalignat") {
817 for (row_type row = 0; row < nrows(); ++row)
818 numbered(row, false);
819 if (smaller(newtype, "align")) {
823 } else if (newtype == "gather" || newtype == "multline") {
831 else if (type_ == "multline" || type_ == "gather") {
832 if (newtype == "gather" || newtype == "multline")
834 else if (newtype == "align" || newtype == "flalign" ||
835 newtype == "alignat" || newtype == "xalignat") {
838 } else if (newtype == "xxalignat") {
840 for (row_type row = 0; row < nrows(); ++row)
841 numbered(row, false);
851 lyxerr << "mutation from '" << type_
852 << "' to '" << newtype << "' not implemented" << endl;
857 string MathHullInset::eolString(row_type row, bool emptyline, bool fragile) const
860 if (numberedType()) {
861 if (!label_[row].empty() && !nonum_[row])
862 res += "\\label{" + label_[row] + '}';
863 if (nonum_[row] && (type_ != "multline"))
864 res += "\\nonumber ";
866 return res + MathGridInset::eolString(row, emptyline, fragile);
870 void MathHullInset::write(WriteStream & os) const
873 MathGridInset::write(os);
878 void MathHullInset::normalize(NormalStream & os) const
880 os << "[formula " << type_ << ' ';
881 MathGridInset::normalize(os);
886 void MathHullInset::mathmlize(MathMLStream & os) const
888 MathGridInset::mathmlize(os);
892 void MathHullInset::infoize(ostream & os) const
894 os << "Type: " << type_;
898 void MathHullInset::check() const
900 BOOST_ASSERT(nonum_.size() == nrows());
901 BOOST_ASSERT(label_.size() == nrows());
905 void MathHullInset::doExtern(LCursor & cur, FuncRequest & func)
909 istringstream iss(func.argument);
910 iss >> lang >> extra;
915 #warning temporarily disabled
916 //if (cur.selection()) {
919 // lyxerr << "use selection: " << ar << endl;
920 // insert(pipeThroughExtern(lang, extra, ar));
926 eq.push_back(MathAtom(new MathCharInset('=')));
928 // go to first item in line
929 cur.idx() -= cur.idx() % ncols();
932 if (getType() == "simple") {
933 size_type pos = cur.cell().find_last(eq);
935 if (cur.inMathed() && cur.selection()) {
936 asArray(grabAndEraseSelection(cur), ar);
937 } else if (pos == cur.cell().size()) {
939 lyxerr << "use whole cell: " << ar << endl;
941 ar = MathArray(cur.cell().begin() + pos + 1, cur.cell().end());
942 lyxerr << "use partial cell form pos: " << pos << endl;
944 cur.cell().append(eq);
945 cur.cell().append(pipeThroughExtern(lang, extra, ar));
946 cur.pos() = cur.lastpos();
950 if (getType() == "equation") {
951 lyxerr << "use equation inset" << endl;
953 MathArray & ar = cur.cell();
954 lyxerr << "use cell: " << ar << endl;
958 cur.cell() = pipeThroughExtern(lang, extra, ar);
959 // move to end of line
960 cur.pos() = cur.lastpos();
965 lyxerr << "use eqnarray" << endl;
966 cur.idx() += 2 - cur.idx() % ncols();
968 MathArray ar = cur.cell();
969 lyxerr << "use cell: " << ar << endl;
971 #warning temporarily disabled
978 cur.cell() = pipeThroughExtern(lang, extra, ar);
979 cur.pos() = cur.lastpos();
984 void MathHullInset::doDispatch(LCursor & cur, FuncRequest & cmd)
986 lyxerr << "action: " << cmd.action << endl;
987 switch (cmd.action) {
989 case LFUN_FINISHED_LEFT:
990 case LFUN_FINISHED_RIGHT:
991 case LFUN_FINISHED_UP:
992 case LFUN_FINISHED_DOWN:
993 lyxerr << "action: " << cmd.action << endl;
994 MathGridInset::doDispatch(cur, cmd);
995 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 const default_label =
1045 (lyxrc.label_init_length >= 0) ? "eq:" : "";
1046 string const contents = cmd.argument.empty() ?
1047 label(r) : cmd.argument;
1049 InsetCommandParams p("label", contents);
1050 string const data = InsetCommandMailer::params2string("label", p);
1052 if (cmd.argument.empty()) {
1053 cur.bv().owner()->getDialogs().show("label", data, 0);
1055 FuncRequest fr(LFUN_INSET_INSERT, data);
1061 case LFUN_INSET_INSERT: {
1062 //lyxerr << "arg: " << cmd.argument << endl;
1063 string const name = cmd.getArg(0);
1064 if (name == "label") {
1065 InsetCommandParams p;
1066 InsetCommandMailer::string2params(name, cmd.argument, p);
1067 string str = p.getContents();
1068 recordUndoInset(cur);
1069 row_type const r = (type_ == "multline") ? nrows() - 1 : cur.row();
1070 str = lyx::support::trim(str);
1073 string old = label(r);
1075 cur.bv().buffer()->changeRefsIfUnique(old, str);
1082 case LFUN_MATH_EXTERN:
1083 recordUndoInset(cur);
1087 case LFUN_MATH_MUTATE: {
1088 recordUndoInset(cur);
1089 row_type row = cur.row();
1090 col_type col = cur.col();
1091 mutate(cmd.argument);
1092 cur.idx() = row * ncols() + col;
1093 if (cur.idx() > cur.lastidx()) {
1094 cur.idx() = cur.lastidx();
1095 cur.pos() = cur.lastpos();
1097 if (cur.pos() > cur.lastpos())
1098 cur.pos() = cur.lastpos();
1099 //cur.dispatched(FINISHED);
1103 case LFUN_MATH_DISPLAY: {
1104 recordUndoInset(cur);
1105 mutate(type_ == "simple" ? "equation" : "simple");
1107 cur.pos() = cur.lastpos();
1108 //cur.dispatched(FINISHED);
1113 MathGridInset::doDispatch(cur, cmd);
1119 bool MathHullInset::getStatus(LCursor & cur, FuncRequest const & cmd,
1120 FuncStatus & status) const
1122 switch (cmd.action) {
1123 case LFUN_FINISHED_LEFT:
1124 case LFUN_FINISHED_RIGHT:
1125 case LFUN_FINISHED_UP:
1126 case LFUN_FINISHED_DOWN:
1127 status.enabled(true);
1129 case LFUN_BREAKLINE:
1130 case LFUN_MATH_NUMBER:
1131 case LFUN_MATH_NONUMBER:
1132 case LFUN_MATH_EXTERN:
1133 case LFUN_MATH_MUTATE:
1134 case LFUN_MATH_DISPLAY:
1136 status.enabled(true);
1138 case LFUN_INSERT_LABEL:
1139 status.enabled(type_ != "simple");
1141 case LFUN_TABULAR_FEATURE: {
1142 istringstream is(cmd.argument);
1146 && (s == "append-row"
1147 || s == "delete-row"
1148 || s == "copy-row")) {
1149 status.message(bformat(
1150 N_("Can't change number of rows in '%1$s'"),
1152 status.enabled(false);
1156 && (s == "append-column"
1157 || s == "delete-column"
1158 || s == "copy-column")) {
1159 status.message(bformat(
1160 N_("Can't change number of columns in '%1$s'"),
1162 status.enabled(false);
1165 if ((type_ == "simple"
1166 || type_ == "equation"
1167 || type_ == "none") &&
1168 (s == "add-hline-above" || s == "add-hline-below")) {
1169 status.message(bformat(
1170 N_("Can't add horizontal grid lines in '%1$s'"),
1172 status.enabled(false);
1175 if (s == "add-vline-left" || s == "add-vline-right") {
1176 status.message(bformat(
1177 N_("Can't add vertical grid lines in '%1$s'"),
1179 status.enabled(false);
1182 if (s == "valign-top" || s == "valign-middle"
1183 || s == "valign-bottom" || s == "align-left"
1184 || s == "align-center" || s == "align-right") {
1185 status.enabled(false);
1188 return MathGridInset::getStatus(cur, cmd, status);
1191 return MathGridInset::getStatus(cur, cmd, status);
1196 /////////////////////////////////////////////////////////////////////
1198 #include "math_arrayinset.h"
1199 #include "math_deliminset.h"
1200 #include "math_factory.h"
1201 #include "math_parser.h"
1202 #include "math_spaceinset.h"
1203 #include "ref_inset.h"
1205 #include "bufferview_funcs.h"
1206 #include "lyxtext.h"
1208 #include "frontends/LyXView.h"
1209 #include "frontends/Dialogs.h"
1211 #include "support/lyxlib.h"
1214 // simply scrap this function if you want
1215 void MathHullInset::mutateToText()
1218 // translate to latex
1220 latex(NULL, os, false, false);
1221 string str = os.str();
1224 LyXText * lt = view_->getLyXText();
1225 string::const_iterator cit = str.begin();
1226 string::const_iterator end = str.end();
1227 for (; cit != end; ++cit)
1228 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
1231 //view_->owner()->dispatch(LFUN_ESCAPE);
1236 void MathHullInset::handleFont(LCursor & cur, string const & arg,
1237 string const & font)
1239 // this whole function is a hack and won't work for incremental font
1242 if (cur.inset().asMathInset()->name() == font)
1243 cur.handleFont(font);
1245 cur.handleNest(createMathInset(font));
1251 void MathHullInset::handleFont2(LCursor & cur, string const & arg)
1256 bv_funcs::string2font(arg, font, b);
1257 if (font.color() != LColor::inherit) {
1258 MathAtom at = MathAtom(new MathColorInset(true, font.color()));
1259 cur.handleNest(at, 0);
1264 void MathHullInset::edit(LCursor & cur, bool left)
1267 left ? idxFirst(cur) : idxLast(cur);
1271 string const MathHullInset::editMessage() const
1273 return _("Math editor mode");
1277 void MathHullInset::revealCodes(LCursor & cur) const
1279 if (!cur.inMathed())
1283 cur.message(os.str());
1285 // write something to the minibuffer
1286 // translate to latex
1290 string str = os.str();
1292 string::size_type pos = 0;
1294 for (string::iterator it = str.begin(); it != str.end(); ++it) {
1297 else if (*it == '\0') {
1299 pos = it - str.begin();
1305 res = res.substr(pos - 30);
1306 if (res.size() > 60)
1307 res = res.substr(0, 60);
1313 InsetBase::Code MathHullInset::lyxCode() const
1319 /////////////////////////////////////////////////////////////////////
1323 bool MathHullInset::searchForward(BufferView * bv, string const & str,
1326 #ifdef WITH_WARNINGS
1327 #warning completely broken
1329 static MathHullInset * lastformula = 0;
1330 static CursorBase current = DocIterator(ibegin(nucleus()));
1331 static MathArray ar;
1332 static string laststr;
1334 if (lastformula != this || laststr != str) {
1335 //lyxerr << "reset lastformula to " << this << endl;
1338 current = ibegin(nucleus());
1340 mathed_parse_cell(ar, str);
1344 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
1346 for (DocIterator it = current; it != iend(nucleus()); increment(it)) {
1347 CursorSlice & top = it.back();
1348 MathArray const & a = top.asMathInset()->cell(top.idx_);
1349 if (a.matchpart(ar, top.pos_)) {
1350 bv->cursor().setSelection(it, ar.size());
1352 top.pos_ += ar.size();
1358 //lyxerr << "not found!" << endl;
1365 void MathHullInset::write(Buffer const &, std::ostream & os) const
1367 WriteStream wi(os, false, false);
1373 void MathHullInset::read(Buffer const &, LyXLex & lex)
1376 mathed_parse_normal(at, lex);
1377 operator=(*at->asHullInset());
1381 int MathHullInset::plaintext(Buffer const &, ostream & os,
1382 OutputParams const &) const
1384 if (0 && display()) {
1388 TextPainter tpain(dim.width(), dim.height());
1389 drawT(tpain, 0, dim.ascent());
1391 // reset metrics cache to "real" values
1393 return tpain.textheight();
1395 WriteStream wi(os, false, true);
1402 int MathHullInset::linuxdoc(Buffer const & buf, ostream & os,
1403 OutputParams const & runparams) const
1405 return docbook(buf, os, runparams);
1409 int MathHullInset::docbook(Buffer const & buf, ostream & os,
1410 OutputParams const & runparams) const
1412 MathMLStream ms(os);
1415 if (getType() == "simple")
1416 name= "inlineequation";
1418 name = "informalequation";
1420 string bname = name;
1421 if (!label(0).empty())
1422 bname += " id=\"" + sgml::cleanID(buf, runparams, label(0)) + "\"";
1423 ms << MTag(bname.c_str());
1426 if (runparams.flavor == OutputParams::XML) {
1427 ms << MTag("alt role=\"tex\" ");
1428 // Workaround for db2latex: db2latex always includes equations with
1429 // \ensuremath{} or \begin{display}\end{display}
1430 // so we strip LyX' math environment
1431 WriteStream wi(ls, false, false);
1432 MathGridInset::write(wi);
1433 ms << subst(subst(ls.str(), "&", "&"), "<", "<");
1436 MathGridInset::mathmlize(ms);
1439 ms << MTag("alt role=\"tex\"");
1440 res = latex(buf, ls, runparams);
1441 ms << subst(subst(ls.str(), "&", "&"), "<", "<");
1445 ms << "<graphic fileref=\"eqn/";
1446 if ( !label(0).empty())
1447 ms << sgml::cleanID(buf, runparams, label(0));
1449 ms << sgml::uniqueID("anon");
1451 if (runparams.flavor == OutputParams::XML)
1456 ms << ETag(name.c_str());
1457 return ms.line() + res;