]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/InsetMathGrid.cpp
Revert "XHTML: remove DOCTYPE, as the document is then understood as HTML4/XHTML1...
[lyx.git] / src / mathed / InsetMathGrid.cpp
index 6aaddc2177ddad47c9c31ed3e3cae875f3290ab3..3de72abcb194cd9dedb022fc108871d345a0977a 100644 (file)
@@ -1624,33 +1624,34 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
                } else {
                        // multiple cells
                        cur.recordUndoInset();
-                       col_type startcol =  col(cur.idx());
+                       col_type startcol = col(cur.idx());
                        row_type startrow = cur.row();
                        col_type oldncols = ncols();
-                       col_type const numcols =
+                       col_type numcols =
                                min(grid.ncols(), ncols() - startcol);
                        row_type const numrows =
                                min(grid.nrows(), nrows() - cur.row());
                        for (row_type r = 0; r < numrows; ++r) {
                                for (col_type c = 0; c < numcols; ++c) {
-                                       idx_type i = index(r + cur.row(), c + col(cur.idx()));
-                                       cell(i).insert(c == 0 ? cur.pos() : 0,
-                                                      grid.cell(grid.index(r, c)));
+                                       idx_type i = index(r + startrow, c + startcol);
+                                       pos_type const ipos = min(cur.pos(), pos_type(cell(i).size()));
+                                       cell(i).insert(ipos, grid.cell(grid.index(r, c)));
                                }
                                if (hline_enabled)
                                        rowinfo_[r].lines += grid.rowinfo_[r].lines;
                                else {
                                        for (unsigned int l = 0; l < grid.rowinfo_[r].lines; ++l) {
-                                               idx_type i = index(r + cur.row(), 0);
+                                               idx_type i = index(r + startrow, 0);
                                                cell(i).insert(0,
                                                        MathAtom(new InsetMathUnknown(from_ascii("\\hline"))));
                                        }
                                }
                                // append columns for the left over horizontal cells
                                for (InsetMath::col_type c = numcols; c < grid.ncols(); ++c) {
-                                       addCol(c + 1);
-                                       idx_type i = index(r + cur.row(), c + 1);
+                                       addCol(c + startcol);
+                                       idx_type i = index(r + startrow, min(c + startcol, ncols() - 1));
                                        cell(i).append(grid.cell(grid.index(r, c)));
+                                       ++numcols;
                                }
                        }
                        // amend cursor position if cols have been appended
@@ -1661,7 +1662,7 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
                                row_type crow = startrow + r;
                                addRow(crow - 1);
                                for (col_type c = 0; c < grid.ncols(); ++c)
-                                       cell(index(crow, c + startcol)).append(grid.cell(grid.index(r, c)));
+                                       cell(index(min(crow, nrows() - 1), min(c + startcol, ncols() - 1))).append(grid.cell(grid.index(r, c)));
                                if (hline_enabled)
                                        rowinfo_[crow].lines += grid.rowinfo_[r].lines;
                                else {