]> git.lyx.org Git - lyx.git/blobdiff - src/tabular.C
More fixes to insettabular/text (and some missing features added).
[lyx.git] / src / tabular.C
index 4dee6913c4a729c6b7a5dae66cb368ddff70e650..707c9c353f39c211cde04a86522145f351294d97 100644 (file)
@@ -92,7 +92,9 @@ LyXTabular::LyXTabular(InsetTabular * inset, LyXTabular const & lt)
 {
     owner_ = inset;
     Init(lt.rows_, lt.columns_);
+#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);
 }
 
@@ -135,8 +137,8 @@ LyXTabular * LyXTabular::Clone(InsetTabular * inset)
     LyXTabular * result = new LyXTabular(inset, *this);
     // 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) {
-       for(int j = 0; j < columns_; ++j) {
+    for (int i = 0; i < rows_; ++i) {
+       for (int j = 0; j < columns_; ++j) {
            result->cell_info[i][j].inset = cell_info[i][j].inset;
            result->cell_info[i][j].inset.setOwner(inset);
        }
@@ -201,13 +203,13 @@ void LyXTabular::AppendRow(int cell)
     cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
                                                          cellstruct()));
 
-    for(int i = 0; i <= row; ++i) {
-       for(int j = 0; j < columns_; ++j) {
+    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) {
+    for (int i = row + 1; i < rows_; ++i) {
+       for (int j = 0; j < columns_; ++j) {
            c_info[i][j] = cell_info[i-1][j];
        }
     }
@@ -307,10 +309,7 @@ void LyXTabular::Reinit()
 
 void LyXTabular::set_row_column_number_info(bool oldformat)
 {
-    //int c = 0;
-    //int column = 0;
     numberofcells = -1;
-    //int row = 0;
     for (int row = 0; row < rows_; ++row) {
        for (int column = 0; column<columns_; ++column) {
            if (cell_info[row][column].multicolumn
@@ -324,27 +323,6 @@ void LyXTabular::set_row_column_number_info(bool oldformat)
     rowofcell.resize(numberofcells);
     columnofcell.resize(numberofcells);
 
-#if 0
-    int row = 0;
-    int column = 0;
-    int c = 0;
-    
-    while (c < numberofcells && row < rows_ && column < columns_) {
-       rowofcell[c] = row;
-       columnofcell[c] = column;
-       ++c;
-       do {
-           ++column;
-       } while (column < columns_ &&
-                cell_info[row][column].multicolumn
-                == LyXTabular::CELL_PART_OF_MULTICOLUMN);
-       if (column == columns_) {
-           column = 0;
-           ++row;
-       }
-    }
-#else
-    // Isn't this the same as the while above? (Lgb)
     for (int row = 0, column = 0, c = 0;
         c < numberofcells && row < rows_ && column < columns_;) {
        rowofcell[c] = row;
@@ -360,7 +338,6 @@ void LyXTabular::set_row_column_number_info(bool oldformat)
            ++row;
        }
     }
-#endif
 
     for (int row = 0; row < rows_; ++row) {
        for (int column = 0; column < columns_; ++column) {
@@ -415,7 +392,7 @@ bool LyXTabular::BottomLine(int cell, bool onlycolumn) const
     // no bottom line underneath non-existent cells if you please
     // Isn't that a programming error? Is so this should
     // be an Assert instead. (Lgb)
-    if(cell >= numberofcells)
+    if (cell >= numberofcells)
        return false;
 
     if (!onlycolumn && IsMultiColumn(cell))
@@ -586,7 +563,7 @@ void LyXTabular::recalculateMulticolCells(int cell, int new_width)
     int i = column1;
     for (; i <= column2; ++i)
         cell_info[row][i].width_of_cell = 0;
-    for(i = cell + 1; (i < numberofcells) && (!IsMultiColumn(i)); ++i)
+    for (i = cell + 1; (i < numberofcells) && (!IsMultiColumn(i)); ++i)
         ;
     if (i < numberofcells)
         recalculateMulticolCells(i, GetWidthOfCell(i) - (2 * WIDTH_OF_LINE));
@@ -616,13 +593,13 @@ bool LyXTabular::SetWidthOfCell(int cell, int new_width)
     }
     if (tmp) {
         int i = 0;
-        for(; i<columns_; ++i)
+        for (; i<columns_; ++i)
             calculate_width_of_column_NMC(i);
-        for(i = 0; (i < numberofcells) && !IsMultiColumn(i); ++i)
+        for (i = 0; (i < numberofcells) && !IsMultiColumn(i); ++i)
             ;
         if (i < numberofcells)
             recalculateMulticolCells(i, GetWidthOfCell(i)-(2 * WIDTH_OF_LINE));
-        for(i = 0; i < columns_; ++i)
+        for (i = 0; i < columns_; ++i)
             calculate_width_of_column(i);
         calculate_width_of_tabular();
         return true;
@@ -659,7 +636,7 @@ bool LyXTabular::SetColumnPWidth(int cell, string const & width)
     column_info[j].p_width = width;
     if (flag) // do this only if there is a width
        SetAlignment(cell, LYX_ALIGN_LEFT);
-    for(int i = 0; i < rows_; ++i) {
+    for (int i = 0; i < rows_; ++i) {
        int c = GetCellNumber(i, j);
        flag = !GetPWidth(c).empty(); // because of multicolumns!
        GetCellInset(c)->SetAutoBreakRows(flag);
@@ -960,7 +937,7 @@ string const write_attribute(string const & name, string const & value)
 
 string const write_attribute(string const & name, bool value)
 {
-    string str = " " + name + "=\"" + tostr((int)value) + "\"";
+    string str = " " + name + "=\"" + tostr(static_cast<int>(value)) + "\"";
     return str;
 }
 #endif
@@ -1175,7 +1152,7 @@ void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
     getTokenValue(line, "endfoot", endfoot);
     getTokenValue(line, "endlastfoot", endlastfoot);
 
-    for(int i = 0; i < rows_; ++i) {
+    for (int i = 0; i < rows_; ++i) {
        l_getline(is, line);
        if (!prefixIs(line, "<Row ")) {
            lyxerr << "Wrong tabular format (expected <Row ...> got" <<
@@ -1263,7 +1240,6 @@ void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
     int e = 0;
     int f = 0;
     int g = 0;
-    int h = 0;
        
     istream & is = lex.getStream();
     string s(fl);
@@ -1315,7 +1291,7 @@ void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
        endfoot = c;
        endlastfoot = d;
        for (i = 0; i < rows_; ++i) {
-           a = b = c = d = e = f = g = h = 0;
+           a = b = c = d = e = f = g = 0;
            is >> a >> b >> c >> d;
            row_info[i].top_line = a;
            row_info[i].bottom_line = b;
@@ -1392,6 +1368,7 @@ void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
     int pos = 0;
     char depth = 0;
     LyXFont font(LyXFont::ALL_SANE);
+    font.setLanguage(owner_->BufferOwner()->GetLanguage());
 
     while (lex.IsOK()) {
         lex.nextToken();
@@ -1432,7 +1409,7 @@ void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
     InsetText * inset = GetCellInset(cell);
     int row;
 
-    for(int i = 0; i < par->Last(); ++i) {
+    for (int i = 0; i < par->Last(); ++i) {
        if (par->IsNewline(i)) {
            ++cell;
            if (cell > GetNumberOfCells()) {
@@ -1460,7 +1437,7 @@ void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
                par->InsertChar(i, ' ');
            }
        }
-       par->CopyIntoMinibuffer(owner_->BufferOwner()->params, i);
+       par->CopyIntoMinibuffer(*owner_->BufferOwner(), i);
        inset->par->InsertFromMinibuffer(inset->par->Last());
     }
     delete par;
@@ -1946,7 +1923,7 @@ int LyXTabular::GetHeightOfTabular() const
 {
     int height = 0;
 
-    for(int row = 0; row < rows_; ++row)
+    for (int row = 0; row < rows_; ++row)
         height += GetAscentOfRow(row) + GetDescentOfRow(row) +
            GetAdditionalHeight(GetCellNumber(row, 0));
     return height;
@@ -2031,13 +2008,13 @@ int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
     }
     if (IsMultiColumn(cell)) {
        os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
-       if (!cellinfo_of_cell(cell+1)->align_special.empty()) {
-           os << cellinfo_of_cell(cell+1)->align_special << "}{";
+       if (!cellinfo_of_cell(cell)->align_special.empty()) {
+           os << cellinfo_of_cell(cell)->align_special << "}{";
        } else {
            if (LeftLine(cell))
                os << '|';
            if (!GetPWidth(cell).empty()) {
-               switch(GetVAlignment(cell)) {
+               switch (GetVAlignment(cell)) {
                case LYX_VALIGN_TOP:
                    os << "p";
                    break;
@@ -2072,7 +2049,7 @@ int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
     }
     if (GetUsebox(cell) == BOX_PARBOX) {
        os << "\\parbox[";
-       switch(GetVAlignment(cell)) {
+       switch (GetVAlignment(cell)) {
        case LYX_VALIGN_TOP:
            os << "t";
            break;
@@ -2086,7 +2063,7 @@ int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
        os << "]{" << GetPWidth(cell) << "}{";
     } else if (GetUsebox(cell) == BOX_MINIPAGE) {
        os << "\\begin{minipage}[";
-       switch(GetVAlignment(cell)) {
+       switch (GetVAlignment(cell)) {
        case LYX_VALIGN_TOP:
            os << "t";
            break;
@@ -2150,7 +2127,7 @@ int LyXTabular::Latex(Buffer const * buf,
        if (!column_info[i].align_special.empty()) {
            os << column_info[i].align_special;
        } else if (!column_info[i].p_width.empty()) {
-           switch(column_info[i].valignment) {
+           switch (column_info[i].valignment) {
            case LYX_VALIGN_TOP:
                os << "p";
                break;
@@ -2188,7 +2165,7 @@ int LyXTabular::Latex(Buffer const * buf,
     //+---------------------------------------------------------------------
 
     //int bret;
-    for(int i = 0; i < rows_; ++i) {
+    for (int i = 0; i < rows_; ++i) {
        ret += TeXTopHLine(os, i);
        int bret = ret;
        if (IsLongTabular()) {
@@ -2214,7 +2191,7 @@ int LyXTabular::Latex(Buffer const * buf,
                ret += TeXBottomHLine(os, i-1);
            ret += TeXTopHLine(os, i);
        }
-       for(int j = 0; j < columns_; ++j) {
+       for (int j = 0; j < columns_; ++j) {
            if (IsPartOfMultiColumn(i,j))
                continue;
            ret += TeXCellPreamble(os, cell);
@@ -2306,14 +2283,14 @@ int LyXTabular::DocBook(Buffer const * buf, ostream & os) const
 
     int cell = 0;
     os << "<tbody>\n";
-    for(int i = 0; i < rows_; ++i) {
+    for (int i = 0; i < rows_; ++i) {
         os << "<row>\n";
-       for(int j = 0; j < columns_; ++j) {
+       for (int j = 0; j < columns_; ++j) {
            if (IsPartOfMultiColumn(i, j))
                continue;
            
            os << "<entry align=\"";
-           switch(GetAlignment(cell)) {
+           switch (GetAlignment(cell)) {
            case LYX_ALIGN_LEFT:
                os << "left";
                break;
@@ -2326,7 +2303,7 @@ int LyXTabular::DocBook(Buffer const * buf, ostream & os) const
            }
            
            os << "\" valign=\"";
-           switch(GetVAlignment(cell)) {
+           switch (GetVAlignment(cell)) {
            case LYX_VALIGN_TOP:
                os << "top";
                break;
@@ -2366,12 +2343,7 @@ static
 inline
 void print_n_chars(ostream & os, unsigned char ch, int n)
 {
-#if 0
-       for(int i = 0; i < n; ++i)
-               os << ch;
-#else
        os << string(n, ch);
-#endif
 }
 
 
@@ -2380,26 +2352,16 @@ int LyXTabular::AsciiTopHLine(ostream & os, int row,
 {
     int const fcell = GetFirstCellInRow(row);
     int const n = NumberOfCellsInRow(fcell) + fcell;
-    //int len;
-    //int column = 0;
-    //unsigned char ch;
-
-#if 0
     int tmp = 0;
 
     for (int i = fcell; i < n; ++i) {
-       if (TopLine(i))
+       if (TopLine(i)) {
            ++tmp;
+           break;
+       }
     }
     if (!tmp)
        return 0;
-#else
-    // Isn't this equivalent? (Lgb)
-    for (int i = fcell; i < n; ++i) {
-           if (TopLine(i))
-                   return 0;
-    }
-#endif
 
     unsigned char ch;
     for (int i = fcell; i < n; ++i) {
@@ -2437,25 +2399,17 @@ int LyXTabular::AsciiBottomHLine(ostream & os, int row,
 {
     int const fcell = GetFirstCellInRow(row);
     int const n = NumberOfCellsInRow(fcell) + fcell;
-    //int len;
-    //int column = 0;
-    //unsigned char ch;
-#if 0
     int tmp = 0;
 
     for (int i = fcell; i < n; ++i) {
-       if (BottomLine(i))
+       if (BottomLine(i)) {
            ++tmp;
+           break;
+       }
     }
     if (!tmp)
        return 0;
-#else
-    // Isn't this equivalent? (Lgb)
-    for (int i = fcell; i < n; ++i) {
-       if (BottomLine(i))
-           return 0;
-    }
-#endif
+
     unsigned char ch;
     for (int i = fcell; i < n; ++i) {
        if (BottomLine(i)) {
@@ -2505,7 +2459,7 @@ int LyXTabular::AsciiPrintCell(Buffer const * buf, ostream & os,
        len2 += clen[column] + 4;
     len2 -= len1;
 
-    switch(GetAlignment(cell)) {
+    switch (GetAlignment(cell)) {
     default:
     case LYX_ALIGN_LEFT:
        len1 = 0;
@@ -2520,10 +2474,10 @@ int LyXTabular::AsciiPrintCell(Buffer const * buf, ostream & os,
        break;
     }
 
-    for(unsigned int i = 0; i < len1; ++i)
+    for (unsigned int i = 0; i < len1; ++i)
        os << " ";
     os << sstr.str();
-    for(unsigned int i = 0; i < len2; ++i)
+    for (unsigned int i = 0; i < len2; ++i)
        os << " ";
     if (RightLine(cell))
        os << " |";
@@ -2544,9 +2498,9 @@ 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) {
+    for (int j = 0; j < columns_; ++j) {
        clen[j] = 0;
-       for(int i = 0; i < rows_; ++i) {
+       for (int i = 0; i < rows_; ++i) {
            int cell = GetCellNumber(i, j);
            if (IsMultiColumn(cell, true))
                continue;
@@ -2557,8 +2511,8 @@ int LyXTabular::Ascii(Buffer const * buf, ostream & os) const
        }
     }
     // then all (real) multicolumn cells!
-    for(int j = 0; j < columns_; ++j) {
-       for(int i = 0; i < rows_; ++i) {
+    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;
@@ -2573,9 +2527,9 @@ int LyXTabular::Ascii(Buffer const * buf, ostream & os) const
        }
     }
     int cell = 0;
-    for(int i = 0; i < rows_; ++i) {
+    for (int i = 0; i < rows_; ++i) {
        AsciiTopHLine(os, i, clen);
-       for(int j = 0; j < columns_; ++j) {
+       for (int j = 0; j < columns_; ++j) {
            if (IsPartOfMultiColumn(i,j))
                continue;
            ret += AsciiPrintCell(buf, os, cell, i, j, clen);
@@ -2594,13 +2548,19 @@ InsetText * LyXTabular::GetCellInset(int cell) const
 }
 
 
+InsetText * LyXTabular::GetCellInset(int row, int column) const
+{
+    return GetCellInset(GetCellNumber(row, column));
+}
+
+
 void LyXTabular::Validate(LaTeXFeatures & features) const
 {
     if (IsLongTabular())
         features.longtable = true;
     if (NeedRotating())
        features.rotating = true;
-    for(int cell = 0; cell < numberofcells; ++cell) {
+    for (int cell = 0; cell < numberofcells; ++cell) {
        if (GetVAlignment(cell) != LYX_VALIGN_TOP)
            features.array = true;
        GetCellInset(cell)->Validate(features);
@@ -2612,8 +2572,8 @@ 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) {
+    for (; par; par = par->next) {
+       for (int i = 0; i < par->Last(); ++i) {
            if (par->GetChar(i) == LyXParagraph::META_NEWLINE)
                return BOX_PARBOX;
        }