]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetTabular.cpp
InsetInfo: enable inset dissolve
[lyx.git] / src / insets / InsetTabular.cpp
index 29c99bb36da082d9fee3651f2af7b0973ed459af..324987c8a445e6474f0b61d8a370b5e59e3c4e15 100644 (file)
@@ -264,6 +264,8 @@ string const tostr(Tabular::BoxType const & num)
                return "parbox";
        case Tabular::BOX_MINIPAGE:
                return "minipage";
+       case Tabular::BOX_VARWIDTH:
+               return "varwidth";
        }
        return string();
 }
@@ -326,6 +328,8 @@ bool string2type(string const & str, Tabular::BoxType & num)
                num = Tabular::BOX_PARBOX;
        else if (str == "minipage")
                num = Tabular::BOX_MINIPAGE;
+       else if (str == "varwidth")
+               num = Tabular::BOX_VARWIDTH;
        else
                return false;
        return true;
@@ -913,8 +917,11 @@ void Tabular::updateIndexes()
        // reset column and row of cells and update their width and alignment
        for (row_type row = 0; row < nrows(); ++row)
                for (col_type column = 0; column < ncols(); ++column) {
-                       if (isPartOfMultiColumn(row, column))
+                       if (isPartOfMultiColumn(row, column)) {
+                               cell_info[row][column].inset->toggleMultiCol(true);
                                continue;
+                       }
+                       cell_info[row][column].inset->toggleMultiCol(false);
                        // columnofcell needs to be called before setting width and aligment
                        // multirow cells inherit the width from the column width
                        if (!isPartOfMultiRow(row, column)) {
@@ -922,8 +929,11 @@ void Tabular::updateIndexes()
                                rowofcell[i] = row;
                        }
                        setFixedWidth(row, column);
-                       if (isPartOfMultiRow(row, column))
+                       if (isPartOfMultiRow(row, column)) {
+                               cell_info[row][column].inset->toggleMultiRow(true);
                                continue;
+                       }
+                       cell_info[row][column].inset->toggleMultiRow(false);
                        cell_info[row][column].inset->setContentAlignment(
                                getAlignment(cellIndex(row, column)));
                        ++i;
@@ -1019,15 +1029,60 @@ int Tabular::cellHeight(idx_type cell) const
 }
 
 
-bool Tabular::updateColumnWidths()
+bool Tabular::updateColumnWidths(MetricsInfo & mi)
 {
        vector<int> max_dwidth(ncols(), 0);
+       // collect max. fixed width of column
+       map<col_type, int> max_pwidth;
+       // collect max. variable width of column
+       map<col_type, int> max_width;
+
        for(col_type c = 0; c < ncols(); ++c)
                for(row_type r = 0; r < nrows(); ++r) {
                        idx_type const i = cellIndex(r, c);
                        if (getAlignment(i) == LYX_ALIGN_DECIMAL)
                                max_dwidth[c] = max(max_dwidth[c], cell_info[r][c].decimal_width);
+                       if (!getPWidth(i).zero())
+                               max_pwidth[c] = max(max_pwidth[c], cell_info[r][c].width);
+                       else if (!column_info[c].varwidth)
+                               max_width[c] = max(max_width[c], cell_info[r][c].width);
+               }
+
+       // If we have a fixed tabular width, we take this into account
+       Length tab_width = tabular_width;
+       bool const tabularx = hasVarwidthColumn();
+       if (tabularx && tab_width.zero())
+               // If no tabular width is specified with X columns,
+               // we use 100% colwidth
+               tab_width = Length(100, Length::PCW);
+       int restwidth = -1;
+       if (!tab_width.zero()) {
+               restwidth = mi.base.inPixels(tab_width);
+               // Substract the fixed widths from the table width
+               for (auto const w : max_pwidth)
+                       restwidth -= w.second;
+       }
+
+       // If we have a fixed width, distribute the available table width
+       // (minus the fixed widths) to the variable-width columns
+       int vcolwidth = -1;
+       int restcols = ncols() - max_pwidth.size();
+       if (restwidth > 0)
+               vcolwidth = restwidth / restcols;
+
+       // Now consider that some variable width columns exceed the vcolwidth
+       if (vcolwidth > 0) {
+               bool changed = false;
+               for (auto const w : max_width) {
+                       if (tabularx || w.second > vcolwidth) {
+                               --restcols;
+                               restwidth -= w.second;
+                               changed = true;
+                       }
                }
+               if (changed && restwidth > 0)
+                       vcolwidth = restwidth / restcols;
+       }
 
        bool update = false;
        // for each col get max of single col cells
@@ -1040,14 +1095,23 @@ bool Tabular::updateColumnWidths()
                                        && cell_info[r][c].decimal_width != 0)
                                        new_width = max(new_width, cellInfo(i).width
                                                + max_dwidth[c] - cellInfo(i).decimal_width);
