2 * \file InsetMathHull.C
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 "MathMLStream.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"
45 #include "outputparams.h"
47 #include "TextPainter.h"
50 #include "insets/render_preview.h"
51 #include "insets/insetlabel.h"
53 #include "frontends/Dialogs.h"
54 #include "frontends/LyXView.h"
56 #include "graphics/PreviewImage.h"
57 #include "graphics/PreviewLoader.h"
59 #include "support/lyxlib.h"
60 #include "support/lstrings.h"
62 #include <boost/bind.hpp>
67 using lyx::cap::grabAndEraseSelection;
68 using lyx::support::bformat;
69 using lyx::support::subst;
76 using std::istringstream;
78 using std::ostringstream;
86 int getCols(HullType type)
103 // returns position of first relation operator in the array
104 // used for "intelligent splitting"
105 size_t firstRelOp(MathArray const & ar)
107 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
108 if ((*it)->isRelOp())
109 return it - ar.begin();
114 char const * star(bool numbered)
116 return numbered ? "" : "*";
120 } // end anon namespace
123 HullType hullType(std::string const & s)
125 if (s == "none") return hullNone;
126 if (s == "simple") return hullSimple;
127 if (s == "equation") return hullEquation;
128 if (s == "eqnarray") return hullEqnArray;
129 if (s == "align") return hullAlign;
130 if (s == "alignat") return hullAlignAt;
131 if (s == "xalignat") return hullXAlignAt;
132 if (s == "xxalignat") return hullXXAlignAt;
133 if (s == "multline") return hullMultline;
134 if (s == "gather") return hullGather;
135 if (s == "flalign") return hullFlAlign;
136 lyxerr << "unknown hull type '" << s << "'" << endl;
141 std::string hullName(HullType type)
144 case hullNone: return "none";
145 case hullSimple: return "simple";
146 case hullEquation: return "equation";
147 case hullEqnArray: return "eqnarray";
148 case hullAlign: return "align";
149 case hullAlignAt: return "alignat";
150 case hullXAlignAt: return "xalignat";
151 case hullXXAlignAt: return "xxalignat";
152 case hullMultline: return "multline";
153 case hullGather: return "gather";
154 case hullFlAlign: return "flalign";
156 lyxerr << "unknown hull type '" << type << "'" << endl;
162 InsetMathHull::InsetMathHull()
163 : InsetMathGrid(1, 1), type_(hullNone), nonum_(1), label_(1),
164 preview_(new RenderPreview(this))
166 //lyxerr << "sizeof InsetMath: " << sizeof(InsetMath) << endl;
167 //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl;
168 //lyxerr << "sizeof InsetMathChar: " << sizeof(InsetMathChar) << endl;
169 //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << endl;
175 InsetMathHull::InsetMathHull(HullType type)
176 : InsetMathGrid(getCols(type), 1), type_(type), nonum_(1), label_(1),
177 preview_(new RenderPreview(this))
184 InsetMathHull::InsetMathHull(InsetMathHull const & other)
185 : InsetMathGrid(other),
186 type_(other.type_), nonum_(other.nonum_), label_(other.label_),
187 preview_(new RenderPreview(this))
191 InsetMathHull::~InsetMathHull()
195 auto_ptr<InsetBase> InsetMathHull::doClone() const
197 return auto_ptr<InsetBase>(new InsetMathHull(*this));
201 InsetMathHull & InsetMathHull::operator=(InsetMathHull const & other)
205 *static_cast<InsetMathGrid*>(this) = InsetMathGrid(other);
207 nonum_ = other.nonum_;
208 label_ = other.label_;
209 preview_.reset(new RenderPreview(*other.preview_, this));
215 InsetBase * InsetMathHull::editXY(LCursor & cur, int x, int y)
221 return InsetMathNest::editXY(cur, x, y);
225 InsetMath::mode_type InsetMathHull::currentMode() const
227 if (type_ == hullNone)
228 return UNDECIDED_MODE;
229 // definitely math mode ...
234 bool InsetMathHull::idxFirst(LCursor & cur) const
242 bool InsetMathHull::idxLast(LCursor & cur) const
244 cur.idx() = nargs() - 1;
245 cur.pos() = cur.lastpos();
250 char InsetMathHull::defaultColAlign(col_type col)
252 if (type_ == hullEqnArray)
254 if (type_ >= hullAlign)
255 return "rl"[col & 1];
260 int InsetMathHull::defaultColSpace(col_type col)
262 if (type_ == hullAlign || type_ == hullAlignAt)
264 if (type_ == hullXAlignAt)
265 return (col & 1) ? 20 : 0;
266 if (type_ == hullXXAlignAt || type_ == hullFlAlign)
267 return (col & 1) ? 40 : 0;
272 char const * InsetMathHull::standardFont() const
274 return type_ == hullNone ? "lyxnochange" : "mathnormal";
278 bool InsetMathHull::previewState(BufferView * bv) const
280 if (!editing(bv) && RenderPreview::status() == LyXRC::PREVIEW_ON) {
281 lyx::graphics::PreviewImage const * pimage =
282 preview_->getPreviewImage(*bv->buffer());
283 return pimage && pimage->image();
289 void InsetMathHull::metrics(MetricsInfo & mi, Dimension & dim) const
291 if (previewState(mi.base.bv)) {
292 preview_->metrics(mi, dim);
293 // insert a one pixel gap in front of the formula
296 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, "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);
333 void InsetMathHull::draw(PainterInfo & pi, int x, int y) const
335 use_preview_ = previewState(pi.base.bv);
338 // one pixel gap in front
339 preview_->draw(pi, x + 1, y);
340 setPosCache(pi, x, y);
344 FontSetChanger dummy1(pi.base, standardFont());
345 StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
346 InsetMathGrid::draw(pi, x + 1, y);
348 if (numberedType()) {
349 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
350 for (row_type row = 0; row < nrows(); ++row) {
351 int const yy = y + rowinfo_[row].offset_;
352 FontSetChanger dummy(pi.base, "mathrm");
353 string const nl = nicelabel(row);
354 docstring const dnl(nl.begin(), nl.end());
355 pi.draw(xx, yy, dnl);
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 string const latex_string(InsetMathHull const & inset)
395 WriteStream wi(ls, false, false);
403 void InsetMathHull::addPreview(lyx::graphics::PreviewLoader & ploader) const
405 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
406 string const snippet = latex_string(*this);
407 preview_->addPreview(snippet, ploader);
412 bool InsetMathHull::notifyCursorLeaves(LCursor & cur)
414 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
415 Buffer const & buffer = cur.buffer();
416 string const snippet = latex_string(*this);
417 preview_->addPreview(snippet, buffer);
418 preview_->startLoading(buffer);
424 string InsetMathHull::label(row_type row) const
426 BOOST_ASSERT(row < nrows());
431 void InsetMathHull::label(row_type row, string const & label)
433 //lyxerr << "setting label '" << label << "' for row " << row << endl;
438 void InsetMathHull::numbered(row_type row, bool num)
444 bool InsetMathHull::numbered(row_type row) const
450 bool InsetMathHull::ams() const
453 type_ == hullAlign ||
454 type_ == hullFlAlign ||
455 type_ == hullMultline ||
456 type_ == hullGather ||
457 type_ == hullAlignAt ||
458 type_ == hullXAlignAt ||
459 type_ == hullXXAlignAt;
463 bool InsetMathHull::display() const
465 return type_ != hullSimple && type_ != hullNone;
469 void InsetMathHull::getLabelList(Buffer const &, vector<string> & labels) const
471 for (row_type row = 0; row < nrows(); ++row)
472 if (!label_[row].empty() && nonum_[row] != 1)
473 labels.push_back(label_[row]);
477 bool InsetMathHull::numberedType() const
479 if (type_ == hullNone)
481 if (type_ == hullSimple)
483 if (type_ == hullXXAlignAt)
485 for (row_type row = 0; row < nrows(); ++row)
492 void InsetMathHull::validate(LaTeXFeatures & features) const
495 features.require("amsmath");
498 // Validation is necessary only if not using AMS math.
499 // To be safe, we will always run mathedvalidate.
500 //if (features.amsstyle)
503 features.require("boldsymbol");
504 //features.binom = true;
506 InsetMathGrid::validate(features);
510 void InsetMathHull::header_write(WriteStream & os) const
512 bool n = numberedType();
514 if (type_ == hullNone)
517 else if (type_ == hullSimple) {
523 else if (type_ == hullEquation) {
525 os << "\\begin{equation" << star(n) << "}\n";
530 else if (type_ == hullEqnArray || type_ == hullAlign || type_ ==
532 || type_ == hullGather || type_ == hullMultline)
533 os << "\\begin{" << type_ << star(n) << "}\n";
535 else if (type_ == hullAlignAt || type_ == hullXAlignAt)
536 os << "\\begin{" << type_ << star(n) << '}'
537 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
539 else if (type_ == hullXXAlignAt)
540 os << "\\begin{" << type_ << '}'
541 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
544 os << "\\begin{unknown" << star(n) << '}';
548 void InsetMathHull::footer_write(WriteStream & os) const
550 bool n = numberedType();
552 if (type_ == hullNone)
555 else if (type_ == hullSimple)
558 else if (type_ == hullEquation)
560 os << "\\end{equation" << star(n) << "}\n";
564 else if (type_ == hullEqnArray || type_ == hullAlign || type_ ==
566 || type_ == hullAlignAt || type_ == hullXAlignAt
567 || type_ == hullGather || type_ == hullMultline)
568 os << "\\end{" << type_ << star(n) << "}\n";
570 else if (type_ == hullXXAlignAt)
571 os << "\\end{" << type_ << "}\n";
574 os << "\\end{unknown" << star(n) << '}';
578 bool InsetMathHull::rowChangeOK() const
581 type_ == hullEqnArray || type_ == hullAlign ||
582 type_ == hullFlAlign || type_ == hullAlignAt ||
583 type_ == hullXAlignAt || type_ == hullXXAlignAt ||
584 type_ == hullGather || type_ == hullMultline;
588 bool InsetMathHull::colChangeOK() const
591 type_ == hullAlign || type_ == hullFlAlign ||type_ == hullAlignAt ||
592 type_ == hullXAlignAt || type_ == hullXXAlignAt;
596 void InsetMathHull::addRow(row_type row)
600 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
601 label_.insert(label_.begin() + row + 1, string());
602 InsetMathGrid::addRow(row);
606 void InsetMathHull::swapRow(row_type row)
610 if (row + 1 == nrows())
612 swap(nonum_[row], nonum_[row + 1]);
613 swap(label_[row], label_[row + 1]);
614 InsetMathGrid::swapRow(row);
618 void InsetMathHull::delRow(row_type row)
620 if (nrows() <= 1 || !rowChangeOK())
622 InsetMathGrid::delRow(row);
623 // The last dummy row has no number info nor a label.
624 // Test nrows() + 1 because we have already erased the row.
625 if (row == nrows() + 1)
627 nonum_.erase(nonum_.begin() + row);
628 label_.erase(label_.begin() + row);
632 void InsetMathHull::addCol(col_type col)
636 InsetMathGrid::addCol(col);
640 void InsetMathHull::delCol(col_type col)
642 if (ncols() <= 1 || !colChangeOK())
644 InsetMathGrid::delCol(col);
648 string InsetMathHull::nicelabel(row_type row) const
652 if (label_[row].empty())
653 return string("(#)");
654 return '(' + label_[row] + ')';
658 void InsetMathHull::glueall()
661 for (idx_type i = 0; i < nargs(); ++i)
663 *this = InsetMathHull(hullSimple);
669 void InsetMathHull::splitTo2Cols()
671 BOOST_ASSERT(ncols() == 1);
672 InsetMathGrid::addCol(1);
673 for (row_type row = 0; row < nrows(); ++row) {
674 idx_type const i = 2 * row;
675 pos_type pos = firstRelOp(cell(i));
676 cell(i + 1) = MathArray(cell(i).begin() + pos, cell(i).end());
677 cell(i).erase(pos, cell(i).size());
682 void InsetMathHull::splitTo3Cols()
684 BOOST_ASSERT(ncols() < 3);
687 InsetMathGrid::addCol(1);
688 for (row_type row = 0; row < nrows(); ++row) {
689 idx_type const i = 3 * row + 1;
690 if (cell(i).size()) {
691 cell(i + 1) = MathArray(cell(i).begin() + 1, cell(i).end());
692 cell(i).erase(1, cell(i).size());
698 void InsetMathHull::changeCols(col_type cols)
702 else if (ncols() < cols) {
708 while (ncols() < cols)
709 InsetMathGrid::addCol(ncols() - 1);
715 for (row_type row = 0; row < nrows(); ++row) {
716 idx_type const i = row * ncols();
717 for (col_type col = cols; col < ncols(); ++col) {
718 cell(i + cols - 1).append(cell(i + col));
722 while (ncols() > cols) {
723 InsetMathGrid::delCol(ncols() - 1);
728 HullType InsetMathHull::getType() const
734 void InsetMathHull::setType(HullType type)
741 void InsetMathHull::mutate(HullType newtype)
743 //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
745 // we try to move along the chain
746 // none <-> simple <-> equation <-> eqnarray -> *align* -> multline, gather -+
748 // +-------------------------------------+
749 // we use eqnarray as intermediate type for mutations that are not
750 // directly supported because it handles labels and numbering for
753 if (newtype == type_) {
757 else if (newtype < hullNone) {
762 else if (type_ == hullNone) {
768 else if (type_ == hullSimple) {
769 if (newtype == hullNone) {
773 setType(hullEquation);
779 else if (type_ == hullEquation) {
780 if (newtype < type_) {
784 } else if (newtype == hullEqnArray) {
785 // split it "nicely" on the first relop
787 setType(hullEqnArray);
788 } else if (newtype == hullMultline || newtype == hullGather) {
798 else if (type_ == hullEqnArray) {
799 if (newtype < type_) {
800 // set correct (no)numbering
801 bool allnonum = true;
802 for (row_type row = 0; row < nrows(); ++row)
806 // set first non-empty label
808 for (row_type row = 0; row < nrows(); ++row) {
809 if (!label_[row].empty()) {
816 nonum_[0] = allnonum;
819 } else { // align & Co.
826 else if (type_ == hullAlign || type_ == hullAlignAt ||
827 type_ == hullXAlignAt || type_ == hullFlAlign) {
828 if (newtype < hullAlign) {
830 setType(hullEqnArray);
832 } else if (newtype == hullGather || newtype == hullMultline) {
835 } else if (newtype == hullXXAlignAt) {
836 for (row_type row = 0; row < nrows(); ++row)
837 numbered(row, false);
844 else if (type_ == hullXXAlignAt) {
845 for (row_type row = 0; row < nrows(); ++row)
846 numbered(row, false);
847 if (newtype < hullAlign) {
849 setType(hullEqnArray);
851 } else if (newtype == hullGather || newtype == hullMultline) {
859 else if (type_ == hullMultline || type_ == hullGather) {
860 if (newtype == hullGather || newtype == hullMultline)
862 else if (newtype == hullAlign || newtype == hullFlAlign ||
863 newtype == hullAlignAt || newtype == hullXAlignAt) {
866 } else if (newtype == hullXXAlignAt) {
868 for (row_type row = 0; row < nrows(); ++row)
869 numbered(row, false);
873 setType(hullEqnArray);
879 lyxerr << "mutation from '" << type_
880 << "' to '" << newtype << "' not implemented" << endl;
885 string InsetMathHull::eolString(row_type row, bool emptyline, bool fragile) const
888 if (numberedType()) {
889 if (!label_[row].empty() && !nonum_[row])
890 res += "\\label{" + label_[row] + '}';
891 if (nonum_[row] && (type_ != hullMultline))
892 res += "\\nonumber ";
894 return res + InsetMathGrid::eolString(row, emptyline, fragile);
898 void InsetMathHull::write(WriteStream & os) const
901 InsetMathGrid::write(os);
906 void InsetMathHull::normalize(NormalStream & os) const
908 os << "[formula " << type_ << ' ';
909 InsetMathGrid::normalize(os);
914 void InsetMathHull::mathmlize(MathMLStream & os) const
916 InsetMathGrid::mathmlize(os);
920 void InsetMathHull::infoize(ostream & os) const
922 os << "Type: " << type_;
926 void InsetMathHull::check() const
928 BOOST_ASSERT(nonum_.size() == nrows());
929 BOOST_ASSERT(label_.size() == nrows());
933 void InsetMathHull::doExtern(LCursor & cur, FuncRequest & func)
937 istringstream iss(lyx::to_utf8(func.argument()));
938 iss >> lang >> extra;
943 #warning temporarily disabled
944 //if (cur.selection()) {
947 // lyxerr << "use selection: " << ar << endl;
948 // insert(pipeThroughExtern(lang, extra, ar));
954 eq.push_back(MathAtom(new InsetMathChar('=')));
956 // go to first item in line
957 cur.idx() -= cur.idx() % ncols();
960 if (getType() == hullSimple) {
961 size_type pos = cur.cell().find_last(eq);
963 if (cur.inMathed() && cur.selection()) {
964 asArray(grabAndEraseSelection(cur), ar);
965 } else if (pos == cur.cell().size()) {
967 lyxerr << "use whole cell: " << ar << endl;
969 ar = MathArray(cur.cell().begin() + pos + 1, cur.cell().end());
970 lyxerr << "use partial cell form pos: " << pos << endl;
972 cur.cell().append(eq);
973 cur.cell().append(pipeThroughExtern(lang, extra, ar));
974 cur.pos() = cur.lastpos();
978 if (getType() == hullEquation) {
979 lyxerr << "use equation inset" << endl;
980 mutate(hullEqnArray);
981 MathArray & ar = cur.cell();
982 lyxerr << "use cell: " << ar << endl;
986 cur.cell() = pipeThroughExtern(lang, extra, ar);
987 // move to end of line
988 cur.pos() = cur.lastpos();
993 lyxerr << "use eqnarray" << endl;
994 cur.idx() += 2 - cur.idx() % ncols();
996 MathArray ar = cur.cell();
997 lyxerr << "use cell: " << ar << endl;
999 #warning temporarily disabled
1006 cur.cell() = pipeThroughExtern(lang, extra, ar);
1007 cur.pos() = cur.lastpos();
1012 void InsetMathHull::doDispatch(LCursor & cur, FuncRequest & cmd)
1014 //lyxerr << "action: " << cmd.action << endl;
1015 switch (cmd.action) {
1017 case LFUN_FINISHED_LEFT:
1018 case LFUN_FINISHED_RIGHT:
1019 case LFUN_FINISHED_UP:
1020 case LFUN_FINISHED_DOWN:
1021 //lyxerr << "action: " << cmd.action << endl;
1022 InsetMathGrid::doDispatch(cur, cmd);
1023 notifyCursorLeaves(cur);
1027 case LFUN_BREAK_PARAGRAPH:
1028 // just swallow this
1031 case LFUN_BREAK_LINE:
1032 // some magic for the common case
1033 if (type_ == hullSimple || type_ == hullEquation) {
1034 recordUndoInset(cur);
1036 cur.bv().buffer()->params().use_amsmath == BufferParams::AMS_ON;
1037 mutate(align ? hullAlign : hullEqnArray);
1039 cur.pos() = cur.lastpos();
1041 InsetMathGrid::doDispatch(cur, cmd);
1044 case LFUN_MATH_NUMBER:
1045 //lyxerr << "toggling all numbers" << endl;
1047 recordUndoInset(cur);
1048 bool old = numberedType();
1049 if (type_ == hullMultline)
1050 numbered(nrows() - 1, !old);
1052 for (row_type row = 0; row < nrows(); ++row)
1053 numbered(row, !old);
1055 cur.message(old ? _("No number") : _("Number"));
1059 case LFUN_MATH_NONUMBER:
1061 recordUndoInset(cur);
1062 row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1063 bool old = numbered(r);
1064 cur.message(old ? _("No number") : _("Number"));
1069 case LFUN_LABEL_INSERT: {
1070 recordUndoInset(cur);
1071 row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1072 string old_label = label(r);
1073 string const default_label =
1074 (lyxrc.label_init_length >= 0) ? "eq:" : "";
1075 if (old_label.empty())
1076 old_label = default_label;
1077 string const contents = cmd.argument().empty() ?
1078 old_label : lyx::to_utf8(cmd.argument());
1080 InsetCommandParams p("label", contents);
1081 string const data = InsetCommandMailer::params2string("label", p);
1083 if (cmd.argument().empty())
1084 cur.bv().owner()->getDialogs().show("label", data, 0);
1086 FuncRequest fr(LFUN_INSET_INSERT, data);
1092 case LFUN_INSET_INSERT: {
1093 //lyxerr << "arg: " << lyx::to_utf8(cmd.argument()) << endl;
1094 string const name = cmd.getArg(0);
1095 if (name == "label") {
1096 InsetCommandParams p;
1097 InsetCommandMailer::string2params(name, lyx::to_utf8(cmd.argument()), p);
1098 string str = p.getContents();
1099 recordUndoInset(cur);
1100 row_type const r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1101 str = lyx::support::trim(str);
1104 string old = label(r);
1106 cur.bv().buffer()->changeRefsIfUnique(old, str);
1112 if (createInsetMath_fromDialogStr(lyx::to_utf8(cmd.argument()), ar)) {
1120 case LFUN_MATH_EXTERN:
1121 recordUndoInset(cur);
1125 case LFUN_MATH_MUTATE: {
1126 recordUndoInset(cur);
1127 row_type row = cur.row();
1128 col_type col = cur.col();
1129 mutate(hullType(lyx::to_utf8(cmd.argument())));
1130 cur.idx() = row * ncols() + col;
1131 if (cur.idx() > cur.lastidx()) {
1132 cur.idx() = cur.lastidx();
1133 cur.pos() = cur.lastpos();
1135 if (cur.pos() > cur.lastpos())
1136 cur.pos() = cur.lastpos();
1137 //cur.dispatched(FINISHED);
1141 case LFUN_MATH_DISPLAY: {
1142 recordUndoInset(cur);
1143 mutate(type_ == hullSimple ? hullEquation : hullSimple);
1145 cur.pos() = cur.lastpos();
1146 //cur.dispatched(FINISHED);
1151 InsetMathGrid::doDispatch(cur, cmd);
1157 bool InsetMathHull::getStatus(LCursor & cur, FuncRequest const & cmd,
1158 FuncStatus & status) const
1160 switch (cmd.action) {
1161 case LFUN_FINISHED_LEFT:
1162 case LFUN_FINISHED_RIGHT:
1163 case LFUN_FINISHED_UP:
1164 case LFUN_FINISHED_DOWN:
1165 status.enabled(true);
1167 case LFUN_BREAK_LINE:
1168 case LFUN_MATH_NUMBER:
1169 case LFUN_MATH_NONUMBER:
1170 case LFUN_MATH_EXTERN:
1171 case LFUN_MATH_MUTATE:
1172 case LFUN_MATH_DISPLAY:
1174 status.enabled(true);
1176 case LFUN_LABEL_INSERT:
1177 status.enabled(type_ != hullSimple);
1179 case LFUN_INSET_INSERT: {
1180 // Don't test createInsetMath_fromDialogStr(), since
1181 // getStatus is not called with a valid reference and the
1182 // dialog would not be applyable.
1183 string const name = cmd.getArg(0);
1184 status.enabled(name == "ref" ||
1185 (name == "label" && type_ != hullSimple));
1188 case LFUN_TABULAR_FEATURE: {
1189 istringstream is(lyx::to_utf8(cmd.argument()));
1193 && (s == "append-row"
1194 || s == "delete-row"
1195 || s == "copy-row")) {
1196 status.message(bformat(
1197 lyx::from_utf8(N_("Can't change number of rows in '%1$s'")),
1198 lyx::from_utf8(hullName(type_))));
1199 status.enabled(false);
1203 && (s == "append-column"
1204 || s == "delete-column"
1205 || s == "copy-column")) {
1206 status.message(bformat(
1207 lyx::from_utf8(N_("Can't change number of columns in '%1$s'")),
1208 lyx::from_utf8(hullName(type_))));
1209 status.enabled(false);
1212 if ((type_ == hullSimple
1213 || type_ == hullEquation
1214 || type_ == hullNone) &&
1215 (s == "add-hline-above" || s == "add-hline-below")) {
1216 status.message(bformat(
1217 lyx::from_utf8(N_("Can't add horizontal grid lines in '%1$s'")),
1218 lyx::from_utf8(hullName(type_))));
1219 status.enabled(false);
1222 if (s == "add-vline-left" || s == "add-vline-right") {
1223 status.message(bformat(
1224 lyx::from_utf8(N_("Can't add vertical grid lines in '%1$s'")),
1225 lyx::from_utf8(hullName(type_))));
1226 status.enabled(false);
1229 if (s == "valign-top" || s == "valign-middle"
1230 || s == "valign-bottom" || s == "align-left"
1231 || s == "align-center" || s == "align-right") {
1232 status.enabled(false);
1235 return InsetMathGrid::getStatus(cur, cmd, status);
1238 return InsetMathGrid::getStatus(cur, cmd, status);
1241 // This cannot really happen, but inserted to shut-up gcc
1242 return InsetMathGrid::getStatus(cur, cmd, status);
1246 /////////////////////////////////////////////////////////////////////
1250 // simply scrap this function if you want
1251 void InsetMathHull::mutateToText()
1254 // translate to latex
1256 latex(NULL, os, false, false);
1257 string str = os.str();
1260 LyXText * lt = view_->getLyXText();
1261 string::const_iterator cit = str.begin();
1262 string::const_iterator end = str.end();
1263 for (; cit != end; ++cit)
1264 view_->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
1267 //view_->owner()->dispatch(LFUN_ESCAPE);
1272 void InsetMathHull::handleFont(LCursor & cur, string const & arg,
1273 string const & font)
1275 // this whole function is a hack and won't work for incremental font
1278 if (cur.inset().asInsetMath()->name() == font)
1279 cur.handleFont(font);
1281 cur.handleNest(createInsetMath(font));
1287 void InsetMathHull::handleFont2(LCursor & cur, string const & arg)
1292 bv_funcs::string2font(arg, font, b);
1293 if (font.color() != LColor::inherit) {
1294 MathAtom at = MathAtom(new InsetMathColor(true, font.color()));
1295 cur.handleNest(at, 0);
1300 void InsetMathHull::edit(LCursor & cur, bool left)
1303 left ? idxFirst(cur) : idxLast(cur);
1307 docstring const InsetMathHull::editMessage() const
1309 return _("Math editor mode");
1313 void InsetMathHull::revealCodes(LCursor & cur) const
1315 if (!cur.inMathed())
1319 cur.message(lyx::from_utf8(os.str()));
1321 // write something to the minibuffer
1322 // translate to latex
1326 string str = os.str();
1328 string::size_type pos = 0;
1330 for (string::iterator it = str.begin(); it != str.end(); ++it) {
1333 else if (*it == '\0') {
1335 pos = it - str.begin();
1341 res = res.substr(pos - 30);
1342 if (res.size() > 60)
1343 res = res.substr(0, 60);
1349 InsetBase::Code InsetMathHull::lyxCode() const
1355 /////////////////////////////////////////////////////////////////////
1359 bool InsetMathHull::searchForward(BufferView * bv, string const & str,
1362 #ifdef WITH_WARNINGS
1363 #warning completely broken
1365 static InsetMathHull * lastformula = 0;
1366 static CursorBase current = DocIterator(ibegin(nucleus()));
1367 static MathArray ar;
1368 static string laststr;
1370 if (lastformula != this || laststr != str) {
1371 //lyxerr << "reset lastformula to " << this << endl;
1374 current = ibegin(nucleus());
1376 mathed_parse_cell(ar, str);
1380 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
1382 for (DocIterator it = current; it != iend(nucleus()); increment(it)) {
1383 CursorSlice & top = it.back();
1384 MathArray const & a = top.asInsetMath()->cell(top.idx_);
1385 if (a.matchpart(ar, top.pos_)) {
1386 bv->cursor().setSelection(it, ar.size());
1388 top.pos_ += ar.size();
1394 //lyxerr << "not found!" << endl;
1401 void InsetMathHull::write(Buffer const &, std::ostream & os) const
1403 WriteStream wi(os, false, false);
1409 void InsetMathHull::read(Buffer const &, LyXLex & lex)
1412 mathed_parse_normal(at, lex);
1413 operator=(*at->asHullInset());
1417 int InsetMathHull::plaintext(Buffer const &, ostream & os,
1418 OutputParams const &) const
1420 if (0 && display()) {
1424 TextPainter tpain(dim.width(), dim.height());
1425 drawT(tpain, 0, dim.ascent());
1427 // reset metrics cache to "real" values
1429 return tpain.textheight();
1431 WriteStream wi(os, false, true);
1438 int InsetMathHull::docbook(Buffer const & buf, ostream & os,
1439 OutputParams const & runparams) const
1441 MathMLStream ms(os);
1444 if (getType() == hullSimple)
1445 name = "inlineequation";
1447 name = "informalequation";
1449 string bname = name;
1450 if (!label(0).empty())
1451 bname += " id=\"" + sgml::cleanID(buf, runparams, label(0)) + "\"";
1452 ms << MTag(bname.c_str());
1455 if (runparams.flavor == OutputParams::XML) {
1456 ms << MTag("alt role=\"tex\" ");
1457 // Workaround for db2latex: db2latex always includes equations with
1458 // \ensuremath{} or \begin{display}\end{display}
1459 // so we strip LyX' math environment
1460 WriteStream wi(ls, false, false);
1461 InsetMathGrid::write(wi);
1462 ms << subst(subst(ls.str(), "&", "&"), "<", "<");
1465 InsetMathGrid::mathmlize(ms);
1468 ms << MTag("alt role=\"tex\"");
1469 res = latex(buf, ls, runparams);
1470 ms << subst(subst(ls.str(), "&", "&"), "<", "<");
1474 ms << "<graphic fileref=\"eqn/";
1475 if ( !label(0).empty())
1476 ms << sgml::cleanID(buf, runparams, label(0));
1478 ms << sgml::uniqueID("anon");
1480 if (runparams.flavor == OutputParams::XML)
1485 ms << ETag(name.c_str());
1486 return ms.line() + res;
1490 int InsetMathHull::textString(Buffer const & buf, ostream & os,
1491 OutputParams const & op) const
1493 return plaintext(buf, os, op);