]> git.lyx.org Git - lyx.git/blobdiff - src/tabular.C
More fixes to the autocollapsing of paragraphs.
[lyx.git] / src / tabular.C
index 1445ad1818e07dd13337623881ad1980f65c4e04..380e8b9d0841a7c16332d9112a927bfebe92ad3d 100644 (file)
@@ -110,11 +110,23 @@ LyXTabular::LyXTabular(InsetTabular * inset, int rows_arg, int columns_arg)
 }
 
 
-LyXTabular::LyXTabular(InsetTabular * inset, LyXTabular const & lt)
+LyXTabular::LyXTabular(InsetTabular * inset, LyXTabular const & lt,
+                       bool same_id)
 {
        owner_ = inset;
        cur_cell = -1;
        Init(lt.rows_, lt.columns_, &lt);
+       // we really should change again to have InsetText as a pointer
+       // and allocate it then we would not have to do this stuff all
+       // double!
+       if (same_id) {
+               for (int i = 0; i < rows_; ++i) {
+                       for (int j = 0; j < columns_; ++j) {
+                               cell_info[i][j].inset.id(lt.cell_info[i][j].inset.id());
+                               cell_info[i][j].inset.setParagraphData(lt.cell_info[i][j].inset.paragraph(),true);
+                       }
+               }
+       }
 #if 0
 #ifdef WITH_WARNINGS
 #warning Jürgen, can you make it the other way round. So that copy assignment depends on the copy constructor and not the other way. (Lgb)
@@ -164,9 +176,9 @@ LyXTabular & LyXTabular::operator=(LyXTabular const & lt)
 }
 
 
-LyXTabular * LyXTabular::clone(InsetTabular * inset)
+LyXTabular * LyXTabular::clone(InsetTabular * inset, bool same_id)
 {
-       LyXTabular * result = new LyXTabular(inset, *this);
+       LyXTabular * result = new LyXTabular(inset, *this, same_id);
 #if 0
        // don't know if this is good but I need to Clone also
        // the text-insets here, this is for the Undo-facility!
@@ -295,12 +307,12 @@ void LyXTabular::AppendColumn(int cell)
                        c_info[i][j] = cell_info[i][j - 1];
                }
                // care about multicolumns
-               if (cell_info[i][column + 1].multicolumn==CELL_BEGIN_OF_MULTICOLUMN) {
-                       cell_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
+               if (c_info[i][column + 1].multicolumn==CELL_BEGIN_OF_MULTICOLUMN) {
+                       c_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
                }
                if ((column + 1) == columns_ ||
-                       cell_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN) {
-                       cell_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
+                       c_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN) {
+                       c_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
                }
        }
        cell_info = c_info;
@@ -329,12 +341,20 @@ void LyXTabular::DeleteColumn(int column)
 }
 
 
-void LyXTabular::Reinit()
-{   
-       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_);
+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_);
+                       }
                }
        }
   
@@ -392,7 +412,7 @@ void LyXTabular::set_row_column_number_info(bool oldformat)
                                        cell_info[row][column+cn-1].right_line;
                        }
                        cell_info[row][column].inset.setAutoBreakRows(
-                               !GetPWidth(GetCellNumber(row, column)).empty());
+                               !GetPWidth(GetCellNumber(row, column)).zero());
                }
        }
 }
@@ -710,9 +730,9 @@ bool LyXTabular::SetVAlignment(int cell, VAlignment align, bool onlycolumn)
 }
 
 
-bool LyXTabular::SetColumnPWidth(int cell, string const & width)
+bool LyXTabular::SetColumnPWidth(int cell, LyXLength const & width)
 {
-       bool flag = !width.empty();
+       bool flag = !width.zero();
        int const j = column_of_cell(cell);
 
        column_info[j].p_width = width;
@@ -720,16 +740,16 @@ bool LyXTabular::SetColumnPWidth(int cell, string const & width)
                SetAlignment(cell, LYX_ALIGN_LEFT);
        for (int i = 0; i < rows_; ++i) {
                int c = GetCellNumber(i, j);
-               flag = !GetPWidth(c).empty(); // because of multicolumns!
+               flag = !GetPWidth(c).zero(); // because of multicolumns!
                GetCellInset(c)->setAutoBreakRows(flag);
        }
        return true;
 }
 
 
