]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/InsetMathGrid.cpp
Change the interface to a paragraph's layout. We still store a LayoutPtr, but now...
[lyx.git] / src / mathed / InsetMathGrid.cpp
index 26319250ecd1600116095af274aa0324c1dbd262..11f08ad9914d7a9987896dc892d78c6969625121 100644 (file)
 #include <config.h>
 
 #include "InsetMathGrid.h"
+
 #include "MathData.h"
 #include "MathParser.h"
 #include "MathStream.h"
+#include "MetricsInfo.h"
 
 #include "BufferView.h"
 #include "CutAndPaste.h"
 #include "FuncStatus.h"
-#include "Color.h"
 #include "Cursor.h"
-#include "debug.h"
 #include "FuncRequest.h"
-#include "gettext.h"
-#include "Undo.h"
 
 #include "frontends/Clipboard.h"
 #include "frontends/Painter.h"
 
+#include "support/debug.h"
+#include "support/docstream.h"
+#include "support/gettext.h"
 #include "support/lstrings.h"
 
 #include <sstream>
 
+using namespace std;
+using namespace lyx::support;
 
 namespace lyx {
 
-using support::bformat;
-
-using std::endl;
-using std::max;
-using std::min;
-using std::swap;
-
-using std::string;
-using std::istream;
-using std::istringstream;
-using std::vector;
-
-
 namespace {
 
 docstring verboseHLine(int n)
@@ -85,7 +75,7 @@ InsetMathGrid::CellInfo::CellInfo()
 
 
 InsetMathGrid::RowInfo::RowInfo()
-       : lines_(0), skip_(0), allow_pagebreak_(true)
+       : lines_(0), skip_(0), allow_newpage_(true)
 {}
 
 
@@ -223,13 +213,11 @@ void InsetMathGrid::halign(docstring const & hh)
                        }
                        --it;
                        if (newcolumn) {
-                               colinfo_[col].lines_ = std::count(
+                               colinfo_[col].lines_ = count(
                                        colinfo_[col].special_.begin(),
                                        colinfo_[col].special_.end(), '|');
-                               LYXERR(Debug::MATHED)
-                                       << "special column separator: `"
-                                       << to_utf8(colinfo_[col].special_)
-                                       << '\'' << endl;
+                               LYXERR(Debug::MATHED, "special column separator: `"
+                                       << to_utf8(colinfo_[col].special_) << '\'');
                                ++col;
                                colinfo_[col].lines_ = 0;
                                colinfo_[col].special_.clear();
@@ -242,13 +230,11 @@ void InsetMathGrid::halign(docstring const & hh)
                        colinfo_[col].align_ = static_cast<char>(c);
                        if (!colinfo_[col].special_.empty()) {
                                colinfo_[col].special_ += c;
-                               colinfo_[col].lines_ = std::count(
+                               colinfo_[col].lines_ = count(
                                                colinfo_[col].special_.begin(),
                                                colinfo_[col].special_.end(), '|');
-                               LYXERR(Debug::MATHED)
-                                       << "special column separator: `"
-                                       << to_utf8(colinfo_[col].special_)
-                                       << '\'' << endl;
+                               LYXERR(Debug::MATHED, "special column separator: `"
+                                       << to_utf8(colinfo_[col].special_) << '\'');
                        }
                        ++col;
                        colinfo_[col].lines_ = 0;
@@ -511,7 +497,7 @@ void InsetMathGrid::metrics(MetricsInfo & mi, Dimension & dim) const
 
 void InsetMathGrid::draw(PainterInfo & pi, int x, int y) const
 {
-       drawWithMargin(pi, x, y, 0, 0);
+       drawWithMargin(pi, x, y, 1, 1);
 }
 
 
@@ -531,7 +517,7 @@ void InsetMathGrid::drawWithMargin(PainterInfo & pi, int x, int y,
                                - i * hlinesep() - hlinesep()/2 - rowsep()/2;
                        pi.pain.line(x + lmargin + 1, yy,
                                     x + dim.width() - rmargin - 1, yy,
-                                    Color::foreground);
+                                    Color_foreground);
                }
 
        for (col_type col = 0; col <= ncols(); ++col)
@@ -540,7 +526,7 @@ void InsetMathGrid::drawWithMargin(PainterInfo & pi, int x, int y,
                                - i * vlinesep() - vlinesep()/2 - colsep()/2;
                        pi.pain.line(xx, y - dim.ascent() + 1,
                                     xx, y + dim.descent() - 1,
-                                    Color::foreground);
+                                    Color_foreground);
                }
        drawMarkers2(pi, x, y);
 }
@@ -639,7 +625,7 @@ void InsetMathGrid::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
 }
 
 
