]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_gridinset.C
Fix event loop to no longer eat CPU
[lyx.git] / src / mathed / math_gridinset.C
index 371836b2734d16df3ecffdbe5ab8522eb3ae0f5b..3ca1497450a44a34f746432e3fd3a88a59657721 100644 (file)
@@ -606,7 +606,7 @@ void MathGridInset::drawT(TextPainter & pain, int x, int y) const
 }
 
 
-string MathGridInset::eolString(row_type row, bool fragile) const
+string MathGridInset::eolString(row_type row, bool emptyline, bool fragile) const
 {
        string eol;
 
@@ -622,7 +622,7 @@ string MathGridInset::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 || !emptyline))
                return string();
 
        return (fragile ? "\\protect\\\\" : "\\\\") + eol;
@@ -776,11 +776,11 @@ int MathGridInset::cellYOffset(idx_type idx) const
 bool MathGridInset::idxUpDown(LCursor & cur, bool up) const
 {
        if (up) {
-               if (cur.idx() < ncols())
+               if (cur.row() == 0)
                        return false;
                cur.idx() -= ncols();
        } else {
-               if (cur.idx() >= ncols() * (nrows() - 1))
+               if (cur.row() + 1 >= nrows())
                        return false;
                cur.idx() += ncols();
        }
@@ -951,6 +951,7 @@ void MathGridInset::mathmlize(MathMLStream & os) const
 
 void MathGridInset::write(WriteStream & os) const
 {
+       string eol;
        for (row_type row = 0; row < nrows(); ++row) {
                os << verboseHLine(rowinfo_[row].lines_);
                // don't write & and empty cells at end of line
@@ -963,17 +964,21 @@ void MathGridInset::write(WriteStream & os) const
                        }
                for (col_type col = 0; col < lastcol; ++col)
                        os << cell(index(row, col)) << eocString(col, lastcol);
-               os << eolString(row, os.fragile());
+               eol = eolString(row, emptyline, 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 < nrows())
                        os << "\n";
        }
        string const s = verboseHLine(rowinfo_[nrows()].lines_);
-       if (!s.empty() && s != " ") {
-               if (os.fragile())
-                       os << "\\protect";
-               os << "\\\\" << s;
+       if (!s.empty()) {
+               if (eol.empty()) {
+                       if (os.fragile())
+                               os << "\\protect";
+                       os << "\\\\";
+               }
+               os << s;
        }
 }
 
@@ -1040,7 +1045,12 @@ void MathGridInset::doDispatch(LCursor & cur, FuncRequest & cmd)
 
        // insert file functions
        case LFUN_DELETE_LINE_FORWARD:
-               recordUndo(cur);
+               // FIXME: We use recordUndoInset when a change reflects more
+               // than one cell, because recordUndo does not work for
+               // multiple cells. Unfortunately this puts the cursor in front
+               // of the inset after undo. This is (especilally for large
+               // grids) annoying.
+               recordUndoInset(cur);
                //autocorrect_ = false;
                //macroModeClose();
                //if (selection_) {
@@ -1060,8 +1070,27 @@ void MathGridInset::doDispatch(LCursor & cur, FuncRequest & cmd)
                splitCell(cur);
                break;
 
+       case LFUN_CELL_BACKWARD:
+               // See below.
+               cur.selection() = false;
+               if (!idxPrev(cur)) {
+                       cmd = FuncRequest(LFUN_FINISHED_LEFT);
+                       cur.undispatched();
+               }
+               break;
+       
+       case LFUN_CELL_FORWARD:
+               // Can't handle selection by additional 'shift' as this is
+               // hard bound to LFUN_CELL_BACKWARD
+               cur.selection() = false;
+               if (!idxNext(cur)) {
+                       cmd = FuncRequest(LFUN_FINISHED_RIGHT);
+                       cur.undispatched();
+               }
+               break;
+
        case LFUN_BREAKLINE: {
-               recordUndo(cur);
+               recordUndoInset(cur);
                row_type const r = cur.row();
                addRow(r);
 
@@ -1082,7 +1111,7 @@ void MathGridInset::doDispatch(LCursor & cur, FuncRequest & cmd)
        }
 
        case LFUN_TABULAR_FEATURE: {
-               recordUndo(cur);
+               recordUndoInset(cur);
                //lyxerr << "handling tabular-feature " << cmd.argument << endl;
                istringstream is(cmd.argument);
                string s;
@@ -1108,11 +1137,21 @@ void MathGridInset::doDispatch(LCursor & cur, FuncRequest & cmd)
                                if (cur.idx() > nargs())
                                        cur.idx() -= ncols();
                        }
-               else if (s == "copy-row")
+               else if (s == "copy-row") {
+                       // Here (as later) we save the cursor col/row 
+                       // in order to restore it after operation. 
+                       row_type const r = cur.row();
+                       col_type const c = cur.col();
                        for (int i = 0, n = extractInt(is); i < n; ++i)
                                copyRow(cur.row());
-               else if (s == "swap-row")
+                       cur.idx() = index(r, c);
+               }
+               else if (s == "swap-row") {
                        swapRow(cur.row());
+                       // Trick to suppress same-idx-means-different-cell 
+                       // assertion crash:
+                       cur.pos() = 0; 
+               }
                else if (s == "add-hline-above")
                        rowinfo_[cur.row()].lines_++;
                else if (s == "add-hline-below")
@@ -1121,26 +1160,30 @@ void MathGridInset::doDispatch(LCursor & cur, FuncRequest & cmd)
                        rowinfo_[cur.row()].lines_--;
                else if (s == "delete-hline-below")
                        rowinfo_[cur.row()+1].lines_--;
-               else if (s == "append-column")
-                       for (int i = 0, n = extractInt(is); i < n; ++i) {
-                               row_type const r = cur.row();
-                               col_type const c = cur.col();
-                               addCol(c);
-                               cur.idx() = index(r, c);
-                       }
-               else if (s == "delete-column")
-                       for (int i = 0, n = extractInt(is); i < n; ++i) {
-                               row_type const r = cur.row();
-                               col_type const c = cur.col();
+               else if (s == "append-column") {
+                       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());
+                       cur.idx() = index(r, c);
+               }
+               else if (s == "delete-column") {
+                       row_type const r = cur.row();
+                       col_type const c = cur.col();
+                       for (int i = 0, n = extractInt(is); i < n; ++i)
                                delCol(col(cur.idx()));
-                               cur.idx() = index(r, c);
-                               if (cur.idx() > nargs())
-                                       cur.idx() -= ncols();
-                       }
-               else if (s == "copy-column")
+                       cur.idx() = index(r, min(c, cur.ncols() - 1));
+               }
+               else if (s == "copy-column") {
+                       row_type const r = cur.row();
+                       col_type const c = cur.col();
                        copyCol(cur.col());
-               else if (s == "swap-column")
+                       cur.idx() = index(r, c);
+               }
+               else if (s == "swap-column") {
                        swapCol(cur.col());
+                       cur.pos() = 0; // trick, see above
+               }
                else if (s == "add-vline-left")
                        colinfo_[cur.col()].lines_++;
                else if (s == "add-vline-right")
