From 8fde2a6fa762cfc72539d38460b9dd5c9e4d2e92 Mon Sep 17 00:00:00 2001 From: Enrico Forestieri Date: Thu, 4 Sep 2008 22:24:07 +0000 Subject: [PATCH] Implement the EnsureMath inset in mathed for nesting math mode in text mode. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@26304 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/Makefile.am | 2 + src/mathed/InsetMathEnsureMath.cpp | 76 ++++++++++++++++++++++++++++++ src/mathed/InsetMathEnsureMath.h | 47 ++++++++++++++++++ src/mathed/MathFactory.cpp | 3 ++ src/mathed/MathParser.cpp | 24 ++++------ 5 files changed, 137 insertions(+), 15 deletions(-) create mode 100644 src/mathed/InsetMathEnsureMath.cpp create mode 100644 src/mathed/InsetMathEnsureMath.h diff --git a/src/Makefile.am b/src/Makefile.am index 88b1e11e49..d66238b64e 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -338,6 +338,7 @@ SOURCEFILESMATHED = \ mathed/InsetMathDelim.cpp \ mathed/InsetMathDiff.cpp \ mathed/InsetMathDots.cpp \ + mathed/InsetMathEnsureMath.cpp \ mathed/InsetMathEnv.cpp \ mathed/InsetMathExFunc.cpp \ mathed/InsetMathExInt.cpp \ @@ -400,6 +401,7 @@ HEADERFILESMATHED = \ mathed/InsetMathDelim.h \ mathed/InsetMathDiff.h \ mathed/InsetMathDots.h \ + mathed/InsetMathEnsureMath.h \ mathed/InsetMathEnv.h \ mathed/InsetMathExFunc.h \ mathed/InsetMathExInt.h \ diff --git a/src/mathed/InsetMathEnsureMath.cpp b/src/mathed/InsetMathEnsureMath.cpp new file mode 100644 index 0000000000..8180667366 --- /dev/null +++ b/src/mathed/InsetMathEnsureMath.cpp @@ -0,0 +1,76 @@ +/** + * \file InsetMathEnsureMath.cpp + * This file is part of LyX, the document processor. + * Licence details can be found in the file COPYING. + * + * \author André Pönitz + * \author Enrico Forestieri + * + * Full author contact details are available in file CREDITS. + */ + +#include + +#include "InsetMathEnsureMath.h" + +#include "MathStream.h" +#include "MathData.h" + +#include + + +namespace lyx { + +InsetMathEnsureMath::InsetMathEnsureMath() + : InsetMathNest(1) +{} + + +Inset * InsetMathEnsureMath::clone() const +{ + return new InsetMathEnsureMath(*this); +} + + +void InsetMathEnsureMath::metrics(MetricsInfo & mi, Dimension & dim) const +{ + FontSetChanger dummy(mi.base, "mathnormal"); + cell(0).metrics(mi, dim); + metricsMarkers(dim); +} + + +void InsetMathEnsureMath::draw(PainterInfo & pi, int x, int y) const +{ + FontSetChanger dummy(pi.base, "mathnormal"); + cell(0).draw(pi, x, y); + drawMarkers(pi, x, y); +} + + +void InsetMathEnsureMath::metricsT(TextMetricsInfo const & mi, Dimension & dim) const +{ + cell(0).metricsT(mi, dim); +} + + +void InsetMathEnsureMath::drawT(TextPainter & pain, int x, int y) const +{ + cell(0).drawT(pain, x, y); +} + + +void InsetMathEnsureMath::write(WriteStream & os) const +{ + ModeSpecifier specifier(os, MATH_MODE); + os << "\\ensuremath{" << cell(0) << "}"; +} + + +void InsetMathEnsureMath::infoize(odocstream & os) const +{ + os << "EnsureMath"; +} + + +} // namespace lyx diff --git a/src/mathed/InsetMathEnsureMath.h b/src/mathed/InsetMathEnsureMath.h new file mode 100644 index 0000000000..0acc7a5fdb --- /dev/null +++ b/src/mathed/InsetMathEnsureMath.h @@ -0,0 +1,47 @@ +// -*- C++ -*- +/** + * \file InsetMathEnsureMath.h + * This file is part of LyX, the document processor. + * Licence details can be found in the file COPYING. + * + * \author André Pönitz + * \author Enrico Forestieri + * + * Full author contact details are available in file CREDITS. + */ + +#ifndef MATH_ENSUREMATHINSET_H +#define MATH_ENSUREMATHINSET_H + +#include "InsetMathNest.h" + + +namespace lyx { + + +/// Inset for ensuring math mode +class InsetMathEnsureMath : public InsetMathNest { +public: + InsetMathEnsureMath(); + /// + mode_type currentMode() const { return MATH_MODE; } + /// + void metrics(MetricsInfo & mi, Dimension & dim) const; + /// + void draw(PainterInfo & pi, int x, int y) const; + /// + void metricsT(TextMetricsInfo const & mi, Dimension & dim) const; + /// + void drawT(TextPainter & pi, int x, int y) const; + /// + void write(WriteStream & os) const; + /// + void infoize(odocstream & os) const; +private: + virtual Inset * clone() const; +}; + + +} // namespace lyx + +#endif diff --git a/src/mathed/MathFactory.cpp b/src/mathed/MathFactory.cpp index 13159b7b43..9e3bbca20b 100644 --- a/src/mathed/MathFactory.cpp +++ b/src/mathed/MathFactory.cpp @@ -20,6 +20,7 @@ #include "InsetMathColor.h" #include "InsetMathDecoration.h" #include "InsetMathDots.h" +#include "InsetMathEnsureMath.h" #include "InsetMathFont.h" #include "InsetMathFontOld.h" #include "InsetMathFrac.h" @@ -443,6 +444,8 @@ MathAtom createInsetMath(docstring const & s) return MathAtom(new InsetMathPhantom(InsetMathPhantom::phantom)); if (s == "vphantom") return MathAtom(new InsetMathPhantom(InsetMathPhantom::vphantom)); + if (s == "ensuremath") + return MathAtom(new InsetMathEnsureMath); return MathAtom(new MathMacro(s)); } diff --git a/src/mathed/MathParser.cpp b/src/mathed/MathParser.cpp index 1831114035..bc398467d0 100644 --- a/src/mathed/MathParser.cpp +++ b/src/mathed/MathParser.cpp @@ -1526,7 +1526,7 @@ void Parser::parse1(InsetMathGrid & grid, unsigned flags, } #endif - else if (t.cs() == "lyxmathsym" || t.cs() == "ensuremath") { + else if (t.cs() == "lyxmathsym") { skipSpaces(); if (getToken().cat() != catBegin) { error("'{' expected in \\" + t.cs()); @@ -1547,22 +1547,16 @@ void Parser::parse1(InsetMathGrid & grid, unsigned flags, error("'}' expected in \\" + t.cs()); return; } - if (t.cs() == "ensuremath") { + docstring rem; + cmd = Encodings::fromLaTeXCommand(cmd, rem); + for (size_t i = 0; i < cmd.size(); ++i) + cell->push_back(MathAtom(new InsetMathChar(cmd[i]))); + if (rem.size()) { + MathAtom at = createInsetMath(t.cs()); + cell->push_back(at); MathData ar; - mathed_parse_cell(ar, cmd); + mathed_parse_cell(ar, '{' + rem + '}'); cell->append(ar); - } else { - docstring rem; - cmd = Encodings::fromLaTeXCommand(cmd, rem); - for (size_t i = 0; i < cmd.size(); ++i) - cell->push_back(MathAtom(new InsetMathChar(cmd[i]))); - if (rem.size()) { - MathAtom at = createInsetMath(t.cs()); - cell->push_back(at); - MathData ar; - mathed_parse_cell(ar, '{' + rem + '}'); - cell->append(ar); - } } } -- 2.39.2