]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/InsetMathGrid.cpp
Allow pasting references to mathed
[lyx.git] / src / mathed / InsetMathGrid.cpp
index 9ba2dda197e45fdf3fffdd06c441f2aa8fe6d973..3a8b37609c2a33e3a31e7fe56452a782b01cbf30 100644 (file)
@@ -78,41 +78,15 @@ static void resetGrid(InsetMathGrid & grid)
 }
 
 
-
 //////////////////////////////////////////////////////////////
 
 
-InsetMathGrid::CellInfo::CellInfo()
-       : multi(CELL_NORMAL)
-{}
-
-
-
-//////////////////////////////////////////////////////////////
-
-
-InsetMathGrid::RowInfo::RowInfo()
-       : descent(0), ascent(0), lines(0), skip(0),
-         allow_newpage(true)
-{}
-
-
-
 int InsetMathGrid::RowInfo::skipPixels(MetricsInfo const & mi) const
 {
        return mi.base.inPixels(crskip);
 }
 
 
-
-//////////////////////////////////////////////////////////////
-
-
-InsetMathGrid::ColInfo::ColInfo()
-       : align('c'), width(0), offset(0), lines(0), skip(0)
-{}
-
-
 //////////////////////////////////////////////////////////////
 
 
@@ -158,7 +132,7 @@ Inset * InsetMathGrid::clone() const
 }
 
 
-InsetMath::idx_type InsetMathGrid::index(row_type row, col_type col) const
+idx_type InsetMathGrid::index(row_type row, col_type col) const
 {
        return col + ncols() * row;
 }
@@ -278,12 +252,12 @@ void InsetMathGrid::setHorizontalAlignments(docstring const & hh)
 }
 
 
-InsetMathGrid::col_type InsetMathGrid::guessColumns(docstring const & hh)
+col_type InsetMathGrid::guessColumns(docstring const & hh)
 {
        col_type col = 0;
-       for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it)
-               if (*it == 'c' || *it == 'l' || *it == 'r'||
-                   *it == 'p' || *it == 'm' || *it == 'b')
+       for (char_type const c : hh)
+               if (c == 'c' || c == 'l' || c == 'r'||
+                   c == 'p' || c == 'm' || c == 'b')
                        ++col;
        // let's have at least one column, even if we did not recognize its
        // alignment
@@ -339,31 +313,31 @@ char InsetMathGrid::verticalAlignment() const
 }
 
 
-InsetMathGrid::col_type InsetMathGrid::ncols() const
+col_type InsetMathGrid::ncols() const
 {
        return colinfo_.size() - 1;
 }
 
 
-InsetMathGrid::row_type InsetMathGrid::nrows() const
+row_type InsetMathGrid::nrows() const
 {
        return rowinfo_.size() - 1;
 }
 
 
-InsetMathGrid::col_type InsetMathGrid::col(idx_type idx) const
+col_type InsetMathGrid::col(idx_type idx) const
 {
        return idx % ncols();
 }
 
 
-InsetMathGrid::row_type InsetMathGrid::row(idx_type idx) const
+row_type InsetMathGrid::row(idx_type idx) const
 {
        return idx / ncols();
 }
 
 
-InsetMathGrid::col_type InsetMathGrid::ncellcols(idx_type idx) const
+col_type InsetMathGrid::ncellcols(idx_type idx) const
 {
        col_type cols = 1;
        if (cellinfo_[idx].multi == CELL_NORMAL)
@@ -483,7 +457,7 @@ void InsetMathGrid::metrics(MetricsInfo & mi, Dimension & dim) const
        colinfo_[ncols()].width  = 0;
 
        // compute horizontal offsets
-       colinfo_[0].offset = border() + colinfo_[0].lines * vlinesep();;
+       colinfo_[0].offset = border() + colinfo_[0].lines * vlinesep();
        for (col_type col = 1; col <= ncols(); ++col) {
                colinfo_[col].offset =
                        colinfo_[col - 1].offset +
@@ -767,12 +741,12 @@ void InsetMathGrid::drawT(TextPainter & /*pain*/, int /*x*/, int /*y*/) const
 }
 
 
-void InsetMathGrid::updateBuffer(ParIterator const & it, UpdateType utype)
+void InsetMathGrid::updateBuffer(ParIterator const & it, UpdateType utype, bool const deleted)
 {
        // pass down
        for (idx_type idx = 0; idx < nargs(); ++idx)
                if (cellinfo_[idx].multi != CELL_PART_OF_MULTICOLUMN)
-                       cell(idx).updateBuffer(it, utype);
+                       cell(idx).updateBuffer(it, utype, deleted);
 }
 
 
@@ -896,6 +870,8 @@ void InsetMathGrid::delCol(col_type col)
                        tmpcellinfo.push_back(cellinfo_[i]);
                }
        swap(cells_, tmpcells);
