]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/InsetMathGrid.cpp
Get rid of Inset::setPosCache
[lyx.git] / src / mathed / InsetMathGrid.cpp
index 7676420d940871d812f6509b17218b906ee19cf0..fca8f380d24ed759edf718555b31f9b519ee82c7 100644 (file)
 #include "Buffer.h"
 #include "BufferParams.h"
 #include "BufferView.h"
-#include "CutAndPaste.h"
-#include "FuncStatus.h"
 #include "Cursor.h"
+#include "CutAndPaste.h"
 #include "FuncRequest.h"
+#include "FuncStatus.h"
+#include "LaTeXFeatures.h"
+#include "TexRow.h"
 
 #include "frontends/Clipboard.h"
 #include "frontends/Painter.h"
@@ -34,7 +36,6 @@
 #include "support/docstream.h"
 #include "support/gettext.h"
 #include "support/lstrings.h"
-
 #include "support/lassert.h"
 
 #include <sstream>
@@ -90,7 +91,8 @@ InsetMathGrid::CellInfo::CellInfo()
 
 
 InsetMathGrid::RowInfo::RowInfo()
-       : lines_(0), skip_(0), allow_newpage_(true)
+       : descent_(0), ascent_(0), offset_(0), lines_(0), skip_(0),
+         allow_newpage_(true)
 {}
 
 
@@ -106,7 +108,7 @@ int InsetMathGrid::RowInfo::skipPixels(MetricsInfo const & mi) const
 
 
 InsetMathGrid::ColInfo::ColInfo()
-       : align_('c'), lines_(0)
+       : align_('c'), width_(0), offset_(0), lines_(0), skip_(0)
 {}
 
 
