]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/InsetMathGrid.cpp
New methods in LaTeXFeatures specifically for collection of CSS
[lyx.git] / src / mathed / InsetMathGrid.cpp
index 2256a08f609bbe6dc7328fec06eacf7834860efe..5b58f09a03eb1b746873057d84b00d19f985ac60 100644 (file)
@@ -18,6 +18,7 @@
 #include "MathStream.h"
 #include "MetricsInfo.h"
 
+#include "Buffer.h"
 #include "BufferView.h"
 #include "CutAndPaste.h"
 #include "FuncStatus.h"
@@ -41,6 +42,7 @@ using namespace std;
 using namespace lyx::support;
 
 
+
 namespace lyx {
 
 static docstring verboseHLine(int n)
@@ -65,9 +67,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();
 }
@@ -379,8 +381,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() +
@@ -425,20 +427,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();
 
 
 /*
@@ -634,7 +636,16 @@ void InsetMathGrid::drawT(TextPainter & /*pain*/, int /*x*/, int /*y*/) const
 }
 
 
-docstring InsetMathGrid::eolString(row_type row, bool fragile) const
+void InsetMathGrid::updateBuffer(ParIterator const & it, UpdateType utype)
+{
+       // pass down
+       for (idx_type idx = 0; idx < nargs(); ++idx)
+               cell(idx).updateBuffer(it, utype);
+}
+
+
+docstring InsetMathGrid::eolString(row_type row, bool fragile,
+               bool /*latex*/, bool last_eoln) const
 {
        docstring eol;
 
@@ -652,7 +663,7 @@ docstring InsetMathGrid::eolString(row_type row, bool fragile) const
        }
 
        // only add \\ if necessary
-       if (eol.empty() && row + 1 == nrows())
+       if (eol.empty() && row + 1 == nrows() && (nrows() == 1 || !last_eoln))
                return docstring();
 
        return (fragile ? "\\protect\\\\" : "\\\\") + eol;
@@ -677,17 +688,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)
@@ -788,7 +788,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;
@@ -975,20 +975,49 @@ 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");
 }
 
 
+// FIXME XHTML
+// We need to do something about alignment here.
+void InsetMathGrid::htmlize(HtmlStream & os, string attrib) const
+{
+       bool const havetable = nrows() > 1 || ncols() > 1;
+       if (!havetable) {
+               os << cell(index(0, 0));
+               return;
+       }
+       os << MTag("table", attrib);
+       for (row_type row = 0; row < nrows(); ++row) {
+               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("table");
+}
+
+
+void InsetMathGrid::htmlize(HtmlStream & os) const
+{
+       htmlize(os, "class='mathtable'");
+}
+
+
 void InsetMathGrid::write(WriteStream & os) const
 {
        write(os, 0, 0, nrows(), ncols());
@@ -1006,19 +1035,23 @@ void InsetMathGrid::write(WriteStream & os,
                // unless there are vertical lines
                col_type lastcol = 0;
                bool emptyline = true;
-               for (col_type col = beg_col; col < end_col; ++col)
-                       if (!cell(index(row, col)).empty()
-                                 || colinfo_[col + 1].lines_) {
+               bool last_eoln = true;
+               for (col_type col = beg_col; col < end_col; ++col) {
+                       bool const empty_cell = cell(index(row, col)).empty();
+                       if (!empty_cell)
+                               last_eoln = false;
+                       if (!empty_cell || colinfo_[col + 1].lines_) {
                                lastcol = col + 1;
                                emptyline = false;
                        }
+               }
                for (col_type col = beg_col; col < lastcol; ++col) {
                        os << cell(index(row, col));
                        if (os.pendingBrace())
                                ModeSpecifier specifier(os, TEXT_MODE);
                        os << eocString(col, lastcol);
                }
-               eol = eolString(row, os.fragile());
+               eol = eolString(row, os.fragile(), os.latex(), last_eoln);
                os << eol;
                // append newline only if line wasn't completely empty
                // and the formula is not written on a single line
@@ -1088,7 +1121,8 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
 
        Parse::flags parseflg = Parse::QUIET | Parse::USETEXT;
 
-       switch (cmd.action) {
+       FuncCode const act = cmd.action();
+       switch (act) {
 
        // insert file functions
        case LFUN_LINE_DELETE:
@@ -1146,18 +1180,23 @@ 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;
        }
 
-       case LFUN_TABULAR_FEATURE: {
+       case LFUN_INSET_MODIFY: {
                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")
@@ -1322,6 +1361,9 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
                                        cell(i).append(grid.cell(grid.index(r, c)));
                }
                cur.clearSelection(); // bug 393
+               // FIXME audit setBuffer calls
+               cur.inset().setBuffer(*buffer_);
+               cur.forceBufferUpdate();
                cur.finishUndo();
                break;
        }
@@ -1332,9 +1374,9 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
        case LFUN_WORD_BACKWARD:
        case LFUN_WORD_LEFT_SELECT:
        case LFUN_WORD_LEFT:
-               cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
-                               cmd.action == LFUN_WORD_LEFT_SELECT ||
-                               cmd.action == LFUN_LINE_BEGIN_SELECT);
+               cur.selHandle(act == LFUN_WORD_BACKWARD_SELECT ||
+                               act == LFUN_WORD_LEFT_SELECT ||
+                               act == LFUN_LINE_BEGIN_SELECT);
                cur.macroModeClose();
                if (cur.pos() != 0) {
                        cur.pos() = 0;
@@ -1356,9 +1398,9 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
        case LFUN_WORD_RIGHT:
        case LFUN_LINE_END_SELECT:
        case LFUN_LINE_END:
-               cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
-                               cmd.action == LFUN_WORD_RIGHT_SELECT ||
-                               cmd.action == LFUN_LINE_END_SELECT);
+               cur.selHandle(act == LFUN_WORD_FORWARD_SELECT ||
+                               act == LFUN_WORD_RIGHT_SELECT ||
+                               act == LFUN_LINE_END_SELECT);
                cur.macroModeClose();
                cur.clearTargetX();
                if (cur.pos() != cur.lastpos()) {
@@ -1384,9 +1426,23 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
 bool InsetMathGrid::getStatus(Cursor & cur, FuncRequest const & cmd,
                FuncStatus & status) const
 {
-       switch (cmd.action) {
-       case LFUN_TABULAR_FEATURE: {
-               string const s = cmd.getArg(0);
+       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;
+               }
+               if (&cur.inset() != this) {
+                       // Table actions requires that the cursor is _inside_ the
+                       // table.
+                       status.setEnabled(false);
+                       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")));
@@ -1467,7 +1523,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:
@@ -1476,8 +1532,9 @@ bool InsetMathGrid::getStatus(Cursor & cur, FuncRequest const & cmd,
                return true;
 
        default:
-               return InsetMathNest::getStatus(cur, cmd, status);
+               break;
        }
+       return InsetMathNest::getStatus(cur, cmd, status);
 }