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);
976 #warning temporarily disabled
977 //if (cur.selection()) {
980 // lyxerr << "use selection: " << ar << endl;
981 // insert(pipeThroughExtern(lang, extra, ar));
987 eq.push_back(MathAtom(new InsetMathChar('=')));
989 // go to first item in line
990 cur.idx() -= cur.idx() % ncols();
993 if (getType() == hullSimple) {
994 size_type pos = cur.cell().find_last(eq);
996 if (cur.inMathed() && cur.selection()) {
997 asArray(grabAndEraseSelection(cur), ar);
998 } else if (pos == cur.cell().size()) {
1000 lyxerr << "use whole cell: " << ar << endl;
1002 ar = MathData(cur.cell().begin() + pos + 1, cur.cell().end());
1003 lyxerr << "use partial cell form pos: " << pos << endl;
1005 cur.cell().append(eq);
1006 cur.cell().append(pipeThroughExtern(lang, extra, ar));
1007 cur.pos() = cur.lastpos();
1011 if (getType() == hullEquation) {
1012 lyxerr << "use equation inset" << endl;
1013 mutate(hullEqnArray);
1014 MathData & ar = cur.cell();
1015 lyxerr << "use cell: " << ar << endl;
1019 cur.cell() = pipeThroughExtern(lang, extra, ar);
1020 // move to end of line
1021 cur.pos() = cur.lastpos();
1026 lyxerr << "use eqnarray" << endl;
1027 cur.idx() += 2 - cur.idx() % ncols();
1029 MathData ar = cur.cell();
1030 lyxerr << "use cell: " << ar << endl;
1031 #ifdef WITH_WARNINGS
1032 #warning temporarily disabled
1039 cur.cell() = pipeThroughExtern(lang, extra, ar);
1040 cur.pos() = cur.lastpos();
1045 void InsetMathHull::doDispatch(Cursor & cur, FuncRequest & cmd)
1047 //lyxerr << "action: " << cmd.action << endl;
1048 switch (cmd.action) {
1050 case LFUN_FINISHED_LEFT:
1051 case LFUN_FINISHED_RIGHT:
1052 //lyxerr << "action: " << cmd.action << endl;
1053 InsetMathGrid::doDispatch(cur, cmd);
1054 notifyCursorLeaves(cur);
1058 case LFUN_BREAK_PARAGRAPH:
1059 // just swallow this
1062 case LFUN_BREAK_LINE:
1063 // some magic for the common case
1064 if (type_ == hullSimple || type_ == hullEquation) {
1065 recordUndoInset(cur);
1067 cur.bv().buffer()->params().use_amsmath == BufferParams::package_on;
1068 mutate(align ? hullAlign : hullEqnArray);
1070 cur.pos() = cur.lastpos();
1072 InsetMathGrid::doDispatch(cur, cmd);
1075 case LFUN_MATH_NUMBER:
1076 //lyxerr << "toggling all numbers" << endl;
1078 recordUndoInset(cur);
1079 bool old = numberedType();
1080 if (type_ == hullMultline)
1081 numbered(nrows() - 1, !old);
1083 for (row_type row = 0; row < nrows(); ++row)
1084 numbered(row, !old);
1086 cur.message(old ? _("No number") : _("Number"));
1090 case LFUN_MATH_NONUMBER:
1092 recordUndoInset(cur);
1093 row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1094 bool old = numbered(r);
1095 cur.message(old ? _("No number") : _("Number"));
1100 case LFUN_LABEL_INSERT: {
1101 recordUndoInset(cur);
1102 row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1103 docstring old_label = label(r);
1104 docstring const default_label = from_ascii(
1105 (lyxrc.label_init_length >= 0) ? "eq:" : "");
1106 if (old_label.empty())
1107 old_label = default_label;
1109 InsetCommandParams p("label");
1110 p["name"] = cmd.argument().empty() ? old_label : cmd.argument();
1111 std::string const data = InsetCommandMailer::params2string("label", p);
1113 if (cmd.argument().empty())
1114 cur.bv().showInsetDialog("label", data, 0);
1116 FuncRequest fr(LFUN_INSET_INSERT, data);
1122 case LFUN_INSET_INSERT: {
1123 //lyxerr << "arg: " << to_utf8(cmd.argument()) << endl;
1124 std::string const name = cmd.getArg(0);
1125 if (name == "label") {
1126 InsetCommandParams p("label");
1127 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), p);
1128 docstring str = p["name"];
1129 recordUndoInset(cur);
1130 row_type const r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1131 str = support::trim(str);
1134 docstring old = label(r);
1136 cur.bv().buffer()->changeRefsIfUnique(old, str,
1142 InsetMathGrid::doDispatch(cur, cmd);
1146 case LFUN_MATH_EXTERN:
1147 recordUndoInset(cur);
1151 case LFUN_MATH_MUTATE: {
1152 recordUndoInset(cur);
1153 row_type row = cur.row();
1154 col_type col = cur.col();
1155 mutate(hullType(cmd.argument()));
1156 cur.idx() = row * ncols() + col;
1157 if (cur.idx() > cur.lastidx()) {
1158 cur.idx() = cur.lastidx();
1159 cur.pos() = cur.lastpos();
1161 if (cur.pos() > cur.lastpos())
1162 cur.pos() = cur.lastpos();
1163 //cur.dispatched(FINISHED);
1167 case LFUN_MATH_DISPLAY: {
1168 recordUndoInset(cur);
1169 mutate(type_ == hullSimple ? hullEquation : hullSimple);
1171 cur.pos() = cur.lastpos();
1172 //cur.dispatched(FINISHED);
1177 InsetMathGrid::doDispatch(cur, cmd);
1183 bool InsetMathHull::getStatus(Cursor & cur, FuncRequest const & cmd,
1184 FuncStatus & status) const
1186 switch (cmd.action) {
1187 case LFUN_FINISHED_LEFT:
1188 case LFUN_FINISHED_RIGHT:
1191 status.enabled(true);
1193 case LFUN_BREAK_LINE:
1194 case LFUN_MATH_NUMBER:
1195 case LFUN_MATH_NONUMBER:
1196 case LFUN_MATH_EXTERN:
1197 case LFUN_MATH_MUTATE:
1198 case LFUN_MATH_DISPLAY:
1200 status.enabled(true);
1202 case LFUN_LABEL_INSERT:
1203 status.enabled(type_ != hullSimple);
1205 case LFUN_INSET_INSERT:
1206 if (cmd.getArg(0) == "label") {
1207 status.enabled(type_ != hullSimple);
1210 return InsetMathGrid::getStatus(cur, cmd, status);
1211 case LFUN_TABULAR_FEATURE: {
1212 istringstream is(to_utf8(cmd.argument()));
1216 && (s == "append-row"
1217 || s == "delete-row"
1218 || s == "copy-row")) {
1219 status.message(bformat(
1220 from_utf8(N_("Can't change number of rows in '%1$s'")),
1222 status.enabled(false);
1226 && (s == "append-column"
1227 || s == "delete-column"
1228 || s == "copy-column")) {
1229 status.message(bformat(
1230 from_utf8(N_("Can't change number of columns in '%1$s'")),
1232 status.enabled(false);
1235 if ((type_ == hullSimple
1236 || type_ == hullEquation
1237 || type_ == hullNone) &&
1238 (s == "add-hline-above" || s == "add-hline-below")) {
1239 status.message(bformat(
1240 from_utf8(N_("Can't add horizontal grid lines in '%1$s'")),
1242 status.enabled(false);
1245 if (s == "add-vline-left" || s == "add-vline-right") {
1246 status.message(bformat(
1247 from_utf8(N_("Can't add vertical grid lines in '%1$s'")),
1249 status.enabled(false);
1252 if (s == "valign-top" || s == "valign-middle"
1253 || s == "valign-bottom" || s == "align-left"
1254 || s == "align-center" || s == "align-right") {
1255 status.enabled(false);
1258 return InsetMathGrid::getStatus(cur, cmd, status);
1261 return InsetMathGrid::getStatus(cur, cmd, status);
1264 // This cannot really happen, but inserted to shut-up gcc
1265 return InsetMathGrid::getStatus(cur, cmd, status);
1269 /////////////////////////////////////////////////////////////////////
1273 // simply scrap this function if you want
1274 void InsetMathHull::mutateToText()
1277 // translate to latex
1279 latex(NULL, os, false, false);
1280 string str = os.str();
1283 Text * lt = view_->cursor().innerText();
1284 string::const_iterator cit = str.begin();
1285 string::const_iterator end = str.end();
1286 for (; cit != end; ++cit)
1287 view_->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
1290 //dispatch(LFUN_ESCAPE);
1295 void InsetMathHull::handleFont(Cursor & cur, docstring const & arg,
1296 docstring const & font)
1298 // this whole function is a hack and won't work for incremental font
1301 if (cur.inset().asInsetMath()->name() == font)
1302 cur.handleFont(to_utf8(font));
1304 cur.handleNest(createInsetMath(font));
1310 void InsetMathHull::handleFont2(Cursor & cur, docstring const & arg)
1315 bv_funcs::string2font(to_utf8(arg), font, b);
1316 if (font.color() != Color::inherit) {
1317 MathAtom at = MathAtom(new InsetMathColor(true, font.color()));
1318 cur.handleNest(at, 0);
1323 void InsetMathHull::edit(Cursor & cur, bool left)
1326 left ? idxFirst(cur) : idxLast(cur);
1327 // The inset formula dimension is not necessarily the same as the
1328 // one of the instant preview image, so we have to indicate to the
1329 // BufferView that a metrics update is needed.
1330 cur.updateFlags(Update::Force);
1334 docstring const InsetMathHull::editMessage() const
1336 return _("Math editor mode");
1340 void InsetMathHull::revealCodes(Cursor & cur) const
1342 if (!cur.inMathed())
1344 odocstringstream os;
1346 cur.message(os.str());
1348 // write something to the minibuffer
1349 // translate to latex
1353 string str = os.str();
1355 string::size_type pos = 0;
1357 for (string::iterator it = str.begin(); it != str.end(); ++it) {
1360 else if (*it == '\0') {
1362 pos = it - str.begin();
1368 res = res.substr(pos - 30);
1369 if (res.size() > 60)
1370 res = res.substr(0, 60);
1376 Inset::Code InsetMathHull::lyxCode() const
1382 /////////////////////////////////////////////////////////////////////
1386 bool InsetMathHull::searchForward(BufferView * bv, string const & str,
1389 #ifdef WITH_WARNINGS
1390 #warning completely broken
1392 static InsetMathHull * lastformula = 0;
1393 static CursorBase current = DocIterator(ibegin(nucleus()));
1395 static string laststr;
1397 if (lastformula != this || laststr != str) {
1398 //lyxerr << "reset lastformula to " << this << endl;
1401 current = ibegin(nucleus());
1403 mathed_parse_cell(ar, str);
1407 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
1409 for (DocIterator it = current; it != iend(nucleus()); increment(it)) {
1410 CursorSlice & top = it.back();
1411 MathData const & a = top.asInsetMath()->cell(top.idx_);
1412 if (a.matchpart(ar, top.pos_)) {
1413 bv->cursor().setSelection(it, ar.size());
1415 top.pos_ += ar.size();
1421 //lyxerr << "not found!" << endl;
1428 void InsetMathHull::write(Buffer const &, std::ostream & os) const
1430 odocstringstream oss;
1431 WriteStream wi(oss, false, false);
1434 os << to_utf8(oss.str());
1438 void InsetMathHull::read(Buffer const &, Lexer & lex)
1441 mathed_parse_normal(at, lex);
1442 operator=(*at->asHullInset());
1446 int InsetMathHull::plaintext(Buffer const &, odocstream & os,
1447 OutputParams const &) const
1449 if (0 && display()) {
1453 TextPainter tpain(dim.width(), dim.height());
1454 drawT(tpain, 0, dim.ascent());
1456 // reset metrics cache to "real" values
1458 return tpain.textheight();
1460 odocstringstream oss;
1461 WriteStream wi(oss, false, true);
1464 docstring const str = oss.str();
1471 int InsetMathHull::docbook(Buffer const & buf, odocstream & os,
1472 OutputParams const & runparams) const
1477 if (getType() == hullSimple)
1478 name = from_ascii("inlineequation");
1480 name = from_ascii("informalequation");
1482 docstring bname = name;
1483 if (!label(0).empty())
1484 bname += " id='" + sgml::cleanID(buf, runparams, label(0)) + "'";
1487 odocstringstream ls;
1488 if (runparams.flavor == OutputParams::XML) {
1489 ms << MTag(from_ascii("alt role='tex' "));
1490 // Workaround for db2latex: db2latex always includes equations with
1491 // \ensuremath{} or \begin{display}\end{display}
1492 // so we strip LyX' math environment
1493 WriteStream wi(ls, false, false);
1494 InsetMathGrid::write(wi);
1495 ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&"), "<", "<"));
1496 ms << ETag(from_ascii("alt"));
1497 ms << MTag(from_ascii("math"));
1498 ms << ETag(from_ascii("alt"));
1499 ms << MTag(from_ascii("math"));
1500 InsetMathGrid::mathmlize(ms);
1501 ms << ETag(from_ascii("math"));
1503 ms << MTag(from_ascii("alt role='tex'"));
1504 res = latex(buf, ls, runparams);
1505 ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&"), "<", "<"));
1506 ms << ETag(from_ascii("alt"));
1509 ms << from_ascii("<graphic fileref=\"eqn/");
1510 if (!label(0).empty())
1511 ms << sgml::cleanID(buf, runparams, label(0));
1513 ms << sgml::uniqueID(from_ascii("anon"));
1515 if (runparams.flavor == OutputParams::XML)
1516 ms << from_ascii("\"/>");
1518 ms << from_ascii("\">");
1521 return ms.line() + res;
1525 void InsetMathHull::textString(Buffer const & buf, odocstream & os) const
1527 plaintext(buf, os, OutputParams(0));