-void InsetMathGrid::drawT(TextPainter & pain, int x, int y) const
+void InsetMathGrid::drawT(TextPainter & /*pain*/, int /*x*/, int /*y*/) const
 {
 //     for (idx_type idx = 0; idx < nargs(); ++idx)
 //             cell(idx).drawT(pain, x + cellXOffset(idx), y + cellYOffset(idx));
@@ -652,7 +638,7 @@ docstring InsetMathGrid::eolString(row_type row, bool emptyline, bool fragile) c
 
        if (!rowinfo_[row].crskip_.zero())
                eol += '[' + from_utf8(rowinfo_[row].crskip_.asLatexString()) + ']';
-       else if(!rowinfo_[row].allow_pagebreak_)
+       else if(!rowinfo_[row].allow_newpage_)
                eol += '*';
 
        // make sure an upcoming '[' does not break anything
@@ -743,9 +729,9 @@ void InsetMathGrid::addCol(col_type newcol)
 
        for (row_type row = 0; row < nr; ++row)
                for (col_type col = 0; col < nc; ++col) {
-                       new_cells[row * (nc + 1) + col + (col > newcol)]
+                       new_cells[row * (nc + 1) + col + (col >= newcol)]
                                = cells_[row * nc + col];
-                       new_cellinfo[row * (nc + 1) + col + (col > newcol)]
+                       new_cellinfo[row * (nc + 1) + col + (col >= newcol)]
                                = cellinfo_[row * nc + col];
                }
        swap(cells_, new_cells);
@@ -827,14 +813,14 @@ bool InsetMathGrid::idxUpDown(Cursor & cur, bool up) const
                        return false;
                cur.idx() += ncols();
        }
-       cur.pos() = cur.cell().x2pos(cur.x_target() - cur.cell().xo(cur.bv()));
+       cur.pos() = cur.cell().x2pos(&cur.bv(), cur.x_target() - cur.cell().xo(cur.bv()));
        return true;
 }
 
 
-bool InsetMathGrid::idxLeft(Cursor & cur) const
+bool InsetMathGrid::idxBackward(Cursor & cur) const
 {
-       // leave matrix if on the left hand edge
+       // leave matrix if at the front edge
        if (cur.col() == 0)
                return false;
        --cur.idx();
@@ -843,9 +829,9 @@ bool InsetMathGrid::idxLeft(Cursor & cur) const
 }
 
 
