]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_gridinset.C
Andreas' patch to prevent crash on click on previewd inset
[lyx.git] / src / mathed / math_gridinset.C
index 8f50cbffe29e9912356e6bef7c6f081a83f3d731..c9fcfcc5141aad10451d4638a5405db61aec1113 100644 (file)
 #include "math_streamstr.h"
 
 #include "BufferView.h"
+#include "CutAndPaste.h"
 #include "FuncStatus.h"
 #include "LColor.h"
 #include "cursor.h"
 #include "debug.h"
 #include "funcrequest.h"
+#include "gettext.h"
+#include "undo.h"
 
 #include "frontends/Painter.h"
 
-#include "support/std_sstream.h"
-
 #include "insets/mailinset.h"
 
+#include "support/lstrings.h"
+
+#include <sstream>
+
+using lyx::support::bformat;
+
 using std::endl;
 using std::max;
 using std::min;
@@ -122,7 +129,7 @@ int MathGridInset::RowInfo::skipPixels() const
 
 
 MathGridInset::ColInfo::ColInfo()
-       : align_('c'), leftline_(false), rightline_(false), lines_(0)
+       : align_('c'), lines_(0)
 {}
 
 
@@ -184,7 +191,7 @@ MathGridInset::~MathGridInset()
 }
 
 
-auto_ptr<InsetBase> MathGridInset::clone() const
+auto_ptr<InsetBase> MathGridInset::doClone() const
 {
        return auto_ptr<InsetBase>(new MathGridInset(*this));
 }
