]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_scriptinset.C
rename commandtags.h to lfuns.h and renumber/cleanup. Rebuild the tree !
[lyx.git] / src / mathed / math_scriptinset.C
index 75bc596e9bf13fd92941adf2a3138289638abaf3..3892ba88328da383d71a7d4ec6411e56dfc5d699 100644 (file)
@@ -1,11 +1,9 @@
-#ifdef __GNUG__
-#pragma implementation
-#endif
 
 #include "math_scriptinset.h"
 #include "math_support.h"
 #include "math_symbolinset.h"
 #include "math_mathmlstream.h"
+#include "funcrequest.h"
 #include "support/LAssert.h"
 #include "debug.h"
 
@@ -14,7 +12,7 @@ using std::max;
 
 
 MathScriptInset::MathScriptInset()
-       : MathNestInset(2), limits_(0)
+       : MathNestInset(3), limits_(0)
 {
        script_[0] = false;
        script_[1] = false;
@@ -22,13 +20,23 @@ MathScriptInset::MathScriptInset()
 
 
 MathScriptInset::MathScriptInset(bool up)
-       : MathNestInset(2), limits_(0)
+       : MathNestInset(3), limits_(0)
 {
        script_[0] = !up;
        script_[1] = up;
 }
 
 
+MathScriptInset::MathScriptInset(MathAtom const & at, bool up)
+       : MathNestInset(3), limits_(0)
+{
+       script_[0] = !up;
+       script_[1] = up;
+       cell(2).push_back(at);
+}
+
+
+
 MathInset * MathScriptInset::clone() const
 {
        return new MathScriptInset(*this);
@@ -47,27 +55,43 @@ MathScriptInset * MathScriptInset::asScriptInset()
 }
 
 
-MathXArray const & MathScriptInset::up() const
+bool MathScriptInset::idxFirst(idx_type & idx, pos_type & pos) const
+{
+       idx = 2;
+       pos = 0;
+       return true;
+}
+
+
+bool MathScriptInset::idxLast(idx_type & idx, pos_type & pos) const
 {
-       return xcell(1);
+       idx = 2;
+       pos = nuc().size();
+       return true;
 }
 
 
-MathXArray const & MathScriptInset::down() const
+MathArray const & MathScriptInset::down() const
 {
-       return xcell(0);
+       return cell(0);
 }
 
 
-MathXArray & MathScriptInset::up()
+MathArray & MathScriptInset::down()
 {
-       return xcell(1);
+       return cell(0);
 }
 
 
-MathXArray & MathScriptInset::down()
+MathArray const & MathScriptInset::up() const
 {
-       return xcell(0);
+       return cell(1);
+}
+
+
+MathArray & MathScriptInset::up()
+{
+       return cell(1);
 }
 
 
@@ -77,13 +101,25 @@ void MathScriptInset::ensure(bool up)
 }
 
 
-int MathScriptInset::dy0(MathInset const * nuc) const
+MathArray const & MathScriptInset::nuc() const
+{
+       return cell(2);
+}
+
+
+MathArray & MathScriptInset::nuc()
 {
-       int nd = ndes(nuc);
+       return cell(2);
+}
+
+
+int MathScriptInset::dy0() const
+{
+       int nd = ndes();
        if (!hasDown())
                return nd;
        int des = down().ascent();
-       if (hasLimits(nuc))
+       if (hasLimits())
                des += nd + 2;
        else
                des = max(des, nd);
@@ -91,170 +127,126 @@ int MathScriptInset::dy0(MathInset const * nuc) const
 }
 
 
-int MathScriptInset::dy1(MathInset const * nuc) const
+int MathScriptInset::dy1() const
 {
-       int na = nasc(nuc);
+       int na = nasc();
        if (!hasUp())
                return na;
        int asc = up().descent();
-       if (hasLimits(nuc))
+       if (hasLimits())
                asc += na + 2;
        else
                asc = max(asc, na);
-       asc = max(asc, mathed_char_ascent(font_, 'I'));
+       asc = max(asc, 5);
        return asc;
 }
 
 
