]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/InsetMathGrid.cpp
* src/mathed/InsetMathHull.cpp:
[lyx.git] / src / mathed / InsetMathGrid.cpp
index 0bf0fb9c81d7cd84350cddbcfdaf03409814948b..21365b0f8bfe6a847aaed3b7ba8a56bc6f4f0201 100644 (file)
@@ -9,47 +9,40 @@
  */
 
 #include <config.h>
+#include <algorithm>
 
 #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 "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>
-
-
-namespace lyx {
+#include "support/lassert.h"
 
-using support::bformat;
-
-using std::endl;
-using std::max;
-using std::min;
-using std::swap;
+#include <sstream>
 
-using std::string;
-using std::istream;
-using std::istringstream;
-using std::vector;
+using namespace std;
+using namespace lyx::support;
 
 
-namespace {
+namespace lyx {
 
-docstring verboseHLine(int n)
+static docstring verboseHLine(int n)
 {
        docstring res;
        for (int i = 0; i < n; ++i)
@@ -60,14 +53,13 @@ docstring verboseHLine(int n)
 }
 
 
-int extractInt(istream & is)
+static int extractInt(istream & is)
 {
        int num = 1;
        is >> num;
        return (num == 0) ? 1 : num;
 }
 
-}
 
 
 //////////////////////////////////////////////////////////////
@@ -79,12 +71,11 @@ InsetMathGrid::CellInfo::CellInfo()
 
 
 
-
 //////////////////////////////////////////////////////////////
 
 
 InsetMathGrid::RowInfo::RowInfo()
-       : lines_(0), skip_(0), allow_pagebreak_(true)
+       : lines_(0), skip_(0), allow_newpage_(true)
 {}
 
 
@@ -107,19 +98,6 @@ InsetMathGrid::ColInfo::ColInfo()
 //////////////////////////////////////////////////////////////
 
 
-InsetMathGrid::InsetMathGrid(char v, docstring const & h)
-       : InsetMathNest(guessColumns(h)),
-         rowinfo_(2),
-         colinfo_(guessColumns(h) + 1),
-         cellinfo_(1 * guessColumns(h))
-{
-       setDefaults();
-       valign(v);
-       halign(h);
-       //lyxerr << "created grid with " << ncols() << " columns" << endl;
-}
-
-
 InsetMathGrid::InsetMathGrid()
        : InsetMathNest(1),
          rowinfo_(1 + 1),
@@ -150,8 +128,8 @@ InsetMathGrid::InsetMathGrid(col_type m, row_type n, char v, docstring const & h
                v_align_(v)
 {
        setDefaults();
-       valign(v);
-       halign(h);
+       setVerticalAlignment(v);
+       setHorizontalAlignments(h);
 }
 
 
@@ -181,7 +159,7 @@ void InsetMathGrid::setDefaults()
 }
 
 
-void InsetMathGrid::halign(docstring const & hh)
+void InsetMathGrid::setHorizontalAlignments(docstring const & hh)
 {
        col_type col = 0;
        for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it) {
@@ -222,13 +200,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();
@@ -241,13 +217,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;
@@ -267,7 +241,7 @@ void InsetMathGrid::halign(docstring const & hh)
 }
 
 
-InsetMathGrid::col_type InsetMathGrid::guessColumns(docstring const & hh) const
+InsetMathGrid::col_type InsetMathGrid::guessColumns(docstring const & hh)
 {
        col_type col = 0;
        for (docstring::const_iterator it = hh.begin(); it != hh.end(); ++it)
@@ -282,7 +256,7 @@ InsetMathGrid::col_type InsetMathGrid::guessColumns(docstring const & hh) const
 }
 
 
-void InsetMathGrid::halign(char h, col_type col)
+void InsetMathGrid::setHorizontalAlignment(char h, col_type col)
 {
        colinfo_[col].align_ = h;
        if (!colinfo_[col].special_.empty()) {
@@ -294,13 +268,13 @@ void InsetMathGrid::halign(char h, col_type col)
 }
 
 
-char InsetMathGrid::halign(col_type col) const
+char InsetMathGrid::horizontalAlignment(col_type col) const
 {
        return colinfo_[col].align_;
 }
 
 
-docstring InsetMathGrid::halign() const
+docstring InsetMathGrid::horizontalAlignments() const
 {
        docstring res;
        for (col_type col = 0; col < ncols(); ++col) {
@@ -316,13 +290,13 @@ docstring InsetMathGrid::halign() const
 }
 
 
-void InsetMathGrid::valign(char c)
+void InsetMathGrid::setVerticalAlignment(char c)
 {
        v_align_ = c;
 }
 
 
-char InsetMathGrid::valign() const
+char InsetMathGrid::verticalAlignment() const
 {
        return v_align_;
 }
@@ -510,7 +484,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);
 }
 
 
@@ -530,7 +504,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)
@@ -539,7 +513,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);
 }
