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"
46 #include "insets/RenderPreview.h"
47 #include "insets/InsetLabel.h"
49 #include "graphics/PreviewImage.h"
50 #include "graphics/PreviewLoader.h"
52 #include "frontends/Painter.h"
54 #include "support/lyxlib.h"
55 #include "support/lstrings.h"
57 #include <boost/bind.hpp>
64 using cap::grabAndEraseSelection;
65 using support::bformat;
71 using std::istringstream;
72 using std::ostringstream;
79 int getCols(HullType type)
96 // returns position of first relation operator in the array
97 // used for "intelligent splitting"
98 size_t firstRelOp(MathData const & ar)
100 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
101 if ((*it)->isRelOp())
102 return it - ar.begin();
107 char const * star(bool numbered)
109 return numbered ? "" : "*";
113 } // end anon namespace
116 HullType hullType(docstring const & s)
118 if (s == "none") return hullNone;
119 if (s == "simple") return hullSimple;
120 if (s == "equation") return hullEquation;
121 if (s == "eqnarray") return hullEqnArray;
122 if (s == "align") return hullAlign;
123 if (s == "alignat") return hullAlignAt;
124 if (s == "xalignat") return hullXAlignAt;
125 if (s == "xxalignat") return hullXXAlignAt;
126 if (s == "multline") return hullMultline;
127 if (s == "gather") return hullGather;
128 if (s == "flalign") return hullFlAlign;
129 lyxerr << "unknown hull type '" << to_utf8(s) << "'" << endl;
134 docstring hullName(HullType type)
137 case hullNone: return from_ascii("none");
138 case hullSimple: return from_ascii("simple");
139 case hullEquation: return from_ascii("equation");
140 case hullEqnArray: return from_ascii("eqnarray");
141 case hullAlign: return from_ascii("align");
142 case hullAlignAt: return from_ascii("alignat");
143 case hullXAlignAt: return from_ascii("xalignat");
144 case hullXXAlignAt: return from_ascii("xxalignat");
145 case hullMultline: return from_ascii("multline");
146 case hullGather: return from_ascii("gather");
147 case hullFlAlign: return from_ascii("flalign");
149 lyxerr << "unknown hull type '" << type << "'" << endl;
150 return from_ascii("none");
155 InsetMathHull::InsetMathHull()
156 : InsetMathGrid(1, 1), type_(hullNone), nonum_(1), label_(1),
157 preview_(new RenderPreview(this))
159 //lyxerr << "sizeof InsetMath: " << sizeof(InsetMath) << endl;
160 //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl;
161 //lyxerr << "sizeof InsetMathChar: " << sizeof(InsetMathChar) << endl;
162 //lyxerr << "sizeof Font: " << sizeof(Font) << endl;
168 InsetMathHull::InsetMathHull(HullType type)
169 : InsetMathGrid(getCols(type), 1), type_(type), nonum_(1), label_(1),
170 preview_(new RenderPreview(this))
177 InsetMathHull::InsetMathHull(InsetMathHull const & other)
178 : InsetMathGrid(other),
179 type_(other.type_), nonum_(other.nonum_), label_(other.label_),
180 preview_(new RenderPreview(*other.preview_, this))
184 InsetMathHull::~InsetMathHull()
188 Inset * InsetMathHull::clone() const
190 return new InsetMathHull(*this);
194 InsetMathHull & InsetMathHull::operator=(InsetMathHull const & other)
198 *static_cast<InsetMathGrid*>(this) = InsetMathGrid(other);
200 nonum_ = other.nonum_;
201 label_ = other.label_;
202 preview_.reset(new RenderPreview(*other.preview_, this));
208 Inset * InsetMathHull::editXY(Cursor & cur, int x, int y)
214 return InsetMathNest::editXY(cur, x, y);
218 InsetMath::mode_type InsetMathHull::currentMode() const
220 if (type_ == hullNone)
221 return UNDECIDED_MODE;
222 // definitely math mode ...
227 bool InsetMathHull::idxFirst(Cursor & cur) const
235 bool InsetMathHull::idxLast(Cursor & cur) const
237 cur.idx() = nargs() - 1;
238 cur.pos() = cur.lastpos();
243 char InsetMathHull::defaultColAlign(col_type col)
245 if (type_ == hullEqnArray)
247 if (type_ >= hullAlign)
248 return "rl"[col & 1];
253 int InsetMathHull::defaultColSpace(col_type col)
255 if (type_ == hullAlign || type_ == hullAlignAt)
257 if (type_ == hullXAlignAt)
258 return (col & 1) ? 20 : 0;
259 if (type_ == hullXXAlignAt || type_ == hullFlAlign)
260 return (col & 1) ? 40 : 0;
265 docstring InsetMathHull::standardFont() const
267 return from_ascii(type_ == hullNone ? "lyxnochange" : "mathnormal");
271 bool InsetMathHull::previewState(BufferView * bv) const
273 if (!editing(bv) && RenderPreview::status() == LyXRC::PREVIEW_ON) {
274 graphics::PreviewImage const * pimage =
275 preview_->getPreviewImage(bv->buffer());
276 return pimage && pimage->image();
282 void InsetMathHull::metrics(MetricsInfo & mi, Dimension & dim) const
284 if (previewState(mi.base.bv)) {
285 preview_->metrics(mi, dim);
286 // insert a one pixel gap in front of the formula
289 dim.des += displayMargin();
290 // Cache the inset dimension.
291 setDimCache(mi, dim);
295 FontSetChanger dummy1(mi.base, standardFont());
296 StyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
298 // let the cells adjust themselves
299 InsetMathGrid::metrics(mi, dim);
302 dim.asc += displayMargin();
303 dim.des += displayMargin();
306 if (numberedType()) {
307 FontSetChanger dummy(mi.base, from_ascii("mathbf"));
309 for (row_type row = 0; row < nrows(); ++row)
310 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
316 // make it at least as high as the current font
319 math_font_max_dim(mi.base.font, asc, des);
320 dim.asc = max(dim.asc, asc);
321 dim.des = max(dim.des, des);
322 // Cache the inset dimension.
323 // FIXME: This will overwrite InsetMathGrid dimension, is that OK?
324 setDimCache(mi, dim);
328 void InsetMathHull::draw(PainterInfo & pi, int x, int y) const
330 use_preview_ = previewState(pi.base.bv);
331 Dimension const dim = dimension(*pi.base.bv);
333 // background of mathed under focus is not painted because
334 // selection at the top level of nested inset is difficult to handle.
335 if (!editing(pi.base.bv))
336 pi.pain.fillRectangle(x + 1, y - dim.asc + 1, dim.wid - 2,
337 dim.asc + dim.des - 1, Color::mathbg);
340 // one pixel gap in front
341 preview_->draw(pi, x + 1, y);
342 setPosCache(pi, x, y);
346 FontSetChanger dummy1(pi.base, standardFont());
347 StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
348 InsetMathGrid::draw(pi, x + 1, y);
350 if (numberedType()) {
351 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
352 for (row_type row = 0; row < nrows(); ++row) {
353 int const yy = y + rowinfo_[row].offset_;
354 FontSetChanger dummy(pi.base, from_ascii("mathrm"));
355 docstring const nl = nicelabel(row);
359 setPosCache(pi, x, y);
363 void InsetMathHull::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
366 InsetMathGrid::metricsT(mi, dim);
369 WriteStream wi(os, false, true);
371 dim.wid = os.str().size();
378 void InsetMathHull::drawT(TextPainter & pain, int x, int y) const
381 InsetMathGrid::drawT(pain, x, y);
384 WriteStream wi(os, false, true);
386 pain.draw(x, y, os.str().c_str());
393 docstring const latex_string(InsetMathHull const & inset)
396 WriteStream wi(ls, false, false);
404 void InsetMathHull::addPreview(graphics::PreviewLoader & ploader) const
406 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
407 docstring const snippet = latex_string(*this);
408 preview_->addPreview(snippet, ploader);
413 bool InsetMathHull::notifyCursorLeaves(Cursor & cur)
415 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
416 Buffer const & buffer = cur.buffer();
417 docstring const snippet = latex_string(*this);
418 preview_->addPreview(snippet, buffer);
419 preview_->startLoading(buffer);
420 cur.updateFlags(Update::Force);
426 docstring InsetMathHull::label(row_type row) const
428 BOOST_ASSERT(row < nrows());
433 void InsetMathHull::label(row_type row, docstring const & label)
435 //lyxerr << "setting label '" << label << "' for row " << row << endl;
440 void InsetMathHull::numbered(row_type row, bool num)
448 bool InsetMathHull::numbered(row_type row) const
454 bool InsetMathHull::ams() const
457 type_ == hullAlign ||
458 type_ == hullFlAlign ||
459 type_ == hullMultline ||
460 type_ == hullGather ||
461 type_ == hullAlignAt ||
462 type_ == hullXAlignAt ||
463 type_ == hullXXAlignAt;
467 Inset::DisplayType InsetMathHull::display() const
469 return (type_ != hullSimple && type_ != hullNone) ? AlignCenter : Inline;
473 void InsetMathHull::getLabelList(Buffer const &, vector<docstring> & labels) const
475 for (row_type row = 0; row < nrows(); ++row)
476 if (!label_[row].empty() && nonum_[row] != 1)
477 labels.push_back(label_[row]);
481 bool InsetMathHull::numberedType() const
483 if (type_ == hullNone)
485 if (type_ == hullSimple)
487 if (type_ == hullXXAlignAt)
489 for (row_type row = 0; row < nrows(); ++row)
496 void InsetMathHull::validate(LaTeXFeatures & features) const
499 features.require("amsmath");
502 // Validation is necessary only if not using AMS math.
503 // To be safe, we will always run mathedvalidate.
504 //if (features.amsstyle)
507 features.require("boldsymbol");
508 //features.binom = true;
510 InsetMathGrid::validate(features);
514 void InsetMathHull::header_write(WriteStream & os) const
516 bool n = numberedType();
530 os << "\\begin{equation" << star(n) << "}\n";
540 os << "\\begin{" << hullName(type_) << star(n) << "}\n";
545 os << "\\begin{" << hullName(type_) << star(n) << '}'
546 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
550 os << "\\begin{" << hullName(type_) << '}'
551 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
555 os << "\\begin{unknown" << star(n) << '}';
561 void InsetMathHull::footer_write(WriteStream & os) const
563 bool n = numberedType();
576 os << "\\end{equation" << star(n) << "}\n";
588 os << "\\end{" << hullName(type_) << star(n) << "}\n";
592 os << "\\end{" << hullName(type_) << "}\n";
596 os << "\\end{unknown" << star(n) << '}';
602 bool InsetMathHull::rowChangeOK() const
605 type_ == hullEqnArray || type_ == hullAlign ||
606 type_ == hullFlAlign || type_ == hullAlignAt ||
607 type_ == hullXAlignAt || type_ == hullXXAlignAt ||
608 type_ == hullGather || type_ == hullMultline;
612 bool InsetMathHull::colChangeOK() const
615 type_ == hullAlign || type_ == hullFlAlign ||type_ == hullAlignAt ||
616 type_ == hullXAlignAt || type_ == hullXXAlignAt;
620 void InsetMathHull::addRow(row_type row)
624 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
625 label_.insert(label_.begin() + row + 1, docstring());
626 InsetMathGrid::addRow(row);
630 void InsetMathHull::swapRow(row_type row)
634 if (row + 1 == nrows())
636 swap(nonum_[row], nonum_[row + 1]);
637 swap(label_[row], label_[row + 1]);
638 InsetMathGrid::swapRow(row);
642 void InsetMathHull::delRow(row_type row)
644 if (nrows() <= 1 || !rowChangeOK())
646 InsetMathGrid::delRow(row);
647 // The last dummy row has no number info nor a label.
648 // Test nrows() + 1 because we have already erased the row.
649 if (row == nrows() + 1)
651 nonum_.erase(nonum_.begin() + row);
652 label_.erase(label_.begin() + row);
656 void InsetMathHull::addCol(col_type col)
660 InsetMathGrid::addCol(col);
664 void InsetMathHull::delCol(col_type col)
666 if (ncols() <= 1 || !colChangeOK())
668 InsetMathGrid::delCol(col);
672 docstring InsetMathHull::nicelabel(row_type row) const
676 if (label_[row].empty())
677 return from_ascii("(#)");
678 return '(' + label_[row] + ')';
682 void InsetMathHull::glueall()
685 for (idx_type i = 0; i < nargs(); ++i)
687 *this = InsetMathHull(hullSimple);
693 void InsetMathHull::splitTo2Cols()
695 BOOST_ASSERT(ncols() == 1);
696 InsetMathGrid::addCol(1);
697 for (row_type row = 0; row < nrows(); ++row) {
698 idx_type const i = 2 * row;
699 pos_type pos = firstRelOp(cell(i));
700 cell(i + 1) = MathData(cell(i).begin() + pos, cell(i).end());
701 cell(i).erase(pos, cell(i).size());
706 void InsetMathHull::splitTo3Cols()
708 BOOST_ASSERT(ncols() < 3);
711 InsetMathGrid::addCol(2);
712 for (row_type row = 0; row < nrows(); ++row) {
713 idx_type const i = 3 * row + 1;
714 if (cell(i).size()) {
715 cell(i + 1) = MathData(cell(i).begin() + 1, cell(i).end());
716 cell(i).erase(1, cell(i).size());
722 void InsetMathHull::changeCols(col_type cols)
726 else if (ncols() < cols) {
732 while (ncols() < cols)
733 InsetMathGrid::addCol(ncols());
739 for (row_type row = 0; row < nrows(); ++row) {
740 idx_type const i = row * ncols();
741 for (col_type col = cols; col < ncols(); ++col) {
742 cell(i + cols - 1).append(cell(i + col));
746 while (ncols() > cols) {
747 InsetMathGrid::delCol(ncols() - 1);
752 HullType InsetMathHull::getType() const
758 void InsetMathHull::setType(HullType type)
765 void InsetMathHull::mutate(HullType newtype)
767 //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
769 // we try to move along the chain
770 // none <-> simple <-> equation <-> eqnarray -> *align* -> multline, gather -+
772 // +-------------------------------------+
773 // we use eqnarray as intermediate type for mutations that are not
774 // directly supported because it handles labels and numbering for
777 if (newtype == type_) {
781 else if (newtype < hullNone) {
786 else if (type_ == hullNone) {
792 else if (type_ == hullSimple) {
793 if (newtype == hullNone) {
797 setType(hullEquation);
803 else if (type_ == hullEquation) {
804 if (newtype < type_) {
808 } else if (newtype == hullEqnArray) {
809 // split it "nicely" on the first relop
811 setType(hullEqnArray);
812 } else if (newtype == hullMultline || newtype == hullGather) {
822 else if (type_ == hullEqnArray) {
823 if (newtype < type_) {
824 // set correct (no)numbering
825 bool allnonum = true;
826 for (row_type row = 0; row < nrows(); ++row)
830 // set first non-empty label
832 for (row_type row = 0; row < nrows(); ++row) {
833 if (!label_[row].empty()) {
840 nonum_[0] = allnonum;
843 } else { // align & Co.
850 else if (type_ == hullAlign || type_ == hullAlignAt ||
851 type_ == hullXAlignAt || type_ == hullFlAlign) {
852 if (newtype < hullAlign) {
854 setType(hullEqnArray);
856 } else if (newtype == hullGather || newtype == hullMultline) {
859 } else if (newtype == hullXXAlignAt) {
860 for (row_type row = 0; row < nrows(); ++row)
861 numbered(row, false);
868 else if (type_ == hullXXAlignAt) {
869 for (row_type row = 0; row < nrows(); ++row)
870 numbered(row, false);
871 if (newtype < hullAlign) {
873 setType(hullEqnArray);
875 } else if (newtype == hullGather || newtype == hullMultline) {
883 else if (type_ == hullMultline || type_ == hullGather) {
884 if (newtype == hullGather || newtype == hullMultline)
886 else if (newtype == hullAlign || newtype == hullFlAlign ||
887 newtype == hullAlignAt || newtype == hullXAlignAt) {
890 } else if (newtype == hullXXAlignAt) {
892 for (row_type row = 0; row < nrows(); ++row)
893 numbered(row, false);
897 setType(hullEqnArray);
903 lyxerr << "mutation from '" << to_utf8(hullName(type_))
904 << "' to '" << to_utf8(hullName(newtype))
905 << "' not implemented" << endl;
910 docstring InsetMathHull::eolString(row_type row, bool emptyline, bool fragile) const
913 if (numberedType()) {
914 if (!label_[row].empty() && !nonum_[row])
915 res += "\\label{" + label_[row] + '}';
916 if (nonum_[row] && (type_ != hullMultline))
917 res += "\\nonumber ";
919 return res + InsetMathGrid::eolString(row, emptyline, fragile);
923 void InsetMathHull::write(WriteStream & os) const
926 InsetMathGrid::write(os);
931 void InsetMathHull::normalize(NormalStream & os) const
933 os << "[formula " << hullName(type_) << ' ';
934 InsetMathGrid::normalize(os);
939 void InsetMathHull::mathmlize(MathStream & os) const
941 InsetMathGrid::mathmlize(os);
945 void InsetMathHull::infoize(odocstream & os) const
947 os << "Type: " << hullName(type_);
951 void InsetMathHull::check() const
953 BOOST_ASSERT(nonum_.size() == nrows());
954 BOOST_ASSERT(label_.size() == nrows());
958 void InsetMathHull::doExtern(Cursor & cur, FuncRequest & func)
962 idocstringstream iss(func.argument());
963 iss >> dlang >> extra;
965 extra = from_ascii("noextra");
966 std::string const lang = to_ascii(dlang);
968 // FIXME: temporarily disabled
969 //if (cur.selection()) {
972 // lyxerr << "use selection: " << ar << endl;
973 // insert(pipeThroughExtern(lang, extra, ar));
978 eq.push_back(MathAtom(new InsetMathChar('=')));
980 // go to first item in line
981 cur.idx() -= cur.idx() % ncols();
984 if (getType() == hullSimple) {
985 size_type pos = cur.cell().find_last(eq);
987 if (cur.inMathed() && cur.selection()) {
988 asArray(grabAndEraseSelection(cur), ar);
989 } else if (pos == cur.cell().size()) {
991 lyxerr << "use whole cell: " << ar << endl;
993 ar = MathData(cur.cell().begin() + pos + 1, cur.cell().end());
994 lyxerr << "use partial cell form pos: " << pos << endl;
996 cur.cell().append(eq);
997 cur.cell().append(pipeThroughExtern(lang, extra, ar));
998 cur.pos() = cur.lastpos();
1002 if (getType() == hullEquation) {
1003 lyxerr << "use equation inset" << endl;
1004 mutate(hullEqnArray);
1005 MathData & ar = cur.cell();
1006 lyxerr << "use cell: " << ar << endl;
1010 cur.cell() = pipeThroughExtern(lang, extra, ar);
1011 // move to end of line
1012 cur.pos() = cur.lastpos();
1017 lyxerr << "use eqnarray" << endl;
1018 cur.idx() += 2 - cur.idx() % ncols();
1020 MathData ar = cur.cell();
1021 lyxerr << "use cell: " << ar << endl;
1022 // FIXME: temporarily disabled
1028 cur.cell() = pipeThroughExtern(lang, extra, ar);
1029 cur.pos() = cur.lastpos();
1034 void InsetMathHull::doDispatch(Cursor & cur, FuncRequest & cmd)
1036 //lyxerr << "action: " << cmd.action << endl;
1037 switch (cmd.action) {
1039 case LFUN_FINISHED_LEFT:
1040 case LFUN_FINISHED_RIGHT:
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 cur.recordUndoInset();
1056 cur.bv().buffer().params().use_amsmath == BufferParams::package_on;
1057 mutate(align ? hullAlign : hullEqnArray);
1058 cur.idx() = nrows() * ncols() - 1;
1059 cur.pos() = cur.lastpos();
1061 InsetMathGrid::doDispatch(cur, cmd);
1064 case LFUN_MATH_NUMBER:
1065 //lyxerr << "toggling all numbers" << endl;
1067 cur.recordUndoInset();
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 cur.recordUndoInset();
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 cur.recordUndoInset();
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 cur.recordUndoInset();
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,
1131 InsetMathGrid::doDispatch(cur, cmd);
1135 case LFUN_MATH_EXTERN:
1136 cur.recordUndoInset();
1140 case LFUN_MATH_MUTATE: {
1141 cur.recordUndoInset();
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 cur.recordUndoInset();
1158 mutate(type_ == hullSimple ? hullEquation : hullSimple);
1160 cur.pos() = cur.lastpos();
1161 //cur.dispatched(FINISHED);
1166 InsetMathGrid::doDispatch(cur, cmd);
1172 bool InsetMathHull::getStatus(Cursor & cur, FuncRequest const & cmd,
1173 FuncStatus & status) const
1175 switch (cmd.action) {
1176 case LFUN_FINISHED_LEFT:
1177 case LFUN_FINISHED_RIGHT:
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 Text * lt = view_->cursor().innerText();
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(Cursor & 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(Cursor & cur, docstring const & arg)
1304 font.fromString(to_utf8(arg), b);
1305 if (font.color() != Color::inherit) {
1306 MathAtom at = MathAtom(new InsetMathColor(true, font.color()));
1307 cur.handleNest(at, 0);
1312 void InsetMathHull::edit(Cursor & cur, bool left)
1315 left ? idxFirst(cur) : idxLast(cur);
1316 // The inset formula dimension is not necessarily the same as the
1317 // one of the instant preview image, so we have to indicate to the
1318 // BufferView that a metrics update is needed.
1319 cur.updateFlags(Update::Force);
1323 docstring const InsetMathHull::editMessage() const
1325 return _("Math editor mode");
1329 void InsetMathHull::revealCodes(Cursor & cur) const
1331 if (!cur.inMathed())
1333 odocstringstream os;
1335 cur.message(os.str());
1337 // write something to the minibuffer
1338 // translate to latex
1342 string str = os.str();
1344 string::size_type pos = 0;
1346 for (string::iterator it = str.begin(); it != str.end(); ++it) {
1349 else if (*it == '\0') {
1351 pos = it - str.begin();
1357 res = res.substr(pos - 30);
1358 if (res.size() > 60)
1359 res = res.substr(0, 60);
1365 InsetCode InsetMathHull::lyxCode() const
1371 /////////////////////////////////////////////////////////////////////
1375 bool InsetMathHull::searchForward(BufferView * bv, string const & str,
1378 // FIXME: completely broken
1379 static InsetMathHull * lastformula = 0;
1380 static CursorBase current = DocIterator(ibegin(nucleus()));
1382 static string laststr;
1384 if (lastformula != this || laststr != str) {
1385 //lyxerr << "reset lastformula to " << this << endl;
1388 current = ibegin(nucleus());
1390 mathed_parse_cell(ar, str);
1394 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
1396 for (DocIterator it = current; it != iend(nucleus()); increment(it)) {
1397 CursorSlice & top = it.back();
1398 MathData const & a = top.asInsetMath()->cell(top.idx_);
1399 if (a.matchpart(ar, top.pos_)) {
1400 bv->cursor().setSelection(it, ar.size());
1402 top.pos_ += ar.size();
1408 //lyxerr << "not found!" << endl;
1415 void InsetMathHull::write(Buffer const &, std::ostream & os) const
1417 odocstringstream oss;
1418 WriteStream wi(oss, false, false);
1421 os << to_utf8(oss.str());
1425 void InsetMathHull::read(Buffer const &, Lexer & lex)
1428 mathed_parse_normal(at, lex);
1429 operator=(*at->asHullInset());
1433 int InsetMathHull::plaintext(Buffer const &, odocstream & os,
1434 OutputParams const &) const
1436 if (0 && display()) {
1440 TextPainter tpain(dim.width(), dim.height());
1441 drawT(tpain, 0, dim.ascent());
1443 // reset metrics cache to "real" values
1445 return tpain.textheight();
1447 odocstringstream oss;
1448 WriteStream wi(oss, false, true);
1451 docstring const str = oss.str();
1458 int InsetMathHull::docbook(Buffer const & buf, odocstream & os,
1459 OutputParams const & runparams) const
1464 if (getType() == hullSimple)
1465 name = from_ascii("inlineequation");
1467 name = from_ascii("informalequation");
1469 docstring bname = name;
1470 if (!label(0).empty())
1471 bname += " id='" + sgml::cleanID(buf, runparams, label(0)) + "'";
1474 odocstringstream ls;
1475 if (runparams.flavor == OutputParams::XML) {
1476 ms << MTag(from_ascii("alt role='tex' "));
1477 // Workaround for db2latex: db2latex always includes equations with
1478 // \ensuremath{} or \begin{display}\end{display}
1479 // so we strip LyX' math environment
1480 WriteStream wi(ls, false, false);
1481 InsetMathGrid::write(wi);
1482 ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&"), "<", "<"));
1483 ms << ETag(from_ascii("alt"));
1484 ms << MTag(from_ascii("math"));
1485 ms << ETag(from_ascii("alt"));
1486 ms << MTag(from_ascii("math"));
1487 InsetMathGrid::mathmlize(ms);
1488 ms << ETag(from_ascii("math"));
1490 ms << MTag(from_ascii("alt role='tex'"));
1491 res = latex(buf, ls, runparams);
1492 ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&"), "<", "<"));
1493 ms << ETag(from_ascii("alt"));
1496 ms << from_ascii("<graphic fileref=\"eqn/");
1497 if (!label(0).empty())
1498 ms << sgml::cleanID(buf, runparams, label(0));
1500 ms << sgml::uniqueID(from_ascii("anon"));
1502 if (runparams.flavor == OutputParams::XML)
1503 ms << from_ascii("\"/>");
1505 ms << from_ascii("\">");
1508 return ms.line() + res;
1512 void InsetMathHull::textString(Buffer const & buf, odocstream & os) const
1514 plaintext(buf, os, OutputParams(0));