]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/MathData.cpp
simplify GuiToc / TocWidget interaction. Much can still be simplified...
[lyx.git] / src / mathed / MathData.cpp
index e579ece41ea812c856266a3eeb4d382c8a4e5546..ec5c443f317dec79efd7f2cc0e9f3b84457f32c1 100644 (file)
@@ -37,6 +37,8 @@
 #include <boost/assert.hpp>
 #include <boost/next_prior.hpp>
 
+#include <cstdlib>
+
 using namespace std;
 
 namespace lyx {
@@ -253,11 +255,11 @@ void MathData::metrics(MetricsInfo & mi, Dimension & dim) const
        dim.asc = 0;
        dim.wid = 0;
        Dimension d;
-       atom_dims_.clear();
+       CoordCacheBase<Inset> & coords = mi.base.bv->coordCache().insets();
        for (size_t i = 0, n = size(); i != n; ++i) {
                MathAtom const & at = operator[](i);
                at->metrics(mi, d);
-               atom_dims_.push_back(d);
+               coords.add(at.nucleus(), d);
                dim += d;
                if (i == n - 1)
                        kerning_ = at->kerning(mi.base.bv);
@@ -287,12 +289,13 @@ void MathData::draw(PainterInfo & pi, int x, int y) const
                || x >= bv. workWidth())
                return;
 
+       CoordCacheBase<Inset> & coords = pi.base.bv->coordCache().insets();
        for (size_t i = 0, n = size(); i != n; ++i) {
                MathAtom const & at = operator[](i);
-               bv.coordCache().insets().add(at.nucleus(), x, y);
+               coords.add(at.nucleus(), x, y);
                at->drawSelection(pi, x, y);
                at->draw(pi, x, y);
-               x += atom_dims_[i].wid;
+               x += coords.dim(at.nucleus()).wid;
        }
 }
 
@@ -560,7 +563,7 @@ void MathData::attachMacroParameters(Cursor * cur,
        // find arguments behind the macro
        if (!interactiveInit) {
                collectOptionalParameters(cur, macroOptionals, detachedArgs, p,
-                       macroPos, thisPos, thisSlice);
+                       scriptToPutAround, macroPos, thisPos, thisSlice);
                collectParameters(cur, macroNumArgs, detachedArgs, p,
                        scriptToPutAround, macroPos, thisPos, thisSlice);
        }
@@ -613,10 +616,13 @@ void MathData::attachMacroParameters(Cursor * cur,
 
 void MathData::collectOptionalParameters(Cursor * cur, 
        const size_type numOptionalParams, vector<MathData> & params, 
-       size_t & pos, const pos_type macroPos, const int thisPos, const int thisSlice)
+       size_t & pos, MathAtom & scriptToPutAround,
+       const pos_type macroPos, const int thisPos, const int thisSlice)
 {
        // insert optional arguments?
-       while (params.size() < numOptionalParams && pos < size()) {
+       while (params.size() < numOptionalParams 
+              && pos < size()
+              && !scriptToPutAround.nucleus()) {
                // is a [] block following which could be an optional parameter?
                if (operator[](pos)->getChar() != '[')
                        break;
@@ -624,9 +630,23 @@ void MathData::collectOptionalParameters(Cursor * cur,
                // found possible optional argument, look for "]"
                size_t right = pos + 1;
                for (; right < size(); ++right) {
-                       if (operator[](right)->getChar() == ']')
+                       MathAtom & cell = operator[](right);
+
+                       if (cell->getChar() == ']')
                                // found right end
                                break;
+                       
+                       // maybe "]" with a script around?
+                       InsetMathScript * script = cell.nucleus()->asScriptInset();
+                       if (!script)
+                               continue;
+                       if (script->nuc().size() != 1)
+                               continue;
+                       if (script->nuc()[0]->getChar() == ']') {
+                               // script will be put around the macro later
+                               scriptToPutAround = cell;
+                               break;
+                       }
                }
                
                // found?
@@ -675,7 +695,9 @@ void MathData::collectParameters(Cursor * cur,
        const pos_type macroPos, const int thisPos, const int thisSlice) 
 {
        // insert normal arguments
-       for (; params.size() < numParams && pos < size();) {
+       while (params.size() < numParams
+              && pos < size()
+              && !scriptToPutAround.nucleus()) {
                MathAtom & cell = operator[](pos);
                
                // fix cursor
@@ -733,45 +755,47 @@ void MathData::collectParameters(Cursor * cur,
 }
 
 
-int MathData::pos2x(size_type pos) const
+int MathData::pos2x(BufferView const * bv, size_type pos) const
 {
-       return pos2x(pos, 0);
+       return pos2x(bv, pos, 0);
 }
 
 
-int MathData::pos2x(size_type pos, int glue) const
+int MathData::pos2x(BufferView const * bv, size_type pos, int glue) const
 {
        int x = 0;
        size_type target = min(pos, size());
+       CoordCacheBase<Inset> const & coords = bv->coordCache().getInsets();
        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() << endl;
-               x += atom_dims_[i].wid;
+               x += coords.dim((*it).nucleus()).wid;
        }
        return x;
 }
 
 
-MathData::size_type MathData::x2pos(int targetx) const
+MathData::size_type MathData::x2pos(BufferView const * bv, int targetx) const
 {
-       return x2pos(targetx, 0);
+       return x2pos(bv, targetx, 0);
 }
 
 
-MathData::size_type MathData::x2pos(int targetx, int glue) const
+MathData::size_type MathData::x2pos(BufferView const * bv, int targetx, int glue) const
 {
        const_iterator it = begin();
        int lastx = 0;
        int currx = 0;
+       CoordCacheBase<Inset> const & coords = bv->coordCache().getInsets();
        // find first position after targetx
        for (; currx < targetx && it < end(); ++it) {
                lastx = currx;
                if ((*it)->getChar() == ' ')
                        currx += glue;
-               currx += atom_dims_[it - begin()].wid;
+               currx += coords.dim((*it).nucleus()).wid;
        }
 
        /**