]> 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 119313692e25b73140631d755e245b60fd9505cf..790034aa1cf1abed796979d1e61e598add2c8c5a 100644 (file)
@@ -1,55 +1,62 @@
-#include <config.h>
+/**
+ * \file math_data.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_data.h"
-#include "math_inset.h"
-#include "math_cursor.h"
-#include "math_deliminset.h"
 #include "math_fontinset.h"
 #include "math_scriptinset.h"
+#include "math_macro.h"
+#include "math_macrotable.h"
 #include "math_mathmlstream.h"
 #include "math_support.h"
 #include "math_replace.h"
+
+#include "coordcache.h"
+#include "LColor.h"
+#include "BufferView.h"
+#include "buffer.h"
+#include "cursor.h"
 #include "debug.h"
-#include "support/LAssert.h"
-#include "metricsinfo.h"
+
 #include "frontends/Painter.h"
-#include "textpainter.h"
 
+#include <boost/assert.hpp>
 
-using std::max;
-using std::min;
 using std::abs;
-
+using std::endl;
+using std::min;
+using std::ostringstream;
+using std::string;
+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)
 {}
 
 
-void MathArray::substitute(MathMacro const & m)
+MathAtom & MathArray::operator[](pos_type pos)
 {
-       for (iterator it = begin(); it != end(); ++it)
-               it->nucleus()->substitute(m);
-}
-
-
-MathAtom & MathArray::operator[](size_type pos)
-{
-       lyx::Assert(pos < size());
+       BOOST_ASSERT(pos < size());
        return base_type::operator[](pos);
 }
 
 
-MathAtom const & MathArray::operator[](size_type pos) const
+MathAtom const & MathArray::operator[](pos_type pos) const
 {
-       lyx::Assert(pos < size());
+       BOOST_ASSERT(pos < size());
        return base_type::operator[](pos);
 }
 
@@ -62,7 +69,7 @@ void MathArray::insert(size_type pos, MathAtom const & t)
 
 void MathArray::insert(size_type pos, MathArray const & ar)
 {
-       lyx::Assert(pos <= size());
+       BOOST_ASSERT(pos <= size());
        base_type::insert(begin() + pos, ar.begin(), ar.end());
 }
 
@@ -133,7 +140,7 @@ bool MathArray::matchpart(MathArray const & ar, pos_type pos) const
                return false;
        const_iterator it = begin() + pos;
        for (const_iterator jt = ar.begin(); jt != ar.end(); ++jt, ++it)
-               if (!(*jt)->match(*it))
+               if (asString(*it) != asString(*jt))
                        return false;
        return true;
 }
@@ -144,7 +151,7 @@ void MathArray::replace(ReplaceData & rep)
        for (size_type i = 0; i < size(); ++i) {
                if (find1(rep.from, i)) {
                        // match found
-                       lyxerr << "match found!\n";
+                       lyxerr << "match found!" << endl;
                        erase(i, i + rep.from.size());
                        insert(i, rep.to);
                }
@@ -160,9 +167,9 @@ void MathArray::replace(ReplaceData & rep)
 
 bool MathArray::find1(MathArray const & ar, size_type pos) const
 {
-       //lyxerr << "finding '" << ar << "' in '" << *this << "'\n";
+       lyxerr << "finding '" << ar << "' in '" << *this << "'" << endl;
        for (size_type i = 0, n = ar.size(); i < n; ++i)
-               if (!operator[](pos + i)->match(ar[i]))
+               if (asString(operator[](pos + i)) != asString(ar[i]))
                        return false;
        return true;
 }
@@ -199,87 +206,138 @@ bool MathArray::contains(MathArray const & ar) const
 
 void MathArray::touch() const
 {
-       clean_  = false;
-       drawn_  = false;
 }
 
 
-Dimension const & MathArray::metrics(MetricsInfo & mi) const
+void MathArray::metrics(MetricsInfo & mi, Dimension & dim) const
 {
-       //if (clean_)
-       //      return;
-       clean_  = true;
-       drawn_  = false;
+       metrics(mi);
+       dim = dim_;
+}
+
 
+namespace {
+
+bool isInside(DocIterator const & it, MathArray const & ar,
+       lyx::pos_type p1, lyx::pos_type p2)
+{
+       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;
+       }
+       return false;
+}
+
+}
+
+
+
+void MathArray::metrics(MetricsInfo & mi) const
+{
        mathed_char_dim(mi.base.font, 'I', dim_);
+
        if (empty())
-               return dim_;
+               return;
 
        dim_.wid = 0;
-       for (const_iterator it = begin(), et = end(); it != et; ++it) {
-               (*it)->metrics(mi);
-               dim_ += (*it)->dimensions();
+       Dimension d;
+       //BufferView & bv  = *mi.base.bv;
+       //Buffer const & buf = *bv.buffer();
+       for (size_t i = 0, n = size(); i != n; ++i) {
+               MathAtom const & at = operator[](i);
+#if 0
+               MathMacro const * mac = at->asMacro();
+               if (mac && buf.hasMacro(mac->name())) {
+                       MacroData const & tmpl = buf.getMacro(mac->name());
+                       int numargs = tmpl.numargs();
+                       if (i + numargs > n)
+                               numargs = n - i - 1;
+                       lyxerr << "metrics:found macro: " << mac->name()
+                               << " numargs: " << numargs << endl;
+                       if (!isInside(bv.cursor(), *this, i + 1, i + numargs + 1)) {
+                               MathArray args(begin() + i + 1, begin() + i + numargs + 1);
+                               MathArray exp;
+                               tmpl.expand(args, exp);
+                               mac->setExpansion(exp, args);
+                               mac->metricsExpanded(mi, d);
+                               dim_.wid += mac->widthExpanded();
+                               i += numargs;
+                               continue;
+                       }
+               }
+#endif
+               at->metrics(mi, d);
+               dim_ += d;
        }
-       return dim_;
 }
 
 
 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;
-
-       if (y + descent() <= 0)                   // don't draw above the workarea
-               return;
-       if (y - ascent() >= pi.pain.paperHeight())   // don't draw below the workarea
-               return;
-       if (x + width() <= 0)                     // don't draw left of workarea
-               return;
-       if (x >= pi.pain.paperWidth())              // don't draw right of workarea
-               return;
+       setXY(x, y);
 
        if (empty()) {
                pi.pain.rectangle(x, y - ascent(), width(), height(), LColor::mathline);
                return;
        }
 
-       for (const_iterator it = begin(), et = end(); it != et; ++it) {
-               (*it)->draw(pi, x, y);
-               x += (*it)->width();
+       // don't draw outside the workarea
+       if (y + descent() <= 0
+               || y - ascent() >= pi.pain.paperHeight()
+               || x + width() <= 0
+               || x >= pi.pain.paperWidth())
+               return;
+
+       //BufferView & bv  = *pi.base.bv;
+       for (size_t i = 0, n = size(); i != n; ++i) {
+               MathAtom const & at = operator[](i);
+#if 0
+       Buffer const & buf = *bv.buffer();
+               // special macro handling
+               MathMacro const * mac = at->asMacro();
+               if (mac && buf.hasMacro(mac->name())) {
+                       MacroData const & tmpl = buf.getMacro(mac->name());
+                       int numargs = tmpl.numargs();
+                       if (i + numargs > n)
+                               numargs = n - i - 1;
+                       if (!isInside(bv.cursor(), *this, i + 1, i + numargs + 1)) {
+                               mac->drawExpanded(pi, x, y);
+                               x += mac->widthExpanded();
+                               i += numargs;
+                               continue;
+                       }
+               }
+#endif
+               theCoords.insets().add(at.nucleus(), x, y);
+               at->drawSelection(pi, x, y);
+               at->draw(pi, x, y);
+               x += at->width();
        }
 }
 
 
-Dimension const & MathArray::metricsT(TextMetricsInfo const & mi) const
+void MathArray::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
 {
-       //if (clean_)
-       //      return;
-       dim_.clear();
+       dim.clear();
+       Dimension d;
        for (const_iterator it = begin(); it != end(); ++it) {
-               (*it)->metricsT(mi);
-               dim_ += (*it)->dimensions();
+               (*it)->metricsT(mi, d);
+               dim += d;
        }
-       return dim_;
 }
 
 
 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;
        }
 }
 
@@ -298,6 +356,8 @@ int MathArray::pos2x(size_type pos, int glue) const
                const_iterator it = begin() + i;
                if ((*it)->getChar() == ' ')
                        x += glue;
+               //lyxerr << "char: " << (*it)->getChar()
+               //      << "width: " << (*it)->width() << std::endl;
                x += (*it)->width();
        }
        return x;
@@ -315,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();
 }
 
@@ -332,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())
@@ -346,67 +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();
-}
-
-
-void MathArray::center(int & x, int & y) const
-{
-       x = xo_ + width() / 2;
-       y = yo_ + (descent() - ascent()) / 2;
-}
-
-
-void MathArray::towards(int & x, int & y) const
+void MathArray::setXY(int x, int y) 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));
+       //lyxerr << "setting position cache for MathArray " << this << std::endl;
+       theCoords.arrays().add(this, x, y);
 }
 
 
-void MathArray::setXY(int x, int y) const
+int MathArray::xo() const
 {
-       xo_ = x;
-       yo_ = y;
+       return theCoords.getArrays().x(this);
 }
 
 
-void MathArray::notifyCursorLeaves()
+int MathArray::yo() const
 {
-       // do not recurse!
-
-       // remove base-only "scripts"
-       for (pos_type i = 0; i + 1 < size(); ++i) {
-               MathScriptInset * p = operator[](i).nucleus()->asScriptInset();
-               if (p && p->cell(0).empty() && p->cell(1).empty()) {
-                       MathArray ar = p->nuc();
-                       erase(i);
-                       insert(i, ar);
-                       mathcursor->adjust(i, ar.size() - 1);
-               }
-       }
-
-       // glue adjacent font insets of the same kind
-       for (pos_type i = 0; i + 1 < size(); ++i) {
-               MathFontInset * p = operator[](i).nucleus()->asFontInset();
-               MathFontInset const * q = operator[](i + 1)->asFontInset();
-               if (p && q && p->name() == q->name()) {
-                       p->cell(0).append(q->cell(0));
-                       erase(i + 1);
-                       mathcursor->adjust(i, -1);
-               }
-       }
-
+       return theCoords.getArrays().y(this);
 }