-                               else
+                               else if (getPWidth(i).zero() && vcolwidth > 0) {
+                                       if (tabularx && !column_info[c].varwidth)
+                                               new_width = max(new_width, cellInfo(i).width);
+                                       else if (tabularx)
+                                               new_width = vcolwidth;
+                                       else
+                                               new_width = max(vcolwidth, max(new_width, cellInfo(i).width));
+                               } else
                                        new_width = max(new_width, cellInfo(i).width);
                        }
                }
 
                if (column_info[c].width != new_width) {
                        column_info[c].width = new_width;
-                       update = true;
+                       // Do not trigger update when no space is left for variable
+                       // columns, as this will loop
+                       update = tab_width.zero() || restwidth > 0;
                }
        }
        // update col widths to fit merged cells
@@ -1064,7 +1128,9 @@ bool Tabular::updateColumnWidths()
 
                        if (cellInfo(i).width > old_width) {
                                column_info[c + span - 1].width += cellInfo(i).width - old_width;
-                               update = true;
+                               // Do not trigger update when no space is left for variable
+                               // columns, as this will loop
+                               update = tab_width.zero() || restwidth > 0;
                        }
                }
 
@@ -1126,14 +1192,16 @@ void Tabular::setVAlignment(idx_type cell, VAlignment align,
 namespace {
 
 /**
- * Allow line and paragraph breaks for fixed width cells or disallow them,
- * merge cell paragraphs and reset layout to standard for variable width
- * cells.
+ * Allow line and paragraph breaks for fixed width multicol/multirow cells
+ * or disallow them, merge cell paragraphs and reset layout to standard
+ * for variable width multicol cells.
  */
-void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
+void toggleFixedWidth(Cursor & cur, InsetTableCell * inset,
+                     bool const fixedWidth, bool const multicol,
+                     bool const multirow)
 {
        inset->toggleFixedWidth(fixedWidth);
-       if (fixedWidth)
+       if (!multirow && (fixedWidth || !multicol))
                return;
 
        // merge all paragraphs to one
@@ -1141,6 +1209,19 @@ void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
        while (inset->paragraphs().size() > 1)
                mergeParagraph(bp, inset->paragraphs(), 0);
 
+       // This is relevant for multirows
+       if (fixedWidth)
+               return;
+
+       // remove newlines
+       ParagraphList::iterator pit = inset->paragraphs().begin();
+       for (; pit != inset->paragraphs().end(); ++pit) {
+               for (pos_type j = 0; j != pit->size(); ++j) {
+                       if (pit->isNewline(j))
+                               pit->eraseChar(j, bp.track_changes);
+               }
+       }
+
        // reset layout
        cur.push(*inset);
        // undo information has already been recorded
@@ -1167,16 +1248,13 @@ void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
                idx_type const cidx = cellIndex(r, c);
                // because of multicolumns
                toggleFixedWidth(cur, cellInset(cidx).get(),
-                                !getPWidth(cidx).zero());
+                                !getPWidth(cidx).zero(), isMultiColumn(cidx),
+                                isMultiRow(cidx));
                if (isMultiRow(cidx))
                        setAlignment(cidx, LYX_ALIGN_LEFT, false);
        }
-       // cur paragraph can become invalid after paragraphs were merged
-       if (cur.pit() > cur.lastpit())
-               cur.pit() = cur.lastpit();
-       // cur position can become invalid after newlines were removed
-       if (cur.pos() > cur.lastpos())
-               cur.pos() = cur.lastpos();
+       // cur can become invalid after paragraphs were merged
+       cur.fixIfBroken();
 }
 
 
