]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetTabular.cpp
msvcUsing "using namespace std" with msvc10 makes also std::tr1::shared_ptr visible...
[lyx.git] / src / insets / InsetTabular.cpp
index 9321f8250a19118b18a1bb021131883a7e8772a7..c8ef61183d903de2f64372a27ded1d836b24b69b 100644 (file)
@@ -70,7 +70,6 @@
 using namespace std;
 using namespace lyx::support;
 
-using boost::shared_ptr;
 
 
 namespace lyx {
@@ -518,6 +517,7 @@ Tabular::CellData::CellData(Buffer * buf)
          multirow(Tabular::CELL_NORMAL),
          alignment(LYX_ALIGN_CENTER),
          valignment(LYX_VALIGN_TOP),
+         voffset(0),
          top_line(false),
          bottom_line(false),
          left_line(false),
@@ -537,6 +537,7 @@ Tabular::CellData::CellData(CellData const & cs)
          multirow(cs.multirow),
          alignment(cs.alignment),
          valignment(cs.valignment),
+         voffset(cs.voffset),
          top_line(cs.top_line),
          bottom_line(cs.bottom_line),
          left_line(cs.left_line),
@@ -563,6 +564,7 @@ void Tabular::CellData::swap(CellData & rhs)
        std::swap(multirow, rhs.multirow);
        std::swap(alignment, rhs.alignment);
        std::swap(valignment, rhs.valignment);
+       std::swap(voffset, rhs.voffset);
        std::swap(top_line, rhs.top_line);
        std::swap(bottom_line, rhs.bottom_line);
        std::swap(left_line, rhs.left_line);
@@ -696,8 +698,8 @@ void Tabular::deleteRow(row_type const row)
                // Care about multirow cells
                if (row + 1 < nrows() &&
                    cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW &&
-                   cell_info[row][c + 1].multirow == CELL_PART_OF_MULTIROW) {
-                               cell_info[row][c + 1].multirow = CELL_BEGIN_OF_MULTIROW;
+                   cell_info[row + 1][c].multirow == CELL_PART_OF_MULTIROW) {
+                               cell_info[row + 1][c].multirow = CELL_BEGIN_OF_MULTIROW;
                }
        }
        row_info.erase(row_info.begin() + row);
@@ -891,20 +893,17 @@ int Tabular::columnWidth(idx_type cell) const
 }
 
 
-int Tabular::rowHeight(idx_type cell) const
+int Tabular::cellHeight(idx_type cell) const
 {
        row_type const span = rowSpan(cell);
        row_type const row = cellRow(cell);
-       int h = rowAscent(row) + rowDescent(row);
-
+       int h = 0;
        for(row_type r = row; r < row + span ; ++r) {
-               if (r > row) {
-                       h += rowAscent(r);
-                       h += interRowSpace(r);
-               }
-               if (r < row + span - 1)
-                       h += rowDescent(r);
+               h += rowAscent(r) + rowDescent(r);
+               if (r != row + span - 1)
+                       h += interRowSpace(r + 1);
        }
+
        return h;
 }
 
@@ -1180,7 +1179,7 @@ LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
 Tabular::VAlignment
 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
 {
-       if (!onlycolumn && isMultiColumn(cell))
+       if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
                return cellInfo(cell).valignment;
        return column_info[cellColumn(cell)].valignment;
 }
@@ -1224,21 +1223,12 @@ int Tabular::textHOffset(idx_type cell) const
 
 int Tabular::textVOffset(idx_type cell) const
 {
-       int h = rowHeight(cell);
-       
-       int y = 0;
-       switch (getVAlignment(cell)) {
-          case LYX_VALIGN_TOP:
-                  break;
-          case LYX_VALIGN_MIDDLE:
-                  y += h/2;
-                  break;
-          case LYX_VALIGN_BOTTOM:
-                  y += h;
-                  break;
+       int voffset = cellInfo(cell).voffset;
+       if (isMultiRow(cell)) {
+               row_type const row = cellRow(cell);
+               voffset += (cellHeight(cell) - rowAscent(row) - rowDescent(row))/2; 
        }
-       
-       return y;
+       return voffset;
 }
 
 
@@ -1503,17 +1493,20 @@ Tabular::CellData & Tabular::cellInfo(idx_type cell) const
 }
 
 
