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 "DispatchResult.h"
36 #include "FuncRequest.h"
38 #include "OutputParams.h"
39 #include "ParIterator.h"
42 #include "TextPainter.h"
43 #include "TocBackend.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/debug.h"
54 #include "support/gettext.h"
55 #include "support/lstrings.h"
60 using namespace lyx::support;
64 using cap::grabAndEraseSelection;
68 int getCols(HullType type)
85 // returns position of first relation operator in the array
86 // used for "intelligent splitting"
87 size_t firstRelOp(MathData const & ar)
89 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
91 return it - ar.begin();
96 char const * star(bool numbered)
98 return numbered ? "" : "*";
102 } // end anon namespace
105 HullType hullType(docstring const & s)
107 if (s == "none") return hullNone;
108 if (s == "simple") return hullSimple;
109 if (s == "equation") return hullEquation;
110 if (s == "eqnarray") return hullEqnArray;
111 if (s == "align") return hullAlign;
112 if (s == "alignat") return hullAlignAt;
113 if (s == "xalignat") return hullXAlignAt;
114 if (s == "xxalignat") return hullXXAlignAt;
115 if (s == "multline") return hullMultline;
116 if (s == "gather") return hullGather;
117 if (s == "flalign") return hullFlAlign;
118 lyxerr << "unknown hull type '" << to_utf8(s) << "'" << endl;
123 docstring hullName(HullType type)
126 case hullNone: return from_ascii("none");
127 case hullSimple: return from_ascii("simple");
128 case hullEquation: return from_ascii("equation");
129 case hullEqnArray: return from_ascii("eqnarray");
130 case hullAlign: return from_ascii("align");
131 case hullAlignAt: return from_ascii("alignat");
132 case hullXAlignAt: return from_ascii("xalignat");
133 case hullXXAlignAt: return from_ascii("xxalignat");
134 case hullMultline: return from_ascii("multline");
135 case hullGather: return from_ascii("gather");
136 case hullFlAlign: return from_ascii("flalign");
138 lyxerr << "unknown hull type '" << type << "'" << endl;
139 return from_ascii("none");
143 static InsetLabel * dummy_pointer = 0;
145 InsetMathHull::InsetMathHull()
146 : InsetMathGrid(1, 1), type_(hullNone), nonum_(1, false), label_(1, dummy_pointer),
147 preview_(new RenderPreview(this))
149 //lyxerr << "sizeof InsetMath: " << sizeof(InsetMath) << endl;
150 //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl;
151 //lyxerr << "sizeof InsetMathChar: " << sizeof(InsetMathChar) << endl;
152 //lyxerr << "sizeof FontInfo: " << sizeof(FontInfo) << endl;
158 InsetMathHull::InsetMathHull(HullType type)
159 : InsetMathGrid(getCols(type), 1), type_(type), nonum_(1, false), label_(1, dummy_pointer),
160 preview_(new RenderPreview(this))
167 InsetMathHull::InsetMathHull(InsetMathHull const & other)
173 InsetMathHull::~InsetMathHull()
175 for (size_t i = 0; i < label_.size(); ++i) {
182 Inset * InsetMathHull::clone() const
184 return new InsetMathHull(*this);
188 InsetMathHull & InsetMathHull::operator=(InsetMathHull const & other)
192 InsetMathGrid::operator=(other);
194 nonum_ = other.nonum_;
195 for (size_t i = 0; i < label_.size(); ++i) {
199 label_ = other.label_;
200 for (size_t i = 0; i != label_.size(); ++i) {
202 label_[i] = new InsetLabel(*label_[i]);
204 preview_.reset(new RenderPreview(*other.preview_, this));
210 void InsetMathHull::setBuffer(Buffer & buffer)
213 for (idx_type i = 0, n = nargs(); i != n; ++i) {
214 MathData & data = cell(i);
215 for (size_t j = 0; j != data.size(); ++j)
216 data[j].nucleus()->setBuffer(buffer);
219 for (size_t i = 0; i != label_.size(); ++i) {
221 label_[i]->setBuffer(buffer);
226 void InsetMathHull::updateLabels(ParIterator const & it)
229 //FIXME: buffer_ should be set at creation for this inset! Problem is
230 // This inset is created at too many places (see Parser::parse1() in
234 for (size_t i = 0; i != label_.size(); ++i) {
236 label_[i]->updateLabels(it);
241 void InsetMathHull::addToToc(ParConstIterator const & pit) const
244 //FIXME: buffer_ should be set at creation for this inset! Problem is
245 // This inset is created at too many places (see Parser::parse1() in
250 // FIXME: it would be way better to directly use InsetLabel instead of this
251 // label list. But it should be possible to copy&paste the code in
252 // InsetLabel::addToToc() anyway.
254 Toc & toc = buffer().tocBackend().toc("equation");
256 for (row_type row = 0; row != nrows(); ++row) {
260 label_[row]->addToToc(pit);
261 toc.push_back(TocItem(pit, 0, nicelabel(row)));
266 Inset * InsetMathHull::editXY(Cursor & cur, int x, int y)
272 return InsetMathNest::editXY(cur, x, y);
276 InsetMath::mode_type InsetMathHull::currentMode() const
278 if (type_ == hullNone)
279 return UNDECIDED_MODE;
280 // definitely math mode ...
285 bool InsetMathHull::idxFirst(Cursor & cur) const
293 bool InsetMathHull::idxLast(Cursor & cur) const
295 cur.idx() = nargs() - 1;
296 cur.pos() = cur.lastpos();
301 char InsetMathHull::defaultColAlign(col_type col)
303 if (type_ == hullEqnArray)
305 if (type_ == hullGather)
307 if (type_ >= hullAlign)
308 return "rl"[col & 1];
313 int InsetMathHull::defaultColSpace(col_type col)
315 if (type_ == hullAlign || type_ == hullAlignAt)
317 if (type_ == hullXAlignAt)
318 return (col & 1) ? 20 : 0;
319 if (type_ == hullXXAlignAt || type_ == hullFlAlign)
320 return (col & 1) ? 40 : 0;
325 docstring InsetMathHull::standardFont() const
327 return from_ascii(type_ == hullNone ? "lyxnochange" : "mathnormal");
331 bool InsetMathHull::previewState(BufferView * bv) const
333 if (!editing(bv) && RenderPreview::status() == LyXRC::PREVIEW_ON) {
334 graphics::PreviewImage const * pimage =
335 preview_->getPreviewImage(bv->buffer());
336 return pimage && pimage->image();
342 void InsetMathHull::metrics(MetricsInfo & mi, Dimension & dim) const
344 if (previewState(mi.base.bv)) {
345 preview_->metrics(mi, dim);
346 // insert a one pixel gap in front of the formula
349 dim.des += displayMargin();
350 // Cache the inset dimension.
351 setDimCache(mi, dim);
355 FontSetChanger dummy1(mi.base, standardFont());
356 StyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
358 // let the cells adjust themselves
359 InsetMathGrid::metrics(mi, dim);
362 dim.asc += displayMargin();
363 dim.des += displayMargin();
366 if (numberedType()) {
367 FontSetChanger dummy(mi.base, from_ascii("mathbf"));
369 for (row_type row = 0; row < nrows(); ++row)
370 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
376 // make it at least as high as the current font
379 math_font_max_dim(mi.base.font, asc, des);
380 dim.asc = max(dim.asc, asc);
381 dim.des = max(dim.des, des);
382 // Cache the inset dimension.
383 // FIXME: This will overwrite InsetMathGrid dimension, is that OK?
384 setDimCache(mi, dim);
388 void InsetMathHull::draw(PainterInfo & pi, int x, int y) const
390 use_preview_ = previewState(pi.base.bv);
391 Dimension const dim = dimension(*pi.base.bv);
393 // background of mathed under focus is not painted because
394 // selection at the top level of nested inset is difficult to handle.
395 if (!editing(pi.base.bv))
396 pi.pain.fillRectangle(x + 1, y - dim.asc + 1, dim.wid - 2,
397 dim.asc + dim.des - 1, Color_mathbg);
400 // one pixel gap in front
401 preview_->draw(pi, x + 1, y);
402 setPosCache(pi, x, y);
406 FontSetChanger dummy1(pi.base, standardFont());
407 StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
408 InsetMathGrid::draw(pi, x + 1, y);
410 if (numberedType()) {
411 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
412 for (row_type row = 0; row < nrows(); ++row) {
413 int const yy = y + rowinfo_[row].offset_;
414 FontSetChanger dummy(pi.base, from_ascii("mathrm"));
415 docstring const nl = nicelabel(row);
419 setPosCache(pi, x, y);
423 void InsetMathHull::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
426 InsetMathGrid::metricsT(mi, dim);
429 WriteStream wi(os, false, true);
431 dim.wid = os.str().size();
438 void InsetMathHull::drawT(TextPainter & pain, int x, int y) const
441 InsetMathGrid::drawT(pain, x, y);
444 WriteStream wi(os, false, true);
446 pain.draw(x, y, os.str().c_str());
453 docstring const latex_string(InsetMathHull const & inset)
456 WriteStream wi(ls, false, false);
464 void InsetMathHull::addPreview(graphics::PreviewLoader & ploader) const
466 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
467 docstring const snippet = latex_string(*this);
468 preview_->addPreview(snippet, ploader);
473 bool InsetMathHull::notifyCursorLeaves(Cursor const & /*old*/, Cursor & cur)
475 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
476 Buffer const & buffer = cur.buffer();
477 docstring const snippet = latex_string(*this);
478 preview_->addPreview(snippet, buffer);
479 preview_->startLoading(buffer);
480 cur.updateFlags(Update::Force);
486 docstring InsetMathHull::label(row_type row) const
488 BOOST_ASSERT(row < nrows());
489 if (InsetLabel * il = label_[row])
490 return il->screenLabel();
495 void InsetMathHull::label(row_type row, docstring const & label)
497 //lyxerr << "setting label '" << label << "' for row " << row << endl;
499 label_[row]->updateCommand(label);
502 InsetCommandParams p(LABEL_CODE);
504 label_[row] = new InsetLabel(p);
506 label_[row]->setBuffer(buffer());
510 void InsetMathHull::numbered(row_type row, bool num)
513 if (nonum_[row] && label_[row]) {
520 bool InsetMathHull::numbered(row_type row) const
526 bool InsetMathHull::ams() const
529 type_ == hullAlign ||
530 type_ == hullFlAlign ||
531 type_ == hullMultline ||
532 type_ == hullGather ||
533 type_ == hullAlignAt ||
534 type_ == hullXAlignAt ||
535 type_ == hullXXAlignAt;
539 Inset::DisplayType InsetMathHull::display() const
541 return (type_ != hullSimple && type_ != hullNone) ? AlignCenter : Inline;
545 void InsetMathHull::getLabelList(vector<docstring> & labels) const
547 for (row_type row = 0; row < nrows(); ++row)
548 if (label_[row] && !nonum_[row])
549 labels.push_back(label_[row]->screenLabel());
553 bool InsetMathHull::numberedType() const
555 if (type_ == hullNone)
557 if (type_ == hullSimple)
559 if (type_ == hullXXAlignAt)
561 for (row_type row = 0; row < nrows(); ++row)
568 void InsetMathHull::validate(LaTeXFeatures & features) const
571 features.require("amsmath");
574 // Validation is necessary only if not using AMS math.
575 // To be safe, we will always run mathedvalidate.
576 //if (features.amsstyle)
579 features.require("boldsymbol");
580 //features.binom = true;
582 InsetMathGrid::validate(features);
586 void InsetMathHull::header_write(WriteStream & os) const
588 bool n = numberedType();
602 os << "\\begin{equation" << star(n) << "}\n";
612 os << "\\begin{" << hullName(type_) << star(n) << "}\n";
617 os << "\\begin{" << hullName(type_) << star(n) << '}'
618 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
622 os << "\\begin{" << hullName(type_) << '}'
623 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
627 os << "\\begin{unknown" << star(n) << '}';
633 void InsetMathHull::footer_write(WriteStream & os) const
635 bool n = numberedType();
648 os << "\\end{equation" << star(n) << "}\n";
660 os << "\\end{" << hullName(type_) << star(n) << "}\n";
664 os << "\\end{" << hullName(type_) << "}\n";
668 os << "\\end{unknown" << star(n) << '}';
674 bool InsetMathHull::rowChangeOK() const
677 type_ == hullEqnArray || type_ == hullAlign ||
678 type_ == hullFlAlign || type_ == hullAlignAt ||
679 type_ == hullXAlignAt || type_ == hullXXAlignAt ||
680 type_ == hullGather || type_ == hullMultline;
684 bool InsetMathHull::colChangeOK() const
687 type_ == hullAlign || type_ == hullFlAlign ||type_ == hullAlignAt ||
688 type_ == hullXAlignAt || type_ == hullXXAlignAt;
692 void InsetMathHull::addRow(row_type row)
696 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
697 label_.insert(label_.begin() + row + 1, dummy_pointer);
698 InsetMathGrid::addRow(row);
702 void InsetMathHull::swapRow(row_type row)
706 if (row + 1 == nrows())
708 // gcc doesn't like this:
709 // swap(nonum_[row], nonum_[row + 1]);
710 // so we do it manually:
711 bool const b = nonum_[row];
712 nonum_[row] = nonum_[row + 1];
714 swap(label_[row], label_[row + 1]);
715 InsetMathGrid::swapRow(row);
719 void InsetMathHull::delRow(row_type row)
721 if (nrows() <= 1 || !rowChangeOK())
723 InsetMathGrid::delRow(row);
724 // The last dummy row has no number info nor a label.
725 // Test nrows() + 1 because we have already erased the row.
726 if (row == nrows() + 1)
728 nonum_.erase(nonum_.begin() + row);
731 label_.erase(label_.begin() + row);
735 void InsetMathHull::addCol(col_type col)
739 InsetMathGrid::addCol(col);
743 void InsetMathHull::delCol(col_type col)
745 if (ncols() <= 1 || !colChangeOK())
747 InsetMathGrid::delCol(col);
751 docstring InsetMathHull::nicelabel(row_type row) const
756 return from_ascii("(#)");
757 return '(' + label_[row]->screenLabel() + ')';
761 void InsetMathHull::glueall()
764 for (idx_type i = 0; i < nargs(); ++i)
766 *this = InsetMathHull(hullSimple);
772 void InsetMathHull::splitTo2Cols()
774 BOOST_ASSERT(ncols() == 1);
775 InsetMathGrid::addCol(1);
776 for (row_type row = 0; row < nrows(); ++row) {
777 idx_type const i = 2 * row;
778 pos_type pos = firstRelOp(cell(i));
779 cell(i + 1) = MathData(cell(i).begin() + pos, cell(i).end());
780 cell(i).erase(pos, cell(i).size());
785 void InsetMathHull::splitTo3Cols()
787 BOOST_ASSERT(ncols() < 3);
790 InsetMathGrid::addCol(2);
791 for (row_type row = 0; row < nrows(); ++row) {
792 idx_type const i = 3 * row + 1;
793 if (cell(i).size()) {
794 cell(i + 1) = MathData(cell(i).begin() + 1, cell(i).end());
795 cell(i).erase(1, cell(i).size());
801 void InsetMathHull::changeCols(col_type cols)
805 else if (ncols() < cols) {
811 while (ncols() < cols)
812 InsetMathGrid::addCol(ncols());
818 for (row_type row = 0; row < nrows(); ++row) {
819 idx_type const i = row * ncols();
820 for (col_type col = cols; col < ncols(); ++col) {
821 cell(i + cols - 1).append(cell(i + col));
825 while (ncols() > cols) {
826 InsetMathGrid::delCol(ncols() - 1);
831 HullType InsetMathHull::getType() const
837 void InsetMathHull::setType(HullType type)
844 void InsetMathHull::mutate(HullType newtype)
846 //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
848 // we try to move along the chain
849 // none <-> simple <-> equation <-> eqnarray -> *align* -> multline, gather -+
851 // +-------------------------------------+
852 // we use eqnarray as intermediate type for mutations that are not
853 // directly supported because it handles labels and numbering for
856 if (newtype == type_) {
860 else if (newtype < hullNone) {
865 else if (type_ == hullNone) {
871 else if (type_ == hullSimple) {
872 if (newtype == hullNone) {
876 setType(hullEquation);
882 else if (type_ == hullEquation) {
883 if (newtype < type_) {
887 } else if (newtype == hullEqnArray) {
888 // split it "nicely" on the first relop
890 setType(hullEqnArray);
891 } else if (newtype == hullMultline || newtype == hullGather) {
901 else if (type_ == hullEqnArray) {
902 if (newtype < type_) {
903 // set correct (no)numbering
905 for (row_type row = 0; row < nrows(); ++row) {
912 // set first non-empty label
913 for (row_type row = 0; row < nrows(); ++row) {
915 label_[0] = label_[row];
922 } else { // align & Co.
929 else if (type_ == hullAlign || type_ == hullAlignAt ||
930 type_ == hullXAlignAt || type_ == hullFlAlign) {
931 if (newtype < hullAlign) {
933 setType(hullEqnArray);
935 } else if (newtype == hullGather || newtype == hullMultline) {
938 } else if (newtype == hullXXAlignAt) {
939 for (row_type row = 0; row < nrows(); ++row)
940 numbered(row, false);
947 else if (type_ == hullXXAlignAt) {
948 for (row_type row = 0; row < nrows(); ++row)
949 numbered(row, false);
950 if (newtype < hullAlign) {
952 setType(hullEqnArray);
954 } else if (newtype == hullGather || newtype == hullMultline) {
962 else if (type_ == hullMultline || type_ == hullGather) {
963 if (newtype == hullGather || newtype == hullMultline)
965 else if (newtype == hullAlign || newtype == hullFlAlign ||
966 newtype == hullAlignAt || newtype == hullXAlignAt) {
969 } else if (newtype == hullXXAlignAt) {
971 for (row_type row = 0; row < nrows(); ++row)
972 numbered(row, false);
976 setType(hullEqnArray);
982 lyxerr << "mutation from '" << to_utf8(hullName(type_))
983 << "' to '" << to_utf8(hullName(newtype))
984 << "' not implemented" << endl;
989 docstring InsetMathHull::eolString(row_type row, bool emptyline, bool fragile) const
992 if (numberedType()) {
993 if (label_[row] && !nonum_[row])
994 res += "\\label{" + label_[row]->getParam("name") + '}';
995 if (nonum_[row] && (type_ != hullMultline))
996 res += "\\nonumber ";
998 return res + InsetMathGrid::eolString(row, emptyline, fragile);
1002 void InsetMathHull::write(WriteStream & os) const
1005 InsetMathGrid::write(os);
1010 void InsetMathHull::normalize(NormalStream & os) const
1012 os << "[formula " << hullName(type_) << ' ';
1013 InsetMathGrid::normalize(os);
1018 void InsetMathHull::mathmlize(MathStream & os) const
1020 InsetMathGrid::mathmlize(os);
1024 void InsetMathHull::infoize(odocstream & os) const
1026 os << "Type: " << hullName(type_);
1030 void InsetMathHull::check() const
1032 BOOST_ASSERT(nonum_.size() == nrows());
1033 BOOST_ASSERT(label_.size() == nrows());
1037 void InsetMathHull::doExtern(Cursor & cur, FuncRequest & func)
1041 idocstringstream iss(func.argument());
1042 iss >> dlang >> extra;
1044 extra = from_ascii("noextra");
1045 string const lang = to_ascii(dlang);
1047 // FIXME: temporarily disabled
1048 //if (cur.selection()) {
1051 // lyxerr << "use selection: " << ar << endl;
1052 // insert(pipeThroughExtern(lang, extra, ar));
1057 eq.push_back(MathAtom(new InsetMathChar('=')));
1059 // go to first item in line
1060 cur.idx() -= cur.idx() % ncols();
1063 if (getType() == hullSimple) {
1064 size_type pos = cur.cell().find_last(eq);
1066 if (cur.inMathed() && cur.selection()) {
1067 asArray(grabAndEraseSelection(cur), ar);
1068 } else if (pos == cur.cell().size()) {
1070 lyxerr << "use whole cell: " << ar << endl;
1072 ar = MathData(cur.cell().begin() + pos + 1, cur.cell().end());
1073 lyxerr << "use partial cell form pos: " << pos << endl;
1075 cur.cell().append(eq);
1076 cur.cell().append(pipeThroughExtern(lang, extra, ar));
1077 cur.pos() = cur.lastpos();
1081 if (getType() == hullEquation) {
1082 lyxerr << "use equation inset" << endl;
1083 mutate(hullEqnArray);
1084 MathData & ar = cur.cell();
1085 lyxerr << "use cell: " << ar << endl;
1089 cur.cell() = pipeThroughExtern(lang, extra, ar);
1090 // move to end of line
1091 cur.pos() = cur.lastpos();
1096 lyxerr << "use eqnarray" << endl;
1097 cur.idx() += 2 - cur.idx() % ncols();
1099 MathData ar = cur.cell();
1100 lyxerr << "use cell: " << ar << endl;
1101 // FIXME: temporarily disabled
1107 cur.cell() = pipeThroughExtern(lang, extra, ar);
1108 cur.pos() = cur.lastpos();
1113 void InsetMathHull::doDispatch(Cursor & cur, FuncRequest & cmd)
1115 //lyxerr << "action: " << cmd.action << endl;
1116 switch (cmd.action) {
1118 case LFUN_FINISHED_BACKWARD:
1119 case LFUN_FINISHED_FORWARD:
1120 case LFUN_FINISHED_RIGHT:
1121 case LFUN_FINISHED_LEFT:
1122 //lyxerr << "action: " << cmd.action << endl;
1123 InsetMathGrid::doDispatch(cur, cmd);
1127 case LFUN_BREAK_PARAGRAPH:
1128 // just swallow this
1132 // some magic for the common case
1133 if (type_ == hullSimple || type_ == hullEquation) {
1134 cur.recordUndoInset();
1136 cur.bv().buffer().params().use_amsmath == BufferParams::package_on;
1137 mutate(align ? hullAlign : hullEqnArray);
1138 cur.idx() = nrows() * ncols() - 1;
1139 cur.pos() = cur.lastpos();
1141 InsetMathGrid::doDispatch(cur, cmd);
1144 case LFUN_MATH_NUMBER_TOGGLE: {
1145 //lyxerr << "toggling all numbers" << endl;
1146 cur.recordUndoInset();
1147 bool old = numberedType();
1148 if (type_ == hullMultline)
1149 numbered(nrows() - 1, !old);
1151 for (row_type row = 0; row < nrows(); ++row)
1152 numbered(row, !old);
1154 cur.message(old ? _("No number") : _("Number"));
1158 case LFUN_MATH_NUMBER_LINE_TOGGLE: {
1159 cur.recordUndoInset();
1160 row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1161 bool old = numbered(r);
1162 cur.message(old ? _("No number") : _("Number"));
1167 case LFUN_LABEL_INSERT: {
1168 cur.recordUndoInset();
1169 row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1170 docstring old_label = label(r);
1171 docstring const default_label = from_ascii(
1172 (lyxrc.label_init_length >= 0) ? "eq:" : "");
1173 if (old_label.empty())
1174 old_label = default_label;
1176 InsetCommandParams p(LABEL_CODE);
1177 p["name"] = cmd.argument().empty() ? old_label : cmd.argument();
1178 string const data = InsetCommandMailer::params2string("label", p);
1180 if (cmd.argument().empty())
1181 cur.bv().showDialog("label", data);
1183 FuncRequest fr(LFUN_INSET_INSERT, data);
1189 case LFUN_INSET_INSERT: {
1190 //lyxerr << "arg: " << to_utf8(cmd.argument()) << endl;
1191 // FIXME: this should be cleaned up to use InsetLabel methods directly.
1192 string const name = cmd.getArg(0);
1193 if (name == "label") {
1194 InsetCommandParams p(LABEL_CODE);
1195 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), p);
1196 docstring str = p["name"];
1197 cur.recordUndoInset();
1198 row_type const r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1202 docstring old = label(r);
1205 // The label will take care of the reference update.
1209 // Newly created inset so initialize it.
1210 label_[r]->initView();
1215 InsetMathGrid::doDispatch(cur, cmd);
1219 case LFUN_MATH_EXTERN:
1220 cur.recordUndoInset();
1224 case LFUN_MATH_MUTATE: {
1225 cur.recordUndoInset();
1226 row_type row = cur.row();
1227 col_type col = cur.col();
1228 mutate(hullType(cmd.argument()));
1229 cur.idx() = row * ncols() + col;
1230 if (cur.idx() > cur.lastidx()) {
1231 cur.idx() = cur.lastidx();
1232 cur.pos() = cur.lastpos();
1234 if (cur.pos() > cur.lastpos())
1235 cur.pos() = cur.lastpos();
1237 // FIXME: find some more clever handling of the selection,
1238 // i.e. preserve it.
1239 cur.clearSelection();
1240 //cur.dispatched(FINISHED);
1244 case LFUN_MATH_DISPLAY: {
1245 cur.recordUndoInset();
1246 mutate(type_ == hullSimple ? hullEquation : hullSimple);
1248 cur.pos() = cur.lastpos();
1249 //cur.dispatched(FINISHED);
1254 InsetMathGrid::doDispatch(cur, cmd);
1260 bool InsetMathHull::getStatus(Cursor & cur, FuncRequest const & cmd,
1261 FuncStatus & status) const
1263 switch (cmd.action) {
1264 case LFUN_FINISHED_BACKWARD:
1265 case LFUN_FINISHED_FORWARD:
1266 case LFUN_FINISHED_RIGHT:
1267 case LFUN_FINISHED_LEFT:
1271 case LFUN_MATH_EXTERN:
1272 case LFUN_MATH_MUTATE:
1273 case LFUN_MATH_DISPLAY:
1275 status.enabled(true);
1277 case LFUN_MATH_NUMBER_TOGGLE:
1278 // FIXME: what is the right test, this or the one of
1280 status.enabled(display());
1281 status.setOnOff(numberedType());
1283 case LFUN_MATH_NUMBER_LINE_TOGGLE: {
1284 // FIXME: what is the right test, this or the one of
1286 status.enabled(display());
1287 row_type const r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1288 status.setOnOff(numbered(r));
1291 case LFUN_LABEL_INSERT:
1292 status.enabled(type_ != hullSimple);
1294 case LFUN_INSET_INSERT:
1295 if (cmd.getArg(0) == "label") {
1296 status.enabled(type_ != hullSimple);
1299 return InsetMathGrid::getStatus(cur, cmd, status);
1300 case LFUN_TABULAR_FEATURE: {
1301 istringstream is(to_utf8(cmd.argument()));
1305 && (s == "append-row"
1306 || s == "delete-row"
1307 || s == "copy-row")) {
1308 status.message(bformat(
1309 from_utf8(N_("Can't change number of rows in '%1$s'")),
1311 status.enabled(false);
1315 && (s == "append-column"
1316 || s == "delete-column"
1317 || s == "copy-column")) {
1318 status.message(bformat(
1319 from_utf8(N_("Can't change number of columns in '%1$s'")),
1321 status.enabled(false);
1324 if ((type_ == hullSimple
1325 || type_ == hullEquation
1326 || type_ == hullNone) &&
1327 (s == "add-hline-above" || s == "add-hline-below")) {
1328 status.message(bformat(
1329 from_utf8(N_("Can't add horizontal grid lines in '%1$s'")),
1331 status.enabled(false);
1334 if (s == "add-vline-left" || s == "add-vline-right") {
1335 status.message(bformat(
1336 from_utf8(N_("Can't add vertical grid lines in '%1$s'")),
1338 status.enabled(false);
1341 if (s == "valign-top" || s == "valign-middle"
1342 || s == "valign-bottom" || s == "align-left"
1343 || s == "align-center" || s == "align-right") {
1344 status.enabled(false);
1347 return InsetMathGrid::getStatus(cur, cmd, status);
1350 return InsetMathGrid::getStatus(cur, cmd, status);
1353 // This cannot really happen, but inserted to shut-up gcc
1354 return InsetMathGrid::getStatus(cur, cmd, status);
1358 /////////////////////////////////////////////////////////////////////
1362 // simply scrap this function if you want
1363 void InsetMathHull::mutateToText()
1366 // translate to latex
1368 latex(NULL, os, false, false);
1369 string str = os.str();
1372 Text * lt = view_->cursor().innerText();
1373 string::const_iterator cit = str.begin();
1374 string::const_iterator end = str.end();
1375 for (; cit != end; ++cit)
1376 view_->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
1379 //dispatch(LFUN_ESCAPE);
1384 void InsetMathHull::handleFont(Cursor & cur, docstring const & arg,
1385 docstring const & font)
1387 // this whole function is a hack and won't work for incremental font
1390 if (cur.inset().asInsetMath()->name() == font)
1391 cur.handleFont(to_utf8(font));
1393 cur.handleNest(createInsetMath(font));
1399 void InsetMathHull::handleFont2(Cursor & cur, docstring const & arg)
1404 font.fromString(to_utf8(arg), b);
1405 if (font.fontInfo().color() != Color_inherit) {
1406 MathAtom at = MathAtom(new InsetMathColor(true, font.fontInfo().color()));
1407 cur.handleNest(at, 0);
1412 void InsetMathHull::edit(Cursor & cur, bool front, EntryDirection entry_from)
1415 bool enter_front = (entry_from == Inset::ENTRY_DIRECTION_LEFT ||
1416 (entry_from == Inset::ENTRY_DIRECTION_IGNORE && front));
1417 enter_front ? idxFirst(cur) : idxLast(cur);
1418 // The inset formula dimension is not necessarily the same as the
1419 // one of the instant preview image, so we have to indicate to the
1420 // BufferView that a metrics update is needed.
1421 cur.updateFlags(Update::Force);
1425 docstring InsetMathHull::editMessage() const
1427 return _("Math editor mode");
1431 void InsetMathHull::revealCodes(Cursor & cur) const
1433 if (!cur.inMathed())
1435 odocstringstream os;
1437 cur.message(os.str());
1439 // write something to the minibuffer
1440 // translate to latex
1444 string str = os.str();
1446 string::size_type pos = 0;
1448 for (string::iterator it = str.begin(); it != str.end(); ++it) {
1451 else if (*it == '\0') {
1453 pos = it - str.begin();
1459 res = res.substr(pos - 30);
1460 if (res.size() > 60)
1461 res = res.substr(0, 60);
1467 InsetCode InsetMathHull::lyxCode() const
1473 /////////////////////////////////////////////////////////////////////
1477 bool InsetMathHull::searchForward(BufferView * bv, string const & str,
1480 // FIXME: completely broken
1481 static InsetMathHull * lastformula = 0;
1482 static CursorBase current = DocIterator(ibegin(nucleus()));
1484 static string laststr;
1486 if (lastformula != this || laststr != str) {
1487 //lyxerr << "reset lastformula to " << this << endl;
1490 current = ibegin(nucleus());
1492 mathed_parse_cell(ar, str);
1496 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
1498 for (DocIterator it = current; it != iend(nucleus()); increment(it)) {
1499 CursorSlice & top = it.back();
1500 MathData const & a = top.asInsetMath()->cell(top.idx_);
1501 if (a.matchpart(ar, top.pos_)) {
1502 bv->cursor().setSelection(it, ar.size());
1504 top.pos_ += ar.size();
1510 //lyxerr << "not found!" << endl;
1517 void InsetMathHull::write(ostream & os) const
1519 odocstringstream oss;
1520 WriteStream wi(oss, false, false);
1523 os << to_utf8(oss.str());
1527 void InsetMathHull::read(Lexer & lex)
1530 mathed_parse_normal(at, lex);
1531 operator=(*at->asHullInset());
1535 int InsetMathHull::plaintext(odocstream & os, OutputParams const &) const
1537 if (0 && display()) {
1541 TextPainter tpain(dim.width(), dim.height());
1542 drawT(tpain, 0, dim.ascent());
1544 // reset metrics cache to "real" values
1546 return tpain.textheight();
1548 odocstringstream oss;
1549 WriteStream wi(oss, false, true);
1552 docstring const str = oss.str();
1559 int InsetMathHull::docbook(odocstream & os, OutputParams const & runparams) const
1564 if (getType() == hullSimple)
1565 name = from_ascii("inlineequation");
1567 name = from_ascii("informalequation");
1569 docstring bname = name;
1570 if (!label(0).empty())
1571 bname += " id='" + sgml::cleanID(buffer(), runparams, label(0)) + "'";
1573 ++ms.tab(); ms.cr(); ms.os() << '<' << bname << '>';
1575 odocstringstream ls;
1576 if (runparams.flavor == OutputParams::XML) {
1577 ms << MTag("alt role='tex' ");
1578 // Workaround for db2latex: db2latex always includes equations with
1579 // \ensuremath{} or \begin{display}\end{display}
1580 // so we strip LyX' math environment
1581 WriteStream wi(ls, false, false);
1582 InsetMathGrid::write(wi);
1583 ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&"), "<", "<"));
1588 InsetMathGrid::mathmlize(ms);
1591 ms << MTag("alt role='tex'");
1592 res = latex(ls, runparams);
1593 ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&"), "<", "<"));
1597 ms << from_ascii("<graphic fileref=\"eqn/");
1598 if (!label(0).empty())
1599 ms << sgml::cleanID(buffer(), runparams, label(0));
1601 ms << sgml::uniqueID(from_ascii("anon"));
1603 if (runparams.flavor == OutputParams::XML)
1604 ms << from_ascii("\"/>");
1606 ms << from_ascii("\">");
1608 ms.cr(); --ms.tab(); ms.os() << "</" << name << '>';
1610 return ms.line() + res;
1614 void InsetMathHull::textString(odocstream & os) const
1616 plaintext(os, OutputParams(0));