2 * \file InsetMathHull.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 "InsetMathArray.h"
14 #include "InsetMathChar.h"
15 #include "InsetMathColor.h"
17 #include "InsetMathDelim.h"
18 #include "MathExtern.h"
19 #include "MathFactory.h"
20 #include "InsetMathHull.h"
21 #include "MathMLStream.h"
22 #include "MathParser.h"
23 #include "InsetMathSpace.h"
24 #include "MathStream.h"
25 #include "MathSupport.h"
26 #include "InsetMathRef.h"
28 #include "bufferview_funcs.h"
32 #include "bufferparams.h"
33 #include "BufferView.h"
34 #include "CutAndPaste.h"
35 #include "FuncStatus.h"
37 #include "LaTeXFeatures.h"
40 #include "dispatchresult.h"
41 #include "funcrequest.h"
45 #include "outputparams.h"
47 #include "TextPainter.h"
50 #include "insets/render_preview.h"
51 #include "insets/insetlabel.h"
53 #include "graphics/PreviewImage.h"
54 #include "graphics/PreviewLoader.h"
56 #include "support/lyxlib.h"
57 #include "support/lstrings.h"
59 #include <boost/bind.hpp>
64 using lyx::cap::grabAndEraseSelection;
65 using lyx::support::bformat;
66 using lyx::support::subst;
73 using std::istringstream;
75 using std::ostringstream;
83 int getCols(HullType type)
100 // returns position of first relation operator in the array
101 // used for "intelligent splitting"
102 size_t firstRelOp(MathArray const & ar)
104 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
105 if ((*it)->isRelOp())
106 return it - ar.begin();
111 char const * star(bool numbered)
113 return numbered ? "" : "*";
117 } // end anon namespace
120 HullType hullType(std::string const & s)
122 if (s == "none") return hullNone;
123 if (s == "simple") return hullSimple;
124 if (s == "equation") return hullEquation;
125 if (s == "eqnarray") return hullEqnArray;
126 if (s == "align") return hullAlign;
127 if (s == "alignat") return hullAlignAt;
128 if (s == "xalignat") return hullXAlignAt;
129 if (s == "xxalignat") return hullXXAlignAt;
130 if (s == "multline") return hullMultline;
131 if (s == "gather") return hullGather;
132 if (s == "flalign") return hullFlAlign;
133 lyxerr << "unknown hull type '" << s << "'" << endl;
138 std::string hullName(HullType type)
141 case hullNone: return "none";
142 case hullSimple: return "simple";
143 case hullEquation: return "equation";
144 case hullEqnArray: return "eqnarray";
145 case hullAlign: return "align";
146 case hullAlignAt: return "alignat";
147 case hullXAlignAt: return "xalignat";
148 case hullXXAlignAt: return "xxalignat";
149 case hullMultline: return "multline";
150 case hullGather: return "gather";
151 case hullFlAlign: return "flalign";
153 lyxerr << "unknown hull type '" << type << "'" << endl;
159 InsetMathHull::InsetMathHull()
160 : InsetMathGrid(1, 1), type_(hullNone), nonum_(1), label_(1),
161 preview_(new RenderPreview(this))
163 //lyxerr << "sizeof InsetMath: " << sizeof(InsetMath) << endl;
164 //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl;
165 //lyxerr << "sizeof InsetMathChar: " << sizeof(InsetMathChar) << endl;
166 //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << endl;
172 InsetMathHull::InsetMathHull(HullType type)
173 : InsetMathGrid(getCols(type), 1), type_(type), nonum_(1), label_(1),
174 preview_(new RenderPreview(this))
181 InsetMathHull::InsetMathHull(InsetMathHull const & other)
182 : InsetMathGrid(other),
183 type_(other.type_), nonum_(other.nonum_), label_(other.label_),
184 preview_(new RenderPreview(this))
188 InsetMathHull::~InsetMathHull()
192 auto_ptr<InsetBase> InsetMathHull::doClone() const
194 return auto_ptr<InsetBase>(new InsetMathHull(*this));
198 InsetMathHull & InsetMathHull::operator=(InsetMathHull const & other)
202 *static_cast<InsetMathGrid*>(this) = InsetMathGrid(other);
204 nonum_ = other.nonum_;
205 label_ = other.label_;
206 preview_.reset(new RenderPreview(*other.preview_, this));
212 InsetBase * InsetMathHull::editXY(LCursor & cur, int x, int y)
218 return InsetMathNest::editXY(cur, x, y);
222 InsetMath::mode_type InsetMathHull::currentMode() const
224 if (type_ == hullNone)
225 return UNDECIDED_MODE;
226 // definitely math mode ...
231 bool InsetMathHull::idxFirst(LCursor & cur) const
239 bool InsetMathHull::idxLast(LCursor & cur) const
241 cur.idx() = nargs() - 1;
242 cur.pos() = cur.lastpos();
247 char InsetMathHull::defaultColAlign(col_type col)
249 if (type_ == hullEqnArray)
251 if (type_ >= hullAlign)
252 return "rl"[col & 1];
257 int InsetMathHull::defaultColSpace(col_type col)
259 if (type_ == hullAlign || type_ == hullAlignAt)
261 if (type_ == hullXAlignAt)
262 return (col & 1) ? 20 : 0;
263 if (type_ == hullXXAlignAt || type_ == hullFlAlign)
264 return (col & 1) ? 40 : 0;
269 char const * InsetMathHull::standardFont() const
271 return type_ == hullNone ? "lyxnochange" : "mathnormal";
275 bool InsetMathHull::previewState(BufferView * bv) const
277 if (!editing(bv) && RenderPreview::status() == LyXRC::PREVIEW_ON) {
278 lyx::graphics::PreviewImage const * pimage =
279 preview_->getPreviewImage(*bv->buffer());
280 return pimage && pimage->image();
286 void InsetMathHull::metrics(MetricsInfo & mi, Dimension & dim) const
288 if (previewState(mi.base.bv)) {
289 preview_->metrics(mi, dim);
290 // insert a one pixel gap in front of the formula
293 dim.des += displayMargin();
298 FontSetChanger dummy1(mi.base, standardFont());
299 StyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
301 // let the cells adjust themselves
302 InsetMathGrid::metrics(mi, dim);
305 dim.asc += displayMargin();
306 dim.des += displayMargin();
309 if (numberedType()) {
310 FontSetChanger dummy(mi.base, "mathbf");
312 for (row_type row = 0; row < nrows(); ++row)
313 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
319 // make it at least as high as the current font
322 math_font_max_dim(mi.base.font, asc, des);
323 dim.asc = max(dim.asc, asc);
324 dim.des = max(dim.des, des);
330 void InsetMathHull::draw(PainterInfo & pi, int x, int y) const
332 use_preview_ = previewState(pi.base.bv);
335 // one pixel gap in front
336 preview_->draw(pi, x + 1, y);
337 setPosCache(pi, x, y);
341 FontSetChanger dummy1(pi.base, standardFont());
342 StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
343 InsetMathGrid::draw(pi, x + 1, y);
345 if (numberedType()) {
346 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
347 for (row_type row = 0; row < nrows(); ++row) {
348 int const yy = y + rowinfo_[row].offset_;
349 FontSetChanger dummy(pi.base, "mathrm");
350 string const nl = nicelabel(row);
351 docstring const dnl(nl.begin(), nl.end());
352 pi.draw(xx, yy, dnl);
355 setPosCache(pi, x, y);
359 void InsetMathHull::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
362 InsetMathGrid::metricsT(mi, dim);
365 WriteStream wi(os, false, true);
367 dim.wid = os.str().size();
374 void InsetMathHull::drawT(TextPainter & pain, int x, int y) const
377 InsetMathGrid::drawT(pain, x, y);
380 WriteStream wi(os, false, true);
382 pain.draw(x, y, os.str().c_str());
389 string const latex_string(InsetMathHull const & inset)
392 WriteStream wi(ls, false, false);
400 void InsetMathHull::addPreview(lyx::graphics::PreviewLoader & ploader) const
402 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
403 string const snippet = latex_string(*this);
404 preview_->addPreview(snippet, ploader);
409 bool InsetMathHull::notifyCursorLeaves(LCursor & cur)
411 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
412 Buffer const & buffer = cur.buffer();
413 string const snippet = latex_string(*this);
414 preview_->addPreview(snippet, buffer);
415 preview_->startLoading(buffer);
421 string InsetMathHull::label(row_type row) const
423 BOOST_ASSERT(row < nrows());
428 void InsetMathHull::label(row_type row, string const & label)
430 //lyxerr << "setting label '" << label << "' for row " << row << endl;
435 void InsetMathHull::numbered(row_type row, bool num)
441 bool InsetMathHull::numbered(row_type row) const
447 bool InsetMathHull::ams() const
450 type_ == hullAlign ||
451 type_ == hullFlAlign ||
452 type_ == hullMultline ||
453 type_ == hullGather ||
454 type_ == hullAlignAt ||
455 type_ == hullXAlignAt ||
456 type_ == hullXXAlignAt;
460 bool InsetMathHull::display() const
462 return type_ != hullSimple && type_ != hullNone;
466 void InsetMathHull::getLabelList(Buffer const &, vector<string> & labels) const
468 for (row_type row = 0; row < nrows(); ++row)
469 if (!label_[row].empty() && nonum_[row] != 1)
470 labels.push_back(label_[row]);
474 bool InsetMathHull::numberedType() const
476 if (type_ == hullNone)
478 if (type_ == hullSimple)
480 if (type_ == hullXXAlignAt)
482 for (row_type row = 0; row < nrows(); ++row)
489 void InsetMathHull::validate(LaTeXFeatures & features) const
492 features.require("amsmath");
495 // Validation is necessary only if not using AMS math.
496 // To be safe, we will always run mathedvalidate.
497 //if (features.amsstyle)
500 features.require("boldsymbol");
501 //features.binom = true;
503 InsetMathGrid::validate(features);
507 void InsetMathHull::header_write(WriteStream & os) const
509 bool n = numberedType();
511 if (type_ == hullNone)
514 else if (type_ == hullSimple) {
520 else if (type_ == hullEquation) {
522 os << "\\begin{equation" << star(n) << "}\n";
527 else if (type_ == hullEqnArray || type_ == hullAlign || type_ ==
529 || type_ == hullGather || type_ == hullMultline)
530 os << "\\begin{" << type_ << star(n) << "}\n";
532 else if (type_ == hullAlignAt || type_ == hullXAlignAt)
533 os << "\\begin{" << type_ << star(n) << '}'
534 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
536 else if (type_ == hullXXAlignAt)
537 os << "\\begin{" << type_ << '}'
538 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
541 os << "\\begin{unknown" << star(n) << '}';
545 void InsetMathHull::footer_write(WriteStream & os) const
547 bool n = numberedType();
549 if (type_ == hullNone)
552 else if (type_ == hullSimple)
555 else if (type_ == hullEquation)
557 os << "\\end{equation" << star(n) << "}\n";
561 else if (type_ == hullEqnArray || type_ == hullAlign || type_ ==
563 || type_ == hullAlignAt || type_ == hullXAlignAt
564 || type_ == hullGather || type_ == hullMultline)
565 os << "\\end{" << type_ << star(n) << "}\n";
567 else if (type_ == hullXXAlignAt)
568 os << "\\end{" << type_ << "}\n";
571 os << "\\end{unknown" << star(n) << '}';
575 bool InsetMathHull::rowChangeOK() const
578 type_ == hullEqnArray || type_ == hullAlign ||
579 type_ == hullFlAlign || type_ == hullAlignAt ||
580 type_ == hullXAlignAt || type_ == hullXXAlignAt ||
581 type_ == hullGather || type_ == hullMultline;
585 bool InsetMathHull::colChangeOK() const
588 type_ == hullAlign || type_ == hullFlAlign ||type_ == hullAlignAt ||
589 type_ == hullXAlignAt || type_ == hullXXAlignAt;
593 void InsetMathHull::addRow(row_type row)
597 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
598 label_.insert(label_.begin() + row + 1, string());
599 InsetMathGrid::addRow(row);
603 void InsetMathHull::swapRow(row_type row)
607 if (row + 1 == nrows())
609 swap(nonum_[row], nonum_[row + 1]);
610 swap(label_[row], label_[row + 1]);
611 InsetMathGrid::swapRow(row);
615 void InsetMathHull::delRow(row_type row)
617 if (nrows() <= 1 || !rowChangeOK())
619 InsetMathGrid::delRow(row);
620 // The last dummy row has no number info nor a label.
621 // Test nrows() + 1 because we have already erased the row.
622 if (row == nrows() + 1)
624 nonum_.erase(nonum_.begin() + row);
625 label_.erase(label_.begin() + row);
629 void InsetMathHull::addCol(col_type col)
633 InsetMathGrid::addCol(col);
637 void InsetMathHull::delCol(col_type col)
639 if (ncols() <= 1 || !colChangeOK())
641 InsetMathGrid::delCol(col);
645 string InsetMathHull::nicelabel(row_type row) const
649 if (label_[row].empty())
650 return string("(#)");
651 return '(' + label_[row] + ')';
655 void InsetMathHull::glueall()
658 for (idx_type i = 0; i < nargs(); ++i)
660 *this = InsetMathHull(hullSimple);
666 void InsetMathHull::splitTo2Cols()
668 BOOST_ASSERT(ncols() == 1);
669 InsetMathGrid::addCol(1);
670 for (row_type row = 0; row < nrows(); ++row) {
671 idx_type const i = 2 * row;
672 pos_type pos = firstRelOp(cell(i));
673 cell(i + 1) = MathArray(cell(i).begin() + pos, cell(i).end());
674 cell(i).erase(pos, cell(i).size());
679 void InsetMathHull::splitTo3Cols()
681 BOOST_ASSERT(ncols() < 3);
684 InsetMathGrid::addCol(1);
685 for (row_type row = 0; row < nrows(); ++row) {
686 idx_type const i = 3 * row + 1;
687 if (cell(i).size()) {
688 cell(i + 1) = MathArray(cell(i).begin() + 1, cell(i).end());
689 cell(i).erase(1, cell(i).size());
695 void InsetMathHull::changeCols(col_type cols)
699 else if (ncols() < cols) {
705 while (ncols() < cols)
706 InsetMathGrid::addCol(ncols() - 1);
712 for (row_type row = 0; row < nrows(); ++row) {
713 idx_type const i = row * ncols();
714 for (col_type col = cols; col < ncols(); ++col) {
715 cell(i + cols - 1).append(cell(i + col));
719 while (ncols() > cols) {
720 InsetMathGrid::delCol(ncols() - 1);
725 HullType InsetMathHull::getType() const
731 void InsetMathHull::setType(HullType type)
738 void InsetMathHull::mutate(HullType newtype)
740 //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
742 // we try to move along the chain
743 // none <-> simple <-> equation <-> eqnarray -> *align* -> multline, gather -+
745 // +-------------------------------------+
746 // we use eqnarray as intermediate type for mutations that are not
747 // directly supported because it handles labels and numbering for
750 if (newtype == type_) {
754 else if (newtype < hullNone) {
759 else if (type_ == hullNone) {
765 else if (type_ == hullSimple) {
766 if (newtype == hullNone) {
770 setType(hullEquation);
776 else if (type_ == hullEquation) {
777 if (newtype < type_) {
781 } else if (newtype == hullEqnArray) {
782 // split it "nicely" on the first relop
784 setType(hullEqnArray);
785 } else if (newtype == hullMultline || newtype == hullGather) {
795 else if (type_ == hullEqnArray) {
796 if (newtype < type_) {
797 // set correct (no)numbering
798 bool allnonum = true;
799 for (row_type row = 0; row < nrows(); ++row)
803 // set first non-empty label
805 for (row_type row = 0; row < nrows(); ++row) {
806 if (!label_[row].empty()) {
813 nonum_[0] = allnonum;
816 } else { // align & Co.
823 else if (type_ == hullAlign || type_ == hullAlignAt ||
824 type_ == hullXAlignAt || type_ == hullFlAlign) {
825 if (newtype < hullAlign) {
827 setType(hullEqnArray);
829 } else if (newtype == hullGather || newtype == hullMultline) {
832 } else if (newtype == hullXXAlignAt) {
833 for (row_type row = 0; row < nrows(); ++row)
834 numbered(row, false);
841 else if (type_ == hullXXAlignAt) {
842 for (row_type row = 0; row < nrows(); ++row)
843 numbered(row, false);
844 if (newtype < hullAlign) {
846 setType(hullEqnArray);
848 } else if (newtype == hullGather || newtype == hullMultline) {
856 else if (type_ == hullMultline || type_ == hullGather) {
857 if (newtype == hullGather || newtype == hullMultline)
859 else if (newtype == hullAlign || newtype == hullFlAlign ||
860 newtype == hullAlignAt || newtype == hullXAlignAt) {
863 } else if (newtype == hullXXAlignAt) {
865 for (row_type row = 0; row < nrows(); ++row)
866 numbered(row, false);
870 setType(hullEqnArray);
876 lyxerr << "mutation from '" << type_
877 << "' to '" << newtype << "' not implemented" << endl;
882 string InsetMathHull::eolString(row_type row, bool emptyline, bool fragile) const
885 if (numberedType()) {
886 if (!label_[row].empty() && !nonum_[row])
887 res += "\\label{" + label_[row] + '}';
888 if (nonum_[row] && (type_ != hullMultline))
889 res += "\\nonumber ";
891 return res + InsetMathGrid::eolString(row, emptyline, fragile);
895 void InsetMathHull::write(WriteStream & os) const
898 InsetMathGrid::write(os);
903 void InsetMathHull::normalize(NormalStream & os) const
905 os << "[formula " << type_ << ' ';
906 InsetMathGrid::normalize(os);
911 void InsetMathHull::mathmlize(MathMLStream & os) const
913 InsetMathGrid::mathmlize(os);
917 void InsetMathHull::infoize(ostream & os) const
919 os << "Type: " << type_;
923 void InsetMathHull::check() const
925 BOOST_ASSERT(nonum_.size() == nrows());
926 BOOST_ASSERT(label_.size() == nrows());
930 void InsetMathHull::doExtern(LCursor & cur, FuncRequest & func)
934 istringstream iss(lyx::to_utf8(func.argument()));
935 iss >> lang >> extra;
940 #warning temporarily disabled
941 //if (cur.selection()) {
944 // lyxerr << "use selection: " << ar << endl;
945 // insert(pipeThroughExtern(lang, extra, ar));
951 eq.push_back(MathAtom(new InsetMathChar('=')));
953 // go to first item in line
954 cur.idx() -= cur.idx() % ncols();
957 if (getType() == hullSimple) {
958 size_type pos = cur.cell().find_last(eq);
960 if (cur.inMathed() && cur.selection()) {
961 asArray(grabAndEraseSelection(cur), ar);
962 } else if (pos == cur.cell().size()) {
964 lyxerr << "use whole cell: " << ar << endl;
966 ar = MathArray(cur.cell().begin() + pos + 1, cur.cell().end());
967 lyxerr << "use partial cell form pos: " << pos << endl;
969 cur.cell().append(eq);
970 cur.cell().append(pipeThroughExtern(lang, extra, ar));
971 cur.pos() = cur.lastpos();
975 if (getType() == hullEquation) {
976 lyxerr << "use equation inset" << endl;
977 mutate(hullEqnArray);
978 MathArray & ar = cur.cell();
979 lyxerr << "use cell: " << ar << endl;
983 cur.cell() = pipeThroughExtern(lang, extra, ar);
984 // move to end of line
985 cur.pos() = cur.lastpos();
990 lyxerr << "use eqnarray" << endl;
991 cur.idx() += 2 - cur.idx() % ncols();
993 MathArray ar = cur.cell();
994 lyxerr << "use cell: " << ar << endl;
996 #warning temporarily disabled
1003 cur.cell() = pipeThroughExtern(lang, extra, ar);
1004 cur.pos() = cur.lastpos();
1009 void InsetMathHull::doDispatch(LCursor & cur, FuncRequest & cmd)
1011 //lyxerr << "action: " << cmd.action << endl;
1012 switch (cmd.action) {
1014 case LFUN_FINISHED_LEFT:
1015 case LFUN_FINISHED_RIGHT:
1016 case LFUN_FINISHED_UP:
1017 case LFUN_FINISHED_DOWN:
1018 //lyxerr << "action: " << cmd.action << endl;
1019 InsetMathGrid::doDispatch(cur, cmd);
1020 notifyCursorLeaves(cur);
1024 case LFUN_BREAK_PARAGRAPH:
1025 // just swallow this
1028 case LFUN_BREAK_LINE:
1029 // some magic for the common case
1030 if (type_ == hullSimple || type_ == hullEquation) {
1031 recordUndoInset(cur);
1033 cur.bv().buffer()->params().use_amsmath == BufferParams::AMS_ON;
1034 mutate(align ? hullAlign : hullEqnArray);
1036 cur.pos() = cur.lastpos();
1038 InsetMathGrid::doDispatch(cur, cmd);
1041 case LFUN_MATH_NUMBER:
1042 //lyxerr << "toggling all numbers" << endl;
1044 recordUndoInset(cur);
1045 bool old = numberedType();
1046 if (type_ == hullMultline)
1047 numbered(nrows() - 1, !old);
1049 for (row_type row = 0; row < nrows(); ++row)
1050 numbered(row, !old);
1052 cur.message(old ? _("No number") : _("Number"));
1056 case LFUN_MATH_NONUMBER:
1058 recordUndoInset(cur);
1059 row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1060 bool old = numbered(r);
1061 cur.message(old ? _("No number") : _("Number"));
1066 case LFUN_LABEL_INSERT: {
1067 recordUndoInset(cur);
1068 row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1069 string old_label = label(r);
1070 string const default_label =
1071 (lyxrc.label_init_length >= 0) ? "eq:" : "";
1072 if (old_label.empty())
1073 old_label = default_label;
1074 string const contents = cmd.argument().empty() ?
1075 old_label : lyx::to_utf8(cmd.argument());
1077 InsetCommandParams p("label", contents);
1078 string const data = InsetCommandMailer::params2string("label", p);
1080 if (cmd.argument().empty())
1081 cur.bv().showInsetDialog("label", data, 0);
1083 FuncRequest fr(LFUN_INSET_INSERT, data);
1089 case LFUN_INSET_INSERT: {
1090 //lyxerr << "arg: " << lyx::to_utf8(cmd.argument()) << endl;
1091 string const name = cmd.getArg(0);
1092 if (name == "label") {
1093 InsetCommandParams p;
1094 InsetCommandMailer::string2params(name, lyx::to_utf8(cmd.argument()), p);
1095 string str = p.getContents();
1096 recordUndoInset(cur);
1097 row_type const r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1098 str = lyx::support::trim(str);
1101 string old = label(r);
1103 cur.bv().buffer()->changeRefsIfUnique(old, str);
1108 InsetMathGrid::doDispatch(cur, cmd);
1112 case LFUN_MATH_EXTERN:
1113 recordUndoInset(cur);
1117 case LFUN_MATH_MUTATE: {
1118 recordUndoInset(cur);
1119 row_type row = cur.row();
1120 col_type col = cur.col();
1121 mutate(hullType(lyx::to_utf8(cmd.argument())));
1122 cur.idx() = row * ncols() + col;
1123 if (cur.idx() > cur.lastidx()) {
1124 cur.idx() = cur.lastidx();
1125 cur.pos() = cur.lastpos();
1127 if (cur.pos() > cur.lastpos())
1128 cur.pos() = cur.lastpos();
1129 //cur.dispatched(FINISHED);
1133 case LFUN_MATH_DISPLAY: {
1134 recordUndoInset(cur);
1135 mutate(type_ == hullSimple ? hullEquation : hullSimple);
1137 cur.pos() = cur.lastpos();
1138 //cur.dispatched(FINISHED);
1143 InsetMathGrid::doDispatch(cur, cmd);
1149 bool InsetMathHull::getStatus(LCursor & cur, FuncRequest const & cmd,
1150 FuncStatus & status) const
1152 switch (cmd.action) {
1153 case LFUN_FINISHED_LEFT:
1154 case LFUN_FINISHED_RIGHT:
1155 case LFUN_FINISHED_UP:
1156 case LFUN_FINISHED_DOWN:
1157 status.enabled(true);
1159 case LFUN_BREAK_LINE:
1160 case LFUN_MATH_NUMBER:
1161 case LFUN_MATH_NONUMBER:
1162 case LFUN_MATH_EXTERN:
1163 case LFUN_MATH_MUTATE:
1164 case LFUN_MATH_DISPLAY:
1166 status.enabled(true);
1168 case LFUN_LABEL_INSERT:
1169 status.enabled(type_ != hullSimple);
1171 case LFUN_INSET_INSERT:
1172 if (cmd.getArg(0) == "label") {
1173 status.enabled(type_ != hullSimple);
1176 return InsetMathGrid::getStatus(cur, cmd, status);
1177 case LFUN_TABULAR_FEATURE: {
1178 istringstream is(lyx::to_utf8(cmd.argument()));
1182 && (s == "append-row"
1183 || s == "delete-row"
1184 || s == "copy-row")) {
1185 status.message(bformat(
1186 lyx::from_utf8(N_("Can't change number of rows in '%1$s'")),
1187 lyx::from_utf8(hullName(type_))));
1188 status.enabled(false);
1192 && (s == "append-column"
1193 || s == "delete-column"
1194 || s == "copy-column")) {
1195 status.message(bformat(
1196 lyx::from_utf8(N_("Can't change number of columns in '%1$s'")),
1197 lyx::from_utf8(hullName(type_))));
1198 status.enabled(false);
1201 if ((type_ == hullSimple
1202 || type_ == hullEquation
1203 || type_ == hullNone) &&
1204 (s == "add-hline-above" || s == "add-hline-below")) {
1205 status.message(bformat(
1206 lyx::from_utf8(N_("Can't add horizontal grid lines in '%1$s'")),
1207 lyx::from_utf8(hullName(type_))));
1208 status.enabled(false);
1211 if (s == "add-vline-left" || s == "add-vline-right") {
1212 status.message(bformat(
1213 lyx::from_utf8(N_("Can't add vertical grid lines in '%1$s'")),
1214 lyx::from_utf8(hullName(type_))));
1215 status.enabled(false);
1218 if (s == "valign-top" || s == "valign-middle"
1219 || s == "valign-bottom" || s == "align-left"
1220 || s == "align-center" || s == "align-right") {
1221 status.enabled(false);
1224 return InsetMathGrid::getStatus(cur, cmd, status);
1227 return InsetMathGrid::getStatus(cur, cmd, status);
1230 // This cannot really happen, but inserted to shut-up gcc
1231 return InsetMathGrid::getStatus(cur, cmd, status);
1235 /////////////////////////////////////////////////////////////////////
1239 // simply scrap this function if you want
1240 void InsetMathHull::mutateToText()
1243 // translate to latex
1245 latex(NULL, os, false, false);
1246 string str = os.str();
1249 LyXText * lt = view_->getLyXText();
1250 string::const_iterator cit = str.begin();
1251 string::const_iterator end = str.end();
1252 for (; cit != end; ++cit)
1253 view_->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
1256 //theApp->lyxFunc().dispatch(LFUN_ESCAPE);
1261 void InsetMathHull::handleFont(LCursor & cur, string const & arg,
1262 string const & font)
1264 // this whole function is a hack and won't work for incremental font
1267 if (cur.inset().asInsetMath()->name() == font)
1268 cur.handleFont(font);
1270 cur.handleNest(createInsetMath(font));
1276 void InsetMathHull::handleFont2(LCursor & cur, string const & arg)
1281 bv_funcs::string2font(arg, font, b);
1282 if (font.color() != LColor::inherit) {
1283 MathAtom at = MathAtom(new InsetMathColor(true, font.color()));
1284 cur.handleNest(at, 0);
1289 void InsetMathHull::edit(LCursor & cur, bool left)
1292 left ? idxFirst(cur) : idxLast(cur);
1296 docstring const InsetMathHull::editMessage() const
1298 return _("Math editor mode");
1302 void InsetMathHull::revealCodes(LCursor & cur) const
1304 if (!cur.inMathed())
1308 cur.message(lyx::from_utf8(os.str()));
1310 // write something to the minibuffer
1311 // translate to latex
1315 string str = os.str();
1317 string::size_type pos = 0;
1319 for (string::iterator it = str.begin(); it != str.end(); ++it) {
1322 else if (*it == '\0') {
1324 pos = it - str.begin();
1330 res = res.substr(pos - 30);
1331 if (res.size() > 60)
1332 res = res.substr(0, 60);
1338 InsetBase::Code InsetMathHull::lyxCode() const
1344 /////////////////////////////////////////////////////////////////////
1348 bool InsetMathHull::searchForward(BufferView * bv, string const & str,
1351 #ifdef WITH_WARNINGS
1352 #warning completely broken
1354 static InsetMathHull * lastformula = 0;
1355 static CursorBase current = DocIterator(ibegin(nucleus()));
1356 static MathArray ar;
1357 static string laststr;
1359 if (lastformula != this || laststr != str) {
1360 //lyxerr << "reset lastformula to " << this << endl;
1363 current = ibegin(nucleus());
1365 mathed_parse_cell(ar, str);
1369 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
1371 for (DocIterator it = current; it != iend(nucleus()); increment(it)) {
1372 CursorSlice & top = it.back();
1373 MathArray const & a = top.asInsetMath()->cell(top.idx_);
1374 if (a.matchpart(ar, top.pos_)) {
1375 bv->cursor().setSelection(it, ar.size());
1377 top.pos_ += ar.size();
1383 //lyxerr << "not found!" << endl;
1390 void InsetMathHull::write(Buffer const &, std::ostream & os) const
1392 WriteStream wi(os, false, false);
1398 void InsetMathHull::read(Buffer const &, LyXLex & lex)
1401 mathed_parse_normal(at, lex);
1402 operator=(*at->asHullInset());
1406 int InsetMathHull::plaintext(Buffer const &, ostream & os,
1407 OutputParams const &) const
1409 if (0 && display()) {
1413 TextPainter tpain(dim.width(), dim.height());
1414 drawT(tpain, 0, dim.ascent());
1416 // reset metrics cache to "real" values
1418 return tpain.textheight();
1420 WriteStream wi(os, false, true);
1427 int InsetMathHull::docbook(Buffer const & buf, ostream & os,
1428 OutputParams const & runparams) const
1430 MathMLStream ms(os);
1433 if (getType() == hullSimple)
1434 name = "inlineequation";
1436 name = "informalequation";
1438 string bname = name;
1439 if (!label(0).empty())
1440 bname += " id=\"" + sgml::cleanID(buf, runparams, label(0)) + "\"";
1441 ms << MTag(bname.c_str());
1444 if (runparams.flavor == OutputParams::XML) {
1445 ms << MTag("alt role=\"tex\" ");
1446 // Workaround for db2latex: db2latex always includes equations with
1447 // \ensuremath{} or \begin{display}\end{display}
1448 // so we strip LyX' math environment
1449 WriteStream wi(ls, false, false);
1450 InsetMathGrid::write(wi);
1451 ms << subst(subst(ls.str(), "&", "&"), "<", "<");
1454 InsetMathGrid::mathmlize(ms);
1457 ms << MTag("alt role=\"tex\"");
1458 res = latex(buf, ls, runparams);
1459 ms << subst(subst(ls.str(), "&", "&"), "<", "<");
1463 ms << "<graphic fileref=\"eqn/";
1464 if ( !label(0).empty())
1465 ms << sgml::cleanID(buf, runparams, label(0));
1467 ms << sgml::uniqueID("anon");
1469 if (runparams.flavor == OutputParams::XML)
1474 ms << ETag(name.c_str());
1475 return ms.line() + res;
1479 int InsetMathHull::textString(Buffer const & buf, ostream & os,
1480 OutputParams const & op) const
1482 return plaintext(buf, os, op);