-void Tabular::setMultiColumn(idx_type cell, idx_type number)
+Tabular::idx_type Tabular::setMultiColumn(idx_type cell, idx_type number)
 {
        idx_type const col = cellColumn(cell);
        idx_type const row = cellRow(cell);
        for (idx_type i = 0; i < number; ++i)
                unsetMultiRow(cellIndex(row, col + i));
 
+       // unsetting of multirow may have invalidated cell index
+       cell = cellIndex(row, col);
        CellData & cs = cellInfo(cell);
        cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
-       cs.alignment = column_info[cellColumn(cell)].alignment;
-       setRightLine(cell, rightLine(cell + number - 1));
+       cs.alignment = column_info[col].alignment;
+       if (col > 0)
+               setRightLine(cell, rightLine(cellIndex(row, col - 1)));
 
        for (idx_type i = 1; i < number; ++i) {
                CellData & cs1 = cellInfo(cell + i);
@@ -1522,6 +1515,7 @@ void Tabular::setMultiColumn(idx_type cell, idx_type number)
                cs1.inset->clear();
        }
        updateIndexes();
+       return cell;
 }
 
 
@@ -1532,24 +1526,25 @@ bool Tabular::isMultiRow(idx_type cell) const
 }
 
 
-void Tabular::setMultiRow(idx_type cell, idx_type number)
+Tabular::idx_type Tabular::setMultiRow(idx_type cell, idx_type number)
 {
        idx_type const col = cellColumn(cell);
        idx_type const row = cellRow(cell);
        for (idx_type i = 0; i < number; ++i)
                unsetMultiColumn(cellIndex(row + i, col));
 
+       // unsetting of multicol may have invalidated cell index
+       cell = cellIndex(row, col);
        CellData & cs = cellInfo(cell);
        cs.multirow = CELL_BEGIN_OF_MULTIROW;
-       // reset the vertical alignment to top because multirows cells
-       // cannot be vertically aligned (they can also only have one paragraph)
-       column_info[col].valignment = LYX_VALIGN_TOP;
-
-       // FIXME: the horizontal alignment can only be changed for
-       // the whole table, support for this needs to be implemented
-       // (assigning this to uwestoehr)
-       // until LyX supports this, the alignment is always left
-       column_info[col].alignment = LYX_ALIGN_LEFT;
+       cs.valignment = LYX_VALIGN_MIDDLE;
+       // FIXME: the horizontal alignment of multirow cells can only
+       // be changed for the whole table,
+       // support for this needs to be implemented but us a fileformat
+       // change (assigning this to uwestoehr)
+       // until LyX supports this, use the deault alignment of multirow
+       // cells: left
+       cs.alignment = LYX_ALIGN_LEFT; 
 
        // set the bottom row of the last selected cell
        setBottomLine(cell, bottomLine(cell + (number - 1)*ncols()));
@@ -1561,6 +1556,7 @@ void Tabular::setMultiRow(idx_type cell, idx_type number)
                cs1.inset->clear();
        }
        updateIndexes();
+       return cell;
 }
 
 
@@ -1605,6 +1601,8 @@ void Tabular::unsetMultiRow(idx_type cell)
        if (!isMultiRow(cell))
                return;
 
+       cellInfo(cell).valignment = LYX_VALIGN_TOP;
+       cellInfo(cell).alignment = LYX_ALIGN_CENTER;
        row_type const row = cellRow(cell);
        col_type const col = cellColumn(cell);
        row_type const span = rowSpan(cell);
@@ -2116,7 +2114,7 @@ int Tabular::TeXCellPreamble(odocstream & os, idx_type cell,
                else
                        // we need to set a default value
                        // needs to be discussed
-                       os << "2.0cm";
+                       os << "*";
                os << "}{";
                } // end if ismultirow
 
