1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 2000-2002 The LyX Team.
8 * @author: Jürgen Vigna
10 * ======================================================
16 #pragma implementation
19 // temporary until verified (08/08/2001 Jug)
20 #define SPECIAL_COLUM_HANDLING 1
27 #include "BufferView.h"
28 #include "frontends/Painter.h"
29 #include "LaTeXFeatures.h"
30 #include "insets/insettabular.h"
31 #include "insets/insettext.h"
32 #include "support/lstrings.h"
33 #include "support/lyxmanip.h"
34 #include "support/LAssert.h"
35 #include "frontends/Alert.h"
37 #include "tabular_funcs.h"
51 #ifndef CXX_GLOBAL_CSTD
57 int const WIDTH_OF_LINE = 5;
61 /// Define a few methods for the inner structs
63 LyXTabular::cellstruct::cellstruct(BufferParams const & bg)
68 multicolumn = LyXTabular::CELL_NORMAL;
69 alignment = LYX_ALIGN_CENTER;
70 valignment = LYX_VALIGN_TOP;
80 LyXTabular::rowstruct::rowstruct()
94 LyXTabular::columnstruct::columnstruct()
98 alignment = LYX_ALIGN_CENTER;
99 valignment = LYX_VALIGN_TOP;
104 LyXTabular::lttype::lttype()
113 LyXTabular::LyXTabular(BufferParams const & bp,
114 InsetTabular * inset, int rows_arg, int columns_arg)
118 Init(bp, rows_arg, columns_arg);
122 LyXTabular::LyXTabular(BufferParams const & bp,
123 InsetTabular * inset, LyXTabular const & lt,
128 Init(bp, lt.rows_, lt.columns_, <);
129 // we really should change again to have InsetText as a pointer
130 // and allocate it then we would not have to do this stuff all
133 for (int i = 0; i < rows_; ++i) {
134 for (int j = 0; j < columns_; ++j) {
135 cell_info[i][j].inset.id(lt.cell_info[i][j].inset.id());
136 cell_info[i][j].inset.setParagraphData(lt.cell_info[i][j].inset.paragraph(),true);
142 #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)
149 LyXTabular::LyXTabular(Buffer const * buf, InsetTabular * inset, LyXLex & lex)
157 LyXTabular & LyXTabular::operator=(LyXTabular const & lt)
160 #warning This while method should look like this: (Lgb)
165 // If this and lt is not of the same size we have a serious bug
166 // So then it is ok to throw an exception, or for now
168 lyx::Assert(rows_ == lt.rows_ && columns_ == lt.columns_);
170 cell_info = lt.cell_info;
171 row_info = lt.row_info;
172 column_info = lt.column_info;
173 SetLongTabular(lt.is_long_tabular);
182 LyXTabular * LyXTabular::clone(BufferParams const & bp,
183 InsetTabular * inset, bool same_id)
185 LyXTabular * result = new LyXTabular(bp, inset, *this, same_id);
187 // don't know if this is good but I need to Clone also
188 // the text-insets here, this is for the Undo-facility!
189 for (int i = 0; i < rows_; ++i) {
190 for (int j = 0; j < columns_; ++j) {
191 result->cell_info[i][j].inset = cell_info[i][j].inset;
192 result->cell_info[i][j].inset.setOwner(inset);
200 /* activates all lines and sets all widths to 0 */
201 void LyXTabular::Init(BufferParams const & bp,
202 int rows_arg, int columns_arg, LyXTabular const * lt)
205 columns_ = columns_arg;
206 row_info = row_vector(rows_, rowstruct());
207 column_info = column_vector(columns_, columnstruct());
208 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
216 for (int i = 0; i < rows_; ++i) {
217 for (int j = 0; j < columns_; ++j) {
218 cell_info[i][j].inset.setOwner(owner_);
219 cell_info[i][j].inset.setDrawFrame(0, InsetText::LOCKED);
220 cell_info[i][j].cellno = cellno++;
222 cell_info[i].back().right_line = true;
224 row_info.back().bottom_line = true;
225 row_info.front().bottom_line = true;
227 for (int i = 0; i < columns_; ++i) {
228 calculate_width_of_column(i);
230 column_info.back().right_line = true;
232 calculate_width_of_tabular();
234 rowofcell = vector<int>();
235 columnofcell = vector<int>();
236 set_row_column_number_info();
237 is_long_tabular = false;
242 void LyXTabular::AppendRow(BufferParams const & bp, int cell)
246 int row = row_of_cell(cell);
248 row_vector::iterator rit = row_info.begin() + row;
249 row_info.insert(rit, rowstruct());
250 // now set the values of the row before
251 row_info[row] = row_info[row + 1];
254 cell_vvector::iterator cit = cell_info.begin() + row;
255 cell_info.insert(cit, vector<cellstruct>(columns_, cellstruct(bp)));
257 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
260 for (int i = 0; i <= row; ++i) {
261 for (int j = 0; j < columns_; ++j) {
262 c_info[i][j] = cell_info[i][j];
265 for (int i = row + 1; i < rows_; ++i) {
266 for (int j = 0; j < columns_; ++j) {
267 c_info[i][j] = cell_info[i-1][j];
272 for (int j = 0; j < columns_; ++j) {
273 cell_info[row][j].inset.clear();
280 void LyXTabular::DeleteRow(int row)
282 if (rows_ == 1) return; // Not allowed to delete last row
284 row_info.erase(row_info.begin() + row); //&row_info[row]);
285 cell_info.erase(cell_info.begin() + row); //&cell_info[row]);
291 void LyXTabular::AppendColumn(BufferParams const & bp, int cell)
295 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
297 int const column = column_of_cell(cell);
298 column_vector::iterator cit = column_info.begin() + column + 1;
299 column_info.insert(cit, columnstruct());
300 // set the column values of the column before
301 column_info[column + 1] = column_info[column];
303 for (int i = 0; i < rows_; ++i) {
304 for (int j = 0; j <= column; ++j) {
305 c_info[i][j] = cell_info[i][j];
307 for (int j = column + 1; j < columns_; ++j) {
308 c_info[i][j] = cell_info[i][j - 1];
310 // care about multicolumns
311 if (c_info[i][column + 1].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
313 c_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
315 if ((column + 2) >= columns_ ||
316 c_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
318 c_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
323 for (int i = 0; i < rows_; ++i) {
324 //cell_info[i][column].inset.clear();
325 cell_info[i][column + 1].inset.clear();
331 void LyXTabular::DeleteColumn(int column)
333 // Similar to DeleteRow
334 //if (!(columns_ - 1))
336 if (columns_ == 1) return; // Not allowed to delete last column
338 column_info.erase(column_info.begin() + column);
339 for (int i = 0; i < rows_; ++i) {
340 cell_info[i].erase(cell_info[i].begin() + column);
347 void LyXTabular::reinit()
353 void LyXTabular::Reinit(bool reset_widths)
356 for (int i = 0; i < rows_; ++i) {
357 for (int j = 0; j < columns_; ++j) {
358 cell_info[i][j].width_of_cell = 0;
359 cell_info[i][j].inset.setOwner(owner_);
364 for (int i = 0; i < columns_; ++i) {
365 calculate_width_of_column(i);
367 calculate_width_of_tabular();
369 set_row_column_number_info();
373 void LyXTabular::set_row_column_number_info(bool oldformat)
376 for (int row = 0; row < rows_; ++row) {
377 for (int column = 0; column<columns_; ++column) {
378 if (cell_info[row][column].multicolumn
379 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
381 cell_info[row][column].cellno = numberofcells;
384 ++numberofcells; // because this is one more than as we start from 0
386 rowofcell.resize(numberofcells);
387 columnofcell.resize(numberofcells);
389 for (int row = 0, column = 0, c = 0;
390 c < numberofcells && row < rows_ && column < columns_;) {
392 columnofcell[c] = column;
396 } while (column < columns_ &&
397 cell_info[row][column].multicolumn
398 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
399 if (column == columns_) {
405 for (int row = 0; row < rows_; ++row) {
406 for (int column = 0; column < columns_; ++column) {
407 if (IsPartOfMultiColumn(row,column))
409 // now set the right line of multicolumns right for oldformat read
411 cell_info[row][column].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
413 int cn=cells_in_multicolumn(cell_info[row][column].cellno);
414 cell_info[row][column].right_line =
415 cell_info[row][column+cn-1].right_line;
417 cell_info[row][column].inset.setAutoBreakRows(
418 !GetPWidth(GetCellNumber(row, column)).zero());
424 int LyXTabular::GetNumberOfCells() const
426 return numberofcells;
430 int LyXTabular::NumberOfCellsInRow(int cell) const
432 int const row = row_of_cell(cell);
434 for (int i = 0; i < columns_; ++i) {
435 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
442 /* returns 1 if there is a topline, returns 0 if not */
443 bool LyXTabular::TopLine(int cell, bool onlycolumn) const
445 int const row = row_of_cell(cell);
447 if (!onlycolumn && IsMultiColumn(cell))
448 return cellinfo_of_cell(cell)->top_line;
449 return row_info[row].top_line;
453 bool LyXTabular::BottomLine(int cell, bool onlycolumn) const
455 // no bottom line underneath non-existent cells if you please
456 // Isn't that a programming error? Is so this should
457 // be an Assert instead. (Lgb)
458 if (cell >= numberofcells)
461 if (!onlycolumn && IsMultiColumn(cell))
462 return cellinfo_of_cell(cell)->bottom_line;
463 return row_info[row_of_cell(cell)].bottom_line;
467 bool LyXTabular::LeftLine(int cell, bool onlycolumn) const
469 if (!onlycolumn && IsMultiColumn(cell) &&
470 (IsFirstCellInRow(cell) || IsMultiColumn(cell-1)))
472 #ifdef SPECIAL_COLUM_HANDLING
473 if (cellinfo_of_cell(cell)->align_special.empty())
474 return cellinfo_of_cell(cell)->left_line;
475 return prefixIs(frontStrip(cellinfo_of_cell(cell)->align_special), "|");
477 return cellinfo_of_cell(cell)->left_line;
480 #ifdef SPECIAL_COLUM_HANDLING
481 if (column_info[column_of_cell(cell)].align_special.empty())
482 return column_info[column_of_cell(cell)].left_line;
483 return prefixIs(frontStrip(column_info[column_of_cell(cell)].align_special), "|");
485 return column_info[column_of_cell(cell)].left_line;
490 bool LyXTabular::RightLine(int cell, bool onlycolumn) const
492 if (!onlycolumn && IsMultiColumn(cell) &&
493 (IsLastCellInRow(cell) || IsMultiColumn(cell+1)))
495 #ifdef SPECIAL_COLUM_HANDLING
496 if (cellinfo_of_cell(cell)->align_special.empty())
497 return cellinfo_of_cell(cell)->right_line;
498 return suffixIs(strip(cellinfo_of_cell(cell)->align_special), "|");
500 return cellinfo_of_cell(cell)->right_line;
503 #ifdef SPECIAL_COLUM_HANDLING
504 if (column_info[column_of_cell(cell)].align_special.empty())
505 return column_info[right_column_of_cell(cell)].right_line;
506 return suffixIs(strip(column_info[column_of_cell(cell)].align_special), "|");
508 return column_info[right_column_of_cell(cell)].right_line;
513 bool LyXTabular::TopAlreadyDrawed(int cell) const
515 int row = row_of_cell(cell);
516 if ((row > 0) && !GetAdditionalHeight(row)) {
517 int column = column_of_cell(cell);
520 && cell_info[row][column].multicolumn
521 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
523 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
524 return row_info[row].bottom_line;
526 return cell_info[row][column].bottom_line;
532 bool LyXTabular::LeftAlreadyDrawed(int cell) const
534 int column = column_of_cell(cell);
536 int row = row_of_cell(cell);
538 (cell_info[row][column].multicolumn ==
539 LyXTabular::CELL_PART_OF_MULTICOLUMN));
540 if (GetAdditionalWidth(cell_info[row][column].cellno))
542 #ifdef SPECIAL_COLUM_HANDLING
543 return RightLine(cell_info[row][column].cellno);
545 return RightLine(cell_info[row][column].cellno, true);
552 bool LyXTabular::IsLastRow(int cell) const
554 return (row_of_cell(cell) == rows_ - 1);
558 int LyXTabular::GetAdditionalHeight(int row) const
560 if (!row || row >= rows_)
566 for (int column = 0; column < columns_ && bottom; ++column) {
567 switch (cell_info[row - 1][column].multicolumn) {
568 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
569 bottom = cell_info[row - 1][column].bottom_line;
571 case LyXTabular::CELL_NORMAL:
572 bottom = row_info[row - 1].bottom_line;
575 for (int column = 0; column < columns_ && top; ++column) {
576 switch (cell_info[row][column].multicolumn) {
577 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
578 top = cell_info[row][column].top_line;
580 case LyXTabular::CELL_NORMAL:
581 top = row_info[row].top_line;
585 return WIDTH_OF_LINE;
590 int LyXTabular::GetAdditionalWidth(int cell) const
592 // internally already set in SetWidthOfCell
593 // used to get it back in text.C
594 int const col = right_column_of_cell(cell);
595 int const row = row_of_cell(cell);
596 if (col < columns_ - 1 && RightLine(cell) &&
597 LeftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
599 return WIDTH_OF_LINE;
606 // returns the maximum over all rows
607 int LyXTabular::GetWidthOfColumn(int cell) const
609 int const column1 = column_of_cell(cell);
610 int const column2 = right_column_of_cell(cell);
612 for (int i = column1; i <= column2; ++i) {
613 result += column_info[i].width_of_column;
619 int LyXTabular::GetWidthOfTabular() const
621 return width_of_tabular;
625 /* returns 1 if a complete update is necessary, otherwise 0 */
626 bool LyXTabular::SetWidthOfMulticolCell(int cell, int new_width)
628 if (!IsMultiColumn(cell))
631 int const row = row_of_cell(cell);
632 int const column1 = column_of_cell(cell);
633 int const column2 = right_column_of_cell(cell);
634 int const old_val = cell_info[row][column2].width_of_cell;
636 // first set columns to 0 so we can calculate the right width
637 for (int i = column1; i <= column2; ++i) {
638 cell_info[row][i].width_of_cell = 0;
640 // set the width to MAX_WIDTH until width > 0
641 int width = (new_width + 2 * WIDTH_OF_LINE);
643 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
644 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
645 width -= column_info[i].width_of_column;
648 cell_info[row][i].width_of_cell = width;
650 if (old_val != cell_info[row][column2].width_of_cell) {
651 // in this case we have to recalculate all multicolumn cells which
652 // have this column as one of theirs but not as last one
653 calculate_width_of_column_NMC(i);
654 recalculateMulticolumnsOfColumn(i);
655 calculate_width_of_column(i);
661 void LyXTabular::recalculateMulticolumnsOfColumn(int column)
663 // the last column does not have to be recalculated because all
664 // multicolumns will have here there last multicolumn cell which
665 // always will have the whole rest of the width of the cell.
666 if (column > (columns_ - 2))
668 for(int row = 0; row < rows_; ++row) {
669 int mc = cell_info[row][column].multicolumn;
670 int nmc = cell_info[row][column+1].multicolumn;
671 // we only have to update multicolumns which do not have this
672 // column as their last column!
673 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
674 ((mc == CELL_PART_OF_MULTICOLUMN) &&
675 (nmc == CELL_PART_OF_MULTICOLUMN)))
677 int const cellno = cell_info[row][column].cellno;
678 SetWidthOfMulticolCell(cellno,
679 GetWidthOfCell(cellno)-(2 * WIDTH_OF_LINE));
685 /* returns 1 if a complete update is necessary, otherwise 0 */
686 bool LyXTabular::SetWidthOfCell(int cell, int new_width)
688 int const row = row_of_cell(cell);
689 int const column1 = column_of_cell(cell);
694 #ifdef SPECIAL_COLUM_HANDLING
695 if (RightLine(cell_info[row][column1].cellno, true) &&
696 (column1 < columns_-1) &&
697 LeftLine(cell_info[row][column1+1].cellno, true))
699 if (column_info[column1].right_line && (column1 < columns_-1) &&
700 column_info[column1+1].left_line) // additional width
704 add_width = WIDTH_OF_LINE;
706 if (GetWidthOfCell(cell) == (new_width+2*WIDTH_OF_LINE+add_width)) {
709 if (IsMultiColumn(cell, true)) {
710 tmp = SetWidthOfMulticolCell(cell, new_width);
712 width = (new_width + 2*WIDTH_OF_LINE + add_width);
713 cell_info[row][column1].width_of_cell = width;
714 tmp = calculate_width_of_column_NMC(column1);
716 recalculateMulticolumnsOfColumn(column1);
719 for (int i = 0; i < columns_; ++i)
720 calculate_width_of_column(i);
721 calculate_width_of_tabular();
728 bool LyXTabular::SetAlignment(int cell, LyXAlignment align, bool onlycolumn)
730 if (!IsMultiColumn(cell) || onlycolumn)
731 column_info[column_of_cell(cell)].alignment = align;
733 cellinfo_of_cell(cell)->alignment = align;
738 bool LyXTabular::SetVAlignment(int cell, VAlignment align, bool onlycolumn)
740 if (!IsMultiColumn(cell) || onlycolumn)
741 column_info[column_of_cell(cell)].valignment = align;
743 cellinfo_of_cell(cell)->valignment = align;
748 bool LyXTabular::SetColumnPWidth(int cell, LyXLength const & width)
750 bool flag = !width.zero();
751 int const j = column_of_cell(cell);
753 column_info[j].p_width = width;
754 if (flag) // do this only if there is a width
755 SetAlignment(cell, LYX_ALIGN_LEFT);
756 for (int i = 0; i < rows_; ++i) {
757 int c = GetCellNumber(i, j);
758 flag = !GetPWidth(c).zero(); // because of multicolumns!
759 GetCellInset(c)->setAutoBreakRows(flag);
765 bool LyXTabular::SetMColumnPWidth(int cell, LyXLength const & width)
767 bool const flag = !width.zero();
769 cellinfo_of_cell(cell)->p_width = width;
770 if (IsMultiColumn(cell)) {
771 GetCellInset(cell)->setAutoBreakRows(flag);
778 bool LyXTabular::SetAlignSpecial(int cell, string const & special,
779 LyXTabular::Feature what)
781 if (what == SET_SPECIAL_MULTI)
782 cellinfo_of_cell(cell)->align_special = special;
784 column_info[column_of_cell(cell)].align_special = special;
789 bool LyXTabular::SetAllLines(int cell, bool line)
791 SetTopLine(cell, line);
792 SetBottomLine(cell, line);
793 SetRightLine(cell, line);
794 SetLeftLine(cell, line);
799 bool LyXTabular::SetTopLine(int cell, bool line, bool onlycolumn)
801 int const row = row_of_cell(cell);
803 if (onlycolumn || !IsMultiColumn(cell))
804 row_info[row].top_line = line;
806 cellinfo_of_cell(cell)->top_line = line;
811 bool LyXTabular::SetBottomLine(int cell, bool line, bool onlycolumn)
813 if (onlycolumn || !IsMultiColumn(cell))
814 row_info[row_of_cell(cell)].bottom_line = line;
816 cellinfo_of_cell(cell)->bottom_line = line;
821 bool LyXTabular::SetLeftLine(int cell, bool line, bool onlycolumn)
823 if (onlycolumn || !IsMultiColumn(cell))
824 column_info[column_of_cell(cell)].left_line = line;
826 cellinfo_of_cell(cell)->left_line = line;
831 bool LyXTabular::SetRightLine(int cell, bool line, bool onlycolumn)
833 if (onlycolumn || !IsMultiColumn(cell))
834 column_info[right_column_of_cell(cell)].right_line = line;
836 cellinfo_of_cell(cell)->right_line = line;
841 LyXAlignment LyXTabular::GetAlignment(int cell, bool onlycolumn) const
843 if (!onlycolumn && IsMultiColumn(cell))
844 return cellinfo_of_cell(cell)->alignment;
846 return column_info[column_of_cell(cell)].alignment;
850 LyXTabular::VAlignment
851 LyXTabular::GetVAlignment(int cell, bool onlycolumn) const
853 if (!onlycolumn && IsMultiColumn(cell))
854 return cellinfo_of_cell(cell)->valignment;
856 return column_info[column_of_cell(cell)].valignment;
860 LyXLength const LyXTabular::GetPWidth(int cell) const
862 if (IsMultiColumn(cell))
863 return cellinfo_of_cell(cell)->p_width;
864 return column_info[column_of_cell(cell)].p_width;
868 LyXLength const LyXTabular::GetColumnPWidth(int cell) const
870 return column_info[column_of_cell(cell)].p_width;
874 LyXLength const LyXTabular::GetMColumnPWidth(int cell) const
876 if (IsMultiColumn(cell))
877 return cellinfo_of_cell(cell)->p_width;
882 string const LyXTabular::GetAlignSpecial(int cell, int what) const
884 if (what == SET_SPECIAL_MULTI)
885 return cellinfo_of_cell(cell)->align_special;
886 return column_info[column_of_cell(cell)].align_special;
890 int LyXTabular::GetWidthOfCell(int cell) const
892 int const row = row_of_cell(cell);
893 int const column1 = column_of_cell(cell);
894 int const column2 = right_column_of_cell(cell);
896 for (int i = column1; i <= column2; ++i) {
897 result += cell_info[row][i].width_of_cell;
903 int LyXTabular::GetBeginningOfTextInCell(int cell) const
907 switch (GetAlignment(cell)) {
908 case LYX_ALIGN_CENTER:
909 x += (GetWidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
911 case LYX_ALIGN_RIGHT:
912 x += GetWidthOfColumn(cell) - GetWidthOfCell(cell);
913 // + GetAdditionalWidth(cell);
915 default: /* LYX_ALIGN_LEFT: nothing :-) */
925 bool LyXTabular::IsFirstCellInRow(int cell) const
927 return column_of_cell(cell) == 0;
931 int LyXTabular::GetFirstCellInRow(int row) const
935 return cell_info[row][0].cellno;
938 bool LyXTabular::IsLastCellInRow(int cell) const
940 return (right_column_of_cell(cell) == (columns_ - 1));
944 int LyXTabular::GetLastCellInRow(int row) const
948 return cell_info[row][columns_-1].cellno;
952 bool LyXTabular::calculate_width_of_column(int column)
954 int const old_column_width = column_info[column].width_of_column;
957 for (int i = 0; i < rows_; ++i) {
958 maximum = max(cell_info[i][column].width_of_cell, maximum);
960 column_info[column].width_of_column = maximum;
961 return (column_info[column].width_of_column != old_column_width);
966 // Calculate the columns regarding ONLY the normal cells and if this
967 // column is inside a multicolumn cell then use it only if its the last
968 // column of this multicolumn cell as this gives an added with to the
969 // column, all the rest should be adapted!
971 bool LyXTabular::calculate_width_of_column_NMC(int column)
973 int const old_column_width = column_info[column].width_of_column;
975 for (int i = 0; i < rows_; ++i) {
976 int cell = GetCellNumber(i, column);
977 bool ismulti = IsMultiColumn(cell, true);
978 if ((!ismulti || (column == right_column_of_cell(cell))) &&
979 (cell_info[i][column].width_of_cell > max))
981 max = cell_info[i][column].width_of_cell;
984 column_info[column].width_of_column = max;
985 return (column_info[column].width_of_column != old_column_width);
989 void LyXTabular::calculate_width_of_tabular()
991 width_of_tabular = 0;
992 for (int i = 0; i < columns_; ++i) {
993 width_of_tabular += column_info[i].width_of_column;
998 int LyXTabular::row_of_cell(int cell) const
1000 if (cell >= numberofcells)
1004 return rowofcell[cell];
1008 int LyXTabular::column_of_cell(int cell) const
1010 if (cell >= numberofcells)
1011 return columns_ - 1;
1014 return columnofcell[cell];
1018 int LyXTabular::right_column_of_cell(int cell) const
1020 int const row = row_of_cell(cell);
1021 int column = column_of_cell(cell);
1022 while (column < (columns_ - 1) &&
1023 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
1029 void LyXTabular::Write(Buffer const * buf, ostream & os) const
1033 << write_attribute("version", 3)
1034 << write_attribute("rows", rows_)
1035 << write_attribute("columns", columns_)
1037 // global longtable options
1039 << write_attribute("rotate", rotate)
1040 << write_attribute("islongtable", is_long_tabular)
1041 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1042 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1043 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1044 << write_attribute("headTopDL", endhead.topDL)
1045 << write_attribute("headBottomDL", endhead.bottomDL)
1046 << write_attribute("footTopDL", endfoot.topDL)
1047 << write_attribute("footBottomDL", endfoot.bottomDL)
1048 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1049 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1050 << write_attribute("lastFootEmpty", endlastfoot.empty)
1052 for (int j = 0; j < columns_; ++j) {
1054 << write_attribute("alignment", column_info[j].alignment)
1055 << write_attribute("valignment", column_info[j].valignment)
1056 << write_attribute("leftline", column_info[j].left_line)
1057 << write_attribute("rightline", column_info[j].right_line)
1058 << write_attribute("width", column_info[j].p_width.asString())
1059 << write_attribute("special", column_info[j].align_special)
1062 for (int i = 0; i < rows_; ++i) {
1064 << write_attribute("topline", row_info[i].top_line)
1065 << write_attribute("bottomline", row_info[i].bottom_line)
1066 << write_attribute("endhead", row_info[i].endhead)
1067 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1068 << write_attribute("endfoot", row_info[i].endfoot)
1069 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1070 << write_attribute("newpage", row_info[i].newpage)
1072 for (int j = 0; j < columns_; ++j) {
1074 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1075 << write_attribute("alignment", cell_info[i][j].alignment)
1076 << write_attribute("valignment", cell_info[i][j].valignment)
1077 << write_attribute("topline", cell_info[i][j].top_line)
1078 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1079 << write_attribute("leftline", cell_info[i][j].left_line)
1080 << write_attribute("rightline", cell_info[i][j].right_line)
1081 << write_attribute("rotate", cell_info[i][j].rotate)
1082 << write_attribute("usebox", cell_info[i][j].usebox)
1083 << write_attribute("width", cell_info[i][j].p_width)
1084 << write_attribute("special", cell_info[i][j].align_special)
1086 os << "\\begin_inset ";
1087 cell_info[i][j].inset.write(buf, os);
1088 os << "\n\\end_inset \n"
1093 os << "</lyxtabular>\n";
1097 void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
1100 istream & is = lex.getStream();
1102 l_getline(is, line);
1103 if (!prefixIs(line, "<lyxtabular ")
1104 && !prefixIs(line, "<LyXTabular ")) {
1105 OldFormatRead(buf->params, lex, line);
1110 if (!getTokenValue(line, "version", version))
1113 ReadOld(buf, is, lex, line);
1114 else if (version >= 2)
1115 ReadNew(buf, is, lex, line, version);
1118 void LyXTabular::setHeaderFooterRows(int hr, int fhr, int fr, int lfr)
1122 row_info[--hr].endhead = true;
1124 // set firstheader info
1125 if (fhr && (fhr < rows_)) {
1126 if (row_info[fhr].endhead) {
1128 row_info[--fhr].endfirsthead = true;
1129 row_info[fhr].endhead = false;
1131 } else if (row_info[fhr - 1].endhead) {
1132 endfirsthead.empty = true;
1134 while((fhr > 0) && !row_info[--fhr].endhead) {
1135 row_info[fhr].endfirsthead = true;
1140 if (fr && (fr < rows_)) {
1141 if (row_info[fr].endhead && row_info[fr-1].endhead) {
1142 while((fr > 0) && !row_info[--fr].endhead) {
1143 row_info[fr].endfoot = true;
1144 row_info[fr].endhead = false;
1146 } else if (row_info[fr].endfirsthead && row_info[fr-1].endfirsthead) {
1147 while((fr > 0) && !row_info[--fr].endfirsthead) {
1148 row_info[fr].endfoot = true;
1149 row_info[fr].endfirsthead = false;
1151 } else if (!row_info[fr - 1].endhead && !row_info[fr - 1].endfirsthead) {
1152 while((fr > 0) && !row_info[--fr].endhead &&
1153 !row_info[fr].endfirsthead)
1155 row_info[fr].endfoot = true;
1159 // set lastfooter info
1160 if (lfr && (lfr < rows_)) {
1161 if (row_info[lfr].endhead && row_info[lfr - 1].endhead) {
1162 while((lfr > 0) && !row_info[--lfr].endhead) {
1163 row_info[lfr].endlastfoot = true;
1164 row_info[lfr].endhead = false;
1166 } else if (row_info[lfr].endfirsthead &&
1167 row_info[lfr - 1].endfirsthead)
1169 while((lfr > 0) && !row_info[--lfr].endfirsthead) {
1170 row_info[lfr].endlastfoot = true;
1171 row_info[lfr].endfirsthead = false;
1173 } else if (row_info[lfr].endfoot
1174 && row_info[lfr - 1].endfoot) {
1175 while((lfr > 0) && !row_info[--lfr].endfoot) {
1176 row_info[lfr].endlastfoot = true;
1177 row_info[lfr].endfoot = false;
1179 } else if (!row_info[fr - 1].endhead
1180 && !row_info[fr - 1].endfirsthead &&
1181 !row_info[fr - 1].endfoot)
1184 !row_info[--lfr].endhead && !row_info[lfr].endfirsthead &&
1185 !row_info[lfr].endfoot)
1187 row_info[lfr].endlastfoot = true;
1189 } else if (haveLTFoot()) {
1190 endlastfoot.empty = true;
1195 void LyXTabular::ReadNew(Buffer const * buf, istream & is,
1196 LyXLex & lex, string const & l, int const version)
1200 if (!getTokenValue(line, "rows", rows_arg))
1203 if (!getTokenValue(line, "columns", columns_arg))
1205 Init(buf->params, rows_arg, columns_arg);
1206 l_getline(is, line);
1207 if (!prefixIs(line, "<features")) {
1208 lyxerr << "Wrong tabular format (expected <features ...> got" <<
1209 line << ")" << endl;
1212 getTokenValue(line, "rotate", rotate);
1213 getTokenValue(line, "islongtable", is_long_tabular);
1214 // compatibility read for old longtable options. Now we can make any
1215 // row part of the header/footer type we want before it was strict
1216 // sequential from the first row down (as LaTeX does it!). So now when
1217 // we find a header/footer line we have to go up the rows and set it
1218 // on all preceding rows till the first or one with already a h/f option
1219 // set. If we find a firstheader on the same line as a header or a
1220 // lastfooter on the same line as a footer then this should be set empty.
1228 getTokenValue(line, "endhead", hrow);
1229 getTokenValue(line, "endfirsthead", fhrow);
1230 getTokenValue(line, "endfoot", frow);
1231 getTokenValue(line, "endlastfoot", lfrow);
1232 setHeaderFooterRows(abs(hrow), abs(fhrow), abs(frow), abs(lfrow));
1234 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1235 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1236 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1237 getTokenValue(line, "headTopDL", endhead.topDL);
1238 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1239 getTokenValue(line, "footTopDL", endfoot.topDL);
1240 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1241 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1242 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1243 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1245 for (int j = 0; j < columns_; ++j) {
1247 if (!prefixIs(line,"<column")) {
1248 lyxerr << "Wrong tabular format (expected <column ...> got" <<
1249 line << ")" << endl;
1252 getTokenValue(line, "alignment", column_info[j].alignment);
1253 getTokenValue(line, "valignment", column_info[j].valignment);
1254 getTokenValue(line, "leftline", column_info[j].left_line);
1255 getTokenValue(line, "rightline", column_info[j].right_line);
1256 getTokenValue(line, "width", column_info[j].p_width);
1257 getTokenValue(line, "special", column_info[j].align_special);
1260 for (int i = 0; i < rows_; ++i) {
1261 l_getline(is, line);
1262 if (!prefixIs(line, "<row")) {
1263 lyxerr << "Wrong tabular format (expected <row ...> got" <<
1264 line << ")" << endl;
1267 getTokenValue(line, "topline", row_info[i].top_line);
1268 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1269 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1270 getTokenValue(line, "endhead", row_info[i].endhead);
1271 getTokenValue(line, "endfoot", row_info[i].endfoot);
1272 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1273 getTokenValue(line, "newpage", row_info[i].newpage);
1274 for (int j = 0; j < columns_; ++j) {
1275 l_getline(is, line);
1276 if (!prefixIs(line, "<cell")) {
1277 lyxerr << "Wrong tabular format (expected <cell ...> got" <<
1278 line << ")" << endl;
1281 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1282 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1283 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1284 getTokenValue(line, "topline", cell_info[i][j].top_line);
1285 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1286 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1287 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1288 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1289 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1290 getTokenValue(line, "width", cell_info[i][j].p_width);
1291 getTokenValue(line, "special", cell_info[i][j].align_special);
1292 l_getline(is, line);
1293 if (prefixIs(line, "\\begin_inset")) {
1294 cell_info[i][j].inset.read(buf, lex);
1295 l_getline(is, line);
1297 if (!prefixIs(line, "</cell>")) {
1298 lyxerr << "Wrong tabular format (expected </cell> got" <<
1299 line << ")" << endl;
1303 l_getline(is, line);
1304 if (!prefixIs(line, "</row>")) {
1305 lyxerr << "Wrong tabular format (expected </row> got" <<
1306 line << ")" << endl;
1310 while (!prefixIs(line, "</lyxtabular>")) {
1311 l_getline(is, line);
1313 set_row_column_number_info();
1317 void LyXTabular::OldFormatRead(BufferParams const & bp,
1318 LyXLex & lex, string const & fl)
1324 int columns_arg = 0;
1325 int is_long_tabular_arg = false;
1326 int rotate_arg = false;
1335 istream & is = lex.getStream();
1338 version = lyx::atoi(s.substr(8, string::npos));
1342 vector<int> cont_row_info;
1345 lyxerr << "Tabular format < 5 is not supported anymore\n"
1346 "Get an older version of LyX (< 1.1.x) for conversion!"
1348 Alert::alert(_("Warning:"),
1349 _("Tabular format < 5 is not supported anymore\n"),
1350 _("Get an older version of LyX (< 1.1.x) for conversion!"));
1352 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1353 >> rotate_arg >> a >> b >> c >> d;
1355 is >> rows_arg >> columns_arg;
1356 Init(bp, rows_arg, columns_arg);
1357 cont_row_info = vector<int>(rows_arg);
1358 SetLongTabular(is_long_tabular_arg);
1359 SetRotateTabular(rotate_arg);
1361 for (i = 0; i < rows_; ++i) {
1363 cont_row_info[i] = false;
1365 for (i = 0; i < columns_; ++i) {
1368 for (i = 0; i < rows_; ++i) {
1369 for (j = 0; j < columns_; ++j) {
1374 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1375 >> rotate_arg >> a >> b >> c >> d;
1376 Init(bp, rows_arg, columns_arg);
1377 cont_row_info = vector<int>(rows_arg);
1378 SetLongTabular(is_long_tabular_arg);
1379 SetRotateTabular(rotate_arg);
1380 setHeaderFooterRows(a+1, b+1 , c+1, d+1);
1381 for (i = 0; i < rows_; ++i) {
1382 a = b = c = d = e = f = g = 0;
1383 is >> a >> b >> c >> d;
1384 row_info[i].top_line = a;
1385 row_info[i].bottom_line = b;
1386 cont_row_info[i] = c;
1387 row_info[i].newpage = d;
1389 for (i = 0; i < columns_; ++i) {
1394 char ch; // skip '"'
1397 // ignore is buggy but we will use it later (Lgb)
1398 is.ignore(); // skip '"'
1400 getline(is, s1, '"');
1402 is >> ch; // skip '"'
1404 // ignore is buggy but we will use it later (Lgb)
1405 is.ignore(); // skip '"'
1407 getline(is, s2, '"');
1408 column_info[i].alignment = static_cast<LyXAlignment>(a);
1409 column_info[i].left_line = b;
1410 column_info[i].right_line = c;
1411 column_info[i].p_width = LyXLength(s1);
1412 column_info[i].align_special = s2;
1414 for (i = 0; i < rows_; ++i) {
1415 for (j = 0; j < columns_; ++j) {
1418 is >> a >> b >> c >> d >> e >> f >> g;
1421 is >> ch; // skip '"'
1423 // ignore is buggy but we will use it later (Lgb)
1424 is.ignore(); // skip '"'
1426 getline(is, s1, '"');
1428 is >> ch; // skip '"'
1430 // ignore is buggy but we will use it later (Lgb)
1431 is.ignore(); // skip '"'
1433 getline(is, s2, '"');
1434 cell_info[i][j].multicolumn = static_cast<char>(a);
1435 cell_info[i][j].alignment = static_cast<LyXAlignment>(b);
1436 cell_info[i][j].top_line = static_cast<char>(c);
1437 cell_info[i][j].bottom_line = static_cast<char>(d);
1438 cell_info[i][j].left_line = column_info[j].left_line;
1439 cell_info[i][j].right_line = column_info[j].right_line;
1440 cell_info[i][j].rotate = static_cast<bool>(f);
1441 cell_info[i][j].usebox = static_cast<BoxType>(g);
1442 cell_info[i][j].align_special = s1;
1443 cell_info[i][j].p_width = LyXLength(s2);
1447 set_row_column_number_info(true);
1449 Paragraph * par = new Paragraph;
1450 Paragraph * return_par = 0;
1452 par->layout(bp.getLyXTextClass().defaultLayout());
1456 Paragraph::depth_type depth = 0;
1457 LyXFont font(LyXFont::ALL_INHERIT);
1458 font.setLanguage(owner_->bufferOwner()->getLanguage());
1460 while (lex.isOK()) {
1462 string const token = lex.getString();
1465 if (token == "\\layout"
1466 || token == "\\end_float" // this should not exist anymore
1467 || token == "\\end_inset" // as it is substituted by this
1468 || token == "\\end_deeper")
1470 lex.pushToken(token);
1471 #ifndef NO_COMPABILITY
1472 // Here we need to insert the inset_ert_contents into the last
1473 // cell of the tabular.
1474 owner_->bufferOwner()->insertErtContents(par, pos);
1478 #ifndef NO_COMPABILITY
1479 if (token == "\\newline")
1480 // Here we need to insert the inset_ert_contents into the last
1481 // cell of the tabular.
1482 owner_->bufferOwner()->insertErtContents(par, pos, false);
1484 if (owner_->bufferOwner()->parseSingleLyXformat2Token(lex, par,
1489 lex.pushToken(token);
1493 lex.printError("New Paragraph allocated! This should not happen!");
1494 lex.pushToken(token);
1500 // now we have the par we should fill the insets with this!
1502 InsetText * inset = GetCellInset(cell);
1505 for (int i = 0; i < par->size(); ++i) {
1506 if (par->isNewline(i)) {
1508 if (cell > numberofcells) {
1509 lyxerr << "Some error in reading old table format occured!" <<
1510 endl << "Terminating when reading cell[" << cell << "]!" <<
1515 row = row_of_cell(cell);
1516 if (cont_row_info[row]) {
1518 cont_row_info.erase(cont_row_info.begin() + row); //&cont_row_info[row]);
1519 while (!IsFirstCellInRow(--cell));
1521 inset = GetCellInset(cell);
1524 inset = GetCellInset(cell);
1525 row = row_of_cell(cell);
1526 if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].usebox)
1528 // insert a space instead
1530 par->insertChar(i, ' ');
1533 par->copyIntoMinibuffer(*owner_->bufferOwner(), i);
1534 inset->paragraph()->insertFromMinibuffer(inset->paragraph()->size());
1541 bool LyXTabular::IsMultiColumn(int cell, bool real) const
1543 return ((!real || (column_of_cell(cell) != right_column_of_cell(cell))) &&
1544 (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL));
1548 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1550 int const row = row_of_cell(cell);
1551 int const column = column_of_cell(cell);
1552 return &cell_info[row][column];
1556 void LyXTabular::SetMultiColumn(Buffer const * buffer, int cell, int number)
1558 cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1559 cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1560 cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1561 cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1562 cellinfo_of_cell(cell)->right_line = column_info[column_of_cell(cell+number-1)].right_line;
1564 for (int i = 1; i < number; ++i) {
1565 cellinfo_of_cell(cell+i)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1566 cellinfo_of_cell(cell)->inset.appendParagraphs(buffer->params,
1567 cellinfo_of_cell(cell+i)->inset.paragraph());
1568 cellinfo_of_cell(cell+i)->inset.clear();
1571 for (number--; number > 0; --number) {
1572 cellinfo_of_cell(cell+number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1575 set_row_column_number_info();
1579 int LyXTabular::cells_in_multicolumn(int cell) const
1581 int const row = row_of_cell(cell);
1582 int column = column_of_cell(cell);
1585 while ((column < columns_) &&
1586 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1595 int LyXTabular::UnsetMultiColumn(int cell)
1597 int const row = row_of_cell(cell);
1598 int column = column_of_cell(cell);
1602 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1603 cell_info[row][column].multicolumn = CELL_NORMAL;
1605 while ((column < columns_) &&
1606 (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1608 cell_info[row][column].multicolumn = CELL_NORMAL;
1613 set_row_column_number_info();
1618 void LyXTabular::SetLongTabular(bool what)
1620 is_long_tabular = what;
1624 bool LyXTabular::IsLongTabular() const
1626 return is_long_tabular;
1630 void LyXTabular::SetRotateTabular(bool flag)
1636 bool LyXTabular::GetRotateTabular() const
1642 void LyXTabular::SetRotateCell(int cell, bool flag)
1644 cellinfo_of_cell(cell)->rotate = flag;
1648 bool LyXTabular::GetRotateCell(int cell) const
1650 return cellinfo_of_cell(cell)->rotate;
1654 bool LyXTabular::NeedRotating() const
1658 for (int i = 0; i < rows_; ++i) {
1659 for (int j = 0; j < columns_; ++j) {
1660 if (cell_info[i][j].rotate)
1668 bool LyXTabular::IsLastCell(int cell) const
1670 if ((cell + 1) < numberofcells)
1676 int LyXTabular::GetCellAbove(int cell) const
1678 if (row_of_cell(cell) > 0)
1679 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1684 int LyXTabular::GetCellBelow(int cell) const
1686 if (row_of_cell(cell) + 1 < rows_)
1687 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1692 int LyXTabular::GetLastCellAbove(int cell) const
1694 if (row_of_cell(cell) <= 0)
1696 if (!IsMultiColumn(cell))
1697 return GetCellAbove(cell);
1698 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1702 int LyXTabular::GetLastCellBelow(int cell) const
1704 if (row_of_cell(cell) + 1 >= rows_)
1706 if (!IsMultiColumn(cell))
1707 return GetCellBelow(cell);
1708 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1712 int LyXTabular::GetCellNumber(int row, int column) const
1715 if (column >= columns_)
1716 column = columns_ - 1;
1717 else if (column < 0)
1724 lyx::Assert(column >= 0 || column < columns_ || row >= 0 || row < rows_);
1726 return cell_info[row][column].cellno;
1730 void LyXTabular::SetUsebox(int cell, BoxType type)
1732 cellinfo_of_cell(cell)->usebox = type;
1736 LyXTabular::BoxType LyXTabular::GetUsebox(int cell) const
1738 if (column_info[column_of_cell(cell)].p_width.zero() &&
1739 !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.zero()))
1741 if (cellinfo_of_cell(cell)->usebox > 1)
1742 return cellinfo_of_cell(cell)->usebox;
1743 return UseParbox(cell);
1748 // This are functions used for the longtable support
1750 void LyXTabular::SetLTHead(int row, bool flag, ltType const & hd, bool first)
1755 row_info[row].endfirsthead = flag;
1759 row_info[row].endhead = flag;
1764 bool LyXTabular::GetRowOfLTHead(int row, ltType & hd) const
1767 hd.set = haveLTHead();
1768 return row_info[row].endhead;
1772 bool LyXTabular::GetRowOfLTFirstHead(int row, ltType & hd) const
1775 hd.set = haveLTFirstHead();
1776 return row_info[row].endfirsthead;
1780 void LyXTabular::SetLTFoot(int row, bool flag, ltType const & fd, bool last)
1785 row_info[row].endlastfoot = flag;
1789 row_info[row].endfoot = flag;
1794 bool LyXTabular::GetRowOfLTFoot(int row, ltType & fd) const
1797 fd.set = haveLTFoot();
1798 return row_info[row].endfoot;
1802 bool LyXTabular::GetRowOfLTLastFoot(int row, ltType & fd) const
1805 fd.set = haveLTLastFoot();
1806 return row_info[row].endlastfoot;
1810 void LyXTabular::SetLTNewPage(int row, bool what)
1812 row_info[row].newpage = what;
1816 bool LyXTabular::GetLTNewPage(int row) const
1818 return row_info[row].newpage;
1822 bool LyXTabular::haveLTHead() const
1824 for(int i=0; i < rows_; ++i) {
1825 if (row_info[i].endhead)
1832 bool LyXTabular::haveLTFirstHead() const
1834 if (endfirsthead.empty)
1836 for(int i=0; i < rows_; ++i) {
1837 if (row_info[i].endfirsthead)
1844 bool LyXTabular::haveLTFoot() const
1846 for(int i=0; i < rows_; ++i) {
1847 if (row_info[i].endfoot)
1854 bool LyXTabular::haveLTLastFoot() const
1856 if (endlastfoot.empty)
1858 for(int i=0; i < rows_; ++i) {
1859 if (row_info[i].endlastfoot)
1866 // end longtable support functions
1868 bool LyXTabular::SetAscentOfRow(int row, int height)
1870 if ((row >= rows_) || (row_info[row].ascent_of_row == height))
1872 row_info[row].ascent_of_row = height;
1877 bool LyXTabular::SetDescentOfRow(int row, int height)
1879 if ((row >= rows_) || (row_info[row].descent_of_row == height))
1881 row_info[row].descent_of_row = height;
1886 int LyXTabular::GetAscentOfRow(int row) const
1890 return row_info[row].ascent_of_row;
1894 int LyXTabular::GetDescentOfRow(int row) const
1898 return row_info[row].descent_of_row;
1902 int LyXTabular::GetHeightOfTabular() const
1906 for (int row = 0; row < rows_; ++row)
1907 height += GetAscentOfRow(row) + GetDescentOfRow(row) +
1908 GetAdditionalHeight(row);
1913 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
1915 if ((row >= rows_) || (column >= columns_))
1917 return (cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN);
1921 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1923 if ((row < 0) || (row >= rows_))
1926 int const fcell = GetFirstCellInRow(row);
1927 int const n = NumberOfCellsInRow(fcell) + fcell;
1930 for (int i = fcell; i < n; ++i) {
1934 if (tmp == (n - fcell)) {
1937 for (int i = fcell; i < n; ++i) {
1940 << column_of_cell(i) + 1
1942 << right_column_of_cell(i) + 1
1954 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1956 if ((row < 0) || (row >= rows_))
1959 int const fcell = GetFirstCellInRow(row);
1960 int const n = NumberOfCellsInRow(fcell) + fcell;
1963 for (int i = fcell; i < n; ++i) {
1967 if (tmp == (n - fcell)) {
1970 for (int i = fcell; i < n; ++i) {
1971 if (BottomLine(i)) {
1973 << column_of_cell(i) + 1
1975 << right_column_of_cell(i) + 1
1987 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1991 if (GetRotateCell(cell)) {
1992 os << "\\begin{sideways}\n";
1995 if (IsMultiColumn(cell)) {
1996 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1997 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1998 os << cellinfo_of_cell(cell)->align_special << "}{";
2000 if (LeftLine(cell) &&
2001 (IsFirstCellInRow(cell) ||
2002 (!IsMultiColumn(cell-1) && !LeftLine(cell, true) &&
2003 !RightLine(cell-1, true))))
2007 if (!GetPWidth(cell).zero()) {
2008 switch (GetVAlignment(cell)) {
2009 case LYX_VALIGN_TOP:
2012 case LYX_VALIGN_CENTER:
2015 case LYX_VALIGN_BOTTOM:
2019 os << "{" << GetPWidth(cell).asLatexString() << '}';
2021 switch (GetAlignment(cell)) {
2022 case LYX_ALIGN_LEFT:
2025 case LYX_ALIGN_RIGHT:
2033 if (RightLine(cell))
2035 if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
2041 if (GetUsebox(cell) == BOX_PARBOX) {
2043 switch (GetVAlignment(cell)) {
2044 case LYX_VALIGN_TOP:
2047 case LYX_VALIGN_CENTER:
2050 case LYX_VALIGN_BOTTOM:
2054 os << "]{" << GetPWidth(cell).asLatexString() << "}{";
2055 } else if (GetUsebox(cell) == BOX_MINIPAGE) {
2056 os << "\\begin{minipage}[";
2057 switch (GetVAlignment(cell)) {
2058 case LYX_VALIGN_TOP:
2061 case LYX_VALIGN_CENTER:
2064 case LYX_VALIGN_BOTTOM:
2068 os << "]{" << GetPWidth(cell).asLatexString() << "}\n";
2075 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
2080 if (GetUsebox(cell) == BOX_PARBOX)
2082 else if (GetUsebox(cell) == BOX_MINIPAGE) {
2083 os << "%\n\\end{minipage}";
2086 if (IsMultiColumn(cell)) {
2089 if (GetRotateCell(cell)) {
2090 os << "%\n\\end{sideways}";
2097 int LyXTabular::TeXLongtableHeaderFooter(ostream & os, Buffer const * buf,
2098 bool fragile, bool fp) const
2100 if (!is_long_tabular)
2104 // output header info
2106 if (endhead.topDL) {
2110 for (int i = 0; i < rows_; ++i) {
2111 if (row_info[i].endhead) {
2112 ret += TeXRow(os, i, buf, fragile, fp);
2115 if (endhead.bottomDL) {
2119 os << "\\endhead\n";
2121 if (endfirsthead.empty) {
2122 os << "\\endfirsthead\n";
2126 // output firstheader info
2127 if (haveLTFirstHead()) {
2128 if (endfirsthead.topDL) {
2132 for (int i = 0; i < rows_; ++i) {
2133 if (row_info[i].endfirsthead) {
2134 ret += TeXRow(os, i, buf, fragile, fp);
2137 if (endfirsthead.bottomDL) {
2141 os << "\\endfirsthead\n";
2144 // output footer info
2146 if (endfoot.topDL) {
2150 for (int i = 0; i < rows_; ++i) {
2151 if (row_info[i].endfoot) {
2152 ret += TeXRow(os, i, buf, fragile, fp);
2155 if (endfoot.bottomDL) {
2159 os << "\\endfoot\n";
2161 if (endlastfoot.empty) {
2162 os << "\\endlastfoot\n";
2166 // output lastfooter info
2167 if (haveLTLastFoot()) {
2168 if (endlastfoot.topDL) {
2172 for (int i = 0; i < rows_; ++i) {
2173 if (row_info[i].endlastfoot) {
2174 ret += TeXRow(os, i, buf, fragile, fp);
2177 if (endlastfoot.bottomDL) {
2181 os << "\\endlastfoot\n";
2188 bool LyXTabular::isValidRow(int const row) const
2190 if (!is_long_tabular)
2192 return (!row_info[row].endhead && !row_info[row].endfirsthead &&
2193 !row_info[row].endfoot && !row_info[row].endlastfoot);
2197 int LyXTabular::TeXRow(ostream & os, int const i, Buffer const * buf,
2198 bool fragile, bool fp) const
2201 int cell = GetCellNumber(i, 0);
2203 ret += TeXTopHLine(os, i);
2204 for (int j = 0; j < columns_; ++j) {
2205 if (IsPartOfMultiColumn(i,j))
2207 ret += TeXCellPreamble(os, cell);
2208 InsetText * inset = GetCellInset(cell);
2210 bool rtl = inset->paragraph()->isRightToLeftPar(buf->params) &&
2211 inset->paragraph()->size() > 0 && GetPWidth(cell).zero();
2215 ret += inset->latex(buf, os, fragile, fp);
2219 ret += TeXCellPostamble(os, cell);
2220 if (!IsLastCellInRow(cell)) { // not last cell in row
2228 ret += TeXBottomHLine(os, i);
2233 int LyXTabular::latex(Buffer const * buf,
2234 ostream & os, bool fragile, bool fp) const
2238 //+---------------------------------------------------------------------
2239 //+ first the opening preamble +
2240 //+---------------------------------------------------------------------
2243 os << "\\begin{sideways}\n";
2246 if (is_long_tabular)
2247 os << "\\begin{longtable}{";
2249 os << "\\begin{tabular}{";
2250 for (int i = 0; i < columns_; ++i) {
2251 if (!column_info[i].align_special.empty()) {
2252 os << column_info[i].align_special;
2254 if (column_info[i].left_line)
2256 if (!column_info[i].p_width.zero()) {
2257 switch (column_info[i].valignment) {
2258 case LYX_VALIGN_TOP:
2261 case LYX_VALIGN_CENTER:
2264 case LYX_VALIGN_BOTTOM:
2269 << column_info[i].p_width.asLatexString()
2272 switch (column_info[i].alignment) {
2273 case LYX_ALIGN_LEFT:
2276 case LYX_ALIGN_RIGHT:
2284 if (column_info[i].right_line)
2291 ret += TeXLongtableHeaderFooter(os, buf, fragile, fp);
2293 //+---------------------------------------------------------------------
2294 //+ the single row and columns (cells) +
2295 //+---------------------------------------------------------------------
2297 for (int i = 0; i < rows_; ++i) {
2298 if (isValidRow(i)) {
2299 ret += TeXRow(os, i, buf, fragile, fp);
2300 if (is_long_tabular && row_info[i].newpage) {
2301 os << "\\newpage\n";
2307 //+---------------------------------------------------------------------
2308 //+ the closing of the tabular +
2309 //+---------------------------------------------------------------------
2311 if (is_long_tabular)
2312 os << "\\end{longtable}";
2314 os << "\\end{tabular}";
2316 os << "\n\\end{sideways}";
2324 int LyXTabular::docbookRow(Buffer const * buf, ostream & os, int row) const
2327 int cell = GetFirstCellInRow(row);
2330 for (int j = 0; j < columns_; ++j) {
2331 if (IsPartOfMultiColumn(row, j))
2334 os << "<entry align=\"";
2335 switch (GetAlignment(cell)) {
2336 case LYX_ALIGN_LEFT:
2339 case LYX_ALIGN_RIGHT:
2347 os << "\" valign=\"";
2348 switch (GetVAlignment(cell)) {
2349 case LYX_VALIGN_TOP:
2352 case LYX_VALIGN_BOTTOM:
2355 case LYX_VALIGN_CENTER:
2360 if (IsMultiColumn(cell)) {
2361 os << " namest=\"col" << j << "\" ";
2362 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< "\"";
2366 ret += GetCellInset(cell)->docbook(buf, os, true);
2375 int LyXTabular::docbook(Buffer const * buf, ostream & os,
2376 bool /*mixcont*/) const
2380 //+---------------------------------------------------------------------
2381 //+ first the opening preamble +
2382 //+---------------------------------------------------------------------
2384 os << "<tgroup cols=\"" << columns_
2385 << "\" colsep=\"1\" rowsep=\"1\">\n";
2387 for (int i = 0; i < columns_; ++i) {
2388 os << "<colspec colname=\"col" << i << "\" align=\"";
2389 switch (column_info[i].alignment) {
2390 case LYX_ALIGN_LEFT:
2393 case LYX_ALIGN_RIGHT:
2404 //+---------------------------------------------------------------------
2405 //+ Long Tabular case +
2406 //+---------------------------------------------------------------------
2408 // output header info
2409 if (haveLTHead() || haveLTFirstHead()) {
2412 for (int i = 0; i < rows_; ++i) {
2413 if (row_info[i].endhead || row_info[i].endfirsthead) {
2414 ret += docbookRow(buf, os, i);
2420 // output footer info
2421 if (haveLTFoot() || haveLTLastFoot()) {
2424 for (int i = 0; i < rows_; ++i) {
2425 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2426 ret += docbookRow(buf, os, i);
2433 //+---------------------------------------------------------------------
2434 //+ the single row and columns (cells) +
2435 //+---------------------------------------------------------------------
2439 for (int i = 0; i < rows_; ++i) {
2440 if (isValidRow(i)) {
2441 ret += docbookRow(buf, os, i);
2446 //+---------------------------------------------------------------------
2447 //+ the closing of the tabular +
2448 //+---------------------------------------------------------------------
2457 // ASCII export function and helpers
2459 int LyXTabular::asciiTopHLine(ostream & os, int row,
2460 vector<unsigned int> const & clen) const
2462 int const fcell = GetFirstCellInRow(row);
2463 int const n = NumberOfCellsInRow(fcell) + fcell;
2466 for (int i = fcell; i < n; ++i) {
2476 for (int i = fcell; i < n; ++i) {
2487 int column = column_of_cell(i);
2488 int len = clen[column];
2489 while (IsPartOfMultiColumn(row, ++column))
2490 len += clen[column] + 4;
2491 os << string(len, ch);
2506 int LyXTabular::asciiBottomHLine(ostream & os, int row,
2507 vector<unsigned int> const & clen) const
2509 int const fcell = GetFirstCellInRow(row);
2510 int const n = NumberOfCellsInRow(fcell) + fcell;
2513 for (int i = fcell; i < n; ++i) {
2514 if (BottomLine(i)) {
2523 for (int i = fcell; i < n; ++i) {
2524 if (BottomLine(i)) {
2534 int column = column_of_cell(i);
2535 int len = clen[column];
2536 while (IsPartOfMultiColumn(row, ++column))
2537 len += clen[column] + 4;
2538 os << string(len, ch);
2539 if (BottomLine(i)) {
2553 int LyXTabular::asciiPrintCell(Buffer const * buf, ostream & os,
2554 int cell, int row, int column,
2555 vector<unsigned int> const & clen,
2556 bool onlydata) const
2559 int ret = GetCellInset(cell)->ascii(buf, sstr, 0);
2571 unsigned int len1 = sstr.str().length();
2572 unsigned int len2 = clen[column];
2573 while (IsPartOfMultiColumn(row, ++column))
2574 len2 += clen[column] + 4;
2577 switch (GetAlignment(cell)) {
2579 case LYX_ALIGN_LEFT:
2582 case LYX_ALIGN_RIGHT:
2586 case LYX_ALIGN_CENTER:
2592 for (unsigned int i = 0; i < len1; ++i)
2595 for (unsigned int i = 0; i < len2; ++i)
2597 if (RightLine(cell))
2606 int LyXTabular::ascii(Buffer const * buf, ostream & os, int const depth,
2607 bool onlydata, unsigned char delim) const
2611 //+---------------------------------------------------------------------
2612 //+ first calculate the width of the single columns +
2613 //+---------------------------------------------------------------------
2614 vector<unsigned int> clen(columns_);
2617 // first all non (real) multicolumn cells!
2618 for (int j = 0; j < columns_; ++j) {
2620 for (int i = 0; i < rows_; ++i) {
2621 int cell = GetCellNumber(i, j);
2622 if (IsMultiColumn(cell, true))
2625 GetCellInset(cell)->ascii(buf, sstr, 0);
2626 if (clen[j] < sstr.str().length())
2627 clen[j] = sstr.str().length();
2630 // then all (real) multicolumn cells!
2631 for (int j = 0; j < columns_; ++j) {
2632 for (int i = 0; i < rows_; ++i) {
2633 int cell = GetCellNumber(i, j);
2634 if (!IsMultiColumn(cell, true) || IsPartOfMultiColumn(i, j))
2637 GetCellInset(cell)->ascii(buf, sstr, 0);
2638 int len = int(sstr.str().length());
2639 int const n = cells_in_multicolumn(cell);
2640 for (int k = j; (len > 0) && (k < (j + n - 1)); ++k)
2642 if (len > int(clen[j + n - 1]))
2643 clen[j + n - 1] = len;
2648 for (int i = 0; i < rows_; ++i) {
2650 if (asciiTopHLine(os, i, clen)) {
2651 for (int j = 0; j < depth; ++j)
2655 for (int j = 0; j < columns_; ++j) {
2656 if (IsPartOfMultiColumn(i,j))
2658 if (onlydata && j > 0)
2660 ret += asciiPrintCell(buf, os, cell, i, j, clen, onlydata);
2665 for (int j = 0; j < depth; ++j)
2667 if (asciiBottomHLine(os, i, clen)) {
2668 for (int j = 0; j < depth; ++j)
2680 InsetText * LyXTabular::GetCellInset(int cell) const
2683 return & cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2687 InsetText * LyXTabular::GetCellInset(int row, int column) const
2689 cur_cell = GetCellNumber(row, column);
2690 return & cell_info[row][column].inset;
2694 int LyXTabular::GetCellFromInset(Inset const * inset, int maybe_cell) const
2696 // is this inset part of the tabular?
2697 if (!inset || inset->owner() != owner_) {
2698 lyxerr[Debug::INSETTEXT]
2699 << "this is not a cell of the tabular!" << endl;
2703 const int save_cur_cell = cur_cell;
2704 int cell = cur_cell;
2705 if (GetCellInset(cell) != inset) {
2707 if (cell == -1 || GetCellInset(cell) != inset) {
2713 for (cell = GetNumberOfCells(); cell >= 0; --cell) {
2714 if (GetCellInset(cell) == inset)
2717 lyxerr[Debug::INSETTEXT]
2718 << "LyXTabular::GetCellFromInset: "
2720 << ", cur_cell=" << save_cur_cell
2721 << ", maybe_cell=" << maybe_cell
2723 // We should have found a cell at this point
2725 lyxerr << "LyXTabular::GetCellFromInset: "
2726 << "Cell not found!" << endl;
2734 void LyXTabular::Validate(LaTeXFeatures & features) const
2736 if (IsLongTabular())
2737 features.require("longtable");
2739 features.require("rotating");
2740 for (int cell = 0; cell < numberofcells; ++cell) {
2741 if (GetVAlignment(cell) != LYX_VALIGN_TOP)
2742 features.require("array");
2743 GetCellInset(cell)->validate(features);
2748 vector<string> const LyXTabular::getLabelList() const
2750 vector<string> label_list;
2751 for (int i = 0; i < rows_; ++i)
2752 for (int j = 0; j < columns_; ++j) {
2753 vector<string> const l =
2754 GetCellInset(i, j)->getLabelList();
2755 label_list.insert(label_list.end(),
2756 l.begin(), l.end());
2762 LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
2764 Paragraph * par = GetCellInset(cell)->paragraph();
2766 for (; par; par = par->next()) {
2767 for (int i = 0; i < par->size(); ++i) {
2768 if (par->getChar(i) == Paragraph::META_NEWLINE)