@@ -180,7 +182,7 @@ void InsetMathGrid::setDefaults()
 bool InsetMathGrid::interpretString(Cursor & cur, docstring const & str)
 {
        if (str == "\\hline") {
-               FuncRequest fr = FuncRequest(LFUN_INSET_MODIFY, "tabular add-hline-above");
+               FuncRequest fr = FuncRequest(LFUN_TABULAR_FEATURE, "add-hline-above");
                FuncStatus status;
                if (getStatus(cur, fr, status)) {
                        if (status.enabled()) {
@@ -416,7 +418,6 @@ void InsetMathGrid::metrics(MetricsInfo & mi, Dimension & dim) const
                rowinfo_[row].ascent_  = asc;
                rowinfo_[row].descent_ = desc;
        }
-       rowinfo_[0].ascent_       += hlinesep() * rowinfo_[0].lines_;
        rowinfo_[nrows()].ascent_  = 0;
        rowinfo_[nrows()].descent_ = 0;
 
@@ -480,12 +481,12 @@ void InsetMathGrid::metrics(MetricsInfo & mi, Dimension & dim) const
        colinfo_[ncols()].width_  = 0;
 
        // compute horizontal offsets
-       colinfo_[0].offset_ = border();
+       colinfo_[0].offset_ = border() + colinfo_[0].lines_ * vlinesep();;
        for (col_type col = 1; col <= ncols(); ++col) {
                colinfo_[col].offset_ =
                        colinfo_[col - 1].offset_ +
                        colinfo_[col - 1].width_ +
-                       colinfo_[col - 1].skip_ +
+                       displayColSpace(col - 1) +
                        colsep() +
                        colinfo_[col].lines_ * vlinesep();
        }
@@ -507,7 +508,7 @@ void InsetMathGrid::metrics(MetricsInfo & mi, Dimension & dim) const
                        int const nextoffset =
                                colinfo_[first].offset_ +
                                wid +
-                               colinfo_[last].skip_ +
+                               displayColSpace(last) +
                                colsep() +
                                colinfo_[last+1].lines_ * vlinesep();
                        int const dx = nextoffset - colinfo_[last+1].offset_;
@@ -533,7 +534,7 @@ void InsetMathGrid::metrics(MetricsInfo & mi, Dimension & dim) const
        dim.des = rowinfo_[nrows() - 1].offset_
                + rowinfo_[nrows() - 1].descent_
                + hlinesep() * rowinfo_[nrows()].lines_
-               + border();
+               + border() + 1;
 
 
 /*
@@ -586,81 +587,75 @@ void InsetMathGrid::metrics(MetricsInfo & mi, Dimension & dim) const
                cxrow->setBaseline(cxrow->getBaseline() - ascent);
        }
 */
-       metricsMarkers2(dim);
+       dim.wid += leftMargin() + rightMargin();
+       metricsMarkers2(mi, dim);
        // Cache the inset dimension.
        setDimCache(mi, dim);
 }
 
 
-void InsetMathGrid::draw(PainterInfo & pi, int x, int y) const
+int InsetMathGrid::vLineHOffset(col_type col, unsigned int line) const
 {
-       drawWithMargin(pi, x, y, 1, 1);
+       if (col < ncols())
+               return leftMargin() + colinfo_[col].offset_
+                       - (colinfo_[col].lines_ - line - 1) * vlinesep()
+                       - vlinesep()/2 - colsep()/2;
+       else {
+               LASSERT(col == ncols(), return 0);
+               return leftMargin() + colinfo_[col-1].offset_ + colinfo_[col-1].width_
+                       + line * vlinesep()
+                       + vlinesep()/2 + colsep()/2;
+       }
+}
+
+
+int InsetMathGrid::hLineVOffset(row_type row, unsigned int line) const
+{
+       return rowinfo_[row].offset_
+               - rowinfo_[row].ascent_
+               - line * hlinesep()
+               - hlinesep()/2 - rowsep()/2;
 }
 
 
-void InsetMathGrid::drawWithMargin(PainterInfo & pi, int x, int y,
-       int lmargin, int rmargin) const
+void InsetMathGrid::draw(PainterInfo & pi, int x, int y) const
 {
-       Dimension const dim = dimension(*pi.base.bv);
        BufferView const & bv = *pi.base.bv;
 
        for (idx_type idx = 0; idx < nargs(); ++idx) {
                if (cellinfo_[idx].multi_ != CELL_PART_OF_MULTICOLUMN) {
                        cell(idx).draw(pi,
-                               x + lmargin + cellXOffset(bv, idx),
-                               y + cellYOffset(idx));
-
-                       // draw inner lines cell by cell because of possible multicolumns
-                       // FIXME: multicolumn lines are not yet considered
-                       row_type const r = row(idx);
-                       col_type const c = col(idx);
-                       if (r > 0 && r < nrows()) {
-                               for (unsigned int i = 0; i < rowinfo_[r].lines_; ++i) {
-                                       int yy = y + rowinfo_[r].offset_
-                                               - rowinfo_[r].ascent_
-                                               - i * hlinesep()
-                                               - hlinesep()/2 - rowsep()/2;
-                                       pi.pain.line(
-                                               x + lmargin + colinfo_[c].offset_,
-                                               yy,
-                                               x + lmargin + colinfo_[c+1].offset_,
-                                               yy, Color_foreground);
-                               }
-                       }
-                       if (c > 0 && c < ncols()) {
+                                      x + leftMargin() + cellXOffset(bv, idx),
+                                      y + cellYOffset(idx));
+
+                       row_type r = row(idx);
+                       int const yy1 = y + hLineVOffset(r, 0);
+                       int const yy2 = y + hLineVOffset(r + 1, rowinfo_[r + 1].lines_ - 1);
+                       auto draw_left_borders = [&](col_type c) {
                                for (unsigned int i = 0; i < colinfo_[c].lines_; ++i) {
-                                       int xx = x + lmargin
-                                               + colinfo_[c].offset_
-                                               - i * vlinesep()
-                                               - vlinesep()/2 - colsep()/2;
-                                       pi.pain.line(xx,
-                                               rowinfo_[r].offset_ - rowinfo_[r].ascent_,
-                                               xx,
-                                               rowinfo_[r].offset_ + rowinfo_[r].descent_,
-                                               Color_foreground);
+                                       int const xx = x + vLineHOffset(c, i);
+                                       pi.pain.line(xx, yy1, xx, yy2, Color_foreground);
                                }
-                       }
+                       };
+                       col_type c = col(idx);
+                       // Draw inner left borders cell-by-cell because of multicolumns
+                       draw_left_borders(c);
+                       // Draw the right border (only once)
+                       if (c == 0)
+                               draw_left_borders(ncols());
                }
        }
 
-       // draw outer lines in one go
-       for (row_type row = 0; row <= nrows(); row += nrows())
-               for (unsigned int i = 0; i < rowinfo_[row].lines_; ++i) {
-                       int yy = y + rowinfo_[row].offset_ - rowinfo_[row].ascent_
-                               - i * hlinesep() - hlinesep()/2 - rowsep()/2;
-                       pi.pain.line(x + lmargin + 1, yy,
-                                    x + dim.width() - rmargin - 1, yy,
-                                    Color_foreground);
+       // Draw horizontal borders
+       for (row_type r = 0; r <= nrows(); ++r) {
+               int const xx1 = x + vLineHOffset(0, 0);
+               int const xx2 = x + vLineHOffset(ncols(), colinfo_[ncols()].lines_ - 1);
+               for (unsigned int i = 0; i < rowinfo_[r].lines_; ++i) {
+                       int const yy = y + hLineVOffset(r, i);
+                       pi.pain.line(xx1, yy, xx2, yy, Color_foreground);
                }
+       }
 
-       for (col_type col = 0; col <= ncols(); col += ncols())
-               for (unsigned int i = 0; i < colinfo_[col].lines_; ++i) {
-                       int xx = x + lmargin + colinfo_[col].offset_
-                               - i * vlinesep() - vlinesep()/2 - colsep()/2;
-                       pi.pain.line(xx, y - dim.ascent() + 1,
-                                    xx, y + dim.descent() - 1,
-                                    Color_foreground);
-               }
        drawMarkers2(pi, x, y);
 }
 
@@ -689,7 +684,6 @@ void InsetMathGrid::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
                rowinfo_[row].ascent_  = asc;
                rowinfo_[row].descent_ = desc;
        }
-       //rowinfo_[0].ascent_       += hlinesep() * rowinfo_[0].lines_;
        rowinfo_[nrows()].ascent_  = 0;
        rowinfo_[nrows()].descent_ = 0;
 
@@ -740,7 +734,7 @@ void InsetMathGrid::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
                colinfo_[col].offset_ =
                        colinfo_[col - 1].offset_ +
                        colinfo_[col - 1].width_ +
-                       colinfo_[col - 1].skip_ +
+                       displayColSpace(col - 1) +
                        1 ; //colsep() +
                        //colinfo_[col].lines_ * vlinesep();
        }
