#include "ColorSet.h"
#include "CutAndPaste.h"
#include "Encoding.h"
+#include "Exporter.h"
#include "FuncRequest.h"
#include "FuncStatus.h"
#include "LaTeXFeatures.h"
static InsetLabel * dummy_pointer = 0;
InsetMathHull::InsetMathHull(Buffer * buf)
- : InsetMathGrid(buf, 1, 1), type_(hullNone), nonum_(1, false),
+ : InsetMathGrid(buf, 1, 1), type_(hullNone), numbered_(1, true),
label_(1, dummy_pointer), preview_(new RenderPreview(this))
{
//lyxerr << "sizeof InsetMath: " << sizeof(InsetMath) << endl;
InsetMathHull::InsetMathHull(Buffer * buf, HullType type)
- : InsetMathGrid(buf, getCols(type), 1), type_(type), nonum_(1, false),
+ : InsetMathGrid(buf, getCols(type), 1), type_(type), numbered_(1, true),
label_(1, dummy_pointer), preview_(new RenderPreview(this))
{
buffer_ = buf;
return *this;
InsetMathGrid::operator=(other);
type_ = other.type_;
- nonum_ = other.nonum_;
+ numbered_ = other.numbered_;
buffer_ = other.buffer_;
for (size_t i = 0; i < label_.size(); ++i)
delete label_[i];
Toc & toc = buffer().tocBackend().toc("equation");
for (row_type row = 0; row != nrows(); ++row) {
- if (nonum_[row])
+ if (!numbered_[row])
continue;
if (label_[row])
label_[row]->addToToc(pit);
}
-docstring InsetMathHull::standardColor() const
+ColorCode InsetMathHull::standardColor() const
{
- docstring color;
+ ColorCode color;
switch (type_) {
case hullRegexp:
- color = from_ascii("foreground");
- break;
case hullNone:
- color = from_ascii("foreground");
+ color = Color_foreground;
break;
default:
- color = from_ascii("math");
+ color = Color_math;
}
return color;
}
return;
}
+ ColorCode color = pi.selected && lyxrc.use_system_colors
+ ? Color_selectiontext : standardColor();
bool const really_change_color = pi.base.font.color() == Color_none;
- ColorChanger dummy0(pi.base.font, standardColor(), really_change_color);
+ ColorChanger dummy0(pi.base.font, color, really_change_color);
FontSetChanger dummy1(pi.base, standardFont());
StyleChanger dummy2(pi.base, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
}
-void InsetMathHull::preparePreview(DocIterator const & pos) const
+void InsetMathHull::preparePreview(DocIterator const & pos,
+ bool forexport) const
{
+ // there is no need to do all the macro stuff if we're not
+ // actually going to generate the preview.
+ if (RenderPreview::status() != LyXRC::PREVIEW_ON && !forexport)
+ return;
+
Buffer const * buffer = pos.buffer();
// collect macros at this position
docstring const snippet = macro_preamble.str() + latexString(*this);
LYXERR(Debug::MACROS, "Preview snippet: " << snippet);
- preview_->addPreview(snippet, *buffer);
+ preview_->addPreview(snippet, *buffer, forexport);
}
}
+void InsetMathHull::loadPreview(DocIterator const & pos) const
+{
+ bool const forexport = true;
+ preparePreview(pos, forexport);
+ preview_->startLoading(*pos.buffer(), forexport);
+}
+
+
bool InsetMathHull::notifyCursorLeaves(Cursor const & old, Cursor & cur)
{
if (RenderPreview::status() == LyXRC::PREVIEW_ON) {
void InsetMathHull::numbered(row_type row, bool num)
{
- nonum_[row] = !num;
- if (nonum_[row] && label_[row]) {
+ numbered_[row] = num;
+ if (!numbered_[row] && label_[row]) {
delete label_[row];
label_[row] = 0;
}
bool InsetMathHull::numbered(row_type row) const
{
- return !nonum_[row];
+ return numbered_[row];
}
if (type_ == hullRegexp)
return false;
for (row_type row = 0; row < nrows(); ++row)
- if (!nonum_[row])
+ if (numbered_[row])
return true;
return false;
}
break;
default:
- os << "\\begin{unknown" << star(n) << '}';
+ os << "\\begin{unknown" << star(n) << "}\n";
break;
}
}
case hullEquation:
if (n)
- os << "\\end{equation" << star(n) << "}\n";
+ os << "\n\\end{equation" << star(n) << "}\n";
else
- os << "\\]\n";
+ os << "\n\\]\n";
break;
case hullEqnArray:
case hullXAlignAt:
case hullGather:
case hullMultline:
- os << "\\end{" << hullName(type_) << star(n) << "}\n";
+ os << "\n\\end{" << hullName(type_) << star(n) << "}\n";
break;
case hullXXAlignAt:
- os << "\\end{" << hullName(type_) << "}\n";
+ os << "\n\\end{" << hullName(type_) << "}\n";
break;
case hullRegexp:
break;
default:
- os << "\\end{unknown" << star(n) << '}';
+ os << "\n\\end{unknown" << star(n) << "}\n";
break;
}
}
docstring lab;
if (type_ == hullMultline) {
if (row + 1 == nrows()) {
- nonum_[row] = true;
+ numbered_[row] = false;
lab = label(row);
} else
numbered = false;
}
- nonum_.insert(nonum_.begin() + row + 1, !numbered);
+ numbered_.insert(numbered_.begin() + row + 1, numbered);
label_.insert(label_.begin() + row + 1, dummy_pointer);
if (!lab.empty())
label(row + 1, lab);
// gcc implements the standard std::vector<bool> which is *not* a container:
// http://www.gotw.ca/publications/N1185.pdf
// As a results, it doesn't like this:
- // swap(nonum_[row], nonum_[row + 1]);
+ // swap(numbered_[row], numbered_[row + 1]);
// so we do it manually:
- bool const b = nonum_[row];
- nonum_[row] = nonum_[row + 1];
- nonum_[row + 1] = b;
+ bool const b = numbered_[row];
+ numbered_[row] = numbered_[row + 1];
+ numbered_[row + 1] = b;
swap(label_[row], label_[row + 1]);
InsetMathGrid::swapRow(row);
}
if (nrows() <= 1 || !rowChangeOK())
return;
if (row + 1 == nrows() && type_ == hullMultline) {
- bool const b = nonum_[row - 1];
- nonum_[row - 1] = nonum_[row];
- nonum_[row] = b;
+ bool const b = numbered_[row - 1];
+ numbered_[row - 1] = numbered_[row];
+ numbered_[row] = b;
swap(label_[row - 1], label_[row]);
InsetMathGrid::delRow(row);
return;
// Test nrows() + 1 because we have already erased the row.
if (row == nrows() + 1)
row--;
- nonum_.erase(nonum_.begin() + row);
+ numbered_.erase(numbered_.begin() + row);
delete label_[row];
label_.erase(label_.begin() + row);
}
docstring InsetMathHull::nicelabel(row_type row) const
{
- if (nonum_[row])
+ if (!numbered_[row])
return docstring();
if (!label_[row])
return from_ascii("(#)");
}
-void InsetMathHull::glueall()
+void InsetMathHull::glueall(HullType type)
{
MathData ar;
for (idx_type i = 0; i < nargs(); ++i)
ar.append(cell(i));
+ InsetLabel * label = 0;
+ if (type == hullEquation) {
+ // preserve first non-empty label
+ for (row_type row = 0; row < nrows(); ++row) {
+ if (label_[row]) {
+ label = label_[row];
+ label_[row] = 0;
+ break;
+ }
+ }
+ }
*this = InsetMathHull(buffer_, hullSimple);
+ label_[0] = label;
cell(0) = ar;
setDefaults();
}
numbered(0, false);
} else {
setType(hullEquation);
- numbered(0, false);
+ numbered(0, label_[0] ? true : false);
mutate(newtype);
}
}
else if (type_ == hullEqnArray) {
if (newtype < type_) {
- // set correct (no)numbering
- nonum_[0] = true;
- for (row_type row = 0; row < nrows(); ++row) {
- if (!nonum_[row]) {
- nonum_[0] = false;
- break;
- }
- }
-
- // set first non-empty label
- for (row_type row = 0; row < nrows(); ++row) {
- if (label_[row]) {
- label_[0] = label_[row];
- break;
- }
- }
-
- glueall();
+ glueall(newtype);
mutate(newtype);
} else { // align & Co.
changeCols(2);
}
-docstring InsetMathHull::eolString(row_type row, bool fragile, bool last_eoln) const
+docstring InsetMathHull::eolString(row_type row, bool fragile, bool latex,
+ bool last_eoln) const
{
docstring res;
if (numberedType()) {
- if (label_[row] && !nonum_[row])
- res += "\\label{" +
- escape(label_[row]->getParam("name")) + '}';
- if (nonum_[row] && (type_ != hullMultline))
+ if (label_[row] && numbered_[row]) {
+ docstring const name =
+ latex ? escape(label_[row]->getParam("name"))
+ : label_[row]->getParam("name");
+ res += "\\label{" + name + '}';
+ }
+ if (!numbered_[row] && (type_ != hullMultline))
res += "\\nonumber ";
}
// Never add \\ on the last empty line of eqnarray and friends
last_eoln = false;
- return res + InsetMathGrid::eolString(row, fragile, last_eoln);
+ return res + InsetMathGrid::eolString(row, fragile, latex, last_eoln);
}
void InsetMathHull::check() const
{
- LASSERT(nonum_.size() == nrows(), /**/);
+ LASSERT(numbered_.size() == nrows(), /**/);
LASSERT(label_.size() == nrows(), /**/);
}
InsetCommandParams p(LABEL_CODE);
p["name"] = cmd.argument().empty() ? old_label : cmd.argument();
- string const data = InsetCommand::params2string("label", p);
+ string const data = InsetCommand::params2string(p);
if (cmd.argument().empty())
cur.bv().showDialog("label", data);
// if there is an argument, find the corresponding label, else
// check whether there is at least one label.
for (row = 0; row != nrows(); ++row)
- if (!nonum_[row] && label_[row]
+ if (numbered_[row] && label_[row]
&& (cmd.argument().empty() || label(row) == cmd.argument()))
break;
}
string const name = cmd.getArg(0);
if (name == "label") {
InsetCommandParams p(LABEL_CODE);
- InsetCommand::string2params(name, to_utf8(cmd.argument()), p);
+ InsetCommand::string2params(to_utf8(cmd.argument()), p);
docstring str = p["name"];
cur.recordUndoInset();
row_type const r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
// if there is no argument and we're inside math, we retrieve
// the row number from the cursor position.
row = (type_ == hullMultline) ? nrows() - 1 : cur.row();
- enabled = numberedType() && label_[row] && !nonum_[row];
+ enabled = numberedType() && label_[row] && numbered_[row];
} else {
// if there is an argument, find the corresponding label, else
// check whether there is at least one label.
for (row_type row = 0; row != nrows(); ++row) {
- if (!nonum_[row] && label_[row] &&
+ if (numbered_[row] && label_[row] &&
(cmd.argument().empty() || label(row) == cmd.argument())) {
enabled = true;
break;
}
-docstring InsetMathHull::xhtml(XHTMLStream & xs, OutputParams const &) const
+docstring InsetMathHull::xhtml(XHTMLStream & xs, OutputParams const & op) const
{
- BufferParams::MathOutput mathtype = buffer().params().html_math_output;
+ BufferParams::MathOutput const mathtype =
+ buffer().params().html_math_output;
+
+ bool success = false;
// FIXME Eventually we would like to do this inset by inset.
- switch (mathtype) {
- case BufferParams::MathML: {
- if (getType() == hullSimple)
- xs << html::StartTag("math",
- "xmlns=\"http://www.w3.org/1998/Math/MathML\"", true);
- else
- xs << html::StartTag("math",
- "display=\"block\" xmlns=\"http://www.w3.org/1998/Math/MathML\"", true);
- MathStream ms(xs.os());
- InsetMathGrid::mathmlize(ms);
- xs << html::EndTag("math");
- break;
- }
- case BufferParams::HTML: {
- string tag = (getType() == hullSimple) ? "span" : "div";
- xs << html::StartTag(tag, "class='formula'", true);
- HtmlStream ms(xs.os());
- InsetMathGrid::htmlize(ms);
- xs << html::EndTag(tag);
- break;
- }
- case BufferParams::Images: {
- LYXERR0("Image output for math presently unsupported.");
- break;
- }
- case BufferParams::LaTeX: {
- // FIXME Obviously, the only real question is how to wrap this.
- LYXERR0("LaTeX output for math presently unsupported.");
+ if (mathtype == BufferParams::MathML) {
+ odocstringstream os;
+ MathStream ms(os);
+ try {
+ InsetMathGrid::mathmlize(ms);
+ success = true;
+ } catch (MathExportException const &) {}
+ if (success) {
+ if (getType() == hullSimple)
+ xs << html::StartTag("math",
+ "xmlns=\"http://www.w3.org/1998/Math/MathML\"", true);
+ else
+ xs << html::StartTag("math",
+ "display=\"block\" xmlns=\"http://www.w3.org/1998/Math/MathML\"", true);
+ xs << XHTMLStream::NextRaw()
+ << os.str()
+ << html::EndTag("math");
+ }
+ } else if (mathtype == BufferParams::HTML) {
+ odocstringstream os;
+ HtmlStream ms(os);
+ try {
+ InsetMathGrid::htmlize(ms);
+ success = true;
+ } catch (MathExportException const &) {}
+ if (success) {
+ string const tag = (getType() == hullSimple) ? "span" : "div";
+ xs << html::StartTag(tag, "class='formula'", true)
+ << XHTMLStream::NextRaw()
+ << os.str()
+ << html::EndTag(tag);
+ }
+ }
+
+ // what we actually want is this:
+ // if (
+ // ((mathtype == BufferParams::MathML || mathtype == BufferParams::HTML)
+ // && !success)
+ // || mathtype == BufferParams::Images
+ // )
+ // but what follows is equivalent, since we'll enter only if either (a) we
+ // tried and failed with MathML or HTML or (b) didn't try yet at all but
+ // aren't doing LaTeX, in which case we are doing Images.
+ if (!success && mathtype != BufferParams::LaTeX) {
+ loadPreview(docit_);
+ graphics::PreviewImage const * pimage = preview_->getPreviewImage(buffer());
+ if (pimage) {
+ // FIXME Do we always have png?
+ string const tag = (getType() == hullSimple) ? "span" : "div";
+ FileName const & mathimg = pimage->filename();
+ xs << html::StartTag(tag)
+ << html::CompTag("img", "src=\"" + mathimg.onlyFileName() + "\"")
+ << html::EndTag(tag);
+ xs.cr();
+ // add the file to the list of files to be exported
+ op.exportdata->addExternalFile("xhtml", mathimg);
+ success = true;
+ }
+ }
+
+ // so we'll pass this test if we've failed everything else, or
+ // if mathtype was LaTeX, since we won't have entered any of the
+ // earlier branches
+ if (!success /* || mathtype != BufferParams::LaTeX */) {
+ string const tag = (getType() == hullSimple) ? "span" : "div";
+ // Unfortunately, we cannot use latexString() because we do not want
+ // $...$ or whatever.
+ odocstringstream ls;
+ WriteStream wi(ls, false, true, WriteStream::wsPreview);
+ ModeSpecifier specifier(wi, MATH_MODE);
+ InsetMathGrid::write(wi);
+ docstring const latex = ls.str();
+
+ // class='math' allows for use of jsMath
+ // http://www.math.union.edu/~dpvc/jsMath/
+ // FIXME XHTML
+ // probably should allow for some kind of customization here
+ xs << html::StartTag(tag, "class='math'")
+ << latex
+ << html::EndTag(tag);
+ xs.cr();
}
- } // end switch
return docstring();
}
}
+void InsetMathHull::recordLocation(DocIterator const & di)
+{
+ docit_ = di;
+}
+
} // namespace lyx