]> git.lyx.org Git - lyx.git/blobdiff - src/tabular.C
Fix crash when running lyx -dbg insets -e ...
[lyx.git] / src / tabular.C
index d7aadbd6bff992618a92ceedcaf3d83c6b1623bb..2202e3367bf818c2ec9959cfb7b9440345ff9c4e 100644 (file)
@@ -3,7 +3,7 @@
  * 
  *           LyX, The Document Processor
  *      
- *           Copyright 2000 The LyX Team.
+ *           Copyright 2000-2001 The LyX Team.
  *
  *           @author: Jürgen Vigna
  *
@@ -41,6 +41,10 @@ using std::max;
 using std::endl;
 using std::vector;
 
+#ifndef CXX_GLOBAL_CSTD
+using std::strlen;
+#endif
+
 namespace {
 
        int const WIDTH_OF_LINE = 5;
@@ -89,6 +93,7 @@ LyXTabular::columnstruct::columnstruct()
 LyXTabular::LyXTabular(InsetTabular * inset, int rows_arg, int columns_arg)
 {
        owner_ = inset;
+       cur_cell = -1;
        Init(rows_arg, columns_arg);
 }
 
@@ -96,28 +101,38 @@ LyXTabular::LyXTabular(InsetTabular * inset, int rows_arg, int columns_arg)
 LyXTabular::LyXTabular(InsetTabular * inset, LyXTabular const & lt)
 {
        owner_ = inset;
-       Init(lt.rows_, lt.columns_);
+       cur_cell = -1;
+       Init(lt.rows_, lt.columns_, &lt);
+#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)
 #endif
        operator=(lt);
+#endif
 }
 
 
 LyXTabular::LyXTabular(Buffer const * buf, InsetTabular * inset, LyXLex & lex)
 {
        owner_ = inset;
+       cur_cell = -1;
        Read(buf, lex);
 }
 
 
 LyXTabular & LyXTabular::operator=(LyXTabular const & lt)
 {
+#if 0
+#warning This while method should look like this: (Lgb)
+
+               LyXTabular tmp(lt);
+               tmp.swap(*this);
+#else
        // If this and lt is not of the same size we have a serious bug
        // So then it is ok to throw an exception, or for now
        // call abort()
        lyx::Assert(rows_ == lt.rows_ && columns_ == lt.columns_);
-
+       cur_cell = -1;
        cell_info = lt.cell_info;
        row_info = lt.row_info;
        column_info = lt.column_info;
@@ -132,14 +147,15 @@ LyXTabular & LyXTabular::operator=(LyXTabular const & lt)
        rotate = lt.rotate;
 
        Reinit();
-       
+#endif
        return *this;
 }
 
 
-LyXTabular * LyXTabular::Clone(InsetTabular * inset)
+LyXTabular * LyXTabular::clone(InsetTabular * inset)
 {
        LyXTabular * result = new LyXTabular(inset, *this);
+#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!
        for (int i = 0; i < rows_; ++i) {
@@ -148,12 +164,13 @@ LyXTabular * LyXTabular::Clone(InsetTabular * inset)
                        result->cell_info[i][j].inset.setOwner(inset);
                }
        }
+#endif
        return result;
 }
 
 
-/* activates all lines and sets all widths to 0 */
-void LyXTabular::Init(int rows_arg, int columns_arg)
+/* activates all lines and sets all widths to 0 */ 
+void LyXTabular::Init(int rows_arg, int columns_arg, LyXTabular const * lt)
 {
        rows_ = rows_arg;
        columns_ = columns_arg;
@@ -161,11 +178,16 @@ void LyXTabular::Init(int rows_arg, int columns_arg)
        column_info = column_vector(columns_, columnstruct());
        cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct()));
 
+       if (lt) {
+               operator=(*lt);
+               return;
+       }
+
        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);
+                       cell_info[i][j].inset.setDrawFrame(0, InsetText::LOCKED);
                        cell_info[i][j].cellno = cellno++;
                }
                cell_info[i].back().right_line = true;
@@ -360,7 +382,7 @@ void LyXTabular::set_row_column_number_info(bool oldformat)
                                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)).empty());
                }
        }
@@ -428,10 +450,8 @@ bool LyXTabular::RightLine(int cell, bool onlycolumn) const
 
 bool LyXTabular::TopAlreadyDrawed(int cell) const
 {
-       if (GetAdditionalHeight(cell))
-               return false;
        int row = row_of_cell(cell);
-       if (row > 0) {
+       if ((row > 0) && !GetAdditionalHeight(row)) {
                int column = column_of_cell(cell);
                --row;
                while (column
@@ -469,11 +489,11 @@ bool LyXTabular::IsLastRow(int cell) const
 }
 
 
-int LyXTabular::GetAdditionalHeight(int cell) const
+int LyXTabular::GetAdditionalHeight(int row) const
 {
-       int const row = row_of_cell(cell);
-       if (!row) return 0;
-       
+       if (!row || row >= rows_)
+               return 0;
+
        bool top = true;
        bool bottom = true;
 
@@ -648,7 +668,7 @@ bool LyXTabular::SetColumnPWidth(int cell, string const & width)
        for (int i = 0; i < rows_; ++i) {
                int c = GetCellNumber(i, j);
                flag = !GetPWidth(c).empty(); // because of multicolumns!
-               GetCellInset(c)->SetAutoBreakRows(flag);
+               GetCellInset(c)->setAutoBreakRows(flag);
        }
        return true;
 }
@@ -660,7 +680,7 @@ bool LyXTabular::SetMColumnPWidth(int cell, string const & width)
 
        cellinfo_of_cell(cell)->p_width = width;
        if (IsMultiColumn(cell)) {
-               GetCellInset(cell)->SetAutoBreakRows(flag);
+               GetCellInset(cell)->setAutoBreakRows(flag);
                return true;
        }
        return false;
@@ -668,7 +688,7 @@ bool LyXTabular::SetMColumnPWidth(int cell, string const & width)
 
 
 bool LyXTabular::SetAlignSpecial(int cell, string const & special,
-                                LyXTabular::Feature what)
+                                 LyXTabular::Feature what)
 {
        if (what == SET_SPECIAL_MULTI)
                cellinfo_of_cell(cell)->align_special = special;
@@ -1058,7 +1078,7 @@ 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);
+                       cell_info[i][j].inset.write(buf, os);
                        os << "\n\\end_inset \n"
                           << "</cell>\n";
                }
@@ -1139,8 +1159,8 @@ bool getTokenValue(string const & str, const char * token, string & ret)
        size_t token_length = strlen(token);
        string::size_type pos = str.find(token);
 
-       if (pos == string::npos || pos+token_length+1 >= str.length()
-               || str[pos+token_length] != '=')
+       if (pos == string::npos || pos + token_length + 1 >= str.length()
+               || str[pos + token_length] != '=')
                return false;
        ret.erase();
        pos += token_length + 1;
@@ -1309,7 +1329,7 @@ void LyXTabular::ReadNew(Buffer const * buf, istream & is,
                        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>")) {
@@ -1466,17 +1486,14 @@ void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
        }
        set_row_column_number_info(true);
 
-       LyXParagraph * par = new LyXParagraph;
-       LyXParagraph * return_par = 0;
-#ifndef NEW_INSETS
-       LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
-       LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
-#endif
+       Paragraph * par = new Paragraph;
+       Paragraph * return_par = 0;
+
        string tmptok;
        int pos = 0;
-       char depth = 0;
+       Paragraph::depth_type depth = 0;
        LyXFont font(LyXFont::ALL_INHERIT);
-       font.setLanguage(owner_->BufferOwner()->GetLanguage());
+       font.setLanguage(owner_->bufferOwner()->getLanguage());
 
        while (lex.IsOK()) {
                lex.nextToken();
@@ -1487,19 +1504,16 @@ void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
                        || token == "\\end_float"
                        || token == "\\end_deeper") {
                        lex.pushToken(token);
+                       // Here we need to insert the inset_ert_contents into the last
+                       // cell of the tabular.
+                       owner_->bufferOwner()->insertErtContents(par, pos, font);
+                       
                        break;
                }
-               if (owner_->BufferOwner()->parseSingleLyXformat2Token(lex, par,
+               if (owner_->bufferOwner()->parseSingleLyXformat2Token(lex, par,
                                                                                                                          return_par,
                                                                                                                          token, pos,
-                                                                                                                         depth, font
-#ifndef NEW_INSETS
-                                                                                                                         ,
-                                                                                                                         footnoteflag,
-                                                                                                                         footnotekind
-#endif
-                                                                                                                         ))
-               {
+                                                                                                                         depth, font)) {
                        // the_end read
                        lex.pushToken(token);
                        break;
@@ -1517,15 +1531,10 @@ void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
        InsetText * inset = GetCellInset(cell);
        int row;
 
-#ifndef NEW_INSETS
-       for (int i = 0; i < par->Last(); ++i)
-#else
-       for (int i = 0; i < par->size(); ++i)
-#endif
-       {
-               if (par->IsNewline(i)) {
+       for (int i = 0; i < par->size(); ++i) {
+               if (par->isNewline(i)) {
                        ++cell;
-                       if (cell > GetNumberOfCells()) {
+                       if (cell > numberofcells) {
                                lyxerr << "Some error in reading old table format occured!" <<
                                        endl << "Terminating when reading cell[" << cell << "]!" <<
                                        endl;
@@ -1546,16 +1555,12 @@ void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
                        if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].usebox)
                        {
                                // insert a space instead
-                               par->Erase(i);
-                               par->InsertChar(i, ' ');
+                               par->erase(i);
+                               par->insertChar(i, ' ');
                        }
                }
-               par->CopyIntoMinibuffer(*owner_->BufferOwner(), i);
-#ifndef NEW_INSETS
-               inset->par->InsertFromMinibuffer(inset->par->Last());
-#else
-               inset->par->InsertFromMinibuffer(inset->par->size());
-#endif
+               par->copyIntoMinibuffer(*owner_->bufferOwner(), i);
+               inset->paragraph()->insertFromMinibuffer(inset->paragraph()->size());
        }
        delete par;
        Reinit();
@@ -1681,7 +1686,7 @@ bool LyXTabular::NeedRotating() const
 
 bool LyXTabular::IsLastCell(int cell) const
 {
-       if ((cell + 1) < GetNumberOfCells())
+       if ((cell + 1) < numberofcells)
                return false;
        return true;
 }
@@ -1725,6 +1730,7 @@ int LyXTabular::GetLastCellBelow(int cell) const
 
 int LyXTabular::GetCellNumber(int row, int column) const
 {
+#if 0
        if (column >= columns_)
                column = columns_ - 1;
        else if (column < 0)
@@ -1733,7 +1739,9 @@ int LyXTabular::GetCellNumber(int row, int column) const
                row = rows_ - 1;
        else if (row < 0)
                row = 0;
-       
+#else
+       lyx::Assert(column >= 0 || column < columns_ || row >= 0 || row < rows_);
+#endif
        return cell_info[row][column].cellno;
 }
 
@@ -1881,7 +1889,7 @@ int LyXTabular::GetHeightOfTabular() const
 
        for (int row = 0; row < rows_; ++row)
                height += GetAscentOfRow(row) + GetDescentOfRow(row) +
-                       GetAdditionalHeight(GetCellNumber(row, 0));
+                       GetAdditionalHeight(row);
        return height;
 }
 
@@ -1973,8 +1981,13 @@ int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
                if (!cellinfo_of_cell(cell)->align_special.empty()) {
                        os << cellinfo_of_cell(cell)->align_special << "}{";
                } else {
-                       if (LeftLine(cell))
+                       if (LeftLine(cell) &&
+                               (IsFirstCellInRow(cell) || 
+                                (!IsMultiColumn(cell-1) && !LeftLine(cell, true) &&
+                                 !RightLine(cell-1, true))))
+                       {
                                os << '|';
+                       }
                        if (!GetPWidth(cell).empty()) {
                                switch (GetVAlignment(cell)) {
                                case LYX_VALIGN_TOP:
@@ -2157,15 +2170,12 @@ int LyXTabular::Latex(Buffer const * buf,
                        ret += TeXCellPreamble(os, cell);
                        InsetText * inset = GetCellInset(cell);
 
-                       bool rtl = inset->par->isRightToLeftPar(buf->params) &&
-#ifndef NEW_INSETS
-                               inset->par->Last() > 0 && GetPWidth(cell).empty();
-#else
-                       inset->par->size() > 0 && GetPWidth(cell).empty();
-#endif
+                       bool rtl = inset->paragraph()->isRightToLeftPar(buf->params) &&
+                                       inset->paragraph()->size() > 0 && GetPWidth(cell).empty();
+
                        if (rtl)
                                os << "\\R{";
-                       ret += inset->Latex(buf, os, fragile, fp);
+                       ret += inset->latex(buf, os, fragile, fp);
                        if (rtl)
                                os << "}";
 
@@ -2294,7 +2304,7 @@ int LyXTabular::DocBook(Buffer const * buf, ostream & os) const
                        }
                
                        os << ">";
-                       ret += GetCellInset(cell)->DocBook(buf, os);
+                       ret += GetCellInset(cell)->docBook(buf, os);
                        os << "</entry>";
                        ++cell;
                }
@@ -2422,7 +2432,7 @@ int LyXTabular::AsciiPrintCell(Buffer const * buf, ostream & os,
                                                           vector<unsigned int> const & clen) const
 {
        ostringstream sstr;
-       int ret = GetCellInset(cell)->Ascii(buf, sstr, 0);
+       int ret = GetCellInset(cell)->ascii(buf, sstr, 0);
 
        if (LeftLine(cell))
                os << "| ";
@@ -2481,7 +2491,7 @@ int LyXTabular::Ascii(Buffer const * buf, ostream & os) const
                        if (IsMultiColumn(cell, true))
                                continue;
                        ostringstream sstr;
-                       GetCellInset(cell)->Ascii(buf, sstr, 0);
+                       GetCellInset(cell)->ascii(buf, sstr, 0);
                        if (clen[j] < sstr.str().length())
                                clen[j] = sstr.str().length();
                }
@@ -2493,7 +2503,7 @@ int LyXTabular::Ascii(Buffer const * buf, ostream & os) const
                        if (!IsMultiColumn(cell, true) || IsPartOfMultiColumn(i, j))
                                continue;
                        ostringstream sstr;
-                       GetCellInset(cell)->Ascii(buf, sstr, 0);
+                       GetCellInset(cell)->ascii(buf, sstr, 0);
                        int len = int(sstr.str().length());
                        int const n = cells_in_multicolumn(cell);
                        for (int k = j; (len > 0) && (k < (j + n - 1)); ++k)
@@ -2520,13 +2530,15 @@ int LyXTabular::Ascii(Buffer const * buf, ostream & os) const
 
 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
 {
-       return GetCellInset(GetCellNumber(row, column));
+       cur_cell = GetCellNumber(row, column);
+       return & cell_info[row][column].inset;
 }
 
 
@@ -2539,7 +2551,7 @@ void LyXTabular::Validate(LaTeXFeatures & features) const
        for (int cell = 0; !features.array && (cell < numberofcells); ++cell) {
                if (GetVAlignment(cell) != LYX_VALIGN_TOP)
                        features.array = true;
-               GetCellInset(cell)->Validate(features);
+               GetCellInset(cell)->validate(features);
        }
 }
 
@@ -2558,30 +2570,22 @@ std::vector<string> const LyXTabular::getLabelList() const
 }
 
                        
-#ifndef NEW_INSETS
 LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
 {
-       LyXParagraph * par = GetCellInset(cell)->par;
-
-       for (; par; par = par->next_) {
-               for (int i = 0; i < par->Last(); ++i) {
-                       if (par->GetChar(i)     == LyXParagraph::META_NEWLINE)
-                               return BOX_PARBOX;
-               }
-       }
-       return BOX_NONE;
-}
-#else
-LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
-{
-       LyXParagraph * par = GetCellInset(cell)->par;
+       Paragraph * par = GetCellInset(cell)->paragraph();
 
        for (; par; par = par->next()) {
                for (int i = 0; i < par->size(); ++i) {
-                       if (par->GetChar(i)     == LyXParagraph::META_NEWLINE)
+                       if (par->getChar(i)     == Paragraph::META_NEWLINE)
                                return BOX_PARBOX;
                }
        }
        return BOX_NONE;
 }
-#endif
+
+/* Emacs:
+ * Local variables:
+ * tab-width: 4
+ * End:
+ * vi:set tabstop=4:
+ */