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 "buffer_funcs.h"
30 #include "BufferParams.h"
31 #include "BufferView.h"
32 #include "CutAndPaste.h"
33 #include "FuncStatus.h"
34 #include "LaTeXFeatures.h"
36 #include "DispatchResult.h"
37 #include "FuncRequest.h"
39 #include "OutputParams.h"
40 #include "ParIterator.h"
43 #include "TextPainter.h"
44 #include "TocBackend.h"
46 #include "insets/RenderPreview.h"
47 #include "insets/InsetLabel.h"
49 #include "graphics/PreviewImage.h"
50 #include "graphics/PreviewLoader.h"
52 #include "frontends/Painter.h"
54 #include "support/lassert.h"
55 #include "support/debug.h"
56 #include "support/gettext.h"
57 #include "support/lstrings.h"
62 using namespace lyx::support;
66 using cap::grabAndEraseSelection;
70 int getCols(HullType type)
87 // returns position of first relation operator in the array
88 // used for "intelligent splitting"
89 size_t firstRelOp(MathData const & ar)
91 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
93 return it - ar.begin();
98 char const * star(bool numbered)
100 return numbered ? "" : "*";
104 } // end anon namespace
107 HullType hullType(docstring const & s)
109 if (s == "none") return hullNone;
110 if (s == "simple") return hullSimple;
111 if (s == "equation") return hullEquation;
112 if (s == "eqnarray") return hullEqnArray;
113 if (s == "align") return hullAlign;
114 if (s == "alignat") return hullAlignAt;
115 if (s == "xalignat") return hullXAlignAt;
116 if (s == "xxalignat") return hullXXAlignAt;
117 if (s == "multline") return hullMultline;
118 if (s == "gather") return hullGather;
119 if (s == "flalign") return hullFlAlign;
120 lyxerr << "unknown hull type '" << to_utf8(s) << "'" << endl;
125 docstring hullName(HullType type)
128 case hullNone: return from_ascii("none");
129 case hullSimple: return from_ascii("simple");
130 case hullEquation: return from_ascii("equation");
131 case hullEqnArray: return from_ascii("eqnarray");
132 case hullAlign: return from_ascii("align");
133 case hullAlignAt: return from_ascii("alignat");
134 case hullXAlignAt: return from_ascii("xalignat");
135 case hullXXAlignAt: return from_ascii("xxalignat");
136 case hullMultline: return from_ascii("multline");
137 case hullGather: return from_ascii("gather");
138 case hullFlAlign: return from_ascii("flalign");
140 lyxerr << "unknown hull type '" << type << "'" << endl;
141 return from_ascii("none");
145 static InsetLabel * dummy_pointer = 0;
147 InsetMathHull::InsetMathHull()
148 : InsetMathGrid(1, 1), type_(hullNone), nonum_(1, false),
149 label_(1, dummy_pointer), preview_(new RenderPreview(this))
151 //lyxerr << "sizeof InsetMath: " << sizeof(InsetMath) << endl;
152 //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl;
153 //lyxerr << "sizeof InsetMathChar: " << sizeof(InsetMathChar) << endl;
154 //lyxerr << "sizeof FontInfo: " << sizeof(FontInfo) << endl;
160 InsetMathHull::InsetMathHull(HullType type)
161 : InsetMathGrid(getCols(type), 1), type_(type), nonum_(1, false),
162 label_(1, dummy_pointer), preview_(new RenderPreview(this))
169 InsetMathHull::InsetMathHull(InsetMathHull const & other) : InsetMathGrid()
175 InsetMathHull::~InsetMathHull()
177 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)
197 label_ = other.label_;
198 for (size_t i = 0; i != label_.size(); ++i) {
200 label_[i] = new InsetLabel(*label_[i]);
202 preview_.reset(new RenderPreview(*other.preview_, this));
208 void InsetMathHull::setBuffer(Buffer & buffer)
211 for (idx_type i = 0, n = nargs(); i != n; ++i) {
212 MathData & data = cell(i);
213 for (size_t j = 0; j != data.size(); ++j)
214 data[j].nucleus()->setBuffer(buffer);
217 for (size_t i = 0; i != label_.size(); ++i) {
219 label_[i]->setBuffer(buffer);
224 void InsetMathHull::updateLabels(ParIterator const & it)
227 //FIXME: buffer_ should be set at creation for this inset! Problem is
228 // This inset is created at too many places (see Parser::parse1() in
232 for (size_t i = 0; i != label_.size(); ++i) {
234 label_[i]->updateLabels(it);
239 void InsetMathHull::addToToc(ParConstIterator const & pit) const
242 //FIXME: buffer_ should be set at creation for this inset! Problem is
243 // This inset is created at too many places (see Parser::parse1() in
248 // FIXME: it would be way better to directly use InsetLabel instead of this
249 // label list. But it should be possible to copy&paste the code in
250 // InsetLabel::addToToc() anyway.
252 Toc & toc = buffer().tocBackend().toc("equation");
254 for (row_type row = 0; row != nrows(); ++row) {
258 label_[row]->addToToc(pit);
259 toc.push_back(TocItem(pit, 0, nicelabel(row)));
264 Inset * InsetMathHull::editXY(Cursor & cur, int x, int y)
270 return InsetMathNest::editXY(cur, x, y);
274 InsetMath::mode_type InsetMathHull::currentMode() const
276 if (type_ == hullNone)
277 return UNDECIDED_MODE;
278 // definitely math mode ...
283 bool InsetMathHull::idxFirst(Cursor & cur) const
291 bool InsetMathHull::idxLast(Cursor & cur) const
293 cur.idx() = nargs() - 1;
294 cur.pos() = cur.lastpos();
299 char InsetMathHull::defaultColAlign(col_type col)
301 if (type_ == hullEqnArray)
303 if (type_ == hullGather)
305 if (type_ >= hullAlign)
306 return "rl"[col & 1];
311 int InsetMathHull::defaultColSpace(col_type col)
313 if (type_ == hullAlign || type_ == hullAlignAt)
315 if (type_ == hullXAlignAt)
316 return (col & 1) ? 20 : 0;
317 if (type_ == hullXXAlignAt || type_ == hullFlAlign)
318 return (col & 1) ? 40 : 0;
323 docstring InsetMathHull::standardFont() const
325 return from_ascii(type_ == hullNone ? "lyxnochange" : "mathnormal");
329 bool InsetMathHull::previewState(BufferView * bv) const
331 if (!editing(bv) && RenderPreview::status() == LyXRC::PREVIEW_ON) {
332 graphics::PreviewImage const * pimage =
333 preview_->getPreviewImage(bv->buffer());
334 return pimage && pimage->image();
340 void InsetMathHull::metrics(MetricsInfo & mi, Dimension & dim) const
342 if (previewState(mi.base.bv)) {
343 preview_->metrics(mi, dim);
344 // insert a one pixel gap in front of the formula
347 dim.des += displayMargin();
348 // Cache the inset dimension.
349 setDimCache(mi, dim);
353 FontSetChanger dummy1(mi.base, standardFont());
354 StyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
356 // let the cells adjust themselves
357 InsetMathGrid::metrics(mi, dim);
360 dim.asc += displayMargin();
361 dim.des += displayMargin();
364 if (numberedType()) {
365 FontSetChanger dummy(mi.base, from_ascii("mathbf"));
367 for (row_type row = 0; row < nrows(); ++row)
368 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
374 // make it at least as high as the current font
377 math_font_max_dim(mi.base.font, asc, des);
378 dim.asc = max(dim.asc, asc);
379 dim.des = max(dim.des, des);
380 // Cache the inset dimension.
381 // FIXME: This will overwrite InsetMathGrid dimension, is that OK?
382 setDimCache(mi, dim);
386 void InsetMathHull::draw(PainterInfo & pi, int x, int y) const
388 use_preview_ = previewState(pi.base.bv);
389 Dimension const dim = dimension(*pi.base.bv);
391 // background of mathed under focus is not painted because
392 // selection at the top level of nested inset is difficult to handle.
393 if (!editing(pi.base.bv))
394 pi.pain.fillRectangle(x + 1, y - dim.asc + 1, dim.wid - 2,
395 dim.asc + dim.des - 1, Color_mathbg);
398 // one pixel gap in front
399 preview_->draw(pi, x + 1, y);
400 setPosCache(pi, x, y);
404 FontSetChanger dummy1(pi.base, standardFont());
405 StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
406 InsetMathGrid::draw(pi, x + 1, y);
408 if (numberedType()) {
409 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
410 for (row_type row = 0; row < nrows(); ++row) {
411 int const yy = y + rowinfo_[row].offset_;
412 FontSetChanger dummy(pi.base, from_ascii("mathrm"));
413 docstring const nl = nicelabel(row);
417 setPosCache(pi, x, y);
421 void InsetMathHull::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
424 InsetMathGrid::metricsT(mi, dim);
427 WriteStream wi(os, false, true);
429 dim.wid = os.str().size();
436 void InsetMathHull::drawT(TextPainter & pain, int x, int y) const
439 InsetMathGrid::drawT(pain, x, y);
442 WriteStream wi(os, false, true);
444 pain.draw(x, y, os.str().c_str());
449 static docstring latexString(InsetMathHull const & inset)
452 WriteStream wi(ls, false, false);
458 void InsetMathHull::addPreview(graphics::PreviewLoader & ploader) const
460 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
461 docstring const snippet = latexString(*this);
462 preview_->addPreview(snippet, ploader);
467 bool InsetMathHull::notifyCursorLeaves(Cursor const & /*old*/, Cursor & cur)
469 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
470 Buffer const & buffer = cur.buffer();
471 docstring const snippet = latexString(*this);
472 preview_->addPreview(snippet, buffer);
473 preview_->startLoading(buffer);
474 cur.updateFlags(Update::Force);
480 docstring InsetMathHull::label(row_type row) const
482 LASSERT(row < nrows(), /**/);
483 if (InsetLabel * il = label_[row])
484 return il->screenLabel();
489 void InsetMathHull::label(row_type row, docstring const & label)
491 //lyxerr << "setting label '" << label << "' for row " << row << endl;
495 label_[row] = dummy_pointer;
497 label_[row]->updateCommand(label);
500 InsetCommandParams p(LABEL_CODE);
502 label_[row] = new InsetLabel(p);
504 label_[row]->setBuffer(buffer());
508 void InsetMathHull::numbered(row_type row, bool num)
511 if (nonum_[row] && label_[row]) {
518 bool InsetMathHull::numbered(row_type row) const
524 bool InsetMathHull::ams() const
527 type_ == hullAlign ||
528 type_ == hullFlAlign ||
529 type_ == hullMultline ||
530 type_ == hullGather ||
531 type_ == hullAlignAt ||
532 type_ == hullXAlignAt ||
533 type_ == hullXXAlignAt;
537 Inset::DisplayType InsetMathHull::display() const
539 return (type_ != hullSimple && type_ != hullNone) ? AlignCenter : Inline;
543 bool InsetMathHull::numberedType() const
545 if (type_ == hullNone)
547 if (type_ == hullSimple)
549 if (type_ == hullXXAlignAt)
551 for (row_type row = 0; row < nrows(); ++row)
558 void InsetMathHull::validate(LaTeXFeatures & features) const
561 features.require("amsmath");
564 // Validation is necessary only if not using AMS math.
565 // To be safe, we will always run mathedvalidate.
566 //if (features.amsstyle)
569 //features.binom = true;
571 InsetMathGrid::validate(features);
575 void InsetMathHull::header_write(WriteStream & os) const
577 bool n = numberedType();
591 os << "\\begin{equation" << star(n) << "}\n";
601 os << "\\begin{" << hullName(type_) << star(n) << "}\n";
606 os << "\\begin{" << hullName(type_) << star(n) << '}'
607 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
611 os << "\\begin{" << hullName(type_) << '}'
612 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
616 os << "\\begin{unknown" << star(n) << '}';
622 void InsetMathHull::footer_write(WriteStream & os) const
624 bool n = numberedType();
637 os << "\\end{equation" << star(n) << "}\n";
649 os << "\\end{" << hullName(type_) << star(n) << "}\n";
653 os << "\\end{" << hullName(type_) << "}\n";
657 os << "\\end{unknown" << star(n) << '}';
663 bool InsetMathHull::rowChangeOK() const
666 type_ == hullEqnArray || type_ == hullAlign ||
667 type_ == hullFlAlign || type_ == hullAlignAt ||
668 type_ == hullXAlignAt || type_ == hullXXAlignAt ||
669 type_ == hullGather || type_ == hullMultline;
673 bool InsetMathHull::colChangeOK() const
676 type_ == hullAlign || type_ == hullFlAlign ||type_ == hullAlignAt ||
677 type_ == hullXAlignAt || type_ == hullXXAlignAt;
681 void InsetMathHull::addRow(row_type row)
686 bool numbered = numberedType();
688 if (type_ == hullMultline) {
689 if (row + 1 == nrows()) {
696 nonum_.insert(nonum_.begin() + row + 1, !numbered);
697 label_.insert(label_.begin() + row + 1, dummy_pointer);
700 InsetMathGrid::addRow(row);
704 void InsetMathHull::swapRow(row_type row)
708 if (row + 1 == nrows())
710 // gcc implements the standard std::vector<bool> which is *not* a container:
711 // http://www.gotw.ca/publications/N1185.pdf
712 // As a results, it doesn't like this:
713 // swap(nonum_[row], nonum_[row + 1]);
714 // so we do it manually:
715 bool const b = nonum_[row];
716 nonum_[row] = nonum_[row + 1];
718 swap(label_[row], label_[row + 1]);
719 InsetMathGrid::swapRow(row);
723 void InsetMathHull::delRow(row_type row)
725 if (nrows() <= 1 || !rowChangeOK())
727 if (row + 1 == nrows() && type_ == hullMultline) {
728 bool const b = nonum_[row - 1];
729 nonum_[row - 1] = nonum_[row];
731 swap(label_[row - 1], label_[row]);
732 InsetMathGrid::delRow(row);
735 InsetMathGrid::delRow(row);
736 // The last dummy row has no number info nor a label.
737 // Test nrows() + 1 because we have already erased the row.
738 if (row == nrows() + 1)
740 nonum_.erase(nonum_.begin() + row);
742 label_.erase(label_.begin() + row);
746 void InsetMathHull::addCol(col_type col)
750 InsetMathGrid::addCol(col);
754 void InsetMathHull::delCol(col_type col)
756 if (ncols() <= 1 || !colChangeOK())
758 InsetMathGrid::delCol(col);
762 docstring InsetMathHull::nicelabel(row_type row) const
767 return from_ascii("(#)");
768 return '(' + label_[row]->screenLabel() + from_ascii(", #)");
772 void InsetMathHull::glueall()
775 for (idx_type i = 0; i < nargs(); ++i)
777 *this = InsetMathHull(hullSimple);
783 void InsetMathHull::splitTo2Cols()
785 LASSERT(ncols() == 1, /**/);
786 InsetMathGrid::addCol(1);
787 for (row_type row = 0; row < nrows(); ++row) {
788 idx_type const i = 2 * row;
789 pos_type pos = firstRelOp(cell(i));
790 cell(i + 1) = MathData(cell(i).begin() + pos, cell(i).end());
791 cell(i).erase(pos, cell(i).size());
796 void InsetMathHull::splitTo3Cols()
798 LASSERT(ncols() < 3, /**/);
801 InsetMathGrid::addCol(2);
802 for (row_type row = 0; row < nrows(); ++row) {
803 idx_type const i = 3 * row + 1;
804 if (cell(i).size()) {
805 cell(i + 1) = MathData(cell(i).begin() + 1, cell(i).end());
806 cell(i).erase(1, cell(i).size());
812 void InsetMathHull::changeCols(col_type cols)
816 else if (ncols() < cols) {
822 while (ncols() < cols)
823 InsetMathGrid::addCol(ncols());
829 for (row_type row = 0; row < nrows(); ++row) {
830 idx_type const i = row * ncols();
831 for (col_type col = cols; col < ncols(); ++col) {
832 cell(i + cols - 1).append(cell(i + col));
836 while (ncols() > cols) {
837 InsetMathGrid::delCol(ncols() - 1);
842 HullType InsetMathHull::getType() const
848 void InsetMathHull::setType(HullType type)
855 void InsetMathHull::mutate(HullType newtype)
857 //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
859 // we try to move along the chain
860 // none <-> simple <-> equation <-> eqnarray -> *align* -> multline, gather -+
862 // +-------------------------------------+
863 // we use eqnarray as intermediate type for mutations that are not
864 // directly supported because it handles labels and numbering for
867 if (newtype == type_) {
871 else if (newtype < hullNone) {
876 else if (type_ == hullNone) {
882 else if (type_ == hullSimple) {
883 if (newtype == hullNone) {
887 setType(hullEquation);
893 else if (type_ == hullEquation) {
894 if (newtype < type_) {
898 } else if (newtype == hullEqnArray) {
899 // split it "nicely" on the first relop
901 setType(hullEqnArray);
902 } else if (newtype == hullMultline || newtype == hullGather) {
912 else if (type_ == hullEqnArray) {
913 if (newtype < type_) {
914 // set correct (no)numbering
916 for (row_type row = 0; row < nrows(); ++row) {
923 // set first non-empty label
924 for (row_type row = 0; row < nrows(); ++row) {
926 label_[0] = label_[row];
933 } else { // align & Co.
940 else if (type_ == hullAlign || type_ == hullAlignAt ||
941 type_ == hullXAlignAt || type_ == hullFlAlign) {
942 if (newtype < hullAlign) {
944 setType(hullEqnArray);
946 } else if (newtype == hullGather || newtype == hullMultline) {
949 } else if (newtype == hullXXAlignAt) {
950 for (row_type row = 0; row < nrows(); ++row)
951 numbered(row, false);
958 else if (type_ == hullXXAlignAt) {
959 for (row_type row = 0; row < nrows(); ++row)
960 numbered(row, false);
961 if (newtype < hullAlign) {
963 setType(hullEqnArray);
965 } else if (newtype == hullGather || newtype == hullMultline) {
973 else if (type_ == hullMultline || type_ == hullGather) {
974 if (newtype == hullGather || newtype == hullMultline)
976 else if (newtype == hullAlign || newtype == hullFlAlign ||
977 newtype == hullAlignAt || newtype == hullXAlignAt) {
980 } else if (newtype == hullXXAlignAt) {
982 for (row_type row = 0; row < nrows(); ++row)
983 numbered(row, false);
987 setType(hullEqnArray);
993 lyxerr << "mutation from '" << to_utf8(hullName(type_))
994 << "' to '" << to_utf8(hullName(newtype))
995 << "' not implemented" << endl;
1000 docstring InsetMathHull::eolString(row_type row, bool emptyline, bool fragile) const
1003 if (numberedType()) {
1004 if (label_[row] && !nonum_[row])
1005 res += "\\label{" + label_[row]->getParam("name") + '}';
1006 if (nonum_[row] && (type_ != hullMultline))
1007 res += "\\nonumber ";
1009 return res + InsetMathGrid::eolString(row, emptyline, fragile);
1013 void InsetMathHull::write(WriteStream & os) const
1016 InsetMathGrid::write(os);
1021 void InsetMathHull::normalize(NormalStream & os) const
1023 os << "[formula " << hullName(type_) << ' ';
1024 InsetMathGrid::normalize(os);
1029 void InsetMathHull::mathmlize(MathStream & os) const
1031 InsetMathGrid::mathmlize(os);
1035 void InsetMathHull::infoize(odocstream & os) const
1037 os << "Type: " << hullName(type_);
1041 void InsetMathHull::check() const
1043 LASSERT(nonum_.size() == nrows(), /**/);
1044 LASSERT(label_.size() == nrows(), /**/);
1048 void InsetMathHull::doExtern(Cursor & cur, FuncRequest & func)
1052 idocstringstream iss(func.argument());
1053 iss >> dlang >> extra;
1055 extra = from_ascii("noextra");
1056 string const lang = to_ascii(dlang);
1058 // FIXME: temporarily disabled
1059 //if (cur.selection()) {
1062 // lyxerr << "use selection: " << ar << endl;
1063 // insert(pipeThroughExtern(lang, extra, ar));
1068 eq.push_back(MathAtom(new InsetMathChar('=')));
1070 // go to first item in line
1071 cur.idx() -= cur.idx() % ncols();
1074 if (getType() == hullSimple) {
1075 size_type pos = cur.cell().find_last(eq);
1077 if (cur.inMathed() && cur.selection()) {
1078 asArray(grabAndEraseSelection(cur), ar);
1079 } else if (pos == cur.cell().size()) {
1081 lyxerr << "use whole cell: " << ar << endl;
1083 ar = MathData(cur.cell().begin() + pos + 1, cur.cell().end());
1084 lyxerr << "use partial cell form pos: " << pos << endl;
1086 cur.cell().append(eq);
1087 cur.cell().append(pipeThroughExtern(lang, extra, ar));
1088 cur.pos() = cur.lastpos();
1092 if (getType() == hullEquation) {
1093 lyxerr << "use equation inset" << endl;
1094 mutate(hullEqnArray);
1095 MathData & ar = cur.cell();
1096 lyxerr << "use cell: " << ar << endl;
1100 cur.cell() = pipeThroughExtern(lang, extra, ar);
1101 // move to end of line
1102 cur.pos() = cur.lastpos();
1107 lyxerr << "use eqnarray" << endl;
1108 cur.idx() += 2 - cur.idx() % ncols();
1110 MathData ar = cur.cell();
1111 lyxerr << "use cell: " << ar << endl;
1112 // FIXME: temporarily disabled
1118 cur.cell() = pipeThroughExtern(lang, extra, ar);
1119 cur.pos() = cur.lastpos();
1124 void InsetMathHull::doDispatch(Cursor & cur, FuncRequest & cmd)
1126 //lyxerr << "action: " << cmd.action << endl;
1127 switch (cmd.action) {
1129 case LFUN_FINISHED_BACKWARD:
1130 case LFUN_FINISHED_FORWARD:
1131 case LFUN_FINISHED_RIGHT:
1132 case LFUN_FINISHED_LEFT:
1133 //lyxerr << "action: " << cmd.action << endl;
1134 InsetMathGrid::doDispatch(cur, cmd);
1138 case LFUN_BREAK_PARAGRAPH:
1139 // just swallow this
1142 case LFUN_NEWLINE_INSERT:
1143 // some magic for the common case
1144 if (type_ == hullSimple || type_ == hullEquation) {
1145 cur.recordUndoInset();
1147 cur.bv().buffer().params().use_amsmath == BufferParams::package_on;
1148 mutate(align ? hullAlign : hullEqnArray);
1149 cur.idx() = nrows() * ncols() - 1;
1150 cur.pos() = cur.lastpos();
1152 InsetMathGrid::doDispatch(cur, cmd);
1155 case LFUN_MATH_NUMBER_TOGGLE: {
1156 //lyxerr << "toggling all numbers" << endl;
1157 cur.recordUndoInset();
1158 bool old = numberedType();
1159 if (type_ == hullMultline)
1160 numbered(nrows() - 1, !old);
1162 for (row_type row = 0; row < nrows(); ++row)
1163 numbered(row, !old);
1165 cur.message(old ? _("No number") : _("Number"));
1169 case LFUN_MATH_NUMBER_LINE_TOGGLE: {
1170 cur.recordUndoInset();
1171 row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1172 bool old = numbered(r);
1173 cur.message(old ? _("No number") : _("Number"));
1178 case LFUN_LABEL_INSERT: {
1179 cur.recordUndoInset();
1180 row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1181 docstring old_label = label(r);
1182 docstring const default_label = from_ascii(
1183 (lyxrc.label_init_length >= 0) ? "eq:" : "");
1184 if (old_label.empty())
1185 old_label = default_label;
1187 InsetCommandParams p(LABEL_CODE);
1188 p["name"] = cmd.argument().empty() ? old_label : cmd.argument();
1189 string const data = InsetCommand::params2string("label", p);
1191 if (cmd.argument().empty())
1192 cur.bv().showDialog("label", data);
1194 FuncRequest fr(LFUN_INSET_INSERT, data);
1200 case LFUN_WORD_DELETE_FORWARD:
1201 case LFUN_CHAR_DELETE_FORWARD:
1202 if (col(cur.idx()) + 1 == ncols()
1203 && cur.pos() == cur.lastpos()) {
1204 if (!label(row(cur.idx())).empty()) {
1205 cur.recordUndoInset();
1206 label(row(cur.idx()), docstring());
1207 } else if (numbered(row(cur.idx()))) {
1208 cur.recordUndoInset();
1209 numbered(row(cur.idx()), false);
1211 InsetMathGrid::doDispatch(cur, cmd);
1215 InsetMathGrid::doDispatch(cur, cmd);
1220 case LFUN_INSET_INSERT: {
1221 //lyxerr << "arg: " << to_utf8(cmd.argument()) << endl;
1222 // FIXME: this should be cleaned up to use InsetLabel methods directly.
1223 string const name = cmd.getArg(0);
1224 if (name == "label") {
1225 InsetCommandParams p(LABEL_CODE);
1226 InsetCommand::string2params(name, to_utf8(cmd.argument()), p);
1227 docstring str = p["name"];
1228 cur.recordUndoInset();
1229 row_type const r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1233 docstring old = label(r);
1236 // The label will take care of the reference update.
1240 // Newly created inset so initialize it.
1241 label_[r]->initView();
1246 InsetMathGrid::doDispatch(cur, cmd);
1250 case LFUN_MATH_EXTERN:
1251 cur.recordUndoInset();
1255 case LFUN_MATH_MUTATE: {
1256 cur.recordUndoInset();
1257 row_type row = cur.row();
1258 col_type col = cur.col();
1259 mutate(hullType(cmd.argument()));
1260 cur.idx() = row * ncols() + col;
1261 if (cur.idx() > cur.lastidx()) {
1262 cur.idx() = cur.lastidx();
1263 cur.pos() = cur.lastpos();
1265 if (cur.pos() > cur.lastpos())
1266 cur.pos() = cur.lastpos();
1268 // FIXME: find some more clever handling of the selection,
1269 // i.e. preserve it.
1270 cur.clearSelection();
1271 //cur.dispatched(FINISHED);
1275 case LFUN_MATH_DISPLAY: {
1276 cur.recordUndoInset();
1277 mutate(type_ == hullSimple ? hullEquation : hullSimple);
1279 cur.pos() = cur.lastpos();
1280 //cur.dispatched(FINISHED);
1285 InsetMathGrid::doDispatch(cur, cmd);
1291 bool InsetMathHull::getStatus(Cursor & cur, FuncRequest const & cmd,
1292 FuncStatus & status) const
1294 switch (cmd.action) {
1295 case LFUN_FINISHED_BACKWARD:
1296 case LFUN_FINISHED_FORWARD:
1297 case LFUN_FINISHED_RIGHT:
1298 case LFUN_FINISHED_LEFT:
1301 case LFUN_NEWLINE_INSERT:
1302 case LFUN_MATH_EXTERN:
1303 case LFUN_MATH_MUTATE:
1304 case LFUN_MATH_DISPLAY:
1306 status.enabled(true);
1308 case LFUN_MATH_NUMBER_TOGGLE:
1309 // FIXME: what is the right test, this or the one of
1311 status.enabled(display());
1312 status.setOnOff(numberedType());
1314 case LFUN_MATH_NUMBER_LINE_TOGGLE: {
1315 // FIXME: what is the right test, this or the one of
1317 bool const enable = (type_ == hullMultline) ?
1318 (nrows() - 1 == cur.row()) : display();
1319 row_type const r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1320 status.enabled(enable);
1321 status.setOnOff(numbered(r));
1324 case LFUN_LABEL_INSERT:
1325 status.enabled(type_ != hullSimple);
1327 case LFUN_INSET_INSERT:
1328 if (cmd.getArg(0) == "label") {
1329 status.enabled(type_ != hullSimple);
1332 return InsetMathGrid::getStatus(cur, cmd, status);
1333 case LFUN_TABULAR_FEATURE: {
1334 istringstream is(to_utf8(cmd.argument()));
1338 && (s == "append-row"
1339 || s == "delete-row"
1340 || s == "copy-row")) {
1341 status.message(bformat(
1342 from_utf8(N_("Can't change number of rows in '%1$s'")),
1344 status.enabled(false);
1348 && (s == "append-column"
1349 || s == "delete-column"
1350 || s == "copy-column")) {
1351 status.message(bformat(
1352 from_utf8(N_("Can't change number of columns in '%1$s'")),
1354 status.enabled(false);
1357 if ((type_ == hullSimple
1358 || type_ == hullEquation
1359 || type_ == hullNone) &&
1360 (s == "add-hline-above" || s == "add-hline-below")) {
1361 status.message(bformat(
1362 from_utf8(N_("Can't add horizontal grid lines in '%1$s'")),
1364 status.enabled(false);
1367 if (s == "add-vline-left" || s == "add-vline-right") {
1368 status.message(bformat(
1369 from_utf8(N_("Can't add vertical grid lines in '%1$s'")),
1371 status.enabled(false);
1374 if (s == "valign-top" || s == "valign-middle"
1375 || s == "valign-bottom" || s == "align-left"
1376 || s == "align-center" || s == "align-right") {
1377 status.enabled(false);
1380 return InsetMathGrid::getStatus(cur, cmd, status);
1383 return InsetMathGrid::getStatus(cur, cmd, status);
1386 // This cannot really happen, but inserted to shut-up gcc
1387 return InsetMathGrid::getStatus(cur, cmd, status);
1391 /////////////////////////////////////////////////////////////////////
1395 // simply scrap this function if you want
1396 void InsetMathHull::mutateToText()
1399 // translate to latex
1401 latex(os, false, false);
1402 string str = os.str();
1405 Text * lt = view_->cursor().innerText();
1406 string::const_iterator cit = str.begin();
1407 string::const_iterator end = str.end();
1408 for (; cit != end; ++cit)
1409 view_->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
1412 //dispatch(LFUN_ESCAPE);
1417 void InsetMathHull::handleFont(Cursor & cur, docstring const & arg,
1418 docstring const & font)
1420 // this whole function is a hack and won't work for incremental font
1423 if (cur.inset().asInsetMath()->name() == font)
1424 cur.handleFont(to_utf8(font));
1426 cur.handleNest(createInsetMath(font));
1432 void InsetMathHull::handleFont2(Cursor & cur, docstring const & arg)
1437 font.fromString(to_utf8(arg), b);
1438 if (font.fontInfo().color() != Color_inherit) {
1439 MathAtom at = MathAtom(new InsetMathColor(true, font.fontInfo().color()));
1440 cur.handleNest(at, 0);
1445 void InsetMathHull::edit(Cursor & cur, bool front, EntryDirection entry_from)
1448 bool enter_front = (entry_from == Inset::ENTRY_DIRECTION_LEFT ||
1449 (entry_from == Inset::ENTRY_DIRECTION_IGNORE && front));
1450 enter_front ? idxFirst(cur) : idxLast(cur);
1451 // The inset formula dimension is not necessarily the same as the
1452 // one of the instant preview image, so we have to indicate to the
1453 // BufferView that a metrics update is needed.
1454 cur.updateFlags(Update::Force);
1458 docstring InsetMathHull::editMessage() const
1460 return _("Math editor mode");
1464 void InsetMathHull::revealCodes(Cursor & cur) const
1466 if (!cur.inMathed())
1468 odocstringstream os;
1470 cur.message(os.str());
1472 // write something to the minibuffer
1473 // translate to latex
1477 string str = os.str();
1479 string::size_type pos = 0;
1481 for (string::iterator it = str.begin(); it != str.end(); ++it) {
1484 else if (*it == '\0') {
1486 pos = it - str.begin();
1492 res = res.substr(pos - 30);
1493 if (res.size() > 60)
1494 res = res.substr(0, 60);
1500 InsetCode InsetMathHull::lyxCode() const
1506 /////////////////////////////////////////////////////////////////////
1510 bool InsetMathHull::searchForward(BufferView * bv, string const & str,
1513 // FIXME: completely broken
1514 static InsetMathHull * lastformula = 0;
1515 static CursorBase current = DocIterator(ibegin(nucleus()));
1517 static string laststr;
1519 if (lastformula != this || laststr != str) {
1520 //lyxerr << "reset lastformula to " << this << endl;
1523 current = ibegin(nucleus());
1525 mathed_parse_cell(ar, str);
1529 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
1531 for (DocIterator it = current; it != iend(nucleus()); increment(it)) {
1532 CursorSlice & top = it.back();
1533 MathData const & a = top.asInsetMath()->cell(top.idx_);
1534 if (a.matchpart(ar, top.pos_)) {
1535 bv->cursor().setSelection(it, ar.size());
1537 top.pos_ += ar.size();
1543 //lyxerr << "not found!" << endl;
1550 void InsetMathHull::write(ostream & os) const
1552 odocstringstream oss;
1553 WriteStream wi(oss, false, false);
1556 os << to_utf8(oss.str());
1560 void InsetMathHull::read(Lexer & lex)
1563 mathed_parse_normal(at, lex);
1564 operator=(*at->asHullInset());
1568 int InsetMathHull::plaintext(odocstream & os, OutputParams const &) const
1570 if (0 && display()) {
1574 TextPainter tpain(dim.width(), dim.height());
1575 drawT(tpain, 0, dim.ascent());
1577 // reset metrics cache to "real" values
1579 return tpain.textheight();
1581 odocstringstream oss;
1582 WriteStream wi(oss, false, true);
1585 docstring const str = oss.str();
1592 int InsetMathHull::docbook(odocstream & os, OutputParams const & runparams) const
1597 if (getType() == hullSimple)
1598 name = from_ascii("inlineequation");
1600 name = from_ascii("informalequation");
1602 docstring bname = name;
1603 if (!label(0).empty())
1604 bname += " id='" + sgml::cleanID(buffer(), runparams, label(0)) + "'";
1606 ++ms.tab(); ms.cr(); ms.os() << '<' << bname << '>';
1608 odocstringstream ls;
1609 if (runparams.flavor == OutputParams::XML) {
1610 ms << MTag("alt role='tex' ");
1611 // Workaround for db2latex: db2latex always includes equations with
1612 // \ensuremath{} or \begin{display}\end{display}
1613 // so we strip LyX' math environment
1614 WriteStream wi(ls, false, false);
1615 InsetMathGrid::write(wi);
1616 ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&"), "<", "<"));
1621 InsetMathGrid::mathmlize(ms);
1624 ms << MTag("alt role='tex'");
1625 res = latex(ls, runparams);
1626 ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&"), "<", "<"));
1630 ms << from_ascii("<graphic fileref=\"eqn/");
1631 if (!label(0).empty())
1632 ms << sgml::cleanID(buffer(), runparams, label(0));
1634 ms << sgml::uniqueID(from_ascii("anon"));
1636 if (runparams.flavor == OutputParams::XML)
1637 ms << from_ascii("\"/>");
1639 ms << from_ascii("\">");
1641 ms.cr(); --ms.tab(); ms.os() << "</" << name << '>';
1643 return ms.line() + res;
1647 void InsetMathHull::textString(odocstream & os) const
1649 plaintext(os, OutputParams(0));
1653 docstring InsetMathHull::contextMenu(BufferView const &, int, int) const
1655 return from_ascii("context-math");