-bool InsetMathGrid::idxRight(Cursor & cur) const
+bool InsetMathGrid::idxForward(Cursor & cur) const
 {
-       // leave matrix if on the right hand edge
+       // leave matrix if at the back edge
        if (cur.col() + 1 == ncols())
                return false;
        ++cur.idx();
@@ -1081,7 +1067,7 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
                // multiple cells. Unfortunately this puts the cursor in front
                // of the inset after undo. This is (especilally for large
                // grids) annoying.
-               recordUndoInset(cur);
+               cur.recordUndoInset();
                //autocorrect_ = false;
                //macroModeClose();
                //if (selection_) {
@@ -1097,7 +1083,7 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
                break;
 
        case LFUN_CELL_SPLIT:
-               recordUndo(cur);
+               cur.recordUndo();
                splitCell(cur);
                break;
 
@@ -1105,7 +1091,7 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
                // See below.
                cur.selection() = false;
                if (!idxPrev(cur)) {
-                       cmd = FuncRequest(LFUN_FINISHED_LEFT);
+                       cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
                        cur.undispatched();
                }
                break;
@@ -1115,13 +1101,13 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
                // hard bound to LFUN_CELL_BACKWARD
                cur.selection() = false;
                if (!idxNext(cur)) {
-                       cmd = FuncRequest(LFUN_FINISHED_RIGHT);
+                       cmd = FuncRequest(LFUN_FINISHED_FORWARD);
                        cur.undispatched();
                }
                break;
 
-       case LFUN_BREAK_LINE: {
-               recordUndoInset(cur);
+       case LFUN_NEW_LINE: {
+               cur.recordUndoInset();
                row_type const r = cur.row();
                addRow(r);
 
@@ -1137,12 +1123,12 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
                cur.pos() = cur.lastpos();
 
                //mathcursor->normalize();
-               //cmd = FuncRequest(LFUN_FINISHED_LEFT);
+               //cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
                break;
        }
 
        case LFUN_TABULAR_FEATURE: {
-               recordUndoInset(cur);
+               cur.recordUndoInset();
                //lyxerr << "handling tabular-feature " << to_utf8(cmd.argument()) << endl;
                istringstream is(to_utf8(cmd.argument()));
                string s;
@@ -1163,6 +1149,7 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
                        for (int i = 0, n = extractInt(is); i < n; ++i)
                                addRow(cur.row());
                else if (s == "delete-row") {
+                       cur.clearSelection(); // bug 4323
                        for (int i = 0, n = extractInt(is); i < n; ++i) {
                                delRow(cur.row());
                                if (cur.idx() >= nargs())
@@ -1197,10 +1184,11 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
                        row_type const r = cur.row();
                        col_type const c = cur.col();
                        for (int i = 0, n = extractInt(is); i < n; ++i)
-                               addCol(cur.col());
+                               addCol(cur.col() + 1);
                        cur.idx() = index(r, c);
                }
                else if (s == "delete-column") {
+                       cur.clearSelection(); // bug 4323
                        row_type const r = cur.row();
                        col_type const c = cur.col();
                        for (int i = 0, n = extractInt(is); i < n; ++i)
@@ -1250,6 +1238,7 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
                        cur.undispatched();
                        break;
                }
+               // perhaps this should be FINISHED_BACKWARD -- just for clarity?
                lyxerr << "returning FINISHED_LEFT" << endl;
                break;
        }
@@ -1264,7 +1253,7 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
                        idocstringstream is(cmd.argument());
                        int n = 0;
                        is >> n;
-                       topaste = cap::getSelection(cur.buffer(), n);
+                       topaste = cap::selection(n);
                }
                InsetMathGrid grid(1, 1);
                if (!topaste.empty())
@@ -1272,12 +1261,12 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
 
                if (grid.nargs() == 1) {
                        // single cell/part of cell
-                       recordUndo(cur);
+                       cur.recordUndo();
                        cur.cell().insert(cur.pos(), grid.cell(0));
                        cur.pos() += grid.cell(0).size();
                } else {
                        // multiple cells
-                       recordUndoInset(cur);
+                       cur.recordUndoInset();
                        col_type const numcols =
                                min(grid.ncols(), ncols() - col(cur.idx()));
                        row_type const numrows =
@@ -1299,7 +1288,7 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
                                        cell(i).append(grid.cell(grid.index(r, c)));
                }
                cur.clearSelection(); // bug 393
-               finishUndo();
+               cur.finishUndo();
                break;
        }
 
@@ -1307,7 +1296,10 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
        case LFUN_LINE_BEGIN:
        case LFUN_WORD_BACKWARD_SELECT:
        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.macroModeClose();
                if (cur.pos() != 0) {
@@ -1319,16 +1311,19 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
                        cur.idx() = 0;
                        cur.pos() = 0;
                } else {
-                       cmd = FuncRequest(LFUN_FINISHED_LEFT);
+                       cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
                        cur.undispatched();
                }
                break;
 
        case LFUN_WORD_FORWARD_SELECT:
        case LFUN_WORD_FORWARD:
+       case LFUN_WORD_RIGHT_SELECT:
+       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.macroModeClose();
                cur.clearTargetX();
@@ -1341,7 +1336,7 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
                        cur.idx() = cur.lastidx();
                        cur.pos() = cur.lastpos();
                } else {
-                       cmd = FuncRequest(LFUN_FINISHED_RIGHT);
+                       cmd = FuncRequest(LFUN_FINISHED_FORWARD);
                        cur.undispatched();
                }
                break;
@@ -1423,7 +1418,7 @@ bool InsetMathGrid::getStatus(Cursor & cur, FuncRequest const & cmd,
                        status.enable(false);
                        break;
                }
-               if (!support::contains("tcb", cmd.argument()[0])) {
+               if (!contains("tcb", cmd.argument()[0])) {
                        status.enable(false);
                        break;
                }