2 * \file InsetMathHull.cpp
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"
44 #include "OutputParams.h"
46 #include "TextPainter.h"
49 #include "insets/RenderPreview.h"
50 #include "insets/InsetLabel.h"
52 #include "graphics/PreviewImage.h"
53 #include "graphics/PreviewLoader.h"
55 #include "frontends/Painter.h"
57 #include "support/lyxlib.h"
58 #include "support/lstrings.h"
60 #include <boost/bind.hpp>
67 using cap::grabAndEraseSelection;
68 using support::bformat;
75 using std::istringstream;
76 using std::ostringstream;
84 int getCols(HullType type)
101 // returns position of first relation operator in the array
102 // used for "intelligent splitting"
103 size_t firstRelOp(MathData const & ar)
105 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
106 if ((*it)->isRelOp())
107 return it - ar.begin();
112 char const * star(bool numbered)
114 return numbered ? "" : "*";
118 } // end anon namespace
121 HullType hullType(docstring const & s)
123 if (s == "none") return hullNone;
124 if (s == "simple") return hullSimple;
125 if (s == "equation") return hullEquation;
126 if (s == "eqnarray") return hullEqnArray;
127 if (s == "align") return hullAlign;
128 if (s == "alignat") return hullAlignAt;
129 if (s == "xalignat") return hullXAlignAt;
130 if (s == "xxalignat") return hullXXAlignAt;
131 if (s == "multline") return hullMultline;
132 if (s == "gather") return hullGather;
133 if (s == "flalign") return hullFlAlign;
134 lyxerr << "unknown hull type '" << to_utf8(s) << "'" << endl;
139 docstring hullName(HullType type)
142 case hullNone: return from_ascii("none");
143 case hullSimple: return from_ascii("simple");
144 case hullEquation: return from_ascii("equation");
145 case hullEqnArray: return from_ascii("eqnarray");
146 case hullAlign: return from_ascii("align");
147 case hullAlignAt: return from_ascii("alignat");
148 case hullXAlignAt: return from_ascii("xalignat");
149 case hullXXAlignAt: return from_ascii("xxalignat");
150 case hullMultline: return from_ascii("multline");
151 case hullGather: return from_ascii("gather");
152 case hullFlAlign: return from_ascii("flalign");
154 lyxerr << "unknown hull type '" << type << "'" << endl;
155 return from_ascii("none");
160 InsetMathHull::InsetMathHull()
161 : InsetMathGrid(1, 1), type_(hullNone), nonum_(1), label_(1),
162 preview_(new RenderPreview(this))
164 //lyxerr << "sizeof InsetMath: " << sizeof(InsetMath) << endl;
165 //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl;
166 //lyxerr << "sizeof InsetMathChar: " << sizeof(InsetMathChar) << endl;
167 //lyxerr << "sizeof Font: " << sizeof(Font) << endl;
173 InsetMathHull::InsetMathHull(HullType type)
174 : InsetMathGrid(getCols(type), 1), type_(type), nonum_(1), label_(1),
175 preview_(new RenderPreview(this))
182 InsetMathHull::InsetMathHull(InsetMathHull const & other)
183 : InsetMathGrid(other),
184 type_(other.type_), nonum_(other.nonum_), label_(other.label_),
185 preview_(new RenderPreview(*other.preview_, this))
189 InsetMathHull::~InsetMathHull()
193 auto_ptr<Inset> InsetMathHull::doClone() const
195 return auto_ptr<Inset>(new InsetMathHull(*this));
199 InsetMathHull & InsetMathHull::operator=(InsetMathHull const & other)
203 *static_cast<InsetMathGrid*>(this) = InsetMathGrid(other);
205 nonum_ = other.nonum_;
206 label_ = other.label_;
207 preview_.reset(new RenderPreview(*other.preview_, this));
213 Inset * InsetMathHull::editXY(Cursor & cur, int x, int y)
219 return InsetMathNest::editXY(cur, x, y);
223 InsetMath::mode_type InsetMathHull::currentMode() const
225 if (type_ == hullNone)
226 return UNDECIDED_MODE;
227 // definitely math mode ...
232 bool InsetMathHull::idxFirst(Cursor & cur) const
240 bool InsetMathHull::idxLast(Cursor & cur) const
242 cur.idx() = nargs() - 1;
243 cur.pos() = cur.lastpos();
248 char InsetMathHull::defaultColAlign(col_type col)
250 if (type_ == hullEqnArray)
252 if (type_ >= hullAlign)
253 return "rl"[col & 1];
258 int InsetMathHull::defaultColSpace(col_type col)
260 if (type_ == hullAlign || type_ == hullAlignAt)
262 if (type_ == hullXAlignAt)
263 return (col & 1) ? 20 : 0;
264 if (type_ == hullXXAlignAt || type_ == hullFlAlign)
265 return (col & 1) ? 40 : 0;
270 docstring InsetMathHull::standardFont() const
272 return from_ascii(type_ == hullNone ? "lyxnochange" : "mathnormal");
276 bool InsetMathHull::previewState(BufferView * bv) const
278 if (!editing(bv) && RenderPreview::status() == LyXRC::PREVIEW_ON) {
279 graphics::PreviewImage const * pimage =
280 preview_->getPreviewImage(bv->buffer());
281 return pimage && pimage->image();
287 bool InsetMathHull::metrics(MetricsInfo & mi, Dimension & dim) const
289 if (previewState(mi.base.bv)) {
290 preview_->metrics(mi, dim);
291 // insert a one pixel gap in front of the formula
294 dim.des += displayMargin();
301 FontSetChanger dummy1(mi.base, standardFont());
302 StyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
304 // let the cells adjust themselves
305 InsetMathGrid::metrics(mi, dim);
308 dim.asc += displayMargin();
309 dim.des += displayMargin();
312 if (numberedType()) {
313 FontSetChanger dummy(mi.base, from_ascii("mathbf"));
315 for (row_type row = 0; row < nrows(); ++row)
316 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
322 // make it at least as high as the current font
325 math_font_max_dim(mi.base.font, asc, des);
326 dim.asc = max(dim.asc, asc);
327 dim.des = max(dim.des, des);
336 void InsetMathHull::draw(PainterInfo & pi, int x, int y) const
338 use_preview_ = previewState(pi.base.bv);
340 // background of mathed under focus is not painted because
341 // selection at the top level of nested inset is difficult to handle.
342 if (!editing(pi.base.bv))
343 pi.pain.fillRectangle(x + 1, y - ascent() + 1, width() - 2,
344 ascent() + descent() - 1, Color::mathbg);
347 // one pixel gap in front
348 preview_->draw(pi, x + 1, y);
349 setPosCache(pi, x, y);
353 FontSetChanger dummy1(pi.base, standardFont());
354 StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
355 InsetMathGrid::draw(pi, x + 1, y);
357 if (numberedType()) {
358 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
359 for (row_type row = 0; row < nrows(); ++row) {
360 int const yy = y + rowinfo_[row].offset_;
361 FontSetChanger dummy(pi.base, from_ascii("mathrm"));
362 docstring const nl = nicelabel(row);
366 setPosCache(pi, x, y);
370 void InsetMathHull::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
373 InsetMathGrid::metricsT(mi, dim);
376 WriteStream wi(os, false, true);
378 dim.wid = os.str().size();
385 void InsetMathHull::drawT(TextPainter & pain, int x, int y) const
388 InsetMathGrid::drawT(pain, x, y);
391 WriteStream wi(os, false, true);
393 pain.draw(x, y, os.str().c_str());
400 docstring const latex_string(InsetMathHull const & inset)
403 WriteStream wi(ls, false, false);
411 void InsetMathHull::addPreview(graphics::PreviewLoader & ploader) const
413 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
414 docstring const snippet = latex_string(*this);
415 preview_->addPreview(snippet, ploader);
420 bool InsetMathHull::notifyCursorLeaves(Cursor & cur)
422 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
423 Buffer const & buffer = cur.buffer();
424 docstring const snippet = latex_string(*this);
425 preview_->addPreview(snippet, buffer);
426 preview_->startLoading(buffer);
427 cur.updateFlags(Update::Force);
433 docstring InsetMathHull::label(row_type row) const
435 BOOST_ASSERT(row < nrows());
440 void InsetMathHull::label(row_type row, docstring const & label)
442 //lyxerr << "setting label '" << label << "' for row " << row << endl;
447 void InsetMathHull::numbered(row_type row, bool num)
455 bool InsetMathHull::numbered(row_type row) const
461 bool InsetMathHull::ams() const
464 type_ == hullAlign ||
465 type_ == hullFlAlign ||
466 type_ == hullMultline ||
467 type_ == hullGather ||
468 type_ == hullAlignAt ||
469 type_ == hullXAlignAt ||
470 type_ == hullXXAlignAt;
474 Inset::DisplayType InsetMathHull::display() const
476 return (type_ != hullSimple && type_ != hullNone) ? AlignCenter : Inline;
480 void InsetMathHull::getLabelList(Buffer const &, vector<docstring> & labels) const
482 for (row_type row = 0; row < nrows(); ++row)
483 if (!label_[row].empty() && nonum_[row] != 1)
484 labels.push_back(label_[row]);
488 bool InsetMathHull::numberedType() const
490 if (type_ == hullNone)
492 if (type_ == hullSimple)
494 if (type_ == hullXXAlignAt)
496 for (row_type row = 0; row < nrows(); ++row)
503 void InsetMathHull::validate(LaTeXFeatures & features) const
506 features.require("amsmath");
509 // Validation is necessary only if not using AMS math.
510 // To be safe, we will always run mathedvalidate.
511 //if (features.amsstyle)
514 features.require("boldsymbol");
515 //features.binom = true;
517 InsetMathGrid::validate(features);
521 void InsetMathHull::header_write(WriteStream & os) const
523 bool n = numberedType();
537 os << "\\begin{equation" << star(n) << "}\n";
547 os << "\\begin{" << hullName(type_) << star(n) << "}\n";
552 os << "\\begin{" << hullName(type_) << star(n) << '}'
553 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
557 os << "\\begin{" << hullName(type_) << '}'
558 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
562 os << "\\begin{unknown" << star(n) << '}';
568 void InsetMathHull::footer_write(WriteStream & os) const
570 bool n = numberedType();
583 os << "\\end{equation" << star(n) << "}\n";
595 os << "\\end{" << hullName(type_) << star(n) << "}\n";
599 os << "\\end{" << hullName(type_) << "}\n";
603 os << "\\end{unknown" << star(n) << '}';
609 bool InsetMathHull::rowChangeOK() const
612 type_ == hullEqnArray || type_ == hullAlign ||
613 type_ == hullFlAlign || type_ == hullAlignAt ||
614 type_ == hullXAlignAt || type_ == hullXXAlignAt ||
615 type_ == hullGather || type_ == hullMultline;
619 bool InsetMathHull::colChangeOK() const
622 type_ == hullAlign || type_ == hullFlAlign ||type_ == hullAlignAt ||
623 type_ == hullXAlignAt || type_ == hullXXAlignAt;
627 void InsetMathHull::addRow(row_type row)
631 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
632 label_.insert(label_.begin() + row + 1, docstring());
633 InsetMathGrid::addRow(row);
637 void InsetMathHull::swapRow(row_type row)
641 if (row + 1 == nrows())
643 swap(nonum_[row], nonum_[row + 1]);
644 swap(label_[row], label_[row + 1]);
645 InsetMathGrid::swapRow(row);
649 void InsetMathHull::delRow(row_type row)
651 if (nrows() <= 1 || !rowChangeOK())
653 InsetMathGrid::delRow(row);
654 // The last dummy row has no number info nor a label.
655 // Test nrows() + 1 because we have already erased the row.
656 if (row == nrows() + 1)
658 nonum_.erase(nonum_.begin() + row);
659 label_.erase(label_.begin() + row);
663 void InsetMathHull::addCol(col_type col)
667 InsetMathGrid::addCol(col);
671 void InsetMathHull::delCol(col_type col)
673 if (ncols() <= 1 || !colChangeOK())
675 InsetMathGrid::delCol(col);
679 docstring InsetMathHull::nicelabel(row_type row) const
683 if (label_[row].empty())
684 return from_ascii("(#)");
685 return '(' + label_[row] + ')';
689 void InsetMathHull::glueall()
692 for (idx_type i = 0; i < nargs(); ++i)
694 *this = InsetMathHull(hullSimple);
700 void InsetMathHull::splitTo2Cols()
702 BOOST_ASSERT(ncols() == 1);
703 InsetMathGrid::addCol(1);
704 for (row_type row = 0; row < nrows(); ++row) {
705 idx_type const i = 2 * row;
706 pos_type pos = firstRelOp(cell(i));
707 cell(i + 1) = MathData(cell(i).begin() + pos, cell(i).end());
708 cell(i).erase(pos, cell(i).size());
713 void InsetMathHull::splitTo3Cols()
715 BOOST_ASSERT(ncols() < 3);
718 InsetMathGrid::addCol(1);
719 for (row_type row = 0; row < nrows(); ++row) {
720 idx_type const i = 3 * row + 1;
721 if (cell(i).size()) {
722 cell(i + 1) = MathData(cell(i).begin() + 1, cell(i).end());
723 cell(i).erase(1, cell(i).size());
729 void InsetMathHull::changeCols(col_type cols)
733 else if (ncols() < cols) {
739 while (ncols() < cols)
740 InsetMathGrid::addCol(ncols() - 1);
746 for (row_type row = 0; row < nrows(); ++row) {
747 idx_type const i = row * ncols();
748 for (col_type col = cols; col < ncols(); ++col) {
749 cell(i + cols - 1).append(cell(i + col));
753 while (ncols() > cols) {
754 InsetMathGrid::delCol(ncols() - 1);
759 HullType InsetMathHull::getType() const
765 void InsetMathHull::setType(HullType type)
772 void InsetMathHull::mutate(HullType newtype)
774 //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
776 // we try to move along the chain
777 // none <-> simple <-> equation <-> eqnarray -> *align* -> multline, gather -+
779 // +-------------------------------------+
780 // we use eqnarray as intermediate type for mutations that are not
781 // directly supported because it handles labels and numbering for
784 if (newtype == type_) {
788 else if (newtype < hullNone) {
793 else if (type_ == hullNone) {
799 else if (type_ == hullSimple) {
800 if (newtype == hullNone) {
804 setType(hullEquation);
810 else if (type_ == hullEquation) {
811 if (newtype < type_) {
815 } else if (newtype == hullEqnArray) {
816 // split it "nicely" on the first relop
818 setType(hullEqnArray);
819 } else if (newtype == hullMultline || newtype == hullGather) {
829 else if (type_ == hullEqnArray) {
830 if (newtype < type_) {
831 // set correct (no)numbering
832 bool allnonum = true;
833 for (row_type row = 0; row < nrows(); ++row)
837 // set first non-empty label
839 for (row_type row = 0; row < nrows(); ++row) {
840 if (!label_[row].empty()) {
847 nonum_[0] = allnonum;
850 } else { // align & Co.
857 else if (type_ == hullAlign || type_ == hullAlignAt ||
858 type_ == hullXAlignAt || type_ == hullFlAlign) {
859 if (newtype < hullAlign) {
861 setType(hullEqnArray);
863 } else if (newtype == hullGather || newtype == hullMultline) {
866 } else if (newtype == hullXXAlignAt) {
867 for (row_type row = 0; row < nrows(); ++row)
868 numbered(row, false);
875 else if (type_ == hullXXAlignAt) {
876 for (row_type row = 0; row < nrows(); ++row)
877 numbered(row, false);
878 if (newtype < hullAlign) {
880 setType(hullEqnArray);
882 } else if (newtype == hullGather || newtype == hullMultline) {
890 else if (type_ == hullMultline || type_ == hullGather) {
891 if (newtype == hullGather || newtype == hullMultline)
893 else if (newtype == hullAlign || newtype == hullFlAlign ||
894 newtype == hullAlignAt || newtype == hullXAlignAt) {
897 } else if (newtype == hullXXAlignAt) {
899 for (row_type row = 0; row < nrows(); ++row)
900 numbered(row, false);
904 setType(hullEqnArray);
910 lyxerr << "mutation from '" << to_utf8(hullName(type_))
911 << "' to '" << to_utf8(hullName(newtype))
912 << "' not implemented" << endl;
917 docstring InsetMathHull::eolString(row_type row, bool emptyline, bool fragile) const
920 if (numberedType()) {
921 if (!label_[row].empty() && !nonum_[row])
922 res += "\\label{" + label_[row] + '}';
923 if (nonum_[row] && (type_ != hullMultline))
924 res += "\\nonumber ";
926 return res + InsetMathGrid::eolString(row, emptyline, fragile);
930 void InsetMathHull::write(WriteStream & os) const
933 InsetMathGrid::write(os);
938 void InsetMathHull::normalize(NormalStream & os) const
940 os << "[formula " << hullName(type_) << ' ';
941 InsetMathGrid::normalize(os);
946 void InsetMathHull::mathmlize(MathStream & os) const
948 InsetMathGrid::mathmlize(os);
952 void InsetMathHull::infoize(odocstream & os) const
954 os << "Type: " << hullName(type_);
958 void InsetMathHull::check() const
960 BOOST_ASSERT(nonum_.size() == nrows());
961 BOOST_ASSERT(label_.size() == nrows());
965 void InsetMathHull::doExtern(Cursor & cur, FuncRequest & func)
969 idocstringstream iss(func.argument());
970 iss >> dlang >> extra;
972 extra = from_ascii("noextra");
973 std::string const lang = to_ascii(dlang);
975 // FIXME: temporarily disabled
976 //if (cur.selection()) {
979 // lyxerr << "use selection: " << ar << endl;
980 // insert(pipeThroughExtern(lang, extra, ar));
985 eq.push_back(MathAtom(new InsetMathChar('=')));
987 // go to first item in line
988 cur.idx() -= cur.idx() % ncols();
991 if (getType() == hullSimple) {
992 size_type pos = cur.cell().find_last(eq);
994 if (cur.inMathed() && cur.selection()) {
995 asArray(grabAndEraseSelection(cur), ar);
996 } else if (pos == cur.cell().size()) {
998 lyxerr << "use whole cell: " << ar << endl;
1000 ar = MathData(cur.cell().begin() + pos + 1, cur.cell().end());
1001 lyxerr << "use partial cell form pos: " << pos << endl;
1003 cur.cell().append(eq);
1004 cur.cell().append(pipeThroughExtern(lang, extra, ar));
1005 cur.pos() = cur.lastpos();
1009 if (getType() == hullEquation) {
1010 lyxerr << "use equation inset" << endl;
1011 mutate(hullEqnArray);
1012 MathData & ar = cur.cell();
1013 lyxerr << "use cell: " << ar << endl;
1017 cur.cell() = pipeThroughExtern(lang, extra, ar);
1018 // move to end of line
1019 cur.pos() = cur.lastpos();
1024 lyxerr << "use eqnarray" << endl;
1025 cur.idx() += 2 - cur.idx() % ncols();
1027 MathData ar = cur.cell();
1028 lyxerr << "use cell: " << ar << endl;
1029 // FIXME: temporarily disabled
1035 cur.cell() = pipeThroughExtern(lang, extra, ar);
1036 cur.pos() = cur.lastpos();
1041 void InsetMathHull::doDispatch(Cursor & cur, FuncRequest & cmd)
1043 //lyxerr << "action: " << cmd.action << endl;
1044 switch (cmd.action) {
1046 case LFUN_FINISHED_LEFT:
1047 case LFUN_FINISHED_RIGHT:
1048 //lyxerr << "action: " << cmd.action << endl;
1049 InsetMathGrid::doDispatch(cur, cmd);
1050 notifyCursorLeaves(cur);
1054 case LFUN_BREAK_PARAGRAPH:
1055 // just swallow this
1058 case LFUN_BREAK_LINE:
1059 // some magic for the common case
1060 if (type_ == hullSimple || type_ == hullEquation) {
1061 recordUndoInset(cur);
1063 cur.bv().buffer().params().use_amsmath == BufferParams::package_on;
1064 mutate(align ? hullAlign : hullEqnArray);
1066 cur.pos() = cur.lastpos();
1068 InsetMathGrid::doDispatch(cur, cmd);
1071 case LFUN_MATH_NUMBER:
1072 //lyxerr << "toggling all numbers" << endl;
1074 recordUndoInset(cur);
1075 bool old = numberedType();
1076 if (type_ == hullMultline)
1077 numbered(nrows() - 1, !old);
1079 for (row_type row = 0; row < nrows(); ++row)
1080 numbered(row, !old);
1082 cur.message(old ? _("No number") : _("Number"));
1086 case LFUN_MATH_NONUMBER:
1088 recordUndoInset(cur);
1089 row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1090 bool old = numbered(r);
1091 cur.message(old ? _("No number") : _("Number"));
1096 case LFUN_LABEL_INSERT: {
1097 recordUndoInset(cur);
1098 row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1099 docstring old_label = label(r);
1100 docstring const default_label = from_ascii(
1101 (lyxrc.label_init_length >= 0) ? "eq:" : "");
1102 if (old_label.empty())
1103 old_label = default_label;
1105 InsetCommandParams p("label");
1106 p["name"] = cmd.argument().empty() ? old_label : cmd.argument();
1107 std::string const data = InsetCommandMailer::params2string("label", p);
1109 if (cmd.argument().empty())
1110 cur.bv().showInsetDialog("label", data, 0);
1112 FuncRequest fr(LFUN_INSET_INSERT, data);
1118 case LFUN_INSET_INSERT: {
1119 //lyxerr << "arg: " << to_utf8(cmd.argument()) << endl;
1120 std::string const name = cmd.getArg(0);
1121 if (name == "label") {
1122 InsetCommandParams p("label");
1123 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), p);
1124 docstring str = p["name"];
1125 recordUndoInset(cur);
1126 row_type const r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1127 str = support::trim(str);
1130 docstring old = label(r);
1132 cur.bv().buffer().changeRefsIfUnique(old, str,
1138 InsetMathGrid::doDispatch(cur, cmd);
1142 case LFUN_MATH_EXTERN:
1143 recordUndoInset(cur);
1147 case LFUN_MATH_MUTATE: {
1148 recordUndoInset(cur);
1149 row_type row = cur.row();
1150 col_type col = cur.col();
1151 mutate(hullType(cmd.argument()));
1152 cur.idx() = row * ncols() + col;
1153 if (cur.idx() > cur.lastidx()) {
1154 cur.idx() = cur.lastidx();
1155 cur.pos() = cur.lastpos();
1157 if (cur.pos() > cur.lastpos())
1158 cur.pos() = cur.lastpos();
1159 //cur.dispatched(FINISHED);
1163 case LFUN_MATH_DISPLAY: {
1164 recordUndoInset(cur);
1165 mutate(type_ == hullSimple ? hullEquation : hullSimple);
1167 cur.pos() = cur.lastpos();
1168 //cur.dispatched(FINISHED);
1173 InsetMathGrid::doDispatch(cur, cmd);
1179 bool InsetMathHull::getStatus(Cursor & cur, FuncRequest const & cmd,
1180 FuncStatus & status) const
1182 switch (cmd.action) {
1183 case LFUN_FINISHED_LEFT:
1184 case LFUN_FINISHED_RIGHT:
1187 status.enabled(true);
1189 case LFUN_BREAK_LINE:
1190 case LFUN_MATH_NUMBER:
1191 case LFUN_MATH_NONUMBER:
1192 case LFUN_MATH_EXTERN:
1193 case LFUN_MATH_MUTATE:
1194 case LFUN_MATH_DISPLAY:
1196 status.enabled(true);
1198 case LFUN_LABEL_INSERT:
1199 status.enabled(type_ != hullSimple);
1201 case LFUN_INSET_INSERT:
1202 if (cmd.getArg(0) == "label") {
1203 status.enabled(type_ != hullSimple);
1206 return InsetMathGrid::getStatus(cur, cmd, status);
1207 case LFUN_TABULAR_FEATURE: {
1208 istringstream is(to_utf8(cmd.argument()));
1212 && (s == "append-row"
1213 || s == "delete-row"
1214 || s == "copy-row")) {
1215 status.message(bformat(
1216 from_utf8(N_("Can't change number of rows in '%1$s'")),
1218 status.enabled(false);
1222 && (s == "append-column"
1223 || s == "delete-column"
1224 || s == "copy-column")) {
1225 status.message(bformat(
1226 from_utf8(N_("Can't change number of columns in '%1$s'")),
1228 status.enabled(false);
1231 if ((type_ == hullSimple
1232 || type_ == hullEquation
1233 || type_ == hullNone) &&
1234 (s == "add-hline-above" || s == "add-hline-below")) {
1235 status.message(bformat(
1236 from_utf8(N_("Can't add horizontal grid lines in '%1$s'")),
1238 status.enabled(false);
1241 if (s == "add-vline-left" || s == "add-vline-right") {
1242 status.message(bformat(
1243 from_utf8(N_("Can't add vertical grid lines in '%1$s'")),
1245 status.enabled(false);
1248 if (s == "valign-top" || s == "valign-middle"
1249 || s == "valign-bottom" || s == "align-left"
1250 || s == "align-center" || s == "align-right") {
1251 status.enabled(false);
1254 return InsetMathGrid::getStatus(cur, cmd, status);
1257 return InsetMathGrid::getStatus(cur, cmd, status);
1260 // This cannot really happen, but inserted to shut-up gcc
1261 return InsetMathGrid::getStatus(cur, cmd, status);
1265 /////////////////////////////////////////////////////////////////////
1269 // simply scrap this function if you want
1270 void InsetMathHull::mutateToText()
1273 // translate to latex
1275 latex(NULL, os, false, false);
1276 string str = os.str();
1279 Text * lt = view_->cursor().innerText();
1280 string::const_iterator cit = str.begin();
1281 string::const_iterator end = str.end();
1282 for (; cit != end; ++cit)
1283 view_->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
1286 //dispatch(LFUN_ESCAPE);
1291 void InsetMathHull::handleFont(Cursor & cur, docstring const & arg,
1292 docstring const & font)
1294 // this whole function is a hack and won't work for incremental font
1297 if (cur.inset().asInsetMath()->name() == font)
1298 cur.handleFont(to_utf8(font));
1300 cur.handleNest(createInsetMath(font));
1306 void InsetMathHull::handleFont2(Cursor & cur, docstring const & arg)
1311 bv_funcs::string2font(to_utf8(arg), font, b);
1312 if (font.color() != Color::inherit) {
1313 MathAtom at = MathAtom(new InsetMathColor(true, font.color()));
1314 cur.handleNest(at, 0);
1319 void InsetMathHull::edit(Cursor & cur, bool left)
1322 left ? idxFirst(cur) : idxLast(cur);
1323 // The inset formula dimension is not necessarily the same as the
1324 // one of the instant preview image, so we have to indicate to the
1325 // BufferView that a metrics update is needed.
1326 cur.updateFlags(Update::Force);
1330 docstring const InsetMathHull::editMessage() const
1332 return _("Math editor mode");
1336 void InsetMathHull::revealCodes(Cursor & cur) const
1338 if (!cur.inMathed())
1340 odocstringstream os;
1342 cur.message(os.str());
1344 // write something to the minibuffer
1345 // translate to latex
1349 string str = os.str();
1351 string::size_type pos = 0;
1353 for (string::iterator it = str.begin(); it != str.end(); ++it) {
1356 else if (*it == '\0') {
1358 pos = it - str.begin();
1364 res = res.substr(pos - 30);
1365 if (res.size() > 60)
1366 res = res.substr(0, 60);
1372 Inset::Code InsetMathHull::lyxCode() const
1378 /////////////////////////////////////////////////////////////////////
1382 bool InsetMathHull::searchForward(BufferView * bv, string const & str,
1385 // FIXME: completely broken
1386 static InsetMathHull * lastformula = 0;
1387 static CursorBase current = DocIterator(ibegin(nucleus()));
1389 static string laststr;
1391 if (lastformula != this || laststr != str) {
1392 //lyxerr << "reset lastformula to " << this << endl;
1395 current = ibegin(nucleus());
1397 mathed_parse_cell(ar, str);
1401 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
1403 for (DocIterator it = current; it != iend(nucleus()); increment(it)) {
1404 CursorSlice & top = it.back();
1405 MathData const & a = top.asInsetMath()->cell(top.idx_);
1406 if (a.matchpart(ar, top.pos_)) {
1407 bv->cursor().setSelection(it, ar.size());
1409 top.pos_ += ar.size();
1415 //lyxerr << "not found!" << endl;
1422 void InsetMathHull::write(Buffer const &, std::ostream & os) const
1424 odocstringstream oss;
1425 WriteStream wi(oss, false, false);
1428 os << to_utf8(oss.str());
1432 void InsetMathHull::read(Buffer const &, Lexer & lex)
1435 mathed_parse_normal(at, lex);
1436 operator=(*at->asHullInset());
1440 int InsetMathHull::plaintext(Buffer const &, odocstream & os,
1441 OutputParams const &) const
1443 if (0 && display()) {
1447 TextPainter tpain(dim.width(), dim.height());
1448 drawT(tpain, 0, dim.ascent());
1450 // reset metrics cache to "real" values
1452 return tpain.textheight();
1454 odocstringstream oss;
1455 WriteStream wi(oss, false, true);
1458 docstring const str = oss.str();
1465 int InsetMathHull::docbook(Buffer const & buf, odocstream & os,
1466 OutputParams const & runparams) const
1471 if (getType() == hullSimple)
1472 name = from_ascii("inlineequation");
1474 name = from_ascii("informalequation");
1476 docstring bname = name;
1477 if (!label(0).empty())
1478 bname += " id='" + sgml::cleanID(buf, runparams, label(0)) + "'";
1481 odocstringstream ls;
1482 if (runparams.flavor == OutputParams::XML) {
1483 ms << MTag(from_ascii("alt role='tex' "));
1484 // Workaround for db2latex: db2latex always includes equations with
1485 // \ensuremath{} or \begin{display}\end{display}
1486 // so we strip LyX' math environment
1487 WriteStream wi(ls, false, false);
1488 InsetMathGrid::write(wi);
1489 ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&"), "<", "<"));
1490 ms << ETag(from_ascii("alt"));
1491 ms << MTag(from_ascii("math"));
1492 ms << ETag(from_ascii("alt"));
1493 ms << MTag(from_ascii("math"));
1494 InsetMathGrid::mathmlize(ms);
1495 ms << ETag(from_ascii("math"));
1497 ms << MTag(from_ascii("alt role='tex'"));
1498 res = latex(buf, ls, runparams);
1499 ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&"), "<", "<"));
1500 ms << ETag(from_ascii("alt"));
1503 ms << from_ascii("<graphic fileref=\"eqn/");
1504 if (!label(0).empty())
1505 ms << sgml::cleanID(buf, runparams, label(0));
1507 ms << sgml::uniqueID(from_ascii("anon"));
1509 if (runparams.flavor == OutputParams::XML)
1510 ms << from_ascii("\"/>");
1512 ms << from_ascii("\">");
1515 return ms.line() + res;
1519 void InsetMathHull::textString(Buffer const & buf, odocstream & os) const
1521 plaintext(buf, os, OutputParams(0));