1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 2000-2002 The LyX Team.
8 * @author: Jürgen Vigna
10 * ======================================================
16 #pragma implementation
19 // temporary until verified (08/08/2001 Jug)
20 #define SPECIAL_COLUM_HANDLING 1
27 #include "BufferView.h"
28 #include "frontends/Painter.h"
29 #include "LaTeXFeatures.h"
30 #include "insets/insettabular.h"
31 #include "insets/insettext.h"
32 #include "support/lstrings.h"
33 #include "support/lyxmanip.h"
34 #include "support/LAssert.h"
35 #include "frontends/Alert.h"
37 #include "tabular_funcs.h"
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 (IsFirstCellInRow(cell) || IsMultiColumn(cell-1)))
471 #ifdef SPECIAL_COLUM_HANDLING
472 if (cellinfo_of_cell(cell)->align_special.empty())
473 return cellinfo_of_cell(cell)->left_line;
474 return prefixIs(frontStrip(cellinfo_of_cell(cell)->align_special), "|");
476 return cellinfo_of_cell(cell)->left_line;
479 #ifdef SPECIAL_COLUM_HANDLING
480 if (column_info[column_of_cell(cell)].align_special.empty())
481 return column_info[column_of_cell(cell)].left_line;
482 return prefixIs(frontStrip(column_info[column_of_cell(cell)].align_special), "|");
484 return column_info[column_of_cell(cell)].left_line;
489 bool LyXTabular::RightLine(int cell, bool onlycolumn) const
491 if (!onlycolumn && IsMultiColumn(cell) &&
492 (IsLastCellInRow(cell) || IsMultiColumn(cell+1)))
494 #ifdef SPECIAL_COLUM_HANDLING
495 if (cellinfo_of_cell(cell)->align_special.empty())
496 return cellinfo_of_cell(cell)->right_line;
497 return suffixIs(strip(cellinfo_of_cell(cell)->align_special), "|");
499 return cellinfo_of_cell(cell)->right_line;
502 #ifdef SPECIAL_COLUM_HANDLING
503 if (column_info[column_of_cell(cell)].align_special.empty())
504 return column_info[right_column_of_cell(cell)].right_line;
505 return suffixIs(strip(column_info[column_of_cell(cell)].align_special), "|");
507 return column_info[right_column_of_cell(cell)].right_line;
512 bool LyXTabular::TopAlreadyDrawed(int cell) const
514 int row = row_of_cell(cell);
515 if ((row > 0) && !GetAdditionalHeight(row)) {
516 int column = column_of_cell(cell);
519 && cell_info[row][column].multicolumn
520 == LyXTabular::CELL_PART_OF_MULTICOLUMN)
522 if (cell_info[row][column].multicolumn == LyXTabular::CELL_NORMAL)
523 return row_info[row].bottom_line;
525 return cell_info[row][column].bottom_line;
531 bool LyXTabular::LeftAlreadyDrawed(int cell) const
533 int column = column_of_cell(cell);
535 int row = row_of_cell(cell);
537 (cell_info[row][column].multicolumn ==
538 LyXTabular::CELL_PART_OF_MULTICOLUMN));
539 if (GetAdditionalWidth(cell_info[row][column].cellno))
541 #ifdef SPECIAL_COLUM_HANDLING
542 return RightLine(cell_info[row][column].cellno);
544 return RightLine(cell_info[row][column].cellno, true);
551 bool LyXTabular::IsLastRow(int cell) const
553 return (row_of_cell(cell) == rows_ - 1);
557 int LyXTabular::GetAdditionalHeight(int row) const
559 if (!row || row >= rows_)
565 for (int column = 0; column < columns_ && bottom; ++column) {
566 switch (cell_info[row - 1][column].multicolumn) {
567 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
568 bottom = cell_info[row - 1][column].bottom_line;
570 case LyXTabular::CELL_NORMAL:
571 bottom = row_info[row - 1].bottom_line;
574 for (int column = 0; column < columns_ && top; ++column) {
575 switch (cell_info[row][column].multicolumn) {
576 case LyXTabular::CELL_BEGIN_OF_MULTICOLUMN:
577 top = cell_info[row][column].top_line;
579 case LyXTabular::CELL_NORMAL:
580 top = row_info[row].top_line;
584 return WIDTH_OF_LINE;
589 int LyXTabular::GetAdditionalWidth(int cell) const
591 // internally already set in SetWidthOfCell
592 // used to get it back in text.C
593 int const col = right_column_of_cell(cell);
594 int const row = row_of_cell(cell);
595 if (col < columns_ - 1 && RightLine(cell) &&
596 LeftLine(cell_info[row][col+1].cellno)) // column_info[col+1].left_line)
598 return WIDTH_OF_LINE;
605 // returns the maximum over all rows
606 int LyXTabular::GetWidthOfColumn(int cell) const
608 int const column1 = column_of_cell(cell);
609 int const column2 = right_column_of_cell(cell);
611 for (int i = column1; i <= column2; ++i) {
612 result += column_info[i].width_of_column;
618 int LyXTabular::GetWidthOfTabular() const
620 return width_of_tabular;
624 /* returns 1 if a complete update is necessary, otherwise 0 */
625 bool LyXTabular::SetWidthOfMulticolCell(int cell, int new_width)
627 if (!IsMultiColumn(cell))
630 int const row = row_of_cell(cell);
631 int const column1 = column_of_cell(cell);
632 int const column2 = right_column_of_cell(cell);
633 int const old_val = cell_info[row][column2].width_of_cell;
635 // first set columns to 0 so we can calculate the right width
636 for (int i = column1; i <= column2; ++i) {
637 cell_info[row][i].width_of_cell = 0;
639 // set the width to MAX_WIDTH until width > 0
640 int width = (new_width + 2 * WIDTH_OF_LINE);
642 for (; i < column2 && width > column_info[i].width_of_column; ++i) {
643 cell_info[row][i].width_of_cell = column_info[i].width_of_column;
644 width -= column_info[i].width_of_column;
647 cell_info[row][i].width_of_cell = width;
649 if (old_val != cell_info[row][column2].width_of_cell) {
650 // in this case we have to recalculate all multicolumn cells which
651 // have this column as one of theirs but not as last one
652 calculate_width_of_column_NMC(i);
653 recalculateMulticolumnsOfColumn(i);
654 calculate_width_of_column(i);
660 void LyXTabular::recalculateMulticolumnsOfColumn(int column)
662 // the last column does not have to be recalculated because all
663 // multicolumns will have here there last multicolumn cell which
664 // always will have the whole rest of the width of the cell.
665 if (column > (columns_ - 2))
667 for(int row = 0; row < rows_; ++row) {
668 int mc = cell_info[row][column].multicolumn;
669 int nmc = cell_info[row][column+1].multicolumn;
670 // we only have to update multicolumns which do not have this
671 // column as their last column!
672 if (mc == CELL_BEGIN_OF_MULTICOLUMN ||
673 ((mc == CELL_PART_OF_MULTICOLUMN) &&
674 (nmc == CELL_PART_OF_MULTICOLUMN)))
676 int const cellno = cell_info[row][column].cellno;
677 SetWidthOfMulticolCell(cellno,
678 GetWidthOfCell(cellno)-(2 * WIDTH_OF_LINE));
684 /* returns 1 if a complete update is necessary, otherwise 0 */
685 bool LyXTabular::SetWidthOfCell(int cell, int new_width)
687 int const row = row_of_cell(cell);
688 int const column1 = column_of_cell(cell);
693 #ifdef SPECIAL_COLUM_HANDLING
694 if (RightLine(cell_info[row][column1].cellno, true) &&
695 (column1 < columns_-1) &&
696 LeftLine(cell_info[row][column1+1].cellno, true))
698 if (column_info[column1].right_line && (column1 < columns_-1) &&
699 column_info[column1+1].left_line) // additional width
703 add_width = WIDTH_OF_LINE;
705 if (GetWidthOfCell(cell) == (new_width+2*WIDTH_OF_LINE+add_width)) {
708 if (IsMultiColumn(cell, true)) {
709 tmp = SetWidthOfMulticolCell(cell, new_width);
711 width = (new_width + 2*WIDTH_OF_LINE + add_width);
712 cell_info[row][column1].width_of_cell = width;
713 tmp = calculate_width_of_column_NMC(column1);
715 recalculateMulticolumnsOfColumn(column1);
718 for (int i = 0; i < columns_; ++i)
719 calculate_width_of_column(i);
720 calculate_width_of_tabular();
727 bool LyXTabular::SetAlignment(int cell, LyXAlignment align, bool onlycolumn)
729 if (!IsMultiColumn(cell) || onlycolumn)
730 column_info[column_of_cell(cell)].alignment = align;
732 cellinfo_of_cell(cell)->alignment = align;
737 bool LyXTabular::SetVAlignment(int cell, VAlignment align, bool onlycolumn)
739 if (!IsMultiColumn(cell) || onlycolumn)
740 column_info[column_of_cell(cell)].valignment = align;
742 cellinfo_of_cell(cell)->valignment = align;
747 bool LyXTabular::SetColumnPWidth(int cell, LyXLength const & width)
749 bool flag = !width.zero();
750 int const j = column_of_cell(cell);
752 column_info[j].p_width = width;
753 if (flag) // do this only if there is a width
754 SetAlignment(cell, LYX_ALIGN_LEFT);
755 for (int i = 0; i < rows_; ++i) {
756 int c = GetCellNumber(i, j);
757 flag = !GetPWidth(c).zero(); // because of multicolumns!
758 GetCellInset(c)->setAutoBreakRows(flag);
764 bool LyXTabular::SetMColumnPWidth(int cell, LyXLength const & width)
766 bool const flag = !width.zero();
768 cellinfo_of_cell(cell)->p_width = width;
769 if (IsMultiColumn(cell)) {
770 GetCellInset(cell)->setAutoBreakRows(flag);
777 bool LyXTabular::SetAlignSpecial(int cell, string const & special,
778 LyXTabular::Feature what)
780 if (what == SET_SPECIAL_MULTI)
781 cellinfo_of_cell(cell)->align_special = special;
783 column_info[column_of_cell(cell)].align_special = special;
788 bool LyXTabular::SetAllLines(int cell, bool line)
790 SetTopLine(cell, line);
791 SetBottomLine(cell, line);
792 SetRightLine(cell, line);
793 SetLeftLine(cell, line);
798 bool LyXTabular::SetTopLine(int cell, bool line, bool onlycolumn)
800 int const row = row_of_cell(cell);
802 if (onlycolumn || !IsMultiColumn(cell))
803 row_info[row].top_line = line;
805 cellinfo_of_cell(cell)->top_line = line;
810 bool LyXTabular::SetBottomLine(int cell, bool line, bool onlycolumn)
812 if (onlycolumn || !IsMultiColumn(cell))
813 row_info[row_of_cell(cell)].bottom_line = line;
815 cellinfo_of_cell(cell)->bottom_line = line;
820 bool LyXTabular::SetLeftLine(int cell, bool line, bool onlycolumn)
822 if (onlycolumn || !IsMultiColumn(cell))
823 column_info[column_of_cell(cell)].left_line = line;
825 cellinfo_of_cell(cell)->left_line = line;
830 bool LyXTabular::SetRightLine(int cell, bool line, bool onlycolumn)
832 if (onlycolumn || !IsMultiColumn(cell))
833 column_info[right_column_of_cell(cell)].right_line = line;
835 cellinfo_of_cell(cell)->right_line = line;
840 LyXAlignment LyXTabular::GetAlignment(int cell, bool onlycolumn) const
842 if (!onlycolumn && IsMultiColumn(cell))
843 return cellinfo_of_cell(cell)->alignment;
845 return column_info[column_of_cell(cell)].alignment;
849 LyXTabular::VAlignment
850 LyXTabular::GetVAlignment(int cell, bool onlycolumn) const
852 if (!onlycolumn && IsMultiColumn(cell))
853 return cellinfo_of_cell(cell)->valignment;
855 return column_info[column_of_cell(cell)].valignment;
859 LyXLength const LyXTabular::GetPWidth(int cell) const
861 if (IsMultiColumn(cell))
862 return cellinfo_of_cell(cell)->p_width;
863 return column_info[column_of_cell(cell)].p_width;
867 LyXLength const LyXTabular::GetColumnPWidth(int cell) const
869 return column_info[column_of_cell(cell)].p_width;
873 LyXLength const LyXTabular::GetMColumnPWidth(int cell) const
875 if (IsMultiColumn(cell))
876 return cellinfo_of_cell(cell)->p_width;
881 string const LyXTabular::GetAlignSpecial(int cell, int what) const
883 if (what == SET_SPECIAL_MULTI)
884 return cellinfo_of_cell(cell)->align_special;
885 return column_info[column_of_cell(cell)].align_special;
889 int LyXTabular::GetWidthOfCell(int cell) const
891 int const row = row_of_cell(cell);
892 int const column1 = column_of_cell(cell);
893 int const column2 = right_column_of_cell(cell);
895 for (int i = column1; i <= column2; ++i) {
896 result += cell_info[row][i].width_of_cell;
902 int LyXTabular::GetBeginningOfTextInCell(int cell) const
906 switch (GetAlignment(cell)) {
907 case LYX_ALIGN_CENTER:
908 x += (GetWidthOfColumn(cell) - GetWidthOfCell(cell)) / 2;
910 case LYX_ALIGN_RIGHT:
911 x += GetWidthOfColumn(cell) - GetWidthOfCell(cell);
912 // + GetAdditionalWidth(cell);
914 default: /* LYX_ALIGN_LEFT: nothing :-) */
924 bool LyXTabular::IsFirstCellInRow(int cell) const
926 return column_of_cell(cell) == 0;
930 int LyXTabular::GetFirstCellInRow(int row) const
934 return cell_info[row][0].cellno;
937 bool LyXTabular::IsLastCellInRow(int cell) const
939 return (right_column_of_cell(cell) == (columns_ - 1));
943 int LyXTabular::GetLastCellInRow(int row) const
947 return cell_info[row][columns_-1].cellno;
951 bool LyXTabular::calculate_width_of_column(int column)
953 int const old_column_width = column_info[column].width_of_column;
956 for (int i = 0; i < rows_; ++i) {
957 maximum = max(cell_info[i][column].width_of_cell, maximum);
959 column_info[column].width_of_column = maximum;
960 return (column_info[column].width_of_column != old_column_width);
965 // Calculate the columns regarding ONLY the normal cells and if this
966 // column is inside a multicolumn cell then use it only if its the last
967 // column of this multicolumn cell as this gives an added with to the
968 // column, all the rest should be adapted!
970 bool LyXTabular::calculate_width_of_column_NMC(int column)
972 int const old_column_width = column_info[column].width_of_column;
974 for (int i = 0; i < rows_; ++i) {
975 int cell = GetCellNumber(i, column);
976 bool ismulti = IsMultiColumn(cell, true);
977 if ((!ismulti || (column == right_column_of_cell(cell))) &&
978 (cell_info[i][column].width_of_cell > max))
980 max = cell_info[i][column].width_of_cell;
983 column_info[column].width_of_column = max;
984 return (column_info[column].width_of_column != old_column_width);
988 void LyXTabular::calculate_width_of_tabular()
990 width_of_tabular = 0;
991 for (int i = 0; i < columns_; ++i) {
992 width_of_tabular += column_info[i].width_of_column;
997 int LyXTabular::row_of_cell(int cell) const
999 if (cell >= numberofcells)
1003 return rowofcell[cell];
1007 int LyXTabular::column_of_cell(int cell) const
1009 if (cell >= numberofcells)
1010 return columns_ - 1;
1013 return columnofcell[cell];
1017 int LyXTabular::right_column_of_cell(int cell) const
1019 int const row = row_of_cell(cell);
1020 int column = column_of_cell(cell);
1021 while (column < (columns_ - 1) &&
1022 cell_info[row][column + 1].multicolumn == LyXTabular::CELL_PART_OF_MULTICOLUMN)
1028 void LyXTabular::Write(Buffer const * buf, ostream & os) const
1032 << write_attribute("version", 3)
1033 << write_attribute("rows", rows_)
1034 << write_attribute("columns", columns_)
1036 // global longtable options
1038 << write_attribute("rotate", rotate)
1039 << write_attribute("islongtable", is_long_tabular)
1040 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1041 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1042 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1043 << write_attribute("headTopDL", endhead.topDL)
1044 << write_attribute("headBottomDL", endhead.bottomDL)
1045 << write_attribute("footTopDL", endfoot.topDL)
1046 << write_attribute("footBottomDL", endfoot.bottomDL)
1047 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1048 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1049 << write_attribute("lastFootEmpty", endlastfoot.empty)
1051 for (int j = 0; j < columns_; ++j) {
1053 << write_attribute("alignment", column_info[j].alignment)
1054 << write_attribute("valignment", column_info[j].valignment)
1055 << write_attribute("leftline", column_info[j].left_line)
1056 << write_attribute("rightline", column_info[j].right_line)
1057 << write_attribute("width", column_info[j].p_width.asString())
1058 << write_attribute("special", column_info[j].align_special)
1061 for (int i = 0; i < rows_; ++i) {
1063 << write_attribute("topline", row_info[i].top_line)
1064 << write_attribute("bottomline", row_info[i].bottom_line)
1065 << write_attribute("endhead", row_info[i].endhead)
1066 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1067 << write_attribute("endfoot", row_info[i].endfoot)
1068 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1069 << write_attribute("newpage", row_info[i].newpage)
1071 for (int j = 0; j < columns_; ++j) {
1073 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1074 << write_attribute("alignment", cell_info[i][j].alignment)
1075 << write_attribute("valignment", cell_info[i][j].valignment)
1076 << write_attribute("topline", cell_info[i][j].top_line)
1077 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1078 << write_attribute("leftline", cell_info[i][j].left_line)
1079 << write_attribute("rightline", cell_info[i][j].right_line)
1080 << write_attribute("rotate", cell_info[i][j].rotate)
1081 << write_attribute("usebox", cell_info[i][j].usebox)
1082 << write_attribute("width", cell_info[i][j].p_width)
1083 << write_attribute("special", cell_info[i][j].align_special)
1085 os << "\\begin_inset ";
1086 cell_info[i][j].inset.write(buf, os);
1087 os << "\n\\end_inset \n"
1092 os << "</lyxtabular>\n";
1096 void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
1099 istream & is = lex.getStream();
1101 l_getline(is, line);
1102 if (!prefixIs(line, "<lyxtabular ")
1103 && !prefixIs(line, "<LyXTabular ")) {
1104 OldFormatRead(buf->params, lex, line);
1109 if (!getTokenValue(line, "version", version))
1112 ReadOld(buf, is, lex, line);
1113 else if (version >= 2)
1114 ReadNew(buf, is, lex, line, version);
1117 void LyXTabular::setHeaderFooterRows(int hr, int fhr, int fr, int lfr)
1121 row_info[--hr].endhead = true;
1123 // set firstheader info
1124 if (fhr && (fhr < rows_)) {
1125 if (row_info[fhr].endhead) {
1127 row_info[--fhr].endfirsthead = true;
1128 row_info[fhr].endhead = false;
1130 } else if (row_info[fhr - 1].endhead) {
1131 endfirsthead.empty = true;
1133 while((fhr > 0) && !row_info[--fhr].endhead) {
1134 row_info[fhr].endfirsthead = true;
1139 if (fr && (fr < rows_)) {
1140 if (row_info[fr].endhead && row_info[fr-1].endhead) {
1141 while((fr > 0) && !row_info[--fr].endhead) {
1142 row_info[fr].endfoot = true;
1143 row_info[fr].endhead = false;
1145 } else if (row_info[fr].endfirsthead && row_info[fr-1].endfirsthead) {
1146 while((fr > 0) && !row_info[--fr].endfirsthead) {
1147 row_info[fr].endfoot = true;
1148 row_info[fr].endfirsthead = false;
1150 } else if (!row_info[fr - 1].endhead && !row_info[fr - 1].endfirsthead) {
1151 while((fr > 0) && !row_info[--fr].endhead &&
1152 !row_info[fr].endfirsthead)
1154 row_info[fr].endfoot = true;
1158 // set lastfooter info
1159 if (lfr && (lfr < rows_)) {
1160 if (row_info[lfr].endhead && row_info[lfr - 1].endhead) {
1161 while((lfr > 0) && !row_info[--lfr].endhead) {
1162 row_info[lfr].endlastfoot = true;
1163 row_info[lfr].endhead = false;
1165 } else if (row_info[lfr].endfirsthead &&
1166 row_info[lfr - 1].endfirsthead)
1168 while((lfr > 0) && !row_info[--lfr].endfirsthead) {
1169 row_info[lfr].endlastfoot = true;
1170 row_info[lfr].endfirsthead = false;
1172 } else if (row_info[lfr].endfoot
1173 && row_info[lfr - 1].endfoot) {
1174 while((lfr > 0) && !row_info[--lfr].endfoot) {
1175 row_info[lfr].endlastfoot = true;
1176 row_info[lfr].endfoot = false;
1178 } else if (!row_info[fr - 1].endhead
1179 && !row_info[fr - 1].endfirsthead &&
1180 !row_info[fr - 1].endfoot)
1183 !row_info[--lfr].endhead && !row_info[lfr].endfirsthead &&
1184 !row_info[lfr].endfoot)
1186 row_info[lfr].endlastfoot = true;
1188 } else if (haveLTFoot()) {
1189 endlastfoot.empty = true;
1194 void LyXTabular::ReadNew(Buffer const * buf, istream & is,
1195 LyXLex & lex, string const & l, int const version)
1199 if (!getTokenValue(line, "rows", rows_arg))
1202 if (!getTokenValue(line, "columns", columns_arg))
1204 Init(buf->params, rows_arg, columns_arg);
1205 l_getline(is, line);
1206 if (!prefixIs(line, "<features")) {
1207 lyxerr << "Wrong tabular format (expected <features ...> got" <<
1208 line << ")" << endl;
1211 getTokenValue(line, "rotate", rotate);
1212 getTokenValue(line, "islongtable", is_long_tabular);
1213 // compatibility read for old longtable options. Now we can make any
1214 // row part of the header/footer type we want before it was strict
1215 // sequential from the first row down (as LaTeX does it!). So now when
1216 // we find a header/footer line we have to go up the rows and set it
1217 // on all preceding rows till the first or one with already a h/f option
1218 // set. If we find a firstheader on the same line as a header or a
1219 // lastfooter on the same line as a footer then this should be set empty.
1227 getTokenValue(line, "endhead", hrow);
1228 getTokenValue(line, "endfirsthead", fhrow);
1229 getTokenValue(line, "endfoot", frow);
1230 getTokenValue(line, "endlastfoot", lfrow);
1231 setHeaderFooterRows(abs(hrow), abs(fhrow), abs(frow), abs(lfrow));
1233 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1234 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1235 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1236 getTokenValue(line, "headTopDL", endhead.topDL);
1237 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1238 getTokenValue(line, "footTopDL", endfoot.topDL);
1239 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1240 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1241 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1242 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1244 for (int j = 0; j < columns_; ++j) {
1246 if (!prefixIs(line,"<column")) {
1247 lyxerr << "Wrong tabular format (expected <column ...> got" <<
1248 line << ")" << endl;
1251 getTokenValue(line, "alignment", column_info[j].alignment);
1252 getTokenValue(line, "valignment", column_info[j].valignment);
1253 getTokenValue(line, "leftline", column_info[j].left_line);
1254 getTokenValue(line, "rightline", column_info[j].right_line);
1255 getTokenValue(line, "width", column_info[j].p_width);
1256 getTokenValue(line, "special", column_info[j].align_special);
1259 for (int i = 0; i < rows_; ++i) {
1260 l_getline(is, line);
1261 if (!prefixIs(line, "<row")) {
1262 lyxerr << "Wrong tabular format (expected <row ...> got" <<
1263 line << ")" << endl;
1266 getTokenValue(line, "topline", row_info[i].top_line);
1267 getTokenValue(line, "bottomline", row_info[i].bottom_line);
1268 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1269 getTokenValue(line, "endhead", row_info[i].endhead);
1270 getTokenValue(line, "endfoot", row_info[i].endfoot);
1271 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1272 getTokenValue(line, "newpage", row_info[i].newpage);
1273 for (int j = 0; j < columns_; ++j) {
1274 l_getline(is, line);
1275 if (!prefixIs(line, "<cell")) {
1276 lyxerr << "Wrong tabular format (expected <cell ...> got" <<
1277 line << ")" << endl;
1280 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1281 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1282 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1283 getTokenValue(line, "topline", cell_info[i][j].top_line);
1284 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1285 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1286 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1287 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1288 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1289 getTokenValue(line, "width", cell_info[i][j].p_width);
1290 getTokenValue(line, "special", cell_info[i][j].align_special);
1291 l_getline(is, line);
1292 if (prefixIs(line, "\\begin_inset")) {
1293 cell_info[i][j].inset.read(buf, lex);
1294 l_getline(is, line);
1296 if (!prefixIs(line, "</cell>")) {
1297 lyxerr << "Wrong tabular format (expected </cell> got" <<
1298 line << ")" << endl;
1302 l_getline(is, line);
1303 if (!prefixIs(line, "</row>")) {
1304 lyxerr << "Wrong tabular format (expected </row> got" <<
1305 line << ")" << endl;
1309 while (!prefixIs(line, "</lyxtabular>")) {
1310 l_getline(is, line);
1312 set_row_column_number_info();
1316 void LyXTabular::OldFormatRead(BufferParams const & bp,
1317 LyXLex & lex, string const & fl)
1323 int columns_arg = 0;
1324 int is_long_tabular_arg = false;
1325 int rotate_arg = false;
1334 istream & is = lex.getStream();
1337 version = lyx::atoi(s.substr(8, string::npos));
1341 vector<int> cont_row_info;
1344 lyxerr << "Tabular format < 5 is not supported anymore\n"
1345 "Get an older version of LyX (< 1.1.x) for conversion!"
1347 Alert::alert(_("Warning:"),
1348 _("Tabular format < 5 is not supported anymore\n"),
1349 _("Get an older version of LyX (< 1.1.x) for conversion!"));
1351 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1352 >> rotate_arg >> a >> b >> c >> d;
1354 is >> rows_arg >> columns_arg;
1355 Init(bp, rows_arg, columns_arg);
1356 cont_row_info = vector<int>(rows_arg);
1357 SetLongTabular(is_long_tabular_arg);
1358 SetRotateTabular(rotate_arg);
1360 for (i = 0; i < rows_; ++i) {
1362 cont_row_info[i] = false;
1364 for (i = 0; i < columns_; ++i) {
1367 for (i = 0; i < rows_; ++i) {
1368 for (j = 0; j < columns_; ++j) {
1373 is >> rows_arg >> columns_arg >> is_long_tabular_arg
1374 >> rotate_arg >> a >> b >> c >> d;
1375 Init(bp, rows_arg, columns_arg);
1376 cont_row_info = vector<int>(rows_arg);
1377 SetLongTabular(is_long_tabular_arg);
1378 SetRotateTabular(rotate_arg);
1379 setHeaderFooterRows(a+1, b+1 , c+1, d+1);
1380 for (i = 0; i < rows_; ++i) {
1381 a = b = c = d = e = f = g = 0;
1382 is >> a >> b >> c >> d;
1383 row_info[i].top_line = a;
1384 row_info[i].bottom_line = b;
1385 cont_row_info[i] = c;
1386 row_info[i].newpage = d;
1388 for (i = 0; i < columns_; ++i) {
1393 char ch; // skip '"'
1396 // ignore is buggy but we will use it later (Lgb)
1397 is.ignore(); // skip '"'
1399 getline(is, s1, '"');
1401 is >> ch; // skip '"'
1403 // ignore is buggy but we will use it later (Lgb)
1404 is.ignore(); // skip '"'
1406 getline(is, s2, '"');
1407 column_info[i].alignment = static_cast<LyXAlignment>(a);
1408 column_info[i].left_line = b;
1409 column_info[i].right_line = c;
1410 column_info[i].p_width = LyXLength(s1);
1411 column_info[i].align_special = s2;
1413 for (i = 0; i < rows_; ++i) {
1414 for (j = 0; j < columns_; ++j) {
1417 is >> a >> b >> c >> d >> e >> f >> g;
1420 is >> ch; // skip '"'
1422 // ignore is buggy but we will use it later (Lgb)
1423 is.ignore(); // skip '"'
1425 getline(is, s1, '"');
1427 is >> ch; // skip '"'
1429 // ignore is buggy but we will use it later (Lgb)
1430 is.ignore(); // skip '"'
1432 getline(is, s2, '"');
1433 cell_info[i][j].multicolumn = static_cast<char>(a);
1434 cell_info[i][j].alignment = static_cast<LyXAlignment>(b);
1435 cell_info[i][j].top_line = static_cast<char>(c);
1436 cell_info[i][j].bottom_line = static_cast<char>(d);
1437 cell_info[i][j].left_line = column_info[j].left_line;
1438 cell_info[i][j].right_line = column_info[j].right_line;
1439 cell_info[i][j].rotate = static_cast<bool>(f);
1440 cell_info[i][j].usebox = static_cast<BoxType>(g);
1441 cell_info[i][j].align_special = s1;
1442 cell_info[i][j].p_width = LyXLength(s2);
1446 set_row_column_number_info(true);
1448 Paragraph * par = new Paragraph;
1449 Paragraph * return_par = 0;
1451 par->layout(textclasslist[bp.textclass].defaultLayoutName());
1454 Paragraph::depth_type depth = 0;
1455 LyXFont font(LyXFont::ALL_INHERIT);
1456 font.setLanguage(owner_->bufferOwner()->getLanguage());
1458 while (lex.isOK()) {
1460 string const token = lex.getString();
1463 if (token == "\\layout"
1464 || token == "\\end_float" // this should not exist anymore
1465 || token == "\\end_inset" // as it is substituted by this
1466 || token == "\\end_deeper")
1468 lex.pushToken(token);
1469 #ifndef NO_COMPABILITY
1470 // Here we need to insert the inset_ert_contents into the last
1471 // cell of the tabular.
1472 owner_->bufferOwner()->insertErtContents(par, pos);
1476 #ifndef NO_COMPABILITY
1477 if (token == "\\newline")
1478 // Here we need to insert the inset_ert_contents into the last
1479 // cell of the tabular.
1480 owner_->bufferOwner()->insertErtContents(par, pos, false);
1482 if (owner_->bufferOwner()->parseSingleLyXformat2Token(lex, par,
1487 lex.pushToken(token);
1491 lex.printError("New Paragraph allocated! This should not happen!");
1492 lex.pushToken(token);
1498 // now we have the par we should fill the insets with this!
1500 InsetText * inset = GetCellInset(cell);
1503 for (int i = 0; i < par->size(); ++i) {
1504 if (par->isNewline(i)) {
1506 if (cell > numberofcells) {
1507 lyxerr << "Some error in reading old table format occured!" <<
1508 endl << "Terminating when reading cell[" << cell << "]!" <<
1513 row = row_of_cell(cell);
1514 if (cont_row_info[row]) {
1516 cont_row_info.erase(cont_row_info.begin() + row); //&cont_row_info[row]);
1517 while (!IsFirstCellInRow(--cell));
1519 inset = GetCellInset(cell);
1522 inset = GetCellInset(cell);
1523 row = row_of_cell(cell);
1524 if (!cell_info[row_of_cell(cell)][column_of_cell(cell)].usebox)
1526 // insert a space instead
1528 par->insertChar(i, ' ');
1531 par->copyIntoMinibuffer(*owner_->bufferOwner(), i);
1532 inset->paragraph()->insertFromMinibuffer(inset->paragraph()->size());
1539 bool LyXTabular::IsMultiColumn(int cell, bool real) const
1541 return ((!real || (column_of_cell(cell) != right_column_of_cell(cell))) &&
1542 (cellinfo_of_cell(cell)->multicolumn != LyXTabular::CELL_NORMAL));
1546 LyXTabular::cellstruct * LyXTabular::cellinfo_of_cell(int cell) const
1548 int const row = row_of_cell(cell);
1549 int const column = column_of_cell(cell);
1550 return &cell_info[row][column];
1554 void LyXTabular::SetMultiColumn(Buffer const * buffer, int cell, int number)
1556 cellinfo_of_cell(cell)->multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1557 cellinfo_of_cell(cell)->alignment = column_info[column_of_cell(cell)].alignment;
1558 cellinfo_of_cell(cell)->top_line = row_info[row_of_cell(cell)].top_line;
1559 cellinfo_of_cell(cell)->bottom_line = row_info[row_of_cell(cell)].bottom_line;
1560 cellinfo_of_cell(cell)->right_line = column_info[column_of_cell(cell+number-1)].right_line;
1562 for (int i = 1; i < number; ++i) {
1563 cellinfo_of_cell(cell+i)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1564 cellinfo_of_cell(cell)->inset.appendParagraphs(buffer->params,
1565 cellinfo_of_cell(cell+i)->inset.paragraph());
1566 cellinfo_of_cell(cell+i)->inset.clear();
1569 for (number--; number > 0; --number) {
1570 cellinfo_of_cell(cell+number)->multicolumn = CELL_PART_OF_MULTICOLUMN;
1573 set_row_column_number_info();
1577 int LyXTabular::cells_in_multicolumn(int cell) const
1579 int const row = row_of_cell(cell);
1580 int column = column_of_cell(cell);
1583 while ((column < columns_) &&
1584 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1593 int LyXTabular::UnsetMultiColumn(int cell)
1595 int const row = row_of_cell(cell);
1596 int column = column_of_cell(cell);
1600 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1601 cell_info[row][column].multicolumn = CELL_NORMAL;
1603 while ((column < columns_) &&
1604 (cell_info[row][column].multicolumn ==CELL_PART_OF_MULTICOLUMN))
1606 cell_info[row][column].multicolumn = CELL_NORMAL;
1611 set_row_column_number_info();
1616 void LyXTabular::SetLongTabular(bool what)
1618 is_long_tabular = what;
1622 bool LyXTabular::IsLongTabular() const
1624 return is_long_tabular;
1628 void LyXTabular::SetRotateTabular(bool flag)
1634 bool LyXTabular::GetRotateTabular() const
1640 void LyXTabular::SetRotateCell(int cell, bool flag)
1642 cellinfo_of_cell(cell)->rotate = flag;
1646 bool LyXTabular::GetRotateCell(int cell) const
1648 return cellinfo_of_cell(cell)->rotate;
1652 bool LyXTabular::NeedRotating() const
1656 for (int i = 0; i < rows_; ++i) {
1657 for (int j = 0; j < columns_; ++j) {
1658 if (cell_info[i][j].rotate)
1666 bool LyXTabular::IsLastCell(int cell) const
1668 if ((cell + 1) < numberofcells)
1674 int LyXTabular::GetCellAbove(int cell) const
1676 if (row_of_cell(cell) > 0)
1677 return cell_info[row_of_cell(cell)-1][column_of_cell(cell)].cellno;
1682 int LyXTabular::GetCellBelow(int cell) const
1684 if (row_of_cell(cell) + 1 < rows_)
1685 return cell_info[row_of_cell(cell)+1][column_of_cell(cell)].cellno;
1690 int LyXTabular::GetLastCellAbove(int cell) const
1692 if (row_of_cell(cell) <= 0)
1694 if (!IsMultiColumn(cell))
1695 return GetCellAbove(cell);
1696 return cell_info[row_of_cell(cell) - 1][right_column_of_cell(cell)].cellno;
1700 int LyXTabular::GetLastCellBelow(int cell) const
1702 if (row_of_cell(cell) + 1 >= rows_)
1704 if (!IsMultiColumn(cell))
1705 return GetCellBelow(cell);
1706 return cell_info[row_of_cell(cell) + 1][right_column_of_cell(cell)].cellno;
1710 int LyXTabular::GetCellNumber(int row, int column) const
1713 if (column >= columns_)
1714 column = columns_ - 1;
1715 else if (column < 0)
1722 lyx::Assert(column >= 0 || column < columns_ || row >= 0 || row < rows_);
1724 return cell_info[row][column].cellno;
1728 void LyXTabular::SetUsebox(int cell, BoxType type)
1730 cellinfo_of_cell(cell)->usebox = type;
1734 LyXTabular::BoxType LyXTabular::GetUsebox(int cell) const
1736 if (column_info[column_of_cell(cell)].p_width.zero() &&
1737 !(IsMultiColumn(cell) && !cellinfo_of_cell(cell)->p_width.zero()))
1739 if (cellinfo_of_cell(cell)->usebox > 1)
1740 return cellinfo_of_cell(cell)->usebox;
1741 return UseParbox(cell);
1746 // This are functions used for the longtable support
1748 void LyXTabular::SetLTHead(int row, bool flag, ltType const & hd, bool first)
1753 row_info[row].endfirsthead = flag;
1757 row_info[row].endhead = flag;
1762 bool LyXTabular::GetRowOfLTHead(int row, ltType & hd) const
1765 hd.set = haveLTHead();
1766 return row_info[row].endhead;
1770 bool LyXTabular::GetRowOfLTFirstHead(int row, ltType & hd) const
1773 hd.set = haveLTFirstHead();
1774 return row_info[row].endfirsthead;
1778 void LyXTabular::SetLTFoot(int row, bool flag, ltType const & fd, bool last)
1783 row_info[row].endlastfoot = flag;
1787 row_info[row].endfoot = flag;
1792 bool LyXTabular::GetRowOfLTFoot(int row, ltType & fd) const
1795 fd.set = haveLTFoot();
1796 return row_info[row].endfoot;
1800 bool LyXTabular::GetRowOfLTLastFoot(int row, ltType & fd) const
1803 fd.set = haveLTLastFoot();
1804 return row_info[row].endlastfoot;
1808 void LyXTabular::SetLTNewPage(int row, bool what)
1810 row_info[row].newpage = what;
1814 bool LyXTabular::GetLTNewPage(int row) const
1816 return row_info[row].newpage;
1820 bool LyXTabular::haveLTHead() const
1822 for(int i=0; i < rows_; ++i) {
1823 if (row_info[i].endhead)
1830 bool LyXTabular::haveLTFirstHead() const
1832 if (endfirsthead.empty)
1834 for(int i=0; i < rows_; ++i) {
1835 if (row_info[i].endfirsthead)
1842 bool LyXTabular::haveLTFoot() const
1844 for(int i=0; i < rows_; ++i) {
1845 if (row_info[i].endfoot)
1852 bool LyXTabular::haveLTLastFoot() const
1854 if (endlastfoot.empty)
1856 for(int i=0; i < rows_; ++i) {
1857 if (row_info[i].endlastfoot)
1864 // end longtable support functions
1866 bool LyXTabular::SetAscentOfRow(int row, int height)
1868 if ((row >= rows_) || (row_info[row].ascent_of_row == height))
1870 row_info[row].ascent_of_row = height;
1875 bool LyXTabular::SetDescentOfRow(int row, int height)
1877 if ((row >= rows_) || (row_info[row].descent_of_row == height))
1879 row_info[row].descent_of_row = height;
1884 int LyXTabular::GetAscentOfRow(int row) const
1888 return row_info[row].ascent_of_row;
1892 int LyXTabular::GetDescentOfRow(int row) const
1896 return row_info[row].descent_of_row;
1900 int LyXTabular::GetHeightOfTabular() const
1904 for (int row = 0; row < rows_; ++row)
1905 height += GetAscentOfRow(row) + GetDescentOfRow(row) +
1906 GetAdditionalHeight(row);
1911 bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
1913 if ((row >= rows_) || (column >= columns_))
1915 return (cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN);
1919 int LyXTabular::TeXTopHLine(ostream & os, int row) const
1921 if ((row < 0) || (row >= rows_))
1924 int const fcell = GetFirstCellInRow(row);
1925 int const n = NumberOfCellsInRow(fcell) + fcell;
1928 for (int i = fcell; i < n; ++i) {
1932 if (tmp == (n - fcell)) {
1935 for (int i = fcell; i < n; ++i) {
1938 << column_of_cell(i) + 1
1940 << right_column_of_cell(i) + 1
1952 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
1954 if ((row < 0) || (row >= rows_))
1957 int const fcell = GetFirstCellInRow(row);
1958 int const n = NumberOfCellsInRow(fcell) + fcell;
1961 for (int i = fcell; i < n; ++i) {
1965 if (tmp == (n - fcell)) {
1968 for (int i = fcell; i < n; ++i) {
1969 if (BottomLine(i)) {
1971 << column_of_cell(i) + 1
1973 << right_column_of_cell(i) + 1
1985 int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
1989 if (GetRotateCell(cell)) {
1990 os << "\\begin{sideways}\n";
1993 if (IsMultiColumn(cell)) {
1994 os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
1995 if (!cellinfo_of_cell(cell)->align_special.empty()) {
1996 os << cellinfo_of_cell(cell)->align_special << "}{";
1998 if (LeftLine(cell) &&
1999 (IsFirstCellInRow(cell) ||
2000 (!IsMultiColumn(cell-1) && !LeftLine(cell, true) &&
2001 !RightLine(cell-1, true))))
2005 if (!GetPWidth(cell).zero()) {
2006 switch (GetVAlignment(cell)) {
2007 case LYX_VALIGN_TOP:
2010 case LYX_VALIGN_CENTER:
2013 case LYX_VALIGN_BOTTOM:
2017 os << "{" << GetPWidth(cell).asLatexString() << '}';
2019 switch (GetAlignment(cell)) {
2020 case LYX_ALIGN_LEFT:
2023 case LYX_ALIGN_RIGHT:
2031 if (RightLine(cell))
2033 if (((cell + 1) < numberofcells) && !IsFirstCellInRow(cell+1) &&
2039 if (GetUsebox(cell) == BOX_PARBOX) {
2041 switch (GetVAlignment(cell)) {
2042 case LYX_VALIGN_TOP:
2045 case LYX_VALIGN_CENTER:
2048 case LYX_VALIGN_BOTTOM:
2052 os << "]{" << GetPWidth(cell).asLatexString() << "}{";
2053 } else if (GetUsebox(cell) == BOX_MINIPAGE) {
2054 os << "\\begin{minipage}[";
2055 switch (GetVAlignment(cell)) {
2056 case LYX_VALIGN_TOP:
2059 case LYX_VALIGN_CENTER:
2062 case LYX_VALIGN_BOTTOM:
2066 os << "]{" << GetPWidth(cell).asLatexString() << "}\n";
2073 int LyXTabular::TeXCellPostamble(ostream & os, int cell) const
2078 if (GetUsebox(cell) == BOX_PARBOX)
2080 else if (GetUsebox(cell) == BOX_MINIPAGE) {
2081 os << "%\n\\end{minipage}";
2084 if (IsMultiColumn(cell)) {
2087 if (GetRotateCell(cell)) {
2088 os << "%\n\\end{sideways}";
2095 int LyXTabular::TeXLongtableHeaderFooter(ostream & os, Buffer const * buf,
2096 bool fragile, bool fp) const
2098 if (!is_long_tabular)
2102 // output header info
2104 if (endhead.topDL) {
2108 for (int i = 0; i < rows_; ++i) {
2109 if (row_info[i].endhead) {
2110 ret += TeXRow(os, i, buf, fragile, fp);
2113 if (endhead.bottomDL) {
2117 os << "\\endhead\n";
2119 if (endfirsthead.empty) {
2120 os << "\\endfirsthead\n";
2124 // output firstheader info
2125 if (haveLTFirstHead()) {
2126 if (endfirsthead.topDL) {
2130 for (int i = 0; i < rows_; ++i) {
2131 if (row_info[i].endfirsthead) {
2132 ret += TeXRow(os, i, buf, fragile, fp);
2135 if (endfirsthead.bottomDL) {
2139 os << "\\endfirsthead\n";
2142 // output footer info
2144 if (endfoot.topDL) {
2148 for (int i = 0; i < rows_; ++i) {
2149 if (row_info[i].endfoot) {
2150 ret += TeXRow(os, i, buf, fragile, fp);
2153 if (endfoot.bottomDL) {
2157 os << "\\endfoot\n";
2159 if (endlastfoot.empty) {
2160 os << "\\endlastfoot\n";
2164 // output lastfooter info
2165 if (haveLTLastFoot()) {
2166 if (endlastfoot.topDL) {
2170 for (int i = 0; i < rows_; ++i) {
2171 if (row_info[i].endlastfoot) {
2172 ret += TeXRow(os, i, buf, fragile, fp);
2175 if (endlastfoot.bottomDL) {
2179 os << "\\endlastfoot\n";
2186 bool LyXTabular::isValidRow(int const row) const
2188 if (!is_long_tabular)
2190 return (!row_info[row].endhead && !row_info[row].endfirsthead &&
2191 !row_info[row].endfoot && !row_info[row].endlastfoot);
2195 int LyXTabular::TeXRow(ostream & os, int const i, Buffer const * buf,
2196 bool fragile, bool fp) const
2199 int cell = GetCellNumber(i, 0);
2201 ret += TeXTopHLine(os, i);
2202 for (int j = 0; j < columns_; ++j) {
2203 if (IsPartOfMultiColumn(i,j))
2205 ret += TeXCellPreamble(os, cell);
2206 InsetText * inset = GetCellInset(cell);
2208 bool rtl = inset->paragraph()->isRightToLeftPar(buf->params) &&
2209 inset->paragraph()->size() > 0 && GetPWidth(cell).zero();
2213 ret += inset->latex(buf, os, fragile, fp);
2217 ret += TeXCellPostamble(os, cell);
2218 if (!IsLastCellInRow(cell)) { // not last cell in row
2226 ret += TeXBottomHLine(os, i);
2231 int LyXTabular::latex(Buffer const * buf,
2232 ostream & os, bool fragile, bool fp) const
2236 //+---------------------------------------------------------------------
2237 //+ first the opening preamble +
2238 //+---------------------------------------------------------------------
2241 os << "\\begin{sideways}\n";
2244 if (is_long_tabular)
2245 os << "\\begin{longtable}{";
2247 os << "\\begin{tabular}{";
2248 for (int i = 0; i < columns_; ++i) {
2249 if (!column_info[i].align_special.empty()) {
2250 os << column_info[i].align_special;
2252 if (column_info[i].left_line)
2254 if (!column_info[i].p_width.zero()) {
2255 switch (column_info[i].valignment) {
2256 case LYX_VALIGN_TOP:
2259 case LYX_VALIGN_CENTER:
2262 case LYX_VALIGN_BOTTOM:
2267 << column_info[i].p_width.asLatexString()
2270 switch (column_info[i].alignment) {
2271 case LYX_ALIGN_LEFT:
2274 case LYX_ALIGN_RIGHT:
2282 if (column_info[i].right_line)
2289 ret += TeXLongtableHeaderFooter(os, buf, fragile, fp);
2291 //+---------------------------------------------------------------------
2292 //+ the single row and columns (cells) +
2293 //+---------------------------------------------------------------------
2295 for (int i = 0; i < rows_; ++i) {
2296 if (isValidRow(i)) {
2297 ret += TeXRow(os, i, buf, fragile, fp);
2298 if (is_long_tabular && row_info[i].newpage) {
2299 os << "\\newpage\n";
2305 //+---------------------------------------------------------------------
2306 //+ the closing of the tabular +
2307 //+---------------------------------------------------------------------
2309 if (is_long_tabular)
2310 os << "\\end{longtable}";
2312 os << "\\end{tabular}";
2314 os << "\n\\end{sideways}";
2322 int LyXTabular::docbookRow(Buffer const * buf, ostream & os, int row) const
2325 int cell = GetFirstCellInRow(row);
2328 for (int j = 0; j < columns_; ++j) {
2329 if (IsPartOfMultiColumn(row, j))
2332 os << "<entry align=\"";
2333 switch (GetAlignment(cell)) {
2334 case LYX_ALIGN_LEFT:
2337 case LYX_ALIGN_RIGHT:
2345 os << "\" valign=\"";
2346 switch (GetVAlignment(cell)) {
2347 case LYX_VALIGN_TOP:
2350 case LYX_VALIGN_BOTTOM:
2353 case LYX_VALIGN_CENTER:
2358 if (IsMultiColumn(cell)) {
2359 os << " namest=\"col" << j << "\" ";
2360 os << "nameend=\"col" << j + cells_in_multicolumn(cell) - 1<< "\"";
2364 ret += GetCellInset(cell)->docbook(buf, os);
2373 int LyXTabular::docBook(Buffer const * buf, ostream & os) const
2377 //+---------------------------------------------------------------------
2378 //+ first the opening preamble +
2379 //+---------------------------------------------------------------------
2381 os << "<tgroup cols=\"" << columns_
2382 << "\" colsep=\"1\" rowsep=\"1\">\n";
2384 for (int i = 0; i < columns_; ++i) {
2385 os << "<colspec colname=\"col" << i << "\" align=\"";
2386 switch (column_info[i].alignment) {
2387 case LYX_ALIGN_LEFT:
2390 case LYX_ALIGN_RIGHT:
2401 //+---------------------------------------------------------------------
2402 //+ Long Tabular case +
2403 //+---------------------------------------------------------------------
2405 // output header info
2406 if (haveLTHead() || haveLTFirstHead()) {
2409 for (int i = 0; i < rows_; ++i) {
2410 if (row_info[i].endhead || row_info[i].endfirsthead) {
2411 ret += docbookRow(buf, os, i);
2417 // output footer info
2418 if (haveLTFoot() || haveLTLastFoot()) {
2421 for (int i = 0; i < rows_; ++i) {
2422 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2423 ret += docbookRow(buf, os, i);
2430 //+---------------------------------------------------------------------
2431 //+ the single row and columns (cells) +
2432 //+---------------------------------------------------------------------
2436 for (int i = 0; i < rows_; ++i) {
2437 if (isValidRow(i)) {
2438 ret += docbookRow(buf, os, i);
2443 //+---------------------------------------------------------------------
2444 //+ the closing of the tabular +
2445 //+---------------------------------------------------------------------
2454 // ASCII export function and helpers
2456 int LyXTabular::asciiTopHLine(ostream & os, int row,
2457 vector<unsigned int> const & clen) const
2459 int const fcell = GetFirstCellInRow(row);
2460 int const n = NumberOfCellsInRow(fcell) + fcell;
2463 for (int i = fcell; i < n; ++i) {
2473 for (int i = fcell; i < n; ++i) {
2484 int column = column_of_cell(i);
2485 int len = clen[column];
2486 while (IsPartOfMultiColumn(row, ++column))
2487 len += clen[column] + 4;
2488 os << string(len, ch);
2503 int LyXTabular::asciiBottomHLine(ostream & os, int row,
2504 vector<unsigned int> const & clen) const
2506 int const fcell = GetFirstCellInRow(row);
2507 int const n = NumberOfCellsInRow(fcell) + fcell;
2510 for (int i = fcell; i < n; ++i) {
2511 if (BottomLine(i)) {
2520 for (int i = fcell; i < n; ++i) {
2521 if (BottomLine(i)) {
2531 int column = column_of_cell(i);
2532 int len = clen[column];
2533 while (IsPartOfMultiColumn(row, ++column))
2534 len += clen[column] + 4;
2535 os << string(len, ch);
2536 if (BottomLine(i)) {
2550 int LyXTabular::asciiPrintCell(Buffer const * buf, ostream & os,
2551 int cell, int row, int column,
2552 vector<unsigned int> const & clen,
2553 bool onlydata) const
2556 int ret = GetCellInset(cell)->ascii(buf, sstr, 0);
2568 unsigned int len1 = sstr.str().length();
2569 unsigned int len2 = clen[column];
2570 while (IsPartOfMultiColumn(row, ++column))
2571 len2 += clen[column] + 4;
2574 switch (GetAlignment(cell)) {
2576 case LYX_ALIGN_LEFT:
2579 case LYX_ALIGN_RIGHT:
2583 case LYX_ALIGN_CENTER:
2589 for (unsigned int i = 0; i < len1; ++i)
2592 for (unsigned int i = 0; i < len2; ++i)
2594 if (RightLine(cell))
2603 int LyXTabular::ascii(Buffer const * buf, ostream & os, int const depth,
2604 bool onlydata, unsigned char delim) const
2608 //+---------------------------------------------------------------------
2609 //+ first calculate the width of the single columns +
2610 //+---------------------------------------------------------------------
2611 vector<unsigned int> clen(columns_);
2614 // first all non (real) multicolumn cells!
2615 for (int j = 0; j < columns_; ++j) {
2617 for (int i = 0; i < rows_; ++i) {
2618 int cell = GetCellNumber(i, j);
2619 if (IsMultiColumn(cell, true))
2622 GetCellInset(cell)->ascii(buf, sstr, 0);
2623 if (clen[j] < sstr.str().length())
2624 clen[j] = sstr.str().length();
2627 // then all (real) multicolumn cells!
2628 for (int j = 0; j < columns_; ++j) {
2629 for (int i = 0; i < rows_; ++i) {
2630 int cell = GetCellNumber(i, j);
2631 if (!IsMultiColumn(cell, true) || IsPartOfMultiColumn(i, j))
2634 GetCellInset(cell)->ascii(buf, sstr, 0);
2635 int len = int(sstr.str().length());
2636 int const n = cells_in_multicolumn(cell);
2637 for (int k = j; (len > 0) && (k < (j + n - 1)); ++k)
2639 if (len > int(clen[j + n - 1]))
2640 clen[j + n - 1] = len;
2645 for (int i = 0; i < rows_; ++i) {
2647 if (asciiTopHLine(os, i, clen)) {
2648 for (int j = 0; j < depth; ++j)
2652 for (int j = 0; j < columns_; ++j) {
2653 if (IsPartOfMultiColumn(i,j))
2655 if (onlydata && j > 0)
2657 ret += asciiPrintCell(buf, os, cell, i, j, clen, onlydata);
2662 for (int j = 0; j < depth; ++j)
2664 if (asciiBottomHLine(os, i, clen)) {
2665 for (int j = 0; j < depth; ++j)
2677 InsetText * LyXTabular::GetCellInset(int cell) const
2680 return & cell_info[row_of_cell(cell)][column_of_cell(cell)].inset;
2684 InsetText * LyXTabular::GetCellInset(int row, int column) const
2686 cur_cell = GetCellNumber(row, column);
2687 return & cell_info[row][column].inset;
2691 int LyXTabular::GetCellFromInset(Inset const * inset, int maybe_cell) const
2693 // is this inset part of the tabular?
2694 if (!inset || inset->owner() != owner_) {
2695 lyxerr[Debug::INSETTEXT]
2696 << "this is not a cell of the tabular!" << endl;
2700 const int save_cur_cell = cur_cell;
2701 int cell = cur_cell;
2702 if (GetCellInset(cell) != inset) {
2704 if (cell == -1 || GetCellInset(cell) != inset) {
2710 for (cell = GetNumberOfCells(); cell >= 0; --cell) {
2711 if (GetCellInset(cell) == inset)
2714 lyxerr[Debug::INSETTEXT]
2715 << "LyXTabular::GetCellFromInset: "
2717 << ", cur_cell=" << save_cur_cell
2718 << ", maybe_cell=" << maybe_cell
2720 // We should have found a cell at this point
2722 lyxerr << "LyXTabular::GetCellFromInset: "
2723 << "Cell not found!" << endl;
2731 void LyXTabular::Validate(LaTeXFeatures & features) const
2733 if (IsLongTabular())
2734 features.require("longtable");
2736 features.require("rotating");
2737 for (int cell = 0; cell < numberofcells; ++cell) {
2738 if (GetVAlignment(cell) != LYX_VALIGN_TOP)
2739 features.require("array");
2740 GetCellInset(cell)->validate(features);
2745 vector<string> const LyXTabular::getLabelList() const
2747 vector<string> label_list;
2748 for (int i = 0; i < rows_; ++i)
2749 for (int j = 0; j < columns_; ++j) {
2750 vector<string> const l =
2751 GetCellInset(i, j)->getLabelList();
2752 label_list.insert(label_list.end(),
2753 l.begin(), l.end());
2759 LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
2761 Paragraph * par = GetCellInset(cell)->paragraph();
2763 for (; par; par = par->next()) {
2764 for (int i = 0; i < par->size(); ++i) {
2765 if (par->getChar(i) == Paragraph::META_NEWLINE)