X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Fmathed%2FInsetMathGrid.cpp;h=d3eb677197dcd2535d717745dae7c2caf3f5f7ba;hb=66fa801e74e1775b31008df548332436ce79e2e1;hp=d37870d4ada3b7cc24908a559b75394e0c789e44;hpb=da1e7e14ce8afdf7925b8a75790c63301d0402aa;p=lyx.git diff --git a/src/mathed/InsetMathGrid.cpp b/src/mathed/InsetMathGrid.cpp index d37870d4ad..d3eb677197 100644 --- a/src/mathed/InsetMathGrid.cpp +++ b/src/mathed/InsetMathGrid.cpp @@ -13,12 +13,14 @@ #include "InsetMathGrid.h" +#include "InsetMathUnknown.h" #include "MathData.h" #include "MathParser.h" #include "MathStream.h" #include "MetricsInfo.h" #include "Buffer.h" +#include "BufferParams.h" #include "BufferView.h" #include "CutAndPaste.h" #include "FuncStatus.h" @@ -26,7 +28,6 @@ #include "FuncRequest.h" #include "frontends/Clipboard.h" -#include "frontends/FontMetrics.h" #include "frontends/Painter.h" #include "support/debug.h" @@ -67,9 +68,9 @@ static int extractInt(istream & is) static void resetGrid(InsetMathGrid & grid) { while (grid.ncols() > 1) - grid.delCol(grid.ncols()); + grid.delCol(grid.ncols() - 1); while (grid.nrows() > 1) - grid.delRow(grid.nrows()); + grid.delRow(grid.nrows() - 1); grid.cell(0).erase(0, grid.cell(0).size()); grid.setDefaults(); } @@ -96,9 +97,7 @@ InsetMathGrid::RowInfo::RowInfo() int InsetMathGrid::RowInfo::skipPixels(MetricsInfo const & mi) const { - frontend::FontMetrics const & fm = theFontMetrics(mi.base.font); - return crskip_.inPixels(mi.base.textwidth, - fm.width(char_type('M'))); + return crskip_.inPixels(mi.base); } @@ -176,6 +175,22 @@ void InsetMathGrid::setDefaults() } +bool InsetMathGrid::interpretString(Cursor & cur, docstring const & str) +{ + if (str == "\\hline") { + FuncRequest fr = FuncRequest(LFUN_INSET_MODIFY, "tabular add-hline-above"); + FuncStatus status; + if (getStatus(cur, fr, status)) { + if (status.enabled()) { + rowinfo_[cur.row()].lines_++; + return true; + } + } + } + return InsetMathNest::interpretString(cur, str); +} + + void InsetMathGrid::setHorizontalAlignments(docstring const & hh) { col_type col = 0; @@ -381,8 +396,8 @@ void InsetMathGrid::metrics(MetricsInfo & mi, Dimension & dim) const // compute vertical offsets rowinfo_[0].offset_ = 0; for (row_type row = 1; row <= nrows(); ++row) { - rowinfo_[row].offset_ = - rowinfo_[row - 1].offset_ + + rowinfo_[row].offset_ = + rowinfo_[row - 1].offset_ + rowinfo_[row - 1].descent_ + rowinfo_[row - 1].skipPixels(mi) + rowsep() + @@ -427,20 +442,20 @@ void InsetMathGrid::metrics(MetricsInfo & mi, Dimension & dim) const } - dim.wid = colinfo_[ncols() - 1].offset_ - + colinfo_[ncols() - 1].width_ - + vlinesep() * colinfo_[ncols()].lines_ - + border(); + dim.wid = colinfo_[ncols() - 1].offset_ + + colinfo_[ncols() - 1].width_ + + vlinesep() * colinfo_[ncols()].lines_ + + border(); - dim.asc = - rowinfo_[0].offset_ - + rowinfo_[0].ascent_ - + hlinesep() * rowinfo_[0].lines_ - + border(); + dim.asc = - rowinfo_[0].offset_ + + rowinfo_[0].ascent_ + + hlinesep() * rowinfo_[0].lines_ + + border(); - dim.des = rowinfo_[nrows() - 1].offset_ - + rowinfo_[nrows() - 1].descent_ - + hlinesep() * rowinfo_[nrows()].lines_ - + border(); + dim.des = rowinfo_[nrows() - 1].offset_ + + rowinfo_[nrows() - 1].descent_ + + hlinesep() * rowinfo_[nrows()].lines_ + + border(); /* @@ -657,7 +672,7 @@ docstring InsetMathGrid::eolString(row_type row, bool fragile, // make sure an upcoming '[' does not break anything if (row + 1 < nrows()) { MathData const & c = cell(index(row + 1, 0)); - if (c.size() && c.front()->getChar() == '[') + if (!c.empty() && c.front()->getChar() == '[') //eol += "[0pt]"; eol += "{}"; } @@ -688,17 +703,6 @@ void InsetMathGrid::addRow(row_type row) } -void InsetMathGrid::appendRow() -{ - rowinfo_.push_back(RowInfo()); - //cells_.insert(cells_.end(), ncols(), MathData()); - for (col_type col = 0; col < ncols(); ++col) { - cells_.push_back(cells_type::value_type()); - cellinfo_.push_back(CellInfo()); - } -} - - void InsetMathGrid::delRow(row_type row) { if (nrows() == 1) @@ -799,7 +803,7 @@ int InsetMathGrid::cellXOffset(BufferView const & bv, idx_type idx) const { col_type c = col(idx); int x = colinfo_[c].offset_; - char align = colinfo_[c].align_; + char align = displayColAlign(c, row(idx)); Dimension const & celldim = cell(idx).dimension(bv); if (align == 'r' || align == 'R') x += colinfo_[c].width_ - celldim.wid; @@ -899,7 +903,7 @@ bool InsetMathGrid::idxDelete(idx_type & idx) // try to delete entire sequence of ncols() empty cells if possible for (idx_type i = idx; i < idx + ncols(); ++i) - if (cell(i).size()) + if (!cell(i).empty()) return false; // move cells if necessary @@ -986,14 +990,14 @@ void InsetMathGrid::mathmlize(MathStream & os) const char const * const celltag = havetable ? "mtd" : "mrow"; for (row_type row = 0; row < nrows(); ++row) { if (havetable) - os << MTag("mtr");; + os << MTag("mtr"); for (col_type col = 0; col < ncols(); ++col) { os << MTag(celltag); os << cell(index(row, col)); os << ETag(celltag); } if (havetable) - os << ETag("mtr");; + os << ETag("mtr"); } if (havetable) os << ETag("mtable"); @@ -1011,13 +1015,13 @@ void InsetMathGrid::htmlize(HtmlStream & os, string attrib) const } os << MTag("table", attrib); for (row_type row = 0; row < nrows(); ++row) { - os << MTag("tr");; + os << MTag("tr"); for (col_type col = 0; col < ncols(); ++col) { os << MTag("td"); os << cell(index(row, col)); os << ETag("td"); } - os << ETag("tr");; + os << ETag("tr"); } os << ETag("table"); } @@ -1136,7 +1140,7 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd) switch (act) { // insert file functions - case LFUN_LINE_DELETE: + case LFUN_LINE_DELETE_FORWARD: cur.recordUndoInset(); //autocorrect_ = false; //macroModeClose(); @@ -1191,7 +1195,7 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd) if (cur.idx() > 0) --cur.idx(); cur.pos() = cur.lastpos(); - + cur.forceBufferUpdate(); //mathcursor->normalize(); //cmd = FuncRequest(LFUN_FINISHED_BACKWARD); break; @@ -1296,7 +1300,7 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd) docstring & special = colinfo_[cur.col()].special_; if (!special.empty()) { docstring::size_type i = special.rfind('|'); - LASSERT(i != docstring::npos, /**/); + LASSERT(i != docstring::npos, break); special.erase(i, 1); } } @@ -1305,7 +1309,7 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd) docstring & special = colinfo_[cur.col()+1].special_; if (!special.empty()) { docstring::size_type i = special.find('|'); - LASSERT(i != docstring::npos, /**/); + LASSERT(i != docstring::npos, break); special.erase(i, 1); } } @@ -1328,26 +1332,40 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd) cap::replaceSelection(cur); docstring topaste; if (cmd.argument().empty() && !theClipboard().isInternal()) - topaste = theClipboard().getAsText(); + topaste = theClipboard().getAsText(Clipboard::PlainTextType); else { idocstringstream is(cmd.argument()); int n = 0; is >> n; - topaste = cap::selection(n); + topaste = cap::selection(n, buffer().params().documentClassPtr()); } InsetMathGrid grid(buffer_, 1, 1); if (!topaste.empty()) - if ((topaste.size() == 1 && topaste.at(0) < 0x80) + if ((topaste.size() == 1 && isAscii(topaste)) || !mathed_parse_normal(grid, topaste, parseflg)) { resetGrid(grid); mathed_parse_normal(grid, topaste, parseflg | Parse::VERBATIM); } + bool hline_enabled = false; + FuncRequest fr = FuncRequest(LFUN_INSET_MODIFY, "tabular add-hline-above"); + FuncStatus status; + if (getStatus(cur, fr, status)) + hline_enabled = status.enabled(); if (grid.nargs() == 1) { // single cell/part of cell - cur.recordUndo(); + cur.recordUndoInset(); cur.cell().insert(cur.pos(), grid.cell(0)); cur.pos() += grid.cell(0).size(); + if (hline_enabled) + rowinfo_[cur.row()].lines_ += grid.rowinfo_[0].lines_; + else { + for (unsigned int l = 0; l < grid.rowinfo_[0].lines_; ++l) { + cur.cell().insert(0, + MathAtom(new InsetMathUnknown(from_ascii("\\hline")))); + cur.pos()++; + } + } } else { // multiple cells cur.recordUndoInset(); @@ -1360,6 +1378,15 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd) idx_type i = index(r + cur.row(), c + col(cur.idx())); cell(i).insert(0, 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); + 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) @@ -1367,9 +1394,18 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd) } // append the left over vertical cells to the last _cell_ idx_type i = nargs() - 1; - for (row_type r = numrows; r < grid.nrows(); ++r) + for (row_type r = numrows; r < grid.nrows(); ++r) { for (col_type c = 0; c < grid.ncols(); ++c) cell(i).append(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) { + cell(i).insert(0, + MathAtom(new InsetMathUnknown(from_ascii("\\hline")))); + } + } + } } cur.clearSelection(); // bug 393 // FIXME audit setBuffer calls @@ -1534,7 +1570,7 @@ bool InsetMathGrid::getStatus(Cursor & cur, FuncRequest const & cmd, } case LFUN_CELL_SPLIT: - status.setEnabled(true); + status.setEnabled(cur.idx() != cur.lastidx()); return true; case LFUN_CELL_BACKWARD: