]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/InsetMathGrid.cpp
Make sure inset buffer is correctly set in math grid
[lyx.git] / src / mathed / InsetMathGrid.cpp
index 6c237f6cdf582d8783bea3f2891d13fb7beaf1e5..c696c2260b9a9b5c4bb389ccc176c6d276df8dac 100644 (file)
@@ -22,6 +22,7 @@
 #include "Buffer.h"
 #include "BufferParams.h"
 #include "BufferView.h"
+#include "CoordCache.h"
 #include "Cursor.h"
 #include "CutAndPaste.h"
 #include "FuncRequest.h"
@@ -397,7 +398,8 @@ void InsetMathGrid::metrics(MetricsInfo & mi, Dimension & dim) const
        for (idx_type i = 0; i < nargs(); ++i) {
                if (cellinfo_[i].multi_ != CELL_PART_OF_MULTICOLUMN) {
                        Dimension dimc;
-                       cell(i).metrics(mi, dimc);
+                       // the 'false' is to make sure that the cell is tall enough
+                       cell(i).metrics(mi, dimc, false);
                }
        }
 
@@ -807,7 +809,7 @@ void InsetMathGrid::addRow(row_type row)
 {
        rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
        cells_.insert
-               (cells_.begin() + (row + 1) * ncols(), ncols(), MathData());
+               (cells_.begin() + (row + 1) * ncols(), ncols(), MathData(buffer_));
        cellinfo_.insert
                (cellinfo_.begin() + (row + 1) * ncols(), ncols(), CellInfo());
 }
@@ -831,8 +833,11 @@ void InsetMathGrid::delRow(row_type row)
 void InsetMathGrid::copyRow(row_type row)
 {
        addRow(row);
-       for (col_type col = 0; col < ncols(); ++col)
+       for (col_type col = 0; col < ncols(); ++col) {
                cells_[(row + 1) * ncols() + col] = cells_[row * ncols() + col];
+               // copying the cell does not set the buffer
+               cells_[(row + 1) * ncols() + col].setBuffer(*buffer_);
+       }
 }
 
 
@@ -862,6 +867,8 @@ void InsetMathGrid::addCol(col_type newcol)
                                = cellinfo_[row * nc + col];
                }
        swap(cells_, new_cells);
+       // copying cells loses the buffer reference
+       setBuffer(*buffer_);
        swap(cellinfo_, new_cellinfo);
 
        ColInfo inf;
@@ -893,8 +900,11 @@ void InsetMathGrid::delCol(col_type col)
 void InsetMathGrid::copyCol(col_type col)
 {
        addCol(col+1);
-       for (row_type row = 0; row < nrows(); ++row)
+       for (row_type row = 0; row < nrows(); ++row) {
                cells_[row * ncols() + col + 1] = cells_[row * ncols() + col];
+               // copying the cell does not set the buffer
+               cells_[row * ncols() + col + 1].setBuffer(*buffer_);
+       }
 }
 
 
@@ -968,7 +978,14 @@ bool InsetMathGrid::idxUpDown(Cursor & cur, bool up) const
                LASSERT(cur.idx() > 0, return false);
                --cur.idx();
        }
-       cur.pos() = cur.cell().x2pos(&cur.bv(), cur.x_target() - cur.cell().xo(cur.bv()));
+       // FIXME: this is only a workaround to avoid a crash if the inset
+       // in not in coord cache. The best would be to force a FitCursor
+       // operation.
+       CoordCache::Arrays const & arraysCache = cur.bv().coordCache().arrays();
+       if (arraysCache.has(&cur.cell()))
+               cur.pos() = cur.cell().x2pos(&cur.bv(), cur.x_target() - cur.cell().xo(cur.bv()));
+       else
+               cur.pos() = 0;
        return true;
 }