]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_data.C
Fix to bug 2362: Deleting superscript also deletes subscript.
[lyx.git] / src / mathed / math_data.C
index b9e3e43d5aa8a68b4a4e17a4e931bd0135b98b57..790034aa1cf1abed796979d1e61e598add2c8c5a 100644 (file)
@@ -19,6 +19,7 @@
 #include "math_support.h"
 #include "math_replace.h"
 
+#include "coordcache.h"
 #include "LColor.h"
 #include "BufferView.h"
 #include "buffer.h"
@@ -38,12 +39,11 @@ using std::vector;
 
 
 MathArray::MathArray()
-       : xo_(0), yo_(0), clean_(false), drawn_(false)
 {}
 
 
 MathArray::MathArray(const_iterator from, const_iterator to)
-       : base_type(from, to), xo_(0), yo_(0), clean_(false), drawn_(false)
+       : base_type(from, to)
 {}
 
 
@@ -206,8 +206,6 @@ bool MathArray::contains(MathArray const & ar) const
 
 void MathArray::touch() const
 {
-       clean_  = false;
-       drawn_  = false;
 }
 
 
@@ -220,10 +218,10 @@ void MathArray::metrics(MetricsInfo & mi, Dimension & dim) const
 
 namespace {
 
-bool isInside(DocIterator const & it, MathArray const & ar, 
+bool isInside(DocIterator const & it, MathArray const & ar,
        lyx::pos_type p1, lyx::pos_type p2)
 {
-       for (size_t i = 0; i != it.size(); ++i) {
+       for (size_t i = 0; i != it.depth(); ++i) {
                CursorSlice const & sl = it[i];
                if (sl.inset().inMathed() && &sl.cell() == &ar)
                        return p1 <= sl.pos() && sl.pos() < p2;
@@ -237,11 +235,6 @@ bool isInside(DocIterator const & it, MathArray const & ar,
 
 void MathArray::metrics(MetricsInfo & mi) const
 {
-       //if (clean_)
-       //      return;
-       clean_  = true;
-       drawn_  = false;
-
        mathed_char_dim(mi.base.font, 'I', dim_);
 
        if (empty())
@@ -282,13 +275,8 @@ void MathArray::metrics(MetricsInfo & mi) const
 
 void MathArray::draw(PainterInfo & pi, int x, int y) const
 {
-       //if (drawn_ && x == xo_ && y == yo_)
-       //      return;
        //lyxerr << "MathArray::draw: x: " << x << " y: " << y << endl;
-
-       xo_    = x;
-       yo_    = y;
-       drawn_ = true;
+       setXY(x, y);
 
        if (empty()) {
                pi.pain.rectangle(x, y - ascent(), width(), height(), LColor::mathline);
@@ -322,6 +310,7 @@ void MathArray::draw(PainterInfo & pi, int x, int y) const
                        }
                }
 #endif
+               theCoords.insets().add(at.nucleus(), x, y);
                at->drawSelection(pi, x, y);
                at->draw(pi, x, y);
                x += at->width();
@@ -331,8 +320,6 @@ void MathArray::draw(PainterInfo & pi, int x, int y) const
 
 void MathArray::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
 {
-       //if (clean_)
-       //      return;
        dim.clear();
        Dimension d;
        for (const_iterator it = begin(); it != end(); ++it) {
@@ -344,16 +331,12 @@ void MathArray::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
 
 void MathArray::drawT(TextPainter & pain, int x, int y) const
 {
-       //if (drawn_ && x == xo_ && y == yo_)
-       //      return;
        //lyxerr << "x: " << x << " y: " << y << ' ' << pain.workAreaHeight() << endl;
-       xo_    = x;
-       yo_    = y;
-       drawn_ = true;
+       setXY(x, y);
 
        for (const_iterator it = begin(), et = end(); it != et; ++it) {
                (*it)->drawT(pain, x, y);
-               //x += (*it->width_;
+               //x += (*it)->width_;
                x += 2;
        }
 }
@@ -392,14 +375,30 @@ MathArray::size_type MathArray::x2pos(int targetx, int glue) const
        const_iterator it = begin();
        int lastx = 0;
        int currx = 0;
+       // find first position after targetx
        for (; currx < targetx && it < end(); ++it) {
                lastx = currx;
                if ((*it)->getChar() == ' ')
                        currx += glue;
                currx += (*it)->width();
        }
-       if (abs(lastx - targetx) < abs(currx - targetx) && it != begin())
+
+       /** 
+        * If we are not at the beginning of the array, go to the left
+        * of the inset if one of the following two condition holds:
+        * - the current inset is editable (so that the cursor tip is
+        *   deeper than us): in this case, we want all intermediate
+        *   cursor slices to be before insets;
+        * - the mouse is closer to the left side of the inset than to
+        *   the right one.
+        * See bug 1918 for details.    
+        **/
+       if (it != begin()
+           && ((*boost::prior(it))->asNestInset()
+               || abs(lastx - targetx) < abs(currx - targetx))) {
                --it;
+       }
+
        return it - begin();
 }
 
@@ -409,6 +408,9 @@ int MathArray::dist(int x, int y) const
        int xx = 0;
        int yy = 0;
 
+       const int xo_ = xo();
+       const int yo_ = yo();
+
        if (x < xo_)
                xx = xo_ - x;
        else if (x > xo_ + width())
@@ -423,45 +425,20 @@ int MathArray::dist(int x, int y) const
 }
 
 
-void MathArray::boundingBox(int & x1, int & x2, int & y1, int & y2)
-{
-       x1 = xo_;
-       x2 = xo_ + width();
-       y1 = yo_ - ascent();
-       y2 = yo_ + descent();
-}
-
-
-bool MathArray::contains(int x, int y) const
-{
-       return xo_ <= x && x <= xo_ + width()
-              && yo_ - ascent() <= y && y <= yo_ + descent();
-}
-
-
-void MathArray::center(int & x, int & y) const
+void MathArray::setXY(int x, int y) const
 {
-       x = xo_ + width() / 2;
-       y = yo_ + (descent() - ascent()) / 2;
+       //lyxerr << "setting position cache for MathArray " << this << std::endl;
+       theCoords.arrays().add(this, x, y);
 }
 
 
-void MathArray::towards(int & x, int & y) const
+int MathArray::xo() const
 {
-       int cx = 0;
-       int cy = 0;
-       center(cx, cy);
-
-       double r = 1.0;
-       //int dist = (x - cx) * (x - cx) + (y - cy) * (y - cy);
-
-       x = cx + int(r * (x - cx));
-       y = cy + int(r * (y - cy));
+       return theCoords.getArrays().x(this);
 }
 
 
-void MathArray::setXY(int x, int y) const
+int MathArray::yo() const
 {
-       xo_ = x;
-       yo_ = y;
+       return theCoords.getArrays().y(this);
 }