@@ -1197,13 +1275,10 @@ bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
                return false;
 
        cellInfo(cell).p_width = width;
-       toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
-       // cur paragraph can become invalid after paragraphs were merged
-       if (cur.pit() > cur.lastpit())
-               cur.pit() = cur.lastpit();
-       // cur position can become invalid after newlines were removed
-       if (cur.pos() > cur.lastpos())
-               cur.pos() = cur.lastpos();
+       toggleFixedWidth(cur, cellInset(cell).get(), !width.zero(),
+                        isMultiColumn(cell), isMultiRow(cell));
+       // cur can become invalid after paragraphs were merged
+       cur.fixIfBroken();
        return true;
 }
 
@@ -1713,6 +1788,16 @@ bool Tabular::hasVarwidthColumn() const
 }
 
 
+bool Tabular::isVTypeColumn(col_type c) const
+{
+       for (row_type r = 0; r < nrows(); ++r) {
+               idx_type idx = cellIndex(r, c);
+               if (getRotateCell(idx) == 0 && useBox(idx) == BOX_VARWIDTH)
+                       return true;
+       }
+       return false;
+}
+
 
 Tabular::CellData const & Tabular::cellInfo(idx_type cell) const
 {
@@ -1726,7 +1811,7 @@ Tabular::CellData & Tabular::cellInfo(idx_type cell)
 }
 
 
