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"
29 #include "BufferParams.h"
30 #include "BufferView.h"
31 #include "CutAndPaste.h"
32 #include "FuncStatus.h"
33 #include "LaTeXFeatures.h"
36 #include "DispatchResult.h"
37 #include "FuncRequest.h"
40 #include "OutputParams.h"
43 #include "TextPainter.h"
45 #include "insets/RenderPreview.h"
46 #include "insets/InsetLabel.h"
48 #include "graphics/PreviewImage.h"
49 #include "graphics/PreviewLoader.h"
51 #include "frontends/Painter.h"
53 #include "support/lyxlib.h"
54 #include "support/lstrings.h"
56 #include <boost/bind.hpp>
63 using cap::grabAndEraseSelection;
64 using support::bformat;
70 using std::istringstream;
71 using std::ostringstream;
78 int getCols(HullType type)
95 // returns position of first relation operator in the array
96 // used for "intelligent splitting"
97 size_t firstRelOp(MathData const & ar)
99 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
100 if ((*it)->isRelOp())
101 return it - ar.begin();
106 char const * star(bool numbered)
108 return numbered ? "" : "*";
112 } // end anon namespace
115 HullType hullType(docstring const & s)
117 if (s == "none") return hullNone;
118 if (s == "simple") return hullSimple;
119 if (s == "equation") return hullEquation;
120 if (s == "eqnarray") return hullEqnArray;
121 if (s == "align") return hullAlign;
122 if (s == "alignat") return hullAlignAt;
123 if (s == "xalignat") return hullXAlignAt;
124 if (s == "xxalignat") return hullXXAlignAt;
125 if (s == "multline") return hullMultline;
126 if (s == "gather") return hullGather;
127 if (s == "flalign") return hullFlAlign;
128 lyxerr << "unknown hull type '" << to_utf8(s) << "'" << endl;
133 docstring hullName(HullType type)
136 case hullNone: return from_ascii("none");
137 case hullSimple: return from_ascii("simple");
138 case hullEquation: return from_ascii("equation");
139 case hullEqnArray: return from_ascii("eqnarray");
140 case hullAlign: return from_ascii("align");
141 case hullAlignAt: return from_ascii("alignat");
142 case hullXAlignAt: return from_ascii("xalignat");
143 case hullXXAlignAt: return from_ascii("xxalignat");
144 case hullMultline: return from_ascii("multline");
145 case hullGather: return from_ascii("gather");
146 case hullFlAlign: return from_ascii("flalign");
148 lyxerr << "unknown hull type '" << type << "'" << endl;
149 return from_ascii("none");
154 InsetMathHull::InsetMathHull()
155 : InsetMathGrid(1, 1), type_(hullNone), nonum_(1), label_(1),
156 preview_(new RenderPreview(this))
158 //lyxerr << "sizeof InsetMath: " << sizeof(InsetMath) << endl;
159 //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl;
160 //lyxerr << "sizeof InsetMathChar: " << sizeof(InsetMathChar) << endl;
161 //lyxerr << "sizeof FontInfo: " << sizeof(FontInfo) << endl;
167 InsetMathHull::InsetMathHull(HullType type)
168 : InsetMathGrid(getCols(type), 1), type_(type), nonum_(1), label_(1),
169 preview_(new RenderPreview(this))
176 InsetMathHull::InsetMathHull(InsetMathHull const & other)
177 : InsetMathGrid(other),
178 type_(other.type_), nonum_(other.nonum_), label_(other.label_),
179 preview_(new RenderPreview(*other.preview_, this))
183 InsetMathHull::~InsetMathHull()
187 Inset * InsetMathHull::clone() const
189 return new InsetMathHull(*this);
193 InsetMathHull & InsetMathHull::operator=(InsetMathHull const & other)
197 *static_cast<InsetMathGrid*>(this) = InsetMathGrid(other);
199 nonum_ = other.nonum_;
200 label_ = other.label_;
201 preview_.reset(new RenderPreview(*other.preview_, this));
207 Inset * InsetMathHull::editXY(Cursor & cur, int x, int y)
213 return InsetMathNest::editXY(cur, x, y);
217 InsetMath::mode_type InsetMathHull::currentMode() const
219 if (type_ == hullNone)
220 return UNDECIDED_MODE;
221 // definitely math mode ...
226 bool InsetMathHull::idxFirst(Cursor & cur) const
234 bool InsetMathHull::idxLast(Cursor & cur) const
236 cur.idx() = nargs() - 1;
237 cur.pos() = cur.lastpos();
242 char InsetMathHull::defaultColAlign(col_type col)
244 if (type_ == hullEqnArray)
246 if (type_ >= hullAlign)
247 return "rl"[col & 1];
252 int InsetMathHull::defaultColSpace(col_type col)
254 if (type_ == hullAlign || type_ == hullAlignAt)
256 if (type_ == hullXAlignAt)
257 return (col & 1) ? 20 : 0;
258 if (type_ == hullXXAlignAt || type_ == hullFlAlign)
259 return (col & 1) ? 40 : 0;
264 docstring InsetMathHull::standardFont() const
266 return from_ascii(type_ == hullNone ? "lyxnochange" : "mathnormal");
270 bool InsetMathHull::previewState(BufferView * bv) const
272 if (!editing(bv) && RenderPreview::status() == LyXRC::PREVIEW_ON) {
273 graphics::PreviewImage const * pimage =
274 preview_->getPreviewImage(bv->buffer());
275 return pimage && pimage->image();
281 void InsetMathHull::metrics(MetricsInfo & mi, Dimension & dim) const
283 if (previewState(mi.base.bv)) {
284 preview_->metrics(mi, dim);
285 // insert a one pixel gap in front of the formula
288 dim.des += displayMargin();
289 // Cache the inset dimension.
290 setDimCache(mi, dim);
294 FontSetChanger dummy1(mi.base, standardFont());
295 StyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
297 // let the cells adjust themselves
298 InsetMathGrid::metrics(mi, dim);
301 dim.asc += displayMargin();
302 dim.des += displayMargin();
305 if (numberedType()) {
306 FontSetChanger dummy(mi.base, from_ascii("mathbf"));
308 for (row_type row = 0; row < nrows(); ++row)
309 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
315 // make it at least as high as the current font
318 math_font_max_dim(mi.base.font, asc, des);
319 dim.asc = max(dim.asc, asc);
320 dim.des = max(dim.des, des);
321 // Cache the inset dimension.
322 // FIXME: This will overwrite InsetMathGrid dimension, is that OK?
323 setDimCache(mi, dim);
327 void InsetMathHull::draw(PainterInfo & pi, int x, int y) const
329 use_preview_ = previewState(pi.base.bv);
330 Dimension const dim = dimension(*pi.base.bv);
332 // background of mathed under focus is not painted because
333 // selection at the top level of nested inset is difficult to handle.
334 if (!editing(pi.base.bv))
335 pi.pain.fillRectangle(x + 1, y - dim.asc + 1, dim.wid - 2,
336 dim.asc + dim.des - 1, Color_mathbg);
339 // one pixel gap in front
340 preview_->draw(pi, x + 1, y);
341 setPosCache(pi, x, y);
345 FontSetChanger dummy1(pi.base, standardFont());
346 StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
347 InsetMathGrid::draw(pi, x + 1, y);
349 if (numberedType()) {
350 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
351 for (row_type row = 0; row < nrows(); ++row) {
352 int const yy = y + rowinfo_[row].offset_;
353 FontSetChanger dummy(pi.base, from_ascii("mathrm"));
354 docstring const nl = nicelabel(row);
358 setPosCache(pi, x, y);
362 void InsetMathHull::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
365 InsetMathGrid::metricsT(mi, dim);
368 WriteStream wi(os, false, true);
370 dim.wid = os.str().size();
377 void InsetMathHull::drawT(TextPainter & pain, int x, int y) const
380 InsetMathGrid::drawT(pain, x, y);
383 WriteStream wi(os, false, true);
385 pain.draw(x, y, os.str().c_str());
392 docstring const latex_string(InsetMathHull const & inset)
395 WriteStream wi(ls, false, false);
403 void InsetMathHull::addPreview(graphics::PreviewLoader & ploader) const
405 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
406 docstring const snippet = latex_string(*this);
407 preview_->addPreview(snippet, ploader);
412 bool InsetMathHull::notifyCursorLeaves(Cursor & cur)
414 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
415 Buffer const & buffer = cur.buffer();
416 docstring const snippet = latex_string(*this);
417 preview_->addPreview(snippet, buffer);
418 preview_->startLoading(buffer);
419 cur.updateFlags(Update::Force);
425 docstring InsetMathHull::label(row_type row) const
427 BOOST_ASSERT(row < nrows());
432 void InsetMathHull::label(row_type row, docstring const & label)
434 //lyxerr << "setting label '" << label << "' for row " << row << endl;
439 void InsetMathHull::numbered(row_type row, bool num)
447 bool InsetMathHull::numbered(row_type row) const
453 bool InsetMathHull::ams() const
456 type_ == hullAlign ||
457 type_ == hullFlAlign ||
458 type_ == hullMultline ||
459 type_ == hullGather ||
460 type_ == hullAlignAt ||
461 type_ == hullXAlignAt ||
462 type_ == hullXXAlignAt;
466 Inset::DisplayType InsetMathHull::display() const
468 return (type_ != hullSimple && type_ != hullNone) ? AlignCenter : Inline;
472 void InsetMathHull::getLabelList(Buffer const &, vector<docstring> & labels) const
474 for (row_type row = 0; row < nrows(); ++row)
475 if (!label_[row].empty() && nonum_[row] != 1)
476 labels.push_back(label_[row]);
480 bool InsetMathHull::numberedType() const
482 if (type_ == hullNone)
484 if (type_ == hullSimple)
486 if (type_ == hullXXAlignAt)
488 for (row_type row = 0; row < nrows(); ++row)
495 void InsetMathHull::validate(LaTeXFeatures & features) const
498 features.require("amsmath");
501 // Validation is necessary only if not using AMS math.
502 // To be safe, we will always run mathedvalidate.
503 //if (features.amsstyle)
506 features.require("boldsymbol");
507 //features.binom = true;
509 InsetMathGrid::validate(features);
513 void InsetMathHull::header_write(WriteStream & os) const
515 bool n = numberedType();
529 os << "\\begin{equation" << star(n) << "}\n";
539 os << "\\begin{" << hullName(type_) << star(n) << "}\n";
544 os << "\\begin{" << hullName(type_) << star(n) << '}'
545 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
549 os << "\\begin{" << hullName(type_) << '}'
550 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
554 os << "\\begin{unknown" << star(n) << '}';
560 void InsetMathHull::footer_write(WriteStream & os) const
562 bool n = numberedType();
575 os << "\\end{equation" << star(n) << "}\n";
587 os << "\\end{" << hullName(type_) << star(n) << "}\n";
591 os << "\\end{" << hullName(type_) << "}\n";
595 os << "\\end{unknown" << star(n) << '}';
601 bool InsetMathHull::rowChangeOK() const
604 type_ == hullEqnArray || type_ == hullAlign ||
605 type_ == hullFlAlign || type_ == hullAlignAt ||
606 type_ == hullXAlignAt || type_ == hullXXAlignAt ||
607 type_ == hullGather || type_ == hullMultline;
611 bool InsetMathHull::colChangeOK() const
614 type_ == hullAlign || type_ == hullFlAlign ||type_ == hullAlignAt ||
615 type_ == hullXAlignAt || type_ == hullXXAlignAt;
619 void InsetMathHull::addRow(row_type row)
623 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
624 label_.insert(label_.begin() + row + 1, docstring());
625 InsetMathGrid::addRow(row);
629 void InsetMathHull::swapRow(row_type row)
633 if (row + 1 == nrows())
635 swap(nonum_[row], nonum_[row + 1]);
636 swap(label_[row], label_[row + 1]);
637 InsetMathGrid::swapRow(row);
641 void InsetMathHull::delRow(row_type row)
643 if (nrows() <= 1 || !rowChangeOK())
645 InsetMathGrid::delRow(row);
646 // The last dummy row has no number info nor a label.
647 // Test nrows() + 1 because we have already erased the row.
648 if (row == nrows() + 1)
650 nonum_.erase(nonum_.begin() + row);
651 label_.erase(label_.begin() + row);
655 void InsetMathHull::addCol(col_type col)
659 InsetMathGrid::addCol(col);
663 void InsetMathHull::delCol(col_type col)
665 if (ncols() <= 1 || !colChangeOK())
667 InsetMathGrid::delCol(col);
671 docstring InsetMathHull::nicelabel(row_type row) const
675 if (label_[row].empty())
676 return from_ascii("(#)");
677 return '(' + label_[row] + ')';
681 void InsetMathHull::glueall()
684 for (idx_type i = 0; i < nargs(); ++i)
686 *this = InsetMathHull(hullSimple);
692 void InsetMathHull::splitTo2Cols()
694 BOOST_ASSERT(ncols() == 1);
695 InsetMathGrid::addCol(1);
696 for (row_type row = 0; row < nrows(); ++row) {
697 idx_type const i = 2 * row;
698 pos_type pos = firstRelOp(cell(i));
699 cell(i + 1) = MathData(cell(i).begin() + pos, cell(i).end());
700 cell(i).erase(pos, cell(i).size());
705 void InsetMathHull::splitTo3Cols()
707 BOOST_ASSERT(ncols() < 3);
710 InsetMathGrid::addCol(2);
711 for (row_type row = 0; row < nrows(); ++row) {
712 idx_type const i = 3 * row + 1;
713 if (cell(i).size()) {
714 cell(i + 1) = MathData(cell(i).begin() + 1, cell(i).end());
715 cell(i).erase(1, cell(i).size());
721 void InsetMathHull::changeCols(col_type cols)
725 else if (ncols() < cols) {
731 while (ncols() < cols)
732 InsetMathGrid::addCol(ncols());
738 for (row_type row = 0; row < nrows(); ++row) {
739 idx_type const i = row * ncols();
740 for (col_type col = cols; col < ncols(); ++col) {
741 cell(i + cols - 1).append(cell(i + col));
745 while (ncols() > cols) {
746 InsetMathGrid::delCol(ncols() - 1);
751 HullType InsetMathHull::getType() const
757 void InsetMathHull::setType(HullType type)
764 void InsetMathHull::mutate(HullType newtype)
766 //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
768 // we try to move along the chain
769 // none <-> simple <-> equation <-> eqnarray -> *align* -> multline, gather -+
771 // +-------------------------------------+
772 // we use eqnarray as intermediate type for mutations that are not
773 // directly supported because it handles labels and numbering for
776 if (newtype == type_) {
780 else if (newtype < hullNone) {
785 else if (type_ == hullNone) {
791 else if (type_ == hullSimple) {
792 if (newtype == hullNone) {
796 setType(hullEquation);
802 else if (type_ == hullEquation) {
803 if (newtype < type_) {
807 } else if (newtype == hullEqnArray) {
808 // split it "nicely" on the first relop
810 setType(hullEqnArray);
811 } else if (newtype == hullMultline || newtype == hullGather) {
821 else if (type_ == hullEqnArray) {
822 if (newtype < type_) {
823 // set correct (no)numbering
824 bool allnonum = true;
825 for (row_type row = 0; row < nrows(); ++row)
829 // set first non-empty label
831 for (row_type row = 0; row < nrows(); ++row) {
832 if (!label_[row].empty()) {
839 nonum_[0] = allnonum;
842 } else { // align & Co.
849 else if (type_ == hullAlign || type_ == hullAlignAt ||
850 type_ == hullXAlignAt || type_ == hullFlAlign) {
851 if (newtype < hullAlign) {
853 setType(hullEqnArray);
855 } else if (newtype == hullGather || newtype == hullMultline) {
858 } else if (newtype == hullXXAlignAt) {
859 for (row_type row = 0; row < nrows(); ++row)
860 numbered(row, false);
867 else if (type_ == hullXXAlignAt) {
868 for (row_type row = 0; row < nrows(); ++row)
869 numbered(row, false);
870 if (newtype < hullAlign) {
872 setType(hullEqnArray);
874 } else if (newtype == hullGather || newtype == hullMultline) {
882 else if (type_ == hullMultline || type_ == hullGather) {
883 if (newtype == hullGather || newtype == hullMultline)
885 else if (newtype == hullAlign || newtype == hullFlAlign ||
886 newtype == hullAlignAt || newtype == hullXAlignAt) {
889 } else if (newtype == hullXXAlignAt) {
891 for (row_type row = 0; row < nrows(); ++row)
892 numbered(row, false);
896 setType(hullEqnArray);
902 lyxerr << "mutation from '" << to_utf8(hullName(type_))
903 << "' to '" << to_utf8(hullName(newtype))
904 << "' not implemented" << endl;
909 docstring InsetMathHull::eolString(row_type row, bool emptyline, bool fragile) const
912 if (numberedType()) {
913 if (!label_[row].empty() && !nonum_[row])
914 res += "\\label{" + label_[row] + '}';
915 if (nonum_[row] && (type_ != hullMultline))
916 res += "\\nonumber ";
918 return res + InsetMathGrid::eolString(row, emptyline, fragile);
922 void InsetMathHull::write(WriteStream & os) const
925 InsetMathGrid::write(os);
930 void InsetMathHull::normalize(NormalStream & os) const
932 os << "[formula " << hullName(type_) << ' ';
933 InsetMathGrid::normalize(os);
938 void InsetMathHull::mathmlize(MathStream & os) const
940 InsetMathGrid::mathmlize(os);
944 void InsetMathHull::infoize(odocstream & os) const
946 os << "Type: " << hullName(type_);
950 void InsetMathHull::check() const
952 BOOST_ASSERT(nonum_.size() == nrows());
953 BOOST_ASSERT(label_.size() == nrows());
957 void InsetMathHull::doExtern(Cursor & cur, FuncRequest & func)
961 idocstringstream iss(func.argument());
962 iss >> dlang >> extra;
964 extra = from_ascii("noextra");
965 std::string const lang = to_ascii(dlang);
967 // FIXME: temporarily disabled
968 //if (cur.selection()) {
971 // lyxerr << "use selection: " << ar << endl;
972 // insert(pipeThroughExtern(lang, extra, ar));
977 eq.push_back(MathAtom(new InsetMathChar('=')));
979 // go to first item in line
980 cur.idx() -= cur.idx() % ncols();
983 if (getType() == hullSimple) {
984 size_type pos = cur.cell().find_last(eq);
986 if (cur.inMathed() && cur.selection()) {
987 asArray(grabAndEraseSelection(cur), ar);
988 } else if (pos == cur.cell().size()) {
990 lyxerr << "use whole cell: " << ar << endl;
992 ar = MathData(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 MathData & 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 MathData ar = cur.cell();
1020 lyxerr << "use cell: " << ar << endl;
1021 // FIXME: temporarily disabled
1027 cur.cell() = pipeThroughExtern(lang, extra, ar);
1028 cur.pos() = cur.lastpos();
1033 void InsetMathHull::doDispatch(Cursor & cur, FuncRequest & cmd)
1035 //lyxerr << "action: " << cmd.action << endl;
1036 switch (cmd.action) {
1038 case LFUN_FINISHED_BACKWARD:
1039 case LFUN_FINISHED_FORWARD:
1040 //lyxerr << "action: " << cmd.action << endl;
1041 InsetMathGrid::doDispatch(cur, cmd);
1042 notifyCursorLeaves(cur);
1046 case LFUN_BREAK_PARAGRAPH:
1047 // just swallow this
1050 case LFUN_BREAK_LINE:
1051 // some magic for the common case
1052 if (type_ == hullSimple || type_ == hullEquation) {
1053 cur.recordUndoInset();
1055 cur.bv().buffer().params().use_amsmath == BufferParams::package_on;
1056 mutate(align ? hullAlign : hullEqnArray);
1057 cur.idx() = nrows() * ncols() - 1;
1058 cur.pos() = cur.lastpos();
1060 InsetMathGrid::doDispatch(cur, cmd);
1063 case LFUN_MATH_NUMBER:
1064 //lyxerr << "toggling all numbers" << endl;
1066 cur.recordUndoInset();
1067 bool old = numberedType();
1068 if (type_ == hullMultline)
1069 numbered(nrows() - 1, !old);
1071 for (row_type row = 0; row < nrows(); ++row)
1072 numbered(row, !old);
1074 cur.message(old ? _("No number") : _("Number"));
1078 case LFUN_MATH_NONUMBER:
1080 cur.recordUndoInset();
1081 row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1082 bool old = numbered(r);
1083 cur.message(old ? _("No number") : _("Number"));
1088 case LFUN_LABEL_INSERT: {
1089 cur.recordUndoInset();
1090 row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1091 docstring old_label = label(r);
1092 docstring const default_label = from_ascii(
1093 (lyxrc.label_init_length >= 0) ? "eq:" : "");
1094 if (old_label.empty())
1095 old_label = default_label;
1097 InsetCommandParams p(LABEL_CODE);
1098 p["name"] = cmd.argument().empty() ? old_label : cmd.argument();
1099 std::string const data = InsetCommandMailer::params2string("label", p);
1101 if (cmd.argument().empty())
1102 cur.bv().showInsetDialog("label", data, 0);
1104 FuncRequest fr(LFUN_INSET_INSERT, data);
1110 case LFUN_INSET_INSERT: {
1111 //lyxerr << "arg: " << to_utf8(cmd.argument()) << endl;
1112 std::string const name = cmd.getArg(0);
1113 if (name == "label") {
1114 InsetCommandParams p(LABEL_CODE);
1115 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), p);
1116 docstring str = p["name"];
1117 cur.recordUndoInset();
1118 row_type const r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1119 str = support::trim(str);
1122 docstring old = label(r);
1124 cur.bv().buffer().changeRefsIfUnique(old, str,
1130 InsetMathGrid::doDispatch(cur, cmd);
1134 case LFUN_MATH_EXTERN:
1135 cur.recordUndoInset();
1139 case LFUN_MATH_MUTATE: {
1140 cur.recordUndoInset();
1141 row_type row = cur.row();
1142 col_type col = cur.col();
1143 mutate(hullType(cmd.argument()));
1144 cur.idx() = row * ncols() + col;
1145 if (cur.idx() > cur.lastidx()) {
1146 cur.idx() = cur.lastidx();
1147 cur.pos() = cur.lastpos();
1149 if (cur.pos() > cur.lastpos())
1150 cur.pos() = cur.lastpos();
1151 //cur.dispatched(FINISHED);
1155 case LFUN_MATH_DISPLAY: {
1156 cur.recordUndoInset();
1157 mutate(type_ == hullSimple ? hullEquation : hullSimple);
1159 cur.pos() = cur.lastpos();
1160 //cur.dispatched(FINISHED);
1165 InsetMathGrid::doDispatch(cur, cmd);
1171 bool InsetMathHull::getStatus(Cursor & cur, FuncRequest const & cmd,
1172 FuncStatus & status) const
1174 switch (cmd.action) {
1175 case LFUN_FINISHED_BACKWARD:
1176 case LFUN_FINISHED_FORWARD:
1179 status.enabled(true);
1181 case LFUN_BREAK_LINE:
1182 case LFUN_MATH_NUMBER:
1183 case LFUN_MATH_NONUMBER:
1184 case LFUN_MATH_EXTERN:
1185 case LFUN_MATH_MUTATE:
1186 case LFUN_MATH_DISPLAY:
1188 status.enabled(true);
1190 case LFUN_LABEL_INSERT:
1191 status.enabled(type_ != hullSimple);
1193 case LFUN_INSET_INSERT:
1194 if (cmd.getArg(0) == "label") {
1195 status.enabled(type_ != hullSimple);
1198 return InsetMathGrid::getStatus(cur, cmd, status);
1199 case LFUN_TABULAR_FEATURE: {
1200 istringstream is(to_utf8(cmd.argument()));
1204 && (s == "append-row"
1205 || s == "delete-row"
1206 || s == "copy-row")) {
1207 status.message(bformat(
1208 from_utf8(N_("Can't change number of rows in '%1$s'")),
1210 status.enabled(false);
1214 && (s == "append-column"
1215 || s == "delete-column"
1216 || s == "copy-column")) {
1217 status.message(bformat(
1218 from_utf8(N_("Can't change number of columns in '%1$s'")),
1220 status.enabled(false);
1223 if ((type_ == hullSimple
1224 || type_ == hullEquation
1225 || type_ == hullNone) &&
1226 (s == "add-hline-above" || s == "add-hline-below")) {
1227 status.message(bformat(
1228 from_utf8(N_("Can't add horizontal grid lines in '%1$s'")),
1230 status.enabled(false);
1233 if (s == "add-vline-left" || s == "add-vline-right") {
1234 status.message(bformat(
1235 from_utf8(N_("Can't add vertical grid lines in '%1$s'")),
1237 status.enabled(false);
1240 if (s == "valign-top" || s == "valign-middle"
1241 || s == "valign-bottom" || s == "align-left"
1242 || s == "align-center" || s == "align-right") {
1243 status.enabled(false);
1246 return InsetMathGrid::getStatus(cur, cmd, status);
1249 return InsetMathGrid::getStatus(cur, cmd, status);
1252 // This cannot really happen, but inserted to shut-up gcc
1253 return InsetMathGrid::getStatus(cur, cmd, status);
1257 /////////////////////////////////////////////////////////////////////
1261 // simply scrap this function if you want
1262 void InsetMathHull::mutateToText()
1265 // translate to latex
1267 latex(NULL, os, false, false);
1268 string str = os.str();
1271 Text * lt = view_->cursor().innerText();
1272 string::const_iterator cit = str.begin();
1273 string::const_iterator end = str.end();
1274 for (; cit != end; ++cit)
1275 view_->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
1278 //dispatch(LFUN_ESCAPE);
1283 void InsetMathHull::handleFont(Cursor & cur, docstring const & arg,
1284 docstring const & font)
1286 // this whole function is a hack and won't work for incremental font
1289 if (cur.inset().asInsetMath()->name() == font)
1290 cur.handleFont(to_utf8(font));
1292 cur.handleNest(createInsetMath(font));
1298 void InsetMathHull::handleFont2(Cursor & cur, docstring const & arg)
1303 font.fromString(to_utf8(arg), b);
1304 if (font.fontInfo().color() != Color_inherit) {
1305 MathAtom at = MathAtom(new InsetMathColor(true, font.fontInfo().color()));
1306 cur.handleNest(at, 0);
1311 void InsetMathHull::edit(Cursor & cur, bool front)
1314 front ? idxFirst(cur) : idxLast(cur);
1315 // The inset formula dimension is not necessarily the same as the
1316 // one of the instant preview image, so we have to indicate to the
1317 // BufferView that a metrics update is needed.
1318 cur.updateFlags(Update::Force);
1322 docstring const InsetMathHull::editMessage() const
1324 return _("Math editor mode");
1328 void InsetMathHull::revealCodes(Cursor & cur) const
1330 if (!cur.inMathed())
1332 odocstringstream os;
1334 cur.message(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 InsetCode InsetMathHull::lyxCode() const
1370 /////////////////////////////////////////////////////////////////////
1374 bool InsetMathHull::searchForward(BufferView * bv, string const & str,
1377 // FIXME: completely broken
1378 static InsetMathHull * lastformula = 0;
1379 static CursorBase current = DocIterator(ibegin(nucleus()));
1381 static string laststr;
1383 if (lastformula != this || laststr != str) {
1384 //lyxerr << "reset lastformula to " << this << endl;
1387 current = ibegin(nucleus());
1389 mathed_parse_cell(ar, str);
1393 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
1395 for (DocIterator it = current; it != iend(nucleus()); increment(it)) {
1396 CursorSlice & top = it.back();
1397 MathData const & a = top.asInsetMath()->cell(top.idx_);
1398 if (a.matchpart(ar, top.pos_)) {
1399 bv->cursor().setSelection(it, ar.size());
1401 top.pos_ += ar.size();
1407 //lyxerr << "not found!" << endl;
1414 void InsetMathHull::write(Buffer const &, std::ostream & os) const
1416 odocstringstream oss;
1417 WriteStream wi(oss, false, false);
1420 os << to_utf8(oss.str());
1424 void InsetMathHull::read(Buffer const &, Lexer & lex)
1427 mathed_parse_normal(at, lex);
1428 operator=(*at->asHullInset());
1432 int InsetMathHull::plaintext(Buffer const &, odocstream & os,
1433 OutputParams const &) const
1435 if (0 && display()) {
1439 TextPainter tpain(dim.width(), dim.height());
1440 drawT(tpain, 0, dim.ascent());
1442 // reset metrics cache to "real" values
1444 return tpain.textheight();
1446 odocstringstream oss;
1447 WriteStream wi(oss, false, true);
1450 docstring const str = oss.str();
1457 int InsetMathHull::docbook(Buffer const & buf, odocstream & os,
1458 OutputParams const & runparams) const
1463 if (getType() == hullSimple)
1464 name = from_ascii("inlineequation");
1466 name = from_ascii("informalequation");
1468 docstring bname = name;
1469 if (!label(0).empty())
1470 bname += " id='" + sgml::cleanID(buf, runparams, label(0)) + "'";
1473 odocstringstream ls;
1474 if (runparams.flavor == OutputParams::XML) {
1475 ms << MTag(from_ascii("alt role='tex' "));
1476 // Workaround for db2latex: db2latex always includes equations with
1477 // \ensuremath{} or \begin{display}\end{display}
1478 // so we strip LyX' math environment
1479 WriteStream wi(ls, false, false);
1480 InsetMathGrid::write(wi);
1481 ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&"), "<", "<"));
1482 ms << ETag(from_ascii("alt"));
1483 ms << MTag(from_ascii("math"));
1484 ms << ETag(from_ascii("alt"));
1485 ms << MTag(from_ascii("math"));
1486 InsetMathGrid::mathmlize(ms);
1487 ms << ETag(from_ascii("math"));
1489 ms << MTag(from_ascii("alt role='tex'"));
1490 res = latex(buf, ls, runparams);
1491 ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&"), "<", "<"));
1492 ms << ETag(from_ascii("alt"));
1495 ms << from_ascii("<graphic fileref=\"eqn/");
1496 if (!label(0).empty())
1497 ms << sgml::cleanID(buf, runparams, label(0));
1499 ms << sgml::uniqueID(from_ascii("anon"));
1501 if (runparams.flavor == OutputParams::XML)
1502 ms << from_ascii("\"/>");
1504 ms << from_ascii("\">");
1507 return ms.line() + res;
1511 void InsetMathHull::textString(Buffer const & buf, odocstream & os) const
1513 plaintext(buf, os, OutputParams(0));