+       // copying cells loses the buffer reference
+       setBuffer(*buffer_);
        swap(cellinfo_, tmpcellinfo);
 
        colinfo_.erase(colinfo_.begin() + col);
@@ -1182,15 +1158,15 @@ void InsetMathGrid::normalize(NormalStream & os) const
 }
 
 
-void InsetMathGrid::mathmlize(MathStream & os) const
+void InsetMathGrid::mathmlize(MathMLStream & ms) const
 {
        bool const havetable = nrows() > 1 || ncols() > 1;
        if (havetable)
-               os << MTag("mtable");
+               ms << MTag("mtable");
        char const * const celltag = havetable ? "mtd" : "mrow";
        for (row_type row = 0; row < nrows(); ++row) {
                if (havetable)
-                       os << MTag("mtr");
+                       ms << MTag("mtr");
                for (col_type col = 0; col < ncols(); ++col) {
                        idx_type const i = index(row, col);
                        if (cellinfo_[i].multi != CELL_PART_OF_MULTICOLUMN) {
@@ -1198,22 +1174,22 @@ void InsetMathGrid::mathmlize(MathStream & os) const
                                ostringstream attr;
                                if (havetable && cellcols > 1)
                                        attr << "colspan='" << cellcols << '\'';
-                               os << MTag(celltag, attr.str());
-                               os << cell(index(row, col));
-                               os << ETag(celltag);
+                               ms << MTag(celltag, attr.str());
+                               ms << cell(index(row, col));
+                               ms << ETag(celltag);
                        }
                }
                if (havetable)
-                       os << ETag("mtr");
+                       ms << ETag("mtr");
        }
        if (havetable)
-               os << ETag("mtable");
+               ms << ETag("mtable");
 }
 
 
 // FIXME XHTML
 // We need to do something about alignment here.
