]> git.lyx.org Git - lyx.git/blobdiff - src/tabular.C
Clean-up FileFilterList API.
[lyx.git] / src / tabular.C
index b5067a263c96e71cf03027c2669a6433e31b5dad..6343b925aef8d377bc08f34a501eddf5192ad7ca 100644 (file)
@@ -1,55 +1,64 @@
-/* This file is part of
- * ======================================================
+/**
+ * \file tabular.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
  *
- *           LyX, The Document Processor
+ * \author Lars Gullik Bjønnes
+ * \author Matthias Ettrich
+ * \author José Matos
+ * \author Jean-Marc Lasgouttes
+ * \author Angus Leeming
+ * \author John Levon
+ * \author André Pönitz
+ * \author Jürgen Vigna
  *
- *           Copyright 2000-2002 The LyX Team.
- *
- *           @author: Jürgen Vigna
- *
- * ======================================================
+ * Full author contact details are available in file CREDITS.
  */
 
 #include <config.h>
 
 #include "tabular.h"
-#include "LaTeXFeatures.h"
-#include "Lsstream.h"
+
 #include "buffer.h"
+#include "bufferparams.h"
 #include "debug.h"
-#include "gettext.h"
+#include "LaTeXFeatures.h"
 #include "lyxlex.h"
-#include "vspace.h"
+#include "outputparams.h"
+#include "paragraph.h"
 
 #include "insets/insettabular.h"
-#include "insets/insettext.h"
 
-#include "support/LAssert.h"
-#include "support/LIstream.h"
 #include "support/lstrings.h"
-#include "support/lyxmanip.h"
 #include "support/tostr.h"
 
-#include "frontends/Alert.h"
-#include "frontends/Painter.h"
+#include <sstream>
 
-#include <algorithm>
-#include <cstdlib>
+using lyx::support::ltrim;
+using lyx::support::prefixIs;
+using lyx::support::rtrim;
+using lyx::support::strToInt;
+using lyx::support::suffixIs;
 
-using namespace lyx::support;
+using boost::shared_ptr;
+using boost::dynamic_pointer_cast;
 
 using std::abs;
-using std::ostream;
-using std::istream;
+using std::endl;
 using std::getline;
 using std::max;
-using std::endl;
+
+using std::istream;
+using std::ostream;
+using std::ostringstream;
 using std::vector;
+using std::string;
 
 #ifndef CXX_GLOBAL_CSTD
 using std::strlen;
 #endif
 
+
 namespace {
 
 int const WIDTH_OF_LINE = 5;
@@ -117,8 +126,8 @@ string const tostr(LyXTabular::VAlignment const & num)
        switch (num) {
        case LyXTabular::LYX_VALIGN_TOP:
                return "top";
-       case LyXTabular::LYX_VALIGN_CENTER:
-               return "center";
+       case LyXTabular::LYX_VALIGN_MIDDLE:
+               return "middle";
        case LyXTabular::LYX_VALIGN_BOTTOM:
                return "bottom";
        }
@@ -139,6 +148,7 @@ string const tostr(LyXTabular::BoxType const & num)
        return string();
 }
 
