commit 097da3a943dc5b7df5272860662c41ed625d849a
Author: Jean-Marc Lasgouttes <lasgout...@lyx.org>
Date:   Mon Nov 5 21:29:47 2018 -1000

    Change cell numbers so that 0 is the main cell
    
    This leads to code simplification in overset, root and underset.
    Further simplification is possible.
---
 src/mathed/InsetMathOverset.cpp  |   43 +++++++++++++++++++----------
 src/mathed/InsetMathOverset.h    |    4 +--
 src/mathed/InsetMathRoot.cpp     |   23 ++++++++-------
 src/mathed/InsetMathRoot.h       |    5 +---
 src/mathed/InsetMathStackrel.cpp |   56 +++++++++++++++++++------------------
 src/mathed/InsetMathStackrel.h   |    4 ---
 src/mathed/InsetMathUnderset.cpp |   41 ++++++++++-----------------
 src/mathed/InsetMathUnderset.h   |    6 ----
 8 files changed, 86 insertions(+), 96 deletions(-)

diff --git a/src/mathed/InsetMathOverset.cpp b/src/mathed/InsetMathOverset.cpp
index 1295685..b659a9e 100644
--- a/src/mathed/InsetMathOverset.cpp
+++ b/src/mathed/InsetMathOverset.cpp
@@ -19,6 +19,8 @@
 #include "LaTeXFeatures.h"
 #include "MetricsInfo.h"
 
+#include "support/lassert.h"
+
 using namespace std;
 
 namespace lyx {
@@ -32,14 +34,14 @@ Inset * InsetMathOverset::clone() const
 void InsetMathOverset::metrics(MetricsInfo & mi, Dimension & dim) const
 {
        Changer dummy2 = mi.base.changeEnsureMath();
-       Dimension dim1;
-       cell(1).metrics(mi, dim1);
-       Changer dummy = mi.base.changeFrac();
        Dimension dim0;
        cell(0).metrics(mi, dim0);
-       dim.wid = max(dim0.width(), dim1.wid) + 4;
-       dim.asc = dim1.asc + dim0.height() + 4;
-       dim.des = dim1.des;
+       Changer dummy = mi.base.changeFrac();
+       Dimension dim1;
+       cell(1).metrics(mi, dim1);
+       dim.wid = max(dim1.width(), dim0.wid) + 4;
+       dim.asc = dim0.asc + dim1.height() + 4;
+       dim.des = dim0.des;
 }
 
 
@@ -47,13 +49,24 @@ void InsetMathOverset::draw(PainterInfo & pi, int x, int y) 
const
 {
        Changer dummy2 = pi.base.changeEnsureMath();
        Dimension const dim = dimension(*pi.base.bv);
-       Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
        Dimension const & dim1 = cell(1).dimension(*pi.base.bv);
+       Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
        int m  = x + dim.wid / 2;
-       int yo = y - dim1.asc - dim0.des - 1;
-       cell(1).draw(pi, m - dim1.wid / 2, y);
+       int yo = y - dim0.asc - dim1.des - 1;
+       cell(0).draw(pi, m - dim0.wid / 2, y);
        Changer dummy = pi.base.changeFrac();
-       cell(0).draw(pi, m - dim0.width() / 2, yo);
+       cell(1).draw(pi, m - dim1.width() / 2, yo);
+}
+
+
+bool InsetMathOverset::idxUpDown(Cursor & cur, bool up) const
+{
+       idx_type target = up; // up ? 1 : 0, since upper cell has idx 1
+       if (cur.idx() == target)
+               return false;
+       cur.idx() = target;
+       cur.pos() = cur.cell().x2pos(&cur.bv(), cur.x_target());
+       return true;
 }
 
 
@@ -62,27 +75,27 @@ void InsetMathOverset::write(WriteStream & os) const
        MathEnsurer ensurer(os);
        if (os.fragile())
                os << "\\protect";
-       os << "\\overset{" << cell(0) << "}{" << cell(1) << '}';
+       os << "\\overset{" << cell(1) << "}{" << cell(0) << '}';
 }
 
 
 void InsetMathOverset::normalize(NormalStream & os) const
 {
-       os << "[overset " << cell(0) << ' ' << cell(1) << ']';
+       os << "[overset " << cell(1) << ' ' << cell(0) << ']';
 }
 
 
 void InsetMathOverset::mathmlize(MathStream & ms) const
 {
-       ms << "<mover accent='false'>" << cell(1) << cell(0) << "</mover>";
+       ms << "<mover accent='false'>" << cell(0) << cell(1) << "</mover>";
 }
 
 
 void InsetMathOverset::htmlize(HtmlStream & os) const
 {
        os << MTag("span", "class='overset'")
-                << MTag("span", "class='top'") << cell(0) << ETag("span")
-                << MTag("span") << cell(1) << ETag("span")
+                << MTag("span", "class='top'") << cell(1) << ETag("span")
+                << MTag("span") << cell(0) << ETag("span")
                 << ETag("span");
 }
 
