]> git.lyx.org Git - lyx.git/blobdiff - src/tabular.C
Fix fuer #209
[lyx.git] / src / tabular.C
index 2bde383bd6c9c2d989ee1f98f2043d60238012b0..97b1a00ad92576017d52ff6138bc6fbfd303cf8c 100644 (file)
@@ -301,11 +301,13 @@ void LyXTabular::AppendColumn(int cell)
                        c_info[i][j] = cell_info[i][j - 1];
                }
                // care about multicolumns
-               if (c_info[i][column + 1].multicolumn==CELL_BEGIN_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_ ||
-                       c_info[i][column + 2].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;
                }
        }
@@ -550,7 +552,7 @@ int LyXTabular::GetAdditionalHeight(int row) const
        bool top = true;
        bool bottom = true;
 
-       for (int column = 0; column < columns_ - 1 && bottom; ++column) {
+       for (int column = 0; column < columns_ && bottom; ++column) {
                switch (cell_info[row - 1][column].multicolumn) {
                case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
                        bottom = cell_info[row - 1][column].bottom_line;
@@ -559,7 +561,7 @@ int LyXTabular::GetAdditionalHeight(int row) const
                        bottom = row_info[row - 1].bottom_line;
                }
        }
-       for (int column = 0; column < columns_ - 1 && top; ++column) {
+       for (int column = 0; column < columns_ && top; ++column) {
                switch (cell_info[row][column].multicolumn){
                case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
                        top = cell_info[row][column].top_line;
@@ -1438,7 +1440,7 @@ void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
 #ifndef NO_COMPABILITY
                        // Here we need to insert the inset_ert_contents into the last
                        // cell of the tabular.
-                       owner_->bufferOwner()->insertErtContents(par, pos, font);
+                       owner_->bufferOwner()->insertErtContents(par, pos);
 #endif
                        break;
                }
@@ -2181,7 +2183,7 @@ int LyXTabular::TeXRow(ostream & os, int const i, Buffer const * buf,
 }
 
 
-int LyXTabular::Latex(Buffer const * buf,
+int LyXTabular::latex(Buffer const * buf,
                                          ostream & os, bool fragile, bool fp) const
 {
        int ret = 0;
@@ -2323,7 +2325,7 @@ int LyXTabular::docbookRow(Buffer const * buf, ostream & os, int row) const
 }      
 
 
-int LyXTabular::DocBook(Buffer const * buf, ostream & os) const
+int LyXTabular::docBook(Buffer const * buf, ostream & os) const
 {
        int ret = 0;
 
@@ -2407,20 +2409,11 @@ int LyXTabular::DocBook(Buffer const * buf, ostream & os) const
        return ret;
 }
 
-
-namespace {
-
-       inline
-       void print_n_chars(ostream & os, unsigned char ch, int n)
-       {
-               os << string(n, ch);
-       }
-
-} // namespace anon
-
-
-int LyXTabular::AsciiTopHLine(ostream & os, int row,
-                                                         vector<unsigned int> const & clen) const
+//--
+// ASCII export function and helpers
+//--
+int LyXTabular::asciiTopHLine(ostream & os, int row,
+                              vector<unsigned int> const & clen) const
 {
        int const fcell = GetFirstCellInRow(row);
        int const n = NumberOfCellsInRow(fcell) + fcell;
@@ -2451,7 +2444,7 @@ int LyXTabular::AsciiTopHLine(ostream & os, int row,
                int len = clen[column];
                while (IsPartOfMultiColumn(row, ++column))
                        len += clen[column] + 4;
-               print_n_chars(os, ch, len);
+               os << string(len, ch);
                if (TopLine(i)) {
                        if (RightLine(i))
                                os << "-+";
@@ -2466,8 +2459,8 @@ int LyXTabular::AsciiTopHLine(ostream & os, int row,
 }
 
 
-int LyXTabular::AsciiBottomHLine(ostream & os, int row,
-                                                                vector<unsigned int> const & clen) const
+int LyXTabular::asciiBottomHLine(ostream & os, int row,
+                                 vector<unsigned int> const & clen) const
 {
        int const fcell = GetFirstCellInRow(row);
        int const n = NumberOfCellsInRow(fcell) + fcell;
@@ -2498,7 +2491,7 @@ int LyXTabular::AsciiBottomHLine(ostream & os, int row,
                int len = clen[column];
                while (IsPartOfMultiColumn(row, ++column))
                        len += clen[column] + 4;
-               print_n_chars(os, ch, len);
+               os << string(len, ch);
                if (BottomLine(i)) {
                        if (RightLine(i))
                                os << "-+";
@@ -2513,13 +2506,19 @@ 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 LyXTabular::asciiPrintCell(Buffer const * buf, ostream & os,
+                               int cell, int row, int column,
+                               vector<unsigned int> const & clen,
+                               bool onlydata) const
 {
        ostringstream sstr;
        int ret = GetCellInset(cell)->ascii(buf, sstr, 0);
 
+       if (onlydata) {
+               os << sstr.str();
+               return ret;
+       }
+       
        if (LeftLine(cell))
                os << "| ";
        else
@@ -2560,7 +2559,8 @@ int LyXTabular::AsciiPrintCell(Buffer const * buf, ostream & os,
 }
 
 
-int LyXTabular::Ascii(Buffer const * buf, ostream & os) const
+int LyXTabular::ascii(Buffer const * buf, ostream & os, int const depth,
+                                         bool onlydata, unsigned char delim) const
 {
        int ret = 0;
 
@@ -2569,49 +2569,68 @@ int LyXTabular::Ascii(Buffer const * buf, ostream & os) const
        //+---------------------------------------------------------------------
        vector<unsigned int> clen(columns_);
 
-       // first all non (real) multicolumn cells!
-       for (int j = 0; j < columns_; ++j) {
-               clen[j] = 0;
-               for (int i = 0; i < rows_; ++i) {
-                       int cell = GetCellNumber(i, j);
-                       if (IsMultiColumn(cell, true))
-                               continue;
-                       ostringstream sstr;
-                       GetCellInset(cell)->ascii(buf, sstr, 0);
-                       if (clen[j] < sstr.str().length())
-                               clen[j] = sstr.str().length();
+       if (!onlydata) {
+               // first all non (real) multicolumn cells!
+               for (int j = 0; j < columns_; ++j) {
+                       clen[j] = 0;
+                       for (int i = 0; i < rows_; ++i) {
+                               int cell = GetCellNumber(i, j);
+                               if (IsMultiColumn(cell, true))
+                                       continue;
+                               ostringstream sstr;
+                               GetCellInset(cell)->ascii(buf, sstr, 0);
+                               if (clen[j] < sstr.str().length())
+                                       clen[j] = sstr.str().length();
+                       }
                }
-       }
-       // then all (real) multicolumn cells!
-       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))
-                               continue;
-                       ostringstream sstr;
-                       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)
-                               len -= clen[k];
-                       if (len > int(clen[j + n - 1]))
-                               clen[j + n - 1] = len;
+               // then all (real) multicolumn cells!
+               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))
+                                       continue;
+                               ostringstream sstr;
+                               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)
+                                       len -= clen[k];
+                               if (len > int(clen[j + n - 1]))
+                                       clen[j + n - 1] = len;
+                       }
                }
        }
        int cell = 0;
        for (int i = 0; i < rows_; ++i) {
-               AsciiTopHLine(os, i, clen);
+               if (!onlydata) {
+                       if (asciiTopHLine(os, i, clen)) {
+                               for (int j = 0; j < depth; ++j)
+                                       os << "  ";
+                       }
+               }
                for (int j = 0; j < columns_; ++j) {
                        if (IsPartOfMultiColumn(i,j))
                                continue;
-                       ret += AsciiPrintCell(buf, os, cell, i, j, clen);
+                       if (onlydata && j > 0)
+                               os << delim;
+                       ret += asciiPrintCell(buf, os, cell, i, j, clen, onlydata);
                        ++cell;
                }
                os << endl;
-               AsciiBottomHLine(os, i, clen);
+               if (!onlydata) {
+                       for (int j = 0; j < depth; ++j)
+                               os << "  ";
+                       if (asciiBottomHLine(os, i, clen)) {
+                               for (int j = 0; j < depth; ++j)
+                                       os << "  ";
+                       }
+               }
        }
        return ret;
 }
+//--
+// end ascii export
+//--
 
 
 InsetText * LyXTabular::GetCellInset(int cell) const
@@ -2628,13 +2647,53 @@ InsetText * LyXTabular::GetCellInset(int row, int column) const
 }
 
 
+int LyXTabular::GetCellFromInset(Inset const * inset, int maybe_cell) const
+{
+       // is this inset part of the tabular?
+       if (!inset || inset->owner() != owner_) {
+               lyxerr[Debug::INSETTEXT]
+                       << "this is not a cell of the tabular!" << endl;
+               return -1;
+       }
+       
+       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;
+               }
+       }
+       
+       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;
+}
+
+
 void LyXTabular::Validate(LaTeXFeatures & features) const
 {
        if (IsLongTabular())
                features.require("longtable");
        if (NeedRotating())
                features.require("rotating");
-       for (int cell = 0; !features.isRequired("array") && (cell < numberofcells); ++cell) {
+       for (int cell = 0; cell < numberofcells; ++cell) {
                if (GetVAlignment(cell) != LYX_VALIGN_TOP)
                        features.require("array");
                GetCellInset(cell)->validate(features);