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"
33 #include "LaTeXFeatures.h"
36 #include "DispatchResult.h"
37 #include "FuncRequest.h"
40 #include "OutputParams.h"
43 #include "TextPainter.h"
45 #include "insets/RenderPreview.h"
46 #include "insets/InsetLabel.h"
48 #include "graphics/PreviewImage.h"
49 #include "graphics/PreviewLoader.h"
51 #include "frontends/Painter.h"
53 #include "support/lyxlib.h"
54 #include "support/lstrings.h"
61 using cap::grabAndEraseSelection;
62 using support::bformat;
68 using std::istringstream;
69 using std::ostringstream;
76 int getCols(HullType type)
93 // returns position of first relation operator in the array
94 // used for "intelligent splitting"
95 size_t firstRelOp(MathData const & ar)
97 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
99 return it - ar.begin();
104 char const * star(bool numbered)
106 return numbered ? "" : "*";
110 } // end anon namespace
113 HullType hullType(docstring const & s)
115 if (s == "none") return hullNone;
116 if (s == "simple") return hullSimple;
117 if (s == "equation") return hullEquation;
118 if (s == "eqnarray") return hullEqnArray;
119 if (s == "align") return hullAlign;
120 if (s == "alignat") return hullAlignAt;
121 if (s == "xalignat") return hullXAlignAt;
122 if (s == "xxalignat") return hullXXAlignAt;
123 if (s == "multline") return hullMultline;
124 if (s == "gather") return hullGather;
125 if (s == "flalign") return hullFlAlign;
126 lyxerr << "unknown hull type '" << to_utf8(s) << "'" << endl;
131 docstring hullName(HullType type)
134 case hullNone: return from_ascii("none");
135 case hullSimple: return from_ascii("simple");
136 case hullEquation: return from_ascii("equation");
137 case hullEqnArray: return from_ascii("eqnarray");
138 case hullAlign: return from_ascii("align");
139 case hullAlignAt: return from_ascii("alignat");
140 case hullXAlignAt: return from_ascii("xalignat");
141 case hullXXAlignAt: return from_ascii("xxalignat");
142 case hullMultline: return from_ascii("multline");
143 case hullGather: return from_ascii("gather");
144 case hullFlAlign: return from_ascii("flalign");
146 lyxerr << "unknown hull type '" << type << "'" << endl;
147 return from_ascii("none");
152 InsetMathHull::InsetMathHull()
153 : InsetMathGrid(1, 1), type_(hullNone), nonum_(1), label_(1),
154 preview_(new RenderPreview(this))
156 //lyxerr << "sizeof InsetMath: " << sizeof(InsetMath) << endl;
157 //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl;
158 //lyxerr << "sizeof InsetMathChar: " << sizeof(InsetMathChar) << endl;
159 //lyxerr << "sizeof FontInfo: " << sizeof(FontInfo) << endl;
165 InsetMathHull::InsetMathHull(HullType type)
166 : InsetMathGrid(getCols(type), 1), type_(type), nonum_(1), label_(1),
167 preview_(new RenderPreview(this))
174 InsetMathHull::InsetMathHull(InsetMathHull const & other)
175 : InsetMathGrid(other),
176 type_(other.type_), nonum_(other.nonum_), label_(other.label_),
177 preview_(new RenderPreview(*other.preview_, this))
181 InsetMathHull::~InsetMathHull()
185 Inset * InsetMathHull::clone() const
187 return new InsetMathHull(*this);
191 InsetMathHull & InsetMathHull::operator=(InsetMathHull const & other)
195 *static_cast<InsetMathGrid*>(this) = InsetMathGrid(other);
197 nonum_ = other.nonum_;
198 label_ = other.label_;
199 preview_.reset(new RenderPreview(*other.preview_, this));
205 Inset * InsetMathHull::editXY(Cursor & cur, int x, int y)
211 return InsetMathNest::editXY(cur, x, y);
215 InsetMath::mode_type InsetMathHull::currentMode() const
217 if (type_ == hullNone)
218 return UNDECIDED_MODE;
219 // definitely math mode ...
224 bool InsetMathHull::idxFirst(Cursor & cur) const
232 bool InsetMathHull::idxLast(Cursor & cur) const
234 cur.idx() = nargs() - 1;
235 cur.pos() = cur.lastpos();
240 char InsetMathHull::defaultColAlign(col_type col)
242 if (type_ == hullEqnArray)
244 if (type_ >= hullAlign)
245 return "rl"[col & 1];
250 int InsetMathHull::defaultColSpace(col_type col)
252 if (type_ == hullAlign || type_ == hullAlignAt)
254 if (type_ == hullXAlignAt)
255 return (col & 1) ? 20 : 0;
256 if (type_ == hullXXAlignAt || type_ == hullFlAlign)
257 return (col & 1) ? 40 : 0;
262 docstring InsetMathHull::standardFont() const
264 return from_ascii(type_ == hullNone ? "lyxnochange" : "mathnormal");
268 bool InsetMathHull::previewState(BufferView * bv) const
270 if (!editing(bv) && RenderPreview::status() == LyXRC::PREVIEW_ON) {
271 graphics::PreviewImage const * pimage =
272 preview_->getPreviewImage(bv->buffer());
273 return pimage && pimage->image();
279 void InsetMathHull::metrics(MetricsInfo & mi, Dimension & dim) const
281 if (previewState(mi.base.bv)) {
282 preview_->metrics(mi, dim);
283 // insert a one pixel gap in front of the formula
286 dim.des += displayMargin();
287 // Cache the inset dimension.
288 setDimCache(mi, dim);
292 FontSetChanger dummy1(mi.base, standardFont());
293 StyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
295 // let the cells adjust themselves
296 InsetMathGrid::metrics(mi, dim);
299 dim.asc += displayMargin();
300 dim.des += displayMargin();
303 if (numberedType()) {
304 FontSetChanger dummy(mi.base, from_ascii("mathbf"));
306 for (row_type row = 0; row < nrows(); ++row)
307 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
313 // make it at least as high as the current font
316 math_font_max_dim(mi.base.font, asc, des);
317 dim.asc = max(dim.asc, asc);
318 dim.des = max(dim.des, des);
319 // Cache the inset dimension.
320 // FIXME: This will overwrite InsetMathGrid dimension, is that OK?
321 setDimCache(mi, dim);
325 void InsetMathHull::draw(PainterInfo & pi, int x, int y) const
327 use_preview_ = previewState(pi.base.bv);
328 Dimension const dim = dimension(*pi.base.bv);
330 // background of mathed under focus is not painted because
331 // selection at the top level of nested inset is difficult to handle.
332 if (!editing(pi.base.bv))
333 pi.pain.fillRectangle(x + 1, y - dim.asc + 1, dim.wid - 2,
334 dim.asc + dim.des - 1, Color_mathbg);
337 // one pixel gap in front
338 preview_->draw(pi, x + 1, y);
339 setPosCache(pi, x, y);
343 FontSetChanger dummy1(pi.base, standardFont());
344 StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
345 InsetMathGrid::draw(pi, x + 1, y);
347 if (numberedType()) {
348 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
349 for (row_type row = 0; row < nrows(); ++row) {
350 int const yy = y + rowinfo_[row].offset_;
351 FontSetChanger dummy(pi.base, from_ascii("mathrm"));
352 docstring const nl = nicelabel(row);
356 setPosCache(pi, x, y);
360 void InsetMathHull::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
363 InsetMathGrid::metricsT(mi, dim);
366 WriteStream wi(os, false, true);
368 dim.wid = os.str().size();
375 void InsetMathHull::drawT(TextPainter & pain, int x, int y) const
378 InsetMathGrid::drawT(pain, x, y);
381 WriteStream wi(os, false, true);
383 pain.draw(x, y, os.str().c_str());
390 docstring const latex_string(InsetMathHull const & inset)
393 WriteStream wi(ls, false, false);
401 void InsetMathHull::addPreview(graphics::PreviewLoader & ploader) const
403 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
404 docstring const snippet = latex_string(*this);
405 preview_->addPreview(snippet, ploader);
410 bool InsetMathHull::notifyCursorLeaves(Cursor & cur)
412 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
413 Buffer const & buffer = cur.buffer();
414 docstring const snippet = latex_string(*this);
415 preview_->addPreview(snippet, buffer);
416 preview_->startLoading(buffer);
417 cur.updateFlags(Update::Force);
423 docstring InsetMathHull::label(row_type row) const
425 BOOST_ASSERT(row < nrows());
430 void InsetMathHull::label(row_type row, docstring const & label)
432 //lyxerr << "setting label '" << label << "' for row " << row << endl;
437 void InsetMathHull::numbered(row_type row, bool num)
445 bool InsetMathHull::numbered(row_type row) const
451 bool InsetMathHull::ams() const
454 type_ == hullAlign ||
455 type_ == hullFlAlign ||
456 type_ == hullMultline ||
457 type_ == hullGather ||
458 type_ == hullAlignAt ||
459 type_ == hullXAlignAt ||
460 type_ == hullXXAlignAt;
464 Inset::DisplayType InsetMathHull::display() const
466 return (type_ != hullSimple && type_ != hullNone) ? AlignCenter : Inline;
470 void InsetMathHull::getLabelList(Buffer const &, vector<docstring> & labels) const
472 for (row_type row = 0; row < nrows(); ++row)
473 if (!label_[row].empty() && nonum_[row] != 1)
474 labels.push_back(label_[row]);
478 bool InsetMathHull::numberedType() const
480 if (type_ == hullNone)
482 if (type_ == hullSimple)
484 if (type_ == hullXXAlignAt)
486 for (row_type row = 0; row < nrows(); ++row)
493 void InsetMathHull::validate(LaTeXFeatures & features) const
496 features.require("amsmath");
499 // Validation is necessary only if not using AMS math.
500 // To be safe, we will always run mathedvalidate.
501 //if (features.amsstyle)
504 features.require("boldsymbol");
505 //features.binom = true;
507 InsetMathGrid::validate(features);
511 void InsetMathHull::header_write(WriteStream & os) const
513 bool n = numberedType();
527 os << "\\begin{equation" << star(n) << "}\n";
537 os << "\\begin{" << hullName(type_) << star(n) << "}\n";
542 os << "\\begin{" << hullName(type_) << star(n) << '}'
543 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
547 os << "\\begin{" << hullName(type_) << '}'
548 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
552 os << "\\begin{unknown" << star(n) << '}';
558 void InsetMathHull::footer_write(WriteStream & os) const
560 bool n = numberedType();
573 os << "\\end{equation" << star(n) << "}\n";
585 os << "\\end{" << hullName(type_) << star(n) << "}\n";
589 os << "\\end{" << hullName(type_) << "}\n";
593 os << "\\end{unknown" << star(n) << '}';
599 bool InsetMathHull::rowChangeOK() const
602 type_ == hullEqnArray || type_ == hullAlign ||
603 type_ == hullFlAlign || type_ == hullAlignAt ||
604 type_ == hullXAlignAt || type_ == hullXXAlignAt ||
605 type_ == hullGather || type_ == hullMultline;
609 bool InsetMathHull::colChangeOK() const
612 type_ == hullAlign || type_ == hullFlAlign ||type_ == hullAlignAt ||
613 type_ == hullXAlignAt || type_ == hullXXAlignAt;
617 void InsetMathHull::addRow(row_type row)
621 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
622 label_.insert(label_.begin() + row + 1, docstring());
623 InsetMathGrid::addRow(row);
627 void InsetMathHull::swapRow(row_type row)
631 if (row + 1 == nrows())
633 swap(nonum_[row], nonum_[row + 1]);
634 swap(label_[row], label_[row + 1]);
635 InsetMathGrid::swapRow(row);
639 void InsetMathHull::delRow(row_type row)
641 if (nrows() <= 1 || !rowChangeOK())
643 InsetMathGrid::delRow(row);
644 // The last dummy row has no number info nor a label.
645 // Test nrows() + 1 because we have already erased the row.
646 if (row == nrows() + 1)
648 nonum_.erase(nonum_.begin() + row);
649 label_.erase(label_.begin() + row);
653 void InsetMathHull::addCol(col_type col)
657 InsetMathGrid::addCol(col);
661 void InsetMathHull::delCol(col_type col)
663 if (ncols() <= 1 || !colChangeOK())
665 InsetMathGrid::delCol(col);
669 docstring InsetMathHull::nicelabel(row_type row) const
673 if (label_[row].empty())
674 return from_ascii("(#)");
675 return '(' + 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) = MathData(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(2);
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) = MathData(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());
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 '" << to_utf8(hullName(type_))
901 << "' to '" << to_utf8(hullName(newtype))
902 << "' not implemented" << endl;
907 docstring 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(MathStream & os) const
938 InsetMathGrid::mathmlize(os);
942 void InsetMathHull::infoize(odocstream & 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(Cursor & cur, FuncRequest & func)
959 idocstringstream iss(func.argument());
960 iss >> dlang >> extra;
962 extra = from_ascii("noextra");
963 std::string const lang = to_ascii(dlang);
965 // FIXME: temporarily disabled
966 //if (cur.selection()) {
969 // lyxerr << "use selection: " << ar << endl;
970 // insert(pipeThroughExtern(lang, extra, ar));
975 eq.push_back(MathAtom(new InsetMathChar('=')));
977 // go to first item in line
978 cur.idx() -= cur.idx() % ncols();
981 if (getType() == hullSimple) {
982 size_type pos = cur.cell().find_last(eq);
984 if (cur.inMathed() && cur.selection()) {
985 asArray(grabAndEraseSelection(cur), ar);
986 } else if (pos == cur.cell().size()) {
988 lyxerr << "use whole cell: " << ar << endl;
990 ar = MathData(cur.cell().begin() + pos + 1, cur.cell().end());
991 lyxerr << "use partial cell form pos: " << pos << endl;
993 cur.cell().append(eq);
994 cur.cell().append(pipeThroughExtern(lang, extra, ar));
995 cur.pos() = cur.lastpos();
999 if (getType() == hullEquation) {
1000 lyxerr << "use equation inset" << endl;
1001 mutate(hullEqnArray);
1002 MathData & ar = cur.cell();
1003 lyxerr << "use cell: " << ar << endl;
1007 cur.cell() = pipeThroughExtern(lang, extra, ar);
1008 // move to end of line
1009 cur.pos() = cur.lastpos();
1014 lyxerr << "use eqnarray" << endl;
1015 cur.idx() += 2 - cur.idx() % ncols();
1017 MathData ar = cur.cell();
1018 lyxerr << "use cell: " << ar << endl;
1019 // FIXME: temporarily disabled
1025 cur.cell() = pipeThroughExtern(lang, extra, ar);
1026 cur.pos() = cur.lastpos();
1031 void InsetMathHull::doDispatch(Cursor & cur, FuncRequest & cmd)
1033 //lyxerr << "action: " << cmd.action << endl;
1034 switch (cmd.action) {
1036 case LFUN_FINISHED_BACKWARD:
1037 case LFUN_FINISHED_FORWARD:
1038 //lyxerr << "action: " << cmd.action << endl;
1039 InsetMathGrid::doDispatch(cur, cmd);
1040 notifyCursorLeaves(cur);
1044 case LFUN_BREAK_PARAGRAPH:
1045 // just swallow this
1048 case LFUN_BREAK_LINE:
1049 // some magic for the common case
1050 if (type_ == hullSimple || type_ == hullEquation) {
1051 cur.recordUndoInset();
1053 cur.bv().buffer().params().use_amsmath == BufferParams::package_on;
1054 mutate(align ? hullAlign : hullEqnArray);
1055 cur.idx() = nrows() * ncols() - 1;
1056 cur.pos() = cur.lastpos();
1058 InsetMathGrid::doDispatch(cur, cmd);
1061 case LFUN_MATH_NUMBER:
1062 //lyxerr << "toggling all numbers" << endl;
1064 cur.recordUndoInset();
1065 bool old = numberedType();
1066 if (type_ == hullMultline)
1067 numbered(nrows() - 1, !old);
1069 for (row_type row = 0; row < nrows(); ++row)
1070 numbered(row, !old);
1072 cur.message(old ? _("No number") : _("Number"));
1076 case LFUN_MATH_NONUMBER:
1078 cur.recordUndoInset();
1079 row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1080 bool old = numbered(r);
1081 cur.message(old ? _("No number") : _("Number"));
1086 case LFUN_LABEL_INSERT: {
1087 cur.recordUndoInset();
1088 row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1089 docstring old_label = label(r);
1090 docstring const default_label = from_ascii(
1091 (lyxrc.label_init_length >= 0) ? "eq:" : "");
1092 if (old_label.empty())
1093 old_label = default_label;
1095 InsetCommandParams p(LABEL_CODE);
1096 p["name"] = cmd.argument().empty() ? old_label : cmd.argument();
1097 std::string const data = InsetCommandMailer::params2string("label", p);
1099 if (cmd.argument().empty())
1100 cur.bv().showDialog("label", data);
1102 FuncRequest fr(LFUN_INSET_INSERT, data);
1108 case LFUN_INSET_INSERT: {
1109 //lyxerr << "arg: " << to_utf8(cmd.argument()) << endl;
1110 std::string const name = cmd.getArg(0);
1111 if (name == "label") {
1112 InsetCommandParams p(LABEL_CODE);
1113 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), p);
1114 docstring str = p["name"];
1115 cur.recordUndoInset();
1116 row_type const r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1117 str = support::trim(str);
1120 docstring old = label(r);
1122 cur.bv().buffer().changeRefsIfUnique(old, str,
1128 InsetMathGrid::doDispatch(cur, cmd);
1132 case LFUN_MATH_EXTERN:
1133 cur.recordUndoInset();
1137 case LFUN_MATH_MUTATE: {
1138 cur.recordUndoInset();
1139 row_type row = cur.row();
1140 col_type col = cur.col();
1141 mutate(hullType(cmd.argument()));
1142 cur.idx() = row * ncols() + col;
1143 if (cur.idx() > cur.lastidx()) {
1144 cur.idx() = cur.lastidx();
1145 cur.pos() = cur.lastpos();
1147 if (cur.pos() > cur.lastpos())
1148 cur.pos() = cur.lastpos();
1149 //cur.dispatched(FINISHED);
1153 case LFUN_MATH_DISPLAY: {
1154 cur.recordUndoInset();
1155 mutate(type_ == hullSimple ? hullEquation : hullSimple);
1157 cur.pos() = cur.lastpos();
1158 //cur.dispatched(FINISHED);
1163 InsetMathGrid::doDispatch(cur, cmd);
1169 bool InsetMathHull::getStatus(Cursor & cur, FuncRequest const & cmd,
1170 FuncStatus & status) const
1172 switch (cmd.action) {
1173 case LFUN_FINISHED_BACKWARD:
1174 case LFUN_FINISHED_FORWARD:
1177 status.enabled(true);
1179 case LFUN_BREAK_LINE:
1180 case LFUN_MATH_NUMBER:
1181 case LFUN_MATH_NONUMBER:
1182 case LFUN_MATH_EXTERN:
1183 case LFUN_MATH_MUTATE:
1184 case LFUN_MATH_DISPLAY:
1186 status.enabled(true);
1188 case LFUN_LABEL_INSERT:
1189 status.enabled(type_ != hullSimple);
1191 case LFUN_INSET_INSERT:
1192 if (cmd.getArg(0) == "label") {
1193 status.enabled(type_ != hullSimple);
1196 return InsetMathGrid::getStatus(cur, cmd, status);
1197 case LFUN_TABULAR_FEATURE: {
1198 istringstream is(to_utf8(cmd.argument()));
1202 && (s == "append-row"
1203 || s == "delete-row"
1204 || s == "copy-row")) {
1205 status.message(bformat(
1206 from_utf8(N_("Can't change number of rows in '%1$s'")),
1208 status.enabled(false);
1212 && (s == "append-column"
1213 || s == "delete-column"
1214 || s == "copy-column")) {
1215 status.message(bformat(
1216 from_utf8(N_("Can't change number of columns in '%1$s'")),
1218 status.enabled(false);
1221 if ((type_ == hullSimple
1222 || type_ == hullEquation
1223 || type_ == hullNone) &&
1224 (s == "add-hline-above" || s == "add-hline-below")) {
1225 status.message(bformat(
1226 from_utf8(N_("Can't add horizontal grid lines in '%1$s'")),
1228 status.enabled(false);
1231 if (s == "add-vline-left" || s == "add-vline-right") {
1232 status.message(bformat(
1233 from_utf8(N_("Can't add vertical grid lines in '%1$s'")),
1235 status.enabled(false);
1238 if (s == "valign-top" || s == "valign-middle"
1239 || s == "valign-bottom" || s == "align-left"
1240 || s == "align-center" || s == "align-right") {
1241 status.enabled(false);
1244 return InsetMathGrid::getStatus(cur, cmd, status);
1247 return InsetMathGrid::getStatus(cur, cmd, status);
1250 // This cannot really happen, but inserted to shut-up gcc
1251 return InsetMathGrid::getStatus(cur, cmd, status);
1255 /////////////////////////////////////////////////////////////////////
1259 // simply scrap this function if you want
1260 void InsetMathHull::mutateToText()
1263 // translate to latex
1265 latex(NULL, os, false, false);
1266 string str = os.str();
1269 Text * lt = view_->cursor().innerText();
1270 string::const_iterator cit = str.begin();
1271 string::const_iterator end = str.end();
1272 for (; cit != end; ++cit)
1273 view_->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
1276 //dispatch(LFUN_ESCAPE);
1281 void InsetMathHull::handleFont(Cursor & cur, docstring const & arg,
1282 docstring const & font)
1284 // this whole function is a hack and won't work for incremental font
1287 if (cur.inset().asInsetMath()->name() == font)
1288 cur.handleFont(to_utf8(font));
1290 cur.handleNest(createInsetMath(font));
1296 void InsetMathHull::handleFont2(Cursor & cur, docstring const & arg)
1301 font.fromString(to_utf8(arg), b);
1302 if (font.fontInfo().color() != Color_inherit) {
1303 MathAtom at = MathAtom(new InsetMathColor(true, font.fontInfo().color()));
1304 cur.handleNest(at, 0);
1309 void InsetMathHull::edit(Cursor & cur, bool front)
1312 front ? idxFirst(cur) : idxLast(cur);
1313 // The inset formula dimension is not necessarily the same as the
1314 // one of the instant preview image, so we have to indicate to the
1315 // BufferView that a metrics update is needed.
1316 cur.updateFlags(Update::Force);
1320 docstring const InsetMathHull::editMessage() const
1322 return _("Math editor mode");
1326 void InsetMathHull::revealCodes(Cursor & cur) const
1328 if (!cur.inMathed())
1330 odocstringstream os;
1332 cur.message(os.str());
1334 // write something to the minibuffer
1335 // translate to latex
1339 string str = os.str();
1341 string::size_type pos = 0;
1343 for (string::iterator it = str.begin(); it != str.end(); ++it) {
1346 else if (*it == '\0') {
1348 pos = it - str.begin();
1354 res = res.substr(pos - 30);
1355 if (res.size() > 60)
1356 res = res.substr(0, 60);
1362 InsetCode InsetMathHull::lyxCode() const
1368 /////////////////////////////////////////////////////////////////////
1372 bool InsetMathHull::searchForward(BufferView * bv, string const & str,
1375 // FIXME: completely broken
1376 static InsetMathHull * lastformula = 0;
1377 static CursorBase current = DocIterator(ibegin(nucleus()));
1379 static string laststr;
1381 if (lastformula != this || laststr != str) {
1382 //lyxerr << "reset lastformula to " << this << endl;
1385 current = ibegin(nucleus());
1387 mathed_parse_cell(ar, str);
1391 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
1393 for (DocIterator it = current; it != iend(nucleus()); increment(it)) {
1394 CursorSlice & top = it.back();
1395 MathData const & a = top.asInsetMath()->cell(top.idx_);
1396 if (a.matchpart(ar, top.pos_)) {
1397 bv->cursor().setSelection(it, ar.size());
1399 top.pos_ += ar.size();
1405 //lyxerr << "not found!" << endl;
1412 void InsetMathHull::write(Buffer const &, std::ostream & os) const
1414 odocstringstream oss;
1415 WriteStream wi(oss, false, false);
1418 os << to_utf8(oss.str());
1422 void InsetMathHull::read(Buffer const &, Lexer & lex)
1425 mathed_parse_normal(at, lex);
1426 operator=(*at->asHullInset());
1430 int InsetMathHull::plaintext(Buffer const &, odocstream & os,
1431 OutputParams const &) const
1433 if (0 && display()) {
1437 TextPainter tpain(dim.width(), dim.height());
1438 drawT(tpain, 0, dim.ascent());
1440 // reset metrics cache to "real" values
1442 return tpain.textheight();
1444 odocstringstream oss;
1445 WriteStream wi(oss, false, true);
1448 docstring const str = oss.str();
1455 int InsetMathHull::docbook(Buffer const & buf, odocstream & os,
1456 OutputParams const & runparams) const
1461 if (getType() == hullSimple)
1462 name = from_ascii("inlineequation");
1464 name = from_ascii("informalequation");
1466 docstring bname = name;
1467 if (!label(0).empty())
1468 bname += " id='" + sgml::cleanID(buf, runparams, label(0)) + "'";
1470 ++ms.tab(); ms.cr(); ms.os() << '<' << bname << '>';
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 << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&"), "<", "<"));
1485 InsetMathGrid::mathmlize(ms);
1488 ms << MTag("alt role='tex'");
1489 res = latex(buf, ls, runparams);
1490 ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&"), "<", "<"));
1494 ms << from_ascii("<graphic fileref=\"eqn/");
1495 if (!label(0).empty())
1496 ms << sgml::cleanID(buf, runparams, label(0));
1498 ms << sgml::uniqueID(from_ascii("anon"));
1500 if (runparams.flavor == OutputParams::XML)
1501 ms << from_ascii("\"/>");
1503 ms << from_ascii("\">");
1505 ms.cr(); --ms.tab(); ms.os() << "</" << name << '>';
1507 return ms.line() + res;
1511 void InsetMathHull::textString(Buffer const & buf, odocstream & os) const
1513 plaintext(buf, os, OutputParams(0));