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"
38 #include "lyxtextclasslist.h"
50 #ifndef CXX_GLOBAL_CSTD
56 int const WIDTH_OF_LINE = 5;
60 /// Define a few methods for the inner structs
62 LyXTabular::cellstruct::cellstruct(BufferParams const & bg)
67 multicolumn = LyXTabular::CELL_NORMAL;
68 alignment = LYX_ALIGN_CENTER;
69 valignment = LYX_VALIGN_TOP;
79 LyXTabular::rowstruct::rowstruct()
93 LyXTabular::columnstruct::columnstruct()
97 alignment = LYX_ALIGN_CENTER;
98 valignment = LYX_VALIGN_TOP;
103 LyXTabular::lttype::lttype()
112 LyXTabular::LyXTabular(BufferParams const & bp,
113 InsetTabular * inset, int rows_arg, int columns_arg)
117 Init(bp, rows_arg, columns_arg);
121 LyXTabular::LyXTabular(BufferParams const & bp,
122 InsetTabular * inset, LyXTabular const & lt,
127 Init(bp, lt.rows_, lt.columns_, <);
128 // we really should change again to have InsetText as a pointer
129 // and allocate it then we would not have to do this stuff all
132 for (int i = 0; i < rows_; ++i) {
133 for (int j = 0; j < columns_; ++j) {
134 cell_info[i][j].inset.id(lt.cell_info[i][j].inset.id());
135 cell_info[i][j].inset.setParagraphData(lt.cell_info[i][j].inset.paragraph(),true);
141 #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)
148 LyXTabular::LyXTabular(Buffer const * buf, InsetTabular * inset, LyXLex & lex)
156 LyXTabular & LyXTabular::operator=(LyXTabular const & lt)
159 #warning This while method should look like this: (Lgb)
164 // If this and lt is not of the same size we have a serious bug
165 // So then it is ok to throw an exception, or for now
167 lyx::Assert(rows_ == lt.rows_ && columns_ == lt.columns_);
169 cell_info = lt.cell_info;
170 row_info = lt.row_info;
171 column_info = lt.column_info;
172 SetLongTabular(lt.is_long_tabular);
181 LyXTabular * LyXTabular::clone(BufferParams const & bp,
182 InsetTabular * inset, bool same_id)
184 LyXTabular * result = new LyXTabular(bp, inset, *this, same_id);
186 // don't know if this is good but I need to Clone also
187 // the text-insets here, this is for the Undo-facility!
188 for (int i = 0; i < rows_; ++i) {
189 for (int j = 0; j < columns_; ++j) {
190 result->cell_info[i][j].inset = cell_info[i][j].inset;
191 result->cell_info[i][j].inset.setOwner(inset);
199 /* activates all lines and sets all widths to 0 */
200 void LyXTabular::Init(BufferParams const & bp,
201 int rows_arg, int columns_arg, LyXTabular const * lt)
204 columns_ = columns_arg;
205 row_info = row_vector(rows_, rowstruct());
206 column_info = column_vector(columns_, columnstruct());
207 cell_info = cell_vvector(rows_, cell_vector(columns_, cellstruct(bp)));
215 for (int i = 0; i < rows_; ++i) {
216 for (int j = 0; j < columns_; ++j) {
217 cell_info[i][j].inset.setOwner(owner_);
218 cell_info[i][j].inset.setDrawFrame(0, InsetText::LOCKED);
219 cell_info[i][j].cellno = cellno++;
221 cell_info[i].back().right_line = true;
223 row_info.back().bottom_line = true;
224 row_info.front().bottom_line = true;
226 for (int i = 0; i < columns_; ++i) {
227 calculate_width_of_column(i);
229 column_info.back().right_line = true;
231 calculate_width_of_tabular();
233 rowofcell = vector<int>();
234 columnofcell = vector<int>();
235 set_row_column_number_info();
236 is_long_tabular = false;
241 void LyXTabular::AppendRow(BufferParams const & bp, int cell)
245 int row = row_of_cell(cell);
247 row_vector::iterator rit = row_info.begin() + row;
248 row_info.insert(rit, rowstruct());
249 // now set the values of the row before
250 row_info[row] = row_info[row + 1];
253 cell_vvector::iterator cit = cell_info.begin() + row;
254 cell_info.insert(cit, vector<cellstruct>(columns_, cellstruct(bp)));
256 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
259 for (int i = 0; i <= row; ++i) {
260 for (int j = 0; j < columns_; ++j) {
261 c_info[i][j] = cell_info[i][j];
264 for (int i = row + 1; i < rows_; ++i) {
265 for (int j = 0; j < columns_; ++j) {
266 c_info[i][j] = cell_info[i-1][j];
271 for (int j = 0; j < columns_; ++j) {
272 cell_info[row][j].inset.clear();
279 void LyXTabular::DeleteRow(int row)
281 if (rows_ == 1) return; // Not allowed to delete last row
283 row_info.erase(row_info.begin() + row); //&row_info[row]);
284 cell_info.erase(cell_info.begin() + row); //&cell_info[row]);
290 void LyXTabular::AppendColumn(BufferParams const & bp, int cell)
294 cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
296 int const column = column_of_cell(cell);
297 column_vector::iterator cit = column_info.begin() + column + 1;
298 column_info.insert(cit, columnstruct());
299 // set the column values of the column before
300 column_info[column + 1] = column_info[column];
302 for (int i = 0; i < rows_; ++i) {
303 for (int j = 0; j <= column; ++j) {
304 c_info[i][j] = cell_info[i][j];
306 for (int j = column + 1; j < columns_; ++j) {
307 c_info[i][j] = cell_info[i][j - 1];
309 // care about multicolumns
310 if (c_info[i][column + 1].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
312 c_info[i][column + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
314 if ((column + 2) >= columns_ ||
315 c_info[i][column + 2].multicolumn != CELL_PART_OF_MULTICOLUMN)
317 c_info[i][column + 1].multicolumn = LyXTabular::CELL_NORMAL;
322 for (int i = 0; i < rows_; ++i) {
323 //cell_info[i][column].inset.clear();
324 cell_info[i][column + 1].inset.clear();
330 void LyXTabular::DeleteColumn(int column)
332 // Similar to DeleteRow
333 //if (!(columns_ - 1))
335 if (columns_ == 1) return; // Not allowed to delete last column
337 column_info.erase(column_info.begin() + column);
338 for (int i = 0; i < rows_; ++i) {
339 cell_info[i].erase(cell_info[i].begin() + column);
346 void LyXTabular::reinit()
352 void LyXTabular::Reinit(bool reset_widths)
355 for (int i = 0; i < rows_; ++i) {
356 for (int j = 0; j < columns_; ++j) {
357 cell_info[i][j].width_of_cell = 0;
358 cell_info[i][j].inset.setOwner(owner_);
363 for (int i = 0; i < columns_; ++i) {
364 calculate_width_of_column(i);
366 calculate_width_of_tabular();
368 set_row_column_number_info();
372 void LyXTabular::set_row_column_number_info(bool oldformat)
375 for (int row = 0; row < rows_; ++row) {
376 for (int column = 0; column<columns_; ++column) {
377 if (cell_info[row][column].multicolumn
378 != LyXTabular::CELL_PART_OF_MULTICOLUMN)
380 cell_info[row][column].cellno = numberofcells;
383 ++numberofcells; // because this is one more than as we start from 0
385 rowofcell.resize(numberofcells);
386 columnofcell.resize(numberofcells);
388 for (int row = 0, column = 0, c = 0;
389 c < numberofcells && row < rows_ && column < columns_;) {
391 columnofcell[c] = column;
395 } while (column < columns_ &&
396 cell_info[row][column].multicolumn
397 == LyXTabular::CELL_PART_OF_MULTICOLUMN);
398 if (column == columns_) {
404 for (int row = 0; row < rows_; ++row) {
405 for (int column = 0; column < columns_; ++column) {
406 if (IsPartOfMultiColumn(row,column))
408 // now set the right line of multicolumns right for oldformat read
410 cell_info[row][column].multicolumn==CELL_BEGIN_OF_MULTICOLUMN)
412 int cn=cells_in_multicolumn(cell_info[row][column].cellno);
413 cell_info[row][column].right_line =
414 cell_info[row][column+cn-1].right_line;
416 cell_info[row][column].inset.setAutoBreakRows(
417 !GetPWidth(GetCellNumber(row, column)).zero());
423 int LyXTabular::GetNumberOfCells() const
425 return numberofcells;
429 int LyXTabular::NumberOfCellsInRow(int cell) const
431 int const row = row_of_cell(cell);
433 for (int i = 0; i < columns_; ++i) {
434 if (cell_info[row][i].multicolumn != LyXTabular::CELL_PART_OF_MULTICOLUMN)
441 /* returns 1 if there is a topline, returns 0 if not */
442 bool LyXTabular::TopLine(int cell, bool onlycolumn) const
444 int const row = row_of_cell(cell);
446 if (!onlycolumn && IsMultiColumn(cell))
447 return cellinfo_of_cell(cell)->top_line;
448 return row_info[row].top_line;
452 bool LyXTabular::BottomLine(int cell, bool onlycolumn) const
454 // no bottom line underneath non-existent cells if you please
455 // Isn't that a programming error? Is so this should
456 // be an Assert instead. (Lgb)
457 if (cell >= numberofcells)
460 if (!onlycolumn && IsMultiColumn(cell))
461 return cellinfo_of_cell(cell)->bottom_line;
462 return row_info[row_of_cell(cell)].bottom_line;
466 bool LyXTabular::LeftLine(int cell, bool onlycolumn) const
468 if (!onlycolumn && IsMultiColumn(cell)) {
469 #ifdef SPECIAL_COLUM_HANDLING
470 if (cellinfo_of_cell(cell)->align_special.empty())
471 return cellinfo_of_cell(cell)->left_line;
472 return prefixIs(frontStrip(cellinfo_of_cell(cell)->align_special), "|");
474 return cellinfo_of_cell(cell)->left_line;
477 #ifdef SPECIAL_COLUM_HANDLING
478 if (column_info[column_of_cell(cell)].align_special.empty())
479 return column_info[column_of_cell(cell)].left_line;
480 return prefixIs(frontStrip(column_info[column_of_cell(cell)].align_special), "|");
482 return column_info[column_of_cell(cell)].left_line;
487 bool LyXTabular::RightLine(int cell, bool onlycolumn) const
489 if (!onlycolumn && IsMultiColumn(cell)) {
490 #ifdef SPECIAL_COLUM_HANDLING
491 if (cellinfo_of_cell(cell)->align_special.empty())
492 return cellinfo_of_cell(cell)->right_line;
493 return suffixIs(strip(cellinfo_of_cell(cell)->align_special), "|");
495 return cellinfo_of_cell(cell)->right_line;
498 #ifdef SPECIAL_COLUM_HANDLING
499 if (column_info[column_of_cell(cell)].align_special.empty())
500 return column_info[right_column_of_cell(cell)].right_line;
501 return suffixIs(strip(column_info[column_of_cell(cell)].align_special), "|");
503 return column_info[right_column_of_cell(cell)].right_line;
508 bool LyXTabular::TopAlreadyDrawed(int cell) const
510 int row = row_of_cell(cell);
511 if ((row > 0) && !GetAdditionalHeight(row)) {
512 int column = column_of_cell(cell);
515 && cell_info[row][column].multicolumn
516 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
518 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
519 return row_info[row].bottom_line;
521 return cell_info[row][column].bottom_line;
527 bool LyXTabular::LeftAlreadyDrawed(int cell) const
529 int column = column_of_cell(cell);
531 int row = row_of_cell(cell);
533 (cell_info[row][column].multicolumn ==
534 LyXTabular::CELL_PART_OF_MULTICOLUMN));
535 if (GetAdditionalWidth(cell_info[row][column].cellno))
537 #ifdef SPECIAL_COLUM_HANDLING
538 return column_info[column].right_line;
540 return RightLine(cell_info[row][column].cellno, true);
547 bool LyXTabular::IsLastRow(int cell) const
549 return (row_of_cell(cell) == rows_ - 1);
553 int LyXTabular::GetAdditionalHeight(int row) const
555 if (!row || row >= rows_)
561 for (int column = 0; column < columns_ && bottom; ++column) {
562 switch (cell_info[row - 1][column].multicolumn) {
563 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
564 bottom = cell_info[row - 1][column].bottom_line;
566 case LyXTabular::CELL_NORMAL:
567 bottom = row_info[row - 1].bottom_line;
570 for (int column = 0; column < columns_ && top; ++column) {
571 switch (cell_info[row][column].multicolumn) {
572 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
573 top = cell_info[row][column].top_line;
575 case LyXTabular::CELL_NORMAL:
576 top = row_info[row].top_line;
580 return WIDTH_OF_LINE;
585 int LyXTabular::GetAdditionalWidth(int cell) const
587 // internally already set in SetWidthOfCell
588 // used to get it back in text.C
589 int const col = right_column_of_cell(cell);
590 int const row = row_of_cell(cell);
591 if (col < columns_ - 1 && RightLine(cell, true) &&
592 LeftLine(cell_info[row][col+1].cellno, true)) // column_info[col+1].left_line)
594 return WIDTH_OF_LINE;
601 // returns the maximum over all rows
602 int LyXTabular::GetWidthOfColumn(int cell) const
604 int const column1 = column_of_cell(cell);
605 int const column2 = right_column_of_cell(cell);
607 for (int i = column1; i <= column2; ++i) {
608 result += column_info[i].width_of_column;
614 int LyXTabular::GetWidthOfTabular() const
616 return width_of_tabular;
620 /* returns 1 if a complete update is necessary, otherwise 0 */
621 bool LyXTabular::SetWidthOfMulticolCell(int cell, int new_width)
623 if (!IsMultiColumn(cell))
626 int const row = row_of_cell(cell);
627 int const column1 = column_of_cell(cell);
628 int const column2 = right_column_of_cell(cell);
629 int const old_val = cell_info[row][column2].width_of_cell;
631 // first set columns to 0 so we can calculate the right width
632 for (int i = column1; i <= column2; ++i) {
633 cell_info[row][i].width_of_cell = 0;
635 // set the width to MAX_WIDTH until width > 0
636 int width = (new_width + 2 * WIDTH_OF_LINE);
638 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
639 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
640 width -= column_info[i].width_of_column;
643 cell_info[row][i].width_of_cell = width;
645 if (old_val != cell_info[row][column2].width_of_cell) {
646 // in this case we have to recalculate all multicolumn cells which
647 // have this column as one of theirs but not as last one
648 calculate_width_of_column_NMC(i);
649 recalculateMulticolumnsOfColumn(i);
650 calculate_width_of_column(i);
656 void LyXTabular::recalculateMulticolumnsOfColumn(int column)
658 // the last column does not have to be recalculated because all
659 // multicolumns will have here there last multicolumn cell which
660 // always will have the whole rest of the width of the cell.
661 if (column > (columns_ - 2))
663 for(int row = 0; row < rows_; ++row) {
664 int mc = cell_info[row][column].multicolumn;
665 int nmc = cell_info[row][column+1].multicolumn;
666 // we only have to update multicolumns which do not have this
667 // column as their last column!
668 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
669 ((mc == CELL_PART_OF_MULTICOLUMN) &&
670 (nmc == CELL_PART_OF_MULTICOLUMN)))
672 int const cellno = cell_info[row][column].cellno;
673 SetWidthOfMulticolCell(cellno,
674 GetWidthOfCell(cellno)-(2 * WIDTH_OF_LINE));
680 /* returns 1 if a complete update is necessary, otherwise 0 */
681 bool LyXTabular::SetWidthOfCell(int cell, int new_width)
683 int const row = row_of_cell(cell);
684 int const column1 = column_of_cell(cell);
689 #ifdef SPECIAL_COLUM_HANDLING
690 if (RightLine(cell_info[row][column1].cellno, true) &&
691 (column1 < columns_-1) &&
692 LeftLine(cell_info[row][column1+1].cellno, true))
694 if (column_info[column1].right_line && (column1 < columns_-1) &&
695 column_info[column1+1].left_line) // additional width
699 add_width = WIDTH_OF_LINE;
701 if (GetWidthOfCell(cell) == (new_width+2*WIDTH_OF_LINE+add_width)) {
704 if (IsMultiColumn(cell, true)) {
705 tmp = SetWidthOfMulticolCell(cell, new_width);
707 width = (new_width + 2*WIDTH_OF_LINE + add_width);
708 cell_info[row][column1].width_of_cell = width;
709 tmp = calculate_width_of_column_NMC(column1);
711 recalculateMulticolumnsOfColumn(column1);
714 for (int i = 0; i < columns_; ++i)
715 calculate_width_of_column(i);
716 calculate_width_of_tabular();
723 bool LyXTabular::SetAlignment(int cell, LyXAlignment align, bool onlycolumn)
725 if (!IsMultiColumn(cell) || onlycolumn)
726 column_info[column_of_cell(cell)].alignment = align;
728 cellinfo_of_cell(cell)->alignment = align;
733 bool LyXTabular::SetVAlignment(int cell, VAlignment align, bool onlycolumn)
735 if (!IsMultiColumn(cell) || onlycolumn)
736 column_info[column_of_cell(cell)].valignment = align;
738 cellinfo_of_cell(cell)->valignment = align;
743 bool LyXTabular::SetColumnPWidth(int cell, LyXLength const & width)
745 bool flag = !width.zero();
746 int const j = column_of_cell(cell);
748 column_info[j].p_width = width;
749 if (flag) // do this only if there is a width
750 SetAlignment(cell, LYX_ALIGN_LEFT);
751 for (int i = 0; i < rows_; ++i) {
752 int c = GetCellNumber(i, j);
753 flag = !GetPWidth(c).zero(); // because of multicolumns!
754 GetCellInset(c)->setAutoBreakRows(flag);
760 bool LyXTabular::SetMColumnPWidth(int cell, LyXLength const & width)
762 bool const flag = !width.zero();
764 cellinfo_of_cell(cell)->p_width = width;
765 if (IsMultiColumn(cell)) {
766 GetCellInset(cell)->setAutoBreakRows(flag);
773 bool LyXTabular::SetAlignSpecial(int cell, string const & special,
774 LyXTabular::Feature what)
776 if (what == SET_SPECIAL_MULTI)
777 cellinfo_of_cell(cell)->align_special = special;
779 column_info[column_of_cell(cell)].align_special = special;
784 bool LyXTabular::SetAllLines(int cell, bool line)
786 SetTopLine(cell, line);
787 SetBottomLine(cell, line);
788 SetRightLine(cell, line);
789 SetLeftLine(cell, line);
794 bool LyXTabular::SetTopLine(int cell, bool line, bool onlycolumn)
796 int const row = row_of_cell(cell);
798 if (onlycolumn || !IsMultiColumn(cell))
799 row_info[row].top_line = line;
801 cellinfo_of_cell(cell)->top_line = line;
806 bool LyXTabular::SetBottomLine(int cell, bool line, bool onlycolumn)
808 if (onlycolumn || !IsMultiColumn(cell))
809 row_info[row_of_cell(cell)].bottom_line = line;
811 cellinfo_of_cell(cell)->bottom_line = line;
816 bool LyXTabular::SetLeftLine(int cell, bool line, bool onlycolumn)
818 if (onlycolumn || !IsMultiColumn(cell))
819 column_info[column_of_cell(cell)].left_line = line;
821 cellinfo_of_cell(cell)->left_line = line;
826 bool LyXTabular::SetRightLine(int cell, bool line, bool onlycolumn)
828 if (onlycolumn || !IsMultiColumn(cell))
829 column_info[right_column_of_cell(cell)].right_line = line;
831 cellinfo_of_cell(cell)->right_line = line;
836 LyXAlignment LyXTabular::GetAlignment(int cell, bool onlycolumn) const
838 if (!onlycolumn && IsMultiColumn(cell))
839 return cellinfo_of_cell(cell)->alignment;
841 return column_info[column_of_cell(cell)].alignment;
845 LyXTabular::VAlignment
846 LyXTabular::GetVAlignment(int cell, bool onlycolumn) const
848 if (!onlycolumn && IsMultiColumn(cell))
849 return cellinfo_of_cell(cell)->valignment;
851 return column_info[column_of_cell(cell)].valignment;
855 LyXLength const LyXTabular::GetPWidth(int cell) const
857 if (IsMultiColumn(cell))
858 return cellinfo_of_cell(cell)->p_width;
859 return column_info[column_of_cell(cell)].p_width;
863 LyXLength const LyXTabular::GetColumnPWidth(int cell) const
865 return column_info[column_of_cell(cell)].p_width;
869 LyXLength const LyXTabular::GetMColumnPWidth(int cell) const
871 if (IsMultiColumn(cell))
872 return cellinfo_of_cell(cell)->p_width;
877 string const LyXTabular::GetAlignSpecial(int cell, int what) const
879 if (what == SET_SPECIAL_MULTI)
880 return cellinfo_of_cell(cell)->align_special;
881 return column_info[column_of_cell(cell)].align_special;
885 int LyXTabular::GetWidthOfCell(int cell) const
887 int const row = row_of_cell(cell);
888 int const column1 = column_of_cell(cell);
889 int const column2 = right_column_of_cell(cell);
891 for (int i = column1; i <= column2; ++i) {
892 result += cell_info[row][i].width_of_cell;
898 int LyXTabular::GetBeginningOfTextInCell(int cell) const
902 switch (GetAlignment(cell)) {
903 case LYX_ALIGN_CENTER:
904 x += (GetWidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
906 case LYX_ALIGN_RIGHT:
907 x += GetWidthOfColumn(cell) - GetWidthOfCell(cell);
908 // + GetAdditionalWidth(cell);
910 default: /* LYX_ALIGN_LEFT: nothing :-) */
920 bool LyXTabular::IsFirstCellInRow(int cell) const
922 return column_of_cell(cell) == 0;
926 int LyXTabular::GetFirstCellInRow(int row) const
930 return cell_info[row][0].cellno;
933 bool LyXTabular::IsLastCellInRow(int cell) const
935 return (right_column_of_cell(cell) == (columns_ - 1));
939 int LyXTabular::GetLastCellInRow(int row) const
943 return cell_info[row][columns_-1].cellno;
947 bool LyXTabular::calculate_width_of_column(int column)
949 int const old_column_width = column_info[column].width_of_column;
952 for (int i = 0; i < rows_; ++i) {
953 maximum = max(cell_info[i][column].width_of_cell, maximum);
955 column_info[column].width_of_column = maximum;
956 return (column_info[column].width_of_column != old_column_width);
961 // Calculate the columns regarding ONLY the normal cells and if this
962 // column is inside a multicolumn cell then use it only if its the last
963 // column of this multicolumn cell as this gives an added with to the
964 // column, all the rest should be adapted!
966 bool LyXTabular::calculate_width_of_column_NMC(int column)
968 int const old_column_width = column_info[column].width_of_column;
970 for (int i = 0; i < rows_; ++i) {
971 int cell = GetCellNumber(i, column);
972 bool ismulti = IsMultiColumn(cell, true);
973 if ((!ismulti || (column == right_column_of_cell(cell))) &&
974 (cell_info[i][column].width_of_cell > max))
976 max = cell_info[i][column].width_of_cell;
979 column_info[column].width_of_column = max;
980 return (column_info[column].width_of_column != old_column_width);
984 void LyXTabular::calculate_width_of_tabular()
986 width_of_tabular = 0;
987 for (int i = 0; i < columns_; ++i) {
988 width_of_tabular += column_info[i].width_of_column;
993 int LyXTabular::row_of_cell(int cell) const
995 if (cell >= numberofcells)
999 return rowofcell[cell];
1003 int LyXTabular::column_of_cell(int cell) const
1005 if (cell >= numberofcells)
1006 return columns_ - 1;
1009 return columnofcell[cell];
1013 int LyXTabular::right_column_of_cell(int cell) const
1015 int const row = row_of_cell(cell);
1016 int column = column_of_cell(cell);
1017 while (column < (columns_ - 1) &&
1018 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
1024 void LyXTabular::Write(Buffer const * buf, ostream & os) const
1028 << write_attribute("version", 3)
1029 << write_attribute("rows", rows_)
1030 << write_attribute("columns", columns_)
1032 // global longtable options
1034 << write_attribute("rotate", rotate)
1035 << write_attribute("islongtable", is_long_tabular)
1036 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1037 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1038 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1039 << write_attribute("headTopDL", endhead.topDL)
1040 << write_attribute("headBottomDL", endhead.bottomDL)
1041 << write_attribute("footTopDL", endfoot.topDL)
1042 << write_attribute("footBottomDL", endfoot.bottomDL)
1043 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1044 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1045 << write_attribute("lastFootEmpty", endlastfoot.empty)
1047 for (int j = 0; j < columns_; ++j) {
1049 << write_attribute("alignment", column_info[j].alignment)
1050 << write_attribute("valignment", column_info[j].valignment)
1051 << write_attribute("leftline", column_info[j].left_line)
1052 << write_attribute("rightline", column_info[j].right_line)
1053 << write_attribute("width", column_info[j].p_width.asString())
1054 << write_attribute("special", column_info[j].align_special)
1057 for (int i = 0; i < rows_; ++i) {
1059 << write_attribute("topline", row_info[i].top_line)
1060 << write_attribute("bottomline", row_info[i].bottom_line)
1061 << write_attribute("endhead", row_info[i].endhead)
1062 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1063 << write_attribute("endfoot", row_info[i].endfoot)
1064 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1065 << write_attribute("newpage", row_info[i].newpage)
1067 for (int j = 0; j < columns_; ++j) {
1069 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1070 << write_attribute("alignment", cell_info[i][j].alignment)
1071 << write_attribute("valignment", cell_info[i][j].valignment)
1072 << write_attribute("topline", cell_info[i][j].top_line)
1073 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1074 << write_attribute("leftline", cell_info[i][j].left_line)
1075 << write_attribute("rightline", cell_info[i][j].right_line)
1076 << write_attribute("rotate", cell_info[i][j].rotate)
1077 << write_attribute("usebox", cell_info[i][j].usebox)
1078 << write_attribute("width", cell_info[i][j].p_width)
1079 << write_attribute("special", cell_info[i][j].align_special)
1081 os << "\\begin_inset ";
1082 cell_info[i][j].inset.write(buf, os);
1083 os << "\n\\end_inset \n"
1088 os << "</lyxtabular>\n";
1092 void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
1095 istream & is = lex.getStream();
1097 l_getline(is, line);
1098 if (!prefixIs(line, "<lyxtabular ")
1099 && !prefixIs(line, "<LyXTabular ")) {
1100 OldFormatRead(buf->params, lex, line);
1105 if (!getTokenValue(line, "version", version))
1108 ReadOld(buf, is, lex, line);
1109 else if (version >= 2)
1110 ReadNew(buf, is, lex, line, version);
1113 void LyXTabular::setHeaderFooterRows(int hr, int fhr, int fr, int lfr)
1117 row_info[--hr].endhead = true;
1119 // set firstheader info
1120 if (fhr && (fhr < rows_)) {
1121 if (row_info[fhr].endhead) {
1123 row_info[--fhr].endfirsthead = true;
1124 row_info[fhr].endhead = false;
1126 } else if (row_info[fhr - 1].endhead) {
1127 endfirsthead.empty = true;
1129 while((fhr > 0) && !row_info[--fhr].endhead) {
1130 row_info[fhr].endfirsthead = true;
1135 if (fr && (fr < rows_)) {
1136 if (row_info[fr].endhead && row_info[fr-1].endhead) {
1137 while((fr > 0) && !row_info[--fr].endhead) {
1138 row_info[fr].endfoot = true;
1139 row_info[fr].endhead = false;
1141 } else if (row_info[fr].endfirsthead && row_info[fr-1].endfirsthead) {
1142 while((fr > 0) && !row_info[--fr].endfirsthead) {
1143 row_info[fr].endfoot = true;
1144 row_info[fr].endfirsthead = false;
1146 } else if (!row_info[fr - 1].endhead && !row_info[fr - 1].endfirsthead) {
1147 while((fr > 0) && !row_info[--fr].endhead &&
1148 !row_info[fr].endfirsthead)
1150 row_info[fr].endfoot = true;
1154 // set lastfooter info
1155 if (lfr && (lfr < rows_)) {
1156 if (row_info[lfr].endhead && row_info[lfr - 1].endhead) {
1157 while((lfr > 0) && !row_info[--lfr].endhead) {
1158 row_info[lfr].endlastfoot = true;
1159 row_info[lfr].endhead = false;
1161 } else if (row_info[lfr].endfirsthead &&
1162 row_info[lfr - 1].endfirsthead)
1164 while((lfr > 0) && !row_info[--lfr].endfirsthead) {
1165 row_info[lfr].endlastfoot = true;
1166 row_info[lfr].endfirsthead = false;
1168 } else if (row_info[lfr].endfoot
1169 && row_info[lfr - 1].endfoot) {
1170 while((lfr > 0) && !row_info[--lfr].endfoot) {
1171 row_info[lfr].endlastfoot = true;
1172 row_info[lfr].endfoot = false;
1174 } else if (!row_info[fr - 1].endhead
1175 && !row_info[fr - 1].endfirsthead &&
1176 !row_info[fr - 1].endfoot)
1179 !row_info[--lfr].endhead && !row_info[lfr].endfirsthead &&
1180 !row_info[lfr].endfoot)
1182 row_info[lfr].endlastfoot = true;
1184 } else if (haveLTFoot()) {
1185 endlastfoot.empty = true;
1190 void LyXTabular::ReadNew(Buffer const * buf, istream & is,
1191 LyXLex & lex, string const & l, int const version)
1195 if (!getTokenValue(line, "rows", rows_arg))
1198 if (!getTokenValue(line, "columns", columns_arg))
1200 Init(buf->params, rows_arg, columns_arg);
1201 l_getline(is, line);
1202 if (!prefixIs(line, "<features")) {
1203 lyxerr << "Wrong tabular format (expected <features ...> got" <<
1204 line << ")" << endl;
1207 getTokenValue(line, "rotate", rotate);
1208 getTokenValue(line, "islongtable", is_long_tabular);
1209 // compatibility read for old longtable options. Now we can make any
1210 // row part of the header/footer type we want before it was strict
1211 // sequential from the first row down (as LaTeX does it!). So now when
1212 // we find a header/footer line we have to go up the rows and set it
1213 // on all preceding rows till the first or one with already a h/f option
1214 // set. If we find a firstheader on the same line as a header or a
1215 // lastfooter on the same line as a footer then this should be set empty.
1223 getTokenValue(line, "endhead", hrow);
1224 getTokenValue(line, "endfirsthead", fhrow);
1225 getTokenValue(line, "endfoot", frow);
1226 getTokenValue(line, "endlastfoot", lfrow);
1227 setHeaderFooterRows(abs(hrow), abs(fhrow), abs(frow), abs(lfrow));
1229 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1230 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1231 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1232 getTokenValue(line, "headTopDL", endhead.topDL);
1233 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1234 getTokenValue(line, "footTopDL", endfoot.topDL);
1235 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1236 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1237 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1238 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1240 for (int j = 0; j < columns_; ++j) {
1242 if (!prefixIs(line,"<column")) {
1243 lyxerr << "Wrong tabular format (expected <column ...> got" <<
1244 line << ")" << endl;
1247 getTokenValue(line, "alignment", column_info[j].alignment);
1248 getTokenValue(line, "valignment", column_info[j].valignment);
1249 getTokenValue(line, "leftline", column_info[j].left_line);
1250 getTokenValue(line, "rightline", column_info[j].right_line);
1251 getTokenValue(line, "width", column_info[j].p_width);
1252 getTokenValue(line, "special", column_info[j].align_special);
1255 for (int i = 0; i < rows_; ++i) {
1256 l_getline(is, line);
1257 if (!prefixIs(line, "<row")) {
1258 lyxerr << "Wrong tabular format (expected <row ...> got" <<
1259 line << ")" << endl;
1262 getTokenValue(line, "topline", row_info[i].top_line);
1263 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1264 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1265 getTokenValue(line, "endhead", row_info[i].endhead);
1266 getTokenValue(line, "endfoot", row_info[i].endfoot);
1267 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1268 getTokenValue(line, "newpage", row_info[i].newpage);
1269 for (int j = 0; j < columns_; ++j) {
1270 l_getline(is, line);
1271 if (!prefixIs(line, "<cell")) {
1272 lyxerr << "Wrong tabular format (expected <cell ...> got" <<
1273 line << ")" << endl;
1276 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1277 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1278 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1279 getTokenValue(line, "topline", cell_info[i][j].top_line);
1280 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1281 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1282 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1283 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1284 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1285 getTokenValue(line, "width", cell_info[i][j].p_width);
1286 getTokenValue(line, "special", cell_info[i][j].align_special);
1287 l_getline(is, line);
1288 if (prefixIs(line, "\\begin_inset")) {
1289 cell_info[i][j].inset.read(buf, lex);
1290 l_getline(is, line);
1292 if (!prefixIs(line, "</cell>")) {
1293 lyxerr << "Wrong tabular format (expected </cell> got" <<
1294 line << ")" << endl;
1298 l_getline(is, line);
1299 if (!prefixIs(line, "</row>")) {
1300 lyxerr << "Wrong tabular format (expected </row> got" <<
1301 line << ")" << endl;
1305 while (!prefixIs(line, "</lyxtabular>")) {
1306 l_getline(is, line);
1308 set_row_column_number_info();
1312 void LyXTabular::OldFormatRead(BufferParams const & bp,
1313 LyXLex & lex, string const & fl)
1319 int columns_arg = 0;
1320 int is_long_tabular_arg = false;
1321 int rotate_arg = false;
1330 istream & is = lex.getStream();
1333 version = lyx::atoi(s.substr(8, string::npos));
1337 vector<int> cont_row_info;
1340 lyxerr << "Tabular format < 5 is not supported anymore\n"
1341 "Get an older version of LyX (< 1.1.x) for conversion!"
1343 Alert::alert(_("Warning:"),
1344 _("Tabular format < 5 is not supported anymore\n"),
1345 _("Get an older version of LyX (< 1.1.x) for conversion!"));
1347 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1348 >> rotate_arg >> a >> b >> c >> d;
1350 is >> rows_arg >> columns_arg;
1351 Init(bp, rows_arg, columns_arg);
1352 cont_row_info = vector<int>(rows_arg);
1353 SetLongTabular(is_long_tabular_arg);
1354 SetRotateTabular(rotate_arg);
1356 for (i = 0; i < rows_; ++i) {
1358 cont_row_info[i] = false;
1360 for (i = 0; i < columns_; ++i) {
1363 for (i = 0; i < rows_; ++i) {
1364 for (j = 0; j < columns_; ++j) {
1369 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1370 >> rotate_arg >> a >> b >> c >> d;
1371 Init(bp, rows_arg, columns_arg);
1372 cont_row_info = vector<int>(rows_arg);
1373 SetLongTabular(is_long_tabular_arg);
1374 SetRotateTabular(rotate_arg);
1375 setHeaderFooterRows(a+1, b+1 , c+1, d+1);
1376 for (i = 0; i < rows_; ++i) {
1377 a = b = c = d = e = f = g = 0;
1378 is >> a >> b >> c >> d;
1379 row_info[i].top_line = a;
1380 row_info[i].bottom_line = b;
1381 cont_row_info[i] = c;
1382 row_info[i].newpage = d;
1384 for (i = 0; i < columns_; ++i) {
1389 char ch; // skip '"'
1392 // ignore is buggy but we will use it later (Lgb)
1393 is.ignore(); // skip '"'
1395 getline(is, s1, '"');
1397 is >> ch; // skip '"'
1399 // ignore is buggy but we will use it later (Lgb)
1400 is.ignore(); // skip '"'
1402 getline(is, s2, '"');
1403 column_info[i].alignment = static_cast<LyXAlignment>(a);
1404 column_info[i].left_line = b;
1405 column_info[i].right_line = c;
1406 column_info[i].p_width = LyXLength(s1);
1407 column_info[i].align_special = s2;
1409 for (i = 0; i < rows_; ++i) {
1410 for (j = 0; j < columns_; ++j) {
1413 is >> a >> b >> c >> d >> e >> f >> g;
1416 is >> ch; // skip '"'
1418 // ignore is buggy but we will use it later (Lgb)
1419 is.ignore(); // skip '"'
1421 getline(is, s1, '"');
1423 is >> ch; // skip '"'
1425 // ignore is buggy but we will use it later (Lgb)
1426 is.ignore(); // skip '"'
1428 getline(is, s2, '"');
1429 cell_info[i][j].multicolumn = static_cast<char>(a);
1430 cell_info[i][j].alignment = static_cast<LyXAlignment>(b);
1431 cell_info[i][j].top_line = static_cast<char>(c);
1432 cell_info[i][j].bottom_line = static_cast<char>(d);
1433 cell_info[i][j].left_line = column_info[j].left_line;
1434 cell_info[i][j].right_line = column_info[j].right_line;
1435 cell_info[i][j].rotate = static_cast<bool>(f);
1436 cell_info[i][j].usebox = static_cast<BoxType>(g);
1437 cell_info[i][j].align_special = s1;
1438 cell_info[i][j].p_width = LyXLength(s2);
1442 set_row_column_number_info(true);
1444 Paragraph * par = new Paragraph;
1445 Paragraph * return_par = 0;
1447 par->layout(textclasslist[bp.textclass].defaultLayoutName());
1450 Paragraph::depth_type depth = 0;
1451 LyXFont font(LyXFont::ALL_INHERIT);
1452 font.setLanguage(owner_->bufferOwner()->getLanguage());
1454 while (lex.isOK()) {
1456 string const token = lex.getString();
1459 if (token == "\\layout"
1460 || token == "\\end_float"
1461 || token == "\\end_deeper") {
1462 lex.pushToken(token);
1463 #ifndef NO_COMPABILITY
1464 // Here we need to insert the inset_ert_contents into the last
1465 // cell of the tabular.
1466 owner_->bufferOwner()->insertErtContents(par, pos);
1470 if (owner_->bufferOwner()->parseSingleLyXformat2Token(lex, par,
1475 lex.pushToken(token);
1479 lex.printError("New Paragraph allocated! This should not happen!");
1480 lex.pushToken(token);
1486 // now we have the par we should fill the insets with this!
1488 InsetText * inset = GetCellInset(cell);
1491 for (int i = 0; i < par->size(); ++i) {
1492 if (par->isNewline(i)) {
1494 if (cell > numberofcells) {
1495 lyxerr << "Some error in reading old table format occured!" <<
1496 endl << "Terminating when reading cell[" << cell << "]!" <<
1501 row = row_of_cell(cell);
1502 if (cont_row_info[row]) {
1504 cont_row_info.erase(cont_row_info.begin() + row); //&cont_row_info[row]);
1505 while (!IsFirstCellInRow(--cell));
1507 inset = GetCellInset(cell);
1510 inset = GetCellInset(cell);
1511 row = row_of_cell(cell);
1512 if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].usebox)
1514 // insert a space instead
1516 par->insertChar(i, ' ');
1519 par->copyIntoMinibuffer(*owner_->bufferOwner(), i);
1520 inset->paragraph()->insertFromMinibuffer(inset->paragraph()->size());
1527 bool LyXTabular::IsMultiColumn(int cell, bool real) const
1529 return ((!real || (column_of_cell(cell) != right_column_of_cell(cell))) &&
1530 (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL));
1534 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1536 int const row = row_of_cell(cell);
1537 int const column = column_of_cell(cell);
1538 return &cell_info[row][column];
1542 void LyXTabular::SetMultiColumn(int cell, int number)
1544 cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1545 cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1546 cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1547 cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1548 for (number--; number > 0; --number) {
1549 cellinfo_of_cell(cell+number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1551 set_row_column_number_info();
1555 int LyXTabular::cells_in_multicolumn(int cell) const
1557 int const row = row_of_cell(cell);
1558 int column = column_of_cell(cell);
1561 while ((column < columns_) &&
1562 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1571 int LyXTabular::UnsetMultiColumn(int cell)
1573 int const row = row_of_cell(cell);
1574 int column = column_of_cell(cell);
1578 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1579 cell_info[row][column].multicolumn = CELL_NORMAL;
1581 while ((column < columns_) &&
1582 (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1584 cell_info[row][column].multicolumn = CELL_NORMAL;
1589 set_row_column_number_info();
1594 void LyXTabular::SetLongTabular(bool what)
1596 is_long_tabular = what;
1600 bool LyXTabular::IsLongTabular() const
1602 return is_long_tabular;
1606 void LyXTabular::SetRotateTabular(bool flag)
1612 bool LyXTabular::GetRotateTabular() const
1618 void LyXTabular::SetRotateCell(int cell, bool flag)
1620 cellinfo_of_cell(cell)->rotate = flag;
1624 bool LyXTabular::GetRotateCell(int cell) const
1626 return cellinfo_of_cell(cell)->rotate;
1630 bool LyXTabular::NeedRotating() const
1634 for (int i = 0; i < rows_; ++i) {
1635 for (int j = 0; j < columns_; ++j) {
1636 if (cell_info[i][j].rotate)
1644 bool LyXTabular::IsLastCell(int cell) const
1646 if ((cell + 1) < numberofcells)
1652 int LyXTabular::GetCellAbove(int cell) const
1654 if (row_of_cell(cell) > 0)
1655 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1660 int LyXTabular::GetCellBelow(int cell) const
1662 if (row_of_cell(cell) + 1 < rows_)
1663 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1668 int LyXTabular::GetLastCellAbove(int cell) const
1670 if (row_of_cell(cell) <= 0)
1672 if (!IsMultiColumn(cell))
1673 return GetCellAbove(cell);
1674 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1678 int LyXTabular::GetLastCellBelow(int cell) const
1680 if (row_of_cell(cell) + 1 >= rows_)
1682 if (!IsMultiColumn(cell))
1683 return GetCellBelow(cell);
1684 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1688 int LyXTabular::GetCellNumber(int row, int column) const
1691 if (column >= columns_)
1692 column = columns_ - 1;
1693 else if (column < 0)
1700 lyx::Assert(column >= 0 || column < columns_ || row >= 0 || row < rows_);
1702 return cell_info[row][column].cellno;
1706 void LyXTabular::SetUsebox(int cell, BoxType type)
1708 cellinfo_of_cell(cell)->usebox = type;
1712 LyXTabular::BoxType LyXTabular::GetUsebox(int cell) const
1714 if (column_info[column_of_cell(cell)].p_width.zero() &&
1715 !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.zero()))
1717 if (cellinfo_of_cell(cell)->usebox > 1)
1718 return cellinfo_of_cell(cell)->usebox;
1719 return UseParbox(cell);
1724 // This are functions used for the longtable support
1726 void LyXTabular::SetLTHead(int row, bool flag, ltType const & hd, bool first)
1731 row_info[row].endfirsthead = flag;
1735 row_info[row].endhead = flag;
1740 bool LyXTabular::GetRowOfLTHead(int row, ltType & hd) const
1743 hd.set = haveLTHead();
1744 return row_info[row].endhead;
1748 bool LyXTabular::GetRowOfLTFirstHead(int row, ltType & hd) const
1751 hd.set = haveLTFirstHead();
1752 return row_info[row].endfirsthead;
1756 void LyXTabular::SetLTFoot(int row, bool flag, ltType const & fd, bool last)
1761 row_info[row].endlastfoot = flag;
1765 row_info[row].endfoot = flag;
1770 bool LyXTabular::GetRowOfLTFoot(int row, ltType & fd) const
1773 fd.set = haveLTFoot();
1774 return row_info[row].endfoot;
1778 bool LyXTabular::GetRowOfLTLastFoot(int row, ltType & fd) const
1781 fd.set = haveLTLastFoot();
1782 return row_info[row].endlastfoot;
1786 void LyXTabular::SetLTNewPage(int row, bool what)
1788 row_info[row].newpage = what;
1792 bool LyXTabular::GetLTNewPage(int row) const
1794 return row_info[row].newpage;
1798 bool LyXTabular::haveLTHead() const
1800 for(int i=0; i < rows_; ++i) {
1801 if (row_info[i].endhead)
1808 bool LyXTabular::haveLTFirstHead() const
1810 if (endfirsthead.empty)
1812 for(int i=0; i < rows_; ++i) {
1813 if (row_info[i].endfirsthead)
1820 bool LyXTabular::haveLTFoot() const
1822 for(int i=0; i < rows_; ++i) {
1823 if (row_info[i].endfoot)
1830 bool LyXTabular::haveLTLastFoot() const
1832 if (endlastfoot.empty)
1834 for(int i=0; i < rows_; ++i) {
1835 if (row_info[i].endlastfoot)
1842 // end longtable support functions
1844 bool LyXTabular::SetAscentOfRow(int row, int height)
1846 if ((row >= rows_) || (row_info[row].ascent_of_row == height))
1848 row_info[row].ascent_of_row = height;
1853 bool LyXTabular::SetDescentOfRow(int row, int height)
1855 if ((row >= rows_) || (row_info[row].descent_of_row == height))
1857 row_info[row].descent_of_row = height;
1862 int LyXTabular::GetAscentOfRow(int row) const
1866 return row_info[row].ascent_of_row;
1870 int LyXTabular::GetDescentOfRow(int row) const
1874 return row_info[row].descent_of_row;
1878 int LyXTabular::GetHeightOfTabular() const
1882 for (int row = 0; row < rows_; ++row)
1883 height += GetAscentOfRow(row) + GetDescentOfRow(row) +
1884 GetAdditionalHeight(row);
1889 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
1891 if ((row >= rows_) || (column >= columns_))
1893 return (cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN);
1897 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1899 if ((row < 0) || (row >= rows_))
1902 int const fcell = GetFirstCellInRow(row);
1903 int const n = NumberOfCellsInRow(fcell) + fcell;
1906 for (int i = fcell; i < n; ++i) {
1910 if (tmp == (n - fcell)) {
1913 for (int i = fcell; i < n; ++i) {
1916 << column_of_cell(i) + 1
1918 << right_column_of_cell(i) + 1
1930 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1932 if ((row < 0) || (row >= rows_))
1935 int const fcell = GetFirstCellInRow(row);
1936 int const n = NumberOfCellsInRow(fcell) + fcell;
1939 for (int i = fcell; i < n; ++i) {
1943 if (tmp == (n - fcell)) {
1946 for (int i = fcell; i < n; ++i) {
1947 if (BottomLine(i)) {
1949 << column_of_cell(i) + 1
1951 << right_column_of_cell(i) + 1
1963 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1967 if (GetRotateCell(cell)) {
1968 os << "\\begin{sideways}\n";
1971 if (IsMultiColumn(cell)) {
1972 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1973 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1974 os << cellinfo_of_cell(cell)->align_special << "}{";
1976 if (LeftLine(cell) &&
1977 (IsFirstCellInRow(cell) ||
1978 (!IsMultiColumn(cell-1) && !LeftLine(cell, true) &&
1979 !RightLine(cell-1, true))))
1983 if (!GetPWidth(cell).zero()) {
1984 switch (GetVAlignment(cell)) {
1985 case LYX_VALIGN_TOP:
1988 case LYX_VALIGN_CENTER:
1991 case LYX_VALIGN_BOTTOM:
1995 os << "{" << GetPWidth(cell).asLatexString() << '}';
1997 switch (GetAlignment(cell)) {
1998 case LYX_ALIGN_LEFT:
2001 case LYX_ALIGN_RIGHT:
2009 if (RightLine(cell))
2011 if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
2017 if (GetUsebox(cell) == BOX_PARBOX) {
2019 switch (GetVAlignment(cell)) {
2020 case LYX_VALIGN_TOP:
2023 case LYX_VALIGN_CENTER:
2026 case LYX_VALIGN_BOTTOM:
2030 os << "]{" << GetPWidth(cell).asLatexString() << "}{";
2031 } else if (GetUsebox(cell) == BOX_MINIPAGE) {
2032 os << "\\begin{minipage}[";
2033 switch (GetVAlignment(cell)) {
2034 case LYX_VALIGN_TOP:
2037 case LYX_VALIGN_CENTER:
2040 case LYX_VALIGN_BOTTOM:
2044 os << "]{" << GetPWidth(cell).asLatexString() << "}\n";
2051 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
2056 if (GetUsebox(cell) == BOX_PARBOX)
2058 else if (GetUsebox(cell) == BOX_MINIPAGE) {
2059 os << "%\n\\end{minipage}";
2062 if (IsMultiColumn(cell)) {
2065 if (GetRotateCell(cell)) {
2066 os << "%\n\\end{sideways}";
2073 int LyXTabular::TeXLongtableHeaderFooter(ostream & os, Buffer const * buf,
2074 bool fragile, bool fp) const
2076 if (!is_long_tabular)
2080 // output header info
2082 if (endhead.topDL) {
2086 for (int i = 0; i < rows_; ++i) {
2087 if (row_info[i].endhead) {
2088 ret += TeXRow(os, i, buf, fragile, fp);
2091 if (endhead.bottomDL) {
2095 os << "\\endhead\n";
2097 if (endfirsthead.empty) {
2098 os << "\\endfirsthead\n";
2102 // output firstheader info
2103 if (haveLTFirstHead()) {
2104 if (endfirsthead.topDL) {
2108 for (int i = 0; i < rows_; ++i) {
2109 if (row_info[i].endfirsthead) {
2110 ret += TeXRow(os, i, buf, fragile, fp);
2113 if (endfirsthead.bottomDL) {
2117 os << "\\endfirsthead\n";
2120 // output footer info
2122 if (endfoot.topDL) {
2126 for (int i = 0; i < rows_; ++i) {
2127 if (row_info[i].endfoot) {
2128 ret += TeXRow(os, i, buf, fragile, fp);
2131 if (endfoot.bottomDL) {
2135 os << "\\endfoot\n";
2137 if (endlastfoot.empty) {
2138 os << "\\endlastfoot\n";
2142 // output lastfooter info
2143 if (haveLTLastFoot()) {
2144 if (endlastfoot.topDL) {
2148 for (int i = 0; i < rows_; ++i) {
2149 if (row_info[i].endlastfoot) {
2150 ret += TeXRow(os, i, buf, fragile, fp);
2153 if (endlastfoot.bottomDL) {
2157 os << "\\endlastfoot\n";
2164 bool LyXTabular::isValidRow(int const row) const
2166 if (!is_long_tabular)
2168 return (!row_info[row].endhead && !row_info[row].endfirsthead &&
2169 !row_info[row].endfoot && !row_info[row].endlastfoot);
2173 int LyXTabular::TeXRow(ostream & os, int const i, Buffer const * buf,
2174 bool fragile, bool fp) const
2177 int cell = GetCellNumber(i, 0);
2179 ret += TeXTopHLine(os, i);
2180 for (int j = 0; j < columns_; ++j) {
2181 if (IsPartOfMultiColumn(i,j))
2183 ret += TeXCellPreamble(os, cell);
2184 InsetText * inset = GetCellInset(cell);
2186 bool rtl = inset->paragraph()->isRightToLeftPar(buf->params) &&
2187 inset->paragraph()->size() > 0 && GetPWidth(cell).zero();
2191 ret += inset->latex(buf, os, fragile, fp);
2195 ret += TeXCellPostamble(os, cell);
2196 if (!IsLastCellInRow(cell)) { // not last cell in row
2204 ret += TeXBottomHLine(os, i);
2209 int LyXTabular::latex(Buffer const * buf,
2210 ostream & os, bool fragile, bool fp) const
2214 //+---------------------------------------------------------------------
2215 //+ first the opening preamble +
2216 //+---------------------------------------------------------------------
2219 os << "\\begin{sideways}\n";
2222 if (is_long_tabular)
2223 os << "\\begin{longtable}{";
2225 os << "\\begin{tabular}{";
2226 for (int i = 0; i < columns_; ++i) {
2227 if (!column_info[i].align_special.empty()) {
2228 os << column_info[i].align_special;
2230 if (column_info[i].left_line)
2232 if (!column_info[i].p_width.zero()) {
2233 switch (column_info[i].valignment) {
2234 case LYX_VALIGN_TOP:
2237 case LYX_VALIGN_CENTER:
2240 case LYX_VALIGN_BOTTOM:
2245 << column_info[i].p_width.asLatexString()
2248 switch (column_info[i].alignment) {
2249 case LYX_ALIGN_LEFT:
2252 case LYX_ALIGN_RIGHT:
2260 if (column_info[i].right_line)
2267 ret += TeXLongtableHeaderFooter(os, buf, fragile, fp);
2269 //+---------------------------------------------------------------------
2270 //+ the single row and columns (cells) +
2271 //+---------------------------------------------------------------------
2273 for (int i = 0; i < rows_; ++i) {
2274 if (isValidRow(i)) {
2275 ret += TeXRow(os, i, buf, fragile, fp);
2276 if (is_long_tabular && row_info[i].newpage) {
2277 os << "\\newpage\n";
2283 //+---------------------------------------------------------------------
2284 //+ the closing of the tabular +
2285 //+---------------------------------------------------------------------
2287 if (is_long_tabular)
2288 os << "\\end{longtable}";
2290 os << "\\end{tabular}";
2292 os << "\n\\end{sideways}";
2300 int LyXTabular::docbookRow(Buffer const * buf, ostream & os, int row) const
2303 int cell = GetFirstCellInRow(row);
2306 for (int j = 0; j < columns_; ++j) {
2307 if (IsPartOfMultiColumn(row, j))
2310 os << "<entry align=\"";
2311 switch (GetAlignment(cell)) {
2312 case LYX_ALIGN_LEFT:
2315 case LYX_ALIGN_RIGHT:
2323 os << "\" valign=\"";
2324 switch (GetVAlignment(cell)) {
2325 case LYX_VALIGN_TOP:
2328 case LYX_VALIGN_BOTTOM:
2331 case LYX_VALIGN_CENTER:
2336 if (IsMultiColumn(cell)) {
2337 os << " namest=\"col" << j << "\" ";
2338 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< "\"";
2342 ret += GetCellInset(cell)->docbook(buf, os);
2351 int LyXTabular::docBook(Buffer const * buf, ostream & os) const
2355 //+---------------------------------------------------------------------
2356 //+ first the opening preamble +
2357 //+---------------------------------------------------------------------
2359 os << "<tgroup cols=\"" << columns_
2360 << "\" colsep=\"1\" rowsep=\"1\">\n";
2362 for (int i = 0; i < columns_; ++i) {
2363 os << "<colspec colname=\"col" << i << "\" align=\"";
2364 switch (column_info[i].alignment) {
2365 case LYX_ALIGN_LEFT:
2368 case LYX_ALIGN_RIGHT:
2379 //+---------------------------------------------------------------------
2380 //+ Long Tabular case +
2381 //+---------------------------------------------------------------------
2383 // output header info
2384 if (haveLTHead() || haveLTFirstHead()) {
2387 for (int i = 0; i < rows_; ++i) {
2388 if (row_info[i].endhead || row_info[i].endfirsthead) {
2389 ret += docbookRow(buf, os, i);
2395 // output footer info
2396 if (haveLTFoot() || haveLTLastFoot()) {
2399 for (int i = 0; i < rows_; ++i) {
2400 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2401 ret += docbookRow(buf, os, i);
2408 //+---------------------------------------------------------------------
2409 //+ the single row and columns (cells) +
2410 //+---------------------------------------------------------------------
2414 for (int i = 0; i < rows_; ++i) {
2415 if (isValidRow(i)) {
2416 ret += docbookRow(buf, os, i);
2421 //+---------------------------------------------------------------------
2422 //+ the closing of the tabular +
2423 //+---------------------------------------------------------------------
2432 // ASCII export function and helpers
2434 int LyXTabular::asciiTopHLine(ostream & os, int row,
2435 vector<unsigned int> const & clen) const
2437 int const fcell = GetFirstCellInRow(row);
2438 int const n = NumberOfCellsInRow(fcell) + fcell;
2441 for (int i = fcell; i < n; ++i) {
2451 for (int i = fcell; i < n; ++i) {
2462 int column = column_of_cell(i);
2463 int len = clen[column];
2464 while (IsPartOfMultiColumn(row, ++column))
2465 len += clen[column] + 4;
2466 os << string(len, ch);
2481 int LyXTabular::asciiBottomHLine(ostream & os, int row,
2482 vector<unsigned int> const & clen) const
2484 int const fcell = GetFirstCellInRow(row);
2485 int const n = NumberOfCellsInRow(fcell) + fcell;
2488 for (int i = fcell; i < n; ++i) {
2489 if (BottomLine(i)) {
2498 for (int i = fcell; i < n; ++i) {
2499 if (BottomLine(i)) {
2509 int column = column_of_cell(i);
2510 int len = clen[column];
2511 while (IsPartOfMultiColumn(row, ++column))
2512 len += clen[column] + 4;
2513 os << string(len, ch);
2514 if (BottomLine(i)) {
2528 int LyXTabular::asciiPrintCell(Buffer const * buf, ostream & os,
2529 int cell, int row, int column,
2530 vector<unsigned int> const & clen,
2531 bool onlydata) const
2534 int ret = GetCellInset(cell)->ascii(buf, sstr, 0);
2546 unsigned int len1 = sstr.str().length();
2547 unsigned int len2 = clen[column];
2548 while (IsPartOfMultiColumn(row, ++column))
2549 len2 += clen[column] + 4;
2552 switch (GetAlignment(cell)) {
2554 case LYX_ALIGN_LEFT:
2557 case LYX_ALIGN_RIGHT:
2561 case LYX_ALIGN_CENTER:
2567 for (unsigned int i = 0; i < len1; ++i)
2570 for (unsigned int i = 0; i < len2; ++i)
2572 if (RightLine(cell))
2581 int LyXTabular::ascii(Buffer const * buf, ostream & os, int const depth,
2582 bool onlydata, unsigned char delim) const
2586 //+---------------------------------------------------------------------
2587 //+ first calculate the width of the single columns +
2588 //+---------------------------------------------------------------------
2589 vector<unsigned int> clen(columns_);
2592 // first all non (real) multicolumn cells!
2593 for (int j = 0; j < columns_; ++j) {
2595 for (int i = 0; i < rows_; ++i) {
2596 int cell = GetCellNumber(i, j);
2597 if (IsMultiColumn(cell, true))
2600 GetCellInset(cell)->ascii(buf, sstr, 0);
2601 if (clen[j] < sstr.str().length())
2602 clen[j] = sstr.str().length();
2605 // then all (real) multicolumn cells!
2606 for (int j = 0; j < columns_; ++j) {
2607 for (int i = 0; i < rows_; ++i) {
2608 int cell = GetCellNumber(i, j);
2609 if (!IsMultiColumn(cell, true) || IsPartOfMultiColumn(i, j))
2612 GetCellInset(cell)->ascii(buf, sstr, 0);
2613 int len = int(sstr.str().length());
2614 int const n = cells_in_multicolumn(cell);
2615 for (int k = j; (len > 0) && (k < (j + n - 1)); ++k)
2617 if (len > int(clen[j + n - 1]))
2618 clen[j + n - 1] = len;
2623 for (int i = 0; i < rows_; ++i) {
2625 if (asciiTopHLine(os, i, clen)) {
2626 for (int j = 0; j < depth; ++j)
2630 for (int j = 0; j < columns_; ++j) {
2631 if (IsPartOfMultiColumn(i,j))
2633 if (onlydata && j > 0)
2635 ret += asciiPrintCell(buf, os, cell, i, j, clen, onlydata);
2640 for (int j = 0; j < depth; ++j)
2642 if (asciiBottomHLine(os, i, clen)) {
2643 for (int j = 0; j < depth; ++j)
2655 InsetText * LyXTabular::GetCellInset(int cell) const
2658 return & cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2662 InsetText * LyXTabular::GetCellInset(int row, int column) const
2664 cur_cell = GetCellNumber(row, column);
2665 return & cell_info[row][column].inset;
2669 int LyXTabular::GetCellFromInset(Inset const * inset, int maybe_cell) const
2671 // is this inset part of the tabular?
2672 if (!inset || inset->owner() != owner_) {
2673 lyxerr[Debug::INSETTEXT]
2674 << "this is not a cell of the tabular!" << endl;
2678 const int save_cur_cell = cur_cell;
2679 int cell = cur_cell;
2680 if (GetCellInset(cell) != inset) {
2682 if (cell == -1 || GetCellInset(cell) != inset) {
2688 for (cell = GetNumberOfCells(); cell >= 0; --cell) {
2689 if (GetCellInset(cell) == inset)
2692 lyxerr[Debug::INSETTEXT]
2693 << "LyXTabular::GetCellFromInset: "
2695 << ", cur_cell=" << save_cur_cell
2696 << ", maybe_cell=" << maybe_cell
2698 // We should have found a cell at this point
2700 lyxerr << "LyXTabular::GetCellFromInset: "
2701 << "Cell not found!" << endl;
2709 void LyXTabular::Validate(LaTeXFeatures & features) const
2711 if (IsLongTabular())
2712 features.require("longtable");
2714 features.require("rotating");
2715 for (int cell = 0; cell < numberofcells; ++cell) {
2716 if (GetVAlignment(cell) != LYX_VALIGN_TOP)
2717 features.require("array");
2718 GetCellInset(cell)->validate(features);
2723 vector<string> const LyXTabular::getLabelList() const
2725 vector<string> label_list;
2726 for (int i = 0; i < rows_; ++i)
2727 for (int j = 0; j < columns_; ++j) {
2728 vector<string> const l =
2729 GetCellInset(i, j)->getLabelList();
2730 label_list.insert(label_list.end(),
2731 l.begin(), l.end());
2737 LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
2739 Paragraph * par = GetCellInset(cell)->paragraph();
2741 for (; par; par = par->next()) {
2742 for (int i = 0; i < par->size(); ++i) {
2743 if (par->getChar(i) == Paragraph::META_NEWLINE)