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 "MathStream.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;
74 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(docstring 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 '" << to_utf8(s) << "'" << endl;
138 docstring hullName(HullType type)
141 case hullNone: return from_ascii("none");
142 case hullSimple: return from_ascii("simple");
143 case hullEquation: return from_ascii("equation");
144 case hullEqnArray: return from_ascii("eqnarray");
145 case hullAlign: return from_ascii("align");
146 case hullAlignAt: return from_ascii("alignat");
147 case hullXAlignAt: return from_ascii("xalignat");
148 case hullXXAlignAt: return from_ascii("xxalignat");
149 case hullMultline: return from_ascii("multline");
150 case hullGather: return from_ascii("gather");
151 case hullFlAlign: return from_ascii("flalign");
153 lyxerr << "unknown hull type '" << type << "'" << endl;
154 return from_ascii("none");
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 docstring InsetMathHull::standardFont() const
271 return from_ascii(type_ == hullNone ? "lyxnochange" : "mathnormal");
275 bool InsetMathHull::previewState(BufferView * bv) const
277 if (!editing(bv) && RenderPreview::status() == LyXRC::PREVIEW_ON) {
278 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, from_ascii("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, from_ascii("mathrm"));
350 docstring const nl = nicelabel(row);
354 setPosCache(pi, x, y);
358 void InsetMathHull::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
361 InsetMathGrid::metricsT(mi, dim);
364 WriteStream wi(os, false, true);
366 dim.wid = os.str().size();
373 void InsetMathHull::drawT(TextPainter & pain, int x, int y) const
376 InsetMathGrid::drawT(pain, x, y);
379 WriteStream wi(os, false, true);
381 pain.draw(x, y, os.str().c_str());
388 docstring const latex_string(InsetMathHull const & inset)
391 WriteStream wi(ls, false, false);
399 void InsetMathHull::addPreview(graphics::PreviewLoader & ploader) const
401 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
402 docstring const snippet = latex_string(*this);
403 preview_->addPreview(snippet, ploader);
408 bool InsetMathHull::notifyCursorLeaves(LCursor & cur)
410 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
411 Buffer const & buffer = cur.buffer();
412 docstring const snippet = latex_string(*this);
413 preview_->addPreview(snippet, buffer);
414 preview_->startLoading(buffer);
420 docstring InsetMathHull::label(row_type row) const
422 BOOST_ASSERT(row < nrows());
427 void InsetMathHull::label(row_type row, docstring const & label)
429 //lyxerr << "setting label '" << label << "' for row " << row << endl;
434 void InsetMathHull::numbered(row_type row, bool num)
440 bool InsetMathHull::numbered(row_type row) const
446 bool InsetMathHull::ams() const
449 type_ == hullAlign ||
450 type_ == hullFlAlign ||
451 type_ == hullMultline ||
452 type_ == hullGather ||
453 type_ == hullAlignAt ||
454 type_ == hullXAlignAt ||
455 type_ == hullXXAlignAt;
459 bool InsetMathHull::display() const
461 return type_ != hullSimple && type_ != hullNone;
465 void InsetMathHull::getLabelList(Buffer const &, vector<docstring> & labels) const
467 for (row_type row = 0; row < nrows(); ++row)
468 if (!label_[row].empty() && nonum_[row] != 1)
469 labels.push_back(label_[row]);
473 bool InsetMathHull::numberedType() const
475 if (type_ == hullNone)
477 if (type_ == hullSimple)
479 if (type_ == hullXXAlignAt)
481 for (row_type row = 0; row < nrows(); ++row)
488 void InsetMathHull::validate(LaTeXFeatures & features) const
491 features.require("amsmath");
494 // Validation is necessary only if not using AMS math.
495 // To be safe, we will always run mathedvalidate.
496 //if (features.amsstyle)
499 features.require("boldsymbol");
500 //features.binom = true;
502 InsetMathGrid::validate(features);
506 void InsetMathHull::header_write(WriteStream & os) const
508 bool n = numberedType();
522 os << "\\begin{equation" << star(n) << "}\n";
532 os << "\\begin{" << hullName(type_) << star(n) << "}\n";
537 os << "\\begin{" << hullName(type_) << star(n) << '}'
538 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
542 os << "\\begin{" << hullName(type_) << '}'
543 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
547 os << "\\begin{unknown" << star(n) << '}';
553 void InsetMathHull::footer_write(WriteStream & os) const
555 bool n = numberedType();
568 os << "\\end{equation" << star(n) << "}\n";
580 os << "\\end{" << hullName(type_) << star(n) << "}\n";
584 os << "\\end{" << hullName(type_) << "}\n";
588 os << "\\end{unknown" << star(n) << '}';
594 bool InsetMathHull::rowChangeOK() const
597 type_ == hullEqnArray || type_ == hullAlign ||
598 type_ == hullFlAlign || type_ == hullAlignAt ||
599 type_ == hullXAlignAt || type_ == hullXXAlignAt ||
600 type_ == hullGather || type_ == hullMultline;
604 bool InsetMathHull::colChangeOK() const
607 type_ == hullAlign || type_ == hullFlAlign ||type_ == hullAlignAt ||
608 type_ == hullXAlignAt || type_ == hullXXAlignAt;
612 void InsetMathHull::addRow(row_type row)
616 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
617 label_.insert(label_.begin() + row + 1, docstring());
618 InsetMathGrid::addRow(row);
622 void InsetMathHull::swapRow(row_type row)
626 if (row + 1 == nrows())
628 swap(nonum_[row], nonum_[row + 1]);
629 swap(label_[row], label_[row + 1]);
630 InsetMathGrid::swapRow(row);
634 void InsetMathHull::delRow(row_type row)
636 if (nrows() <= 1 || !rowChangeOK())
638 InsetMathGrid::delRow(row);
639 // The last dummy row has no number info nor a label.
640 // Test nrows() + 1 because we have already erased the row.
641 if (row == nrows() + 1)
643 nonum_.erase(nonum_.begin() + row);
644 label_.erase(label_.begin() + row);
648 void InsetMathHull::addCol(col_type col)
652 InsetMathGrid::addCol(col);
656 void InsetMathHull::delCol(col_type col)
658 if (ncols() <= 1 || !colChangeOK())
660 InsetMathGrid::delCol(col);
664 docstring InsetMathHull::nicelabel(row_type row) const
668 if (label_[row].empty())
669 return from_ascii("(#)");
671 return '(' + label_[row] + ')';
675 void InsetMathHull::glueall()
678 for (idx_type i = 0; i < nargs(); ++i)
680 *this = InsetMathHull(hullSimple);
686 void InsetMathHull::splitTo2Cols()
688 BOOST_ASSERT(ncols() == 1);
689 InsetMathGrid::addCol(1);
690 for (row_type row = 0; row < nrows(); ++row) {
691 idx_type const i = 2 * row;
692 pos_type pos = firstRelOp(cell(i));
693 cell(i + 1) = MathArray(cell(i).begin() + pos, cell(i).end());
694 cell(i).erase(pos, cell(i).size());
699 void InsetMathHull::splitTo3Cols()
701 BOOST_ASSERT(ncols() < 3);
704 InsetMathGrid::addCol(1);
705 for (row_type row = 0; row < nrows(); ++row) {
706 idx_type const i = 3 * row + 1;
707 if (cell(i).size()) {
708 cell(i + 1) = MathArray(cell(i).begin() + 1, cell(i).end());
709 cell(i).erase(1, cell(i).size());
715 void InsetMathHull::changeCols(col_type cols)
719 else if (ncols() < cols) {
725 while (ncols() < cols)
726 InsetMathGrid::addCol(ncols() - 1);
732 for (row_type row = 0; row < nrows(); ++row) {
733 idx_type const i = row * ncols();
734 for (col_type col = cols; col < ncols(); ++col) {
735 cell(i + cols - 1).append(cell(i + col));
739 while (ncols() > cols) {
740 InsetMathGrid::delCol(ncols() - 1);
745 HullType InsetMathHull::getType() const
751 void InsetMathHull::setType(HullType type)
758 void InsetMathHull::mutate(HullType newtype)
760 //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
762 // we try to move along the chain
763 // none <-> simple <-> equation <-> eqnarray -> *align* -> multline, gather -+
765 // +-------------------------------------+
766 // we use eqnarray as intermediate type for mutations that are not
767 // directly supported because it handles labels and numbering for
770 if (newtype == type_) {
774 else if (newtype < hullNone) {
779 else if (type_ == hullNone) {
785 else if (type_ == hullSimple) {
786 if (newtype == hullNone) {
790 setType(hullEquation);
796 else if (type_ == hullEquation) {
797 if (newtype < type_) {
801 } else if (newtype == hullEqnArray) {
802 // split it "nicely" on the first relop
804 setType(hullEqnArray);
805 } else if (newtype == hullMultline || newtype == hullGather) {
815 else if (type_ == hullEqnArray) {
816 if (newtype < type_) {
817 // set correct (no)numbering
818 bool allnonum = true;
819 for (row_type row = 0; row < nrows(); ++row)
823 // set first non-empty label
825 for (row_type row = 0; row < nrows(); ++row) {
826 if (!label_[row].empty()) {
833 nonum_[0] = allnonum;
836 } else { // align & Co.
843 else if (type_ == hullAlign || type_ == hullAlignAt ||
844 type_ == hullXAlignAt || type_ == hullFlAlign) {
845 if (newtype < hullAlign) {
847 setType(hullEqnArray);
849 } else if (newtype == hullGather || newtype == hullMultline) {
852 } else if (newtype == hullXXAlignAt) {
853 for (row_type row = 0; row < nrows(); ++row)
854 numbered(row, false);
861 else if (type_ == hullXXAlignAt) {
862 for (row_type row = 0; row < nrows(); ++row)
863 numbered(row, false);
864 if (newtype < hullAlign) {
866 setType(hullEqnArray);
868 } else if (newtype == hullGather || newtype == hullMultline) {
876 else if (type_ == hullMultline || type_ == hullGather) {
877 if (newtype == hullGather || newtype == hullMultline)
879 else if (newtype == hullAlign || newtype == hullFlAlign ||
880 newtype == hullAlignAt || newtype == hullXAlignAt) {
883 } else if (newtype == hullXXAlignAt) {
885 for (row_type row = 0; row < nrows(); ++row)
886 numbered(row, false);
890 setType(hullEqnArray);
896 lyxerr << "mutation from '" << to_utf8(hullName(type_))
897 << "' to '" << to_utf8(hullName(newtype))
898 << "' not implemented" << endl;
903 docstring InsetMathHull::eolString(row_type row, bool emptyline, bool fragile) const
906 if (numberedType()) {
907 if (!label_[row].empty() && !nonum_[row])
908 res += "\\label{" + label_[row] + '}';
909 if (nonum_[row] && (type_ != hullMultline))
910 res += "\\nonumber ";
912 return res + InsetMathGrid::eolString(row, emptyline, fragile);
916 void InsetMathHull::write(WriteStream & os) const
919 InsetMathGrid::write(os);
924 void InsetMathHull::normalize(NormalStream & os) const
926 os << "[formula " << hullName(type_) << ' ';
927 InsetMathGrid::normalize(os);
932 void InsetMathHull::mathmlize(MathStream & os) const
934 InsetMathGrid::mathmlize(os);
938 void InsetMathHull::infoize(odocstream & os) const
940 os << "Type: " << hullName(type_);
944 void InsetMathHull::check() const
946 BOOST_ASSERT(nonum_.size() == nrows());
947 BOOST_ASSERT(label_.size() == nrows());
951 void InsetMathHull::doExtern(LCursor & cur, FuncRequest & func)
955 idocstringstream iss(func.argument());
956 iss >> dlang >> extra;
958 extra = from_ascii("noextra");
959 std::string const lang = to_ascii(dlang);
962 #warning temporarily disabled
963 //if (cur.selection()) {
966 // lyxerr << "use selection: " << ar << endl;
967 // insert(pipeThroughExtern(lang, extra, ar));
973 eq.push_back(MathAtom(new InsetMathChar('=')));
975 // go to first item in line
976 cur.idx() -= cur.idx() % ncols();
979 if (getType() == hullSimple) {
980 size_type pos = cur.cell().find_last(eq);
982 if (cur.inMathed() && cur.selection()) {
984 asArray(grabAndEraseSelection(cur), ar);
985 } else if (pos == cur.cell().size()) {
987 lyxerr << "use whole cell: " << ar << endl;
989 ar = MathArray(cur.cell().begin() + pos + 1, cur.cell().end());
990 lyxerr << "use partial cell form pos: " << pos << endl;
992 cur.cell().append(eq);
993 cur.cell().append(pipeThroughExtern(lang, extra, ar));
994 cur.pos() = cur.lastpos();
998 if (getType() == hullEquation) {
999 lyxerr << "use equation inset" << endl;
1000 mutate(hullEqnArray);
1001 MathArray & ar = cur.cell();
1002 lyxerr << "use cell: " << ar << endl;
1006 cur.cell() = pipeThroughExtern(lang, extra, ar);
1007 // move to end of line
1008 cur.pos() = cur.lastpos();
1013 lyxerr << "use eqnarray" << endl;
1014 cur.idx() += 2 - cur.idx() % ncols();
1016 MathArray ar = cur.cell();
1017 lyxerr << "use cell: " << ar << endl;
1018 #ifdef WITH_WARNINGS
1019 #warning temporarily disabled
1026 cur.cell() = pipeThroughExtern(lang, extra, ar);
1027 cur.pos() = cur.lastpos();
1032 void InsetMathHull::doDispatch(LCursor & cur, FuncRequest & cmd)
1034 //lyxerr << "action: " << cmd.action << endl;
1035 switch (cmd.action) {
1037 case LFUN_FINISHED_LEFT:
1038 case LFUN_FINISHED_RIGHT:
1039 case LFUN_FINISHED_UP:
1040 case LFUN_FINISHED_DOWN:
1041 //lyxerr << "action: " << cmd.action << endl;
1042 InsetMathGrid::doDispatch(cur, cmd);
1043 notifyCursorLeaves(cur);
1047 case LFUN_BREAK_PARAGRAPH:
1048 // just swallow this
1051 case LFUN_BREAK_LINE:
1052 // some magic for the common case
1053 if (type_ == hullSimple || type_ == hullEquation) {
1054 recordUndoInset(cur);
1056 cur.bv().buffer()->params().use_amsmath == BufferParams::package_on;
1057 mutate(align ? hullAlign : hullEqnArray);
1059 cur.pos() = cur.lastpos();
1061 InsetMathGrid::doDispatch(cur, cmd);
1064 case LFUN_MATH_NUMBER:
1065 //lyxerr << "toggling all numbers" << endl;
1067 recordUndoInset(cur);
1068 bool old = numberedType();
1069 if (type_ == hullMultline)
1070 numbered(nrows() - 1, !old);
1072 for (row_type row = 0; row < nrows(); ++row)
1073 numbered(row, !old);
1075 cur.message(old ? _("No number") : _("Number"));
1079 case LFUN_MATH_NONUMBER:
1081 recordUndoInset(cur);
1082 row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1083 bool old = numbered(r);
1084 cur.message(old ? _("No number") : _("Number"));
1089 case LFUN_LABEL_INSERT: {
1090 recordUndoInset(cur);
1091 row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1092 docstring old_label = label(r);
1093 docstring const default_label = from_ascii(
1094 (lyxrc.label_init_length >= 0) ? "eq:" : "");
1095 if (old_label.empty())
1096 old_label = default_label;
1098 InsetCommandParams p("label");
1099 p["name"] = cmd.argument().empty() ? old_label : cmd.argument();
1100 std::string const data = InsetCommandMailer::params2string("label", p);
1102 if (cmd.argument().empty())
1103 cur.bv().showInsetDialog("label", data, 0);
1105 FuncRequest fr(LFUN_INSET_INSERT, data);
1111 case LFUN_INSET_INSERT: {
1112 //lyxerr << "arg: " << to_utf8(cmd.argument()) << endl;
1113 std::string const name = cmd.getArg(0);
1114 if (name == "label") {
1115 InsetCommandParams p("label");
1116 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), p);
1117 docstring str = p["name"];
1118 recordUndoInset(cur);
1119 row_type const r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1120 str = support::trim(str);
1123 docstring old = label(r);
1125 cur.bv().buffer()->changeRefsIfUnique(old, str,
1126 InsetBase::REF_CODE);
1131 InsetMathGrid::doDispatch(cur, cmd);
1135 case LFUN_MATH_EXTERN:
1136 recordUndoInset(cur);
1140 case LFUN_MATH_MUTATE: {
1141 recordUndoInset(cur);
1142 row_type row = cur.row();
1143 col_type col = cur.col();
1144 mutate(hullType(cmd.argument()));
1145 cur.idx() = row * ncols() + col;
1146 if (cur.idx() > cur.lastidx()) {
1147 cur.idx() = cur.lastidx();
1148 cur.pos() = cur.lastpos();
1150 if (cur.pos() > cur.lastpos())
1151 cur.pos() = cur.lastpos();
1152 //cur.dispatched(FINISHED);
1156 case LFUN_MATH_DISPLAY: {
1157 recordUndoInset(cur);
1158 mutate(type_ == hullSimple ? hullEquation : hullSimple);
1160 cur.pos() = cur.lastpos();
1161 //cur.dispatched(FINISHED);
1166 InsetMathGrid::doDispatch(cur, cmd);
1172 bool InsetMathHull::getStatus(LCursor & cur, FuncRequest const & cmd,
1173 FuncStatus & status) const
1175 switch (cmd.action) {
1176 case LFUN_FINISHED_LEFT:
1177 case LFUN_FINISHED_RIGHT:
1178 case LFUN_FINISHED_UP:
1179 case LFUN_FINISHED_DOWN:
1180 status.enabled(true);
1182 case LFUN_BREAK_LINE:
1183 case LFUN_MATH_NUMBER:
1184 case LFUN_MATH_NONUMBER:
1185 case LFUN_MATH_EXTERN:
1186 case LFUN_MATH_MUTATE:
1187 case LFUN_MATH_DISPLAY:
1189 status.enabled(true);
1191 case LFUN_LABEL_INSERT:
1192 status.enabled(type_ != hullSimple);
1194 case LFUN_INSET_INSERT:
1195 if (cmd.getArg(0) == "label") {
1196 status.enabled(type_ != hullSimple);
1199 return InsetMathGrid::getStatus(cur, cmd, status);
1200 case LFUN_TABULAR_FEATURE: {
1201 istringstream is(to_utf8(cmd.argument()));
1205 && (s == "append-row"
1206 || s == "delete-row"
1207 || s == "copy-row")) {
1208 status.message(bformat(
1209 from_utf8(N_("Can't change number of rows in '%1$s'")),
1211 status.enabled(false);
1215 && (s == "append-column"
1216 || s == "delete-column"
1217 || s == "copy-column")) {
1218 status.message(bformat(
1219 from_utf8(N_("Can't change number of columns in '%1$s'")),
1221 status.enabled(false);
1224 if ((type_ == hullSimple
1225 || type_ == hullEquation
1226 || type_ == hullNone) &&
1227 (s == "add-hline-above" || s == "add-hline-below")) {
1228 status.message(bformat(
1229 from_utf8(N_("Can't add horizontal grid lines in '%1$s'")),
1231 status.enabled(false);
1234 if (s == "add-vline-left" || s == "add-vline-right") {
1235 status.message(bformat(
1236 from_utf8(N_("Can't add vertical grid lines in '%1$s'")),
1238 status.enabled(false);
1241 if (s == "valign-top" || s == "valign-middle"
1242 || s == "valign-bottom" || s == "align-left"
1243 || s == "align-center" || s == "align-right") {
1244 status.enabled(false);
1247 return InsetMathGrid::getStatus(cur, cmd, status);
1250 return InsetMathGrid::getStatus(cur, cmd, status);
1253 // This cannot really happen, but inserted to shut-up gcc
1254 return InsetMathGrid::getStatus(cur, cmd, status);
1258 /////////////////////////////////////////////////////////////////////
1262 // simply scrap this function if you want
1263 void InsetMathHull::mutateToText()
1266 // translate to latex
1268 latex(NULL, os, false, false);
1269 string str = os.str();
1272 LyXText * lt = view_->getLyXText();
1273 string::const_iterator cit = str.begin();
1274 string::const_iterator end = str.end();
1275 for (; cit != end; ++cit)
1276 view_->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
1279 //dispatch(LFUN_ESCAPE);
1284 void InsetMathHull::handleFont(LCursor & cur, docstring const & arg,
1285 docstring const & font)
1287 // this whole function is a hack and won't work for incremental font
1290 if (cur.inset().asInsetMath()->name() == font)
1291 cur.handleFont(to_utf8(font));
1293 cur.handleNest(createInsetMath(font));
1299 void InsetMathHull::handleFont2(LCursor & cur, docstring const & arg)
1304 bv_funcs::string2font(to_utf8(arg), font, b);
1305 if (font.color() != LColor::inherit) {
1306 MathAtom at = MathAtom(new InsetMathColor(true, font.color()));
1307 cur.handleNest(at, 0);
1312 void InsetMathHull::edit(LCursor & cur, bool left)
1315 left ? idxFirst(cur) : idxLast(cur);
1319 docstring const InsetMathHull::editMessage() const
1321 return _("Math editor mode");
1325 void InsetMathHull::revealCodes(LCursor & cur) const
1327 if (!cur.inMathed())
1329 odocstringstream os;
1331 cur.message(os.str());
1333 // write something to the minibuffer
1334 // translate to latex
1338 string str = os.str();
1340 string::size_type pos = 0;
1342 for (string::iterator it = str.begin(); it != str.end(); ++it) {
1345 else if (*it == '\0') {
1347 pos = it - str.begin();
1353 res = res.substr(pos - 30);
1354 if (res.size() > 60)
1355 res = res.substr(0, 60);
1361 InsetBase::Code InsetMathHull::lyxCode() const
1367 /////////////////////////////////////////////////////////////////////
1371 bool InsetMathHull::searchForward(BufferView * bv, string const & str,
1374 #ifdef WITH_WARNINGS
1375 #warning completely broken
1377 static InsetMathHull * lastformula = 0;
1378 static CursorBase current = DocIterator(ibegin(nucleus()));
1379 static MathArray ar;
1380 static string laststr;
1382 if (lastformula != this || laststr != str) {
1383 //lyxerr << "reset lastformula to " << this << endl;
1386 current = ibegin(nucleus());
1388 mathed_parse_cell(ar, str);
1392 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
1394 for (DocIterator it = current; it != iend(nucleus()); increment(it)) {
1395 CursorSlice & top = it.back();
1396 MathArray const & a = top.asInsetMath()->cell(top.idx_);
1397 if (a.matchpart(ar, top.pos_)) {
1398 bv->cursor().setSelection(it, ar.size());
1400 top.pos_ += ar.size();
1406 //lyxerr << "not found!" << endl;
1413 void InsetMathHull::write(Buffer const &, std::ostream & os) const
1415 odocstringstream oss;
1416 WriteStream wi(oss, false, false);
1419 os << to_utf8(oss.str());
1423 void InsetMathHull::read(Buffer const &, LyXLex & lex)
1426 mathed_parse_normal(at, lex);
1427 operator=(*at->asHullInset());
1431 int InsetMathHull::plaintext(Buffer const &, odocstream & os,
1432 OutputParams const &) const
1434 if (0 && display()) {
1438 TextPainter tpain(dim.width(), dim.height());
1439 drawT(tpain, 0, dim.ascent());
1441 // reset metrics cache to "real" values
1443 return tpain.textheight();
1445 WriteStream wi(os, false, true);
1452 int InsetMathHull::docbook(Buffer const & buf, odocstream & os,
1453 OutputParams const & runparams) const
1458 if (getType() == hullSimple)
1459 name = from_ascii("inlineequation");
1461 name = from_ascii("informalequation");
1463 docstring bname = name;
1464 if (!label(0).empty())
1465 bname += " id='" + sgml::cleanID(buf, runparams, label(0)) + "'";
1468 odocstringstream ls;
1469 if (runparams.flavor == OutputParams::XML) {
1470 ms << MTag(from_ascii("alt role='tex' "));
1471 // Workaround for db2latex: db2latex always includes equations with
1472 // \ensuremath{} or \begin{display}\end{display}
1473 // so we strip LyX' math environment
1474 WriteStream wi(ls, false, false);
1475 InsetMathGrid::write(wi);
1476 ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&"), "<", "<"));
1477 ms << ETag(from_ascii("alt"));
1478 ms << MTag(from_ascii("math"));
1479 ms << ETag(from_ascii("alt"));
1480 ms << MTag(from_ascii("math"));
1481 InsetMathGrid::mathmlize(ms);
1482 ms << ETag(from_ascii("math"));
1484 ms << MTag(from_ascii("alt role='tex'"));
1485 res = latex(buf, ls, runparams);
1486 ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&"), "<", "<"));
1487 ms << ETag(from_ascii("alt"));
1490 ms << from_ascii("<graphic fileref=\"eqn/");
1491 if (!label(0).empty())
1492 ms << sgml::cleanID(buf, runparams, label(0));
1494 ms << sgml::uniqueID(from_ascii("anon"));
1496 if (runparams.flavor == OutputParams::XML)
1497 ms << from_ascii("\"/>");
1499 ms << from_ascii("\">");
1502 return ms.line() + res;
1506 int InsetMathHull::textString(Buffer const & buf, odocstream & os,
1507 OutputParams const & op) const
1509 return plaintext(buf, os, op);