-Tabular::idx_type Tabular::setMultiColumn(idx_type cell, idx_type number,
+Tabular::idx_type Tabular::setMultiColumn(Cursor & cur, idx_type cell, idx_type number,
                                          bool const right_border)
 {
        idx_type const col = cellColumn(cell);
@@ -1741,6 +1826,14 @@ Tabular::idx_type Tabular::setMultiColumn(idx_type cell, idx_type number,
        if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
                cs.alignment = column_info[col].alignment;
        setRightLine(cell, right_border);
+       // non-fixed width multicolumns cannot have multiple paragraphs
+       if (getPWidth(cell).zero()) {
+               toggleFixedWidth(cur, cellInset(cell).get(),
+                                !getPWidth(cell).zero(), isMultiColumn(cell),
+                                isMultiRow(cell));
+               // cur can become invalid after paragraphs were merged
+               cur.fixIfBroken();
+       }
 
        idx_type lastcell = cellIndex(row, col + number - 1);
        for (idx_type i = 1; i < lastcell - cell + 1; ++i) {
@@ -1769,7 +1862,7 @@ bool Tabular::hasMultiRow(row_type r) const
        return false;
 }
 
-Tabular::idx_type Tabular::setMultiRow(idx_type cell, idx_type number,
+Tabular::idx_type Tabular::setMultiRow(Cursor & cur, idx_type cell, idx_type number,
                                       bool const bottom_border,
                                       LyXAlignment const halign)
 {
@@ -1792,6 +1885,15 @@ Tabular::idx_type Tabular::setMultiRow(idx_type cell, idx_type number,
        else
                cs.alignment = LYX_ALIGN_LEFT;
 
+       // Multirows cannot have multiple paragraphs
+       if (getPWidth(cell).zero()) {
+               toggleFixedWidth(cur, cellInset(cell).get(),
+                                !getPWidth(cell).zero(),
+                                isMultiColumn(cell), isMultiRow(cell));
+               // cur can become invalid after paragraphs were merged
+               cur.fixIfBroken();
+       }
+
        // set the bottom line of the last selected cell
        setBottomLine(cell, bottom_border);
 
@@ -1937,16 +2039,15 @@ void Tabular::setUsebox(idx_type cell, BoxType type)
 }
 
 
-// FIXME: Remove this routine because we cannot insert \parboxes when the user
-// adds line breaks, see bug 4886.
 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
 {
-       if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
-               (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
+       if (getRotateCell(cell) == 0
+           && ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
+               (isMultiColumn(cell) && !cellInfo(cell).p_width.zero())))
                return BOX_NONE;
        if (cellInfo(cell).usebox > 1)
                return cellInfo(cell).usebox;
-       return useParbox(cell);
+       return useBox(cell);
 }
 
 
@@ -2075,11 +2176,11 @@ bool Tabular::haveLTLastFoot(bool withcaptions) const
 }
 
 
-Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
+Tabular::idx_type Tabular::setLTCaption(Cursor & cur, row_type row, bool what)
 {
        idx_type i = getFirstCellInRow(row);
        if (what) {
-               setMultiColumn(i, numberOfCellsInRow(row), false);
+               setMultiColumn(cur, i, numberOfCellsInRow(row), false);
                setTopLine(i, false);
                setBottomLine(i, false);
                setLeftLine(i, false);
@@ -2487,7 +2588,21 @@ void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
                }
                os << "]{" << from_ascii(getPWidth(cell).asLatexString())
                   << "}\n";
+       } else if (getRotateCell(cell) != 0 && getUsebox(cell) == BOX_VARWIDTH) {
+               os << "\\begin{varwidth}[";
+               switch (valign) {
+               case LYX_VALIGN_TOP:
+                       os << 't';
+                       break;
+               case LYX_VALIGN_MIDDLE:
+                       os << 'm';
+                       break;
+               case LYX_VALIGN_BOTTOM:
+                       os << 'b';
+                       break;
        }
+       os << "]{\\linewidth}\n";
+}
 }
 
 
@@ -2503,6 +2618,8 @@ void Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
                os << '}';
        else if (getUsebox(cell) == BOX_MINIPAGE)
                os << breakln << "\\end{minipage}";
+       else if (getRotateCell(cell) != 0 && getUsebox(cell) == BOX_VARWIDTH)
+               os << breakln << "\\end{varwidth}";
        if (getRotateCell(cell) != 0)
                os << breakln << "\\end{turn}";
        if (ismultirow)
@@ -2945,6 +3062,25 @@ void Tabular::latex(otexstream & os, OutputParams const & runparams) const
                                        break;
                                }
                                os << 'X';
+                       } else if (isVTypeColumn(c)) {
+                               switch (column_info[c].alignment) {
+                               case LYX_ALIGN_LEFT:
+                                       os << ">{\\raggedright}";
+                                       break;
+                               case LYX_ALIGN_RIGHT:
+                                       os << ">{\\raggedleft}";
+                                       break;
+                               case LYX_ALIGN_CENTER:
+                                       os << ">{\\centering}";
+                                       break;
+                               case LYX_ALIGN_NONE:
+                               case LYX_ALIGN_BLOCK:
+                               case LYX_ALIGN_LAYOUT:
+                               case LYX_ALIGN_SPECIAL:
+                               case LYX_ALIGN_DECIMAL:
+                                       break;
+                               }
+                               os << "V{\\linewidth}";
                        } else {
                                switch (column_info[c].alignment) {
                                case LYX_ALIGN_LEFT:
@@ -3566,8 +3702,11 @@ void Tabular::validate(LaTeXFeatures & features) const
        for (idx_type cell = 0; cell < numberofcells; ++cell) {
                if (isMultiRow(cell))
                        features.require("multirow");
+               if (getUsebox(cell) == BOX_VARWIDTH)
+                       features.require("varwidth");
                if (getVAlignment(cell) != LYX_VALIGN_TOP
-                   || !getPWidth(cell).zero())
+                   || !getPWidth(cell).zero()
+                   || isVTypeColumn(cellColumn(cell)))
                        features.require("array");
                // Tell footnote that we need a savenote
                // environment in non-long tables or
@@ -3583,16 +3722,19 @@ void Tabular::validate(LaTeXFeatures & features) const
 }
 
 
-Tabular::BoxType Tabular::useParbox(idx_type cell) const
+Tabular::BoxType Tabular::useBox(idx_type cell) const
 {
        ParagraphList const & parlist = cellInset(cell)->paragraphs();
+       if (parlist.size() > 1)
+               return BOX_VARWIDTH;
+
        ParagraphList::const_iterator cit = parlist.begin();
        ParagraphList::const_iterator end = parlist.end();
 
        for (; cit != end; ++cit)
                for (int i = 0; i < cit->size(); ++i)
-                       if (cit->isNewline(i))
-                               return BOX_PARBOX;
+                       if (cit->isNewline(i) || cit->layout().isEnvironment())
+                               return BOX_VARWIDTH;
 
        return BOX_NONE;
 }
@@ -3606,13 +3748,13 @@ Tabular::BoxType Tabular::useParbox(idx_type cell) const
 
 InsetTableCell::InsetTableCell(Buffer * buf)
        : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
-         contentAlign(LYX_ALIGN_CENTER)
+         isMultiColumn(false), isMultiRow(false), contentAlign(LYX_ALIGN_CENTER)
 {}
 
 
 bool InsetTableCell::forcePlainLayout(idx_type) const
 {
-       return !isFixedWidth;
+       return isMultiRow || (isMultiColumn && !isFixedWidth);
 }
 
 
@@ -3679,6 +3821,33 @@ docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
 }
 
 
+void InsetTableCell::metrics(MetricsInfo & mi, Dimension & dim) const
+{
+       TextMetrics & tm = mi.base.bv->textMetrics(&text());
+
+       // Hand font through to contained lyxtext:
+       tm.font_.fontInfo() = mi.base.font;
+       mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
+
+       // This can happen when a layout has a left and right margin,
+       // and the view is made very narrow. We can't do better than
+       // to draw it partly out of view (bug 5890).
+       if (mi.base.textwidth < 1)
+               mi.base.textwidth = 1;
+
+       // We tell metrics here not to expand on multiple pars
+       // This is the difference to InsetText::Metrics
+       if (hasFixedWidth())
+               tm.metrics(mi, dim, mi.base.textwidth, false);
+       else
+               tm.metrics(mi, dim, 0, false);
+       mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
+       dim.asc += TEXT_TO_INSET_OFFSET;
+       dim.des += TEXT_TO_INSET_OFFSET;
+       dim.wid += 2 * TEXT_TO_INSET_OFFSET;
+}
+
+
 
 /////////////////////////////////////////////////////////////////////
 //
@@ -3824,8 +3993,10 @@ void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
                        Length const p_width = tabular.getPWidth(cell);
                        if (!p_width.zero())
                                m.base.textwidth = mi.base.inPixels(p_width);
+                       else if (tabular.column_info[c].varwidth)
+                               m.base.textwidth = tabular.column_info[c].width;
                        tabular.cellInset(cell)->metrics(m, dim0);
-                       if (!p_width.zero())
+                       if (!p_width.zero() || tabular.column_info[c].varwidth)
                                dim0.wid = m.base.textwidth;
                        tabular.cellInfo(cell).width = dim0.wid + 2 * WIDTH_OF_LINE
                                + tabular.interColumnSpace(cell);
@@ -3892,15 +4063,17 @@ void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
                tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
        }
 
