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"
34 #include "LaTeXFeatures.h"
37 #include "DispatchResult.h"
38 #include "FuncRequest.h"
41 #include "OutputParams.h"
44 #include "TextPainter.h"
47 #include "insets/RenderPreview.h"
48 #include "insets/InsetLabel.h"
50 #include "graphics/PreviewImage.h"
51 #include "graphics/PreviewLoader.h"
53 #include "frontends/Painter.h"
55 #include "support/lyxlib.h"
56 #include "support/lstrings.h"
58 #include <boost/bind.hpp>
65 using cap::grabAndEraseSelection;
66 using support::bformat;
72 using std::istringstream;
73 using std::ostringstream;
80 int getCols(HullType type)
97 // returns position of first relation operator in the array
98 // used for "intelligent splitting"
99 size_t firstRelOp(MathData const & ar)
101 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
102 if ((*it)->isRelOp())
103 return it - ar.begin();
108 char const * star(bool numbered)
110 return numbered ? "" : "*";
114 } // end anon namespace
117 HullType hullType(docstring const & s)
119 if (s == "none") return hullNone;
120 if (s == "simple") return hullSimple;
121 if (s == "equation") return hullEquation;
122 if (s == "eqnarray") return hullEqnArray;
123 if (s == "align") return hullAlign;
124 if (s == "alignat") return hullAlignAt;
125 if (s == "xalignat") return hullXAlignAt;
126 if (s == "xxalignat") return hullXXAlignAt;
127 if (s == "multline") return hullMultline;
128 if (s == "gather") return hullGather;
129 if (s == "flalign") return hullFlAlign;
130 lyxerr << "unknown hull type '" << to_utf8(s) << "'" << endl;
135 docstring hullName(HullType type)
138 case hullNone: return from_ascii("none");
139 case hullSimple: return from_ascii("simple");
140 case hullEquation: return from_ascii("equation");
141 case hullEqnArray: return from_ascii("eqnarray");
142 case hullAlign: return from_ascii("align");
143 case hullAlignAt: return from_ascii("alignat");
144 case hullXAlignAt: return from_ascii("xalignat");
145 case hullXXAlignAt: return from_ascii("xxalignat");
146 case hullMultline: return from_ascii("multline");
147 case hullGather: return from_ascii("gather");
148 case hullFlAlign: return from_ascii("flalign");
150 lyxerr << "unknown hull type '" << type << "'" << endl;
151 return from_ascii("none");
156 InsetMathHull::InsetMathHull()
157 : InsetMathGrid(1, 1), type_(hullNone), nonum_(1), label_(1),
158 preview_(new RenderPreview(this))
160 //lyxerr << "sizeof InsetMath: " << sizeof(InsetMath) << endl;
161 //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl;
162 //lyxerr << "sizeof InsetMathChar: " << sizeof(InsetMathChar) << endl;
163 //lyxerr << "sizeof Font: " << sizeof(Font) << endl;
169 InsetMathHull::InsetMathHull(HullType type)
170 : InsetMathGrid(getCols(type), 1), type_(type), nonum_(1), label_(1),
171 preview_(new RenderPreview(this))
178 InsetMathHull::InsetMathHull(InsetMathHull const & other)
179 : InsetMathGrid(other),
180 type_(other.type_), nonum_(other.nonum_), label_(other.label_),
181 preview_(new RenderPreview(*other.preview_, this))
185 InsetMathHull::~InsetMathHull()
189 Inset * InsetMathHull::clone() const
191 return new InsetMathHull(*this);
195 InsetMathHull & InsetMathHull::operator=(InsetMathHull const & other)
199 *static_cast<InsetMathGrid*>(this) = InsetMathGrid(other);
201 nonum_ = other.nonum_;
202 label_ = other.label_;
203 preview_.reset(new RenderPreview(*other.preview_, this));
209 Inset * InsetMathHull::editXY(Cursor & cur, int x, int y)
215 return InsetMathNest::editXY(cur, x, y);
219 InsetMath::mode_type InsetMathHull::currentMode() const
221 if (type_ == hullNone)
222 return UNDECIDED_MODE;
223 // definitely math mode ...
228 bool InsetMathHull::idxFirst(Cursor & cur) const
236 bool InsetMathHull::idxLast(Cursor & cur) const
238 cur.idx() = nargs() - 1;
239 cur.pos() = cur.lastpos();
244 char InsetMathHull::defaultColAlign(col_type col)
246 if (type_ == hullEqnArray)
248 if (type_ >= hullAlign)
249 return "rl"[col & 1];
254 int InsetMathHull::defaultColSpace(col_type col)
256 if (type_ == hullAlign || type_ == hullAlignAt)
258 if (type_ == hullXAlignAt)
259 return (col & 1) ? 20 : 0;
260 if (type_ == hullXXAlignAt || type_ == hullFlAlign)
261 return (col & 1) ? 40 : 0;
266 docstring InsetMathHull::standardFont() const
268 return from_ascii(type_ == hullNone ? "lyxnochange" : "mathnormal");
272 bool InsetMathHull::previewState(BufferView * bv) const
274 if (!editing(bv) && RenderPreview::status() == LyXRC::PREVIEW_ON) {
275 graphics::PreviewImage const * pimage =
276 preview_->getPreviewImage(bv->buffer());
277 return pimage && pimage->image();
283 void InsetMathHull::metrics(MetricsInfo & mi, Dimension & dim) const
285 if (previewState(mi.base.bv)) {
286 preview_->metrics(mi, dim);
287 // insert a one pixel gap in front of the formula
290 dim.des += displayMargin();
291 // Cache the inset dimension.
292 setDimCache(mi, dim);
296 FontSetChanger dummy1(mi.base, standardFont());
297 StyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
299 // let the cells adjust themselves
300 InsetMathGrid::metrics(mi, dim);
303 dim.asc += displayMargin();
304 dim.des += displayMargin();
307 if (numberedType()) {
308 FontSetChanger dummy(mi.base, from_ascii("mathbf"));
310 for (row_type row = 0; row < nrows(); ++row)
311 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
317 // make it at least as high as the current font
320 math_font_max_dim(mi.base.font, asc, des);
321 dim.asc = max(dim.asc, asc);
322 dim.des = max(dim.des, des);
323 // Cache the inset dimension.
324 // FIXME: This will overwrite InsetMathGrid dimension, is that OK?
325 setDimCache(mi, dim);
329 void InsetMathHull::draw(PainterInfo & pi, int x, int y) const
331 use_preview_ = previewState(pi.base.bv);
332 Dimension const dim = dimension(*pi.base.bv);
334 // background of mathed under focus is not painted because
335 // selection at the top level of nested inset is difficult to handle.
336 if (!editing(pi.base.bv))
337 pi.pain.fillRectangle(x + 1, y - dim.asc + 1, dim.wid - 2,
338 dim.asc + dim.des - 1, Color::mathbg);
341 // one pixel gap in front
342 preview_->draw(pi, x + 1, y);
343 setPosCache(pi, x, y);
347 FontSetChanger dummy1(pi.base, standardFont());
348 StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
349 InsetMathGrid::draw(pi, x + 1, y);
351 if (numberedType()) {
352 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
353 for (row_type row = 0; row < nrows(); ++row) {
354 int const yy = y + rowinfo_[row].offset_;
355 FontSetChanger dummy(pi.base, from_ascii("mathrm"));
356 docstring const nl = nicelabel(row);
360 setPosCache(pi, x, y);
364 void InsetMathHull::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
367 InsetMathGrid::metricsT(mi, dim);
370 WriteStream wi(os, false, true);
372 dim.wid = os.str().size();
379 void InsetMathHull::drawT(TextPainter & pain, int x, int y) const
382 InsetMathGrid::drawT(pain, x, y);
385 WriteStream wi(os, false, true);
387 pain.draw(x, y, os.str().c_str());
394 docstring const latex_string(InsetMathHull const & inset)
397 WriteStream wi(ls, false, false);
405 void InsetMathHull::addPreview(graphics::PreviewLoader & ploader) const
407 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
408 docstring const snippet = latex_string(*this);
409 preview_->addPreview(snippet, ploader);
414 bool InsetMathHull::notifyCursorLeaves(Cursor & cur)
416 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
417 Buffer const & buffer = cur.buffer();
418 docstring const snippet = latex_string(*this);
419 preview_->addPreview(snippet, buffer);
420 preview_->startLoading(buffer);
421 cur.updateFlags(Update::Force);
427 docstring InsetMathHull::label(row_type row) const
429 BOOST_ASSERT(row < nrows());
434 void InsetMathHull::label(row_type row, docstring const & label)
436 //lyxerr << "setting label '" << label << "' for row " << row << endl;
441 void InsetMathHull::numbered(row_type row, bool num)
449 bool InsetMathHull::numbered(row_type row) const
455 bool InsetMathHull::ams() const
458 type_ == hullAlign ||
459 type_ == hullFlAlign ||
460 type_ == hullMultline ||
461 type_ == hullGather ||
462 type_ == hullAlignAt ||
463 type_ == hullXAlignAt ||
464 type_ == hullXXAlignAt;
468 Inset::DisplayType InsetMathHull::display() const
470 return (type_ != hullSimple && type_ != hullNone) ? AlignCenter : Inline;
474 void InsetMathHull::getLabelList(Buffer const &, vector<docstring> & labels) const
476 for (row_type row = 0; row < nrows(); ++row)
477 if (!label_[row].empty() && nonum_[row] != 1)
478 labels.push_back(label_[row]);
482 bool InsetMathHull::numberedType() const
484 if (type_ == hullNone)
486 if (type_ == hullSimple)
488 if (type_ == hullXXAlignAt)
490 for (row_type row = 0; row < nrows(); ++row)
497 void InsetMathHull::validate(LaTeXFeatures & features) const
500 features.require("amsmath");
503 // Validation is necessary only if not using AMS math.
504 // To be safe, we will always run mathedvalidate.
505 //if (features.amsstyle)
508 features.require("boldsymbol");
509 //features.binom = true;
511 InsetMathGrid::validate(features);
515 void InsetMathHull::header_write(WriteStream & os) const
517 bool n = numberedType();
531 os << "\\begin{equation" << star(n) << "}\n";
541 os << "\\begin{" << hullName(type_) << star(n) << "}\n";
546 os << "\\begin{" << hullName(type_) << star(n) << '}'
547 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
551 os << "\\begin{" << hullName(type_) << '}'
552 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
556 os << "\\begin{unknown" << star(n) << '}';
562 void InsetMathHull::footer_write(WriteStream & os) const
564 bool n = numberedType();
577 os << "\\end{equation" << star(n) << "}\n";
589 os << "\\end{" << hullName(type_) << star(n) << "}\n";
593 os << "\\end{" << hullName(type_) << "}\n";
597 os << "\\end{unknown" << star(n) << '}';
603 bool InsetMathHull::rowChangeOK() const
606 type_ == hullEqnArray || type_ == hullAlign ||
607 type_ == hullFlAlign || type_ == hullAlignAt ||
608 type_ == hullXAlignAt || type_ == hullXXAlignAt ||
609 type_ == hullGather || type_ == hullMultline;
613 bool InsetMathHull::colChangeOK() const
616 type_ == hullAlign || type_ == hullFlAlign ||type_ == hullAlignAt ||
617 type_ == hullXAlignAt || type_ == hullXXAlignAt;
621 void InsetMathHull::addRow(row_type row)
625 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
626 label_.insert(label_.begin() + row + 1, docstring());
627 InsetMathGrid::addRow(row);
631 void InsetMathHull::swapRow(row_type row)
635 if (row + 1 == nrows())
637 swap(nonum_[row], nonum_[row + 1]);
638 swap(label_[row], label_[row + 1]);
639 InsetMathGrid::swapRow(row);
643 void InsetMathHull::delRow(row_type row)
645 if (nrows() <= 1 || !rowChangeOK())
647 InsetMathGrid::delRow(row);
648 // The last dummy row has no number info nor a label.
649 // Test nrows() + 1 because we have already erased the row.
650 if (row == nrows() + 1)
652 nonum_.erase(nonum_.begin() + row);
653 label_.erase(label_.begin() + row);
657 void InsetMathHull::addCol(col_type col)
661 InsetMathGrid::addCol(col);
665 void InsetMathHull::delCol(col_type col)
667 if (ncols() <= 1 || !colChangeOK())
669 InsetMathGrid::delCol(col);
673 docstring InsetMathHull::nicelabel(row_type row) const
677 if (label_[row].empty())
678 return from_ascii("(#)");
679 return '(' + label_[row] + ')';
683 void InsetMathHull::glueall()
686 for (idx_type i = 0; i < nargs(); ++i)
688 *this = InsetMathHull(hullSimple);
694 void InsetMathHull::splitTo2Cols()
696 BOOST_ASSERT(ncols() == 1);
697 InsetMathGrid::addCol(1);
698 for (row_type row = 0; row < nrows(); ++row) {
699 idx_type const i = 2 * row;
700 pos_type pos = firstRelOp(cell(i));
701 cell(i + 1) = MathData(cell(i).begin() + pos, cell(i).end());
702 cell(i).erase(pos, cell(i).size());
707 void InsetMathHull::splitTo3Cols()
709 BOOST_ASSERT(ncols() < 3);
712 InsetMathGrid::addCol(2);
713 for (row_type row = 0; row < nrows(); ++row) {
714 idx_type const i = 3 * row + 1;
715 if (cell(i).size()) {
716 cell(i + 1) = MathData(cell(i).begin() + 1, cell(i).end());
717 cell(i).erase(1, cell(i).size());
723 void InsetMathHull::changeCols(col_type cols)
727 else if (ncols() < cols) {
733 while (ncols() < cols)
734 InsetMathGrid::addCol(ncols());
740 for (row_type row = 0; row < nrows(); ++row) {
741 idx_type const i = row * ncols();
742 for (col_type col = cols; col < ncols(); ++col) {
743 cell(i + cols - 1).append(cell(i + col));
747 while (ncols() > cols) {
748 InsetMathGrid::delCol(ncols() - 1);
753 HullType InsetMathHull::getType() const
759 void InsetMathHull::setType(HullType type)
766 void InsetMathHull::mutate(HullType newtype)
768 //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
770 // we try to move along the chain
771 // none <-> simple <-> equation <-> eqnarray -> *align* -> multline, gather -+
773 // +-------------------------------------+
774 // we use eqnarray as intermediate type for mutations that are not
775 // directly supported because it handles labels and numbering for
778 if (newtype == type_) {
782 else if (newtype < hullNone) {
787 else if (type_ == hullNone) {
793 else if (type_ == hullSimple) {
794 if (newtype == hullNone) {
798 setType(hullEquation);
804 else if (type_ == hullEquation) {
805 if (newtype < type_) {
809 } else if (newtype == hullEqnArray) {
810 // split it "nicely" on the first relop
812 setType(hullEqnArray);
813 } else if (newtype == hullMultline || newtype == hullGather) {
823 else if (type_ == hullEqnArray) {
824 if (newtype < type_) {
825 // set correct (no)numbering
826 bool allnonum = true;
827 for (row_type row = 0; row < nrows(); ++row)
831 // set first non-empty label
833 for (row_type row = 0; row < nrows(); ++row) {
834 if (!label_[row].empty()) {
841 nonum_[0] = allnonum;
844 } else { // align & Co.
851 else if (type_ == hullAlign || type_ == hullAlignAt ||
852 type_ == hullXAlignAt || type_ == hullFlAlign) {
853 if (newtype < hullAlign) {
855 setType(hullEqnArray);
857 } else if (newtype == hullGather || newtype == hullMultline) {
860 } else if (newtype == hullXXAlignAt) {
861 for (row_type row = 0; row < nrows(); ++row)
862 numbered(row, false);
869 else if (type_ == hullXXAlignAt) {
870 for (row_type row = 0; row < nrows(); ++row)
871 numbered(row, false);
872 if (newtype < hullAlign) {
874 setType(hullEqnArray);
876 } else if (newtype == hullGather || newtype == hullMultline) {
884 else if (type_ == hullMultline || type_ == hullGather) {
885 if (newtype == hullGather || newtype == hullMultline)
887 else if (newtype == hullAlign || newtype == hullFlAlign ||
888 newtype == hullAlignAt || newtype == hullXAlignAt) {
891 } else if (newtype == hullXXAlignAt) {
893 for (row_type row = 0; row < nrows(); ++row)
894 numbered(row, false);
898 setType(hullEqnArray);
904 lyxerr << "mutation from '" << to_utf8(hullName(type_))
905 << "' to '" << to_utf8(hullName(newtype))
906 << "' not implemented" << endl;
911 docstring InsetMathHull::eolString(row_type row, bool emptyline, bool fragile) const
914 if (numberedType()) {
915 if (!label_[row].empty() && !nonum_[row])
916 res += "\\label{" + label_[row] + '}';
917 if (nonum_[row] && (type_ != hullMultline))
918 res += "\\nonumber ";
920 return res + InsetMathGrid::eolString(row, emptyline, fragile);
924 void InsetMathHull::write(WriteStream & os) const
927 InsetMathGrid::write(os);
932 void InsetMathHull::normalize(NormalStream & os) const
934 os << "[formula " << hullName(type_) << ' ';
935 InsetMathGrid::normalize(os);
940 void InsetMathHull::mathmlize(MathStream & os) const
942 InsetMathGrid::mathmlize(os);
946 void InsetMathHull::infoize(odocstream & os) const
948 os << "Type: " << hullName(type_);
952 void InsetMathHull::check() const
954 BOOST_ASSERT(nonum_.size() == nrows());
955 BOOST_ASSERT(label_.size() == nrows());
959 void InsetMathHull::doExtern(Cursor & cur, FuncRequest & func)
963 idocstringstream iss(func.argument());
964 iss >> dlang >> extra;
966 extra = from_ascii("noextra");
967 std::string const lang = to_ascii(dlang);
969 // FIXME: temporarily disabled
970 //if (cur.selection()) {
973 // lyxerr << "use selection: " << ar << endl;
974 // insert(pipeThroughExtern(lang, extra, ar));
979 eq.push_back(MathAtom(new InsetMathChar('=')));
981 // go to first item in line
982 cur.idx() -= cur.idx() % ncols();
985 if (getType() == hullSimple) {
986 size_type pos = cur.cell().find_last(eq);
988 if (cur.inMathed() && cur.selection()) {
989 asArray(grabAndEraseSelection(cur), ar);
990 } else if (pos == cur.cell().size()) {
992 lyxerr << "use whole cell: " << ar << endl;
994 ar = MathData(cur.cell().begin() + pos + 1, cur.cell().end());
995 lyxerr << "use partial cell form pos: " << pos << endl;
997 cur.cell().append(eq);
998 cur.cell().append(pipeThroughExtern(lang, extra, ar));
999 cur.pos() = cur.lastpos();
1003 if (getType() == hullEquation) {
1004 lyxerr << "use equation inset" << endl;
1005 mutate(hullEqnArray);
1006 MathData & ar = cur.cell();
1007 lyxerr << "use cell: " << ar << endl;
1011 cur.cell() = pipeThroughExtern(lang, extra, ar);
1012 // move to end of line
1013 cur.pos() = cur.lastpos();
1018 lyxerr << "use eqnarray" << endl;
1019 cur.idx() += 2 - cur.idx() % ncols();
1021 MathData ar = cur.cell();
1022 lyxerr << "use cell: " << ar << endl;
1023 // FIXME: temporarily disabled
1029 cur.cell() = pipeThroughExtern(lang, extra, ar);
1030 cur.pos() = cur.lastpos();
1035 void InsetMathHull::doDispatch(Cursor & cur, FuncRequest & cmd)
1037 //lyxerr << "action: " << cmd.action << endl;
1038 switch (cmd.action) {
1040 case LFUN_FINISHED_LEFT:
1041 case LFUN_FINISHED_RIGHT:
1042 //lyxerr << "action: " << cmd.action << endl;
1043 InsetMathGrid::doDispatch(cur, cmd);
1044 notifyCursorLeaves(cur);
1048 case LFUN_BREAK_PARAGRAPH:
1049 // just swallow this
1052 case LFUN_BREAK_LINE:
1053 // some magic for the common case
1054 if (type_ == hullSimple || type_ == hullEquation) {
1055 recordUndoInset(cur);
1057 cur.bv().buffer().params().use_amsmath == BufferParams::package_on;
1058 mutate(align ? hullAlign : hullEqnArray);
1059 cur.idx() = nrows() * ncols() - 1;
1060 cur.pos() = cur.lastpos();
1062 InsetMathGrid::doDispatch(cur, cmd);
1065 case LFUN_MATH_NUMBER:
1066 //lyxerr << "toggling all numbers" << endl;
1068 recordUndoInset(cur);
1069 bool old = numberedType();
1070 if (type_ == hullMultline)
1071 numbered(nrows() - 1, !old);
1073 for (row_type row = 0; row < nrows(); ++row)
1074 numbered(row, !old);
1076 cur.message(old ? _("No number") : _("Number"));
1080 case LFUN_MATH_NONUMBER:
1082 recordUndoInset(cur);
1083 row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1084 bool old = numbered(r);
1085 cur.message(old ? _("No number") : _("Number"));
1090 case LFUN_LABEL_INSERT: {
1091 recordUndoInset(cur);
1092 row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1093 docstring old_label = label(r);
1094 docstring const default_label = from_ascii(
1095 (lyxrc.label_init_length >= 0) ? "eq:" : "");
1096 if (old_label.empty())
1097 old_label = default_label;
1099 InsetCommandParams p("label");
1100 p["name"] = cmd.argument().empty() ? old_label : cmd.argument();
1101 std::string const data = InsetCommandMailer::params2string("label", p);
1103 if (cmd.argument().empty())
1104 cur.bv().showInsetDialog("label", data, 0);
1106 FuncRequest fr(LFUN_INSET_INSERT, data);
1112 case LFUN_INSET_INSERT: {
1113 //lyxerr << "arg: " << to_utf8(cmd.argument()) << endl;
1114 std::string const name = cmd.getArg(0);
1115 if (name == "label") {
1116 InsetCommandParams p("label");
1117 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), p);
1118 docstring str = p["name"];
1119 recordUndoInset(cur);
1120 row_type const r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1121 str = support::trim(str);
1124 docstring old = label(r);
1126 cur.bv().buffer().changeRefsIfUnique(old, str,
1132 InsetMathGrid::doDispatch(cur, cmd);
1136 case LFUN_MATH_EXTERN:
1137 recordUndoInset(cur);
1141 case LFUN_MATH_MUTATE: {
1142 recordUndoInset(cur);
1143 row_type row = cur.row();
1144 col_type col = cur.col();
1145 mutate(hullType(cmd.argument()));
1146 cur.idx() = row * ncols() + col;
1147 if (cur.idx() > cur.lastidx()) {
1148 cur.idx() = cur.lastidx();
1149 cur.pos() = cur.lastpos();
1151 if (cur.pos() > cur.lastpos())
1152 cur.pos() = cur.lastpos();
1153 //cur.dispatched(FINISHED);
1157 case LFUN_MATH_DISPLAY: {
1158 recordUndoInset(cur);
1159 mutate(type_ == hullSimple ? hullEquation : hullSimple);
1161 cur.pos() = cur.lastpos();
1162 //cur.dispatched(FINISHED);
1167 InsetMathGrid::doDispatch(cur, cmd);
1173 bool InsetMathHull::getStatus(Cursor & cur, FuncRequest const & cmd,
1174 FuncStatus & status) const
1176 switch (cmd.action) {
1177 case LFUN_FINISHED_LEFT:
1178 case LFUN_FINISHED_RIGHT:
1181 status.enabled(true);
1183 case LFUN_BREAK_LINE:
1184 case LFUN_MATH_NUMBER:
1185 case LFUN_MATH_NONUMBER:
1186 case LFUN_MATH_EXTERN:
1187 case LFUN_MATH_MUTATE:
1188 case LFUN_MATH_DISPLAY:
1190 status.enabled(true);
1192 case LFUN_LABEL_INSERT:
1193 status.enabled(type_ != hullSimple);
1195 case LFUN_INSET_INSERT:
1196 if (cmd.getArg(0) == "label") {
1197 status.enabled(type_ != hullSimple);
1200 return InsetMathGrid::getStatus(cur, cmd, status);
1201 case LFUN_TABULAR_FEATURE: {
1202 istringstream is(to_utf8(cmd.argument()));
1206 && (s == "append-row"
1207 || s == "delete-row"
1208 || s == "copy-row")) {
1209 status.message(bformat(
1210 from_utf8(N_("Can't change number of rows in '%1$s'")),
1212 status.enabled(false);
1216 && (s == "append-column"
1217 || s == "delete-column"
1218 || s == "copy-column")) {
1219 status.message(bformat(
1220 from_utf8(N_("Can't change number of columns in '%1$s'")),
1222 status.enabled(false);
1225 if ((type_ == hullSimple
1226 || type_ == hullEquation
1227 || type_ == hullNone) &&
1228 (s == "add-hline-above" || s == "add-hline-below")) {
1229 status.message(bformat(
1230 from_utf8(N_("Can't add horizontal grid lines in '%1$s'")),
1232 status.enabled(false);
1235 if (s == "add-vline-left" || s == "add-vline-right") {
1236 status.message(bformat(
1237 from_utf8(N_("Can't add vertical grid lines in '%1$s'")),
1239 status.enabled(false);
1242 if (s == "valign-top" || s == "valign-middle"
1243 || s == "valign-bottom" || s == "align-left"
1244 || s == "align-center" || s == "align-right") {
1245 status.enabled(false);
1248 return InsetMathGrid::getStatus(cur, cmd, status);
1251 return InsetMathGrid::getStatus(cur, cmd, status);
1254 // This cannot really happen, but inserted to shut-up gcc
1255 return InsetMathGrid::getStatus(cur, cmd, status);
1259 /////////////////////////////////////////////////////////////////////
1263 // simply scrap this function if you want
1264 void InsetMathHull::mutateToText()
1267 // translate to latex
1269 latex(NULL, os, false, false);
1270 string str = os.str();
1273 Text * lt = view_->cursor().innerText();
1274 string::const_iterator cit = str.begin();
1275 string::const_iterator end = str.end();
1276 for (; cit != end; ++cit)
1277 view_->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
1280 //dispatch(LFUN_ESCAPE);
1285 void InsetMathHull::handleFont(Cursor & cur, docstring const & arg,
1286 docstring const & font)
1288 // this whole function is a hack and won't work for incremental font
1291 if (cur.inset().asInsetMath()->name() == font)
1292 cur.handleFont(to_utf8(font));
1294 cur.handleNest(createInsetMath(font));
1300 void InsetMathHull::handleFont2(Cursor & cur, docstring const & arg)
1305 font.fromString(to_utf8(arg), b);
1306 if (font.color() != Color::inherit) {
1307 MathAtom at = MathAtom(new InsetMathColor(true, font.color()));
1308 cur.handleNest(at, 0);
1313 void InsetMathHull::edit(Cursor & cur, bool left)
1316 left ? idxFirst(cur) : idxLast(cur);
1317 // The inset formula dimension is not necessarily the same as the
1318 // one of the instant preview image, so we have to indicate to the
1319 // BufferView that a metrics update is needed.
1320 cur.updateFlags(Update::Force);
1324 docstring const InsetMathHull::editMessage() const
1326 return _("Math editor mode");
1330 void InsetMathHull::revealCodes(Cursor & cur) const
1332 if (!cur.inMathed())
1334 odocstringstream os;
1336 cur.message(os.str());
1338 // write something to the minibuffer
1339 // translate to latex
1343 string str = os.str();
1345 string::size_type pos = 0;
1347 for (string::iterator it = str.begin(); it != str.end(); ++it) {
1350 else if (*it == '\0') {
1352 pos = it - str.begin();
1358 res = res.substr(pos - 30);
1359 if (res.size() > 60)
1360 res = res.substr(0, 60);
1366 InsetCode InsetMathHull::lyxCode() const
1372 /////////////////////////////////////////////////////////////////////
1376 bool InsetMathHull::searchForward(BufferView * bv, string const & str,
1379 // FIXME: completely broken
1380 static InsetMathHull * lastformula = 0;
1381 static CursorBase current = DocIterator(ibegin(nucleus()));
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 MathData 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 &, Lexer & 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 odocstringstream oss;
1449 WriteStream wi(oss, false, true);
1452 docstring const str = oss.str();
1459 int InsetMathHull::docbook(Buffer const & buf, odocstream & os,
1460 OutputParams const & runparams) const
1465 if (getType() == hullSimple)
1466 name = from_ascii("inlineequation");
1468 name = from_ascii("informalequation");
1470 docstring bname = name;
1471 if (!label(0).empty())
1472 bname += " id='" + sgml::cleanID(buf, runparams, label(0)) + "'";
1475 odocstringstream ls;
1476 if (runparams.flavor == OutputParams::XML) {
1477 ms << MTag(from_ascii("alt role='tex' "));
1478 // Workaround for db2latex: db2latex always includes equations with
1479 // \ensuremath{} or \begin{display}\end{display}
1480 // so we strip LyX' math environment
1481 WriteStream wi(ls, false, false);
1482 InsetMathGrid::write(wi);
1483 ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&"), "<", "<"));
1484 ms << ETag(from_ascii("alt"));
1485 ms << MTag(from_ascii("math"));
1486 ms << ETag(from_ascii("alt"));
1487 ms << MTag(from_ascii("math"));
1488 InsetMathGrid::mathmlize(ms);
1489 ms << ETag(from_ascii("math"));
1491 ms << MTag(from_ascii("alt role='tex'"));
1492 res = latex(buf, ls, runparams);
1493 ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&"), "<", "<"));
1494 ms << ETag(from_ascii("alt"));
1497 ms << from_ascii("<graphic fileref=\"eqn/");
1498 if (!label(0).empty())
1499 ms << sgml::cleanID(buf, runparams, label(0));
1501 ms << sgml::uniqueID(from_ascii("anon"));
1503 if (runparams.flavor == OutputParams::XML)
1504 ms << from_ascii("\"/>");
1506 ms << from_ascii("\">");
1509 return ms.line() + res;
1513 void InsetMathHull::textString(Buffer const & buf, odocstream & os) const
1515 plaintext(buf, os, OutputParams(0));