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 calculate_width_of_column_NMC(i);
648 recalculateMulticolumnsOfColumn(i);
649 calculate_width_of_column(i);
655 void LyXTabular::recalculateMulticolumnsOfColumn(int column)
657 // the last column does not have to be recalculated because all
658 // multicolumns will have here there last multicolumn cell which
659 // always will have the whole rest of the width of the cell.
660 if (column > (columns_ - 2))
662 for(int row = 0; row < rows_; ++row) {
663 int mc = cell_info[row][column].multicolumn;
664 int nmc = cell_info[row][column+1].multicolumn;
665 // we only have to update multicolumns which do not have this
666 // column as their last column!
667 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
668 ((mc == CELL_PART_OF_MULTICOLUMN) &&
669 (nmc == CELL_PART_OF_MULTICOLUMN)))
671 int const cellno = cell_info[row][column].cellno;
672 SetWidthOfMulticolCell(cellno,
673 GetWidthOfCell(cellno)-(2 * WIDTH_OF_LINE));
679 /* returns 1 if a complete update is necessary, otherwise 0 */
680 bool LyXTabular::SetWidthOfCell(int cell, int new_width)
682 int const row = row_of_cell(cell);
683 int const column1 = column_of_cell(cell);
688 #ifdef SPECIAL_COLUM_HANDLING
689 if (RightLine(cell_info[row][column1].cellno, true) &&
690 (column1 < columns_-1) &&
691 LeftLine(cell_info[row][column1+1].cellno, true))
693 if (column_info[column1].right_line && (column1 < columns_-1) &&
694 column_info[column1+1].left_line) // additional width
698 add_width = WIDTH_OF_LINE;
700 if (GetWidthOfCell(cell) == (new_width+2*WIDTH_OF_LINE+add_width)) {
703 if (IsMultiColumn(cell, true)) {
704 tmp = SetWidthOfMulticolCell(cell, new_width);
706 width = (new_width + 2*WIDTH_OF_LINE + add_width);
707 cell_info[row][column1].width_of_cell = width;
708 tmp = calculate_width_of_column_NMC(column1);
710 recalculateMulticolumnsOfColumn(column1);
713 for (int i = 0; i < columns_; ++i)
714 calculate_width_of_column(i);
715 calculate_width_of_tabular();
722 bool LyXTabular::SetAlignment(int cell, LyXAlignment align, bool onlycolumn)
724 if (!IsMultiColumn(cell) || onlycolumn)
725 column_info[column_of_cell(cell)].alignment = align;
727 cellinfo_of_cell(cell)->alignment = align;
732 bool LyXTabular::SetVAlignment(int cell, VAlignment align, bool onlycolumn)
734 if (!IsMultiColumn(cell) || onlycolumn)
735 column_info[column_of_cell(cell)].valignment = align;
737 cellinfo_of_cell(cell)->valignment = align;
742 bool LyXTabular::SetColumnPWidth(int cell, LyXLength const & width)
744 bool flag = !width.zero();
745 int const j = column_of_cell(cell);
747 column_info[j].p_width = width;
748 if (flag) // do this only if there is a width
749 SetAlignment(cell, LYX_ALIGN_LEFT);
750 for (int i = 0; i < rows_; ++i) {
751 int c = GetCellNumber(i, j);
752 flag = !GetPWidth(c).zero(); // because of multicolumns!
753 GetCellInset(c)->setAutoBreakRows(flag);
759 bool LyXTabular::SetMColumnPWidth(int cell, LyXLength const & width)
761 bool const flag = !width.zero();
763 cellinfo_of_cell(cell)->p_width = width;
764 if (IsMultiColumn(cell)) {
765 GetCellInset(cell)->setAutoBreakRows(flag);
772 bool LyXTabular::SetAlignSpecial(int cell, string const & special,
773 LyXTabular::Feature what)
775 if (what == SET_SPECIAL_MULTI)
776 cellinfo_of_cell(cell)->align_special = special;
778 column_info[column_of_cell(cell)].align_special = special;
783 bool LyXTabular::SetAllLines(int cell, bool line)
785 SetTopLine(cell, line);
786 SetBottomLine(cell, line);
787 SetRightLine(cell, line);
788 SetLeftLine(cell, line);
793 bool LyXTabular::SetTopLine(int cell, bool line, bool onlycolumn)
795 int const row = row_of_cell(cell);
797 if (onlycolumn || !IsMultiColumn(cell))
798 row_info[row].top_line = line;
800 cellinfo_of_cell(cell)->top_line = line;
805 bool LyXTabular::SetBottomLine(int cell, bool line, bool onlycolumn)
807 if (onlycolumn || !IsMultiColumn(cell))
808 row_info[row_of_cell(cell)].bottom_line = line;
810 cellinfo_of_cell(cell)->bottom_line = line;
815 bool LyXTabular::SetLeftLine(int cell, bool line, bool onlycolumn)
817 if (onlycolumn || !IsMultiColumn(cell))
818 column_info[column_of_cell(cell)].left_line = line;
820 cellinfo_of_cell(cell)->left_line = line;
825 bool LyXTabular::SetRightLine(int cell, bool line, bool onlycolumn)
827 if (onlycolumn || !IsMultiColumn(cell))
828 column_info[right_column_of_cell(cell)].right_line = line;
830 cellinfo_of_cell(cell)->right_line = line;
835 LyXAlignment LyXTabular::GetAlignment(int cell, bool onlycolumn) const
837 if (!onlycolumn && IsMultiColumn(cell))
838 return cellinfo_of_cell(cell)->alignment;
840 return column_info[column_of_cell(cell)].alignment;
844 LyXTabular::VAlignment
845 LyXTabular::GetVAlignment(int cell, bool onlycolumn) const
847 if (!onlycolumn && IsMultiColumn(cell))
848 return cellinfo_of_cell(cell)->valignment;
850 return column_info[column_of_cell(cell)].valignment;
854 LyXLength const LyXTabular::GetPWidth(int cell) const
856 if (IsMultiColumn(cell))
857 return cellinfo_of_cell(cell)->p_width;
858 return column_info[column_of_cell(cell)].p_width;
862 LyXLength const LyXTabular::GetColumnPWidth(int cell) const
864 return column_info[column_of_cell(cell)].p_width;
868 LyXLength const LyXTabular::GetMColumnPWidth(int cell) const
870 if (IsMultiColumn(cell))
871 return cellinfo_of_cell(cell)->p_width;
876 string const LyXTabular::GetAlignSpecial(int cell, int what) const
878 if (what == SET_SPECIAL_MULTI)
879 return cellinfo_of_cell(cell)->align_special;
880 return column_info[column_of_cell(cell)].align_special;
884 int LyXTabular::GetWidthOfCell(int cell) const
886 int const row = row_of_cell(cell);
887 int const column1 = column_of_cell(cell);
888 int const column2 = right_column_of_cell(cell);
890 for (int i = column1; i <= column2; ++i) {
891 result += cell_info[row][i].width_of_cell;
897 int LyXTabular::GetBeginningOfTextInCell(int cell) const
901 switch (GetAlignment(cell)) {
902 case LYX_ALIGN_CENTER:
903 x += (GetWidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
905 case LYX_ALIGN_RIGHT:
906 x += GetWidthOfColumn(cell) - GetWidthOfCell(cell);
907 // + GetAdditionalWidth(cell);
909 default: /* LYX_ALIGN_LEFT: nothing :-) */
919 bool LyXTabular::IsFirstCellInRow(int cell) const
921 return column_of_cell(cell) == 0;
925 int LyXTabular::GetFirstCellInRow(int row) const
929 return cell_info[row][0].cellno;
932 bool LyXTabular::IsLastCellInRow(int cell) const
934 return (right_column_of_cell(cell) == (columns_ - 1));
938 int LyXTabular::GetLastCellInRow(int row) const
942 return cell_info[row][columns_-1].cellno;
946 bool LyXTabular::calculate_width_of_column(int column)
948 int const old_column_width = column_info[column].width_of_column;
951 for (int i = 0; i < rows_; ++i) {
952 maximum = max(cell_info[i][column].width_of_cell, maximum);
954 column_info[column].width_of_column = maximum;
955 return (column_info[column].width_of_column != old_column_width);
960 // Calculate the columns regarding ONLY the normal cells and if this
961 // column is inside a multicolumn cell then use it only if its the last
962 // column of this multicolumn cell as this gives an added with to the
963 // column, all the rest should be adapted!
965 bool LyXTabular::calculate_width_of_column_NMC(int column)
967 int const old_column_width = column_info[column].width_of_column;
969 for (int i = 0; i < rows_; ++i) {
970 int cell = GetCellNumber(i, column);
971 bool ismulti = IsMultiColumn(cell, true);
972 if ((!ismulti || (column == right_column_of_cell(cell))) &&
973 (cell_info[i][column].width_of_cell > max))
975 max = cell_info[i][column].width_of_cell;
978 column_info[column].width_of_column = max;
979 return (column_info[column].width_of_column != old_column_width);
983 void LyXTabular::calculate_width_of_tabular()
985 width_of_tabular = 0;
986 for (int i = 0; i < columns_; ++i) {
987 width_of_tabular += column_info[i].width_of_column;
992 int LyXTabular::row_of_cell(int cell) const
994 if (cell >= numberofcells)
998 return rowofcell[cell];
1002 int LyXTabular::column_of_cell(int cell) const
1004 if (cell >= numberofcells)
1005 return columns_ - 1;
1008 return columnofcell[cell];
1012 int LyXTabular::right_column_of_cell(int cell) const
1014 int const row = row_of_cell(cell);
1015 int column = column_of_cell(cell);
1016 while (column < (columns_ - 1) &&
1017 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
1023 void LyXTabular::Write(Buffer const * buf, ostream & os) const
1027 << write_attribute("version", 3)
1028 << write_attribute("rows", rows_)
1029 << write_attribute("columns", columns_)
1031 // global longtable options
1033 << write_attribute("rotate", rotate)
1034 << write_attribute("islongtable", is_long_tabular)
1035 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1036 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1037 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1038 << write_attribute("headTopDL", endhead.topDL)
1039 << write_attribute("headBottomDL", endhead.bottomDL)
1040 << write_attribute("footTopDL", endfoot.topDL)
1041 << write_attribute("footBottomDL", endfoot.bottomDL)
1042 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1043 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1044 << write_attribute("lastFootEmpty", endlastfoot.empty)
1046 for (int j = 0; j < columns_; ++j) {
1048 << write_attribute("alignment", column_info[j].alignment)
1049 << write_attribute("valignment", column_info[j].valignment)
1050 << write_attribute("leftline", column_info[j].left_line)
1051 << write_attribute("rightline", column_info[j].right_line)
1052 << write_attribute("width", column_info[j].p_width.asString())
1053 << write_attribute("special", column_info[j].align_special)
1056 for (int i = 0; i < rows_; ++i) {
1058 << write_attribute("topline", row_info[i].top_line)
1059 << write_attribute("bottomline", row_info[i].bottom_line)
1060 << write_attribute("endhead", row_info[i].endhead)
1061 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1062 << write_attribute("endfoot", row_info[i].endfoot)
1063 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1064 << write_attribute("newpage", row_info[i].newpage)
1066 for (int j = 0; j < columns_; ++j) {
1068 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1069 << write_attribute("alignment", cell_info[i][j].alignment)
1070 << write_attribute("valignment", cell_info[i][j].valignment)
1071 << write_attribute("topline", cell_info[i][j].top_line)
1072 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1073 << write_attribute("leftline", cell_info[i][j].left_line)
1074 << write_attribute("rightline", cell_info[i][j].right_line)
1075 << write_attribute("rotate", cell_info[i][j].rotate)
1076 << write_attribute("usebox", cell_info[i][j].usebox)
1077 << write_attribute("width", cell_info[i][j].p_width)
1078 << write_attribute("special", cell_info[i][j].align_special)
1080 os << "\\begin_inset ";
1081 cell_info[i][j].inset.write(buf, os);
1082 os << "\n\\end_inset \n"
1087 os << "</lyxtabular>\n";
1091 void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
1094 istream & is = lex.getStream();
1096 l_getline(is, line);
1097 if (!prefixIs(line, "<lyxtabular ")
1098 && !prefixIs(line, "<LyXTabular ")) {
1099 OldFormatRead(buf->params, lex, line);
1104 if (!getTokenValue(line, "version", version))
1107 ReadOld(buf, is, lex, line);
1108 else if (version >= 2)
1109 ReadNew(buf, is, lex, line, version);
1112 void LyXTabular::setHeaderFooterRows(int hr, int fhr, int fr, int lfr)
1116 row_info[--hr].endhead = true;
1118 // set firstheader info
1119 if (fhr && (fhr < rows_)) {
1120 if (row_info[fhr].endhead) {
1122 row_info[--fhr].endfirsthead = true;
1123 row_info[fhr].endhead = false;
1125 } else if (row_info[fhr - 1].endhead) {
1126 endfirsthead.empty = true;
1128 while((fhr > 0) && !row_info[--fhr].endhead) {
1129 row_info[fhr].endfirsthead = true;
1134 if (fr && (fr < rows_)) {
1135 if (row_info[fr].endhead && row_info[fr-1].endhead) {
1136 while((fr > 0) && !row_info[--fr].endhead) {
1137 row_info[fr].endfoot = true;
1138 row_info[fr].endhead = false;
1140 } else if (row_info[fr].endfirsthead && row_info[fr-1].endfirsthead) {
1141 while((fr > 0) && !row_info[--fr].endfirsthead) {
1142 row_info[fr].endfoot = true;
1143 row_info[fr].endfirsthead = false;
1145 } else if (!row_info[fr - 1].endhead && !row_info[fr - 1].endfirsthead) {
1146 while((fr > 0) && !row_info[--fr].endhead &&
1147 !row_info[fr].endfirsthead)
1149 row_info[fr].endfoot = true;
1153 // set lastfooter info
1154 if (lfr && (lfr < rows_)) {
1155 if (row_info[lfr].endhead && row_info[lfr - 1].endhead) {
1156 while((lfr > 0) && !row_info[--lfr].endhead) {
1157 row_info[lfr].endlastfoot = true;
1158 row_info[lfr].endhead = false;
1160 } else if (row_info[lfr].endfirsthead &&
1161 row_info[lfr - 1].endfirsthead)
1163 while((lfr > 0) && !row_info[--lfr].endfirsthead) {
1164 row_info[lfr].endlastfoot = true;
1165 row_info[lfr].endfirsthead = false;
1167 } else if (row_info[lfr].endfoot
1168 && row_info[lfr - 1].endfoot) {
1169 while((lfr > 0) && !row_info[--lfr].endfoot) {
1170 row_info[lfr].endlastfoot = true;
1171 row_info[lfr].endfoot = false;
1173 } else if (!row_info[fr - 1].endhead
1174 && !row_info[fr - 1].endfirsthead &&
1175 !row_info[fr - 1].endfoot)
1178 !row_info[--lfr].endhead && !row_info[lfr].endfirsthead &&
1179 !row_info[lfr].endfoot)
1181 row_info[lfr].endlastfoot = true;
1183 } else if (haveLTFoot()) {
1184 endlastfoot.empty = true;
1189 void LyXTabular::ReadNew(Buffer const * buf, istream & is,
1190 LyXLex & lex, string const & l, int const version)
1194 if (!getTokenValue(line, "rows", rows_arg))
1197 if (!getTokenValue(line, "columns", columns_arg))
1199 Init(buf->params, rows_arg, columns_arg);
1200 l_getline(is, line);
1201 if (!prefixIs(line, "<features")) {
1202 lyxerr << "Wrong tabular format (expected <features ...> got" <<
1203 line << ")" << endl;
1206 getTokenValue(line, "rotate", rotate);
1207 getTokenValue(line, "islongtable", is_long_tabular);
1208 // compatibility read for old longtable options. Now we can make any
1209 // row part of the header/footer type we want before it was strict
1210 // sequential from the first row down (as LaTeX does it!). So now when
1211 // we find a header/footer line we have to go up the rows and set it
1212 // on all preceding rows till the first or one with already a h/f option
1213 // set. If we find a firstheader on the same line as a header or a
1214 // lastfooter on the same line as a footer then this should be set empty.
1222 getTokenValue(line, "endhead", hrow);
1223 getTokenValue(line, "endfirsthead", fhrow);
1224 getTokenValue(line, "endfoot", frow);
1225 getTokenValue(line, "endlastfoot", lfrow);
1226 setHeaderFooterRows(abs(hrow), abs(fhrow), abs(frow), abs(lfrow));
1228 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1229 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1230 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1231 getTokenValue(line, "headTopDL", endhead.topDL);
1232 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1233 getTokenValue(line, "footTopDL", endfoot.topDL);
1234 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1235 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1236 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1237 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1239 for (int j = 0; j < columns_; ++j) {
1241 if (!prefixIs(line,"<column")) {
1242 lyxerr << "Wrong tabular format (expected <column ...> got" <<
1243 line << ")" << endl;
1246 getTokenValue(line, "alignment", column_info[j].alignment);
1247 getTokenValue(line, "valignment", column_info[j].valignment);
1248 getTokenValue(line, "leftline", column_info[j].left_line);
1249 getTokenValue(line, "rightline", column_info[j].right_line);
1250 getTokenValue(line, "width", column_info[j].p_width);
1251 getTokenValue(line, "special", column_info[j].align_special);
1254 for (int i = 0; i < rows_; ++i) {
1255 l_getline(is, line);
1256 if (!prefixIs(line, "<row")) {
1257 lyxerr << "Wrong tabular format (expected <row ...> got" <<
1258 line << ")" << endl;
1261 getTokenValue(line, "topline", row_info[i].top_line);
1262 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1263 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1264 getTokenValue(line, "endhead", row_info[i].endhead);
1265 getTokenValue(line, "endfoot", row_info[i].endfoot);
1266 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1267 getTokenValue(line, "newpage", row_info[i].newpage);
1268 for (int j = 0; j < columns_; ++j) {
1269 l_getline(is, line);
1270 if (!prefixIs(line, "<cell")) {
1271 lyxerr << "Wrong tabular format (expected <cell ...> got" <<
1272 line << ")" << endl;
1275 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1276 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1277 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1278 getTokenValue(line, "topline", cell_info[i][j].top_line);
1279 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1280 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1281 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1282 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1283 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1284 getTokenValue(line, "width", cell_info[i][j].p_width);
1285 getTokenValue(line, "special", cell_info[i][j].align_special);
1286 l_getline(is, line);
1287 if (prefixIs(line, "\\begin_inset")) {
1288 cell_info[i][j].inset.read(buf, lex);
1289 l_getline(is, line);
1291 if (!prefixIs(line, "</cell>")) {
1292 lyxerr << "Wrong tabular format (expected </cell> got" <<
1293 line << ")" << endl;
1297 l_getline(is, line);
1298 if (!prefixIs(line, "</row>")) {
1299 lyxerr << "Wrong tabular format (expected </row> got" <<
1300 line << ")" << endl;
1304 while (!prefixIs(line, "</lyxtabular>")) {
1305 l_getline(is, line);
1307 set_row_column_number_info();
1311 void LyXTabular::OldFormatRead(BufferParams const & bp,
1312 LyXLex & lex, string const & fl)
1318 int columns_arg = 0;
1319 int is_long_tabular_arg = false;
1320 int rotate_arg = false;
1329 istream & is = lex.getStream();
1332 version = lyx::atoi(s.substr(8, string::npos));
1336 vector<int> cont_row_info;
1339 lyxerr << "Tabular format < 5 is not supported anymore\n"
1340 "Get an older version of LyX (< 1.1.x) for conversion!"
1342 Alert::alert(_("Warning:"),
1343 _("Tabular format < 5 is not supported anymore\n"),
1344 _("Get an older version of LyX (< 1.1.x) for conversion!"));
1346 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1347 >> rotate_arg >> a >> b >> c >> d;
1349 is >> rows_arg >> columns_arg;
1350 Init(bp, rows_arg, columns_arg);
1351 cont_row_info = vector<int>(rows_arg);
1352 SetLongTabular(is_long_tabular_arg);
1353 SetRotateTabular(rotate_arg);
1355 for (i = 0; i < rows_; ++i) {
1357 cont_row_info[i] = false;
1359 for (i = 0; i < columns_; ++i) {
1362 for (i = 0; i < rows_; ++i) {
1363 for (j = 0; j < columns_; ++j) {
1368 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1369 >> rotate_arg >> a >> b >> c >> d;
1370 Init(bp, rows_arg, columns_arg);
1371 cont_row_info = vector<int>(rows_arg);
1372 SetLongTabular(is_long_tabular_arg);
1373 SetRotateTabular(rotate_arg);
1374 setHeaderFooterRows(a+1, b+1 , c+1, d+1);
1375 for (i = 0; i < rows_; ++i) {
1376 a = b = c = d = e = f = g = 0;
1377 is >> a >> b >> c >> d;
1378 row_info[i].top_line = a;
1379 row_info[i].bottom_line = b;
1380 cont_row_info[i] = c;
1381 row_info[i].newpage = d;
1383 for (i = 0; i < columns_; ++i) {
1388 char ch; // skip '"'
1391 // ignore is buggy but we will use it later (Lgb)
1392 is.ignore(); // skip '"'
1394 getline(is, s1, '"');
1396 is >> ch; // skip '"'
1398 // ignore is buggy but we will use it later (Lgb)
1399 is.ignore(); // skip '"'
1401 getline(is, s2, '"');
1402 column_info[i].alignment = static_cast<LyXAlignment>(a);
1403 column_info[i].left_line = b;
1404 column_info[i].right_line = c;
1405 column_info[i].p_width = LyXLength(s1);
1406 column_info[i].align_special = s2;
1408 for (i = 0; i < rows_; ++i) {
1409 for (j = 0; j < columns_; ++j) {
1412 is >> a >> b >> c >> d >> e >> f >> g;
1415 is >> ch; // skip '"'
1417 // ignore is buggy but we will use it later (Lgb)
1418 is.ignore(); // skip '"'
1420 getline(is, s1, '"');
1422 is >> ch; // skip '"'
1424 // ignore is buggy but we will use it later (Lgb)
1425 is.ignore(); // skip '"'
1427 getline(is, s2, '"');
1428 cell_info[i][j].multicolumn = static_cast<char>(a);
1429 cell_info[i][j].alignment = static_cast<LyXAlignment>(b);
1430 cell_info[i][j].top_line = static_cast<char>(c);
1431 cell_info[i][j].bottom_line = static_cast<char>(d);
1432 cell_info[i][j].left_line = column_info[j].left_line;
1433 cell_info[i][j].right_line = column_info[j].right_line;
1434 cell_info[i][j].rotate = static_cast<bool>(f);
1435 cell_info[i][j].usebox = static_cast<BoxType>(g);
1436 cell_info[i][j].align_special = s1;
1437 cell_info[i][j].p_width = LyXLength(s2);
1441 set_row_column_number_info(true);
1443 Paragraph * par = new Paragraph;
1444 Paragraph * return_par = 0;
1448 Paragraph::depth_type depth = 0;
1449 LyXFont font(LyXFont::ALL_INHERIT);
1450 font.setLanguage(owner_->bufferOwner()->getLanguage());
1452 while (lex.isOK()) {
1454 string const token = lex.getString();
1457 if (token == "\\layout"
1458 || token == "\\end_float"
1459 || token == "\\end_deeper") {
1460 lex.pushToken(token);
1461 #ifndef NO_COMPABILITY
1462 // Here we need to insert the inset_ert_contents into the last
1463 // cell of the tabular.
1464 owner_->bufferOwner()->insertErtContents(par, pos);
1468 if (owner_->bufferOwner()->parseSingleLyXformat2Token(lex, par,
1473 lex.pushToken(token);
1477 lex.printError("New Paragraph allocated! This should not happen!");
1478 lex.pushToken(token);
1484 // now we have the par we should fill the insets with this!
1486 InsetText * inset = GetCellInset(cell);
1489 for (int i = 0; i < par->size(); ++i) {
1490 if (par->isNewline(i)) {
1492 if (cell > numberofcells) {
1493 lyxerr << "Some error in reading old table format occured!" <<
1494 endl << "Terminating when reading cell[" << cell << "]!" <<
1499 row = row_of_cell(cell);
1500 if (cont_row_info[row]) {
1502 cont_row_info.erase(cont_row_info.begin() + row); //&cont_row_info[row]);
1503 while (!IsFirstCellInRow(--cell));
1505 inset = GetCellInset(cell);
1508 inset = GetCellInset(cell);
1509 row = row_of_cell(cell);
1510 if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].usebox)
1512 // insert a space instead
1514 par->insertChar(i, ' ');
1517 par->copyIntoMinibuffer(*owner_->bufferOwner(), i);
1518 inset->paragraph()->insertFromMinibuffer(inset->paragraph()->size());
1525 bool LyXTabular::IsMultiColumn(int cell, bool real) const
1527 return ((!real || (column_of_cell(cell) != right_column_of_cell(cell))) &&
1528 (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL));
1532 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1534 int const row = row_of_cell(cell);
1535 int const column = column_of_cell(cell);
1536 return &cell_info[row][column];
1540 void LyXTabular::SetMultiColumn(int cell, int number)
1542 cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1543 cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1544 cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1545 cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1546 for (number--; number > 0; --number) {
1547 cellinfo_of_cell(cell+number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1549 set_row_column_number_info();
1553 int LyXTabular::cells_in_multicolumn(int cell) const
1555 int const row = row_of_cell(cell);
1556 int column = column_of_cell(cell);
1559 while ((column < columns_) &&
1560 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1569 int LyXTabular::UnsetMultiColumn(int cell)
1571 int const row = row_of_cell(cell);
1572 int column = column_of_cell(cell);
1576 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1577 cell_info[row][column].multicolumn = CELL_NORMAL;
1579 while ((column < columns_) &&
1580 (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1582 cell_info[row][column].multicolumn = CELL_NORMAL;
1587 set_row_column_number_info();
1592 void LyXTabular::SetLongTabular(bool what)
1594 is_long_tabular = what;
1598 bool LyXTabular::IsLongTabular() const
1600 return is_long_tabular;
1604 void LyXTabular::SetRotateTabular(bool flag)
1610 bool LyXTabular::GetRotateTabular() const
1616 void LyXTabular::SetRotateCell(int cell, bool flag)
1618 cellinfo_of_cell(cell)->rotate = flag;
1622 bool LyXTabular::GetRotateCell(int cell) const
1624 return cellinfo_of_cell(cell)->rotate;
1628 bool LyXTabular::NeedRotating() const
1632 for (int i = 0; i < rows_; ++i) {
1633 for (int j = 0; j < columns_; ++j) {
1634 if (cell_info[i][j].rotate)
1642 bool LyXTabular::IsLastCell(int cell) const
1644 if ((cell + 1) < numberofcells)
1650 int LyXTabular::GetCellAbove(int cell) const
1652 if (row_of_cell(cell) > 0)
1653 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1658 int LyXTabular::GetCellBelow(int cell) const
1660 if (row_of_cell(cell) + 1 < rows_)
1661 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1666 int LyXTabular::GetLastCellAbove(int cell) const
1668 if (row_of_cell(cell) <= 0)
1670 if (!IsMultiColumn(cell))
1671 return GetCellAbove(cell);
1672 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1676 int LyXTabular::GetLastCellBelow(int cell) const
1678 if (row_of_cell(cell) + 1 >= rows_)
1680 if (!IsMultiColumn(cell))
1681 return GetCellBelow(cell);
1682 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1686 int LyXTabular::GetCellNumber(int row, int column) const
1689 if (column >= columns_)
1690 column = columns_ - 1;
1691 else if (column < 0)
1698 lyx::Assert(column >= 0 || column < columns_ || row >= 0 || row < rows_);
1700 return cell_info[row][column].cellno;
1704 void LyXTabular::SetUsebox(int cell, BoxType type)
1706 cellinfo_of_cell(cell)->usebox = type;
1710 LyXTabular::BoxType LyXTabular::GetUsebox(int cell) const
1712 if (column_info[column_of_cell(cell)].p_width.zero() &&
1713 !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.zero()))
1715 if (cellinfo_of_cell(cell)->usebox > 1)
1716 return cellinfo_of_cell(cell)->usebox;
1717 return UseParbox(cell);
1722 // This are functions used for the longtable support
1724 void LyXTabular::SetLTHead(int row, bool flag, ltType const & hd, bool first)
1729 row_info[row].endfirsthead = flag;
1733 row_info[row].endhead = flag;
1738 bool LyXTabular::GetRowOfLTHead(int row, ltType & hd) const
1741 hd.set = haveLTHead();
1742 return row_info[row].endhead;
1746 bool LyXTabular::GetRowOfLTFirstHead(int row, ltType & hd) const
1749 hd.set = haveLTFirstHead();
1750 return row_info[row].endfirsthead;
1754 void LyXTabular::SetLTFoot(int row, bool flag, ltType const & fd, bool last)
1759 row_info[row].endlastfoot = flag;
1763 row_info[row].endfoot = flag;
1768 bool LyXTabular::GetRowOfLTFoot(int row, ltType & fd) const
1771 fd.set = haveLTFoot();
1772 return row_info[row].endfoot;
1776 bool LyXTabular::GetRowOfLTLastFoot(int row, ltType & fd) const
1779 fd.set = haveLTLastFoot();
1780 return row_info[row].endlastfoot;
1784 void LyXTabular::SetLTNewPage(int row, bool what)
1786 row_info[row].newpage = what;
1790 bool LyXTabular::GetLTNewPage(int row) const
1792 return row_info[row].newpage;
1796 bool LyXTabular::haveLTHead() const
1798 for(int i=0; i < rows_; ++i) {
1799 if (row_info[i].endhead)
1806 bool LyXTabular::haveLTFirstHead() const
1808 if (endfirsthead.empty)
1810 for(int i=0; i < rows_; ++i) {
1811 if (row_info[i].endfirsthead)
1818 bool LyXTabular::haveLTFoot() const
1820 for(int i=0; i < rows_; ++i) {
1821 if (row_info[i].endfoot)
1828 bool LyXTabular::haveLTLastFoot() const
1830 if (endlastfoot.empty)
1832 for(int i=0; i < rows_; ++i) {
1833 if (row_info[i].endlastfoot)
1840 // end longtable support functions
1842 bool LyXTabular::SetAscentOfRow(int row, int height)
1844 if ((row >= rows_) || (row_info[row].ascent_of_row == height))
1846 row_info[row].ascent_of_row = height;
1851 bool LyXTabular::SetDescentOfRow(int row, int height)
1853 if ((row >= rows_) || (row_info[row].descent_of_row == height))
1855 row_info[row].descent_of_row = height;
1860 int LyXTabular::GetAscentOfRow(int row) const
1864 return row_info[row].ascent_of_row;
1868 int LyXTabular::GetDescentOfRow(int row) const
1872 return row_info[row].descent_of_row;
1876 int LyXTabular::GetHeightOfTabular() const
1880 for (int row = 0; row < rows_; ++row)
1881 height += GetAscentOfRow(row) + GetDescentOfRow(row) +
1882 GetAdditionalHeight(row);
1887 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
1889 if ((row >= rows_) || (column >= columns_))
1891 return (cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN);
1895 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1897 if ((row < 0) || (row >= rows_))
1900 int const fcell = GetFirstCellInRow(row);
1901 int const n = NumberOfCellsInRow(fcell) + fcell;
1904 for (int i = fcell; i < n; ++i) {
1908 if (tmp == (n - fcell)) {
1911 for (int i = fcell; i < n; ++i) {
1914 << column_of_cell(i) + 1
1916 << right_column_of_cell(i) + 1
1928 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1930 if ((row < 0) || (row >= rows_))
1933 int const fcell = GetFirstCellInRow(row);
1934 int const n = NumberOfCellsInRow(fcell) + fcell;
1937 for (int i = fcell; i < n; ++i) {
1941 if (tmp == (n - fcell)) {
1944 for (int i = fcell; i < n; ++i) {
1945 if (BottomLine(i)) {
1947 << column_of_cell(i) + 1
1949 << right_column_of_cell(i) + 1
1961 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1965 if (GetRotateCell(cell)) {
1966 os << "\\begin{sideways}\n";
1969 if (IsMultiColumn(cell)) {
1970 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1971 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1972 os << cellinfo_of_cell(cell)->align_special << "}{";
1974 if (LeftLine(cell) &&
1975 (IsFirstCellInRow(cell) ||
1976 (!IsMultiColumn(cell-1) && !LeftLine(cell, true) &&
1977 !RightLine(cell-1, true))))
1981 if (!GetPWidth(cell).zero()) {
1982 switch (GetVAlignment(cell)) {
1983 case LYX_VALIGN_TOP:
1986 case LYX_VALIGN_CENTER:
1989 case LYX_VALIGN_BOTTOM:
1993 os << "{" << GetPWidth(cell).asLatexString() << '}';
1995 switch (GetAlignment(cell)) {
1996 case LYX_ALIGN_LEFT:
1999 case LYX_ALIGN_RIGHT:
2007 if (RightLine(cell))
2009 if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
2015 if (GetUsebox(cell) == BOX_PARBOX) {
2017 switch (GetVAlignment(cell)) {
2018 case LYX_VALIGN_TOP:
2021 case LYX_VALIGN_CENTER:
2024 case LYX_VALIGN_BOTTOM:
2028 os << "]{" << GetPWidth(cell).asLatexString() << "}{";
2029 } else if (GetUsebox(cell) == BOX_MINIPAGE) {
2030 os << "\\begin{minipage}[";
2031 switch (GetVAlignment(cell)) {
2032 case LYX_VALIGN_TOP:
2035 case LYX_VALIGN_CENTER:
2038 case LYX_VALIGN_BOTTOM:
2042 os << "]{" << GetPWidth(cell).asLatexString() << "}\n";
2049 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
2054 if (GetUsebox(cell) == BOX_PARBOX)
2056 else if (GetUsebox(cell) == BOX_MINIPAGE) {
2057 os << "%\n\\end{minipage}";
2060 if (IsMultiColumn(cell)) {
2063 if (GetRotateCell(cell)) {
2064 os << "%\n\\end{sideways}";
2071 int LyXTabular::TeXLongtableHeaderFooter(ostream & os, Buffer const * buf,
2072 bool fragile, bool fp) const
2074 if (!is_long_tabular)
2078 // output header info
2080 if (endhead.topDL) {
2084 for (int i = 0; i < rows_; ++i) {
2085 if (row_info[i].endhead) {
2086 ret += TeXRow(os, i, buf, fragile, fp);
2089 if (endhead.bottomDL) {
2093 os << "\\endhead\n";
2095 if (endfirsthead.empty) {
2096 os << "\\endfirsthead\n";
2100 // output firstheader info
2101 if (haveLTFirstHead()) {
2102 if (endfirsthead.topDL) {
2106 for (int i = 0; i < rows_; ++i) {
2107 if (row_info[i].endfirsthead) {
2108 ret += TeXRow(os, i, buf, fragile, fp);
2111 if (endfirsthead.bottomDL) {
2115 os << "\\endfirsthead\n";
2118 // output footer info
2120 if (endfoot.topDL) {
2124 for (int i = 0; i < rows_; ++i) {
2125 if (row_info[i].endfoot) {
2126 ret += TeXRow(os, i, buf, fragile, fp);
2129 if (endfoot.bottomDL) {
2133 os << "\\endfoot\n";
2135 if (endlastfoot.empty) {
2136 os << "\\endlastfoot\n";
2140 // output lastfooter info
2141 if (haveLTLastFoot()) {
2142 if (endlastfoot.topDL) {
2146 for (int i = 0; i < rows_; ++i) {
2147 if (row_info[i].endlastfoot) {
2148 ret += TeXRow(os, i, buf, fragile, fp);
2151 if (endlastfoot.bottomDL) {
2155 os << "\\endlastfoot\n";
2162 bool LyXTabular::isValidRow(int const row) const
2164 if (!is_long_tabular)
2166 return (!row_info[row].endhead && !row_info[row].endfirsthead &&
2167 !row_info[row].endfoot && !row_info[row].endlastfoot);
2171 int LyXTabular::TeXRow(ostream & os, int const i, Buffer const * buf,
2172 bool fragile, bool fp) const
2175 int cell = GetCellNumber(i, 0);
2177 ret += TeXTopHLine(os, i);
2178 for (int j = 0; j < columns_; ++j) {
2179 if (IsPartOfMultiColumn(i,j))
2181 ret += TeXCellPreamble(os, cell);
2182 InsetText * inset = GetCellInset(cell);
2184 bool rtl = inset->paragraph()->isRightToLeftPar(buf->params) &&
2185 inset->paragraph()->size() > 0 && GetPWidth(cell).zero();
2189 ret += inset->latex(buf, os, fragile, fp);
2193 ret += TeXCellPostamble(os, cell);
2194 if (!IsLastCellInRow(cell)) { // not last cell in row
2202 ret += TeXBottomHLine(os, i);
2207 int LyXTabular::latex(Buffer const * buf,
2208 ostream & os, bool fragile, bool fp) const
2212 //+---------------------------------------------------------------------
2213 //+ first the opening preamble +
2214 //+---------------------------------------------------------------------
2217 os << "\\begin{sideways}\n";
2220 if (is_long_tabular)
2221 os << "\\begin{longtable}{";
2223 os << "\\begin{tabular}{";
2224 for (int i = 0; i < columns_; ++i) {
2225 if (!column_info[i].align_special.empty()) {
2226 os << column_info[i].align_special;
2228 if (column_info[i].left_line)
2230 if (!column_info[i].p_width.zero()) {
2231 switch (column_info[i].valignment) {
2232 case LYX_VALIGN_TOP:
2235 case LYX_VALIGN_CENTER:
2238 case LYX_VALIGN_BOTTOM:
2243 << column_info[i].p_width.asLatexString()
2246 switch (column_info[i].alignment) {
2247 case LYX_ALIGN_LEFT:
2250 case LYX_ALIGN_RIGHT:
2258 if (column_info[i].right_line)
2265 ret += TeXLongtableHeaderFooter(os, buf, fragile, fp);
2267 //+---------------------------------------------------------------------
2268 //+ the single row and columns (cells) +
2269 //+---------------------------------------------------------------------
2271 for (int i = 0; i < rows_; ++i) {
2272 if (isValidRow(i)) {
2273 ret += TeXRow(os, i, buf, fragile, fp);
2274 if (is_long_tabular && row_info[i].newpage) {
2275 os << "\\newpage\n";
2281 //+---------------------------------------------------------------------
2282 //+ the closing of the tabular +
2283 //+---------------------------------------------------------------------
2285 if (is_long_tabular)
2286 os << "\\end{longtable}";
2288 os << "\\end{tabular}";
2290 os << "\n\\end{sideways}";
2298 int LyXTabular::docbookRow(Buffer const * buf, ostream & os, int row) const
2301 int cell = GetFirstCellInRow(row);
2304 for (int j = 0; j < columns_; ++j) {
2305 if (IsPartOfMultiColumn(row, j))
2308 os << "<entry align=\"";
2309 switch (GetAlignment(cell)) {
2310 case LYX_ALIGN_LEFT:
2313 case LYX_ALIGN_RIGHT:
2321 os << "\" valign=\"";
2322 switch (GetVAlignment(cell)) {
2323 case LYX_VALIGN_TOP:
2326 case LYX_VALIGN_BOTTOM:
2329 case LYX_VALIGN_CENTER:
2334 if (IsMultiColumn(cell)) {
2335 os << " namest=\"col" << j << "\" ";
2336 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< "\"";
2340 ret += GetCellInset(cell)->docbook(buf, os);
2349 int LyXTabular::docBook(Buffer const * buf, ostream & os) const
2353 //+---------------------------------------------------------------------
2354 //+ first the opening preamble +
2355 //+---------------------------------------------------------------------
2357 os << "<tgroup cols=\"" << columns_
2358 << "\" colsep=\"1\" rowsep=\"1\">\n";
2360 for (int i = 0; i < columns_; ++i) {
2361 os << "<colspec colname=\"col" << i << "\" align=\"";
2362 switch (column_info[i].alignment) {
2363 case LYX_ALIGN_LEFT:
2366 case LYX_ALIGN_RIGHT:
2377 //+---------------------------------------------------------------------
2378 //+ Long Tabular case +
2379 //+---------------------------------------------------------------------
2381 // output header info
2382 if (haveLTHead() || haveLTFirstHead()) {
2385 for (int i = 0; i < rows_; ++i) {
2386 if (row_info[i].endhead || row_info[i].endfirsthead) {
2387 ret += docbookRow(buf, os, i);
2393 // output footer info
2394 if (haveLTFoot() || haveLTLastFoot()) {
2397 for (int i = 0; i < rows_; ++i) {
2398 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2399 ret += docbookRow(buf, os, i);
2406 //+---------------------------------------------------------------------
2407 //+ the single row and columns (cells) +
2408 //+---------------------------------------------------------------------
2412 for (int i = 0; i < rows_; ++i) {
2413 if (isValidRow(i)) {
2414 ret += docbookRow(buf, os, i);
2419 //+---------------------------------------------------------------------
2420 //+ the closing of the tabular +
2421 //+---------------------------------------------------------------------
2430 // ASCII export function and helpers
2432 int LyXTabular::asciiTopHLine(ostream & os, int row,
2433 vector<unsigned int> const & clen) const
2435 int const fcell = GetFirstCellInRow(row);
2436 int const n = NumberOfCellsInRow(fcell) + fcell;
2439 for (int i = fcell; i < n; ++i) {
2449 for (int i = fcell; i < n; ++i) {
2460 int column = column_of_cell(i);
2461 int len = clen[column];
2462 while (IsPartOfMultiColumn(row, ++column))
2463 len += clen[column] + 4;
2464 os << string(len, ch);
2479 int LyXTabular::asciiBottomHLine(ostream & os, int row,
2480 vector<unsigned int> const & clen) const
2482 int const fcell = GetFirstCellInRow(row);
2483 int const n = NumberOfCellsInRow(fcell) + fcell;
2486 for (int i = fcell; i < n; ++i) {
2487 if (BottomLine(i)) {
2496 for (int i = fcell; i < n; ++i) {
2497 if (BottomLine(i)) {
2507 int column = column_of_cell(i);
2508 int len = clen[column];
2509 while (IsPartOfMultiColumn(row, ++column))
2510 len += clen[column] + 4;
2511 os << string(len, ch);
2512 if (BottomLine(i)) {
2526 int LyXTabular::asciiPrintCell(Buffer const * buf, ostream & os,
2527 int cell, int row, int column,
2528 vector<unsigned int> const & clen,
2529 bool onlydata) const
2532 int ret = GetCellInset(cell)->ascii(buf, sstr, 0);
2544 unsigned int len1 = sstr.str().length();
2545 unsigned int len2 = clen[column];
2546 while (IsPartOfMultiColumn(row, ++column))
2547 len2 += clen[column] + 4;
2550 switch (GetAlignment(cell)) {
2552 case LYX_ALIGN_LEFT:
2555 case LYX_ALIGN_RIGHT:
2559 case LYX_ALIGN_CENTER:
2565 for (unsigned int i = 0; i < len1; ++i)
2568 for (unsigned int i = 0; i < len2; ++i)
2570 if (RightLine(cell))
2579 int LyXTabular::ascii(Buffer const * buf, ostream & os, int const depth,
2580 bool onlydata, unsigned char delim) const
2584 //+---------------------------------------------------------------------
2585 //+ first calculate the width of the single columns +
2586 //+---------------------------------------------------------------------
2587 vector<unsigned int> clen(columns_);
2590 // first all non (real) multicolumn cells!
2591 for (int j = 0; j < columns_; ++j) {
2593 for (int i = 0; i < rows_; ++i) {
2594 int cell = GetCellNumber(i, j);
2595 if (IsMultiColumn(cell, true))
2598 GetCellInset(cell)->ascii(buf, sstr, 0);
2599 if (clen[j] < sstr.str().length())
2600 clen[j] = sstr.str().length();
2603 // then all (real) multicolumn cells!
2604 for (int j = 0; j < columns_; ++j) {
2605 for (int i = 0; i < rows_; ++i) {
2606 int cell = GetCellNumber(i, j);
2607 if (!IsMultiColumn(cell, true) || IsPartOfMultiColumn(i, j))
2610 GetCellInset(cell)->ascii(buf, sstr, 0);
2611 int len = int(sstr.str().length());
2612 int const n = cells_in_multicolumn(cell);
2613 for (int k = j; (len > 0) && (k < (j + n - 1)); ++k)
2615 if (len > int(clen[j + n - 1]))
2616 clen[j + n - 1] = len;
2621 for (int i = 0; i < rows_; ++i) {
2623 if (asciiTopHLine(os, i, clen)) {
2624 for (int j = 0; j < depth; ++j)
2628 for (int j = 0; j < columns_; ++j) {
2629 if (IsPartOfMultiColumn(i,j))
2631 if (onlydata && j > 0)
2633 ret += asciiPrintCell(buf, os, cell, i, j, clen, onlydata);
2638 for (int j = 0; j < depth; ++j)
2640 if (asciiBottomHLine(os, i, clen)) {
2641 for (int j = 0; j < depth; ++j)
2653 InsetText * LyXTabular::GetCellInset(int cell) const
2656 return & cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2660 InsetText * LyXTabular::GetCellInset(int row, int column) const
2662 cur_cell = GetCellNumber(row, column);
2663 return & cell_info[row][column].inset;
2667 int LyXTabular::GetCellFromInset(Inset const * inset, int maybe_cell) const
2669 // is this inset part of the tabular?
2670 if (!inset || inset->owner() != owner_) {
2671 lyxerr[Debug::INSETTEXT]
2672 << "this is not a cell of the tabular!" << endl;
2676 const int save_cur_cell = cur_cell;
2677 int cell = cur_cell;
2678 if (GetCellInset(cell) != inset) {
2680 if (cell == -1 || GetCellInset(cell) != inset) {
2686 for (cell = GetNumberOfCells(); cell >= 0; --cell) {
2687 if (GetCellInset(cell) == inset)
2690 lyxerr[Debug::INSETTEXT]
2691 << "LyXTabular::GetCellFromInset: "
2693 << ", cur_cell=" << save_cur_cell
2694 << ", maybe_cell=" << maybe_cell
2696 // We should have found a cell at this point
2698 lyxerr << "LyXTabular::GetCellFromInset: "
2699 << "Cell not found!" << endl;
2707 void LyXTabular::Validate(LaTeXFeatures & features) const
2709 if (IsLongTabular())
2710 features.require("longtable");
2712 features.require("rotating");
2713 for (int cell = 0; cell < numberofcells; ++cell) {
2714 if (GetVAlignment(cell) != LYX_VALIGN_TOP)
2715 features.require("array");
2716 GetCellInset(cell)->validate(features);
2721 vector<string> const LyXTabular::getLabelList() const
2723 vector<string> label_list;
2724 for (int i = 0; i < rows_; ++i)
2725 for (int j = 0; j < columns_; ++j) {
2726 vector<string> const l =
2727 GetCellInset(i, j)->getLabelList();
2728 label_list.insert(label_list.end(),
2729 l.begin(), l.end());
2735 LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
2737 Paragraph * par = GetCellInset(cell)->paragraph();
2739 for (; par; par = par->next()) {
2740 for (int i = 0; i < par->size(); ++i) {
2741 if (par->getChar(i) == Paragraph::META_NEWLINE)