]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_scriptinset.C
bug + spped fixes + small stuff
[lyx.git] / src / mathed / math_scriptinset.C
index ebd2dab51e92ffc47e6681d4c0162f3d8ffd4100..6e6f75514109c6cfb71e642ab773f3c46b4f61aa 100644 (file)
@@ -1,17 +1,32 @@
-#ifdef __GNUG__
-#pragma implementation
-#endif
+/**
+ * \file math_scriptinset.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author André Pönitz
+ *
+ * Full author contact details are available in file CREDITS.
+ */
+
+#include <config.h>
 
 #include "math_scriptinset.h"
+#include "math_data.h"
+#include "math_mathmlstream.h"
 #include "math_support.h"
 #include "math_symbolinset.h"
-#include "math_mathmlstream.h"
-#include "funcrequest.h"
-#include "support/LAssert.h"
+#include "dispatchresult.h"
+#include "cursor.h"
 #include "debug.h"
+#include "funcrequest.h"
 
+#include <boost/assert.hpp>
 
+using std::string;
 using std::max;
+using std::auto_ptr;
+using std::endl;
+
 
 
 MathScriptInset::MathScriptInset()
@@ -40,9 +55,9 @@ MathScriptInset::MathScriptInset(MathAtom const & at, bool up)
 
 
 
-MathInset * MathScriptInset::clone() const
+auto_ptr<InsetBase> MathScriptInset::clone() const
 {
-       return new MathScriptInset(*this);
+       return auto_ptr<InsetBase>(new MathScriptInset(*this));
 }
 
 
@@ -58,18 +73,18 @@ MathScriptInset * MathScriptInset::asScriptInset()
 }
 
 
-bool MathScriptInset::idxFirst(idx_type & idx, pos_type & pos) const
+bool MathScriptInset::idxFirst(LCursor & cur) const
 {
-       idx = 2;
-       pos = 0;
+       cur.idx() = 2;
+       cur.pos() = 0;
        return true;
 }
 
 
-bool MathScriptInset::idxLast(idx_type & idx, pos_type & pos) const
+bool MathScriptInset::idxLast(LCursor & cur) const
 {
-       idx = 2;
-       pos = nuc().size();
+       cur.idx() = 2;
+       cur.pos() = nuc().size();
        return true;
 }
 
@@ -147,21 +162,21 @@ int MathScriptInset::dy1() const
 
 int MathScriptInset::dx0() const
 {
-       lyx::Assert(hasDown());
-       return hasLimits() ? (dim_.w - down().width()) / 2 : nwid();
+       BOOST_ASSERT(hasDown());
+       return hasLimits() ? (dim_.wid - down().width()) / 2 : nwid();
 }
 
 
 int MathScriptInset::dx1() const
 {
-       lyx::Assert(hasUp());
-       return hasLimits() ? (dim_.w - up().width()) / 2 : nwid();
+       BOOST_ASSERT(hasUp());
+       return hasLimits() ? (dim_.wid - up().width()) / 2 : nwid();
 }
 
 
 int MathScriptInset::dxx() const
 {
-       return hasLimits() ? (dim_.w - nwid()) / 2  :  0;
+       return hasLimits() ? (dim_.wid - nwid()) / 2  :  0;
 }
 
 
@@ -183,57 +198,58 @@ int MathScriptInset::ndes() const
 }
 
 
-void MathScriptInset::metrics(MathMetricsInfo & mi) const
+void MathScriptInset::metrics(MetricsInfo & mi, Dimension & dim) const
 {
        cell(2).metrics(mi);
-       MathScriptChanger dummy(mi.base);
+       ScriptChanger dummy(mi.base);
        cell(0).metrics(mi);
        cell(1).metrics(mi);
-       dim_.w = 0;
+       dim.wid = 0;
        if (hasLimits()) {
-               dim_.w = nwid();
+               dim.wid = nwid();
                if (hasUp())
-                       dim_.w = max(dim_.w, up().width());
+                       dim.wid = max(dim.wid, up().width());
                if (hasDown())
-                       dim_.w = max(dim_.w, down().width());
+                       dim.wid = max(dim.wid, down().width());
        } else {
                if (hasUp())
-                       dim_.w = max(dim_.w, up().width());
+                       dim.wid = max(dim.wid, up().width());
                if (hasDown())
-                       dim_.w = max(dim_.w, down().width());
-               dim_.w += nwid();
+                       dim.wid = max(dim.wid, down().width());
+               dim.wid += nwid();
        }
-       dim_.a = dy1() + (hasUp() ? up().ascent() : 0);
-       dim_.d = dy0() + (hasDown() ? down().descent() : 0);
-       metricsMarkers2();
+       dim.asc = dy1() + (hasUp() ? up().ascent() : 0);
+       dim.des = dy0() + (hasDown() ? down().descent() : 0);
+       metricsMarkers(dim);
+       dim_ = dim;
 }
 
 
-void MathScriptInset::draw(MathPainterInfo & pi, int x, int y) const
+void MathScriptInset::draw(PainterInfo & pi, int x, int y) const
 {
        if (nuc().size())
                nuc().draw(pi, x + dxx(), y);
        else {
                nuc().setXY(x + dxx(), y);
-               if (editing())
+               if (editing(pi.base.bv))
                        drawStr(pi, pi.base.font, x + dxx(), y, ".");
        }
-       MathScriptChanger dummy(pi.base);
+       ScriptChanger 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);
+       drawMarkers(pi, x, y);
 }
 
 
