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"
35 #include "support/debug.h"
36 #include "DispatchResult.h"
37 #include "FuncRequest.h"
38 #include "support/gettext.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/lstrings.h"
58 using namespace lyx::support;
62 using cap::grabAndEraseSelection;
66 int getCols(HullType type)
83 // returns position of first relation operator in the array
84 // used for "intelligent splitting"
85 size_t firstRelOp(MathData const & ar)
87 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
89 return it - ar.begin();
94 char const * star(bool numbered)
96 return numbered ? "" : "*";
100 } // end anon namespace
103 HullType hullType(docstring const & s)
105 if (s == "none") return hullNone;
106 if (s == "simple") return hullSimple;
107 if (s == "equation") return hullEquation;
108 if (s == "eqnarray") return hullEqnArray;
109 if (s == "align") return hullAlign;
110 if (s == "alignat") return hullAlignAt;
111 if (s == "xalignat") return hullXAlignAt;
112 if (s == "xxalignat") return hullXXAlignAt;
113 if (s == "multline") return hullMultline;
114 if (s == "gather") return hullGather;
115 if (s == "flalign") return hullFlAlign;
116 lyxerr << "unknown hull type '" << to_utf8(s) << "'" << endl;
121 docstring hullName(HullType type)
124 case hullNone: return from_ascii("none");
125 case hullSimple: return from_ascii("simple");
126 case hullEquation: return from_ascii("equation");
127 case hullEqnArray: return from_ascii("eqnarray");
128 case hullAlign: return from_ascii("align");
129 case hullAlignAt: return from_ascii("alignat");
130 case hullXAlignAt: return from_ascii("xalignat");
131 case hullXXAlignAt: return from_ascii("xxalignat");
132 case hullMultline: return from_ascii("multline");
133 case hullGather: return from_ascii("gather");
134 case hullFlAlign: return from_ascii("flalign");
136 lyxerr << "unknown hull type '" << type << "'" << endl;
137 return from_ascii("none");
142 InsetMathHull::InsetMathHull()
143 : InsetMathGrid(1, 1), type_(hullNone), nonum_(1), label_(1),
144 preview_(new RenderPreview(this))
146 //lyxerr << "sizeof InsetMath: " << sizeof(InsetMath) << endl;
147 //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl;
148 //lyxerr << "sizeof InsetMathChar: " << sizeof(InsetMathChar) << endl;
149 //lyxerr << "sizeof FontInfo: " << sizeof(FontInfo) << endl;
155 InsetMathHull::InsetMathHull(HullType type)
156 : InsetMathGrid(getCols(type), 1), type_(type), nonum_(1), label_(1),
157 preview_(new RenderPreview(this))
164 InsetMathHull::InsetMathHull(InsetMathHull const & other)
165 : InsetMathGrid(other),
166 type_(other.type_), nonum_(other.nonum_), label_(other.label_),
167 preview_(new RenderPreview(*other.preview_, this))
171 InsetMathHull::~InsetMathHull()
175 Inset * InsetMathHull::clone() const
177 return new InsetMathHull(*this);
181 InsetMathHull & InsetMathHull::operator=(InsetMathHull const & other)
185 *static_cast<InsetMathGrid*>(this) = InsetMathGrid(other);
187 nonum_ = other.nonum_;
188 label_ = other.label_;
189 preview_.reset(new RenderPreview(*other.preview_, this));
195 Inset * InsetMathHull::editXY(Cursor & cur, int x, int y)
201 return InsetMathNest::editXY(cur, x, y);
205 InsetMath::mode_type InsetMathHull::currentMode() const
207 if (type_ == hullNone)
208 return UNDECIDED_MODE;
209 // definitely math mode ...
214 bool InsetMathHull::idxFirst(Cursor & cur) const
222 bool InsetMathHull::idxLast(Cursor & cur) const
224 cur.idx() = nargs() - 1;
225 cur.pos() = cur.lastpos();
230 char InsetMathHull::defaultColAlign(col_type col)
232 if (type_ == hullEqnArray)
234 if (type_ >= hullAlign && type_ != hullGather)
235 return "rl"[col & 1];
240 int InsetMathHull::defaultColSpace(col_type col)
242 if (type_ == hullAlign || type_ == hullAlignAt)
244 if (type_ == hullXAlignAt)
245 return (col & 1) ? 20 : 0;
246 if (type_ == hullXXAlignAt || type_ == hullFlAlign)
247 return (col & 1) ? 40 : 0;
252 docstring InsetMathHull::standardFont() const
254 return from_ascii(type_ == hullNone ? "lyxnochange" : "mathnormal");
258 bool InsetMathHull::previewState(BufferView * bv) const
260 if (!editing(bv) && RenderPreview::status() == LyXRC::PREVIEW_ON) {
261 graphics::PreviewImage const * pimage =
262 preview_->getPreviewImage(bv->buffer());
263 return pimage && pimage->image();
269 void InsetMathHull::metrics(MetricsInfo & mi, Dimension & dim) const
271 if (previewState(mi.base.bv)) {
272 preview_->metrics(mi, dim);
273 // insert a one pixel gap in front of the formula
276 dim.des += displayMargin();
277 // Cache the inset dimension.
278 setDimCache(mi, dim);
282 FontSetChanger dummy1(mi.base, standardFont());
283 StyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
285 // let the cells adjust themselves
286 InsetMathGrid::metrics(mi, dim);
289 dim.asc += displayMargin();
290 dim.des += displayMargin();
293 if (numberedType()) {
294 FontSetChanger dummy(mi.base, from_ascii("mathbf"));
296 for (row_type row = 0; row < nrows(); ++row)
297 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
303 // make it at least as high as the current font
306 math_font_max_dim(mi.base.font, asc, des);
307 dim.asc = max(dim.asc, asc);
308 dim.des = max(dim.des, des);
309 // Cache the inset dimension.
310 // FIXME: This will overwrite InsetMathGrid dimension, is that OK?
311 setDimCache(mi, dim);
315 void InsetMathHull::draw(PainterInfo & pi, int x, int y) const
317 use_preview_ = previewState(pi.base.bv);
318 Dimension const dim = dimension(*pi.base.bv);
320 // background of mathed under focus is not painted because
321 // selection at the top level of nested inset is difficult to handle.
322 if (!editing(pi.base.bv))
323 pi.pain.fillRectangle(x + 1, y - dim.asc + 1, dim.wid - 2,
324 dim.asc + dim.des - 1, Color_mathbg);
327 // one pixel gap in front
328 preview_->draw(pi, x + 1, y);
329 setPosCache(pi, x, y);
333 FontSetChanger dummy1(pi.base, standardFont());
334 StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
335 InsetMathGrid::draw(pi, x + 1, y);
337 if (numberedType()) {
338 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
339 for (row_type row = 0; row < nrows(); ++row) {
340 int const yy = y + rowinfo_[row].offset_;
341 FontSetChanger dummy(pi.base, from_ascii("mathrm"));
342 docstring const nl = nicelabel(row);
346 setPosCache(pi, x, y);
350 void InsetMathHull::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
353 InsetMathGrid::metricsT(mi, dim);
356 WriteStream wi(os, false, true);
358 dim.wid = os.str().size();
365 void InsetMathHull::drawT(TextPainter & pain, int x, int y) const
368 InsetMathGrid::drawT(pain, x, y);
371 WriteStream wi(os, false, true);
373 pain.draw(x, y, os.str().c_str());
380 docstring const latex_string(InsetMathHull const & inset)
383 WriteStream wi(ls, false, false);
391 void InsetMathHull::addPreview(graphics::PreviewLoader & ploader) const
393 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
394 docstring const snippet = latex_string(*this);
395 preview_->addPreview(snippet, ploader);
400 bool InsetMathHull::notifyCursorLeaves(Cursor & cur)
402 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
403 Buffer const & buffer = cur.buffer();
404 docstring const snippet = latex_string(*this);
405 preview_->addPreview(snippet, buffer);
406 preview_->startLoading(buffer);
407 cur.updateFlags(Update::Force);
413 docstring InsetMathHull::label(row_type row) const
415 BOOST_ASSERT(row < nrows());
420 void InsetMathHull::label(row_type row, docstring const & label)
422 //lyxerr << "setting label '" << label << "' for row " << row << endl;
427 void InsetMathHull::numbered(row_type row, bool num)
435 bool InsetMathHull::numbered(row_type row) const
441 bool InsetMathHull::ams() const
444 type_ == hullAlign ||
445 type_ == hullFlAlign ||
446 type_ == hullMultline ||
447 type_ == hullGather ||
448 type_ == hullAlignAt ||
449 type_ == hullXAlignAt ||
450 type_ == hullXXAlignAt;
454 Inset::DisplayType InsetMathHull::display() const
456 return (type_ != hullSimple && type_ != hullNone) ? AlignCenter : Inline;
460 void InsetMathHull::getLabelList(Buffer const &, vector<docstring> & labels) const
462 for (row_type row = 0; row < nrows(); ++row)
463 if (!label_[row].empty() && nonum_[row] != 1)
464 labels.push_back(label_[row]);
468 bool InsetMathHull::numberedType() const
470 if (type_ == hullNone)
472 if (type_ == hullSimple)
474 if (type_ == hullXXAlignAt)
476 for (row_type row = 0; row < nrows(); ++row)
483 void InsetMathHull::validate(LaTeXFeatures & features) const
486 features.require("amsmath");
489 // Validation is necessary only if not using AMS math.
490 // To be safe, we will always run mathedvalidate.
491 //if (features.amsstyle)
494 features.require("boldsymbol");
495 //features.binom = true;
497 InsetMathGrid::validate(features);
501 void InsetMathHull::header_write(WriteStream & os) const
503 bool n = numberedType();
517 os << "\\begin{equation" << star(n) << "}\n";
527 os << "\\begin{" << hullName(type_) << star(n) << "}\n";
532 os << "\\begin{" << hullName(type_) << star(n) << '}'
533 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
537 os << "\\begin{" << hullName(type_) << '}'
538 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
542 os << "\\begin{unknown" << star(n) << '}';
548 void InsetMathHull::footer_write(WriteStream & os) const
550 bool n = numberedType();
563 os << "\\end{equation" << star(n) << "}\n";
575 os << "\\end{" << hullName(type_) << star(n) << "}\n";
579 os << "\\end{" << hullName(type_) << "}\n";
583 os << "\\end{unknown" << star(n) << '}';
589 bool InsetMathHull::rowChangeOK() const
592 type_ == hullEqnArray || type_ == hullAlign ||
593 type_ == hullFlAlign || type_ == hullAlignAt ||
594 type_ == hullXAlignAt || type_ == hullXXAlignAt ||
595 type_ == hullGather || type_ == hullMultline;
599 bool InsetMathHull::colChangeOK() const
602 type_ == hullAlign || type_ == hullFlAlign ||type_ == hullAlignAt ||
603 type_ == hullXAlignAt || type_ == hullXXAlignAt;
607 void InsetMathHull::addRow(row_type row)
611 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
612 label_.insert(label_.begin() + row + 1, docstring());
613 InsetMathGrid::addRow(row);
617 void InsetMathHull::swapRow(row_type row)
621 if (row + 1 == nrows())
623 swap(nonum_[row], nonum_[row + 1]);
624 swap(label_[row], label_[row + 1]);
625 InsetMathGrid::swapRow(row);
629 void InsetMathHull::delRow(row_type row)
631 if (nrows() <= 1 || !rowChangeOK())
633 InsetMathGrid::delRow(row);
634 // The last dummy row has no number info nor a label.
635 // Test nrows() + 1 because we have already erased the row.
636 if (row == nrows() + 1)
638 nonum_.erase(nonum_.begin() + row);
639 label_.erase(label_.begin() + row);
643 void InsetMathHull::addCol(col_type col)
647 InsetMathGrid::addCol(col);
651 void InsetMathHull::delCol(col_type col)
653 if (ncols() <= 1 || !colChangeOK())
655 InsetMathGrid::delCol(col);
659 docstring InsetMathHull::nicelabel(row_type row) const
663 if (label_[row].empty())
664 return from_ascii("(#)");
665 return '(' + label_[row] + ')';
669 void InsetMathHull::glueall()
672 for (idx_type i = 0; i < nargs(); ++i)
674 *this = InsetMathHull(hullSimple);
680 void InsetMathHull::splitTo2Cols()
682 BOOST_ASSERT(ncols() == 1);
683 InsetMathGrid::addCol(1);
684 for (row_type row = 0; row < nrows(); ++row) {
685 idx_type const i = 2 * row;
686 pos_type pos = firstRelOp(cell(i));
687 cell(i + 1) = MathData(cell(i).begin() + pos, cell(i).end());
688 cell(i).erase(pos, cell(i).size());
693 void InsetMathHull::splitTo3Cols()
695 BOOST_ASSERT(ncols() < 3);
698 InsetMathGrid::addCol(2);
699 for (row_type row = 0; row < nrows(); ++row) {
700 idx_type const i = 3 * row + 1;
701 if (cell(i).size()) {
702 cell(i + 1) = MathData(cell(i).begin() + 1, cell(i).end());
703 cell(i).erase(1, cell(i).size());
709 void InsetMathHull::changeCols(col_type cols)
713 else if (ncols() < cols) {
719 while (ncols() < cols)
720 InsetMathGrid::addCol(ncols());
726 for (row_type row = 0; row < nrows(); ++row) {
727 idx_type const i = row * ncols();
728 for (col_type col = cols; col < ncols(); ++col) {
729 cell(i + cols - 1).append(cell(i + col));
733 while (ncols() > cols) {
734 InsetMathGrid::delCol(ncols() - 1);
739 HullType InsetMathHull::getType() const
745 void InsetMathHull::setType(HullType type)
752 void InsetMathHull::mutate(HullType newtype)
754 //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
756 // we try to move along the chain
757 // none <-> simple <-> equation <-> eqnarray -> *align* -> multline, gather -+
759 // +-------------------------------------+
760 // we use eqnarray as intermediate type for mutations that are not
761 // directly supported because it handles labels and numbering for
764 if (newtype == type_) {
768 else if (newtype < hullNone) {
773 else if (type_ == hullNone) {
779 else if (type_ == hullSimple) {
780 if (newtype == hullNone) {
784 setType(hullEquation);
790 else if (type_ == hullEquation) {
791 if (newtype < type_) {
795 } else if (newtype == hullEqnArray) {
796 // split it "nicely" on the first relop
798 setType(hullEqnArray);
799 } else if (newtype == hullMultline || newtype == hullGather) {
809 else if (type_ == hullEqnArray) {
810 if (newtype < type_) {
811 // set correct (no)numbering
812 bool allnonum = true;
813 for (row_type row = 0; row < nrows(); ++row)
817 // set first non-empty label
819 for (row_type row = 0; row < nrows(); ++row) {
820 if (!label_[row].empty()) {
827 nonum_[0] = allnonum;
830 } else { // align & Co.
837 else if (type_ == hullAlign || type_ == hullAlignAt ||
838 type_ == hullXAlignAt || type_ == hullFlAlign) {
839 if (newtype < hullAlign) {
841 setType(hullEqnArray);
843 } else if (newtype == hullGather || newtype == hullMultline) {
846 } else if (newtype == hullXXAlignAt) {
847 for (row_type row = 0; row < nrows(); ++row)
848 numbered(row, false);
855 else if (type_ == hullXXAlignAt) {
856 for (row_type row = 0; row < nrows(); ++row)
857 numbered(row, false);
858 if (newtype < hullAlign) {
860 setType(hullEqnArray);
862 } else if (newtype == hullGather || newtype == hullMultline) {
870 else if (type_ == hullMultline || type_ == hullGather) {
871 if (newtype == hullGather || newtype == hullMultline)
873 else if (newtype == hullAlign || newtype == hullFlAlign ||
874 newtype == hullAlignAt || newtype == hullXAlignAt) {
877 } else if (newtype == hullXXAlignAt) {
879 for (row_type row = 0; row < nrows(); ++row)
880 numbered(row, false);
884 setType(hullEqnArray);
890 lyxerr << "mutation from '" << to_utf8(hullName(type_))
891 << "' to '" << to_utf8(hullName(newtype))
892 << "' not implemented" << endl;
897 docstring InsetMathHull::eolString(row_type row, bool emptyline, bool fragile) const
900 if (numberedType()) {
901 if (!label_[row].empty() && !nonum_[row])
902 res += "\\label{" + label_[row] + '}';
903 if (nonum_[row] && (type_ != hullMultline))
904 res += "\\nonumber ";
906 return res + InsetMathGrid::eolString(row, emptyline, fragile);
910 void InsetMathHull::write(WriteStream & os) const
913 InsetMathGrid::write(os);
918 void InsetMathHull::normalize(NormalStream & os) const
920 os << "[formula " << hullName(type_) << ' ';
921 InsetMathGrid::normalize(os);
926 void InsetMathHull::mathmlize(MathStream & os) const
928 InsetMathGrid::mathmlize(os);
932 void InsetMathHull::infoize(odocstream & os) const
934 os << "Type: " << hullName(type_);
938 void InsetMathHull::check() const
940 BOOST_ASSERT(nonum_.size() == nrows());
941 BOOST_ASSERT(label_.size() == nrows());
945 void InsetMathHull::doExtern(Cursor & cur, FuncRequest & func)
949 idocstringstream iss(func.argument());
950 iss >> dlang >> extra;
952 extra = from_ascii("noextra");
953 string const lang = to_ascii(dlang);
955 // FIXME: temporarily disabled
956 //if (cur.selection()) {
959 // lyxerr << "use selection: " << ar << endl;
960 // insert(pipeThroughExtern(lang, extra, ar));
965 eq.push_back(MathAtom(new InsetMathChar('=')));
967 // go to first item in line
968 cur.idx() -= cur.idx() % ncols();
971 if (getType() == hullSimple) {
972 size_type pos = cur.cell().find_last(eq);
974 if (cur.inMathed() && cur.selection()) {
975 asArray(grabAndEraseSelection(cur), ar);
976 } else if (pos == cur.cell().size()) {
978 lyxerr << "use whole cell: " << ar << endl;
980 ar = MathData(cur.cell().begin() + pos + 1, cur.cell().end());
981 lyxerr << "use partial cell form pos: " << pos << endl;
983 cur.cell().append(eq);
984 cur.cell().append(pipeThroughExtern(lang, extra, ar));
985 cur.pos() = cur.lastpos();
989 if (getType() == hullEquation) {
990 lyxerr << "use equation inset" << endl;
991 mutate(hullEqnArray);
992 MathData & ar = cur.cell();
993 lyxerr << "use cell: " << ar << endl;
997 cur.cell() = pipeThroughExtern(lang, extra, ar);
998 // move to end of line
999 cur.pos() = cur.lastpos();
1004 lyxerr << "use eqnarray" << endl;
1005 cur.idx() += 2 - cur.idx() % ncols();
1007 MathData ar = cur.cell();
1008 lyxerr << "use cell: " << ar << endl;
1009 // FIXME: temporarily disabled
1015 cur.cell() = pipeThroughExtern(lang, extra, ar);
1016 cur.pos() = cur.lastpos();
1021 void InsetMathHull::doDispatch(Cursor & cur, FuncRequest & cmd)
1023 //lyxerr << "action: " << cmd.action << endl;
1024 switch (cmd.action) {
1026 case LFUN_FINISHED_BACKWARD:
1027 case LFUN_FINISHED_FORWARD:
1028 //lyxerr << "action: " << cmd.action << endl;
1029 InsetMathGrid::doDispatch(cur, cmd);
1030 notifyCursorLeaves(cur);
1034 case LFUN_BREAK_PARAGRAPH:
1035 // just swallow this
1039 // some magic for the common case
1040 if (type_ == hullSimple || type_ == hullEquation) {
1041 cur.recordUndoInset();
1043 cur.bv().buffer().params().use_amsmath == BufferParams::package_on;
1044 mutate(align ? hullAlign : hullEqnArray);
1045 cur.idx() = nrows() * ncols() - 1;
1046 cur.pos() = cur.lastpos();
1048 InsetMathGrid::doDispatch(cur, cmd);
1051 case LFUN_MATH_NUMBER:
1052 //lyxerr << "toggling all numbers" << endl;
1054 cur.recordUndoInset();
1055 bool old = numberedType();
1056 if (type_ == hullMultline)
1057 numbered(nrows() - 1, !old);
1059 for (row_type row = 0; row < nrows(); ++row)
1060 numbered(row, !old);
1062 cur.message(old ? _("No number") : _("Number"));
1066 case LFUN_MATH_NONUMBER:
1068 cur.recordUndoInset();
1069 row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1070 bool old = numbered(r);
1071 cur.message(old ? _("No number") : _("Number"));
1076 case LFUN_LABEL_INSERT: {
1077 cur.recordUndoInset();
1078 row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1079 docstring old_label = label(r);
1080 docstring const default_label = from_ascii(
1081 (lyxrc.label_init_length >= 0) ? "eq:" : "");
1082 if (old_label.empty())
1083 old_label = default_label;
1085 InsetCommandParams p(LABEL_CODE);
1086 p["name"] = cmd.argument().empty() ? old_label : cmd.argument();
1087 string const data = InsetCommandMailer::params2string("label", p);
1089 if (cmd.argument().empty())
1090 cur.bv().showDialog("label", data);
1092 FuncRequest fr(LFUN_INSET_INSERT, data);
1098 case LFUN_INSET_INSERT: {
1099 //lyxerr << "arg: " << to_utf8(cmd.argument()) << endl;
1100 string const name = cmd.getArg(0);
1101 if (name == "label") {
1102 InsetCommandParams p(LABEL_CODE);
1103 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), p);
1104 docstring str = p["name"];
1105 cur.recordUndoInset();
1106 row_type const r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1110 docstring old = label(r);
1112 cur.bv().buffer().changeRefsIfUnique(old, str,
1118 InsetMathGrid::doDispatch(cur, cmd);
1122 case LFUN_MATH_EXTERN:
1123 cur.recordUndoInset();
1127 case LFUN_MATH_MUTATE: {
1128 cur.recordUndoInset();
1129 row_type row = cur.row();
1130 col_type col = cur.col();
1131 mutate(hullType(cmd.argument()));
1132 cur.idx() = row * ncols() + col;
1133 if (cur.idx() > cur.lastidx()) {
1134 cur.idx() = cur.lastidx();
1135 cur.pos() = cur.lastpos();
1137 if (cur.pos() > cur.lastpos())
1138 cur.pos() = cur.lastpos();
1139 //cur.dispatched(FINISHED);
1143 case LFUN_MATH_DISPLAY: {
1144 cur.recordUndoInset();
1145 mutate(type_ == hullSimple ? hullEquation : hullSimple);
1147 cur.pos() = cur.lastpos();
1148 //cur.dispatched(FINISHED);
1153 InsetMathGrid::doDispatch(cur, cmd);
1159 bool InsetMathHull::getStatus(Cursor & cur, FuncRequest const & cmd,
1160 FuncStatus & status) const
1162 switch (cmd.action) {
1163 case LFUN_FINISHED_BACKWARD:
1164 case LFUN_FINISHED_FORWARD:
1167 status.enabled(true);
1170 case LFUN_MATH_NUMBER:
1171 case LFUN_MATH_NONUMBER:
1172 case LFUN_MATH_EXTERN:
1173 case LFUN_MATH_MUTATE:
1174 case LFUN_MATH_DISPLAY:
1176 status.enabled(true);
1178 case LFUN_LABEL_INSERT:
1179 status.enabled(type_ != hullSimple);
1181 case LFUN_INSET_INSERT:
1182 if (cmd.getArg(0) == "label") {
1183 status.enabled(type_ != hullSimple);
1186 return InsetMathGrid::getStatus(cur, cmd, status);
1187 case LFUN_TABULAR_FEATURE: {
1188 istringstream is(to_utf8(cmd.argument()));
1192 && (s == "append-row"
1193 || s == "delete-row"
1194 || s == "copy-row")) {
1195 status.message(bformat(
1196 from_utf8(N_("Can't change number of rows in '%1$s'")),
1198 status.enabled(false);
1202 && (s == "append-column"
1203 || s == "delete-column"
1204 || s == "copy-column")) {
1205 status.message(bformat(
1206 from_utf8(N_("Can't change number of columns in '%1$s'")),
1208 status.enabled(false);
1211 if ((type_ == hullSimple
1212 || type_ == hullEquation
1213 || type_ == hullNone) &&
1214 (s == "add-hline-above" || s == "add-hline-below")) {
1215 status.message(bformat(
1216 from_utf8(N_("Can't add horizontal grid lines in '%1$s'")),
1218 status.enabled(false);
1221 if (s == "add-vline-left" || s == "add-vline-right") {
1222 status.message(bformat(
1223 from_utf8(N_("Can't add vertical grid lines in '%1$s'")),
1225 status.enabled(false);
1228 if (s == "valign-top" || s == "valign-middle"
1229 || s == "valign-bottom" || s == "align-left"
1230 || s == "align-center" || s == "align-right") {
1231 status.enabled(false);
1234 return InsetMathGrid::getStatus(cur, cmd, status);
1237 return InsetMathGrid::getStatus(cur, cmd, status);
1240 // This cannot really happen, but inserted to shut-up gcc
1241 return InsetMathGrid::getStatus(cur, cmd, status);
1245 /////////////////////////////////////////////////////////////////////
1249 // simply scrap this function if you want
1250 void InsetMathHull::mutateToText()
1253 // translate to latex
1255 latex(NULL, os, false, false);
1256 string str = os.str();
1259 Text * lt = view_->cursor().innerText();
1260 string::const_iterator cit = str.begin();
1261 string::const_iterator end = str.end();
1262 for (; cit != end; ++cit)
1263 view_->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
1266 //dispatch(LFUN_ESCAPE);
1271 void InsetMathHull::handleFont(Cursor & cur, docstring const & arg,
1272 docstring const & font)
1274 // this whole function is a hack and won't work for incremental font
1277 if (cur.inset().asInsetMath()->name() == font)
1278 cur.handleFont(to_utf8(font));
1280 cur.handleNest(createInsetMath(font));
1286 void InsetMathHull::handleFont2(Cursor & cur, docstring const & arg)
1291 font.fromString(to_utf8(arg), b);
1292 if (font.fontInfo().color() != Color_inherit) {
1293 MathAtom at = MathAtom(new InsetMathColor(true, font.fontInfo().color()));
1294 cur.handleNest(at, 0);
1299 void InsetMathHull::edit(Cursor & cur, bool front)
1302 front ? idxFirst(cur) : idxLast(cur);
1303 // The inset formula dimension is not necessarily the same as the
1304 // one of the instant preview image, so we have to indicate to the
1305 // BufferView that a metrics update is needed.
1306 cur.updateFlags(Update::Force);
1310 docstring const InsetMathHull::editMessage() const
1312 return _("Math editor mode");
1316 void InsetMathHull::revealCodes(Cursor & cur) const
1318 if (!cur.inMathed())
1320 odocstringstream os;
1322 cur.message(os.str());
1324 // write something to the minibuffer
1325 // translate to latex
1329 string str = os.str();
1331 string::size_type pos = 0;
1333 for (string::iterator it = str.begin(); it != str.end(); ++it) {
1336 else if (*it == '\0') {
1338 pos = it - str.begin();
1344 res = res.substr(pos - 30);
1345 if (res.size() > 60)
1346 res = res.substr(0, 60);
1352 InsetCode InsetMathHull::lyxCode() const
1358 /////////////////////////////////////////////////////////////////////
1362 bool InsetMathHull::searchForward(BufferView * bv, string const & str,
1365 // FIXME: completely broken
1366 static InsetMathHull * lastformula = 0;
1367 static CursorBase current = DocIterator(ibegin(nucleus()));
1369 static string laststr;
1371 if (lastformula != this || laststr != str) {
1372 //lyxerr << "reset lastformula to " << this << endl;
1375 current = ibegin(nucleus());
1377 mathed_parse_cell(ar, str);
1381 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
1383 for (DocIterator it = current; it != iend(nucleus()); increment(it)) {
1384 CursorSlice & top = it.back();
1385 MathData const & a = top.asInsetMath()->cell(top.idx_);
1386 if (a.matchpart(ar, top.pos_)) {
1387 bv->cursor().setSelection(it, ar.size());
1389 top.pos_ += ar.size();
1395 //lyxerr << "not found!" << endl;
1402 void InsetMathHull::write(Buffer const &, ostream & os) const
1404 odocstringstream oss;
1405 WriteStream wi(oss, false, false);
1408 os << to_utf8(oss.str());
1412 void InsetMathHull::read(Buffer const &, Lexer & lex)
1415 mathed_parse_normal(at, lex);
1416 operator=(*at->asHullInset());
1420 int InsetMathHull::plaintext(Buffer const &, odocstream & os,
1421 OutputParams const &) const
1423 if (0 && display()) {
1427 TextPainter tpain(dim.width(), dim.height());
1428 drawT(tpain, 0, dim.ascent());
1430 // reset metrics cache to "real" values
1432 return tpain.textheight();
1434 odocstringstream oss;
1435 WriteStream wi(oss, false, true);
1438 docstring const str = oss.str();
1445 int InsetMathHull::docbook(Buffer const & buf, odocstream & os,
1446 OutputParams const & runparams) const
1451 if (getType() == hullSimple)
1452 name = from_ascii("inlineequation");
1454 name = from_ascii("informalequation");
1456 docstring bname = name;
1457 if (!label(0).empty())
1458 bname += " id='" + sgml::cleanID(buf, runparams, label(0)) + "'";
1460 ++ms.tab(); ms.cr(); ms.os() << '<' << bname << '>';
1462 odocstringstream ls;
1463 if (runparams.flavor == OutputParams::XML) {
1464 ms << MTag("alt role='tex' ");
1465 // Workaround for db2latex: db2latex always includes equations with
1466 // \ensuremath{} or \begin{display}\end{display}
1467 // so we strip LyX' math environment
1468 WriteStream wi(ls, false, false);
1469 InsetMathGrid::write(wi);
1470 ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&"), "<", "<"));
1475 InsetMathGrid::mathmlize(ms);
1478 ms << MTag("alt role='tex'");
1479 res = latex(buf, ls, runparams);
1480 ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&"), "<", "<"));
1484 ms << from_ascii("<graphic fileref=\"eqn/");
1485 if (!label(0).empty())
1486 ms << sgml::cleanID(buf, runparams, label(0));
1488 ms << sgml::uniqueID(from_ascii("anon"));
1490 if (runparams.flavor == OutputParams::XML)
1491 ms << from_ascii("\"/>");
1493 ms << from_ascii("\">");
1495 ms.cr(); --ms.tab(); ms.os() << "</" << name << '>';
1497 return ms.line() + res;
1501 void InsetMathHull::textString(Buffer const & buf, odocstream & os) const
1503 plaintext(buf, os, OutputParams(0));