-bool LyXTabular::SetMColumnPWidth(int cell, string const & width)
+bool LyXTabular::SetMColumnPWidth(int cell, LyXLength const & width)
 {
-       bool const flag = !width.empty();
+       bool const flag = !width.zero();
 
        cellinfo_of_cell(cell)->p_width = width;
        if (IsMultiColumn(cell)) {
@@ -822,7 +842,7 @@ LyXTabular::GetVAlignment(int cell, bool onlycolumn) const
 }
 
 
-string const LyXTabular::GetPWidth(int cell) const
+LyXLength const LyXTabular::GetPWidth(int cell) const
 {
        if (IsMultiColumn(cell))
                return cellinfo_of_cell(cell)->p_width;
@@ -830,17 +850,17 @@ string const LyXTabular::GetPWidth(int cell) const
 }
 
 
-string const LyXTabular::GetColumnPWidth(int cell) const
+LyXLength const LyXTabular::GetColumnPWidth(int cell) const
 {
        return column_info[column_of_cell(cell)].p_width;
 }
 
 
-string const LyXTabular::GetMColumnPWidth(int cell) const
+LyXLength const LyXTabular::GetMColumnPWidth(int cell) const
 {
        if (IsMultiColumn(cell))
                return cellinfo_of_cell(cell)->p_width;
-       return string();
+       return LyXLength();
 }
 
 
@@ -1002,6 +1022,11 @@ string const write_attribute(string const & name, bool const & b)
        return write_attribute(name, int(b));
 }
 
+template <>
+string const write_attribute(string const & name, LyXLength const & value)
+{
+       return write_attribute(name, value.asString());
+}
 #else
 
 string const write_attribute(string const & name, int value)
@@ -1023,6 +1048,13 @@ string const write_attribute(string const & name, bool value)
        string str = " " + name + "=\"" + tostr(static_cast<int>(value)) + "\"";
        return str;
 }
+
+
+string const write_attribute(string const & name, LyXLength const & value)
+{
+       string str = " " + name + "=\"" + value.asString() + "\"";
+       return str;
+}
 #endif
 
 
@@ -1105,9 +1137,7 @@ void LyXTabular::Write(Buffer const * buf, ostream & os) const
                   << write_attribute("valignment", tostr(column_info[j].valignment))
                   << write_attribute("leftline", tostr(column_info[j].left_line))
                   << write_attribute("rightline", tostr(column_info[j].right_line))
-                  << write_attribute("width",
-                                                         VSpace(column_info[j].p_width)
-                                                         .asLyXCommand())
+                  << write_attribute("width", column_info[j].p_width.asString())
                   << write_attribute("special", column_info[j].align_special)
                   << ">\n";
        }
@@ -1223,7 +1253,7 @@ bool getTokenValue(string const & str, const char * token, string & ret)
                ret += ch;
                ch = ' ';
        }
-       while((pos < str.length() - 1) && (str[++pos] != ch))
+       while ((pos < str.length() - 1) && (str[++pos] != ch))
                ret += str[pos];
 
        return true;
@@ -1278,6 +1308,15 @@ bool getTokenValue(string const & str, const char * token, bool & flag)
 }    
 
 
+bool getTokenValue(string const & str, const char * token, LyXLength & len)
+{
+       string tmp;
+       if (!getTokenValue(str, token, tmp))
+               return false;
+       return isValidLength(tmp, &len);
+}    
+
+
 inline
 void l_getline(istream & is, string & str)
 {
@@ -1505,7 +1544,7 @@ void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
                        column_info[i].alignment = static_cast<LyXAlignment>(a);
                        column_info[i].left_line = b;
                        column_info[i].right_line = c;
-                       column_info[i].p_width = s1;
+                       column_info[i].p_width = LyXLength(s1);
                        column_info[i].align_special = s2;
                }
                for (i = 0; i < rows_; ++i) {
@@ -1537,7 +1576,7 @@ void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
                                cell_info[i][j].rotate = static_cast<bool>(f);
                                cell_info[i][j].usebox = static_cast<BoxType>(g);
                                cell_info[i][j].align_special = s1;
-                               cell_info[i][j].p_width = s2;
+                               cell_info[i][j].p_width = LyXLength(s2);
                        }
                }
        }
@@ -1603,7 +1642,7 @@ void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
                        if (cont_row_info[row]) {
                                DeleteRow(row);
                                cont_row_info.erase(cont_row_info.begin() + row); //&cont_row_info[row]);
-                               while(!IsFirstCellInRow(--cell));
+                               while (!IsFirstCellInRow(--cell));
                        } else {
                                inset = GetCellInset(cell);
                                continue;
@@ -1812,8 +1851,8 @@ void LyXTabular::SetUsebox(int cell, BoxType type)
 
 LyXTabular::BoxType LyXTabular::GetUsebox(int cell) const
 {
-       if (column_info[column_of_cell(cell)].p_width.empty() &&
-               !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.empty()))
+       if (column_info[column_of_cell(cell)].p_width.zero() &&
+               !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.zero()))
                return BOX_NONE;
        if (cellinfo_of_cell(cell)->usebox > 1)
                return cellinfo_of_cell(cell)->usebox;