-       tabular.updateColumnWidths();
+       // We need to recalculate the metrics after column width calculation
+       // with xtabular (possibly multiple times, so the call is recursive).
+       if (tabular.updateColumnWidths(mi) && tabular.hasVarwidthColumn())
+               metrics(mi, dim);
        dim.asc = tabular.rowAscent(0) - tabular.offsetVAlignment();
        dim.des = tabular.height() - dim.asc;
        dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
 }
 
 
-bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
-       const
+bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col) const
 {
        if (&cur.inset() == this && cur.selection()) {
                if (cur.selIsMultiCell()) {
@@ -4038,35 +4211,44 @@ void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
 }
 
 
+namespace {
+
+void tabline(PainterInfo const & pi, int x1, int y1, int x2, int y2,
+             bool drawline, bool heavy = false)
+{
+       ColorCode const col = drawline ? Color_tabularline : Color_tabularonoffline;
+       pi.pain.line(x1, y1, x2, y2, pi.textColor(col),
+                                drawline ? Painter::line_solid : Painter::line_onoffdash,
+                                (heavy ? 2 : 1) * Painter::thin_line);
+}
+
+}
+
+
 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
                                 row_type row, idx_type cell) const
 {
        y -= tabular.rowAscent(row);
        int const w = tabular.cellWidth(cell);
        int const h = tabular.cellHeight(cell);
-       Color const linecolor = pi.textColor(Color_tabularline);
-       Color const gridcolor = pi.textColor(Color_tabularonoffline);
 
        // Top
        bool drawline = tabular.topLine(cell)
                || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
-       pi.pain.line(x, y, x + w, y,
-               drawline ? linecolor : gridcolor,
-               drawline ? Painter::line_solid : Painter::line_onoffdash);
+       bool heavy = tabular.use_booktabs && row == 0 && tabular.rowTopLine(row);
+       tabline(pi, x, y, x + w, y, drawline, heavy);
 
        // Bottom
        drawline = tabular.bottomLine(cell);
-       pi.pain.line(x, y + h, x + w, y + h,
-               drawline ? linecolor : gridcolor,
-               drawline ? Painter::line_solid : Painter::line_onoffdash);
+       heavy = tabular.use_booktabs && row == tabular.nrows() - 1
+               && tabular.rowBottomLine(row);
+       tabline(pi, x, y + h, x + w, y + h, drawline, heavy);
 
        // Left
        col_type const col = tabular.cellColumn(cell);
        drawline = tabular.leftLine(cell)
                || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
-       pi.pain.line(x, y, x, y + h,
-               drawline ? linecolor : gridcolor,
-               drawline ? Painter::line_solid : Painter::line_onoffdash);
+       tabline(pi, x, y, x, y + h, drawline);
 
        // Right
        x -= tabular.interColumnSpace(cell);
@@ -4077,9 +4259,7 @@ void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
        drawline = tabular.rightLine(cell)
                   || (next_cell_col < tabular.ncols()
                       && tabular.leftLine(tabular.cellIndex(row, next_cell_col)));
-       pi.pain.line(x + w, y, x + w, y + h,
-               drawline ? linecolor : gridcolor,
-               drawline ? Painter::line_solid : Painter::line_onoffdash);
+       tabline(pi, x + w, y, x + w, y + h, drawline);
 }
 
 
@@ -5168,13 +5348,13 @@ bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
                        return true;
                }
                // fall through
