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 "support/lyxlib.h"
56 #include "support/lstrings.h"
58 #include <boost/bind.hpp>
65 using cap::grabAndEraseSelection;
66 using support::bformat;
73 using std::istringstream;
74 using std::ostringstream;
82 int getCols(HullType type)
99 // returns position of first relation operator in the array
100 // used for "intelligent splitting"
101 size_t firstRelOp(MathData const & ar)
103 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
104 if ((*it)->isRelOp())
105 return it - ar.begin();
110 char const * star(bool numbered)
112 return numbered ? "" : "*";
116 } // end anon namespace
119 HullType hullType(docstring const & s)
121 if (s == "none") return hullNone;
122 if (s == "simple") return hullSimple;
123 if (s == "equation") return hullEquation;
124 if (s == "eqnarray") return hullEqnArray;
125 if (s == "align") return hullAlign;
126 if (s == "alignat") return hullAlignAt;
127 if (s == "xalignat") return hullXAlignAt;
128 if (s == "xxalignat") return hullXXAlignAt;
129 if (s == "multline") return hullMultline;
130 if (s == "gather") return hullGather;
131 if (s == "flalign") return hullFlAlign;
132 lyxerr << "unknown hull type '" << to_utf8(s) << "'" << endl;
137 docstring hullName(HullType type)
140 case hullNone: return from_ascii("none");
141 case hullSimple: return from_ascii("simple");
142 case hullEquation: return from_ascii("equation");
143 case hullEqnArray: return from_ascii("eqnarray");
144 case hullAlign: return from_ascii("align");
145 case hullAlignAt: return from_ascii("alignat");
146 case hullXAlignAt: return from_ascii("xalignat");
147 case hullXXAlignAt: return from_ascii("xxalignat");
148 case hullMultline: return from_ascii("multline");
149 case hullGather: return from_ascii("gather");
150 case hullFlAlign: return from_ascii("flalign");
152 lyxerr << "unknown hull type '" << type << "'" << endl;
153 return from_ascii("none");
158 InsetMathHull::InsetMathHull()
159 : InsetMathGrid(1, 1), type_(hullNone), nonum_(1), label_(1),
160 preview_(new RenderPreview(this))
162 //lyxerr << "sizeof InsetMath: " << sizeof(InsetMath) << endl;
163 //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl;
164 //lyxerr << "sizeof InsetMathChar: " << sizeof(InsetMathChar) << endl;
165 //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << endl;
171 InsetMathHull::InsetMathHull(HullType type)
172 : InsetMathGrid(getCols(type), 1), type_(type), nonum_(1), label_(1),
173 preview_(new RenderPreview(this))
180 InsetMathHull::InsetMathHull(InsetMathHull const & other)
181 : InsetMathGrid(other),
182 type_(other.type_), nonum_(other.nonum_), label_(other.label_),
183 preview_(new RenderPreview(this))
187 InsetMathHull::~InsetMathHull()
191 auto_ptr<InsetBase> InsetMathHull::doClone() const
193 return auto_ptr<InsetBase>(new InsetMathHull(*this));
197 InsetMathHull & InsetMathHull::operator=(InsetMathHull const & other)
201 *static_cast<InsetMathGrid*>(this) = InsetMathGrid(other);
203 nonum_ = other.nonum_;
204 label_ = other.label_;
205 preview_.reset(new RenderPreview(*other.preview_, this));
211 InsetBase * InsetMathHull::editXY(Cursor & cur, int x, int y)
217 return InsetMathNest::editXY(cur, x, y);
221 InsetMath::mode_type InsetMathHull::currentMode() const
223 if (type_ == hullNone)
224 return UNDECIDED_MODE;
225 // definitely math mode ...
230 bool InsetMathHull::idxFirst(Cursor & cur) const
238 bool InsetMathHull::idxLast(Cursor & cur) const
240 cur.idx() = nargs() - 1;
241 cur.pos() = cur.lastpos();
246 char InsetMathHull::defaultColAlign(col_type col)
248 if (type_ == hullEqnArray)
250 if (type_ >= hullAlign)
251 return "rl"[col & 1];
256 int InsetMathHull::defaultColSpace(col_type col)
258 if (type_ == hullAlign || type_ == hullAlignAt)
260 if (type_ == hullXAlignAt)
261 return (col & 1) ? 20 : 0;
262 if (type_ == hullXXAlignAt || type_ == hullFlAlign)
263 return (col & 1) ? 40 : 0;
268 docstring InsetMathHull::standardFont() const
270 return from_ascii(type_ == hullNone ? "lyxnochange" : "mathnormal");
274 bool InsetMathHull::previewState(BufferView * bv) const
276 if (!editing(bv) && RenderPreview::status() == LyXRC::PREVIEW_ON) {
277 graphics::PreviewImage const * pimage =
278 preview_->getPreviewImage(*bv->buffer());
279 return pimage && pimage->image();
285 bool InsetMathHull::metrics(MetricsInfo & mi, Dimension & dim) const
287 if (previewState(mi.base.bv)) {
288 preview_->metrics(mi, dim);
289 // insert a one pixel gap in front of the formula
292 dim.des += displayMargin();
299 FontSetChanger dummy1(mi.base, standardFont());
300 StyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
302 // let the cells adjust themselves
303 InsetMathGrid::metrics(mi, dim);
306 dim.asc += displayMargin();
307 dim.des += displayMargin();
310 if (numberedType()) {
311 FontSetChanger dummy(mi.base, from_ascii("mathbf"));
313 for (row_type row = 0; row < nrows(); ++row)
314 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
320 // make it at least as high as the current font
323 math_font_max_dim(mi.base.font, asc, des);
324 dim.asc = max(dim.asc, asc);
325 dim.des = max(dim.des, des);
334 void InsetMathHull::draw(PainterInfo & pi, int x, int y) const
336 use_preview_ = previewState(pi.base.bv);
339 // one pixel gap in front
340 preview_->draw(pi, x + 1, y);
341 setPosCache(pi, x, y);
345 FontSetChanger dummy1(pi.base, standardFont());
346 StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
347 InsetMathGrid::draw(pi, x + 1, y);
349 if (numberedType()) {
350 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
351 for (row_type row = 0; row < nrows(); ++row) {
352 int const yy = y + rowinfo_[row].offset_;
353 FontSetChanger dummy(pi.base, from_ascii("mathrm"));
354 docstring const nl = nicelabel(row);
358 setPosCache(pi, x, y);
362 void InsetMathHull::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
365 InsetMathGrid::metricsT(mi, dim);
368 WriteStream wi(os, false, true);
370 dim.wid = os.str().size();
377 void InsetMathHull::drawT(TextPainter & pain, int x, int y) const
380 InsetMathGrid::drawT(pain, x, y);
383 WriteStream wi(os, false, true);
385 pain.draw(x, y, os.str().c_str());
392 docstring const latex_string(InsetMathHull const & inset)
395 WriteStream wi(ls, false, false);
403 void InsetMathHull::addPreview(graphics::PreviewLoader & ploader) const
405 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
406 docstring const snippet = latex_string(*this);
407 preview_->addPreview(snippet, ploader);
412 bool InsetMathHull::notifyCursorLeaves(Cursor & cur)
414 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
415 Buffer const & buffer = cur.buffer();
416 docstring const snippet = latex_string(*this);
417 preview_->addPreview(snippet, buffer);
418 preview_->startLoading(buffer);
419 cur.updateFlags(Update::Force);
425 docstring InsetMathHull::label(row_type row) const
427 BOOST_ASSERT(row < nrows());
432 void InsetMathHull::label(row_type row, docstring const & label)
434 //lyxerr << "setting label '" << label << "' for row " << row << endl;
439 void InsetMathHull::numbered(row_type row, bool num)
447 bool InsetMathHull::numbered(row_type row) const
453 bool InsetMathHull::ams() const
456 type_ == hullAlign ||
457 type_ == hullFlAlign ||
458 type_ == hullMultline ||
459 type_ == hullGather ||
460 type_ == hullAlignAt ||
461 type_ == hullXAlignAt ||
462 type_ == hullXXAlignAt;
466 bool InsetMathHull::display() const
468 return type_ != hullSimple && type_ != hullNone;
472 void InsetMathHull::getLabelList(Buffer const &, vector<docstring> & labels) const
474 for (row_type row = 0; row < nrows(); ++row)
475 if (!label_[row].empty() && nonum_[row] != 1)
476 labels.push_back(label_[row]);
480 bool InsetMathHull::numberedType() const
482 if (type_ == hullNone)
484 if (type_ == hullSimple)
486 if (type_ == hullXXAlignAt)
488 for (row_type row = 0; row < nrows(); ++row)
495 void InsetMathHull::validate(LaTeXFeatures & features) const
498 features.require("amsmath");
501 // Validation is necessary only if not using AMS math.
502 // To be safe, we will always run mathedvalidate.
503 //if (features.amsstyle)
506 features.require("boldsymbol");
507 //features.binom = true;
509 InsetMathGrid::validate(features);
513 void InsetMathHull::header_write(WriteStream & os) const
515 bool n = numberedType();
529 os << "\\begin{equation" << star(n) << "}\n";
539 os << "\\begin{" << hullName(type_) << star(n) << "}\n";
544 os << "\\begin{" << hullName(type_) << star(n) << '}'
545 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
549 os << "\\begin{" << hullName(type_) << '}'
550 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
554 os << "\\begin{unknown" << star(n) << '}';
560 void InsetMathHull::footer_write(WriteStream & os) const
562 bool n = numberedType();
575 os << "\\end{equation" << star(n) << "}\n";
587 os << "\\end{" << hullName(type_) << star(n) << "}\n";
591 os << "\\end{" << hullName(type_) << "}\n";
595 os << "\\end{unknown" << star(n) << '}';
601 bool InsetMathHull::rowChangeOK() const
604 type_ == hullEqnArray || type_ == hullAlign ||
605 type_ == hullFlAlign || type_ == hullAlignAt ||
606 type_ == hullXAlignAt || type_ == hullXXAlignAt ||
607 type_ == hullGather || type_ == hullMultline;
611 bool InsetMathHull::colChangeOK() const
614 type_ == hullAlign || type_ == hullFlAlign ||type_ == hullAlignAt ||
615 type_ == hullXAlignAt || type_ == hullXXAlignAt;
619 void InsetMathHull::addRow(row_type row)
623 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
624 label_.insert(label_.begin() + row + 1, docstring());
625 InsetMathGrid::addRow(row);
629 void InsetMathHull::swapRow(row_type row)
633 if (row + 1 == nrows())
635 swap(nonum_[row], nonum_[row + 1]);
636 swap(label_[row], label_[row + 1]);
637 InsetMathGrid::swapRow(row);
641 void InsetMathHull::delRow(row_type row)
643 if (nrows() <= 1 || !rowChangeOK())
645 InsetMathGrid::delRow(row);
646 // The last dummy row has no number info nor a label.
647 // Test nrows() + 1 because we have already erased the row.
648 if (row == nrows() + 1)
650 nonum_.erase(nonum_.begin() + row);
651 label_.erase(label_.begin() + row);
655 void InsetMathHull::addCol(col_type col)
659 InsetMathGrid::addCol(col);
663 void InsetMathHull::delCol(col_type col)
665 if (ncols() <= 1 || !colChangeOK())
667 InsetMathGrid::delCol(col);
671 docstring InsetMathHull::nicelabel(row_type row) const
675 if (label_[row].empty())
676 return from_ascii("(#)");
677 return '(' + label_[row] + ')';
681 void InsetMathHull::glueall()
684 for (idx_type i = 0; i < nargs(); ++i)
686 *this = InsetMathHull(hullSimple);
692 void InsetMathHull::splitTo2Cols()
694 BOOST_ASSERT(ncols() == 1);
695 InsetMathGrid::addCol(1);
696 for (row_type row = 0; row < nrows(); ++row) {
697 idx_type const i = 2 * row;
698 pos_type pos = firstRelOp(cell(i));
699 cell(i + 1) = MathData(cell(i).begin() + pos, cell(i).end());
700 cell(i).erase(pos, cell(i).size());
705 void InsetMathHull::splitTo3Cols()
707 BOOST_ASSERT(ncols() < 3);
710 InsetMathGrid::addCol(1);
711 for (row_type row = 0; row < nrows(); ++row) {
712 idx_type const i = 3 * row + 1;
713 if (cell(i).size()) {
714 cell(i + 1) = MathData(cell(i).begin() + 1, cell(i).end());
715 cell(i).erase(1, cell(i).size());
721 void InsetMathHull::changeCols(col_type cols)
725 else if (ncols() < cols) {
731 while (ncols() < cols)
732 InsetMathGrid::addCol(ncols() - 1);
738 for (row_type row = 0; row < nrows(); ++row) {
739 idx_type const i = row * ncols();
740 for (col_type col = cols; col < ncols(); ++col) {
741 cell(i + cols - 1).append(cell(i + col));
745 while (ncols() > cols) {
746 InsetMathGrid::delCol(ncols() - 1);
751 HullType InsetMathHull::getType() const
757 void InsetMathHull::setType(HullType type)
764 void InsetMathHull::mutate(HullType newtype)
766 //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
768 // we try to move along the chain
769 // none <-> simple <-> equation <-> eqnarray -> *align* -> multline, gather -+
771 // +-------------------------------------+
772 // we use eqnarray as intermediate type for mutations that are not
773 // directly supported because it handles labels and numbering for
776 if (newtype == type_) {
780 else if (newtype < hullNone) {
785 else if (type_ == hullNone) {
791 else if (type_ == hullSimple) {
792 if (newtype == hullNone) {
796 setType(hullEquation);
802 else if (type_ == hullEquation) {
803 if (newtype < type_) {
807 } else if (newtype == hullEqnArray) {
808 // split it "nicely" on the first relop
810 setType(hullEqnArray);
811 } else if (newtype == hullMultline || newtype == hullGather) {
821 else if (type_ == hullEqnArray) {
822 if (newtype < type_) {
823 // set correct (no)numbering
824 bool allnonum = true;
825 for (row_type row = 0; row < nrows(); ++row)
829 // set first non-empty label
831 for (row_type row = 0; row < nrows(); ++row) {
832 if (!label_[row].empty()) {
839 nonum_[0] = allnonum;
842 } else { // align & Co.
849 else if (type_ == hullAlign || type_ == hullAlignAt ||
850 type_ == hullXAlignAt || type_ == hullFlAlign) {
851 if (newtype < hullAlign) {
853 setType(hullEqnArray);
855 } else if (newtype == hullGather || newtype == hullMultline) {
858 } else if (newtype == hullXXAlignAt) {
859 for (row_type row = 0; row < nrows(); ++row)
860 numbered(row, false);
867 else if (type_ == hullXXAlignAt) {
868 for (row_type row = 0; row < nrows(); ++row)
869 numbered(row, false);
870 if (newtype < hullAlign) {
872 setType(hullEqnArray);
874 } else if (newtype == hullGather || newtype == hullMultline) {
882 else if (type_ == hullMultline || type_ == hullGather) {
883 if (newtype == hullGather || newtype == hullMultline)
885 else if (newtype == hullAlign || newtype == hullFlAlign ||
886 newtype == hullAlignAt || newtype == hullXAlignAt) {
889 } else if (newtype == hullXXAlignAt) {
891 for (row_type row = 0; row < nrows(); ++row)
892 numbered(row, false);
896 setType(hullEqnArray);
902 lyxerr << "mutation from '" << to_utf8(hullName(type_))
903 << "' to '" << to_utf8(hullName(newtype))
904 << "' not implemented" << endl;
909 docstring InsetMathHull::eolString(row_type row, bool emptyline, bool fragile) const
912 if (numberedType()) {
913 if (!label_[row].empty() && !nonum_[row])
914 res += "\\label{" + label_[row] + '}';
915 if (nonum_[row] && (type_ != hullMultline))
916 res += "\\nonumber ";
918 return res + InsetMathGrid::eolString(row, emptyline, fragile);
922 void InsetMathHull::write(WriteStream & os) const
925 InsetMathGrid::write(os);
930 void InsetMathHull::normalize(NormalStream & os) const
932 os << "[formula " << hullName(type_) << ' ';
933 InsetMathGrid::normalize(os);
938 void InsetMathHull::mathmlize(MathStream & os) const
940 InsetMathGrid::mathmlize(os);
944 void InsetMathHull::infoize(odocstream & os) const
946 os << "Type: " << hullName(type_);
950 void InsetMathHull::check() const
952 BOOST_ASSERT(nonum_.size() == nrows());
953 BOOST_ASSERT(label_.size() == nrows());
957 void InsetMathHull::doExtern(Cursor & cur, FuncRequest & func)
961 idocstringstream iss(func.argument());
962 iss >> dlang >> extra;
964 extra = from_ascii("noextra");
965 std::string const lang = to_ascii(dlang);
968 #warning temporarily disabled
969 //if (cur.selection()) {
972 // lyxerr << "use selection: " << ar << endl;
973 // insert(pipeThroughExtern(lang, extra, ar));
979 eq.push_back(MathAtom(new InsetMathChar('=')));
981 // go to first item in line
982 cur.idx() -= cur.idx() % ncols();
985 if (getType() == hullSimple) {
986 size_type pos = cur.cell().find_last(eq);
988 if (cur.inMathed() && cur.selection()) {
989 asArray(grabAndEraseSelection(cur), ar);
990 } else if (pos == cur.cell().size()) {
992 lyxerr << "use whole cell: " << ar << endl;
994 ar = MathData(cur.cell().begin() + pos + 1, cur.cell().end());
995 lyxerr << "use partial cell form pos: " << pos << endl;
997 cur.cell().append(eq);
998 cur.cell().append(pipeThroughExtern(lang, extra, ar));
999 cur.pos() = cur.lastpos();
1003 if (getType() == hullEquation) {
1004 lyxerr << "use equation inset" << endl;
1005 mutate(hullEqnArray);
1006 MathData & ar = cur.cell();
1007 lyxerr << "use cell: " << ar << endl;
1011 cur.cell() = pipeThroughExtern(lang, extra, ar);
1012 // move to end of line
1013 cur.pos() = cur.lastpos();
1018 lyxerr << "use eqnarray" << endl;
1019 cur.idx() += 2 - cur.idx() % ncols();
1021 MathData ar = cur.cell();
1022 lyxerr << "use cell: " << ar << endl;
1023 #ifdef WITH_WARNINGS
1024 #warning temporarily disabled
1031 cur.cell() = pipeThroughExtern(lang, extra, ar);
1032 cur.pos() = cur.lastpos();
1037 void InsetMathHull::doDispatch(Cursor & cur, FuncRequest & cmd)
1039 //lyxerr << "action: " << cmd.action << endl;
1040 switch (cmd.action) {
1042 case LFUN_FINISHED_LEFT:
1043 case LFUN_FINISHED_RIGHT:
1044 case LFUN_FINISHED_UP:
1045 case LFUN_FINISHED_DOWN:
1046 //lyxerr << "action: " << cmd.action << endl;
1047 InsetMathGrid::doDispatch(cur, cmd);
1048 notifyCursorLeaves(cur);
1052 case LFUN_BREAK_PARAGRAPH:
1053 // just swallow this
1056 case LFUN_BREAK_LINE:
1057 // some magic for the common case
1058 if (type_ == hullSimple || type_ == hullEquation) {
1059 recordUndoInset(cur);
1061 cur.bv().buffer()->params().use_amsmath == BufferParams::package_on;
1062 mutate(align ? hullAlign : hullEqnArray);
1064 cur.pos() = cur.lastpos();
1066 InsetMathGrid::doDispatch(cur, cmd);
1069 case LFUN_MATH_NUMBER:
1070 //lyxerr << "toggling all numbers" << endl;
1072 recordUndoInset(cur);
1073 bool old = numberedType();
1074 if (type_ == hullMultline)
1075 numbered(nrows() - 1, !old);
1077 for (row_type row = 0; row < nrows(); ++row)
1078 numbered(row, !old);
1080 cur.message(old ? _("No number") : _("Number"));
1084 case LFUN_MATH_NONUMBER:
1086 recordUndoInset(cur);
1087 row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1088 bool old = numbered(r);
1089 cur.message(old ? _("No number") : _("Number"));
1094 case LFUN_LABEL_INSERT: {
1095 recordUndoInset(cur);
1096 row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1097 docstring old_label = label(r);
1098 docstring const default_label = from_ascii(
1099 (lyxrc.label_init_length >= 0) ? "eq:" : "");
1100 if (old_label.empty())
1101 old_label = default_label;
1103 InsetCommandParams p("label");
1104 p["name"] = cmd.argument().empty() ? old_label : cmd.argument();
1105 std::string const data = InsetCommandMailer::params2string("label", p);
1107 if (cmd.argument().empty())
1108 cur.bv().showInsetDialog("label", data, 0);
1110 FuncRequest fr(LFUN_INSET_INSERT, data);
1116 case LFUN_INSET_INSERT: {
1117 //lyxerr << "arg: " << to_utf8(cmd.argument()) << endl;
1118 std::string const name = cmd.getArg(0);
1119 if (name == "label") {
1120 InsetCommandParams p("label");
1121 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), p);
1122 docstring str = p["name"];
1123 recordUndoInset(cur);
1124 row_type const r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1125 str = support::trim(str);
1128 docstring old = label(r);
1130 cur.bv().buffer()->changeRefsIfUnique(old, str,
1131 InsetBase::REF_CODE);
1136 InsetMathGrid::doDispatch(cur, cmd);
1140 case LFUN_MATH_EXTERN:
1141 recordUndoInset(cur);
1145 case LFUN_MATH_MUTATE: {
1146 recordUndoInset(cur);
1147 row_type row = cur.row();
1148 col_type col = cur.col();
1149 mutate(hullType(cmd.argument()));
1150 cur.idx() = row * ncols() + col;
1151 if (cur.idx() > cur.lastidx()) {
1152 cur.idx() = cur.lastidx();
1153 cur.pos() = cur.lastpos();
1155 if (cur.pos() > cur.lastpos())
1156 cur.pos() = cur.lastpos();
1157 //cur.dispatched(FINISHED);
1161 case LFUN_MATH_DISPLAY: {
1162 recordUndoInset(cur);
1163 mutate(type_ == hullSimple ? hullEquation : hullSimple);
1165 cur.pos() = cur.lastpos();
1166 //cur.dispatched(FINISHED);
1171 InsetMathGrid::doDispatch(cur, cmd);
1177 bool InsetMathHull::getStatus(Cursor & cur, FuncRequest const & cmd,
1178 FuncStatus & status) const
1180 switch (cmd.action) {
1181 case LFUN_FINISHED_LEFT:
1182 case LFUN_FINISHED_RIGHT:
1183 case LFUN_FINISHED_UP:
1184 case LFUN_FINISHED_DOWN:
1185 status.enabled(true);
1187 case LFUN_BREAK_LINE:
1188 case LFUN_MATH_NUMBER:
1189 case LFUN_MATH_NONUMBER:
1190 case LFUN_MATH_EXTERN:
1191 case LFUN_MATH_MUTATE:
1192 case LFUN_MATH_DISPLAY:
1194 status.enabled(true);
1196 case LFUN_LABEL_INSERT:
1197 status.enabled(type_ != hullSimple);
1199 case LFUN_INSET_INSERT:
1200 if (cmd.getArg(0) == "label") {
1201 status.enabled(type_ != hullSimple);
1204 return InsetMathGrid::getStatus(cur, cmd, status);
1205 case LFUN_TABULAR_FEATURE: {
1206 istringstream is(to_utf8(cmd.argument()));
1210 && (s == "append-row"
1211 || s == "delete-row"
1212 || s == "copy-row")) {
1213 status.message(bformat(
1214 from_utf8(N_("Can't change number of rows in '%1$s'")),
1216 status.enabled(false);
1220 && (s == "append-column"
1221 || s == "delete-column"
1222 || s == "copy-column")) {
1223 status.message(bformat(
1224 from_utf8(N_("Can't change number of columns in '%1$s'")),
1226 status.enabled(false);
1229 if ((type_ == hullSimple
1230 || type_ == hullEquation
1231 || type_ == hullNone) &&
1232 (s == "add-hline-above" || s == "add-hline-below")) {
1233 status.message(bformat(
1234 from_utf8(N_("Can't add horizontal grid lines in '%1$s'")),
1236 status.enabled(false);
1239 if (s == "add-vline-left" || s == "add-vline-right") {
1240 status.message(bformat(
1241 from_utf8(N_("Can't add vertical grid lines in '%1$s'")),
1243 status.enabled(false);
1246 if (s == "valign-top" || s == "valign-middle"
1247 || s == "valign-bottom" || s == "align-left"
1248 || s == "align-center" || s == "align-right") {
1249 status.enabled(false);
1252 return InsetMathGrid::getStatus(cur, cmd, status);
1255 return InsetMathGrid::getStatus(cur, cmd, status);
1258 // This cannot really happen, but inserted to shut-up gcc
1259 return InsetMathGrid::getStatus(cur, cmd, status);
1263 /////////////////////////////////////////////////////////////////////
1267 // simply scrap this function if you want
1268 void InsetMathHull::mutateToText()
1271 // translate to latex
1273 latex(NULL, os, false, false);
1274 string str = os.str();
1277 LyXText * lt = view_->cursor().innerText();
1278 string::const_iterator cit = str.begin();
1279 string::const_iterator end = str.end();
1280 for (; cit != end; ++cit)
1281 view_->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
1284 //dispatch(LFUN_ESCAPE);
1289 void InsetMathHull::handleFont(Cursor & cur, docstring const & arg,
1290 docstring const & font)
1292 // this whole function is a hack and won't work for incremental font
1295 if (cur.inset().asInsetMath()->name() == font)
1296 cur.handleFont(to_utf8(font));
1298 cur.handleNest(createInsetMath(font));
1304 void InsetMathHull::handleFont2(Cursor & cur, docstring const & arg)
1309 bv_funcs::string2font(to_utf8(arg), font, b);
1310 if (font.color() != LColor::inherit) {
1311 MathAtom at = MathAtom(new InsetMathColor(true, font.color()));
1312 cur.handleNest(at, 0);
1317 void InsetMathHull::edit(Cursor & cur, bool left)
1320 left ? idxFirst(cur) : idxLast(cur);
1321 // The inset formula dimension is not necessarily the same as the
1322 // one of the instant preview image, so we have to indicate to the
1323 // BufferView that a metrics update is needed.
1324 cur.updateFlags(Update::Force);
1328 docstring const InsetMathHull::editMessage() const
1330 return _("Math editor mode");
1334 void InsetMathHull::revealCodes(Cursor & cur) const
1336 if (!cur.inMathed())
1338 odocstringstream os;
1340 cur.message(os.str());
1342 // write something to the minibuffer
1343 // translate to latex
1347 string str = os.str();
1349 string::size_type pos = 0;
1351 for (string::iterator it = str.begin(); it != str.end(); ++it) {
1354 else if (*it == '\0') {
1356 pos = it - str.begin();
1362 res = res.substr(pos - 30);
1363 if (res.size() > 60)
1364 res = res.substr(0, 60);
1370 InsetBase::Code InsetMathHull::lyxCode() const
1376 /////////////////////////////////////////////////////////////////////
1380 bool InsetMathHull::searchForward(BufferView * bv, string const & str,
1383 #ifdef WITH_WARNINGS
1384 #warning completely broken
1386 static InsetMathHull * lastformula = 0;
1387 static CursorBase current = DocIterator(ibegin(nucleus()));
1389 static string laststr;
1391 if (lastformula != this || laststr != str) {
1392 //lyxerr << "reset lastformula to " << this << endl;
1395 current = ibegin(nucleus());
1397 mathed_parse_cell(ar, str);
1401 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
1403 for (DocIterator it = current; it != iend(nucleus()); increment(it)) {
1404 CursorSlice & top = it.back();
1405 MathData const & a = top.asInsetMath()->cell(top.idx_);
1406 if (a.matchpart(ar, top.pos_)) {
1407 bv->cursor().setSelection(it, ar.size());
1409 top.pos_ += ar.size();
1415 //lyxerr << "not found!" << endl;
1422 void InsetMathHull::write(Buffer const &, std::ostream & os) const
1424 odocstringstream oss;
1425 WriteStream wi(oss, false, false);
1428 os << to_utf8(oss.str());
1432 void InsetMathHull::read(Buffer const &, Lexer & lex)
1435 mathed_parse_normal(at, lex);
1436 operator=(*at->asHullInset());
1440 int InsetMathHull::plaintext(Buffer const &, odocstream & os,
1441 OutputParams const &) const
1443 if (0 && display()) {
1447 TextPainter tpain(dim.width(), dim.height());
1448 drawT(tpain, 0, dim.ascent());
1450 // reset metrics cache to "real" values
1452 return tpain.textheight();
1454 odocstringstream oss;
1455 WriteStream wi(oss, false, true);
1458 docstring const str = oss.str();
1465 int InsetMathHull::docbook(Buffer const & buf, odocstream & os,
1466 OutputParams const & runparams) const
1471 if (getType() == hullSimple)
1472 name = from_ascii("inlineequation");
1474 name = from_ascii("informalequation");
1476 docstring bname = name;
1477 if (!label(0).empty())
1478 bname += " id='" + sgml::cleanID(buf, runparams, label(0)) + "'";
1481 odocstringstream ls;
1482 if (runparams.flavor == OutputParams::XML) {
1483 ms << MTag(from_ascii("alt role='tex' "));
1484 // Workaround for db2latex: db2latex always includes equations with
1485 // \ensuremath{} or \begin{display}\end{display}
1486 // so we strip LyX' math environment
1487 WriteStream wi(ls, false, false);
1488 InsetMathGrid::write(wi);
1489 ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&"), "<", "<"));
1490 ms << ETag(from_ascii("alt"));
1491 ms << MTag(from_ascii("math"));
1492 ms << ETag(from_ascii("alt"));
1493 ms << MTag(from_ascii("math"));
1494 InsetMathGrid::mathmlize(ms);
1495 ms << ETag(from_ascii("math"));
1497 ms << MTag(from_ascii("alt role='tex'"));
1498 res = latex(buf, ls, runparams);
1499 ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&"), "<", "<"));
1500 ms << ETag(from_ascii("alt"));
1503 ms << from_ascii("<graphic fileref=\"eqn/");
1504 if (!label(0).empty())
1505 ms << sgml::cleanID(buf, runparams, label(0));
1507 ms << sgml::uniqueID(from_ascii("anon"));
1509 if (runparams.flavor == OutputParams::XML)
1510 ms << from_ascii("\"/>");
1512 ms << from_ascii("\">");
1515 return ms.line() + res;
1519 void InsetMathHull::textString(Buffer const & buf, odocstream & os) const
1521 plaintext(buf, os, OutputParams(0));