@@ -213,11 +220,12 @@ void MathGridInset::halign(string const & hh)
 {
        col_type col = 0;
        for (string::const_iterator it = hh.begin(); it != hh.end(); ++it) {
-               if (col >= ncols())
-                       break;
                char c = *it;
                if (c == '|') {
                        colinfo_[col].lines_++;
+               } else if (col >= ncols()) {
+                       // Only '|' is allowed in the last dummy column
+                       break;
                } else if (c == 'c' || c == 'l' || c == 'r') {
                        colinfo_[col].align_ = c;
                        ++col;
@@ -470,22 +478,29 @@ void MathGridInset::metrics(MetricsInfo & mi, Dimension & dim) const
 
 
 void MathGridInset::draw(PainterInfo & pi, int x, int y) const
+{
+       drawWithMargin(pi, x, y, 0, 0);
+}
+
+void MathGridInset::drawWithMargin(PainterInfo & pi, int x, int y,
+       int lmargin, int rmargin) const
 {
        for (idx_type idx = 0; idx < nargs(); ++idx)
-               cell(idx).draw(pi, x + cellXOffset(idx), y + cellYOffset(idx));
+               cell(idx).draw(pi, x + lmargin + cellXOffset(idx),
+                       y + cellYOffset(idx));
 
        for (row_type row = 0; row <= nrows(); ++row)
-               for (int i = 0; i < rowinfo_[row].lines_; ++i) {
+               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 + 1, yy,
-                                    x + dim_.width() - 1, yy,
+                       pi.pain.line(x + lmargin + 1, yy,
+                                    x + dim_.width() - rmargin - 1, yy,
                                     LColor::foreground);
                }
 
        for (col_type col = 0; col <= ncols(); ++col)
-               for (int i = 0; i < colinfo_[col].lines_; ++i) {
-                       int xx = x + colinfo_[col].offset_
+               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,
@@ -761,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();
        }
@@ -1006,7 +1021,7 @@ void MathGridInset::splitCell(LCursor & cur)
 }
 
 
-void MathGridInset::priv_dispatch(LCursor & cur, FuncRequest & cmd)
+void MathGridInset::doDispatch(LCursor & cur, FuncRequest & cmd)
 {
        //lyxerr << "*** MathGridInset: request: " << cmd << endl;
        switch (cmd.action) {
@@ -1016,7 +1031,7 @@ void MathGridInset::priv_dispatch(LCursor & cur, FuncRequest & cmd)
                //      GridInsetMailer(*this).showDialog();
                //      return DispatchResult(true, true);
                //}
-               MathNestInset::priv_dispatch(cur, cmd);
+               MathNestInset::doDispatch(cur, cmd);
                break;
 
        case LFUN_INSET_DIALOG_UPDATE:
@@ -1025,6 +1040,12 @@ void MathGridInset::priv_dispatch(LCursor & cur, FuncRequest & cmd)
 
        // insert file functions
        case LFUN_DELETE_LINE_FORWARD:
+               // 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_) {
@@ -1040,12 +1061,31 @@ void MathGridInset::priv_dispatch(LCursor & cur, FuncRequest & cmd)
                break;
 
        case LFUN_CELL_SPLIT:
-               ////recordUndo(cur, Undo::ATOMIC);
+               recordUndo(cur);
                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, Undo::INSERT);
+               recordUndoInset(cur);
                row_type const r = cur.row();
                addRow(r);
 
@@ -1058,14 +1098,15 @@ void MathGridInset::priv_dispatch(LCursor & cur, FuncRequest & cmd)
                swap(cell(cur.idx()), cell(cur.idx() + ncols() - 1));
                if (cur.idx() > 0)
                        --cur.idx();
-               cur.idx() = cur.lastpos();
+               cur.pos() = cur.lastpos();
 
                //mathcursor->normalize();
-               cmd = FuncRequest(LFUN_FINISHED_LEFT);
+               //cmd = FuncRequest(LFUN_FINISHED_LEFT);
                break;
        }
 
        case LFUN_TABULAR_FEATURE: {
+               recordUndoInset(cur);
                //lyxerr << "handling tabular-feature " << cmd.argument << endl;
                istringstream is(cmd.argument);
                string s;
@@ -1077,11 +1118,11 @@ void MathGridInset::priv_dispatch(LCursor & cur, FuncRequest & cmd)
                else if (s == "valign-bottom")
                        valign('b');
                else if (s == "align-left")
-                       halign('l', col(cur.idx()));
+                       halign('l', cur.col());
                else if (s == "align-right")
-                       halign('r', col(cur.idx()));
+                       halign('r', cur.col());
                else if (s == "align-center")
-                       halign('c', col(cur.idx()));
+                       halign('c', cur.col());
                else if (s == "append-row")
                        for (int i = 0, n = extractInt(is); i < n; ++i)
                                addRow(cur.row());
@@ -1091,31 +1132,61 @@ void MathGridInset::priv_dispatch(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());
-               else if (s == "append-column")
-                       for (int i = 0, n = extractInt(is); i < n; ++i) {
-                               row_type r = cur.row();
-                               col_type c = col(cur.idx());
-                               addCol(c);
-                               cur.idx() = index(r, c);
-                       }
-               else if (s == "delete-column")
-                       for (int i = 0, n = extractInt(is); i < n; ++i) {
-                               row_type r = cur.row();
-                               col_type c = col(cur.idx());
+                       // 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")
+                       rowinfo_[cur.row()+1].lines_++;
+               else if (s == "delete-hline-above")
+                       rowinfo_[cur.row()].lines_--;
+               else if (s == "delete-hline-below")
+                       rowinfo_[cur.row()+1].lines_--;
+               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")
-                       copyCol(col(cur.idx()));
-               else if (s == "swap-column")
-                       swapCol(col(cur.idx()));
+                       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());
+                       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")
+                       colinfo_[cur.col()+1].lines_++;
+               else if (s == "delete-vline-left")
+                       colinfo_[cur.col()].lines_--;
+               else if (s == "delete-vline-right")
+                       colinfo_[cur.col()+1].lines_--;
                else {
                        cur.undispatched();
                        break;
@@ -1125,15 +1196,20 @@ void MathGridInset::priv_dispatch(LCursor & cur, FuncRequest & cmd)
        }
 
        case LFUN_PASTE: {
-               //lyxerr << "pasting '" << cmd.argument << "'" << endl;
+               lyxerr << "MathGridInset: PASTE: " << cmd << std::endl;
+               istringstream is(cmd.argument);
+               int n = 0;
+               is >> n;
                MathGridInset grid(1, 1);
-               mathed_parse_normal(grid, cmd.argument);
+               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 =
@@ -1163,12 +1239,18 @@ void MathGridInset::priv_dispatch(LCursor & cur, FuncRequest & cmd)
        case LFUN_WORDLEFT:
                cur.selHandle(cmd.action == LFUN_WORDLEFTSEL || cmd.action == LFUN_HOMESEL);
                cur.macroModeClose();
-               if (cur.pos() != 0)
+               if (cur.pos() != 0) {
+                       cur.pos() = 0;
+               } else if (cur.idx() % cur.ncols() != 0) {
+                       cur.idx() -= cur.idx() % cur.ncols();
                        cur.pos() = 0;
-               else if (cur.idx() != 0)
+               } else if (cur.idx() != 0) {
                        cur.idx() = 0;
-               else
+                       cur.pos() = 0;
+               } else {
                        cmd = FuncRequest(LFUN_FINISHED_LEFT);
+                       cur.undispatched();
+               }
                break;
 
        case LFUN_WORDRIGHTSEL:
@@ -1178,47 +1260,117 @@ void MathGridInset::priv_dispatch(LCursor & cur, FuncRequest & cmd)
                cur.selHandle(cmd.action == LFUN_WORDRIGHTSEL || cmd.action == LFUN_ENDSEL);
                cur.macroModeClose();
                cur.clearTargetX();
-               if (cur.pos() != cur.lastpos())
+               if (cur.pos() != cur.lastpos()) {
                        cur.pos() = cur.lastpos();
-               else if (cur.idx() != cur.lastidx())
+               } else if ((cur.idx() + 1) % cur.ncols() != 0) {
+                       cur.idx() += cur.ncols() - 1 - cur.idx() % cur.ncols();
+                       cur.pos() = cur.lastpos();
+               } else if (cur.idx() != cur.lastidx()) {
                        cur.idx() = cur.lastidx();
-               else
+                       cur.pos() = cur.lastpos();
+               } else {
                        cmd = FuncRequest(LFUN_FINISHED_RIGHT);
+                       cur.undispatched();
+               }
                break;
 
        default:
-               MathNestInset::priv_dispatch(cur, cmd);
+               MathNestInset::doDispatch(cur, cmd);
        }
 }
 
 
 bool MathGridInset::getStatus(LCursor & cur, FuncRequest const & cmd,
-               FuncStatus & flag) const
+               FuncStatus & status) const
 {
-       bool ret = true;
        switch (cmd.action) {
-       case LFUN_TABULAR_FEATURE:
+       case LFUN_TABULAR_FEATURE: {
+               string const s = cmd.argument;
+               if (nrows() <= 1 && (s == "delete-row" || s == "swap-row")) {
+                       status.enabled(false);
+                       status.message(N_("Only one row"));
+                       return true;
+               }
+               if (ncols() <= 1 &&
+                   (s == "delete-column" || s == "swap-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")) {
+                       status.enabled(false);
+                       status.message(N_("No hline to delete"));
+                       return true;
+               }
+
+               if ((colinfo_[cur.col()].lines_ == 0 &&
+                    s == "delete-vline-left") ||
+                   (colinfo_[cur.col() + 1].lines_ == 0 &&
+                    s == "delete-vline-right")) {
+                       status.enabled(false);
+                       status.message(N_("No vline to delete"));
+                       return true;
+               }
+               if (s == "valign-top" || s == "valign-middle" ||
+                   s == "valign-bottom" || s == "align-left" ||
+                   s == "align-right" || s == "align-center" ||
+                   s == "append-row" || s == "delete-row" ||
+                   s == "copy-row" || s == "swap-row" ||
+                   s == "add-hline-above" || s == "add-hline-below" ||
+                   s == "delete-hline-above" || s == "delete-hline-below" ||
+                   s == "append-column" || s == "delete-column" ||
+                   s == "copy-column" || s == "swap-column" ||
+                   s == "add-vline-left" || s == "add-vline-right" ||
+                   s == "delete-vline-left" || s == "delete-vline-right")
+                       status.enabled(true);
+               else {
+                       status.enabled(false);
+                       status.message(bformat(
+                               N_("Unknown tabular feature '%1$s'"), s));
+               }
+
+               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'
+                          || s == "valign-bottom" && valign() == 'b'
+                          || s == "valign-middle" && valign() == 'm');
+
 #if 0
+               // FIXME: What did this code do?
+               // 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 (!contains("tcb", cmd.argument[0])) {
-                       flag.enable(false);
+               if (!lyx::support::contains("tcb", cmd.argument[0])) {
+                       status.enable(false);
                        break;
                }
-               flag.setOnOff(cmd.argument[0] == v_align_);
+               status.setOnOff(cmd.argument[0] == v_align_);
+               status.enabled(true);
 #endif
-               flag.enabled(true);
-               break;
+               return true;
+       }
+
+       case LFUN_CELL_SPLIT:
+               status.enabled(true);
+               return true;
+
+       case LFUN_CELL_BACKWARD:
+       case LFUN_CELL_FORWARD:
+               status.enabled(true);
+               return true;
+
        default:
-               ret = MathNestInset::getStatus(cur, cmd, flag);
-               break;
+               return MathNestInset::getStatus(cur, cmd, status);
        }
-       return ret;
 }