]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetTabular.cpp
Move the global formats and system_formats variables into the
[lyx.git] / src / insets / InsetTabular.cpp
index 6d8f7d0737138f45d5a1b6183e0290c2f96b66d9..88817357c16c663aa91c5aeef8ef3777ec457c80 100644 (file)
@@ -45,6 +45,8 @@
 #include "Paragraph.h"
 #include "ParagraphParameters.h"
 #include "ParIterator.h"
+#include "TexRow.h"
+#include "texstream.h"
 #include "TextClass.h"
 #include "TextMetrics.h"
 
@@ -61,8 +63,7 @@
 #include "support/gettext.h"
 #include "support/lassert.h"
 #include "support/lstrings.h"
-
-#include <boost/scoped_ptr.hpp>
+#include "support/unique_ptr.h"
 
 #include <cstring>
 #include <iostream>
@@ -96,7 +97,7 @@ int const WIDTH_OF_LINE = 5; // space between double lines
 
 
 ///
-boost::scoped_ptr<Tabular> paste_tabular;
+unique_ptr<Tabular> paste_tabular;
 
 
 struct TabularFeature {
@@ -526,7 +527,7 @@ string const featureAsString(Tabular::Feature action)
 }
 
 
-DocIterator separatorPos(InsetTableCell * cell, docstring const & align_d)
+DocIterator separatorPos(InsetTableCell const * cell, docstring const & align_d)
 {
        DocIterator dit = doc_iterator_begin(&(cell->buffer()), cell);
        for (; dit; dit.forwardChar())
@@ -542,7 +543,7 @@ InsetTableCell splitCell(InsetTableCell & head, docstring const & align_d, bool
 {
        InsetTableCell tail = InsetTableCell(head);
        DocIterator const dit = separatorPos(&head, align_d);
-       hassep = dit;
+       hassep = (bool)dit;
        if (hassep) {
                pit_type const psize = head.paragraphs().front().size();
                head.paragraphs().front().eraseChars(dit.pos(), psize, false);
@@ -1032,7 +1033,7 @@ bool Tabular::updateColumnWidths()
                        idx_type const i = cellIndex(r, c);
                        if (columnSpan(i) == 1) {
                                if (getAlignment(i) == LYX_ALIGN_DECIMAL
-                                       && cell_info[r][c].decimal_width!=0)
+                                       && cell_info[r][c].decimal_width != 0)
                                        new_width = max(new_width, cellInfo(i).width
                                                + max_dwidth[c] - cellInfo(i).decimal_width);
                                else
@@ -1103,7 +1104,7 @@ void Tabular::setAlignment(idx_type cell, LyXAlignment align,
                        dpoint = from_utf8(lyxrc.default_decimal_point);
        } else {
                cellInfo(cell).alignment = align;
-               cellInset(cell).get()->setContentAlignment(align);
+               cellInset(cell)->setContentAlignment(align);
        }
 }
 
@@ -1321,6 +1322,27 @@ Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
 }
 
 
+int Tabular::offsetVAlignment() const
+{
+       // for top-alignment the first horizontal table line must be exactly at
+       // the position of the base line of the surrounding text line
+       // for bottom alignment, the same is for the last table line
+       int offset_valign = 0;
+       switch (tabular_valignment) {
+       case Tabular::LYX_VALIGN_BOTTOM:
+               offset_valign = rowAscent(0) - height();
+               break;
+       case Tabular::LYX_VALIGN_MIDDLE:
+               offset_valign = (- height()) / 2 + rowAscent(0);
+               break;
+       case Tabular::LYX_VALIGN_TOP:
+               offset_valign = rowAscent(0);
+               break;
+       }
+       return offset_valign;
+}
+
+
 Length const Tabular::getPWidth(idx_type cell) const
 {
        if (isMultiColumn(cell))
@@ -1669,7 +1691,13 @@ bool Tabular::hasMultiColumn(col_type c) const
 }
 
 
-Tabular::CellData & Tabular::cellInfo(idx_type cell) const
+Tabular::CellData const & Tabular::cellInfo(idx_type cell) const
+{
+       return cell_info[cellRow(cell)][cellColumn(cell)];
+}
+
+
+Tabular::CellData & Tabular::cellInfo(idx_type cell)
 {
        return cell_info[cellRow(cell)][cellColumn(cell)];
 }
@@ -2309,7 +2337,7 @@ void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
        // we center in multicol when no decimal point
        if (column_info[c].alignment == LYX_ALIGN_DECIMAL) {
                docstring const align_d = column_info[c].decimal_point;
-               DocIterator const dit = separatorPos(cellInset(cell).get(), align_d);
+               DocIterator const dit = separatorPos(cellInset(cell), align_d);
                ismulticol |= !dit;
        }
 
@@ -2534,7 +2562,7 @@ void Tabular::TeXRow(otexstream & os, row_type row,
                     OutputParams const & runparams) const
 {
        idx_type cell = cellIndex(row, 0);
-       shared_ptr<InsetTableCell> inset = cellInset(cell);
+       InsetTableCell const * inset = cellInset(cell);
        Paragraph const & par = inset->paragraphs().front();
        string const lang = par.getParLanguage(buffer().params())->lang();
 
@@ -2573,7 +2601,7 @@ void Tabular::TeXRow(otexstream & os, row_type row,
                }
 
                TeXCellPreamble(os, cell, ismulticol, ismultirow);
-               shared_ptr<InsetTableCell> inset = cellInset(cell);
+               InsetTableCell const * inset = cellInset(cell);
 
                Paragraph const & par = inset->paragraphs().front();
 
@@ -2606,8 +2634,8 @@ void Tabular::TeXRow(otexstream & os, row_type row,
 
                if (getAlignment(cell) == LYX_ALIGN_DECIMAL) {
                        // copy cell and split in 2
-                       InsetTableCell head = InsetTableCell(*cellInset(cell).get());
-                       head.setBuffer(buffer());
+                       InsetTableCell head = InsetTableCell(*cellInset(cell));
+                       head.setBuffer(const_cast<Buffer &>(buffer()));
                        DocIterator dit = cellInset(cell)->getText(0)->macrocontextPosition();
                        dit.pop_back();
                        dit.push_back(CursorSlice(head));
@@ -2680,8 +2708,7 @@ void Tabular::TeXRow(otexstream & os, row_type row,
 void Tabular::latex(otexstream & os, OutputParams const & runparams) const
 {
        bool const is_tabular_star = !tabular_width.zero();
-       TexRow::RowEntry pos = TexRow::textEntry(runparams.lastid,
-                                                                                        runparams.lastpos);
+       TexRow::RowEntry pos = TexRow::textEntry(runparams.lastid, runparams.lastpos);
 
        //+---------------------------------------------------------------------
        //+                      first the opening preamble                    +
@@ -2735,6 +2762,7 @@ void Tabular::latex(otexstream & os, OutputParams const & runparams) const
                        os << column_info[c].align_special;
                } else {
                        if (!column_info[c].p_width.zero()) {
+                               bool decimal = false;
                                switch (column_info[c].alignment) {
                                case LYX_ALIGN_LEFT:
                                        os << ">{\\raggedright}";
@@ -2749,24 +2777,49 @@ void Tabular::latex(otexstream & os, OutputParams const & runparams) const
                                case LYX_ALIGN_BLOCK:
                                case LYX_ALIGN_LAYOUT:
                                case LYX_ALIGN_SPECIAL:
+                                       break;
                                case LYX_ALIGN_DECIMAL:
+                                       os << ">{\\raggedleft}";
+                                       decimal = true;
                                        break;
                                }
 
+                               char valign = 'p';
                                switch (column_info[c].valignment) {
                                case LYX_VALIGN_TOP:
-                                       os << 'p';
+                                       // this is the default
                                        break;
                                case LYX_VALIGN_MIDDLE:
-                                       os << 'm';
+                                       valign = 'm';
                                        break;
                                case LYX_VALIGN_BOTTOM:
-                                       os << 'b';
+                                       valign = 'b';
                                        break;
-                       }
-                               os << '{'
-                                  << from_ascii(column_info[c].p_width.asLatexString())
-                                  << '}';
+                               }
+                               os << valign;
+
+                               // Fixed-width cells with alignment at decimal separator
+                               // are output as two cells of half the width with the decimal
+                               // separator as column sep. This effectively puts the content
+                               // centered, which differs from the normal decimal sep alignment
+                               // and is not ideal, but we cannot do better ATM (see #9568).
+                               // FIXME: Implement proper decimal sep alignment, e.g. via siunitx.
+                               if (decimal) {
+                                       docstring const halffixedwith =
+                                               from_ascii(Length(column_info[c].p_width.value() / 2,
+                                                                 column_info[c].p_width.unit()).asLatexString());
+                                       os << '{'
+                                          << halffixedwith
+                                          << '}'
+                                          << "@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}"
+                                          << valign
+                                          << '{'
+                                          << halffixedwith
+                                          << '}';
+                               } else
+                                       os << '{'
+                                          << from_ascii(column_info[c].p_width.asLatexString())
+                                          << '}';
                        } else {
                                switch (column_info[c].alignment) {
                                case LYX_ALIGN_LEFT:
@@ -2988,6 +3041,13 @@ docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
                        continue;
 
                stringstream attr;
+               
+               Length const cwidth = column_info[c].p_width;
+               if (!cwidth.zero()) {
+                       string const hwidth = cwidth.asHTMLString();
+                       attr << "style =\"width: " << hwidth << ";\" ";
+               }
+               
                attr << "align='";
                switch (getAlignment(cell)) {
                case LYX_ALIGN_LEFT:
@@ -3019,7 +3079,7 @@ docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
                else if (isMultiRow(cell))
                        attr << " rowspan='" << rowSpan(cell) << "'";
 
-               xs << html::StartTag(celltag, attr.str()) << html::CR();
+               xs << html::StartTag(celltag, attr.str(), true) << html::CR();
                ret += cellInset(cell)->xhtml(xs, runparams);
                xs << html::EndTag(celltag) << html::CR();
                ++cell;
@@ -3322,21 +3382,26 @@ void Tabular::plaintext(odocstringstream & os,
 }
 
 
-shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
+shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell)
 {
        return cell_info[cellRow(cell)][cellColumn(cell)].inset;
 }
 
 
-shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
-                                              col_type column) const
+shared_ptr<InsetTableCell> Tabular::cellInset(row_type row, col_type column)
 {
        return cell_info[row][column].inset;
 }
 
 
+InsetTableCell const * Tabular::cellInset(idx_type cell) const
+{
+       return cell_info[cellRow(cell)][cellColumn(cell)].inset.get();
+}
+
+
 void Tabular::setCellInset(row_type row, col_type column,
-                             shared_ptr<InsetTableCell> ins) const
+                           shared_ptr<InsetTableCell> ins)
 {
        CellData & cd = cell_info[row][column];
        cd.inset = ins;
@@ -3413,13 +3478,6 @@ bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
 {
        bool enabled = true;
        switch (cmd.action()) {
-       case LFUN_LAYOUT:
-               enabled = !forcePlainLayout();
-               break;
-       case LFUN_LAYOUT_PARAGRAPH:
-               enabled = allowParagraphCustomization();
-               break;
-
        case LFUN_MATH_DISPLAY:
                if (!hasFixedWidth()) {
                        enabled = false;
@@ -3452,9 +3510,9 @@ docstring InsetTableCell::asString(bool intoInsets)
 
 
 void InsetTableCell::addToToc(DocIterator const & di, bool output_active,
-                                                         UpdateType utype) const
+                                                         UpdateType utype, TocBackend & backend) const
 {
-       InsetText::iterateForToc(di, output_active, utype);
+       InsetText::iterateForToc(di, output_active, utype, backend);
 }
 
 
@@ -3476,13 +3534,14 @@ docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
                           col_type columns)
        : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))),
-         first_visible_cell_(0), offset_valign_(0), rowselect_(false), colselect_(false)
+         rowselect_(false), colselect_(false)
 {
 }
 
 
 InsetTabular::InsetTabular(InsetTabular const & tab)
-       : Inset(tab), tabular(tab.tabular)
+       : Inset(tab), tabular(tab.tabular),
+         rowselect_(false), colselect_(false)
 {
 }
 
@@ -3521,7 +3580,7 @@ bool InsetTabular::insetAllowed(InsetCode code) const
 bool InsetTabular::allowsCaptionVariation(std::string const & newtype) const
 {
        return tabular.is_long_tabular &&
-               (newtype == "Standard" || newtype == "LongTableNoNumber");
+               (newtype == "Standard" || newtype == "Unnumbered");
 }
 
 
@@ -3569,7 +3628,7 @@ void InsetTabular::read(Lexer & lex)
 int InsetTabular::rowFromY(Cursor & cur, int y) const
 {
        // top y coordinate of tabular
-       int h = yo(cur.bv()) - tabular.rowAscent(0) + offset_valign_;
+       int h = yo(cur.bv()) - tabular.rowAscent(0) + tabular.offsetVAlignment();
        row_type r = 0;
        for (; r < tabular.nrows() && y > h; ++r)
                h += tabular.rowAscent(r) + tabular.rowDescent(r)
@@ -3623,7 +3682,7 @@ void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
                        // determine horizontal offset because of decimal align (if necessary)
                        int decimal_width = 0;
                        if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
-                               InsetTableCell tail = InsetTableCell(*tabular.cellInset(cell).get());
+                               InsetTableCell tail = InsetTableCell(*tabular.cellInset(cell));
                                tail.setBuffer(tabular.buffer());
                                // we need to set macrocontext position everywhere
                                // otherwise we crash with nested insets (e.g. footnotes)
@@ -3678,23 +3737,8 @@ void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
                tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
        }
 
-       // for top-alignment the first horizontal table line must be exactly at
-       // the position of the base line of the surrounding text line
-       // for bottom alignment, the same is for the last table line
-       switch (tabular.tabular_valignment) {
-       case Tabular::LYX_VALIGN_BOTTOM:
-               offset_valign_ = tabular.rowAscent(0) - tabular.height();
-               break;
-       case Tabular::LYX_VALIGN_MIDDLE:
-               offset_valign_ = (- tabular.height()) / 2 + tabular.rowAscent(0);
-               break;
-       case Tabular::LYX_VALIGN_TOP:
-               offset_valign_ = tabular.rowAscent(0);
-               break;
-       }
-
        tabular.updateColumnWidths();
-       dim.asc = tabular.rowAscent(0) - offset_valign_;
+       dim.asc = tabular.rowAscent(0) - tabular.offsetVAlignment();
        dim.des = tabular.height() - dim.asc;
        dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
 }
@@ -3745,9 +3789,8 @@ void InsetTabular::draw(PainterInfo & pi, int x, int y) const
        bool const original_selection_state = pi.selected;
 
        idx_type idx = 0;
-       first_visible_cell_ = Tabular::npos;
 
-       int yy = y + offset_valign_;
+       int yy = y + tabular.offsetVAlignment();
        for (row_type r = 0; r < tabular.nrows(); ++r) {
                int nx = x;
                for (col_type c = 0; c < tabular.ncols(); ++c) {
@@ -3761,9 +3804,6 @@ void InsetTabular::draw(PainterInfo & pi, int x, int y) const
                                continue;
                        }
 
-                       if (first_visible_cell_ == Tabular::npos)
-                               first_visible_cell_ = idx;
-
                        pi.selected |= isCellSelected(cur, r, c);
                        int const cx = nx + tabular.textHOffset(idx);
                        int const cy = yy + tabular.textVOffset(idx);
@@ -3785,7 +3825,7 @@ void InsetTabular::draw(PainterInfo & pi, int x, int y) const
 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
 {
        x += ADD_TO_TABULAR_WIDTH;
-       y += offset_valign_ - tabular.rowAscent(0);
+       y += tabular.offsetVAlignment() - tabular.rowAscent(0);
        pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
                pi.backgroundColor(this));
 }
@@ -3822,7 +3862,7 @@ void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
                                }
                                int const w = tabular.cellWidth(cell);
                                int const h = tabular.cellHeight(cell);
-                               int const yy = y - tabular.rowAscent(r) + offset_valign_;
+                               int const yy = y - tabular.rowAscent(r) + tabular.offsetVAlignment();
                                if (isCellSelected(cur, r, c))
                                        pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
                                xx += w;
@@ -3941,13 +3981,36 @@ void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
 
 
 void InsetTabular::addToToc(DocIterator const & cpit, bool output_active,
-                                                       UpdateType utype) const
+                                                       UpdateType utype, TocBackend & backend) const
 {
        DocIterator dit = cpit;
        dit.forwardPos();
        size_t const end = dit.nargs();
        for ( ; dit.idx() < end; dit.top().forwardIdx())
-               cell(dit.idx())->addToToc(dit, output_active, utype);
+               cell(dit.idx())->addToToc(dit, output_active, utype, backend);
+}
+
+
+bool InsetTabular::hitSelectRow(BufferView const & bv, int x) const
+{
+       int const x0 = xo(bv) + ADD_TO_TABULAR_WIDTH;
+       return x < x0 || x > x0 + tabular.width();
+}
+
+
+bool InsetTabular::hitSelectColumn(BufferView const & bv, int y) const
+{
+       int const y0 = yo(bv) - tabular.rowAscent(0) + tabular.offsetVAlignment();
+       // FIXME: using ADD_TO_TABULAR_WIDTH is not really correct since
+       // there is no margin added vertically to tabular insets.
+       // However, it works for now.
+       return y < y0 + ADD_TO_TABULAR_WIDTH || y > y0 + tabular.height() - ADD_TO_TABULAR_WIDTH;
+}
+
+
+bool InsetTabular::clickable(BufferView const & bv, int x, int y) const
+{
+       return hitSelectRow(bv, x) || hitSelectColumn(bv, y);
 }
 
 
@@ -3965,8 +4028,7 @@ void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
        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()) {
+               if (hitSelectRow(cur.bv(), cmd.x())) {
                        row_type r = rowFromY(cur, cmd.y());
                        cur.idx() = tabular.getFirstCellInRow(r);
                        cur.pit() = 0;
@@ -3975,15 +4037,13 @@ void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
                        cur.idx() = tabular.getLastCellInRow(r);
                        cur.pit() = cur.lastpit();
                        cur.pos() = cur.lastpos();
-                       cur.setSelection(true);
+                       cur.selection(true);
                        bvcur = cur;
                        rowselect_ = true;
                        break;
                }
                // select column
-               int const y0 = yo(cur.bv()) - tabular.rowAscent(0) + offset_valign_;
-               if (cmd.y() < y0 + ADD_TO_TABULAR_WIDTH
-                       || cmd.y() > y0 + tabular.height()) {
+               if (hitSelectColumn(cur.bv(), cmd.y())) {
                        col_type c = columnFromX(cur, cmd.x());
                        cur.idx() = tabular.cellIndex(0, c);
                        cur.pit() = 0;
@@ -3992,7 +4052,7 @@ void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
                        cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
                        cur.pit() = cur.lastpit();
                        cur.pos() = cur.lastpos();
-                       cur.setSelection(true);
+                       cur.selection(true);
                        bvcur = cur;
                        colselect_ = true;
                        break;
@@ -4025,7 +4085,7 @@ void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
                                cur.pit() = 0;
                                cur.pos() = 0;
                                bvcur.setCursor(cur);
-                               bvcur.setSelection(true);
+                               bvcur.selection(true);
                                break;
                        }
                        // select (additional) column
@@ -4037,7 +4097,7 @@ void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
                                cur.pit() = 0;
                                cur.pos() = 0;
                                bvcur.setCursor(cur);
-                               bvcur.setSelection(true);
+                               bvcur.selection(true);
                                break;
                        }
                        // only update if selection changes
@@ -4046,7 +4106,7 @@ void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
                                cur.noScreenUpdate();
                        setCursorFromCoordinates(cur, cmd.x(), cmd.y());
                        bvcur.setCursor(cur);
-                       bvcur.setSelection(true);
+                       bvcur.selection(true);
                        // if this is a multicell selection, we just set the cursor to
                        // the beginning of the cell's text.
                        if (bvcur.selIsMultiCell()) {
@@ -4063,12 +4123,12 @@ void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
 
        case LFUN_CELL_BACKWARD:
                movePrevCell(cur);
-               cur.setSelection(false);
+               cur.selection(false);
                break;
 
        case LFUN_CELL_FORWARD:
                moveNextCell(cur);
-               cur.setSelection(false);
+               cur.selection(false);
                break;
 
        case LFUN_CHAR_FORWARD_SELECT:
@@ -4265,41 +4325,6 @@ void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
                cur.screenUpdateFlags(Update::Force | Update::FitCursor);
                break;
 
-//     case LFUN_SCREEN_DOWN: {
-//             //if (hasSelection())
-//             //      cur.selection() = false;
-//             col_type const col = tabular.cellColumn(cur.idx());
-//             int const t =   cur.bv().top_y() + cur.bv().height();
-//             if (t < yo() + tabular.getHeightOfTabular()) {
-//                     cur.bv().scrollDocView(t, true);
-//                     cur.idx() = tabular.cellBelow(first_visible_cell_) + col;
-//             } else {
-//                     cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
-//             }
-//             cur.par() = 0;
-//             cur.pos() = 0;
-//             break;
-//     }
-//
-//     case LFUN_SCREEN_UP: {
-//             //if (hasSelection())
-//             //      cur.selection() = false;
-//             col_type const col = tabular.cellColumn(cur.idx());
-//             int const t =   cur.bv().top_y() + cur.bv().height();
-//             if (yo() < 0) {
-//                     cur.bv().scrollDocView(t, true);
-//                     if (yo() > 0)
-//                             cur.idx() = col;
-//                     else
-//                             cur.idx() = tabular.cellBelow(first_visible_cell_) + col;
-//             } else {
-//                     cur.idx() = col;
-//             }
-//             cur.par() = cur.lastpar();
-//             cur.pos() = cur.lastpos();
-//             break;
-//     }
-
        case LFUN_LAYOUT_TABULAR:
                cur.bv().showDialog("tabular");
                break;
@@ -4922,7 +4947,7 @@ bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
                }
                // check if there is already a caption
                bool have_caption = false;
-               InsetTableCell itc = InsetTableCell(*tabular.cellInset(cur.idx()).get());
+               InsetTableCell itc = InsetTableCell(*tabular.cellInset(cur.idx()));
                ParagraphList::const_iterator pit = itc.paragraphs().begin();
                ParagraphList::const_iterator pend = itc.paragraphs().end();
                for (; pit != pend; ++pit) {
@@ -5093,13 +5118,7 @@ docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
 void InsetTabular::validate(LaTeXFeatures & features) const
 {
        tabular.validate(features);
-       // FIXME XHTML
-       // It'd be better to be able to get this from an InsetLayout, but at present
-       // InsetLayouts do not seem really to work for things that aren't InsetTexts.
-       if (features.runparams().flavor == OutputParams::HTML)
-               features.addCSSSnippet(
-                       "table { border: 1px solid black; display: inline-block; }\n"
-                       "td { border: 1px solid black; padding: 0.5ex; }");
+       features.useInsetLayout(getLayout());
 }
 
 
@@ -5123,7 +5142,7 @@ void InsetTabular::cursorPos(BufferView const & bv,
        // y offset     correction
        y += cellYPos(sl.idx());
        y += tabular.textVOffset(sl.idx());
-       y += offset_valign_;
+       y += tabular.offsetVAlignment();
 
        // x offset correction
        x += cellXPos(sl.idx());
@@ -5166,7 +5185,6 @@ int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
 {
        //lyxerr << "InsetTabular::editXY: " << this << endl;
-       cur.setSelection(false);
        cur.push(*this);
        cur.idx() = getNearestCell(cur.bv(), x, y);
        return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
@@ -5525,7 +5543,7 @@ void InsetTabular::tabularFeatures(Cursor & cur,
                cur.idx() = tabular.cellIndex(sel_row_start, column);
                cur.pit() = 0;
                cur.pos() = 0;
-               cur.setSelection(false);
+               cur.selection(false);
                break;
 
        case Tabular::DELETE_COLUMN:
@@ -5548,7 +5566,7 @@ void InsetTabular::tabularFeatures(Cursor & cur,
                cur.idx() = tabular.cellIndex(row, sel_col_start);
                cur.pit() = 0;
                cur.pos() = 0;
-               cur.setSelection(false);
+               cur.selection(false);
                break;
 
        case Tabular::COPY_ROW:
@@ -5664,7 +5682,7 @@ void InsetTabular::tabularFeatures(Cursor & cur,
                                                   tabular.rightLine(cur.selEnd().idx()));
                cur.pit() = 0;
                cur.pos() = 0;
-               cur.setSelection(false);
+               cur.selection(false);
                break;
        }
 
@@ -5721,7 +5739,7 @@ void InsetTabular::tabularFeatures(Cursor & cur,
                                                tabular.getAlignment(cur.selEnd().idx()));
                cur.pit() = 0;
                cur.pos() = 0;
-               cur.setSelection(false);
+               cur.selection(false);
                break;
        }
 
@@ -5926,7 +5944,7 @@ void InsetTabular::tabularFeatures(Cursor & cur,
                cur.idx() = tabular.setLTCaption(row, true);
                cur.pit() = 0;
                cur.pos() = 0;
-               cur.setSelection(false);
+               cur.selection(false);
                // If a row is set as caption, then also insert
                // a caption. Otherwise the LaTeX output is broken.
                // Select cell if it is non-empty
@@ -5942,7 +5960,7 @@ void InsetTabular::tabularFeatures(Cursor & cur,
                cur.idx() = tabular.setLTCaption(row, false);
                cur.pit() = 0;
                cur.pos() = 0;
-               cur.setSelection(false);
+               cur.selection(false);
                FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
                if (lyx::getStatus(fr).enabled())
                        lyx::dispatch(fr);