]> git.lyx.org Git - features.git/commitdiff
Implement the EnsureMath inset in mathed for nesting math mode in text mode.
authorEnrico Forestieri <forenr@lyx.org>
Thu, 4 Sep 2008 22:24:07 +0000 (22:24 +0000)
committerEnrico Forestieri <forenr@lyx.org>
Thu, 4 Sep 2008 22:24:07 +0000 (22:24 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@26304 a592a061-630c-0410-9148-cb99ea01b6c8

src/Makefile.am
src/mathed/InsetMathEnsureMath.cpp [new file with mode: 0644]
src/mathed/InsetMathEnsureMath.h [new file with mode: 0644]
src/mathed/MathFactory.cpp
src/mathed/MathParser.cpp

index 88b1e11e4911978f29f01e3483b3e3841dc3ab09..d66238b64ea6f0aedb5cfb8f2fc20ca28e4e324c 100644 (file)
@@ -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 (file)
index 0000000..8180667
--- /dev/null
@@ -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 <config.h>
+
+#include "InsetMathEnsureMath.h"
+
+#include "MathStream.h"
+#include "MathData.h"
+
+#include <ostream>
+
+
+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 (file)
index 0000000..0acc7a5
--- /dev/null
@@ -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
index 13159b7b43dffa5150e2ce4eb90ea42c4d0f20f3..9e3bbca20b6cba44483f7d9abdf08a3687f96eae 100644 (file)
@@ -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));
 }
index 18311140351910f7fe84d72acdf3ae87001a7943..bc398467d0a6567cef4c1a43d64a4ab366aa4a7b 100644 (file)
@@ -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);
-                               }
                        }
                }