-int MathScriptInset::dx0(MathInset const * nuc) const
+int MathScriptInset::dx0() const
 {
        lyx::Assert(hasDown());
-       return hasLimits(nuc) ? (width2(nuc) - down().width()) / 2 : nwid(nuc);
+       return hasLimits() ? (dim_.w - down().width()) / 2 : nwid();
 }
 
 
-int MathScriptInset::dx1(MathInset const * nuc) const
+int MathScriptInset::dx1() const
 {
        lyx::Assert(hasUp());
-       return hasLimits(nuc) ? (width2(nuc) - up().width()) / 2 : nwid(nuc);
-}
-
-
-int MathScriptInset::dxx(MathInset const * nuc) const
-{
-       //lyx::Assert(nuc());
-       return hasLimits(nuc)  ?  (width2(nuc) - nwid(nuc)) / 2  :  0;
-}
-
-
-void MathScriptInset::dimensions2
-       (MathInset const * nuc, int & w, int & a, int & d) const
-{
-       a = dy1(nuc) + (hasUp() ? up().ascent() : 0);
-       d = dy0(nuc) + (hasDown() ? down().descent() : 0);
-       w = width2(nuc);
+       return hasLimits() ? (dim_.w - up().width()) / 2 : nwid();
 }
 
 
-int MathScriptInset::width2(MathInset const * nuc) const
+int MathScriptInset::dxx() const
 {
-       int w = 0;
-       if (hasLimits(nuc)) {
-               w = nwid(nuc);
-               if (hasUp())
-                       w = max(w, up().width());
-               if (hasDown())
-                       w = max(w, down().width());
-       } else {
-               if (hasUp())
-                       w = max(w, up().width());
-               if (hasDown())
-                       w = max(w, down().width());
-               w += nwid(nuc);
-       }
-       return w;
+       return hasLimits() ? (dim_.w - nwid()) / 2  :  0;
 }
 
 
-int MathScriptInset::nwid(MathInset const * nuc) const
+int MathScriptInset::nwid() const
 {
-       return nuc ?  nuc->width() : mathed_char_width(font_, '.');
+       return nuc().size() ? nuc().width() : 2;
 }
 
 
-int MathScriptInset::nasc(MathInset const * nuc) const
+int MathScriptInset::nasc() const
 {
-       return nuc ? nuc->ascent() : mathed_char_ascent(font_, 'I');
+       return nuc().size() ? nuc().ascent() : 5;
 }
 
 
-int MathScriptInset::ndes(MathInset const * nuc) const
+int MathScriptInset::ndes() const
 {
-       return nuc ? nuc->descent() : mathed_char_descent(font_, 'I');
+       return nuc().size() ? nuc().descent() : 0;
 }
 
 
 void MathScriptInset::metrics(MathMetricsInfo & mi) const
 {
-       metrics(0, mi);
-}
-
-
-void MathScriptInset::metrics(MathInset const * nuc, MathMetricsInfo & mi) const
-{
-       if (nuc)
-               nuc->metrics(mi);
-       MathNestInset::metrics(mi);
+       cell(2).metrics(mi);
        MathScriptChanger dummy(mi.base);
-       dimensions2(nuc, width_, ascent_, descent_);
+       cell(0).metrics(mi);
+       cell(1).metrics(mi);
+       dim_.w = 0;
+       if (hasLimits()) {
+               dim_.w = nwid();
+               if (hasUp())
+                       dim_.w = max(dim_.w, up().width());
+               if (hasDown())
+                       dim_.w = max(dim_.w, down().width());
+       } else {
+               if (hasUp())
+                       dim_.w = max(dim_.w, up().width());
+               if (hasDown())
+                       dim_.w = max(dim_.w, down().width());
+               dim_.w += nwid();
+       }
+       dim_.a = dy1() + (hasUp() ? up().ascent() : 0);
+       dim_.d = dy0() + (hasDown() ? down().descent() : 0);
+       metricsMarkers2();
 }
 
 
 void MathScriptInset::draw(MathPainterInfo & pi, int x, int y) const
 {
-       //lyxerr << "unexpected call to MathScriptInset::draw()\n";
-       draw(0, pi, x, y);
-}
-
-
-void MathScriptInset::draw(MathInset const * nuc, MathPainterInfo & pi,
-       int x, int y) const
-{
-       if (nuc)
-               nuc->draw(pi, x + dxx(nuc), y);
-       else if (editing())
-               drawStr(pi, font_, x + dxx(nuc), y, ".");
-
+       if (nuc().size())
+               nuc().draw(pi, x + dxx(), y);
+       else {
+               nuc().setXY(x + dxx(), y);
+               if (editing())
+                       drawStr(pi, pi.base.font, x + dxx(), y, ".");
+       }
        MathScriptChanger dummy(pi.base);
        if (hasUp())
-               up().draw(pi, x + dx1(nuc), y - dy1(nuc));
+               up().draw(pi, x + dx1(), y - dy1());
        if (hasDown())
-               down().draw(pi, x + dx0(nuc), y + dy0(nuc));
+               down().draw(pi, x + dx0(), y + dy0());
+       drawMarkers2(pi, x, y);
 }
 
 
 void MathScriptInset::metricsT(TextMetricsInfo const & mi) const