@@ -3056,7 +3054,7 @@ bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
        FuncStatus & status) const
 {
        bool enabled;
-       switch (cmd.action) {
+       switch (cmd.action()) {
        case LFUN_LAYOUT:
                enabled = !forcePlainLayout();
                break;
@@ -3209,8 +3207,8 @@ void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
        }
 
        for (row_type r = 0; r < tabular.nrows(); ++r) {
-               int maxAsc = 0;
-               int maxDesc = 0;
+               int maxasc = 0;
+               int maxdes = 0;
                for (col_type c = 0; c < tabular.ncols(); ++c) {
                        if (tabular.isPartOfMultiColumn(r, c)
                                || tabular.isPartOfMultiRow(r, c))
@@ -3219,29 +3217,44 @@ void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
                        idx_type const cell = tabular.cellIndex(r, c);
                        Dimension dim;
                        MetricsInfo m = mi;
-                       Length p_width;
-                       if (tabular.cell_info[r][c].multicolumn ==
-                               Tabular::CELL_BEGIN_OF_MULTICOLUMN)
-                               p_width = tabular.cellInfo(cell).p_width;
-                       else
-                               p_width = tabular.column_info[c].p_width;
+                       Length const p_width = tabular.getPWidth(cell);
                        if (!p_width.zero())
                                m.base.textwidth = p_width.inPixels(mi.base.textwidth);
                        tabular.cellInset(cell)->metrics(m, dim);
                        if (!p_width.zero())
                                dim.wid = m.base.textwidth;
                        tabular.setCellWidth(cell, dim.wid);
-                       maxAsc  = max(maxAsc, dim.asc);
-                       maxDesc = max(maxDesc, dim.des);
+
+                       // FIXME(?): do we need a second metrics call?
+                       TextMetrics const & tm = 
+                               mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
+                       // with LYX_VALIGN_BOTTOM the descent is relative to the last par
+                       // = descent of text in last par + TEXT_TO_INSET_OFFSET:
+                       int const lastpardes = tm.last().second->descent()
+                               + TEXT_TO_INSET_OFFSET;
+                       int offset = 0;
+                       switch (tabular.getVAlignment(cell)) { 
+                               case Tabular::LYX_VALIGN_TOP:
+                                       break; 
+                               case Tabular::LYX_VALIGN_MIDDLE:
+                                       offset = -(dim.des - lastpardes)/2; 
+                                       break; 
+                               case Tabular::LYX_VALIGN_BOTTOM:
+                                       offset = -(dim.des - lastpardes); 
+                                       break;
+                       }
+                       tabular.cell_info[r][c].voffset = offset;
+                       maxasc = max(maxasc, dim.asc - offset);
+                       maxdes = max(maxdes, dim.des + offset);
                }
                int const top_space = tabular.row_info[r].top_space_default ?
                        default_line_space :
                        tabular.row_info[r].top_space.inPixels(mi.base.textwidth);
-               tabular.setRowAscent(r, maxAsc + ADD_TO_HEIGHT + top_space);
+               tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
                int const bottom_space = tabular.row_info[r].bottom_space_default ?
                        default_line_space :
                        tabular.row_info[r].bottom_space.inPixels(mi.base.textwidth);
-               tabular.setRowDescent(r, maxDesc + ADD_TO_HEIGHT + bottom_space);
+               tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
        }
        tabular.updateColumnWidths();
        dim.asc = tabular.rowAscent(0);
@@ -3249,6 +3262,7 @@ void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
        dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
 }
 
+
 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col) 
        const
 {
@@ -3258,7 +3272,11 @@ bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
                        col_type cs, ce;
                        getSelection(cur, rs, re, cs, ce);
                        
-                       if (col >= cs && col <= ce && row >= rs && row <= re)
+                       idx_type const cell = tabular.cellIndex(row, col);
+                       col_type const cspan = tabular.columnSpan(cell);
+                       row_type const rspan = tabular.rowSpan(cell);
+                       if (col + cspan - 1 >= cs && col <= ce 
+                               && row + rspan - 1 >= rs && row <= re)
                                return true;
                } else 
                        if (col == tabular.cellColumn(cur.idx()) 
@@ -3312,7 +3330,7 @@ void InsetTabular::draw(PainterInfo & pi, int x, int y) const
                        int const cx = nx + tabular.textHOffset(idx);
                        int const cy = y  + tabular.textVOffset(idx);
                        // Cache the Inset position.
-                       bv->coordCache().insets().add(cell(idx).get(), cx, y);
+                       bv->coordCache().insets().add(cell(idx).get(), cx, cy);
                        cell(idx)->draw(pi, cx, cy);
                        drawCellLines(pi.pain, nx, y, r, idx, pi.change_);
                        nx += tabular.columnWidth(idx);
@@ -3320,8 +3338,8 @@ void InsetTabular::draw(PainterInfo & pi, int x, int y) const
                }
 
                if (r + 1 < tabular.nrows())
