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 "graphics/PreviewImage.h"
54 #include "graphics/PreviewLoader.h"
56 #include "support/lyxlib.h"
57 #include "support/lstrings.h"
59 #include <boost/bind.hpp>
64 using lyx::odocstream;
65 using lyx::odocstringstream;
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 docstring const nl = nicelabel(row);
357 setPosCache(pi, x, y);
361 void InsetMathHull::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
364 InsetMathGrid::metricsT(mi, dim);
367 WriteStream wi(os, false, true);
369 dim.wid = os.str().size();
376 void InsetMathHull::drawT(TextPainter & pain, int x, int y) const
379 InsetMathGrid::drawT(pain, x, y);
382 WriteStream wi(os, false, true);
384 pain.draw(x, y, os.str().c_str());
391 docstring const latex_string(InsetMathHull const & inset)
394 WriteStream wi(ls, false, false);
402 void InsetMathHull::addPreview(lyx::graphics::PreviewLoader & ploader) const
404 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
405 docstring const snippet = latex_string(*this);
406 preview_->addPreview(snippet, ploader);
411 bool InsetMathHull::notifyCursorLeaves(LCursor & cur)
413 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
414 Buffer const & buffer = cur.buffer();
415 docstring const snippet = latex_string(*this);
416 preview_->addPreview(snippet, buffer);
417 preview_->startLoading(buffer);
423 string InsetMathHull::label(row_type row) const
425 BOOST_ASSERT(row < nrows());
430 void InsetMathHull::label(row_type row, string const & label)
432 //lyxerr << "setting label '" << label << "' for row " << row << endl;
437 void InsetMathHull::numbered(row_type row, bool num)
443 bool InsetMathHull::numbered(row_type row) const
449 bool InsetMathHull::ams() const
452 type_ == hullAlign ||
453 type_ == hullFlAlign ||
454 type_ == hullMultline ||
455 type_ == hullGather ||
456 type_ == hullAlignAt ||
457 type_ == hullXAlignAt ||
458 type_ == hullXXAlignAt;
462 bool InsetMathHull::display() const
464 return type_ != hullSimple && type_ != hullNone;
468 void InsetMathHull::getLabelList(Buffer const &, vector<docstring> & labels) const
470 for (row_type row = 0; row < nrows(); ++row)
471 if (!label_[row].empty() && nonum_[row] != 1)
473 labels.push_back(lyx::from_utf8(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();
526 os << "\\begin{equation" << star(n) << "}\n";
536 os << "\\begin{" << hullName(type_) << star(n) << "}\n";
541 os << "\\begin{" << hullName(type_) << star(n) << '}'
542 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
546 os << "\\begin{" << hullName(type_) << '}'
547 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
551 os << "\\begin{unknown" << star(n) << '}';
557 void InsetMathHull::footer_write(WriteStream & os) const
559 bool n = numberedType();
572 os << "\\end{equation" << star(n) << "}\n";
584 os << "\\end{" << hullName(type_) << star(n) << "}\n";
588 os << "\\end{" << hullName(type_) << "}\n";
592 os << "\\end{unknown" << star(n) << '}';
598 bool InsetMathHull::rowChangeOK() const
601 type_ == hullEqnArray || type_ == hullAlign ||
602 type_ == hullFlAlign || type_ == hullAlignAt ||
603 type_ == hullXAlignAt || type_ == hullXXAlignAt ||
604 type_ == hullGather || type_ == hullMultline;
608 bool InsetMathHull::colChangeOK() const
611 type_ == hullAlign || type_ == hullFlAlign ||type_ == hullAlignAt ||
612 type_ == hullXAlignAt || type_ == hullXXAlignAt;
616 void InsetMathHull::addRow(row_type row)
620 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
621 label_.insert(label_.begin() + row + 1, string());
622 InsetMathGrid::addRow(row);
626 void InsetMathHull::swapRow(row_type row)
630 if (row + 1 == nrows())
632 swap(nonum_[row], nonum_[row + 1]);
633 swap(label_[row], label_[row + 1]);
634 InsetMathGrid::swapRow(row);
638 void InsetMathHull::delRow(row_type row)
640 if (nrows() <= 1 || !rowChangeOK())
642 InsetMathGrid::delRow(row);
643 // The last dummy row has no number info nor a label.
644 // Test nrows() + 1 because we have already erased the row.
645 if (row == nrows() + 1)
647 nonum_.erase(nonum_.begin() + row);
648 label_.erase(label_.begin() + row);
652 void InsetMathHull::addCol(col_type col)
656 InsetMathGrid::addCol(col);
660 void InsetMathHull::delCol(col_type col)
662 if (ncols() <= 1 || !colChangeOK())
664 InsetMathGrid::delCol(col);
668 docstring InsetMathHull::nicelabel(row_type row) const
672 if (label_[row].empty())
673 return lyx::from_ascii("(#)");
675 return lyx::from_utf8('(' + label_[row] + ')');
679 void InsetMathHull::glueall()
682 for (idx_type i = 0; i < nargs(); ++i)
684 *this = InsetMathHull(hullSimple);
690 void InsetMathHull::splitTo2Cols()
692 BOOST_ASSERT(ncols() == 1);
693 InsetMathGrid::addCol(1);
694 for (row_type row = 0; row < nrows(); ++row) {
695 idx_type const i = 2 * row;
696 pos_type pos = firstRelOp(cell(i));
697 cell(i + 1) = MathArray(cell(i).begin() + pos, cell(i).end());
698 cell(i).erase(pos, cell(i).size());
703 void InsetMathHull::splitTo3Cols()
705 BOOST_ASSERT(ncols() < 3);
708 InsetMathGrid::addCol(1);
709 for (row_type row = 0; row < nrows(); ++row) {
710 idx_type const i = 3 * row + 1;
711 if (cell(i).size()) {
712 cell(i + 1) = MathArray(cell(i).begin() + 1, cell(i).end());
713 cell(i).erase(1, cell(i).size());
719 void InsetMathHull::changeCols(col_type cols)
723 else if (ncols() < cols) {
729 while (ncols() < cols)
730 InsetMathGrid::addCol(ncols() - 1);
736 for (row_type row = 0; row < nrows(); ++row) {
737 idx_type const i = row * ncols();
738 for (col_type col = cols; col < ncols(); ++col) {
739 cell(i + cols - 1).append(cell(i + col));
743 while (ncols() > cols) {
744 InsetMathGrid::delCol(ncols() - 1);
749 HullType InsetMathHull::getType() const
755 void InsetMathHull::setType(HullType type)
762 void InsetMathHull::mutate(HullType newtype)
764 //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
766 // we try to move along the chain
767 // none <-> simple <-> equation <-> eqnarray -> *align* -> multline, gather -+
769 // +-------------------------------------+
770 // we use eqnarray as intermediate type for mutations that are not
771 // directly supported because it handles labels and numbering for
774 if (newtype == type_) {
778 else if (newtype < hullNone) {
783 else if (type_ == hullNone) {
789 else if (type_ == hullSimple) {
790 if (newtype == hullNone) {
794 setType(hullEquation);
800 else if (type_ == hullEquation) {
801 if (newtype < type_) {
805 } else if (newtype == hullEqnArray) {
806 // split it "nicely" on the first relop
808 setType(hullEqnArray);
809 } else if (newtype == hullMultline || newtype == hullGather) {
819 else if (type_ == hullEqnArray) {
820 if (newtype < type_) {
821 // set correct (no)numbering
822 bool allnonum = true;
823 for (row_type row = 0; row < nrows(); ++row)
827 // set first non-empty label
829 for (row_type row = 0; row < nrows(); ++row) {
830 if (!label_[row].empty()) {
837 nonum_[0] = allnonum;
840 } else { // align & Co.
847 else if (type_ == hullAlign || type_ == hullAlignAt ||
848 type_ == hullXAlignAt || type_ == hullFlAlign) {
849 if (newtype < hullAlign) {
851 setType(hullEqnArray);
853 } else if (newtype == hullGather || newtype == hullMultline) {
856 } else if (newtype == hullXXAlignAt) {
857 for (row_type row = 0; row < nrows(); ++row)
858 numbered(row, false);
865 else if (type_ == hullXXAlignAt) {
866 for (row_type row = 0; row < nrows(); ++row)
867 numbered(row, false);
868 if (newtype < hullAlign) {
870 setType(hullEqnArray);
872 } else if (newtype == hullGather || newtype == hullMultline) {
880 else if (type_ == hullMultline || type_ == hullGather) {
881 if (newtype == hullGather || newtype == hullMultline)
883 else if (newtype == hullAlign || newtype == hullFlAlign ||
884 newtype == hullAlignAt || newtype == hullXAlignAt) {
887 } else if (newtype == hullXXAlignAt) {
889 for (row_type row = 0; row < nrows(); ++row)
890 numbered(row, false);
894 setType(hullEqnArray);
900 lyxerr << "mutation from '" << hullName(type_)
901 << "' to '" << hullName(newtype)
902 << "' not implemented" << endl;
907 string InsetMathHull::eolString(row_type row, bool emptyline, bool fragile) const
910 if (numberedType()) {
911 if (!label_[row].empty() && !nonum_[row])
912 res += "\\label{" + label_[row] + '}';
913 if (nonum_[row] && (type_ != hullMultline))
914 res += "\\nonumber ";
916 return res + InsetMathGrid::eolString(row, emptyline, fragile);
920 void InsetMathHull::write(WriteStream & os) const
923 InsetMathGrid::write(os);
928 void InsetMathHull::normalize(NormalStream & os) const
930 os << "[formula " << hullName(type_) << ' ';
931 InsetMathGrid::normalize(os);
936 void InsetMathHull::mathmlize(MathMLStream & os) const
938 InsetMathGrid::mathmlize(os);
942 void InsetMathHull::infoize(ostream & os) const
944 os << "Type: " << hullName(type_);
948 void InsetMathHull::check() const
950 BOOST_ASSERT(nonum_.size() == nrows());
951 BOOST_ASSERT(label_.size() == nrows());
955 void InsetMathHull::doExtern(LCursor & cur, FuncRequest & func)
959 lyx::idocstringstream iss(func.argument());
960 iss >> dlang >> extra;
962 extra = lyx::from_ascii("noextra");
963 string const lang = lyx::to_ascii(dlang);
966 #warning temporarily disabled
967 //if (cur.selection()) {
970 // lyxerr << "use selection: " << ar << endl;
971 // insert(pipeThroughExtern(lang, extra, ar));
977 eq.push_back(MathAtom(new InsetMathChar('=')));
979 // go to first item in line
980 cur.idx() -= cur.idx() % ncols();
983 if (getType() == hullSimple) {
984 size_type pos = cur.cell().find_last(eq);
986 if (cur.inMathed() && cur.selection()) {
988 asArray(lyx::from_utf8(grabAndEraseSelection(cur)), ar);
989 } else if (pos == cur.cell().size()) {
991 lyxerr << "use whole cell: " << ar << endl;
993 ar = MathArray(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 MathArray & 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 MathArray ar = cur.cell();
1021 lyxerr << "use cell: " << ar << endl;
1022 #ifdef WITH_WARNINGS
1023 #warning temporarily disabled
1030 cur.cell() = pipeThroughExtern(lang, extra, ar);
1031 cur.pos() = cur.lastpos();
1036 void InsetMathHull::doDispatch(LCursor & cur, FuncRequest & cmd)
1038 //lyxerr << "action: " << cmd.action << endl;
1039 switch (cmd.action) {
1041 case LFUN_FINISHED_LEFT:
1042 case LFUN_FINISHED_RIGHT:
1043 case LFUN_FINISHED_UP:
1044 case LFUN_FINISHED_DOWN:
1045 //lyxerr << "action: " << cmd.action << endl;
1046 InsetMathGrid::doDispatch(cur, cmd);
1047 notifyCursorLeaves(cur);
1051 case LFUN_BREAK_PARAGRAPH:
1052 // just swallow this
1055 case LFUN_BREAK_LINE:
1056 // some magic for the common case
1057 if (type_ == hullSimple || type_ == hullEquation) {
1058 recordUndoInset(cur);
1060 cur.bv().buffer()->params().use_amsmath == BufferParams::AMS_ON;
1061 mutate(align ? hullAlign : hullEqnArray);
1063 cur.pos() = cur.lastpos();
1065 InsetMathGrid::doDispatch(cur, cmd);
1068 case LFUN_MATH_NUMBER:
1069 //lyxerr << "toggling all numbers" << endl;
1071 recordUndoInset(cur);
1072 bool old = numberedType();
1073 if (type_ == hullMultline)
1074 numbered(nrows() - 1, !old);
1076 for (row_type row = 0; row < nrows(); ++row)
1077 numbered(row, !old);
1079 cur.message(old ? _("No number") : _("Number"));
1083 case LFUN_MATH_NONUMBER:
1085 recordUndoInset(cur);
1086 row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1087 bool old = numbered(r);
1088 cur.message(old ? _("No number") : _("Number"));
1093 case LFUN_LABEL_INSERT: {
1094 recordUndoInset(cur);
1095 row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1096 docstring old_label = lyx::from_utf8(label(r));
1097 docstring const default_label = lyx::from_ascii(
1098 (lyxrc.label_init_length >= 0) ? "eq:" : "");
1099 if (old_label.empty())
1100 old_label = default_label;
1102 InsetCommandParams p("label");
1103 p["name"] = cmd.argument().empty() ? old_label : cmd.argument();
1104 string const data = InsetCommandMailer::params2string("label", p);
1106 if (cmd.argument().empty())
1107 cur.bv().showInsetDialog("label", data, 0);
1109 FuncRequest fr(LFUN_INSET_INSERT, data);
1115 case LFUN_INSET_INSERT: {
1116 //lyxerr << "arg: " << lyx::to_utf8(cmd.argument()) << endl;
1117 string const name = cmd.getArg(0);
1118 if (name == "label") {
1119 InsetCommandParams p("label");
1120 InsetCommandMailer::string2params(name, lyx::to_utf8(cmd.argument()), p);
1121 string str = p.getContents();
1122 recordUndoInset(cur);
1123 row_type const r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1124 str = lyx::support::trim(str);
1127 string old = label(r);
1129 cur.bv().buffer()->changeRefsIfUnique(old, str,
1130 InsetBase::REF_CODE);
1135 InsetMathGrid::doDispatch(cur, cmd);
1139 case LFUN_MATH_EXTERN:
1140 recordUndoInset(cur);
1144 case LFUN_MATH_MUTATE: {
1145 recordUndoInset(cur);
1146 row_type row = cur.row();
1147 col_type col = cur.col();
1148 mutate(hullType(lyx::to_utf8(cmd.argument())));
1149 cur.idx() = row * ncols() + col;
1150 if (cur.idx() > cur.lastidx()) {
1151 cur.idx() = cur.lastidx();
1152 cur.pos() = cur.lastpos();
1154 if (cur.pos() > cur.lastpos())
1155 cur.pos() = cur.lastpos();
1156 //cur.dispatched(FINISHED);
1160 case LFUN_MATH_DISPLAY: {
1161 recordUndoInset(cur);
1162 mutate(type_ == hullSimple ? hullEquation : hullSimple);
1164 cur.pos() = cur.lastpos();
1165 //cur.dispatched(FINISHED);
1170 InsetMathGrid::doDispatch(cur, cmd);
1176 bool InsetMathHull::getStatus(LCursor & cur, FuncRequest const & cmd,
1177 FuncStatus & status) const
1179 switch (cmd.action) {
1180 case LFUN_FINISHED_LEFT:
1181 case LFUN_FINISHED_RIGHT:
1182 case LFUN_FINISHED_UP:
1183 case LFUN_FINISHED_DOWN:
1184 status.enabled(true);
1186 case LFUN_BREAK_LINE:
1187 case LFUN_MATH_NUMBER:
1188 case LFUN_MATH_NONUMBER:
1189 case LFUN_MATH_EXTERN:
1190 case LFUN_MATH_MUTATE:
1191 case LFUN_MATH_DISPLAY:
1193 status.enabled(true);
1195 case LFUN_LABEL_INSERT:
1196 status.enabled(type_ != hullSimple);
1198 case LFUN_INSET_INSERT:
1199 if (cmd.getArg(0) == "label") {
1200 status.enabled(type_ != hullSimple);
1203 return InsetMathGrid::getStatus(cur, cmd, status);
1204 case LFUN_TABULAR_FEATURE: {
1205 istringstream is(lyx::to_utf8(cmd.argument()));
1209 && (s == "append-row"
1210 || s == "delete-row"
1211 || s == "copy-row")) {
1212 status.message(bformat(
1213 lyx::from_utf8(N_("Can't change number of rows in '%1$s'")),
1214 lyx::from_utf8(hullName(type_))));
1215 status.enabled(false);
1219 && (s == "append-column"
1220 || s == "delete-column"
1221 || s == "copy-column")) {
1222 status.message(bformat(
1223 lyx::from_utf8(N_("Can't change number of columns in '%1$s'")),
1224 lyx::from_utf8(hullName(type_))));
1225 status.enabled(false);
1228 if ((type_ == hullSimple
1229 || type_ == hullEquation
1230 || type_ == hullNone) &&
1231 (s == "add-hline-above" || s == "add-hline-below")) {
1232 status.message(bformat(
1233 lyx::from_utf8(N_("Can't add horizontal grid lines in '%1$s'")),
1234 lyx::from_utf8(hullName(type_))));
1235 status.enabled(false);
1238 if (s == "add-vline-left" || s == "add-vline-right") {
1239 status.message(bformat(
1240 lyx::from_utf8(N_("Can't add vertical grid lines in '%1$s'")),
1241 lyx::from_utf8(hullName(type_))));
1242 status.enabled(false);
1245 if (s == "valign-top" || s == "valign-middle"
1246 || s == "valign-bottom" || s == "align-left"
1247 || s == "align-center" || s == "align-right") {
1248 status.enabled(false);
1251 return InsetMathGrid::getStatus(cur, cmd, status);
1254 return InsetMathGrid::getStatus(cur, cmd, status);
1257 // This cannot really happen, but inserted to shut-up gcc
1258 return InsetMathGrid::getStatus(cur, cmd, status);
1262 /////////////////////////////////////////////////////////////////////
1266 // simply scrap this function if you want
1267 void InsetMathHull::mutateToText()
1270 // translate to latex
1272 latex(NULL, os, false, false);
1273 string str = os.str();
1276 LyXText * lt = view_->getLyXText();
1277 string::const_iterator cit = str.begin();
1278 string::const_iterator end = str.end();
1279 for (; cit != end; ++cit)
1280 view_->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
1283 //lyx::dispatch(LFUN_ESCAPE);
1288 void InsetMathHull::handleFont(LCursor & cur, string const & arg,
1289 string const & font)
1291 // this whole function is a hack and won't work for incremental font
1294 if (cur.inset().asInsetMath()->name() == font)
1295 cur.handleFont(font);
1297 cur.handleNest(createInsetMath(font));
1303 void InsetMathHull::handleFont2(LCursor & cur, string const & arg)
1308 bv_funcs::string2font(arg, font, b);
1309 if (font.color() != LColor::inherit) {
1310 MathAtom at = MathAtom(new InsetMathColor(true, font.color()));
1311 cur.handleNest(at, 0);
1316 void InsetMathHull::edit(LCursor & cur, bool left)
1319 left ? idxFirst(cur) : idxLast(cur);
1323 docstring const InsetMathHull::editMessage() const
1325 return _("Math editor mode");
1329 void InsetMathHull::revealCodes(LCursor & cur) const
1331 if (!cur.inMathed())
1335 cur.message(lyx::from_utf8(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 InsetBase::Code InsetMathHull::lyxCode() const
1371 /////////////////////////////////////////////////////////////////////
1375 bool InsetMathHull::searchForward(BufferView * bv, string const & str,
1378 #ifdef WITH_WARNINGS
1379 #warning completely broken
1381 static InsetMathHull * lastformula = 0;
1382 static CursorBase current = DocIterator(ibegin(nucleus()));
1383 static MathArray ar;
1384 static string laststr;
1386 if (lastformula != this || laststr != str) {
1387 //lyxerr << "reset lastformula to " << this << endl;
1390 current = ibegin(nucleus());
1392 mathed_parse_cell(ar, str);
1396 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
1398 for (DocIterator it = current; it != iend(nucleus()); increment(it)) {
1399 CursorSlice & top = it.back();
1400 MathArray const & a = top.asInsetMath()->cell(top.idx_);
1401 if (a.matchpart(ar, top.pos_)) {
1402 bv->cursor().setSelection(it, ar.size());
1404 top.pos_ += ar.size();
1410 //lyxerr << "not found!" << endl;
1417 void InsetMathHull::write(Buffer const &, std::ostream & os) const
1419 odocstringstream oss;
1420 WriteStream wi(oss, false, false);
1423 os << lyx::to_utf8(oss.str());
1427 void InsetMathHull::read(Buffer const &, LyXLex & lex)
1430 mathed_parse_normal(at, lex);
1431 operator=(*at->asHullInset());
1435 int InsetMathHull::plaintext(Buffer const &, odocstream & os,
1436 OutputParams const &) const
1438 if (0 && display()) {
1442 TextPainter tpain(dim.width(), dim.height());
1443 drawT(tpain, 0, dim.ascent());
1445 // reset metrics cache to "real" values
1447 return tpain.textheight();
1449 WriteStream wi(os, false, true);
1456 int InsetMathHull::docbook(Buffer const & buf, odocstream & os,
1457 OutputParams const & runparams) const
1459 MathMLStream ms(os);
1462 if (getType() == hullSimple)
1463 name = "inlineequation";
1465 name = "informalequation";
1467 string bname = name;
1468 if (!label(0).empty())
1469 bname += " id=\"" + sgml::cleanID(buf, runparams, label(0)) + "\"";
1470 ms << MTag(bname.c_str());
1472 odocstringstream ls;
1473 if (runparams.flavor == OutputParams::XML) {
1474 ms << MTag("alt role=\"tex\" ");
1475 // Workaround for db2latex: db2latex always includes equations with
1476 // \ensuremath{} or \begin{display}\end{display}
1477 // so we strip LyX' math environment
1478 WriteStream wi(ls, false, false);
1479 InsetMathGrid::write(wi);
1480 ms << subst(subst(lyx::to_utf8(ls.str()), "&", "&"), "<", "<");
1483 InsetMathGrid::mathmlize(ms);
1486 ms << MTag("alt role=\"tex\"");
1487 res = latex(buf, ls, runparams);
1488 ms << subst(subst(lyx::to_utf8(ls.str()), "&", "&"), "<", "<");
1492 ms << "<graphic fileref=\"eqn/";
1493 if ( !label(0).empty())
1494 ms << sgml::cleanID(buf, runparams, label(0));
1496 ms << sgml::uniqueID("anon");
1498 if (runparams.flavor == OutputParams::XML)
1503 ms << ETag(name.c_str());
1504 return ms.line() + res;
1508 int InsetMathHull::textString(Buffer const & buf, odocstream & os,
1509 OutputParams const & op) const
1511 return plaintext(buf, os, op);