-       case LFUN_NEWLINE_INSERT: {
-               if (tabular.getPWidth(cur.idx()).zero()) {
+       case LFUN_NEWLINE_INSERT:
+               if ((tabular.isMultiColumn(cur.idx()) || tabular.isMultiRow(cur.idx()))
+                   && tabular.getPWidth(cur.idx()).zero()) {
                        status.setEnabled(false);
                        return true;
-               } else
-                       return cell(cur.idx())->getStatus(cur, cmd, status);
-       }
+               }
+               return cell(cur.idx())->getStatus(cur, cmd, status);
 
        case LFUN_NEWPAGE_INSERT:
                status.setEnabled(false);
@@ -5660,10 +5840,12 @@ void InsetTabular::tabularFeatures(Cursor & cur,
 
        case Tabular::SET_PWIDTH: {
                Length const len(value);
-               tabular.setColumnPWidth(cur, cur.idx(), len);
-               if (len.zero()
-                   && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
-                       tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
+               for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
+                       tabular.setColumnPWidth(cur, tabular.cellIndex(row, c), len);
+                       if (len.zero()
+                           && tabular.getAlignment(tabular.cellIndex(row, c), true) == LYX_ALIGN_BLOCK)
+                               tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
+               }
                break;
        }
 
@@ -5673,7 +5855,8 @@ void InsetTabular::tabularFeatures(Cursor & cur,
 
        case Tabular::TOGGLE_VARWIDTH_COLUMN: {
                bool const varwidth = value == "on";
-               tabular.toggleVarwidth(cur.idx(), varwidth);
+               for (col_type c = sel_col_start; c <= sel_col_end; ++c)
+                       tabular.toggleVarwidth(tabular.cellIndex(row, c), varwidth);
                break;
        }
 
@@ -5841,7 +6024,7 @@ void InsetTabular::tabularFeatures(Cursor & cur,
                        // just multicol for one single cell
                        // check whether we are completely in a multicol
                        if (!tabular.isMultiColumn(cur.idx()))
-                               tabular.setMultiColumn(cur.idx(), 1,
+                               tabular.setMultiColumn(cur, cur.idx(), 1,
                                        tabular.rightLine(cur.idx()));
                        break;
                }
@@ -5850,7 +6033,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());
-               cur.idx() = tabular.setMultiColumn(s_start, col_end - col_start + 1,
+               cur.idx() = tabular.setMultiColumn(cur, s_start, col_end - col_start + 1,
                                                   tabular.rightLine(cur.selEnd().idx()));
                cur.pit() = 0;
                cur.pos() = 0;
@@ -5896,7 +6079,7 @@ void InsetTabular::tabularFeatures(Cursor & cur,
                        // just multirow for one single cell
                        // check whether we are completely in a multirow
                        if (!tabular.isMultiRow(cur.idx()))
-                               tabular.setMultiRow(cur.idx(), 1,
+                               tabular.setMultiRow(cur, cur.idx(), 1,
                                                    tabular.bottomLine(cur.idx()),
                                                    tabular.getAlignment(cur.idx()));
                        break;
@@ -5906,7 +6089,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());
-               cur.idx() = tabular.setMultiRow(s_start, row_end - row_start + 1,
+               cur.idx() = tabular.setMultiRow(cur, s_start, row_end - row_start + 1,
                                                tabular.bottomLine(cur.selEnd().idx()),
                                                tabular.getAlignment(cur.selEnd().idx()));
                cur.pit() = 0;
@@ -6033,8 +6216,6 @@ void InsetTabular::tabularFeatures(Cursor & cur,
                tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
                break;
 
-
-
        case Tabular::SET_ROTATE_CELL:
                for (row_type r = sel_row_start; r <= sel_row_end; ++r)
                        for (col_type c = sel_col_start; c <= sel_col_end; ++c)
@@ -6119,7 +6300,7 @@ void InsetTabular::tabularFeatures(Cursor & cur,
        case Tabular::SET_LTCAPTION: {
                if (tabular.ltCaption(row))
                        break;
-               cur.idx() = tabular.setLTCaption(row, true);
+               cur.idx() = tabular.setLTCaption(cur, row, true);
                cur.pit() = 0;
                cur.pos() = 0;
                cur.selection(false);
@@ -6135,7 +6316,7 @@ void InsetTabular::tabularFeatures(Cursor & cur,
        case Tabular::UNSET_LTCAPTION: {
                if (!tabular.ltCaption(row))
                        break;
-               cur.idx() = tabular.setLTCaption(row, false);
+               cur.idx() = tabular.setLTCaption(cur, row, false);
                cur.pit() = 0;
                cur.pos() = 0;
                cur.selection(false);
@@ -6391,12 +6572,12 @@ void InsetTabular::getSelection(Cursor & cur,
        cs = tabular.cellColumn(beg.idx());
        ce = tabular.cellColumn(end.idx());
        if (cs > ce)
-               swap(cs, ce);
+               std::swap(cs, ce);
 
        rs = tabular.cellRow(beg.idx());
        re = tabular.cellRow(end.idx());
        if (rs > re)
-               swap(rs, re);
+               std::swap(rs, re);
 }
 
 
@@ -6435,7 +6616,7 @@ bool InsetTabular::allowParagraphCustomization(idx_type cell) const
 
 bool InsetTabular::forcePlainLayout(idx_type cell) const
 {
-       return !tabular.getPWidth(cell).zero();
+       return tabular.isMultiColumn(cell) && !tabular.getPWidth(cell).zero();
 }