]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_scriptinset.C
no special treatment for scriptinsets
[lyx.git] / src / mathed / math_scriptinset.C
index a66ec9c67624912ff435eb340c65745d484e0484..10223d75b15adf06231067900fa59fe24472da80 100644 (file)
@@ -1,24 +1,20 @@
-
-#include <config.h>
-#include "Lsstream.h"
-
-#include "debug.h"
-#include "support.h"
-#include "support/LOstream.h"
-#include "support/LAssert.h"
-
 #ifdef __GNUG__
 #pragma implementation
 #endif
 
 #include "math_scriptinset.h"
+#include "math_support.h"
+#include "math_symbolinset.h"
+#include "math_mathmlstream.h"
+#include "support/LAssert.h"
+#include "debug.h"
 
-using std::ostream;
-using std::ostringstream;
+
+using std::max;
 
 
 MathScriptInset::MathScriptInset()
-       : MathNestInset(2), limits_(0)
+       : MathNestInset(3), limits_(0)
 {
        script_[0] = false;
        script_[1] = false;
@@ -26,13 +22,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);
@@ -51,9 +57,19 @@ MathScriptInset * MathScriptInset::asScriptInset()
 }
 
 
-MathXArray const & MathScriptInset::up() const
+bool MathScriptInset::idxFirst(idx_type & idx, pos_type & pos) const
 {
-       return xcell(1);
+       idx = 2;
+       pos = 0;
+       return true;
+}
+
+
+bool MathScriptInset::idxLast(idx_type & idx, pos_type & pos) const
+{
+       idx = 2;
+       pos = nuc().size();
+       return true;
 }
 
 
@@ -63,15 +79,21 @@ MathXArray const & MathScriptInset::down() const
 }
 
 
-MathXArray & MathScriptInset::up()
+MathXArray & MathScriptInset::down()
+{
+       return xcell(0);
+}
+
+
+MathXArray const & MathScriptInset::up() const
 {
        return xcell(1);
 }
 
 
-MathXArray & MathScriptInset::down()
+MathXArray & MathScriptInset::up()
 {
-       return xcell(0);
+       return xcell(1);
 }
 
 
@@ -81,268 +103,364 @@ void MathScriptInset::ensure(bool up)
 }
 
 
-int MathScriptInset::dy0(MathInset const * nuc) const
+MathXArray const & MathScriptInset::nuc() const
 {
-       int nd = ndes(nuc);
+       return xcell(2);
+}
+
+
+MathXArray & MathScriptInset::nuc()
+{
+       return xcell(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 = std::max(des, nd);
+       else
+               des = max(des, nd);
        return des;
 }
 
 
-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 = std::max(asc, na);
-       asc = std::max(asc, mathed_char_ascent(LM_TC_VAR, mi_, 'I'));
+       else
+               asc = max(asc, na);
+       asc = max(asc, mathed_char_ascent(font_, 'I'));
        return asc;
 }
 
 
