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/filetools.h"
57 #include "support/gettext.h"
58 #include "support/lstrings.h"
63 using namespace lyx::support;
67 using cap::grabAndEraseSelection;
71 int getCols(HullType type)
88 // returns position of first relation operator in the array
89 // used for "intelligent splitting"
90 size_t firstRelOp(MathData const & ar)
92 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
94 return it - ar.begin();
99 char const * star(bool numbered)
101 return numbered ? "" : "*";
105 } // end anon namespace
108 HullType hullType(docstring const & s)
110 if (s == "none") return hullNone;
111 if (s == "simple") return hullSimple;
112 if (s == "equation") return hullEquation;
113 if (s == "eqnarray") return hullEqnArray;
114 if (s == "align") return hullAlign;
115 if (s == "alignat") return hullAlignAt;
116 if (s == "xalignat") return hullXAlignAt;
117 if (s == "xxalignat") return hullXXAlignAt;
118 if (s == "multline") return hullMultline;
119 if (s == "gather") return hullGather;
120 if (s == "flalign") return hullFlAlign;
121 if (s == "regexp") return hullRegexp;
122 lyxerr << "unknown hull type '" << to_utf8(s) << "'" << endl;
127 docstring hullName(HullType type)
130 case hullNone: return from_ascii("none");
131 case hullSimple: return from_ascii("simple");
132 case hullEquation: return from_ascii("equation");
133 case hullEqnArray: return from_ascii("eqnarray");
134 case hullAlign: return from_ascii("align");
135 case hullAlignAt: return from_ascii("alignat");
136 case hullXAlignAt: return from_ascii("xalignat");
137 case hullXXAlignAt: return from_ascii("xxalignat");
138 case hullMultline: return from_ascii("multline");
139 case hullGather: return from_ascii("gather");
140 case hullFlAlign: return from_ascii("flalign");
141 case hullRegexp: return from_ascii("regexp");
143 lyxerr << "unknown hull type '" << type << "'" << endl;
144 return from_ascii("none");
148 static InsetLabel * dummy_pointer = 0;
150 InsetMathHull::InsetMathHull(Buffer * buf)
151 : InsetMathGrid(buf, 1, 1), type_(hullNone), numbered_(1, true),
152 numbers_(1, empty_docstring()), label_(1, dummy_pointer),
153 preview_(new RenderPreview(this))
155 //lyxerr << "sizeof InsetMath: " << sizeof(InsetMath) << endl;
156 //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl;
157 //lyxerr << "sizeof InsetMathChar: " << sizeof(InsetMathChar) << endl;
158 //lyxerr << "sizeof FontInfo: " << sizeof(FontInfo) << endl;
165 InsetMathHull::InsetMathHull(Buffer * buf, HullType type)
166 : InsetMathGrid(buf, getCols(type), 1), type_(type), numbered_(1, true),
167 numbers_(1, empty_docstring()), label_(1, dummy_pointer),
168 preview_(new RenderPreview(this))
176 InsetMathHull::InsetMathHull(InsetMathHull const & other) : InsetMathGrid(other)
182 InsetMathHull::~InsetMathHull()
184 for (size_t i = 0; i < label_.size(); ++i)
189 Inset * InsetMathHull::clone() const
191 return new InsetMathHull(*this);
195 InsetMathHull & InsetMathHull::operator=(InsetMathHull const & other)
199 InsetMathGrid::operator=(other);
201 numbered_ = other.numbered_;
202 numbers_ = other.numbers_;
203 buffer_ = other.buffer_;
204 for (size_t i = 0; i < label_.size(); ++i)
206 label_ = other.label_;
207 for (size_t i = 0; i != label_.size(); ++i) {
209 label_[i] = new InsetLabel(*label_[i]);
211 preview_.reset(new RenderPreview(*other.preview_, this));
217 void InsetMathHull::setBuffer(Buffer & buffer)
219 InsetMathGrid::setBuffer(buffer);
221 for (size_t i = 0; i != label_.size(); ++i) {
223 label_[i]->setBuffer(buffer);
229 const char * counters_to_save[] = {"section", "chapter"};
230 unsigned int const numcnts = sizeof(counters_to_save)/sizeof(char *);
234 void InsetMathHull::updateBuffer(ParIterator const & it, UpdateType utype)
237 //FIXME: buffer_ should be set at creation for this inset! Problem is
238 // This inset is created at too many places (see Parser::parse1() in
243 // if any of the equations are numbered, then we want to save the values
244 // of some of the counters.
246 BufferParams const & bp = buffer_->params();
247 string const & lang = it->getParLanguage(bp)->code();
249 buffer_->masterBuffer()->params().documentClass().counters();
251 // right now, we only need to do this at export time
252 if (utype == OutputUpdate) {
253 for (size_t i = 0; i < numcnts; ++i) {
254 docstring const cnt = from_ascii(counters_to_save[i]);
255 if (cnts.hasCounter(cnt))
256 counter_map[cnt] = cnts.value(cnt);
260 // this has to be done separately
261 docstring const eqstr = from_ascii("equation");
262 if (cnts.hasCounter(eqstr)) {
263 if (utype == OutputUpdate)
264 counter_map[eqstr] = cnts.value(eqstr);
265 for (size_t i = 0; i != label_.size(); ++i) {
267 cnts.step(eqstr, utype);
268 numbers_[i] = cnts.theCounter(eqstr, lang);
270 numbers_[i] = empty_docstring();
276 for (size_t i = 0; i != label_.size(); ++i) {
278 label_[i]->updateBuffer(it, utype);
281 InsetMathGrid::updateBuffer(it, utype);
285 void InsetMathHull::addToToc(DocIterator const & pit) const
288 //FIXME: buffer_ should be set at creation for this inset! Problem is
289 // This inset is created at too many places (see Parser::parse1() in
294 Toc & toc = buffer().tocBackend().toc("equation");
296 for (row_type row = 0; row != nrows(); ++row) {
300 label_[row]->addToToc(pit);
301 toc.push_back(TocItem(pit, 0, nicelabel(row)));
306 Inset * InsetMathHull::editXY(Cursor & cur, int x, int y)
312 return InsetMathNest::editXY(cur, x, y);
316 InsetMath::mode_type InsetMathHull::currentMode() const
318 if (type_ == hullNone)
319 return UNDECIDED_MODE;
320 // definitely math mode ...
325 bool InsetMathHull::idxFirst(Cursor & cur) const
333 bool InsetMathHull::idxLast(Cursor & cur) const
335 cur.idx() = nargs() - 1;
336 cur.pos() = cur.lastpos();
341 char InsetMathHull::defaultColAlign(col_type col)
343 if (type_ == hullEqnArray)
345 if (type_ == hullGather)
347 if (type_ >= hullAlign)
348 return "rl"[col & 1];
353 int InsetMathHull::defaultColSpace(col_type col)
355 if (type_ == hullAlign || type_ == hullAlignAt)
357 if (type_ == hullXAlignAt)
358 return (col & 1) ? 20 : 0;
359 if (type_ == hullXXAlignAt || type_ == hullFlAlign)
360 return (col & 1) ? 40 : 0;
365 docstring InsetMathHull::standardFont() const
370 font_name = from_ascii("texttt");
373 font_name = from_ascii("lyxnochange");
376 font_name = from_ascii("mathnormal");
382 ColorCode InsetMathHull::standardColor() const
388 color = Color_foreground;
397 bool InsetMathHull::previewState(BufferView * bv) const
399 if (!editing(bv) && RenderPreview::status() == LyXRC::PREVIEW_ON) {
400 graphics::PreviewImage const * pimage =
401 preview_->getPreviewImage(bv->buffer());
402 return pimage && pimage->image();
408 void InsetMathHull::metrics(MetricsInfo & mi, Dimension & dim) const
410 if (previewState(mi.base.bv)) {
411 preview_->metrics(mi, dim);
412 // insert a one pixel gap in front of the formula
415 dim.des += displayMargin();
416 // Cache the inset dimension.
417 setDimCache(mi, dim);
421 FontSetChanger dummy1(mi.base, standardFont());
422 StyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
424 // let the cells adjust themselves
425 InsetMathGrid::metrics(mi, dim);
428 dim.asc += displayMargin();
429 dim.des += displayMargin();
432 if (numberedType()) {
433 FontSetChanger dummy(mi.base, from_ascii("mathbf"));
435 for (row_type row = 0; row < nrows(); ++row)
436 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
442 // make it at least as high as the current font
445 math_font_max_dim(mi.base.font, asc, des);
446 dim.asc = max(dim.asc, asc);
447 dim.des = max(dim.des, des);
448 // Cache the inset dimension.
449 // FIXME: This will overwrite InsetMathGrid dimension, is that OK?
450 setDimCache(mi, dim);
454 ColorCode InsetMathHull::backgroundColor(PainterInfo const & pi) const
456 if (previewState(pi.base.bv))
457 return graphics::PreviewLoader::backgroundColor();
462 void InsetMathHull::drawBackground(PainterInfo & pi, int x, int y) const
464 Dimension const dim = dimension(*pi.base.bv);
465 pi.pain.fillRectangle(x + 1, y - dim.asc + 1, dim.wid - 2,
466 dim.asc + dim.des - 1, pi.backgroundColor(this));
470 void InsetMathHull::draw(PainterInfo & pi, int x, int y) const
472 use_preview_ = previewState(pi.base.bv);
474 if (type_ == hullRegexp) {
475 Dimension const dim = dimension(*pi.base.bv);
476 pi.pain.rectangle(x + 1, y - dim.ascent() + 1,
477 dim.width() - 2, dim.height() - 2, Color_regexpframe);
480 // one pixel gap in front
481 preview_->draw(pi, x + 1, y);
482 setPosCache(pi, x, y);
486 ColorCode color = pi.selected && lyxrc.use_system_colors
487 ? Color_selectiontext : standardColor();
488 bool const really_change_color = pi.base.font.color() == Color_none;
489 ColorChanger dummy0(pi.base.font, color, really_change_color);
490 FontSetChanger dummy1(pi.base, standardFont());
491 StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
493 InsetMathGrid::draw(pi, x + 1, y);
495 if (numberedType()) {
496 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
497 for (row_type row = 0; row < nrows(); ++row) {
498 int const yy = y + rowinfo_[row].offset_;
499 FontSetChanger dummy(pi.base, from_ascii("mathrm"));
500 docstring const nl = nicelabel(row);
504 setPosCache(pi, x, y);
508 void InsetMathHull::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
511 InsetMathGrid::metricsT(mi, dim);
514 WriteStream wi(os, false, true, WriteStream::wsDefault);
516 dim.wid = os.str().size();
523 void InsetMathHull::drawT(TextPainter & pain, int x, int y) const
526 InsetMathGrid::drawT(pain, x, y);
529 WriteStream wi(os, false, true, WriteStream::wsDefault);
531 pain.draw(x, y, os.str().c_str());
536 static docstring latexString(InsetMathHull const & inset)
539 // This has to be static, because a preview snippet or a math
540 // macro containing math in text mode (such as $\text{$\phi$}$ or
541 // \newcommand{\xxx}{\text{$\phi$}}) gets processed twice. The
542 // first time as a whole, and the second time only the inner math.
543 // In this last case inset.buffer() would be invalid.
544 static Encoding const * encoding = 0;
545 if (inset.isBufferValid())
546 encoding = &(inset.buffer().params().encoding());
547 WriteStream wi(ls, false, true, WriteStream::wsPreview, encoding);
553 void InsetMathHull::initUnicodeMath() const
555 // Trigger classification of the unicode symbols in this inset
556 docstring const dummy = latexString(*this);
560 void InsetMathHull::addPreview(DocIterator const & inset_pos,
561 graphics::PreviewLoader & /*ploader*/) const
563 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
564 preparePreview(inset_pos);
569 void InsetMathHull::preparePreview(DocIterator const & pos,
570 bool forexport) const
572 // there is no need to do all the macro stuff if we're not
573 // actually going to generate the preview.
574 if (RenderPreview::status() != LyXRC::PREVIEW_ON && !forexport)
577 Buffer const * buffer = pos.buffer();
579 // collect macros at this position
581 buffer->listMacroNames(macros);
582 MacroNameSet::iterator it = macros.begin();
583 MacroNameSet::iterator end = macros.end();
584 odocstringstream macro_preamble;
585 for (; it != end; ++it) {
586 MacroData const * data = buffer->getMacro(*it, pos, true);
588 data->write(macro_preamble, true);
589 macro_preamble << endl;
594 if (forexport && haveNumbers()) {
595 docstring eqstr = from_ascii("equation");
596 CounterMap::const_iterator it = counter_map.find(eqstr);
597 if (it != counter_map.end()) {
598 int num = it->second;
600 setcnt += from_ascii("\\setcounter{") + eqstr + '}' +
601 '{' + convert<docstring>(num) + '}' + '\n';
603 for (size_t i = 0; i != numcnts; ++i) {
604 docstring cnt = from_ascii(counters_to_save[i]);
605 it = counter_map.find(cnt);
606 if (it == counter_map.end())
608 int num = it->second;
610 setcnt += from_ascii("\\setcounter{") + cnt + '}' +
611 '{' + convert<docstring>(num) + '}';
614 docstring const snippet = macro_preamble.str() +
615 setcnt + latexString(*this);
616 LYXERR(Debug::MACROS, "Preview snippet: " << snippet);
617 preview_->addPreview(snippet, *buffer, forexport);
621 void InsetMathHull::reloadPreview(DocIterator const & pos) const
624 preview_->startLoading(*pos.buffer());
628 void InsetMathHull::loadPreview(DocIterator const & pos) const
630 bool const forexport = true;
631 preparePreview(pos, forexport);
632 preview_->startLoading(*pos.buffer(), forexport);
636 bool InsetMathHull::notifyCursorLeaves(Cursor const & old, Cursor & cur)
638 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
640 cur.screenUpdateFlags(Update::Force);
646 docstring InsetMathHull::label(row_type row) const
648 LASSERT(row < nrows(), /**/);
649 if (InsetLabel * il = label_[row])
650 return il->screenLabel();
655 void InsetMathHull::label(row_type row, docstring const & label)
657 //lyxerr << "setting label '" << label << "' for row " << row << endl;
661 label_[row] = dummy_pointer;
664 label_[row]->updateCommand(label);
666 label_[row]->setParam("name", label);
670 InsetCommandParams p(LABEL_CODE);
672 label_[row] = new InsetLabel(buffer_, p);
674 label_[row]->setBuffer(buffer());
678 void InsetMathHull::numbered(row_type row, bool num)
680 numbered_[row] = num;
681 if (!numbered_[row] && label_[row]) {
688 bool InsetMathHull::numbered(row_type row) const
690 return numbered_[row];
694 bool InsetMathHull::ams() const
696 return type_ == hullAlign
697 || type_ == hullFlAlign
698 || type_ == hullMultline
699 || type_ == hullGather
700 || type_ == hullAlignAt
701 || type_ == hullXAlignAt
702 || type_ == hullXXAlignAt;
706 Inset::DisplayType InsetMathHull::display() const
708 if (type_ == hullSimple || type_ == hullNone || type_ == hullRegexp)
713 bool InsetMathHull::numberedType() const
715 if (type_ == hullNone)
717 if (type_ == hullSimple)
719 if (type_ == hullXXAlignAt)
721 if (type_ == hullRegexp)
723 for (row_type row = 0; row < nrows(); ++row)
730 void InsetMathHull::validate(LaTeXFeatures & features) const
732 if (features.runparams().isLaTeX()) {
734 features.require("amsmath");
736 if (type_ == hullRegexp) {
737 features.require("color");
738 string frcol = lcolor.getLaTeXName(Color_regexpframe);
739 string bgcol = "white";
740 features.addPreambleSnippet(
741 string("\\newcommand{\\regexp}[1]{\\fcolorbox{")
742 + frcol + string("}{")
743 + bgcol + string("}{\\texttt{#1}}}"));
746 // Validation is necessary only if not using AMS math.
747 // To be safe, we will always run mathedvalidate.
748 //if (features.amsstyle)
751 //features.binom = true;
752 } else if (features.runparams().math_flavor == OutputParams::MathAsHTML) {
753 // it would be better to do this elsewhere, but we can't validate in
754 // InsetMathMatrix and we have no way, outside MathExtern, to know if
755 // we even have any matrices.
756 features.addPreambleSnippet("<style type=\"text/css\">\n"
757 "table.matrix{display: inline-block; vertical-align: middle; text-align:center;}\n"
758 "table.matrix td{padding: 0.25px;}\n"
759 "td.ldelim{width: 0.5ex; border: thin solid black; border-right: none;}\n"
760 "td.rdelim{width: 0.5ex; border: thin solid black; border-left: none;}\n"
763 InsetMathGrid::validate(features);
767 void InsetMathHull::header_write(WriteStream & os) const
769 bool n = numberedType();
783 os << "\n\\begin{equation" << star(n) << "}\n";
793 os << "\n\\begin{" << hullName(type_) << star(n) << "}\n";
798 os << "\n\\begin{" << hullName(type_) << star(n) << '}'
799 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
803 os << "\n\\begin{" << hullName(type_) << '}'
804 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
812 os << "\n\\begin{unknown" << star(n) << "}\n";
818 void InsetMathHull::footer_write(WriteStream & os) const
820 bool n = numberedType();
833 os << "\n\\end{equation" << star(n) << "}\n";
845 os << "\n\\end{" << hullName(type_) << star(n) << "}\n";
849 os << "\n\\end{" << hullName(type_) << "}\n";
857 os << "\n\\end{unknown" << star(n) << "}\n";
863 bool InsetMathHull::rowChangeOK() const
866 type_ == hullEqnArray || type_ == hullAlign ||
867 type_ == hullFlAlign || type_ == hullAlignAt ||
868 type_ == hullXAlignAt || type_ == hullXXAlignAt ||
869 type_ == hullGather || type_ == hullMultline;
873 bool InsetMathHull::colChangeOK() const
876 type_ == hullAlign || type_ == hullFlAlign ||type_ == hullAlignAt ||
877 type_ == hullXAlignAt || type_ == hullXXAlignAt;
881 void InsetMathHull::addRow(row_type row)
886 bool numbered = numberedType();
887 // Move the number and raw pointer, do not call label() (bug 7511)
888 InsetLabel * label = dummy_pointer;
889 docstring number = empty_docstring();
890 if (type_ == hullMultline) {
891 if (row + 1 == nrows()) {
892 numbered_[row] = false;
893 swap(label, label_[row]);
894 swap(number, numbers_[row]);
899 numbered_.insert(numbered_.begin() + row + 1, numbered);
900 numbers_.insert(numbers_.begin() + row + 1, number);
901 label_.insert(label_.begin() + row + 1, label);
902 InsetMathGrid::addRow(row);
906 void InsetMathHull::swapRow(row_type row)
910 if (row + 1 == nrows())
912 // gcc implements the standard std::vector<bool> which is *not* a container:
913 // http://www.gotw.ca/publications/N1185.pdf
914 // As a results, it doesn't like this:
915 // swap(numbered_[row], numbered_[row + 1]);
916 // so we do it manually:
917 bool const b = numbered_[row];
918 numbered_[row] = numbered_[row + 1];
919 numbered_[row + 1] = b;
920 swap(numbers_[row], numbers_[row + 1]);
921 swap(label_[row], label_[row + 1]);
922 InsetMathGrid::swapRow(row);
926 void InsetMathHull::delRow(row_type row)
928 if (nrows() <= 1 || !rowChangeOK())
930 if (row + 1 == nrows() && type_ == hullMultline) {
931 bool const b = numbered_[row - 1];
932 numbered_[row - 1] = numbered_[row];
934 swap(numbers_[row - 1], numbers_[row]);
935 swap(label_[row - 1], label_[row]);
936 InsetMathGrid::delRow(row);
939 InsetMathGrid::delRow(row);
940 // The last dummy row has no number info nor a label.
941 // Test nrows() + 1 because we have already erased the row.
942 if (row == nrows() + 1)
944 numbered_.erase(numbered_.begin() + row);
945 numbers_.erase(numbers_.begin() + row);
947 label_.erase(label_.begin() + row);
951 void InsetMathHull::addCol(col_type col)
955 InsetMathGrid::addCol(col);
959 void InsetMathHull::delCol(col_type col)
961 if (ncols() <= 1 || !colChangeOK())
963 InsetMathGrid::delCol(col);
967 docstring InsetMathHull::nicelabel(row_type row) const
971 docstring const & val = numbers_[row];
973 return '(' + val + ')';
974 return '(' + val + ',' + label_[row]->screenLabel() + ')';
978 void InsetMathHull::glueall(HullType type)
981 for (idx_type i = 0; i < nargs(); ++i)
983 InsetLabel * label = 0;
984 if (type == hullEquation) {
985 // preserve first non-empty label
986 for (row_type row = 0; row < nrows(); ++row) {
994 *this = InsetMathHull(buffer_, hullSimple);
1001 void InsetMathHull::splitTo2Cols()
1003 LASSERT(ncols() == 1, /**/);
1004 InsetMathGrid::addCol(1);
1005 for (row_type row = 0; row < nrows(); ++row) {
1006 idx_type const i = 2 * row;
1007 pos_type pos = firstRelOp(cell(i));
1008 cell(i + 1) = MathData(buffer_, cell(i).begin() + pos, cell(i).end());
1009 cell(i).erase(pos, cell(i).size());
1014 void InsetMathHull::splitTo3Cols()
1016 LASSERT(ncols() < 3, /**/);
1019 InsetMathGrid::addCol(2);
1020 for (row_type row = 0; row < nrows(); ++row) {
1021 idx_type const i = 3 * row + 1;
1022 if (cell(i).size()) {
1023 cell(i + 1) = MathData(buffer_, cell(i).begin() + 1, cell(i).end());
1024 cell(i).erase(1, cell(i).size());
1030 void InsetMathHull::changeCols(col_type cols)
1032 if (ncols() == cols)
1034 else if (ncols() < cols) {
1040 while (ncols() < cols)
1041 InsetMathGrid::addCol(ncols());
1047 for (row_type row = 0; row < nrows(); ++row) {
1048 idx_type const i = row * ncols();
1049 for (col_type col = cols; col < ncols(); ++col) {
1050 cell(i + cols - 1).append(cell(i + col));
1054 while (ncols() > cols) {
1055 InsetMathGrid::delCol(ncols() - 1);
1060 HullType InsetMathHull::getType() const
1066 void InsetMathHull::setType(HullType type)
1073 void InsetMathHull::mutate(HullType newtype)
1075 //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
1077 // we try to move along the chain
1078 // none <-> simple <-> equation <-> eqnarray -> *align* -> multline, gather -+
1080 // +-------------------------------------+
1081 // we use eqnarray as intermediate type for mutations that are not
1082 // directly supported because it handles labels and numbering for
1085 if (newtype == type_) {
1089 else if (newtype < hullNone) {
1094 else if (type_ == hullNone) {
1095 setType(hullSimple);
1100 else if (type_ == hullSimple) {
1101 if (newtype == hullNone) {
1105 setType(hullEquation);
1106 numbered(0, label_[0] ? true : false);
1111 else if (type_ == hullEquation) {
1112 if (newtype < type_) {
1113 setType(hullSimple);
1116 } else if (newtype == hullEqnArray) {
1117 // split it "nicely" on the first relop
1119 setType(hullEqnArray);
1120 } else if (newtype == hullMultline || newtype == hullGather) {
1123 // split it "nicely"
1130 else if (type_ == hullEqnArray) {
1131 if (newtype < type_) {
1134 } else { // align & Co.
1141 else if (type_ == hullAlign || type_ == hullAlignAt ||
1142 type_ == hullXAlignAt || type_ == hullFlAlign) {
1143 if (newtype < hullAlign) {
1145 setType(hullEqnArray);
1147 } else if (newtype == hullGather || newtype == hullMultline) {
1150 } else if (newtype == hullXXAlignAt) {
1151 for (row_type row = 0; row < nrows(); ++row)
1152 numbered(row, false);
1159 else if (type_ == hullXXAlignAt) {
1160 for (row_type row = 0; row < nrows(); ++row)
1161 numbered(row, false);
1162 if (newtype < hullAlign) {
1164 setType(hullEqnArray);
1166 } else if (newtype == hullGather || newtype == hullMultline) {
1174 else if (type_ == hullMultline || type_ == hullGather) {
1175 if (newtype == hullGather || newtype == hullMultline)
1177 else if (newtype == hullAlign || newtype == hullFlAlign ||
1178 newtype == hullAlignAt || newtype == hullXAlignAt) {
1181 } else if (newtype == hullXXAlignAt) {
1183 for (row_type row = 0; row < nrows(); ++row)
1184 numbered(row, false);
1188 setType(hullEqnArray);
1194 lyxerr << "mutation from '" << to_utf8(hullName(type_))
1195 << "' to '" << to_utf8(hullName(newtype))
1196 << "' not implemented" << endl;
1201 docstring InsetMathHull::eolString(row_type row, bool fragile, bool latex,
1202 bool last_eoln) const
1205 if (numberedType()) {
1206 if (label_[row] && numbered_[row]) {
1207 docstring const name =
1208 latex ? escape(label_[row]->getParam("name"))
1209 : label_[row]->getParam("name");
1210 res += "\\label{" + name + '}';
1212 if (!numbered_[row] && (type_ != hullMultline))
1213 res += "\\nonumber ";
1215 // Never add \\ on the last empty line of eqnarray and friends
1217 return res + InsetMathGrid::eolString(row, fragile, latex, last_eoln);
1221 void InsetMathHull::write(WriteStream & os) const
1223 ModeSpecifier specifier(os, MATH_MODE);
1225 InsetMathGrid::write(os);
1230 void InsetMathHull::normalize(NormalStream & os) const
1232 os << "[formula " << hullName(type_) << ' ';
1233 InsetMathGrid::normalize(os);
1238 void InsetMathHull::infoize(odocstream & os) const
1240 os << "Type: " << hullName(type_);
1244 void InsetMathHull::check() const
1246 LASSERT(numbered_.size() == nrows(), /**/);
1247 LASSERT(numbers_.size() == nrows(), /**/);
1248 LASSERT(label_.size() == nrows(), /**/);
1252 void InsetMathHull::doExtern(Cursor & cur, FuncRequest & func)
1256 idocstringstream iss(func.argument());
1257 iss >> dlang >> extra;
1259 extra = from_ascii("noextra");
1260 string const lang = to_ascii(dlang);
1262 // FIXME: temporarily disabled
1263 //if (cur.selection()) {
1266 // lyxerr << "use selection: " << ar << endl;
1267 // insert(pipeThroughExtern(lang, extra, ar));
1271 // only inline, display or eqnarray math is allowed
1272 if (getType() > hullEqnArray) {
1273 frontend::Alert::warning(_("Bad math environment"),
1274 _("Computation cannot be performed for AMS "
1275 "math environments.\nChange the math "
1276 "formula type and try again."));
1281 eq.push_back(MathAtom(new InsetMathChar('=')));
1283 // go to first item in line
1284 cur.idx() -= cur.idx() % ncols();
1287 if (getType() == hullSimple) {
1288 size_type pos = cur.cell().find_last(eq);
1290 if (cur.inMathed() && cur.selection()) {
1291 asArray(grabAndEraseSelection(cur), ar);
1292 } else if (pos == cur.cell().size()) {
1294 lyxerr << "use whole cell: " << ar << endl;
1296 ar = MathData(buffer_, cur.cell().begin() + pos + 1, cur.cell().end());
1297 lyxerr << "use partial cell form pos: " << pos << endl;
1299 cur.cell().append(eq);
1300 cur.cell().append(pipeThroughExtern(lang, extra, ar));
1301 cur.pos() = cur.lastpos();
1305 if (getType() == hullEquation) {
1306 lyxerr << "use equation inset" << endl;
1307 mutate(hullEqnArray);
1308 MathData & ar = cur.cell();
1309 lyxerr << "use cell: " << ar << endl;
1313 cur.cell() = pipeThroughExtern(lang, extra, ar);
1314 // move to end of line
1315 cur.pos() = cur.lastpos();
1320 lyxerr << "use eqnarray" << endl;
1321 cur.idx() += 2 - cur.idx() % ncols();
1323 MathData ar = cur.cell();
1324 lyxerr << "use cell: " << ar << endl;
1325 // FIXME: temporarily disabled
1331 cur.cell() = pipeThroughExtern(lang, extra, ar);
1332 cur.pos() = cur.lastpos();
1337 void InsetMathHull::doDispatch(Cursor & cur, FuncRequest & cmd)
1339 //lyxerr << "action: " << cmd.action() << endl;
1340 switch (cmd.action()) {
1342 case LFUN_FINISHED_BACKWARD:
1343 case LFUN_FINISHED_FORWARD:
1344 case LFUN_FINISHED_RIGHT:
1345 case LFUN_FINISHED_LEFT:
1346 //lyxerr << "action: " << cmd.action() << endl;
1347 InsetMathGrid::doDispatch(cur, cmd);
1351 case LFUN_BREAK_PARAGRAPH:
1352 // just swallow this
1355 case LFUN_NEWLINE_INSERT:
1356 // some magic for the common case
1357 if (type_ == hullSimple || type_ == hullEquation) {
1358 cur.recordUndoInset();
1360 cur.bv().buffer().params().use_amsmath == BufferParams::package_on;
1361 mutate(align ? hullAlign : hullEqnArray);
1362 // mutate() may change labels and such.
1363 cur.forceBufferUpdate();
1364 cur.idx() = nrows() * ncols() - 1;
1365 cur.pos() = cur.lastpos();
1367 InsetMathGrid::doDispatch(cur, cmd);
1370 case LFUN_MATH_NUMBER_TOGGLE: {
1371 //lyxerr << "toggling all numbers" << endl;
1372 cur.recordUndoInset();
1373 bool old = numberedType();
1374 if (type_ == hullMultline)
1375 numbered(nrows() - 1, !old);
1377 for (row_type row = 0; row < nrows(); ++row)
1378 numbered(row, !old);
1380 cur.message(old ? _("No number") : _("Number"));
1381 cur.forceBufferUpdate();
1385 case LFUN_MATH_NUMBER_LINE_TOGGLE: {
1386 cur.recordUndoInset();
1387 row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1388 bool old = numbered(r);
1389 cur.message(old ? _("No number") : _("Number"));
1391 cur.forceBufferUpdate();
1395 case LFUN_LABEL_INSERT: {
1396 cur.recordUndoInset();
1397 row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1398 docstring old_label = label(r);
1399 docstring const default_label = from_ascii("eq:");
1400 if (old_label.empty())
1401 old_label = default_label;
1403 InsetCommandParams p(LABEL_CODE);
1404 p["name"] = cmd.argument().empty() ? old_label : cmd.argument();
1405 string const data = InsetCommand::params2string(p);
1407 if (cmd.argument().empty())
1408 cur.bv().showDialog("label", data);
1410 FuncRequest fr(LFUN_INSET_INSERT, data);
1416 case LFUN_LABEL_COPY_AS_REF: {
1418 if (cmd.argument().empty() && &cur.inset() == this)
1419 // if there is no argument and we're inside math, we retrieve
1420 // the row number from the cursor position.
1421 row = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1423 // if there is an argument, find the corresponding label, else
1424 // check whether there is at least one label.
1425 for (row = 0; row != nrows(); ++row)
1426 if (numbered_[row] && label_[row]
1427 && (cmd.argument().empty() || label(row) == cmd.argument()))
1434 InsetCommandParams p(REF_CODE, "ref");
1435 p["reference"] = label(row);
1436 cap::clearSelection();
1437 cap::copyInset(cur, new InsetRef(buffer_, p), label(row));
1441 case LFUN_WORD_DELETE_FORWARD:
1442 case LFUN_CHAR_DELETE_FORWARD:
1443 if (col(cur.idx()) + 1 == ncols()
1444 && cur.pos() == cur.lastpos()
1445 && !cur.selection()) {
1446 if (!label(row(cur.idx())).empty()) {
1447 cur.recordUndoInset();
1448 label(row(cur.idx()), docstring());
1449 } else if (numbered(row(cur.idx()))) {
1450 cur.recordUndoInset();
1451 numbered(row(cur.idx()), false);
1452 cur.forceBufferUpdate();
1454 InsetMathGrid::doDispatch(cur, cmd);
1458 InsetMathGrid::doDispatch(cur, cmd);
1463 case LFUN_INSET_INSERT: {
1464 //lyxerr << "arg: " << to_utf8(cmd.argument()) << endl;
1465 // FIXME: this should be cleaned up to use InsetLabel methods directly.
1466 string const name = cmd.getArg(0);
1467 if (name == "label") {
1468 InsetCommandParams p(LABEL_CODE);
1469 InsetCommand::string2params(to_utf8(cmd.argument()), p);
1470 docstring str = p["name"];
1471 cur.recordUndoInset();
1472 row_type const r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1476 docstring old = label(r);
1479 // The label will take care of the reference update.
1483 // Newly created inset so initialize it.
1484 label_[r]->initView();
1487 cur.forceBufferUpdate();
1490 InsetMathGrid::doDispatch(cur, cmd);
1494 case LFUN_MATH_EXTERN:
1495 cur.recordUndoInset();
1499 case LFUN_MATH_MUTATE: {
1500 cur.recordUndoInset();
1501 row_type row = cur.row();
1502 col_type col = cur.col();
1503 mutate(hullType(cmd.argument()));
1504 cur.idx() = row * ncols() + col;
1505 if (cur.idx() > cur.lastidx()) {
1506 cur.idx() = cur.lastidx();
1507 cur.pos() = cur.lastpos();
1509 if (cur.pos() > cur.lastpos())
1510 cur.pos() = cur.lastpos();
1512 cur.forceBufferUpdate();
1513 // FIXME: find some more clever handling of the selection,
1514 // i.e. preserve it.
1515 cur.clearSelection();
1516 //cur.dispatched(FINISHED);
1520 case LFUN_MATH_DISPLAY: {
1521 cur.recordUndoInset();
1522 mutate(type_ == hullSimple ? hullEquation : hullSimple);
1524 cur.pos() = cur.lastpos();
1525 //cur.dispatched(FINISHED);
1530 InsetMathGrid::doDispatch(cur, cmd);
1536 bool InsetMathHull::getStatus(Cursor & cur, FuncRequest const & cmd,
1537 FuncStatus & status) const
1539 switch (cmd.action()) {
1540 case LFUN_FINISHED_BACKWARD:
1541 case LFUN_FINISHED_FORWARD:
1542 case LFUN_FINISHED_RIGHT:
1543 case LFUN_FINISHED_LEFT:
1546 case LFUN_NEWLINE_INSERT:
1547 case LFUN_MATH_EXTERN:
1549 status.setEnabled(true);
1552 // we never allow this in math, and we want to bind enter
1553 // to another actions in command-alternatives
1554 case LFUN_BREAK_PARAGRAPH:
1555 status.setEnabled(false);
1557 case LFUN_MATH_MUTATE: {
1558 HullType const ht = hullType(cmd.argument());
1559 status.setOnOff(type_ == ht);
1560 status.setEnabled(true);
1562 if (ht != hullSimple) {
1563 Cursor tmpcur = cur;
1564 while (!tmpcur.empty()) {
1565 InsetCode code = tmpcur.inset().lyxCode();
1566 if (code == BOX_CODE) {
1568 } else if (code == TABULAR_CODE) {
1569 FuncRequest tmpcmd(LFUN_MATH_DISPLAY);
1570 if (tmpcur.getStatus(tmpcmd, status) && !status.enabled())
1578 case LFUN_MATH_DISPLAY: {
1580 if (cur.depth() > 1) {
1581 Inset const & in = cur[cur.depth()-2].inset();
1582 if (in.lyxCode() == SCRIPT_CODE)
1583 enable = display() != Inline;
1585 status.setEnabled(enable);
1589 case LFUN_MATH_NUMBER_TOGGLE:
1590 // FIXME: what is the right test, this or the one of
1592 status.setEnabled(display() != Inline);
1593 status.setOnOff(numberedType());
1596 case LFUN_MATH_NUMBER_LINE_TOGGLE: {
1597 // FIXME: what is the right test, this or the one of
1599 bool const enable = (type_ == hullMultline)
1600 ? (nrows() - 1 == cur.row())
1601 : display() != Inline;
1602 row_type const r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1603 status.setEnabled(enable);
1604 status.setOnOff(enable && numbered(r));
1608 case LFUN_LABEL_INSERT:
1609 status.setEnabled(type_ != hullSimple);
1612 case LFUN_LABEL_COPY_AS_REF: {
1613 bool enabled = false;
1615 if (cmd.argument().empty() && &cur.inset() == this) {
1616 // if there is no argument and we're inside math, we retrieve
1617 // the row number from the cursor position.
1618 row = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1619 enabled = numberedType() && label_[row] && numbered_[row];
1621 // if there is an argument, find the corresponding label, else
1622 // check whether there is at least one label.
1623 for (row_type row = 0; row != nrows(); ++row) {
1624 if (numbered_[row] && label_[row] &&
1625 (cmd.argument().empty() || label(row) == cmd.argument())) {
1631 status.setEnabled(enabled);
1635 case LFUN_INSET_INSERT:
1636 if (cmd.getArg(0) == "label") {
1637 status.setEnabled(type_ != hullSimple);
1640 return InsetMathGrid::getStatus(cur, cmd, status);
1642 case LFUN_INSET_MODIFY: {
1643 istringstream is(to_utf8(cmd.argument()));
1647 return InsetMathGrid::getStatus(cur, cmd, status);
1650 && (s == "append-row"
1651 || s == "delete-row"
1652 || s == "copy-row")) {
1653 status.message(bformat(
1654 from_utf8(N_("Can't change number of rows in '%1$s'")),
1656 status.setEnabled(false);
1660 && (s == "append-column"
1661 || s == "delete-column"
1662 || s == "copy-column")) {
1663 status.message(bformat(
1664 from_utf8(N_("Can't change number of columns in '%1$s'")),
1666 status.setEnabled(false);
1669 if ((type_ == hullSimple
1670 || type_ == hullEquation
1671 || type_ == hullNone) &&
1672 (s == "add-hline-above" || s == "add-hline-below")) {
1673 status.message(bformat(
1674 from_utf8(N_("Can't add horizontal grid lines in '%1$s'")),
1676 status.setEnabled(false);
1679 if (s == "add-vline-left" || s == "add-vline-right") {
1680 status.message(bformat(
1681 from_utf8(N_("Can't add vertical grid lines in '%1$s'")),
1683 status.setEnabled(false);
1686 if (s == "valign-top" || s == "valign-middle"
1687 || s == "valign-bottom" || s == "align-left"
1688 || s == "align-center" || s == "align-right") {
1689 status.setEnabled(false);
1692 return InsetMathGrid::getStatus(cur, cmd, status);
1696 return InsetMathGrid::getStatus(cur, cmd, status);
1699 // This cannot really happen, but inserted to shut-up gcc
1700 return InsetMathGrid::getStatus(cur, cmd, status);
1704 /////////////////////////////////////////////////////////////////////
1708 // simply scrap this function if you want
1709 void InsetMathHull::mutateToText()
1712 // translate to latex
1714 latex(os, false, false);
1715 string str = os.str();
1718 Text * lt = view_->cursor().innerText();
1719 string::const_iterator cit = str.begin();
1720 string::const_iterator end = str.end();
1721 for (; cit != end; ++cit)
1722 view_->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
1725 //dispatch(LFUN_ESCAPE);
1730 void InsetMathHull::handleFont(Cursor & cur, docstring const & arg,
1731 docstring const & font)
1733 // this whole function is a hack and won't work for incremental font
1736 if (cur.inset().asInsetMath()->name() == font)
1737 cur.handleFont(to_utf8(font));
1739 cur.handleNest(createInsetMath(font, cur.buffer()));
1745 void InsetMathHull::handleFont2(Cursor & cur, docstring const & arg)
1750 font.fromString(to_utf8(arg), b);
1751 if (font.fontInfo().color() != Color_inherit) {
1752 MathAtom at = MathAtom(new InsetMathColor(buffer_, true, font.fontInfo().color()));
1753 cur.handleNest(at, 0);
1758 void InsetMathHull::edit(Cursor & cur, bool front, EntryDirection entry_from)
1761 bool enter_front = (entry_from == Inset::ENTRY_DIRECTION_LEFT ||
1762 (entry_from == Inset::ENTRY_DIRECTION_IGNORE && front));
1763 enter_front ? idxFirst(cur) : idxLast(cur);
1764 // The inset formula dimension is not necessarily the same as the
1765 // one of the instant preview image, so we have to indicate to the
1766 // BufferView that a metrics update is needed.
1767 cur.screenUpdateFlags(Update::Force);
1771 void InsetMathHull::revealCodes(Cursor & cur) const
1773 if (!cur.inMathed())
1775 odocstringstream os;
1777 cur.message(os.str());
1779 // write something to the minibuffer
1780 // translate to latex
1784 string str = os.str();
1786 string::size_type pos = 0;
1788 for (string::iterator it = str.begin(); it != str.end(); ++it) {
1791 else if (*it == '\0') {
1793 pos = it - str.begin();
1799 res = res.substr(pos - 30);
1800 if (res.size() > 60)
1801 res = res.substr(0, 60);
1807 /////////////////////////////////////////////////////////////////////
1811 bool InsetMathHull::searchForward(BufferView * bv, string const & str,
1814 // FIXME: completely broken
1815 static InsetMathHull * lastformula = 0;
1816 static CursorBase current = DocIterator(ibegin(nucleus()));
1818 static string laststr;
1820 if (lastformula != this || laststr != str) {
1821 //lyxerr << "reset lastformula to " << this << endl;
1824 current = ibegin(nucleus());
1826 mathed_parse_cell(ar, str, Parse::NORMAL, &buffer());
1830 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
1832 for (DocIterator it = current; it != iend(nucleus()); increment(it)) {
1833 CursorSlice & top = it.back();
1834 MathData const & a = top.asInsetMath()->cell(top.idx_);
1835 if (a.matchpart(ar, top.pos_)) {
1836 bv->cursor().setSelection(it, ar.size());
1838 top.pos_ += ar.size();
1844 //lyxerr << "not found!" << endl;
1851 void InsetMathHull::write(ostream & os) const
1853 odocstringstream oss;
1854 WriteStream wi(oss, false, false, WriteStream::wsDefault);
1857 os << to_utf8(oss.str());
1861 void InsetMathHull::read(Lexer & lex)
1864 mathed_parse_normal(buffer_, at, lex, Parse::TRACKMACRO);
1865 operator=(*at->asHullInset());
1869 bool InsetMathHull::readQuiet(Lexer & lex)
1872 bool success = mathed_parse_normal(buffer_, at, lex, Parse::QUIET);
1874 operator=(*at->asHullInset());
1879 int InsetMathHull::plaintext(odocstream & os, OutputParams const &) const
1881 // disables ASCII-art for export of equations. See #2275.
1882 if (0 && display()) {
1886 TextPainter tpain(dim.width(), dim.height());
1887 drawT(tpain, 0, dim.ascent());
1889 // reset metrics cache to "real" values
1891 return tpain.textheight();
1893 odocstringstream oss;
1894 Encoding const * const enc = encodings.fromLyXName("utf8");
1895 WriteStream wi(oss, false, true, WriteStream::wsDefault, enc);
1897 if (type_ == hullRegexp)
1900 for (row_type r = 0; r < nrows(); ++r) {
1901 for (col_type c = 0; c < ncols(); ++c)
1902 wi << (c == 0 ? "" : "\t") << cell(index(r, c));
1906 docstring const str = oss.str();
1913 int InsetMathHull::docbook(odocstream & os, OutputParams const & runparams) const
1918 if (getType() == hullSimple)
1919 name = from_ascii("inlineequation");
1921 name = from_ascii("informalequation");
1923 docstring bname = name;
1924 if (!label(0).empty())
1925 bname += " id='" + sgml::cleanID(buffer(), runparams, label(0)) + "'";
1927 ++ms.tab(); ms.cr(); ms.os() << '<' << bname << '>';
1929 odocstringstream ls;
1930 if (runparams.flavor == OutputParams::XML) {
1931 ms << MTag("alt role='tex' ");
1932 // Workaround for db2latex: db2latex always includes equations with
1933 // \ensuremath{} or \begin{display}\end{display}
1934 // so we strip LyX' math environment
1935 WriteStream wi(ls, false, false, WriteStream::wsDefault, runparams.encoding);
1936 InsetMathGrid::write(wi);
1937 ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&"), "<", "<"));
1942 InsetMathGrid::mathmlize(ms);
1947 otexstream ols(ls, texrow);
1948 ms << MTag("alt role='tex'");
1949 latex(ols, runparams);
1950 res = texrow.rows();
1951 ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&"), "<", "<"));
1955 ms << from_ascii("<graphic fileref=\"eqn/");
1956 if (!label(0).empty())
1957 ms << sgml::cleanID(buffer(), runparams, label(0));
1959 ms << sgml::uniqueID(from_ascii("anon"));
1961 if (runparams.flavor == OutputParams::XML)
1962 ms << from_ascii("\"/>");
1964 ms << from_ascii("\">");
1966 ms.cr(); --ms.tab(); ms.os() << "</" << name << '>';
1968 return ms.line() + res;
1972 bool InsetMathHull::haveNumbers() const
1974 bool havenumbers = false;
1975 // inline formulas are never numbered (bug 7351 part 3)
1976 if (getType() == hullSimple)
1978 for (size_t i = 0; i != numbered_.size(); ++i) {
1989 // We need to do something about alignment here.
1991 // This duplicates code from InsetMathGrid, but
1992 // we need access here to number information,
1993 // and we simply do not have that in InsetMathGrid.
1994 void InsetMathHull::htmlize(HtmlStream & os) const
1996 bool const havenumbers = haveNumbers();
1997 bool const havetable = havenumbers || nrows() > 1 || ncols() > 1;
2000 os << cell(index(0, 0));
2004 os << MTag("table", "class='mathtable'");
2005 for (row_type row = 0; row < nrows(); ++row) {
2007 for (col_type col = 0; col < ncols(); ++col) {
2009 os << cell(index(row, col));
2014 docstring const & num = numbers_[row];
2016 os << '(' << num << ')';
2021 os << ETag("table");
2025 // this duplicates code from InsetMathGrid, but
2026 // we need access here to number information,
2027 // and we simply do not have that in InsetMathGrid.
2028 void InsetMathHull::mathmlize(MathStream & os) const
2030 bool const havenumbers = haveNumbers();
2031 bool const havetable = havenumbers || nrows() > 1 || ncols() > 1;
2034 os << MTag("mtable");
2035 char const * const celltag = havetable ? "mtd" : "mrow";
2036 // FIXME There does not seem to be wide support at the moment
2037 // for mlabeledtr, so we have to use just mtr for now.
2038 // char const * const rowtag = havenumbers ? "mlabeledtr" : "mtr";
2039 char const * const rowtag = "mtr";
2040 for (row_type row = 0; row < nrows(); ++row) {
2043 for (col_type col = 0; col < ncols(); ++col) {
2045 << cell(index(row, col))
2051 docstring const & num = numbers_[row];
2053 os << '(' << num << ')';
2060 os << ETag("mtable");
2064 void InsetMathHull::mathAsLatex(WriteStream & os) const
2066 MathEnsurer ensurer(os, false);
2067 bool havenumbers = haveNumbers();
2068 bool const havetable = havenumbers || nrows() > 1 || ncols() > 1;
2071 os << cell(index(0, 0));
2075 os << "<table class='mathtable'>";
2076 for (row_type row = 0; row < nrows(); ++row) {
2078 for (col_type col = 0; col < ncols(); ++col) {
2079 os << "<td class='math'>";
2080 os << cell(index(row, col));
2085 docstring const & num = numbers_[row];
2087 os << '(' << num << ')';
2096 docstring InsetMathHull::xhtml(XHTMLStream & xs, OutputParams const & op) const
2098 BufferParams::MathOutput const mathtype =
2099 buffer().params().html_math_output;
2101 bool success = false;
2103 // we output all the labels just at the beginning of the equation.
2104 // this should be fine.
2105 for (size_t i = 0; i != label_.size(); ++i) {
2106 InsetLabel const * const il = label_[i];
2112 // FIXME Eventually we would like to do this inset by inset.
2113 if (mathtype == BufferParams::MathML) {
2114 odocstringstream os;
2119 } catch (MathExportException const &) {}
2121 if (getType() == hullSimple)
2122 xs << html::StartTag("math",
2123 "xmlns=\"http://www.w3.org/1998/Math/MathML\"", true);
2125 xs << html::StartTag("math",
2126 "display=\"block\" xmlns=\"http://www.w3.org/1998/Math/MathML\"", true);
2127 xs << XHTMLStream::ESCAPE_NONE
2129 << html::EndTag("math");
2131 } else if (mathtype == BufferParams::HTML) {
2132 odocstringstream os;
2137 } catch (MathExportException const &) {}
2139 string const tag = (getType() == hullSimple) ? "span" : "div";
2140 xs << html::StartTag(tag, "class='formula'", true)
2141 << XHTMLStream::ESCAPE_NONE
2143 << html::EndTag(tag);
2147 // what we actually want is this:
2149 // ((mathtype == BufferParams::MathML || mathtype == BufferParams::HTML)
2151 // || mathtype == BufferParams::Images
2153 // but what follows is equivalent, since we'll enter only if either (a) we
2154 // tried and failed with MathML or HTML or (b) didn't try yet at all but
2155 // aren't doing LaTeX, in which case we are doing Images.
2156 if (!success && mathtype != BufferParams::LaTeX) {
2157 graphics::PreviewImage const * pimage = 0;
2159 loadPreview(docit_);
2160 pimage = preview_->getPreviewImage(buffer());
2161 // FIXME Do we always have png?
2164 if (pimage || op.dryrun) {
2165 string const filename = pimage ? pimage->filename().onlyFileName()
2166 : "previewimage.png";
2168 // if we are not in the master buffer, then we need to see that the
2169 // generated image is copied there; otherwise, preview fails.
2170 Buffer const * mbuf = buffer().masterBuffer();
2171 if (mbuf != &buffer()) {
2172 string mbtmp = mbuf->temppath();
2173 FileName const mbufimg(support::addName(mbtmp, filename));
2174 pimage->filename().copyTo(mbufimg);
2176 // add the file to the list of files to be exported
2177 op.exportdata->addExternalFile("xhtml", pimage->filename());
2180 string const tag = (getType() == hullSimple) ? "span" : "div";
2182 << html::StartTag(tag)
2183 << html::CompTag("img", "src=\"" + filename + "\"")
2184 << html::EndTag(tag)
2190 // so we'll pass this test if we've failed everything else, or
2191 // if mathtype was LaTeX, since we won't have entered any of the
2193 if (!success /* || mathtype != BufferParams::LaTeX */) {
2194 // Unfortunately, we cannot use latexString() because we do not want
2195 // $...$ or whatever.
2196 odocstringstream ls;
2197 WriteStream wi(ls, false, true, WriteStream::wsPreview);
2198 ModeSpecifier specifier(wi, MATH_MODE);
2200 docstring const latex = ls.str();
2202 // class='math' allows for use of jsMath
2203 // http://www.math.union.edu/~dpvc/jsMath/
2205 // probably should allow for some kind of customization here
2206 string const tag = (getType() == hullSimple) ? "span" : "div";
2207 xs << html::StartTag(tag, "class='math'")
2209 << html::EndTag(tag)
2216 void InsetMathHull::toString(odocstream & os) const
2218 plaintext(os, OutputParams(0));
2222 void InsetMathHull::forToc(docstring & os, size_t) const
2224 odocstringstream ods;
2225 plaintext(ods, OutputParams(0));
2230 docstring InsetMathHull::contextMenuName() const
2232 return from_ascii("context-math");
2236 void InsetMathHull::recordLocation(DocIterator const & di)