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"
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"
49 #ifndef CXX_GLOBAL_CSTD
55 int const WIDTH_OF_LINE = 5;
59 /// Define a few methods for the inner structs
61 LyXTabular::cellstruct::cellstruct(BufferParams const & bg)
66 multicolumn = LyXTabular::CELL_NORMAL;
67 alignment = LYX_ALIGN_CENTER;
68 valignment = LYX_VALIGN_TOP;
78 LyXTabular::rowstruct::rowstruct()
92 LyXTabular::columnstruct::columnstruct()
96 alignment = LYX_ALIGN_CENTER;
97 valignment = LYX_VALIGN_TOP;
102 LyXTabular::lttype::lttype()
111 LyXTabular::LyXTabular(BufferParams const & bp,
112 InsetTabular * inset, int rows_arg, int columns_arg)
116 Init(bp, rows_arg, columns_arg);
120 LyXTabular::LyXTabular(BufferParams const & bp,
121 InsetTabular * inset, LyXTabular const & lt,
126 Init(bp, lt.rows_, lt.columns_, <);
127 // we really should change again to have InsetText as a pointer
128 // and allocate it then we would not have to do this stuff all
131 for (int i = 0; i < rows_; ++i) {
132 for (int j = 0; j < columns_; ++j) {
133 cell_info[i][j].inset.id(lt.cell_info[i][j].inset.id());
134 cell_info[i][j].inset.setParagraphData(lt.cell_info[i][j].inset.paragraph(),true);
140 #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)
147 LyXTabular::LyXTabular(Buffer const * buf, InsetTabular * inset, LyXLex & lex)
155 LyXTabular & LyXTabular::operator=(LyXTabular const & lt)
158 #warning This while method should look like this: (Lgb)
163 // If this and lt is not of the same size we have a serious bug
164 // So then it is ok to throw an exception, or for now
166 lyx::Assert(rows_ == lt.rows_ && columns_ == lt.columns_);
168 cell_info = lt.cell_info;
169 row_info = lt.row_info;
170 column_info = lt.column_info;
171 SetLongTabular(lt.is_long_tabular);
180 LyXTabular * LyXTabular::clone(BufferParams const & bp,
181 InsetTabular * inset, bool same_id)
183 LyXTabular * result = new LyXTabular(bp, inset, *this, same_id);
185 // don't know if this is good but I need to Clone also
186 // the text-insets here, this is for the Undo-facility!
187 for (int i = 0; i < rows_; ++i) {
188 for (int j = 0; j < columns_; ++j) {
189 result->cell_info[i][j].inset = cell_info[i][j].inset;
190 result->cell_info[i][j].inset.setOwner(inset);
198 /* activates all lines and sets all widths to 0 */
199 void LyXTabular::Init(BufferParams const & bp,
200 int rows_arg, int columns_arg, LyXTabular const * lt)
203 columns_ = columns_arg;
204 row_info = row_vector(rows_, rowstruct());
205 column_info = column_vector(columns_, columnstruct());
206 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
214 for (int i = 0; i < rows_; ++i) {
215 for (int j = 0; j < columns_; ++j) {
216 cell_info[i][j].inset.setOwner(owner_);
217 cell_info[i][j].inset.setDrawFrame(0, InsetText::LOCKED);
218 cell_info[i][j].cellno = cellno++;
220 cell_info[i].back().right_line = true;
222 row_info.back().bottom_line = true;
223 row_info.front().bottom_line = true;
225 for (int i = 0; i < columns_; ++i) {
226 calculate_width_of_column(i);
228 column_info.back().right_line = true;
230 calculate_width_of_tabular();
232 rowofcell = vector<int>();
233 columnofcell = vector<int>();
234 set_row_column_number_info();
235 is_long_tabular = false;
240 void LyXTabular::AppendRow(BufferParams const & bp, int cell)
244 int row = row_of_cell(cell);
246 row_vector::iterator rit = row_info.begin() + row;
247 row_info.insert(rit, rowstruct());
248 // now set the values of the row before
249 row_info[row] = row_info[row + 1];
252 cell_vvector::iterator cit = cell_info.begin() + row;
253 cell_info.insert(cit, vector<cellstruct>(columns_, cellstruct(bp)));
255 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
258 for (int i = 0; i <= row; ++i) {
259 for (int j = 0; j < columns_; ++j) {
260 c_info[i][j] = cell_info[i][j];
263 for (int i = row + 1; i < rows_; ++i) {
264 for (int j = 0; j < columns_; ++j) {
265 c_info[i][j] = cell_info[i-1][j];
270 for (int j = 0; j < columns_; ++j) {
271 cell_info[row][j].inset.clear();
278 void LyXTabular::DeleteRow(int row)
280 if (rows_ == 1) return; // Not allowed to delete last row
282 row_info.erase(row_info.begin() + row); //&row_info[row]);
283 cell_info.erase(cell_info.begin() + row); //&cell_info[row]);
289 void LyXTabular::AppendColumn(BufferParams const & bp, int cell)
293 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
295 int const column = column_of_cell(cell);
296 column_vector::iterator cit = column_info.begin() + column + 1;
297 column_info.insert(cit, columnstruct());
298 // set the column values of the column before
299 column_info[column + 1] = column_info[column];
301 for (int i = 0; i < rows_; ++i) {
302 for (int j = 0; j <= column; ++j) {
303 c_info[i][j] = cell_info[i][j];
305 for (int j = column + 1; j < columns_; ++j) {
306 c_info[i][j] = cell_info[i][j - 1];
308 // care about multicolumns
309 if (c_info[i][column + 1].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
311 c_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
313 if ((column + 2) >= columns_ ||
314 c_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
316 c_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
321 for (int i = 0; i < rows_; ++i) {
322 //cell_info[i][column].inset.clear();
323 cell_info[i][column + 1].inset.clear();
329 void LyXTabular::DeleteColumn(int column)
331 // Similar to DeleteRow
332 //if (!(columns_ - 1))
334 if (columns_ == 1) return; // Not allowed to delete last column
336 column_info.erase(column_info.begin() + column);
337 for (int i = 0; i < rows_; ++i) {
338 cell_info[i].erase(cell_info[i].begin() + column);
345 void LyXTabular::reinit()
351 void LyXTabular::Reinit(bool reset_widths)
354 for (int i = 0; i < rows_; ++i) {
355 for (int j = 0; j < columns_; ++j) {
356 cell_info[i][j].width_of_cell = 0;
357 cell_info[i][j].inset.setOwner(owner_);
362 for (int i = 0; i < columns_; ++i) {
363 calculate_width_of_column(i);
365 calculate_width_of_tabular();
367 set_row_column_number_info();
371 void LyXTabular::set_row_column_number_info(bool oldformat)
374 for (int row = 0; row < rows_; ++row) {
375 for (int column = 0; column<columns_; ++column) {
376 if (cell_info[row][column].multicolumn
377 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
379 cell_info[row][column].cellno = numberofcells;
382 ++numberofcells; // because this is one more than as we start from 0
384 rowofcell.resize(numberofcells);
385 columnofcell.resize(numberofcells);
387 for (int row = 0, column = 0, c = 0;
388 c < numberofcells && row < rows_ && column < columns_;) {
390 columnofcell[c] = column;
394 } while (column < columns_ &&
395 cell_info[row][column].multicolumn
396 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
397 if (column == columns_) {
403 for (int row = 0; row < rows_; ++row) {
404 for (int column = 0; column < columns_; ++column) {
405 if (IsPartOfMultiColumn(row,column))
407 // now set the right line of multicolumns right for oldformat read
409 cell_info[row][column].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
411 int cn=cells_in_multicolumn(cell_info[row][column].cellno);
412 cell_info[row][column].right_line =
413 cell_info[row][column+cn-1].right_line;
415 cell_info[row][column].inset.setAutoBreakRows(
416 !GetPWidth(GetCellNumber(row, column)).zero());
422 int LyXTabular::GetNumberOfCells() const
424 return numberofcells;
428 int LyXTabular::NumberOfCellsInRow(int cell) const
430 int const row = row_of_cell(cell);
432 for (int i = 0; i < columns_; ++i) {
433 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
440 /* returns 1 if there is a topline, returns 0 if not */
441 bool LyXTabular::TopLine(int cell, bool onlycolumn) const
443 int const row = row_of_cell(cell);
445 if (!onlycolumn && IsMultiColumn(cell))
446 return cellinfo_of_cell(cell)->top_line;
447 return row_info[row].top_line;
451 bool LyXTabular::BottomLine(int cell, bool onlycolumn) const
453 // no bottom line underneath non-existent cells if you please
454 // Isn't that a programming error? Is so this should
455 // be an Assert instead. (Lgb)
456 if (cell >= numberofcells)
459 if (!onlycolumn && IsMultiColumn(cell))
460 return cellinfo_of_cell(cell)->bottom_line;
461 return row_info[row_of_cell(cell)].bottom_line;
465 bool LyXTabular::LeftLine(int cell, bool onlycolumn) const
467 if (!onlycolumn && IsMultiColumn(cell)) {
468 #ifdef SPECIAL_COLUM_HANDLING
469 if (cellinfo_of_cell(cell)->align_special.empty())
470 return cellinfo_of_cell(cell)->left_line;
471 return prefixIs(frontStrip(cellinfo_of_cell(cell)->align_special), "|");
473 return cellinfo_of_cell(cell)->left_line;
476 #ifdef SPECIAL_COLUM_HANDLING
477 if (column_info[column_of_cell(cell)].align_special.empty())
478 return column_info[column_of_cell(cell)].left_line;
479 return prefixIs(frontStrip(column_info[column_of_cell(cell)].align_special), "|");
481 return column_info[column_of_cell(cell)].left_line;
486 bool LyXTabular::RightLine(int cell, bool onlycolumn) const
488 if (!onlycolumn && IsMultiColumn(cell)) {
489 #ifdef SPECIAL_COLUM_HANDLING
490 if (cellinfo_of_cell(cell)->align_special.empty())
491 return cellinfo_of_cell(cell)->right_line;
492 return suffixIs(strip(cellinfo_of_cell(cell)->align_special), "|");
494 return cellinfo_of_cell(cell)->right_line;
497 #ifdef SPECIAL_COLUM_HANDLING
498 if (column_info[column_of_cell(cell)].align_special.empty())
499 return column_info[right_column_of_cell(cell)].right_line;
500 return suffixIs(strip(column_info[column_of_cell(cell)].align_special), "|");
502 return column_info[right_column_of_cell(cell)].right_line;
507 bool LyXTabular::TopAlreadyDrawed(int cell) const
509 int row = row_of_cell(cell);
510 if ((row > 0) && !GetAdditionalHeight(row)) {
511 int column = column_of_cell(cell);
514 && cell_info[row][column].multicolumn
515 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
517 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
518 return row_info[row].bottom_line;
520 return cell_info[row][column].bottom_line;
526 bool LyXTabular::LeftAlreadyDrawed(int cell) const
528 int column = column_of_cell(cell);
530 int row = row_of_cell(cell);
532 (cell_info[row][column].multicolumn ==
533 LyXTabular::CELL_PART_OF_MULTICOLUMN));
534 if (GetAdditionalWidth(cell_info[row][column].cellno))
536 #ifdef SPECIAL_COLUM_HANDLING
537 return column_info[column].right_line;
539 return RightLine(cell_info[row][column].cellno, true);
546 bool LyXTabular::IsLastRow(int cell) const
548 return (row_of_cell(cell) == rows_ - 1);
552 int LyXTabular::GetAdditionalHeight(int row) const
554 if (!row || row >= rows_)
560 for (int column = 0; column < columns_ && bottom; ++column) {
561 switch (cell_info[row - 1][column].multicolumn) {
562 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
563 bottom = cell_info[row - 1][column].bottom_line;
565 case LyXTabular::CELL_NORMAL:
566 bottom = row_info[row - 1].bottom_line;
569 for (int column = 0; column < columns_ && top; ++column) {
570 switch (cell_info[row][column].multicolumn) {
571 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
572 top = cell_info[row][column].top_line;
574 case LyXTabular::CELL_NORMAL:
575 top = row_info[row].top_line;
579 return WIDTH_OF_LINE;
584 int LyXTabular::GetAdditionalWidth(int cell) const
586 // internally already set in SetWidthOfCell
587 // used to get it back in text.C
588 int const col = right_column_of_cell(cell);
589 int const row = row_of_cell(cell);
590 if (col < columns_ - 1 && RightLine(cell, true) &&
591 LeftLine(cell_info[row][col+1].cellno, true)) // column_info[col+1].left_line)
593 return WIDTH_OF_LINE;
600 // returns the maximum over all rows
601 int LyXTabular::GetWidthOfColumn(int cell) const
603 int const column1 = column_of_cell(cell);
604 int const column2 = right_column_of_cell(cell);
606 for (int i = column1; i <= column2; ++i) {
607 result += column_info[i].width_of_column;
613 int LyXTabular::GetWidthOfTabular() const
615 return width_of_tabular;
619 /* returns 1 if a complete update is necessary, otherwise 0 */
620 bool LyXTabular::SetWidthOfMulticolCell(int cell, int new_width)
622 if (!IsMultiColumn(cell))
625 int const row = row_of_cell(cell);
626 int const column1 = column_of_cell(cell);
627 int const column2 = right_column_of_cell(cell);
628 int const old_val = cell_info[row][column2].width_of_cell;
630 // first set columns to 0 so we can calculate the right width
631 for (int i = column1; i <= column2; ++i) {
632 cell_info[row][i].width_of_cell = 0;
634 // set the width to MAX_WIDTH until width > 0
635 int width = (new_width + 2 * WIDTH_OF_LINE);
637 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
638 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
639 width -= column_info[i].width_of_column;
642 cell_info[row][i].width_of_cell = width;
644 if (old_val != cell_info[row][column2].width_of_cell) {
645 // in this case we have to recalculate all multicolumn cells which
646 // have this column as one of theirs but not as last one
647 recalculateMulticolumnsOfColumn(i);
653 void LyXTabular::recalculateMulticolumnsOfColumn(int column)
655 // the last column does not have to be recalculated because all
656 // multicolumns will have here there last multicolumn cell which
657 // always will have the whole rest of the width of the cell.
658 if (column > (columns_ - 2))
660 for(int row = 0; row < rows_; ++row) {
661 int mc = cell_info[row][column].multicolumn;
662 int nmc = cell_info[row][column+1].multicolumn;
663 // we only have to update multicolumns which do not have this
664 // column as their last column!
665 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
666 ((mc == CELL_PART_OF_MULTICOLUMN) &&
667 (nmc == CELL_PART_OF_MULTICOLUMN)))
669 int const cellno = cell_info[row][column].cellno;
670 SetWidthOfMulticolCell(cellno,
671 GetWidthOfCell(cellno)-(2 * WIDTH_OF_LINE));
677 /* returns 1 if a complete update is necessary, otherwise 0 */
678 bool LyXTabular::SetWidthOfCell(int cell, int new_width)
680 int const row = row_of_cell(cell);
681 int const column1 = column_of_cell(cell);
686 #ifdef SPECIAL_COLUM_HANDLING
687 if (RightLine(cell_info[row][column1].cellno, true) &&
688 (column1 < columns_-1) &&
689 LeftLine(cell_info[row][column1+1].cellno, true))
691 if (column_info[column1].right_line && (column1 < columns_-1) &&
692 column_info[column1+1].left_line) // additional width
696 add_width = WIDTH_OF_LINE;
698 if (GetWidthOfCell(cell) == (new_width+2*WIDTH_OF_LINE+add_width)) {
701 if (IsMultiColumn(cell, true)) {
702 tmp = SetWidthOfMulticolCell(cell, new_width);
704 width = (new_width + 2*WIDTH_OF_LINE + add_width);
705 cell_info[row][column1].width_of_cell = width;
706 tmp = calculate_width_of_column_NMC(column1);
708 recalculateMulticolumnsOfColumn(column1);
711 for (int i = 0; i < columns_; ++i)
712 calculate_width_of_column(i);
713 calculate_width_of_tabular();
720 bool LyXTabular::SetAlignment(int cell, LyXAlignment align, bool onlycolumn)
722 if (!IsMultiColumn(cell) || onlycolumn)
723 column_info[column_of_cell(cell)].alignment = align;
725 cellinfo_of_cell(cell)->alignment = align;
730 bool LyXTabular::SetVAlignment(int cell, VAlignment align, bool onlycolumn)
732 if (!IsMultiColumn(cell) || onlycolumn)
733 column_info[column_of_cell(cell)].valignment = align;
735 cellinfo_of_cell(cell)->valignment = align;
740 bool LyXTabular::SetColumnPWidth(int cell, LyXLength const & width)
742 bool flag = !width.zero();
743 int const j = column_of_cell(cell);
745 column_info[j].p_width = width;
746 if (flag) // do this only if there is a width
747 SetAlignment(cell, LYX_ALIGN_LEFT);
748 for (int i = 0; i < rows_; ++i) {
749 int c = GetCellNumber(i, j);
750 flag = !GetPWidth(c).zero(); // because of multicolumns!
751 GetCellInset(c)->setAutoBreakRows(flag);
757 bool LyXTabular::SetMColumnPWidth(int cell, LyXLength const & width)
759 bool const flag = !width.zero();
761 cellinfo_of_cell(cell)->p_width = width;
762 if (IsMultiColumn(cell)) {
763 GetCellInset(cell)->setAutoBreakRows(flag);
770 bool LyXTabular::SetAlignSpecial(int cell, string const & special,
771 LyXTabular::Feature what)
773 if (what == SET_SPECIAL_MULTI)
774 cellinfo_of_cell(cell)->align_special = special;
776 column_info[column_of_cell(cell)].align_special = special;
781 bool LyXTabular::SetAllLines(int cell, bool line)
783 SetTopLine(cell, line);
784 SetBottomLine(cell, line);
785 SetRightLine(cell, line);
786 SetLeftLine(cell, line);
791 bool LyXTabular::SetTopLine(int cell, bool line, bool onlycolumn)
793 int const row = row_of_cell(cell);
795 if (onlycolumn || !IsMultiColumn(cell))
796 row_info[row].top_line = line;
798 cellinfo_of_cell(cell)->top_line = line;
803 bool LyXTabular::SetBottomLine(int cell, bool line, bool onlycolumn)
805 if (onlycolumn || !IsMultiColumn(cell))
806 row_info[row_of_cell(cell)].bottom_line = line;
808 cellinfo_of_cell(cell)->bottom_line = line;
813 bool LyXTabular::SetLeftLine(int cell, bool line, bool onlycolumn)
815 if (onlycolumn || !IsMultiColumn(cell))
816 column_info[column_of_cell(cell)].left_line = line;
818 cellinfo_of_cell(cell)->left_line = line;
823 bool LyXTabular::SetRightLine(int cell, bool line, bool onlycolumn)
825 if (onlycolumn || !IsMultiColumn(cell))
826 column_info[right_column_of_cell(cell)].right_line = line;
828 cellinfo_of_cell(cell)->right_line = line;
833 LyXAlignment LyXTabular::GetAlignment(int cell, bool onlycolumn) const
835 if (!onlycolumn && IsMultiColumn(cell))
836 return cellinfo_of_cell(cell)->alignment;
838 return column_info[column_of_cell(cell)].alignment;
842 LyXTabular::VAlignment
843 LyXTabular::GetVAlignment(int cell, bool onlycolumn) const
845 if (!onlycolumn && IsMultiColumn(cell))
846 return cellinfo_of_cell(cell)->valignment;
848 return column_info[column_of_cell(cell)].valignment;
852 LyXLength const LyXTabular::GetPWidth(int cell) const
854 if (IsMultiColumn(cell))
855 return cellinfo_of_cell(cell)->p_width;
856 return column_info[column_of_cell(cell)].p_width;
860 LyXLength const LyXTabular::GetColumnPWidth(int cell) const
862 return column_info[column_of_cell(cell)].p_width;
866 LyXLength const LyXTabular::GetMColumnPWidth(int cell) const
868 if (IsMultiColumn(cell))
869 return cellinfo_of_cell(cell)->p_width;
874 string const LyXTabular::GetAlignSpecial(int cell, int what) const
876 if (what == SET_SPECIAL_MULTI)
877 return cellinfo_of_cell(cell)->align_special;
878 return column_info[column_of_cell(cell)].align_special;
882 int LyXTabular::GetWidthOfCell(int cell) const
884 int const row = row_of_cell(cell);
885 int const column1 = column_of_cell(cell);
886 int const column2 = right_column_of_cell(cell);
888 for (int i = column1; i <= column2; ++i) {
889 result += cell_info[row][i].width_of_cell;
895 int LyXTabular::GetBeginningOfTextInCell(int cell) const
899 switch (GetAlignment(cell)) {
900 case LYX_ALIGN_CENTER:
901 x += (GetWidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
903 case LYX_ALIGN_RIGHT:
904 x += GetWidthOfColumn(cell) - GetWidthOfCell(cell);
905 // + GetAdditionalWidth(cell);
907 default: /* LYX_ALIGN_LEFT: nothing :-) */
917 bool LyXTabular::IsFirstCellInRow(int cell) const
919 return column_of_cell(cell) == 0;
923 int LyXTabular::GetFirstCellInRow(int row) const
927 return cell_info[row][0].cellno;
930 bool LyXTabular::IsLastCellInRow(int cell) const
932 return (right_column_of_cell(cell) == (columns_ - 1));
936 int LyXTabular::GetLastCellInRow(int row) const
940 return cell_info[row][columns_-1].cellno;
944 bool LyXTabular::calculate_width_of_column(int column)
946 int const old_column_width = column_info[column].width_of_column;
949 for (int i = 0; i < rows_; ++i) {
950 maximum = max(cell_info[i][column].width_of_cell, maximum);
952 column_info[column].width_of_column = maximum;
953 return (column_info[column].width_of_column != old_column_width);
958 // calculate the with of the column without regarding REAL MultiColumn
959 // cells. This means MultiColumn-cells spanning more than 1 column.
961 bool LyXTabular::calculate_width_of_column_NMC(int column)
963 int const old_column_width = column_info[column].width_of_column;
965 for (int i = 0; i < rows_; ++i) {
966 if (!IsMultiColumn(GetCellNumber(i, column), true) &&
967 (cell_info[i][column].width_of_cell > max)) {
968 max = cell_info[i][column].width_of_cell;
971 column_info[column].width_of_column = max;
972 return (column_info[column].width_of_column != old_column_width);
976 void LyXTabular::calculate_width_of_tabular()
978 width_of_tabular = 0;
979 for (int i = 0; i < columns_; ++i) {
980 width_of_tabular += column_info[i].width_of_column;
985 int LyXTabular::row_of_cell(int cell) const
987 if (cell >= numberofcells)
991 return rowofcell[cell];
995 int LyXTabular::column_of_cell(int cell) const
997 if (cell >= numberofcells)
1001 return columnofcell[cell];
1005 int LyXTabular::right_column_of_cell(int cell) const
1007 int const row = row_of_cell(cell);
1008 int column = column_of_cell(cell);
1009 while (column < (columns_ - 1) &&
1010 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
1016 void LyXTabular::Write(Buffer const * buf, ostream & os) const
1020 << write_attribute("version", 3)
1021 << write_attribute("rows", rows_)
1022 << write_attribute("columns", columns_)
1024 // global longtable options
1026 << write_attribute("rotate", rotate)
1027 << write_attribute("islongtable", is_long_tabular)
1028 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1029 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1030 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1031 << write_attribute("headTopDL", endhead.topDL)
1032 << write_attribute("headBottomDL", endhead.bottomDL)
1033 << write_attribute("footTopDL", endfoot.topDL)
1034 << write_attribute("footBottomDL", endfoot.bottomDL)
1035 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1036 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1037 << write_attribute("lastFootEmpty", endlastfoot.empty)
1039 for (int j = 0; j < columns_; ++j) {
1041 << write_attribute("alignment", column_info[j].alignment)
1042 << write_attribute("valignment", column_info[j].valignment)
1043 << write_attribute("leftline", column_info[j].left_line)
1044 << write_attribute("rightline", column_info[j].right_line)
1045 << write_attribute("width", column_info[j].p_width.asString())
1046 << write_attribute("special", column_info[j].align_special)
1049 for (int i = 0; i < rows_; ++i) {
1051 << write_attribute("topline", row_info[i].top_line)
1052 << write_attribute("bottomline", row_info[i].bottom_line)
1053 << write_attribute("endhead", row_info[i].endhead)
1054 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1055 << write_attribute("endfoot", row_info[i].endfoot)
1056 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1057 << write_attribute("newpage", row_info[i].newpage)
1059 for (int j = 0; j < columns_; ++j) {
1061 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1062 << write_attribute("alignment", cell_info[i][j].alignment)
1063 << write_attribute("valignment", cell_info[i][j].valignment)
1064 << write_attribute("topline", cell_info[i][j].top_line)
1065 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1066 << write_attribute("leftline", cell_info[i][j].left_line)
1067 << write_attribute("rightline", cell_info[i][j].right_line)
1068 << write_attribute("rotate", cell_info[i][j].rotate)
1069 << write_attribute("usebox", cell_info[i][j].usebox)
1070 << write_attribute("width", cell_info[i][j].p_width)
1071 << write_attribute("special", cell_info[i][j].align_special)
1073 os << "\\begin_inset ";
1074 cell_info[i][j].inset.write(buf, os);
1075 os << "\n\\end_inset \n"
1080 os << "</lyxtabular>\n";
1084 void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
1087 istream & is = lex.getStream();
1089 l_getline(is, line);
1090 if (!prefixIs(line, "<lyxtabular ")
1091 && !prefixIs(line, "<LyXTabular ")) {
1092 OldFormatRead(buf->params, lex, line);
1097 if (!getTokenValue(line, "version", version))
1100 ReadOld(buf, is, lex, line);
1101 else if (version >= 2)
1102 ReadNew(buf, is, lex, line, version);
1105 void LyXTabular::setHeaderFooterRows(int hr, int fhr, int fr, int lfr)
1109 row_info[--hr].endhead = true;
1111 // set firstheader info
1112 if (fhr && (fhr < rows_)) {
1113 if (row_info[fhr].endhead) {
1115 row_info[--fhr].endfirsthead = true;
1116 row_info[fhr].endhead = false;
1118 } else if (row_info[fhr - 1].endhead) {
1119 endfirsthead.empty = true;
1121 while((fhr > 0) && !row_info[--fhr].endhead) {
1122 row_info[fhr].endfirsthead = true;
1127 if (fr && (fr < rows_)) {
1128 if (row_info[fr].endhead && row_info[fr-1].endhead) {
1129 while((fr > 0) && !row_info[--fr].endhead) {
1130 row_info[fr].endfoot = true;
1131 row_info[fr].endhead = false;
1133 } else if (row_info[fr].endfirsthead && row_info[fr-1].endfirsthead) {
1134 while((fr > 0) && !row_info[--fr].endfirsthead) {
1135 row_info[fr].endfoot = true;
1136 row_info[fr].endfirsthead = false;
1138 } else if (!row_info[fr - 1].endhead && !row_info[fr - 1].endfirsthead) {
1139 while((fr > 0) && !row_info[--fr].endhead &&
1140 !row_info[fr].endfirsthead)
1142 row_info[fr].endfoot = true;
1146 // set lastfooter info
1147 if (lfr && (lfr < rows_)) {
1148 if (row_info[lfr].endhead && row_info[lfr - 1].endhead) {
1149 while((lfr > 0) && !row_info[--lfr].endhead) {
1150 row_info[lfr].endlastfoot = true;
1151 row_info[lfr].endhead = false;
1153 } else if (row_info[lfr].endfirsthead &&
1154 row_info[lfr - 1].endfirsthead)
1156 while((lfr > 0) && !row_info[--lfr].endfirsthead) {
1157 row_info[lfr].endlastfoot = true;
1158 row_info[lfr].endfirsthead = false;
1160 } else if (row_info[lfr].endfoot
1161 && row_info[lfr - 1].endfoot) {
1162 while((lfr > 0) && !row_info[--lfr].endfoot) {
1163 row_info[lfr].endlastfoot = true;
1164 row_info[lfr].endfoot = false;
1166 } else if (!row_info[fr - 1].endhead
1167 && !row_info[fr - 1].endfirsthead &&
1168 !row_info[fr - 1].endfoot)
1171 !row_info[--lfr].endhead && !row_info[lfr].endfirsthead &&
1172 !row_info[lfr].endfoot)
1174 row_info[lfr].endlastfoot = true;
1176 } else if (haveLTFoot()) {
1177 endlastfoot.empty = true;
1182 void LyXTabular::ReadNew(Buffer const * buf, istream & is,
1183 LyXLex & lex, string const & l, int const version)
1187 if (!getTokenValue(line, "rows", rows_arg))
1190 if (!getTokenValue(line, "columns", columns_arg))
1192 Init(buf->params, rows_arg, columns_arg);
1193 l_getline(is, line);
1194 if (!prefixIs(line, "<features")) {
1195 lyxerr << "Wrong tabular format (expected <features ...> got" <<
1196 line << ")" << endl;
1199 getTokenValue(line, "rotate", rotate);
1200 getTokenValue(line, "islongtable", is_long_tabular);
1201 // compatibility read for old longtable options. Now we can make any
1202 // row part of the header/footer type we want before it was strict
1203 // sequential from the first row down (as LaTeX does it!). So now when
1204 // we find a header/footer line we have to go up the rows and set it
1205 // on all preceding rows till the first or one with already a h/f option
1206 // set. If we find a firstheader on the same line as a header or a
1207 // lastfooter on the same line as a footer then this should be set empty.
1215 getTokenValue(line, "endhead", hrow);
1216 getTokenValue(line, "endfirsthead", fhrow);
1217 getTokenValue(line, "endfoot", frow);
1218 getTokenValue(line, "endlastfoot", lfrow);
1219 setHeaderFooterRows(abs(hrow), abs(fhrow), abs(frow), abs(lfrow));
1221 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1222 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1223 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1224 getTokenValue(line, "headTopDL", endhead.topDL);
1225 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1226 getTokenValue(line, "footTopDL", endfoot.topDL);
1227 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1228 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1229 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1230 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1232 for (int j = 0; j < columns_; ++j) {
1234 if (!prefixIs(line,"<column")) {
1235 lyxerr << "Wrong tabular format (expected <column ...> got" <<
1236 line << ")" << endl;
1239 getTokenValue(line, "alignment", column_info[j].alignment);
1240 getTokenValue(line, "valignment", column_info[j].valignment);
1241 getTokenValue(line, "leftline", column_info[j].left_line);
1242 getTokenValue(line, "rightline", column_info[j].right_line);
1243 getTokenValue(line, "width", column_info[j].p_width);
1244 getTokenValue(line, "special", column_info[j].align_special);
1247 for (int i = 0; i < rows_; ++i) {
1248 l_getline(is, line);
1249 if (!prefixIs(line, "<row")) {
1250 lyxerr << "Wrong tabular format (expected <row ...> got" <<
1251 line << ")" << endl;
1254 getTokenValue(line, "topline", row_info[i].top_line);
1255 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1256 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1257 getTokenValue(line, "endhead", row_info[i].endhead);
1258 getTokenValue(line, "endfoot", row_info[i].endfoot);
1259 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1260 getTokenValue(line, "newpage", row_info[i].newpage);
1261 for (int j = 0; j < columns_; ++j) {
1262 l_getline(is, line);
1263 if (!prefixIs(line, "<cell")) {
1264 lyxerr << "Wrong tabular format (expected <cell ...> got" <<
1265 line << ")" << endl;
1268 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1269 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1270 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1271 getTokenValue(line, "topline", cell_info[i][j].top_line);
1272 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1273 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1274 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1275 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1276 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1277 getTokenValue(line, "width", cell_info[i][j].p_width);
1278 getTokenValue(line, "special", cell_info[i][j].align_special);
1279 l_getline(is, line);
1280 if (prefixIs(line, "\\begin_inset")) {
1281 cell_info[i][j].inset.read(buf, lex);
1282 l_getline(is, line);
1284 if (!prefixIs(line, "</cell>")) {
1285 lyxerr << "Wrong tabular format (expected </cell> got" <<
1286 line << ")" << endl;
1290 l_getline(is, line);
1291 if (!prefixIs(line, "</row>")) {
1292 lyxerr << "Wrong tabular format (expected </row> got" <<
1293 line << ")" << endl;
1297 while (!prefixIs(line, "</lyxtabular>")) {
1298 l_getline(is, line);
1300 set_row_column_number_info();
1304 void LyXTabular::OldFormatRead(BufferParams const & bp,
1305 LyXLex & lex, string const & fl)
1311 int columns_arg = 0;
1312 int is_long_tabular_arg = false;
1313 int rotate_arg = false;
1322 istream & is = lex.getStream();
1325 version = lyx::atoi(s.substr(8, string::npos));
1329 vector<int> cont_row_info;
1332 lyxerr << "Tabular format < 5 is not supported anymore\n"
1333 "Get an older version of LyX (< 1.1.x) for conversion!"
1335 Alert::alert(_("Warning:"),
1336 _("Tabular format < 5 is not supported anymore\n"),
1337 _("Get an older version of LyX (< 1.1.x) for conversion!"));
1339 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1340 >> rotate_arg >> a >> b >> c >> d;
1342 is >> rows_arg >> columns_arg;
1343 Init(bp, rows_arg, columns_arg);
1344 cont_row_info = vector<int>(rows_arg);
1345 SetLongTabular(is_long_tabular_arg);
1346 SetRotateTabular(rotate_arg);
1348 for (i = 0; i < rows_; ++i) {
1350 cont_row_info[i] = false;
1352 for (i = 0; i < columns_; ++i) {
1355 for (i = 0; i < rows_; ++i) {
1356 for (j = 0; j < columns_; ++j) {
1361 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1362 >> rotate_arg >> a >> b >> c >> d;
1363 Init(bp, rows_arg, columns_arg);
1364 cont_row_info = vector<int>(rows_arg);
1365 SetLongTabular(is_long_tabular_arg);
1366 SetRotateTabular(rotate_arg);
1367 setHeaderFooterRows(a+1, b+1 , c+1, d+1);
1368 for (i = 0; i < rows_; ++i) {
1369 a = b = c = d = e = f = g = 0;
1370 is >> a >> b >> c >> d;
1371 row_info[i].top_line = a;
1372 row_info[i].bottom_line = b;
1373 cont_row_info[i] = c;
1374 row_info[i].newpage = d;
1376 for (i = 0; i < columns_; ++i) {
1381 char ch; // skip '"'
1384 // ignore is buggy but we will use it later (Lgb)
1385 is.ignore(); // skip '"'
1387 getline(is, s1, '"');
1389 is >> ch; // skip '"'
1391 // ignore is buggy but we will use it later (Lgb)
1392 is.ignore(); // skip '"'
1394 getline(is, s2, '"');
1395 column_info[i].alignment = static_cast<LyXAlignment>(a);
1396 column_info[i].left_line = b;
1397 column_info[i].right_line = c;
1398 column_info[i].p_width = LyXLength(s1);
1399 column_info[i].align_special = s2;
1401 for (i = 0; i < rows_; ++i) {
1402 for (j = 0; j < columns_; ++j) {
1405 is >> a >> b >> c >> d >> e >> f >> g;
1408 is >> ch; // skip '"'
1410 // ignore is buggy but we will use it later (Lgb)
1411 is.ignore(); // skip '"'
1413 getline(is, s1, '"');
1415 is >> ch; // skip '"'
1417 // ignore is buggy but we will use it later (Lgb)
1418 is.ignore(); // skip '"'
1420 getline(is, s2, '"');
1421 cell_info[i][j].multicolumn = static_cast<char>(a);
1422 cell_info[i][j].alignment = static_cast<LyXAlignment>(b);
1423 cell_info[i][j].top_line = static_cast<char>(c);
1424 cell_info[i][j].bottom_line = static_cast<char>(d);
1425 cell_info[i][j].left_line = column_info[j].left_line;
1426 cell_info[i][j].right_line = column_info[j].right_line;
1427 cell_info[i][j].rotate = static_cast<bool>(f);
1428 cell_info[i][j].usebox = static_cast<BoxType>(g);
1429 cell_info[i][j].align_special = s1;
1430 cell_info[i][j].p_width = LyXLength(s2);
1434 set_row_column_number_info(true);
1436 Paragraph * par = new Paragraph;
1437 Paragraph * return_par = 0;
1441 Paragraph::depth_type depth = 0;
1442 LyXFont font(LyXFont::ALL_INHERIT);
1443 font.setLanguage(owner_->bufferOwner()->getLanguage());
1445 while (lex.isOK()) {
1447 string const token = lex.getString();
1450 if (token == "\\layout"
1451 || token == "\\end_float"
1452 || token == "\\end_deeper") {
1453 lex.pushToken(token);
1454 #ifndef NO_COMPABILITY
1455 // Here we need to insert the inset_ert_contents into the last
1456 // cell of the tabular.
1457 owner_->bufferOwner()->insertErtContents(par, pos);
1461 if (owner_->bufferOwner()->parseSingleLyXformat2Token(lex, par,
1466 lex.pushToken(token);
1470 lex.printError("New Paragraph allocated! This should not happen!");
1471 lex.pushToken(token);
1477 // now we have the par we should fill the insets with this!
1479 InsetText * inset = GetCellInset(cell);
1482 for (int i = 0; i < par->size(); ++i) {
1483 if (par->isNewline(i)) {
1485 if (cell > numberofcells) {
1486 lyxerr << "Some error in reading old table format occured!" <<
1487 endl << "Terminating when reading cell[" << cell << "]!" <<
1492 row = row_of_cell(cell);
1493 if (cont_row_info[row]) {
1495 cont_row_info.erase(cont_row_info.begin() + row); //&cont_row_info[row]);
1496 while (!IsFirstCellInRow(--cell));
1498 inset = GetCellInset(cell);
1501 inset = GetCellInset(cell);
1502 row = row_of_cell(cell);
1503 if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].usebox)
1505 // insert a space instead
1507 par->insertChar(i, ' ');
1510 par->copyIntoMinibuffer(*owner_->bufferOwner(), i);
1511 inset->paragraph()->insertFromMinibuffer(inset->paragraph()->size());
1518 bool LyXTabular::IsMultiColumn(int cell, bool real) const
1520 return ((!real || (column_of_cell(cell) != right_column_of_cell(cell))) &&
1521 (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL));
1525 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1527 int const row = row_of_cell(cell);
1528 int const column = column_of_cell(cell);
1529 return &cell_info[row][column];
1533 void LyXTabular::SetMultiColumn(int cell, int number)
1535 cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1536 cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1537 cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1538 cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1539 for (number--; number > 0; --number) {
1540 cellinfo_of_cell(cell+number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1542 set_row_column_number_info();
1546 int LyXTabular::cells_in_multicolumn(int cell) const
1548 int const row = row_of_cell(cell);
1549 int column = column_of_cell(cell);
1552 while ((column < columns_) &&
1553 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1562 int LyXTabular::UnsetMultiColumn(int cell)
1564 int const row = row_of_cell(cell);
1565 int column = column_of_cell(cell);
1569 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1570 cell_info[row][column].multicolumn = CELL_NORMAL;
1572 while ((column < columns_) &&
1573 (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1575 cell_info[row][column].multicolumn = CELL_NORMAL;
1580 set_row_column_number_info();
1585 void LyXTabular::SetLongTabular(bool what)
1587 is_long_tabular = what;
1591 bool LyXTabular::IsLongTabular() const
1593 return is_long_tabular;
1597 void LyXTabular::SetRotateTabular(bool flag)
1603 bool LyXTabular::GetRotateTabular() const
1609 void LyXTabular::SetRotateCell(int cell, bool flag)
1611 cellinfo_of_cell(cell)->rotate = flag;
1615 bool LyXTabular::GetRotateCell(int cell) const
1617 return cellinfo_of_cell(cell)->rotate;
1621 bool LyXTabular::NeedRotating() const
1625 for (int i = 0; i < rows_; ++i) {
1626 for (int j = 0; j < columns_; ++j) {
1627 if (cell_info[i][j].rotate)
1635 bool LyXTabular::IsLastCell(int cell) const
1637 if ((cell + 1) < numberofcells)
1643 int LyXTabular::GetCellAbove(int cell) const
1645 if (row_of_cell(cell) > 0)
1646 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1651 int LyXTabular::GetCellBelow(int cell) const
1653 if (row_of_cell(cell) + 1 < rows_)
1654 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1659 int LyXTabular::GetLastCellAbove(int cell) const
1661 if (row_of_cell(cell) <= 0)
1663 if (!IsMultiColumn(cell))
1664 return GetCellAbove(cell);
1665 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1669 int LyXTabular::GetLastCellBelow(int cell) const
1671 if (row_of_cell(cell) + 1 >= rows_)
1673 if (!IsMultiColumn(cell))
1674 return GetCellBelow(cell);
1675 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1679 int LyXTabular::GetCellNumber(int row, int column) const
1682 if (column >= columns_)
1683 column = columns_ - 1;
1684 else if (column < 0)
1691 lyx::Assert(column >= 0 || column < columns_ || row >= 0 || row < rows_);
1693 return cell_info[row][column].cellno;
1697 void LyXTabular::SetUsebox(int cell, BoxType type)
1699 cellinfo_of_cell(cell)->usebox = type;
1703 LyXTabular::BoxType LyXTabular::GetUsebox(int cell) const
1705 if (column_info[column_of_cell(cell)].p_width.zero() &&
1706 !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.zero()))
1708 if (cellinfo_of_cell(cell)->usebox > 1)
1709 return cellinfo_of_cell(cell)->usebox;
1710 return UseParbox(cell);
1715 // This are functions used for the longtable support
1717 void LyXTabular::SetLTHead(int row, bool flag, ltType const & hd, bool first)
1722 row_info[row].endfirsthead = flag;
1726 row_info[row].endhead = flag;
1731 bool LyXTabular::GetRowOfLTHead(int row, ltType & hd) const
1734 hd.set = haveLTHead();
1735 return row_info[row].endhead;
1739 bool LyXTabular::GetRowOfLTFirstHead(int row, ltType & hd) const
1742 hd.set = haveLTFirstHead();
1743 return row_info[row].endfirsthead;
1747 void LyXTabular::SetLTFoot(int row, bool flag, ltType const & fd, bool last)
1752 row_info[row].endlastfoot = flag;
1756 row_info[row].endfoot = flag;
1761 bool LyXTabular::GetRowOfLTFoot(int row, ltType & fd) const
1764 fd.set = haveLTFoot();
1765 return row_info[row].endfoot;
1769 bool LyXTabular::GetRowOfLTLastFoot(int row, ltType & fd) const
1772 fd.set = haveLTLastFoot();
1773 return row_info[row].endlastfoot;
1777 void LyXTabular::SetLTNewPage(int row, bool what)
1779 row_info[row].newpage = what;
1783 bool LyXTabular::GetLTNewPage(int row) const
1785 return row_info[row].newpage;
1789 bool LyXTabular::haveLTHead() const
1791 for(int i=0; i < rows_; ++i) {
1792 if (row_info[i].endhead)
1799 bool LyXTabular::haveLTFirstHead() const
1801 if (endfirsthead.empty)
1803 for(int i=0; i < rows_; ++i) {
1804 if (row_info[i].endfirsthead)
1811 bool LyXTabular::haveLTFoot() const
1813 for(int i=0; i < rows_; ++i) {
1814 if (row_info[i].endfoot)
1821 bool LyXTabular::haveLTLastFoot() const
1823 if (endlastfoot.empty)
1825 for(int i=0; i < rows_; ++i) {
1826 if (row_info[i].endlastfoot)
1833 // end longtable support functions
1835 bool LyXTabular::SetAscentOfRow(int row, int height)
1837 if ((row >= rows_) || (row_info[row].ascent_of_row == height))
1839 row_info[row].ascent_of_row = height;
1844 bool LyXTabular::SetDescentOfRow(int row, int height)
1846 if ((row >= rows_) || (row_info[row].descent_of_row == height))
1848 row_info[row].descent_of_row = height;
1853 int LyXTabular::GetAscentOfRow(int row) const
1857 return row_info[row].ascent_of_row;
1861 int LyXTabular::GetDescentOfRow(int row) const
1865 return row_info[row].descent_of_row;
1869 int LyXTabular::GetHeightOfTabular() const
1873 for (int row = 0; row < rows_; ++row)
1874 height += GetAscentOfRow(row) + GetDescentOfRow(row) +
1875 GetAdditionalHeight(row);
1880 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
1882 if ((row >= rows_) || (column >= columns_))
1884 return (cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN);
1888 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1890 if ((row < 0) || (row >= rows_))
1893 int const fcell = GetFirstCellInRow(row);
1894 int const n = NumberOfCellsInRow(fcell) + fcell;
1897 for (int i = fcell; i < n; ++i) {
1901 if (tmp == (n - fcell)) {
1904 for (int i = fcell; i < n; ++i) {
1907 << column_of_cell(i) + 1
1909 << right_column_of_cell(i) + 1
1921 int LyXTabular::TeXBottomHLine(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) {
1938 if (BottomLine(i)) {
1940 << column_of_cell(i) + 1
1942 << right_column_of_cell(i) + 1
1954 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1958 if (GetRotateCell(cell)) {
1959 os << "\\begin{sideways}\n";
1962 if (IsMultiColumn(cell)) {
1963 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1964 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1965 os << cellinfo_of_cell(cell)->align_special << "}{";
1967 if (LeftLine(cell) &&
1968 (IsFirstCellInRow(cell) ||
1969 (!IsMultiColumn(cell-1) && !LeftLine(cell, true) &&
1970 !RightLine(cell-1, true))))
1974 if (!GetPWidth(cell).zero()) {
1975 switch (GetVAlignment(cell)) {
1976 case LYX_VALIGN_TOP:
1979 case LYX_VALIGN_CENTER:
1982 case LYX_VALIGN_BOTTOM:
1986 os << "{" << GetPWidth(cell).asLatexString() << '}';
1988 switch (GetAlignment(cell)) {
1989 case LYX_ALIGN_LEFT:
1992 case LYX_ALIGN_RIGHT:
2000 if (RightLine(cell))
2002 if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
2008 if (GetUsebox(cell) == BOX_PARBOX) {
2010 switch (GetVAlignment(cell)) {
2011 case LYX_VALIGN_TOP:
2014 case LYX_VALIGN_CENTER:
2017 case LYX_VALIGN_BOTTOM:
2021 os << "]{" << GetPWidth(cell).asLatexString() << "}{";
2022 } else if (GetUsebox(cell) == BOX_MINIPAGE) {
2023 os << "\\begin{minipage}[";
2024 switch (GetVAlignment(cell)) {
2025 case LYX_VALIGN_TOP:
2028 case LYX_VALIGN_CENTER:
2031 case LYX_VALIGN_BOTTOM:
2035 os << "]{" << GetPWidth(cell).asLatexString() << "}\n";
2042 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
2047 if (GetUsebox(cell) == BOX_PARBOX)
2049 else if (GetUsebox(cell) == BOX_MINIPAGE) {
2050 os << "%\n\\end{minipage}";
2053 if (IsMultiColumn(cell)) {
2056 if (GetRotateCell(cell)) {
2057 os << "%\n\\end{sideways}";
2064 int LyXTabular::TeXLongtableHeaderFooter(ostream & os, Buffer const * buf,
2065 bool fragile, bool fp) const
2067 if (!is_long_tabular)
2071 // output header info
2073 if (endhead.topDL) {
2077 for (int i = 0; i < rows_; ++i) {
2078 if (row_info[i].endhead) {
2079 ret += TeXRow(os, i, buf, fragile, fp);
2082 if (endhead.bottomDL) {
2086 os << "\\endhead\n";
2088 if (endfirsthead.empty) {
2089 os << "\\endfirsthead\n";
2093 // output firstheader info
2094 if (haveLTFirstHead()) {
2095 if (endfirsthead.topDL) {
2099 for (int i = 0; i < rows_; ++i) {
2100 if (row_info[i].endfirsthead) {
2101 ret += TeXRow(os, i, buf, fragile, fp);
2104 if (endfirsthead.bottomDL) {
2108 os << "\\endfirsthead\n";
2111 // output footer info
2113 if (endfoot.topDL) {
2117 for (int i = 0; i < rows_; ++i) {
2118 if (row_info[i].endfoot) {
2119 ret += TeXRow(os, i, buf, fragile, fp);
2122 if (endfoot.bottomDL) {
2126 os << "\\endfoot\n";
2128 if (endlastfoot.empty) {
2129 os << "\\endlastfoot\n";
2133 // output lastfooter info
2134 if (haveLTLastFoot()) {
2135 if (endlastfoot.topDL) {
2139 for (int i = 0; i < rows_; ++i) {
2140 if (row_info[i].endlastfoot) {
2141 ret += TeXRow(os, i, buf, fragile, fp);
2144 if (endlastfoot.bottomDL) {
2148 os << "\\endlastfoot\n";
2155 bool LyXTabular::isValidRow(int const row) const
2157 if (!is_long_tabular)
2159 return (!row_info[row].endhead && !row_info[row].endfirsthead &&
2160 !row_info[row].endfoot && !row_info[row].endlastfoot);
2164 int LyXTabular::TeXRow(ostream & os, int const i, Buffer const * buf,
2165 bool fragile, bool fp) const
2168 int cell = GetCellNumber(i, 0);
2170 ret += TeXTopHLine(os, i);
2171 for (int j = 0; j < columns_; ++j) {
2172 if (IsPartOfMultiColumn(i,j))
2174 ret += TeXCellPreamble(os, cell);
2175 InsetText * inset = GetCellInset(cell);
2177 bool rtl = inset->paragraph()->isRightToLeftPar(buf->params) &&
2178 inset->paragraph()->size() > 0 && GetPWidth(cell).zero();
2182 ret += inset->latex(buf, os, fragile, fp);
2186 ret += TeXCellPostamble(os, cell);
2187 if (!IsLastCellInRow(cell)) { // not last cell in row
2195 ret += TeXBottomHLine(os, i);
2200 int LyXTabular::latex(Buffer const * buf,
2201 ostream & os, bool fragile, bool fp) const
2205 //+---------------------------------------------------------------------
2206 //+ first the opening preamble +
2207 //+---------------------------------------------------------------------
2210 os << "\\begin{sideways}\n";
2213 if (is_long_tabular)
2214 os << "\\begin{longtable}{";
2216 os << "\\begin{tabular}{";
2217 for (int i = 0; i < columns_; ++i) {
2218 if (!column_info[i].align_special.empty()) {
2219 os << column_info[i].align_special;
2221 if (column_info[i].left_line)
2223 if (!column_info[i].p_width.zero()) {
2224 switch (column_info[i].valignment) {
2225 case LYX_VALIGN_TOP:
2228 case LYX_VALIGN_CENTER:
2231 case LYX_VALIGN_BOTTOM:
2236 << column_info[i].p_width.asLatexString()
2239 switch (column_info[i].alignment) {
2240 case LYX_ALIGN_LEFT:
2243 case LYX_ALIGN_RIGHT:
2251 if (column_info[i].right_line)
2258 ret += TeXLongtableHeaderFooter(os, buf, fragile, fp);
2260 //+---------------------------------------------------------------------
2261 //+ the single row and columns (cells) +
2262 //+---------------------------------------------------------------------
2264 for (int i = 0; i < rows_; ++i) {
2265 if (isValidRow(i)) {
2266 ret += TeXRow(os, i, buf, fragile, fp);
2267 if (is_long_tabular && row_info[i].newpage) {
2268 os << "\\newpage\n";
2274 //+---------------------------------------------------------------------
2275 //+ the closing of the tabular +
2276 //+---------------------------------------------------------------------
2278 if (is_long_tabular)
2279 os << "\\end{longtable}";
2281 os << "\\end{tabular}";
2283 os << "\n\\end{sideways}";
2291 int LyXTabular::docbookRow(Buffer const * buf, ostream & os, int row) const
2294 int cell = GetFirstCellInRow(row);
2297 for (int j = 0; j < columns_; ++j) {
2298 if (IsPartOfMultiColumn(row, j))
2301 os << "<entry align=\"";
2302 switch (GetAlignment(cell)) {
2303 case LYX_ALIGN_LEFT:
2306 case LYX_ALIGN_RIGHT:
2314 os << "\" valign=\"";
2315 switch (GetVAlignment(cell)) {
2316 case LYX_VALIGN_TOP:
2319 case LYX_VALIGN_BOTTOM:
2322 case LYX_VALIGN_CENTER:
2327 if (IsMultiColumn(cell)) {
2328 os << " namest=\"col" << j << "\" ";
2329 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< "\"";
2333 ret += GetCellInset(cell)->docbook(buf, os);
2342 int LyXTabular::docBook(Buffer const * buf, ostream & os) const
2346 //+---------------------------------------------------------------------
2347 //+ first the opening preamble +
2348 //+---------------------------------------------------------------------
2350 os << "<tgroup cols=\"" << columns_
2351 << "\" colsep=\"1\" rowsep=\"1\">\n";
2353 for (int i = 0; i < columns_; ++i) {
2354 os << "<colspec colname=\"col" << i << "\" align=\"";
2355 switch (column_info[i].alignment) {
2356 case LYX_ALIGN_LEFT:
2359 case LYX_ALIGN_RIGHT:
2370 //+---------------------------------------------------------------------
2371 //+ Long Tabular case +
2372 //+---------------------------------------------------------------------
2374 // output header info
2375 if (haveLTHead() || haveLTFirstHead()) {
2378 for (int i = 0; i < rows_; ++i) {
2379 if (row_info[i].endhead || row_info[i].endfirsthead) {
2380 ret += docbookRow(buf, os, i);
2386 // output footer info
2387 if (haveLTFoot() || haveLTLastFoot()) {
2390 for (int i = 0; i < rows_; ++i) {
2391 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2392 ret += docbookRow(buf, os, i);
2399 //+---------------------------------------------------------------------
2400 //+ the single row and columns (cells) +
2401 //+---------------------------------------------------------------------
2405 for (int i = 0; i < rows_; ++i) {
2406 if (isValidRow(i)) {
2407 ret += docbookRow(buf, os, i);
2412 //+---------------------------------------------------------------------
2413 //+ the closing of the tabular +
2414 //+---------------------------------------------------------------------
2423 // ASCII export function and helpers
2425 int LyXTabular::asciiTopHLine(ostream & os, int row,
2426 vector<unsigned int> const & clen) const
2428 int const fcell = GetFirstCellInRow(row);
2429 int const n = NumberOfCellsInRow(fcell) + fcell;
2432 for (int i = fcell; i < n; ++i) {
2442 for (int i = fcell; i < n; ++i) {
2453 int column = column_of_cell(i);
2454 int len = clen[column];
2455 while (IsPartOfMultiColumn(row, ++column))
2456 len += clen[column] + 4;
2457 os << string(len, ch);
2472 int LyXTabular::asciiBottomHLine(ostream & os, int row,
2473 vector<unsigned int> const & clen) const
2475 int const fcell = GetFirstCellInRow(row);
2476 int const n = NumberOfCellsInRow(fcell) + fcell;
2479 for (int i = fcell; i < n; ++i) {
2480 if (BottomLine(i)) {
2489 for (int i = fcell; i < n; ++i) {
2490 if (BottomLine(i)) {
2500 int column = column_of_cell(i);
2501 int len = clen[column];
2502 while (IsPartOfMultiColumn(row, ++column))
2503 len += clen[column] + 4;
2504 os << string(len, ch);
2505 if (BottomLine(i)) {
2519 int LyXTabular::asciiPrintCell(Buffer const * buf, ostream & os,
2520 int cell, int row, int column,
2521 vector<unsigned int> const & clen,
2522 bool onlydata) const
2525 int ret = GetCellInset(cell)->ascii(buf, sstr, 0);
2537 unsigned int len1 = sstr.str().length();
2538 unsigned int len2 = clen[column];
2539 while (IsPartOfMultiColumn(row, ++column))
2540 len2 += clen[column] + 4;
2543 switch (GetAlignment(cell)) {
2545 case LYX_ALIGN_LEFT:
2548 case LYX_ALIGN_RIGHT:
2552 case LYX_ALIGN_CENTER:
2558 for (unsigned int i = 0; i < len1; ++i)
2561 for (unsigned int i = 0; i < len2; ++i)
2563 if (RightLine(cell))
2572 int LyXTabular::ascii(Buffer const * buf, ostream & os, int const depth,
2573 bool onlydata, unsigned char delim) const
2577 //+---------------------------------------------------------------------
2578 //+ first calculate the width of the single columns +
2579 //+---------------------------------------------------------------------
2580 vector<unsigned int> clen(columns_);
2583 // first all non (real) multicolumn cells!
2584 for (int j = 0; j < columns_; ++j) {
2586 for (int i = 0; i < rows_; ++i) {
2587 int cell = GetCellNumber(i, j);
2588 if (IsMultiColumn(cell, true))
2591 GetCellInset(cell)->ascii(buf, sstr, 0);
2592 if (clen[j] < sstr.str().length())
2593 clen[j] = sstr.str().length();
2596 // then all (real) multicolumn cells!
2597 for (int j = 0; j < columns_; ++j) {
2598 for (int i = 0; i < rows_; ++i) {
2599 int cell = GetCellNumber(i, j);
2600 if (!IsMultiColumn(cell, true) || IsPartOfMultiColumn(i, j))
2603 GetCellInset(cell)->ascii(buf, sstr, 0);
2604 int len = int(sstr.str().length());
2605 int const n = cells_in_multicolumn(cell);
2606 for (int k = j; (len > 0) && (k < (j + n - 1)); ++k)
2608 if (len > int(clen[j + n - 1]))
2609 clen[j + n - 1] = len;
2614 for (int i = 0; i < rows_; ++i) {
2616 if (asciiTopHLine(os, i, clen)) {
2617 for (int j = 0; j < depth; ++j)
2621 for (int j = 0; j < columns_; ++j) {
2622 if (IsPartOfMultiColumn(i,j))
2624 if (onlydata && j > 0)
2626 ret += asciiPrintCell(buf, os, cell, i, j, clen, onlydata);
2631 for (int j = 0; j < depth; ++j)
2633 if (asciiBottomHLine(os, i, clen)) {
2634 for (int j = 0; j < depth; ++j)
2646 InsetText * LyXTabular::GetCellInset(int cell) const
2649 return & cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2653 InsetText * LyXTabular::GetCellInset(int row, int column) const
2655 cur_cell = GetCellNumber(row, column);
2656 return & cell_info[row][column].inset;
2660 int LyXTabular::GetCellFromInset(Inset const * inset, int maybe_cell) const
2662 // is this inset part of the tabular?
2663 if (!inset || inset->owner() != owner_) {
2664 lyxerr[Debug::INSETTEXT]
2665 << "this is not a cell of the tabular!" << endl;
2669 const int save_cur_cell = cur_cell;
2670 int cell = cur_cell;
2671 if (GetCellInset(cell) != inset) {
2673 if (cell == -1 || GetCellInset(cell) != inset) {
2679 for (cell = GetNumberOfCells(); cell >= 0; --cell) {
2680 if (GetCellInset(cell) == inset)
2683 lyxerr[Debug::INSETTEXT]
2684 << "LyXTabular::GetCellFromInset: "
2686 << ", cur_cell=" << save_cur_cell
2687 << ", maybe_cell=" << maybe_cell
2689 // We should have found a cell at this point
2691 lyxerr << "LyXTabular::GetCellFromInset: "
2692 << "Cell not found!" << endl;
2700 void LyXTabular::Validate(LaTeXFeatures & features) const
2702 if (IsLongTabular())
2703 features.require("longtable");
2705 features.require("rotating");
2706 for (int cell = 0; cell < numberofcells; ++cell) {
2707 if (GetVAlignment(cell) != LYX_VALIGN_TOP)
2708 features.require("array");
2709 GetCellInset(cell)->validate(features);
2714 vector<string> const LyXTabular::getLabelList() const
2716 vector<string> label_list;
2717 for (int i = 0; i < rows_; ++i)
2718 for (int j = 0; j < columns_; ++j) {
2719 vector<string> const l =
2720 GetCellInset(i, j)->getLabelList();
2721 label_list.insert(label_list.end(),
2722 l.begin(), l.end());
2728 LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
2730 Paragraph * par = GetCellInset(cell)->paragraph();
2732 for (; par; par = par->next()) {
2733 for (int i = 0; i < par->size(); ++i) {
2734 if (par->getChar(i) == Paragraph::META_NEWLINE)