@@ -2027,7 +2066,7 @@ int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
                        {
                                os << '|';
                        }
-                       if (!GetPWidth(cell).empty()) {
+                       if (!GetPWidth(cell).zero()) {
                                switch (GetVAlignment(cell)) {
                                case LYX_VALIGN_TOP:
                                        os << "p";
@@ -2039,7 +2078,7 @@ int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
                                        os << "b";
                                        break;
                                }
-                               os << "{" << GetPWidth(cell) << '}';
+                               os << "{" << GetPWidth(cell).asLatexString() << '}';
                        } else {
                                switch (GetAlignment(cell)) {
                                case LYX_ALIGN_LEFT:
@@ -2074,7 +2113,7 @@ int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
                        os << "b";
                        break;
                }
-               os << "]{" << GetPWidth(cell) << "}{";
+               os << "]{" << GetPWidth(cell).asLatexString() << "}{";
        } else if (GetUsebox(cell) == BOX_MINIPAGE) {
                os << "\\begin{minipage}[";
                switch (GetVAlignment(cell)) {
@@ -2088,7 +2127,7 @@ int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
                        os << "b";
                        break;
                }
-               os << "]{" << GetPWidth(cell) << "}\n";
+               os << "]{" << GetPWidth(cell).asLatexString() << "}\n";
                ++ret;
        }
        return ret;
@@ -2141,7 +2180,7 @@ int LyXTabular::Latex(Buffer const * buf,
                } else { 
                        if (column_info[i].left_line)
                                os << '|';
-                       if (!column_info[i].p_width.empty()) {
+                       if (!column_info[i].p_width.zero()) {
                                switch (column_info[i].valignment) {
                                case LYX_VALIGN_TOP:
                                        os << "p";
@@ -2154,7 +2193,7 @@ int LyXTabular::Latex(Buffer const * buf,
                                        break;
                        }
                                os << "{"
-                                  << column_info[i].p_width
+                                  << column_info[i].p_width.asLatexString()
                                   << '}';
                        } else {
                                switch (column_info[i].alignment) {
@@ -2196,7 +2235,7 @@ int LyXTabular::Latex(Buffer const * buf,
                        InsetText * inset = GetCellInset(cell);
 
                        bool rtl = inset->paragraph()->isRightToLeftPar(buf->params) &&
-                                       inset->paragraph()->size() > 0 && GetPWidth(cell).empty();
+                                       inset->paragraph()->size() > 0 && GetPWidth(cell).zero();
 
                        if (rtl)
                                os << "\\R{";
@@ -2436,7 +2475,7 @@ int LyXTabular::AsciiTopHLine(ostream & os, int row,
                }
                int column = column_of_cell(i);
                int len = clen[column];
-               while(IsPartOfMultiColumn(row, ++column))
+               while (IsPartOfMultiColumn(row, ++column))
                        len += clen[column] + 4;
                print_n_chars(os, ch, len);
                if (TopLine(i)) {
@@ -2483,7 +2522,7 @@ int LyXTabular::AsciiBottomHLine(ostream & os, int row,
                }
                int column = column_of_cell(i);
                int len = clen[column];
-               while(IsPartOfMultiColumn(row, ++column))
+               while (IsPartOfMultiColumn(row, ++column))
                        len += clen[column] + 4;
                print_n_chars(os, ch, len);
                if (BottomLine(i)) {
@@ -2501,8 +2540,8 @@ int LyXTabular::AsciiBottomHLine(ostream & os, int row,
 
 
 int LyXTabular::AsciiPrintCell(Buffer const * buf, ostream & os,
-                                                          int cell, int row, int column,
-                                                          vector<unsigned int> const & clen) const
+                              int cell, int row, int column,
+                              vector<unsigned int> const & clen) const
 {
        ostringstream sstr;
        int ret = GetCellInset(cell)->ascii(buf, sstr, 0);
@@ -2514,7 +2553,7 @@ int LyXTabular::AsciiPrintCell(Buffer const * buf, ostream & os,
 
        unsigned int len1 = sstr.str().length();
        unsigned int len2 = clen[column];
-       while(IsPartOfMultiColumn(row, ++column))
+       while (IsPartOfMultiColumn(row, ++column))
                len2 += clen[column] + 4;
        len2 -= len1;