@@ -638,7 +612,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));
@@ -651,7 +625,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
@@ -826,14 +800,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();
@@ -842,9 +816,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();
@@ -993,6 +967,7 @@ void InsetMathGrid::mathmlize(MathStream & os) const
 
 void InsetMathGrid::write(WriteStream & os) const
 {
+       MathEnsurer ensurer(os, false);
        docstring eol;
        for (row_type row = 0; row < nrows(); ++row) {
                os << verboseHLine(rowinfo_[row].lines_);
@@ -1004,8 +979,12 @@ void InsetMathGrid::write(WriteStream & os) const
                                lastcol = col + 1;
                                emptyline = false;
                        }
-               for (col_type col = 0; col < lastcol; ++col)
-                       os << cell(index(row, col)) << eocString(col, lastcol);
+               for (col_type col = 0; col < lastcol; ++col) {
+                       os << cell(index(row, col));
+                       if (os.pendingBrace())
+                               ModeSpecifier specifier(os, TEXT_MODE);
+                       os << eocString(col, lastcol);
+               }
                eol = eolString(row, emptyline, os.fragile());
                os << eol;
                // append newline only if line wasn't completely empty
@@ -1104,7 +1083,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;
@@ -1114,12 +1093,12 @@ 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: {
+       case LFUN_NEWLINE_INSERT: {
                cur.recordUndoInset();
                row_type const r = cur.row();
                addRow(r);
@@ -1136,7 +1115,7 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
                cur.pos() = cur.lastpos();
 
                //mathcursor->normalize();
-               //cmd = FuncRequest(LFUN_FINISHED_LEFT);
+               //cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
                break;
        }
 
@@ -1147,21 +1126,22 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
                string s;
                is >> s;
                if (s == "valign-top")
-                       valign('t');
+                       setVerticalAlignment('t');
                else if (s == "valign-middle")
-                       valign('c');
+                       setVerticalAlignment('c');
                else if (s == "valign-bottom")
-                       valign('b');
+                       setVerticalAlignment('b');
                else if (s == "align-left")
-                       halign('l', cur.col());
+                       setHorizontalAlignment('l', cur.col());
                else if (s == "align-right")
-                       halign('r', cur.col());
+                       setHorizontalAlignment('r', cur.col());
                else if (s == "align-center")
-                       halign('c', cur.col());
+                       setHorizontalAlignment('c', cur.col());
                else if (s == "append-row")
                        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())
@@ -1196,10 +1176,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)
@@ -1232,7 +1213,7 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
                        docstring & special = colinfo_[cur.col()].special_;
                        if (!special.empty()) {
                                docstring::size_type i = special.rfind('|');
-                               BOOST_ASSERT(i != docstring::npos);
+                               LASSERT(i != docstring::npos, /**/);
                                special.erase(i, 1);
                        }
                }
@@ -1241,7 +1222,7 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
                        docstring & special = colinfo_[cur.col()+1].special_;
                        if (!special.empty()) {
                                docstring::size_type i = special.find('|');
-                               BOOST_ASSERT(i != docstring::npos);
+                               LASSERT(i != docstring::npos, /**/);
                                special.erase(i, 1);
                        }
                }
@@ -1249,6 +1230,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;
        }
@@ -1263,7 +1245,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())
@@ -1306,7 +1288,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) {
@@ -1318,16 +1303,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();
@@ -1340,7 +1328,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;
@@ -1358,13 +1346,13 @@ bool InsetMathGrid::getStatus(Cursor & cur, FuncRequest const & cmd,
        case LFUN_TABULAR_FEATURE: {
                string const s = to_utf8(cmd.argument());
                if (nrows() <= 1 && (s == "delete-row" || s == "swap-row")) {
-                       status.enabled(false);
+                       status.setEnabled(false);
                        status.message(from_utf8(N_("Only one row")));
                        return true;
                }
                if (ncols() <= 1 &&
                    (s == "delete-column" || s == "swap-column")) {
-                       status.enabled(false);
+                       status.setEnabled(false);
                        status.message(from_utf8(N_("Only one column")));
                        return true;
                }
@@ -1372,7 +1360,7 @@ bool InsetMathGrid::getStatus(Cursor & cur, FuncRequest const & cmd,
                     s == "delete-hline-above") ||
                    (rowinfo_[cur.row() + 1].lines_ == 0 &&
                     s == "delete-hline-below")) {
-                       status.enabled(false);
+                       status.setEnabled(false);
                        status.message(from_utf8(N_("No hline to delete")));
                        return true;
                }
@@ -1381,7 +1369,7 @@ bool InsetMathGrid::getStatus(Cursor & cur, FuncRequest const & cmd,
                     s == "delete-vline-left") ||
                    (colinfo_[cur.col() + 1].lines_ == 0 &&
                     s == "delete-vline-right")) {
-                       status.enabled(false);
+                       status.setEnabled(false);
                        status.message(from_utf8(N_("No vline to delete")));
                        return true;
                }
@@ -1396,19 +1384,21 @@ bool InsetMathGrid::getStatus(Cursor & 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")
-                       status.enabled(true);
+                       status.setEnabled(true);
                else {
-                       status.enabled(false);
+                       status.setEnabled(false);
                        status.message(bformat(
                                from_utf8(N_("Unknown tabular feature '%1$s'")), lyx::from_ascii(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'));
+               char const ha = horizontalAlignment(cur.col());
+               char const va = verticalAlignment();
+               status.setOnOff((s == "align-left" && ha == 'l')
+                          || (s == "align-right"   && ha == 'r')
+                          || (s == "align-center"  && ha == 'c')
+                          || (s == "valign-top"    && va == 't')
+                          || (s == "valign-bottom" && va == 'b')
+                          || (s == "valign-middle" && va == 'm'));
 
 #if 0
                // FIXME: What did this code do?
@@ -1422,23 +1412,23 @@ 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;
                }
                status.setOnOff(cmd.argument()[0] == v_align_);
-               status.enabled(true);
+               status.setEnabled(true);
 #endif
                return true;
        }
 
        case LFUN_CELL_SPLIT:
-               status.enabled(true);
+               status.setEnabled(true);
                return true;
 
        case LFUN_CELL_BACKWARD:
        case LFUN_CELL_FORWARD:
-               status.enabled(true);
+               status.setEnabled(true);
                return true;
 
        default: