]> git.lyx.org Git - lyx.git/blobdiff - src/tabular.C
more changes, read the Changelog
[lyx.git] / src / tabular.C
index dcf7e77e9ef4cfba443c9162de214c2bc5635a35..feb9c652b79df35aaec89e061df5b9772df3fa66 100644 (file)
@@ -40,6 +40,8 @@ using std::vector;
 
 static int const WIDTH_OF_LINE = 5;
 
+extern BufferView * current_view;
+
 /// Define a few methods for the inner structs
 
 LyXTabular::cellstruct::cellstruct() 
@@ -52,42 +54,6 @@ LyXTabular::cellstruct::cellstruct()
     bottom_line = false;
     rotate = false;
     linebreaks = false;
-    inset = 0;
-}
-
-
-LyXTabular::cellstruct::~cellstruct() 
-{
-    delete inset;
-}
-
-
-LyXTabular::cellstruct::cellstruct(cellstruct const & cs)
-{
-    cellno = cs.cellno;
-    width_of_cell = cs.width_of_cell;
-    multicolumn = cs.multicolumn;
-    alignment = cs.alignment;
-    top_line = cs.top_line;
-    bottom_line = cs.bottom_line;
-    rotate = cs.rotate;
-    linebreaks = cs.linebreaks;
-    inset = 0;
-}
-
-
-LyXTabular::cellstruct & 
-LyXTabular::cellstruct::operator=(cellstruct const & cs)
-{
-    cellno = cs.cellno;
-    width_of_cell = cs.width_of_cell;
-    multicolumn = cs.multicolumn;
-    alignment = cs.alignment;
-    top_line = cs.top_line;
-    bottom_line = cs.bottom_line;
-    rotate = cs.rotate;
-    linebreaks = cs.linebreaks;
-    return *this;
 }
 
 
@@ -127,10 +93,10 @@ LyXTabular::LyXTabular(InsetTabular * inset, LyXTabular const & lt)
 }
 
 
-LyXTabular::LyXTabular(InsetTabular * inset, LyXLex & lex)
+LyXTabular::LyXTabular(Buffer const * buf, InsetTabular * inset, LyXLex & lex)
 {
     owner_ = inset;
-    Read(lex);
+    Read(buf, lex);
 }
 
 
@@ -177,10 +143,8 @@ LyXTabular * LyXTabular::Clone(InsetTabular * inset)
     int i,j;
     for(i=0; i < rows_; ++i) {
        for(j=0; j < columns_; ++j) {
-           delete result->cell_info[i][j].inset;
-           result->cell_info[i][j].inset=new InsetText(*cell_info[i][j].inset,
-                                                       inset->BufferOwner());
-           result->cell_info[i][j].inset->setOwner(inset);
+           result->cell_info[i][j].inset = cell_info[i][j].inset;
+           result->cell_info[i][j].inset.setOwner(inset);
        }
     }
     return result;
@@ -203,9 +167,8 @@ void LyXTabular::Init(int rows_arg, int columns_arg)
     // Jürgen, use iterators.
     for (i = 0; i < rows_; ++i) {
         for (j = 0; j < columns_; ++j) {
-            cell_info[i][j].inset = new InsetText(owner_->BufferOwner());
-           cell_info[i][j].inset->setOwner(owner_);
-           cell_info[i][j].inset->SetDrawLockedFrame(true);
+           cell_info[i][j].inset.setOwner(owner_);
+           cell_info[i][j].inset.SetDrawFrame(0, InsetText::LOCKED);
             cell_info[i][j].cellno = cellno++;
         }
     }
@@ -231,146 +194,99 @@ void LyXTabular::Init(int rows_arg, int columns_arg)
 }
 
 
-void LyXTabular::AppendRow(int /* cell */)
+void LyXTabular::AppendRow(int cell )
 {
-#if 0
+    ++rows_;
+   
     int row = row_of_cell(cell);
-    rowstruct * row_info2 = new rowstruct[rows_ + 1];
-    cellstruct ** cell_info2 = new cellstruct * [rows_ + 1];
-    int i;
 
-    for (i = 0; i <= row; ++i) {
-        cell_info2[i] = cell_info[i];
-        row_info2[i] = row_info[i];
+    row_vector::iterator rit = row_info.begin() + row;
+    row_info.insert(rit, rowstruct());
+
+#if 0
+    cell_vvector::iterator cit = cell_info.begin() + row;
+    cell_info.insert(cit, vector<cellstruct>(columns_, cellstruct()));
+#else
+    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) {
+           c_info[i][j] = cell_info[i][j];
+       }
     }
-    for (i = rows_ - 1; i >= row; --i) {
-        cell_info2[i + 1] = cell_info[i];
-        row_info2[i + 1] = row_info[i];
+    for(int i = row+1; i < rows_; ++i) {
+       for(int j = 0; j < columns_; ++j) {
+           c_info[i][j] = cell_info[i-1][j];
+       }
     }
-    row_info2[row + 1].top_line = row_info[i].top_line;
-    cell_info2[row + 1] = new cellstruct[columns_](owner_->BufferOwner());
-    for (i = 0; i < columns_; ++i) {
-        cell_info2[row + 1][i].width_of_cell = 0;
-        cell_info2[row + 1][i] = cell_info2[row][i];
+    cell_info = c_info;
+    ++row;
+    for (int j = 0; j < columns_; ++j) {
+       cell_info[row][j].inset.clear();
     }
-   
-    delete[] cell_info;
-    cell_info = cell_info2;
-    delete[] row_info;
-    row_info = row_info2;
-   
-    ++rows_;
-   
-    Reinit();
 #endif
+    Reinit();
 }
 
 
 void LyXTabular::DeleteRow(int row)
 {
-       row_info.erase(row_info.begin() + row); //&row_info[row]);
-       cell_info.erase(cell_info.begin() + row); //&cell_info[row]);
-       --rows_;
-       Reinit();
+    if (!(rows_ - 1))
+       return;
+    row_info.erase(row_info.begin() + row); //&row_info[row]);
+    cell_info.erase(cell_info.begin() + row); //&cell_info[row]);
+    --rows_;
+    Reinit();
 }
 
 
-void LyXTabular::AppendColumn(int /*cell*/)
+void LyXTabular::AppendColumn(int cell)
 {
-#if 0
-    int j;
-    columnstruct * column_info2 = new columnstruct[columns_ + 1];
-    int column = right_column_of_cell(cell);
+    ++columns_;
+   
+    cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
+                                                         cellstruct()));
+    int column = column_of_cell(cell);
+    int i, j;
+    column_vector::iterator cit = column_info.begin() + column;
+    column_info.insert(cit, columnstruct());
 
-    int i = 0;
-    for (; i <= column; ++i) {
-        column_info2[i] = column_info[i];
-    }
-    for (i = columns_ - 1; i >= column; --i) {
-        column_info2[i + 1] = column_info[i];
-    }
-    
-    delete[] column_info;
-    column_info = column_info2;
-    
     for (i = 0; i < rows_; ++i) {
-        cellstruct * tmp = cell_info[i];
-        cell_info[i] = new cellstruct[columns_ + 1](owner_->BufferOwner());
         for (j = 0; j <= column; ++j) {
-            cell_info[i][j] = tmp[j];
+            c_info[i][j] = cell_info[i][j];
         }
-        for (j = column; j < columns_; ++j) {
-            cell_info[i][j + 1] = tmp[j];
+        for (j = column+1; j < columns_; ++j) {
+            c_info[i][j] = cell_info[i][j-1];
         }
         // care about multicolumns
-        if (cell_info[i][column + 1].multicolumn
-            == LyXTabular::CELL_BEGIN_OF_MULTICOLUMN){
-            cell_info[i][column + 1].multicolumn = 
-                LyXTabular::CELL_PART_OF_MULTICOLUMN;
+        if (cell_info[i][column+1].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
+            cell_info[i][column+1].multicolumn = CELL_PART_OF_MULTICOLUMN;
         }
-        if (column + 1 == columns_
-            || cell_info[i][column + 2].multicolumn
-            != LyXTabular::CELL_PART_OF_MULTICOLUMN){
-            cell_info[i][column + 1].multicolumn = 
-                LyXTabular::CELL_NORMAL;
+        if (((column+1) == columns_) ||
+            (cell_info[i][column+2].multicolumn != CELL_PART_OF_MULTICOLUMN)) {
+            cell_info[i][column+1].multicolumn = LyXTabular::CELL_NORMAL;
         }
-        delete[] tmp;
     }
-    
-    ++columns_;
-    Reinit();
-#endif
-}
-
-
-void LyXTabular::DeleteColumn(int /*cell*/)
-{
-#if 0
-    int column1 = column_of_cell(cell);
-    int column2 = right_column_of_cell(cell);
-    
-    if (column1 == 0 && column2 == columns_ - 1)
-       return;
-    
-    for (int column = column1; column <= column2; ++column) {
-       delete_column(column1);
+    cell_info = c_info;
+    ++column;
+    for (i = 0; i < rows_; ++i) {
+       cell_info[i][column].inset.clear();
     }
     Reinit();
-#endif
 }
 
 
-void LyXTabular::delete_column(int /*column*/)
+void LyXTabular::DeleteColumn(int column)
 {
-#if 0
-    int i, j;
-    columnstruct * column_info2 = new columnstruct[columns_-1];
-   
-    for (i = 0; i < column; ++i) {
-        column_info2[i] = column_info[i];
-    }
-    for (i = column; i < columns_ - 1; ++i) {
-        column_info2[i] = column_info[i + 1];
-    }
-   
-    delete[] column_info;
-    column_info = column_info2;
-
-    for (i = 0; i < rows_; ++i) {
-        cellstruct * tmp = cell_info[i];
-        cell_info[i] = new cellstruct[columns_ - 1](owner_->BufferOwner());
-        for (j = 0; j < column; ++j) {
-            cell_info[i][j] = tmp[j];
-        }
-        for (j = column; j < columns_ - 1; ++j) {
-            cell_info[i][j] = tmp[j + 1];
-        }
-        delete[] tmp;
+    if (!(columns_ - 1))
+       return;
+    column_info.erase(column_info.begin() + column);
+    for (int i = 0; i < rows_; ++i) {
+       cell_info[i].erase(cell_info[i].begin() + column);
     }
-
     --columns_;
     Reinit();
-#endif
 }
 
 
@@ -384,6 +300,7 @@ void LyXTabular::Reinit()
     for (; i < rows_; ++i) {
        for (j = 0; j < columns_; ++j) {
            cell_info[i][j].width_of_cell = 0;
+           cell_info[i][j].inset.setOwner(owner_);
        }
     }
   
@@ -437,7 +354,7 @@ void LyXTabular::set_row_column_number_info()
        for (column = 0; column<columns_; ++column) {
            if (IsPartOfMultiColumn(row,column))
                continue;
-           cell_info[row][column].inset->SetAutoBreakRows(
+           cell_info[row][column].inset.SetAutoBreakRows(
                !GetPWidth(GetCellNumber(row, column)).empty());
        }
     }
@@ -462,25 +379,6 @@ int LyXTabular::NumberOfCellsInRow(int cell) const
 }
 
 
-int LyXTabular::AppendCellAfterCell(int append_cell, int question_cell)
-{
-    return (right_column_of_cell(append_cell) ==
-           right_column_of_cell(question_cell));
-}
-
-
-int LyXTabular::DeleteCellIfColumnIsDeleted(int cell, int delete_column_cell)
-{
-    if (column_of_cell(delete_column_cell) == 0 && 
-        right_column_of_cell(delete_column_cell) == columns_ - 1)
-        return 0;
-    else
-        return
-            (column_of_cell(cell) >= column_of_cell(delete_column_cell) &&
-             column_of_cell(cell) <= right_column_of_cell(delete_column_cell));
-}
-
-
 /* returns 1 if there is a topline, returns 0 if not */ 
 bool LyXTabular::TopLine(int cell) const
 {
@@ -952,7 +850,7 @@ int LyXTabular::right_column_of_cell(int cell) const
 }
 
 
-void LyXTabular::Write(ostream & os) const
+void LyXTabular::Write(Buffer const * buf, ostream & os) const
 {
     int i, j;
 
@@ -991,7 +889,7 @@ void LyXTabular::Write(ostream & os) const
                "\" special=\"" << cell_info[i][j].align_special <<
                "\">" << endl;
            os << "\\begin_inset ";
-           cell_info[i][j].inset->Write(os);
+           cell_info[i][j].inset.Write(buf, os);
            os << "\n\\end_inset " << endl;
            os << "</Cell>" << endl;
            os << "</Column>" << endl;
@@ -1084,7 +982,7 @@ void l_getline(istream & is, string & str)
 }
 
 
-void LyXTabular::Read(LyXLex & lex)
+void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
 {
     string line;
     istream & is = lex.getStream();
@@ -1157,7 +1055,7 @@ void LyXTabular::Read(LyXLex & lex)
            (void)getTokenValue(line, "special", cell_info[i][j].align_special);
            l_getline(is, line);
            if (prefixIs(line, "\\begin_inset")) {
-               cell_info[i][j].inset->Read(lex);
+               cell_info[i][j].inset.Read(buf, lex);
                l_getline(is, line);
            }
            if (line != "</Cell>") {
@@ -1339,10 +1237,10 @@ void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
     }
     // now we have the par we should fill the insets with this!
     int cell = 0;
-    InsetText *inset = GetCellInset(cell);
+    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()) {
@@ -1369,7 +1267,7 @@ void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
                par->InsertChar(i, ' ');
            }
        }
-       par->CopyIntoMinibuffer(i);
+       par->CopyIntoMinibuffer(current_view->buffer()->params, i);
        inset->par->InsertFromMinibuffer(inset->par->Last());
     }
     Reinit();
@@ -1561,19 +1459,14 @@ LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
 
 void LyXTabular::SetMultiColumn(int cell, int number)
 {
-    int new_width = cellinfo_of_cell(cell)->width_of_cell;
-    
-    cellinfo_of_cell(cell)->multicolumn = LyXTabular::CELL_BEGIN_OF_MULTICOLUMN;
+    cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
     cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
     cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
     cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
     for (number--; number > 0; --number) {
-        cellinfo_of_cell(cell+number)->multicolumn = 
-            LyXTabular::CELL_PART_OF_MULTICOLUMN;
-        new_width += cellinfo_of_cell(cell+number)->width_of_cell;
+        cellinfo_of_cell(cell+number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
     }
     set_row_column_number_info();
-    SetWidthOfCell(cell, new_width);
 }
 
 
@@ -1583,8 +1476,9 @@ int LyXTabular::cells_in_multicolumn(int cell) const
     int column = column_of_cell(cell);
     int result = 1;
     ++column;
-    while (column < columns_ && cell_info[row][column].multicolumn
-           == LyXTabular::CELL_PART_OF_MULTICOLUMN){
+    while ((column < columns_) &&
+          cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
+    {
         ++result;
         ++column;
     }
@@ -1599,15 +1493,13 @@ int LyXTabular::UnsetMultiColumn(int cell)
     
     int result = 0;
     
-    if (cell_info[row][column].multicolumn
-        == LyXTabular::CELL_BEGIN_OF_MULTICOLUMN){
-        cell_info[row][column].multicolumn = LyXTabular::CELL_NORMAL;
+    if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
+        cell_info[row][column].multicolumn = CELL_NORMAL;
         ++column;
-        while (column < columns_ &&
-               cell_info[row][column].multicolumn
-               == LyXTabular::CELL_PART_OF_MULTICOLUMN){
-            cell_info[row][column].multicolumn = 
-                LyXTabular::CELL_NORMAL;
+        while ((column < columns_) &&
+               (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
+       {
+            cell_info[row][column].multicolumn = CELL_NORMAL;
             ++column;
             ++result;
         }
@@ -1669,7 +1561,7 @@ bool LyXTabular::NeedRotating() const
 
 bool LyXTabular::IsLastCell(int cell) const
 {
-    if (cell < GetNumberOfCells())
+    if ((cell+1) < GetNumberOfCells())
         return false;
     return true;
 }
@@ -1683,6 +1575,34 @@ int LyXTabular::GetCellAbove(int cell) const
 }
 
 
+int LyXTabular::GetCellBelow(int cell) const
+{
+    if (row_of_cell(cell)+1 < rows_)
+        return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
+    return cell;
+}
+
+
+int LyXTabular::GetLastCellAbove(int cell) const
+{
+    if (row_of_cell(cell) <= 0)
+       return cell;
+    if (!IsMultiColumn(cell))
+       return GetCellAbove(cell);
+    return cell_info[row_of_cell(cell)-1][right_column_of_cell(cell)].cellno;
+}
+
+
+int LyXTabular::GetLastCellBelow(int cell) const
+{
+    if (row_of_cell(cell)+1 >= rows_)
+       return cell;
+    if (!IsMultiColumn(cell))
+       return GetCellBelow(cell);
+    return cell_info[row_of_cell(cell)+1][right_column_of_cell(cell)].cellno;
+}
+
+
 int LyXTabular::GetCellNumber(int row, int column) const
 {
     if (column >= columns_)
@@ -1792,19 +1712,21 @@ bool LyXTabular::GetLTNewPage(int cell) const
 }
 
 
-void LyXTabular::SetAscentOfRow(int row, int height)
+bool LyXTabular::SetAscentOfRow(int row, int height)
 {
-    if (row >= rows_)
-        return;
+    if ((row >= rows_) || (row_info[row].ascent_of_row == height))
+        return false;
     row_info[row].ascent_of_row = height;
+    return true;
 }
 
 
-void LyXTabular::SetDescentOfRow(int row, int height)
+bool LyXTabular::SetDescentOfRow(int row, int height)
 {
-    if (row >= rows_)
-        return;
+    if ((row >= rows_) || (row_info[row].descent_of_row == height))
+        return false;
     row_info[row].descent_of_row = height;
+    return true;
 }
 
 
@@ -1970,7 +1892,7 @@ int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
 }
 
 
-int LyXTabular::Latex(ostream & os, bool fragile, bool fp) const
+int LyXTabular::Latex(Buffer const * buf, ostream & os, bool fragile, bool fp) const
 {
     int ret = 0;
     int i,j;
@@ -2026,7 +1948,7 @@ int LyXTabular::Latex(ostream & os, bool fragile, bool fp) const
            if (IsPartOfMultiColumn(i,j))
                continue;
            ret += TeXCellPreamble(os, cell);
-           ret += GetCellInset(cell)->Latex(os, fragile, fp);
+           ret += GetCellInset(cell)->Latex(buf, os, fragile, fp);
            ret += TeXCellPostamble(os, cell);
            if (!IsLastCellInRow(cell)) { // not last cell in row
                os << "&" << endl;
@@ -2079,7 +2001,7 @@ int LyXTabular::Latex(ostream & os, bool fragile, bool fp) const
 
 InsetText * LyXTabular::GetCellInset(int cell) const
 {
-    return cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
+    return cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
 }
 
 void LyXTabular::Validate(LaTeXFeatures & features) const