]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/InsetMathGrid.cpp
Do not throw exceptions here either. See r22806.
[lyx.git] / src / mathed / InsetMathGrid.cpp
index af85c890bec1a9aad363786441036115147d2ad5..19c9d8c54b79ccc11848f348ca7a76d24212dd51 100644 (file)
@@ -18,6 +18,7 @@
 #include "MathStream.h"
 #include "MetricsInfo.h"
 
+#include "Buffer.h"
 #include "BufferView.h"
 #include "CutAndPaste.h"
 #include "FuncStatus.h"
@@ -112,8 +113,8 @@ InsetMathGrid::ColInfo::ColInfo()
 //////////////////////////////////////////////////////////////
 
 
-InsetMathGrid::InsetMathGrid()
-       : InsetMathNest(1),
+InsetMathGrid::InsetMathGrid(Buffer * buf)
+       : InsetMathNest(buf, 1),
          rowinfo_(1 + 1),
                colinfo_(1 + 1),
                cellinfo_(1),
@@ -123,8 +124,8 @@ InsetMathGrid::InsetMathGrid()
 }
 
 
-InsetMathGrid::InsetMathGrid(col_type m, row_type n)
-       : InsetMathNest(m * n),
+InsetMathGrid::InsetMathGrid(Buffer * buf, col_type m, row_type n)
+       : InsetMathNest(buf, m * n),
          rowinfo_(n + 1),
                colinfo_(m + 1),
                cellinfo_(m * n),
@@ -134,8 +135,9 @@ InsetMathGrid::InsetMathGrid(col_type m, row_type n)
 }
 
 
-InsetMathGrid::InsetMathGrid(col_type m, row_type n, char v, docstring const & h)
-       : InsetMathNest(m * n),
+InsetMathGrid::InsetMathGrid(Buffer * buf, col_type m, row_type n, char v,
+       docstring const & h)
+       : InsetMathNest(buf, m * n),
          rowinfo_(n + 1),
          colinfo_(m + 1),
                cellinfo_(m * n),
@@ -633,7 +635,15 @@ void InsetMathGrid::drawT(TextPainter & /*pain*/, int /*x*/, int /*y*/) const
 }
 
 
-docstring InsetMathGrid::eolString(row_type row, bool emptyline, 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) const
 {
        docstring eol;
 
@@ -651,7 +661,7 @@ docstring InsetMathGrid::eolString(row_type row, bool emptyline, bool fragile) c
        }
 
        // only add \\ if necessary
-       if (eol.empty() && row + 1 == nrows() && (nrows() == 1 || !emptyline))
+       if (eol.empty() && row + 1 == nrows())
                return docstring();
 
        return (fragile ? "\\protect\\\\" : "\\\\") + eol;
@@ -968,14 +978,52 @@ void InsetMathGrid::normalize(NormalStream & os) const
 
 void InsetMathGrid::mathmlize(MathStream & os) const
 {
-       os << MTag("mtable");
+       bool const havetable = nrows() > 1 || ncols() > 1;
+       if (havetable)
+               os << MTag("mtable");
+       char const * const celltag = havetable ? "mtd" : "mrow";
        for (row_type row = 0; row < nrows(); ++row) {
-               os << MTag("mtr");
-               for (col_type col = 0; col < ncols(); ++col)
+               if (havetable)
+                       os << MTag("mtr");;
+               for (col_type col = 0; col < ncols(); ++col) {
+                       os << MTag(celltag);
                        os << cell(index(row, col));
-               os << ETag("mtr");
+                       os << ETag(celltag);
+               }
+               if (havetable)
+                       os << ETag("mtr");;
        }
-       os << ETag("mtable");
+       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'");
 }
 
 
@@ -992,11 +1040,13 @@ void InsetMathGrid::write(WriteStream & os,
        docstring eol;
        for (row_type row = beg_row; row < end_row; ++row) {
                os << verboseHLine(rowinfo_[row].lines_);
-               // don't write & and empty cells at end of line
+               // don't write & and empty cells at end of line,
+               // 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()) {
+                       if (!cell(index(row, col)).empty()
+                                 || colinfo_[col + 1].lines_) {
                                lastcol = col + 1;
                                emptyline = false;
                        }
@@ -1006,11 +1056,12 @@ void InsetMathGrid::write(WriteStream & os,
                                ModeSpecifier specifier(os, TEXT_MODE);
                        os << eocString(col, lastcol);
                }
-               eol = eolString(row, emptyline, os.fragile());
+               eol = eolString(row, os.fragile());
                os << eol;
                // append newline only if line wasn't completely empty
-               // and this was not the last line in the grid
-               if (!emptyline && row + 1 < end_row)
+               // and the formula is not written on a single line
+               bool const empty = emptyline && eol.empty();
+               if (!empty && nrows() > 1)
                        os << "\n";
        }
        // @TODO use end_row instead of nrows() ?
@@ -1075,7 +1126,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:
@@ -1139,12 +1191,17 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
                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")
@@ -1272,13 +1329,12 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
                        is >> n;
                        topaste = cap::selection(n);
                }
-               InsetMathGrid grid(1, 1);
+               InsetMathGrid grid(buffer_, 1, 1);
                if (!topaste.empty())
                        if ((topaste.size() == 1 && topaste.at(0) < 0x80)
                            || !mathed_parse_normal(grid, topaste, parseflg)) {
                                resetGrid(grid);
-                               mathed_parse_normal(grid, topaste,
-                                               parseflg | Parse::VERBATIM);
+                               mathed_parse_normal(grid, topaste, parseflg | Parse::VERBATIM);
                        }
 
                if (grid.nargs() == 1) {
@@ -1310,6 +1366,10 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
                                        cell(i).append(grid.cell(grid.index(r, c)));
                }
                cur.clearSelection(); // bug 393
+               // FIXME audit setBuffer/updateBuffer calls
+               cur.inset().setBuffer(*buffer_);
+               // FIXME audit setBuffer/updateBuffer calls
+               cur.buffer()->updateBuffer();
                cur.finishUndo();
                break;
        }
@@ -1320,9 +1380,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;
@@ -1344,9 +1404,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()) {
@@ -1372,9 +1432,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")));
@@ -1464,8 +1538,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);
 }