-void MathScriptInset::metricsT(TextMetricsInfo const & mi) const
+void MathScriptInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
 {
        if (hasUp())
-               up().metricsT(mi);
+               up().metricsT(mi, dim);
        if (hasDown())
-               down().metricsT(mi);
-       nuc().metricsT(mi);
+               down().metricsT(mi, dim);
+       nuc().metricsT(mi, dim);
 }
 
 
@@ -273,16 +289,6 @@ bool MathScriptInset::hasLimits() 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();
@@ -308,49 +314,50 @@ bool MathScriptInset::hasDown() const
 }
 
 
-bool MathScriptInset::idxRight(idx_type &, pos_type &) const
+bool MathScriptInset::idxRight(LCursor &) const
 {
        return false;
 }
 
 
-bool MathScriptInset::idxLeft(idx_type &, pos_type &) const
+bool MathScriptInset::idxLeft(LCursor &) const
 {
        return false;
 }
 
 
-bool MathScriptInset::idxUpDown(idx_type & idx, pos_type & pos, bool up,
-       int) const
+bool MathScriptInset::idxUpDown(LCursor & cur, bool up) const
 {
-       if (idx == 1) {
+       if (cur.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();   
+               cur.idx() = 2;
+               cur.pos() = cur.lastpos();
        }
 
-       else if (idx == 0) {
+       else if (cur.idx() == 0) {
                // if we are 'down' we can't go further down
                if (!up)
                        return false;
-               idx = 2;
-               pos = cell(2).size();   
+               cur.idx() = 2;
+               cur.pos() = cur.lastpos();
        }
-       
+
        else {
                // in nucleus
-               // don't go up/down unless in last position
-               if (pos != cell(2).size())
-                       return false;   
                // don't go up/down if there is no cell.
                if (!has(up))
                        return false;
-               // otherwise move into the first position
-               idx = up;
-               pos = 0;
+               // go up/down only if in the last position
+               // or in the first position of something with displayed limits
+               if (cur.pos() == cur.lastpos() || (cur.pos() == 0 && hasLimits())) {
+                       cur.idx() = up;
+                       cur.pos() = 0;
+                       return true;
+               }
+               return false;
        }
        return true;
 }
@@ -368,7 +375,8 @@ void MathScriptInset::write(WriteStream & os) const
                //}
        } else {
                if (os.firstitem())
-                       lyxerr[Debug::MATHED] << "suppressing {} when writing\n";
+                       lyxerr[Debug::MATHED] << "suppressing {} when writing"
+                                             << endl;
                else
                        os << "{}";
        }
@@ -410,7 +418,7 @@ void MathScriptInset::normalize(NormalStream & os) const
 }
 
 
-void MathScriptInset::maplize(MapleStream & os) const
+void MathScriptInset::maple(MapleStream & os) const
 {
        if (nuc().size())
                os << nuc();
@@ -421,28 +429,29 @@ void MathScriptInset::maplize(MapleStream & os) const
 }
 
 
-void MathScriptInset::mathematicize(MathematicaStream & os) const
+void MathScriptInset::mathematica(MathematicaStream & os) const
 {
        bool d = hasDown() && down().size();
        bool u = hasUp() && up().size();
 
        if (nuc().size()) {
-               if (d) 
+               if (d)
                        os << "Subscript[" << nuc();
                else
                        os << nuc();
        }
 
        if (u)
-               os << "^(" << up() << ")";
+               os << "^(" << up() << ')';
 
-       if (nuc().size())
+       if (nuc().size()) {
                if (d)
-                       os << "," << down() << "]"; 
+                       os << ',' << down() << ']';
+       }
 }
 
 
-void MathScriptInset::mathmlize( MathMLStream & os) const
+void MathScriptInset::mathmlize(MathMLStream & os) const
 {
        bool d = hasDown() && down().size();
        bool u = hasUp() && up().size();
@@ -468,7 +477,7 @@ void MathScriptInset::mathmlize( MathMLStream & os) const
 }
 
 
-void MathScriptInset::octavize(OctaveStream & os) const
+void MathScriptInset::octave(OctaveStream & os) const
 {
        if (nuc().size())
                os << nuc();
@@ -482,14 +491,33 @@ void MathScriptInset::octavize(OctaveStream & os) const
 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");
 }
 
 
-MathInset::result_type MathScriptInset::dispatch
-       (FuncRequest const & cmd, idx_type & idx, pos_type & pos)
+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;
+       }
+}
+
+
+DispatchResult
+MathScriptInset::priv_dispatch(LCursor & cur, FuncRequest const & cmd)
+{
+       lyxerr << "MathScriptInset: request: " << cmd << std::endl;
+
        if (cmd.action == LFUN_MATH_LIMITS) {
                if (!cmd.argument.empty()) {
                        if (cmd.argument == "limits")
@@ -499,11 +527,11 @@ MathInset::result_type MathScriptInset::dispatch
                        else
                                limits_ = 0;
                } else if (limits_ == 0)
-                       limits_ =  (hasLimits()) ? -1 : 1;
+                       limits_ = hasLimits() ? -1 : 1;
                else
                        limits_ = 0;
-               return DISPATCHED;
+               return DispatchResult(true, true);
        }
 
-       return MathNestInset::dispatch(cmd, idx, pos);
+       return MathNestInset::priv_dispatch(cur, cmd);
 }