-                       y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
-                            + tabular.interRowSpace(r + 1);
+                       y += tabular.rowDescent(r) + tabular.rowAscent(r + 1) 
+                               + tabular.interRowSpace(r + 1);
        }
 }
 
@@ -3353,7 +3371,6 @@ void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
                tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
 
        if (cur.selIsMultiCell() || full_cell_selected) {
-               y -= tabular.rowAscent(0);
                for (row_type r = 0; r < tabular.nrows(); ++r) {
                        int xx = x;
                        for (col_type c = 0; c < tabular.ncols(); ++c) {
@@ -3367,9 +3384,10 @@ void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
                                        continue;
                                }
                                int const w = tabular.columnWidth(cell);
-                               int const h = tabular.rowHeight(cell);
+                               int const h = tabular.cellHeight(cell);
+                               int const yy = y - tabular.rowAscent(r);
                                if (isCellSelected(cur, r, c))
-                                       pi.pain.fillRectangle(xx, y, w, h, Color_selection);
+                                       pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
                                xx += w;
                        }
                        if (r + 1 < tabular.nrows())
@@ -3386,49 +3404,43 @@ void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
 
 
 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
-                                row_type row, idx_type cell, Change const & change) const
+                                                                row_type row, idx_type cell, Change const & change) const
 {
-       y = y - tabular.rowAscent(row);
+       y -= tabular.rowAscent(row);
        int const w = tabular.columnWidth(cell);
-       int const h = tabular.rowHeight(cell);
-       bool on_off = false;
-       Color col = Color_tabularline;
-       Color onoffcol = Color_tabularonoffline;
-
-       if (change.changed()) {
-               col = change.color();
-               onoffcol = change.color();
-       }
-
-       bool topalreadydrawn = row > 0 && !tabular.rowTopLine(row) 
-               && tabular.bottomLine(tabular.cellAbove(cell));
-
-       if (!topalreadydrawn) {
-               on_off = !tabular.topLine(cell);
-               pain.line(x, y, x + w, y,
-                       on_off ? onoffcol : col,
-                       on_off ? Painter::line_onoffdash : Painter::line_solid);
-       }
-       on_off = !tabular.bottomLine(cell);
+       int const h = tabular.cellHeight(cell);
+       Color linecolor = change.changed() ? change.color() : Color_tabularline;
+       Color gridcolor = change.changed() ? change.color() : Color_tabularonoffline;
+
+       // Top
+       bool drawline = tabular.topLine(cell)
+               || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
+       pain.line(x, y, x + w, y,
+               drawline ? linecolor : gridcolor,
+               drawline ? Painter::line_solid : Painter::line_onoffdash);
+
+       // Bottom
+       drawline = tabular.bottomLine(cell);
        pain.line(x, y + h, x + w, y + h,
-               on_off ? onoffcol : col,
-               on_off ? Painter::line_onoffdash : Painter::line_solid);
+               drawline ? linecolor : gridcolor,
+               drawline ? Painter::line_solid : Painter::line_onoffdash);
 