@@ -952,7 +946,7 @@ int InsetMathGrid::cellWidth(idx_type idx) const
                col_type c2 = c1 + ncellcols(idx);
                return colinfo_[c2].offset_
                        - colinfo_[c1].offset_
-                       - colinfo_[c2].skip_
+                       - displayColSpace(c2)
                        - colsep()
                        - colinfo_[c2].lines_ * vlinesep();
        }
@@ -1236,6 +1230,20 @@ void InsetMathGrid::htmlize(HtmlStream & os) const
 }
 
 
+void InsetMathGrid::validate(LaTeXFeatures & features) const
+{
+       if (features.runparams().math_flavor == OutputParams::MathAsHTML
+           && (nrows() > 1 || ncols() > 1)) {
+               // CSS taken from InsetMathCases
+               features.addCSSSnippet(
+                       "table.mathtable{display: inline-block; text-align: center; border: none;"
+                       "border-left: thin solid black; vertical-align: middle; padding-left: 0.5ex;}\n"
+                       "table.mathtable td {text-align: left; border: none;}");
+       }
+       InsetMathNest::validate(features);
+}
+
+
 void InsetMathGrid::write(WriteStream & os) const
 {
        write(os, 0, 0, nrows(), ncols());
@@ -1256,19 +1264,25 @@ void InsetMathGrid::write(WriteStream & os,
                bool last_eoln = true;
                for (col_type col = beg_col; col < end_col; ++col) {
                        idx_type const idx = index(row, col);
-                       if (cellinfo_[idx].multi_ == CELL_PART_OF_MULTICOLUMN)
-                               continue;
                        bool const empty_cell = cell(idx).empty();
-                       if (!empty_cell)
+                       if (!empty_cell || cellinfo_[idx].multi_ != CELL_NORMAL)
                                last_eoln = false;
-                       if (!empty_cell || colinfo_[col + 1].lines_) {
+                       if (!empty_cell || cellinfo_[idx].multi_ != CELL_NORMAL ||
+                           colinfo_[col + 1].lines_) {
                                lastcol = col + 1;
                                emptyline = false;
                        }
                }
-               for (col_type col = beg_col; col < lastcol;) {
+               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);
+                       os.texrow().start(entry);
+                       if (col >= lastcol) {
+                               ++col;
+                               continue;
+                       }
+                       Changer dummy = os.changeRowEntry(entry);
                        if (cellinfo_[idx].multi_ == CELL_BEGIN_OF_MULTICOLUMN) {
                                size_t s = col + 1;
                                while (s < ncols() &&
@@ -1284,7 +1298,7 @@ void InsetMathGrid::write(WriteStream & os,
                                ModeSpecifier specifier(os, TEXT_MODE);
                        if (cellinfo_[idx].multi_ == CELL_BEGIN_OF_MULTICOLUMN)
                                os << '}';
-                       os << eocString(col, lastcol);
+                       os << eocString(col + nccols - 1, lastcol);
                        col += nccols;
                }
                eol = eolString(row, os.fragile(), os.latex(), last_eoln);
@@ -1370,6 +1384,11 @@ char InsetMathGrid::displayColAlign(idx_type idx) const
 }
 
 
+int InsetMathGrid::displayColSpace(col_type col) const
+{
+       return colinfo_[col].skip_;
+}
+
 void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
 {
        //lyxerr << "*** InsetMathGrid: request: " << cmd << endl;
@@ -1403,7 +1422,7 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
 
        case LFUN_CELL_BACKWARD:
                // See below.
-               cur.setSelection(false);
+               cur.selection(false);
                if (!idxPrev(cur)) {
                        cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
                        cur.undispatched();
@@ -1413,7 +1432,7 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
        case LFUN_CELL_FORWARD:
                // Can't handle selection by additional 'shift' as this is
                // hard bound to LFUN_CELL_BACKWARD
-               cur.setSelection(false);
+               cur.selection(false);
                if (!idxNext(cur)) {
                        cmd = FuncRequest(LFUN_FINISHED_FORWARD);
                        cur.undispatched();
@@ -1431,7 +1450,8 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
 
                // split cell
                splitCell(cur);
-               swap(cell(cur.idx()), cell(cur.idx() + ncols() - 1));
+               if (ncols() > 1)
+                       swap(cell(cur.idx()), cell(cur.idx() + ncols() - 1));
                if (cur.idx() > 0)
                        --cur.idx();
                cur.pos() = cur.lastpos();
@@ -1441,17 +1461,12 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
                break;
        }
 
-       case LFUN_INSET_MODIFY: {
+       case LFUN_TABULAR_FEATURE: {
                cur.recordUndoInset();
                //lyxerr << "handling tabular-feature " << to_utf8(cmd.argument()) << endl;
                istringstream is(to_utf8(cmd.argument()));
                string s;
                is >> s;
-               if (s != "tabular") {
-                       InsetMathNest::doDispatch(cur, cmd);
-                       return;
-               }
-               is >> s;
                if (s == "valign-top")
                        setVerticalAlignment('t');
                else if (s == "valign-middle")
@@ -1572,7 +1587,7 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
                cap::replaceSelection(cur);
                docstring topaste;
                if (cmd.argument().empty() && !theClipboard().isInternal())
-                       topaste = theClipboard().getAsText(Clipboard::PlainTextType);
+                       topaste = theClipboard().getAsText(frontend::Clipboard::PlainTextType);
                else {
                        idocstringstream is(cmd.argument());
                        int n = 0;
@@ -1588,7 +1603,7 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
                        }
 
                bool hline_enabled = false;
-               FuncRequest fr = FuncRequest(LFUN_INSET_MODIFY, "tabular add-hline-above");
+               FuncRequest fr = FuncRequest(LFUN_TABULAR_FEATURE, "add-hline-above");
                FuncStatus status;
                if (getStatus(cur, fr, status))
                        hline_enabled = status.enabled();
@@ -1659,6 +1674,7 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
        case LFUN_WORD_BACKWARD:
        case LFUN_WORD_LEFT:
                cur.screenUpdateFlags(Update::Decoration | Update::FitCursor);
+               // fall through
        case LFUN_LINE_BEGIN_SELECT:
        case LFUN_WORD_BACKWARD_SELECT:
        case LFUN_WORD_LEFT_SELECT:
@@ -1684,6 +1700,7 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
        case LFUN_WORD_RIGHT:
        case LFUN_LINE_END:
                cur.screenUpdateFlags(Update::Decoration | Update::FitCursor);
+               // fall through
        case LFUN_WORD_FORWARD_SELECT:
        case LFUN_WORD_RIGHT_SELECT:
        case LFUN_LINE_END_SELECT:
@@ -1716,14 +1733,8 @@ bool InsetMathGrid::getStatus(Cursor & cur, FuncRequest const & cmd,
                FuncStatus & status) const
 {
        switch (cmd.action()) {
-       case LFUN_INSET_MODIFY: {
-               istringstream is(to_utf8(cmd.argument()));
-               string s;
-               is >> s;
-               if (s != "tabular") {
-                       // We only now about table actions here.
-                       break;
-               }
+       case LFUN_TABULAR_FEATURE: {
+               string s = cmd.getArg(0);
                if (&cur.inset() != this) {
                        // Table actions requires that the cursor is _inside_ the
                        // table.
@@ -1731,7 +1742,6 @@ bool InsetMathGrid::getStatus(Cursor & cur, FuncRequest const & cmd,
                        status.message(from_utf8(N_("Cursor not in table")));
                        return true;
                }
-               is >> s;
                if (nrows() <= 1 && (s == "delete-row" || s == "swap-row")) {
                        status.setEnabled(false);
                        status.message(from_utf8(N_("Only one row")));
@@ -1786,7 +1796,8 @@ bool InsetMathGrid::getStatus(Cursor & cur, FuncRequest const & cmd,
                } else {
                        status.setEnabled(false);
                        status.message(bformat(
-                               from_utf8(N_("Unknown tabular feature '%1$s'")), lyx::from_ascii(s)));
+                           from_utf8(N_("Unknown tabular feature '%1$s'")),
+                           from_utf8(s)));
                }
 
 #if 0
@@ -1827,4 +1838,69 @@ bool InsetMathGrid::getStatus(Cursor & cur, FuncRequest const & cmd,
 }
 
 
+// static
+char InsetMathGrid::colAlign(HullType type, col_type col)
+{
+       switch (type) {
+       case hullEqnArray:
+               return "rcl"[col % 3];
+
+       case hullMultline:
+       case hullGather:
+               return 'c';
+
+       case hullAlign:
+       case hullAlignAt:
+       case hullXAlignAt:
+       case hullXXAlignAt:
+       case hullFlAlign:
+               return "rl"[col & 1];
+
+       case hullUnknown:
+       case hullNone:
+       case hullSimple:
+       case hullEquation:
+       case hullRegexp:
+               return 'c';
+       }
+       // avoid warning
+       return 'c';
+}
+
+
+//static
+int InsetMathGrid::colSpace(HullType type, col_type col)
+{
+       int alignInterSpace = 0;
+       switch (type) {
+       case hullUnknown:
+       case hullNone:
+       case hullSimple:
+       case hullEquation:
+       case hullMultline:
+       case hullGather:
+       case hullRegexp:
+               return 0;
+
+       case hullEqnArray:
+               return 5;
+
+       case hullAlign:
+               alignInterSpace = 20;
+               break;
+       case hullAlignAt:
+               alignInterSpace = 0;
+               break;
+       case hullXAlignAt:
+               alignInterSpace = 40;
+               break;
+       case hullXXAlignAt:
+       case hullFlAlign:
+               alignInterSpace = 60;
+               break;
+       }
+       return (col % 2) ? alignInterSpace : 0;
+}
+
+
 } // namespace lyx