-int MathScriptInset::dx0(MathInset const * nuc) const
+int MathScriptInset::dx0() const
 {
        lyx::Assert(hasDown());
-       return hasLimits(nuc) ? (width(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) ? (width(nuc) - up().width()) / 2 : nwid(nuc);
+       return hasLimits() ? (dim_.w - up().width()) / 2 : nwid();
+}
+
+
+int MathScriptInset::dxx() const
+{
+       return hasLimits() ? (dim_.w - nwid()) / 2  :  0;
 }
 
 
-int MathScriptInset::dxx(MathInset const * nuc) const
+int MathScriptInset::nwid() const
 {
-       //lyx::Assert(nuc());
-       return hasLimits(nuc)  ?  (width(nuc) - nwid(nuc)) / 2  :  0;
+       return nuc().size() ? nuc().width() : mathed_char_width(font_, '.');
 }
 
 
-int MathScriptInset::ascent(MathInset const * nuc) const
+int MathScriptInset::nasc() const
 {
-       return dy1(nuc) + (hasUp() ? up().ascent() : 0);
+       return nuc().size() ? nuc().ascent() : mathed_char_ascent(font_, 'I');
 }
 
 
-int MathScriptInset::descent(MathInset const * nuc) const
+int MathScriptInset::ndes() const
 {
-       return dy0(nuc) + (hasDown() ? down().descent() : 0);
+       return nuc().size() ? nuc().descent() : mathed_char_descent(font_, 'I');
 }
 
 
-int MathScriptInset::width(MathInset const * nuc) const
+void MathScriptInset::metrics(MathMetricsInfo & mi) const
 {
-       int wid = 0;
-       if (hasLimits(nuc)) {
-               wid = nwid(nuc);
+       MathNestInset::metrics(mi);
+       MathScriptChanger dummy(mi.base);
+       dim_.w = 0;
+       if (hasLimits()) {
+               dim_.w = nwid();
                if (hasUp())
-                       wid = std::max(wid, up().width());
+                       dim_.w = max(dim_.w, up().width());
                if (hasDown())
-                       wid = std::max(wid, down().width());
+                       dim_.w = max(dim_.w, down().width());
        } else {
                if (hasUp())
-                       wid = std::max(wid, up().width());
+                       dim_.w = max(dim_.w, up().width());
                if (hasDown())
-                       wid = std::max(wid, down().width());
-               wid += nwid(nuc);
+                       dim_.w = max(dim_.w, down().width());
+               dim_.w += nwid();
        }
-       return wid;
+       dim_.a = dy1() + (hasUp() ? up().ascent() : 0);
+       dim_.d = dy0() + (hasDown() ? down().descent() : 0);
+       metricsMarkers2();
 }
 
 
-int MathScriptInset::nwid(MathInset const * nuc) const
+void MathScriptInset::draw(MathPainterInfo & pi, int x, int y) const
 {
-       return nuc ?
-               nuc->width() :
-               mathed_char_width(LM_TC_TEX, mi_, '.');
+       if (nuc().size())
+               nuc().draw(pi, x + dxx(), y);
+       else if (editing())
+               drawStr(pi, font_, x + dxx(), y, ".");
+       MathScriptChanger dummy(pi.base);
+       if (hasUp())
+               up().draw(pi, x + dx1(), y - dy1());
+       if (hasDown())
+               down().draw(pi, x + dx0(), y + dy0());
+       drawMarkers2(pi, x, y);
 }
 
 
-int MathScriptInset::nasc(MathInset const * nuc) const
+void MathScriptInset::metricsT(TextMetricsInfo const & mi) const
 {
-       return nuc ? nuc->ascent()
-               : mathed_char_ascent(LM_TC_VAR, mi_, 'I');
+       if (hasUp())
+               up().metricsT(mi);
+       if (hasDown())
+               down().metricsT(mi);
+       nuc().metricsT(mi);
 }
 
 
-int MathScriptInset::ndes(MathInset const * nuc) const
+void MathScriptInset::drawT(TextPainter & pain, int x, int y) const
 {
-       return nuc ? nuc->descent()
-               : mathed_char_descent(LM_TC_VAR, mi_, 'I');
+       if (nuc().size())
+               nuc().drawT(pain, x + dxx(), y);
+       if (hasUp())
+               up().drawT(pain, x + dx1(), y - dy1());
+       if (hasDown())
+               down().drawT(pain, x + dx0(), y + dy0());
 }
 
 
-void MathScriptInset::metrics(MathMetricsInfo const & mi) const
-{      
-       metrics(0, mi);
-}
-
 
-void MathScriptInset::metrics(MathInset const * nuc,
-       MathMetricsInfo const & mi) const
-{      
-       MathNestInset::metrics(mi);
-       if (nuc)
-               nuc->metrics(mi);
+bool MathScriptInset::hasLimits() const
+{
+       // obvious cases
+       if (limits_ == 1)
+               return true;
+       if (limits_ == -1)
+               return false;
 
-       ascent_  = ascent(nuc);
-       descent_ = descent(nuc);
-       width_   = width(nuc);
-}
+       // we can only display limits if the nucleus wants some
+       if (!nuc().size())
+               return false;
+       if (!nuc().back()->isScriptable())
+               return false;
 
+       // per default \int has limits beside the \int even in displayed formulas
+       if (nuc().back()->asSymbolInset())
+               if (nuc().back()->asSymbolInset()->name().find("int") != string::npos)
+                       return false;
 
-void MathScriptInset::draw(Painter & pain, int x, int y) const
-{  
-       //lyxerr << "unexpected call to MathScriptInset::draw()\n";
-       draw(0, pain, x, y);
+       // assume "real" limits for everything else
+       return true;
 }
 
 
-void MathScriptInset::draw(MathInset const * nuc, Painter & pain,
-       int x, int y) const
-{  
-       if (nuc)
-               nuc->draw(pain, x + dxx(nuc), y);
-       else
-               drawStr(pain, LM_TC_TEX, mi_, x + dxx(nuc), y, ".");
+void MathScriptInset::removeEmptyScripts()
+{
+       for (int i = 0; i <= 1; ++i)
+               if (script_[i] && cell(i).size() == 0) {
+                       cell(i).clear();
+                       script_[i] = false;
+               }
+}
 
-       if (hasUp())
-               up().draw(pain, x + dx1(nuc), y - dy1(nuc));
 
-       if (hasDown())
-               down().draw(pain, x + dx0(nuc), y + dy0(nuc));
+void MathScriptInset::removeScript(bool up)
+{
+       cell(up).clear();
+       script_[up] = false;
 }
 
 
-void MathScriptInset::write(MathWriteInfo & os) const
-{  
-       //lyxerr << "unexpected call to MathScriptInset::write()\n";
-       write(0, os);
+bool MathScriptInset::has(bool up) const
+{
+       return script_[up];
 }
 
 
-void MathScriptInset::write(MathInset const * nuc, MathWriteInfo & os) const
+bool MathScriptInset::empty() const
 {
-       if (nuc) {
-               nuc->write(os);
-               if (nuc->takesLimits()) {
-                       if (limits_ == -1)
-                               os << "\\nolimits ";
-                       if (limits_ == 1)
-                               os << "\\limits ";
-               }
-       }
-       else
-               os << "{}";
+       return !script_[0] && !script_[1] && cell(2).empty();
+}
 
-       if (hasDown() && down().data_.size())
-               os << "_{" << down().data_ << '}';
 
-       if (hasUp() && up().data_.size())
-               os << "^{" << up().data_ << '}';
+bool MathScriptInset::hasUp() const
+{
+       return script_[1];
 }
 
 
-void MathScriptInset::writeNormal(ostream & os) const
-{  
-       //lyxerr << "unexpected call to MathScriptInset::writeNormal()\n";
-       writeNormal(0, os);
+bool MathScriptInset::hasDown() const
+{
+       return script_[0];
 }
 
 
-void MathScriptInset::writeNormal(MathInset const * nuc, ostream & os) const
+bool MathScriptInset::idxRight(idx_type &, pos_type &) const
 {
-       bool d = hasDown() && down().data_.size();
-       bool u = hasUp() && up().data_.size();
-
-       ostringstream osb;
-       if (nuc)
-               nuc->writeNormal(osb);
-       else
-               osb << "[par]";
-       string base = osb.str();
-
-       if (u && d) {
-               os << "[sup [sub " << osb.str() << " ";
-               down().data_.writeNormal(os);
-               os << "] ";
-               up().data_.writeNormal(os);
-               os << ']';
-       } else if (u) {
-               os << "[sup " << osb.str() << " ";
-               up().data_.writeNormal(os);
-               os << ']';
-       } else if (d) {
-               os << "[sub " << osb.str() << " ";
-               down().data_.writeNormal(os);
-               os << ']';
-       }
+       return false;
 }
 
 
-bool MathScriptInset::hasLimits(MathInset const * nuc) const
+bool MathScriptInset::idxLeft(idx_type &, pos_type &) const
 {
-       return limits_ == 1 || (limits_ == 0 && nuc && nuc->isScriptable());
+       return false;
 }
 
 
-void MathScriptInset::removeEmptyScripts()
+bool MathScriptInset::idxUpDown(idx_type & idx, pos_type & pos, bool up,
+       int) const
 {
-       for (int i = 0; i <= 1; ++i)
-               if (script_[i] && !cell(i).size())
-                       script_[i] = false;
+       if ((idx == 1 && up) || (idx == 0 && !up))
+               return false;
+
+       // in nuclues?
+       if (idx == 2) {
+               idx = up;
+               pos = 0;
+       } else {
+               idx = 2;
+               pos = cell(2).size();   
+       }
+       return true;
 }
 
 
-void MathScriptInset::removeScript(bool up)
+void MathScriptInset::write(WriteStream & os) const
 {
-       cell(up).clear();
-       script_[up] = false;
+       if (nuc().size()) {
+               os << nuc().data();
+               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().size())
+               os << "_{" << down().data() << '}';
+
+       if (hasUp() && up().size())
+               os << "^{" << up().data() << '}';
 }
 
 
-bool MathScriptInset::has(bool up) const
+void MathScriptInset::normalize(NormalStream & os) const
 {
-       return script_[up];
+       bool d = hasDown() && down().size();
+       bool u = hasUp() && up().size();
+
+       if (u && d)
+               os << "[subsup ";
+       else if (u)
+               os << "[sup ";
+       else if (d)
+               os << "[sub ";
+
+       if (nuc().size())
+               os << nuc().data() << ' ';
+       else
+               os << "[par]";
+
+       if (u && d)
+               os << down().data() << ' ' << up().data() << ']';
+       else if (d)
+               os << down().data() << ']';
+       else if (u)
+               os << up().data() << ']';
 }
 
 
-bool MathScriptInset::hasUp() const
+void MathScriptInset::maplize(MapleStream & os) const
 {
-       return script_[1];
+       if (nuc().size())
+               os << nuc().data();
+       if (hasDown() && down().size())
+               os << '[' << down().data() << ']';
+       if (hasUp() && up().size())
+               os << "^(" << up().data() << ')';
 }
 
 
-bool MathScriptInset::hasDown() const
+void MathScriptInset::mathematicize(MathematicaStream & os) const
 {
-       return script_[0];
+       bool d = hasDown() && down().size();
+       bool u = hasUp() && up().size();
+
+       if (nuc().size()) {
+               if (d) 
+                       os << "Subscript[" << nuc().data();
+               else
+                       os << nuc().data();
+       }
+
+       if (u)
+               os << "^(" << up().data() << ")";
+
+       if (nuc().size())
+               if (d)
+                       os << "," << down().data() << "]"; 
 }
 
 
-bool MathScriptInset::idxRight(MathInset::idx_type &,
-                                MathInset::pos_type &) const
+void MathScriptInset::mathmlize( MathMLStream & os) const
 {
-       return false;
+       bool d = hasDown() && down().size();
+       bool u = hasUp() && up().size();
+
+       if (u && d)
+               os << MTag("msubsup");
+       else if (u)
+               os << MTag("msup");
+       else if (d)
+               os << MTag("msub");
+
+       if (nuc().size())
+               os << nuc().data();
+       else
+               os << "<mrow/>";
+
+       if (u && d)
+               os << down().data() << up().data() << ETag("msubsup");
+       else if (u)
+               os << up().data() << ETag("msup");
+       else if (d)
+               os << down().data() << ETag("msub");
 }
 
 
-bool MathScriptInset::idxLeft(MathInset::idx_type &,
-                               MathInset::pos_type &) const
+void MathScriptInset::octavize(OctaveStream & os) const
 {
-       return false;
+       if (nuc().size())
+               os << nuc().data();
+       if (hasDown() && down().size())
+               os << '[' << down().data() << ']';
+       if (hasUp() && up().size())
+               os << "^(" << up().data() << ')';
 }
 
 
+void MathScriptInset::infoize(std::ostream & os) const
+{
+       os << "Scripts";
+       if (limits_)
+               os << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
+}