-       col_type const column = tabular.cellColumn(cell);
-       bool leftalreadydrawn = column > 0  && !tabular.leftLine(cell)
-               && tabular.rightLine(tabular.cellIndex(row, column - 1));
+       // Left
+       col_type const col = tabular.cellColumn(cell);
+       drawline = tabular.leftLine(cell)
+               || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
+       pain.line(x, y, x, y + h,
+               drawline ? linecolor : gridcolor,
+               drawline ? Painter::line_solid : Painter::line_onoffdash);
 
-       if (!leftalreadydrawn) {
-               on_off = !tabular.leftLine(cell);
-               pain.line(x, y, x, y + h,
-                       on_off ? onoffcol : col,
-                       on_off ? Painter::line_onoffdash : Painter::line_solid);
-       }
-       on_off = !tabular.rightLine(cell);
-       pain.line(x + w - tabular.interColumnSpace(cell), y,
-               x + w - tabular.interColumnSpace(cell), y + h,
-               on_off ? onoffcol : col,
-               on_off ? Painter::line_onoffdash : Painter::line_solid);
+       // Right
+       x -= tabular.interColumnSpace(cell);
+       drawline = tabular.rightLine(cell)
+                  || (col + 1 < tabular.ncols()
+                      && tabular.leftLine(tabular.cellIndex(row, col + 1)));
+       pain.line(x + w, y, x + w, y + h,
+               drawline ? linecolor : gridcolor,
+               drawline ? Painter::line_solid : Painter::line_onoffdash);
 }
 
 
@@ -3497,14 +3509,16 @@ void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
        CursorSlice sl = cur.top();
        Cursor & bvcur = cur.bv().cursor();
 
