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)
180 Inset * InsetMathHull::clone() const
182 return new InsetMathHull(*this);
186 InsetMathHull & InsetMathHull::operator=(InsetMathHull const & other)
190 InsetMathGrid::operator=(other);
192 nonum_ = other.nonum_;
193 for (size_t i = 0; i < label_.size(); ++i)
195 label_ = other.label_;
196 for (size_t i = 0; i != label_.size(); ++i) {
198 label_[i] = new InsetLabel(*label_[i]);
200 preview_.reset(new RenderPreview(*other.preview_, this));
206 void InsetMathHull::setBuffer(Buffer & buffer)
209 for (idx_type i = 0, n = nargs(); i != n; ++i) {
210 MathData & data = cell(i);
211 for (size_t j = 0; j != data.size(); ++j)
212 data[j].nucleus()->setBuffer(buffer);
215 for (size_t i = 0; i != label_.size(); ++i) {
217 label_[i]->setBuffer(buffer);
222 void InsetMathHull::updateLabels(ParIterator const & it)
225 //FIXME: buffer_ should be set at creation for this inset! Problem is
226 // This inset is created at too many places (see Parser::parse1() in
230 for (size_t i = 0; i != label_.size(); ++i) {
232 label_[i]->updateLabels(it);
237 void InsetMathHull::addToToc(ParConstIterator const & pit) const
240 //FIXME: buffer_ should be set at creation for this inset! Problem is
241 // This inset is created at too many places (see Parser::parse1() in
246 // FIXME: it would be way better to directly use InsetLabel instead of this
247 // label list. But it should be possible to copy&paste the code in
248 // InsetLabel::addToToc() anyway.
250 Toc & toc = buffer().tocBackend().toc("equation");
252 for (row_type row = 0; row != nrows(); ++row) {
256 label_[row]->addToToc(pit);
257 toc.push_back(TocItem(pit, 0, nicelabel(row)));
262 Inset * InsetMathHull::editXY(Cursor & cur, int x, int y)
268 return InsetMathNest::editXY(cur, x, y);
272 InsetMath::mode_type InsetMathHull::currentMode() const
274 if (type_ == hullNone)
275 return UNDECIDED_MODE;
276 // definitely math mode ...
281 bool InsetMathHull::idxFirst(Cursor & cur) const
289 bool InsetMathHull::idxLast(Cursor & cur) const
291 cur.idx() = nargs() - 1;
292 cur.pos() = cur.lastpos();
297 char InsetMathHull::defaultColAlign(col_type col)
299 if (type_ == hullEqnArray)
301 if (type_ == hullGather)
303 if (type_ >= hullAlign)
304 return "rl"[col & 1];
309 int InsetMathHull::defaultColSpace(col_type col)
311 if (type_ == hullAlign || type_ == hullAlignAt)
313 if (type_ == hullXAlignAt)
314 return (col & 1) ? 20 : 0;
315 if (type_ == hullXXAlignAt || type_ == hullFlAlign)
316 return (col & 1) ? 40 : 0;
321 docstring InsetMathHull::standardFont() const
323 return from_ascii(type_ == hullNone ? "lyxnochange" : "mathnormal");
327 bool InsetMathHull::previewState(BufferView * bv) const
329 if (!editing(bv) && RenderPreview::status() == LyXRC::PREVIEW_ON) {
330 graphics::PreviewImage const * pimage =
331 preview_->getPreviewImage(bv->buffer());
332 return pimage && pimage->image();
338 void InsetMathHull::metrics(MetricsInfo & mi, Dimension & dim) const
340 if (previewState(mi.base.bv)) {
341 preview_->metrics(mi, dim);
342 // insert a one pixel gap in front of the formula
345 dim.des += displayMargin();
346 // Cache the inset dimension.
347 setDimCache(mi, dim);
351 FontSetChanger dummy1(mi.base, standardFont());
352 StyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
354 // let the cells adjust themselves
355 InsetMathGrid::metrics(mi, dim);
358 dim.asc += displayMargin();
359 dim.des += displayMargin();
362 if (numberedType()) {
363 FontSetChanger dummy(mi.base, from_ascii("mathbf"));
365 for (row_type row = 0; row < nrows(); ++row)
366 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
372 // make it at least as high as the current font
375 math_font_max_dim(mi.base.font, asc, des);
376 dim.asc = max(dim.asc, asc);
377 dim.des = max(dim.des, des);
378 // Cache the inset dimension.
379 // FIXME: This will overwrite InsetMathGrid dimension, is that OK?
380 setDimCache(mi, dim);
384 void InsetMathHull::draw(PainterInfo & pi, int x, int y) const
386 use_preview_ = previewState(pi.base.bv);
387 Dimension const dim = dimension(*pi.base.bv);
389 // background of mathed under focus is not painted because
390 // selection at the top level of nested inset is difficult to handle.
391 if (!editing(pi.base.bv))
392 pi.pain.fillRectangle(x + 1, y - dim.asc + 1, dim.wid - 2,
393 dim.asc + dim.des - 1, Color_mathbg);
396 // one pixel gap in front
397 preview_->draw(pi, x + 1, y);
398 setPosCache(pi, x, y);
402 FontSetChanger dummy1(pi.base, standardFont());
403 StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
404 InsetMathGrid::draw(pi, x + 1, y);
406 if (numberedType()) {
407 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
408 for (row_type row = 0; row < nrows(); ++row) {
409 int const yy = y + rowinfo_[row].offset_;
410 FontSetChanger dummy(pi.base, from_ascii("mathrm"));
411 docstring const nl = nicelabel(row);
415 setPosCache(pi, x, y);
419 void InsetMathHull::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
422 InsetMathGrid::metricsT(mi, dim);
425 WriteStream wi(os, false, true);
427 dim.wid = os.str().size();
434 void InsetMathHull::drawT(TextPainter & pain, int x, int y) const
437 InsetMathGrid::drawT(pain, x, y);
440 WriteStream wi(os, false, true);
442 pain.draw(x, y, os.str().c_str());
449 docstring const latex_string(InsetMathHull const & inset)
452 WriteStream wi(ls, false, false);
460 void InsetMathHull::addPreview(graphics::PreviewLoader & ploader) const
462 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
463 docstring const snippet = latex_string(*this);
464 preview_->addPreview(snippet, ploader);
469 bool InsetMathHull::notifyCursorLeaves(Cursor const & /*old*/, Cursor & cur)
471 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
472 Buffer const & buffer = cur.buffer();
473 docstring const snippet = latex_string(*this);
474 preview_->addPreview(snippet, buffer);
475 preview_->startLoading(buffer);
476 cur.updateFlags(Update::Force);
482 docstring InsetMathHull::label(row_type row) const
484 BOOST_ASSERT(row < nrows());
485 if (InsetLabel * il = label_[row])
486 return il->screenLabel();
491 void InsetMathHull::label(row_type row, docstring const & label)
493 //lyxerr << "setting label '" << label << "' for row " << row << endl;
495 label_[row]->updateCommand(label);
498 InsetCommandParams p(LABEL_CODE);
500 label_[row] = new InsetLabel(p);
502 label_[row]->setBuffer(buffer());
506 void InsetMathHull::numbered(row_type row, bool num)
509 if (nonum_[row] && label_[row]) {
516 bool InsetMathHull::numbered(row_type row) const
522 bool InsetMathHull::ams() const
525 type_ == hullAlign ||
526 type_ == hullFlAlign ||
527 type_ == hullMultline ||
528 type_ == hullGather ||
529 type_ == hullAlignAt ||
530 type_ == hullXAlignAt ||
531 type_ == hullXXAlignAt;
535 Inset::DisplayType InsetMathHull::display() const
537 return (type_ != hullSimple && type_ != hullNone) ? AlignCenter : Inline;
541 void InsetMathHull::getLabelList(vector<docstring> & labels) const
543 for (row_type row = 0; row < nrows(); ++row)
544 if (label_[row] && !nonum_[row])
545 labels.push_back(label_[row]->screenLabel());
549 bool InsetMathHull::numberedType() const
551 if (type_ == hullNone)
553 if (type_ == hullSimple)
555 if (type_ == hullXXAlignAt)
557 for (row_type row = 0; row < nrows(); ++row)
564 void InsetMathHull::validate(LaTeXFeatures & features) const
567 features.require("amsmath");
570 // Validation is necessary only if not using AMS math.
571 // To be safe, we will always run mathedvalidate.
572 //if (features.amsstyle)
575 features.require("boldsymbol");
576 //features.binom = true;
578 InsetMathGrid::validate(features);
582 void InsetMathHull::header_write(WriteStream & os) const
584 bool n = numberedType();
598 os << "\\begin{equation" << star(n) << "}\n";
608 os << "\\begin{" << hullName(type_) << star(n) << "}\n";
613 os << "\\begin{" << hullName(type_) << star(n) << '}'
614 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
618 os << "\\begin{" << hullName(type_) << '}'
619 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
623 os << "\\begin{unknown" << star(n) << '}';
629 void InsetMathHull::footer_write(WriteStream & os) const
631 bool n = numberedType();
644 os << "\\end{equation" << star(n) << "}\n";
656 os << "\\end{" << hullName(type_) << star(n) << "}\n";
660 os << "\\end{" << hullName(type_) << "}\n";
664 os << "\\end{unknown" << star(n) << '}';
670 bool InsetMathHull::rowChangeOK() const
673 type_ == hullEqnArray || type_ == hullAlign ||
674 type_ == hullFlAlign || type_ == hullAlignAt ||
675 type_ == hullXAlignAt || type_ == hullXXAlignAt ||
676 type_ == hullGather || type_ == hullMultline;
680 bool InsetMathHull::colChangeOK() const
683 type_ == hullAlign || type_ == hullFlAlign ||type_ == hullAlignAt ||
684 type_ == hullXAlignAt || type_ == hullXXAlignAt;
688 void InsetMathHull::addRow(row_type row)
692 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
693 label_.insert(label_.begin() + row + 1, dummy_pointer);
694 InsetMathGrid::addRow(row);
698 void InsetMathHull::swapRow(row_type row)
702 if (row + 1 == nrows())
704 // gcc implements the standard std::vector<bool> which is *not* a container:
705 // http://www.gotw.ca/publications/N1185.pdf
706 // As a results, it doesn't like this:
707 // swap(nonum_[row], nonum_[row + 1]);
708 // so we do it manually:
709 bool const b = nonum_[row];
710 nonum_[row] = nonum_[row + 1];
712 swap(label_[row], label_[row + 1]);
713 InsetMathGrid::swapRow(row);
717 void InsetMathHull::delRow(row_type row)
719 if (nrows() <= 1 || !rowChangeOK())
721 InsetMathGrid::delRow(row);
722 // The last dummy row has no number info nor a label.
723 // Test nrows() + 1 because we have already erased the row.
724 if (row == nrows() + 1)
726 nonum_.erase(nonum_.begin() + row);
728 label_.erase(label_.begin() + row);
732 void InsetMathHull::addCol(col_type col)
736 InsetMathGrid::addCol(col);
740 void InsetMathHull::delCol(col_type col)
742 if (ncols() <= 1 || !colChangeOK())
744 InsetMathGrid::delCol(col);
748 docstring InsetMathHull::nicelabel(row_type row) const
753 return from_ascii("(#)");
754 return '(' + label_[row]->screenLabel() + ')';
758 void InsetMathHull::glueall()
761 for (idx_type i = 0; i < nargs(); ++i)
763 *this = InsetMathHull(hullSimple);
769 void InsetMathHull::splitTo2Cols()
771 BOOST_ASSERT(ncols() == 1);
772 InsetMathGrid::addCol(1);
773 for (row_type row = 0; row < nrows(); ++row) {
774 idx_type const i = 2 * row;
775 pos_type pos = firstRelOp(cell(i));
776 cell(i + 1) = MathData(cell(i).begin() + pos, cell(i).end());
777 cell(i).erase(pos, cell(i).size());
782 void InsetMathHull::splitTo3Cols()
784 BOOST_ASSERT(ncols() < 3);
787 InsetMathGrid::addCol(2);
788 for (row_type row = 0; row < nrows(); ++row) {
789 idx_type const i = 3 * row + 1;
790 if (cell(i).size()) {
791 cell(i + 1) = MathData(cell(i).begin() + 1, cell(i).end());
792 cell(i).erase(1, cell(i).size());
798 void InsetMathHull::changeCols(col_type cols)
802 else if (ncols() < cols) {
808 while (ncols() < cols)
809 InsetMathGrid::addCol(ncols());
815 for (row_type row = 0; row < nrows(); ++row) {
816 idx_type const i = row * ncols();
817 for (col_type col = cols; col < ncols(); ++col) {
818 cell(i + cols - 1).append(cell(i + col));
822 while (ncols() > cols) {
823 InsetMathGrid::delCol(ncols() - 1);
828 HullType InsetMathHull::getType() const
834 void InsetMathHull::setType(HullType type)
841 void InsetMathHull::mutate(HullType newtype)
843 //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
845 // we try to move along the chain
846 // none <-> simple <-> equation <-> eqnarray -> *align* -> multline, gather -+
848 // +-------------------------------------+
849 // we use eqnarray as intermediate type for mutations that are not
850 // directly supported because it handles labels and numbering for
853 if (newtype == type_) {
857 else if (newtype < hullNone) {
862 else if (type_ == hullNone) {
868 else if (type_ == hullSimple) {
869 if (newtype == hullNone) {
873 setType(hullEquation);
879 else if (type_ == hullEquation) {
880 if (newtype < type_) {
884 } else if (newtype == hullEqnArray) {
885 // split it "nicely" on the first relop
887 setType(hullEqnArray);
888 } else if (newtype == hullMultline || newtype == hullGather) {
898 else if (type_ == hullEqnArray) {
899 if (newtype < type_) {
900 // set correct (no)numbering
902 for (row_type row = 0; row < nrows(); ++row) {
909 // set first non-empty label
910 for (row_type row = 0; row < nrows(); ++row) {
912 label_[0] = label_[row];
919 } else { // align & Co.
926 else if (type_ == hullAlign || type_ == hullAlignAt ||
927 type_ == hullXAlignAt || type_ == hullFlAlign) {
928 if (newtype < hullAlign) {
930 setType(hullEqnArray);
932 } else if (newtype == hullGather || newtype == hullMultline) {
935 } else if (newtype == hullXXAlignAt) {
936 for (row_type row = 0; row < nrows(); ++row)
937 numbered(row, false);
944 else if (type_ == hullXXAlignAt) {
945 for (row_type row = 0; row < nrows(); ++row)
946 numbered(row, false);
947 if (newtype < hullAlign) {
949 setType(hullEqnArray);
951 } else if (newtype == hullGather || newtype == hullMultline) {
959 else if (type_ == hullMultline || type_ == hullGather) {
960 if (newtype == hullGather || newtype == hullMultline)
962 else if (newtype == hullAlign || newtype == hullFlAlign ||
963 newtype == hullAlignAt || newtype == hullXAlignAt) {
966 } else if (newtype == hullXXAlignAt) {
968 for (row_type row = 0; row < nrows(); ++row)
969 numbered(row, false);
973 setType(hullEqnArray);
979 lyxerr << "mutation from '" << to_utf8(hullName(type_))
980 << "' to '" << to_utf8(hullName(newtype))
981 << "' not implemented" << endl;
986 docstring InsetMathHull::eolString(row_type row, bool emptyline, bool fragile) const
989 if (numberedType()) {
990 if (label_[row] && !nonum_[row])
991 res += "\\label{" + label_[row]->getParam("name") + '}';
992 if (nonum_[row] && (type_ != hullMultline))
993 res += "\\nonumber ";
995 return res + InsetMathGrid::eolString(row, emptyline, fragile);
999 void InsetMathHull::write(WriteStream & os) const
1002 InsetMathGrid::write(os);
1007 void InsetMathHull::normalize(NormalStream & os) const
1009 os << "[formula " << hullName(type_) << ' ';
1010 InsetMathGrid::normalize(os);
1015 void InsetMathHull::mathmlize(MathStream & os) const
1017 InsetMathGrid::mathmlize(os);
1021 void InsetMathHull::infoize(odocstream & os) const
1023 os << "Type: " << hullName(type_);
1027 void InsetMathHull::check() const
1029 BOOST_ASSERT(nonum_.size() == nrows());
1030 BOOST_ASSERT(label_.size() == nrows());
1034 void InsetMathHull::doExtern(Cursor & cur, FuncRequest & func)
1038 idocstringstream iss(func.argument());
1039 iss >> dlang >> extra;
1041 extra = from_ascii("noextra");
1042 string const lang = to_ascii(dlang);
1044 // FIXME: temporarily disabled
1045 //if (cur.selection()) {
1048 // lyxerr << "use selection: " << ar << endl;
1049 // insert(pipeThroughExtern(lang, extra, ar));
1054 eq.push_back(MathAtom(new InsetMathChar('=')));
1056 // go to first item in line
1057 cur.idx() -= cur.idx() % ncols();
1060 if (getType() == hullSimple) {
1061 size_type pos = cur.cell().find_last(eq);
1063 if (cur.inMathed() && cur.selection()) {
1064 asArray(grabAndEraseSelection(cur), ar);
1065 } else if (pos == cur.cell().size()) {
1067 lyxerr << "use whole cell: " << ar << endl;
1069 ar = MathData(cur.cell().begin() + pos + 1, cur.cell().end());
1070 lyxerr << "use partial cell form pos: " << pos << endl;
1072 cur.cell().append(eq);
1073 cur.cell().append(pipeThroughExtern(lang, extra, ar));
1074 cur.pos() = cur.lastpos();
1078 if (getType() == hullEquation) {
1079 lyxerr << "use equation inset" << endl;
1080 mutate(hullEqnArray);
1081 MathData & ar = cur.cell();
1082 lyxerr << "use cell: " << ar << endl;
1086 cur.cell() = pipeThroughExtern(lang, extra, ar);
1087 // move to end of line
1088 cur.pos() = cur.lastpos();
1093 lyxerr << "use eqnarray" << endl;
1094 cur.idx() += 2 - cur.idx() % ncols();
1096 MathData ar = cur.cell();
1097 lyxerr << "use cell: " << ar << endl;
1098 // FIXME: temporarily disabled
1104 cur.cell() = pipeThroughExtern(lang, extra, ar);
1105 cur.pos() = cur.lastpos();
1110 void InsetMathHull::doDispatch(Cursor & cur, FuncRequest & cmd)
1112 //lyxerr << "action: " << cmd.action << endl;
1113 switch (cmd.action) {
1115 case LFUN_FINISHED_BACKWARD:
1116 case LFUN_FINISHED_FORWARD:
1117 case LFUN_FINISHED_RIGHT:
1118 case LFUN_FINISHED_LEFT:
1119 //lyxerr << "action: " << cmd.action << endl;
1120 InsetMathGrid::doDispatch(cur, cmd);
1124 case LFUN_BREAK_PARAGRAPH:
1125 // just swallow this
1129 // some magic for the common case
1130 if (type_ == hullSimple || type_ == hullEquation) {
1131 cur.recordUndoInset();
1133 cur.bv().buffer().params().use_amsmath == BufferParams::package_on;
1134 mutate(align ? hullAlign : hullEqnArray);
1135 cur.idx() = nrows() * ncols() - 1;
1136 cur.pos() = cur.lastpos();
1138 InsetMathGrid::doDispatch(cur, cmd);
1141 case LFUN_MATH_NUMBER_TOGGLE: {
1142 //lyxerr << "toggling all numbers" << endl;
1143 cur.recordUndoInset();
1144 bool old = numberedType();
1145 if (type_ == hullMultline)
1146 numbered(nrows() - 1, !old);
1148 for (row_type row = 0; row < nrows(); ++row)
1149 numbered(row, !old);
1151 cur.message(old ? _("No number") : _("Number"));
1155 case LFUN_MATH_NUMBER_LINE_TOGGLE: {
1156 cur.recordUndoInset();
1157 row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1158 bool old = numbered(r);
1159 cur.message(old ? _("No number") : _("Number"));
1164 case LFUN_LABEL_INSERT: {
1165 cur.recordUndoInset();
1166 row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1167 docstring old_label = label(r);
1168 docstring const default_label = from_ascii(
1169 (lyxrc.label_init_length >= 0) ? "eq:" : "");
1170 if (old_label.empty())
1171 old_label = default_label;
1173 InsetCommandParams p(LABEL_CODE);
1174 p["name"] = cmd.argument().empty() ? old_label : cmd.argument();
1175 string const data = InsetCommandMailer::params2string("label", p);
1177 if (cmd.argument().empty())
1178 cur.bv().showDialog("label", data);
1180 FuncRequest fr(LFUN_INSET_INSERT, data);
1186 case LFUN_INSET_INSERT: {
1187 //lyxerr << "arg: " << to_utf8(cmd.argument()) << endl;
1188 // FIXME: this should be cleaned up to use InsetLabel methods directly.
1189 string const name = cmd.getArg(0);
1190 if (name == "label") {
1191 InsetCommandParams p(LABEL_CODE);
1192 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), p);
1193 docstring str = p["name"];
1194 cur.recordUndoInset();
1195 row_type const r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1199 docstring old = label(r);
1202 // The label will take care of the reference update.
1206 // Newly created inset so initialize it.
1207 label_[r]->initView();
1212 InsetMathGrid::doDispatch(cur, cmd);
1216 case LFUN_MATH_EXTERN:
1217 cur.recordUndoInset();
1221 case LFUN_MATH_MUTATE: {
1222 cur.recordUndoInset();
1223 row_type row = cur.row();
1224 col_type col = cur.col();
1225 mutate(hullType(cmd.argument()));
1226 cur.idx() = row * ncols() + col;
1227 if (cur.idx() > cur.lastidx()) {
1228 cur.idx() = cur.lastidx();
1229 cur.pos() = cur.lastpos();
1231 if (cur.pos() > cur.lastpos())
1232 cur.pos() = cur.lastpos();
1234 // FIXME: find some more clever handling of the selection,
1235 // i.e. preserve it.
1236 cur.clearSelection();
1237 //cur.dispatched(FINISHED);
1241 case LFUN_MATH_DISPLAY: {
1242 cur.recordUndoInset();
1243 mutate(type_ == hullSimple ? hullEquation : hullSimple);
1245 cur.pos() = cur.lastpos();
1246 //cur.dispatched(FINISHED);
1251 InsetMathGrid::doDispatch(cur, cmd);
1257 bool InsetMathHull::getStatus(Cursor & cur, FuncRequest const & cmd,
1258 FuncStatus & status) const
1260 switch (cmd.action) {
1261 case LFUN_FINISHED_BACKWARD:
1262 case LFUN_FINISHED_FORWARD:
1263 case LFUN_FINISHED_RIGHT:
1264 case LFUN_FINISHED_LEFT:
1268 case LFUN_MATH_EXTERN:
1269 case LFUN_MATH_MUTATE:
1270 case LFUN_MATH_DISPLAY:
1272 status.enabled(true);
1274 case LFUN_MATH_NUMBER_TOGGLE:
1275 // FIXME: what is the right test, this or the one of
1277 status.enabled(display());
1278 status.setOnOff(numberedType());
1280 case LFUN_MATH_NUMBER_LINE_TOGGLE: {
1281 // FIXME: what is the right test, this or the one of
1283 status.enabled(display());
1284 row_type const r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1285 status.setOnOff(numbered(r));
1288 case LFUN_LABEL_INSERT:
1289 status.enabled(type_ != hullSimple);
1291 case LFUN_INSET_INSERT:
1292 if (cmd.getArg(0) == "label") {
1293 status.enabled(type_ != hullSimple);
1296 return InsetMathGrid::getStatus(cur, cmd, status);
1297 case LFUN_TABULAR_FEATURE: {
1298 istringstream is(to_utf8(cmd.argument()));
1302 && (s == "append-row"
1303 || s == "delete-row"
1304 || s == "copy-row")) {
1305 status.message(bformat(
1306 from_utf8(N_("Can't change number of rows in '%1$s'")),
1308 status.enabled(false);
1312 && (s == "append-column"
1313 || s == "delete-column"
1314 || s == "copy-column")) {
1315 status.message(bformat(
1316 from_utf8(N_("Can't change number of columns in '%1$s'")),
1318 status.enabled(false);
1321 if ((type_ == hullSimple
1322 || type_ == hullEquation
1323 || type_ == hullNone) &&
1324 (s == "add-hline-above" || s == "add-hline-below")) {
1325 status.message(bformat(
1326 from_utf8(N_("Can't add horizontal grid lines in '%1$s'")),
1328 status.enabled(false);
1331 if (s == "add-vline-left" || s == "add-vline-right") {
1332 status.message(bformat(
1333 from_utf8(N_("Can't add vertical grid lines in '%1$s'")),
1335 status.enabled(false);
1338 if (s == "valign-top" || s == "valign-middle"
1339 || s == "valign-bottom" || s == "align-left"
1340 || s == "align-center" || s == "align-right") {
1341 status.enabled(false);
1344 return InsetMathGrid::getStatus(cur, cmd, status);
1347 return InsetMathGrid::getStatus(cur, cmd, status);
1350 // This cannot really happen, but inserted to shut-up gcc
1351 return InsetMathGrid::getStatus(cur, cmd, status);
1355 /////////////////////////////////////////////////////////////////////
1359 // simply scrap this function if you want
1360 void InsetMathHull::mutateToText()
1363 // translate to latex
1365 latex(NULL, os, false, false);
1366 string str = os.str();
1369 Text * lt = view_->cursor().innerText();
1370 string::const_iterator cit = str.begin();
1371 string::const_iterator end = str.end();
1372 for (; cit != end; ++cit)
1373 view_->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
1376 //dispatch(LFUN_ESCAPE);
1381 void InsetMathHull::handleFont(Cursor & cur, docstring const & arg,
1382 docstring const & font)
1384 // this whole function is a hack and won't work for incremental font
1387 if (cur.inset().asInsetMath()->name() == font)
1388 cur.handleFont(to_utf8(font));
1390 cur.handleNest(createInsetMath(font));
1396 void InsetMathHull::handleFont2(Cursor & cur, docstring const & arg)
1401 font.fromString(to_utf8(arg), b);
1402 if (font.fontInfo().color() != Color_inherit) {
1403 MathAtom at = MathAtom(new InsetMathColor(true, font.fontInfo().color()));
1404 cur.handleNest(at, 0);
1409 void InsetMathHull::edit(Cursor & cur, bool front, EntryDirection entry_from)
1412 bool enter_front = (entry_from == Inset::ENTRY_DIRECTION_LEFT ||
1413 (entry_from == Inset::ENTRY_DIRECTION_IGNORE && front));
1414 enter_front ? idxFirst(cur) : idxLast(cur);
1415 // The inset formula dimension is not necessarily the same as the
1416 // one of the instant preview image, so we have to indicate to the
1417 // BufferView that a metrics update is needed.
1418 cur.updateFlags(Update::Force);
1422 docstring InsetMathHull::editMessage() const
1424 return _("Math editor mode");
1428 void InsetMathHull::revealCodes(Cursor & cur) const
1430 if (!cur.inMathed())
1432 odocstringstream os;
1434 cur.message(os.str());
1436 // write something to the minibuffer
1437 // translate to latex
1441 string str = os.str();
1443 string::size_type pos = 0;
1445 for (string::iterator it = str.begin(); it != str.end(); ++it) {
1448 else if (*it == '\0') {
1450 pos = it - str.begin();
1456 res = res.substr(pos - 30);
1457 if (res.size() > 60)
1458 res = res.substr(0, 60);
1464 InsetCode InsetMathHull::lyxCode() const
1470 /////////////////////////////////////////////////////////////////////
1474 bool InsetMathHull::searchForward(BufferView * bv, string const & str,
1477 // FIXME: completely broken
1478 static InsetMathHull * lastformula = 0;
1479 static CursorBase current = DocIterator(ibegin(nucleus()));
1481 static string laststr;
1483 if (lastformula != this || laststr != str) {
1484 //lyxerr << "reset lastformula to " << this << endl;
1487 current = ibegin(nucleus());
1489 mathed_parse_cell(ar, str);
1493 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
1495 for (DocIterator it = current; it != iend(nucleus()); increment(it)) {
1496 CursorSlice & top = it.back();
1497 MathData const & a = top.asInsetMath()->cell(top.idx_);
1498 if (a.matchpart(ar, top.pos_)) {
1499 bv->cursor().setSelection(it, ar.size());
1501 top.pos_ += ar.size();
1507 //lyxerr << "not found!" << endl;
1514 void InsetMathHull::write(ostream & os) const
1516 odocstringstream oss;
1517 WriteStream wi(oss, false, false);
1520 os << to_utf8(oss.str());
1524 void InsetMathHull::read(Lexer & lex)
1527 mathed_parse_normal(at, lex);
1528 operator=(*at->asHullInset());
1532 int InsetMathHull::plaintext(odocstream & os, OutputParams const &) const
1534 if (0 && display()) {
1538 TextPainter tpain(dim.width(), dim.height());
1539 drawT(tpain, 0, dim.ascent());
1541 // reset metrics cache to "real" values
1543 return tpain.textheight();
1545 odocstringstream oss;
1546 WriteStream wi(oss, false, true);
1549 docstring const str = oss.str();
1556 int InsetMathHull::docbook(odocstream & os, OutputParams const & runparams) const
1561 if (getType() == hullSimple)
1562 name = from_ascii("inlineequation");
1564 name = from_ascii("informalequation");
1566 docstring bname = name;
1567 if (!label(0).empty())
1568 bname += " id='" + sgml::cleanID(buffer(), runparams, label(0)) + "'";
1570 ++ms.tab(); ms.cr(); ms.os() << '<' << bname << '>';
1572 odocstringstream ls;
1573 if (runparams.flavor == OutputParams::XML) {
1574 ms << MTag("alt role='tex' ");
1575 // Workaround for db2latex: db2latex always includes equations with
1576 // \ensuremath{} or \begin{display}\end{display}
1577 // so we strip LyX' math environment
1578 WriteStream wi(ls, false, false);
1579 InsetMathGrid::write(wi);
1580 ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&"), "<", "<"));
1585 InsetMathGrid::mathmlize(ms);
1588 ms << MTag("alt role='tex'");
1589 res = latex(ls, runparams);
1590 ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&"), "<", "<"));
1594 ms << from_ascii("<graphic fileref=\"eqn/");
1595 if (!label(0).empty())
1596 ms << sgml::cleanID(buffer(), runparams, label(0));
1598 ms << sgml::uniqueID(from_ascii("anon"));
1600 if (runparams.flavor == OutputParams::XML)
1601 ms << from_ascii("\"/>");
1603 ms << from_ascii("\">");
1605 ms.cr(); --ms.tab(); ms.os() << "</" << name << '>';
1607 return ms.line() + res;
1611 void InsetMathHull::textString(odocstream & os) const
1613 plaintext(os, OutputParams(0));