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"
28 #include "bufferview_funcs.h"
32 #include "BufferParams.h"
33 #include "BufferView.h"
34 #include "CutAndPaste.h"
35 #include "FuncStatus.h"
37 #include "LaTeXFeatures.h"
40 #include "DispatchResult.h"
41 #include "FuncRequest.h"
44 #include "OutputParams.h"
46 #include "TextPainter.h"
49 #include "insets/RenderPreview.h"
50 #include "insets/InsetLabel.h"
52 #include "graphics/PreviewImage.h"
53 #include "graphics/PreviewLoader.h"
55 #include "frontends/Painter.h"
57 #include "support/lyxlib.h"
58 #include "support/lstrings.h"
60 #include <boost/bind.hpp>
67 using cap::grabAndEraseSelection;
68 using support::bformat;
74 using std::istringstream;
75 using std::ostringstream;
82 int getCols(HullType type)
99 // returns position of first relation operator in the array
100 // used for "intelligent splitting"
101 size_t firstRelOp(MathData const & ar)
103 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
104 if ((*it)->isRelOp())
105 return it - ar.begin();
110 char const * star(bool numbered)
112 return numbered ? "" : "*";
116 } // end anon namespace
119 HullType hullType(docstring const & s)
121 if (s == "none") return hullNone;
122 if (s == "simple") return hullSimple;
123 if (s == "equation") return hullEquation;
124 if (s == "eqnarray") return hullEqnArray;
125 if (s == "align") return hullAlign;
126 if (s == "alignat") return hullAlignAt;
127 if (s == "xalignat") return hullXAlignAt;
128 if (s == "xxalignat") return hullXXAlignAt;
129 if (s == "multline") return hullMultline;
130 if (s == "gather") return hullGather;
131 if (s == "flalign") return hullFlAlign;
132 lyxerr << "unknown hull type '" << to_utf8(s) << "'" << endl;
137 docstring hullName(HullType type)
140 case hullNone: return from_ascii("none");
141 case hullSimple: return from_ascii("simple");
142 case hullEquation: return from_ascii("equation");
143 case hullEqnArray: return from_ascii("eqnarray");
144 case hullAlign: return from_ascii("align");
145 case hullAlignAt: return from_ascii("alignat");
146 case hullXAlignAt: return from_ascii("xalignat");
147 case hullXXAlignAt: return from_ascii("xxalignat");
148 case hullMultline: return from_ascii("multline");
149 case hullGather: return from_ascii("gather");
150 case hullFlAlign: return from_ascii("flalign");
152 lyxerr << "unknown hull type '" << type << "'" << endl;
153 return from_ascii("none");
158 InsetMathHull::InsetMathHull()
159 : InsetMathGrid(1, 1), type_(hullNone), nonum_(1), label_(1),
160 preview_(new RenderPreview(this))
162 //lyxerr << "sizeof InsetMath: " << sizeof(InsetMath) << endl;
163 //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl;
164 //lyxerr << "sizeof InsetMathChar: " << sizeof(InsetMathChar) << endl;
165 //lyxerr << "sizeof Font: " << sizeof(Font) << endl;
171 InsetMathHull::InsetMathHull(HullType type)
172 : InsetMathGrid(getCols(type), 1), type_(type), nonum_(1), label_(1),
173 preview_(new RenderPreview(this))
180 InsetMathHull::InsetMathHull(InsetMathHull const & other)
181 : InsetMathGrid(other),
182 type_(other.type_), nonum_(other.nonum_), label_(other.label_),
183 preview_(new RenderPreview(*other.preview_, this))
187 InsetMathHull::~InsetMathHull()
191 Inset * InsetMathHull::clone() const
193 return new InsetMathHull(*this);
197 InsetMathHull & InsetMathHull::operator=(InsetMathHull const & other)
201 *static_cast<InsetMathGrid*>(this) = InsetMathGrid(other);
203 nonum_ = other.nonum_;
204 label_ = other.label_;
205 preview_.reset(new RenderPreview(*other.preview_, this));
211 Inset * InsetMathHull::editXY(Cursor & cur, int x, int y)
217 return InsetMathNest::editXY(cur, x, y);
221 InsetMath::mode_type InsetMathHull::currentMode() const
223 if (type_ == hullNone)
224 return UNDECIDED_MODE;
225 // definitely math mode ...
230 bool InsetMathHull::idxFirst(Cursor & cur) const
238 bool InsetMathHull::idxLast(Cursor & cur) const
240 cur.idx() = nargs() - 1;
241 cur.pos() = cur.lastpos();
246 char InsetMathHull::defaultColAlign(col_type col)
248 if (type_ == hullEqnArray)
250 if (type_ >= hullAlign)
251 return "rl"[col & 1];
256 int InsetMathHull::defaultColSpace(col_type col)
258 if (type_ == hullAlign || type_ == hullAlignAt)
260 if (type_ == hullXAlignAt)
261 return (col & 1) ? 20 : 0;
262 if (type_ == hullXXAlignAt || type_ == hullFlAlign)
263 return (col & 1) ? 40 : 0;
268 docstring InsetMathHull::standardFont() const
270 return from_ascii(type_ == hullNone ? "lyxnochange" : "mathnormal");
274 bool InsetMathHull::previewState(BufferView * bv) const
276 if (!editing(bv) && RenderPreview::status() == LyXRC::PREVIEW_ON) {
277 graphics::PreviewImage const * pimage =
278 preview_->getPreviewImage(bv->buffer());
279 return pimage && pimage->image();
285 bool InsetMathHull::metrics(MetricsInfo & mi, Dimension & dim) const
287 if (previewState(mi.base.bv)) {
288 preview_->metrics(mi, dim);
289 // insert a one pixel gap in front of the formula
292 dim.des += displayMargin();
299 FontSetChanger dummy1(mi.base, standardFont());
300 StyleChanger dummy2(mi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
302 // let the cells adjust themselves
303 InsetMathGrid::metrics(mi, dim);
306 dim.asc += displayMargin();
307 dim.des += displayMargin();
310 if (numberedType()) {
311 FontSetChanger dummy(mi.base, from_ascii("mathbf"));
313 for (row_type row = 0; row < nrows(); ++row)
314 l = max(l, mathed_string_width(mi.base.font, nicelabel(row)));
320 // make it at least as high as the current font
323 math_font_max_dim(mi.base.font, asc, des);
324 dim.asc = max(dim.asc, asc);
325 dim.des = max(dim.des, des);
334 void InsetMathHull::draw(PainterInfo & pi, int x, int y) const
336 use_preview_ = previewState(pi.base.bv);
338 // background of mathed under focus is not painted because
339 // selection at the top level of nested inset is difficult to handle.
340 if (!editing(pi.base.bv))
341 pi.pain.fillRectangle(x + 1, y - ascent() + 1, width() - 2,
342 ascent() + descent() - 1, Color::mathbg);
345 // one pixel gap in front
346 preview_->draw(pi, x + 1, y);
347 setPosCache(pi, x, y);
351 FontSetChanger dummy1(pi.base, standardFont());
352 StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
353 InsetMathGrid::draw(pi, x + 1, y);
355 if (numberedType()) {
356 int const xx = x + colinfo_.back().offset_ + colinfo_.back().width_ + 20;
357 for (row_type row = 0; row < nrows(); ++row) {
358 int const yy = y + rowinfo_[row].offset_;
359 FontSetChanger dummy(pi.base, from_ascii("mathrm"));
360 docstring const nl = nicelabel(row);
364 setPosCache(pi, x, y);
368 void InsetMathHull::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
371 InsetMathGrid::metricsT(mi, dim);
374 WriteStream wi(os, false, true);
376 dim.wid = os.str().size();
383 void InsetMathHull::drawT(TextPainter & pain, int x, int y) const
386 InsetMathGrid::drawT(pain, x, y);
389 WriteStream wi(os, false, true);
391 pain.draw(x, y, os.str().c_str());
398 docstring const latex_string(InsetMathHull const & inset)
401 WriteStream wi(ls, false, false);
409 void InsetMathHull::addPreview(graphics::PreviewLoader & ploader) const
411 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
412 docstring const snippet = latex_string(*this);
413 preview_->addPreview(snippet, ploader);
418 bool InsetMathHull::notifyCursorLeaves(Cursor & cur)
420 if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
421 Buffer const & buffer = cur.buffer();
422 docstring const snippet = latex_string(*this);
423 preview_->addPreview(snippet, buffer);
424 preview_->startLoading(buffer);
425 cur.updateFlags(Update::Force);
431 docstring InsetMathHull::label(row_type row) const
433 BOOST_ASSERT(row < nrows());
438 void InsetMathHull::label(row_type row, docstring const & label)
440 //lyxerr << "setting label '" << label << "' for row " << row << endl;
445 void InsetMathHull::numbered(row_type row, bool num)
453 bool InsetMathHull::numbered(row_type row) const
459 bool InsetMathHull::ams() const
462 type_ == hullAlign ||
463 type_ == hullFlAlign ||
464 type_ == hullMultline ||
465 type_ == hullGather ||
466 type_ == hullAlignAt ||
467 type_ == hullXAlignAt ||
468 type_ == hullXXAlignAt;
472 Inset::DisplayType InsetMathHull::display() const
474 return (type_ != hullSimple && type_ != hullNone) ? AlignCenter : Inline;
478 void InsetMathHull::getLabelList(Buffer const &, vector<docstring> & labels) const
480 for (row_type row = 0; row < nrows(); ++row)
481 if (!label_[row].empty() && nonum_[row] != 1)
482 labels.push_back(label_[row]);
486 bool InsetMathHull::numberedType() const
488 if (type_ == hullNone)
490 if (type_ == hullSimple)
492 if (type_ == hullXXAlignAt)
494 for (row_type row = 0; row < nrows(); ++row)
501 void InsetMathHull::validate(LaTeXFeatures & features) const
504 features.require("amsmath");
507 // Validation is necessary only if not using AMS math.
508 // To be safe, we will always run mathedvalidate.
509 //if (features.amsstyle)
512 features.require("boldsymbol");
513 //features.binom = true;
515 InsetMathGrid::validate(features);
519 void InsetMathHull::header_write(WriteStream & os) const
521 bool n = numberedType();
535 os << "\\begin{equation" << star(n) << "}\n";
545 os << "\\begin{" << hullName(type_) << star(n) << "}\n";
550 os << "\\begin{" << hullName(type_) << star(n) << '}'
551 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
555 os << "\\begin{" << hullName(type_) << '}'
556 << '{' << static_cast<unsigned int>((ncols() + 1)/2) << "}\n";
560 os << "\\begin{unknown" << star(n) << '}';
566 void InsetMathHull::footer_write(WriteStream & os) const
568 bool n = numberedType();
581 os << "\\end{equation" << star(n) << "}\n";
593 os << "\\end{" << hullName(type_) << star(n) << "}\n";
597 os << "\\end{" << hullName(type_) << "}\n";
601 os << "\\end{unknown" << star(n) << '}';
607 bool InsetMathHull::rowChangeOK() const
610 type_ == hullEqnArray || type_ == hullAlign ||
611 type_ == hullFlAlign || type_ == hullAlignAt ||
612 type_ == hullXAlignAt || type_ == hullXXAlignAt ||
613 type_ == hullGather || type_ == hullMultline;
617 bool InsetMathHull::colChangeOK() const
620 type_ == hullAlign || type_ == hullFlAlign ||type_ == hullAlignAt ||
621 type_ == hullXAlignAt || type_ == hullXXAlignAt;
625 void InsetMathHull::addRow(row_type row)
629 nonum_.insert(nonum_.begin() + row + 1, !numberedType());
630 label_.insert(label_.begin() + row + 1, docstring());
631 InsetMathGrid::addRow(row);
635 void InsetMathHull::swapRow(row_type row)
639 if (row + 1 == nrows())
641 swap(nonum_[row], nonum_[row + 1]);
642 swap(label_[row], label_[row + 1]);
643 InsetMathGrid::swapRow(row);
647 void InsetMathHull::delRow(row_type row)
649 if (nrows() <= 1 || !rowChangeOK())
651 InsetMathGrid::delRow(row);
652 // The last dummy row has no number info nor a label.
653 // Test nrows() + 1 because we have already erased the row.
654 if (row == nrows() + 1)
656 nonum_.erase(nonum_.begin() + row);
657 label_.erase(label_.begin() + row);
661 void InsetMathHull::addCol(col_type col)
665 InsetMathGrid::addCol(col);
669 void InsetMathHull::delCol(col_type col)
671 if (ncols() <= 1 || !colChangeOK())
673 InsetMathGrid::delCol(col);
677 docstring InsetMathHull::nicelabel(row_type row) const
681 if (label_[row].empty())
682 return from_ascii("(#)");
683 return '(' + label_[row] + ')';
687 void InsetMathHull::glueall()
690 for (idx_type i = 0; i < nargs(); ++i)
692 *this = InsetMathHull(hullSimple);
698 void InsetMathHull::splitTo2Cols()
700 BOOST_ASSERT(ncols() == 1);
701 InsetMathGrid::addCol(1);
702 for (row_type row = 0; row < nrows(); ++row) {
703 idx_type const i = 2 * row;
704 pos_type pos = firstRelOp(cell(i));
705 cell(i + 1) = MathData(cell(i).begin() + pos, cell(i).end());
706 cell(i).erase(pos, cell(i).size());
711 void InsetMathHull::splitTo3Cols()
713 BOOST_ASSERT(ncols() < 3);
716 InsetMathGrid::addCol(1);
717 for (row_type row = 0; row < nrows(); ++row) {
718 idx_type const i = 3 * row + 1;
719 if (cell(i).size()) {
720 cell(i + 1) = MathData(cell(i).begin() + 1, cell(i).end());
721 cell(i).erase(1, cell(i).size());
727 void InsetMathHull::changeCols(col_type cols)
731 else if (ncols() < cols) {
737 while (ncols() < cols)
738 InsetMathGrid::addCol(ncols() - 1);
744 for (row_type row = 0; row < nrows(); ++row) {
745 idx_type const i = row * ncols();
746 for (col_type col = cols; col < ncols(); ++col) {
747 cell(i + cols - 1).append(cell(i + col));
751 while (ncols() > cols) {
752 InsetMathGrid::delCol(ncols() - 1);
757 HullType InsetMathHull::getType() const
763 void InsetMathHull::setType(HullType type)
770 void InsetMathHull::mutate(HullType newtype)
772 //lyxerr << "mutating from '" << type_ << "' to '" << newtype << "'" << endl;
774 // we try to move along the chain
775 // none <-> simple <-> equation <-> eqnarray -> *align* -> multline, gather -+
777 // +-------------------------------------+
778 // we use eqnarray as intermediate type for mutations that are not
779 // directly supported because it handles labels and numbering for
782 if (newtype == type_) {
786 else if (newtype < hullNone) {
791 else if (type_ == hullNone) {
797 else if (type_ == hullSimple) {
798 if (newtype == hullNone) {
802 setType(hullEquation);
808 else if (type_ == hullEquation) {
809 if (newtype < type_) {
813 } else if (newtype == hullEqnArray) {
814 // split it "nicely" on the first relop
816 setType(hullEqnArray);
817 } else if (newtype == hullMultline || newtype == hullGather) {
827 else if (type_ == hullEqnArray) {
828 if (newtype < type_) {
829 // set correct (no)numbering
830 bool allnonum = true;
831 for (row_type row = 0; row < nrows(); ++row)
835 // set first non-empty label
837 for (row_type row = 0; row < nrows(); ++row) {
838 if (!label_[row].empty()) {
845 nonum_[0] = allnonum;
848 } else { // align & Co.
855 else if (type_ == hullAlign || type_ == hullAlignAt ||
856 type_ == hullXAlignAt || type_ == hullFlAlign) {
857 if (newtype < hullAlign) {
859 setType(hullEqnArray);
861 } else if (newtype == hullGather || newtype == hullMultline) {
864 } else if (newtype == hullXXAlignAt) {
865 for (row_type row = 0; row < nrows(); ++row)
866 numbered(row, false);
873 else if (type_ == hullXXAlignAt) {
874 for (row_type row = 0; row < nrows(); ++row)
875 numbered(row, false);
876 if (newtype < hullAlign) {
878 setType(hullEqnArray);
880 } else if (newtype == hullGather || newtype == hullMultline) {
888 else if (type_ == hullMultline || type_ == hullGather) {
889 if (newtype == hullGather || newtype == hullMultline)
891 else if (newtype == hullAlign || newtype == hullFlAlign ||
892 newtype == hullAlignAt || newtype == hullXAlignAt) {
895 } else if (newtype == hullXXAlignAt) {
897 for (row_type row = 0; row < nrows(); ++row)
898 numbered(row, false);
902 setType(hullEqnArray);
908 lyxerr << "mutation from '" << to_utf8(hullName(type_))
909 << "' to '" << to_utf8(hullName(newtype))
910 << "' not implemented" << endl;
915 docstring InsetMathHull::eolString(row_type row, bool emptyline, bool fragile) const
918 if (numberedType()) {
919 if (!label_[row].empty() && !nonum_[row])
920 res += "\\label{" + label_[row] + '}';
921 if (nonum_[row] && (type_ != hullMultline))
922 res += "\\nonumber ";
924 return res + InsetMathGrid::eolString(row, emptyline, fragile);
928 void InsetMathHull::write(WriteStream & os) const
931 InsetMathGrid::write(os);
936 void InsetMathHull::normalize(NormalStream & os) const
938 os << "[formula " << hullName(type_) << ' ';
939 InsetMathGrid::normalize(os);
944 void InsetMathHull::mathmlize(MathStream & os) const
946 InsetMathGrid::mathmlize(os);
950 void InsetMathHull::infoize(odocstream & os) const
952 os << "Type: " << hullName(type_);
956 void InsetMathHull::check() const
958 BOOST_ASSERT(nonum_.size() == nrows());
959 BOOST_ASSERT(label_.size() == nrows());
963 void InsetMathHull::doExtern(Cursor & cur, FuncRequest & func)
967 idocstringstream iss(func.argument());
968 iss >> dlang >> extra;
970 extra = from_ascii("noextra");
971 std::string const lang = to_ascii(dlang);
973 // FIXME: temporarily disabled
974 //if (cur.selection()) {
977 // lyxerr << "use selection: " << ar << endl;
978 // insert(pipeThroughExtern(lang, extra, ar));
983 eq.push_back(MathAtom(new InsetMathChar('=')));
985 // go to first item in line
986 cur.idx() -= cur.idx() % ncols();
989 if (getType() == hullSimple) {
990 size_type pos = cur.cell().find_last(eq);
992 if (cur.inMathed() && cur.selection()) {
993 asArray(grabAndEraseSelection(cur), ar);
994 } else if (pos == cur.cell().size()) {
996 lyxerr << "use whole cell: " << ar << endl;
998 ar = MathData(cur.cell().begin() + pos + 1, cur.cell().end());
999 lyxerr << "use partial cell form pos: " << pos << endl;
1001 cur.cell().append(eq);
1002 cur.cell().append(pipeThroughExtern(lang, extra, ar));
1003 cur.pos() = cur.lastpos();
1007 if (getType() == hullEquation) {
1008 lyxerr << "use equation inset" << endl;
1009 mutate(hullEqnArray);
1010 MathData & ar = cur.cell();
1011 lyxerr << "use cell: " << ar << endl;
1015 cur.cell() = pipeThroughExtern(lang, extra, ar);
1016 // move to end of line
1017 cur.pos() = cur.lastpos();
1022 lyxerr << "use eqnarray" << endl;
1023 cur.idx() += 2 - cur.idx() % ncols();
1025 MathData ar = cur.cell();
1026 lyxerr << "use cell: " << ar << endl;
1027 // FIXME: temporarily disabled
1033 cur.cell() = pipeThroughExtern(lang, extra, ar);
1034 cur.pos() = cur.lastpos();
1039 void InsetMathHull::doDispatch(Cursor & cur, FuncRequest & cmd)
1041 //lyxerr << "action: " << cmd.action << endl;
1042 switch (cmd.action) {
1044 case LFUN_FINISHED_LEFT:
1045 case LFUN_FINISHED_RIGHT:
1046 //lyxerr << "action: " << cmd.action << endl;
1047 InsetMathGrid::doDispatch(cur, cmd);
1048 notifyCursorLeaves(cur);
1052 case LFUN_BREAK_PARAGRAPH:
1053 // just swallow this
1056 case LFUN_BREAK_LINE:
1057 // some magic for the common case
1058 if (type_ == hullSimple || type_ == hullEquation) {
1059 recordUndoInset(cur);
1061 cur.bv().buffer().params().use_amsmath == BufferParams::package_on;
1062 mutate(align ? hullAlign : hullEqnArray);
1064 cur.pos() = cur.lastpos();
1066 InsetMathGrid::doDispatch(cur, cmd);
1069 case LFUN_MATH_NUMBER:
1070 //lyxerr << "toggling all numbers" << endl;
1072 recordUndoInset(cur);
1073 bool old = numberedType();
1074 if (type_ == hullMultline)
1075 numbered(nrows() - 1, !old);
1077 for (row_type row = 0; row < nrows(); ++row)
1078 numbered(row, !old);
1080 cur.message(old ? _("No number") : _("Number"));
1084 case LFUN_MATH_NONUMBER:
1086 recordUndoInset(cur);
1087 row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1088 bool old = numbered(r);
1089 cur.message(old ? _("No number") : _("Number"));
1094 case LFUN_LABEL_INSERT: {
1095 recordUndoInset(cur);
1096 row_type r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1097 docstring old_label = label(r);
1098 docstring const default_label = from_ascii(
1099 (lyxrc.label_init_length >= 0) ? "eq:" : "");
1100 if (old_label.empty())
1101 old_label = default_label;
1103 InsetCommandParams p("label");
1104 p["name"] = cmd.argument().empty() ? old_label : cmd.argument();
1105 std::string const data = InsetCommandMailer::params2string("label", p);
1107 if (cmd.argument().empty())
1108 cur.bv().showInsetDialog("label", data, 0);
1110 FuncRequest fr(LFUN_INSET_INSERT, data);
1116 case LFUN_INSET_INSERT: {
1117 //lyxerr << "arg: " << to_utf8(cmd.argument()) << endl;
1118 std::string const name = cmd.getArg(0);
1119 if (name == "label") {
1120 InsetCommandParams p("label");
1121 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), p);
1122 docstring str = p["name"];
1123 recordUndoInset(cur);
1124 row_type const r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
1125 str = support::trim(str);
1128 docstring old = label(r);
1130 cur.bv().buffer().changeRefsIfUnique(old, str,
1136 InsetMathGrid::doDispatch(cur, cmd);
1140 case LFUN_MATH_EXTERN:
1141 recordUndoInset(cur);
1145 case LFUN_MATH_MUTATE: {
1146 recordUndoInset(cur);
1147 row_type row = cur.row();
1148 col_type col = cur.col();
1149 mutate(hullType(cmd.argument()));
1150 cur.idx() = row * ncols() + col;
1151 if (cur.idx() > cur.lastidx()) {
1152 cur.idx() = cur.lastidx();
1153 cur.pos() = cur.lastpos();
1155 if (cur.pos() > cur.lastpos())
1156 cur.pos() = cur.lastpos();
1157 //cur.dispatched(FINISHED);
1161 case LFUN_MATH_DISPLAY: {
1162 recordUndoInset(cur);
1163 mutate(type_ == hullSimple ? hullEquation : hullSimple);
1165 cur.pos() = cur.lastpos();
1166 //cur.dispatched(FINISHED);
1171 InsetMathGrid::doDispatch(cur, cmd);
1177 bool InsetMathHull::getStatus(Cursor & cur, FuncRequest const & cmd,
1178 FuncStatus & status) const
1180 switch (cmd.action) {
1181 case LFUN_FINISHED_LEFT:
1182 case LFUN_FINISHED_RIGHT:
1185 status.enabled(true);
1187 case LFUN_BREAK_LINE:
1188 case LFUN_MATH_NUMBER:
1189 case LFUN_MATH_NONUMBER:
1190 case LFUN_MATH_EXTERN:
1191 case LFUN_MATH_MUTATE:
1192 case LFUN_MATH_DISPLAY:
1194 status.enabled(true);
1196 case LFUN_LABEL_INSERT:
1197 status.enabled(type_ != hullSimple);
1199 case LFUN_INSET_INSERT:
1200 if (cmd.getArg(0) == "label") {
1201 status.enabled(type_ != hullSimple);
1204 return InsetMathGrid::getStatus(cur, cmd, status);
1205 case LFUN_TABULAR_FEATURE: {
1206 istringstream is(to_utf8(cmd.argument()));
1210 && (s == "append-row"
1211 || s == "delete-row"
1212 || s == "copy-row")) {
1213 status.message(bformat(
1214 from_utf8(N_("Can't change number of rows in '%1$s'")),
1216 status.enabled(false);
1220 && (s == "append-column"
1221 || s == "delete-column"
1222 || s == "copy-column")) {
1223 status.message(bformat(
1224 from_utf8(N_("Can't change number of columns in '%1$s'")),
1226 status.enabled(false);
1229 if ((type_ == hullSimple
1230 || type_ == hullEquation
1231 || type_ == hullNone) &&
1232 (s == "add-hline-above" || s == "add-hline-below")) {
1233 status.message(bformat(
1234 from_utf8(N_("Can't add horizontal grid lines in '%1$s'")),
1236 status.enabled(false);
1239 if (s == "add-vline-left" || s == "add-vline-right") {
1240 status.message(bformat(
1241 from_utf8(N_("Can't add vertical grid lines in '%1$s'")),
1243 status.enabled(false);
1246 if (s == "valign-top" || s == "valign-middle"
1247 || s == "valign-bottom" || s == "align-left"
1248 || s == "align-center" || s == "align-right") {
1249 status.enabled(false);
1252 return InsetMathGrid::getStatus(cur, cmd, status);
1255 return InsetMathGrid::getStatus(cur, cmd, status);
1258 // This cannot really happen, but inserted to shut-up gcc
1259 return InsetMathGrid::getStatus(cur, cmd, status);
1263 /////////////////////////////////////////////////////////////////////
1267 // simply scrap this function if you want
1268 void InsetMathHull::mutateToText()
1271 // translate to latex
1273 latex(NULL, os, false, false);
1274 string str = os.str();
1277 Text * lt = view_->cursor().innerText();
1278 string::const_iterator cit = str.begin();
1279 string::const_iterator end = str.end();
1280 for (; cit != end; ++cit)
1281 view_->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
1284 //dispatch(LFUN_ESCAPE);
1289 void InsetMathHull::handleFont(Cursor & cur, docstring const & arg,
1290 docstring const & font)
1292 // this whole function is a hack and won't work for incremental font
1295 if (cur.inset().asInsetMath()->name() == font)
1296 cur.handleFont(to_utf8(font));
1298 cur.handleNest(createInsetMath(font));
1304 void InsetMathHull::handleFont2(Cursor & cur, docstring const & arg)
1309 bv_funcs::string2font(to_utf8(arg), font, b);
1310 if (font.color() != Color::inherit) {
1311 MathAtom at = MathAtom(new InsetMathColor(true, font.color()));
1312 cur.handleNest(at, 0);
1317 void InsetMathHull::edit(Cursor & cur, bool left)
1320 left ? idxFirst(cur) : idxLast(cur);
1321 // The inset formula dimension is not necessarily the same as the
1322 // one of the instant preview image, so we have to indicate to the
1323 // BufferView that a metrics update is needed.
1324 cur.updateFlags(Update::Force);
1328 docstring const InsetMathHull::editMessage() const
1330 return _("Math editor mode");
1334 void InsetMathHull::revealCodes(Cursor & cur) const
1336 if (!cur.inMathed())
1338 odocstringstream os;
1340 cur.message(os.str());
1342 // write something to the minibuffer
1343 // translate to latex
1347 string str = os.str();
1349 string::size_type pos = 0;
1351 for (string::iterator it = str.begin(); it != str.end(); ++it) {
1354 else if (*it == '\0') {
1356 pos = it - str.begin();
1362 res = res.substr(pos - 30);
1363 if (res.size() > 60)
1364 res = res.substr(0, 60);
1370 Inset::Code InsetMathHull::lyxCode() const
1376 /////////////////////////////////////////////////////////////////////
1380 bool InsetMathHull::searchForward(BufferView * bv, string const & str,
1383 // FIXME: completely broken
1384 static InsetMathHull * lastformula = 0;
1385 static CursorBase current = DocIterator(ibegin(nucleus()));
1387 static string laststr;
1389 if (lastformula != this || laststr != str) {
1390 //lyxerr << "reset lastformula to " << this << endl;
1393 current = ibegin(nucleus());
1395 mathed_parse_cell(ar, str);
1399 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
1401 for (DocIterator it = current; it != iend(nucleus()); increment(it)) {
1402 CursorSlice & top = it.back();
1403 MathData const & a = top.asInsetMath()->cell(top.idx_);
1404 if (a.matchpart(ar, top.pos_)) {
1405 bv->cursor().setSelection(it, ar.size());
1407 top.pos_ += ar.size();
1413 //lyxerr << "not found!" << endl;
1420 void InsetMathHull::write(Buffer const &, std::ostream & os) const
1422 odocstringstream oss;
1423 WriteStream wi(oss, false, false);
1426 os << to_utf8(oss.str());
1430 void InsetMathHull::read(Buffer const &, Lexer & lex)
1433 mathed_parse_normal(at, lex);
1434 operator=(*at->asHullInset());
1438 int InsetMathHull::plaintext(Buffer const &, odocstream & os,
1439 OutputParams const &) const
1441 if (0 && display()) {
1445 TextPainter tpain(dim.width(), dim.height());
1446 drawT(tpain, 0, dim.ascent());
1448 // reset metrics cache to "real" values
1450 return tpain.textheight();
1452 odocstringstream oss;
1453 WriteStream wi(oss, false, true);
1456 docstring const str = oss.str();
1463 int InsetMathHull::docbook(Buffer const & buf, odocstream & os,
1464 OutputParams const & runparams) const
1469 if (getType() == hullSimple)
1470 name = from_ascii("inlineequation");
1472 name = from_ascii("informalequation");
1474 docstring bname = name;
1475 if (!label(0).empty())
1476 bname += " id='" + sgml::cleanID(buf, runparams, label(0)) + "'";
1479 odocstringstream ls;
1480 if (runparams.flavor == OutputParams::XML) {
1481 ms << MTag(from_ascii("alt role='tex' "));
1482 // Workaround for db2latex: db2latex always includes equations with
1483 // \ensuremath{} or \begin{display}\end{display}
1484 // so we strip LyX' math environment
1485 WriteStream wi(ls, false, false);
1486 InsetMathGrid::write(wi);
1487 ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&"), "<", "<"));
1488 ms << ETag(from_ascii("alt"));
1489 ms << MTag(from_ascii("math"));
1490 ms << ETag(from_ascii("alt"));
1491 ms << MTag(from_ascii("math"));
1492 InsetMathGrid::mathmlize(ms);
1493 ms << ETag(from_ascii("math"));
1495 ms << MTag(from_ascii("alt role='tex'"));
1496 res = latex(buf, ls, runparams);
1497 ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&"), "<", "<"));
1498 ms << ETag(from_ascii("alt"));
1501 ms << from_ascii("<graphic fileref=\"eqn/");
1502 if (!label(0).empty())
1503 ms << sgml::cleanID(buf, runparams, label(0));
1505 ms << sgml::uniqueID(from_ascii("anon"));
1507 if (runparams.flavor == OutputParams::XML)
1508 ms << from_ascii("\"/>");
1510 ms << from_ascii("\">");
1513 return ms.line() + res;
1517 void InsetMathHull::textString(Buffer const & buf, odocstream & os) const
1519 plaintext(buf, os, OutputParams(0));