-void InsetMathGrid::htmlize(HtmlStream & os, string attrib) const
+void InsetMathGrid::htmlize(HtmlStream & os, string const & attrib) const
 {
        bool const havetable = nrows() > 1 || ncols() > 1;
        if (!havetable) {
@@ -1261,14 +1237,14 @@ void InsetMathGrid::validate(LaTeXFeatures & features) const
 }
 
 
-void InsetMathGrid::write(WriteStream & os) const
+void InsetMathGrid::write(TeXMathStream & os) const
 {
        write(os, 0, 0, nrows(), ncols());
 }
 
-void InsetMathGrid::write(WriteStream & os,
-                         row_type beg_row, col_type beg_col,
-                         row_type end_row, col_type end_col) const
+void InsetMathGrid::write(TeXMathStream & os,
+                          row_type beg_row, col_type beg_col,
+                          row_type end_row, col_type end_col) const
 {
        MathEnsurer ensurer(os, false);
        docstring eol;
@@ -1282,7 +1258,7 @@ void InsetMathGrid::write(WriteStream & os,
                for (col_type col = beg_col; col < end_col; ++col) {
                        idx_type const idx = index(row, col);
                        bool const empty_cell = cell(idx).empty();
-                       if (!empty_cell || cellinfo_[idx].multi != CELL_NORMAL)
+                       if (last_eoln && (!empty_cell || cellinfo_[idx].multi != CELL_NORMAL))
                                last_eoln = false;
                        if (!empty_cell || cellinfo_[idx].multi != CELL_NORMAL ||
                            colinfo_[col + 1].lines) {
@@ -1293,7 +1269,7 @@ void InsetMathGrid::write(WriteStream & os,
                for (col_type col = beg_col; col < end_col;) {
                        int nccols = 1;
                        idx_type const idx = index(row, col);
-                       TexRow::RowEntry entry = TexRow::mathEntry(id(),idx);
+                       TexRow::RowEntry const entry = TexRow::mathEntry(id(),idx);
                        os.texrow().start(entry);
                        if (col >= lastcol) {
                                ++col;
@@ -1590,7 +1566,7 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
                        idocstringstream is(cmd.argument());
                        int n = 0;
                        is >> n;
-                       topaste = cap::selection(n, buffer().params().documentClassPtr());
+                       topaste = cap::selection(n, buffer().params().documentClassPtr(), true);
                }
                InsetMathGrid grid(buffer_, 1, 1);
                if (!topaste.empty())
@@ -1622,36 +1598,47 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
                } else {
                        // multiple cells
                        cur.recordUndoInset();
-                       col_type const numcols =
-                               min(grid.ncols(), ncols() - col(cur.idx()));
+                       col_type startcol = col(cur.idx());
+                       row_type startrow = cur.row();
+                       col_type oldncols = ncols();
+                       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(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 the left over horizontal cells to the last column
-                               idx_type i = index(r + cur.row(), ncols() - 1);
-                               for (InsetMath::col_type c = numcols; c < grid.ncols(); ++c)
+                               // append columns for the left over horizontal cells
+                               for (col_type c = numcols; c < grid.ncols(); ++c) {
+                                       addCol(c + startcol);
+                                       idx_type i = index(r + startrow, min(c + startcol, ncols() - 1));
                                        cell(i).append(grid.cell(grid.index(r, c)));
+                                       ++numcols;
+                               }
                        }
-                       // append the left over vertical cells to the last _cell_
+                       // amend cursor position if cols have been appended
+                       cur.idx() += startrow * (ncols() - oldncols);
+                       // append rows for the left over vertical cells
                        idx_type i = nargs() - 1;
                        for (row_type r = numrows; r < grid.nrows(); ++r) {
+                               row_type crow = startrow + r;
+                               addRow(crow - 1);
                                for (col_type c = 0; c < grid.ncols(); ++c)
-                                       cell(i).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_[r].lines += grid.rowinfo_[r].lines;
+                                       rowinfo_[crow].lines += grid.rowinfo_[r].lines;
                                else {
                                        for (unsigned int l = 0; l < grid.rowinfo_[r].lines; ++l) {
                                                cell(i).insert(0,
@@ -1790,25 +1777,6 @@ bool InsetMathGrid::getStatus(Cursor & cur, FuncRequest const & cmd,
                            from_utf8(s)));
                }
 
-#if 0
-               // FIXME: What did this code do?
-               // Please check whether it is still needed!
-               // should be more precise
-               if (v_align_ == '\0') {
-                       status.enable(true);
-                       break;
-               }
-               if (cmd.argument().empty()) {
-                       status.enable(false);
-                       break;
-               }
-               if (!contains("tcb", cmd.argument()[0])) {
-                       status.enable(false);
-                       break;
-               }
-               status.setOnOff(cmd.argument()[0] == v_align_);
-               status.setEnabled(true);
-#endif
                return true;
        }
 
@@ -1828,8 +1796,7 @@ bool InsetMathGrid::getStatus(Cursor & cur, FuncRequest const & cmd,
 }
 
 
-// static
-char InsetMathGrid::colAlign(HullType type, col_type col, BufferParams const & bp)
+char InsetMathGrid::colAlign(HullType type, col_type col) const
 {
        switch (type) {
        case hullEqnArray:
@@ -1838,10 +1805,13 @@ char InsetMathGrid::colAlign(HullType type, col_type col, BufferParams const & b
        case hullMultline:
                return 'c';
        case hullGather:
-               if (!bp.is_math_indent)
-                       return 'c';
-               else
+               LASSERT(isBufferValid(),
+                               LYXERR0("Buffer not set correctly. Please report!");
+                               return 'c';);
+               if (buffer().params().is_math_indent)
                        return 'l';
+               else
+                       return 'c';
 
        case hullAlign:
        case hullAlignAt: