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"
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"
44 #include "OutputParams.h"
46 #include "TextPainter.h"
49 #include "insets/RenderPreview.h"
50 #include "insets/InsetLabel.h"
52 #include "graphics/PreviewImage.h"
53 #include "graphics/PreviewLoader.h"
55 #include "frontends/Painter.h"
57 #include "support/lyxlib.h"
58 #include "support/lstrings.h"
60 #include <boost/bind.hpp>
67 using cap::grabAndEraseSelection;
68 using support::bformat;
75 using std::istringstream;
76 using std::ostringstream;
84 int getCols(HullType type)
101 // returns position of first relation operator in the array
102 // used for "intelligent splitting"
103 size_t firstRelOp(MathData const & ar)
105 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
106 if ((*it)->isRelOp())
107 return it - ar.begin();
112 char const * star(bool numbered)
114 return numbered ? "" : "*";
118 } // end anon namespace
121 HullType hullType(docstring const & s)
123 if (s == "none") return hullNone;
124 if (s == "simple") return hullSimple;
125 if (s == "equation") return hullEquation;
126 if (s == "eqnarray") return hullEqnArray;
127 if (s == "align") return hullAlign;
128 if (s == "alignat") return hullAlignAt;
129 if (s == "xalignat") return hullXAlignAt;
130 if (s == "xxalignat") return hullXXAlignAt;
131 if (s == "multline") return hullMultline;
132 if (s == "gather") return hullGather;
133 if (s == "flalign") return hullFlAlign;
134 lyxerr << "unknown hull type '" << to_utf8(s) << "'" << endl;
139 docstring hullName(HullType type)
142 case hullNone: return from_ascii("none");
143 case hullSimple: return from_ascii("simple");
144 case hullEquation: return from_ascii("equation");
145 case hullEqnArray: return from_ascii("eqnarray");
146 case hullAlign: return from_ascii("align");
147 case hullAlignAt: return from_ascii("alignat");
148 case hullXAlignAt: return from_ascii("xalignat");
149 case hullXXAlignAt: return from_ascii("xxalignat");
150 case hullMultline: return from_ascii("multline");
151 case hullGather: return from_ascii("gather");
152 case hullFlAlign: return from_ascii("flalign");
154 lyxerr << "unknown hull type '" << type << "'" << endl;
155 return from_ascii("none");
160 InsetMathHull::InsetMathHull()
161 : InsetMathGrid(1, 1), type_(hullNone), nonum_(1), label_(1),
162 preview_(new RenderPreview(this))
164 //lyxerr << "sizeof InsetMath: " << sizeof(InsetMath) << endl;
165 //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl;
166 //lyxerr << "sizeof InsetMathChar: " << sizeof(InsetMathChar) << endl;
167 //lyxerr << "sizeof Font: " << sizeof(Font) << endl;
173 InsetMathHull::InsetMathHull(HullType type)
174 : InsetMathGrid(getCols(type), 1), type_(type), nonum_(1), label_(1),
175 preview_(new RenderPreview(this))
182 InsetMathHull::InsetMathHull(InsetMathHull const & other)
183 : InsetMathGrid(other),
184 type_(other.type_), nonum_(other.nonum_), label_(other.label_),
185 preview_(new RenderPreview(this))
189 InsetMathHull::~InsetMathHull()
193 auto_ptr<Inset> InsetMathHull::doClone() const
195 return auto_ptr<Inset>(new InsetMathHull(*this));
199 InsetMathHull & InsetMathHull::operator=(InsetMathHull const & other)
203 *static_cast<InsetMathGrid*>(this) = InsetMathGrid(other);
205 nonum_ = other.nonum_;
206 label_ = other.label_;
207 preview_.reset(new RenderPreview(*other.preview_, this));
213 Inset * InsetMathHull::editXY(Cursor & cur, int x, int y)
219 return InsetMathNest::editXY(cur, x, y);
223 InsetMath::mode_type InsetMathHull::currentMode() const
225 if (type_ == hullNone)
226 return UNDECIDED_MODE;
227 // definitely math mode ...
232 bool InsetMathHull::idxFirst(Cursor & cur) const
240 bool InsetMathHull::idxLast(Cursor & cur) const
242 cur.idx() = nargs() - 1;
243 cur.pos() = cur.lastpos();
248 char InsetMathHull::defaultColAlign(col_type col)
250 if (type_ == hullEqnArray)
252 if (type_ >= hullAlign)
253 return "rl"[col & 1];
258 int InsetMathHull::defaultColSpace(col_type col)
260 if (type_ == hullAlign || type_ == hullAlignAt)
262 if (type_ == hullXAlignAt)
263 return (col & 1) ? 20 : 0;
264 if (type_ == hullXXAlignAt || type_ == hullFlAlign)
265 return (col & 1) ? 40 : 0;
270 docstring InsetMathHull::standardFont() const
272 return from_ascii(type_ == hullNone ? "lyxnochange" : "mathnormal");
276 bool InsetMathHull::previewState(BufferView * bv) const
278 if (!editing(bv) && RenderPreview::status() == LyXRC::PREVIEW_ON) {
279 graphics::PreviewImage const * pimage =
280 preview_->getPreviewImage(*bv->buffer());
281 return pimage && pimage->image();
287 bool InsetMathHull::metrics(MetricsInfo & mi, Dimension & dim) const
289 if (previewState(mi.base.bv)) {
290 preview_->metrics(mi, dim);
291 // insert a one pixel gap in front of the formula
294 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, from_ascii("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);
336 void InsetMathHull::draw(PainterInfo & pi, int x, int y) const
338 use_preview_ = previewState(pi.base.bv);
340 Cursor & cur = pi.base.bv->cursor();
341 // background of mathed under focus is not painted because
342 // selection at the top level of nested inset is difficult to handle.
343 if (!editing(pi.base.bv))
344 pi.pain.fillRectangle(x + 1, y - ascent() + 1, width() - 2,
345 ascent() + descent() - 1, Color::mathbg);
348 // one pixel gap in front
349 preview_->draw(pi, x + 1, y);
350 setPosCache(pi, x, y);
354 FontSetChanger dummy1(pi.base, standardFont());
355 StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
356 InsetMathGrid::draw(pi, x + 1, y);
358 if (numberedType()) {
359 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
360 for (row_type row = 0; row < nrows(); ++row) {
361 int const yy = y + rowinfo_[row].offset_;
362 FontSetChanger dummy(pi.base, from_ascii("mathrm"));
363 docstring const nl = nicelabel(row);
367 setPosCache(pi, x, y);
371 void InsetMathHull::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
374 InsetMathGrid::metricsT(mi, dim);
377 WriteStream wi(os, false, true);
379 dim.wid = os.str().size();
386 void InsetMathHull::drawT(TextPainter & pain, int x, int y) const
389 InsetMathGrid::drawT(pain, x, y);
392 WriteStream wi(os, false, true);
394 pain.draw(x, y, os.str().c_str());
401 docstring const latex_string(InsetMathHull const & inset)
404 WriteStream wi(ls, false, false);
412 void InsetMathHull::addPreview(graphics::PreviewLoader & ploader) const
414 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
415 docstring const snippet = latex_string(*this);
416 preview_->addPreview(snippet, ploader);
421 bool InsetMathHull::notifyCursorLeaves(Cursor & cur)
423 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
424 Buffer const & buffer = cur.buffer();
425 docstring const snippet = latex_string(*this);
426 preview_->addPreview(snippet, buffer);
427 preview_->startLoading(buffer);
428 cur.updateFlags(Update::Force);
434 docstring InsetMathHull::label(row_type row) const
436 BOOST_ASSERT(row < nrows());
441 void InsetMathHull::label(row_type row, docstring const & label)
443 //lyxerr << "setting label '" << label << "' for row " << row << endl;
448 void InsetMathHull::numbered(row_type row, bool num)
456 bool InsetMathHull::numbered(row_type row) const
462 bool InsetMathHull::ams() const
465 type_ == hullAlign ||
466 type_ == hullFlAlign ||
467 type_ == hullMultline ||
468 type_ == hullGather ||
469 type_ == hullAlignAt ||
470 type_ == hullXAlignAt ||
471 type_ == hullXXAlignAt;
475 bool InsetMathHull::display() const
477 return type_ != hullSimple && type_ != hullNone;
481 void InsetMathHull::getLabelList(Buffer const &, vector<docstring> & labels) const
483 for (row_type row = 0; row < nrows(); ++row)
484 if (!label_[row].empty() && nonum_[row] != 1)
485 labels.push_back(label_[row]);
489 bool InsetMathHull::numberedType() const
491 if (type_ == hullNone)
493 if (type_ == hullSimple)
495 if (type_ == hullXXAlignAt)
497 for (row_type row = 0; row < nrows(); ++row)
504 void InsetMathHull::validate(LaTeXFeatures & features) const
507 features.require("amsmath");
510 // Validation is necessary only if not using AMS math.
511 // To be safe, we will always run mathedvalidate.
512 //if (features.amsstyle)
515 features.require("boldsymbol");
516 //features.binom = true;
518 InsetMathGrid::validate(features);
522 void InsetMathHull::header_write(WriteStream & os) const
524 bool n = numberedType();
538 os << "\\begin{equation" << star(n) << "}\n";
548 os << "\\begin{" << hullName(type_) << star(n) << "}\n";
553 os << "\\begin{" << hullName(type_) << star(n) << '}'
554 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
558 os << "\\begin{" << hullName(type_) << '}'
559 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
563 os << "\\begin{unknown" << star(n) << '}';
569 void InsetMathHull::footer_write(WriteStream & os) const
571 bool n = numberedType();
584 os << "\\end{equation" << star(n) << "}\n";
596 os << "\\end{" << hullName(type_) << star(n) << "}\n";
600 os << "\\end{" << hullName(type_) << "}\n";
604 os << "\\end{unknown" << star(n) << '}';
610 bool InsetMathHull::rowChangeOK() const
613 type_ == hullEqnArray || type_ == hullAlign ||
614 type_ == hullFlAlign || type_ == hullAlignAt ||
615 type_ == hullXAlignAt || type_ == hullXXAlignAt ||
616 type_ == hullGather || type_ == hullMultline;
620 bool InsetMathHull::colChangeOK() const
623 type_ == hullAlign || type_ == hullFlAlign ||type_ == hullAlignAt ||
624 type_ == hullXAlignAt || type_ == hullXXAlignAt;
628 void InsetMathHull::addRow(row_type row)
632 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
633 label_.insert(label_.begin() + row + 1, docstring());
634 InsetMathGrid::addRow(row);
638 void InsetMathHull::swapRow(row_type row)
642 if (row + 1 == nrows())
644 swap(nonum_[row], nonum_[row + 1]);
645 swap(label_[row], label_[row + 1]);
646 InsetMathGrid::swapRow(row);
650 void InsetMathHull::delRow(row_type row)
652 if (nrows() <= 1 || !rowChangeOK())
654 InsetMathGrid::delRow(row);
655 // The last dummy row has no number info nor a label.
656 // Test nrows() + 1 because we have already erased the row.
657 if (row == nrows() + 1)
659 nonum_.erase(nonum_.begin() + row);
660 label_.erase(label_.begin() + row);
664 void InsetMathHull::addCol(col_type col)
668 InsetMathGrid::addCol(col);
672 void InsetMathHull::delCol(col_type col)
674 if (ncols() <= 1 || !colChangeOK())
676 InsetMathGrid::delCol(col);
680 docstring InsetMathHull::nicelabel(row_type row) const
684 if (label_[row].empty())
685 return from_ascii("(#)");
686 return '(' + label_[row] + ')';
690 void InsetMathHull::glueall()
693 for (idx_type i = 0; i < nargs(); ++i)
695 *this = InsetMathHull(hullSimple);
701 void InsetMathHull::splitTo2Cols()
703 BOOST_ASSERT(ncols() == 1);
704 InsetMathGrid::addCol(1);
705 for (row_type row = 0; row < nrows(); ++row) {
706 idx_type const i = 2 * row;
707 pos_type pos = firstRelOp(cell(i));
708 cell(i + 1) = MathData(cell(i).begin() + pos, cell(i).end());
709 cell(i).erase(pos, cell(i).size());
714 void InsetMathHull::splitTo3Cols()
716 BOOST_ASSERT(ncols() < 3);
719 InsetMathGrid::addCol(1);
720 for (row_type row = 0; row < nrows(); ++row) {
721 idx_type const i = 3 * row + 1;
722 if (cell(i).size()) {
723 cell(i + 1) = MathData(cell(i).begin() + 1, cell(i).end());
724 cell(i).erase(1, cell(i).size());
730 void InsetMathHull::changeCols(col_type cols)
734 else if (ncols() < cols) {
740 while (ncols() < cols)
741 InsetMathGrid::addCol(ncols() - 1);
747 for (row_type row = 0; row < nrows(); ++row) {
748 idx_type const i = row * ncols();
749 for (col_type col = cols; col < ncols(); ++col) {
750 cell(i + cols - 1).append(cell(i + col));
754 while (ncols() > cols) {
755 InsetMathGrid::delCol(ncols() - 1);
760 HullType InsetMathHull::getType() const
766 void InsetMathHull::setType(HullType type)
773 void InsetMathHull::mutate(HullType newtype)
775 //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
777 // we try to move along the chain
778 // none <-> simple <-> equation <-> eqnarray -> *align* -> multline, gather -+
780 // +-------------------------------------+
781 // we use eqnarray as intermediate type for mutations that are not
782 // directly supported because it handles labels and numbering for
785 if (newtype == type_) {
789 else if (newtype < hullNone) {
794 else if (type_ == hullNone) {
800 else if (type_ == hullSimple) {
801 if (newtype == hullNone) {
805 setType(hullEquation);
811 else if (type_ == hullEquation) {
812 if (newtype < type_) {
816 } else if (newtype == hullEqnArray) {
817 // split it "nicely" on the first relop
819 setType(hullEqnArray);
820 } else if (newtype == hullMultline || newtype == hullGather) {
830 else if (type_ == hullEqnArray) {
831 if (newtype < type_) {
832 // set correct (no)numbering
833 bool allnonum = true;
834 for (row_type row = 0; row < nrows(); ++row)
838 // set first non-empty label
840 for (row_type row = 0; row < nrows(); ++row) {
841 if (!label_[row].empty()) {
848 nonum_[0] = allnonum;
851 } else { // align & Co.
858 else if (type_ == hullAlign || type_ == hullAlignAt ||
859 type_ == hullXAlignAt || type_ == hullFlAlign) {
860 if (newtype < hullAlign) {
862 setType(hullEqnArray);
864 } else if (newtype == hullGather || newtype == hullMultline) {
867 } else if (newtype == hullXXAlignAt) {
868 for (row_type row = 0; row < nrows(); ++row)
869 numbered(row, false);
876 else if (type_ == hullXXAlignAt) {
877 for (row_type row = 0; row < nrows(); ++row)
878 numbered(row, false);
879 if (newtype < hullAlign) {
881 setType(hullEqnArray);
883 } else if (newtype == hullGather || newtype == hullMultline) {
891 else if (type_ == hullMultline || type_ == hullGather) {
892 if (newtype == hullGather || newtype == hullMultline)
894 else if (newtype == hullAlign || newtype == hullFlAlign ||
895 newtype == hullAlignAt || newtype == hullXAlignAt) {
898 } else if (newtype == hullXXAlignAt) {
900 for (row_type row = 0; row < nrows(); ++row)
901 numbered(row, false);
905 setType(hullEqnArray);
911 lyxerr << "mutation from '" << to_utf8(hullName(type_))
912 << "' to '" << to_utf8(hullName(newtype))
913 << "' not implemented" << endl;
918 docstring InsetMathHull::eolString(row_type row, bool emptyline, bool fragile) const
921 if (numberedType()) {
922 if (!label_[row].empty() && !nonum_[row])
923 res += "\\label{" + label_[row] + '}';
924 if (nonum_[row] && (type_ != hullMultline))
925 res += "\\nonumber ";
927 return res + InsetMathGrid::eolString(row, emptyline, fragile);
931 void InsetMathHull::write(WriteStream & os) const
934 InsetMathGrid::write(os);
939 void InsetMathHull::normalize(NormalStream & os) const
941 os << "[formula " << hullName(type_) << ' ';
942 InsetMathGrid::normalize(os);
947 void InsetMathHull::mathmlize(MathStream & os) const
949 InsetMathGrid::mathmlize(os);
953 void InsetMathHull::infoize(odocstream & os) const
955 os << "Type: " << hullName(type_);
959 void InsetMathHull::check() const
961 BOOST_ASSERT(nonum_.size() == nrows());
962 BOOST_ASSERT(label_.size() == nrows());
966 void InsetMathHull::doExtern(Cursor & cur, FuncRequest & func)
970 idocstringstream iss(func.argument());
971 iss >> dlang >> extra;
973 extra = from_ascii("noextra");
974 std::string const lang = to_ascii(dlang);
977 #warning temporarily disabled
978 //if (cur.selection()) {
981 // lyxerr << "use selection: " << ar << endl;
982 // insert(pipeThroughExtern(lang, extra, ar));
988 eq.push_back(MathAtom(new InsetMathChar('=')));
990 // go to first item in line
991 cur.idx() -= cur.idx() % ncols();
994 if (getType() == hullSimple) {
995 size_type pos = cur.cell().find_last(eq);
997 if (cur.inMathed() && cur.selection()) {
998 asArray(grabAndEraseSelection(cur), ar);
999 } else if (pos == cur.cell().size()) {
1001 lyxerr << "use whole cell: " << ar << endl;
1003 ar = MathData(cur.cell().begin() + pos + 1, cur.cell().end());
1004 lyxerr << "use partial cell form pos: " << pos << endl;
1006 cur.cell().append(eq);
1007 cur.cell().append(pipeThroughExtern(lang, extra, ar));
1008 cur.pos() = cur.lastpos();
1012 if (getType() == hullEquation) {
1013 lyxerr << "use equation inset" << endl;
1014 mutate(hullEqnArray);
1015 MathData & ar = cur.cell();
1016 lyxerr << "use cell: " << ar << endl;
1020 cur.cell() = pipeThroughExtern(lang, extra, ar);
1021 // move to end of line
1022 cur.pos() = cur.lastpos();
1027 lyxerr << "use eqnarray" << endl;
1028 cur.idx() += 2 - cur.idx() % ncols();
1030 MathData ar = cur.cell();
1031 lyxerr << "use cell: " << ar << endl;
1032 #ifdef WITH_WARNINGS
1033 #warning temporarily disabled
1040 cur.cell() = pipeThroughExtern(lang, extra, ar);
1041 cur.pos() = cur.lastpos();
1046 void InsetMathHull::doDispatch(Cursor & cur, FuncRequest & cmd)
1048 //lyxerr << "action: " << cmd.action << endl;
1049 switch (cmd.action) {
1051 case LFUN_FINISHED_LEFT:
1052 case LFUN_FINISHED_RIGHT:
1053 case LFUN_FINISHED_UP:
1054 case LFUN_FINISHED_DOWN:
1055 //lyxerr << "action: " << cmd.action << endl;
1056 InsetMathGrid::doDispatch(cur, cmd);
1057 notifyCursorLeaves(cur);
1061 case LFUN_BREAK_PARAGRAPH:
1062 // just swallow this
1065 case LFUN_BREAK_LINE:
1066 // some magic for the common case
1067 if (type_ == hullSimple || type_ == hullEquation) {
1068 recordUndoInset(cur);
1070 cur.bv().buffer()->params().use_amsmath == BufferParams::package_on;
1071 mutate(align ? hullAlign : hullEqnArray);
1073 cur.pos() = cur.lastpos();
1075 InsetMathGrid::doDispatch(cur, cmd);
1078 case LFUN_MATH_NUMBER:
1079 //lyxerr << "toggling all numbers" << endl;
1081 recordUndoInset(cur);
1082 bool old = numberedType();
1083 if (type_ == hullMultline)
1084 numbered(nrows() - 1, !old);
1086 for (row_type row = 0; row < nrows(); ++row)
1087 numbered(row, !old);
1089 cur.message(old ? _("No number") : _("Number"));
1093 case LFUN_MATH_NONUMBER:
1095 recordUndoInset(cur);
1096 row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1097 bool old = numbered(r);
1098 cur.message(old ? _("No number") : _("Number"));
1103 case LFUN_LABEL_INSERT: {
1104 recordUndoInset(cur);
1105 row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1106 docstring old_label = label(r);
1107 docstring const default_label = from_ascii(
1108 (lyxrc.label_init_length >= 0) ? "eq:" : "");
1109 if (old_label.empty())
1110 old_label = default_label;
1112 InsetCommandParams p("label");
1113 p["name"] = cmd.argument().empty() ? old_label : cmd.argument();
1114 std::string const data = InsetCommandMailer::params2string("label", p);
1116 if (cmd.argument().empty())
1117 cur.bv().showInsetDialog("label", data, 0);
1119 FuncRequest fr(LFUN_INSET_INSERT, data);
1125 case LFUN_INSET_INSERT: {
1126 //lyxerr << "arg: " << to_utf8(cmd.argument()) << endl;
1127 std::string const name = cmd.getArg(0);
1128 if (name == "label") {
1129 InsetCommandParams p("label");
1130 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), p);
1131 docstring str = p["name"];
1132 recordUndoInset(cur);
1133 row_type const r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1134 str = support::trim(str);
1137 docstring old = label(r);
1139 cur.bv().buffer()->changeRefsIfUnique(old, str,
1145 InsetMathGrid::doDispatch(cur, cmd);
1149 case LFUN_MATH_EXTERN:
1150 recordUndoInset(cur);
1154 case LFUN_MATH_MUTATE: {
1155 recordUndoInset(cur);
1156 row_type row = cur.row();
1157 col_type col = cur.col();
1158 mutate(hullType(cmd.argument()));
1159 cur.idx() = row * ncols() + col;
1160 if (cur.idx() > cur.lastidx()) {
1161 cur.idx() = cur.lastidx();
1162 cur.pos() = cur.lastpos();
1164 if (cur.pos() > cur.lastpos())
1165 cur.pos() = cur.lastpos();
1166 //cur.dispatched(FINISHED);
1170 case LFUN_MATH_DISPLAY: {
1171 recordUndoInset(cur);
1172 mutate(type_ == hullSimple ? hullEquation : hullSimple);
1174 cur.pos() = cur.lastpos();
1175 //cur.dispatched(FINISHED);
1180 InsetMathGrid::doDispatch(cur, cmd);
1186 bool InsetMathHull::getStatus(Cursor & cur, FuncRequest const & cmd,
1187 FuncStatus & status) const
1189 switch (cmd.action) {
1190 case LFUN_FINISHED_LEFT:
1191 case LFUN_FINISHED_RIGHT:
1192 case LFUN_FINISHED_UP:
1193 case LFUN_FINISHED_DOWN:
1194 status.enabled(true);
1196 case LFUN_BREAK_LINE:
1197 case LFUN_MATH_NUMBER:
1198 case LFUN_MATH_NONUMBER:
1199 case LFUN_MATH_EXTERN:
1200 case LFUN_MATH_MUTATE:
1201 case LFUN_MATH_DISPLAY:
1203 status.enabled(true);
1205 case LFUN_LABEL_INSERT:
1206 status.enabled(type_ != hullSimple);
1208 case LFUN_INSET_INSERT:
1209 if (cmd.getArg(0) == "label") {
1210 status.enabled(type_ != hullSimple);
1213 return InsetMathGrid::getStatus(cur, cmd, status);
1214 case LFUN_TABULAR_FEATURE: {
1215 istringstream is(to_utf8(cmd.argument()));
1219 && (s == "append-row"
1220 || s == "delete-row"
1221 || s == "copy-row")) {
1222 status.message(bformat(
1223 from_utf8(N_("Can't change number of rows in '%1$s'")),
1225 status.enabled(false);
1229 && (s == "append-column"
1230 || s == "delete-column"
1231 || s == "copy-column")) {
1232 status.message(bformat(
1233 from_utf8(N_("Can't change number of columns in '%1$s'")),
1235 status.enabled(false);
1238 if ((type_ == hullSimple
1239 || type_ == hullEquation
1240 || type_ == hullNone) &&
1241 (s == "add-hline-above" || s == "add-hline-below")) {
1242 status.message(bformat(
1243 from_utf8(N_("Can't add horizontal grid lines in '%1$s'")),
1245 status.enabled(false);
1248 if (s == "add-vline-left" || s == "add-vline-right") {
1249 status.message(bformat(
1250 from_utf8(N_("Can't add vertical grid lines in '%1$s'")),
1252 status.enabled(false);
1255 if (s == "valign-top" || s == "valign-middle"
1256 || s == "valign-bottom" || s == "align-left"
1257 || s == "align-center" || s == "align-right") {
1258 status.enabled(false);
1261 return InsetMathGrid::getStatus(cur, cmd, status);
1264 return InsetMathGrid::getStatus(cur, cmd, status);
1267 // This cannot really happen, but inserted to shut-up gcc
1268 return InsetMathGrid::getStatus(cur, cmd, status);
1272 /////////////////////////////////////////////////////////////////////
1276 // simply scrap this function if you want
1277 void InsetMathHull::mutateToText()
1280 // translate to latex
1282 latex(NULL, os, false, false);
1283 string str = os.str();
1286 Text * lt = view_->cursor().innerText();
1287 string::const_iterator cit = str.begin();
1288 string::const_iterator end = str.end();
1289 for (; cit != end; ++cit)
1290 view_->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
1293 //dispatch(LFUN_ESCAPE);
1298 void InsetMathHull::handleFont(Cursor & cur, docstring const & arg,
1299 docstring const & font)
1301 // this whole function is a hack and won't work for incremental font
1304 if (cur.inset().asInsetMath()->name() == font)
1305 cur.handleFont(to_utf8(font));
1307 cur.handleNest(createInsetMath(font));
1313 void InsetMathHull::handleFont2(Cursor & cur, docstring const & arg)
1318 bv_funcs::string2font(to_utf8(arg), font, b);
1319 if (font.color() != Color::inherit) {
1320 MathAtom at = MathAtom(new InsetMathColor(true, font.color()));
1321 cur.handleNest(at, 0);
1326 void InsetMathHull::edit(Cursor & cur, bool left)
1329 left ? idxFirst(cur) : idxLast(cur);
1330 // The inset formula dimension is not necessarily the same as the
1331 // one of the instant preview image, so we have to indicate to the
1332 // BufferView that a metrics update is needed.
1333 cur.updateFlags(Update::Force);
1337 docstring const InsetMathHull::editMessage() const
1339 return _("Math editor mode");
1343 void InsetMathHull::revealCodes(Cursor & cur) const
1345 if (!cur.inMathed())
1347 odocstringstream os;
1349 cur.message(os.str());
1351 // write something to the minibuffer
1352 // translate to latex
1356 string str = os.str();
1358 string::size_type pos = 0;
1360 for (string::iterator it = str.begin(); it != str.end(); ++it) {
1363 else if (*it == '\0') {
1365 pos = it - str.begin();
1371 res = res.substr(pos - 30);
1372 if (res.size() > 60)
1373 res = res.substr(0, 60);
1379 Inset::Code InsetMathHull::lyxCode() const
1385 /////////////////////////////////////////////////////////////////////
1389 bool InsetMathHull::searchForward(BufferView * bv, string const & str,
1392 #ifdef WITH_WARNINGS
1393 #warning completely broken
1395 static InsetMathHull * lastformula = 0;
1396 static CursorBase current = DocIterator(ibegin(nucleus()));
1398 static string laststr;
1400 if (lastformula != this || laststr != str) {
1401 //lyxerr << "reset lastformula to " << this << endl;
1404 current = ibegin(nucleus());
1406 mathed_parse_cell(ar, str);
1410 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
1412 for (DocIterator it = current; it != iend(nucleus()); increment(it)) {
1413 CursorSlice & top = it.back();
1414 MathData const & a = top.asInsetMath()->cell(top.idx_);
1415 if (a.matchpart(ar, top.pos_)) {
1416 bv->cursor().setSelection(it, ar.size());
1418 top.pos_ += ar.size();
1424 //lyxerr << "not found!" << endl;
1431 void InsetMathHull::write(Buffer const &, std::ostream & os) const
1433 odocstringstream oss;
1434 WriteStream wi(oss, false, false);
1437 os << to_utf8(oss.str());
1441 void InsetMathHull::read(Buffer const &, Lexer & lex)
1444 mathed_parse_normal(at, lex);
1445 operator=(*at->asHullInset());
1449 int InsetMathHull::plaintext(Buffer const &, odocstream & os,
1450 OutputParams const &) const
1452 if (0 && display()) {
1456 TextPainter tpain(dim.width(), dim.height());
1457 drawT(tpain, 0, dim.ascent());
1459 // reset metrics cache to "real" values
1461 return tpain.textheight();
1463 odocstringstream oss;
1464 WriteStream wi(oss, false, true);
1467 docstring const str = oss.str();
1474 int InsetMathHull::docbook(Buffer const & buf, odocstream & os,
1475 OutputParams const & runparams) const
1480 if (getType() == hullSimple)
1481 name = from_ascii("inlineequation");
1483 name = from_ascii("informalequation");
1485 docstring bname = name;
1486 if (!label(0).empty())
1487 bname += " id='" + sgml::cleanID(buf, runparams, label(0)) + "'";
1490 odocstringstream ls;
1491 if (runparams.flavor == OutputParams::XML) {
1492 ms << MTag(from_ascii("alt role='tex' "));
1493 // Workaround for db2latex: db2latex always includes equations with
1494 // \ensuremath{} or \begin{display}\end{display}
1495 // so we strip LyX' math environment
1496 WriteStream wi(ls, false, false);
1497 InsetMathGrid::write(wi);
1498 ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&"), "<", "<"));
1499 ms << ETag(from_ascii("alt"));
1500 ms << MTag(from_ascii("math"));
1501 ms << ETag(from_ascii("alt"));
1502 ms << MTag(from_ascii("math"));
1503 InsetMathGrid::mathmlize(ms);
1504 ms << ETag(from_ascii("math"));
1506 ms << MTag(from_ascii("alt role='tex'"));
1507 res = latex(buf, ls, runparams);
1508 ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&"), "<", "<"));
1509 ms << ETag(from_ascii("alt"));
1512 ms << from_ascii("<graphic fileref=\"eqn/");
1513 if (!label(0).empty())
1514 ms << sgml::cleanID(buf, runparams, label(0));
1516 ms << sgml::uniqueID(from_ascii("anon"));
1518 if (runparams.flavor == OutputParams::XML)
1519 ms << from_ascii("\"/>");
1521 ms << from_ascii("\">");
1524 return ms.line() + res;
1528 void InsetMathHull::textString(Buffer const & buf, odocstream & os) const
1530 plaintext(buf, os, OutputParams(0));