-{
-       metricsT(0, mi);
-}
-
-
-void MathScriptInset::metricsT(MathInset const * nuc,
-       TextMetricsInfo const & mi) const
 {
        if (hasUp())
                up().metricsT(mi);
        if (hasDown())
                down().metricsT(mi);
-       if (nuc)
-               nuc->metricsT(mi);
-       //ascent_  = ascent2(nuc);
-       //descent_ = descent2(nuc);
-       //width_   = width2(nuc);
+       nuc().metricsT(mi);
 }
 
 
 void MathScriptInset::drawT(TextPainter & pain, int x, int y) const
 {
-       //lyxerr << "unexpected call to MathScriptInset::draw()\n";
-       drawT(0, pain, x, y);
-}
-
-
-void MathScriptInset::drawT(MathInset const * nuc, TextPainter & pain,
-       int x, int y) const
-{
-       if (nuc)
-               nuc->drawT(pain, x + dxx(nuc), y);
+       if (nuc().size())
+               nuc().drawT(pain, x + dxx(), y);
        if (hasUp())
-               up().drawT(pain, x + dx1(nuc), y - dy1(nuc));
+               up().drawT(pain, x + dx1(), y - dy1());
        if (hasDown())
-               down().drawT(pain, x + dx0(nuc), y + dy0(nuc));
+               down().drawT(pain, x + dx0(), y + dy0());
 }
 
 
 
-bool MathScriptInset::hasLimits(MathInset const * nuc) const
+bool MathScriptInset::hasLimits() const
 {
        // obvious cases
        if (limits_ == 1)
@@ -263,14 +255,14 @@ bool MathScriptInset::hasLimits(MathInset const * nuc) const
                return false;
 
        // we can only display limits if the nucleus wants some
-       if (!nuc)
+       if (!nuc().size())
                return false;
-       if (!nuc->isScriptable())
+       if (!nuc().back()->isScriptable())
                return false;
 
        // per default \int has limits beside the \int even in displayed formulas
-       if (nuc->asSymbolInset())
-               if (nuc->asSymbolInset()->name().find("int") != string::npos)
+       if (nuc().back()->asSymbolInset())
+               if (nuc().back()->asSymbolInset()->name().find("int") != string::npos)
                        return false;
 
        // assume "real" limits for everything else
@@ -278,16 +270,6 @@ bool MathScriptInset::hasLimits(MathInset const * nuc) const
 }
 
 
-void MathScriptInset::removeEmptyScripts()
-{
-       for (int i = 0; i <= 1; ++i)
-               if (script_[i] && cell(i).size() == 0) {
-                       cell(i).clear();
-                       script_[i] = false;
-               }
-}
-
-
 void MathScriptInset::removeScript(bool up)
 {
        cell(up).clear();
@@ -301,12 +283,6 @@ bool MathScriptInset::has(bool up) const
 }
 
 
-bool MathScriptInset::empty() const
-{
-       return !script_[0] && !script_[1];
-}
-
-
 bool MathScriptInset::hasUp() const
 {
        return script_[1];
@@ -319,114 +295,146 @@ bool MathScriptInset::hasDown() const
 }
 
 
-bool MathScriptInset::idxRight(MathInset::idx_type &,
-                                MathInset::pos_type &) const
+bool MathScriptInset::idxRight(idx_type &, pos_type &) const
 {
        return false;
 }
 
 
