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 "InsetMathHull.h"
15 #include "InsetMathChar.h"
16 #include "InsetMathColor.h"
17 #include "MathExtern.h"
18 #include "MathFactory.h"
19 #include "MathStream.h"
20 #include "MathSupport.h"
23 #include "BufferParams.h"
24 #include "BufferView.h"
26 #include "CutAndPaste.h"
29 #include "FuncRequest.h"
30 #include "FuncStatus.h"
32 #include "LaTeXFeatures.h"
34 #include "MacroTable.h"
35 #include "output_xhtml.h"
36 #include "Paragraph.h"
37 #include "ParIterator.h"
39 #include "TextClass.h"
40 #include "TextPainter.h"
41 #include "TocBackend.h"
43 #include "insets/InsetLabel.h"
44 #include "insets/InsetRef.h"
45 #include "insets/RenderPreview.h"
47 #include "graphics/PreviewImage.h"
48 #include "graphics/PreviewLoader.h"
50 #include "frontends/alert.h"
51 #include "frontends/Painter.h"
53 #include "support/convert.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 if (s == "regexp") return hullRegexp;
121 lyxerr << "unknown hull type '" << to_utf8(s) << "'" << endl;
126 docstring hullName(HullType type)
129 case hullNone: return from_ascii("none");
130 case hullSimple: return from_ascii("simple");
131 case hullEquation: return from_ascii("equation");
132 case hullEqnArray: return from_ascii("eqnarray");
133 case hullAlign: return from_ascii("align");
134 case hullAlignAt: return from_ascii("alignat");
135 case hullXAlignAt: return from_ascii("xalignat");
136 case hullXXAlignAt: return from_ascii("xxalignat");
137 case hullMultline: return from_ascii("multline");
138 case hullGather: return from_ascii("gather");
139 case hullFlAlign: return from_ascii("flalign");
140 case hullRegexp: return from_ascii("regexp");
142 lyxerr << "unknown hull type '" << type << "'" << endl;
143 return from_ascii("none");
147 static InsetLabel * dummy_pointer = 0;
149 InsetMathHull::InsetMathHull(Buffer * buf)
150 : InsetMathGrid(buf, 1, 1), type_(hullNone), numbered_(1, true),
151 numbers_(1, empty_docstring()), label_(1, dummy_pointer),
152 preview_(new RenderPreview(this))
154 //lyxerr << "sizeof InsetMath: " << sizeof(InsetMath) << endl;
155 //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl;
156 //lyxerr << "sizeof InsetMathChar: " << sizeof(InsetMathChar) << endl;
157 //lyxerr << "sizeof FontInfo: " << sizeof(FontInfo) << endl;
164 InsetMathHull::InsetMathHull(Buffer * buf, HullType type)
165 : InsetMathGrid(buf, getCols(type), 1), type_(type), numbered_(1, true),
166 numbers_(1, empty_docstring()), label_(1, dummy_pointer),
167 preview_(new RenderPreview(this))
175 InsetMathHull::InsetMathHull(InsetMathHull const & other) : InsetMathGrid(other)
181 InsetMathHull::~InsetMathHull()
183 for (size_t i = 0; i < label_.size(); ++i)
188 Inset * InsetMathHull::clone() const
190 return new InsetMathHull(*this);
194 InsetMathHull & InsetMathHull::operator=(InsetMathHull const & other)
198 InsetMathGrid::operator=(other);
200 numbered_ = other.numbered_;
201 numbers_ = other.numbers_;
202 buffer_ = other.buffer_;
203 for (size_t i = 0; i < label_.size(); ++i)
205 label_ = other.label_;
206 for (size_t i = 0; i != label_.size(); ++i) {
208 label_[i] = new InsetLabel(*label_[i]);
210 preview_.reset(new RenderPreview(*other.preview_, this));
216 void InsetMathHull::setBuffer(Buffer & buffer)
218 InsetMathGrid::setBuffer(buffer);
220 for (size_t i = 0; i != label_.size(); ++i) {
222 label_[i]->setBuffer(buffer);
227 void InsetMathHull::updateBuffer(ParIterator const & it, UpdateType utype)
230 //FIXME: buffer_ should be set at creation for this inset! Problem is
231 // This inset is created at too many places (see Parser::parse1() in
236 BufferParams const & bp = buffer_->params();
237 string const & lang = it->getParLanguage(bp)->code();
238 Counters & cnts = bp.documentClass().counters();
239 // so we don't have to write it ten times...
240 docstring const eqstr = from_ascii("equation");
242 for (size_t i = 0; i != label_.size(); ++i) {
243 if (numbered(i) && cnts.hasCounter(eqstr)) {
244 cnts.step(eqstr, utype);
245 numbers_[i] = cnts.theCounter(eqstr, lang);
247 numbers_[i] = empty_docstring();
249 label_[i]->updateBuffer(it, utype);
252 InsetMathGrid::updateBuffer(it, utype);
256 void InsetMathHull::addToToc(DocIterator const & pit)
259 //FIXME: buffer_ should be set at creation for this inset! Problem is
260 // This inset is created at too many places (see Parser::parse1() in
265 Toc & toc = buffer().tocBackend().toc("equation");
267 for (row_type row = 0; row != nrows(); ++row) {
271 label_[row]->addToToc(pit);
272 toc.push_back(TocItem(pit, 0, nicelabel(row)));
277 Inset * InsetMathHull::editXY(Cursor & cur, int x, int y)
283 return InsetMathNest::editXY(cur, x, y);
287 InsetMath::mode_type InsetMathHull::currentMode() const
289 if (type_ == hullNone)
290 return UNDECIDED_MODE;
291 // definitely math mode ...
296 bool InsetMathHull::idxFirst(Cursor & cur) const
304 bool InsetMathHull::idxLast(Cursor & cur) const
306 cur.idx() = nargs() - 1;
307 cur.pos() = cur.lastpos();
312 char InsetMathHull::defaultColAlign(col_type col)
314 if (type_ == hullEqnArray)
316 if (type_ == hullGather)
318 if (type_ >= hullAlign)
319 return "rl"[col & 1];
324 int InsetMathHull::defaultColSpace(col_type col)
326 if (type_ == hullAlign || type_ == hullAlignAt)
328 if (type_ == hullXAlignAt)
329 return (col & 1) ? 20 : 0;
330 if (type_ == hullXXAlignAt || type_ == hullFlAlign)
331 return (col & 1) ? 40 : 0;
336 docstring InsetMathHull::standardFont() const
341 font_name = from_ascii("texttt");
344 font_name = from_ascii("lyxnochange");
347 font_name = from_ascii("mathnormal");
353 ColorCode InsetMathHull::standardColor() const
359 color = Color_foreground;
368 bool InsetMathHull::previewState(BufferView * bv) const
370 if (!editing(bv) && RenderPreview::status() == LyXRC::PREVIEW_ON) {
371 graphics::PreviewImage const * pimage =
372 preview_->getPreviewImage(bv->buffer());
373 return pimage && pimage->image();
379 void InsetMathHull::metrics(MetricsInfo & mi, Dimension & dim) const
381 if (previewState(mi.base.bv)) {
382 preview_->metrics(mi, dim);
383 // insert a one pixel gap in front of the formula
386 dim.des += displayMargin();
387 // Cache the inset dimension.
388 setDimCache(mi, dim);
392 FontSetChanger dummy1(mi.base, standardFont());
393 StyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
395 // let the cells adjust themselves
396 InsetMathGrid::metrics(mi, dim);
399 dim.asc += displayMargin();
400 dim.des += displayMargin();
403 if (numberedType()) {
404 FontSetChanger dummy(mi.base, from_ascii("mathbf"));
406 for (row_type row = 0; row < nrows(); ++row)
407 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
413 // make it at least as high as the current font
416 math_font_max_dim(mi.base.font, asc, des);
417 dim.asc = max(dim.asc, asc);
418 dim.des = max(dim.des, des);
419 // Cache the inset dimension.
420 // FIXME: This will overwrite InsetMathGrid dimension, is that OK?
421 setDimCache(mi, dim);
425 ColorCode InsetMathHull::backgroundColor(PainterInfo const & pi) const
427 if (previewState(pi.base.bv))
428 return graphics::PreviewLoader::backgroundColor();
433 void InsetMathHull::drawBackground(PainterInfo & pi, int x, int y) const
435 Dimension const dim = dimension(*pi.base.bv);
436 pi.pain.fillRectangle(x + 1, y - dim.asc + 1, dim.wid - 2,
437 dim.asc + dim.des - 1, pi.backgroundColor(this));
441 void InsetMathHull::draw(PainterInfo & pi, int x, int y) const
443 use_preview_ = previewState(pi.base.bv);
445 if (type_ == hullRegexp) {
446 Dimension const dim = dimension(*pi.base.bv);
447 pi.pain.rectangle(x + 1, y - dim.ascent() + 1,
448 dim.width() - 2, dim.height() - 2, Color_regexpframe);
451 // one pixel gap in front
452 preview_->draw(pi, x + 1, y);
453 setPosCache(pi, x, y);
457 ColorCode color = pi.selected && lyxrc.use_system_colors
458 ? Color_selectiontext : standardColor();
459 bool const really_change_color = pi.base.font.color() == Color_none;
460 ColorChanger dummy0(pi.base.font, color, really_change_color);
461 FontSetChanger dummy1(pi.base, standardFont());
462 StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
464 InsetMathGrid::draw(pi, x + 1, y);
466 if (numberedType()) {
467 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
468 for (row_type row = 0; row < nrows(); ++row) {
469 int const yy = y + rowinfo_[row].offset_;
470 FontSetChanger dummy(pi.base, from_ascii("mathrm"));
471 docstring const nl = nicelabel(row);
475 setPosCache(pi, x, y);
479 void InsetMathHull::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
482 InsetMathGrid::metricsT(mi, dim);
485 WriteStream wi(os, false, true, WriteStream::wsDefault);
487 dim.wid = os.str().size();
494 void InsetMathHull::drawT(TextPainter & pain, int x, int y) const
497 InsetMathGrid::drawT(pain, x, y);
500 WriteStream wi(os, false, true, WriteStream::wsDefault);
502 pain.draw(x, y, os.str().c_str());
507 static docstring latexString(InsetMathHull const & inset)
510 // This has to be static, because a preview snippet or a math
511 // macro containing math in text mode (such as $\text{$\phi$}$ or
512 // \newcommand{\xxx}{\text{$\phi$}}) gets processed twice. The
513 // first time as a whole, and the second time only the inner math.
514 // In this last case inset.buffer() would be invalid.
515 static Encoding const * encoding = 0;
516 if (inset.isBufferValid())
517 encoding = &(inset.buffer().params().encoding());
518 WriteStream wi(ls, false, true, WriteStream::wsPreview, encoding);
524 void InsetMathHull::initUnicodeMath() const
526 // Trigger classification of the unicode symbols in this inset
527 docstring const dummy = latexString(*this);
531 void InsetMathHull::addPreview(DocIterator const & inset_pos,
532 graphics::PreviewLoader & /*ploader*/) const
534 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
535 preparePreview(inset_pos);
540 void InsetMathHull::preparePreview(DocIterator const & pos,
541 bool forexport) const
543 // there is no need to do all the macro stuff if we're not
544 // actually going to generate the preview.
545 if (RenderPreview::status() != LyXRC::PREVIEW_ON && !forexport)
548 Buffer const * buffer = pos.buffer();
550 // collect macros at this position
552 buffer->listMacroNames(macros);
553 MacroNameSet::iterator it = macros.begin();
554 MacroNameSet::iterator end = macros.end();
555 odocstringstream macro_preamble;
556 for (; it != end; ++it) {
557 MacroData const * data = buffer->getMacro(*it, pos, true);
559 data->write(macro_preamble, true);
560 macro_preamble << endl;
564 docstring const snippet = macro_preamble.str() + latexString(*this);
565 LYXERR(Debug::MACROS, "Preview snippet: " << snippet);
566 preview_->addPreview(snippet, *buffer, forexport);
570 void InsetMathHull::reloadPreview(DocIterator const & pos) const
573 preview_->startLoading(*pos.buffer());
577 void InsetMathHull::loadPreview(DocIterator const & pos) const
579 bool const forexport = true;
580 preparePreview(pos, forexport);
581 preview_->startLoading(*pos.buffer(), forexport);
585 bool InsetMathHull::notifyCursorLeaves(Cursor const & old, Cursor & cur)
587 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
589 cur.screenUpdateFlags(Update::Force);
595 docstring InsetMathHull::label(row_type row) const
597 LASSERT(row < nrows(), /**/);
598 if (InsetLabel * il = label_[row])
599 return il->screenLabel();
604 void InsetMathHull::label(row_type row, docstring const & label)
606 //lyxerr << "setting label '" << label << "' for row " << row << endl;
610 label_[row] = dummy_pointer;
613 label_[row]->updateCommand(label);
615 label_[row]->setParam("name", label);
619 InsetCommandParams p(LABEL_CODE);
621 label_[row] = new InsetLabel(buffer_, p);
623 label_[row]->setBuffer(buffer());
627 void InsetMathHull::numbered(row_type row, bool num)
629 numbered_[row] = num;
630 if (!numbered_[row] && label_[row]) {
637 bool InsetMathHull::numbered(row_type row) const
639 return numbered_[row];
643 bool InsetMathHull::ams() const
645 return type_ == hullAlign
646 || type_ == hullFlAlign
647 || type_ == hullMultline
648 || type_ == hullGather
649 || type_ == hullAlignAt
650 || type_ == hullXAlignAt
651 || type_ == hullXXAlignAt;
655 Inset::DisplayType InsetMathHull::display() const
657 if (type_ == hullSimple || type_ == hullNone || type_ == hullRegexp)
662 bool InsetMathHull::numberedType() const
664 if (type_ == hullNone)
666 if (type_ == hullSimple)
668 if (type_ == hullXXAlignAt)
670 if (type_ == hullRegexp)
672 for (row_type row = 0; row < nrows(); ++row)
679 void InsetMathHull::validate(LaTeXFeatures & features) const
681 if (features.runparams().isLaTeX()) {
683 features.require("amsmath");
685 if (type_ == hullRegexp) {
686 features.require("color");
687 string frcol = lcolor.getLaTeXName(Color_regexpframe);
688 string bgcol = "white";
689 features.addPreambleSnippet(
690 string("\\newcommand{\\regexp}[1]{\\fcolorbox{")
691 + frcol + string("}{")
692 + bgcol + string("}{\\texttt{#1}}}"));
695 // Validation is necessary only if not using AMS math.
696 // To be safe, we will always run mathedvalidate.
697 //if (features.amsstyle)
700 //features.binom = true;
701 } else if (features.runparams().math_flavor == OutputParams::MathAsHTML) {
702 // it would be better to do this elsewhere, but we can't validate in
703 // InsetMathMatrix and we have no way, outside MathExtern, to know if
704 // we even have any matrices.
705 features.addPreambleSnippet("<style type=\"text/css\">\n"
706 "table.matrix{display: inline-block; vertical-align: middle; text-align:center;}\n"
707 "table.matrix td{padding: 0.25px;}\n"
708 "td.ldelim{width: 0.5ex; border: thin solid black; border-right: none;}\n"
709 "td.rdelim{width: 0.5ex; border: thin solid black; border-left: none;}\n"
712 InsetMathGrid::validate(features);
716 void InsetMathHull::header_write(WriteStream & os) const
718 bool n = numberedType();
732 os << "\\begin{equation" << star(n) << "}\n";
742 os << "\\begin{" << hullName(type_) << star(n) << "}\n";
747 os << "\\begin{" << hullName(type_) << star(n) << '}'
748 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
752 os << "\\begin{" << hullName(type_) << '}'
753 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
761 os << "\\begin{unknown" << star(n) << "}\n";
767 void InsetMathHull::footer_write(WriteStream & os) const
769 bool n = numberedType();
782 os << "\n\\end{equation" << star(n) << "}\n";
794 os << "\n\\end{" << hullName(type_) << star(n) << "}\n";
798 os << "\n\\end{" << hullName(type_) << "}\n";
806 os << "\n\\end{unknown" << star(n) << "}\n";
812 bool InsetMathHull::rowChangeOK() const
815 type_ == hullEqnArray || type_ == hullAlign ||
816 type_ == hullFlAlign || type_ == hullAlignAt ||
817 type_ == hullXAlignAt || type_ == hullXXAlignAt ||
818 type_ == hullGather || type_ == hullMultline;
822 bool InsetMathHull::colChangeOK() const
825 type_ == hullAlign || type_ == hullFlAlign ||type_ == hullAlignAt ||
826 type_ == hullXAlignAt || type_ == hullXXAlignAt;
830 void InsetMathHull::addRow(row_type row)
835 bool numbered = numberedType();
837 if (type_ == hullMultline) {
838 if (row + 1 == nrows()) {
839 numbered_[row] = false;
845 numbered_.insert(numbered_.begin() + row + 1, numbered);
846 numbers_.insert(numbers_.begin() + row + 1, empty_docstring());
847 label_.insert(label_.begin() + row + 1, dummy_pointer);
850 InsetMathGrid::addRow(row);
854 void InsetMathHull::swapRow(row_type row)
858 if (row + 1 == nrows())
860 // gcc implements the standard std::vector<bool> which is *not* a container:
861 // http://www.gotw.ca/publications/N1185.pdf
862 // As a results, it doesn't like this:
863 // swap(numbered_[row], numbered_[row + 1]);
864 // so we do it manually:
865 bool const b = numbered_[row];
866 numbered_[row] = numbered_[row + 1];
867 numbered_[row + 1] = b;
868 swap(numbers_[row], numbers_[row + 1]);
869 swap(label_[row], label_[row + 1]);
870 InsetMathGrid::swapRow(row);
874 void InsetMathHull::delRow(row_type row)
876 if (nrows() <= 1 || !rowChangeOK())
878 if (row + 1 == nrows() && type_ == hullMultline) {
879 bool const b = numbered_[row - 1];
880 numbered_[row - 1] = numbered_[row];
882 swap(numbers_[row - 1], numbers_[row]);
883 swap(label_[row - 1], label_[row]);
884 InsetMathGrid::delRow(row);
887 InsetMathGrid::delRow(row);
888 // The last dummy row has no number info nor a label.
889 // Test nrows() + 1 because we have already erased the row.
890 if (row == nrows() + 1)
892 numbered_.erase(numbered_.begin() + row);
893 numbers_.erase(numbers_.begin() + row);
895 label_.erase(label_.begin() + row);
899 void InsetMathHull::addCol(col_type col)
903 InsetMathGrid::addCol(col);
907 void InsetMathHull::delCol(col_type col)
909 if (ncols() <= 1 || !colChangeOK())
911 InsetMathGrid::delCol(col);
915 docstring InsetMathHull::nicelabel(row_type row) const
919 docstring const & val = numbers_[row];
921 return '(' + val + ')';
922 return '(' + val + ',' + label_[row]->screenLabel() + ')';
926 void InsetMathHull::glueall(HullType type)
929 for (idx_type i = 0; i < nargs(); ++i)
931 InsetLabel * label = 0;
932 if (type == hullEquation) {
933 // preserve first non-empty label
934 for (row_type row = 0; row < nrows(); ++row) {
942 *this = InsetMathHull(buffer_, hullSimple);
949 void InsetMathHull::splitTo2Cols()
951 LASSERT(ncols() == 1, /**/);
952 InsetMathGrid::addCol(1);
953 for (row_type row = 0; row < nrows(); ++row) {
954 idx_type const i = 2 * row;
955 pos_type pos = firstRelOp(cell(i));
956 cell(i + 1) = MathData(buffer_, cell(i).begin() + pos, cell(i).end());
957 cell(i).erase(pos, cell(i).size());
962 void InsetMathHull::splitTo3Cols()
964 LASSERT(ncols() < 3, /**/);
967 InsetMathGrid::addCol(2);
968 for (row_type row = 0; row < nrows(); ++row) {
969 idx_type const i = 3 * row + 1;
970 if (cell(i).size()) {
971 cell(i + 1) = MathData(buffer_, cell(i).begin() + 1, cell(i).end());
972 cell(i).erase(1, cell(i).size());
978 void InsetMathHull::changeCols(col_type cols)
982 else if (ncols() < cols) {
988 while (ncols() < cols)
989 InsetMathGrid::addCol(ncols());
995 for (row_type row = 0; row < nrows(); ++row) {
996 idx_type const i = row * ncols();
997 for (col_type col = cols; col < ncols(); ++col) {
998 cell(i + cols - 1).append(cell(i + col));
1002 while (ncols() > cols) {
1003 InsetMathGrid::delCol(ncols() - 1);
1008 HullType InsetMathHull::getType() const
1014 void InsetMathHull::setType(HullType type)
1021 void InsetMathHull::mutate(HullType newtype)
1023 //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
1025 // we try to move along the chain
1026 // none <-> simple <-> equation <-> eqnarray -> *align* -> multline, gather -+
1028 // +-------------------------------------+
1029 // we use eqnarray as intermediate type for mutations that are not
1030 // directly supported because it handles labels and numbering for
1033 if (newtype == type_) {
1037 else if (newtype < hullNone) {
1042 else if (type_ == hullNone) {
1043 setType(hullSimple);
1048 else if (type_ == hullSimple) {
1049 if (newtype == hullNone) {
1053 setType(hullEquation);
1054 numbered(0, label_[0] ? true : false);
1059 else if (type_ == hullEquation) {
1060 if (newtype < type_) {
1061 setType(hullSimple);
1064 } else if (newtype == hullEqnArray) {
1065 // split it "nicely" on the first relop
1067 setType(hullEqnArray);
1068 } else if (newtype == hullMultline || newtype == hullGather) {
1071 // split it "nicely"
1078 else if (type_ == hullEqnArray) {
1079 if (newtype < type_) {
1082 } else { // align & Co.
1089 else if (type_ == hullAlign || type_ == hullAlignAt ||
1090 type_ == hullXAlignAt || type_ == hullFlAlign) {
1091 if (newtype < hullAlign) {
1093 setType(hullEqnArray);
1095 } else if (newtype == hullGather || newtype == hullMultline) {
1098 } else if (newtype == hullXXAlignAt) {
1099 for (row_type row = 0; row < nrows(); ++row)
1100 numbered(row, false);
1107 else if (type_ == hullXXAlignAt) {
1108 for (row_type row = 0; row < nrows(); ++row)
1109 numbered(row, false);
1110 if (newtype < hullAlign) {
1112 setType(hullEqnArray);
1114 } else if (newtype == hullGather || newtype == hullMultline) {
1122 else if (type_ == hullMultline || type_ == hullGather) {
1123 if (newtype == hullGather || newtype == hullMultline)
1125 else if (newtype == hullAlign || newtype == hullFlAlign ||
1126 newtype == hullAlignAt || newtype == hullXAlignAt) {
1129 } else if (newtype == hullXXAlignAt) {
1131 for (row_type row = 0; row < nrows(); ++row)
1132 numbered(row, false);
1136 setType(hullEqnArray);
1142 lyxerr << "mutation from '" << to_utf8(hullName(type_))
1143 << "' to '" << to_utf8(hullName(newtype))
1144 << "' not implemented" << endl;
1149 docstring InsetMathHull::eolString(row_type row, bool fragile, bool latex,
1150 bool last_eoln) const
1153 if (numberedType()) {
1154 if (label_[row] && numbered_[row]) {
1155 docstring const name =
1156 latex ? escape(label_[row]->getParam("name"))
1157 : label_[row]->getParam("name");
1158 res += "\\label{" + name + '}';
1160 if (!numbered_[row] && (type_ != hullMultline))
1161 res += "\\nonumber ";
1163 // Never add \\ on the last empty line of eqnarray and friends
1165 return res + InsetMathGrid::eolString(row, fragile, latex, last_eoln);
1169 void InsetMathHull::write(WriteStream & os) const
1171 ModeSpecifier specifier(os, MATH_MODE);
1173 InsetMathGrid::write(os);
1178 void InsetMathHull::normalize(NormalStream & os) const
1180 os << "[formula " << hullName(type_) << ' ';
1181 InsetMathGrid::normalize(os);
1186 void InsetMathHull::infoize(odocstream & os) const
1188 os << "Type: " << hullName(type_);
1192 void InsetMathHull::check() const
1194 LASSERT(numbered_.size() == nrows(), /**/);
1195 LASSERT(numbers_.size() == nrows(), /**/);
1196 LASSERT(label_.size() == nrows(), /**/);
1200 void InsetMathHull::doExtern(Cursor & cur, FuncRequest & func)
1204 idocstringstream iss(func.argument());
1205 iss >> dlang >> extra;
1207 extra = from_ascii("noextra");
1208 string const lang = to_ascii(dlang);
1210 // FIXME: temporarily disabled
1211 //if (cur.selection()) {
1214 // lyxerr << "use selection: " << ar << endl;
1215 // insert(pipeThroughExtern(lang, extra, ar));
1219 // only inline, display or eqnarray math is allowed
1220 if (getType() > hullEqnArray) {
1221 frontend::Alert::warning(_("Bad math environment"),
1222 _("Computation cannot be performed for AMS "
1223 "math environments.\nChange the math "
1224 "formula type and try again."));
1229 eq.push_back(MathAtom(new InsetMathChar('=')));
1231 // go to first item in line
1232 cur.idx() -= cur.idx() % ncols();
1235 if (getType() == hullSimple) {
1236 size_type pos = cur.cell().find_last(eq);
1238 if (cur.inMathed() && cur.selection()) {
1239 asArray(grabAndEraseSelection(cur), ar);
1240 } else if (pos == cur.cell().size()) {
1242 lyxerr << "use whole cell: " << ar << endl;
1244 ar = MathData(buffer_, cur.cell().begin() + pos + 1, cur.cell().end());
1245 lyxerr << "use partial cell form pos: " << pos << endl;
1247 cur.cell().append(eq);
1248 cur.cell().append(pipeThroughExtern(lang, extra, ar));
1249 cur.pos() = cur.lastpos();
1253 if (getType() == hullEquation) {
1254 lyxerr << "use equation inset" << endl;
1255 mutate(hullEqnArray);
1256 MathData & ar = cur.cell();
1257 lyxerr << "use cell: " << ar << endl;
1261 cur.cell() = pipeThroughExtern(lang, extra, ar);
1262 // move to end of line
1263 cur.pos() = cur.lastpos();
1268 lyxerr << "use eqnarray" << endl;
1269 cur.idx() += 2 - cur.idx() % ncols();
1271 MathData ar = cur.cell();
1272 lyxerr << "use cell: " << ar << endl;
1273 // FIXME: temporarily disabled
1279 cur.cell() = pipeThroughExtern(lang, extra, ar);
1280 cur.pos() = cur.lastpos();
1285 void InsetMathHull::doDispatch(Cursor & cur, FuncRequest & cmd)
1287 //lyxerr << "action: " << cmd.action() << endl;
1288 switch (cmd.action()) {
1290 case LFUN_FINISHED_BACKWARD:
1291 case LFUN_FINISHED_FORWARD:
1292 case LFUN_FINISHED_RIGHT:
1293 case LFUN_FINISHED_LEFT:
1294 //lyxerr << "action: " << cmd.action() << endl;
1295 InsetMathGrid::doDispatch(cur, cmd);
1299 case LFUN_BREAK_PARAGRAPH:
1300 // just swallow this
1303 case LFUN_NEWLINE_INSERT:
1304 // some magic for the common case
1305 if (type_ == hullSimple || type_ == hullEquation) {
1306 cur.recordUndoInset();
1308 cur.bv().buffer().params().use_amsmath == BufferParams::package_on;
1309 mutate(align ? hullAlign : hullEqnArray);
1310 // mutate() may change labels and such.
1311 cur.forceBufferUpdate();
1312 cur.idx() = nrows() * ncols() - 1;
1313 cur.pos() = cur.lastpos();
1315 InsetMathGrid::doDispatch(cur, cmd);
1318 case LFUN_MATH_NUMBER_TOGGLE: {
1319 //lyxerr << "toggling all numbers" << endl;
1320 cur.recordUndoInset();
1321 bool old = numberedType();
1322 if (type_ == hullMultline)
1323 numbered(nrows() - 1, !old);
1325 for (row_type row = 0; row < nrows(); ++row)
1326 numbered(row, !old);
1328 cur.message(old ? _("No number") : _("Number"));
1329 cur.forceBufferUpdate();
1333 case LFUN_MATH_NUMBER_LINE_TOGGLE: {
1334 cur.recordUndoInset();
1335 row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1336 bool old = numbered(r);
1337 cur.message(old ? _("No number") : _("Number"));
1339 cur.forceBufferUpdate();
1343 case LFUN_LABEL_INSERT: {
1344 cur.recordUndoInset();
1345 row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1346 docstring old_label = label(r);
1348 // Allow customization of this separator
1349 docstring const default_label = from_ascii("eq:");
1350 if (old_label.empty())
1351 old_label = default_label;
1353 InsetCommandParams p(LABEL_CODE);
1354 p["name"] = cmd.argument().empty() ? old_label : cmd.argument();
1355 string const data = InsetCommand::params2string(p);
1357 if (cmd.argument().empty())
1358 cur.bv().showDialog("label", data);
1360 FuncRequest fr(LFUN_INSET_INSERT, data);
1366 case LFUN_LABEL_COPY_AS_REF: {
1368 if (cmd.argument().empty() && &cur.inset() == this)
1369 // if there is no argument and we're inside math, we retrieve
1370 // the row number from the cursor position.
1371 row = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1373 // if there is an argument, find the corresponding label, else
1374 // check whether there is at least one label.
1375 for (row = 0; row != nrows(); ++row)
1376 if (numbered_[row] && label_[row]
1377 && (cmd.argument().empty() || label(row) == cmd.argument()))
1384 InsetCommandParams p(REF_CODE, "ref");
1385 p["reference"] = label(row);
1386 cap::clearSelection();
1387 cap::copyInset(cur, new InsetRef(buffer_, p), label(row));
1391 case LFUN_WORD_DELETE_FORWARD:
1392 case LFUN_CHAR_DELETE_FORWARD:
1393 if (col(cur.idx()) + 1 == ncols()
1394 && cur.pos() == cur.lastpos()
1395 && !cur.selection()) {
1396 if (!label(row(cur.idx())).empty()) {
1397 cur.recordUndoInset();
1398 label(row(cur.idx()), docstring());
1399 } else if (numbered(row(cur.idx()))) {
1400 cur.recordUndoInset();
1401 numbered(row(cur.idx()), false);
1402 cur.forceBufferUpdate();
1404 InsetMathGrid::doDispatch(cur, cmd);
1408 InsetMathGrid::doDispatch(cur, cmd);
1413 case LFUN_INSET_INSERT: {
1414 //lyxerr << "arg: " << to_utf8(cmd.argument()) << endl;
1415 // FIXME: this should be cleaned up to use InsetLabel methods directly.
1416 string const name = cmd.getArg(0);
1417 if (name == "label") {
1418 InsetCommandParams p(LABEL_CODE);
1419 InsetCommand::string2params(to_utf8(cmd.argument()), p);
1420 docstring str = p["name"];
1421 cur.recordUndoInset();
1422 row_type const r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1426 docstring old = label(r);
1429 // The label will take care of the reference update.
1433 // Newly created inset so initialize it.
1434 label_[r]->initView();
1437 cur.forceBufferUpdate();
1440 InsetMathGrid::doDispatch(cur, cmd);
1444 case LFUN_MATH_EXTERN:
1445 cur.recordUndoInset();
1449 case LFUN_MATH_MUTATE: {
1450 cur.recordUndoInset();
1451 row_type row = cur.row();
1452 col_type col = cur.col();
1453 mutate(hullType(cmd.argument()));
1454 cur.idx() = row * ncols() + col;
1455 if (cur.idx() > cur.lastidx()) {
1456 cur.idx() = cur.lastidx();
1457 cur.pos() = cur.lastpos();
1459 if (cur.pos() > cur.lastpos())
1460 cur.pos() = cur.lastpos();
1462 cur.forceBufferUpdate();
1463 // FIXME: find some more clever handling of the selection,
1464 // i.e. preserve it.
1465 cur.clearSelection();
1466 //cur.dispatched(FINISHED);
1470 case LFUN_MATH_DISPLAY: {
1471 cur.recordUndoInset();
1472 mutate(type_ == hullSimple ? hullEquation : hullSimple);
1474 cur.pos() = cur.lastpos();
1475 //cur.dispatched(FINISHED);
1480 InsetMathGrid::doDispatch(cur, cmd);
1486 bool InsetMathHull::getStatus(Cursor & cur, FuncRequest const & cmd,
1487 FuncStatus & status) const
1489 switch (cmd.action()) {
1490 case LFUN_FINISHED_BACKWARD:
1491 case LFUN_FINISHED_FORWARD:
1492 case LFUN_FINISHED_RIGHT:
1493 case LFUN_FINISHED_LEFT:
1496 case LFUN_NEWLINE_INSERT:
1497 case LFUN_MATH_EXTERN:
1499 status.setEnabled(true);
1502 // we never allow this in math, and we want to bind enter
1503 // to another actions in command-alternatives
1504 case LFUN_BREAK_PARAGRAPH:
1505 status.setEnabled(false);
1507 case LFUN_MATH_MUTATE: {
1508 HullType const ht = hullType(cmd.argument());
1509 status.setOnOff(type_ == ht);
1510 status.setEnabled(true);
1512 if (ht != hullSimple) {
1513 Cursor tmpcur = cur;
1514 while (!tmpcur.empty()) {
1515 InsetCode code = tmpcur.inset().lyxCode();
1516 if (code == BOX_CODE) {
1518 } else if (code == TABULAR_CODE) {
1519 FuncRequest tmpcmd(LFUN_MATH_DISPLAY);
1520 if (tmpcur.getStatus(tmpcmd, status) && !status.enabled())
1528 case LFUN_MATH_DISPLAY: {
1530 if (cur.depth() > 1) {
1531 Inset const & in = cur[cur.depth()-2].inset();
1532 if (in.lyxCode() == SCRIPT_CODE)
1533 enable = display() != Inline;
1535 status.setEnabled(enable);
1539 case LFUN_MATH_NUMBER_TOGGLE:
1540 // FIXME: what is the right test, this or the one of
1542 status.setEnabled(display() != Inline);
1543 status.setOnOff(numberedType());
1546 case LFUN_MATH_NUMBER_LINE_TOGGLE: {
1547 // FIXME: what is the right test, this or the one of
1549 bool const enable = (type_ == hullMultline)
1550 ? (nrows() - 1 == cur.row())
1551 : display() != Inline && nrows() > 1;
1552 row_type const r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1553 status.setEnabled(enable);
1554 status.setOnOff(enable && numbered(r));
1558 case LFUN_LABEL_INSERT:
1559 status.setEnabled(type_ != hullSimple);
1562 case LFUN_LABEL_COPY_AS_REF: {
1563 bool enabled = false;
1565 if (cmd.argument().empty() && &cur.inset() == this) {
1566 // if there is no argument and we're inside math, we retrieve
1567 // the row number from the cursor position.
1568 row = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1569 enabled = numberedType() && label_[row] && numbered_[row];
1571 // if there is an argument, find the corresponding label, else
1572 // check whether there is at least one label.
1573 for (row_type row = 0; row != nrows(); ++row) {
1574 if (numbered_[row] && label_[row] &&
1575 (cmd.argument().empty() || label(row) == cmd.argument())) {
1581 status.setEnabled(enabled);
1585 case LFUN_INSET_INSERT:
1586 if (cmd.getArg(0) == "label") {
1587 status.setEnabled(type_ != hullSimple);
1590 return InsetMathGrid::getStatus(cur, cmd, status);
1592 case LFUN_INSET_MODIFY: {
1593 istringstream is(to_utf8(cmd.argument()));
1597 return InsetMathGrid::getStatus(cur, cmd, status);
1600 && (s == "append-row"
1601 || s == "delete-row"
1602 || s == "copy-row")) {
1603 status.message(bformat(
1604 from_utf8(N_("Can't change number of rows in '%1$s'")),
1606 status.setEnabled(false);
1610 && (s == "append-column"
1611 || s == "delete-column"
1612 || s == "copy-column")) {
1613 status.message(bformat(
1614 from_utf8(N_("Can't change number of columns in '%1$s'")),
1616 status.setEnabled(false);
1619 if ((type_ == hullSimple
1620 || type_ == hullEquation
1621 || type_ == hullNone) &&
1622 (s == "add-hline-above" || s == "add-hline-below")) {
1623 status.message(bformat(
1624 from_utf8(N_("Can't add horizontal grid lines in '%1$s'")),
1626 status.setEnabled(false);
1629 if (s == "add-vline-left" || s == "add-vline-right") {
1630 status.message(bformat(
1631 from_utf8(N_("Can't add vertical grid lines in '%1$s'")),
1633 status.setEnabled(false);
1636 if (s == "valign-top" || s == "valign-middle"
1637 || s == "valign-bottom" || s == "align-left"
1638 || s == "align-center" || s == "align-right") {
1639 status.setEnabled(false);
1642 return InsetMathGrid::getStatus(cur, cmd, status);
1646 return InsetMathGrid::getStatus(cur, cmd, status);
1649 // This cannot really happen, but inserted to shut-up gcc
1650 return InsetMathGrid::getStatus(cur, cmd, status);
1654 /////////////////////////////////////////////////////////////////////
1658 // simply scrap this function if you want
1659 void InsetMathHull::mutateToText()
1662 // translate to latex
1664 latex(os, false, false);
1665 string str = os.str();
1668 Text * lt = view_->cursor().innerText();
1669 string::const_iterator cit = str.begin();
1670 string::const_iterator end = str.end();
1671 for (; cit != end; ++cit)
1672 view_->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
1675 //dispatch(LFUN_ESCAPE);
1680 void InsetMathHull::handleFont(Cursor & cur, docstring const & arg,
1681 docstring const & font)
1683 // this whole function is a hack and won't work for incremental font
1686 if (cur.inset().asInsetMath()->name() == font)
1687 cur.handleFont(to_utf8(font));
1689 cur.handleNest(createInsetMath(font, cur.buffer()));
1695 void InsetMathHull::handleFont2(Cursor & cur, docstring const & arg)
1700 font.fromString(to_utf8(arg), b);
1701 if (font.fontInfo().color() != Color_inherit) {
1702 MathAtom at = MathAtom(new InsetMathColor(buffer_, true, font.fontInfo().color()));
1703 cur.handleNest(at, 0);
1708 void InsetMathHull::edit(Cursor & cur, bool front, EntryDirection entry_from)
1711 bool enter_front = (entry_from == Inset::ENTRY_DIRECTION_LEFT ||
1712 (entry_from == Inset::ENTRY_DIRECTION_IGNORE && front));
1713 enter_front ? idxFirst(cur) : idxLast(cur);
1714 // The inset formula dimension is not necessarily the same as the
1715 // one of the instant preview image, so we have to indicate to the
1716 // BufferView that a metrics update is needed.
1717 cur.screenUpdateFlags(Update::Force);
1721 void InsetMathHull::revealCodes(Cursor & cur) const
1723 if (!cur.inMathed())
1725 odocstringstream os;
1727 cur.message(os.str());
1729 // write something to the minibuffer
1730 // translate to latex
1734 string str = os.str();
1736 string::size_type pos = 0;
1738 for (string::iterator it = str.begin(); it != str.end(); ++it) {
1741 else if (*it == '\0') {
1743 pos = it - str.begin();
1749 res = res.substr(pos - 30);
1750 if (res.size() > 60)
1751 res = res.substr(0, 60);
1757 /////////////////////////////////////////////////////////////////////
1761 bool InsetMathHull::searchForward(BufferView * bv, string const & str,
1764 // FIXME: completely broken
1765 static InsetMathHull * lastformula = 0;
1766 static CursorBase current = DocIterator(ibegin(nucleus()));
1768 static string laststr;
1770 if (lastformula != this || laststr != str) {
1771 //lyxerr << "reset lastformula to " << this << endl;
1774 current = ibegin(nucleus());
1776 mathed_parse_cell(ar, str, Parse::NORMAL, &buffer());
1780 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
1782 for (DocIterator it = current; it != iend(nucleus()); increment(it)) {
1783 CursorSlice & top = it.back();
1784 MathData const & a = top.asInsetMath()->cell(top.idx_);
1785 if (a.matchpart(ar, top.pos_)) {
1786 bv->cursor().setSelection(it, ar.size());
1788 top.pos_ += ar.size();
1794 //lyxerr << "not found!" << endl;
1801 void InsetMathHull::write(ostream & os) const
1803 odocstringstream oss;
1804 WriteStream wi(oss, false, false, WriteStream::wsDefault);
1807 os << to_utf8(oss.str());
1811 void InsetMathHull::read(Lexer & lex)
1814 mathed_parse_normal(buffer_, at, lex, Parse::TRACKMACRO);
1815 operator=(*at->asHullInset());
1819 bool InsetMathHull::readQuiet(Lexer & lex)
1822 bool success = mathed_parse_normal(buffer_, at, lex, Parse::QUIET);
1824 operator=(*at->asHullInset());
1829 int InsetMathHull::plaintext(odocstream & os, OutputParams const &) const
1831 // disables ASCII-art for export of equations. See #2275.
1832 if (0 && display()) {
1836 TextPainter tpain(dim.width(), dim.height());
1837 drawT(tpain, 0, dim.ascent());
1839 // reset metrics cache to "real" values
1841 return tpain.textheight();
1843 odocstringstream oss;
1844 Encoding const * const enc = encodings.fromLyXName("utf8");
1845 WriteStream wi(oss, false, true, WriteStream::wsDefault, enc);
1847 if (type_ == hullRegexp)
1851 docstring const str = oss.str();
1858 int InsetMathHull::docbook(odocstream & os, OutputParams const & runparams) const
1863 if (getType() == hullSimple)
1864 name = from_ascii("inlineequation");
1866 name = from_ascii("informalequation");
1868 docstring bname = name;
1869 if (!label(0).empty())
1870 bname += " id='" + sgml::cleanID(buffer(), runparams, label(0)) + "'";
1872 ++ms.tab(); ms.cr(); ms.os() << '<' << bname << '>';
1874 odocstringstream ls;
1875 if (runparams.flavor == OutputParams::XML) {
1876 ms << MTag("alt role='tex' ");
1877 // Workaround for db2latex: db2latex always includes equations with
1878 // \ensuremath{} or \begin{display}\end{display}
1879 // so we strip LyX' math environment
1880 WriteStream wi(ls, false, false, WriteStream::wsDefault, runparams.encoding);
1881 InsetMathGrid::write(wi);
1882 ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&"), "<", "<"));
1887 InsetMathGrid::mathmlize(ms);
1890 ms << MTag("alt role='tex'");
1891 res = latex(ls, runparams);
1892 ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&"), "<", "<"));
1896 ms << from_ascii("<graphic fileref=\"eqn/");
1897 if (!label(0).empty())
1898 ms << sgml::cleanID(buffer(), runparams, label(0));
1900 ms << sgml::uniqueID(from_ascii("anon"));
1902 if (runparams.flavor == OutputParams::XML)
1903 ms << from_ascii("\"/>");
1905 ms << from_ascii("\">");
1907 ms.cr(); --ms.tab(); ms.os() << "</" << name << '>';
1909 return ms.line() + res;
1914 // We need to do something about alignment here.
1916 // This duplicates code from InsetMathGrid, but
1917 // we need access here to number information,
1918 // and we simply do not have that in InsetMathGrid.
1919 void InsetMathHull::htmlize(HtmlStream & os) const
1921 bool havenumbers = false;
1922 for (size_t i = 0; i != numbered_.size(); ++i) {
1928 bool const havetable = havenumbers || nrows() > 1 || ncols() > 1;
1931 os << cell(index(0, 0));
1935 os << MTag("table", "class='mathtable'");
1936 for (row_type row = 0; row < nrows(); ++row) {
1938 for (col_type col = 0; col < ncols(); ++col) {
1940 os << cell(index(row, col));
1945 docstring const & num = numbers_[row];
1947 os << '(' << num << ')';
1952 os << ETag("table");
1956 // this duplicates code from InsetMathGrid, but
1957 // we need access here to number information,
1958 // and we simply do not have that in InsetMathGrid.
1959 void InsetMathHull::mathmlize(MathStream & os) const
1961 bool havenumbers = false;
1962 for (size_t i = 0; i != numbered_.size(); ++i) {
1968 bool const havetable = havenumbers || nrows() > 1 || ncols() > 1;
1971 os << MTag("mtable");
1972 char const * const celltag = havetable ? "mtd" : "mrow";
1973 // FIXME There does not seem to be wide support at the moment
1974 // for mlabeledtr, so we have to use just mtr for now.
1975 // char const * const rowtag = havenumbers ? "mlabeledtr" : "mtr";
1976 char const * const rowtag = "mtr";
1977 for (row_type row = 0; row < nrows(); ++row) {
1980 for (col_type col = 0; col < ncols(); ++col) {
1982 << cell(index(row, col))
1988 docstring const & num = numbers_[row];
1990 os << '(' << num << ')';
1997 os << ETag("mtable");
2001 void InsetMathHull::mathAsLatex(WriteStream & os) const
2003 MathEnsurer ensurer(os, false);
2004 bool havenumbers = false;
2005 for (size_t i = 0; i != numbered_.size(); ++i) {
2011 bool const havetable = havenumbers || nrows() > 1 || ncols() > 1;
2014 os << cell(index(0, 0));
2018 os << "<table class='mathtable'>";
2019 for (row_type row = 0; row < nrows(); ++row) {
2021 for (col_type col = 0; col < ncols(); ++col) {
2022 os << "<td class='math'>";
2023 os << cell(index(row, col));
2028 docstring const & num = numbers_[row];
2030 os << '(' << num << ')';
2039 docstring InsetMathHull::xhtml(XHTMLStream & xs, OutputParams const & op) const
2041 BufferParams::MathOutput const mathtype =
2042 buffer().params().html_math_output;
2044 bool success = false;
2045 // FIXME Eventually we would like to do this inset by inset.
2046 if (mathtype == BufferParams::MathML) {
2047 odocstringstream os;
2052 } catch (MathExportException const &) {}
2054 if (getType() == hullSimple)
2055 xs << html::StartTag("math",
2056 "xmlns=\"http://www.w3.org/1998/Math/MathML\"", true);
2058 xs << html::StartTag("math",
2059 "display=\"block\" xmlns=\"http://www.w3.org/1998/Math/MathML\"", true);
2060 xs << XHTMLStream::ESCAPE_NONE
2062 << html::EndTag("math");
2064 } else if (mathtype == BufferParams::HTML) {
2065 odocstringstream os;
2070 } catch (MathExportException const &) {}
2072 string const tag = (getType() == hullSimple) ? "span" : "div";
2073 xs << html::StartTag(tag, "class='formula'", true)
2074 << XHTMLStream::ESCAPE_NONE
2076 << html::EndTag(tag);
2080 // what we actually want is this:
2082 // ((mathtype == BufferParams::MathML || mathtype == BufferParams::HTML)
2084 // || mathtype == BufferParams::Images
2086 // but what follows is equivalent, since we'll enter only if either (a) we
2087 // tried and failed with MathML or HTML or (b) didn't try yet at all but
2088 // aren't doing LaTeX, in which case we are doing Images.
2089 if (!success && mathtype != BufferParams::LaTeX) {
2090 loadPreview(docit_);
2091 graphics::PreviewImage const * pimage = preview_->getPreviewImage(buffer());
2093 // FIXME Do we always have png?
2094 string const tag = (getType() == hullSimple) ? "span" : "div";
2095 FileName const & mathimg = pimage->filename();
2096 xs << html::StartTag(tag)
2097 << html::CompTag("img", "src=\"" + mathimg.onlyFileName() + "\"")
2098 << html::EndTag(tag);
2100 // add the file to the list of files to be exported
2101 op.exportdata->addExternalFile("xhtml", mathimg);
2106 // so we'll pass this test if we've failed everything else, or
2107 // if mathtype was LaTeX, since we won't have entered any of the
2109 if (!success /* || mathtype != BufferParams::LaTeX */) {
2110 string const tag = (getType() == hullSimple) ? "span" : "div";
2111 // Unfortunately, we cannot use latexString() because we do not want
2112 // $...$ or whatever.
2113 odocstringstream ls;
2114 WriteStream wi(ls, false, true, WriteStream::wsPreview);
2115 ModeSpecifier specifier(wi, MATH_MODE);
2117 docstring const latex = ls.str();
2119 // class='math' allows for use of jsMath
2120 // http://www.math.union.edu/~dpvc/jsMath/
2122 // probably should allow for some kind of customization here
2123 xs << html::StartTag(tag, "class='math'")
2124 << XHTMLStream::ESCAPE_AND
2126 << html::EndTag(tag);
2133 void InsetMathHull::toString(odocstream & os) const
2135 plaintext(os, OutputParams(0));
2139 void InsetMathHull::forToc(docstring & os, size_t) const
2141 odocstringstream ods;
2142 plaintext(ods, OutputParams(0));
2147 docstring InsetMathHull::contextMenuName() const
2149 return from_ascii("context-math");
2153 void InsetMathHull::recordLocation(DocIterator const & di)