]> 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 30821404d13192e784b4ccf4851cedf605008927..790034aa1cf1abed796979d1e61e598add2c8c5a 100644 (file)
-#ifdef __GNUG__
-#pragma implementation
-#endif
+/**
+ * \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_inset.h"
-#include "math_charinset.h"
+#include "math_data.h"
+#include "math_fontinset.h"
 #include "math_scriptinset.h"
-#include "math_stringinset.h"
+#include "math_macro.h"
+#include "math_macrotable.h"
 #include "math_mathmlstream.h"
 #include "math_support.h"
-#include "math_data.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 "frontends/Painter.h"
+
+#include <boost/assert.hpp>
+
+using std::abs;
+using std::endl;
+using std::min;
+using std::ostringstream;
+using std::string;
+using std::vector;
 
 
 MathArray::MathArray()
 {}
 
 
-MathArray::MathArray(MathArray const & ar, size_type from, size_type to)
-       : bf_(ar.begin() + from, ar.begin() + to)
+MathArray::MathArray(const_iterator from, const_iterator to)
+       : 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);
+       BOOST_ASSERT(pos < size());
+       return base_type::operator[](pos);
 }
 
 
-MathScriptInset const * MathArray::asScript(const_iterator it) const
+MathAtom const & MathArray::operator[](pos_type pos) const
 {
-       if (it->nucleus()->asScriptInset())
-               return 0;
-       const_iterator jt = it + 1;
-       if (jt == end())
-               return 0;
-       if (!jt->nucleus())
-               return 0;
-       return jt->nucleus()->asScriptInset();
+       BOOST_ASSERT(pos < size());
+       return base_type::operator[](pos);
 }
 
 
-MathAtom & MathArray::at(size_type pos)
+void MathArray::insert(size_type pos, MathAtom const & t)
 {
-       lyx::Assert(pos < size());
-       return bf_[pos];
+       base_type::insert(begin() + pos, t);
 }
 
 
-MathAtom const & MathArray::at(size_type pos) const
+void MathArray::insert(size_type pos, MathArray const & ar)
 {
-       lyx::Assert(pos < size());
-       return bf_[pos];
+       BOOST_ASSERT(pos <= size());
+       base_type::insert(begin() + pos, ar.begin(), ar.end());
 }
 
 
-void MathArray::insert(size_type pos, MathAtom const & t)
+void MathArray::append(MathArray const & ar)
 {
-       bf_.insert(begin() + pos, t);
+       insert(size(), ar);
 }
 
 
-void MathArray::insert(size_type pos, MathArray const & ar)
+void MathArray::erase(size_type pos)
 {
-       bf_.insert(begin() + pos, ar.begin(), ar.end());
+       if (pos < size())
+               erase(pos, pos + 1);
 }
 
 
-void MathArray::push_back(MathAtom const & t)
-{      
-       bf_.push_back(t);
+void MathArray::erase(iterator pos1, iterator pos2)
+{
+       base_type::erase(pos1, pos2);
 }
 
 
-void MathArray::push_back(MathArray const & ar)
+void MathArray::erase(iterator pos)
 {
-       insert(size(), ar);
+       base_type::erase(pos);
 }
 
 
-void MathArray::clear()
+void MathArray::erase(size_type pos1, size_type pos2)
 {
-       erase();
+       base_type::erase(begin() + pos1, begin() + pos2);
 }
 
 
-void MathArray::swap(MathArray & ar)
+void MathArray::dump2() const
 {
-       if (this != &ar) 
-               bf_.swap(ar.bf_);
+       NormalStream ns(lyxerr);
+       for (const_iterator it = begin(); it != end(); ++it)
+               ns << *it << ' ';
 }
 
 
-bool MathArray::empty() const
+void MathArray::dump() const
 {
-       return bf_.empty();
+       NormalStream ns(lyxerr);
+       for (const_iterator it = begin(); it != end(); ++it)
+               ns << '<' << *it << '>';
 }
 
 
-MathArray::size_type MathArray::size() const
+void MathArray::validate(LaTeXFeatures & features) const
 {
-       return bf_.size();
+       for (const_iterator it = begin(); it != end(); ++it)
+               (*it)->validate(features);
 }
 
 
-void MathArray::erase()
+bool MathArray::match(MathArray const & ar) const
 {
-       erase(0, size());
+       return size() == ar.size() && matchpart(ar, 0);
 }
 
 
-void MathArray::erase(size_type pos)
+bool MathArray::matchpart(MathArray const & ar, pos_type pos) const
 {
-       if (pos < size())
-               erase(pos, pos + 1);
+       if (size() < ar.size() + pos)
+               return false;
+       const_iterator it = begin() + pos;
+       for (const_iterator jt = ar.begin(); jt != ar.end(); ++jt, ++it)
+               if (asString(*it) != asString(*jt))
+                       return false;
+       return true;
 }
 
 
-void MathArray::erase(size_type pos1, size_type pos2)
+void MathArray::replace(ReplaceData & rep)
 {
-       bf_.erase(begin() + pos1, begin() + pos2);
+       for (size_type i = 0; i < size(); ++i) {
+               if (find1(rep.from, i)) {
+                       // match found
+                       lyxerr << "match found!" << endl;
+                       erase(i, i + rep.from.size());
+                       insert(i, rep.to);
+               }
+       }
+
+#ifdef WITH_WARNINGS
+#warning temporarily disabled
+       // for (const_iterator it = begin(); it != end(); ++it)
+       //      it->nucleus()->replace(rep);
+#endif
 }
 
 
-MathAtom & MathArray::back()
+bool MathArray::find1(MathArray const & ar, size_type pos) const
 {
-       return bf_.back();
+       lyxerr << "finding '" << ar << "' in '" << *this << "'" << endl;
+       for (size_type i = 0, n = ar.size(); i < n; ++i)
+               if (asString(operator[](pos + i)) != asString(ar[i]))
+                       return false;
+       return true;
 }
 
 
-void MathArray::dump2() const
+MathArray::size_type MathArray::find(MathArray const & ar) const
 {
-       NormalStream ns(lyxerr);
-       for (const_iterator it = begin(); it != end(); ++it)
-               ns << it->nucleus() << ' ';
+       for (int i = 0, last = size() - ar.size(); i < last; ++i)
+               if (find1(ar, i))
+                       return i;
+       return size();
 }
 
 
-void MathArray::dump() const
+MathArray::size_type MathArray::find_last(MathArray const & ar) const
 {
-       NormalStream ns(lyxerr);
-       for (const_iterator it = begin(); it != end(); ++it)
-               ns << "<" << it->nucleus() << ">";
+       for (int i = size() - ar.size(); i >= 0; --i)
+               if (find1(ar, i))
+                       return i;
+       return size();
 }
 
 
-// returns sequence of char with same code starting at it up to end
-// it might be less, though...
-string charSequence(MathArray::const_iterator it, MathArray::const_iterator end)
+bool MathArray::contains(MathArray const & ar) const
 {
-       string s;
-       MathCharInset const * p = it->nucleus()->asCharInset();
-       if (!p)
-               return s;
-
-       for (MathTextCodes c = p->code(); it != end; ++it) {
-               if (!it->nucleus())
-                       break;
-               p = it->nucleus()->asCharInset();
-               if (!p || p->code() != c)
-                       break;
-               s += p->getChar();
-       }
-       return s;
+       if (find(ar) != size())
+               return true;
+       for (const_iterator it = begin(); it != end(); ++it)
+               if ((*it)->contains(ar))
+                       return true;
+       return false;
 }
 
 
-MathArray MathArray::glueChars() const
+void MathArray::touch() const
 {
-       MathArray ar;
-       const_iterator it = begin();
-       while (it != end()) {
-               if (it->nucleus() && it->nucleus()->asCharInset()) {
-                       string s = charSequence(it, end());
-                       MathTextCodes c = it->nucleus()->asCharInset()->code();
-                       ar.push_back(MathAtom(new MathStringInset(s, c)));
-                       it += s.size();
-               } else {
-                       ar.push_back(*it);
-                       ++it;
-               }
-       }
-       return ar;
 }
 
 
-bool needAsterisk(MathAtom const &, MathAtom const &)
+void MathArray::metrics(MetricsInfo & mi, Dimension & dim) const
 {
-       return false;
+       metrics(mi);
+       dim = dim_;
 }
 
 
-MathArray MathArray::guessAsterisks() const
+namespace {
+
+bool isInside(DocIterator const & it, MathArray const & ar,
+       lyx::pos_type p1, lyx::pos_type p2)
 {
-       if (size() <= 1)
-               return *this;
-       MathArray ar;
-       ar.push_back(*begin());
-       for (const_iterator it = begin(), jt = begin()+1 ; jt != end(); ++it, ++jt) {
-               if (needAsterisk(*it, *jt))
-                       ar.push_back(MathAtom(new MathCharInset('*')));
-               ar.push_back(*it);
+       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;
        }
-       ar.push_back(*end());
-       return ar;
+       return false;
+}
+
 }
 
 
-void MathArray::write(MathWriteInfo & wi) const
+
+void MathArray::metrics(MetricsInfo & mi) const
 {
-       MathArray ar = glueChars();
-       for (const_iterator it = ar.begin(); it != ar.end(); ++it) {
-               MathInset const * p = it->nucleus();
-               if (MathScriptInset const * q = ar.asScript(it)) {
-                       q->write(p, wi);
-                       ++it;
-               } else {
-                       p->write(wi);
+       mathed_char_dim(mi.base.font, 'I', dim_);
+
+       if (empty())
+               return;
+
+       dim_.wid = 0;
+       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;
        }
 }
 
 
-void MathArray::writeNormal(NormalStream & os) const
+void MathArray::draw(PainterInfo & pi, int x, int y) const
 {
-       MathArray ar = glueChars();
-       for (const_iterator it = ar.begin(); it != ar.end(); ++it) {
-               MathInset const * p = it->nucleus();
-               if (MathScriptInset const * q = ar.asScript(it)) {
-                       q->writeNormal(p, os);
-                       ++it;
-               } else 
-                       p->writeNormal(os);
+       //lyxerr << "MathArray::draw: x: " << x << " y: " << y << endl;
+       setXY(x, y);
+
+       if (empty()) {
+               pi.pain.rectangle(x, y - ascent(), width(), height(), LColor::mathline);
+               return;
        }
-}
 
+       // don't draw outside the workarea
+       if (y + descent() <= 0
+               || y - ascent() >= pi.pain.paperHeight()
+               || x + width() <= 0
+               || x >= pi.pain.paperWidth())
+               return;
 
-void MathArray::octavize(OctaveStream & os) const
-{
-       MathArray ar = glueChars();
-       for (const_iterator it = ar.begin(); it != ar.end(); ++it) {
-               MathInset const * p = it->nucleus();
-               if (MathScriptInset const * q = ar.asScript(it)) {
-                       q->octavize(p, os);
-                       ++it;
-               } else 
-                       p->octavize(os);
+       //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();
        }
 }
 
 
-void MathArray::maplize(MapleStream & os) const
+void MathArray::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
 {
-       MathArray ar = glueChars();
-       for (const_iterator it = ar.begin(); it != ar.end(); ++it) {
-               MathInset const * p = it->nucleus();
-               if (MathScriptInset const * q = ar.asScript(it)) {
-                       q->maplize(p, os);
-                       ++it;
-               } else 
-                       p->maplize(os);
+       dim.clear();
+       Dimension d;
+       for (const_iterator it = begin(); it != end(); ++it) {
+               (*it)->metricsT(mi, d);
+               dim += d;
        }
 }
 
 
-void MathArray::mathmlize(MathMLStream & os) const
+void MathArray::drawT(TextPainter & pain, int x, int y) const
 {
-       MathArray ar = glueChars();
-       if (ar.size() == 0)
-               os << "<mrow/>";
-       else if (ar.size() == 1)
-               os << ar.begin()->nucleus();
-       else {
-               os << MTag("mrow");
-               for (const_iterator it = ar.begin(); it != ar.end(); ++it) {
-                       MathInset const * p = it->nucleus();
-                       if (MathScriptInset const * q = ar.asScript(it)) {
-                               q->mathmlize(p, os);
-                               ++it;
-                       } else 
-                               p->mathmlize(os);
-               }
-               os << ETag("mrow");
+       //lyxerr << "x: " << x << " y: " << y << ' ' << pain.workAreaHeight() << endl;
+       setXY(x, y);
+
+       for (const_iterator it = begin(), et = end(); it != et; ++it) {
+               (*it)->drawT(pain, x, y);
+               //x += (*it)->width_;
+               x += 2;
        }
 }
 
 
-void MathArray::validate(LaTeXFeatures & features) const
+int MathArray::pos2x(size_type pos) const
 {
-       for (const_iterator it = begin(); it != end(); ++it)
-               if (it->nucleus())
-                       it->nucleus()->validate(features);
+       return pos2x(pos, 0);
 }
 
 
-void MathArray::pop_back()
-{      
-       if (!size()) {
-               lyxerr << "pop_back from empty array!\n";
-               return;
+int MathArray::pos2x(size_type pos, int glue) const
+{
+       int x = 0;
+       size_type target = min(pos, size());
+       for (size_type i = 0; i < target; ++i) {
+               const_iterator it = begin() + i;
+               if ((*it)->getChar() == ' ')
+                       x += glue;
+               //lyxerr << "char: " << (*it)->getChar()
+               //      << "width: " << (*it)->width() << std::endl;
+               x += (*it)->width();
        }
-       bf_.pop_back();
+       return x;
 }
 
 
-MathArray::const_iterator MathArray::begin() const
+MathArray::size_type MathArray::x2pos(int targetx) const
 {
-       return bf_.begin();
+       return x2pos(targetx, 0);
 }
 
 
-MathArray::const_iterator MathArray::end() const
+MathArray::size_type MathArray::x2pos(int targetx, int glue) const
 {
-       return bf_.end();
+       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 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();
 }
 
 
-MathArray::iterator MathArray::begin()
+int MathArray::dist(int x, int y) const
 {
-       return bf_.begin();
+       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())
+               xx = x - xo_ - width();
+
+       if (y < yo_ - ascent())
+               yy = yo_ - ascent() - y;
+       else if (y > yo_ + descent())
+               yy = y - yo_ - descent();
+
+       return xx + yy;
 }
 
 
-MathArray::iterator MathArray::end()
+void MathArray::setXY(int x, int y) const
 {
-       return bf_.end();
+       //lyxerr << "setting position cache for MathArray " << this << std::endl;
+       theCoords.arrays().add(this, x, y);
 }
 
 
-bool MathArray::isMatrix() const
+int MathArray::xo() const
 {
-       return size() == 1 && begin()->nucleus() && begin()->nucleus()->isMatrix();
+       return theCoords.getArrays().x(this);
 }
 
 
+int MathArray::yo() const
+{
+       return theCoords.getArrays().y(this);
+}