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/lyxlib.h"
54 #include "support/lstrings.h"
59 using namespace lyx::support;
63 using cap::grabAndEraseSelection;
67 int getCols(HullType type)
84 // returns position of first relation operator in the array
85 // used for "intelligent splitting"
86 size_t firstRelOp(MathData const & ar)
88 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
90 return it - ar.begin();
95 char const * star(bool numbered)
97 return numbered ? "" : "*";
101 } // end anon namespace
104 HullType hullType(docstring const & s)
106 if (s == "none") return hullNone;
107 if (s == "simple") return hullSimple;
108 if (s == "equation") return hullEquation;
109 if (s == "eqnarray") return hullEqnArray;
110 if (s == "align") return hullAlign;
111 if (s == "alignat") return hullAlignAt;
112 if (s == "xalignat") return hullXAlignAt;
113 if (s == "xxalignat") return hullXXAlignAt;
114 if (s == "multline") return hullMultline;
115 if (s == "gather") return hullGather;
116 if (s == "flalign") return hullFlAlign;
117 lyxerr << "unknown hull type '" << to_utf8(s) << "'" << endl;
122 docstring hullName(HullType type)
125 case hullNone: return from_ascii("none");
126 case hullSimple: return from_ascii("simple");
127 case hullEquation: return from_ascii("equation");
128 case hullEqnArray: return from_ascii("eqnarray");
129 case hullAlign: return from_ascii("align");
130 case hullAlignAt: return from_ascii("alignat");
131 case hullXAlignAt: return from_ascii("xalignat");
132 case hullXXAlignAt: return from_ascii("xxalignat");
133 case hullMultline: return from_ascii("multline");
134 case hullGather: return from_ascii("gather");
135 case hullFlAlign: return from_ascii("flalign");
137 lyxerr << "unknown hull type '" << type << "'" << endl;
138 return from_ascii("none");
143 InsetMathHull::InsetMathHull()
144 : InsetMathGrid(1, 1), type_(hullNone), nonum_(1), label_(1),
145 preview_(new RenderPreview(this))
147 //lyxerr << "sizeof InsetMath: " << sizeof(InsetMath) << endl;
148 //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl;
149 //lyxerr << "sizeof InsetMathChar: " << sizeof(InsetMathChar) << endl;
150 //lyxerr << "sizeof FontInfo: " << sizeof(FontInfo) << endl;
156 InsetMathHull::InsetMathHull(HullType type)
157 : InsetMathGrid(getCols(type), 1), type_(type), nonum_(1), label_(1),
158 preview_(new RenderPreview(this))
165 InsetMathHull::InsetMathHull(InsetMathHull const & other)
166 : InsetMathGrid(other),
167 type_(other.type_), nonum_(other.nonum_), label_(other.label_),
168 preview_(new RenderPreview(*other.preview_, this))
172 InsetMathHull::~InsetMathHull()
176 Inset * InsetMathHull::clone() const
178 return new InsetMathHull(*this);
182 InsetMathHull & InsetMathHull::operator=(InsetMathHull const & other)
186 *static_cast<InsetMathGrid*>(this) = InsetMathGrid(other);
188 nonum_ = other.nonum_;
189 label_ = other.label_;
190 preview_.reset(new RenderPreview(*other.preview_, this));
196 Inset * InsetMathHull::editXY(Cursor & cur, int x, int y)
202 return InsetMathNest::editXY(cur, x, y);
206 InsetMath::mode_type InsetMathHull::currentMode() const
208 if (type_ == hullNone)
209 return UNDECIDED_MODE;
210 // definitely math mode ...
215 bool InsetMathHull::idxFirst(Cursor & cur) const
223 bool InsetMathHull::idxLast(Cursor & cur) const
225 cur.idx() = nargs() - 1;
226 cur.pos() = cur.lastpos();
231 char InsetMathHull::defaultColAlign(col_type col)
233 if (type_ == hullEqnArray)
235 if (type_ >= hullAlign)
236 return "rl"[col & 1];
241 int InsetMathHull::defaultColSpace(col_type col)
243 if (type_ == hullAlign || type_ == hullAlignAt)
245 if (type_ == hullXAlignAt)
246 return (col & 1) ? 20 : 0;
247 if (type_ == hullXXAlignAt || type_ == hullFlAlign)
248 return (col & 1) ? 40 : 0;
253 docstring InsetMathHull::standardFont() const
255 return from_ascii(type_ == hullNone ? "lyxnochange" : "mathnormal");
259 bool InsetMathHull::previewState(BufferView * bv) const
261 if (!editing(bv) && RenderPreview::status() == LyXRC::PREVIEW_ON) {
262 graphics::PreviewImage const * pimage =
263 preview_->getPreviewImage(bv->buffer());
264 return pimage && pimage->image();
270 void InsetMathHull::metrics(MetricsInfo & mi, Dimension & dim) const
272 if (previewState(mi.base.bv)) {
273 preview_->metrics(mi, dim);
274 // insert a one pixel gap in front of the formula
277 dim.des += displayMargin();
278 // Cache the inset dimension.
279 setDimCache(mi, dim);
283 FontSetChanger dummy1(mi.base, standardFont());
284 StyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
286 // let the cells adjust themselves
287 InsetMathGrid::metrics(mi, dim);
290 dim.asc += displayMargin();
291 dim.des += displayMargin();
294 if (numberedType()) {
295 FontSetChanger dummy(mi.base, from_ascii("mathbf"));
297 for (row_type row = 0; row < nrows(); ++row)
298 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
304 // make it at least as high as the current font
307 math_font_max_dim(mi.base.font, asc, des);
308 dim.asc = max(dim.asc, asc);
309 dim.des = max(dim.des, des);
310 // Cache the inset dimension.
311 // FIXME: This will overwrite InsetMathGrid dimension, is that OK?
312 setDimCache(mi, dim);
316 void InsetMathHull::draw(PainterInfo & pi, int x, int y) const
318 use_preview_ = previewState(pi.base.bv);
319 Dimension const dim = dimension(*pi.base.bv);
321 // background of mathed under focus is not painted because
322 // selection at the top level of nested inset is difficult to handle.
323 if (!editing(pi.base.bv))
324 pi.pain.fillRectangle(x + 1, y - dim.asc + 1, dim.wid - 2,
325 dim.asc + dim.des - 1, Color_mathbg);
328 // one pixel gap in front
329 preview_->draw(pi, x + 1, y);
330 setPosCache(pi, x, y);
334 FontSetChanger dummy1(pi.base, standardFont());
335 StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
336 InsetMathGrid::draw(pi, x + 1, y);
338 if (numberedType()) {
339 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
340 for (row_type row = 0; row < nrows(); ++row) {
341 int const yy = y + rowinfo_[row].offset_;
342 FontSetChanger dummy(pi.base, from_ascii("mathrm"));
343 docstring const nl = nicelabel(row);
347 setPosCache(pi, x, y);
351 void InsetMathHull::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
354 InsetMathGrid::metricsT(mi, dim);
357 WriteStream wi(os, false, true);
359 dim.wid = os.str().size();
366 void InsetMathHull::drawT(TextPainter & pain, int x, int y) const
369 InsetMathGrid::drawT(pain, x, y);
372 WriteStream wi(os, false, true);
374 pain.draw(x, y, os.str().c_str());
381 docstring const latex_string(InsetMathHull const & inset)
384 WriteStream wi(ls, false, false);
392 void InsetMathHull::addPreview(graphics::PreviewLoader & ploader) const
394 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
395 docstring const snippet = latex_string(*this);
396 preview_->addPreview(snippet, ploader);
401 bool InsetMathHull::notifyCursorLeaves(Cursor & cur)
403 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
404 Buffer const & buffer = cur.buffer();
405 docstring const snippet = latex_string(*this);
406 preview_->addPreview(snippet, buffer);
407 preview_->startLoading(buffer);
408 cur.updateFlags(Update::Force);
414 docstring InsetMathHull::label(row_type row) const
416 BOOST_ASSERT(row < nrows());
421 void InsetMathHull::label(row_type row, docstring const & label)
423 //lyxerr << "setting label '" << label << "' for row " << row << endl;
428 void InsetMathHull::numbered(row_type row, bool num)
436 bool InsetMathHull::numbered(row_type row) const
442 bool InsetMathHull::ams() const
445 type_ == hullAlign ||
446 type_ == hullFlAlign ||
447 type_ == hullMultline ||
448 type_ == hullGather ||
449 type_ == hullAlignAt ||
450 type_ == hullXAlignAt ||
451 type_ == hullXXAlignAt;
455 Inset::DisplayType InsetMathHull::display() const
457 return (type_ != hullSimple && type_ != hullNone) ? AlignCenter : Inline;
461 void InsetMathHull::getLabelList(Buffer const &, vector<docstring> & labels) const
463 for (row_type row = 0; row < nrows(); ++row)
464 if (!label_[row].empty() && nonum_[row] != 1)
465 labels.push_back(label_[row]);
469 bool InsetMathHull::numberedType() const
471 if (type_ == hullNone)
473 if (type_ == hullSimple)
475 if (type_ == hullXXAlignAt)
477 for (row_type row = 0; row < nrows(); ++row)
484 void InsetMathHull::validate(LaTeXFeatures & features) const
487 features.require("amsmath");
490 // Validation is necessary only if not using AMS math.
491 // To be safe, we will always run mathedvalidate.
492 //if (features.amsstyle)
495 features.require("boldsymbol");
496 //features.binom = true;
498 InsetMathGrid::validate(features);
502 void InsetMathHull::header_write(WriteStream & os) const
504 bool n = numberedType();
518 os << "\\begin{equation" << star(n) << "}\n";
528 os << "\\begin{" << hullName(type_) << star(n) << "}\n";
533 os << "\\begin{" << hullName(type_) << star(n) << '}'
534 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
538 os << "\\begin{" << hullName(type_) << '}'
539 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
543 os << "\\begin{unknown" << star(n) << '}';
549 void InsetMathHull::footer_write(WriteStream & os) const
551 bool n = numberedType();
564 os << "\\end{equation" << star(n) << "}\n";
576 os << "\\end{" << hullName(type_) << star(n) << "}\n";
580 os << "\\end{" << hullName(type_) << "}\n";
584 os << "\\end{unknown" << star(n) << '}';
590 bool InsetMathHull::rowChangeOK() const
593 type_ == hullEqnArray || type_ == hullAlign ||
594 type_ == hullFlAlign || type_ == hullAlignAt ||
595 type_ == hullXAlignAt || type_ == hullXXAlignAt ||
596 type_ == hullGather || type_ == hullMultline;
600 bool InsetMathHull::colChangeOK() const
603 type_ == hullAlign || type_ == hullFlAlign ||type_ == hullAlignAt ||
604 type_ == hullXAlignAt || type_ == hullXXAlignAt;
608 void InsetMathHull::addRow(row_type row)
612 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
613 label_.insert(label_.begin() + row + 1, docstring());
614 InsetMathGrid::addRow(row);
618 void InsetMathHull::swapRow(row_type row)
622 if (row + 1 == nrows())
624 swap(nonum_[row], nonum_[row + 1]);
625 swap(label_[row], label_[row + 1]);
626 InsetMathGrid::swapRow(row);
630 void InsetMathHull::delRow(row_type row)
632 if (nrows() <= 1 || !rowChangeOK())
634 InsetMathGrid::delRow(row);
635 // The last dummy row has no number info nor a label.
636 // Test nrows() + 1 because we have already erased the row.
637 if (row == nrows() + 1)
639 nonum_.erase(nonum_.begin() + row);
640 label_.erase(label_.begin() + row);
644 void InsetMathHull::addCol(col_type col)
648 InsetMathGrid::addCol(col);
652 void InsetMathHull::delCol(col_type col)
654 if (ncols() <= 1 || !colChangeOK())
656 InsetMathGrid::delCol(col);
660 docstring InsetMathHull::nicelabel(row_type row) const
664 if (label_[row].empty())
665 return from_ascii("(#)");
666 return '(' + label_[row] + ')';
670 void InsetMathHull::glueall()
673 for (idx_type i = 0; i < nargs(); ++i)
675 *this = InsetMathHull(hullSimple);
681 void InsetMathHull::splitTo2Cols()
683 BOOST_ASSERT(ncols() == 1);
684 InsetMathGrid::addCol(1);
685 for (row_type row = 0; row < nrows(); ++row) {
686 idx_type const i = 2 * row;
687 pos_type pos = firstRelOp(cell(i));
688 cell(i + 1) = MathData(cell(i).begin() + pos, cell(i).end());
689 cell(i).erase(pos, cell(i).size());
694 void InsetMathHull::splitTo3Cols()
696 BOOST_ASSERT(ncols() < 3);
699 InsetMathGrid::addCol(2);
700 for (row_type row = 0; row < nrows(); ++row) {
701 idx_type const i = 3 * row + 1;
702 if (cell(i).size()) {
703 cell(i + 1) = MathData(cell(i).begin() + 1, cell(i).end());
704 cell(i).erase(1, cell(i).size());
710 void InsetMathHull::changeCols(col_type cols)
714 else if (ncols() < cols) {
720 while (ncols() < cols)
721 InsetMathGrid::addCol(ncols());
727 for (row_type row = 0; row < nrows(); ++row) {
728 idx_type const i = row * ncols();
729 for (col_type col = cols; col < ncols(); ++col) {
730 cell(i + cols - 1).append(cell(i + col));
734 while (ncols() > cols) {
735 InsetMathGrid::delCol(ncols() - 1);
740 HullType InsetMathHull::getType() const
746 void InsetMathHull::setType(HullType type)
753 void InsetMathHull::mutate(HullType newtype)
755 //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
757 // we try to move along the chain
758 // none <-> simple <-> equation <-> eqnarray -> *align* -> multline, gather -+
760 // +-------------------------------------+
761 // we use eqnarray as intermediate type for mutations that are not
762 // directly supported because it handles labels and numbering for
765 if (newtype == type_) {
769 else if (newtype < hullNone) {
774 else if (type_ == hullNone) {
780 else if (type_ == hullSimple) {
781 if (newtype == hullNone) {
785 setType(hullEquation);
791 else if (type_ == hullEquation) {
792 if (newtype < type_) {
796 } else if (newtype == hullEqnArray) {
797 // split it "nicely" on the first relop
799 setType(hullEqnArray);
800 } else if (newtype == hullMultline || newtype == hullGather) {
810 else if (type_ == hullEqnArray) {
811 if (newtype < type_) {
812 // set correct (no)numbering
813 bool allnonum = true;
814 for (row_type row = 0; row < nrows(); ++row)
818 // set first non-empty label
820 for (row_type row = 0; row < nrows(); ++row) {
821 if (!label_[row].empty()) {
828 nonum_[0] = allnonum;
831 } else { // align & Co.
838 else if (type_ == hullAlign || type_ == hullAlignAt ||
839 type_ == hullXAlignAt || type_ == hullFlAlign) {
840 if (newtype < hullAlign) {
842 setType(hullEqnArray);
844 } else if (newtype == hullGather || newtype == hullMultline) {
847 } else if (newtype == hullXXAlignAt) {
848 for (row_type row = 0; row < nrows(); ++row)
849 numbered(row, false);
856 else if (type_ == hullXXAlignAt) {
857 for (row_type row = 0; row < nrows(); ++row)
858 numbered(row, false);
859 if (newtype < hullAlign) {
861 setType(hullEqnArray);
863 } else if (newtype == hullGather || newtype == hullMultline) {
871 else if (type_ == hullMultline || type_ == hullGather) {
872 if (newtype == hullGather || newtype == hullMultline)
874 else if (newtype == hullAlign || newtype == hullFlAlign ||
875 newtype == hullAlignAt || newtype == hullXAlignAt) {
878 } else if (newtype == hullXXAlignAt) {
880 for (row_type row = 0; row < nrows(); ++row)
881 numbered(row, false);
885 setType(hullEqnArray);
891 lyxerr << "mutation from '" << to_utf8(hullName(type_))
892 << "' to '" << to_utf8(hullName(newtype))
893 << "' not implemented" << endl;
898 docstring InsetMathHull::eolString(row_type row, bool emptyline, bool fragile) const
901 if (numberedType()) {
902 if (!label_[row].empty() && !nonum_[row])
903 res += "\\label{" + label_[row] + '}';
904 if (nonum_[row] && (type_ != hullMultline))
905 res += "\\nonumber ";
907 return res + InsetMathGrid::eolString(row, emptyline, fragile);
911 void InsetMathHull::write(WriteStream & os) const
914 InsetMathGrid::write(os);
919 void InsetMathHull::normalize(NormalStream & os) const
921 os << "[formula " << hullName(type_) << ' ';
922 InsetMathGrid::normalize(os);
927 void InsetMathHull::mathmlize(MathStream & os) const
929 InsetMathGrid::mathmlize(os);
933 void InsetMathHull::infoize(odocstream & os) const
935 os << "Type: " << hullName(type_);
939 void InsetMathHull::check() const
941 BOOST_ASSERT(nonum_.size() == nrows());
942 BOOST_ASSERT(label_.size() == nrows());
946 void InsetMathHull::doExtern(Cursor & cur, FuncRequest & func)
950 idocstringstream iss(func.argument());
951 iss >> dlang >> extra;
953 extra = from_ascii("noextra");
954 string const lang = to_ascii(dlang);
956 // FIXME: temporarily disabled
957 //if (cur.selection()) {
960 // lyxerr << "use selection: " << ar << endl;
961 // insert(pipeThroughExtern(lang, extra, ar));
966 eq.push_back(MathAtom(new InsetMathChar('=')));
968 // go to first item in line
969 cur.idx() -= cur.idx() % ncols();
972 if (getType() == hullSimple) {
973 size_type pos = cur.cell().find_last(eq);
975 if (cur.inMathed() && cur.selection()) {
976 asArray(grabAndEraseSelection(cur), ar);
977 } else if (pos == cur.cell().size()) {
979 lyxerr << "use whole cell: " << ar << endl;
981 ar = MathData(cur.cell().begin() + pos + 1, cur.cell().end());
982 lyxerr << "use partial cell form pos: " << pos << endl;
984 cur.cell().append(eq);
985 cur.cell().append(pipeThroughExtern(lang, extra, ar));
986 cur.pos() = cur.lastpos();
990 if (getType() == hullEquation) {
991 lyxerr << "use equation inset" << endl;
992 mutate(hullEqnArray);
993 MathData & ar = cur.cell();
994 lyxerr << "use cell: " << ar << endl;
998 cur.cell() = pipeThroughExtern(lang, extra, ar);
999 // move to end of line
1000 cur.pos() = cur.lastpos();
1005 lyxerr << "use eqnarray" << endl;
1006 cur.idx() += 2 - cur.idx() % ncols();
1008 MathData ar = cur.cell();
1009 lyxerr << "use cell: " << ar << endl;
1010 // FIXME: temporarily disabled
1016 cur.cell() = pipeThroughExtern(lang, extra, ar);
1017 cur.pos() = cur.lastpos();
1022 void InsetMathHull::doDispatch(Cursor & cur, FuncRequest & cmd)
1024 //lyxerr << "action: " << cmd.action << endl;
1025 switch (cmd.action) {
1027 case LFUN_FINISHED_BACKWARD:
1028 case LFUN_FINISHED_FORWARD:
1029 //lyxerr << "action: " << cmd.action << endl;
1030 InsetMathGrid::doDispatch(cur, cmd);
1031 notifyCursorLeaves(cur);
1035 case LFUN_BREAK_PARAGRAPH:
1036 // just swallow this
1040 // some magic for the common case
1041 if (type_ == hullSimple || type_ == hullEquation) {
1042 cur.recordUndoInset();
1044 cur.bv().buffer().params().use_amsmath == BufferParams::package_on;
1045 mutate(align ? hullAlign : hullEqnArray);
1046 cur.idx() = nrows() * ncols() - 1;
1047 cur.pos() = cur.lastpos();
1049 InsetMathGrid::doDispatch(cur, cmd);
1052 case LFUN_MATH_NUMBER:
1053 //lyxerr << "toggling all numbers" << endl;
1055 cur.recordUndoInset();
1056 bool old = numberedType();
1057 if (type_ == hullMultline)
1058 numbered(nrows() - 1, !old);
1060 for (row_type row = 0; row < nrows(); ++row)
1061 numbered(row, !old);
1063 cur.message(old ? _("No number") : _("Number"));
1067 case LFUN_MATH_NONUMBER:
1069 cur.recordUndoInset();
1070 row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1071 bool old = numbered(r);
1072 cur.message(old ? _("No number") : _("Number"));
1077 case LFUN_LABEL_INSERT: {
1078 cur.recordUndoInset();
1079 row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1080 docstring old_label = label(r);
1081 docstring const default_label = from_ascii(
1082 (lyxrc.label_init_length >= 0) ? "eq:" : "");
1083 if (old_label.empty())
1084 old_label = default_label;
1086 InsetCommandParams p(LABEL_CODE);
1087 p["name"] = cmd.argument().empty() ? old_label : cmd.argument();
1088 string const data = InsetCommandMailer::params2string("label", p);
1090 if (cmd.argument().empty())
1091 cur.bv().showDialog("label", data);
1093 FuncRequest fr(LFUN_INSET_INSERT, data);
1099 case LFUN_INSET_INSERT: {
1100 //lyxerr << "arg: " << to_utf8(cmd.argument()) << endl;
1101 string const name = cmd.getArg(0);
1102 if (name == "label") {
1103 InsetCommandParams p(LABEL_CODE);
1104 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), p);
1105 docstring str = p["name"];
1106 cur.recordUndoInset();
1107 row_type const r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1108 str = support::trim(str);
1111 docstring old = label(r);
1113 cur.bv().buffer().changeRefsIfUnique(old, str,
1119 InsetMathGrid::doDispatch(cur, cmd);
1123 case LFUN_MATH_EXTERN:
1124 cur.recordUndoInset();
1128 case LFUN_MATH_MUTATE: {
1129 cur.recordUndoInset();
1130 row_type row = cur.row();
1131 col_type col = cur.col();
1132 mutate(hullType(cmd.argument()));
1133 cur.idx() = row * ncols() + col;
1134 if (cur.idx() > cur.lastidx()) {
1135 cur.idx() = cur.lastidx();
1136 cur.pos() = cur.lastpos();
1138 if (cur.pos() > cur.lastpos())
1139 cur.pos() = cur.lastpos();
1140 //cur.dispatched(FINISHED);
1144 case LFUN_MATH_DISPLAY: {
1145 cur.recordUndoInset();
1146 mutate(type_ == hullSimple ? hullEquation : hullSimple);
1148 cur.pos() = cur.lastpos();
1149 //cur.dispatched(FINISHED);
1154 InsetMathGrid::doDispatch(cur, cmd);
1160 bool InsetMathHull::getStatus(Cursor & cur, FuncRequest const & cmd,
1161 FuncStatus & status) const
1163 switch (cmd.action) {
1164 case LFUN_FINISHED_BACKWARD:
1165 case LFUN_FINISHED_FORWARD:
1168 status.enabled(true);
1171 case LFUN_MATH_NUMBER:
1172 case LFUN_MATH_NONUMBER:
1173 case LFUN_MATH_EXTERN:
1174 case LFUN_MATH_MUTATE:
1175 case LFUN_MATH_DISPLAY:
1177 status.enabled(true);
1179 case LFUN_LABEL_INSERT:
1180 status.enabled(type_ != hullSimple);
1182 case LFUN_INSET_INSERT:
1183 if (cmd.getArg(0) == "label") {
1184 status.enabled(type_ != hullSimple);
1187 return InsetMathGrid::getStatus(cur, cmd, status);
1188 case LFUN_TABULAR_FEATURE: {
1189 istringstream is(to_utf8(cmd.argument()));
1193 && (s == "append-row"
1194 || s == "delete-row"
1195 || s == "copy-row")) {
1196 status.message(bformat(
1197 from_utf8(N_("Can't change number of rows in '%1$s'")),
1199 status.enabled(false);
1203 && (s == "append-column"
1204 || s == "delete-column"
1205 || s == "copy-column")) {
1206 status.message(bformat(
1207 from_utf8(N_("Can't change number of columns in '%1$s'")),
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 from_utf8(N_("Can't add horizontal grid lines in '%1$s'")),
1219 status.enabled(false);
1222 if (s == "add-vline-left" || s == "add-vline-right") {
1223 status.message(bformat(
1224 from_utf8(N_("Can't add vertical grid lines in '%1$s'")),
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 Text * lt = view_->cursor().innerText();
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 //dispatch(LFUN_ESCAPE);
1272 void InsetMathHull::handleFont(Cursor & cur, docstring const & arg,
1273 docstring 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(to_utf8(font));
1281 cur.handleNest(createInsetMath(font));
1287 void InsetMathHull::handleFont2(Cursor & cur, docstring const & arg)
1292 font.fromString(to_utf8(arg), b);
1293 if (font.fontInfo().color() != Color_inherit) {
1294 MathAtom at = MathAtom(new InsetMathColor(true, font.fontInfo().color()));
1295 cur.handleNest(at, 0);
1300 void InsetMathHull::edit(Cursor & cur, bool front)
1303 front ? idxFirst(cur) : idxLast(cur);
1304 // The inset formula dimension is not necessarily the same as the
1305 // one of the instant preview image, so we have to indicate to the
1306 // BufferView that a metrics update is needed.
1307 cur.updateFlags(Update::Force);
1311 docstring const InsetMathHull::editMessage() const
1313 return _("Math editor mode");
1317 void InsetMathHull::revealCodes(Cursor & cur) const
1319 if (!cur.inMathed())
1321 odocstringstream os;
1323 cur.message(os.str());
1325 // write something to the minibuffer
1326 // translate to latex
1330 string str = os.str();
1332 string::size_type pos = 0;
1334 for (string::iterator it = str.begin(); it != str.end(); ++it) {
1337 else if (*it == '\0') {
1339 pos = it - str.begin();
1345 res = res.substr(pos - 30);
1346 if (res.size() > 60)
1347 res = res.substr(0, 60);
1353 InsetCode InsetMathHull::lyxCode() const
1359 /////////////////////////////////////////////////////////////////////
1363 bool InsetMathHull::searchForward(BufferView * bv, string const & str,
1366 // FIXME: completely broken
1367 static InsetMathHull * lastformula = 0;
1368 static CursorBase current = DocIterator(ibegin(nucleus()));
1370 static string laststr;
1372 if (lastformula != this || laststr != str) {
1373 //lyxerr << "reset lastformula to " << this << endl;
1376 current = ibegin(nucleus());
1378 mathed_parse_cell(ar, str);
1382 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
1384 for (DocIterator it = current; it != iend(nucleus()); increment(it)) {
1385 CursorSlice & top = it.back();
1386 MathData const & a = top.asInsetMath()->cell(top.idx_);
1387 if (a.matchpart(ar, top.pos_)) {
1388 bv->cursor().setSelection(it, ar.size());
1390 top.pos_ += ar.size();
1396 //lyxerr << "not found!" << endl;
1403 void InsetMathHull::write(Buffer const &, ostream & os) const
1405 odocstringstream oss;
1406 WriteStream wi(oss, false, false);
1409 os << to_utf8(oss.str());
1413 void InsetMathHull::read(Buffer const &, Lexer & lex)
1416 mathed_parse_normal(at, lex);
1417 operator=(*at->asHullInset());
1421 int InsetMathHull::plaintext(Buffer const &, odocstream & os,
1422 OutputParams const &) const
1424 if (0 && display()) {
1428 TextPainter tpain(dim.width(), dim.height());
1429 drawT(tpain, 0, dim.ascent());
1431 // reset metrics cache to "real" values
1433 return tpain.textheight();
1435 odocstringstream oss;
1436 WriteStream wi(oss, false, true);
1439 docstring const str = oss.str();
1446 int InsetMathHull::docbook(Buffer const & buf, odocstream & os,
1447 OutputParams const & runparams) const
1452 if (getType() == hullSimple)
1453 name = from_ascii("inlineequation");
1455 name = from_ascii("informalequation");
1457 docstring bname = name;
1458 if (!label(0).empty())
1459 bname += " id='" + sgml::cleanID(buf, runparams, label(0)) + "'";
1461 ++ms.tab(); ms.cr(); ms.os() << '<' << bname << '>';
1463 odocstringstream ls;
1464 if (runparams.flavor == OutputParams::XML) {
1465 ms << MTag("alt role='tex' ");
1466 // Workaround for db2latex: db2latex always includes equations with
1467 // \ensuremath{} or \begin{display}\end{display}
1468 // so we strip LyX' math environment
1469 WriteStream wi(ls, false, false);
1470 InsetMathGrid::write(wi);
1471 ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&"), "<", "<"));
1476 InsetMathGrid::mathmlize(ms);
1479 ms << MTag("alt role='tex'");
1480 res = latex(buf, ls, runparams);
1481 ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&"), "<", "<"));
1485 ms << from_ascii("<graphic fileref=\"eqn/");
1486 if (!label(0).empty())
1487 ms << sgml::cleanID(buf, runparams, label(0));
1489 ms << sgml::uniqueID(from_ascii("anon"));
1491 if (runparams.flavor == OutputParams::XML)
1492 ms << from_ascii("\"/>");
1494 ms << from_ascii("\">");
1496 ms.cr(); --ms.tab(); ms.os() << "</" << name << '>';
1498 return ms.line() + res;
1502 void InsetMathHull::textString(Buffer const & buf, odocstream & os) const
1504 plaintext(buf, os, OutputParams(0));