@@ -1158,8 +1201,8 @@ void MathGridInset::doDispatch(LCursor & cur, FuncRequest & cmd)
        }
 
        case LFUN_PASTE: {
-               recordUndo(cur);
-               lyxerr << "MathGridInset: PASTE: " << cmd << std::endl;
+               cur.message(_("Paste"));
+               lyx::cap::replaceSelection(cur);
                istringstream is(cmd.argument);
                int n = 0;
                is >> n;
@@ -1167,10 +1210,12 @@ void MathGridInset::doDispatch(LCursor & cur, FuncRequest & cmd)
                mathed_parse_normal(grid, lyx::cap::getSelection(cur.buffer(), n));
                if (grid.nargs() == 1) {
                        // single cell/part of cell
+                       recordUndo(cur);
                        cur.cell().insert(cur.pos(), grid.cell(0));
                        cur.pos() += grid.cell(0).size();
                } else {
                        // multiple cells
+                       recordUndoInset(cur);
                        col_type const numcols =
                                min(grid.ncols(), ncols() - col(cur.idx()));
                        row_type const numrows =
@@ -1191,6 +1236,9 @@ void MathGridInset::doDispatch(LCursor & cur, FuncRequest & cmd)
                                for (col_type c = 0; c < grid.ncols(); ++c)
                                        cell(i).append(grid.cell(grid.index(r, c)));
                }
+               cur.clearSelection(); // bug 393
+               cur.bv().switchKeyMap();
+               finishUndo();
                break;
        }
 