-bool MathScriptInset::idxLeft(MathInset::idx_type &,
-                               MathInset::pos_type &) const
+bool MathScriptInset::idxLeft(idx_type &, pos_type &) const
 {
        return false;
 }
 
 
-void MathScriptInset::write(WriteStream & os) const
+bool MathScriptInset::idxUpDown(idx_type & idx, pos_type & pos, bool up,
+       int) const
 {
-       //lyxerr << "unexpected call to MathScriptInset::write()\n";
-       write2(0, os);
+       if (idx == 1) {
+               // if we are 'up' we can't go further up
+               if (up)
+                       return false;
+               // otherwise go to last base position
+               idx = 2;
+               pos = cell(2).size();
+       }
+
+       else if (idx == 0) {
+               // if we are 'down' we can't go further down
+               if (!up)
+                       return false;
+               idx = 2;
+               pos = cell(2).size();
+       }
+
+       else {
+               // in nucleus
+               // don't go up/down if there is no cell.
+               if (!has(up))
+                       return false;
+               // go up/down only if in the last position
+               // or in the first position of something with displayed limits
+               if (pos == cell(2).size() || (pos == 0 && hasLimits())) {
+                       idx = up;
+                       pos = 0;
+                       return true;
+               }
+               return false;
+       }
+       return true;
 }
 
 
-void MathScriptInset::write2(MathInset const * nuc, WriteStream & os) const
+void MathScriptInset::write(WriteStream & os) const
 {
-       if (nuc) {
-               os << nuc;
-               if (nuc->takesLimits()) {
+       if (nuc().size()) {
+               os << nuc();
+               //if (nuc().back()->takesLimits()) {
                        if (limits_ == -1)
                                os << "\\nolimits ";
                        if (limits_ == 1)
                                os << "\\limits ";
-               }
-       } else
-                       if (os.firstitem())
-                               lyxerr[Debug::MATHED] << "suppressing {} when writing\n";
-                       else
-                               os << "{}";
-
-       if (hasDown() && down().data().size())
-               os << "_{" << down().data() << '}';
+               //}
+       } else {
+               if (os.firstitem())
+                       lyxerr[Debug::MATHED] << "suppressing {} when writing\n";
+               else
+                       os << "{}";
+       }
 
-       if (hasUp() && up().data().size())
-               os << "^{" << up().data() << '}';
-}
+       if (hasDown() && down().size())
+               os << "_{" << down() << '}';
 
+       if (hasUp() && up().size())
+               os << "^{" << up() << '}';
 
-void MathScriptInset::normalize(NormalStream & os) const
-{
-       //lyxerr << "unexpected call to MathScriptInset::normalize()\n";
-       normalize2(0, os);
+       if (lock_ && !os.latex())
+               os << "\\lyxlock ";
 }
 
 
-void MathScriptInset::normalize2(MathInset const * nuc, NormalStream & os) const
+void MathScriptInset::normalize(NormalStream & os) const
 {
-       bool d = hasDown() && down().data().size();
-       bool u = hasUp() && up().data().size();
+       bool d = hasDown() && down().size();
+       bool u = hasUp() && up().size();
 
-       if (u)
+       if (u && d)
+               os << "[subsup ";
+       else if (u)
                os << "[sup ";
-       if (d)
+       else if (d)
                os << "[sub ";
 
-       if (nuc)
-               os << nuc << ' ';
+       if (nuc().size())
+               os << nuc() << ' ';
        else
                os << "[par]";
 
-       if (d)
-               os << down().data() << ']';
-       if (u)
-               os << up().data() << ']';
+       if (u && d)
+               os << down() << ' ' << up() << ']';
+       else if (d)
+               os << down() << ']';
+       else if (u)
+               os << up() << ']';
 }
 
 
-void MathScriptInset::maplize2(MathInset const * nuc, MapleStream & os) const
+void MathScriptInset::maple(MapleStream & os) const
 {
-       if (nuc)
-               os << nuc;
-       if (hasDown() && down().data().size())
-               os << '[' << down().data() << ']';
-       if (hasUp() && up().data().size())
-               os << "^(" << up().data() << ')';
+       if (nuc().size())
+               os << nuc();
+       if (hasDown() && down().size())
+               os << '[' << down() << ']';
+       if (hasUp() && up().size())
+               os << "^(" << up() << ')';
 }
 
 