-       switch (cmd.action) {
+       FuncCode const act = cmd.action();
+       
+       switch (act) {
 
        case LFUN_MOUSE_PRESS: {
                //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
                // select row
-               if (cmd.x < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
-                       || cmd.x > xo(cur.bv()) + tabular.width()) {
-                       row_type r = rowFromY(cur, cmd.y);
+               if (cmd.x() < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
+                       || cmd.x() > xo(cur.bv()) + tabular.width()) {
+                       row_type r = rowFromY(cur, cmd.y());
                        cur.idx() = tabular.getFirstCellInRow(r);
                        cur.pos() = 0;
                        cur.resetAnchor();
@@ -3517,9 +3531,9 @@ void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
                }
                // select column
                int const y0 = yo(cur.bv()) - tabular.rowAscent(0);
-               if (cmd.y < y0 + ADD_TO_TABULAR_WIDTH 
-                       || cmd.y > y0 + tabular.height()) {
-                       col_type c = columnFromX(cur, cmd.x);
+               if (cmd.y() < y0 + ADD_TO_TABULAR_WIDTH 
+                       || cmd.y() > y0 + tabular.height()) {
+                       col_type c = columnFromX(cur, cmd.x());
                        cur.idx() = tabular.cellIndex(0, c);
                        cur.pos() = 0;
                        cur.resetAnchor();
@@ -3545,13 +3559,13 @@ void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
                //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
                if (cmd.button() == mouse_button::button1) {
                        // only accept motions to places not deeper nested than the real anchor
-                       if (!bvcur.anchor_.hasPart(cur)) {
+                       if (!bvcur.realAnchor().hasPart(cur)) {
                                cur.undispatched();
                                break;
                        }
                        // select (additional) row
                        if (rowselect_) {
-                               row_type r = rowFromY(cur, cmd.y);
+                               row_type r = rowFromY(cur, cmd.y());
                                cur.idx() = tabular.getLastCellInRow(r);
                                // we need to reset the cursor's pit and pos now, as the old ones
                                // may no longer be valid.
@@ -3563,7 +3577,7 @@ void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
                        }
                        // select (additional) column
                        if (colselect_) {
-                               col_type c = columnFromX(cur, cmd.x);
+                               col_type c = columnFromX(cur, cmd.x());
                                cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
                                // we need to reset the cursor's pit and pos now, as the old ones
                                // may no longer be valid.
@@ -3575,9 +3589,9 @@ void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
                        }
                        // only update if selection changes
                        if (bvcur.idx() == cur.idx() &&
-                               !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
+                               !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
                                cur.noUpdate();
-                       setCursorFromCoordinates(cur, cmd.x, cmd.y);
+                       setCursorFromCoordinates(cur, cmd.x(), cmd.y());
                        bvcur.setCursor(cur);
                        bvcur.setSelection(true);
                        // if this is a multicell selection, we just set the cursor to
@@ -3619,21 +3633,21 @@ void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
                EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
                FuncCode finish_lfun;
 
-               if (cmd.action == LFUN_CHAR_FORWARD 
-                               || cmd.action == LFUN_CHAR_FORWARD_SELECT) {
+               if (act == LFUN_CHAR_FORWARD 
+                               || act == LFUN_CHAR_FORWARD_SELECT) {
                        next_cell = true;
                        finish_lfun = LFUN_FINISHED_FORWARD;
                }
-               else if (cmd.action == LFUN_CHAR_BACKWARD
-                               || cmd.action == LFUN_CHAR_BACKWARD_SELECT) {
+               else if (act == LFUN_CHAR_BACKWARD
+                               || act == LFUN_CHAR_BACKWARD_SELECT) {
                        next_cell = false;
                        finish_lfun = LFUN_FINISHED_BACKWARD;
                }
                // LEFT or RIGHT commands --- the interpretation will depend on the 
                // table's direction.
                else {
-                       bool const right = cmd.action == LFUN_CHAR_RIGHT
-                               || cmd.action == LFUN_CHAR_RIGHT_SELECT;
+                       bool const right = act == LFUN_CHAR_RIGHT
+                               || act == LFUN_CHAR_RIGHT_SELECT;
                        next_cell = isRightToLeft(cur) != right;
                        
                        if (lyxrc.visual_cursor)
@@ -3642,10 +3656,10 @@ void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
                        finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
                }
 
-               bool const select = cmd.action == LFUN_CHAR_FORWARD_SELECT ||
-                   cmd.action == LFUN_CHAR_BACKWARD_SELECT ||
-                   cmd.action == LFUN_CHAR_RIGHT_SELECT ||
-                   cmd.action == LFUN_CHAR_LEFT_SELECT;
+               bool const select = act == LFUN_CHAR_FORWARD_SELECT ||
+                   act == LFUN_CHAR_BACKWARD_SELECT ||
+                   act == LFUN_CHAR_RIGHT_SELECT ||
+                   act == LFUN_CHAR_LEFT_SELECT;
 
                // If we have a multicell selection or we're 
                // not doing some LFUN_*_SELECT thing anyway...
@@ -3707,7 +3721,7 @@ void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
                        // if our Text didn't do anything to the cursor
                        // then we try to put the cursor into the cell below
                        // setting also the right targetX.
-                       cur.selHandle(cmd.action == LFUN_DOWN_SELECT);
+                       cur.selHandle(act == LFUN_DOWN_SELECT);
                        if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
                                cur.idx() = tabular.cellBelow(cur.idx());
                                cur.pit() = 0;
@@ -3740,7 +3754,7 @@ void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
                        // if our Text didn't do anything to the cursor
                        // then we try to put the cursor into the cell above
                        // setting also the right targetX.
-                       cur.selHandle(cmd.action == LFUN_UP_SELECT);
+                       cur.selHandle(act == LFUN_UP_SELECT);
                        if (tabular.cellRow(cur.idx()) != 0) {
                                cur.idx() = tabular.cellAbove(cur.idx());
                                cur.pit() = cur.lastpit();
@@ -3868,7 +3882,7 @@ void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
 
        case LFUN_CLIPBOARD_PASTE:
        case LFUN_PRIMARY_SELECTION_PASTE: {
-               docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
+               docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
                        theClipboard().getAsText() :
                        theSelection().get();
                if (clip.empty())
@@ -3969,35 +3983,28 @@ void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
        FuncStatus & status) const
 {
-       switch (cmd.action) {
+       switch (cmd.action()) {
        case LFUN_INSET_MODIFY: {
-               istringstream is(to_utf8(cmd.argument()));
-               string s;
-               is >> s;
-               if (insetCode(s) != TABULAR_CODE) {
-                       status.clear();
-                       status.setEnabled(false);
+               if (&cur.inset() != this || cmd.getArg(0) != "tabular") 
                        break;
-               }
-               is >> s;
+
+               string const s = cmd.getArg(1);
                // FIXME: We only check for the very first argument...
                int action = Tabular::LAST_ACTION;
                int i = 0;
                for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
-                       string const tmp = tabularFeature[i].feature;
-                       if (tmp == s.substr(0, tmp.length())) {
+                       if (tabularFeature[i].feature == s) {
                                action = tabularFeature[i].action;
                                break;
                        }
                }
                if (action == Tabular::LAST_ACTION) {
                        status.clear();
-                       status.unknown(true);
+                       status.setUnknown(true);
                        return true;
                }
 
-               string const argument
-                       = ltrim(s.substr(tabularFeature[i].feature.length()));
+               string const argument = cmd.getLongArg(2);
 
                row_type sel_row_start = 0;
                row_type sel_row_end = 0;
@@ -4287,6 +4294,7 @@ bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
        case LFUN_BRANCH_INSERT:
        case LFUN_PHANTOM_INSERT:
        case LFUN_WRAP_INSERT:
+       case LFUN_PREVIEW_INSERT:
        case LFUN_ERT_INSERT: {
                if (cur.selIsMultiCell()) {
                        status.setEnabled(false);
@@ -4447,14 +4455,10 @@ void InsetTabular::cursorPos(BufferView const & bv,
        int const col = tabular.cellColumn(sl.idx());
 
        // y offset     correction
-       for (int r = 0; r <= row; ++r) {
-               if (r != 0) {
-                       y += tabular.rowAscent(r);
-                       y += tabular.interRowSpace(r);
-               }
-               if (r != row)
-                       y += tabular.rowDescent(r);
-       }
+       for (int r = 0; r < row; ++r)
+               y += tabular.rowDescent(r) + tabular.rowAscent(r + 1) 
+                       + tabular.interRowSpace(r + 1);
+
        y += tabular.textVOffset(sl.idx());
 
        // x offset correction
@@ -4478,7 +4482,7 @@ int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
        row_type const row = tabular.cellRow(cell);
        int const ybeg = o.y_ - tabular.rowAscent(row)
                - tabular.interRowSpace(row);
-       int const yend = ybeg + tabular.rowHeight(cell);
+       int const yend = ybeg + tabular.cellHeight(cell);
 
        if (x < xbeg)
                xx = xbeg - x;
@@ -4850,7 +4854,7 @@ void InsetTabular::tabularFeatures(Cursor & cur,
                break;
 
        case Tabular::SET_SPECIAL_MULTIROW:
-               //FIXME: noting to do here?
+               // nothing to do
                break;
 
        case Tabular::APPEND_ROW:
@@ -4975,8 +4979,7 @@ void InsetTabular::tabularFeatures(Cursor & cur,
                idx_type const s_start = cur.selBegin().idx();
                row_type const col_start = tabular.cellColumn(s_start);
                row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
-               tabular.setMultiColumn(s_start, col_end - col_start + 1);
-               cur.idx() = s_start;
+               cur.idx() = tabular.setMultiColumn(s_start, col_end - col_start + 1);
                cur.pit() = 0;
                cur.pos() = 0;
                cur.setSelection(false);
@@ -4998,8 +5001,7 @@ void InsetTabular::tabularFeatures(Cursor & cur,
                idx_type const s_start = cur.selBegin().idx();
                row_type const row_start = tabular.cellRow(s_start);
                row_type const row_end = tabular.cellRow(cur.selEnd().idx());
-               tabular.setMultiRow(s_start, row_end - row_start + 1);
-               cur.idx() = s_start;
+               cur.idx() = tabular.setMultiRow(s_start, row_end - row_start + 1);
                cur.pit() = 0;
                cur.pos() = 0;
                cur.setSelection(false);