]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/InsetMathGrid.cpp
fix off-by-two drawing error
[lyx.git] / src / mathed / InsetMathGrid.cpp
index cc542399c23aa01d5f53f57f12aa5bf72bdcfa57..21365b0f8bfe6a847aaed3b7ba8a56bc6f4f0201 100644 (file)
@@ -9,6 +9,7 @@
  */
 
 #include <config.h>
+#include <algorithm>
 
 #include "InsetMathGrid.h"
 
 #include "CutAndPaste.h"
 #include "FuncStatus.h"
 #include "Cursor.h"
-#include "support/debug.h"
 #include "FuncRequest.h"
-#include "support/gettext.h"
 
 #include "frontends/Clipboard.h"
 #include "frontends/Painter.h"
 
-#include "support/lstrings.h"
+#include "support/debug.h"
 #include "support/docstream.h"
+#include "support/gettext.h"
+#include "support/lstrings.h"
+
+#include "support/lassert.h"
 
 #include <sstream>
 
 using namespace std;
 using namespace lyx::support;
 
-namespace lyx {
 
-namespace {
+namespace lyx {
 
-docstring verboseHLine(int n)
+static docstring verboseHLine(int n)
 {
        docstring res;
        for (int i = 0; i < n; ++i)
@@ -51,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;
 }
 
-}
 
 
 //////////////////////////////////////////////////////////////
@@ -70,7 +71,6 @@ InsetMathGrid::CellInfo::CellInfo()
 
 
 
-
 //////////////////////////////////////////////////////////////
 
 
@@ -98,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),
@@ -141,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);
 }
 
 
@@ -172,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) {
@@ -254,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)
@@ -269,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()) {
@@ -281,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) {
@@ -303,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_;
 }
@@ -497,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);
 }
 
 
@@ -625,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));
@@ -813,7 +800,7 @@ 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;
 }
 
@@ -980,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_);
@@ -991,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
@@ -1106,7 +1098,7 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
                }
                break;
 
-       case LFUN_NEW_LINE: {
+       case LFUN_NEWLINE_INSERT: {
                cur.recordUndoInset();
                row_type const r = cur.row();
                addRow(r);
@@ -1134,17 +1126,17 @@ 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());
@@ -1221,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);
                        }
                }
@@ -1230,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);
                        }
                }
@@ -1253,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())
@@ -1354,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;
                }
@@ -1368,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;
                }
@@ -1377,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;
                }
@@ -1392,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?
@@ -1423,18 +1417,18 @@ bool InsetMathGrid::getStatus(Cursor & cur, FuncRequest const & cmd,
                        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: