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>
66 using cap::grabAndEraseSelection;
67 using support::bformat;
75 using std::istringstream;
77 using std::ostringstream;
85 int getCols(HullType type)
102 // returns position of first relation operator in the array
103 // used for "intelligent splitting"
104 size_t firstRelOp(MathArray const & ar)
106 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
107 if ((*it)->isRelOp())
108 return it - ar.begin();
113 char const * star(bool numbered)
115 return numbered ? "" : "*";
119 } // end anon namespace
122 HullType hullType(std::string const & s)
124 if (s == "none") return hullNone;
125 if (s == "simple") return hullSimple;
126 if (s == "equation") return hullEquation;
127 if (s == "eqnarray") return hullEqnArray;
128 if (s == "align") return hullAlign;
129 if (s == "alignat") return hullAlignAt;
130 if (s == "xalignat") return hullXAlignAt;
131 if (s == "xxalignat") return hullXXAlignAt;
132 if (s == "multline") return hullMultline;
133 if (s == "gather") return hullGather;
134 if (s == "flalign") return hullFlAlign;
135 lyxerr << "unknown hull type '" << s << "'" << endl;
140 std::string hullName(HullType type)
143 case hullNone: return "none";
144 case hullSimple: return "simple";
145 case hullEquation: return "equation";
146 case hullEqnArray: return "eqnarray";
147 case hullAlign: return "align";
148 case hullAlignAt: return "alignat";
149 case hullXAlignAt: return "xalignat";
150 case hullXXAlignAt: return "xxalignat";
151 case hullMultline: return "multline";
152 case hullGather: return "gather";
153 case hullFlAlign: return "flalign";
155 lyxerr << "unknown hull type '" << type << "'" << endl;
161 InsetMathHull::InsetMathHull()
162 : InsetMathGrid(1, 1), type_(hullNone), nonum_(1), label_(1),
163 preview_(new RenderPreview(this))
165 //lyxerr << "sizeof InsetMath: " << sizeof(InsetMath) << endl;
166 //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl;
167 //lyxerr << "sizeof InsetMathChar: " << sizeof(InsetMathChar) << endl;
168 //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << endl;
174 InsetMathHull::InsetMathHull(HullType type)
175 : InsetMathGrid(getCols(type), 1), type_(type), nonum_(1), label_(1),
176 preview_(new RenderPreview(this))
183 InsetMathHull::InsetMathHull(InsetMathHull const & other)
184 : InsetMathGrid(other),
185 type_(other.type_), nonum_(other.nonum_), label_(other.label_),
186 preview_(new RenderPreview(this))
190 InsetMathHull::~InsetMathHull()
194 auto_ptr<InsetBase> InsetMathHull::doClone() const
196 return auto_ptr<InsetBase>(new InsetMathHull(*this));
200 InsetMathHull & InsetMathHull::operator=(InsetMathHull const & other)
204 *static_cast<InsetMathGrid*>(this) = InsetMathGrid(other);
206 nonum_ = other.nonum_;
207 label_ = other.label_;
208 preview_.reset(new RenderPreview(*other.preview_, this));
214 InsetBase * InsetMathHull::editXY(LCursor & cur, int x, int y)
220 return InsetMathNest::editXY(cur, x, y);
224 InsetMath::mode_type InsetMathHull::currentMode() const
226 if (type_ == hullNone)
227 return UNDECIDED_MODE;
228 // definitely math mode ...
233 bool InsetMathHull::idxFirst(LCursor & cur) const
241 bool InsetMathHull::idxLast(LCursor & cur) const
243 cur.idx() = nargs() - 1;
244 cur.pos() = cur.lastpos();
249 char InsetMathHull::defaultColAlign(col_type col)
251 if (type_ == hullEqnArray)
253 if (type_ >= hullAlign)
254 return "rl"[col & 1];
259 int InsetMathHull::defaultColSpace(col_type col)
261 if (type_ == hullAlign || type_ == hullAlignAt)
263 if (type_ == hullXAlignAt)
264 return (col & 1) ? 20 : 0;
265 if (type_ == hullXXAlignAt || type_ == hullFlAlign)
266 return (col & 1) ? 40 : 0;
271 char const * InsetMathHull::standardFont() const
273 return type_ == hullNone ? "lyxnochange" : "mathnormal";
277 bool InsetMathHull::previewState(BufferView * bv) const
279 if (!editing(bv) && RenderPreview::status() == LyXRC::PREVIEW_ON) {
280 graphics::PreviewImage const * pimage =
281 preview_->getPreviewImage(*bv->buffer());
282 return pimage && pimage->image();
288 void InsetMathHull::metrics(MetricsInfo & mi, Dimension & dim) const
290 if (previewState(mi.base.bv)) {
291 preview_->metrics(mi, dim);
292 // insert a one pixel gap in front of the formula
295 dim.des += displayMargin();
300 FontSetChanger dummy1(mi.base, standardFont());
301 StyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
303 // let the cells adjust themselves
304 InsetMathGrid::metrics(mi, dim);
307 dim.asc += displayMargin();
308 dim.des += displayMargin();
311 if (numberedType()) {
312 FontSetChanger dummy(mi.base, "mathbf");
314 for (row_type row = 0; row < nrows(); ++row)
315 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
321 // make it at least as high as the current font
324 math_font_max_dim(mi.base.font, asc, des);
325 dim.asc = max(dim.asc, asc);
326 dim.des = max(dim.des, des);
332 void InsetMathHull::draw(PainterInfo & pi, int x, int y) const
334 use_preview_ = previewState(pi.base.bv);
337 // one pixel gap in front
338 preview_->draw(pi, x + 1, y);
339 setPosCache(pi, x, y);
343 FontSetChanger dummy1(pi.base, standardFont());
344 StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
345 InsetMathGrid::draw(pi, x + 1, y);
347 if (numberedType()) {
348 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
349 for (row_type row = 0; row < nrows(); ++row) {
350 int const yy = y + rowinfo_[row].offset_;
351 FontSetChanger dummy(pi.base, "mathrm");
352 docstring const nl = nicelabel(row);
356 setPosCache(pi, x, y);
360 void InsetMathHull::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
363 InsetMathGrid::metricsT(mi, dim);
366 WriteStream wi(os, false, true);
368 dim.wid = os.str().size();
375 void InsetMathHull::drawT(TextPainter & pain, int x, int y) const
378 InsetMathGrid::drawT(pain, x, y);
381 WriteStream wi(os, false, true);
383 pain.draw(x, y, os.str().c_str());
390 docstring const latex_string(InsetMathHull const & inset)
393 WriteStream wi(ls, false, false);
401 void InsetMathHull::addPreview(graphics::PreviewLoader & ploader) const
403 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
404 docstring const snippet = latex_string(*this);
405 preview_->addPreview(snippet, ploader);
410 bool InsetMathHull::notifyCursorLeaves(LCursor & cur)
412 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
413 Buffer const & buffer = cur.buffer();
414 docstring const snippet = latex_string(*this);
415 preview_->addPreview(snippet, buffer);
416 preview_->startLoading(buffer);
422 string InsetMathHull::label(row_type row) const
424 BOOST_ASSERT(row < nrows());
429 void InsetMathHull::label(row_type row, string const & label)
431 //lyxerr << "setting label '" << label << "' for row " << row << endl;
436 void InsetMathHull::numbered(row_type row, bool num)
442 bool InsetMathHull::numbered(row_type row) const
448 bool InsetMathHull::ams() const
451 type_ == hullAlign ||
452 type_ == hullFlAlign ||
453 type_ == hullMultline ||
454 type_ == hullGather ||
455 type_ == hullAlignAt ||
456 type_ == hullXAlignAt ||
457 type_ == hullXXAlignAt;
461 bool InsetMathHull::display() const
463 return type_ != hullSimple && type_ != hullNone;
467 void InsetMathHull::getLabelList(Buffer const &, vector<docstring> & labels) const
469 for (row_type row = 0; row < nrows(); ++row)
470 if (!label_[row].empty() && nonum_[row] != 1)
472 labels.push_back(from_utf8(label_[row]));
476 bool InsetMathHull::numberedType() const
478 if (type_ == hullNone)
480 if (type_ == hullSimple)
482 if (type_ == hullXXAlignAt)
484 for (row_type row = 0; row < nrows(); ++row)
491 void InsetMathHull::validate(LaTeXFeatures & features) const
494 features.require("amsmath");
497 // Validation is necessary only if not using AMS math.
498 // To be safe, we will always run mathedvalidate.
499 //if (features.amsstyle)
502 features.require("boldsymbol");
503 //features.binom = true;
505 InsetMathGrid::validate(features);
509 void InsetMathHull::header_write(WriteStream & os) const
511 bool n = numberedType();
525 os << "\\begin{equation" << star(n) << "}\n";
535 os << "\\begin{" << hullName(type_) << star(n) << "}\n";
540 os << "\\begin{" << hullName(type_) << star(n) << '}'
541 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
545 os << "\\begin{" << hullName(type_) << '}'
546 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
550 os << "\\begin{unknown" << star(n) << '}';
556 void InsetMathHull::footer_write(WriteStream & os) const
558 bool n = numberedType();
571 os << "\\end{equation" << star(n) << "}\n";
583 os << "\\end{" << hullName(type_) << star(n) << "}\n";
587 os << "\\end{" << hullName(type_) << "}\n";
591 os << "\\end{unknown" << star(n) << '}';
597 bool InsetMathHull::rowChangeOK() const
600 type_ == hullEqnArray || type_ == hullAlign ||
601 type_ == hullFlAlign || type_ == hullAlignAt ||
602 type_ == hullXAlignAt || type_ == hullXXAlignAt ||
603 type_ == hullGather || type_ == hullMultline;
607 bool InsetMathHull::colChangeOK() const
610 type_ == hullAlign || type_ == hullFlAlign ||type_ == hullAlignAt ||
611 type_ == hullXAlignAt || type_ == hullXXAlignAt;
615 void InsetMathHull::addRow(row_type row)
619 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
620 label_.insert(label_.begin() + row + 1, string());
621 InsetMathGrid::addRow(row);
625 void InsetMathHull::swapRow(row_type row)
629 if (row + 1 == nrows())
631 swap(nonum_[row], nonum_[row + 1]);
632 swap(label_[row], label_[row + 1]);
633 InsetMathGrid::swapRow(row);
637 void InsetMathHull::delRow(row_type row)
639 if (nrows() <= 1 || !rowChangeOK())
641 InsetMathGrid::delRow(row);
642 // The last dummy row has no number info nor a label.
643 // Test nrows() + 1 because we have already erased the row.
644 if (row == nrows() + 1)
646 nonum_.erase(nonum_.begin() + row);
647 label_.erase(label_.begin() + row);
651 void InsetMathHull::addCol(col_type col)
655 InsetMathGrid::addCol(col);
659 void InsetMathHull::delCol(col_type col)
661 if (ncols() <= 1 || !colChangeOK())
663 InsetMathGrid::delCol(col);
667 docstring InsetMathHull::nicelabel(row_type row) const
671 if (label_[row].empty())
672 return from_ascii("(#)");
674 return from_utf8('(' + label_[row] + ')');
678 void InsetMathHull::glueall()
681 for (idx_type i = 0; i < nargs(); ++i)
683 *this = InsetMathHull(hullSimple);
689 void InsetMathHull::splitTo2Cols()
691 BOOST_ASSERT(ncols() == 1);
692 InsetMathGrid::addCol(1);
693 for (row_type row = 0; row < nrows(); ++row) {
694 idx_type const i = 2 * row;
695 pos_type pos = firstRelOp(cell(i));
696 cell(i + 1) = MathArray(cell(i).begin() + pos, cell(i).end());
697 cell(i).erase(pos, cell(i).size());
702 void InsetMathHull::splitTo3Cols()
704 BOOST_ASSERT(ncols() < 3);
707 InsetMathGrid::addCol(1);
708 for (row_type row = 0; row < nrows(); ++row) {
709 idx_type const i = 3 * row + 1;
710 if (cell(i).size()) {
711 cell(i + 1) = MathArray(cell(i).begin() + 1, cell(i).end());
712 cell(i).erase(1, cell(i).size());
718 void InsetMathHull::changeCols(col_type cols)
722 else if (ncols() < cols) {
728 while (ncols() < cols)
729 InsetMathGrid::addCol(ncols() - 1);
735 for (row_type row = 0; row < nrows(); ++row) {
736 idx_type const i = row * ncols();
737 for (col_type col = cols; col < ncols(); ++col) {
738 cell(i + cols - 1).append(cell(i + col));
742 while (ncols() > cols) {
743 InsetMathGrid::delCol(ncols() - 1);
748 HullType InsetMathHull::getType() const
754 void InsetMathHull::setType(HullType type)
761 void InsetMathHull::mutate(HullType newtype)
763 //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
765 // we try to move along the chain
766 // none <-> simple <-> equation <-> eqnarray -> *align* -> multline, gather -+
768 // +-------------------------------------+
769 // we use eqnarray as intermediate type for mutations that are not
770 // directly supported because it handles labels and numbering for
773 if (newtype == type_) {
777 else if (newtype < hullNone) {
782 else if (type_ == hullNone) {
788 else if (type_ == hullSimple) {
789 if (newtype == hullNone) {
793 setType(hullEquation);
799 else if (type_ == hullEquation) {
800 if (newtype < type_) {
804 } else if (newtype == hullEqnArray) {
805 // split it "nicely" on the first relop
807 setType(hullEqnArray);
808 } else if (newtype == hullMultline || newtype == hullGather) {
818 else if (type_ == hullEqnArray) {
819 if (newtype < type_) {
820 // set correct (no)numbering
821 bool allnonum = true;
822 for (row_type row = 0; row < nrows(); ++row)
826 // set first non-empty label
828 for (row_type row = 0; row < nrows(); ++row) {
829 if (!label_[row].empty()) {
836 nonum_[0] = allnonum;
839 } else { // align & Co.
846 else if (type_ == hullAlign || type_ == hullAlignAt ||
847 type_ == hullXAlignAt || type_ == hullFlAlign) {
848 if (newtype < hullAlign) {
850 setType(hullEqnArray);
852 } else if (newtype == hullGather || newtype == hullMultline) {
855 } else if (newtype == hullXXAlignAt) {
856 for (row_type row = 0; row < nrows(); ++row)
857 numbered(row, false);
864 else if (type_ == hullXXAlignAt) {
865 for (row_type row = 0; row < nrows(); ++row)
866 numbered(row, false);
867 if (newtype < hullAlign) {
869 setType(hullEqnArray);
871 } else if (newtype == hullGather || newtype == hullMultline) {
879 else if (type_ == hullMultline || type_ == hullGather) {
880 if (newtype == hullGather || newtype == hullMultline)
882 else if (newtype == hullAlign || newtype == hullFlAlign ||
883 newtype == hullAlignAt || newtype == hullXAlignAt) {
886 } else if (newtype == hullXXAlignAt) {
888 for (row_type row = 0; row < nrows(); ++row)
889 numbered(row, false);
893 setType(hullEqnArray);
899 lyxerr << "mutation from '" << hullName(type_)
900 << "' to '" << hullName(newtype)
901 << "' not implemented" << endl;
906 string InsetMathHull::eolString(row_type row, bool emptyline, bool fragile) const
909 if (numberedType()) {
910 if (!label_[row].empty() && !nonum_[row])
911 res += "\\label{" + label_[row] + '}';
912 if (nonum_[row] && (type_ != hullMultline))
913 res += "\\nonumber ";
915 return res + InsetMathGrid::eolString(row, emptyline, fragile);
919 void InsetMathHull::write(WriteStream & os) const
922 InsetMathGrid::write(os);
927 void InsetMathHull::normalize(NormalStream & os) const
929 os << "[formula " << hullName(type_) << ' ';
930 InsetMathGrid::normalize(os);
935 void InsetMathHull::mathmlize(MathMLStream & os) const
937 InsetMathGrid::mathmlize(os);
941 void InsetMathHull::infoize(ostream & os) const
943 os << "Type: " << hullName(type_);
947 void InsetMathHull::check() const
949 BOOST_ASSERT(nonum_.size() == nrows());
950 BOOST_ASSERT(label_.size() == nrows());
954 void InsetMathHull::doExtern(LCursor & cur, FuncRequest & func)
958 idocstringstream iss(func.argument());
959 iss >> dlang >> extra;
961 extra = from_ascii("noextra");
962 string const lang = to_ascii(dlang);
965 #warning temporarily disabled
966 //if (cur.selection()) {
969 // lyxerr << "use selection: " << ar << endl;
970 // insert(pipeThroughExtern(lang, extra, ar));
976 eq.push_back(MathAtom(new InsetMathChar('=')));
978 // go to first item in line
979 cur.idx() -= cur.idx() % ncols();
982 if (getType() == hullSimple) {
983 size_type pos = cur.cell().find_last(eq);
985 if (cur.inMathed() && cur.selection()) {
987 asArray(from_utf8(grabAndEraseSelection(cur)), ar);
988 } else if (pos == cur.cell().size()) {
990 lyxerr << "use whole cell: " << ar << endl;
992 ar = MathArray(cur.cell().begin() + pos + 1, cur.cell().end());
993 lyxerr << "use partial cell form pos: " << pos << endl;
995 cur.cell().append(eq);
996 cur.cell().append(pipeThroughExtern(lang, extra, ar));
997 cur.pos() = cur.lastpos();
1001 if (getType() == hullEquation) {
1002 lyxerr << "use equation inset" << endl;
1003 mutate(hullEqnArray);
1004 MathArray & ar = cur.cell();
1005 lyxerr << "use cell: " << ar << endl;
1009 cur.cell() = pipeThroughExtern(lang, extra, ar);
1010 // move to end of line
1011 cur.pos() = cur.lastpos();
1016 lyxerr << "use eqnarray" << endl;
1017 cur.idx() += 2 - cur.idx() % ncols();
1019 MathArray ar = cur.cell();
1020 lyxerr << "use cell: " << ar << endl;
1021 #ifdef WITH_WARNINGS
1022 #warning temporarily disabled
1029 cur.cell() = pipeThroughExtern(lang, extra, ar);
1030 cur.pos() = cur.lastpos();
1035 void InsetMathHull::doDispatch(LCursor & cur, FuncRequest & cmd)
1037 //lyxerr << "action: " << cmd.action << endl;
1038 switch (cmd.action) {
1040 case LFUN_FINISHED_LEFT:
1041 case LFUN_FINISHED_RIGHT:
1042 case LFUN_FINISHED_UP:
1043 case LFUN_FINISHED_DOWN:
1044 //lyxerr << "action: " << cmd.action << endl;
1045 InsetMathGrid::doDispatch(cur, cmd);
1046 notifyCursorLeaves(cur);
1050 case LFUN_BREAK_PARAGRAPH:
1051 // just swallow this
1054 case LFUN_BREAK_LINE:
1055 // some magic for the common case
1056 if (type_ == hullSimple || type_ == hullEquation) {
1057 recordUndoInset(cur);
1059 cur.bv().buffer()->params().use_amsmath == BufferParams::AMS_ON;
1060 mutate(align ? hullAlign : hullEqnArray);
1062 cur.pos() = cur.lastpos();
1064 InsetMathGrid::doDispatch(cur, cmd);
1067 case LFUN_MATH_NUMBER:
1068 //lyxerr << "toggling all numbers" << endl;
1070 recordUndoInset(cur);
1071 bool old = numberedType();
1072 if (type_ == hullMultline)
1073 numbered(nrows() - 1, !old);
1075 for (row_type row = 0; row < nrows(); ++row)
1076 numbered(row, !old);
1078 cur.message(old ? _("No number") : _("Number"));
1082 case LFUN_MATH_NONUMBER:
1084 recordUndoInset(cur);
1085 row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1086 bool old = numbered(r);
1087 cur.message(old ? _("No number") : _("Number"));
1092 case LFUN_LABEL_INSERT: {
1093 recordUndoInset(cur);
1094 row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1095 docstring old_label = from_utf8(label(r));
1096 docstring const default_label = from_ascii(
1097 (lyxrc.label_init_length >= 0) ? "eq:" : "");
1098 if (old_label.empty())
1099 old_label = default_label;
1101 InsetCommandParams p("label");
1102 p["name"] = cmd.argument().empty() ? old_label : cmd.argument();
1103 string const data = InsetCommandMailer::params2string("label", p);
1105 if (cmd.argument().empty())
1106 cur.bv().showInsetDialog("label", data, 0);
1108 FuncRequest fr(LFUN_INSET_INSERT, data);
1114 case LFUN_INSET_INSERT: {
1115 //lyxerr << "arg: " << to_utf8(cmd.argument()) << endl;
1116 string const name = cmd.getArg(0);
1117 if (name == "label") {
1118 InsetCommandParams p("label");
1119 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), p);
1120 string str = to_utf8(p["name"]);
1121 recordUndoInset(cur);
1122 row_type const r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1123 str = support::trim(str);
1126 string old = label(r);
1128 cur.bv().buffer()->changeRefsIfUnique(old, str,
1129 InsetBase::REF_CODE);
1134 InsetMathGrid::doDispatch(cur, cmd);
1138 case LFUN_MATH_EXTERN:
1139 recordUndoInset(cur);
1143 case LFUN_MATH_MUTATE: {
1144 recordUndoInset(cur);
1145 row_type row = cur.row();
1146 col_type col = cur.col();
1147 mutate(hullType(to_utf8(cmd.argument())));
1148 cur.idx() = row * ncols() + col;
1149 if (cur.idx() > cur.lastidx()) {
1150 cur.idx() = cur.lastidx();
1151 cur.pos() = cur.lastpos();
1153 if (cur.pos() > cur.lastpos())
1154 cur.pos() = cur.lastpos();
1155 //cur.dispatched(FINISHED);
1159 case LFUN_MATH_DISPLAY: {
1160 recordUndoInset(cur);
1161 mutate(type_ == hullSimple ? hullEquation : hullSimple);
1163 cur.pos() = cur.lastpos();
1164 //cur.dispatched(FINISHED);
1169 InsetMathGrid::doDispatch(cur, cmd);
1175 bool InsetMathHull::getStatus(LCursor & cur, FuncRequest const & cmd,
1176 FuncStatus & status) const
1178 switch (cmd.action) {
1179 case LFUN_FINISHED_LEFT:
1180 case LFUN_FINISHED_RIGHT:
1181 case LFUN_FINISHED_UP:
1182 case LFUN_FINISHED_DOWN:
1183 status.enabled(true);
1185 case LFUN_BREAK_LINE:
1186 case LFUN_MATH_NUMBER:
1187 case LFUN_MATH_NONUMBER:
1188 case LFUN_MATH_EXTERN:
1189 case LFUN_MATH_MUTATE:
1190 case LFUN_MATH_DISPLAY:
1192 status.enabled(true);
1194 case LFUN_LABEL_INSERT:
1195 status.enabled(type_ != hullSimple);
1197 case LFUN_INSET_INSERT:
1198 if (cmd.getArg(0) == "label") {
1199 status.enabled(type_ != hullSimple);
1202 return InsetMathGrid::getStatus(cur, cmd, status);
1203 case LFUN_TABULAR_FEATURE: {
1204 istringstream is(to_utf8(cmd.argument()));
1208 && (s == "append-row"
1209 || s == "delete-row"
1210 || s == "copy-row")) {
1211 status.message(bformat(
1212 from_utf8(N_("Can't change number of rows in '%1$s'")),
1213 from_utf8(hullName(type_))));
1214 status.enabled(false);
1218 && (s == "append-column"
1219 || s == "delete-column"
1220 || s == "copy-column")) {
1221 status.message(bformat(
1222 from_utf8(N_("Can't change number of columns in '%1$s'")),
1223 from_utf8(hullName(type_))));
1224 status.enabled(false);
1227 if ((type_ == hullSimple
1228 || type_ == hullEquation
1229 || type_ == hullNone) &&
1230 (s == "add-hline-above" || s == "add-hline-below")) {
1231 status.message(bformat(
1232 from_utf8(N_("Can't add horizontal grid lines in '%1$s'")),
1233 from_utf8(hullName(type_))));
1234 status.enabled(false);
1237 if (s == "add-vline-left" || s == "add-vline-right") {
1238 status.message(bformat(
1239 from_utf8(N_("Can't add vertical grid lines in '%1$s'")),
1240 from_utf8(hullName(type_))));
1241 status.enabled(false);
1244 if (s == "valign-top" || s == "valign-middle"
1245 || s == "valign-bottom" || s == "align-left"
1246 || s == "align-center" || s == "align-right") {
1247 status.enabled(false);
1250 return InsetMathGrid::getStatus(cur, cmd, status);
1253 return InsetMathGrid::getStatus(cur, cmd, status);
1256 // This cannot really happen, but inserted to shut-up gcc
1257 return InsetMathGrid::getStatus(cur, cmd, status);
1261 /////////////////////////////////////////////////////////////////////
1265 // simply scrap this function if you want
1266 void InsetMathHull::mutateToText()
1269 // translate to latex
1271 latex(NULL, os, false, false);
1272 string str = os.str();
1275 LyXText * lt = view_->getLyXText();
1276 string::const_iterator cit = str.begin();
1277 string::const_iterator end = str.end();
1278 for (; cit != end; ++cit)
1279 view_->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
1282 //dispatch(LFUN_ESCAPE);
1287 void InsetMathHull::handleFont(LCursor & cur, string const & arg,
1288 string const & font)
1290 // this whole function is a hack and won't work for incremental font
1293 if (cur.inset().asInsetMath()->name() == font)
1294 cur.handleFont(font);
1296 cur.handleNest(createInsetMath(font));
1302 void InsetMathHull::handleFont2(LCursor & cur, string const & arg)
1307 bv_funcs::string2font(arg, font, b);
1308 if (font.color() != LColor::inherit) {
1309 MathAtom at = MathAtom(new InsetMathColor(true, font.color()));
1310 cur.handleNest(at, 0);
1315 void InsetMathHull::edit(LCursor & cur, bool left)
1318 left ? idxFirst(cur) : idxLast(cur);
1322 docstring const InsetMathHull::editMessage() const
1324 return _("Math editor mode");
1328 void InsetMathHull::revealCodes(LCursor & cur) const
1330 if (!cur.inMathed())
1334 cur.message(from_utf8(os.str()));
1336 // write something to the minibuffer
1337 // translate to latex
1341 string str = os.str();
1343 string::size_type pos = 0;
1345 for (string::iterator it = str.begin(); it != str.end(); ++it) {
1348 else if (*it == '\0') {
1350 pos = it - str.begin();
1356 res = res.substr(pos - 30);
1357 if (res.size() > 60)
1358 res = res.substr(0, 60);
1364 InsetBase::Code InsetMathHull::lyxCode() const
1370 /////////////////////////////////////////////////////////////////////
1374 bool InsetMathHull::searchForward(BufferView * bv, string const & str,
1377 #ifdef WITH_WARNINGS
1378 #warning completely broken
1380 static InsetMathHull * lastformula = 0;
1381 static CursorBase current = DocIterator(ibegin(nucleus()));
1382 static MathArray ar;
1383 static string laststr;
1385 if (lastformula != this || laststr != str) {
1386 //lyxerr << "reset lastformula to " << this << endl;
1389 current = ibegin(nucleus());
1391 mathed_parse_cell(ar, str);
1395 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
1397 for (DocIterator it = current; it != iend(nucleus()); increment(it)) {
1398 CursorSlice & top = it.back();
1399 MathArray const & a = top.asInsetMath()->cell(top.idx_);
1400 if (a.matchpart(ar, top.pos_)) {
1401 bv->cursor().setSelection(it, ar.size());
1403 top.pos_ += ar.size();
1409 //lyxerr << "not found!" << endl;
1416 void InsetMathHull::write(Buffer const &, std::ostream & os) const
1418 odocstringstream oss;
1419 WriteStream wi(oss, false, false);
1422 os << to_utf8(oss.str());
1426 void InsetMathHull::read(Buffer const &, LyXLex & lex)
1429 mathed_parse_normal(at, lex);
1430 operator=(*at->asHullInset());
1434 int InsetMathHull::plaintext(Buffer const &, odocstream & os,
1435 OutputParams const &) const
1437 if (0 && display()) {
1441 TextPainter tpain(dim.width(), dim.height());
1442 drawT(tpain, 0, dim.ascent());
1444 // reset metrics cache to "real" values
1446 return tpain.textheight();
1448 WriteStream wi(os, false, true);
1455 int InsetMathHull::docbook(Buffer const & buf, odocstream & os,
1456 OutputParams const & runparams) const
1458 MathMLStream ms(os);
1461 if (getType() == hullSimple)
1462 name = "inlineequation";
1464 name = "informalequation";
1466 string bname = name;
1467 if (!label(0).empty())
1468 bname += " id=\"" + sgml::cleanID(buf, runparams, label(0)) + "\"";
1469 ms << MTag(bname.c_str());
1471 odocstringstream ls;
1472 if (runparams.flavor == OutputParams::XML) {
1473 ms << MTag("alt role=\"tex\" ");
1474 // Workaround for db2latex: db2latex always includes equations with
1475 // \ensuremath{} or \begin{display}\end{display}
1476 // so we strip LyX' math environment
1477 WriteStream wi(ls, false, false);
1478 InsetMathGrid::write(wi);
1479 ms << subst(subst(to_utf8(ls.str()), "&", "&"), "<", "<");
1482 InsetMathGrid::mathmlize(ms);
1485 ms << MTag("alt role=\"tex\"");
1486 res = latex(buf, ls, runparams);
1487 ms << subst(subst(to_utf8(ls.str()), "&", "&"), "<", "<");
1491 ms << "<graphic fileref=\"eqn/";
1492 if ( !label(0).empty())
1493 ms << sgml::cleanID(buf, runparams, label(0));
1495 ms << sgml::uniqueID("anon");
1497 if (runparams.flavor == OutputParams::XML)
1502 ms << ETag(name.c_str());
1503 return ms.line() + res;
1507 int InsetMathHull::textString(Buffer const & buf, odocstream & os,
1508 OutputParams const & op) const
1510 return plaintext(buf, os, op);