+
 // I would have liked a fromstr template a lot better. (Lgb)
 bool string2type(string const str, LyXAlignment & num)
 {
@@ -162,8 +172,8 @@ bool string2type(string const str, LyXTabular::VAlignment & num)
 {
        if (str == "top")
                num = LyXTabular::LYX_VALIGN_TOP;
-       else if (str == "center")
-               num = LyXTabular::LYX_VALIGN_CENTER;
+       else if (str == "middle" )
+               num = LyXTabular::LYX_VALIGN_MIDDLE;
        else if (str == "bottom")
                num = LyXTabular::LYX_VALIGN_BOTTOM;
        else
@@ -198,7 +208,7 @@ bool string2type(string const str, bool & num)
 }
 
 
-bool getTokenValue(string const & str, const char * token, string & ret)
+bool getTokenValue(string const & str, char const * token, string & ret)
 {
        ret.erase();
        size_t token_length = strlen(token);
@@ -209,18 +219,18 @@ bool getTokenValue(string const & str, const char * token, string & ret)
                return false;
        pos += token_length + 1;
        char ch = str[pos];
-       if ((ch != '"') && (ch != '\'')) { // only read till next space
+       if (ch != '"' && ch != '\'') { // only read till next space
                ret += ch;
                ch = ' ';
        }
-       while ((pos < str.length() - 1) && (str[++pos] != ch))
+       while (pos < str.length() - 1 && str[++pos] != ch)
                ret += str[pos];
 
        return true;
 }
 
 
-bool getTokenValue(string const & str, const char * token, int & num)
+bool getTokenValue(string const & str, char const * token, int & num)
 {
        string tmp;
        num = 0;
@@ -231,56 +241,46 @@ bool getTokenValue(string const & str, const char * token, int & num)
 }
 
 
-bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
+bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
 {
        string tmp;
-       if (!getTokenValue(str, token, tmp))
-               return false;
-       return string2type(tmp, num);
+       return getTokenValue(str, token, tmp) && string2type(tmp, num);
 }
 
 
-bool getTokenValue(string const & str, const char * token,
+bool getTokenValue(string const & str, char const * token,
                                   LyXTabular::VAlignment & num)
 {
        string tmp;
-       if (!getTokenValue(str, token, tmp))
-               return false;
-       return string2type(tmp, num);
+       return getTokenValue(str, token, tmp) && string2type(tmp, num);
 }
 
 
-bool getTokenValue(string const & str, const char * token,
+bool getTokenValue(string const & str, char const * token,
                                   LyXTabular::BoxType & num)
 {
        string tmp;
-       if (!getTokenValue(str, token, tmp))
-               return false;
-       return string2type(tmp, num);
+       return getTokenValue(str, token, tmp) && string2type(tmp, num);
 }
 
 
-bool getTokenValue(string const & str, const char * token, bool & flag)
+bool getTokenValue(string const & str, char const * token, bool & flag)
 {
        // set the flag always to false as this should be the default for bools
        // not in the file-format.
        flag = false;
        string tmp;
-       if (!getTokenValue(str, token, tmp))
-               return false;
-       return string2type(tmp, flag);
+       return getTokenValue(str, token, tmp) && string2type(tmp, flag);
 }
 
 
-bool getTokenValue(string const & str, const char * token, LyXLength & len)
+bool getTokenValue(string const & str, char const * token, LyXLength & len)
 {
        // set the lenght to be zero() as default as this it should be if not
        // in the file format.
        len = LyXLength();
        string tmp;
-       if (!getTokenValue(str, token, tmp))
-               return false;
-       return isValidLength(tmp, &len);
+       return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
 }
 
 
@@ -298,164 +298,174 @@ void l_getline(istream & is, string & str)
 
 /// Define a few methods for the inner structs
 
-LyXTabular::cellstruct::cellstruct(BufferParams const & bg)
-       : inset(bg)
-{
-       cellno = 0;
-       width_of_cell = 0;
-       multicolumn = LyXTabular::CELL_NORMAL;
-       alignment = LYX_ALIGN_CENTER;
-       valignment = LYX_VALIGN_TOP;
-       top_line = true;
-       bottom_line = false;
-       left_line = true;
-       right_line = false;
-       usebox = BOX_NONE;
-       rotate = false;
+LyXTabular::cellstruct::cellstruct(BufferParams const & bp)
+       : cellno(0),
+         width_of_cell(0),
+         multicolumn(LyXTabular::CELL_NORMAL),
+         alignment(LYX_ALIGN_CENTER),
+         valignment(LYX_VALIGN_TOP),
+         top_line(true),
+         bottom_line(false),
+         left_line(true),
+         right_line(false),
+         usebox(BOX_NONE),
+         rotate(false),
+         inset(new InsetText(bp))
+{}
+
+
+LyXTabular::cellstruct::cellstruct(cellstruct const & cs)
+       : cellno(cs.cellno),
+         width_of_cell(cs.width_of_cell),
+         multicolumn(cs.multicolumn),
+         alignment(cs.alignment),
+         valignment(cs.valignment),
+         top_line(cs.top_line),
+         bottom_line(cs.bottom_line),
+         left_line(cs.left_line),
+         right_line(cs.right_line),
+         usebox(cs.usebox),
+         rotate(cs.rotate),
+         align_special(cs.align_special),
+         p_width(cs.p_width),
+         inset(dynamic_cast<InsetText*>(cs.inset->clone().release()))
+{}
+
+
+LyXTabular::cellstruct &
+LyXTabular::cellstruct::operator=(cellstruct cs)
+{
+       swap(cs);
+       return *this;
+}
+
+
+void
+LyXTabular::cellstruct::swap(cellstruct & rhs)
+{
+       std::swap(cellno, rhs.cellno);
+       std::swap(width_of_cell, rhs.width_of_cell);
+       std::swap(multicolumn, rhs.multicolumn);
+       std::swap(alignment, rhs.alignment);
+       std::swap(valignment, rhs.valignment);
+       std::swap(top_line, rhs.top_line);
+       std::swap(bottom_line, rhs.bottom_line);
+       std::swap(left_line, rhs.left_line);
+       std::swap(right_line, rhs.right_line);
+       std::swap(usebox, rhs.usebox);
+       std::swap(rotate, rhs.rotate);
+       std::swap(align_special, rhs.align_special);
+       p_width.swap(rhs.p_width);
+       inset.swap(rhs.inset);
 }
 
 
 LyXTabular::rowstruct::rowstruct()
-{
-       top_line = true;
-       bottom_line = false;
-       ascent_of_row = 0;
-       descent_of_row = 0;
-       endhead = false;
-       endfirsthead = false;
-       endfoot = false;
-       endlastfoot = false;
-       newpage = false;
-}
+       : ascent_of_row(0),
+         descent_of_row(0),
+         top_line(true),
+         bottom_line(false),
+         endhead(false),
+         endfirsthead(false),
+         endfoot(false),
+         endlastfoot(false),
+         newpage(false)
+{}
 
 
 LyXTabular::columnstruct::columnstruct()
+       : alignment(LYX_ALIGN_CENTER),
+         valignment(LYX_VALIGN_TOP),
+         left_line(true),
+         right_line(false),
+         width_of_column(0)
 {
-       left_line = true;
-       right_line = false;
-       alignment = LYX_ALIGN_CENTER;
-       valignment = LYX_VALIGN_TOP;
-       width_of_column = 0;
 }
 
 
 LyXTabular::ltType::ltType()
-{
-       topDL = false;
-       bottomDL = false;
-       empty = false;
-}
+       : topDL(false),
+         bottomDL(false),
+         empty(false)
+{}
 
 
-LyXTabular::LyXTabular(BufferParams const & bp,
-                      InsetTabular * inset, int rows_arg, int columns_arg)
+LyXTabular::LyXTabular(BufferParams const & bp, int rows_arg, int columns_arg)
 {
-       owner_ = inset;
-       cur_cell = -1;
        init(bp, rows_arg, columns_arg);
 }
 
 
-LyXTabular::LyXTabular(BufferParams const & bp,
-                      InsetTabular * inset, LyXTabular const & lt)
-{
-       owner_ = inset;
-       cur_cell = -1;
-       init(bp, lt.rows_, lt.columns_, &lt);
-}
-
-
-LyXTabular::LyXTabular(Buffer const * buf, InsetTabular * inset, LyXLex & lex)
-{
-       owner_ = inset;
-       cur_cell = -1;
-       read(buf, lex);
-}
-
-
 // activates all lines and sets all widths to 0
-void LyXTabular::init(BufferParams const & bp,
-                     int rows_arg, int columns_arg, LyXTabular const * lt)
+void LyXTabular::init(BufferParams const & bp, int rows_arg, int columns_arg)
 {
-       rows_ = rows_arg;
+       rows_    = rows_arg;
        columns_ = columns_arg;
-       row_info = row_vector(rows_, rowstruct());
-       column_info = column_vector(columns_, columnstruct());
+       row_info = row_vector(rows_);
+       column_info = column_vector(columns_);
        cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
+       row_info.reserve(10);
+       column_info.reserve(10);
+       cell_info.reserve(100);
+       fixCellNums();
+       for (int i = 0; i < rows_; ++i)
+               cell_info[i].back().right_line = true;
+       row_info.back().bottom_line = true;
+       row_info.front().bottom_line = true;
+       column_info.back().right_line = true;
+       is_long_tabular = false;
+       rotate = false;
+}
 
-       if (lt) {
-               operator=(*lt);
-               return;
-       }
 
+void LyXTabular::fixCellNums()
+{
        int cellno = 0;
        for (int i = 0; i < rows_; ++i) {
                for (int j = 0; j < columns_; ++j) {
-                       cell_info[i][j].inset.setOwner(owner_);
-                       cell_info[i][j].inset.setDrawFrame(0, InsetText::LOCKED);
+                       // When debugging it can be nice to set
+                       // this to true.
+                       cell_info[i][j].inset->setDrawFrame(false);
                        cell_info[i][j].cellno = cellno++;
                }
                cell_info[i].back().right_line = true;
        }
-       row_info.back().bottom_line = true;
-       row_info.front().bottom_line = true;
-
-       for (int i = 0; i < columns_; ++i) {
-               calculate_width_of_column(i);
-       }
-       column_info.back().right_line = true;
-
-       calculate_width_of_tabular();
 
-       rowofcell = vector<int>();
-       columnofcell = vector<int>();
        set_row_column_number_info();
-       is_long_tabular = false;
-       rotate = false;
 }
 
 
-void LyXTabular::appendRow(BufferParams const & bp, int cell)
+void LyXTabular::appendRow(BufferParams const & bp, int const cell)
 {
        ++rows_;
 
-       int row = row_of_cell(cell);
+       int const row = row_of_cell(cell);
 
        row_vector::iterator rit = row_info.begin() + row;
        row_info.insert(rit, rowstruct());
        // now set the values of the row before
        row_info[row] = row_info[row + 1];
 
-#if 0
-       cell_vvector::iterator cit = cell_info.begin() + row;
-       cell_info.insert(cit, vector<cellstruct>(columns_, cellstruct(bp)));
-#else
-       cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
-                                                             cellstruct(bp)));
+       cell_vvector old(rows_ - 1);
+       for (int i = 0; i < rows_ - 1; ++i)
+               swap(cell_info[i], old[i]);
 
-       for (int i = 0; i <= row; ++i) {
-               for (int j = 0; j < columns_; ++j) {
-                       c_info[i][j] = cell_info[i][j];
-               }
-       }
-       for (int i = row + 1; i < rows_; ++i) {
-               for (int j = 0; j < columns_; ++j) {
-                       c_info[i][j] = cell_info[i-1][j];
-               }
-       }
-       cell_info = c_info;
-       ++row;
-       for (int j = 0; j < columns_; ++j) {
-               cell_info[row][j].inset.clear(false);
-               if (bp.tracking_changes)
-                       cell_info[row][j].inset.markNew(true);
-       }
-#endif
-       Reinit();
+       cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
+
+       for (int i = 0; i <= row; ++i)
+               swap(cell_info[i], old[i]);
+       for (int i = row + 2; i < rows_; ++i)
+               swap(cell_info[i], old[i - 1]);
+
+       if (bp.tracking_changes)
+               for (int j = 0; j < columns_; ++j)
+                       cell_info[row + 1][j].inset->markNew(true);
+
+       set_row_column_number_info();
 }
 
 
-void LyXTabular::deleteRow(int row)
+void LyXTabular::deleteRow(int const row)
 {
        // Not allowed to delete last row
        if (rows_ == 1)
@@ -464,16 +474,14 @@ void LyXTabular::deleteRow(int row)
        row_info.erase(row_info.begin() + row);
        cell_info.erase(cell_info.begin() + row);
        --rows_;
-       Reinit();
+       fixCellNums();
 }
 
 
-void LyXTabular::appendColumn(BufferParams const & bp, int cell)
+void LyXTabular::appendColumn(BufferParams const & bp, int const cell)
 {
        ++columns_;
 
-       cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
-                                                             cellstruct(bp)));
        int const column = column_of_cell(cell);
        column_vector::iterator cit = column_info.begin() + column + 1;
        column_info.insert(cit, columnstruct());
@@ -481,35 +489,27 @@ void LyXTabular::appendColumn(BufferParams const & bp, int cell)
        column_info[column + 1] = column_info[column];
 
        for (int i = 0; i < rows_; ++i) {
-               for (int j = 0; j <= column; ++j) {
-                       c_info[i][j] = cell_info[i][j];
-               }
-               for (int j = column + 1; j < columns_; ++j) {
-                       c_info[i][j] = cell_info[i][j - 1];
-               }
+               cell_info[i].insert(cell_info[i].begin() + column + 1, cellstruct(bp));
+
                // care about multicolumns
-               if (c_info[i][column + 1].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
-               {
-                       c_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
-               }
-               if (column + 2 >= columns_ ||
-                       c_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
-               {
-                       c_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
-               }
+               if (cell_info[i][column + 1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
+                       cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
+
+               if (column + 2 >= columns_
+                   || cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
+                       cell_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
        }
-       cell_info = c_info;
        //++column;
        for (int i = 0; i < rows_; ++i) {
-               cell_info[i][column + 1].inset.clear(false);
+               cell_info[i][column + 1].inset->clear(false);
                if (bp.tracking_changes)
-                       cell_info[i][column + 1].inset.markNew(true);
+                       cell_info[i][column + 1].inset->markNew(true);
        }
-       Reinit();
+       fixCellNums();
 }
 
 
-void LyXTabular::deleteColumn(int column)
+void LyXTabular::deleteColumn(int const column)
 {
        // Not allowed to delete last column
        if (columns_ == 1)
@@ -519,37 +519,11 @@ void LyXTabular::deleteColumn(int column)
        for (int i = 0; i < rows_; ++i)
                cell_info[i].erase(cell_info[i].begin() + column);
        --columns_;
-       Reinit();
+       fixCellNums();
 }
 
 
-void LyXTabular::reinit()
-{
-       Reinit(false);
-}
-
-
-void LyXTabular::Reinit(bool reset_widths)
-{
-       if (reset_widths) {
-               for (int i = 0; i < rows_; ++i) {
-                       for (int j = 0; j < columns_; ++j) {
-                               cell_info[i][j].width_of_cell = 0;
-                               cell_info[i][j].inset.setOwner(owner_);
-                       }
-               }
-       }
-
-       for (int i = 0; i < columns_; ++i)
-               calculate_width_of_column(i);
-
-       calculate_width_of_tabular();
-
-       set_row_column_number_info();
-}
-
-
-void LyXTabular::set_row_column_number_info(bool oldformat)
+void LyXTabular::set_row_column_number_info()
 {
        numberofcells = -1;
        for (int row = 0; row < rows_; ++row) {
@@ -575,6 +549,7 @@ void LyXTabular::set_row_column_number_info(bool oldformat)
                } while (column < columns_ &&
                                 cell_info[row][column].multicolumn
                                 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
+
                if (column == columns_) {
                        column = 0;
                        ++row;
@@ -585,15 +560,7 @@ void LyXTabular::set_row_column_number_info(bool oldformat)
                for (int column = 0; column < columns_; ++column) {
                        if (isPartOfMultiColumn(row,column))
                                continue;
-                       // now set the right line of multicolumns right for oldformat read
-                       if (oldformat &&
-                               cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
-                       {
-                               int cn = cells_in_multicolumn(cell_info[row][column].cellno);
-                               cell_info[row][column].right_line =
-                                       cell_info[row][column+cn-1].right_line;
-                       }
-                       cell_info[row][column].inset.setAutoBreakRows(
+                       cell_info[row][column].inset->setAutoBreakRows(
                                !getPWidth(getCellNumber(row, column)).zero());
                }
        }
@@ -606,7 +573,7 @@ int LyXTabular::getNumberOfCells() const
 }
 
 
-int LyXTabular::numberOfCellsInRow(int cell) const
+int LyXTabular::numberOfCellsInRow(int const cell) const
 {
        int const row = row_of_cell(cell);
        int result = 0;
@@ -618,7 +585,7 @@ int LyXTabular::numberOfCellsInRow(int cell) const
 
 
 // returns 1 if there is a topline, returns 0 if not
-bool LyXTabular::topLine(int cell, bool onlycolumn) const
+bool LyXTabular::topLine(int const cell, bool const onlycolumn) const
 {
        if (!onlycolumn && isMultiColumn(cell))
                return cellinfo_of_cell(cell).top_line;
@@ -652,7 +619,7 @@ bool LyXTabular::leftLine(int cell, bool onlycolumn) const
 bool LyXTabular::rightLine(int cell, bool onlycolumn) const
 {
        if (!onlycolumn && isMultiColumn(cell) &&
-               (isLastCellInRow(cell) || isMultiColumn(cell+1)))
+               (isLastCellInRow(cell) || isMultiColumn(cell + 1)))
        {
                if (cellinfo_of_cell(cell).align_special.empty())
                        return cellinfo_of_cell(cell).right_line;
@@ -701,7 +668,7 @@ bool LyXTabular::leftAlreadyDrawn(int cell) const
 
 bool LyXTabular::isLastRow(int cell) const
 {
-       return (row_of_cell(cell) == rows_ - 1);
+       return row_of_cell(cell) == rows_ - 1;
 }
 
 
@@ -831,7 +798,7 @@ void LyXTabular::recalculateMulticolumnsOfColumn(int column)
 
 
 // returns 1 if a complete update is necessary, otherwise 0
-bool LyXTabular::setWidthOfCell(int cell, int new_width)
+void LyXTabular::setWidthOfCell(int cell, int new_width)
 {
        int const row = row_of_cell(cell);
        int const column1 = column_of_cell(cell);
@@ -843,13 +810,13 @@ bool LyXTabular::setWidthOfCell(int cell, int new_width)
                column1 < columns_ - 1 &&
                leftLine(cell_info[row][column1+1].cellno, true))
        {
-               // additional width
                add_width = WIDTH_OF_LINE;
        }
-       if (getWidthOfCell(cell) == new_width + 2 * WIDTH_OF_LINE + add_width) {
-               return false;
-       }
-       if (isMultiColumn(cell, true)) {
+
+       if (getWidthOfCell(cell) == new_width + 2 * WIDTH_OF_LINE + add_width)
+               return;
+
+       if (isMultiColumnReal(cell)) {
                tmp = setWidthOfMulticolCell(cell, new_width);
        } else {
                width = new_width + 2 * WIDTH_OF_LINE + add_width;
@@ -862,9 +829,7 @@ bool LyXTabular::setWidthOfCell(int cell, int new_width)
                for (int i = 0; i < columns_; ++i)
                        calculate_width_of_column(i);
                calculate_width_of_tabular();
-               return true;
        }
-       return false;
 }
 
 
@@ -894,21 +859,19 @@ void LyXTabular::setColumnPWidth(int cell, LyXLength const & width)
        for (int i = 0; i < rows_; ++i) {
                int const cell = getCellNumber(i, j);
                // because of multicolumns
-               getCellInset(cell).setAutoBreakRows(!getPWidth(cell).zero());
+               getCellInset(cell)->setAutoBreakRows(!getPWidth(cell).zero());
        }
 }
 
 
 bool LyXTabular::setMColumnPWidth(int cell, LyXLength const & width)
 {
-       bool const flag = !width.zero();
+       if (!isMultiColumn(cell))
+               return false;
 
        cellinfo_of_cell(cell).p_width = width;
-       if (isMultiColumn(cell)) {
-               getCellInset(cell).setAutoBreakRows(flag);
-               return true;
-       }
-       return false;
+       getCellInset(cell)->setAutoBreakRows(!width.zero());
+       return true;
 }
 
 
@@ -1099,7 +1062,7 @@ bool LyXTabular::calculate_width_of_column_NMC(int column)
        int max = 0;
        for (int i = 0; i < rows_; ++i) {
                int cell = getCellNumber(i, column);
-               bool ismulti = isMultiColumn(cell, true);
+               bool ismulti = isMultiColumnReal(cell);
                if ((!ismulti || column == right_column_of_cell(cell)) &&
                        cell_info[i][column].width_of_cell > max)
                {
@@ -1150,7 +1113,7 @@ int LyXTabular::right_column_of_cell(int cell) const
 }
 
 
-void LyXTabular::write(Buffer const * buf, ostream & os) const
+void LyXTabular::write(Buffer const & buf, ostream & os) const
 {
        // header line
        os << "<lyxtabular"
@@ -1208,8 +1171,8 @@ void LyXTabular::write(Buffer const * buf, ostream & os) const
                           << write_attribute("special", cell_info[i][j].align_special)
                           << ">\n";
                        os << "\\begin_inset ";
-                       cell_info[i][j].inset.write(buf, os);
-                       os << "\n\\end_inset \n"
+                       cell_info[i][j].inset->write(buf, os);
+                       os << "\n\\end_inset\n"
                           << "</cell>\n";
                }
                os << "</row>\n";
@@ -1296,7 +1259,7 @@ void LyXTabular::setHeaderFooterRows(int hr, int fhr, int fr, int lfr)
 }
 
 
-void LyXTabular::read(Buffer const * buf, LyXLex & lex)
+void LyXTabular::read(Buffer const & buf, LyXLex & lex)
 {
        string line;
        istream & is = lex.getStream();
@@ -1304,14 +1267,14 @@ void LyXTabular::read(Buffer const * buf, LyXLex & lex)
        l_getline(is, line);
        if (!prefixIs(line, "<lyxtabular ")
                && !prefixIs(line, "<LyXTabular ")) {
-               Assert(false);
+               BOOST_ASSERT(false);
                return;
        }
 
        int version;
        if (!getTokenValue(line, "version", version))
                return;
-       Assert(version >= 2);
+       BOOST_ASSERT(version >= 2);
 
        int rows_arg;
        if (!getTokenValue(line, "rows", rows_arg))
@@ -1319,7 +1282,7 @@ void LyXTabular::read(Buffer const * buf, LyXLex & lex)
        int columns_arg;
        if (!getTokenValue(line, "columns", columns_arg))
                return;
-       init(buf->params, rows_arg, columns_arg);
+       init(buf.params(), rows_arg, columns_arg);
        l_getline(is, line);
        if (!prefixIs(line, "<features")) {
                lyxerr << "Wrong tabular format (expected <features ...> got"
@@ -1408,7 +1371,7 @@ void LyXTabular::read(Buffer const * buf, LyXLex & lex)
                        getTokenValue(line, "special", cell_info[i][j].align_special);
                        l_getline(is, line);
                        if (prefixIs(line, "\\begin_inset")) {
-                               cell_info[i][j].inset.read(buf, lex);
+                               cell_info[i][j].inset->read(buf, lex);
                                l_getline(is, line);
                        }
                        if (!prefixIs(line, "</cell>")) {
@@ -1431,9 +1394,15 @@ void LyXTabular::read(Buffer const * buf, LyXLex & lex)
 }
 
 
-bool LyXTabular::isMultiColumn(int cell, bool real) const
+bool LyXTabular::isMultiColumn(int cell) const
 {
-       return (!real || column_of_cell(cell) != right_column_of_cell(cell)) &&
+       return cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
+}
+
+
+bool LyXTabular::isMultiColumnReal(int cell) const
+{
+       return column_of_cell(cell) != right_column_of_cell(cell) &&
                        cellinfo_of_cell(cell).multicolumn != LyXTabular::CELL_NORMAL;
 }
 
@@ -1455,8 +1424,8 @@ void LyXTabular::setMultiColumn(Buffer * buffer, int cell, int number)
        for (int i = 1; i < number; ++i) {
                cellstruct & cs1 = cellinfo_of_cell(cell + i);
                cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
-               cs.inset.appendParagraphs(buffer, cs1.inset.paragraphs);
-               cs1.inset.clear(false);
+               cs.inset->appendParagraphs(buffer, cs1.inset->paragraphs());
+               cs1.inset->clear(false);
        }
        set_row_column_number_info();
 }
@@ -1595,7 +1564,7 @@ int LyXTabular::getLastCellBelow(int cell) const
 
 int LyXTabular::getCellNumber(int row, int column) const
 {
-       Assert(column >= 0 || column < columns_ || row >= 0 || row < rows_);
+       BOOST_ASSERT(column >= 0 || column < columns_ || row >= 0 || row < rows_);
        return cell_info[row][column].cellno;
 }
 
@@ -1734,21 +1703,19 @@ bool LyXTabular::haveLTLastFoot() const
 
 // end longtable support functions
 
-bool LyXTabular::setAscentOfRow(int row, int height)
+void LyXTabular::setAscentOfRow(int row, int height)
 {
        if (row >= rows_ || row_info[row].ascent_of_row == height)
-               return false;
+               return;
        row_info[row].ascent_of_row = height;
-       return true;
 }
 
 
-bool LyXTabular::setDescentOfRow(int row, int height)
+void LyXTabular::setDescentOfRow(int row, int height)
 {
        if (row >= rows_ || row_info[row].descent_of_row == height)
-               return false;
+               return;
        row_info[row].descent_of_row = height;
-       return true;
 }
 
 
@@ -1762,8 +1729,7 @@ int LyXTabular::getAscentOfRow(int row) const
 
 int LyXTabular::getDescentOfRow(int row) const
 {
-       if (row >= rows_)
-               return 0;
+       BOOST_ASSERT(row < rows_);
        return row_info[row].descent_of_row;
 }
 
@@ -1780,16 +1746,16 @@ int LyXTabular::getHeightOfTabular() const
 
 bool LyXTabular::isPartOfMultiColumn(int row, int column) const
 {
-       if (row >= rows_ || column >= columns_)
-               return false;
+       BOOST_ASSERT(row < rows_);
+       BOOST_ASSERT(column < columns_);
        return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
 }
 
 
 int LyXTabular::TeXTopHLine(ostream & os, int row) const
 {
-       if (row < 0 || row >= rows_)
-               return 0;
+       BOOST_ASSERT(row >= 0);
+       BOOST_ASSERT(row < rows_);
 
        int const fcell = getFirstCellInRow(row);
        int const n = numberOfCellsInRow(fcell) + fcell;
@@ -1877,7 +1843,7 @@ int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
                                case LYX_VALIGN_TOP:
                                        os << 'p';
                                        break;
-                               case LYX_VALIGN_CENTER:
+                               case LYX_VALIGN_MIDDLE:
                                        os << 'm';
                                        break;
                                case LYX_VALIGN_BOTTOM:
@@ -1914,7 +1880,7 @@ int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
                case LYX_VALIGN_TOP:
                        os << 't';
                        break;
-               case LYX_VALIGN_CENTER:
+               case LYX_VALIGN_MIDDLE:
                        os << 'c';
                        break;
                case LYX_VALIGN_BOTTOM:
@@ -1928,7 +1894,7 @@ int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
                case LYX_VALIGN_TOP:
                        os << 't';
                        break;
-               case LYX_VALIGN_CENTER:
+               case LYX_VALIGN_MIDDLE:
                        os << 'm';
                        break;
                case LYX_VALIGN_BOTTOM:
@@ -1964,8 +1930,8 @@ int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
 }
 
 
-int LyXTabular::TeXLongtableHeaderFooter(ostream & os, Buffer const * buf,
-                                        LatexRunParams const & runparams) const
+int LyXTabular::TeXLongtableHeaderFooter(ostream & os, Buffer const & buf,
+                                        OutputParams const & runparams) const
 {
        if (!is_long_tabular)
                return 0;
@@ -2055,34 +2021,35 @@ int LyXTabular::TeXLongtableHeaderFooter(ostream & os, Buffer const * buf,
 }
 
 
-bool LyXTabular::isValidRow(int const row) const
+bool LyXTabular::isValidRow(int row) const
 {
        if (!is_long_tabular)
                return true;
-       return (!row_info[row].endhead && !row_info[row].endfirsthead &&
-                       !row_info[row].endfoot && !row_info[row].endlastfoot);
+       return !row_info[row].endhead && !row_info[row].endfirsthead &&
+                       !row_info[row].endfoot && !row_info[row].endlastfoot;
 }
 
 
-int LyXTabular::TeXRow(ostream & os, int const i, Buffer const * buf,
-                      LatexRunParams const & runparams) const
+int LyXTabular::TeXRow(ostream & os, int i, Buffer const & buf,
+                      OutputParams const & runparams) const
 {
-       int ret = 0;
        int cell = getCellNumber(i, 0);
 
-       ret += TeXTopHLine(os, i);
+       int ret = TeXTopHLine(os, i);
        for (int j = 0; j < columns_; ++j) {
                if (isPartOfMultiColumn(i, j))
                        continue;
                ret += TeXCellPreamble(os, cell);
-               InsetText & inset = getCellInset(cell);
+               shared_ptr<InsetText> inset = getCellInset(cell);
 
-               bool rtl = inset.paragraphs.begin()->isRightToLeftPar(buf->params) &&
-                       !inset.paragraphs.begin()->empty() && getPWidth(cell).zero();
+               Paragraph const & par = inset->paragraphs().front();
+               bool rtl = par.isRightToLeftPar(buf.params())
+                       && !par.empty()
+                       && getPWidth(cell).zero();
 
                if (rtl)
                        os << "\\R{";
-               ret += inset.latex(buf, os, runparams);
+               ret += inset->latex(buf, os, runparams);
                if (rtl)
                        os << '}';
 
@@ -2100,8 +2067,8 @@ int LyXTabular::TeXRow(ostream & os, int const i, Buffer const * buf,
 }
 
 
-int LyXTabular::latex(Buffer const * buf, ostream & os,
-                     LatexRunParams const & runparams) const
+int LyXTabular::latex(Buffer const & buf, ostream & os,
+                     OutputParams const & runparams) const
 {
        int ret = 0;
 
@@ -2145,7 +2112,7 @@ int LyXTabular::latex(Buffer const * buf, ostream & os,
                                case LYX_VALIGN_TOP:
                                        os << 'p';
                                        break;
-                               case LYX_VALIGN_CENTER:
+                               case LYX_VALIGN_MIDDLE:
                                        os << 'm';
                                        break;
                                case LYX_VALIGN_BOTTOM:
@@ -2208,7 +2175,8 @@ int LyXTabular::latex(Buffer const * buf, ostream & os,
 }
 
 
-int LyXTabular::linuxdoc(Buffer const * buf, ostream & os) const
+int LyXTabular::linuxdoc(Buffer const & buf, ostream & os,
+                        const OutputParams & runparams) const
 {
        os << "<tabular ca=\"";
        for (int i = 0; i < columns_; ++i) {
@@ -2231,9 +2199,9 @@ int LyXTabular::linuxdoc(Buffer const * buf, ostream & os) const
                for (int j = 0; j < columns_; ++j) {
                        if (isPartOfMultiColumn(i, j))
                                continue;
-                       InsetText & inset = getCellInset(cell);
-       
-                       ret += inset.linuxdoc(buf, os);
+                       shared_ptr<InsetText> inset = getCellInset(cell);
+
+                       ret += inset->linuxdoc(buf, os, runparams);
 
                        if (isLastCellInRow(cell)) {
                                os << "@\n";
@@ -2249,7 +2217,8 @@ int LyXTabular::linuxdoc(Buffer const * buf, ostream & os) const
 }
 
 
-int LyXTabular::docbookRow(Buffer const * buf, ostream & os, int row) const
+int LyXTabular::docbookRow(Buffer const & buf, ostream & os, int row,
+                          OutputParams const & runparams) const
 {
        int ret = 0;
        int cell = getFirstCellInRow(row);
@@ -2280,7 +2249,7 @@ int LyXTabular::docbookRow(Buffer const * buf, ostream & os, int row) const
                case LYX_VALIGN_BOTTOM:
                        os << "bottom";
                        break;
-               case LYX_VALIGN_CENTER:
+               case LYX_VALIGN_MIDDLE:
                        os << "middle";
                }
                os << '"';
@@ -2291,7 +2260,7 @@ int LyXTabular::docbookRow(Buffer const * buf, ostream & os, int row) const
                }
 
                os << '>';
-               ret += getCellInset(cell).docbook(buf, os, true);
+               ret += getCellInset(cell)->docbook(buf, os, runparams);
                os << "</entry>\n";
                ++cell;
        }
@@ -2300,8 +2269,8 @@ int LyXTabular::docbookRow(Buffer const * buf, ostream & os, int row) const
 }
 
 
-int LyXTabular::docbook(Buffer const * buf, ostream & os,
-                       bool /*mixcont*/) const
+int LyXTabular::docbook(Buffer const & buf, ostream & os,
+                       OutputParams const & runparams) const
 {
        int ret = 0;
 
@@ -2325,7 +2294,10 @@ int LyXTabular::docbook(Buffer const * buf, ostream & os,
                        os << "center";
                        break;
                }
-               os << "\">\n";
+               os << '"';
+               if (runparams.flavor == OutputParams::XML)
+                       os << '/';
+               os << ">\n";
                ++ret;
        }
 
@@ -2339,7 +2311,7 @@ int LyXTabular::docbook(Buffer const * buf, ostream & os,
                ++ret;
                for (int i = 0; i < rows_; ++i) {
                        if (row_info[i].endhead || row_info[i].endfirsthead) {
-                               ret += docbookRow(buf, os, i);
+                               ret += docbookRow(buf, os, i, runparams);
                        }
                }
                os << "</thead>\n";
@@ -2351,7 +2323,7 @@ int LyXTabular::docbook(Buffer const * buf, ostream & os,
                ++ret;
                for (int i = 0; i < rows_; ++i) {
                        if (row_info[i].endfoot || row_info[i].endlastfoot) {
-                               ret += docbookRow(buf, os, i);
+                               ret += docbookRow(buf, os, i, runparams);
                        }
                }
                os << "</tfoot>\n";
@@ -2366,7 +2338,7 @@ int LyXTabular::docbook(Buffer const * buf, ostream & os,
        ++ret;
        for (int i = 0; i < rows_; ++i) {
                if (isValidRow(i)) {
-                       ret += docbookRow(buf, os, i);
+                       ret += docbookRow(buf, os, i, runparams);
                }
        }
        os << "</tbody>\n";
@@ -2412,7 +2384,8 @@ int LyXTabular::asciiTopHLine(ostream & os, int row,
                }
                int column = column_of_cell(i);
                int len = clen[column];
-               while (isPartOfMultiColumn(row, ++column))
+               while (column < columns_ - 1
+                      && isPartOfMultiColumn(row, ++column))
                        len += clen[column] + 4;
                os << string(len, ch);
                if (topLine(i)) {
@@ -2459,7 +2432,8 @@ int LyXTabular::asciiBottomHLine(ostream & os, int row,
                }
                int column = column_of_cell(i);
                int len = clen[column];
-               while (isPartOfMultiColumn(row, ++column))
+               while (column < columns_ -1
+                      && isPartOfMultiColumn(row, ++column))
                        len += clen[column] + 4;
                os << string(len, ch);
                if (bottomLine(i)) {
@@ -2476,13 +2450,14 @@ int LyXTabular::asciiBottomHLine(ostream & os, int row,
 }
 
 
-int LyXTabular::asciiPrintCell(Buffer const * buf, ostream & os,
+int LyXTabular::asciiPrintCell(Buffer const & buf, ostream & os,
+                              OutputParams const & runparams,
                               int cell, int row, int column,
                               vector<unsigned int> const & clen,
                               bool onlydata) const
 {
        ostringstream sstr;
-       int ret = getCellInset(cell).ascii(buf, sstr, 0);
+       int const ret = getCellInset(cell)->plaintext(buf, sstr, runparams);
 
        if (onlydata) {
                os << sstr.str();
@@ -2496,7 +2471,8 @@ int LyXTabular::asciiPrintCell(Buffer const * buf, ostream & os,
 
        unsigned int len1 = sstr.str().length();
        unsigned int len2 = clen[column];
-       while (isPartOfMultiColumn(row, ++column))
+       while (column < columns_ -1
+              && isPartOfMultiColumn(row, ++column))
                len2 += clen[column] + 4;
        len2 -= len1;
 
@@ -2526,8 +2502,10 @@ int LyXTabular::asciiPrintCell(Buffer const * buf, ostream & os,
 }
 
 
-int LyXTabular::ascii(Buffer const * buf, ostream & os, int const depth,
-                                         bool onlydata, unsigned char delim) const
+int LyXTabular::plaintext(Buffer const & buf, ostream & os,
+                     OutputParams const & runparams,
+                     int const depth,
+                     bool onlydata, unsigned char delim) const
 {
        int ret = 0;
 
@@ -2540,10 +2518,10 @@ int LyXTabular::ascii(Buffer const * buf, ostream & os, int const depth,
                        clen[j] = 0;
                        for (int i = 0; i < rows_; ++i) {
                                int cell = getCellNumber(i, j);
-                               if (isMultiColumn(cell, true))
+                               if (isMultiColumnReal(cell))
                                        continue;
                                ostringstream sstr;
-                               getCellInset(cell).ascii(buf, sstr, 0);
+                               getCellInset(cell)->plaintext(buf, sstr, runparams);
                                if (clen[j] < sstr.str().length())
                                        clen[j] = sstr.str().length();
                        }
@@ -2552,10 +2530,10 @@ int LyXTabular::ascii(Buffer const * buf, ostream & os, int const depth,
                for (int j = 0; j < columns_; ++j) {
                        for (int i = 0; i < rows_; ++i) {
                                int cell = getCellNumber(i, j);
-                               if (!isMultiColumn(cell, true) || isPartOfMultiColumn(i, j))
+                               if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
                                        continue;
                                ostringstream sstr;
-                               getCellInset(cell).ascii(buf, sstr, 0);
+                               getCellInset(cell)->plaintext(buf, sstr, runparams);
                                int len = int(sstr.str().length());
                                int const n = cells_in_multicolumn(cell);
                                for (int k = j; len > 0 && k < j + n - 1; ++k)
@@ -2568,79 +2546,60 @@ int LyXTabular::ascii(Buffer const * buf, ostream & os, int const depth,
        int cell = 0;
        for (int i = 0; i < rows_; ++i) {
                if (!onlydata && asciiTopHLine(os, i, clen))
-                       for (int j = 0; j < depth; ++j)
-                               os << "  ";
+                       os << string(depth * 2, ' ');
                for (int j = 0; j < columns_; ++j) {
                        if (isPartOfMultiColumn(i, j))
                                continue;
                        if (onlydata && j > 0)
                                os << delim;
-                       ret += asciiPrintCell(buf, os, cell, i, j, clen, onlydata);
+                       ret += asciiPrintCell(buf, os, runparams,
+                                             cell, i, j, clen, onlydata);
                        ++cell;
                }
                os << endl;
                if (!onlydata) {
-                       for (int j = 0; j < depth; ++j)
-                               os << "  ";
+                       os << string(depth * 2, ' ');
                        if (asciiBottomHLine(os, i, clen))
-                               for (int j = 0; j < depth; ++j)
-                                       os << "  ";
+                               os << string(depth * 2, ' ');
                }
        }
        return ret;
 }
 
 
-InsetText & LyXTabular::getCellInset(int cell) const
+shared_ptr<InsetText> LyXTabular::getCellInset(int cell) const
 {
-       cur_cell = cell;
        return cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
 }
 
 
-InsetText & LyXTabular::getCellInset(int row, int column) const
+shared_ptr<InsetText> LyXTabular::getCellInset(int row, int column) const
 {
-       cur_cell = getCellNumber(row, column);
        return cell_info[row][column].inset;
 }
 
 
-int LyXTabular::getCellFromInset(InsetOld const * inset, int maybe_cell) const
+int LyXTabular::getCellFromInset(InsetBase const * inset) const
 {
        // is this inset part of the tabular?
-       if (!inset || inset->owner() != owner_) {
-               lyxerr << "this is not a cell of the tabular!" << endl;
-               Assert(0);
+       if (!inset) {
+               lyxerr << "Error: this is not a cell of the tabular!" << endl;
+               BOOST_ASSERT(false);
        }
 
-       const int save_cur_cell = cur_cell;
-       int cell = cur_cell;
-       if (&getCellInset(cell) != inset) {
-               cell = maybe_cell;
-               if (cell == -1 || &getCellInset(cell) != inset) {
-                       cell = -1;
+       for (int cell = 0, n = getNumberOfCells(); cell < n; ++cell)
+               if (getCellInset(cell).get() == inset) {
+                       lyxerr[Debug::INSETTEXT] << "LyXTabular::getCellFromInset: "
+                               << "cell=" << cell << endl;
+                       return cell;
                }
-       }
 
-       if (cell == -1) {
-               for (cell = getNumberOfCells(); cell >= 0; --cell) {
-                       if (&getCellInset(cell) == inset)
-                               break;
-               }
-               lyxerr[Debug::INSETTEXT]
-                        << "LyXTabular::getCellFromInset: "
-                                   << "cell=" << cell
-                                   << ", cur_cell=" << save_cur_cell
-                                   << ", maybe_cell=" << maybe_cell
-                                   << endl;
-               // We should have found a cell at this point
-               if (cell == -1) {
-                       lyxerr << "LyXTabular::getCellFromInset: "
-                              << "Cell not found!" << endl;
-               }
-       }
-
-       return cell;
+       // We should have found a cell at this point
+       lyxerr << "LyXTabular::getCellFromInset: Cell of inset "
+               << inset << " not found!" << endl;
+       BOOST_ASSERT(false);
+       // shut up compiler
+       return 0;
 }
 
 
@@ -2655,22 +2614,23 @@ void LyXTabular::validate(LaTeXFeatures & features) const
                if (getVAlignment(cell) != LYX_VALIGN_TOP ||
                     (!getPWidth(cell).zero() && !isMultiColumn(cell)))
                        features.require("array");
-               getCellInset(cell).validate(features);
+               getCellInset(cell)->validate(features);
        }
 }
 
 
-void LyXTabular::getLabelList(std::vector<string> & list) const
+void LyXTabular::getLabelList(Buffer const & buffer,
+                             std::vector<string> & list) const
 {
        for (int i = 0; i < rows_; ++i)
                for (int j = 0; j < columns_; ++j)
-                       getCellInset(i, j).getLabelList(list);
+                       getCellInset(i, j)->getLabelList(buffer, list);
 }
 
 
 LyXTabular::BoxType LyXTabular::useParbox(int cell) const
 {
-       ParagraphList const & parlist = getCellInset(cell).paragraphs;
+       ParagraphList const & parlist = getCellInset(cell)->paragraphs();
        ParagraphList::const_iterator cit = parlist.begin();
        ParagraphList::const_iterator end = parlist.end();