-void MathScriptInset::mathematicize2(MathInset const * nuc, MathematicaStream & os) const
+void MathScriptInset::mathematica(MathematicaStream & os) const
 {
-       bool d = hasDown() && down().data().size();
-       bool u = hasUp() && up().data().size();
+       bool d = hasDown() && down().size();
+       bool u = hasUp() && up().size();
 
-       if (nuc)
-               if (d)  //subscript only if nuc !
-                       os << "Subscript[" << nuc;
+       if (nuc().size()) {
+               if (d)
+                       os << "Subscript[" << nuc();
                else
-                       os << nuc;
+                       os << nuc();
+       }
+
        if (u)
-               os << "^(" << up().data() << ")";
+               os << "^(" << up() << ')';
 
-       if (nuc)
+       if (nuc().size())
                if (d)
-               os << "," << down().data() << "]"; 
+                       os << ',' << down() << ']';
 }
 
 
-void MathScriptInset::mathmlize2(MathInset const * nuc, MathMLStream & os) const
+void MathScriptInset::mathmlize( MathMLStream & os) const
 {
-       bool d = hasDown() && down().data().size();
-       bool u = hasUp() && up().data().size();
+       bool d = hasDown() && down().size();
+       bool u = hasUp() && up().size();
 
        if (u && d)
                os << MTag("msubsup");
@@ -435,33 +443,73 @@ void MathScriptInset::mathmlize2(MathInset const * nuc, MathMLStream & os) const
        else if (d)
                os << MTag("msub");
 
-       if (nuc)
-               os << nuc;
+       if (nuc().size())
+               os << nuc();
        else
                os << "<mrow/>";
 
        if (u && d)
-               os << down().data() << up().data() << ETag("msubsup");
+               os << down() << up() << ETag("msubsup");
        else if (u)
-               os << up().data() << ETag("msup");
+               os << up() << ETag("msup");
        else if (d)
-               os << down().data() << ETag("msub");
+               os << down() << ETag("msub");
 }
 
 
-void MathScriptInset::octavize2(MathInset const * nuc, OctaveStream & os) const
+void MathScriptInset::octave(OctaveStream & os) const
 {
-       if (nuc)
-               os << nuc;
-       if (hasDown() && down().data().size())
-               os << '[' << down().data() << ']';
-       if (hasUp() && up().data().size())
-               os << "^(" << up().data() << ')';
+       if (nuc().size())
+               os << nuc();
+       if (hasDown() && down().size())
+               os << '[' << down() << ']';
+       if (hasUp() && up().size())
+               os << "^(" << up() << ')';
 }
 
 
 void MathScriptInset::infoize(std::ostream & os) const
+{
+       os << "Scripts";
+}
+
+
+void MathScriptInset::infoize2(std::ostream & os) const
 {
        if (limits_)
-               os << (limits_ == 1 ? "Displayed limits " : "Inlined limits ");
+               os << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
+}
+
+
+void MathScriptInset::notifyCursorLeaves(idx_type idx)
+{
+       MathNestInset::notifyCursorLeaves(idx);
+
+       // remove empty scripts if possible
+       if (idx != 2 && script_[idx] && cell(idx).empty()) {
+               cell(idx).clear();
+               script_[idx] = false;
+       }
+}
+
+
+dispatch_result MathScriptInset::dispatch
+       (FuncRequest const & cmd, idx_type & idx, pos_type & pos)
+{
+       if (cmd.action == LFUN_MATH_LIMITS) {
+               if (!cmd.argument.empty()) {
+                       if (cmd.argument == "limits")
+                               limits_ = 1;
+                       else if (cmd.argument == "nolimits")
+                               limits_ = -1;
+                       else
+                               limits_ = 0;
+               } else if (limits_ == 0)
+                       limits_ =  (hasLimits()) ? -1 : 1;
+               else
+                       limits_ = 0;
+               return DISPATCHED;
+       }
+
+       return MathNestInset::dispatch(cmd, idx, pos);
 }