@@ -1242,28 +1290,28 @@ void MathGridInset::doDispatch(LCursor & cur, FuncRequest & cmd)
 
 
 bool MathGridInset::getStatus(LCursor & cur, FuncRequest const & cmd,
-               FuncStatus & flag) const
+               FuncStatus & status) const
 {
        switch (cmd.action) {
        case LFUN_TABULAR_FEATURE: {
                string const s = cmd.argument;
                if (nrows() <= 1 && (s == "delete-row" || s == "swap-row")) {
-                       flag.enabled(false);
-                       flag.message(N_("Only one row"));
+                       status.enabled(false);
+                       status.message(N_("Only one row"));
                        return true;
                }
                if (ncols() <= 1 &&
                    (s == "delete-column" || s == "swap-column")) {
-                       flag.enabled(false);
-                       flag.message(N_("Only one column"));
+                       status.enabled(false);
+                       status.message(N_("Only one column"));
                        return true;
                }
                if ((rowinfo_[cur.row()].lines_ == 0 &&
                     s == "delete-hline-above") ||
                    (rowinfo_[cur.row() + 1].lines_ == 0 &&
                     s == "delete-hline-below")) {
-                       flag.enabled(false);
-                       flag.message(N_("No hline to delete"));
+                       status.enabled(false);
+                       status.message(N_("No hline to delete"));
                        return true;
                }
 
@@ -1271,8 +1319,8 @@ bool MathGridInset::getStatus(LCursor & cur, FuncRequest const & cmd,
                     s == "delete-vline-left") ||
                    (colinfo_[cur.col() + 1].lines_ == 0 &&
                     s == "delete-vline-right")) {
-                       flag.enabled(false);
-                       flag.message(N_("No vline to delete"));
+                       status.enabled(false);
+                       status.message(N_("No vline to delete"));
                        return true;
                }
                if (s == "valign-top" || s == "valign-middle" ||
@@ -1286,14 +1334,14 @@ bool MathGridInset::getStatus(LCursor & cur, FuncRequest const & cmd,
                    s == "copy-column" || s == "swap-column" ||
                    s == "add-vline-left" || s == "add-vline-right" ||
                    s == "delete-vline-left" || s == "delete-vline-right")
-                       flag.enabled(true);
+                       status.enabled(true);
                else {
-                       flag.enabled(false);
-                       flag.message(bformat(
+                       status.enabled(false);
+                       status.message(bformat(
                                N_("Unknown tabular feature '%1$s'"), s));
                }
 
-               flag.setOnOff(s == "align-left"    && halign(cur.col()) == 'l'
+               status.setOnOff(s == "align-left"    && halign(cur.col()) == 'l'
                           || s == "align-right"   && halign(cur.col()) == 'r'
                           || s == "align-center"  && halign(cur.col()) == 'c'
                           || s == "valign-top"    && valign() == 't'
@@ -1302,29 +1350,36 @@ bool MathGridInset::getStatus(LCursor & cur, FuncRequest const & cmd,
 
 #if 0
                // FIXME: What did this code do?
-               // Please check wether it is still needed!
+               // Please check whether it is still needed!
                // should be more precise
                if (v_align_ == '\0') {
-                       flag.enable(true);
+                       status.enable(true);
                        break;
                }
                if (cmd.argument.empty()) {
-                       flag.enable(false);
+                       status.enable(false);
                        break;
                }
                if (!lyx::support::contains("tcb", cmd.argument[0])) {
-                       flag.enable(false);
+                       status.enable(false);
                        break;
                }
-               flag.setOnOff(cmd.argument[0] == v_align_);
-               flag.enabled(true);
+               status.setOnOff(cmd.argument[0] == v_align_);
+               status.enabled(true);
 #endif
                return true;
        }
+
        case LFUN_CELL_SPLIT:
-               flag.enabled(true);
+               status.enabled(true);
+               return true;
+
+       case LFUN_CELL_BACKWARD:
+       case LFUN_CELL_FORWARD:
+               status.enabled(true);
                return true;
+
        default:
-               return MathNestInset::getStatus(cur, cmd, flag);
+               return MathNestInset::getStatus(cur, cmd, status);
        }
 }