diff --git a/src/mathed/InsetMathOverset.h b/src/mathed/InsetMathOverset.h
index 1202bbc..eb6b86b 100644
--- a/src/mathed/InsetMathOverset.h
+++ b/src/mathed/InsetMathOverset.h
@@ -28,9 +28,7 @@ public:
        ///
        void draw(PainterInfo & pi, int x, int y) const;
        ///
-       idx_type firstIdx() const { return 1; }
-       ///
-       idx_type lastIdx() const { return 1; }
+       bool idxUpDown(Cursor & cur, bool up) const;
        ///
        void write(WriteStream & os) const;
        ///
diff --git a/src/mathed/InsetMathRoot.cpp b/src/mathed/InsetMathRoot.cpp
index 3cc8e2e..b3723fa 100644
--- a/src/mathed/InsetMathRoot.cpp
+++ b/src/mathed/InsetMathRoot.cpp
@@ -22,6 +22,7 @@
 
 #include "frontends/Painter.h"
 
+#include "support/lassert.h"
 
 using namespace std;
 
@@ -84,7 +85,7 @@ void mathed_root_metrics(MetricsInfo & mi, MathData const & 
nucleus,
 
 void InsetMathRoot::metrics(MetricsInfo & mi, Dimension & dim) const
 {
-       mathed_root_metrics(mi, cell(1), &cell(0), dim);
+       mathed_root_metrics(mi, cell(0), &cell(1), dim);
 }
 
 
@@ -127,26 +128,26 @@ void mathed_draw_root(PainterInfo & pi, int x, int y, 
MathData const & nucleus,
 
 void InsetMathRoot::draw(PainterInfo & pi, int x, int y) const
 {
-       mathed_draw_root(pi, x, y, cell(1), &cell(0), dimension(*pi.base.bv));
+       mathed_draw_root(pi, x, y, cell(0), &cell(1), dimension(*pi.base.bv));
 }
 
 
 void InsetMathRoot::write(WriteStream & os) const
 {
        MathEnsurer ensurer(os);
-       os << "\\sqrt[" << cell(0) << "]{" << cell(1) << '}';
+       os << "\\sqrt[" << cell(1) << "]{" << cell(0) << '}';
 }
 
 
 void InsetMathRoot::normalize(NormalStream & os) const
 {
-       os << "[root " << cell(0) << ' ' << cell(1) << ']';
+       os << "[root " << cell(1) << ' ' << cell(0) << ']';
 }
 
 
 bool InsetMathRoot::idxUpDown(Cursor & cur, bool up) const
 {
-       Cursor::idx_type const target = up ? 0 : 1;
+       Cursor::idx_type const target = up; //up ? 1 : 0;
        if (cur.idx() == target)
                return false;
        cur.idx() = target;
@@ -157,34 +158,34 @@ bool InsetMathRoot::idxUpDown(Cursor & cur, bool up) const
 
 void InsetMathRoot::maple(MapleStream & os) const
 {
-       os << '(' << cell(1) << ")^(1/(" << cell(0) <<"))";
+       os << '(' << cell(0) << ")^(1/(" << cell(1) <<"))";
 }
 
 
 void InsetMathRoot::mathematica(MathematicaStream & os) const
 {
-       os << '(' << cell(1) << ")^(1/(" << cell(0) <<"))";
+       os << '(' << cell(0) << ")^(1/(" << cell(1) <<"))";
 }
 
 
 void InsetMathRoot::octave(OctaveStream & os) const
 {
-       os << '(' << cell(1) << ")^(1/(" << cell(0) <<"))";
+       os << '(' << cell(0) << ")^(1/(" << cell(1) <<"))";
 }
 
 
 void InsetMathRoot::mathmlize(MathStream & os) const
 {
-       os << MTag("mroot") << cell(1) << cell(0) << ETag("mroot");
+       os << MTag("mroot") << cell(0) << cell(1) << ETag("mroot");
 }
 
 
 void InsetMathRoot::htmlize(HtmlStream & os) const
 {
        os << MTag("span", "class='root'")
-          << MTag("sup") << cell(0) << ETag("sup")
+          << MTag("sup") << cell(1) << ETag("sup")
           << from_ascii("&radic;")
-          << MTag("span", "class='rootof'")    << cell(1) << ETag("span")
+          << MTag("span", "class='rootof'")    << cell(0) << ETag("span")
                 << ETag("span");
 }
 
diff --git a/src/mathed/InsetMathRoot.h b/src/mathed/InsetMathRoot.h
index cb5c34f..63c35b6 100644
--- a/src/mathed/InsetMathRoot.h
+++ b/src/mathed/InsetMathRoot.h
@@ -26,14 +26,11 @@ public:
        InsetMathRoot(Buffer * buf);
        ///
        bool idxUpDown(Cursor & cur, bool up) const;
+
        ///
        void metrics(MetricsInfo & mi, Dimension & dim) const;
        ///
        void draw(PainterInfo & pi, int x, int y) const;
-       ///
-       idx_type firstIdx() const { return 1; }
-       ///
-       idx_type lastIdx() const { return 1; }
 
        ///
        void write(WriteStream & os) const;
diff --git a/src/mathed/InsetMathStackrel.cpp b/src/mathed/InsetMathStackrel.cpp
index dc01ffc..4f9623e 100644
--- a/src/mathed/InsetMathStackrel.cpp
+++ b/src/mathed/InsetMathStackrel.cpp
@@ -36,15 +36,17 @@ Inset * InsetMathStackrel::clone() const
 
 bool InsetMathStackrel::idxUpDown(Cursor & cur, bool up) const
 {
+       idx_type const npos = 1234; // impossible number
+       idx_type target = npos;
        if (up) {
-               if (cur.idx() == 0)
-                       return false;
+               idx_type const targets[] = { 1, npos, 0 };
+               target = targets[cur.idx()];
        } else {
-               if (cur.idx() + 1 ==  nargs())
-                       return false;
+               idx_type const targets[] = { 2, 0, npos };
+               target = targets[cur.idx()];
        }
-       InsetMath::idx_type target = up ? cur.idx() - 1 : cur.idx() + 1;
-       if (cur.idx() == target)
+
+       if (target == npos || target == nargs())
                return false;
        cur.idx() = target;
        cur.pos() = cell(target).x2pos(&cur.bv(), cur.x_target());
@@ -62,21 +64,21 @@ MathClass InsetMathStackrel::mathClass() const
 void InsetMathStackrel::metrics(MetricsInfo & mi, Dimension & dim) const
 {
        Changer dummy2 = mi.base.changeEnsureMath();
-       Dimension dim1;
-       cell(1).metrics(mi, dim1);
-       Changer dummy = mi.base.changeFrac();
        Dimension dim0;
        cell(0).metrics(mi, dim0);
+       Changer dummy = mi.base.changeFrac();
+       Dimension dim1;
+       cell(1).metrics(mi, dim1);
        if (nargs() > 2) {
                Dimension dim2;
                cell(2).metrics(mi, dim2);
-               dim.wid = max(max(dim0.width(), dim1.width()), dim2.width()) + 
4;
-               dim.asc = dim1.ascent() + dim0.height() + 4;
-               dim.des = dim1.descent() + dim2.height() + dim2.descent() + 1;
+               dim.wid = max(max(dim1.width(), dim0.width()), dim2.width()) + 
4;
+               dim.asc = dim0.ascent() + dim1.height() + 4;
+               dim.des = dim0.descent() + dim2.height() + dim2.descent() + 1;
        } else {
-               dim.wid = max(dim0.width(), dim1.width()) + 4;
-               dim.asc = dim1.ascent() + dim0.height() + 4;
-               dim.des = dim1.descent();
+               dim.wid = max(dim1.width(), dim0.width()) + 4;
+               dim.asc = dim0.ascent() + dim1.height() + 4;
+               dim.des = dim0.descent();
        }
 }
 
@@ -88,13 +90,13 @@ void InsetMathStackrel::draw(PainterInfo & pi, int x, int 
y) const
        Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
        Dimension const & dim1 = cell(1).dimension(*pi.base.bv);
        int m  = x + dim.width() / 2;
-       int yo = y - dim1.ascent() - dim0.descent() - 1;
-       cell(1).draw(pi, m - dim1.width() / 2, y);
+       int yo = y - dim0.ascent() - dim1.descent() - 1;
+       cell(0).draw(pi, m - dim0.width() / 2, y);
        Changer dummy = pi.base.changeFrac();
-       cell(0).draw(pi, m - dim0.width() / 2, yo);
+       cell(1).draw(pi, m - dim1.width() / 2, yo);
        if (nargs() > 2) {
                Dimension const & dim2 = cell(2).dimension(*pi.base.bv);
-               int y2 = y + dim1.descent() + dim2.ascent() + 1;
+               int y2 = y + dim0.descent() + dim2.ascent() + 1;
                cell(2).draw(pi, m - dim2.width() / 2, y2);
        }
 }
@@ -106,13 +108,13 @@ void InsetMathStackrel::write(WriteStream & os) const
        os << "\\stackrel";
        if (nargs() > 2)
                os << '[' << cell(2) << ']';
-       os << '{' << cell(0) << "}{" << cell(1) << '}';
+       os << '{' << cell(1) << "}{" << cell(0) << '}';
 }
 
 
 void InsetMathStackrel::normalize(NormalStream & os) const
 {
-       os << "[stackrel " << cell(0) << ' ' << cell(1);
+       os << "[stackrel " << cell(1) << ' ' << cell(0);
        if (nargs() > 2)
                os << ' ' << cell(2);
        os << ']';
@@ -122,9 +124,9 @@ void InsetMathStackrel::normalize(NormalStream & os) const
 void InsetMathStackrel::mathmlize(MathStream & ms) const
 {
        if (nargs() > 2)
-               ms << "<munderover>" << cell(1) << cell(2) << cell(0) << 
"</munderover>";
+               ms << "<munderover>" << cell(0) << cell(2) << cell(1) << 
"</munderover>";
        else
-               ms << "<mover accent='false'>" << cell(1) << cell(0) << 
"</mover>";
+               ms << "<mover accent='false'>" << cell(0) << cell(1) << 
"</mover>";
 }
 
 
@@ -132,14 +134,14 @@ void InsetMathStackrel::htmlize(HtmlStream & os) const
 {
        if (nargs() > 2) {
                os << MTag("span", "class='underoverset'")
-                  << MTag("span", "class='top'") << cell(0) << ETag("span")
-                  << MTag("span") << cell(1) << ETag("span")
+                  << MTag("span", "class='top'") << cell(1) << ETag("span")
+                  << MTag("span") << cell(0) << ETag("span")
                   << MTag("span", "class='bottom'") << cell(2) << ETag("span");
        } else {
                // at the moment, this is exactly the same as overset
                os << MTag("span", "class='overset'")
-                  << MTag("span", "class='top'") << cell(0) << ETag("span")
-                  << MTag("span") << cell(1) << ETag("span");
+                  << MTag("span", "class='top'") << cell(1) << ETag("span")
+                  << MTag("span") << cell(0) << ETag("span");
        }
        os << ETag("span");
 }
diff --git a/src/mathed/InsetMathStackrel.h b/src/mathed/InsetMathStackrel.h
index 2dc7a49..d6ac815 100644
--- a/src/mathed/InsetMathStackrel.h
+++ b/src/mathed/InsetMathStackrel.h
@@ -30,10 +30,6 @@ public:
        ///
        void draw(PainterInfo & pi, int x, int y) const;
        ///
-       idx_type firstIdx() const { return 1; }
-       ///
-       idx_type lastIdx() const { return 1; }
-       ///
        void write(WriteStream & os) const;
        ///
        void normalize(NormalStream &) const;
diff --git a/src/mathed/InsetMathUnderset.cpp b/src/mathed/InsetMathUnderset.cpp
index 4df7f59..5598890 100644
--- a/src/mathed/InsetMathUnderset.cpp
+++ b/src/mathed/InsetMathUnderset.cpp
@@ -33,14 +33,14 @@ Inset * InsetMathUnderset::clone() const
 void InsetMathUnderset::metrics(MetricsInfo & mi, Dimension & dim) const
 {
        Changer dummy2 = mi.base.changeEnsureMath();
-       Dimension dim1;
-       cell(1).metrics(mi, dim1);
-       Changer dummy = mi.base.changeFrac();
        Dimension dim0;
        cell(0).metrics(mi, dim0);
-       dim.wid = max(dim0.width(), dim1.width()) + 4;
-       dim.asc = dim1.ascent();
-       dim.des = dim1.descent() + dim0.height() + 4;
+       Changer dummy = mi.base.changeFrac();
+       Dimension dim1;
+       cell(1).metrics(mi, dim1);
+       dim.wid = max(dim1.width(), dim0.width()) + 4;
+       dim.asc = dim0.ascent();
+       dim.des = dim0.descent() + dim1.height() + 4;
 }
 
 
@@ -48,24 +48,13 @@ void InsetMathUnderset::draw(PainterInfo & pi, int x, int 
y) const
 {
        Changer dummy2 = pi.base.changeEnsureMath();
        Dimension const dim = dimension(*pi.base.bv);
-       Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
        Dimension const & dim1 = cell(1).dimension(*pi.base.bv);
+       Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
        int m  = x + dim.wid / 2;
-       int yo = y + dim1.descent() + dim0.ascent() + 1;
-       cell(1).draw(pi, m - dim1.width() / 2, y);
+       int yo = y + dim0.descent() + dim1.ascent() + 1;
+       cell(0).draw(pi, m - dim0.width() / 2, y);
        Changer dummy = pi.base.changeFrac();
-       cell(0).draw(pi, m - dim0.width() / 2, yo);
-}
-
-
-bool InsetMathUnderset::idxUpDown(Cursor & cur, bool up) const
-{
-       idx_type target = up; // up ? 1 : 0, since upper cell has idx 1
-       if (cur.idx() == target)
-               return false;
-       cur.idx() = target;
-       cur.pos() = cur.cell().x2pos(&cur.bv(), cur.x_target());
-       return true;
+       cell(1).draw(pi, m - dim1.width() / 2, yo);
 }
 
 
@@ -74,27 +63,27 @@ void InsetMathUnderset::write(WriteStream & os) const
        MathEnsurer ensurer(os);
        if (os.fragile())
                os << "\\protect";
-       os << "\\underset{" << cell(0) << "}{" << cell(1) << '}';
+       os << "\\underset{" << cell(1) << "}{" << cell(0) << '}';
 }
 
 
 void InsetMathUnderset::normalize(NormalStream & os) const
 {
-       os << "[underset " << cell(0) << ' ' << cell(1) << ']';
+       os << "[underset " << cell(1) << ' ' << cell(0) << ']';
 }
 
 
 void InsetMathUnderset::mathmlize(MathStream & ms) const
 {
-       ms << "<munder accent='false'>" << cell(1) << cell(0) << "</munder>";
+       ms << "<munder accent='false'>" << cell(0) << cell(1) << "</munder>";
 }
 
 
 void InsetMathUnderset::htmlize(HtmlStream & os) const
 {
        os << MTag("span", "class='underset'")
-                << MTag("span") << cell(0) << ETag("span")
-                << MTag("span", "class='bottom'") << cell(1) << ETag("span")
+                << MTag("span") << cell(1) << ETag("span")
+                << MTag("span", "class='bottom'") << cell(0) << ETag("span")
                 << ETag("span");
 }
 
diff --git a/src/mathed/InsetMathUnderset.h b/src/mathed/InsetMathUnderset.h
index ff671ad..fbc3e77 100644
--- a/src/mathed/InsetMathUnderset.h
+++ b/src/mathed/InsetMathUnderset.h
@@ -28,12 +28,6 @@ public:
        ///
        void draw(PainterInfo & pi, int x, int y) const;
        ///
-       idx_type firstIdx() const { return 1; }
-       ///
-       idx_type lastIdx() const { return 1; }
-       ///
-       bool idxUpDown(Cursor & cur, bool up) const;
-       ///
        void write(WriteStream & ws) const;
        ///
        void normalize(NormalStream & ns) const;

Reply via email to